com.google.protobuf.util.Timestamps Java Examples

The following examples show how to use com.google.protobuf.util.Timestamps. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GrpclbClientLoadRecorder.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example #2
Source File: PutOperationStage.java    From bazel-buildfarm with Apache License 2.0 6 votes vote down vote up
private void removeStaleData(Timestamp now) {
  // currentSlot != lastUsedSlot means stepping over to a new bucket.
  // The data in the new bucket should be thrown away before storing new data.
  int currentSlot = getCurrentSlot(now);
  if (lastOperationCompleteTime != null && lastUsedSlot >= 0) {
    Duration duration = Timestamps.between(lastOperationCompleteTime, now);
    // if 1) duration between the new added operation and last added one is longer than period
    // or 2) the duration is shorter than period but longer than time range of a single bucket
    //       and at the same time currentSlot == lastUsedSlot
    if ((Durations.toMillis(duration) >= Durations.toMillis(period))
        || (lastUsedSlot == currentSlot
            && Durations.toMillis(duration) > (Durations.toMillis(period) / buckets.length))) {
      for (OperationStageDurations bucket : buckets) {
        bucket.reset();
      }
    } else if (lastUsedSlot != currentSlot) {
      // currentSlot < lastUsedSlot means wrap around happened
      currentSlot = currentSlot < lastUsedSlot ? currentSlot + NumOfSlots : currentSlot;
      for (int i = lastUsedSlot + 1; i <= currentSlot; i++) {
        buckets[i % buckets.length].reset();
      }
    }
  }
}
 
Example #3
Source File: PutOperationStage.java    From bazel-buildfarm with Apache License 2.0 6 votes vote down vote up
void set(ExecutedActionMetadata metadata) {
  queuedToMatch =
      Timestamps.between(metadata.getQueuedTimestamp(), metadata.getWorkerStartTimestamp());
  matchToInputFetchStart =
      Timestamps.between(
          metadata.getWorkerStartTimestamp(), metadata.getInputFetchStartTimestamp());
  inputFetchStartToComplete =
      Timestamps.between(
          metadata.getInputFetchStartTimestamp(), metadata.getInputFetchCompletedTimestamp());
  inputFetchCompleteToExecutionStart =
      Timestamps.between(
          metadata.getInputFetchCompletedTimestamp(), metadata.getExecutionStartTimestamp());
  executionStartToComplete =
      Timestamps.between(
          metadata.getExecutionStartTimestamp(), metadata.getExecutionCompletedTimestamp());
  executionCompleteToOutputUploadStart =
      Timestamps.between(
          metadata.getExecutionCompletedTimestamp(), metadata.getOutputUploadStartTimestamp());
  outputUploadStartToComplete =
      Timestamps.between(
          metadata.getOutputUploadStartTimestamp(),
          metadata.getOutputUploadCompletedTimestamp());
  operationCount = 1;
}
 
Example #4
Source File: LocalJobsService.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
JobStats getJobStats(JobStatsRequest request) {
  final long startDate = Timestamps.toMillis(request.getStartDate());
  final long endDate = Timestamps.toMillis(request.getEndDate());

  final List<Integer> counts = store.getCounts(JOBS_STATS_TYPE_TO_SEARCH_QUERY_MAPPING.stream()
      .map(SimpleEntry::getValue)
      .map(searchQuery -> SearchQueryUtils.and(
          SearchQueryUtils.newRangeLong(JobIndexKeys.START_TIME.getIndexFieldName(), startDate, endDate, true, true),
          searchQuery))
      .toArray(SearchQuery[]::new));

  // same order as above
  final JobStats.Builder jobStats = JobStats.newBuilder();
  for (int i = 0; i < counts.size(); i++) {
    jobStats.addCounts(JobStats.JobCountWithType.newBuilder()
        .setType(JOBS_STATS_TYPE_TO_SEARCH_QUERY_MAPPING.get(i).getKey())
        .setCount(counts.get(i)));
  }
  return jobStats.build();
}
 
Example #5
Source File: PBUserServiceImpl.java    From dubbo-benchmark with Apache License 2.0 6 votes vote down vote up
@Override
public PagePB.Response getUser(PagePB.Request request) {
    final long id = request.getId();
    final PagePB.User.Builder user = PagePB.User.newBuilder();
    user.setId(id);
    user.setName(new String("Doug Lea"));
    user.setSex(1);
    user.setBirthday(Timestamps.fromMillis(System.currentTimeMillis()));
    user.setEmail(new String("[email protected]"));
    user.setMobile(new String("18612345678"));
    user.setAddress(new String("北京市 中关村 中关村大街1号 鼎好大厦 1605"));
    user.setIcon(new String("https://www.baidu.com/img/bd_logo1.png"));
    user.setStatus(1);
    user.setCreateTime(Timestamps.fromMillis(System.currentTimeMillis()));
    user.setUpdateTime(user.getCreateTime());
    List<Integer> permissions = new ArrayList<Integer>(
            Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 19, 88, 86, 89, 90, 91, 92));
    user.addAllPermissions(permissions);

    return PagePB.Response.newBuilder().setUser(user.build()).build();
}
 
Example #6
Source File: ChannelzProtoUtil.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
private static List<ChannelTraceEvent> toChannelTraceEvents(List<Event> events) {
  List<ChannelTraceEvent> channelTraceEvents = new ArrayList<>();
  for (Event event : events) {
    ChannelTraceEvent.Builder builder = ChannelTraceEvent.newBuilder()
        .setDescription(event.description)
        .setSeverity(Severity.valueOf(event.severity.name()))
        .setTimestamp(Timestamps.fromNanos(event.timestampNanos));
    if (event.channelRef != null) {
      builder.setChannelRef(toChannelRef(event.channelRef));
    }
    if (event.subchannelRef != null) {
      builder.setSubchannelRef(toSubchannelRef(event.subchannelRef));
    }
    channelTraceEvents.add(builder.build());
  }
  return Collections.unmodifiableList(channelTraceEvents);
}
 
Example #7
Source File: ClientPb.java    From dubbo-benchmark with Apache License 2.0 6 votes vote down vote up
@Benchmark
@BenchmarkMode({Mode.Throughput, Mode.AverageTime, Mode.SampleTime})
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public boolean createUser() throws Exception {
    final int count = counter.getAndIncrement();

    final PagePB.User.Builder user = PagePB.User.newBuilder();
    user.setId(count);
    user.setName(new String("Doug Lea"));
    user.setSex(1);
    user.setBirthday(Timestamps.fromMillis(System.currentTimeMillis()));
    user.setEmail(new String("[email protected]"));
    user.setMobile(new String("18612345678"));
    user.setAddress(new String("北京市 中关村 中关村大街1号 鼎好大厦 1605"));
    user.setIcon(new String("https://www.baidu.com/img/bd_logo1.png"));
    user.setStatus(1);
    user.setCreateTime(Timestamps.fromMillis(System.currentTimeMillis()));
    user.setUpdateTime(user.getCreateTime());
    List<Integer> permissions = new ArrayList<Integer>(
            Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 19, 88, 86, 89, 90, 91, 92));
    user.addAllPermissions(permissions);
    final PagePB.Request.Builder builder = PagePB.Request.newBuilder();
    return userService.createUser(builder.setUser(user.build()).build()).getState();

}
 
Example #8
Source File: ProtoUnmarshaler.java    From Mastering-Distributed-Tracing with MIT License 6 votes vote down vote up
private Span fromProto(Model.Span protoSpan) {
    Span span = new Span();
    span.traceId = asHexString(protoSpan.getTraceId());
    span.spanId = asHexString(protoSpan.getSpanId());
    span.operationName = protoSpan.getOperationName();
    span.serviceName = protoSpan.getProcess().getServiceName();
    span.startTimeMicros = Timestamps.toMicros(protoSpan.getStartTime());
    span.tags = new HashMap<>();
    for (Model.KeyValue kv : protoSpan.getTagsList()) {
        if (!Model.ValueType.STRING.equals(kv.getVType())) {
            continue;
        }
        String value = kv.getVStr();
        if (value != null) {
            span.tags.put(kv.getKey(), value);
        }
    }
    return span;
}
 
Example #9
Source File: BigQueryRunner.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
private TimeSeries prepareMetric(MetricDescriptor requiredMetric, long metricValue) {
  TimeInterval interval =
      TimeInterval.newBuilder()
          .setEndTime(Timestamps.fromMillis(System.currentTimeMillis()))
          .build();
  TypedValue value = TypedValue.newBuilder().setInt64Value(metricValue).build();

  Point point = Point.newBuilder().setInterval(interval).setValue(value).build();

  List<Point> pointList = Lists.newArrayList();
  pointList.add(point);

  Metric metric = Metric.newBuilder().setType(requiredMetric.getName()).build();

  return TimeSeries.newBuilder().setMetric(metric).addAllPoints(pointList).build();
}
 
Example #10
Source File: GrpclbClientLoadRecorder.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
/**
 * 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<>(localCallsDroppedPerToken.size());
    }
  }
  for (Entry<String, LongHolder> entry : localCallsDroppedPerToken.entrySet()) {
    statsBuilder.addCallsFinishedWithDrop(
        ClientStatsPerToken.newBuilder()
            .setLoadBalanceToken(entry.getKey())
            .setNumCalls(entry.getValue().num)
            .build());
  }
  return statsBuilder.build();
}
 
Example #11
Source File: ProtobufTimestampParserTest.java    From secor with Apache License 2.0 6 votes vote down vote up
@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(), null)));

    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(), null)));
}
 
Example #12
Source File: BuildEventServiceProtoUtilTest.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Test
public void testBuildFinished() {
  Timestamp expected = Timestamps.fromMillis(clock.advanceMillis(100));
  assertThat(BES_PROTO_UTIL.buildFinished(expected, Result.COMMAND_SUCCEEDED))
      .isEqualTo(
          PublishLifecycleEventRequest.newBuilder()
              .setServiceLevel(ServiceLevel.INTERACTIVE)
              .setProjectId(PROJECT_ID)
              .setBuildEvent(
                  OrderedBuildEvent.newBuilder()
                      .setStreamId(
                          StreamId.newBuilder()
                              .setBuildId(BUILD_REQUEST_ID)
                              .setComponent(BuildComponent.CONTROLLER))
                      .setSequenceNumber(2)
                      .setEvent(
                          BuildEvent.newBuilder()
                              .setEventTime(expected)
                              .setBuildFinished(
                                  BuildFinished.newBuilder()
                                      .setStatus(
                                          BuildStatus.newBuilder()
                                              .setResult(Result.COMMAND_SUCCEEDED)))))
              .build());
}
 
Example #13
Source File: BuildEventServiceProtoUtilTest.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvocationAttemptFinished() {
  Timestamp expected = Timestamps.fromMillis(clock.advanceMillis(100));
  assertThat(BES_PROTO_UTIL.invocationFinished(expected, Result.COMMAND_SUCCEEDED))
      .isEqualTo(
          PublishLifecycleEventRequest.newBuilder()
              .setServiceLevel(ServiceLevel.INTERACTIVE)
              .setProjectId(PROJECT_ID)
              .setBuildEvent(
                  OrderedBuildEvent.newBuilder()
                      .setStreamId(
                          StreamId.newBuilder()
                              .setBuildId(BUILD_REQUEST_ID)
                              .setInvocationId(BUILD_INVOCATION_ID)
                              .setComponent(BuildComponent.CONTROLLER))
                      .setSequenceNumber(2)
                      .setEvent(
                          BuildEvent.newBuilder()
                              .setEventTime(expected)
                              .setInvocationAttemptFinished(
                                  InvocationAttemptFinished.newBuilder()
                                      .setInvocationStatus(
                                          BuildStatus.newBuilder()
                                              .setResult(Result.COMMAND_SUCCEEDED)))))
              .build());
}
 
Example #14
Source File: BuildEventServiceProtoUtilTest.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvocationAttemptStarted() {
  Timestamp expected = Timestamps.fromMillis(clock.advanceMillis(100));
  assertThat(BES_PROTO_UTIL.invocationStarted(expected))
      .isEqualTo(
          PublishLifecycleEventRequest.newBuilder()
              .setServiceLevel(ServiceLevel.INTERACTIVE)
              .setProjectId(PROJECT_ID)
              .setBuildEvent(
                  OrderedBuildEvent.newBuilder()
                      .setStreamId(
                          StreamId.newBuilder()
                              .setBuildId(BUILD_REQUEST_ID)
                              .setInvocationId(BUILD_INVOCATION_ID)
                              .setComponent(BuildComponent.CONTROLLER))
                      .setSequenceNumber(1)
                      .setEvent(
                          BuildEvent.newBuilder()
                              .setEventTime(expected)
                              .setInvocationAttemptStarted(
                                  InvocationAttemptStarted.newBuilder().setAttemptNumber(1))))
              .build());
}
 
Example #15
Source File: ChannelzProtoUtil.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
private static List<ChannelTraceEvent> toChannelTraceEvents(List<Event> events) {
  List<ChannelTraceEvent> channelTraceEvents = new ArrayList<>();
  for (Event event : events) {
    ChannelTraceEvent.Builder builder = ChannelTraceEvent.newBuilder()
        .setDescription(event.description)
        .setSeverity(Severity.valueOf(event.severity.name()))
        .setTimestamp(Timestamps.fromNanos(event.timestampNanos));
    if (event.channelRef != null) {
      builder.setChannelRef(toChannelRef(event.channelRef));
    }
    if (event.subchannelRef != null) {
      builder.setSubchannelRef(toSubchannelRef(event.subchannelRef));
    }
    channelTraceEvents.add(builder.build());
  }
  return Collections.unmodifiableList(channelTraceEvents);
}
 
Example #16
Source File: BuildEventServiceProtoUtilTest.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Test
public void testBuildEnqueued() {
  Timestamp expected = Timestamps.fromMillis(clock.advanceMillis(100));
  assertThat(BES_PROTO_UTIL.buildEnqueued(expected))
      .isEqualTo(
          PublishLifecycleEventRequest.newBuilder()
              .setServiceLevel(ServiceLevel.INTERACTIVE)
              .setProjectId(PROJECT_ID)
              .setBuildEvent(
                  OrderedBuildEvent.newBuilder()
                      .setStreamId(
                          StreamId.newBuilder()
                              .setBuildId(BUILD_REQUEST_ID)
                              .setComponent(BuildComponent.CONTROLLER))
                      .setSequenceNumber(1)
                      .setEvent(
                          BuildEvent.newBuilder()
                              .setEventTime(expected)
                              .setBuildEnqueued(BuildEnqueued.newBuilder())))
              .build());
}
 
Example #17
Source File: DefaultHealthService.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
public Details getServerStatus() {
    RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean();
    Duration uptime = Durations.fromMillis(rb.getUptime());

    if (!leaderActivator.isLeader()) {
        return Details.newBuilder()
                .setStatus(NOT_SERVING)
                .setLeader(false)
                .setActive(false)
                .setUptime(uptime)
                .build();
    }

    boolean active = leaderActivator.isActivated();

    List<ServiceActivation> serviceActivations = activationLifecycle.getServiceActionTimesMs().stream()
            .sorted(Comparator.comparing(Pair::getRight))
            .map(pair -> ServiceActivation.newBuilder()
                    .setName(pair.getLeft())
                    .setActivationTime(Durations.fromMillis(pair.getRight()))
                    .build())
            .collect(Collectors.toList());

    Details.Builder details = Details.newBuilder()
            .setStatus(SERVING)
            .setCell(cellInfoResolver.getCellName())
            .setLeader(true)
            .setActive(active)
            .setUptime(uptime)
            .setElectionTimestamp(Timestamps.fromMillis(leaderActivator.getElectionTimestamp()))
            .setActivationTimestamp(Timestamps.fromMillis(leaderActivator.getActivationEndTimestamp()))
            .addAllServiceActivationTimes(serviceActivations);

    if (active) {
        details.setActivationTime(Durations.fromMillis(leaderActivator.getActivationTime()));
    }

    return details.build();
}
 
Example #18
Source File: MessageMarshallerTest.java    From curiostack with MIT License 5 votes vote down vote up
@Test
public void timestamp() throws Exception {
  TestTimestamp message =
      TestTimestamp.newBuilder()
          .setTimestampValue(Timestamps.parse("1970-01-01T00:00:00Z"))
          .build();
  assertMatchesUpstream(message);
}
 
Example #19
Source File: ServerStatusResource.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
@GET
public ServerStatusRepresentation getServerStatus() {
    Details details = healthService.getServerStatus();

    if (details.getStatus() != ServingStatus.SERVING) {
        return new ServerStatusRepresentation(
                false,
                false,
                DateTimeExt.toTimeUnitString(details.getUptime()),
                NOT_APPLICABLE,
                NOT_APPLICABLE,
                NOT_APPLICABLE,
                Collections.emptyList(),
                Collections.emptyList()
        );
    }

    List<ServerStatusRepresentation.ServiceActivation> sortedByActivationTime = details.getServiceActivationTimesList().stream()
            .sorted(Comparator.comparing(ServiceActivation::getActivationTime, Durations.comparator()))
            .map(s -> new ServerStatusRepresentation.ServiceActivation(s.getName(), DateTimeExt.toTimeUnitString(s.getActivationTime())))
            .collect(Collectors.toList());

    List<String> namesSortedByActivationTimestamp = details.getServiceActivationTimesList().stream()
            .map(ServiceActivation::getName)
            .collect(Collectors.toList());

    return new ServerStatusRepresentation(
            details.getLeader(),
            details.getActive(),
            DateTimeExt.toTimeUnitString(details.getUptime()),
            Timestamps.toString(details.getElectionTimestamp()),
            Timestamps.toString(details.getActivationTimestamp()),
            details.getActive() ? DateTimeExt.toTimeUnitString(details.getActivationTime()) : NOT_APPLICABLE,
            sortedByActivationTime,
            namesSortedByActivationTimestamp
    );
}
 
Example #20
Source File: ChannelzProtoUtil.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
static ServerData toServerData(ServerStats stats) {
  return ServerData
      .newBuilder()
      .setCallsStarted(stats.callsStarted)
      .setCallsSucceeded(stats.callsSucceeded)
      .setCallsFailed(stats.callsFailed)
      .setLastCallStartedTimestamp(Timestamps.fromNanos(stats.lastCallStartedNanos))
      .build();
}
 
Example #21
Source File: GRpcPingServiceImpl.java    From genie with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void ping(
    final PingRequest request,
    final StreamObserver<PongResponse> responseObserver
) {
    final PongResponse response = PongResponse.newBuilder()
        .setRequestId(request.getRequestId())
        .setTimestamp(Timestamps.fromMillis(System.currentTimeMillis()))
        .putServerMetadata(ServerMetadataKeys.SERVER_NAME, hostName)
        .build();
    responseObserver.onNext(response);
    responseObserver.onCompleted();

    final StringBuilder sb = new StringBuilder();

    sb.append(
        String.format(
            "Received ping with id: '%s' from client: '%s' and timestamp: '%s'. ",
            request.getRequestId(),
            request.getSourceName(),
            Timestamps.toString(request.getTimestamp())
        )
    );

    sb.append("Client metadata: [ ");
    for (final Map.Entry<String, String> clientMetadataEntry : request.getClientMetadataMap().entrySet()) {
        sb
            .append("{")
            .append(clientMetadataEntry.getKey())
            .append(" : ")
            .append(clientMetadataEntry.getValue())
            .append("}, ");
    }
    sb.append("]");

    log.info(sb.toString());
}
 
Example #22
Source File: MatchStage.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
private OperationContext match(OperationContext operationContext) {
  Timestamp workerStartTimestamp = Timestamps.fromMillis(System.currentTimeMillis());

  ExecuteEntry executeEntry = operationContext.queueEntry.getExecuteEntry();
  // this may be superfluous - we can probably just set the name and action digest
  Operation operation =
      Operation.newBuilder()
          .setName(executeEntry.getOperationName())
          .setMetadata(
              Any.pack(
                  ExecuteOperationMetadata.newBuilder()
                      .setActionDigest(executeEntry.getActionDigest())
                      .setStage(QUEUED)
                      .setStdoutStreamName(executeEntry.getStdoutStreamName())
                      .setStderrStreamName(executeEntry.getStderrStreamName())
                      .build()))
          .build();

  OperationContext matchedOperationContext =
      operationContext.toBuilder().setOperation(operation).build();

  matchedOperationContext
      .executeResponse
      .getResultBuilder()
      .getExecutionMetadataBuilder()
      .setWorker(workerContext.getName())
      .setQueuedTimestamp(executeEntry.getQueuedTimestamp())
      .setWorkerStartTimestamp(workerStartTimestamp);
  return matchedOperationContext;
}
 
Example #23
Source File: GrpclbLoadBalancerTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
private void assertNextReport(
    InOrder inOrder, StreamObserver<LoadBalanceRequest> lbRequestObserver,
    long loadReportIntervalMillis, ClientStats expectedReport) {
  assertEquals(0, fakeClock.forwardTime(loadReportIntervalMillis - 1, TimeUnit.MILLISECONDS));
  inOrder.verifyNoMoreInteractions();
  assertEquals(1, fakeClock.forwardTime(1, TimeUnit.MILLISECONDS));
  assertEquals(1, fakeClock.numPendingTasks());
  inOrder.verify(lbRequestObserver).onNext(
      eq(LoadBalanceRequest.newBuilder()
          .setClientStats(
              ClientStats.newBuilder(expectedReport)
              .setTimestamp(Timestamps.fromNanos(fakeClock.getTicker().read()))
              .build())
          .build()));
}
 
Example #24
Source File: WellKnownTypeMarshaller.java    From curiostack with MIT License 5 votes vote down vote up
@Override
public void doMerge(JsonParser parser, int unused, Message.Builder messageBuilder)
    throws IOException {
  Timestamp.Builder builder = (Timestamp.Builder) messageBuilder;
  try {
    builder.mergeFrom(Timestamps.parse(ParseSupport.parseString(parser)));
  } catch (ParseException e) {
    throw new InvalidProtocolBufferException(
        "Failed to readValue timestamp: " + parser.getText());
  }
}
 
Example #25
Source File: ProtobufMessageParser.java    From secor with Apache License 2.0 5 votes vote down vote up
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());
    }
}
 
Example #26
Source File: Snippets.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
/** Demonstrates listing time series and aggregating and reducing them. */
void listTimeSeriesReduce() throws IOException {
  // [START monitoring_read_timeseries_reduce]
  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();

  Aggregation aggregation =
      Aggregation.newBuilder()
          .setAlignmentPeriod(Duration.newBuilder().setSeconds(600).build())
          .setPerSeriesAligner(Aggregation.Aligner.ALIGN_MEAN)
          .setCrossSeriesReducer(Aggregation.Reducer.REDUCE_MEAN)
          .build();

  ListTimeSeriesRequest.Builder requestBuilder =
      ListTimeSeriesRequest.newBuilder()
          .setName(name.toString())
          .setFilter("metric.type=\"compute.googleapis.com/instance/cpu/utilization\"")
          .setInterval(interval)
          .setAggregation(aggregation);

  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_reduce]
}
 
Example #27
Source File: TimestampSerializer.java    From jackson-datatype-protobuf with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(
        Timestamp timestamp,
        JsonGenerator generator,
        SerializerProvider serializerProvider
) throws IOException {
  generator.writeString(Timestamps.toString(timestamp));
}
 
Example #28
Source File: Snippets.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
/** Demonstrates listing time series and aggregating them. */
void listTimeSeriesAggregrate() throws IOException {
  // [START monitoring_read_timeseries_align]
  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();

  Aggregation aggregation =
      Aggregation.newBuilder()
          .setAlignmentPeriod(Duration.newBuilder().setSeconds(600).build())
          .setPerSeriesAligner(Aggregation.Aligner.ALIGN_MEAN)
          .build();

  ListTimeSeriesRequest.Builder requestBuilder =
      ListTimeSeriesRequest.newBuilder()
          .setName(name.toString())
          .setFilter("metric.type=\"compute.googleapis.com/instance/cpu/utilization\"")
          .setInterval(interval)
          .setAggregation(aggregation);

  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_align]
}
 
Example #29
Source File: BuildEventServiceUploader.java    From bazel with Apache License 2.0 5 votes vote down vote up
/** Enqueues an event for uploading to a BES backend. */
void enqueueEvent(BuildEvent event) {
  // This needs to happen outside a synchronized block as it may trigger
  // stdout/stderr and lead to a deadlock. See b/109725432
  ListenableFuture<PathConverter> localFileUploadFuture =
      buildEventUploader.uploadReferencedLocalFiles(event.referencedLocalFiles());

  // The generation of the sequence number and the addition to the {@link #eventQueue} should be
  // atomic since BES expects the events in that exact order.
  // More details can be found in b/131393380.
  // TODO(bazel-team): Consider relaxing this invariant by having a more relaxed order.
  synchronized (lock) {
    if (startedClose) {
      return;
    }
    // BuildCompletingEvent marks the end of the build in the BEP event stream.
    if (event instanceof BuildCompletingEvent) {
      this.buildStatus = extractBuildStatus((BuildCompletingEvent) event);
    }
    ensureUploadThreadStarted();

    // TODO(b/131393380): {@link #nextSeqNum} doesn't need to be an AtomicInteger if it's
    //  always used under lock. It would be cleaner and more performant to update the sequence
    //  number when we take the item off the queue.
    eventQueue.addLast(
        new SendRegularBuildEventCommand(
            event,
            localFileUploadFuture,
            nextSeqNum.getAndIncrement(),
            Timestamps.fromMillis(clock.currentTimeMillis())));
  }
}
 
Example #30
Source File: RemoteExecutionEventListener.java    From buck with Apache License 2.0 5 votes vote down vote up
/** Event specific subscriber method. */
@Subscribe
public void onActionEventTerminal(RemoteExecutionActionEvent.Terminal event) {
  hasFirstRemoteActionStarted.set(true);
  getStateCount(State.WAITING).decrement();
  getStateCount(event.getState()).increment();
  if (event.getExecutedActionMetadata().isPresent()) {
    ExecutedActionInfo executedActionInfo =
        event.getRemoteExecutionMetadata().get().getExecutedActionInfo();
    remoteCpuTimeMs.add(TimeUnit.MICROSECONDS.toMillis(executedActionInfo.getCpuStatUsageUsec()));

    Duration queueDuration =
        Timestamps.between(
            event.getExecutedActionMetadata().get().getQueuedTimestamp(),
            event.getExecutedActionMetadata().get().getWorkerStartTimestamp());
    remoteQueueTimeMs.add(
        TimeUnit.SECONDS.toMillis(queueDuration.getSeconds())
            + TimeUnit.NANOSECONDS.toMillis(queueDuration.getNanos()));

    Duration totalDuration =
        Timestamps.between(
            event.getExecutedActionMetadata().get().getWorkerStartTimestamp(),
            event.getExecutedActionMetadata().get().getWorkerCompletedTimestamp());
    totalRemoteTimeMs.add(
        TimeUnit.SECONDS.toMillis(totalDuration.getSeconds())
            + TimeUnit.NANOSECONDS.toMillis(totalDuration.getNanos()));
  }
}