Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.store.primitives.resources.impl; |
| 17 | |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 18 | import java.util.Arrays; |
| 19 | import java.util.HashMap; |
| 20 | import java.util.LinkedHashMap; |
| 21 | import java.util.LinkedList; |
| 22 | import java.util.List; |
| 23 | import java.util.Map; |
| 24 | import java.util.Optional; |
| 25 | import java.util.Set; |
| 26 | import java.util.concurrent.atomic.AtomicLong; |
| 27 | import java.util.function.Supplier; |
| 28 | import java.util.stream.Collectors; |
| 29 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 30 | import com.google.common.base.MoreObjects; |
| 31 | import com.google.common.base.Objects; |
| 32 | import com.google.common.base.Throwables; |
| 33 | import com.google.common.collect.ImmutableSet; |
| 34 | import com.google.common.collect.Lists; |
| 35 | import com.google.common.collect.Maps; |
| 36 | import com.google.common.collect.Sets; |
| 37 | import io.atomix.protocols.raft.service.AbstractRaftService; |
| 38 | import io.atomix.protocols.raft.service.Commit; |
| 39 | import io.atomix.protocols.raft.service.RaftServiceExecutor; |
| 40 | import io.atomix.protocols.raft.session.RaftSession; |
| 41 | import io.atomix.protocols.raft.storage.snapshot.SnapshotReader; |
| 42 | import io.atomix.protocols.raft.storage.snapshot.SnapshotWriter; |
| 43 | import org.onlab.util.KryoNamespace; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 44 | import org.onosproject.cluster.Leader; |
| 45 | import org.onosproject.cluster.Leadership; |
| 46 | import org.onosproject.cluster.NodeId; |
| 47 | import org.onosproject.event.Change; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 48 | import org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorOperations.Anoint; |
| 49 | import org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorOperations.Evict; |
| 50 | import org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorOperations.GetElectedTopics; |
| 51 | import org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorOperations.GetLeadership; |
| 52 | import org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorOperations.Promote; |
| 53 | import org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorOperations.Run; |
| 54 | import org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorOperations.Withdraw; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 55 | import org.onosproject.store.service.Serializer; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 56 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 57 | import static org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorEvents.CHANGE; |
| 58 | import static org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorOperations.ADD_LISTENER; |
| 59 | import static org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorOperations.ANOINT; |
| 60 | import static org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorOperations.EVICT; |
| 61 | import static org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorOperations.GET_ALL_LEADERSHIPS; |
| 62 | import static org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorOperations.GET_ELECTED_TOPICS; |
| 63 | import static org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorOperations.GET_LEADERSHIP; |
| 64 | import static org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorOperations.PROMOTE; |
| 65 | import static org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorOperations.REMOVE_LISTENER; |
| 66 | import static org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorOperations.RUN; |
| 67 | import static org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorOperations.WITHDRAW; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * State machine for {@link AtomixLeaderElector} resource. |
| 71 | */ |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 72 | public class AtomixLeaderElectorService extends AbstractRaftService { |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 73 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 74 | private static final Serializer SERIALIZER = Serializer.using(KryoNamespace.newBuilder() |
| 75 | .register(AtomixLeaderElectorOperations.NAMESPACE) |
| 76 | .register(AtomixLeaderElectorEvents.NAMESPACE) |
| 77 | .register(ElectionState.class) |
| 78 | .register(Registration.class) |
| 79 | .register(new LinkedHashMap<>().keySet().getClass()) |
| 80 | .build()); |
| 81 | |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 82 | private Map<String, AtomicLong> termCounters = new HashMap<>(); |
| 83 | private Map<String, ElectionState> elections = new HashMap<>(); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 84 | private Map<Long, RaftSession> listeners = new LinkedHashMap<>(); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 85 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 86 | @Override |
| 87 | public void snapshot(SnapshotWriter writer) { |
| 88 | writer.writeObject(Sets.newHashSet(listeners.keySet()), SERIALIZER::encode); |
| 89 | writer.writeObject(termCounters, SERIALIZER::encode); |
| 90 | writer.writeObject(elections, SERIALIZER::encode); |
Jordan Halterman | 6807c8f | 2017-08-28 20:58:24 -0700 | [diff] [blame^] | 91 | logger().debug("Took state machine snapshot"); |
Madan Jampani | 86cb243 | 2016-02-17 11:07:56 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 94 | @Override |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 95 | public void install(SnapshotReader reader) { |
| 96 | listeners = new LinkedHashMap<>(); |
| 97 | for (Long sessionId : reader.<Set<Long>>readObject(SERIALIZER::decode)) { |
Jordan Halterman | 6807c8f | 2017-08-28 20:58:24 -0700 | [diff] [blame^] | 98 | listeners.put(sessionId, sessions().getSession(sessionId)); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 99 | } |
| 100 | termCounters = reader.readObject(SERIALIZER::decode); |
| 101 | elections = reader.readObject(SERIALIZER::decode); |
Jordan Halterman | 6807c8f | 2017-08-28 20:58:24 -0700 | [diff] [blame^] | 102 | logger().debug("Reinstated state machine from snapshot"); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | @Override |
| 106 | protected void configure(RaftServiceExecutor executor) { |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 107 | // Notification |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 108 | executor.register(ADD_LISTENER, this::listen); |
| 109 | executor.register(REMOVE_LISTENER, this::unlisten); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 110 | // Commands |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 111 | executor.register(RUN, SERIALIZER::decode, this::run, SERIALIZER::encode); |
| 112 | executor.register(WITHDRAW, SERIALIZER::decode, this::withdraw); |
| 113 | executor.register(ANOINT, SERIALIZER::decode, this::anoint, SERIALIZER::encode); |
| 114 | executor.register(PROMOTE, SERIALIZER::decode, this::promote, SERIALIZER::encode); |
| 115 | executor.register(EVICT, SERIALIZER::decode, this::evict); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 116 | // Queries |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 117 | executor.register(GET_LEADERSHIP, SERIALIZER::decode, this::getLeadership, SERIALIZER::encode); |
| 118 | executor.register(GET_ALL_LEADERSHIPS, this::allLeaderships, SERIALIZER::encode); |
| 119 | executor.register(GET_ELECTED_TOPICS, SERIALIZER::decode, this::electedTopics, SERIALIZER::encode); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | private void notifyLeadershipChange(Leadership previousLeadership, Leadership newLeadership) { |
Madan Jampani | db2afd3 | 2016-02-23 16:26:45 -0800 | [diff] [blame] | 123 | notifyLeadershipChanges(Lists.newArrayList(new Change<>(previousLeadership, newLeadership))); |
Madan Jampani | 0c0cdc6 | 2016-02-22 16:54:06 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | private void notifyLeadershipChanges(List<Change<Leadership>> changes) { |
| 127 | if (changes.isEmpty()) { |
| 128 | return; |
| 129 | } |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 130 | listeners.values().forEach(session -> session.publish(CHANGE, SERIALIZER::encode, changes)); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Applies listen commits. |
| 135 | * |
| 136 | * @param commit listen commit |
| 137 | */ |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 138 | public void listen(Commit<Void> commit) { |
| 139 | listeners.put(commit.session().sessionId().id(), commit.session()); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Applies unlisten commits. |
| 144 | * |
| 145 | * @param commit unlisten commit |
| 146 | */ |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 147 | public void unlisten(Commit<Void> commit) { |
| 148 | listeners.remove(commit.session().sessionId().id()); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /** |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 152 | * Applies an {@link AtomixLeaderElectorOperations.Run} commit. |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 153 | * @param commit commit entry |
| 154 | * @return topic leader. If no previous leader existed this is the node that just entered the race. |
| 155 | */ |
Madan Jampani | fc98177 | 2016-02-16 09:46:42 -0800 | [diff] [blame] | 156 | public Leadership run(Commit<? extends Run> commit) { |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 157 | try { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 158 | String topic = commit.value().topic(); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 159 | Leadership oldLeadership = leadership(topic); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 160 | Registration registration = new Registration(commit.value().nodeId(), commit.session().sessionId().id()); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 161 | elections.compute(topic, (k, v) -> { |
| 162 | if (v == null) { |
| 163 | return new ElectionState(registration, termCounter(topic)::incrementAndGet); |
| 164 | } else { |
| 165 | if (!v.isDuplicate(registration)) { |
| 166 | return new ElectionState(v).addRegistration(registration, termCounter(topic)::incrementAndGet); |
| 167 | } else { |
| 168 | return v; |
| 169 | } |
| 170 | } |
| 171 | }); |
| 172 | Leadership newLeadership = leadership(topic); |
| 173 | |
| 174 | if (!Objects.equal(oldLeadership, newLeadership)) { |
| 175 | notifyLeadershipChange(oldLeadership, newLeadership); |
| 176 | } |
| 177 | return newLeadership; |
Madan Jampani | ea98f41 | 2016-06-22 09:05:40 -0700 | [diff] [blame] | 178 | } catch (Exception e) { |
Jordan Halterman | 6807c8f | 2017-08-28 20:58:24 -0700 | [diff] [blame^] | 179 | logger().error("State machine operation failed", e); |
Madan Jampani | ea98f41 | 2016-06-22 09:05:40 -0700 | [diff] [blame] | 180 | throw Throwables.propagate(e); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
| 184 | /** |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 185 | * Applies an {@link AtomixLeaderElectorOperations.Withdraw} commit. |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 186 | * @param commit withdraw commit |
| 187 | */ |
Madan Jampani | fc98177 | 2016-02-16 09:46:42 -0800 | [diff] [blame] | 188 | public void withdraw(Commit<? extends Withdraw> commit) { |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 189 | try { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 190 | String topic = commit.value().topic(); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 191 | Leadership oldLeadership = leadership(topic); |
| 192 | elections.computeIfPresent(topic, (k, v) -> v.cleanup(commit.session(), |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 193 | termCounter(topic)::incrementAndGet)); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 194 | Leadership newLeadership = leadership(topic); |
| 195 | if (!Objects.equal(oldLeadership, newLeadership)) { |
| 196 | notifyLeadershipChange(oldLeadership, newLeadership); |
| 197 | } |
Madan Jampani | ea98f41 | 2016-06-22 09:05:40 -0700 | [diff] [blame] | 198 | } catch (Exception e) { |
Jordan Halterman | 6807c8f | 2017-08-28 20:58:24 -0700 | [diff] [blame^] | 199 | logger().error("State machine operation failed", e); |
Madan Jampani | ea98f41 | 2016-06-22 09:05:40 -0700 | [diff] [blame] | 200 | throw Throwables.propagate(e); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
| 204 | /** |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 205 | * Applies an {@link AtomixLeaderElectorOperations.Anoint} commit. |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 206 | * @param commit anoint commit |
| 207 | * @return {@code true} if changes were made and the transfer occurred; {@code false} if it did not. |
| 208 | */ |
Madan Jampani | fc98177 | 2016-02-16 09:46:42 -0800 | [diff] [blame] | 209 | public boolean anoint(Commit<? extends Anoint> commit) { |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 210 | try { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 211 | String topic = commit.value().topic(); |
| 212 | NodeId nodeId = commit.value().nodeId(); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 213 | Leadership oldLeadership = leadership(topic); |
| 214 | ElectionState electionState = elections.computeIfPresent(topic, |
Madan Jampani | c94b485 | 2016-02-23 18:18:37 -0800 | [diff] [blame] | 215 | (k, v) -> v.transferLeadership(nodeId, termCounter(topic))); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 216 | Leadership newLeadership = leadership(topic); |
| 217 | if (!Objects.equal(oldLeadership, newLeadership)) { |
| 218 | notifyLeadershipChange(oldLeadership, newLeadership); |
| 219 | } |
| 220 | return (electionState != null && |
| 221 | electionState.leader() != null && |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 222 | commit.value().nodeId().equals(electionState.leader().nodeId())); |
Madan Jampani | ea98f41 | 2016-06-22 09:05:40 -0700 | [diff] [blame] | 223 | } catch (Exception e) { |
Jordan Halterman | 6807c8f | 2017-08-28 20:58:24 -0700 | [diff] [blame^] | 224 | logger().error("State machine operation failed", e); |
Madan Jampani | ea98f41 | 2016-06-22 09:05:40 -0700 | [diff] [blame] | 225 | throw Throwables.propagate(e); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
| 229 | /** |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 230 | * Applies an {@link AtomixLeaderElectorOperations.Promote} commit. |
Madan Jampani | 0c0cdc6 | 2016-02-22 16:54:06 -0800 | [diff] [blame] | 231 | * @param commit promote commit |
| 232 | * @return {@code true} if changes desired end state is achieved. |
| 233 | */ |
| 234 | public boolean promote(Commit<? extends Promote> commit) { |
| 235 | try { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 236 | String topic = commit.value().topic(); |
| 237 | NodeId nodeId = commit.value().nodeId(); |
Madan Jampani | 0c0cdc6 | 2016-02-22 16:54:06 -0800 | [diff] [blame] | 238 | Leadership oldLeadership = leadership(topic); |
| 239 | if (oldLeadership == null || !oldLeadership.candidates().contains(nodeId)) { |
| 240 | return false; |
| 241 | } |
Madan Jampani | c94b485 | 2016-02-23 18:18:37 -0800 | [diff] [blame] | 242 | elections.computeIfPresent(topic, (k, v) -> v.promote(nodeId)); |
Madan Jampani | 0c0cdc6 | 2016-02-22 16:54:06 -0800 | [diff] [blame] | 243 | Leadership newLeadership = leadership(topic); |
| 244 | if (!Objects.equal(oldLeadership, newLeadership)) { |
| 245 | notifyLeadershipChange(oldLeadership, newLeadership); |
| 246 | } |
| 247 | return true; |
Madan Jampani | ea98f41 | 2016-06-22 09:05:40 -0700 | [diff] [blame] | 248 | } catch (Exception e) { |
Jordan Halterman | 6807c8f | 2017-08-28 20:58:24 -0700 | [diff] [blame^] | 249 | logger().error("State machine operation failed", e); |
Madan Jampani | ea98f41 | 2016-06-22 09:05:40 -0700 | [diff] [blame] | 250 | throw Throwables.propagate(e); |
Madan Jampani | 0c0cdc6 | 2016-02-22 16:54:06 -0800 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
| 254 | /** |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 255 | * Applies an {@link AtomixLeaderElectorOperations.Evict} commit. |
Madan Jampani | 0c0cdc6 | 2016-02-22 16:54:06 -0800 | [diff] [blame] | 256 | * @param commit evict commit |
| 257 | */ |
| 258 | public void evict(Commit<? extends Evict> commit) { |
| 259 | try { |
Madan Jampani | db2afd3 | 2016-02-23 16:26:45 -0800 | [diff] [blame] | 260 | List<Change<Leadership>> changes = Lists.newArrayList(); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 261 | NodeId nodeId = commit.value().nodeId(); |
Madan Jampani | 0c0cdc6 | 2016-02-22 16:54:06 -0800 | [diff] [blame] | 262 | Set<String> topics = Maps.filterValues(elections, e -> e.candidates().contains(nodeId)).keySet(); |
| 263 | topics.forEach(topic -> { |
| 264 | Leadership oldLeadership = leadership(topic); |
| 265 | elections.compute(topic, (k, v) -> v.evict(nodeId, termCounter(topic)::incrementAndGet)); |
| 266 | Leadership newLeadership = leadership(topic); |
| 267 | if (!Objects.equal(oldLeadership, newLeadership)) { |
| 268 | changes.add(new Change<>(oldLeadership, newLeadership)); |
| 269 | } |
| 270 | }); |
| 271 | notifyLeadershipChanges(changes); |
Madan Jampani | ea98f41 | 2016-06-22 09:05:40 -0700 | [diff] [blame] | 272 | } catch (Exception e) { |
Jordan Halterman | 6807c8f | 2017-08-28 20:58:24 -0700 | [diff] [blame^] | 273 | logger().error("State machine operation failed", e); |
Madan Jampani | ea98f41 | 2016-06-22 09:05:40 -0700 | [diff] [blame] | 274 | throw Throwables.propagate(e); |
Madan Jampani | 0c0cdc6 | 2016-02-22 16:54:06 -0800 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | |
| 278 | /** |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 279 | * Applies an {@link AtomixLeaderElectorOperations.GetLeadership} commit. |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 280 | * @param commit GetLeadership commit |
| 281 | * @return leader |
| 282 | */ |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 283 | public Leadership getLeadership(Commit<? extends GetLeadership> commit) { |
| 284 | String topic = commit.value().topic(); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 285 | try { |
| 286 | return leadership(topic); |
Madan Jampani | ea98f41 | 2016-06-22 09:05:40 -0700 | [diff] [blame] | 287 | } catch (Exception e) { |
Jordan Halterman | 6807c8f | 2017-08-28 20:58:24 -0700 | [diff] [blame^] | 288 | logger().error("State machine operation failed", e); |
Madan Jampani | ea98f41 | 2016-06-22 09:05:40 -0700 | [diff] [blame] | 289 | throw Throwables.propagate(e); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
| 293 | /** |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 294 | * Applies an {@link AtomixLeaderElectorOperations.GetElectedTopics} commit. |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 295 | * @param commit commit entry |
| 296 | * @return set of topics for which the node is the leader |
| 297 | */ |
Madan Jampani | fc98177 | 2016-02-16 09:46:42 -0800 | [diff] [blame] | 298 | public Set<String> electedTopics(Commit<? extends GetElectedTopics> commit) { |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 299 | try { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 300 | NodeId nodeId = commit.value().nodeId(); |
Aaron Kruglikov | c0c27c0 | 2016-06-07 16:05:00 -0700 | [diff] [blame] | 301 | return ImmutableSet.copyOf(Maps.filterEntries(elections, e -> { |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 302 | Leader leader = leadership(e.getKey()).leader(); |
| 303 | return leader != null && leader.nodeId().equals(nodeId); |
Aaron Kruglikov | c0c27c0 | 2016-06-07 16:05:00 -0700 | [diff] [blame] | 304 | }).keySet()); |
Madan Jampani | ea98f41 | 2016-06-22 09:05:40 -0700 | [diff] [blame] | 305 | } catch (Exception e) { |
Jordan Halterman | 6807c8f | 2017-08-28 20:58:24 -0700 | [diff] [blame^] | 306 | logger().error("State machine operation failed", e); |
Madan Jampani | ea98f41 | 2016-06-22 09:05:40 -0700 | [diff] [blame] | 307 | throw Throwables.propagate(e); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
| 311 | /** |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 312 | * Applies an {@link AtomixLeaderElectorOperations#GET_ALL_LEADERSHIPS} commit. |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 313 | * @param commit GetAllLeaderships commit |
| 314 | * @return topic to leader mapping |
| 315 | */ |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 316 | public Map<String, Leadership> allLeaderships(Commit<Void> commit) { |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame] | 317 | Map<String, Leadership> result = new HashMap<>(); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 318 | try { |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame] | 319 | result.putAll(Maps.transformEntries(elections, (k, v) -> leadership(k))); |
| 320 | return result; |
Madan Jampani | ea98f41 | 2016-06-22 09:05:40 -0700 | [diff] [blame] | 321 | } catch (Exception e) { |
Jordan Halterman | 6807c8f | 2017-08-28 20:58:24 -0700 | [diff] [blame^] | 322 | logger().error("State machine operation failed", e); |
Madan Jampani | ea98f41 | 2016-06-22 09:05:40 -0700 | [diff] [blame] | 323 | throw Throwables.propagate(e); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
| 327 | private Leadership leadership(String topic) { |
| 328 | return new Leadership(topic, |
| 329 | leader(topic), |
| 330 | candidates(topic)); |
| 331 | } |
| 332 | |
| 333 | private Leader leader(String topic) { |
| 334 | ElectionState electionState = elections.get(topic); |
| 335 | return electionState == null ? null : electionState.leader(); |
| 336 | } |
| 337 | |
| 338 | private List<NodeId> candidates(String topic) { |
| 339 | ElectionState electionState = elections.get(topic); |
| 340 | return electionState == null ? new LinkedList<>() : electionState.candidates(); |
| 341 | } |
| 342 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 343 | private void onSessionEnd(RaftSession session) { |
| 344 | listeners.remove(session.sessionId().id()); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 345 | Set<String> topics = elections.keySet(); |
Madan Jampani | db2afd3 | 2016-02-23 16:26:45 -0800 | [diff] [blame] | 346 | List<Change<Leadership>> changes = Lists.newArrayList(); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 347 | topics.forEach(topic -> { |
| 348 | Leadership oldLeadership = leadership(topic); |
| 349 | elections.compute(topic, (k, v) -> v.cleanup(session, termCounter(topic)::incrementAndGet)); |
| 350 | Leadership newLeadership = leadership(topic); |
| 351 | if (!Objects.equal(oldLeadership, newLeadership)) { |
Madan Jampani | db2afd3 | 2016-02-23 16:26:45 -0800 | [diff] [blame] | 352 | changes.add(new Change<>(oldLeadership, newLeadership)); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 353 | } |
| 354 | }); |
Madan Jampani | db2afd3 | 2016-02-23 16:26:45 -0800 | [diff] [blame] | 355 | notifyLeadershipChanges(changes); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | private static class Registration { |
| 359 | private final NodeId nodeId; |
| 360 | private final long sessionId; |
| 361 | |
| 362 | public Registration(NodeId nodeId, long sessionId) { |
| 363 | this.nodeId = nodeId; |
| 364 | this.sessionId = sessionId; |
| 365 | } |
| 366 | |
| 367 | public NodeId nodeId() { |
| 368 | return nodeId; |
| 369 | } |
| 370 | |
| 371 | public long sessionId() { |
| 372 | return sessionId; |
| 373 | } |
| 374 | |
| 375 | @Override |
| 376 | public String toString() { |
| 377 | return MoreObjects.toStringHelper(getClass()) |
| 378 | .add("nodeId", nodeId) |
| 379 | .add("sessionId", sessionId) |
| 380 | .toString(); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | private static class ElectionState { |
| 385 | final Registration leader; |
| 386 | final long term; |
| 387 | final long termStartTime; |
| 388 | final List<Registration> registrations; |
| 389 | |
| 390 | public ElectionState(Registration registration, Supplier<Long> termCounter) { |
| 391 | registrations = Arrays.asList(registration); |
| 392 | term = termCounter.get(); |
| 393 | termStartTime = System.currentTimeMillis(); |
| 394 | leader = registration; |
| 395 | } |
| 396 | |
| 397 | public ElectionState(ElectionState other) { |
| 398 | registrations = Lists.newArrayList(other.registrations); |
| 399 | leader = other.leader; |
| 400 | term = other.term; |
| 401 | termStartTime = other.termStartTime; |
| 402 | } |
| 403 | |
| 404 | public ElectionState(List<Registration> registrations, |
| 405 | Registration leader, |
| 406 | long term, |
| 407 | long termStartTime) { |
| 408 | this.registrations = Lists.newArrayList(registrations); |
| 409 | this.leader = leader; |
| 410 | this.term = term; |
| 411 | this.termStartTime = termStartTime; |
| 412 | } |
| 413 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 414 | public ElectionState cleanup(RaftSession session, Supplier<Long> termCounter) { |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 415 | Optional<Registration> registration = |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 416 | registrations.stream().filter(r -> r.sessionId() == session.sessionId().id()).findFirst(); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 417 | if (registration.isPresent()) { |
| 418 | List<Registration> updatedRegistrations = |
| 419 | registrations.stream() |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 420 | .filter(r -> r.sessionId() != session.sessionId().id()) |
| 421 | .collect(Collectors.toList()); |
| 422 | if (leader.sessionId() == session.sessionId().id()) { |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame] | 423 | if (!updatedRegistrations.isEmpty()) { |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 424 | return new ElectionState(updatedRegistrations, |
| 425 | updatedRegistrations.get(0), |
| 426 | termCounter.get(), |
| 427 | System.currentTimeMillis()); |
| 428 | } else { |
| 429 | return new ElectionState(updatedRegistrations, null, term, termStartTime); |
| 430 | } |
| 431 | } else { |
| 432 | return new ElectionState(updatedRegistrations, leader, term, termStartTime); |
| 433 | } |
| 434 | } else { |
| 435 | return this; |
| 436 | } |
| 437 | } |
| 438 | |
Madan Jampani | 0c0cdc6 | 2016-02-22 16:54:06 -0800 | [diff] [blame] | 439 | public ElectionState evict(NodeId nodeId, Supplier<Long> termCounter) { |
| 440 | Optional<Registration> registration = |
| 441 | registrations.stream().filter(r -> r.nodeId.equals(nodeId)).findFirst(); |
| 442 | if (registration.isPresent()) { |
| 443 | List<Registration> updatedRegistrations = |
| 444 | registrations.stream() |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 445 | .filter(r -> !r.nodeId().equals(nodeId)) |
| 446 | .collect(Collectors.toList()); |
Madan Jampani | 0c0cdc6 | 2016-02-22 16:54:06 -0800 | [diff] [blame] | 447 | if (leader.nodeId().equals(nodeId)) { |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame] | 448 | if (!updatedRegistrations.isEmpty()) { |
Madan Jampani | 0c0cdc6 | 2016-02-22 16:54:06 -0800 | [diff] [blame] | 449 | return new ElectionState(updatedRegistrations, |
| 450 | updatedRegistrations.get(0), |
| 451 | termCounter.get(), |
| 452 | System.currentTimeMillis()); |
| 453 | } else { |
| 454 | return new ElectionState(updatedRegistrations, null, term, termStartTime); |
| 455 | } |
| 456 | } else { |
| 457 | return new ElectionState(updatedRegistrations, leader, term, termStartTime); |
| 458 | } |
| 459 | } else { |
| 460 | return this; |
| 461 | } |
| 462 | } |
| 463 | |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 464 | public boolean isDuplicate(Registration registration) { |
| 465 | return registrations.stream().anyMatch(r -> r.sessionId() == registration.sessionId()); |
| 466 | } |
| 467 | |
| 468 | public Leader leader() { |
| 469 | if (leader == null) { |
| 470 | return null; |
| 471 | } else { |
| 472 | NodeId leaderNodeId = leader.nodeId(); |
| 473 | return new Leader(leaderNodeId, term, termStartTime); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | public List<NodeId> candidates() { |
| 478 | return registrations.stream().map(registration -> registration.nodeId()).collect(Collectors.toList()); |
| 479 | } |
| 480 | |
| 481 | public ElectionState addRegistration(Registration registration, Supplier<Long> termCounter) { |
| 482 | if (!registrations.stream().anyMatch(r -> r.sessionId() == registration.sessionId())) { |
| 483 | List<Registration> updatedRegistrations = new LinkedList<>(registrations); |
| 484 | updatedRegistrations.add(registration); |
| 485 | boolean newLeader = leader == null; |
| 486 | return new ElectionState(updatedRegistrations, |
| 487 | newLeader ? registration : leader, |
| 488 | newLeader ? termCounter.get() : term, |
| 489 | newLeader ? System.currentTimeMillis() : termStartTime); |
| 490 | } |
| 491 | return this; |
| 492 | } |
| 493 | |
| 494 | public ElectionState transferLeadership(NodeId nodeId, AtomicLong termCounter) { |
| 495 | Registration newLeader = registrations.stream() |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 496 | .filter(r -> r.nodeId().equals(nodeId)) |
| 497 | .findFirst() |
| 498 | .orElse(null); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 499 | if (newLeader != null) { |
| 500 | return new ElectionState(registrations, |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 501 | newLeader, |
| 502 | termCounter.incrementAndGet(), |
| 503 | System.currentTimeMillis()); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 504 | } else { |
| 505 | return this; |
| 506 | } |
| 507 | } |
Madan Jampani | 0c0cdc6 | 2016-02-22 16:54:06 -0800 | [diff] [blame] | 508 | |
| 509 | public ElectionState promote(NodeId nodeId) { |
| 510 | Registration registration = registrations.stream() |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 511 | .filter(r -> r.nodeId().equals(nodeId)) |
| 512 | .findFirst() |
| 513 | .orElse(null); |
Madan Jampani | c94b485 | 2016-02-23 18:18:37 -0800 | [diff] [blame] | 514 | List<Registration> updatedRegistrations = Lists.newArrayList(); |
Madan Jampani | 0c0cdc6 | 2016-02-22 16:54:06 -0800 | [diff] [blame] | 515 | updatedRegistrations.add(registration); |
| 516 | registrations.stream() |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 517 | .filter(r -> !r.nodeId().equals(nodeId)) |
| 518 | .forEach(updatedRegistrations::add); |
Madan Jampani | 0c0cdc6 | 2016-02-22 16:54:06 -0800 | [diff] [blame] | 519 | return new ElectionState(updatedRegistrations, |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 520 | leader, |
| 521 | term, |
| 522 | termStartTime); |
Madan Jampani | 0c0cdc6 | 2016-02-22 16:54:06 -0800 | [diff] [blame] | 523 | |
| 524 | } |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | @Override |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 528 | public void onExpire(RaftSession session) { |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 529 | onSessionEnd(session); |
| 530 | } |
| 531 | |
| 532 | @Override |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 533 | public void onClose(RaftSession session) { |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 534 | onSessionEnd(session); |
| 535 | } |
| 536 | |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 537 | private AtomicLong termCounter(String topic) { |
| 538 | return termCounters.computeIfAbsent(topic, k -> new AtomicLong(0)); |
| 539 | } |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 540 | } |