.setElementCountThreshold(10L) .setRequestByteThreshold(1024L) .setDelayThreshold(Duration.ofMillis(10)) .setFlowControlSettings( FlowControlSettings.newBuilder()
.setElementCountThreshold(1000L) .setRequestByteThreshold(1048576L) .setDelayThreshold(Duration.ofMillis(50)) .setFlowControlSettings( FlowControlSettings.newBuilder()
@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); } }
public static GooglePubsubPublisher buildPublisher(GooglePubsubPublisherConfig config, ObjectMapper mapper) { GooglePubsubPublisher publisher = new GooglePubsubPublisher(); publisher.setName(config.getName()); ProjectTopicName fullName = ProjectTopicName.of(config.getProject(), config.getTopicName()); publisher.setTopicName(config.getTopicName()); publisher.setFullTopicName(fullName.toString()); publisher.setContent(config.getContent()); publisher.setMapper(mapper); BatchingSettings batchingSettings = BatchingSettings.newBuilder() .setElementCountThreshold(config.getBatchCountThreshold()) .setDelayThreshold(Duration.ofMillis(config.getDelayMillisecondsThreshold())) .build(); try { Publisher p = Publisher.newBuilder(fullName) .setCredentialsProvider(new GooglePubsubCredentialsProvider(config.getJsonPath())) .setBatchingSettings(batchingSettings) .build(); publisher.setPublisher(p); } catch (IOException ioe) { log.error("Could not create Google Pubsub Publishers: {}", ioe); } return publisher; }
private CPSPublisherTask(StartRequest request) { super(request, "gcloud", MetricsHandler.MetricName.PUBLISH_ACK_LATENCY); try { this.publisher = Publisher.defaultBuilder(TopicName.create(request.getProject(), request.getTopic())) .setBatchingSettings( BatchingSettings.newBuilder() .setElementCountThreshold(950L) .setRequestByteThreshold(9500000L) .setDelayThreshold( Duration.ofMillis(Durations.toMillis(request.getPublishBatchDuration()))) .build()) .build(); } catch (Exception e) { throw new RuntimeException(e); } this.payload = ByteString.copyFromUtf8(LoadTestRunner.createMessage(request.getMessageSize())); this.batchSize = request.getPublishBatchSize(); this.messageSize = request.getMessageSize(); this.id = (new Random()).nextInt(); }
@Test public void batching() throws Exception { BatchingSettings batchingSettings = BatchingSettings.newBuilder() .setDelayThreshold(Duration.ofSeconds(1)) .setElementCountThreshold(2L) .build(); BatchingCallSettings<LabeledIntList, List<Integer>> batchingCallSettings = BatchingCallSettings.newBuilder(SQUARER_BATCHING_DESC) .setBatchingSettings(batchingSettings) .build(); UnaryCallable<LabeledIntList, List<Integer>> callable = FakeCallableFactory.createBatchingCallable( callLabeledIntSquarer, batchingCallSettings, clientContext); ApiFuture<List<Integer>> f1 = callable.futureCall(new LabeledIntList("one", 1, 2)); ApiFuture<List<Integer>> f2 = callable.futureCall(new LabeledIntList("one", 3, 4)); Truth.assertThat(f1.get()).isEqualTo(Arrays.asList(1, 4)); Truth.assertThat(f2.get()).isEqualTo(Arrays.asList(9, 16)); }
public void batchingWithBlockingCallThreshold() throws Exception { BatchingSettings batchingSettings = BatchingSettings.newBuilder() .setDelayThreshold(Duration.ofSeconds(1)) .setElementCountThreshold(2L) .build(); BatchingCallSettings<LabeledIntList, List<Integer>> batchingCallSettings = BatchingCallSettings.newBuilder(SQUARER_BATCHING_DESC) .setBatchingSettings(batchingSettings) .build(); UnaryCallable<LabeledIntList, List<Integer>> callable = FakeCallableFactory.createBatchingCallable( callLabeledIntSquarer, batchingCallSettings, clientContext); ApiFuture<List<Integer>> f1 = callable.futureCall(new LabeledIntList("one", 1)); ApiFuture<List<Integer>> f2 = callable.futureCall(new LabeledIntList("one", 3)); Truth.assertThat(f1.get()).isEqualTo(Arrays.asList(1)); Truth.assertThat(f2.get()).isEqualTo(Arrays.asList(9)); }
@Bean @ConditionalOnMissingBean(name = "publisherBatchSettings") public BatchingSettings publisherBatchSettings() { BatchingSettings.Builder builder = BatchingSettings.newBuilder(); GcpPubSubProperties.Batching batching = this.gcpPubSubProperties.getPublisher() .getBatching(); FlowControlSettings flowControlSettings = buildFlowControlSettings(batching.getFlowControl()); if (flowControlSettings != null) { builder.setFlowControlSettings(flowControlSettings); } return ifNotNull(batching.getDelayThresholdSeconds(), (x) -> builder.setDelayThreshold(Duration.ofSeconds(x))) .apply(ifNotNull(batching.getElementCountThreshold(), builder::setElementCountThreshold) .apply(ifNotNull(batching.getEnabled(), builder::setIsEnabled) .apply(ifNotNull(batching.getRequestByteThreshold(), builder::setRequestByteThreshold) .apply(false)))) ? builder.build() : null; }
/** Get a new builder. */ public static Builder newBuilder() { return new AutoValue_BatchingSettings.Builder() .setIsEnabled(true) .setElementCountThreshold(1L) .setRequestByteThreshold(1L) .setDelayThreshold(Duration.ofMillis(1)) .setFlowControlSettings( FlowControlSettings.newBuilder() .setLimitExceededBehavior(LimitExceededBehavior.Ignore) .build()); }
/** Get a new builder. */ public static Builder newBuilder() { return new AutoValue_BatchingSettings.Builder() .setIsEnabled(true) .setElementCountThreshold(1L) .setRequestByteThreshold(1L) .setDelayThreshold(Duration.ofMillis(1)) .setFlowControlSettings( FlowControlSettings.newBuilder() .setLimitExceededBehavior(LimitExceededBehavior.Ignore) .build()); }
Publisher.Builder.DEFAULT_BATCHING_SETTINGS .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) { Publisher.Builder.DEFAULT_BATCHING_SETTINGS .toBuilder() .setDelayThreshold(Duration.ofMillis(-1)) .build()); fail("Should have thrown an IllegalArgumentException");
.getBatchingSettings() .toBuilder() .setDelayThreshold(FLUSH_PERIOD) .setElementCountThreshold(FLUSH_COUNT) .build());
.toBuilder() .setElementCountThreshold(2L) .setDelayThreshold(Duration.ofSeconds(5)) .build()) .build();
@Test public void testSinglePublishByNumBytes() throws Exception { Publisher publisher = getTestPublisherBuilder() .setBatchingSettings( Publisher.Builder.DEFAULT_BATCHING_SETTINGS .toBuilder() .setElementCountThreshold(2L) .setDelayThreshold(Duration.ofSeconds(100)) .build()) .build(); testPublisherServiceImpl .addPublishResponse(PublishResponse.newBuilder().addMessageIds("1").addMessageIds("2")) .addPublishResponse(PublishResponse.newBuilder().addMessageIds("3").addMessageIds("4")); ApiFuture<String> publishFuture1 = sendTestMessage(publisher, "A"); ApiFuture<String> publishFuture2 = sendTestMessage(publisher, "B"); ApiFuture<String> publishFuture3 = sendTestMessage(publisher, "C"); // Note we are not advancing time but message should still get published assertEquals("1", publishFuture1.get()); assertEquals("2", publishFuture2.get()); assertFalse(publishFuture3.isDone()); ApiFuture<String> publishFuture4 = sendTestMessage(publisher, "D"); assertEquals("3", publishFuture3.get()); assertEquals("4", publishFuture4.get()); assertEquals(2, testPublisherServiceImpl.getCapturedRequests().size()); publisher.shutdown(); publisher.awaitTermination(1, TimeUnit.MINUTES); }
@Test public void testPublishByNumBatchedMessages() throws Exception { Publisher publisher = getTestPublisherBuilder() .setBatchingSettings( Publisher.Builder.DEFAULT_BATCHING_SETTINGS .toBuilder() .setElementCountThreshold(2L) .setDelayThreshold(Duration.ofSeconds(100)) .build()) .build(); testPublisherServiceImpl .addPublishResponse(PublishResponse.newBuilder().addMessageIds("1").addMessageIds("2")) .addPublishResponse(PublishResponse.newBuilder().addMessageIds("3").addMessageIds("4")); ApiFuture<String> publishFuture1 = sendTestMessage(publisher, "A"); ApiFuture<String> publishFuture2 = sendTestMessage(publisher, "B"); ApiFuture<String> publishFuture3 = sendTestMessage(publisher, "C"); // Note we are not advancing time but message should still get published assertEquals("1", publishFuture1.get()); assertEquals("2", publishFuture2.get()); assertFalse(publishFuture3.isDone()); ApiFuture<String> publishFuture4 = publisher.publish(PubsubMessage.newBuilder().setData(ByteString.copyFromUtf8("D")).build()); assertEquals("3", publishFuture3.get()); assertEquals("4", publishFuture4.get()); assertEquals(2, testPublisherServiceImpl.getCapturedRequests().get(0).getMessagesCount()); assertEquals(2, testPublisherServiceImpl.getCapturedRequests().get(1).getMessagesCount()); publisher.shutdown(); publisher.awaitTermination(1, TimeUnit.MINUTES); }
@Test(expected = ExecutionException.class) public void testPublishFailureRetries_nonRetryableFailsImmediately() throws Exception { Publisher publisher = getTestPublisherBuilder() .setExecutorProvider(SINGLE_THREAD_EXECUTOR) .setRetrySettings( Publisher.Builder.DEFAULT_RETRY_SETTINGS .toBuilder() .setTotalTimeout(Duration.ofSeconds(10)) .build()) .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 StatusException(Status.INVALID_ARGUMENT)); ApiFuture<String> publishFuture1 = sendTestMessage(publisher, "A"); try { publishFuture1.get(); } finally { assertTrue(testPublisherServiceImpl.getCapturedRequests().size() >= 1); publisher.shutdown(); publisher.awaitTermination(1, TimeUnit.MINUTES); } }
@Test public void testPublishByDuration() throws Exception { Publisher publisher = getTestPublisherBuilder() // To demonstrate that reaching duration will trigger publish .setBatchingSettings( Publisher.Builder.DEFAULT_BATCHING_SETTINGS .toBuilder() .setDelayThreshold(Duration.ofSeconds(5)) .setElementCountThreshold(10L) .build()) .build(); testPublisherServiceImpl.addPublishResponse( PublishResponse.newBuilder().addMessageIds("1").addMessageIds("2")); ApiFuture<String> publishFuture1 = sendTestMessage(publisher, "A"); ApiFuture<String> publishFuture2 = sendTestMessage(publisher, "B"); assertFalse(publishFuture1.isDone()); assertFalse(publishFuture2.isDone()); fakeExecutor.advanceTime(Duration.ofSeconds(10)); assertEquals("1", publishFuture1.get()); assertEquals("2", publishFuture2.get()); assertEquals(2, testPublisherServiceImpl.getCapturedRequests().get(0).getMessagesCount()); publisher.shutdown(); publisher.awaitTermination(1, TimeUnit.MINUTES); }
@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); }
public Publisher getPublisherWithCustomBatchSettings(ProjectTopicName topicName) throws Exception { // [START pubsub_publisher_batch_settings] // Batch settings control how the publisher batches messages long requestBytesThreshold = 5000L; // default : 1 byte long messageCountBatchSize = 10L; // default : 1 message Duration publishDelayThreshold = Duration.ofMillis(100); // default : 1 ms // Publish request get triggered based on request size, messages count & time since last publish BatchingSettings batchingSettings = BatchingSettings.newBuilder() .setElementCountThreshold(messageCountBatchSize) .setRequestByteThreshold(requestBytesThreshold) .setDelayThreshold(publishDelayThreshold) .build(); Publisher publisher = Publisher.newBuilder(topicName).setBatchingSettings(batchingSettings).build(); // [END pubsub_publisher_batch_settings] return publisher; }
@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); }