sijie commented on a change in pull request #2400: PIP-22: Dead Letter Topic
URL: https://github.com/apache/incubator-pulsar/pull/2400#discussion_r211723819
 
 

 ##########
 File path: 
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentDispatcherMultipleConsumers.java
 ##########
 @@ -556,13 +618,90 @@ public synchronized void 
redeliverUnacknowledgedMessages(Consumer consumer) {
 
     @Override
     public synchronized void redeliverUnacknowledgedMessages(Consumer 
consumer, List<PositionImpl> positions) {
-        positions.forEach(position -> 
messagesToReplay.add(position.getLedgerId(), position.getEntryId()));
         if (log.isDebugEnabled()) {
             log.debug("[{}-{}] Redelivering unacknowledged messages for 
consumer {}", name, consumer, positions);
         }
+        if (maxRedeliveryCount > 0 && redeliveryTracker != null) {
+            for (PositionImpl position : positions) {
+                if (redeliveryTracker.incrementAndGetRedeliveryCount(position) 
<= maxRedeliveryCount) {
+                    messagesToReplay.add(position.getLedgerId(), 
position.getEntryId());
+                } else {
+                    messagesToDeadLetter.add(position);
+                }
+            }
+            if (messagesToDeadLetter.size() > 0) {
+                CountDownLatch latch = new 
CountDownLatch(messagesToDeadLetter.size());
+                for (PositionImpl position : messagesToDeadLetter) {
+                    cursor.asyncReadEntry(position, new 
AsyncCallbacks.ReadEntryCallback() {
+                        @Override
+                        public void readEntryComplete(Entry entry, Object ctx) 
{
+                            if (entry == null) {
+                                log.error("[{}-{}] Read an null entry from 
cursor {}", name, consumer, position);
+                                latch.countDown();
+                            } else {
+                                try {
+                                    ByteBuf headersAndPayload = 
entry.getDataBuffer();
+                                    MessageImpl<byte[]> msg = 
MessageImpl.deserialize(headersAndPayload);
+                                    headersAndPayload.retain();
+                                    msg.setReplicatedFrom("DLQ");
+                                    CompletableFuture<MessageId> future = 
deadLetterTopicProducer.sendAsync(msg);
+                                    future.whenCompleteAsync((messageId, 
error) -> {
+                                        if (error != null) {
+                                            log.error("[{}-{}] Fail to send 
message to dead letter topic {} {} {}",
+                                                    name, consumer, 
deadLetterTopic, error.getMessage(), error);
+                                            
messagesToReplay.add(position.getLedgerId(), position.getEntryId());
+                                            latch.countDown();
+                                        } else {
+                                            cursor.asyncDelete(position, 
deleteCallback, position);
+                                            redeliveryTracker.remove(position);
 
 Review comment:
   so the `redeliveryTracker.remove` should be called in `deleteCallback`, 
right?

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to