@Test public void testExtractNestedTimestampMillis() throws Exception { Map<String, String> classPerTopic = new HashMap<String, String>(); classPerTopic.put("*", TimestampedMessages.UnitTestTimestamp2.class.getName()); Mockito.when(mConfig.getMessageTimestampName()).thenReturn("internal.timestamp"); Mockito.when(mConfig.getProtobufMessageClassPerTopic()).thenReturn(classPerTopic); ProtobufMessageParser parser = new ProtobufMessageParser(mConfig); Timestamp timestamp = Timestamps.fromMillis(1405970352000L); TimestampedMessages.UnitTestTimestamp2 message = TimestampedMessages.UnitTestTimestamp2.newBuilder() .setInternal(TimestampedMessages.UnitTestTimestamp2.Internal.newBuilder().setTimestamp(timestamp).build()).build(); assertEquals(1405970352000l, parser.extractTimestampMillis(new Message("test", 0, 0, null, message.toByteArray(), timestamp.getSeconds()))); timestamp = Timestamps.fromMillis(1405970352123l); message = TimestampedMessages.UnitTestTimestamp2.newBuilder() .setInternal(TimestampedMessages.UnitTestTimestamp2.Internal.newBuilder().setTimestamp(timestamp).build()).build(); assertEquals(1405970352123l, parser.extractTimestampMillis(new Message("test", 0, 0, null, message.toByteArray(), timestamp.getSeconds()))); } }
public long extractTimestampMillis(String topic, final byte[] bytes) throws IOException { if (timestampFieldPath != null) { com.google.protobuf.Message decodedMessage = protobufUtil.decodeProtobufOrJsonMessage(topic, bytes); int i = 0; for (; i < timestampFieldPath.length - 1; ++i) { decodedMessage = (com.google.protobuf.Message) decodedMessage .getField(decodedMessage.getDescriptorForType().findFieldByName(timestampFieldPath[i])); } Object timestampObject = decodedMessage .getField(decodedMessage.getDescriptorForType().findFieldByName(timestampFieldPath[i])); if (timestampObject instanceof com.google.protobuf.Timestamp){ return Timestamps.toMillis((com.google.protobuf.Timestamp) timestampObject); }else { return toMillis((Long) timestampObject); } } else { // Assume that the timestamp field is the first field, is required, // and is a uint64. CodedInputStream input = CodedInputStream.newInstance(bytes); // Don't really care about the tag, but need to read it to get, to // the payload. input.readTag(); return toMillis(input.readUInt64()); } } }
.setRequestId(requestID) .setSourceName(source) .setTimestamp(Timestamps.fromMillis(System.currentTimeMillis())) .putClientMetadata(CLIENT_HOST_NAME_METADATA_KEY, agentMetadata.getAgentHostName()) .build(); "Sending Ping with id: {}, timestamp: {}", requestID, Timestamps.toString(request.getTimestamp()) );
@Test public void writeClosesOldSingleUseContext() throws ParseException { ReadContext ctx = session.singleUse(TimestampBound.strong()); Mockito.when(rpc.commit(Mockito.<CommitRequest>any(), Mockito.eq(options))) .thenReturn( CommitResponse.newBuilder() .setCommitTimestamp(Timestamps.parse("2015-10-01T10:54:20.021Z")) .build()); session.writeAtLeastOnce(Arrays.<Mutation>asList()); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("invalidated"); ctx.read("Dummy", KeySet.all(), Arrays.asList("C")); }
@Override public void serialize( Timestamp timestamp, JsonGenerator generator, SerializerProvider serializerProvider ) throws IOException { generator.writeString(Timestamps.toString(timestamp)); } }
private void printStats(Map<ClientType, Controller.LoadtestStats> results) { long startMillis = Timestamps.toMillis(Timestamps.add(Client.startTime, Client.burnInDuration)); if (System.currentTimeMillis() < startMillis) { log.info( "Still under burn in time, will start in " + ((startMillis - System.currentTimeMillis()) / 1000) + " seconds."); return; } log.info("==============================================="); results.forEach( (type, stats) -> { log.info("Results for " + type + ":"); log.info("50%: " + LatencyDistribution.getNthPercentile(stats.bucketValues, 50.0)); log.info("99%: " + LatencyDistribution.getNthPercentile(stats.bucketValues, 99.0)); log.info("99.9%: " + LatencyDistribution.getNthPercentile(stats.bucketValues, 99.9)); log.info( "Average throughput: " + new DecimalFormat("#.##").format(stats.getQPS() * messageSize / 1000000.0) + " MB/s"); }); log.info("==============================================="); }
/** * Create a Timestamp from the number of nanoseconds elapsed from the epoch. * * @deprecated Use {@link Timestamps#fromNanos} instead. */ @Deprecated public static Timestamp createTimestampFromNanos(long nanoseconds) { return Timestamps.fromNanos(nanoseconds); }
/** * Add a duration to a timestamp. * * @deprecated Use {@link Timestamps#add} instead. */ @Deprecated public static Timestamp add(Timestamp start, Duration length) { return Timestamps.add(start, length); }
@Test public void testBatchReadOnlyTxnWithBound() throws Exception { Session sessionProto = Session.newBuilder().setName(SESSION_NAME).build(); when(gapicRpc.createSession( eq(DB_NAME), (Map<String, String>) anyMap(), optionsCaptor.capture())) .thenReturn(sessionProto); com.google.protobuf.Timestamp timestamp = Timestamps.parse(TIMESTAMP); Transaction txnMetadata = Transaction.newBuilder().setId(TXN_ID).setReadTimestamp(timestamp).build(); when(spannerOptions.getSpannerRpcV1()).thenReturn(gapicRpc); when(gapicRpc.beginTransaction(Mockito.<BeginTransactionRequest>any(), optionsCaptor.capture())) .thenReturn(txnMetadata); BatchReadOnlyTransaction batchTxn = client.batchReadOnlyTransaction(TimestampBound.strong()); assertThat(batchTxn.getBatchTransactionId().getSessionId()).isEqualTo(SESSION_NAME); assertThat(batchTxn.getBatchTransactionId().getTransactionId()).isEqualTo(TXN_ID); Timestamp t = Timestamp.parseTimestamp(TIMESTAMP); assertThat(batchTxn.getReadTimestamp()).isEqualTo(t); assertThat(batchTxn.getReadTimestamp()) .isEqualTo(batchTxn.getBatchTransactionId().getTimestamp()); }
/** * Convert Timestamp to RFC 3339 date string format. The output will always * be Z-normalized and uses 3, 6 or 9 fractional digits as required to * represent the exact value. Note that Timestamp can only represent time * from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z. See * https://www.ietf.org/rfc/rfc3339.txt * * <p>Example of generated format: "1972-01-01T10:00:20.021Z" * * @return The string representation of the given timestamp. * @throws IllegalArgumentException if the given timestamp is not in the * valid range. * @deprecated Use {@link Timestamps#toString} instead. */ @Deprecated public static String toString(Timestamp timestamp) { return Timestamps.toString(timestamp); }
protected Task(StartRequest request, String type, MetricsHandler.MetricName metricName) { this.metricsHandler = new MetricsHandler(request.getProject(), type, metricName); this.burnInTimeMillis = Timestamps.toMillis(Timestamps.add(request.getStartTime(), request.getBurnInDuration())); rateLimiter = RateLimiter.create(request.getRequestRate()); outstandingRequestLimiter = new Semaphore(request.getMaxOutstandingRequests(), false); }
/** * Generate the report with the data recorded this LB stream since the last report. */ ClientStats generateLoadReport() { ClientStats.Builder statsBuilder = ClientStats.newBuilder() .setTimestamp(Timestamps.fromNanos(time.currentTimeNanos())) .setNumCallsStarted(callsStartedUpdater.getAndSet(this, 0)) .setNumCallsFinished(callsFinishedUpdater.getAndSet(this, 0)) .setNumCallsFinishedWithClientFailedToSend(callsFailedToSendUpdater.getAndSet(this, 0)) .setNumCallsFinishedKnownReceived(callsFinishedKnownReceivedUpdater.getAndSet(this, 0)); Map<String, LongHolder> localCallsDroppedPerToken = Collections.emptyMap(); synchronized (this) { if (!callsDroppedPerToken.isEmpty()) { localCallsDroppedPerToken = callsDroppedPerToken; callsDroppedPerToken = new HashMap<String, LongHolder>(localCallsDroppedPerToken.size()); } } for (Entry<String, LongHolder> entry : localCallsDroppedPerToken.entrySet()) { statsBuilder.addCallsFinishedWithDrop( ClientStatsPerToken.newBuilder() .setLoadBalanceToken(entry.getKey()) .setNumCalls(entry.getValue().num) .build()); } return statsBuilder.build(); }
/** * Demonstrates listing time series using a filter. */ void listTimeSeries(String filter) throws IOException { // [START monitoring_read_timeseries_simple] MetricServiceClient metricServiceClient = MetricServiceClient.create(); String projectId = System.getProperty("projectId"); ProjectName name = ProjectName.of(projectId); // Restrict time to last 20 minutes long startMillis = System.currentTimeMillis() - ((60 * 20) * 1000); TimeInterval interval = TimeInterval.newBuilder() .setStartTime(Timestamps.fromMillis(startMillis)) .setEndTime(Timestamps.fromMillis(System.currentTimeMillis())) .build(); ListTimeSeriesRequest.Builder requestBuilder = ListTimeSeriesRequest.newBuilder() .setName(name.toString()) .setFilter(filter) .setInterval(interval); ListTimeSeriesRequest request = requestBuilder.build(); ListTimeSeriesPagedResponse response = metricServiceClient.listTimeSeries(request); System.out.println("Got timeseries: "); for (TimeSeries ts : response.iterateAll()) { System.out.println(ts); } // [END monitoring_read_timeseries_simple] }
public static Date getDateFromTimestamp(Timestamp timestamp) { return new Date(Timestamps.toMillis(timestamp)); }
.setRequestId(requestID) .setSourceName(source) .setTimestamp(Timestamps.fromMillis(System.currentTimeMillis())) .putClientMetadata(CLIENT_HOST_NAME_METADATA_KEY, agentMetadata.getAgentHostName()) .build(); "Sending Ping with id: {}, timestamp: {}", requestID, Timestamps.toString(request.getTimestamp()) );
@Test public void multiUseReadOnlyTransactionReturnsMissingTransactionId() throws ParseException { com.google.protobuf.Timestamp t = Timestamps.parse("2015-10-01T10:54:20.021Z"); Transaction txnMetadata = Transaction.newBuilder().setReadTimestamp(t).build(); PartialResultSet resultSet = PartialResultSet.newBuilder() .setMetadata(newMetadata(Type.struct(Type.StructField.of("C", Type.string())))) .build(); Mockito.when(rpc.beginTransaction(Mockito.<BeginTransactionRequest>any(), Mockito.eq(options))) .thenReturn(txnMetadata); mockRead(resultSet); ReadOnlyTransaction txn = session.readOnlyTransaction(TimestampBound.strong()); expectedException.expect(SpannerMatchers.isSpannerException(ErrorCode.INTERNAL)); txn.readRow("Dummy", Key.of(), Arrays.asList("C")); } }
/** Prints google.protobuf.Timestamp */ private void printTimestamp(MessageOrBuilder message) throws IOException { Timestamp value = Timestamp.parseFrom(toByteString(message)); generator.print("\"" + Timestamps.toString(value) + "\""); }
/** * Demonstrates listing time series headers. */ void listTimeSeriesHeaders() throws IOException { // [START monitoring_read_timeseries_fields] MetricServiceClient metricServiceClient = MetricServiceClient.create(); String projectId = System.getProperty("projectId"); ProjectName name = ProjectName.of(projectId); // Restrict time to last 20 minutes long startMillis = System.currentTimeMillis() - ((60 * 20) * 1000); TimeInterval interval = TimeInterval.newBuilder() .setStartTime(Timestamps.fromMillis(startMillis)) .setEndTime(Timestamps.fromMillis(System.currentTimeMillis())) .build(); ListTimeSeriesRequest.Builder requestBuilder = ListTimeSeriesRequest.newBuilder() .setName(name.toString()) .setFilter("metric.type=\"compute.googleapis.com/instance/cpu/utilization\"") .setInterval(interval) .setView(ListTimeSeriesRequest.TimeSeriesView.HEADERS); ListTimeSeriesRequest request = requestBuilder.build(); ListTimeSeriesPagedResponse response = metricServiceClient.listTimeSeries(request); System.out.println("Got timeseries headers: "); for (TimeSeries ts : response.iterateAll()) { System.out.println(ts); } // [END monitoring_read_timeseries_fields] }
/** * Convert a Timestamp to the number of milliseconds elapsed from the epoch. * * <p>The result will be rounded down to the nearest millisecond. E.g., if the * timestamp represents "1969-12-31T23:59:59.999999999Z", it will be rounded * to -1 millisecond. * * @deprecated Use {@link Timestamps#toMillis} instead. */ @Deprecated public static long toMillis(Timestamp timestamp) { return Timestamps.toMillis(timestamp); }
@Test public void writeAtLeastOnce() throws ParseException { String timestampString = "2015-10-01T10:54:20.021Z"; ArgumentCaptor<CommitRequest> commit = ArgumentCaptor.forClass(CommitRequest.class); CommitResponse response = CommitResponse.newBuilder().setCommitTimestamp(Timestamps.parse(timestampString)).build(); Mockito.when(rpc.commit(commit.capture(), Mockito.eq(options))).thenReturn(response); Timestamp timestamp = session.writeAtLeastOnce( Arrays.asList(Mutation.newInsertBuilder("T").set("C").to("x").build())); assertThat(timestamp.getSeconds()) .isEqualTo(utcTimeSeconds(2015, Calendar.OCTOBER, 1, 10, 54, 20)); assertThat(timestamp.getNanos()).isEqualTo(TimeUnit.MILLISECONDS.toNanos(21)); CommitRequest request = commit.getValue(); assertThat(request.getSingleUseTransaction()).isNotNull(); assertThat(request.getSingleUseTransaction().getReadWrite()).isNotNull(); com.google.spanner.v1.Mutation mutation = com.google.spanner.v1.Mutation.newBuilder() .setInsert( Write.newBuilder() .setTable("T") .addColumns("C") .addValues( ListValue.newBuilder() .addValues(com.google.protobuf.Value.newBuilder().setStringValue("x")))) .build(); assertThat(request.getMutationsList()).containsExactly(mutation); }