sijie closed pull request #1386: Improve sync send logic for allowing turning
batching as default produce behavior
URL: https://github.com/apache/incubator-pulsar/pull/1386
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git
a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/PartitionedProducerImpl.java
b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/PartitionedProducerImpl.java
index 494fbce27..ba6fd2807 100644
---
a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/PartitionedProducerImpl.java
+++
b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/PartitionedProducerImpl.java
@@ -138,6 +138,14 @@ private void start() {
}
+ @Override
+ public MessageId send(Message<T> message) throws PulsarClientException {
+ int partition = routerPolicy.choosePartition(message, topicMetadata);
+ checkArgument(partition >= 0 && partition <
topicMetadata.numPartitions(),
+ "Illegal partition index chosen by the message routing policy");
+ return producers.get(partition).send(message);
+ }
+
@Override
public CompletableFuture<MessageId> sendAsync(Message<T> message) {
diff --git
a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/ProducerBase.java
b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/ProducerBase.java
index 45453aedb..0cf8bab8e 100644
---
a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/ProducerBase.java
+++
b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/ProducerBase.java
@@ -54,23 +54,6 @@ public MessageId send(T message) throws
PulsarClientException {
return
sendAsync(MessageBuilder.create(schema).setValue(message).build());
}
- @Override
- public MessageId send(Message<T> message) throws PulsarClientException {
- try {
- return sendAsync(message).get();
- } catch (ExecutionException e) {
- Throwable t = e.getCause();
- if (t instanceof PulsarClientException) {
- throw (PulsarClientException) t;
- } else {
- throw new PulsarClientException(t);
- }
- } catch (InterruptedException e) {
- Thread.currentThread().interrupt();
- throw new PulsarClientException(e);
- }
- }
-
@Override
abstract public CompletableFuture<MessageId> sendAsync(Message<T> message);
diff --git
a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/ProducerImpl.java
b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/ProducerImpl.java
index 75661cbc9..82c087634 100644
---
a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/ProducerImpl.java
+++
b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/ProducerImpl.java
@@ -32,6 +32,7 @@
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
@@ -1153,6 +1154,39 @@ public void run(Timeout timeout) throws Exception {
}
};
+ @Override
+ public MessageId send(Message<T> message) throws PulsarClientException {
+ try {
+ // enqueue the message to the buffer
+ CompletableFuture<MessageId> sendFuture = sendAsync(message);
+
+ if (!sendFuture.isDone()) {
+ // the send request wasn't completed yet (e.g. not failing at
enqueuing), then attempt to flush it out
+ flush();
+ }
+
+ return sendFuture.get();
+ } catch (ExecutionException e) {
+ Throwable t = e.getCause();
+ if (t instanceof PulsarClientException) {
+ throw (PulsarClientException) t;
+ } else {
+ throw new PulsarClientException(t);
+ }
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ throw new PulsarClientException(e);
+ }
+ }
+
+ private void flush() {
+ if (isBatchMessagingEnabled()) {
+ synchronized (ProducerImpl.this) {
+ batchMessageAndSend();
+ }
+ }
+ }
+
// must acquire semaphore before enqueuing
private void batchMessageAndSend() {
if (log.isDebugEnabled()) {
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services