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