Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.store.cluster.impl; |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 17 | |
Jonathan Hart | 82b9fec | 2015-02-03 16:18:54 -0800 | [diff] [blame] | 18 | import com.google.common.collect.Maps; |
| 19 | import com.hazelcast.config.TopicConfig; |
| 20 | import com.hazelcast.core.IAtomicLong; |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame^] | 21 | |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
| 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 27 | import org.apache.felix.scr.annotations.Service; |
Jonathan Hart | 82b9fec | 2015-02-03 16:18:54 -0800 | [diff] [blame] | 28 | import org.onlab.util.KryoNamespace; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 29 | import org.onosproject.cluster.ClusterService; |
| 30 | import org.onosproject.cluster.Leadership; |
| 31 | import org.onosproject.cluster.LeadershipEvent; |
| 32 | import org.onosproject.cluster.LeadershipEventListener; |
| 33 | import org.onosproject.cluster.LeadershipService; |
| 34 | import org.onosproject.cluster.NodeId; |
| 35 | import org.onosproject.event.AbstractListenerRegistry; |
| 36 | import org.onosproject.event.EventDeliveryService; |
Madan Jampani | 97cf7c4 | 2015-02-19 16:33:37 -0800 | [diff] [blame] | 37 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 38 | import org.onosproject.store.cluster.messaging.ClusterMessage; |
| 39 | import org.onosproject.store.cluster.messaging.ClusterMessageHandler; |
| 40 | import org.onosproject.store.cluster.messaging.MessageSubject; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 41 | import org.onosproject.store.hz.StoreService; |
| 42 | import org.onosproject.store.serializers.KryoNamespaces; |
| 43 | import org.onosproject.store.serializers.KryoSerializer; |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 44 | import org.slf4j.Logger; |
| 45 | import org.slf4j.LoggerFactory; |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 46 | |
Jonathan Hart | 82b9fec | 2015-02-03 16:18:54 -0800 | [diff] [blame] | 47 | import java.util.HashMap; |
| 48 | import java.util.Map; |
| 49 | import java.util.concurrent.ExecutorService; |
| 50 | import java.util.concurrent.Executors; |
| 51 | import java.util.concurrent.Future; |
| 52 | import java.util.concurrent.locks.Lock; |
| 53 | |
| 54 | import static com.google.common.base.Preconditions.checkArgument; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 55 | import static org.onlab.util.Tools.groupedThreads; |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 56 | |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 57 | /** |
| 58 | * Distributed implementation of LeadershipService that is based on Hazelcast. |
| 59 | * <p> |
| 60 | * The election is eventually-consistent: if there is Hazelcast partitioning, |
| 61 | * and the partitioning is healed, there could be a short window of time |
| 62 | * until the leaders in each partition discover each other. If this happens, |
| 63 | * the leaders release the leadership and run again for election. |
| 64 | * </p> |
| 65 | * <p> |
| 66 | * The leader election is based on Hazelcast's Global Lock, which is stongly |
| 67 | * consistent. In addition, each leader periodically advertises events |
| 68 | * (using a Hazelcast Topic) that it is the elected leader. Those events are |
| 69 | * used for two purposes: (1) Discover multi-leader collisions (in case of |
| 70 | * healed Hazelcast partitions), and (2) Inform all listeners who is |
| 71 | * the current leader (e.g., for informational purpose). |
| 72 | * </p> |
| 73 | */ |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 74 | @Component(immediate = true) |
| 75 | @Service |
Madan Jampani | 97cf7c4 | 2015-02-19 16:33:37 -0800 | [diff] [blame] | 76 | public class HazelcastLeadershipService implements LeadershipService { |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 77 | private static final Logger log = |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 78 | LoggerFactory.getLogger(HazelcastLeadershipService.class); |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 79 | |
| 80 | private static final KryoSerializer SERIALIZER = new KryoSerializer() { |
| 81 | @Override |
| 82 | protected void setupKryoPool() { |
| 83 | serializerPool = KryoNamespace.newBuilder() |
| 84 | .register(KryoNamespaces.API) |
| 85 | .build() |
| 86 | .populate(1); |
| 87 | } |
| 88 | }; |
| 89 | |
| 90 | private static final long LEADERSHIP_PERIODIC_INTERVAL_MS = 5 * 1000; // 5s |
| 91 | private static final long LEADERSHIP_REMOTE_TIMEOUT_MS = 15 * 1000; // 15s |
Pavlin Radoslavov | bcb454c | 2014-12-02 11:47:47 -0800 | [diff] [blame] | 92 | private static final String TOPIC_HZ_ID = "LeadershipService/AllTopics"; |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 93 | |
Jonathan Hart | 82b9fec | 2015-02-03 16:18:54 -0800 | [diff] [blame] | 94 | // indicates there is no term value yet |
| 95 | private static final long NO_TERM = 0; |
| 96 | |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 97 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Madan Jampani | 97cf7c4 | 2015-02-19 16:33:37 -0800 | [diff] [blame] | 98 | protected ClusterCommunicationService clusterCommunicator; |
| 99 | |
| 100 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 101 | protected ClusterService clusterService; |
| 102 | |
| 103 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 104 | protected StoreService storeService; |
| 105 | |
| 106 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 107 | protected EventDeliveryService eventDispatcher; |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 108 | |
| 109 | private AbstractListenerRegistry<LeadershipEvent, LeadershipEventListener> |
| 110 | listenerRegistry; |
| 111 | private final Map<String, Topic> topics = Maps.newConcurrentMap(); |
Madan Jampani | 8d21c79 | 2014-12-01 16:31:07 -0800 | [diff] [blame] | 112 | private NodeId localNodeId; |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 113 | |
Madan Jampani | 97cf7c4 | 2015-02-19 16:33:37 -0800 | [diff] [blame] | 114 | private static final MessageSubject LEADERSHIP_EVENT_MESSAGE_SUBJECT = |
| 115 | new MessageSubject("hz-leadership-events"); |
Pavlin Radoslavov | bcb454c | 2014-12-02 11:47:47 -0800 | [diff] [blame] | 116 | |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame^] | 117 | private ExecutorService messageHandlingExecutor; |
| 118 | |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 119 | @Activate |
| 120 | protected void activate() { |
Madan Jampani | 8d21c79 | 2014-12-01 16:31:07 -0800 | [diff] [blame] | 121 | localNodeId = clusterService.getLocalNode().id(); |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 122 | listenerRegistry = new AbstractListenerRegistry<>(); |
| 123 | eventDispatcher.addSink(LeadershipEvent.class, listenerRegistry); |
| 124 | |
Pavlin Radoslavov | bcb454c | 2014-12-02 11:47:47 -0800 | [diff] [blame] | 125 | TopicConfig topicConfig = new TopicConfig(); |
| 126 | topicConfig.setGlobalOrderingEnabled(true); |
| 127 | topicConfig.setName(TOPIC_HZ_ID); |
| 128 | storeService.getHazelcastInstance().getConfig().addTopicConfig(topicConfig); |
Madan Jampani | 97cf7c4 | 2015-02-19 16:33:37 -0800 | [diff] [blame] | 129 | |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame^] | 130 | messageHandlingExecutor = Executors.newSingleThreadExecutor( |
| 131 | groupedThreads("onos/store/leadership", "message-handler")); |
| 132 | |
| 133 | clusterCommunicator.addSubscriber( |
| 134 | LEADERSHIP_EVENT_MESSAGE_SUBJECT, |
| 135 | new InternalLeadershipEventListener(), |
| 136 | messageHandlingExecutor); |
Pavlin Radoslavov | bcb454c | 2014-12-02 11:47:47 -0800 | [diff] [blame] | 137 | |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 138 | log.info("Hazelcast Leadership Service started"); |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 141 | @Deactivate |
| 142 | protected void deactivate() { |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 143 | eventDispatcher.removeSink(LeadershipEvent.class); |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame^] | 144 | messageHandlingExecutor.shutdown(); |
Madan Jampani | 97cf7c4 | 2015-02-19 16:33:37 -0800 | [diff] [blame] | 145 | clusterCommunicator.removeSubscriber(LEADERSHIP_EVENT_MESSAGE_SUBJECT); |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 146 | |
| 147 | for (Topic topic : topics.values()) { |
| 148 | topic.stop(); |
| 149 | } |
| 150 | topics.clear(); |
| 151 | |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 152 | log.info("Hazelcast Leadership Service stopped"); |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | @Override |
Madan Jampani | 8d21c79 | 2014-12-01 16:31:07 -0800 | [diff] [blame] | 156 | public NodeId getLeader(String path) { |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 157 | Topic topic = topics.get(path); |
| 158 | if (topic == null) { |
| 159 | return null; |
| 160 | } |
| 161 | return topic.leader(); |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public void runForLeadership(String path) { |
| 166 | checkArgument(path != null); |
| 167 | Topic topic = new Topic(path); |
| 168 | Topic oldTopic = topics.putIfAbsent(path, topic); |
| 169 | if (oldTopic == null) { |
| 170 | topic.start(); |
Pavlin Radoslavov | bcb454c | 2014-12-02 11:47:47 -0800 | [diff] [blame] | 171 | topic.runForLeadership(); |
| 172 | } else { |
| 173 | oldTopic.runForLeadership(); |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public void withdraw(String path) { |
| 179 | checkArgument(path != null); |
| 180 | Topic topic = topics.get(path); |
| 181 | if (topic != null) { |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 182 | topics.remove(path, topic); |
Yuta HIGUCHI | 8f06827 | 2015-02-18 22:28:32 +0900 | [diff] [blame] | 183 | topic.stop(); |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
| 187 | @Override |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 188 | public Map<String, Leadership> getLeaderBoard() { |
Pavlin Radoslavov | 187dff6 | 2014-12-02 07:21:35 -0800 | [diff] [blame] | 189 | Map<String, Leadership> result = new HashMap<>(); |
| 190 | |
Pavlin Radoslavov | 187dff6 | 2014-12-02 07:21:35 -0800 | [diff] [blame] | 191 | for (Topic topic : topics.values()) { |
| 192 | Leadership leadership = new Leadership(topic.topicName(), |
| 193 | topic.leader(), |
Jonathan Hart | 82b9fec | 2015-02-03 16:18:54 -0800 | [diff] [blame] | 194 | topic.term()); |
Pavlin Radoslavov | 187dff6 | 2014-12-02 07:21:35 -0800 | [diff] [blame] | 195 | result.put(topic.topicName(), leadership); |
| 196 | } |
| 197 | return result; |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | @Override |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 201 | public void addListener(LeadershipEventListener listener) { |
| 202 | listenerRegistry.addListener(listener); |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public void removeListener(LeadershipEventListener listener) { |
| 207 | listenerRegistry.removeListener(listener); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Class for keeping per-topic information. |
| 212 | */ |
Pavlin Radoslavov | bcb454c | 2014-12-02 11:47:47 -0800 | [diff] [blame] | 213 | private final class Topic { |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 214 | private final String topicName; |
| 215 | private volatile boolean isShutdown = true; |
Pavlin Radoslavov | bcb454c | 2014-12-02 11:47:47 -0800 | [diff] [blame] | 216 | private volatile boolean isRunningForLeadership = false; |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 217 | private volatile long lastLeadershipUpdateMs = 0; |
| 218 | private ExecutorService leaderElectionExecutor; |
| 219 | |
Jonathan Hart | 82b9fec | 2015-02-03 16:18:54 -0800 | [diff] [blame] | 220 | private volatile IAtomicLong term; |
| 221 | // This is local state, recording the term number for the last time |
| 222 | // this instance was leader for this topic. The current term could be |
| 223 | // higher if the mastership has changed any times. |
| 224 | private long myLastLeaderTerm = NO_TERM; |
| 225 | |
Madan Jampani | 8d21c79 | 2014-12-01 16:31:07 -0800 | [diff] [blame] | 226 | private NodeId leader; |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 227 | private Lock leaderLock; |
| 228 | private Future<?> getLockFuture; |
| 229 | private Future<?> periodicProcessingFuture; |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 230 | |
| 231 | /** |
| 232 | * Constructor. |
| 233 | * |
| 234 | * @param topicName the topic name |
| 235 | */ |
| 236 | private Topic(String topicName) { |
| 237 | this.topicName = topicName; |
| 238 | } |
| 239 | |
| 240 | /** |
Pavlin Radoslavov | 187dff6 | 2014-12-02 07:21:35 -0800 | [diff] [blame] | 241 | * Gets the topic name. |
| 242 | * |
| 243 | * @return the topic name |
| 244 | */ |
| 245 | private String topicName() { |
| 246 | return topicName; |
| 247 | } |
| 248 | |
| 249 | /** |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 250 | * Gets the leader for the topic. |
| 251 | * |
| 252 | * @return the leader for the topic |
| 253 | */ |
Madan Jampani | 8d21c79 | 2014-12-01 16:31:07 -0800 | [diff] [blame] | 254 | private NodeId leader() { |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 255 | return leader; |
| 256 | } |
| 257 | |
| 258 | /** |
Jonathan Hart | 82b9fec | 2015-02-03 16:18:54 -0800 | [diff] [blame] | 259 | * Gets the current term for the topic. |
| 260 | * |
| 261 | * @return the term for the topic |
| 262 | */ |
| 263 | private long term() { |
| 264 | if (term == null) { |
| 265 | return NO_TERM; |
| 266 | } |
| 267 | return term.get(); |
| 268 | } |
| 269 | |
| 270 | /** |
Pavlin Radoslavov | bcb454c | 2014-12-02 11:47:47 -0800 | [diff] [blame] | 271 | * Starts operation. |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 272 | */ |
Yuta HIGUCHI | 8f06827 | 2015-02-18 22:28:32 +0900 | [diff] [blame] | 273 | private synchronized void start() { |
| 274 | if (!isShutdown) { |
| 275 | // already running |
| 276 | return; |
| 277 | } |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 278 | isShutdown = false; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 279 | String threadPoolName = "election-" + topicName + "-%d"; |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 280 | leaderElectionExecutor = Executors.newScheduledThreadPool(2, |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 281 | groupedThreads("onos/leadership", threadPoolName)); |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 282 | |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 283 | periodicProcessingFuture = |
| 284 | leaderElectionExecutor.submit(new Runnable() { |
| 285 | @Override |
| 286 | public void run() { |
| 287 | doPeriodicProcessing(); |
| 288 | } |
| 289 | }); |
| 290 | } |
| 291 | |
| 292 | /** |
Pavlin Radoslavov | bcb454c | 2014-12-02 11:47:47 -0800 | [diff] [blame] | 293 | * Runs for leadership. |
| 294 | */ |
Yuta HIGUCHI | 8f06827 | 2015-02-18 22:28:32 +0900 | [diff] [blame] | 295 | private synchronized void runForLeadership() { |
Pavlin Radoslavov | bcb454c | 2014-12-02 11:47:47 -0800 | [diff] [blame] | 296 | if (isRunningForLeadership) { |
| 297 | return; // Nothing to do: already running |
| 298 | } |
| 299 | if (isShutdown) { |
| 300 | start(); |
| 301 | } |
Yuta HIGUCHI | 8f06827 | 2015-02-18 22:28:32 +0900 | [diff] [blame] | 302 | isRunningForLeadership = true; |
Pavlin Radoslavov | bcb454c | 2014-12-02 11:47:47 -0800 | [diff] [blame] | 303 | String lockHzId = "LeadershipService/" + topicName + "/lock"; |
Jonathan Hart | 82b9fec | 2015-02-03 16:18:54 -0800 | [diff] [blame] | 304 | String termHzId = "LeadershipService/" + topicName + "/term"; |
Pavlin Radoslavov | bcb454c | 2014-12-02 11:47:47 -0800 | [diff] [blame] | 305 | leaderLock = storeService.getHazelcastInstance().getLock(lockHzId); |
Jonathan Hart | 82b9fec | 2015-02-03 16:18:54 -0800 | [diff] [blame] | 306 | term = storeService.getHazelcastInstance().getAtomicLong(termHzId); |
| 307 | |
Pavlin Radoslavov | bcb454c | 2014-12-02 11:47:47 -0800 | [diff] [blame] | 308 | getLockFuture = leaderElectionExecutor.submit(new Runnable() { |
| 309 | @Override |
| 310 | public void run() { |
| 311 | doLeaderElectionThread(); |
| 312 | } |
| 313 | }); |
| 314 | } |
| 315 | |
| 316 | /** |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 317 | * Stops leadership election for the topic. |
| 318 | */ |
Yuta HIGUCHI | 8f06827 | 2015-02-18 22:28:32 +0900 | [diff] [blame] | 319 | private synchronized void stop() { |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 320 | isShutdown = true; |
Pavlin Radoslavov | bcb454c | 2014-12-02 11:47:47 -0800 | [diff] [blame] | 321 | isRunningForLeadership = false; |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 322 | // getLockFuture.cancel(true); |
| 323 | // periodicProcessingFuture.cancel(true); |
| 324 | leaderElectionExecutor.shutdownNow(); |
| 325 | } |
| 326 | |
Pavlin Radoslavov | bcb454c | 2014-12-02 11:47:47 -0800 | [diff] [blame] | 327 | /** |
| 328 | * Received a Leadership Event. |
| 329 | * |
| 330 | * @param leadershipEvent the received Leadership Event |
| 331 | */ |
| 332 | private void receivedLeadershipEvent(LeadershipEvent leadershipEvent) { |
Madan Jampani | 8d21c79 | 2014-12-01 16:31:07 -0800 | [diff] [blame] | 333 | NodeId eventLeaderId = leadershipEvent.subject().leader(); |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 334 | if (!leadershipEvent.subject().topic().equals(topicName)) { |
| 335 | return; // Not our topic: ignore |
| 336 | } |
Madan Jampani | 8d21c79 | 2014-12-01 16:31:07 -0800 | [diff] [blame] | 337 | if (eventLeaderId.equals(localNodeId)) { |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 338 | return; // My own message: ignore |
| 339 | } |
| 340 | |
| 341 | synchronized (this) { |
| 342 | switch (leadershipEvent.type()) { |
| 343 | case LEADER_ELECTED: |
| 344 | // FALLTHROUGH |
| 345 | case LEADER_REELECTED: |
| 346 | // |
| 347 | // Another leader: if we are also a leader, then give up |
| 348 | // leadership and run for re-election. |
| 349 | // |
Pavlin Radoslavov | bcb454c | 2014-12-02 11:47:47 -0800 | [diff] [blame] | 350 | if ((leader != null) && leader.equals(localNodeId)) { |
| 351 | if (getLockFuture != null) { |
| 352 | getLockFuture.cancel(true); |
| 353 | } |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 354 | } else { |
| 355 | // Just update the current leader |
| 356 | leader = leadershipEvent.subject().leader(); |
| 357 | lastLeadershipUpdateMs = System.currentTimeMillis(); |
| 358 | } |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 359 | break; |
| 360 | case LEADER_BOOTED: |
| 361 | // Remove the state for the current leader |
Pavlin Radoslavov | bcb454c | 2014-12-02 11:47:47 -0800 | [diff] [blame] | 362 | if ((leader != null) && eventLeaderId.equals(leader)) { |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 363 | leader = null; |
| 364 | } |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 365 | break; |
| 366 | default: |
| 367 | break; |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | private void doPeriodicProcessing() { |
| 373 | |
| 374 | while (!isShutdown) { |
| 375 | |
| 376 | // |
| 377 | // Periodic tasks: |
| 378 | // (a) Advertise ourselves as the leader |
| 379 | // OR |
| 380 | // (b) Expire a stale (remote) leader |
| 381 | // |
| 382 | synchronized (this) { |
| 383 | LeadershipEvent leadershipEvent; |
| 384 | if (leader != null) { |
Madan Jampani | 8d21c79 | 2014-12-01 16:31:07 -0800 | [diff] [blame] | 385 | if (leader.equals(localNodeId)) { |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 386 | // |
| 387 | // Advertise ourselves as the leader |
| 388 | // |
| 389 | leadershipEvent = new LeadershipEvent( |
| 390 | LeadershipEvent.Type.LEADER_REELECTED, |
Jonathan Hart | 82b9fec | 2015-02-03 16:18:54 -0800 | [diff] [blame] | 391 | new Leadership(topicName, localNodeId, myLastLeaderTerm)); |
Pavlin Radoslavov | bcb454c | 2014-12-02 11:47:47 -0800 | [diff] [blame] | 392 | // Dispatch to all instances |
Madan Jampani | 97cf7c4 | 2015-02-19 16:33:37 -0800 | [diff] [blame] | 393 | |
| 394 | clusterCommunicator.broadcastIncludeSelf( |
| 395 | new ClusterMessage( |
| 396 | clusterService.getLocalNode().id(), |
| 397 | LEADERSHIP_EVENT_MESSAGE_SUBJECT, |
| 398 | SERIALIZER.encode(leadershipEvent))); |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 399 | } else { |
| 400 | // |
| 401 | // Test if time to expire a stale leader |
| 402 | // |
| 403 | long delta = System.currentTimeMillis() - |
| 404 | lastLeadershipUpdateMs; |
| 405 | if (delta > LEADERSHIP_REMOTE_TIMEOUT_MS) { |
| 406 | leadershipEvent = new LeadershipEvent( |
| 407 | LeadershipEvent.Type.LEADER_BOOTED, |
Jonathan Hart | 82b9fec | 2015-02-03 16:18:54 -0800 | [diff] [blame] | 408 | new Leadership(topicName, leader, myLastLeaderTerm)); |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 409 | // Dispatch only to the local listener(s) |
| 410 | eventDispatcher.post(leadershipEvent); |
| 411 | leader = null; |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | // Sleep before re-advertising |
| 418 | try { |
| 419 | Thread.sleep(LEADERSHIP_PERIODIC_INTERVAL_MS); |
| 420 | } catch (InterruptedException e) { |
Pavlin Radoslavov | 2e57b3b | 2014-11-30 18:36:05 -0800 | [diff] [blame] | 421 | log.debug("Leader Election periodic thread interrupted"); |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * Performs the leader election by using Hazelcast. |
| 428 | */ |
| 429 | private void doLeaderElectionThread() { |
| 430 | |
| 431 | while (!isShutdown) { |
| 432 | LeadershipEvent leadershipEvent; |
| 433 | // |
| 434 | // Try to acquire the lock and keep it until the instance is |
| 435 | // shutdown. |
| 436 | // |
Pavlin Radoslavov | 2e57b3b | 2014-11-30 18:36:05 -0800 | [diff] [blame] | 437 | log.debug("Leader Election begin for topic {}", |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 438 | topicName); |
| 439 | try { |
| 440 | // Block until it becomes the leader |
| 441 | leaderLock.lockInterruptibly(); |
| 442 | } catch (InterruptedException e) { |
| 443 | // |
| 444 | // Thread interrupted. Either shutdown or run for |
| 445 | // re-election. |
| 446 | // |
Pavlin Radoslavov | 2e57b3b | 2014-11-30 18:36:05 -0800 | [diff] [blame] | 447 | log.debug("Election interrupted for topic {}", |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 448 | topicName); |
| 449 | continue; |
| 450 | } |
| 451 | |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 452 | try { |
Yuta HIGUCHI | 8f06827 | 2015-02-18 22:28:32 +0900 | [diff] [blame] | 453 | synchronized (this) { |
| 454 | // |
| 455 | // This instance is now the leader |
| 456 | // |
| 457 | log.info("Leader Elected for topic {}", topicName); |
| 458 | |
| 459 | updateTerm(); |
| 460 | |
| 461 | leader = localNodeId; |
| 462 | leadershipEvent = new LeadershipEvent( |
| 463 | LeadershipEvent.Type.LEADER_ELECTED, |
| 464 | new Leadership(topicName, localNodeId, myLastLeaderTerm)); |
Madan Jampani | 97cf7c4 | 2015-02-19 16:33:37 -0800 | [diff] [blame] | 465 | clusterCommunicator.broadcastIncludeSelf( |
| 466 | new ClusterMessage( |
| 467 | clusterService.getLocalNode().id(), |
| 468 | LEADERSHIP_EVENT_MESSAGE_SUBJECT, |
| 469 | SERIALIZER.encode(leadershipEvent))); |
Yuta HIGUCHI | 8f06827 | 2015-02-18 22:28:32 +0900 | [diff] [blame] | 470 | } |
| 471 | |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 472 | // Sleep forever until interrupted |
| 473 | Thread.sleep(Long.MAX_VALUE); |
| 474 | } catch (InterruptedException e) { |
| 475 | // |
| 476 | // Thread interrupted. Either shutdown or run for |
| 477 | // re-election. |
| 478 | // |
Pavlin Radoslavov | 2e57b3b | 2014-11-30 18:36:05 -0800 | [diff] [blame] | 479 | log.debug("Leader Interrupted for topic {}", |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 480 | topicName); |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 481 | |
Yuta HIGUCHI | 8f06827 | 2015-02-18 22:28:32 +0900 | [diff] [blame] | 482 | } finally { |
| 483 | synchronized (this) { |
| 484 | // If we reach here, we should release the leadership |
| 485 | log.debug("Leader Lock Released for topic {}", topicName); |
| 486 | if ((leader != null) && |
| 487 | leader.equals(localNodeId)) { |
| 488 | leader = null; |
| 489 | } |
| 490 | leadershipEvent = new LeadershipEvent( |
| 491 | LeadershipEvent.Type.LEADER_BOOTED, |
| 492 | new Leadership(topicName, localNodeId, myLastLeaderTerm)); |
Madan Jampani | 97cf7c4 | 2015-02-19 16:33:37 -0800 | [diff] [blame] | 493 | clusterCommunicator.broadcastIncludeSelf( |
| 494 | new ClusterMessage( |
| 495 | clusterService.getLocalNode().id(), |
| 496 | LEADERSHIP_EVENT_MESSAGE_SUBJECT, |
| 497 | SERIALIZER.encode(leadershipEvent))); |
Yuta HIGUCHI | 8f06827 | 2015-02-18 22:28:32 +0900 | [diff] [blame] | 498 | leaderLock.unlock(); |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 499 | } |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 500 | } |
| 501 | } |
Yuta HIGUCHI | 8f06827 | 2015-02-18 22:28:32 +0900 | [diff] [blame] | 502 | isRunningForLeadership = false; |
Jonathan Hart | 82b9fec | 2015-02-03 16:18:54 -0800 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | // Globally guarded by the leadership lock for this term |
| 506 | // Locally guarded by synchronized (this) |
| 507 | private void updateTerm() { |
| 508 | long oldTerm = term.get(); |
| 509 | long newTerm = term.incrementAndGet(); |
| 510 | myLastLeaderTerm = newTerm; |
| 511 | log.debug("Topic {} updated term from {} to {}", topicName, |
| 512 | oldTerm, newTerm); |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 513 | } |
| 514 | } |
Madan Jampani | 97cf7c4 | 2015-02-19 16:33:37 -0800 | [diff] [blame] | 515 | |
| 516 | private class InternalLeadershipEventListener implements ClusterMessageHandler { |
| 517 | |
| 518 | @Override |
| 519 | public void handle(ClusterMessage message) { |
| 520 | LeadershipEvent leadershipEvent = |
| 521 | SERIALIZER.decode(message.payload()); |
| 522 | |
| 523 | log.trace("Leadership Event: time = {} type = {} event = {}", |
| 524 | leadershipEvent.time(), leadershipEvent.type(), |
| 525 | leadershipEvent); |
| 526 | // |
| 527 | // If there is no entry for the topic, then create a new one to |
| 528 | // keep track of the leadership, but don't run for leadership itself. |
| 529 | // |
| 530 | String topicName = leadershipEvent.subject().topic(); |
| 531 | Topic topic = topics.get(topicName); |
| 532 | if (topic == null) { |
| 533 | topic = new Topic(topicName); |
| 534 | Topic oldTopic = topics.putIfAbsent(topicName, topic); |
| 535 | if (oldTopic == null) { |
| 536 | // encountered new topic, start periodic processing |
| 537 | topic.start(); |
| 538 | } else { |
| 539 | topic = oldTopic; |
| 540 | } |
| 541 | } |
| 542 | topic.receivedLeadershipEvent(leadershipEvent); |
| 543 | eventDispatcher.post(leadershipEvent); |
| 544 | } |
| 545 | } |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 546 | } |