|
| 1 | +/** |
| 2 | + * The MIT License |
| 3 | + * Copyright (c) 2014-2016 Ilkka Seppälä |
| 4 | + * <p> |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * <p> |
| 12 | + * The above copyright notice and this permission notice shall be included in |
| 13 | + * all copies or substantial portions of the Software. |
| 14 | + * <p> |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | + * THE SOFTWARE. |
| 22 | + */ |
| 23 | + |
| 24 | +package com.iluwatar.leaderelection; |
| 25 | + |
| 26 | +import org.slf4j.Logger; |
| 27 | +import org.slf4j.LoggerFactory; |
| 28 | + |
| 29 | +import java.util.Queue; |
| 30 | +import java.util.concurrent.ConcurrentLinkedQueue; |
| 31 | + |
| 32 | +/** |
| 33 | + * Abstract class of all the instance implementation classes. |
| 34 | + */ |
| 35 | +public abstract class AbstractInstance implements Instance, Runnable { |
| 36 | + |
| 37 | + private static final Logger LOGGER = LoggerFactory.getLogger(AbstractInstance.class); |
| 38 | + |
| 39 | + protected static final int HEARTBEAT_INTERVAL = 5000; |
| 40 | + |
| 41 | + protected MessageManager messageManager; |
| 42 | + protected Queue<Message> messageQueue; |
| 43 | + protected final int localId; |
| 44 | + protected int leaderId; |
| 45 | + protected boolean alive; |
| 46 | + |
| 47 | + /** |
| 48 | + * Constructor of BullyInstance. |
| 49 | + */ |
| 50 | + public AbstractInstance(MessageManager messageManager, int localId, int leaderId) { |
| 51 | + this.messageManager = messageManager; |
| 52 | + this.messageQueue = new ConcurrentLinkedQueue<>(); |
| 53 | + this.localId = localId; |
| 54 | + this.leaderId = leaderId; |
| 55 | + this.alive = true; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * The instance will execute the message in its message queue periodically once it is alive. |
| 60 | + */ |
| 61 | + @Override |
| 62 | + public void run() { |
| 63 | + while (true) { |
| 64 | + if (!this.messageQueue.isEmpty()) { |
| 65 | + this.processMessage(this.messageQueue.remove()); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Once messages are sent to the certain instance, it will firstly be added to the queue and wait to be executed. |
| 72 | + * @param message Message sent by other instances |
| 73 | + */ |
| 74 | + @Override |
| 75 | + public void onMessage(Message message) { |
| 76 | + messageQueue.offer(message); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Check if the instance is alive or not. |
| 81 | + * @return {@code true} if the instance is alive. |
| 82 | + */ |
| 83 | + @Override |
| 84 | + public boolean isAlive() { |
| 85 | + return alive; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Set the health status of the certain instance. |
| 90 | + * @param alive {@code true} for alive. |
| 91 | + */ |
| 92 | + @Override |
| 93 | + public void setAlive(boolean alive) { |
| 94 | + this.alive = alive; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Process the message according to its type. |
| 99 | + * @param message Message polled from queue. |
| 100 | + */ |
| 101 | + private void processMessage(Message message) { |
| 102 | + switch (message.getType()) { |
| 103 | + case ELECTION: |
| 104 | + LOGGER.info("Instance " + localId + " - Election Message handling..."); |
| 105 | + handleElectionMessage(message); |
| 106 | + break; |
| 107 | + case LEADER: |
| 108 | + LOGGER.info("Instance " + localId + " - Leader Message handling..."); |
| 109 | + handleLeaderMessage(message); |
| 110 | + break; |
| 111 | + case HEARTBEAT: |
| 112 | + LOGGER.info("Instance " + localId + " - Heartbeat Message handling..."); |
| 113 | + handleHeartbeatMessage(message); |
| 114 | + break; |
| 115 | + case ELECTION_INVOKE: |
| 116 | + LOGGER.info("Instance " + localId + " - Election Invoke Message handling..."); |
| 117 | + handleElectionInvokeMessage(); |
| 118 | + break; |
| 119 | + case LEADER_INVOKE: |
| 120 | + LOGGER.info("Instance " + localId + " - Leader Invoke Message handling..."); |
| 121 | + handleLeaderInvokeMessage(); |
| 122 | + break; |
| 123 | + case HEARTBEAT_INVOKE: |
| 124 | + LOGGER.info("Instance " + localId + " - Heartbeat Invoke Message handling..."); |
| 125 | + handleHeartbeatInvokeMessage(); |
| 126 | + break; |
| 127 | + default: |
| 128 | + break; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Abstract methods to handle different types of message. These methods need to be implemented in concrete instance |
| 134 | + * class to implement corresponding leader-selection pattern. |
| 135 | + */ |
| 136 | + protected abstract void handleElectionMessage(Message message); |
| 137 | + |
| 138 | + protected abstract void handleElectionInvokeMessage(); |
| 139 | + |
| 140 | + protected abstract void handleLeaderMessage(Message message); |
| 141 | + |
| 142 | + protected abstract void handleLeaderInvokeMessage(); |
| 143 | + |
| 144 | + protected abstract void handleHeartbeatMessage(Message message); |
| 145 | + |
| 146 | + protected abstract void handleHeartbeatInvokeMessage(); |
| 147 | + |
| 148 | +} |
0 commit comments