-
Notifications
You must be signed in to change notification settings - Fork 62
fix: possible tight busy loop on certain connection errors #1629
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,7 +36,8 @@ class SyncStreamQueueSourceTest { | |
| private ChannelConnector mockConnector; | ||
| private FlagSyncServiceBlockingStub blockingStub; | ||
| private FlagSyncServiceStub stub; | ||
| private FlagSyncServiceStub errorStub; | ||
| private FlagSyncServiceStub syncErrorStub; | ||
| private FlagSyncServiceStub asyncErrorStub; | ||
| private StreamObserver<SyncFlagsResponse> observer; | ||
| private CountDownLatch latch; // used to wait for observer to be initialized | ||
|
|
||
|
|
@@ -60,25 +61,76 @@ public void setup() throws Exception { | |
| .when(stub) | ||
| .syncFlags(any(SyncFlagsRequest.class), any(StreamObserver.class)); // Mock the initialize | ||
|
|
||
| errorStub = mock(FlagSyncServiceStub.class); | ||
| when(errorStub.withDeadlineAfter(anyLong(), any())).thenReturn(errorStub); | ||
| syncErrorStub = mock(FlagSyncServiceStub.class); | ||
| when(syncErrorStub.withDeadlineAfter(anyLong(), any())).thenReturn(syncErrorStub); | ||
| doAnswer((Answer<Void>) invocation -> { | ||
| Object[] args = invocation.getArguments(); | ||
| observer = (StreamObserver<SyncFlagsResponse>) args[1]; | ||
| latch.countDown(); | ||
| throw new StatusRuntimeException(io.grpc.Status.NOT_FOUND); | ||
| }) | ||
| .when(errorStub) | ||
| .when(syncErrorStub) | ||
| .syncFlags(any(SyncFlagsRequest.class), any(StreamObserver.class)); // Mock the initialize | ||
|
|
||
| asyncErrorStub = mock(FlagSyncServiceStub.class); | ||
| when(asyncErrorStub.withDeadlineAfter(anyLong(), any())).thenReturn(asyncErrorStub); | ||
| doAnswer((Answer<Void>) invocation -> { | ||
| Object[] args = invocation.getArguments(); | ||
| observer = (StreamObserver<SyncFlagsResponse>) args[1]; | ||
| latch.countDown(); | ||
|
|
||
| // Start a thread to call onError after a short delay | ||
| new Thread(() -> { | ||
| try { | ||
| Thread.sleep(10); // Wait 100ms before calling onError | ||
| observer.onError(new StatusRuntimeException(io.grpc.Status.INTERNAL)); | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| } | ||
| }) | ||
| .start(); | ||
|
|
||
| return null; | ||
| }) | ||
| .when(asyncErrorStub) | ||
| .syncFlags(any(SyncFlagsRequest.class), any(StreamObserver.class)); // Mock the initialize | ||
| } | ||
|
|
||
| @Test | ||
| void syncInitError_DoesNotBusyWait() throws Exception { | ||
| // make sure we do not spin in a busy loop on immediately errors | ||
|
|
||
| int maxBackoffMs = 1000; | ||
| SyncStreamQueueSource queueSource = new SyncStreamQueueSource( | ||
| FlagdOptions.builder().retryBackoffMaxMs(maxBackoffMs).build(), | ||
| mockConnector, | ||
| syncErrorStub, | ||
| blockingStub); | ||
| latch = new CountDownLatch(1); | ||
| queueSource.init(); | ||
| latch.await(); | ||
|
|
||
| BlockingQueue<QueuePayload> streamQueue = queueSource.getStreamQueue(); | ||
| QueuePayload payload = streamQueue.poll(1000, TimeUnit.MILLISECONDS); | ||
| assertNotNull(payload); | ||
| assertEquals(QueuePayloadType.ERROR, payload.getType()); | ||
| Thread.sleep(maxBackoffMs + (maxBackoffMs / 2)); // wait 1.5x our delay for reties | ||
|
|
||
| // should have retried the stream (2 calls); initial + 1 retry | ||
| // it's very important that the retry count is low, to confirm no busy-loop | ||
| verify(syncErrorStub, times(2)).syncFlags(any(), any()); | ||
| } | ||
|
|
||
| @Test | ||
| void initError_DoesNotBusyWait() throws Exception { | ||
| // make sure we do not spin in a busy loop on errors | ||
| void asyncInitError_DoesNotBusyWait() throws Exception { | ||
|
Member
Author
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. This test exercises the fix (the |
||
| // make sure we do not spin in a busy loop on async errors | ||
|
|
||
| int maxBackoffMs = 1000; | ||
| SyncStreamQueueSource queueSource = new SyncStreamQueueSource( | ||
| FlagdOptions.builder().retryBackoffMaxMs(maxBackoffMs).build(), mockConnector, errorStub, blockingStub); | ||
| FlagdOptions.builder().retryBackoffMaxMs(maxBackoffMs).build(), | ||
| mockConnector, | ||
| asyncErrorStub, | ||
| blockingStub); | ||
| latch = new CountDownLatch(1); | ||
| queueSource.init(); | ||
| latch.await(); | ||
|
|
@@ -91,7 +143,7 @@ void initError_DoesNotBusyWait() throws Exception { | |
|
|
||
| // should have retried the stream (2 calls); initial + 1 retry | ||
| // it's very important that the retry count is low, to confirm no busy-loop | ||
| verify(errorStub, times(2)).syncFlags(any(), any()); | ||
| verify(asyncErrorStub, times(2)).syncFlags(any(), any()); | ||
| } | ||
|
|
||
| @Test | ||
|
|
||
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.
Git is rendering this oddly; this is the previous unchanged test (other than the name). The new one is below.