-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-12879: Revert changes from KAFKA-12339 and instead add retry capability to KafkaBasedLog #11797
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
KAFKA-12879: Revert changes from KAFKA-12339 and instead add retry capability to KafkaBasedLog #11797
Changes from 7 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
4f4cb59
initial commit - wip
philipnee d902d73
wip - tests
philipnee e1eed37
Clean up commits
philipnee 411d330
RetryUtil
philipnee e77aaa7
clean up
philipnee 03e67c7
clean up tests
philipnee 8d2e324
Revert KAFKA-12339 (#10152)
philipnee 074521d
Address PR comments
philipnee 1da527f
PR comment
philipnee dd636a3
Update PR
philipnee 93202cc
PR Revision
philipnee 5ab4d6a
Update connect/runtime/src/main/java/org/apache/kafka/connect/util/Re…
philipnee 5e5c6a3
Apply suggestions from code review
philipnee a5390fa
Retry until timeout instead of exhausting number of retries
philipnee 6821683
Apply suggestions from code review
philipnee e46f3f8
Added message supplier to supply custom message
philipnee b53f372
less retries to speed up the process
philipnee 01c0a87
timeout test if unable to complete within a reasonable timeframe
philipnee 0b6da79
Clean up the documentation
philipnee ec89772
Apply suggestions from code review
philipnee 669feee
retryEndOffsets should be public
philipnee 5b8ff9f
update retryEndOffsets
philipnee 73deef2
correcting test
philipnee 298fdc9
validating retry and timeout
philipnee 6170bea
Apply suggestions from code review
philipnee 1634208
make warning message debug instead
philipnee ef0583d
Handling small edge case when no retry might happen for small timeout
philipnee 8145729
Refactor retry logic
philipnee 4584555
handing timeoutDuration and backoffMs edge cases
philipnee 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
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
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
52 changes: 52 additions & 0 deletions
52
connect/runtime/src/main/java/org/apache/kafka/connect/util/RetryUtil.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,52 @@ | ||
| /* | ||
| * 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.connect.util; | ||
|
|
||
| import org.apache.kafka.common.errors.RetriableException; | ||
| import org.apache.kafka.common.errors.WakeupException; | ||
| import org.apache.kafka.common.utils.Utils; | ||
| import org.apache.kafka.connect.errors.ConnectException; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.util.concurrent.Callable; | ||
|
|
||
| public class RetryUtil { | ||
| private static final Logger log = LoggerFactory.getLogger(RetryUtil.class); | ||
|
|
||
| public static <T> T retry(Callable<T> callable, long maxRetries, long retryBackoffMs) throws Exception { | ||
| Throwable lastError = null; | ||
| int retries = 0; | ||
| while (retries++ < maxRetries) { | ||
| try { | ||
| return callable.call(); | ||
|
rhauch marked this conversation as resolved.
Outdated
|
||
| } catch (RetriableException | org.apache.kafka.connect.errors.RetriableException e) { | ||
| log.warn("RetriableException caught, retrying automatically up to {} more times. " + | ||
| "Reason: {}", maxRetries - retries, e.getMessage()); | ||
| lastError = e; | ||
| } catch (WakeupException e) { | ||
| lastError = e; | ||
| } catch (Exception e) { | ||
| log.warn("Non-retriable exception caught. Re-throwing. Reason: {}, {}", e.getClass(), e.getMessage()); | ||
| throw e; | ||
|
rhauch marked this conversation as resolved.
Outdated
|
||
| } | ||
| Utils.sleep(retryBackoffMs); | ||
|
rhauch marked this conversation as resolved.
|
||
| } | ||
|
|
||
| throw new ConnectException("Fail to retry the operation after " + maxRetries + " attempts. Reason: " + lastError, lastError); | ||
| } | ||
| } | ||
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
107 changes: 107 additions & 0 deletions
107
connect/runtime/src/test/java/org/apache/kafka/connect/util/RetryUtilTest.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,107 @@ | ||
| /* | ||
| * 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.connect.util; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.fail; | ||
|
|
||
| import org.apache.kafka.common.errors.TimeoutException; | ||
| import org.apache.kafka.connect.errors.ConnectException; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.mockito.Mock; | ||
| import org.mockito.Mockito; | ||
| import org.powermock.modules.junit4.PowerMockRunner; | ||
|
|
||
| import java.util.concurrent.Callable; | ||
|
|
||
| @RunWith(PowerMockRunner.class) | ||
| public class RetryUtilTest { | ||
|
|
||
| @Mock | ||
|
philipnee marked this conversation as resolved.
Outdated
|
||
| private Callable<String> mockCallable; | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| @Before | ||
| public void setUp() throws Exception { | ||
| mockCallable = Mockito.mock(Callable.class); | ||
| } | ||
|
|
||
| @Test | ||
| public void TestSuccess() throws Exception { | ||
|
rhauch marked this conversation as resolved.
Outdated
|
||
| Mockito.when(mockCallable.call()).thenReturn("success"); | ||
| //EasyMock.expect(mockCallable.call()).andReturn("success").times(1); | ||
|
rhauch marked this conversation as resolved.
Outdated
|
||
| try { | ||
| assertEquals("success", RetryUtil.retry(mockCallable, 10, 100)); | ||
| } catch (Exception e) { | ||
| fail("Not expecting an exception: ", e.getCause()); | ||
| } | ||
|
rhauch marked this conversation as resolved.
Outdated
|
||
| Mockito.verify(mockCallable, Mockito.times(1)).call(); | ||
| } | ||
|
|
||
| @Test | ||
| public void TestFailingRetries() throws Exception { | ||
| Mockito.when(mockCallable.call()).thenThrow(new TimeoutException()); | ||
| try { | ||
| RetryUtil.retry(mockCallable, 10, 100); | ||
| fail("Expect exception being thrown here"); | ||
| } catch (ConnectException e) { | ||
| // expecting a connect exception | ||
| } catch (Exception e) { | ||
| fail("Only expecting ConnectException"); | ||
| } | ||
|
rhauch marked this conversation as resolved.
Outdated
|
||
| Mockito.verify(mockCallable, Mockito.times(10)).call(); | ||
| } | ||
|
|
||
| @Test | ||
| public void RetriesEventuallySucceed() throws Exception { | ||
| Mockito.when(mockCallable.call()) | ||
| .thenThrow(new TimeoutException()) | ||
| .thenThrow(new TimeoutException()) | ||
| .thenThrow(new TimeoutException()) | ||
| .thenReturn("success"); | ||
| try { | ||
| assertEquals("success", RetryUtil.retry(mockCallable, 10, 100)); | ||
| } catch (Exception e) { | ||
| fail("Not expecting an exception: ", e.getCause()); | ||
| } | ||
|
rhauch marked this conversation as resolved.
Outdated
|
||
| Mockito.verify(mockCallable, Mockito.times(4)).call(); | ||
| } | ||
|
|
||
| @Test | ||
| public void FailWithNonRetriableException() throws Exception { | ||
| Mockito.when(mockCallable.call()) | ||
| .thenThrow(new TimeoutException("timeout")) | ||
| .thenThrow(new TimeoutException("timeout")) | ||
| .thenThrow(new TimeoutException("timeout")) | ||
| .thenThrow(new TimeoutException("timeout")) | ||
| .thenThrow(new TimeoutException("timeout")) | ||
| .thenThrow(new NullPointerException("Non retriable")); | ||
| try { | ||
| for (int i = 0; i < 10; i++) { | ||
| RetryUtil.retry(mockCallable, 10, 100); | ||
| } | ||
| fail("Not expecting an exception: "); | ||
| } catch (ConnectException e) { | ||
| fail("Should fail with NPE"); | ||
| } catch (NullPointerException e) { | ||
| // good | ||
| } | ||
|
rhauch marked this conversation as resolved.
Outdated
|
||
| Mockito.verify(mockCallable, Mockito.times(6)).call(); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.