Skip to content

Commit

Permalink
Process maxRedeliverCount is 0 of DeadLeddterPolicy (apache#14706)
Browse files Browse the repository at this point in the history
Signed-off-by: xiaolongran <[email protected]>

Fixes apache#14704

When the user uses the function of DeadLetterPolicy, it is better than misoperation. MaxRedeliverCount may be set to 0. When it is set to 0, according to the current processing logic of the Java Client, the message will be pushed to the DeadLetter Topic every time.

- When MaxRedeliverCount <= 0 in DeadLetterPolicy, we reset MaxRedeliverCount to default value

(cherry picked from commit 601fbdd)
(cherry picked from commit d73942b)
  • Loading branch information
wolfstudy authored and nicoloboschi committed Apr 5, 2022
1 parent 08d7485 commit 646d9c7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,8 @@ public ConsumerBuilder<T> deadLetterPolicy(DeadLetterPolicy deadLetterPolicy) {
if (conf.getAckTimeoutMillis() == 0) {
conf.setAckTimeoutMillis(DEFAULT_ACK_TIMEOUT_MILLIS_FOR_DEAD_LETTER);
}

checkArgument(deadLetterPolicy.getMaxRedeliverCount() > 0, "MaxRedeliverCount must be > 0.");
conf.setDeadLetterPolicy(deadLetterPolicy);
}
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@
*/
package org.apache.pulsar.client.impl;

import org.apache.pulsar.client.api.*;
import org.apache.pulsar.client.api.BatchReceivePolicy;
import org.apache.pulsar.client.api.Consumer;
import org.apache.pulsar.client.api.DeadLetterPolicy;
import org.apache.pulsar.client.api.PulsarClientException;
import org.apache.pulsar.client.api.Schema;
import org.apache.pulsar.client.api.SubscriptionInitialPosition;
import org.apache.pulsar.client.api.SubscriptionMode;
import org.apache.pulsar.client.impl.conf.ConsumerConfigurationData;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -283,6 +289,15 @@ public void testConsumerBuilderImplWhenBatchReceivePolicyIsNotValid() {
.build());
}

@Test(expectedExceptions = IllegalArgumentException.class)
public void testRedeliverCountOfDeadLetterPolicy() {
consumerBuilderImpl.deadLetterPolicy(DeadLetterPolicy.builder()
.maxRedeliverCount(0)
.deadLetterTopic("test-dead-letter-topic")
.retryLetterTopic("test-retry-letter-topic")
.build());
}

@Test
public void testConsumerBuilderImplWhenNumericPropertiesAreValid() {
consumerBuilderImpl.negativeAckRedeliveryDelay(1, TimeUnit.MILLISECONDS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,12 @@ public void consumerBuilderTest() throws IOException {
consumerHandler.clearQueryParams();
consumerHandler.putQueryParam("receiverQueueSize", "1001");
consumerHandler.putQueryParam("deadLetterTopic", "dead-letter-topic");
consumerHandler.putQueryParam("maxRedeliverCount", "3");

conf = consumerHandler.getConf();
// receive queue size is the minimum value of default value (1000) and user defined value(1001)
assertEquals(conf.getReceiverQueueSize(), 1000);
assertEquals(conf.getDeadLetterPolicy().getDeadLetterTopic(), "dead-letter-topic");
assertEquals(conf.getDeadLetterPolicy().getMaxRedeliverCount(), 0);
assertEquals(conf.getDeadLetterPolicy().getMaxRedeliverCount(), 3);
}
}

0 comments on commit 646d9c7

Please sign in to comment.