-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Defensive Renew Lease #47038
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
Open
tvaron3
wants to merge
15
commits into
Azure:main
Choose a base branch
from
tvaron3:tvaron3/rebasedOnMainRenewLease
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Defensive Renew Lease #47038
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f338b60
defensive mechanism for leases
tvaron3 ae971f1
remove testing changes
tvaron3 11cb774
react to comments
tvaron3 6499df1
fix and add more tests
tvaron3 b22f652
react to comments, revert empty lines, add more logs
tvaron3 06a7392
fix test
tvaron3 efc93e2
fix tests
tvaron3 2951b91
fix tests
tvaron3 f33329e
removed noisy log
tvaron3 6b8ca46
add logs for exceptions
tvaron3 075b553
add logs for exceptions
tvaron3 49067ab
Revert last two commits
tvaron3 6853c78
Merge branch 'main' of https://github.com/Azure/azure-sdk-for-java in…
tvaron3 b2a9957
update changelog
tvaron3 bcb090d
react to comments
tvaron3 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
146 changes: 146 additions & 0 deletions
146
...a/com/azure/cosmos/implementation/changefeed/epkversion/PartitionSupervisorImplTests.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,146 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
package com.azure.cosmos.implementation.changefeed.epkversion; | ||
|
||
import com.azure.cosmos.implementation.changefeed.CancellationToken; | ||
import com.azure.cosmos.implementation.changefeed.CancellationTokenSource; | ||
import com.azure.cosmos.implementation.changefeed.ChangeFeedObserver; | ||
import com.azure.cosmos.implementation.changefeed.Lease; | ||
import com.azure.cosmos.implementation.changefeed.LeaseRenewer; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import org.mockito.Mockito; | ||
import org.testng.annotations.Test; | ||
import reactor.core.publisher.Mono; | ||
import reactor.core.scheduler.Schedulers; | ||
|
||
import java.lang.reflect.Method; | ||
import java.time.Duration; | ||
import java.time.Instant; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests for the PartitionSupervisorImpl class | ||
*/ | ||
public class PartitionSupervisorImplTests { | ||
|
||
private Lease leaseMock; | ||
private PartitionProcessor processorMock; | ||
private LeaseRenewer renewerMock; | ||
private ChangeFeedObserver<JsonNode> observerMock; // added field | ||
|
||
@SuppressWarnings({"unchecked", "rawtypes"}) | ||
public void setup() { | ||
leaseMock = Mockito.mock(Lease.class); | ||
Mockito.when(leaseMock.getLeaseToken()).thenReturn("-FF"); | ||
observerMock = Mockito.mock(ChangeFeedObserver.class); // assign to field | ||
processorMock = Mockito.mock(PartitionProcessor.class); | ||
renewerMock = Mockito.mock(LeaseRenewer.class); | ||
|
||
Mockito.doNothing().when(observerMock).open(Mockito.any()); | ||
Mockito.when(processorMock.run(Mockito.any())).thenReturn(Mono.empty()); | ||
Mockito.when(renewerMock.run(Mockito.any())).thenReturn(Mono.empty()); | ||
Mockito.when(renewerMock.getLeaseRenewInterval()).thenReturn(Duration.ofSeconds(1)); | ||
} | ||
|
||
private PartitionSupervisorImpl createSupervisor() { | ||
return new PartitionSupervisorImpl( | ||
leaseMock, | ||
observerMock, | ||
processorMock, | ||
renewerMock, | ||
Schedulers.immediate()); | ||
} | ||
|
||
private boolean invokeShouldContinue(PartitionSupervisorImpl sup, CancellationToken token) throws Exception { | ||
Method m = PartitionSupervisorImpl.class.getDeclaredMethod("shouldContinue", CancellationToken.class); | ||
m.setAccessible(true); | ||
return (boolean) m.invoke(sup, token); | ||
} | ||
|
||
@Test(groups = "unit") | ||
public void shouldContinue_NoVerificationWindow_NotCancelled_NoErrors() throws Exception { | ||
this.setup(); | ||
PartitionSupervisorImpl sup = createSupervisor(); | ||
CancellationTokenSource cts = new CancellationTokenSource(); | ||
Mockito.when(processorMock.getResultException()).thenReturn(null); | ||
Mockito.when(renewerMock.getResultException()).thenReturn(null); | ||
Mockito.when(processorMock.getLastProcessedTime()).thenReturn(Instant.now()); | ||
|
||
boolean result = invokeShouldContinue(sup, cts.getToken()); | ||
// double check this metho | ||
assertThat(result).isTrue(); | ||
} | ||
|
||
@Test(groups = "unit") | ||
public void shouldContinue_VerificationWindow_ProcessedBatchesTrue() throws Exception { | ||
this.setup(); | ||
PartitionSupervisorImpl sup = createSupervisor(); | ||
CancellationTokenSource cts = new CancellationTokenSource(); | ||
Duration renewInterval = renewerMock.getLeaseRenewInterval(); | ||
// Force verification window elapsed: interval * 25 + 1s | ||
Mockito.when(processorMock.getLastProcessedTime()).thenReturn(Instant.now().minus(renewInterval | ||
.multipliedBy(23)).minusSeconds(1)); | ||
Mockito.when(processorMock.getResultException()).thenReturn(null); | ||
Mockito.when(renewerMock.getResultException()).thenReturn(null); | ||
|
||
|
||
boolean result = invokeShouldContinue(sup, cts.getToken()); | ||
|
||
assertThat(result).isTrue(); | ||
} | ||
|
||
@Test(groups = "unit") | ||
public void shouldContinue_VerificationWindow_ProcessedBatchesFalse() throws Exception { | ||
this.setup(); | ||
PartitionSupervisorImpl sup = createSupervisor(); | ||
CancellationTokenSource cts = new CancellationTokenSource(); | ||
Duration renewInterval = renewerMock.getLeaseRenewInterval(); | ||
Mockito.when(processorMock.getLastProcessedTime()).thenReturn(Instant.now().minus(renewInterval.multipliedBy(25)).minusSeconds(1)); | ||
Mockito.when(processorMock.getResultException()).thenReturn(null); | ||
Mockito.when(renewerMock.getResultException()).thenReturn(null); | ||
|
||
boolean result = invokeShouldContinue(sup, cts.getToken()); | ||
assertThat(result).isFalse(); // should stop due to no processed batches | ||
} | ||
|
||
@Test(groups = "unit") | ||
public void shouldContinue_ProcessorError_Stops() throws Exception { | ||
this.setup(); | ||
PartitionSupervisorImpl sup = createSupervisor(); | ||
CancellationTokenSource cts = new CancellationTokenSource(); | ||
Mockito.when(processorMock.getLastProcessedTime()).thenReturn(Instant.now()); | ||
Mockito.when(processorMock.getResultException()).thenReturn(new RuntimeException("failure")); | ||
Mockito.when(renewerMock.getResultException()).thenReturn(null); | ||
|
||
boolean result = invokeShouldContinue(sup, cts.getToken()); | ||
assertThat(result).isFalse(); | ||
} | ||
|
||
@Test(groups = "unit") | ||
public void shouldContinue_RenewerError_Stops() throws Exception { | ||
this.setup(); | ||
PartitionSupervisorImpl sup = createSupervisor(); | ||
CancellationTokenSource cts = new CancellationTokenSource(); | ||
Mockito.when(processorMock.getLastProcessedTime()).thenReturn(Instant.now()); | ||
Mockito.when(processorMock.getResultException()).thenReturn(null); | ||
Mockito.when(renewerMock.getResultException()).thenReturn(new RuntimeException("lease error")); | ||
|
||
boolean result = invokeShouldContinue(sup, cts.getToken()); | ||
assertThat(result).isFalse(); | ||
} | ||
|
||
@Test(groups = "unit") | ||
public void shouldContinue_ShutdownRequested_Stops() throws Exception { | ||
this.setup(); | ||
PartitionSupervisorImpl sup = createSupervisor(); | ||
CancellationTokenSource cts = new CancellationTokenSource(); | ||
cts.cancel(); | ||
Mockito.when(processorMock.getLastProcessedTime()).thenReturn(Instant.now()); | ||
Mockito.when(processorMock.getResultException()).thenReturn(null); | ||
Mockito.when(renewerMock.getResultException()).thenReturn(null); | ||
|
||
boolean result = invokeShouldContinue(sup, cts.getToken()); | ||
assertThat(result).isFalse(); | ||
} | ||
} |
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.
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.