Skip to content

Commit

Permalink
refactor: LeaderElector::canBecomeLeader for clarity (6005)
Browse files Browse the repository at this point in the history
  • Loading branch information
wind57 committed May 15, 2024
1 parent e97a19e commit a3f154e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ protected final boolean isLeader(LeaderElectionRecord leaderElectionRecord) {

protected final boolean canBecomeLeader(LeaderElectionRecord leaderElectionRecord) {
return Utils.isNullOrEmpty(leaderElectionRecord.getHolderIdentity())
|| !leaderElectionRecord.getRenewTime().plus(leaderElectionConfig.getLeaseDuration()).isAfter(now());
|| now().isAfter(leaderElectionRecord.getRenewTime().plus(leaderElectionConfig.getLeaseDuration()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class LeaderElectorTest {
@Test
void runShouldAbortAfterRenewDeadlineExpired() throws Exception {
// Given
final Long renewDeadlineMillis = 1000L;
final long renewDeadlineMillis = 1000L;
final LeaderElectionConfig lec = mockLeaderElectionConfiguration();
when(lec.getRenewDeadline()).thenReturn(Duration.ofMillis(renewDeadlineMillis));
final Lock mockedLock = lec.getLock();
Expand Down Expand Up @@ -349,12 +349,11 @@ void jitterWithNegativeShouldReturnDuration() {
assertTrue(result.toMillis() > 1000L);
}

private LeaderElectionConfig mockLeaderElectionConfiguration() throws Exception {
private LeaderElectionConfig mockLeaderElectionConfiguration() {
return mockLeaderElectionConfiguration(new AtomicReference<>());
}

private LeaderElectionConfig mockLeaderElectionConfiguration(AtomicReference<LeaderElectionRecord> activeLer)
throws Exception {
private LeaderElectionConfig mockLeaderElectionConfiguration(AtomicReference<LeaderElectionRecord> activeLer) {
final LeaderElectionConfig lec = mock(LeaderElectionConfig.class, Answers.RETURNS_DEEP_STUBS);
when(lec.getLeaseDuration()).thenReturn(Duration.ofSeconds(2L));
when(lec.getRenewDeadline()).thenReturn(Duration.ofSeconds(1L));
Expand Down

0 comments on commit a3f154e

Please sign in to comment.