Fixed most reported issues by SonarCloud.
This commit is contained in:
@ -36,6 +36,7 @@ public abstract class AbstractInstance implements Instance, Runnable {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractInstance.class);
|
||||
|
||||
protected static final int HEARTBEAT_INTERVAL = 5000;
|
||||
private static final String INSTANCE = "Instance ";
|
||||
|
||||
protected MessageManager messageManager;
|
||||
protected Queue<Message> messageQueue;
|
||||
@ -106,27 +107,27 @@ public abstract class AbstractInstance implements Instance, Runnable {
|
||||
private void processMessage(Message message) {
|
||||
switch (message.getType()) {
|
||||
case ELECTION:
|
||||
LOGGER.info("Instance " + localId + " - Election Message handling...");
|
||||
LOGGER.info(INSTANCE + localId + " - Election Message handling...");
|
||||
handleElectionMessage(message);
|
||||
break;
|
||||
case LEADER:
|
||||
LOGGER.info("Instance " + localId + " - Leader Message handling...");
|
||||
LOGGER.info(INSTANCE + localId + " - Leader Message handling...");
|
||||
handleLeaderMessage(message);
|
||||
break;
|
||||
case HEARTBEAT:
|
||||
LOGGER.info("Instance " + localId + " - Heartbeat Message handling...");
|
||||
LOGGER.info(INSTANCE + localId + " - Heartbeat Message handling...");
|
||||
handleHeartbeatMessage(message);
|
||||
break;
|
||||
case ELECTION_INVOKE:
|
||||
LOGGER.info("Instance " + localId + " - Election Invoke Message handling...");
|
||||
LOGGER.info(INSTANCE + localId + " - Election Invoke Message handling...");
|
||||
handleElectionInvokeMessage();
|
||||
break;
|
||||
case LEADER_INVOKE:
|
||||
LOGGER.info("Instance " + localId + " - Leader Invoke Message handling...");
|
||||
LOGGER.info(INSTANCE + localId + " - Leader Invoke Message handling...");
|
||||
handleLeaderInvokeMessage();
|
||||
break;
|
||||
case HEARTBEAT_INVOKE:
|
||||
LOGGER.info("Instance " + localId + " - Heartbeat Invoke Message handling...");
|
||||
LOGGER.info(INSTANCE + localId + " - Heartbeat Invoke Message handling...");
|
||||
handleHeartbeatInvokeMessage();
|
||||
break;
|
||||
default:
|
||||
|
@ -41,6 +41,7 @@ import org.slf4j.LoggerFactory;
|
||||
public class BullyInstance extends AbstractInstance {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(BullyInstance.class);
|
||||
private static final String INSTANCE = "Instance ";
|
||||
|
||||
/**
|
||||
* Constructor of BullyInstance.
|
||||
@ -59,20 +60,20 @@ public class BullyInstance extends AbstractInstance {
|
||||
try {
|
||||
boolean isLeaderAlive = messageManager.sendHeartbeatMessage(leaderId);
|
||||
if (isLeaderAlive) {
|
||||
LOGGER.info("Instance " + localId + "- Leader is alive.");
|
||||
LOGGER.info(INSTANCE + localId + "- Leader is alive.");
|
||||
Thread.sleep(HEARTBEAT_INTERVAL);
|
||||
messageManager.sendHeartbeatInvokeMessage(localId);
|
||||
} else {
|
||||
LOGGER.info("Instance " + localId + "- Leader is not alive. Start election.");
|
||||
LOGGER.info(INSTANCE + localId + "- Leader is not alive. Start election.");
|
||||
boolean electionResult =
|
||||
messageManager.sendElectionMessage(localId, String.valueOf(localId));
|
||||
if (electionResult) {
|
||||
LOGGER.info("Instance " + localId + "- Succeed in election. Start leader notification.");
|
||||
LOGGER.info(INSTANCE + localId + "- Succeed in election. Start leader notification.");
|
||||
messageManager.sendLeaderMessage(localId, localId);
|
||||
}
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
LOGGER.info("Instance " + localId + "- Interrupted.");
|
||||
LOGGER.info(INSTANCE + localId + "- Interrupted.");
|
||||
}
|
||||
}
|
||||
|
||||
@ -84,10 +85,10 @@ public class BullyInstance extends AbstractInstance {
|
||||
@Override
|
||||
protected void handleElectionInvokeMessage() {
|
||||
if (!isLeader()) {
|
||||
LOGGER.info("Instance " + localId + "- Start election.");
|
||||
LOGGER.info(INSTANCE + localId + "- Start election.");
|
||||
boolean electionResult = messageManager.sendElectionMessage(localId, String.valueOf(localId));
|
||||
if (electionResult) {
|
||||
LOGGER.info("Instance " + localId + "- Succeed in election. Start leader notification.");
|
||||
LOGGER.info(INSTANCE + localId + "- Succeed in election. Start leader notification.");
|
||||
leaderId = localId;
|
||||
messageManager.sendLeaderMessage(localId, localId);
|
||||
messageManager.sendHeartbeatInvokeMessage(localId);
|
||||
@ -101,25 +102,25 @@ public class BullyInstance extends AbstractInstance {
|
||||
@Override
|
||||
protected void handleLeaderMessage(Message message) {
|
||||
leaderId = Integer.valueOf(message.getContent());
|
||||
LOGGER.info("Instance " + localId + " - Leader update done.");
|
||||
LOGGER.info(INSTANCE + localId + " - Leader update done.");
|
||||
}
|
||||
|
||||
private boolean isLeader() {
|
||||
return localId == leaderId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Not used in Bully instance.
|
||||
*/
|
||||
@Override
|
||||
protected void handleLeaderInvokeMessage() {
|
||||
// Not used in Bully Instance
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handleHeartbeatMessage(Message message) {
|
||||
// Not used in Bully Instance
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handleElectionMessage(Message message) {
|
||||
// Not used in Bully Instance
|
||||
}
|
||||
}
|
||||
|
@ -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.
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -25,15 +25,16 @@ package com.iluwatar.leaderelection.bully;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* BullyApp unit test.
|
||||
*/
|
||||
public class BullyAppTest {
|
||||
class BullyAppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
BullyApp.main(args);
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> BullyApp.main(new String[]{}));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -25,15 +25,16 @@ package com.iluwatar.leaderelection.ring;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* RingApp unit test.
|
||||
*/
|
||||
public class RingAppTest {
|
||||
class RingAppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
RingApp.main(args);
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> RingApp.main(new String[]{}));
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user