-
Notifications
You must be signed in to change notification settings - Fork 340
Enable Pub/Sub message ordering for publishing #408
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ | |
import com.google.cloud.spring.pubsub.core.PubSubTemplate; | ||
import com.google.cloud.spring.pubsub.integration.outbound.PubSubMessageHandler; | ||
import com.google.cloud.spring.pubsub.support.AcknowledgeablePubsubMessage; | ||
import com.google.cloud.spring.pubsub.support.GcpPubSubHeaders; | ||
import com.google.cloud.spring.pubsub.support.converter.ConvertedAcknowledgeablePubsubMessage; | ||
import com.google.cloud.spring.pubsub.support.converter.JacksonPubSubMessageConverter; | ||
import com.google.cloud.spring.pubsub.support.converter.PubSubMessageConverter; | ||
|
@@ -65,7 +66,8 @@ | |
public class PubSubTemplateDocumentationIntegrationTests { | ||
|
||
private ApplicationContextRunner contextRunner = new ApplicationContextRunner() | ||
.withPropertyValues("spring.cloud.gcp.pubsub.subscriber.max-ack-extension-period=0") | ||
.withPropertyValues("spring.cloud.gcp.pubsub.subscriber.max-ack-extension-period=0", | ||
"spring.cloud.gcp.pubsub.publisher.enable-message-ordering=true") | ||
.withConfiguration(AutoConfigurations.of(GcpContextAutoConfiguration.class, | ||
GcpPubSubAutoConfiguration.class)); | ||
|
||
|
@@ -93,6 +95,35 @@ public void testCreatePublishPullNextAndDelete() { | |
}); | ||
} | ||
|
||
@Test | ||
public void testCreatePublishPullNextAndDelete_ordering() { | ||
pubSubTest((AssertableApplicationContext context, PubSubTemplate pubSubTemplate, String subscriptionName, String topicName) -> { | ||
//tag::publish_ordering[] | ||
Map<String, String> headers = Collections.singletonMap(GcpPubSubHeaders.ORDERING_KEY, "key1"); | ||
pubSubTemplate.publish(topicName, "message1", headers).get(); | ||
pubSubTemplate.publish(topicName, "message2", headers).get(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add a comment to explain that messages with the same ordering key will be delivered in the same order they were sent. |
||
//end::publish_ordering[] | ||
|
||
// message1 | ||
await().atMost(Duration.ofSeconds(30)).untilAsserted(() -> { | ||
PubsubMessage pubsubMessage = pubSubTemplate.pullNext(subscriptionName); | ||
|
||
assertThat(pubsubMessage).isNotNull(); | ||
assertThat(pubsubMessage.getData()).isEqualTo(ByteString.copyFromUtf8("message1")); | ||
assertThat(pubsubMessage.getAttributesCount()).isZero(); | ||
}); | ||
|
||
// message2 | ||
await().atMost(Duration.ofSeconds(30)).untilAsserted(() -> { | ||
PubsubMessage pubsubMessage = pubSubTemplate.pullNext(subscriptionName); | ||
|
||
assertThat(pubsubMessage).isNotNull(); | ||
assertThat(pubsubMessage.getData()).isEqualTo(ByteString.copyFromUtf8("message2")); | ||
assertThat(pubsubMessage.getAttributesCount()).isZero(); | ||
}); | ||
}); | ||
} | ||
|
||
|
||
private void pubSubTest(PubSubTest pubSubTest, Class... configClass) { | ||
ApplicationContextRunner contextRunner = configClass.length == 0 ? this.contextRunner | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,6 +61,8 @@ public class DefaultPublisherFactory implements PublisherFactory { | |
|
||
private BatchingSettings batchingSettings; | ||
|
||
private Boolean enableMessageOrdering; | ||
|
||
/** | ||
* Create {@link DefaultPublisherFactory} instance based on the provided {@link GcpProjectIdProvider}. | ||
* <p>The {@link GcpProjectIdProvider} must not be null, neither provide an empty {@code projectId}. | ||
|
@@ -123,36 +125,22 @@ public void setBatchingSettings(BatchingSettings batchingSettings) { | |
this.batchingSettings = batchingSettings; | ||
} | ||
|
||
/** | ||
* Set whether message ordering should be enabled on the publisher. | ||
* @param enableMessageOrdering whether to enable message ordering | ||
*/ | ||
public void setEnableMessageOrdering(Boolean enableMessageOrdering) { | ||
this.enableMessageOrdering = enableMessageOrdering; | ||
} | ||
|
||
@Override | ||
public Publisher createPublisher(String topic) { | ||
return this.publishers.computeIfAbsent(topic, key -> { | ||
try { | ||
Publisher.Builder publisherBuilder = | ||
Publisher.newBuilder(PubSubTopicUtils.toTopicName(topic, this.projectId)); | ||
|
||
if (this.executorProvider != null) { | ||
publisherBuilder.setExecutorProvider(this.executorProvider); | ||
} | ||
|
||
if (this.channelProvider != null) { | ||
publisherBuilder.setChannelProvider(this.channelProvider); | ||
} | ||
|
||
if (this.credentialsProvider != null) { | ||
publisherBuilder.setCredentialsProvider(this.credentialsProvider); | ||
} | ||
|
||
if (this.headerProvider != null) { | ||
publisherBuilder.setHeaderProvider(this.headerProvider); | ||
} | ||
|
||
if (this.retrySettings != null) { | ||
publisherBuilder.setRetrySettings(this.retrySettings); | ||
} | ||
|
||
if (this.batchingSettings != null) { | ||
publisherBuilder.setBatchingSettings(this.batchingSettings); | ||
} | ||
applyPublisherSettings(publisherBuilder); | ||
|
||
return publisherBuilder.build(); | ||
} | ||
|
@@ -163,6 +151,36 @@ public Publisher createPublisher(String topic) { | |
}); | ||
} | ||
|
||
void applyPublisherSettings(Publisher.Builder publisherBuilder) { | ||
if (this.executorProvider != null) { | ||
publisherBuilder.setExecutorProvider(this.executorProvider); | ||
} | ||
|
||
if (this.channelProvider != null) { | ||
publisherBuilder.setChannelProvider(this.channelProvider); | ||
} | ||
|
||
if (this.credentialsProvider != null) { | ||
publisherBuilder.setCredentialsProvider(this.credentialsProvider); | ||
} | ||
|
||
if (this.headerProvider != null) { | ||
publisherBuilder.setHeaderProvider(this.headerProvider); | ||
} | ||
|
||
if (this.retrySettings != null) { | ||
publisherBuilder.setRetrySettings(this.retrySettings); | ||
} | ||
|
||
if (this.batchingSettings != null) { | ||
publisherBuilder.setBatchingSettings(this.batchingSettings); | ||
} | ||
|
||
if (this.enableMessageOrdering != null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it's primitive, then we don't need to check for null here, just straight set the value, or, to be extra safe, check if true. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The question is whether we want to allow not setting the value at all and letting the client lib control the default. |
||
publisherBuilder.setEnableMessageOrdering(this.enableMessageOrdering); | ||
} | ||
} | ||
|
||
Map<String, Publisher> getCache() { | ||
return this.publishers; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not a primitive boolean? Default state is false; would be no need to mess with nulls.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 yeah would also prefer the primitive boolean to avoid implying that not setting it is different than setting it to
false
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sometimes we follow the pattern that is to allow not setting it, so that the client library default can be used automatically. Otherwise, we have to maintain the default, which in this case probably doesn't matter.