Yuta HIGUCHI | 80912e6 | 2014-10-12 00:15:47 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.mastership.impl; |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 2 | |
Yuta HIGUCHI | 80912e6 | 2014-10-12 00:15:47 -0700 | [diff] [blame] | 3 | import static org.onlab.onos.mastership.MastershipEvent.Type.MASTER_CHANGED; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 4 | |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 5 | import java.util.Collections; |
Ayaka Koshibe | 45503ce | 2014-10-14 11:26:45 -0700 | [diff] [blame] | 6 | import java.util.LinkedList; |
| 7 | import java.util.List; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 8 | import java.util.Map; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 9 | import java.util.Set; |
Yuta HIGUCHI | c8e19d4 | 2014-09-24 17:20:52 -0700 | [diff] [blame] | 10 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 11 | import org.apache.felix.scr.annotations.Activate; |
| 12 | import org.apache.felix.scr.annotations.Component; |
| 13 | import org.apache.felix.scr.annotations.Deactivate; |
| 14 | import org.apache.felix.scr.annotations.Reference; |
| 15 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 16 | import org.apache.felix.scr.annotations.Service; |
| 17 | import org.onlab.onos.cluster.ClusterService; |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 18 | import org.onlab.onos.cluster.NodeId; |
Yuta HIGUCHI | 80912e6 | 2014-10-12 00:15:47 -0700 | [diff] [blame] | 19 | import org.onlab.onos.mastership.MastershipEvent; |
| 20 | import org.onlab.onos.mastership.MastershipStore; |
| 21 | import org.onlab.onos.mastership.MastershipStoreDelegate; |
| 22 | import org.onlab.onos.mastership.MastershipTerm; |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 23 | import org.onlab.onos.net.DeviceId; |
| 24 | import org.onlab.onos.net.MastershipRole; |
Yuta HIGUCHI | b5df76d | 2014-09-27 20:54:00 -0700 | [diff] [blame] | 25 | import org.onlab.onos.store.common.AbstractHazelcastStore; |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 26 | import org.onlab.onos.store.common.SMap; |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 27 | import org.onlab.onos.store.serializers.KryoNamespaces; |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 28 | import org.onlab.onos.store.serializers.KryoSerializer; |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 29 | import org.onlab.util.KryoNamespace; |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 30 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 31 | import com.google.common.collect.ImmutableSet; |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 32 | import com.hazelcast.core.EntryEvent; |
| 33 | import com.hazelcast.core.EntryListener; |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 34 | import com.hazelcast.core.IAtomicLong; |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 35 | import com.hazelcast.core.MapEvent; |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 36 | |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 37 | import static org.onlab.onos.net.MastershipRole.*; |
| 38 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 39 | /** |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 40 | * Distributed implementation of the mastership store. The store is |
| 41 | * responsible for the master selection process. |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 42 | */ |
| 43 | @Component(immediate = true) |
| 44 | @Service |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 45 | public class DistributedMastershipStore |
Yuta HIGUCHI | 2e96389 | 2014-09-27 13:00:39 -0700 | [diff] [blame] | 46 | extends AbstractHazelcastStore<MastershipEvent, MastershipStoreDelegate> |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 47 | implements MastershipStore { |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 48 | |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 49 | //initial term/TTL value |
Ayaka Koshibe | 8583ff3 | 2014-10-02 16:25:30 -0700 | [diff] [blame] | 50 | private static final Integer INIT = 0; |
Ayaka Koshibe | 8583ff3 | 2014-10-02 16:25:30 -0700 | [diff] [blame] | 51 | |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 52 | //device to node roles |
| 53 | protected SMap<DeviceId, RoleValue> roleMap; |
Ayaka Koshibe | 8583ff3 | 2014-10-02 16:25:30 -0700 | [diff] [blame] | 54 | //devices to terms |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 55 | protected SMap<DeviceId, Integer> terms; |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 56 | //last-known cluster size, used for tie-breaking when partitioning occurs |
| 57 | protected IAtomicLong clusterSize; |
| 58 | |
Ayaka Koshibe | 8583ff3 | 2014-10-02 16:25:30 -0700 | [diff] [blame] | 59 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 60 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 61 | protected ClusterService clusterService; |
| 62 | |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 63 | @SuppressWarnings({ "unchecked", "rawtypes" }) |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 64 | @Override |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 65 | @Activate |
| 66 | public void activate() { |
| 67 | super.activate(); |
| 68 | |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 69 | this.serializer = new KryoSerializer() { |
| 70 | @Override |
| 71 | protected void setupKryoPool() { |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 72 | serializerPool = KryoNamespace.newBuilder() |
| 73 | .register(KryoNamespaces.API) |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 74 | |
| 75 | .register(RoleValue.class, new RoleValueSerializer()) |
| 76 | .build() |
| 77 | .populate(1); |
| 78 | } |
| 79 | }; |
| 80 | |
| 81 | roleMap = new SMap(theInstance.getMap("nodeRoles"), this.serializer); |
| 82 | terms = new SMap(theInstance.getMap("terms"), this.serializer); |
| 83 | clusterSize = theInstance.getAtomicLong("clustersize"); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 84 | roleMap.addEntryListener((new RemoteMasterShipEventHandler()), true); |
Yuta HIGUCHI | c8e19d4 | 2014-09-24 17:20:52 -0700 | [diff] [blame] | 85 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 86 | log.info("Started"); |
| 87 | } |
| 88 | |
| 89 | @Deactivate |
| 90 | public void deactivate() { |
| 91 | log.info("Stopped"); |
| 92 | } |
| 93 | |
| 94 | @Override |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 95 | public MastershipRole getRole(NodeId nodeId, DeviceId deviceId) { |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 96 | NodeId current = getNode(MASTER, deviceId); |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 97 | if (current == null) { |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 98 | if (isRole(STANDBY, nodeId, deviceId)) { |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 99 | //was previously standby, or set to standby from master |
| 100 | return MastershipRole.STANDBY; |
| 101 | } else { |
| 102 | return MastershipRole.NONE; |
| 103 | } |
| 104 | } else { |
| 105 | if (current.equals(nodeId)) { |
| 106 | //*should* be in unusable, not always |
| 107 | return MastershipRole.MASTER; |
| 108 | } else { |
| 109 | //may be in backups or unusable from earlier retirement |
| 110 | return MastershipRole.STANDBY; |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | @Override |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 116 | public MastershipEvent setMaster(NodeId nodeId, DeviceId deviceId) { |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 117 | |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 118 | MastershipRole role = getRole(nodeId, deviceId); |
| 119 | roleMap.lock(deviceId); |
Ayaka Koshibe | 8583ff3 | 2014-10-02 16:25:30 -0700 | [diff] [blame] | 120 | try { |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 121 | RoleValue rv = getRoleValue(deviceId); |
Ayaka Koshibe | 8583ff3 | 2014-10-02 16:25:30 -0700 | [diff] [blame] | 122 | switch (role) { |
| 123 | case MASTER: |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 124 | //reinforce mastership |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 125 | rv.reassign(nodeId, STANDBY, NONE); |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 126 | roleMap.put(deviceId, rv); |
Ayaka Koshibe | 8583ff3 | 2014-10-02 16:25:30 -0700 | [diff] [blame] | 127 | return null; |
| 128 | case STANDBY: |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 129 | NodeId current = rv.get(MASTER); |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 130 | if (current != null) { |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 131 | //backup and replace current master |
| 132 | rv.reassign(nodeId, NONE, STANDBY); |
| 133 | rv.replace(current, nodeId, MASTER); |
| 134 | } else { |
| 135 | //no master before so just add. |
| 136 | rv.add(MASTER, nodeId); |
Ayaka Koshibe | 8583ff3 | 2014-10-02 16:25:30 -0700 | [diff] [blame] | 137 | } |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 138 | rv.reassign(nodeId, STANDBY, NONE); |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 139 | roleMap.put(deviceId, rv); |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 140 | updateTerm(deviceId); |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 141 | return new MastershipEvent(MASTER_CHANGED, deviceId, nodeId); |
| 142 | case NONE: |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 143 | rv.add(MASTER, nodeId); |
| 144 | rv.reassign(nodeId, STANDBY, NONE); |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 145 | roleMap.put(deviceId, rv); |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 146 | updateTerm(deviceId); |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 147 | return new MastershipEvent(MASTER_CHANGED, deviceId, nodeId); |
Ayaka Koshibe | 8583ff3 | 2014-10-02 16:25:30 -0700 | [diff] [blame] | 148 | default: |
| 149 | log.warn("unknown Mastership Role {}", role); |
| 150 | return null; |
| 151 | } |
Ayaka Koshibe | 8583ff3 | 2014-10-02 16:25:30 -0700 | [diff] [blame] | 152 | } finally { |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 153 | roleMap.unlock(deviceId); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public NodeId getMaster(DeviceId deviceId) { |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 159 | return getNode(MASTER, deviceId); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Ayaka Koshibe | 45503ce | 2014-10-14 11:26:45 -0700 | [diff] [blame] | 162 | |
| 163 | @Override |
| 164 | public List<NodeId> getNodes(DeviceId deviceId) { |
Ayaka Koshibe | 45503ce | 2014-10-14 11:26:45 -0700 | [diff] [blame] | 165 | List<NodeId> nodes = new LinkedList<>(); |
| 166 | |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 167 | //add current master to head - if there is one. |
| 168 | roleMap.lock(deviceId); |
Ayaka Koshibe | 45503ce | 2014-10-14 11:26:45 -0700 | [diff] [blame] | 169 | try { |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 170 | RoleValue rv = getRoleValue(deviceId); |
| 171 | NodeId master = rv.get(MASTER); |
Ayaka Koshibe | 45503ce | 2014-10-14 11:26:45 -0700 | [diff] [blame] | 172 | if (master != null) { |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 173 | nodes.add(master); |
Ayaka Koshibe | 45503ce | 2014-10-14 11:26:45 -0700 | [diff] [blame] | 174 | } |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 175 | //We ignore NONE nodes. |
| 176 | nodes.addAll(rv.nodesOfRole(STANDBY)); |
| 177 | return Collections.unmodifiableList(nodes); |
Ayaka Koshibe | 45503ce | 2014-10-14 11:26:45 -0700 | [diff] [blame] | 178 | } finally { |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 179 | roleMap.unlock(deviceId); |
Ayaka Koshibe | 45503ce | 2014-10-14 11:26:45 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 183 | @Override |
| 184 | public Set<DeviceId> getDevices(NodeId nodeId) { |
| 185 | ImmutableSet.Builder<DeviceId> builder = ImmutableSet.builder(); |
Ayaka Koshibe | 8583ff3 | 2014-10-02 16:25:30 -0700 | [diff] [blame] | 186 | |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 187 | for (Map.Entry<DeviceId, RoleValue> el : roleMap.entrySet()) { |
| 188 | if (nodeId.equals(el.getValue().get(MASTER))) { |
| 189 | builder.add(el.getKey()); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 190 | } |
| 191 | } |
Ayaka Koshibe | 8583ff3 | 2014-10-02 16:25:30 -0700 | [diff] [blame] | 192 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 193 | return builder.build(); |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public MastershipRole requestRole(DeviceId deviceId) { |
Ayaka Koshibe | 8583ff3 | 2014-10-02 16:25:30 -0700 | [diff] [blame] | 198 | NodeId local = clusterService.getLocalNode().id(); |
Ayaka Koshibe | 8583ff3 | 2014-10-02 16:25:30 -0700 | [diff] [blame] | 199 | |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 200 | roleMap.lock(deviceId); |
Ayaka Koshibe | 8583ff3 | 2014-10-02 16:25:30 -0700 | [diff] [blame] | 201 | try { |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 202 | RoleValue rv = getRoleValue(deviceId); |
Ayaka Koshibe | 8583ff3 | 2014-10-02 16:25:30 -0700 | [diff] [blame] | 203 | MastershipRole role = getRole(local, deviceId); |
| 204 | switch (role) { |
| 205 | case MASTER: |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 206 | rv.reassign(local, STANDBY, NONE); |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 207 | roleMap.put(deviceId, rv); |
Ayaka Koshibe | 8583ff3 | 2014-10-02 16:25:30 -0700 | [diff] [blame] | 208 | break; |
| 209 | case STANDBY: |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 210 | rv.reassign(local, NONE, STANDBY); |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 211 | roleMap.put(deviceId, rv); |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 212 | terms.putIfAbsent(deviceId, INIT); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 213 | |
Ayaka Koshibe | 8583ff3 | 2014-10-02 16:25:30 -0700 | [diff] [blame] | 214 | break; |
| 215 | case NONE: |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 216 | //claim mastership |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 217 | rv.add(MASTER, local); |
| 218 | rv.reassign(local, STANDBY, NONE); |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 219 | roleMap.put(deviceId, rv); |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 220 | updateTerm(deviceId); |
Ayaka Koshibe | 8583ff3 | 2014-10-02 16:25:30 -0700 | [diff] [blame] | 221 | role = MastershipRole.MASTER; |
| 222 | break; |
| 223 | default: |
| 224 | log.warn("unknown Mastership Role {}", role); |
| 225 | } |
| 226 | return role; |
| 227 | } finally { |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 228 | roleMap.unlock(deviceId); |
Ayaka Koshibe | 8583ff3 | 2014-10-02 16:25:30 -0700 | [diff] [blame] | 229 | } |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | @Override |
Ayaka Koshibe | b70d34b | 2014-09-25 15:43:01 -0700 | [diff] [blame] | 233 | public MastershipTerm getTermFor(DeviceId deviceId) { |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 234 | RoleValue rv = getRoleValue(deviceId); |
| 235 | if ((rv.get(MASTER) == null) || (terms.get(deviceId) == null)) { |
Ayaka Koshibe | 8583ff3 | 2014-10-02 16:25:30 -0700 | [diff] [blame] | 236 | return null; |
| 237 | } |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 238 | return MastershipTerm.of(rv.get(MASTER), terms.get(deviceId)); |
Ayaka Koshibe | b70d34b | 2014-09-25 15:43:01 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Ayaka Koshibe | d9f693e | 2014-09-29 18:04:54 -0700 | [diff] [blame] | 241 | @Override |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 242 | public MastershipEvent setStandby(NodeId nodeId, DeviceId deviceId) { |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 243 | MastershipEvent event = null; |
Ayaka Koshibe | 8583ff3 | 2014-10-02 16:25:30 -0700 | [diff] [blame] | 244 | |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 245 | roleMap.lock(deviceId); |
Ayaka Koshibe | 8583ff3 | 2014-10-02 16:25:30 -0700 | [diff] [blame] | 246 | try { |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 247 | RoleValue rv = getRoleValue(deviceId); |
Ayaka Koshibe | 8583ff3 | 2014-10-02 16:25:30 -0700 | [diff] [blame] | 248 | MastershipRole role = getRole(nodeId, deviceId); |
| 249 | switch (role) { |
| 250 | case MASTER: |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 251 | event = reelect(nodeId, deviceId, rv); |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 252 | //fall through to reinforce role |
Ayaka Koshibe | 8583ff3 | 2014-10-02 16:25:30 -0700 | [diff] [blame] | 253 | case STANDBY: |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 254 | //fall through to reinforce role |
Ayaka Koshibe | 8583ff3 | 2014-10-02 16:25:30 -0700 | [diff] [blame] | 255 | case NONE: |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 256 | rv.reassign(nodeId, NONE, STANDBY); |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 257 | roleMap.put(deviceId, rv); |
Ayaka Koshibe | 8583ff3 | 2014-10-02 16:25:30 -0700 | [diff] [blame] | 258 | break; |
| 259 | default: |
| 260 | log.warn("unknown Mastership Role {}", role); |
| 261 | } |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 262 | return event; |
Ayaka Koshibe | 8583ff3 | 2014-10-02 16:25:30 -0700 | [diff] [blame] | 263 | } finally { |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 264 | roleMap.unlock(deviceId); |
Ayaka Koshibe | 8583ff3 | 2014-10-02 16:25:30 -0700 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 268 | @Override |
| 269 | public MastershipEvent relinquishRole(NodeId nodeId, DeviceId deviceId) { |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 270 | MastershipEvent event = null; |
Ayaka Koshibe | 25fd23a | 2014-10-03 15:50:43 -0700 | [diff] [blame] | 271 | |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 272 | roleMap.lock(deviceId); |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 273 | try { |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 274 | RoleValue rv = getRoleValue(deviceId); |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 275 | MastershipRole role = getRole(nodeId, deviceId); |
| 276 | switch (role) { |
| 277 | case MASTER: |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 278 | event = reelect(nodeId, deviceId, rv); |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 279 | //fall through to reinforce relinquishment |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 280 | case STANDBY: |
| 281 | //fall through to reinforce relinquishment |
| 282 | case NONE: |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 283 | rv.reassign(nodeId, STANDBY, NONE); |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 284 | roleMap.put(deviceId, rv); |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 285 | break; |
| 286 | default: |
| 287 | log.warn("unknown Mastership Role {}", role); |
Ayaka Koshibe | 8583ff3 | 2014-10-02 16:25:30 -0700 | [diff] [blame] | 288 | } |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 289 | return event; |
| 290 | } finally { |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 291 | roleMap.unlock(deviceId); |
Ayaka Koshibe | 8583ff3 | 2014-10-02 16:25:30 -0700 | [diff] [blame] | 292 | } |
Ayaka Koshibe | d9f693e | 2014-09-29 18:04:54 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 295 | //helper to fetch a new master candidate for a given device. |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 296 | private MastershipEvent reelect( |
| 297 | NodeId current, DeviceId deviceId, RoleValue rv) { |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 298 | |
| 299 | //if this is an queue it'd be neater. |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 300 | NodeId backup = null; |
| 301 | for (NodeId n : rv.nodesOfRole(STANDBY)) { |
| 302 | if (!current.equals(n)) { |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 303 | backup = n; |
| 304 | break; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | if (backup == null) { |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 309 | log.info("{} giving up and going to NONE for {}", current, deviceId); |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 310 | rv.remove(MASTER, current); |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 311 | roleMap.put(deviceId, rv); |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 312 | return null; |
| 313 | } else { |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 314 | log.info("{} trying to pass mastership for {} to {}", current, deviceId, backup); |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 315 | rv.replace(current, backup, MASTER); |
| 316 | rv.reassign(backup, STANDBY, NONE); |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 317 | roleMap.put(deviceId, rv); |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 318 | Integer term = terms.get(deviceId); |
| 319 | terms.put(deviceId, ++term); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 320 | return new MastershipEvent(MASTER_CHANGED, deviceId, backup); |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 324 | //return the RoleValue structure for a device, or create one |
| 325 | private RoleValue getRoleValue(DeviceId deviceId) { |
| 326 | RoleValue value = roleMap.get(deviceId); |
| 327 | if (value == null) { |
| 328 | value = new RoleValue(); |
| 329 | roleMap.put(deviceId, value); |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 330 | } |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 331 | return value; |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 334 | //get first applicable node out of store-unique structure. |
| 335 | private NodeId getNode(MastershipRole role, DeviceId deviceId) { |
| 336 | RoleValue value = roleMap.get(deviceId); |
| 337 | if (value != null) { |
| 338 | return value.get(role); |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 339 | } |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 340 | return null; |
| 341 | } |
| 342 | |
| 343 | //check if node is a certain role given a device |
| 344 | private boolean isRole( |
| 345 | MastershipRole role, NodeId nodeId, DeviceId deviceId) { |
| 346 | RoleValue value = roleMap.get(deviceId); |
| 347 | if (value != null) { |
| 348 | return value.contains(role, nodeId); |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 349 | } |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 350 | return false; |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | //adds or updates term information. |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 354 | private void updateTerm(DeviceId deviceId) { |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 355 | terms.lock(deviceId); |
| 356 | try { |
| 357 | Integer term = terms.get(deviceId); |
| 358 | if (term == null) { |
| 359 | terms.put(deviceId, INIT); |
| 360 | } else { |
| 361 | terms.put(deviceId, ++term); |
| 362 | } |
| 363 | } finally { |
| 364 | terms.unlock(deviceId); |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 365 | } |
Ayaka Koshibe | 8583ff3 | 2014-10-02 16:25:30 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 368 | private class RemoteMasterShipEventHandler implements EntryListener<DeviceId, RoleValue> { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 369 | |
| 370 | @Override |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 371 | public void entryAdded(EntryEvent<DeviceId, RoleValue> event) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | @Override |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 375 | public void entryRemoved(EntryEvent<DeviceId, RoleValue> event) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | @Override |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 379 | public void entryUpdated(EntryEvent<DeviceId, RoleValue> event) { |
| 380 | NodeId myId = clusterService.getLocalNode().id(); |
| 381 | NodeId node = event.getValue().get(MASTER); |
| 382 | if (myId.equals(node)) { |
| 383 | // XXX or do we just let it get sent and caught by ourself? |
| 384 | return; |
| 385 | } |
| 386 | notifyDelegate(new MastershipEvent( |
| 387 | MASTER_CHANGED, event.getKey(), event.getValue().get(MASTER))); |
| 388 | } |
| 389 | |
| 390 | @Override |
| 391 | public void entryEvicted(EntryEvent<DeviceId, RoleValue> event) { |
| 392 | } |
| 393 | |
| 394 | @Override |
| 395 | public void mapEvicted(MapEvent event) { |
| 396 | } |
| 397 | |
| 398 | @Override |
| 399 | public void mapCleared(MapEvent event) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 403 | } |