codelipenghui commented on a change in pull request #13707: URL: https://github.com/apache/pulsar/pull/13707#discussion_r791477797
########## File path: pulsar-client/src/main/java/org/apache/pulsar/client/impl/MultiplierRedeliveryBackoff.java ########## @@ -0,0 +1,95 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.pulsar.client.impl; + +import com.google.common.base.Preconditions; +import org.apache.pulsar.client.api.RedeliveryBackoff; + +/** + * MultiplierRedeliveryBackoff + */ +public class MultiplierRedeliveryBackoff implements RedeliveryBackoff { + + private final long minDelayMs; + private final long maxDelayMs; + private final int multiplier; + private final int maxMultiplierPow; + + private MultiplierRedeliveryBackoff(long minDelayMs, long maxDelayMs, int multiplier) { Review comment: Better to add some check here, maxDelayMs should >= minDelayMs, and multiplier should > 0? ########## File path: pulsar-broker/src/test/java/org/apache/pulsar/client/impl/UnAcknowledgedMessagesTimeoutTest.java ########## @@ -322,7 +398,7 @@ public void testFailoverSingleAckedPartitionedTopic() throws Exception { assertEquals(messagesReceived, totalMessages); // 5. Check if Messages redelivered again - Thread.sleep(ackTimeOutMillis); + Thread.sleep(ackTimeOutMillis * 2); Review comment: Looks like we can remove this line by increasing receive timeout of consumer1? ########## File path: pulsar-broker/src/test/java/org/apache/pulsar/client/impl/UnAcknowledgedMessagesTimeoutTest.java ########## @@ -270,25 +318,53 @@ public void testFailoverSingleAckedPartitionedTopic() throws Exception { .create(); // 2. Create consumer - Consumer<byte[]> consumer1 = pulsarClient.newConsumer() - .topic(topicName) - .subscriptionName(subscriptionName) - .receiverQueueSize(7) - .subscriptionType(SubscriptionType.Shared) - .ackTimeout(ackTimeOutMillis, TimeUnit.MILLISECONDS) - .acknowledgmentGroupTime(0, TimeUnit.SECONDS) - .consumerName("Consumer-1") - .subscribe(); - Consumer<byte[]> consumer2 = pulsarClient.newConsumer() - .topic(topicName) - .subscriptionName(subscriptionName) - .receiverQueueSize(7) - .subscriptionType(SubscriptionType.Shared) - .ackTimeout(ackTimeOutMillis, TimeUnit.MILLISECONDS) - .acknowledgmentGroupTime(0, TimeUnit.SECONDS) - .consumerName("Consumer-2") - .subscribe(); - + Consumer<byte[]> consumer1 = null; + Consumer<byte[]> consumer2 = null; + if( isRedeliveryTracker ) { Review comment: ```suggestion if (isRedeliveryTracker) { ``` ########## File path: pulsar-broker/src/test/java/org/apache/pulsar/client/impl/UnAcknowledgedMessagesTimeoutTest.java ########## @@ -402,6 +485,68 @@ public void testCheckUnAcknowledgedMessageTimer() throws PulsarClientException, assertEquals(consumer.getUnAckedMessageTracker().size(), 0); } + @DataProvider(name = "variationsBackoff") + public static Object[][] variationsBackoff() { + return new Object[][]{ + // ackTimeOutMillis / minDelayMs / maxDelayMs / multiplier + { 2000, 1000, 2000, 2 }, + { 3000, 1000, 3000, 3 } + }; + } + + @Test(dataProvider = "variationsBackoff") + public void testCheckUnAcknowledgedMessageRedeliveryTimer(long ackTimeOutMillis, long minDelayMs, + long maxDelayMs, int multiplier) + throws PulsarClientException, InterruptedException { + String key = "testCheckUnAcknowledgedMessageRedeliveryTimer"; + final String topicName = "persistent://prop/ns-abc/topic-" + key; + final String subscriptionName = "my-ex-subscription-" + key; + final String messagePredicate = "my-message-" + key + "-"; + final int totalMessages = 3; + + // 1. producer connect + Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName) + .enableBatching(false) + .messageRoutingMode(MessageRoutingMode.SinglePartition) + .create(); + + // 2. Create consumer + ConsumerImpl<byte[]> consumer = (ConsumerImpl<byte[]>) pulsarClient.newConsumer().topic(topicName) + .subscriptionName(subscriptionName).receiverQueueSize(7).subscriptionType(SubscriptionType.Shared) + .ackTimeout(ackTimeOutMillis, TimeUnit.MILLISECONDS) + .ackTimeoutRedeliveryBackoff(MultiplierRedeliveryBackoff.builder() + .minDelayMs(minDelayMs) + .maxDelayMs(maxDelayMs) + .multiplier(multiplier).build()) + .subscribe(); + + // 3. producer publish messages + for (int i = 0; i < totalMessages; i++) { + String message = messagePredicate + i; + log.info("Producer produced: " + message); + producer.send(message.getBytes()); + } + + Thread.sleep((long) (ackTimeOutMillis * 1.1)); Review comment: Sleep here can't make sure of the redelivery delay because the message might already in the consumer cache. We should make sure the timestamp when received message is >= ackTimeout + backoff delay -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
