mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-18 09:25:54 +00:00
docs: improve leader election
This commit is contained in:
+1
-1
@@ -44,7 +44,7 @@ public abstract class AbstractMessageManager implements MessageManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the next instance with smallest ID.
|
||||
* Find the next instance with the smallest ID.
|
||||
*
|
||||
* @return The next instance.
|
||||
*/
|
||||
|
||||
@@ -26,7 +26,6 @@ package com.iluwatar.leaderelection.bully;
|
||||
|
||||
import com.iluwatar.leaderelection.Instance;
|
||||
import com.iluwatar.leaderelection.Message;
|
||||
import com.iluwatar.leaderelection.MessageManager;
|
||||
import com.iluwatar.leaderelection.MessageType;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -100,7 +100,7 @@ public class BullyInstance extends AbstractInstance {
|
||||
*/
|
||||
@Override
|
||||
protected void handleLeaderMessage(Message message) {
|
||||
leaderId = Integer.valueOf(message.getContent());
|
||||
leaderId = Integer.parseInt(message.getContent());
|
||||
LOGGER.info(INSTANCE + localId + " - Leader update done.");
|
||||
}
|
||||
|
||||
|
||||
+2
-3
@@ -52,8 +52,7 @@ public class BullyMessageManager extends AbstractMessageManager {
|
||||
@Override
|
||||
public boolean sendHeartbeatMessage(int leaderId) {
|
||||
var leaderInstance = instanceMap.get(leaderId);
|
||||
var alive = leaderInstance.isAlive();
|
||||
return alive;
|
||||
return leaderInstance.isAlive();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -70,7 +69,7 @@ public class BullyMessageManager extends AbstractMessageManager {
|
||||
return true;
|
||||
} else {
|
||||
var electionMessage = new Message(MessageType.ELECTION_INVOKE, "");
|
||||
candidateList.stream().forEach((i) -> instanceMap.get(i).onMessage(electionMessage));
|
||||
candidateList.forEach((i) -> instanceMap.get(i).onMessage(electionMessage));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,6 @@ package com.iluwatar.leaderelection.ring;
|
||||
|
||||
import com.iluwatar.leaderelection.Instance;
|
||||
import com.iluwatar.leaderelection.Message;
|
||||
import com.iluwatar.leaderelection.MessageManager;
|
||||
import com.iluwatar.leaderelection.MessageType;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -37,7 +37,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
* check its health. If one certain instance finds the server done, it will send an election message
|
||||
* to the next alive instance in the ring, which contains its own ID. Then the next instance add its
|
||||
* ID into the message and pass it to the next. After all the alive instances' ID are add to the
|
||||
* message, the message is send back to the first instance and it will choose the instance with
|
||||
* message, the message is send back to the first instance, and it will choose the instance with the
|
||||
* smallest ID to be the new leader, and then send a leader message to other instances to inform the
|
||||
* result.
|
||||
*/
|
||||
@@ -76,7 +76,7 @@ public class RingInstance extends AbstractInstance {
|
||||
|
||||
/**
|
||||
* Process election message. If the local ID is contained in the ID list, the instance will select
|
||||
* the alive instance with smallest ID to be the new leader, and send the leader inform message.
|
||||
* the alive instance with the smallest ID to be the new leader, and send the leader inform message.
|
||||
* If not, it will add its local ID to the list and send the message to the next instance in the
|
||||
* ring.
|
||||
*/
|
||||
|
||||
+1
-2
@@ -51,8 +51,7 @@ public class RingMessageManager extends AbstractMessageManager {
|
||||
@Override
|
||||
public boolean sendHeartbeatMessage(int leaderId) {
|
||||
var leaderInstance = instanceMap.get(leaderId);
|
||||
var alive = leaderInstance.isAlive();
|
||||
return alive;
|
||||
return leaderInstance.isAlive();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user