Skip to content

Commit

Permalink
Force election after switching leader modes in RaftJournalTest
Browse files Browse the repository at this point in the history
### What changes are proposed in this pull request?
Sending correct argument via reflection to force election upon switching
election modes for RATIS nodes.

### Why are the changes needed?
Without this change, election might not get triggered and test will
timeout waiting for seeing the new leader.

Fix #13746

pr-link: #13779
change-id: cid-775e12623c5d87317ee3ac9c1915554952994fc9
  • Loading branch information
Göktürk Gezer committed Jul 13, 2021
1 parent 2539a05 commit c5a2bf6
Showing 1 changed file with 8 additions and 6 deletions.
Expand Up @@ -396,13 +396,15 @@ public void gainPrimacyAfterCatchup() throws Exception {
}

private void promoteFollower() throws Exception {
System.out.printf("Leader is leader? %s", mLeaderJournalSystem.isLeader());
changeToFollower(mLeaderJournalSystem);
System.out.printf("Follower is leader? %s", mFollowerJournalSystem.isLeader());
Assert.assertTrue(mLeaderJournalSystem.isLeader());
Assert.assertFalse(mFollowerJournalSystem.isLeader());
// Triggering rigged election via reflection to switch the leader.
changeToFollower(mLeaderJournalSystem);
changeToCandidate(mFollowerJournalSystem);
CommonUtils.waitFor("follower becomes leader",
() -> mFollowerJournalSystem.isLeader(), mWaitOptions);
CommonUtils.waitFor("follower becomes leader", () -> mFollowerJournalSystem.isLeader(),
mWaitOptions);
Assert.assertFalse(mLeaderJournalSystem.isLeader());
Assert.assertTrue(mFollowerJournalSystem.isLeader());
mFollowerJournalSystem.gainPrimacy();
}

Expand Down Expand Up @@ -534,7 +536,7 @@ void changeToCandidate(RaftJournalSystem journalSystem) throws Exception {
Class<?> raftServerImpl = (Class.forName("org.apache.ratis.server.impl.RaftServerImpl"));
Method method = raftServerImpl.getDeclaredMethod("changeToCandidate", boolean.class);
method.setAccessible(true);
method.invoke(serverImpl, false);
method.invoke(serverImpl, true);
}

@VisibleForTesting
Expand Down

0 comments on commit c5a2bf6

Please sign in to comment.