Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publish pending batches before sending requested message #4578

Merged
merged 1 commit into from
Mar 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -269,18 +269,18 @@ && hasBatchingBytes()

if (batchToSend != null) {
logger.log(Level.FINER, "Scheduling a batch for immediate sending.");
publishOutstandingBatch(batchToSend);
publishAllOutstanding();
publishOutstandingBatch(batchToSend);
}

// If the message is over the size limit, it was not added to the pending messages and it will
// be sent in its own batch immediately.
if (hasBatchingBytes() && messageSize >= getMaxBatchBytes()) {
logger.log(
Level.FINER, "Message exceeds the max batch bytes, scheduling it for immediate send.");
publishAllOutstanding();
publishOutstandingBatch(
new OutstandingBatch(ImmutableList.of(outstandingPublish), messageSize, orderingKey));
publishAllOutstanding();
}

return publishResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,39 @@ public void testBatchedMessagesWithOrderingKeyByDuration() throws Exception {
publisher.shutdown();
}

@Test
public void testLargeMessagesDoNotReorderBatches() throws Exception {
// Set the maximum batching size to 20 bytes.
Publisher publisher =
getTestPublisherBuilder()
.setBatchingSettings(
Publisher.Builder.DEFAULT_BATCHING_SETTINGS
.toBuilder()
.setElementCountThreshold(10L)
.setRequestByteThreshold(20L)
.setDelayThreshold(Duration.ofSeconds(100))
.build())
.setEnableMessageOrdering(true)
.build();
testPublisherServiceImpl.setAutoPublishResponse(true);
ApiFuture<String> publishFuture1 = sendTestMessageWithOrderingKey(publisher, "m1", "OrderA");
ApiFuture<String> publishFuture2 = sendTestMessageWithOrderingKey(publisher, "m2", "OrderB");

assertFalse(publishFuture1.isDone());
assertFalse(publishFuture2.isDone());

ApiFuture<String> publishFuture3 =
sendTestMessageWithOrderingKey(publisher, "VeryLargeMessage", "OrderB");
// Verify that messages with "OrderB" were delivered in order.
assertTrue(Integer.parseInt(publishFuture2.get()) < Integer.parseInt(publishFuture3.get()));

assertTrue(publishFuture1.isDone());
assertTrue(publishFuture2.isDone());
assertTrue(publishFuture3.isDone());

publisher.shutdown();
}

@Test
public void testOrderingKeyWhenDisabled_throwsException() throws Exception {
// Message ordering is disabled by default.
Expand Down