Leader Election Pattern (#923)

* Fix issue #761: ThreadSafeDoubleCheckLocking.java: Instantiating by Reflection call will be successful if you do that firstly

* Create leader election module

* Create Interface of Instance and MessageManager

* Create implementations with token ring algorithm

* Change package structure.
Create basic message system.

* Implement heartbeat and heartbeat invoking message system

* Implement election message handler

* Add leader message handler

* Add main entry point

* Add comments

* Update README.md

* Fix checkstyle issue

* Add Unit Tests

* Add Unit Tests

* Add bully leader selection

* Change System.out to log print.
Add MIT license in each file.

* Add More java doc comments

* Add unit test

* Add unit tests
This commit is contained in:
Azureyjt
2019-10-08 23:29:59 +08:00
committed by Ilkka Seppälä
parent 41b8d80479
commit 90ea4506ca
22 changed files with 1719 additions and 0 deletions

View File

@ -0,0 +1,48 @@
/**
* The MIT License
* Copyright (c) 2014-2016 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.leaderelection;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Message test case.
*/
public class MessageTest {
@Test
public void testGetType() {
Message message = new Message(MessageType.HEARTBEAT, "");
assertEquals(MessageType.HEARTBEAT, message.getType());
}
@Test
public void testGetContent() {
String content = "test";
Message message = new Message(MessageType.HEARTBEAT, content);
assertEquals(content, message.getContent());
}
}

View File

@ -0,0 +1,39 @@
/**
* The MIT License
* Copyright (c) 2014-2016 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.leaderelection.bully;
import org.junit.jupiter.api.Test;
/**
* BullyApp unit test.
*/
public class BullyAppTest {
@Test
public void test() {
String[] args = {};
BullyApp.main(args);
}
}

View File

@ -0,0 +1,151 @@
/**
* The MIT License
* Copyright (c) 2014-2016 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.leaderelection.bully;
import com.iluwatar.leaderelection.*;
import com.iluwatar.leaderelection.ring.RingInstance;
import com.iluwatar.leaderelection.ring.RingMessageManager;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Queue;
import static org.junit.jupiter.api.Assertions.*;
/**
* BullyMessageManager unit test.
*/
public class BullyMessageManagerTest {
@Test
public void testSendHeartbeatMessage() {
Instance instance1 = new BullyInstance(null, 1, 1);
Map<Integer, Instance> instanceMap = new HashMap<>();
instanceMap.put(1, instance1);
MessageManager messageManager = new BullyMessageManager(instanceMap);
assertTrue(messageManager.sendHeartbeatMessage(1));
}
@Test
public void testSendElectionMessageNotAccepted() {
try {
Instance instance1 = new BullyInstance(null, 1, 1);
Instance instance2 = new BullyInstance(null, 1, 2);
Instance instance3 = new BullyInstance(null, 1, 3);
Instance instance4 = new BullyInstance(null, 1, 4);
Map<Integer, Instance> instanceMap = new HashMap<>();
instanceMap.put(1, instance1);
instanceMap.put(2, instance2);
instanceMap.put(3, instance3);
instanceMap.put(4, instance4);
instance1.setAlive(false);
MessageManager messageManager = new BullyMessageManager(instanceMap);
boolean result = messageManager.sendElectionMessage(3, "3");
Class instanceClass = AbstractInstance.class;
Field messageQueueField = instanceClass.getDeclaredField("messageQueue");
messageQueueField.setAccessible(true);
Message message2 = ((Queue<Message>) messageQueueField.get(instance2)).poll();
int instance4QueueSize = ((Queue<Message>) messageQueueField.get(instance4)).size();
Message expectedMessage = new Message(MessageType.ELECTION_INVOKE, "");
assertEquals(message2, expectedMessage);
assertEquals(instance4QueueSize, 0);
assertEquals(result, false);
} catch (IllegalAccessException | NoSuchFieldException e) {
fail("Error to access private field.");
}
}
@Test
public void testElectionMessageAccepted() {
Instance instance1 = new BullyInstance(null, 1, 1);
Instance instance2 = new BullyInstance(null, 1, 2);
Instance instance3 = new BullyInstance(null, 1, 3);
Instance instance4 = new BullyInstance(null, 1, 4);
Map<Integer, Instance> instanceMap = new HashMap<>();
instanceMap.put(1, instance1);
instanceMap.put(2, instance2);
instanceMap.put(3, instance3);
instanceMap.put(4, instance4);
instance1.setAlive(false);
MessageManager messageManager = new BullyMessageManager(instanceMap);
boolean result = messageManager.sendElectionMessage(2, "2");
assertEquals(result, true);
}
@Test
public void testSendLeaderMessage() {
try {
Instance instance1 = new BullyInstance(null, 1, 1);
Instance instance2 = new BullyInstance(null, 1, 2);
Instance instance3 = new BullyInstance(null, 1, 3);
Instance instance4 = new BullyInstance(null, 1, 4);
Map<Integer, Instance> instanceMap = new HashMap<>();
instanceMap.put(1, instance1);
instanceMap.put(2, instance2);
instanceMap.put(3, instance3);
instanceMap.put(4, instance4);
instance1.setAlive(false);
MessageManager messageManager = new BullyMessageManager(instanceMap);
messageManager.sendLeaderMessage(2, 2);
Class instanceClass = AbstractInstance.class;
Field messageQueueField = instanceClass.getDeclaredField("messageQueue");
messageQueueField.setAccessible(true);
Message message3 = ((Queue<Message>) messageQueueField.get(instance3)).poll();
Message message4 = ((Queue<Message>) messageQueueField.get(instance4)).poll();
Message expectedMessage = new Message(MessageType.LEADER, "2");
assertEquals(message3, expectedMessage);
assertEquals(message4, expectedMessage);
} catch (IllegalAccessException | NoSuchFieldException e) {
fail("Error to access private field.");
}
}
@Test
public void testSendHeartbeatInvokeMessage() {
try {
Instance instance1 = new BullyInstance(null, 1, 1);
Instance instance2 = new BullyInstance(null, 1, 2);
Instance instance3 = new BullyInstance(null, 1, 3);
Map<Integer, Instance> instanceMap = new HashMap<>();
instanceMap.put(1, instance1);
instanceMap.put(2, instance2);
instanceMap.put(3, instance3);
MessageManager messageManager = new BullyMessageManager(instanceMap);
messageManager.sendHeartbeatInvokeMessage(2);
Message message = new Message(MessageType.HEARTBEAT_INVOKE, "");
Class instanceClass = AbstractInstance.class;
Field messageQueueField = instanceClass.getDeclaredField("messageQueue");
messageQueueField.setAccessible(true);
Message messageSent = ((Queue<Message>) messageQueueField.get(instance3)).poll();
assertEquals(messageSent.getType(), message.getType());
assertEquals(messageSent.getContent(), message.getContent());
} catch (NoSuchFieldException | IllegalAccessException e) {
fail("Error to access private field.");
}
}
}

View File

@ -0,0 +1,78 @@
/**
* The MIT License
* Copyright (c) 2014-2016 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.leaderelection.bully;
import com.iluwatar.leaderelection.AbstractInstance;
import com.iluwatar.leaderelection.Message;
import com.iluwatar.leaderelection.MessageType;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Field;
import java.util.Queue;
import static org.junit.jupiter.api.Assertions.*;
/**
* BullyInstance unit test.
*/
public class BullyinstanceTest {
@Test
public void testOnMessage() {
try {
final BullyInstance bullyInstance = new BullyInstance(null, 1, 1);
Message bullyMessage = new Message(MessageType.HEARTBEAT, "");
bullyInstance.onMessage(bullyMessage);
Class instanceClass = AbstractInstance.class;
Field messageQueueField = instanceClass.getDeclaredField("messageQueue");
messageQueueField.setAccessible(true);
assertEquals(bullyMessage, ((Queue<Message>) messageQueueField.get(bullyInstance)).poll());
} catch (IllegalAccessException | NoSuchFieldException e) {
fail("fail to access messasge queue.");
}
}
@Test
public void testIsAlive() {
try {
final BullyInstance bullyInstance = new BullyInstance(null, 1, 1);
Class instanceClass = AbstractInstance.class;
Field aliveField = instanceClass.getDeclaredField("alive");
aliveField.setAccessible(true);
aliveField.set(bullyInstance, false);
assertFalse(bullyInstance.isAlive());
} catch (NoSuchFieldException | IllegalAccessException e) {
fail("Fail to access field alive.");
}
}
@Test
public void testSetAlive() {
final BullyInstance bullyInstance = new BullyInstance(null, 1, 1);
bullyInstance.setAlive(false);
assertFalse(bullyInstance.isAlive());
}
}

View File

@ -0,0 +1,39 @@
/**
* The MIT License
* Copyright (c) 2014-2016 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.leaderelection.ring;
import org.junit.jupiter.api.Test;
/**
* RingApp unit test.
*/
public class RingAppTest {
@Test
public void test() {
String[] args = {};
RingApp.main(args);
}
}

View File

@ -0,0 +1,76 @@
/**
* The MIT License
* Copyright (c) 2014-2016 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.leaderelection.ring;
import com.iluwatar.leaderelection.AbstractInstance;
import com.iluwatar.leaderelection.Message;
import com.iluwatar.leaderelection.MessageType;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Field;
import java.util.Queue;
import static org.junit.jupiter.api.Assertions.*;
/**
* RingInstance unit test.
*/
public class RingInstanceTest {
@Test
public void testOnMessage() {
try {
final RingInstance ringInstance = new RingInstance(null, 1, 1);
Message ringMessage = new Message(MessageType.HEARTBEAT, "");
ringInstance.onMessage(ringMessage);
Class ringInstanceClass = AbstractInstance.class;
Field messageQueueField = ringInstanceClass.getDeclaredField("messageQueue");
messageQueueField.setAccessible(true);
assertEquals(ringMessage, ((Queue<Message>) messageQueueField.get(ringInstance)).poll());
} catch (IllegalAccessException | NoSuchFieldException e) {
fail("fail to access messasge queue.");
}
}
@Test
public void testIsAlive() {
try {
final RingInstance ringInstance = new RingInstance(null, 1, 1);
Class ringInstanceClass = AbstractInstance.class;
Field aliveField = ringInstanceClass.getDeclaredField("alive");
aliveField.setAccessible(true);
aliveField.set(ringInstance, false);
assertFalse(ringInstance.isAlive());
} catch (NoSuchFieldException | IllegalAccessException e) {
fail("Fail to access field alive.");
}
}
@Test
public void testSetAlive() {
final RingInstance ringInstance = new RingInstance(null, 1, 1);
ringInstance.setAlive(false);
assertFalse(ringInstance.isAlive());
}
}

View File

@ -0,0 +1,123 @@
/**
* The MIT License
* Copyright (c) 2014-2016 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.leaderelection.ring;
import com.iluwatar.leaderelection.*;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Queue;
import static org.junit.jupiter.api.Assertions.*;
/**
* RingMessageManager unit test.
*/
public class RingMessageManagerTest {
@Test
public void testSendHeartbeatMessage() {
Instance instance1 = new RingInstance(null, 1, 1);
Map<Integer, Instance> instanceMap = new HashMap<>();
instanceMap.put(1, instance1);
MessageManager messageManager = new RingMessageManager(instanceMap);
assertTrue(messageManager.sendHeartbeatMessage(1));
}
@Test
public void testSendElectionMessage() {
try {
Instance instance1 = new RingInstance(null, 1, 1);
Instance instance2 = new RingInstance(null, 1, 2);
Instance instance3 = new RingInstance(null, 1, 3);
Map<Integer, Instance> instanceMap = new HashMap<>();
instanceMap.put(1, instance1);
instanceMap.put(2, instance2);
instanceMap.put(3, instance3);
MessageManager messageManager = new RingMessageManager(instanceMap);
String messageContent = "2";
messageManager.sendElectionMessage(2, messageContent);
Message ringMessage = new Message(MessageType.ELECTION, messageContent);
Class instanceClass = AbstractInstance.class;
Field messageQueueField = instanceClass.getDeclaredField("messageQueue");
messageQueueField.setAccessible(true);
Message ringMessageSent = ((Queue<Message>) messageQueueField.get(instance3)).poll();
assertEquals(ringMessageSent.getType(), ringMessage.getType());
assertEquals(ringMessageSent.getContent(), ringMessage.getContent());
} catch (NoSuchFieldException | IllegalAccessException e) {
fail("Error to access private field.");
}
}
@Test
public void testSendLeaderMessage() {
try {
Instance instance1 = new RingInstance(null, 1, 1);
Instance instance2 = new RingInstance(null, 1, 2);
Instance instance3 = new RingInstance(null, 1, 3);
Map<Integer, Instance> instanceMap = new HashMap<>();
instanceMap.put(1, instance1);
instanceMap.put(2, instance2);
instanceMap.put(3, instance3);
MessageManager messageManager = new RingMessageManager(instanceMap);
String messageContent = "3";
messageManager.sendLeaderMessage(2, 3);
Message ringMessage = new Message(MessageType.LEADER, messageContent);
Class instanceClass = AbstractInstance.class;
Field messageQueueField = instanceClass.getDeclaredField("messageQueue");
messageQueueField.setAccessible(true);
Message ringMessageSent = ((Queue<Message>) messageQueueField.get(instance3)).poll();
assertEquals(ringMessageSent, ringMessage);
} catch (NoSuchFieldException | IllegalAccessException e) {
fail("Error to access private field.");
}
}
@Test
public void testSendHeartbeatInvokeMessage() {
try {
Instance instance1 = new RingInstance(null, 1, 1);
Instance instance2 = new RingInstance(null, 1, 2);
Instance instance3 = new RingInstance(null, 1, 3);
Map<Integer, Instance> instanceMap = new HashMap<>();
instanceMap.put(1, instance1);
instanceMap.put(2, instance2);
instanceMap.put(3, instance3);
MessageManager messageManager = new RingMessageManager(instanceMap);
messageManager.sendHeartbeatInvokeMessage(2);
Message ringMessage = new Message(MessageType.HEARTBEAT_INVOKE, "");
Class instanceClass = AbstractInstance.class;
Field messageQueueField = instanceClass.getDeclaredField("messageQueue");
messageQueueField.setAccessible(true);
Message ringMessageSent = ((Queue<Message>) messageQueueField.get(instance3)).poll();
assertEquals(ringMessageSent.getType(), ringMessage.getType());
assertEquals(ringMessageSent.getContent(), ringMessage.getContent());
} catch (NoSuchFieldException | IllegalAccessException e) {
fail("Error to access private field.");
}
}
}