-
Notifications
You must be signed in to change notification settings - Fork 14.8k
KAFKA-19928: Added retry and backoff mechanism in NetworkPartitionMetadataClient #21001
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c45f96c
KAFKA-19928: Added retry and backoff mechanism in NetworkPartitionMet…
chirag-wadhwa5 db69337
KAFKA-19928: Added the kafka license to ExponentialBackoffManager
chirag-wadhwa5 2fe22eb
KAFKA-19928: minor changes to resolve checkstyle failures
chirag-wadhwa5 6f7bb60
KAFKA-19928: Added tests for ExponentialBackoffManager
chirag-wadhwa5 de2bf10
KAFKA-19928: Minor changes for better readability
chirag-wadhwa5 cfdce65
Merge branch 'trunk' into KAFKA-19928
chirag-wadhwa5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
clients/src/main/java/org/apache/kafka/common/utils/ExponentialBackoffManager.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /* | ||
| * 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.kafka.common.utils; | ||
|
|
||
| /** | ||
| * Manages retry attempts and exponential backoff for requests. | ||
| */ | ||
| public class ExponentialBackoffManager { | ||
|
Collaborator
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. Since this is a public class now, please add a few unit tests. |
||
| private final int maxAttempts; | ||
| private int attempts; | ||
| private final ExponentialBackoff backoff; | ||
|
|
||
| public ExponentialBackoffManager(int maxAttempts, long initialInterval, int multiplier, long maxInterval, double jitter) { | ||
| this.maxAttempts = maxAttempts; | ||
| this.backoff = new ExponentialBackoff( | ||
| initialInterval, | ||
| multiplier, | ||
| maxInterval, | ||
| jitter); | ||
| } | ||
|
|
||
| public void incrementAttempt() { | ||
| attempts++; | ||
| } | ||
|
|
||
| public void resetAttempts() { | ||
| attempts = 0; | ||
| } | ||
|
|
||
| public boolean canAttempt() { | ||
| return attempts < maxAttempts; | ||
| } | ||
|
|
||
| public long backOff() { | ||
| return this.backoff.backoff(attempts); | ||
| } | ||
|
|
||
| public int attempts() { | ||
| return attempts; | ||
| } | ||
| } | ||
104 changes: 104 additions & 0 deletions
104
clients/src/test/java/org/apache/kafka/common/utils/ExponentialBackoffManagerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /* | ||
| * 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.kafka.common.utils; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| class ExponentialBackoffManagerTest { | ||
|
|
||
| private static final ArrayList<Long> BACKOFF_LIST = new ArrayList<>(List.of(100L, 200L, 400L, 800L, 1600L)); | ||
|
|
||
| @Test | ||
| public void testInitialState() { | ||
| ExponentialBackoffManager manager = new ExponentialBackoffManager( | ||
| 5, 100, 2, 1000, 0.0); | ||
| assertEquals(0, manager.attempts()); | ||
| assertTrue(manager.canAttempt()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testIncrementAttempt() { | ||
| ExponentialBackoffManager manager = new ExponentialBackoffManager( | ||
| 5, 100, 2, 1000, 0.0); | ||
| assertEquals(0, manager.attempts()); | ||
| manager.incrementAttempt(); | ||
| assertEquals(1, manager.attempts()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testResetAttempts() { | ||
| ExponentialBackoffManager manager = new ExponentialBackoffManager( | ||
| 5, 100, 2, 1000, 0.0); | ||
| manager.incrementAttempt(); | ||
| manager.incrementAttempt(); | ||
| manager.incrementAttempt(); | ||
| assertEquals(3, manager.attempts()); | ||
|
|
||
| manager.resetAttempts(); | ||
| assertEquals(0, manager.attempts()); | ||
| assertTrue(manager.canAttempt()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCanAttempt() { | ||
| ExponentialBackoffManager manager = new ExponentialBackoffManager( | ||
| 3, 100, 2, 1000, 0.0); | ||
| // Initially can attempt | ||
| assertTrue(manager.canAttempt()); | ||
| assertEquals(0, manager.attempts()); | ||
|
|
||
| manager.incrementAttempt(); | ||
| manager.incrementAttempt(); | ||
| manager.incrementAttempt(); | ||
| // After all retry attempts are exhausted | ||
| assertFalse(manager.canAttempt()); | ||
| assertEquals(3, manager.attempts()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testBackOffWithoutJitter() { | ||
| ExponentialBackoffManager manager = new ExponentialBackoffManager( | ||
| 5, 100, 2, 1000, 0.0); | ||
| for (int i = 0; i < 5; i++) { | ||
| long backoff = manager.backOff(); | ||
| // without jitter, the backoff values should be exact multiples. | ||
| assertEquals(Math.min(1000L, BACKOFF_LIST.get(i)), backoff); | ||
| manager.incrementAttempt(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testBackOffWithJitter() { | ||
| ExponentialBackoffManager manager = new ExponentialBackoffManager( | ||
| 5, 100, 2, 1000, 0.2); | ||
| for (int i = 0; i < 5; i++) { | ||
| long backoff = manager.backOff(); | ||
| // with jitter, the backoff values should be within 20% of the expected value. | ||
| assertTrue(backoff >= 0.8 * Math.min(1000L, BACKOFF_LIST.get(i))); | ||
| assertTrue(backoff <= 1.2 * Math.min(1000L, BACKOFF_LIST.get(i))); | ||
| manager.incrementAttempt(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do we really require a new class and can't use some existing mechanism?
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.
The PersisterStateManager already had a subclass called BackOffManager, and I requred a similar thing. Instead of having 2 subclasses at different places, I think having a utility class is better. It could be used in future elsewhere as well.
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.
There already exists https://github.com/apache/kafka/blob/trunk/clients/src/main/java/org/apache/kafka/common/utils/ExponentialBackoff.java, can't that be used?