Resolves checkstyle errors for api-gateway, lazy-loading, leader-election (#1066)

* Reduces checkstyle errors in lazy-loading

* Reduces checkstyle errors in leader-election

* Reduces checkstyle errors in api-gateway
This commit is contained in:
Anurag Agarwal
2019-11-10 22:43:40 +05:30
committed by Ilkka Seppälä
parent 7f06f3b78c
commit eae09fc07e
30 changed files with 188 additions and 172 deletions

View File

@ -23,11 +23,10 @@
package com.iluwatar.leaderelection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Abstract class of all the instance implementation classes.
@ -69,7 +68,9 @@ public abstract class AbstractInstance implements Instance, Runnable {
}
/**
* Once messages are sent to the certain instance, it will firstly be added to the queue and wait to be executed.
* Once messages are sent to the certain instance, it will firstly be added to the queue and wait
* to be executed.
*
* @param message Message sent by other instances
*/
@Override
@ -79,6 +80,7 @@ public abstract class AbstractInstance implements Instance, Runnable {
/**
* Check if the instance is alive or not.
*
* @return {@code true} if the instance is alive.
*/
@Override
@ -88,6 +90,7 @@ public abstract class AbstractInstance implements Instance, Runnable {
/**
* Set the health status of the certain instance.
*
* @param alive {@code true} for alive.
*/
@Override
@ -97,6 +100,7 @@ public abstract class AbstractInstance implements Instance, Runnable {
/**
* Process the message according to its type.
*
* @param message Message polled from queue.
*/
private void processMessage(Message message) {
@ -131,8 +135,8 @@ public abstract class AbstractInstance implements Instance, Runnable {
}
/**
* Abstract methods to handle different types of message. These methods need to be implemented in concrete instance
* class to implement corresponding leader-selection pattern.
* Abstract methods to handle different types of message. These methods need to be implemented in
* concrete instance class to implement corresponding leader-selection pattern.
*/
protected abstract void handleElectionMessage(Message message);

View File

@ -38,7 +38,7 @@ public abstract class AbstractMessageManager implements MessageManager {
protected Map<Integer, Instance> instanceMap;
/**
* Construtor of AbstractMessageManager
* Construtor of AbstractMessageManager.
*/
public AbstractMessageManager(Map<Integer, Instance> instanceMap) {
this.instanceMap = instanceMap;
@ -46,15 +46,16 @@ public abstract class AbstractMessageManager implements MessageManager {
/**
* Find the next instance with smallest ID.
*
* @return The next instance.
*/
protected Instance findNextInstance(int currentId) {
Instance result = null;
List<Integer> candidateList = instanceMap.keySet()
.stream()
.filter((i) -> i > currentId && instanceMap.get(i).isAlive())
.sorted()
.collect(Collectors.toList());
.stream()
.filter((i) -> i > currentId && instanceMap.get(i).isAlive())
.sorted()
.collect(Collectors.toList());
if (candidateList.isEmpty()) {
int index = instanceMap.keySet()
.stream()

View File

@ -24,24 +24,27 @@
package com.iluwatar.leaderelection;
/**
* Instance interface
* Instance interface.
*/
public interface Instance {
/**
* Check if the instance is alive or not.
*
* @return {@code true} if the instance is alive.
*/
boolean isAlive();
/**
* Set the health status of the certain instance.
*
* @param alive {@code true} for alive.
*/
void setAlive(boolean alive);
/**
* Consume messages from other instances.
*
* @param message Message sent by other instances
*/
void onMessage(Message message);

View File

@ -26,7 +26,7 @@ package com.iluwatar.leaderelection;
import java.util.Objects;
/**
* Message used to transport data between instances.
* Message used to transport data between instances.
*/
public class Message {
@ -34,7 +34,8 @@ public class Message {
private String content;
public Message() {}
public Message() {
}
public Message(MessageType type, String content) {
this.type = type;

View File

@ -24,12 +24,13 @@
package com.iluwatar.leaderelection;
/**
* MessageManager interface
* MessageManager interface.
*/
public interface MessageManager {
/**
* Send heartbeat message to leader instance to check whether the leader instance is alive.
*
* @param leaderId Instance ID of leader instance.
* @return {@code true} if leader instance is alive, or {@code false} if not.
*/
@ -37,22 +38,25 @@ public interface MessageManager {
/**
* Send election message to other instances.
*
* @param currentId Instance ID of which sends this message.
* @param content Election message content.
* @param content Election message content.
* @return {@code true} if the message is accepted by the target instances.
*/
boolean sendElectionMessage(int currentId, String content);
/**
* Send new leader notification message to other instances.
*
* @param currentId Instance ID of which sends this message.
* @param leaderId Leader message content.
* @param leaderId Leader message content.
* @return {@code true} if the message is accepted by the target instances.
*/
boolean sendLeaderMessage(int currentId, int leaderId);
/**
* Send heartbeat invoke message. This will invoke heartbeat task in the target instance.
*
* @param currentId Instance ID of which sends this message.
*/
void sendHeartbeatInvokeMessage(int currentId);

View File

@ -24,7 +24,7 @@
package com.iluwatar.leaderelection;
/**
* Message Type enum
* Message Type enum.
*/
public enum MessageType {

View File

@ -27,19 +27,18 @@ 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;
/**
* Example of how to use bully leader election. Initially 5 instances is created in the clould
* system, and the instance with ID 1 is set as leader. After the system is started stop the
* leader instance, and the new leader will be elected.
* system, and the instance with ID 1 is set as leader. After the system is started stop the leader
* instance, and the new leader will be elected.
*/
public class BullyApp {
/**
* Program entry point
* Program entry point.
*/
public static void main(String[] args) {
@ -60,11 +59,11 @@ public class BullyApp {
instance4.onMessage(new Message(MessageType.HEARTBEAT_INVOKE, ""));
Thread thread1 = new Thread(instance1);
Thread thread2 = new Thread(instance2);
Thread thread3 = new Thread(instance3);
Thread thread4 = new Thread(instance4);
Thread thread5 = new Thread(instance5);
final Thread thread1 = new Thread(instance1);
final Thread thread2 = new Thread(instance2);
final Thread thread3 = new Thread(instance3);
final Thread thread4 = new Thread(instance4);
final Thread thread5 = new Thread(instance5);
thread1.start();
thread2.start();

View File

@ -32,11 +32,11 @@ import org.slf4j.LoggerFactory;
/**
* Impelemetation with bully algorithm. Each instance should have a sequential id and is able to
* communicate with other instances in the system. Initially the instance with smallest (or largest)
* ID is selected to be the leader. All the other instances send heartbeat message to leader periodically
* to check its health. If one certain instance finds the server done, it will send an election message
* to all the instances of which the ID is larger. If the target instance is alive, it will return an
* alive message (in this sample return true) and then send election message with its ID. If not,
* the original instance will send leader message to all the other instances.
* ID is selected to be the leader. All the other instances send heartbeat message to leader
* periodically to check its health. If one certain instance finds the server done, it will send an
* election message to all the instances of which the ID is larger. If the target instance is alive,
* it will return an alive message (in this sample return true) and then send election message with
* its ID. If not, the original instance will send leader message to all the other instances.
*/
public class BullyInstance extends AbstractInstance {
@ -50,9 +50,9 @@ public class BullyInstance extends AbstractInstance {
}
/**
* Process the heartbeat invoke message. After receiving the message, the instance will send a heartbeat
* to leader to check its health. If alive, it will inform the next instance to do the heartbeat. If not,
* it will start the election process.
* Process the heartbeat invoke message. After receiving the message, the instance will send a
* heartbeat to leader to check its health. If alive, it will inform the next instance to do the
* heartbeat. If not, it will start the election process.
*/
@Override
protected void handleHeartbeatInvokeMessage() {
@ -64,7 +64,8 @@ public class BullyInstance extends AbstractInstance {
messageManager.sendHeartbeatInvokeMessage(localId);
} else {
LOGGER.info("Instance " + localId + "- Leader is not alive. Start election.");
boolean electionResult = messageManager.sendElectionMessage(localId, String.valueOf(localId));
boolean electionResult =
messageManager.sendElectionMessage(localId, String.valueOf(localId));
if (electionResult) {
LOGGER.info("Instance " + localId + "- Succeed in election. Start leader notification.");
messageManager.sendLeaderMessage(localId, localId);
@ -76,9 +77,9 @@ public class BullyInstance extends AbstractInstance {
}
/**
* Process election invoke message. Send election message to all the instances with smaller ID. If any
* one of them is alive, do nothing. If no instance alive, send leader message to all the alive instance
* and restart heartbeat.
* Process election invoke message. Send election message to all the instances with smaller ID. If
* any one of them is alive, do nothing. If no instance alive, send leader message to all the
* alive instance and restart heartbeat.
*/
@Override
protected void handleElectionInvokeMessage() {
@ -111,11 +112,14 @@ public class BullyInstance extends AbstractInstance {
* Not used in Bully instance.
*/
@Override
protected void handleLeaderInvokeMessage() {}
protected void handleLeaderInvokeMessage() {
}
@Override
protected void handleHeartbeatMessage(Message message) {}
protected void handleHeartbeatMessage(Message message) {
}
@Override
protected void handleElectionMessage(Message message) {}
protected void handleElectionMessage(Message message) {
}
}

View File

@ -27,13 +27,12 @@ import com.iluwatar.leaderelection.AbstractMessageManager;
import com.iluwatar.leaderelection.Instance;
import com.iluwatar.leaderelection.Message;
import com.iluwatar.leaderelection.MessageType;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Implementation of BullyMessageManager
* Implementation of BullyMessageManager.
*/
public class BullyMessageManager extends AbstractMessageManager {
@ -46,6 +45,7 @@ public class BullyMessageManager extends AbstractMessageManager {
/**
* Send heartbeat message to current leader instance to check the health.
*
* @param leaderId leaderID
* @return {@code true} if the leader is alive.
*/
@ -58,8 +58,9 @@ public class BullyMessageManager extends AbstractMessageManager {
/**
* Send election message to all the instances with smaller ID.
*
* @param currentId Instance ID of which sends this message.
* @param content Election message content.
* @param content Election message content.
* @return {@code true} if no alive instance has smaller ID, so that the election is accepted.
*/
@Override
@ -70,29 +71,31 @@ public class BullyMessageManager extends AbstractMessageManager {
} else {
Message electionMessage = new Message(MessageType.ELECTION_INVOKE, "");
candidateList.stream()
.forEach((i) -> instanceMap.get(i).onMessage(electionMessage));
.forEach((i) -> instanceMap.get(i).onMessage(electionMessage));
return false;
}
}
/**
* Send leader message to all the instances to notify the new leader.
*
* @param currentId Instance ID of which sends this message.
* @param leaderId Leader message content.
* @param leaderId Leader message content.
* @return {@code true} if the message is accepted.
*/
@Override
public boolean sendLeaderMessage(int currentId, int leaderId) {
Message leaderMessage = new Message(MessageType.LEADER, String.valueOf(leaderId));
instanceMap.keySet()
.stream()
.filter((i) -> i != currentId)
.forEach((i) -> instanceMap.get(i).onMessage(leaderMessage));
.stream()
.filter((i) -> i != currentId)
.forEach((i) -> instanceMap.get(i).onMessage(leaderMessage));
return false;
}
/**
* Send heartbeat invoke message to the next instance.
*
* @param currentId Instance ID of which sends this message.
*/
@Override
@ -104,14 +107,15 @@ public class BullyMessageManager extends AbstractMessageManager {
/**
* Find all the alive instances with smaller ID than current instance.
*
* @param currentId ID of current instance.
* @return ID list of all the candidate instance.
*/
private List<Integer> findElectionCandidateInstanceList(int currentId) {
return instanceMap.keySet()
.stream()
.filter((i) -> i < currentId && instanceMap.get(i).isAlive())
.collect(Collectors.toList());
.stream()
.filter((i) -> i < currentId && instanceMap.get(i).isAlive())
.collect(Collectors.toList());
}
}

View File

@ -27,19 +27,18 @@ 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;
/**
* Example of how to use ring leader election. Initially 5 instances is created in the clould
* system, and the instance with ID 1 is set as leader. After the system is started stop the
* leader instance, and the new leader will be elected.
* system, and the instance with ID 1 is set as leader. After the system is started stop the leader
* instance, and the new leader will be elected.
*/
public class RingApp {
/**
* Program entry point
* Program entry point.
*/
public static void main(String[] args) {
@ -60,11 +59,11 @@ public class RingApp {
instance2.onMessage(new Message(MessageType.HEARTBEAT_INVOKE, ""));
Thread thread1 = new Thread(instance1);
Thread thread2 = new Thread(instance2);
Thread thread3 = new Thread(instance3);
Thread thread4 = new Thread(instance4);
Thread thread5 = new Thread(instance5);
final Thread thread1 = new Thread(instance1);
final Thread thread2 = new Thread(instance2);
final Thread thread3 = new Thread(instance3);
final Thread thread4 = new Thread(instance4);
final Thread thread5 = new Thread(instance5);
thread1.start();
thread2.start();

View File

@ -26,23 +26,22 @@ package com.iluwatar.leaderelection.ring;
import com.iluwatar.leaderelection.AbstractInstance;
import com.iluwatar.leaderelection.Message;
import com.iluwatar.leaderelection.MessageManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Implementation with token ring algorithm. The instances in the system are organized as a ring.
* Each instance should have a sequential id and the instance with smallest (or largest) id should
* be the initial leader. All the other instances send heartbeat message to leader periodically
* to 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 smallest ID to be the new leader, and then send a leader message to other instances to
* inform the result.
* be the initial leader. All the other instances send heartbeat message to leader periodically to
* 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
* smallest ID to be the new leader, and then send a leader message to other instances to inform the
* result.
*/
public class RingInstance extends AbstractInstance {
@ -56,9 +55,9 @@ public class RingInstance extends AbstractInstance {
}
/**
* Process the heartbeat invoke message. After receiving the message, the instance will send a heartbeat
* to leader to check its health. If alive, it will inform the next instance to do the heartbeat. If not,
* it will start the election process.
* Process the heartbeat invoke message. After receiving the message, the instance will send a
* heartbeat to leader to check its health. If alive, it will inform the next instance to do the
* heartbeat. If not, it will start the election process.
*/
@Override
protected void handleHeartbeatInvokeMessage() {
@ -78,9 +77,10 @@ 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. If not,
* it will add its local ID to the list and send the message to the next instance in the ring.
* 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.
* If not, it will add its local ID to the list and send the message to the next instance in the
* ring.
*/
@Override
protected void handleElectionMessage(Message message) {
@ -88,9 +88,9 @@ public class RingInstance extends AbstractInstance {
LOGGER.info("Instance " + localId + " - Election Message: " + content);
List<Integer> candidateList =
Arrays.stream(content.trim().split(","))
.map(Integer::valueOf)
.sorted()
.collect(Collectors.toList());
.map(Integer::valueOf)
.sorted()
.collect(Collectors.toList());
if (candidateList.contains(localId)) {
int newLeaderId = candidateList.get(0);
LOGGER.info("Instance " + localId + " - New leader should be " + newLeaderId + ".");
@ -102,8 +102,8 @@ public class RingInstance extends AbstractInstance {
}
/**
* Process leader Message. The instance will set the leader ID to be the new one and send the message to
* the next instance until all the alive instance in the ring is informed.
* Process leader Message. The instance will set the leader ID to be the new one and send the
* message to the next instance until all the alive instance in the ring is informed.
*/
@Override
protected void handleLeaderMessage(Message message) {
@ -122,12 +122,15 @@ public class RingInstance extends AbstractInstance {
* Not used in Ring instance.
*/
@Override
protected void handleLeaderInvokeMessage() {}
protected void handleLeaderInvokeMessage() {
}
@Override
protected void handleHeartbeatMessage(Message message) {}
protected void handleHeartbeatMessage(Message message) {
}
@Override
protected void handleElectionInvokeMessage() {}
protected void handleElectionInvokeMessage() {
}
}

View File

@ -27,11 +27,10 @@ import com.iluwatar.leaderelection.AbstractMessageManager;
import com.iluwatar.leaderelection.Instance;
import com.iluwatar.leaderelection.Message;
import com.iluwatar.leaderelection.MessageType;
import java.util.Map;
/**
* Implementation of RingMessageManager
* Implementation of RingMessageManager.
*/
public class RingMessageManager extends AbstractMessageManager {
@ -44,6 +43,7 @@ public class RingMessageManager extends AbstractMessageManager {
/**
* Send heartbeat message to current leader instance to check the health.
*
* @param leaderId leaderID
* @return {@code true} if the leader is alive.
*/
@ -56,8 +56,10 @@ public class RingMessageManager extends AbstractMessageManager {
/**
* Send election message to the next instance.
*
* @param currentId currentID
* @param content list contains all the IDs of instances which have received this election message.
* @param content list contains all the IDs of instances which have received this election
* message.
* @return {@code true} if the election message is accepted by the target instance.
*/
@Override
@ -70,8 +72,9 @@ public class RingMessageManager extends AbstractMessageManager {
/**
* Send leader message to the next instance.
*
* @param currentId Instance ID of which sends this message.
* @param leaderId Leader message content.
* @param leaderId Leader message content.
* @return {@code true} if the leader message is accepted by the target instance.
*/
@Override
@ -84,6 +87,7 @@ public class RingMessageManager extends AbstractMessageManager {
/**
* Send heartbeat invoke message to the next instance.
*
* @param currentId Instance ID of which sends this message.
*/
@Override