deps: Refactor dependencies (#3224)

* remove spring dep
move junit, logging, mockito under dep mgmt

* upgrade anti-corruption-layer deps

* async method invocation

* balking, bloc

* bridge to bytecode

* caching

* callback - cqrs

* component - health check

* hexagonal - metadata mapping

* rest of the patterns

* remove checkstyle, take spotless into use
This commit is contained in:
Ilkka Seppälä authored and GitHub committed 2025-03-29 19:34:27 +02:00
1 parent 371439aeaa
commit 0ca162a55c
1863 files changed
+14403 -17632

No files matched your search

+8
View File
@@ -34,6 +34,14 @@
</parent>
<artifactId>leader-election</artifactId>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
@@ -28,9 +28,7 @@ import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import lombok.extern.slf4j.Slf4j;
/**
* Abstract class of all the instance implementation classes.
*/
/** Abstract class of all the instance implementation classes. */
@Slf4j
public abstract class AbstractInstance implements Instance, Runnable {
@@ -43,9 +41,7 @@ public abstract class AbstractInstance implements Instance, Runnable {
protected int leaderId;
protected boolean alive;
/**
* Constructor of BullyInstance.
*/
/** Constructor of BullyInstance. */
public AbstractInstance(MessageManager messageManager, int localId, int leaderId) {
this.messageManager = messageManager;
this.messageQueue = new ConcurrentLinkedQueue<>();
@@ -54,9 +50,7 @@ public abstract class AbstractInstance implements Instance, Runnable {
this.alive = true;
}
/**
* The instance will execute the message in its message queue periodically once it is alive.
*/
/** The instance will execute the message in its message queue periodically once it is alive. */
@Override
@SuppressWarnings("squid:S2189")
public void run() {
@@ -129,8 +123,7 @@ public abstract class AbstractInstance implements Instance, Runnable {
LOGGER.info(INSTANCE + localId + " - Heartbeat Invoke Message handling...");
handleHeartbeatInvokeMessage();
}
default -> {
}
default -> {}
}
}
@@ -149,5 +142,4 @@ public abstract class AbstractInstance implements Instance, Runnable {
protected abstract void handleHeartbeatMessage(Message message);
protected abstract void handleHeartbeatInvokeMessage();
}
@@ -26,19 +26,13 @@ package com.iluwatar.leaderelection;
import java.util.Map;
/**
* Abstract class of all the message manager classes.
*/
/** Abstract class of all the message manager classes. */
public abstract class AbstractMessageManager implements MessageManager {
/**
* Contain all the instances in the system. Key is its ID, and value is the instance itself.
*/
/** Contain all the instances in the system. Key is its ID, and value is the instance itself. */
protected Map<Integer, Instance> instanceMap;
/**
* Constructor of AbstractMessageManager.
*/
/** Constructor of AbstractMessageManager. */
public AbstractMessageManager(Map<Integer, Instance> instanceMap) {
this.instanceMap = instanceMap;
}
@@ -50,18 +44,18 @@ public abstract class AbstractMessageManager implements MessageManager {
*/
protected Instance findNextInstance(int currentId) {
Instance result = null;
var candidateList = instanceMap.keySet()
.stream()
.filter((i) -> i > currentId && instanceMap.get(i).isAlive())
.sorted()
.toList();
var candidateList =
instanceMap.keySet().stream()
.filter((i) -> i > currentId && instanceMap.get(i).isAlive())
.sorted()
.toList();
if (candidateList.isEmpty()) {
var index = instanceMap.keySet()
.stream()
.filter((i) -> instanceMap.get(i).isAlive())
.sorted()
.toList()
.get(0);
var index =
instanceMap.keySet().stream()
.filter((i) -> instanceMap.get(i).isAlive())
.sorted()
.toList()
.get(0);
result = instanceMap.get(index);
} else {
var index = candidateList.get(0);
@@ -69,5 +63,4 @@ public abstract class AbstractMessageManager implements MessageManager {
}
return result;
}
}
@@ -24,9 +24,7 @@
*/
package com.iluwatar.leaderelection;
/**
* Instance interface.
*/
/** Instance interface. */
public interface Instance {
/**
@@ -49,5 +47,4 @@ public interface Instance {
* @param message Message sent by other instances
*/
void onMessage(Message message);
}
@@ -30,9 +30,7 @@ import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
/**
* Message used to transport data between instances.
*/
/** Message used to transport data between instances. */
@Setter
@Getter
@EqualsAndHashCode
@@ -42,5 +40,4 @@ public class Message {
private MessageType type;
private String content;
}
@@ -24,9 +24,7 @@
*/
package com.iluwatar.leaderelection;
/**
* MessageManager interface.
*/
/** MessageManager interface. */
public interface MessageManager {
/**
@@ -41,7 +39,7 @@ 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);
@@ -50,7 +48,7 @@ public interface MessageManager {
* 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);
@@ -61,5 +59,4 @@ public interface MessageManager {
* @param currentId Instance ID of which sends this message.
*/
void sendHeartbeatInvokeMessage(int currentId);
}
@@ -24,40 +24,24 @@
*/
package com.iluwatar.leaderelection;
/**
* Message Type enum.
*/
/** Message Type enum. */
public enum MessageType {
/**
* Start the election. The content of the message stores ID(s) of the candidate instance(s).
*/
/** Start the election. The content of the message stores ID(s) of the candidate instance(s). */
ELECTION,
/**
* Nodify the new leader. The content of the message should be the leader ID.
*/
/** Nodify the new leader. The content of the message should be the leader ID. */
LEADER,
/**
* Check health of current leader instance.
*/
/** Check health of current leader instance. */
HEARTBEAT,
/**
* Inform target instance to start election.
*/
/** Inform target instance to start election. */
ELECTION_INVOKE,
/**
* Inform target instance to notify all the other instance that it is the new leader.
*/
/** Inform target instance to notify all the other instance that it is the new leader. */
LEADER_INVOKE,
/**
* Inform target instance to start heartbeat.
*/
/** Inform target instance to start heartbeat. */
HEARTBEAT_INVOKE
}
@@ -37,9 +37,7 @@ import java.util.Map;
*/
public class BullyApp {
/**
* Program entry point.
*/
/** Program entry point. */
public static void main(String[] args) {
Map<Integer, Instance> instanceMap = new HashMap<>();
@@ -42,9 +42,7 @@ import lombok.extern.slf4j.Slf4j;
public class BullyInstance extends AbstractInstance {
private static final String INSTANCE = "Instance ";
/**
* Constructor of BullyInstance.
*/
/** Constructor of BullyInstance. */
public BullyInstance(MessageManager messageManager, int localId, int leaderId) {
super(messageManager, localId, leaderId);
}
@@ -95,9 +93,7 @@ public class BullyInstance extends AbstractInstance {
}
}
/**
* Process leader message. Update local leader information.
*/
/** Process leader message. Update local leader information. */
@Override
protected void handleLeaderMessage(Message message) {
leaderId = Integer.parseInt(message.getContent());
@@ -31,14 +31,10 @@ import com.iluwatar.leaderelection.MessageType;
import java.util.List;
import java.util.Map;
/**
* Implementation of BullyMessageManager.
*/
/** Implementation of BullyMessageManager. */
public class BullyMessageManager extends AbstractMessageManager {
/**
* Constructor of BullyMessageManager.
*/
/** Constructor of BullyMessageManager. */
public BullyMessageManager(Map<Integer, Instance> instanceMap) {
super(instanceMap);
}
@@ -59,7 +55,7 @@ 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
@@ -78,14 +74,13 @@ public class BullyMessageManager extends AbstractMessageManager {
* 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) {
var leaderMessage = new Message(MessageType.LEADER, String.valueOf(leaderId));
instanceMap.keySet()
.stream()
instanceMap.keySet().stream()
.filter((i) -> i != currentId)
.forEach((i) -> instanceMap.get(i).onMessage(leaderMessage));
return false;
@@ -110,10 +105,8 @@ public class BullyMessageManager extends AbstractMessageManager {
* @return ID list of all the candidate instance.
*/
private List<Integer> findElectionCandidateInstanceList(int currentId) {
return instanceMap.keySet()
.stream()
return instanceMap.keySet().stream()
.filter((i) -> i < currentId && instanceMap.get(i).isAlive())
.toList();
}
}
@@ -37,9 +37,7 @@ import java.util.Map;
*/
public class RingApp {
/**
* Program entry point.
*/
/** Program entry point. */
public static void main(String[] args) {
Map<Integer, Instance> instanceMap = new HashMap<>();
@@ -45,9 +45,7 @@ import lombok.extern.slf4j.Slf4j;
public class RingInstance extends AbstractInstance {
private static final String INSTANCE = "Instance ";
/**
* Constructor of RingInstance.
*/
/** Constructor of RingInstance. */
public RingInstance(MessageManager messageManager, int localId, int leaderId) {
super(messageManager, localId, leaderId);
}
@@ -76,18 +74,16 @@ 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 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.
* 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.
*/
@Override
protected void handleElectionMessage(Message message) {
var content = message.getContent();
LOGGER.info(INSTANCE + localId + " - Election Message: " + content);
var candidateList = Arrays.stream(content.trim().split(","))
.map(Integer::valueOf)
.sorted()
.toList();
var candidateList =
Arrays.stream(content.trim().split(",")).map(Integer::valueOf).sorted().toList();
if (candidateList.contains(localId)) {
var newLeaderId = candidateList.get(0);
LOGGER.info(INSTANCE + localId + " - New leader should be " + newLeaderId + ".");
@@ -115,9 +111,7 @@ public class RingInstance extends AbstractInstance {
}
}
/**
* Not used in Ring instance.
*/
/** Not used in Ring instance. */
@Override
protected void handleLeaderInvokeMessage() {
// Not used in Ring instance.
@@ -132,5 +126,4 @@ public class RingInstance extends AbstractInstance {
protected void handleElectionInvokeMessage() {
// Not used in Ring instance.
}
}
@@ -30,14 +30,10 @@ 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 {
/**
* Constructor of RingMessageManager.
*/
/** Constructor of RingMessageManager. */
public RingMessageManager(Map<Integer, Instance> instanceMap) {
super(instanceMap);
}
@@ -58,8 +54,8 @@ 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
@@ -74,7 +70,7 @@ 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
@@ -96,5 +92,4 @@ public class RingMessageManager extends AbstractMessageManager {
var heartbeatInvokeMessage = new Message(MessageType.HEARTBEAT_INVOKE, "");
nextInstance.onMessage(heartbeatInvokeMessage);
}
}
@@ -28,9 +28,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
/**
* Message test case.
*/
/** Message test case. */
class MessageTest {
@Test
@@ -45,5 +43,4 @@ class MessageTest {
var message = new Message(MessageType.HEARTBEAT, content);
assertEquals(content, message.getContent());
}
}
@@ -24,18 +24,15 @@
*/
package com.iluwatar.leaderelection.bully;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
/**
* BullyApp unit test.
*/
import org.junit.jupiter.api.Test;
/** BullyApp unit test. */
class BullyAppTest {
@Test
void shouldExecuteApplicationWithoutException() {
assertDoesNotThrow(() -> BullyApp.main(new String[]{}));
assertDoesNotThrow(() -> BullyApp.main(new String[] {}));
}
}
@@ -37,9 +37,7 @@ import java.util.Map;
import java.util.Queue;
import org.junit.jupiter.api.Test;
/**
* BullyMessageManager unit test.
*/
/** BullyMessageManager unit test. */
class BullyMessageManagerTest {
@Test
@@ -57,7 +55,8 @@ class BullyMessageManagerTest {
var instance2 = new BullyInstance(null, 1, 2);
var instance3 = new BullyInstance(null, 1, 3);
var instance4 = new BullyInstance(null, 1, 4);
Map<Integer, Instance> instanceMap = Map.of(1, instance1, 2, instance2, 3, instance3, 4, instance4);
Map<Integer, Instance> instanceMap =
Map.of(1, instance1, 2, instance2, 3, instance3, 4, instance4);
instance1.setAlive(false);
var messageManager = new BullyMessageManager(instanceMap);
var result = messageManager.sendElectionMessage(3, "3");
@@ -81,7 +80,8 @@ class BullyMessageManagerTest {
var instance2 = new BullyInstance(null, 1, 2);
var instance3 = new BullyInstance(null, 1, 3);
var instance4 = new BullyInstance(null, 1, 4);
Map<Integer, Instance> instanceMap = Map.of(1, instance1, 2, instance2, 3, instance3, 4, instance4);
Map<Integer, Instance> instanceMap =
Map.of(1, instance1, 2, instance2, 3, instance3, 4, instance4);
instance1.setAlive(false);
var messageManager = new BullyMessageManager(instanceMap);
var result = messageManager.sendElectionMessage(2, "2");
@@ -95,7 +95,8 @@ class BullyMessageManagerTest {
var instance2 = new BullyInstance(null, 1, 2);
var instance3 = new BullyInstance(null, 1, 3);
var instance4 = new BullyInstance(null, 1, 4);
Map<Integer, Instance> instanceMap = Map.of(1, instance1, 2, instance2, 3, instance3, 4, instance4);
Map<Integer, Instance> instanceMap =
Map.of(1, instance1, 2, instance2, 3, instance3, 4, instance4);
instance1.setAlive(false);
var messageManager = new BullyMessageManager(instanceMap);
messageManager.sendLeaderMessage(2, 2);
@@ -132,6 +133,4 @@ class BullyMessageManagerTest {
fail("Error to access private field.");
}
}
}
@@ -34,9 +34,7 @@ import com.iluwatar.leaderelection.MessageType;
import java.util.Queue;
import org.junit.jupiter.api.Test;
/**
* BullyInstance unit test.
*/
/** BullyInstance unit test. */
class BullyinstanceTest {
@Test
@@ -52,7 +50,6 @@ class BullyinstanceTest {
} catch (IllegalAccessException | NoSuchFieldException e) {
fail("fail to access messasge queue.");
}
}
@Test
@@ -75,5 +72,4 @@ class BullyinstanceTest {
bullyInstance.setAlive(false);
assertFalse(bullyInstance.isAlive());
}
}
@@ -24,18 +24,15 @@
*/
package com.iluwatar.leaderelection.ring;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
/**
* RingApp unit test.
*/
import org.junit.jupiter.api.Test;
/** RingApp unit test. */
class RingAppTest {
@Test
void shouldExecuteApplicationWithoutException() {
assertDoesNotThrow(() -> RingApp.main(new String[]{}));
assertDoesNotThrow(() -> RingApp.main(new String[] {}));
}
}
@@ -34,9 +34,7 @@ import com.iluwatar.leaderelection.MessageType;
import java.util.Queue;
import org.junit.jupiter.api.Test;
/**
* RingInstance unit test.
*/
/** RingInstance unit test. */
class RingInstanceTest {
@Test
@@ -36,9 +36,7 @@ import java.util.Map;
import java.util.Queue;
import org.junit.jupiter.api.Test;
/**
* RingMessageManager unit test.
*/
/** RingMessageManager unit test. */
class RingMessageManagerTest {
@Test
@@ -112,5 +110,4 @@ class RingMessageManagerTest {
fail("Error to access private field.");
}
}
}