Skip to content

Commit 5f90f54

Browse files
mjpt777vyazelenko
authored andcommitted
[Java] Change role to leader after winning election so leadership can be asserted during replay or replication.
(cherry picked from commit 796d050)
1 parent 30bc137 commit 5f90f54

1 file changed

Lines changed: 15 additions & 34 deletions

File tree

aeron-cluster/src/main/java/io/aeron/cluster/Election.java

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ boolean isLeaderStartup()
157157
return isLeaderStartup;
158158
}
159159

160+
int thisMemberId()
161+
{
162+
return thisMember.id();
163+
}
164+
160165
int doWork(final long nowNs)
161166
{
162167
int workCount = 0;
@@ -309,17 +314,9 @@ void onCanvassPosition(
309314

310315
if (logLeadershipTermId < this.leadershipTermId)
311316
{
312-
switch (state)
317+
if (Cluster.Role.LEADER == consensusModuleAgent.role())
313318
{
314-
case LEADER_LOG_REPLICATION:
315-
case LEADER_INIT:
316-
case LEADER_READY:
317-
case LEADER_REPLAY:
318-
publishNewLeadershipTerm(follower, logLeadershipTermId, ctx.clusterClock().time());
319-
break;
320-
321-
default:
322-
break;
319+
publishNewLeadershipTerm(follower, logLeadershipTermId, ctx.clusterClock().time());
323320
}
324321
}
325322
else if (logLeadershipTermId > this.leadershipTermId)
@@ -663,15 +660,6 @@ private int init(final long nowNs)
663660
return 1;
664661
}
665662

666-
private void prepareForNewLeadership(final long nowNs)
667-
{
668-
final long lastAppendPosition = consensusModuleAgent.prepareForNewLeadership(logPosition, nowNs);
669-
if (NULL_POSITION != lastAppendPosition)
670-
{
671-
appendPosition = lastAppendPosition;
672-
}
673-
}
674-
675663
private int canvass(final long nowNs)
676664
{
677665
int workCount = 0;
@@ -790,14 +778,6 @@ private int followerBallot(final long nowNs)
790778
return workCount;
791779
}
792780

793-
/**
794-
* Leader log replication must wait until we have consensus on the leaders append position. However,
795-
* we want to be careful about updating the commit position as this will cause the clustered service to progress
796-
* forward to early.
797-
*
798-
* @param nowNs current time
799-
* @return work done
800-
*/
801781
private int leaderLogReplication(final long nowNs)
802782
{
803783
int workCount = 0;
@@ -917,7 +897,7 @@ private int followerLogReplication(final long nowNs)
917897
if (replicationCommitPosition >= appendPosition)
918898
{
919899
ConsensusModuleAgent.logReplicationEnded(
920-
thisMemberId(),
900+
thisMember.id(),
921901
"ELECTION",
922902
logReplication.srcArchiveChannel(),
923903
logReplication.recordingId(),
@@ -1313,11 +1293,8 @@ private void state(final ElectionState newState, final long nowNs)
13131293
break;
13141294

13151295
case LEADER_LOG_REPLICATION:
1316-
logSessionId = consensusModuleAgent.addLogPublication(appendPosition);
1317-
break;
1318-
1319-
case LEADER_INIT:
13201296
consensusModuleAgent.role(Cluster.Role.LEADER);
1297+
logSessionId = consensusModuleAgent.addLogPublication(appendPosition);
13211298
break;
13221299

13231300
case FOLLOWER_LOG_REPLICATION:
@@ -1486,9 +1463,13 @@ private void logStateChange(
14861463
*/
14871464
}
14881465

1489-
int thisMemberId()
1466+
private void prepareForNewLeadership(final long nowNs)
14901467
{
1491-
return thisMember.id();
1468+
final long lastAppendPosition = consensusModuleAgent.prepareForNewLeadership(logPosition, nowNs);
1469+
if (NULL_POSITION != lastAppendPosition)
1470+
{
1471+
appendPosition = lastAppendPosition;
1472+
}
14921473
}
14931474

14941475
public String toString()

0 commit comments

Comments
 (0)