mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-19 19:26:36 +00:00
Fixed most reported issues by SonarCloud.
This commit is contained in:
@@ -46,6 +46,7 @@ import org.slf4j.LoggerFactory;
|
||||
public class RingInstance extends AbstractInstance {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(RingInstance.class);
|
||||
private static final String INSTANCE = "Instance ";
|
||||
|
||||
/**
|
||||
* Constructor of RingInstance.
|
||||
@@ -64,15 +65,15 @@ public class RingInstance extends AbstractInstance {
|
||||
try {
|
||||
var isLeaderAlive = messageManager.sendHeartbeatMessage(this.leaderId);
|
||||
if (isLeaderAlive) {
|
||||
LOGGER.info("Instance " + localId + "- Leader is alive. Start next heartbeat in 5 second.");
|
||||
LOGGER.info(INSTANCE + localId + "- Leader is alive. Start next heartbeat in 5 second.");
|
||||
Thread.sleep(HEARTBEAT_INTERVAL);
|
||||
messageManager.sendHeartbeatInvokeMessage(this.localId);
|
||||
} else {
|
||||
LOGGER.info("Instance " + localId + "- Leader is not alive. Start election.");
|
||||
LOGGER.info(INSTANCE + localId + "- Leader is not alive. Start election.");
|
||||
messageManager.sendElectionMessage(this.localId, String.valueOf(this.localId));
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
LOGGER.info("Instance " + localId + "- Interrupted.");
|
||||
LOGGER.info(INSTANCE + localId + "- Interrupted.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,14 +86,14 @@ public class RingInstance extends AbstractInstance {
|
||||
@Override
|
||||
protected void handleElectionMessage(Message message) {
|
||||
var content = message.getContent();
|
||||
LOGGER.info("Instance " + localId + " - Election Message: " + content);
|
||||
LOGGER.info(INSTANCE + localId + " - Election Message: " + content);
|
||||
var candidateList = Arrays.stream(content.trim().split(","))
|
||||
.map(Integer::valueOf)
|
||||
.sorted()
|
||||
.collect(Collectors.toList());
|
||||
if (candidateList.contains(localId)) {
|
||||
var newLeaderId = candidateList.get(0);
|
||||
LOGGER.info("Instance " + localId + " - New leader should be " + newLeaderId + ".");
|
||||
LOGGER.info(INSTANCE + localId + " - New leader should be " + newLeaderId + ".");
|
||||
messageManager.sendLeaderMessage(localId, newLeaderId);
|
||||
} else {
|
||||
content += "," + localId;
|
||||
@@ -108,11 +109,11 @@ public class RingInstance extends AbstractInstance {
|
||||
protected void handleLeaderMessage(Message message) {
|
||||
var newLeaderId = Integer.valueOf(message.getContent());
|
||||
if (this.leaderId != newLeaderId) {
|
||||
LOGGER.info("Instance " + localId + " - Update leaderID");
|
||||
LOGGER.info(INSTANCE + localId + " - Update leaderID");
|
||||
this.leaderId = newLeaderId;
|
||||
messageManager.sendLeaderMessage(localId, newLeaderId);
|
||||
} else {
|
||||
LOGGER.info("Instance " + localId + " - Leader update done. Start heartbeat.");
|
||||
LOGGER.info(INSTANCE + localId + " - Leader update done. Start heartbeat.");
|
||||
messageManager.sendHeartbeatInvokeMessage(localId);
|
||||
}
|
||||
}
|
||||
@@ -122,14 +123,17 @@ public class RingInstance extends AbstractInstance {
|
||||
*/
|
||||
@Override
|
||||
protected void handleLeaderInvokeMessage() {
|
||||
// Not used in Ring instance.
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handleHeartbeatMessage(Message message) {
|
||||
// Not used in Ring instance.
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handleElectionInvokeMessage() {
|
||||
// Not used in Ring instance.
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user