protected Builder(ClientContext clientContext) { super(clientContext); deleteLogSettings = UnaryCallSettings.newUnaryCallSettingsBuilder(); writeLogEntriesSettings = BatchingCallSettings.newBuilder(WRITE_LOG_ENTRIES_BATCHING_DESC) .setBatchingSettings(BatchingSettings.newBuilder().build()); listLogEntriesSettings = PagedCallSettings.newBuilder(LIST_LOG_ENTRIES_PAGE_STR_FACT); listMonitoredResourceDescriptorsSettings = PagedCallSettings.newBuilder(LIST_MONITORED_RESOURCE_DESCRIPTORS_PAGE_STR_FACT); listLogsSettings = PagedCallSettings.newBuilder(LIST_LOGS_PAGE_STR_FACT); unaryMethodSettingsBuilders = ImmutableList.<UnaryCallSettings.Builder<?, ?>>of( deleteLogSettings, writeLogEntriesSettings, listLogEntriesSettings, listMonitoredResourceDescriptorsSettings, listLogsSettings); initDefaults(this); }
@Test public void testPublisherGetters() throws Exception { Publisher.Builder builder = Publisher.newBuilder(TEST_TOPIC); builder.setChannelProvider(TEST_CHANNEL_PROVIDER); builder.setExecutorProvider(SINGLE_THREAD_EXECUTOR); builder.setBatchingSettings( BatchingSettings.newBuilder() .setRequestByteThreshold(10L) .setDelayThreshold(Duration.ofMillis(11)) .setElementCountThreshold(12L) .build()); builder.setCredentialsProvider(NoCredentialsProvider.create()); Publisher publisher = builder.build(); assertEquals(TEST_TOPIC, publisher.getTopicName()); assertEquals(10, (long) publisher.getBatchingSettings().getRequestByteThreshold()); assertEquals(Duration.ofMillis(11), publisher.getBatchingSettings().getDelayThreshold()); assertEquals(12, (long) publisher.getBatchingSettings().getElementCountThreshold()); publisher.shutdown(); publisher.awaitTermination(1, TimeUnit.MINUTES); }
public Builder setBatchingSettings(BatchingSettings batchingSettings) { Preconditions.checkNotNull(batchingSettings); Preconditions.checkNotNull(batchingSettings.getElementCountThreshold()); Preconditions.checkArgument(batchingSettings.getElementCountThreshold() > 0); Preconditions.checkNotNull(batchingSettings.getRequestByteThreshold()); Preconditions.checkArgument(batchingSettings.getRequestByteThreshold() > 0); Preconditions.checkNotNull(batchingSettings.getDelayThreshold()); Preconditions.checkArgument(batchingSettings.getDelayThreshold().toMillis() > 0); this.batchingSettings = batchingSettings; return this; }
@Test public void batchingWithFlowControl() throws Exception { BatchingSettings batchingSettings = BatchingSettings.newBuilder() .setDelayThreshold(Duration.ofSeconds(1)) .setElementCountThreshold(4L) .build(); TrackedFlowController trackedFlowController = new TrackedFlowController(batchingSettings.getFlowControlSettings());
private ImmutableList<BatchingThreshold<Batch<RequestT, ResponseT>>> getThresholds( BatchingSettings batchingSettings) { ImmutableList.Builder<BatchingThreshold<Batch<RequestT, ResponseT>>> listBuilder = ImmutableList.builder(); if (batchingSettings.getElementCountThreshold() != null) { ElementCounter<Batch<RequestT, ResponseT>> elementCounter = new BatchElementCounter<>(batchingDescriptor); BatchingThreshold<Batch<RequestT, ResponseT>> countThreshold = new NumericThreshold<>(batchingSettings.getElementCountThreshold(), elementCounter); listBuilder.add(countThreshold); } if (batchingSettings.getRequestByteThreshold() != null) { ElementCounter<Batch<RequestT, ResponseT>> requestByteCounter = new BatchByteCounter<RequestT, ResponseT>(); BatchingThreshold<Batch<RequestT, ResponseT>> byteThreshold = new NumericThreshold<>(batchingSettings.getRequestByteThreshold(), requestByteCounter); listBuilder.add(byteThreshold); } return listBuilder.build(); } }
.setBatchingSettings( oldBatchSettings .toBuilder() .setFlowControlSettings( oldBatchSettings .getFlowControlSettings() .toBuilder() .setLimitExceededBehavior(LimitExceededBehavior.Block)
@Test public void testErrorPropagation() throws Exception { Publisher publisher = getTestPublisherBuilder() .setExecutorProvider(SINGLE_THREAD_EXECUTOR) .setBatchingSettings( Publisher.Builder.DEFAULT_BATCHING_SETTINGS .toBuilder() .setElementCountThreshold(1L) .setDelayThreshold(Duration.ofSeconds(5)) .build()) .build(); testPublisherServiceImpl.addPublishError(Status.DATA_LOSS.asException()); try { sendTestMessage(publisher, "A").get(); fail("should throw exception"); } catch (ExecutionException e) { assertThat(e.getCause()).isInstanceOf(DataLossException.class); } }
private void setupDurationBasedPublishAlarm() { if (!activeAlarm.getAndSet(true)) { long delayThresholdMs = getBatchingSettings().getDelayThreshold().toMillis(); logger.log(Level.FINER, "Setting up alarm for the next {0} ms.", delayThresholdMs); currentAlarmFuture = executor.schedule( new Runnable() { @Override public void run() { logger.log(Level.FINER, "Sending messages based on schedule."); activeAlarm.getAndSet(false); publishAllOutstanding(); } }, delayThresholdMs, TimeUnit.MILLISECONDS); } }
private BatchingCallSettings(Builder<RequestT, ResponseT> builder) { super(builder); this.batchingDescriptor = builder.batchingDescriptor; this.batchingSettings = Preconditions.checkNotNull(builder.batchingSettings); FlowController flowControllerToUse = builder.flowController; if (flowControllerToUse == null) { flowControllerToUse = new FlowController(batchingSettings.getFlowControlSettings()); } this.flowController = flowControllerToUse; }
private long getMaxBatchBytes() { return getBatchingSettings().getRequestByteThreshold(); }
if (messagesBatch.size() == getBatchingSettings().getElementCountThreshold()) { batchToSend = new OutstandingBatch(messagesBatch, batchedBytes); messagesBatch = new LinkedList<>();
@Test public void testBuilderParametersAndDefaults() { Publisher.Builder builder = Publisher.newBuilder(TEST_TOPIC); assertEquals(TEST_TOPIC.toString(), builder.topicName); assertEquals(Publisher.Builder.DEFAULT_EXECUTOR_PROVIDER, builder.executorProvider); assertEquals( Publisher.Builder.DEFAULT_REQUEST_BYTES_THRESHOLD, builder.batchingSettings.getRequestByteThreshold().longValue()); assertEquals( Publisher.Builder.DEFAULT_DELAY_THRESHOLD, builder.batchingSettings.getDelayThreshold()); assertEquals( Publisher.Builder.DEFAULT_ELEMENT_COUNT_THRESHOLD, builder.batchingSettings.getElementCountThreshold().longValue()); assertEquals(Publisher.Builder.DEFAULT_RETRY_SETTINGS, builder.retrySettings); }
private ImmutableList<BatchingThreshold<Batch<RequestT, ResponseT>>> getThresholds( BatchingSettings batchingSettings) { ImmutableList.Builder<BatchingThreshold<Batch<RequestT, ResponseT>>> listBuilder = ImmutableList.builder(); if (batchingSettings.getElementCountThreshold() != null) { ElementCounter<Batch<RequestT, ResponseT>> elementCounter = new BatchElementCounter<>(batchingDescriptor); BatchingThreshold<Batch<RequestT, ResponseT>> countThreshold = new NumericThreshold<>(batchingSettings.getElementCountThreshold(), elementCounter); listBuilder.add(countThreshold); } if (batchingSettings.getRequestByteThreshold() != null) { ElementCounter<Batch<RequestT, ResponseT>> requestByteCounter = new BatchByteCounter<RequestT, ResponseT>(); BatchingThreshold<Batch<RequestT, ResponseT>> byteThreshold = new NumericThreshold<>(batchingSettings.getRequestByteThreshold(), requestByteCounter); listBuilder.add(byteThreshold); } return listBuilder.build(); } }
.setBatchingSettings( oldBatchSettings .toBuilder() .setFlowControlSettings( oldBatchSettings .getFlowControlSettings() .toBuilder() .setLimitExceededBehavior(LimitExceededBehavior.Block)
@Test public void testPublishFailureRetries() throws Exception { Publisher publisher = getTestPublisherBuilder() .setExecutorProvider(SINGLE_THREAD_EXECUTOR) .setBatchingSettings( Publisher.Builder.DEFAULT_BATCHING_SETTINGS .toBuilder() .setElementCountThreshold(1L) .setDelayThreshold(Duration.ofSeconds(5)) .build()) .build(); // To demonstrate that reaching duration will trigger publish testPublisherServiceImpl.addPublishError(new Throwable("Transiently failing")); testPublisherServiceImpl.addPublishResponse(PublishResponse.newBuilder().addMessageIds("1")); ApiFuture<String> publishFuture1 = sendTestMessage(publisher, "A"); assertEquals("1", publishFuture1.get()); assertEquals(2, testPublisherServiceImpl.getCapturedRequests().size()); publisher.shutdown(); publisher.awaitTermination(1, TimeUnit.MINUTES); }
private ThresholdBatcher<Batch<RequestT, ResponseT>> createBatcher(PartitionKey partitionKey) { BatchExecutor<RequestT, ResponseT> processor = new BatchExecutor<>(batchingDescriptor, partitionKey); BatchingFlowController<Batch<RequestT, ResponseT>> batchingFlowController = new BatchingFlowController<>( flowController, new BatchElementCounter<>(batchingDescriptor), new BatchByteCounter<RequestT, ResponseT>()); return ThresholdBatcher.<Batch<RequestT, ResponseT>>newBuilder() .setThresholds(getThresholds(batchingSettings)) .setExecutor(executor) .setMaxDelay(batchingSettings.getDelayThreshold()) .setReceiver(processor) .setFlowController(batchingFlowController) .setBatchMerger(new BatchMergerImpl<RequestT, ResponseT>()) .build(); }
private BatchingCallSettings(Builder<RequestT, ResponseT> builder) { super(builder); this.batchingDescriptor = builder.batchingDescriptor; this.batchingSettings = Preconditions.checkNotNull(builder.batchingSettings); FlowController flowControllerToUse = builder.flowController; if (flowControllerToUse == null) { flowControllerToUse = new FlowController(batchingSettings.getFlowControlSettings()); } this.flowController = flowControllerToUse; }
.publishSettings() .setBatchingSettings( BatchingSettings.newBuilder() .setElementCountThreshold(10L) .setRequestByteThreshold(1024L)
/** Build the BatchingSettings object. */ public BatchingSettings build() { BatchingSettings settings = autoBuild(); Preconditions.checkArgument( settings.getElementCountThreshold() == null || settings.getElementCountThreshold() > 0, "elementCountThreshold must be either unset or positive"); Preconditions.checkArgument( settings.getRequestByteThreshold() == null || settings.getRequestByteThreshold() > 0, "requestByteThreshold must be either unset or positive"); Preconditions.checkArgument( settings.getDelayThreshold() == null || settings.getDelayThreshold().compareTo(Duration.ZERO) > 0, "delayThreshold must be either unset or positive"); return settings; } }
builder.setBatchingSettings( Publisher.Builder.DEFAULT_BATCHING_SETTINGS .toBuilder() .setRequestByteThreshold((Long) null) .build()); builder.setBatchingSettings( Publisher.Builder.DEFAULT_BATCHING_SETTINGS .toBuilder() .setRequestByteThreshold(0L) .build()); builder.setBatchingSettings( Publisher.Builder.DEFAULT_BATCHING_SETTINGS .toBuilder() .setRequestByteThreshold(-1L) .build()); .toBuilder() .setDelayThreshold(Duration.ofMillis(1)) .build()); try { builder.setBatchingSettings( Publisher.Builder.DEFAULT_BATCHING_SETTINGS.toBuilder().setDelayThreshold(null).build()); fail("Should have thrown an NullPointerException"); } catch (NullPointerException expected) { builder.setBatchingSettings( Publisher.Builder.DEFAULT_BATCHING_SETTINGS