com.google.protobuf.util.Durations Java Examples

The following examples show how to use com.google.protobuf.util.Durations. 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: 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 #2
Source File: ChannelzProtoUtil.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
static SocketOption toSocketOptionLinger(int lingerSeconds) {
  final SocketOptionLinger lingerOpt;
  if (lingerSeconds >= 0) {
    lingerOpt = SocketOptionLinger
        .newBuilder()
        .setActive(true)
        .setDuration(Durations.fromSeconds(lingerSeconds))
        .build();
  } else {
    lingerOpt = SocketOptionLinger.getDefaultInstance();
  }
  return SocketOption
      .newBuilder()
      .setName(SO_LINGER)
      .setAdditional(Any.pack(lingerOpt))
      .build();
}
 
Example #3
Source File: ChannelzProtoUtil.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
static SocketOption toSocketOptionLinger(int lingerSeconds) {
  final SocketOptionLinger lingerOpt;
  if (lingerSeconds >= 0) {
    lingerOpt = SocketOptionLinger
        .newBuilder()
        .setActive(true)
        .setDuration(Durations.fromSeconds(lingerSeconds))
        .build();
  } else {
    lingerOpt = SocketOptionLinger.getDefaultInstance();
  }
  return SocketOption
      .newBuilder()
      .setName(SO_LINGER)
      .setAdditional(Any.pack(lingerOpt))
      .build();
}
 
Example #4
Source File: LoadReportClient.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
private void sendLoadReport() {
  long interval = reportStopwatch.elapsed(TimeUnit.NANOSECONDS);
  reportStopwatch.reset().start();
  LoadStatsRequest.Builder requestBuilder = LoadStatsRequest.newBuilder().setNode(node);
  for (String name : clusterNames) {
    if (loadStatsStoreMap.containsKey(name)) {
      Map<String, LoadStatsStore> clusterLoadStatsStores = loadStatsStoreMap.get(name);
      for (LoadStatsStore statsStore : clusterLoadStatsStores.values()) {
        ClusterStats report =
            statsStore.generateLoadReport()
                .toBuilder()
                .setLoadReportInterval(Durations.fromNanos(interval))
                .build();
        requestBuilder.addClusterStats(report);
      }
    }
  }
  LoadStatsRequest request = requestBuilder.build();
  lrsRequestWriter.onNext(request);
  logger.log(XdsLogLevel.DEBUG, "Sent LoadStatsRequest\n{0}", request);
  scheduleNextLoadReport();
}
 
Example #5
Source File: ParameterOverrides.java    From pubsub with Apache License 2.0 6 votes vote down vote up
public TestParameters apply(TestParameters source) {
  TestParameters.Builder builder = source.toBuilder();
  if (messageSize != null) {
    builder.setMessageSize(messageSize);
  }
  if (burnInMinutes != null) {
    builder.setBurnInDuration(Durations.fromSeconds(burnInMinutes * 60));
  }
  if (testMinutes != null) {
    builder.setLoadtestDuration(Durations.fromSeconds(testMinutes * 60));
  }
  if (numCores != null) {
    builder.setNumCoresPerWorker(numCores);
  }
  if (scalingFactor != null) {
    builder.setSubscriberCpuScaling(scalingFactor);
  }
  return builder.build();
}
 
Example #6
Source File: MessageMarshallerTest.java    From curiostack with MIT License 6 votes vote down vote up
@Test
public void anyInMaps() throws Exception {
  TestAny.Builder testAny = TestAny.newBuilder();
  testAny.putAnyMap("int32_wrapper", Any.pack(Int32Value.newBuilder().setValue(123).build()));
  testAny.putAnyMap("int64_wrapper", Any.pack(Int64Value.newBuilder().setValue(456).build()));
  testAny.putAnyMap("timestamp", Any.pack(Timestamps.parse("1969-12-31T23:59:59Z")));
  testAny.putAnyMap("duration", Any.pack(Durations.parse("12345.1s")));
  testAny.putAnyMap("field_mask", Any.pack(FieldMaskUtil.fromString("foo.bar,baz")));
  Value numberValue = Value.newBuilder().setNumberValue(1.125).build();
  Struct.Builder struct = Struct.newBuilder();
  struct.putFields("number", numberValue);
  testAny.putAnyMap("struct", Any.pack(struct.build()));
  Value nullValue = Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build();
  testAny.putAnyMap(
      "list_value",
      Any.pack(ListValue.newBuilder().addValues(numberValue).addValues(nullValue).build()));
  testAny.putAnyMap("number_value", Any.pack(numberValue));
  testAny.putAnyMap("any_value_number", Any.pack(Any.pack(numberValue)));
  testAny.putAnyMap("any_value_default", Any.pack(Any.getDefaultInstance()));
  testAny.putAnyMap("default", Any.getDefaultInstance());

  assertMatchesUpstream(testAny.build(), TestAllTypes.getDefaultInstance());
}
 
Example #7
Source File: KafkaPublisherTask.java    From pubsub with Apache License 2.0 6 votes vote down vote up
private KafkaPublisherTask(
    LoadtestProto.StartRequest request, MetricsHandler handler, int workerCount) {
  super(request, handler, workerCount);
  this.topic = request.getTopic();
  Properties props = new Properties();
  props.putAll(
      new ImmutableMap.Builder<>()
          .put("max.block.ms", "30000")
          .put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer")
          .put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer")
          .put("acks", "all")
          .put("bootstrap.servers", request.getKafkaOptions().getBroker())
          .put("buffer.memory", Integer.toString(1000 * 1000 * 1000)) // 1 GB
          .put(
              "batch.size",
              Long.toString(
                  getBatchSize(
                      request.getPublisherOptions().getBatchSize(),
                      request.getPublisherOptions().getMessageSize())))
          .put(
              "linger.ms",
              Long.toString(Durations.toMillis(request.getPublisherOptions().getBatchDuration())))
          .build());
  this.publisher = new KafkaProducer<>(props);
}
 
Example #8
Source File: CPSPublisherTask.java    From pubsub with Apache License 2.0 6 votes vote down vote up
CPSPublisherTask(StartRequest request, MetricsHandler metricsHandler, int workerCount) {
  super(request, metricsHandler, workerCount);
  log.warn("constructing CPS publisher");
  this.payload = getPayload();
  try {
    this.publisher =
        Publisher.newBuilder(ProjectTopicName.of(request.getProject(), request.getTopic()))
            .setBatchingSettings(
                BatchingSettings.newBuilder()
                    .setElementCountThreshold((long) request.getPublisherOptions().getBatchSize())
                    .setRequestByteThreshold(9500000L)
                    .setDelayThreshold(
                        Duration.ofMillis(
                            Durations.toMillis(request.getPublisherOptions().getBatchDuration())))
                    .setIsEnabled(true)
                    .build())
            .build();
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}
 
Example #9
Source File: PutOperationStage.java    From bazel-buildfarm with Apache License 2.0 6 votes vote down vote up
private OperationStageDurations computeAverage(int weight) {
  OperationStageDurations average = new OperationStageDurations();
  if (weight == 0) {
    return average;
  }
  average.queuedToMatch = Durations.fromNanos(Durations.toNanos(this.queuedToMatch) / weight);
  average.matchToInputFetchStart =
      Durations.fromNanos(Durations.toNanos(this.matchToInputFetchStart) / weight);
  average.inputFetchStartToComplete =
      Durations.fromNanos(Durations.toNanos(this.inputFetchStartToComplete) / weight);
  average.inputFetchCompleteToExecutionStart =
      Durations.fromNanos(Durations.toNanos(this.inputFetchCompleteToExecutionStart) / weight);
  average.executionStartToComplete =
      Durations.fromNanos(Durations.toNanos(this.executionStartToComplete) / weight);
  average.executionCompleteToOutputUploadStart =
      Durations.fromNanos(
          Durations.toNanos(this.executionCompleteToOutputUploadStart) / weight);
  average.outputUploadStartToComplete =
      Durations.fromNanos(Durations.toNanos(this.outputUploadStartToComplete) / weight);
  average.operationCount = this.operationCount;

  return average;
}
 
Example #10
Source File: MemoryInstance.java    From bazel-buildfarm with Apache License 2.0 6 votes vote down vote up
@Override
protected void validateAction(
    String operationName,
    Action action,
    PreconditionFailure.Builder preconditionFailure,
    RequestMetadata requestMetadata)
    throws InterruptedException, StatusException {
  if (action.hasTimeout() && config.hasMaximumActionTimeout()) {
    Duration timeout = action.getTimeout();
    Duration maximum = config.getMaximumActionTimeout();
    if (timeout.getSeconds() > maximum.getSeconds()
        || (timeout.getSeconds() == maximum.getSeconds()
            && timeout.getNanos() > maximum.getNanos())) {
      preconditionFailure
          .addViolationsBuilder()
          .setType(VIOLATION_TYPE_INVALID)
          .setSubject(Durations.toString(timeout) + " > " + Durations.toString(maximum))
          .setDescription(TIMEOUT_OUT_OF_BOUNDS);
    }
  }

  super.validateAction(operationName, action, preconditionFailure, requestMetadata);
}
 
Example #11
Source File: ShardInstance.java    From bazel-buildfarm with Apache License 2.0 6 votes vote down vote up
@Override
protected void validateAction(
    Action action,
    @Nullable Command command,
    Map<Digest, Directory> directoriesIndex,
    Consumer<Digest> onInputDigest,
    PreconditionFailure.Builder preconditionFailure) {
  if (action.hasTimeout() && hasMaxActionTimeout()) {
    Duration timeout = action.getTimeout();
    if (timeout.getSeconds() > maxActionTimeout.getSeconds()
        || (timeout.getSeconds() == maxActionTimeout.getSeconds()
            && timeout.getNanos() > maxActionTimeout.getNanos())) {
      preconditionFailure
          .addViolationsBuilder()
          .setType(VIOLATION_TYPE_INVALID)
          .setSubject(Durations.toString(timeout) + " > " + Durations.toString(maxActionTimeout))
          .setDescription(TIMEOUT_OUT_OF_BOUNDS);
    }
  }

  super.validateAction(action, command, directoriesIndex, onInputDigest, preconditionFailure);
}
 
Example #12
Source File: PutOperationStage.java    From bazel-buildfarm with Apache License 2.0 6 votes vote down vote up
void addOperations(OperationStageDurations other) {
  this.queuedToMatch = Durations.add(this.queuedToMatch, other.queuedToMatch);
  this.matchToInputFetchStart =
      Durations.add(this.matchToInputFetchStart, other.matchToInputFetchStart);
  this.inputFetchStartToComplete =
      Durations.add(this.inputFetchStartToComplete, other.inputFetchStartToComplete);
  this.inputFetchCompleteToExecutionStart =
      Durations.add(
          this.inputFetchCompleteToExecutionStart, other.inputFetchCompleteToExecutionStart);
  this.executionStartToComplete =
      Durations.add(this.executionStartToComplete, other.executionStartToComplete);
  this.executionCompleteToOutputUploadStart =
      Durations.add(
          this.executionCompleteToOutputUploadStart,
          other.executionCompleteToOutputUploadStart);
  this.outputUploadStartToComplete =
      Durations.add(this.outputUploadStartToComplete, other.outputUploadStartToComplete);
  this.operationCount += other.operationCount;
}
 
Example #13
Source File: LoadReportClient.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
private void handleResponse(LoadStatsResponse response) {
  if (closed) {
    return;
  }

  if (!initialResponseReceived) {
    logger.log(XdsLogLevel.DEBUG, "Received LRS initial response:\n{0}", response);
    initialResponseReceived = true;
  } else {
    logger.log(XdsLogLevel.DEBUG, "Received LRS response:\n{0}", response);
  }
  long interval =  Durations.toNanos(response.getLoadReportingInterval());
  if (interval != loadReportIntervalNano) {
    logger.log(XdsLogLevel.INFO, "Update load reporting interval to {0} ns", interval);
    loadReportIntervalNano = interval;
    callback.onReportResponse(loadReportIntervalNano);
  }
  if (clusterNames.size() != response.getClustersCount()
      || !clusterNames.containsAll(response.getClustersList())) {
    logger.log(
        XdsLogLevel.INFO,
        "Update load reporting clusters to {0}", response.getClustersList());
    clusterNames.clear();
    clusterNames.addAll(response.getClustersList());
  }
  scheduleNextLoadReport();
}
 
Example #14
Source File: LoadReportClientTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
private static LoadStatsResponse buildLrsResponse(
    List<String> clusterNames, long loadReportIntervalNanos) {
  return
      LoadStatsResponse
          .newBuilder()
          .addAllClusters(clusterNames)
          .setLoadReportingInterval(Durations.fromNanos(loadReportIntervalNanos))
          .build();
}
 
Example #15
Source File: KafkaSubscriberTask.java    From pubsub with Apache License 2.0 5 votes vote down vote up
private KafkaSubscriberTask(StartRequest request, MetricsHandler handler, int numWorkers) {
  super(request, handler, numWorkers);
  this.pollLength = Durations.toMillis(request.getKafkaOptions().getPollDuration());
  Properties props = new Properties();
  props.putAll(
      ImmutableMap.of(
          "key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer",
          "value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer",
          "group.id", "SUBSCRIBER_ID",
          "enable.auto.commit", "true",
          "session.timeout.ms", "30000"));
  props.put("bootstrap.servers", request.getKafkaOptions().getBroker());
  subscriber = new KafkaConsumer<>(props);
  subscriber.subscribe(Collections.singletonList(request.getTopic()));
}
 
Example #16
Source File: StorageStubProvider.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
private Map<String, Object> getGrpcServiceConfig() {
  Map<String, Object> name = ImmutableMap.of("service", "google.storage.v1.Storage");

  Map<String, Object> retryPolicy =
      ImmutableMap.<String, Object>builder()
          .put("maxAttempts", GRPC_MAX_RETRY_ATTEMPTS)
          .put(
              "initialBackoff",
              Durations.toString(
                  Durations.fromMillis(readOptions.getBackoffInitialIntervalMillis())))
          .put(
              "maxBackoff",
              Durations.toString(Durations.fromMillis(readOptions.getBackoffMaxIntervalMillis())))
          .put("backoffMultiplier", readOptions.getBackoffMultiplier())
          .put("retryableStatusCodes", ImmutableList.of("UNAVAILABLE", "RESOURCE_EXHAUSTED"))
          .build();

  Map<String, Object> methodConfig =
      ImmutableMap.of("name", ImmutableList.of(name), "retryPolicy", retryPolicy);

  // When channel pooling is enabled, force the pick_first grpclb strategy.
  // This is necessary to avoid the multiplicative effect of creating channel pool with
  // `poolSize` number of `ManagedChannel`s, each with a `subSetting` number of number of
  // subchannels.
  // See the service config proto definition for more details:
  // https://github.com/grpc/grpc-proto/blob/master/grpc/service_config/service_config.proto#L182
  Map<String, Object> pickFirstStrategy = ImmutableMap.of("pick_first", ImmutableMap.of());

  Map<String, Object> childPolicy =
      ImmutableMap.of("childPolicy", ImmutableList.of(pickFirstStrategy));

  Map<String, Object> grpcLbPolicy = ImmutableMap.of("grpclb", childPolicy);

  return ImmutableMap.of(
      "methodConfig", ImmutableList.of(methodConfig),
      "loadBalancingConfig", ImmutableList.of(grpcLbPolicy));
}
 
Example #17
Source File: OrcaOobUtilTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void reportWithMostFrequentIntervalRequested() {
  setOrcaReportConfig(parentHelperWrapper, SHORT_INTERVAL_CONFIG);
  setOrcaReportConfig(childHelperWrapper, LONG_INTERVAL_CONFIG);
  createSubchannel(childHelperWrapper.asHelper(), 0, Attributes.EMPTY);
  deliverSubchannelState(0, ConnectivityStateInfo.forNonError(READY));
  verify(mockStateListeners[0]).onSubchannelState(eq(ConnectivityStateInfo.forNonError(READY)));
  assertThat(orcaServiceImps[0].calls).hasSize(1);
  assertLog(subchannels[0].logs,
      "DEBUG: Starting ORCA reporting for " + subchannels[0].getAllAddresses());

  // The real report interval to be requested is the minimum of intervals requested by helpers.
  assertThat(Durations.toNanos(orcaServiceImps[0].calls.peek().request.getReportInterval()))
      .isEqualTo(SHORT_INTERVAL_CONFIG.getReportIntervalNanos());

  // Child helper wants reporting to be more frequent than its current setting while it is still
  // less frequent than parent helper. Nothing should happen on existing RPC.
  setOrcaReportConfig(childHelperWrapper, MEDIUM_INTERVAL_CONFIG);
  assertThat(orcaServiceImps[0].calls.peek().cancelled).isFalse();
  assertThat(subchannels[0].logs).isEmpty();

  // Parent helper wants reporting to be less frequent.
  setOrcaReportConfig(parentHelperWrapper, MEDIUM_INTERVAL_CONFIG);
  assertThat(orcaServiceImps[0].calls.poll().cancelled).isTrue();
  assertThat(orcaServiceImps[0].calls).hasSize(1);
  assertLog(subchannels[0].logs,
      "DEBUG: Starting ORCA reporting for " + subchannels[0].getAllAddresses());
  // ORCA reporting RPC restarts and the the real report interval is adjusted.
  assertThat(Durations.toNanos(orcaServiceImps[0].calls.poll().request.getReportInterval()))
      .isEqualTo(MEDIUM_INTERVAL_CONFIG.getReportIntervalNanos());
}
 
Example #18
Source File: RemoteSpawnRunnerTest.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Test
public void accountingAddsDurationsForStages() {
  SpawnMetrics.Builder builder =
      SpawnMetrics.Builder.forRemoteExec()
          .setQueueTime(Duration.ofSeconds(1))
          .setSetupTime(Duration.ofSeconds(2))
          .setExecutionWallTime(Duration.ofSeconds(2))
          .setProcessOutputsTime(Duration.ofSeconds(2));
  Timestamp queued = Timestamp.getDefaultInstance();
  com.google.protobuf.Duration oneSecond = Durations.fromMillis(1000);
  Timestamp workerStart = Timestamps.add(queued, oneSecond);
  Timestamp executionStart = Timestamps.add(workerStart, oneSecond);
  Timestamp executionCompleted = Timestamps.add(executionStart, oneSecond);
  Timestamp outputUploadStart = Timestamps.add(executionCompleted, oneSecond);
  Timestamp outputUploadComplete = Timestamps.add(outputUploadStart, oneSecond);
  ExecutedActionMetadata executedMetadata =
      ExecutedActionMetadata.newBuilder()
          .setWorker("test worker")
          .setQueuedTimestamp(queued)
          .setWorkerStartTimestamp(workerStart)
          .setExecutionStartTimestamp(executionStart)
          .setExecutionCompletedTimestamp(executionCompleted)
          .setOutputUploadStartTimestamp(outputUploadStart)
          .setOutputUploadCompletedTimestamp(outputUploadComplete)
          .build();
  RemoteSpawnRunner.spawnMetricsAccounting(builder, executedMetadata);
  SpawnMetrics spawnMetrics = builder.build();
  // remote queue time is accumulated
  assertThat(spawnMetrics.queueTime()).isEqualTo(Duration.ofSeconds(2));
  // setup time is substituted
  assertThat(spawnMetrics.setupTime()).isEqualTo(Duration.ofSeconds(1));
  // execution time is unspecified, assume substituted
  assertThat(spawnMetrics.executionWallTime()).isEqualTo(Duration.ofSeconds(1));
  // ProcessOutputs time is unspecified, assume substituted
  assertThat(spawnMetrics.processOutputsTime()).isEqualTo(Duration.ofSeconds(1));
}
 
Example #19
Source File: GrpclbLoadBalancerTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
private static LoadBalanceResponse buildInitialResponse(long loadReportIntervalMillis) {
  return LoadBalanceResponse.newBuilder()
      .setInitialResponse(
          InitialLoadBalanceResponse.newBuilder()
              .setClientStatsReportInterval(Durations.fromMillis(loadReportIntervalMillis)))
      .build();
}
 
Example #20
Source File: OrcaOobUtil.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
void start() {
  stopwatch.reset().start();
  call.start(this, new Metadata());
  call.sendMessage(
      OrcaLoadReportRequest.newBuilder()
          .setReportInterval(Durations.fromNanos(overallConfig.getReportIntervalNanos()))
          .build());
  call.halfClose();
  call.request(1);
}
 
Example #21
Source File: TestParameters.java    From pubsub with Apache License 2.0 5 votes vote down vote up
public static Builder builder() {
  return new AutoValue_TestParameters.Builder()
      .setMessageSize(1000)
      .setPublishBatchDuration(Durations.fromMillis(50))
      .setPublishBatchSize(1000)
      .setBurnInDuration(Durations.fromSeconds(5 * 60))
      .setLoadtestDuration(Durations.fromSeconds(10 * 60))
      .setNumPublisherWorkers(1)
      .setNumSubscriberWorkers(1)
      .setSubscriberCpuScaling(5);
}
 
Example #22
Source File: XdsClientImplTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
/**
 * Tests sending a streaming LRS RPC for each cluster to report loads for.
 */
@Test
public void reportLoadStatsToServer() {
  String clusterName = "cluster-foo.googleapis.com";
  LoadStatsStore loadStatsStore = new LoadStatsStoreImpl(clusterName, null);
  ArgumentCaptor<LoadStatsRequest> requestCaptor = ArgumentCaptor.forClass(null);
  xdsClient.reportClientStats(clusterName, null, loadStatsStore);
  LoadReportCall lrsCall = loadReportCalls.poll();
  verify(lrsCall.requestObserver).onNext(requestCaptor.capture());
  assertThat(requestCaptor.getValue().getClusterStatsCount())
      .isEqualTo(0);  // initial request

  lrsCall.responseObserver.onNext(
      LoadStatsResponse.newBuilder()
          .addClusters(clusterName)
          .setLoadReportingInterval(Durations.fromNanos(1000L))
          .build());
  fakeClock.forwardNanos(1000L);
  verify(lrsCall.requestObserver, times(2)).onNext(requestCaptor.capture());
  ClusterStats report = Iterables.getOnlyElement(requestCaptor.getValue().getClusterStatsList());
  assertThat(report.getClusterName()).isEqualTo(clusterName);

  xdsClient.cancelClientStatsReport(clusterName, null);
  fakeClock.forwardNanos(1000L);
  verify(lrsCall.requestObserver, times(3)).onNext(requestCaptor.capture());
  assertThat(requestCaptor.getValue().getClusterStatsCount())
      .isEqualTo(0);  // no more stats reported

  // See more test on LoadReportClientTest.java
}
 
Example #23
Source File: BuildFarmServerTest.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  String uniqueServerName = "in-process server for " + getClass();

  memoryInstanceConfig =
      MemoryInstanceConfig.newBuilder()
          .setListOperationsDefaultPageSize(1024)
          .setListOperationsMaxPageSize(16384)
          .setTreeDefaultPageSize(1024)
          .setTreeMaxPageSize(16384)
          .setOperationPollTimeout(Durations.fromSeconds(10))
          .setOperationCompletedDelay(Durations.fromSeconds(10))
          .setCasConfig(
              ContentAddressableStorageConfig.newBuilder()
                  .setMemory(MemoryCASConfig.newBuilder().setMaxSizeBytes(640 * 1024)))
          .setActionCacheConfig(
              ActionCacheConfig.newBuilder()
                  .setDelegateCas(DelegateCASConfig.getDefaultInstance())
                  .build())
          .setDefaultActionTimeout(Durations.fromSeconds(600))
          .setMaximumActionTimeout(Durations.fromSeconds(3600))
          .build();

  BuildFarmServerConfig.Builder configBuilder = BuildFarmServerConfig.newBuilder().setPort(0);
  configBuilder
      .addInstancesBuilder()
      .setName(INSTANCE_NAME)
      .setDigestFunction(DigestFunction.Value.SHA256)
      .setMemoryInstanceConfig(memoryInstanceConfig);

  server =
      new BuildFarmServer(
          "test",
          InProcessServerBuilder.forName(uniqueServerName).directExecutor(),
          configBuilder.build());
  server.start();
  inProcessChannel = InProcessChannelBuilder.forName(uniqueServerName).directExecutor().build();
}
 
Example #24
Source File: MemoryInstanceTest.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
@Test
public void actionWithExcessiveTimeoutFailsValidation()
    throws InterruptedException, InvalidProtocolBufferException {
  Duration timeout = Durations.fromSeconds(9000);
  Digest actionDigestWithExcessiveTimeout = createAction(Action.newBuilder().setTimeout(timeout));

  Watcher watcher = mock(Watcher.class);
  IllegalStateException timeoutInvalid = null;
  instance.execute(
      actionDigestWithExcessiveTimeout,
      /* skipCacheLookup=*/ true,
      ExecutionPolicy.getDefaultInstance(),
      ResultsCachePolicy.getDefaultInstance(),
      RequestMetadata.getDefaultInstance(),
      watcher);
  ArgumentCaptor<Operation> watchCaptor = ArgumentCaptor.forClass(Operation.class);
  verify(watcher, times(1)).observe(watchCaptor.capture());
  Operation watchOperation = watchCaptor.getValue();
  assertThat(watchOperation.getResponse().is(ExecuteResponse.class)).isTrue();
  Status status = watchOperation.getResponse().unpack(ExecuteResponse.class).getStatus();
  assertThat(status.getCode()).isEqualTo(Code.FAILED_PRECONDITION.getNumber());
  assertThat(status.getDetailsCount()).isEqualTo(1);
  assertThat(status.getDetails(0).is(PreconditionFailure.class)).isTrue();
  PreconditionFailure preconditionFailure =
      status.getDetails(0).unpack(PreconditionFailure.class);
  assertThat(preconditionFailure.getViolationsList())
      .contains(
          Violation.newBuilder()
              .setType(VIOLATION_TYPE_INVALID)
              .setSubject(
                  Durations.toString(timeout)
                      + " > "
                      + Durations.toString(MAXIMUM_ACTION_TIMEOUT))
              .setDescription(TIMEOUT_OUT_OF_BOUNDS)
              .build());
}
 
Example #25
Source File: MemoryInstanceTest.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  outstandingOperations = new MemoryInstance.OutstandingOperations();
  watchers =
      synchronizedSetMultimap(
          MultimapBuilder.hashKeys().hashSetValues(/* expectedValuesPerKey=*/ 1).build());
  watcherService = newDirectExecutorService();
  MemoryInstanceConfig memoryInstanceConfig =
      MemoryInstanceConfig.newBuilder()
          .setListOperationsDefaultPageSize(1024)
          .setListOperationsMaxPageSize(16384)
          .setTreeDefaultPageSize(1024)
          .setTreeMaxPageSize(16384)
          .setOperationPollTimeout(Durations.fromSeconds(10))
          .setOperationCompletedDelay(Durations.fromSeconds(10))
          .setDefaultActionTimeout(Durations.fromSeconds(600))
          .setMaximumActionTimeout(MAXIMUM_ACTION_TIMEOUT)
          .setActionCacheConfig(
              ActionCacheConfig.newBuilder()
                  .setDelegateCas(DelegateCASConfig.getDefaultInstance())
                  .build())
          .build();

  storage = Maps.newHashMap();
  workers = Lists.newArrayList();
  requeuers = Maps.newHashMap();
  operationTimeoutDelays = Maps.newHashMap();
  instance =
      new MemoryInstance(
          "memory",
          DIGEST_UTIL,
          memoryInstanceConfig,
          casMapDecorator(storage),
          watchers,
          watcherService,
          outstandingOperations,
          workers,
          requeuers,
          operationTimeoutDelays);
}
 
Example #26
Source File: ChannelzProtoUtil.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
static SocketOption toSocketOptionTimeout(String name, int timeoutMillis) {
  Preconditions.checkNotNull(name);
  return SocketOption
      .newBuilder()
      .setName(name)
      .setAdditional(
          Any.pack(
              SocketOptionTimeout
                  .newBuilder()
                  .setDuration(Durations.fromMillis(timeoutMillis))
                  .build()))
      .build();
}
 
Example #27
Source File: DurationDeserializer.java    From jackson-datatype-protobuf with Apache License 2.0 5 votes vote down vote up
@Override
public Duration deserialize(JsonParser parser, DeserializationContext context) throws IOException {
  switch (parser.getCurrentToken()) {
    case VALUE_STRING:
      try {
        return Durations.parse(parser.getText());
      } catch (ParseException e) {
        throw context.weirdStringException(parser.getText(), Duration.class, e.getMessage());
      }
    default:
      context.reportWrongTokenException(Duration.class, JsonToken.VALUE_STRING, wrongTokenMessage(context));
      // the previous method should have thrown
      throw new AssertionError();
  }
}
 
Example #28
Source File: GrpclbLoadBalancerTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
private static LoadBalanceResponse buildInitialResponse(long loadReportIntervalMillis) {
  return LoadBalanceResponse.newBuilder()
      .setInitialResponse(
          InitialLoadBalanceResponse.newBuilder()
          .setClientStatsReportInterval(Durations.fromMillis(loadReportIntervalMillis)))
      .build();
}
 
Example #29
Source File: DurationSerializer.java    From jackson-datatype-protobuf with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(
        Duration duration,
        JsonGenerator generator,
        SerializerProvider serializerProvider
) throws IOException {
  generator.writeString(Durations.toString(duration));
}
 
Example #30
Source File: LoadReportClientTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
LoadStatsRequestMatcher(Collection<ClusterStats> clusterStats, long expectedIntervalNano) {
  for (ClusterStats stats : clusterStats) {
    ClusterStats statsWithInterval =
        stats.toBuilder()
            .setLoadReportInterval(Durations.fromNanos(expectedIntervalNano))
            .build();
    expectedStats.put(statsWithInterval.getClusterName(), statsWithInterval);
  }
}