blob: dd5041a1f1d69e15d5d7575910b37fba2e6d92e4 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
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 */
Yuta HIGUCHI80912e62014-10-12 00:15:47 -070016package org.onlab.onos.store.mastership.impl;
tomb41d1ac2014-09-24 01:51:24 -070017
Yuta HIGUCHI80912e62014-10-12 00:15:47 -070018import static org.onlab.onos.mastership.MastershipEvent.Type.MASTER_CHANGED;
Ayaka Koshibe98bd12f2014-11-01 20:13:37 -070019import static org.onlab.onos.mastership.MastershipEvent.Type.BACKUPS_CHANGED;
Yuta HIGUCHIf1d159a2014-10-29 23:31:40 -070020import static org.apache.commons.lang3.concurrent.ConcurrentUtils.putIfAbsent;
alshabib339a3d92014-09-26 17:54:32 -070021
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -070022import java.util.HashSet;
alshabib339a3d92014-09-26 17:54:32 -070023import java.util.Map;
alshabib339a3d92014-09-26 17:54:32 -070024import java.util.Set;
Yuta HIGUCHIc8e19d42014-09-24 17:20:52 -070025
tomb41d1ac2014-09-24 01:51:24 -070026import org.apache.felix.scr.annotations.Activate;
27import org.apache.felix.scr.annotations.Component;
28import org.apache.felix.scr.annotations.Deactivate;
29import org.apache.felix.scr.annotations.Reference;
30import org.apache.felix.scr.annotations.ReferenceCardinality;
31import org.apache.felix.scr.annotations.Service;
32import org.onlab.onos.cluster.ClusterService;
tomb41d1ac2014-09-24 01:51:24 -070033import org.onlab.onos.cluster.NodeId;
Ayaka Koshibeabedb092014-10-20 17:01:31 -070034import org.onlab.onos.cluster.RoleInfo;
Yuta HIGUCHI80912e62014-10-12 00:15:47 -070035import org.onlab.onos.mastership.MastershipEvent;
36import org.onlab.onos.mastership.MastershipStore;
37import org.onlab.onos.mastership.MastershipStoreDelegate;
38import org.onlab.onos.mastership.MastershipTerm;
tomb41d1ac2014-09-24 01:51:24 -070039import org.onlab.onos.net.DeviceId;
40import org.onlab.onos.net.MastershipRole;
Yuta HIGUCHI41f2ec02014-10-27 09:54:43 -070041import org.onlab.onos.store.hz.AbstractHazelcastStore;
42import org.onlab.onos.store.hz.SMap;
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070043import org.onlab.onos.store.serializers.KryoNamespaces;
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070044import org.onlab.onos.store.serializers.KryoSerializer;
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070045import org.onlab.util.KryoNamespace;
tomb41d1ac2014-09-24 01:51:24 -070046
Ayaka Koshibe98bd12f2014-11-01 20:13:37 -070047import com.google.common.base.Objects;
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -070048import com.hazelcast.core.EntryEvent;
49import com.hazelcast.core.EntryListener;
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -070050import com.hazelcast.core.MapEvent;
Yuta HIGUCHI41f2ec02014-10-27 09:54:43 -070051
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070052import static org.onlab.onos.net.MastershipRole.*;
53
tomb41d1ac2014-09-24 01:51:24 -070054/**
Ayaka Koshibec4047702014-10-07 14:43:52 -070055 * Distributed implementation of the mastership store. The store is
56 * responsible for the master selection process.
tomb41d1ac2014-09-24 01:51:24 -070057 */
58@Component(immediate = true)
59@Service
tom0755a362014-09-24 11:54:43 -070060public class DistributedMastershipStore
Yuta HIGUCHIb0daa152014-11-10 16:58:57 -080061 extends AbstractHazelcastStore<MastershipEvent, MastershipStoreDelegate>
62 implements MastershipStore {
tomb41d1ac2014-09-24 01:51:24 -070063
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -070064 //term number representing that master has never been chosen yet
65 private static final Integer NOTHING = 0;
Ayaka Koshibec4047702014-10-07 14:43:52 -070066 //initial term/TTL value
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -070067 private static final Integer INIT = 1;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070068
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070069 //device to node roles
70 protected SMap<DeviceId, RoleValue> roleMap;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070071 //devices to terms
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070072 protected SMap<DeviceId, Integer> terms;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070073
tomb41d1ac2014-09-24 01:51:24 -070074 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
75 protected ClusterService clusterService;
76
Ayaka Koshibe406d0102014-09-24 16:08:12 -070077 @Override
tomb41d1ac2014-09-24 01:51:24 -070078 @Activate
79 public void activate() {
80 super.activate();
81
Ayaka Koshibee8e45352014-10-16 00:37:19 -070082 this.serializer = new KryoSerializer() {
83 @Override
84 protected void setupKryoPool() {
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070085 serializerPool = KryoNamespace.newBuilder()
86 .register(KryoNamespaces.API)
Ayaka Koshibee8e45352014-10-16 00:37:19 -070087
88 .register(RoleValue.class, new RoleValueSerializer())
89 .build()
90 .populate(1);
91 }
92 };
93
Yuta HIGUCHI9def0472014-10-23 15:51:10 -070094 roleMap = new SMap<>(theInstance.<byte[], byte[]>getMap("nodeRoles"), this.serializer);
Ayaka Koshibe67af1f42014-10-20 15:26:37 -070095 roleMap.addEntryListener((new RemoteMasterShipEventHandler()), true);
Yuta HIGUCHI9def0472014-10-23 15:51:10 -070096 terms = new SMap<>(theInstance.<byte[], byte[]>getMap("terms"), this.serializer);
Yuta HIGUCHIc8e19d42014-09-24 17:20:52 -070097
tomb41d1ac2014-09-24 01:51:24 -070098 log.info("Started");
99 }
100
101 @Deactivate
102 public void deactivate() {
103 log.info("Stopped");
104 }
105
106 @Override
Ayaka Koshibec4047702014-10-07 14:43:52 -0700107 public MastershipRole getRole(NodeId nodeId, DeviceId deviceId) {
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700108 final RoleValue roleInfo = roleMap.get(deviceId);
109 if (roleInfo != null) {
110 return roleInfo.getRole(nodeId);
Ayaka Koshibea7384a82014-10-22 18:59:06 -0700111 }
112 return NONE;
Ayaka Koshibec4047702014-10-07 14:43:52 -0700113 }
114
115 @Override
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700116 public MastershipEvent setMaster(NodeId newMaster, DeviceId deviceId) {
tomb41d1ac2014-09-24 01:51:24 -0700117
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700118 roleMap.lock(deviceId);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700119 try {
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700120 final RoleValue rv = getRoleValue(deviceId);
121 final MastershipRole currentRole = rv.getRole(newMaster);
122 switch (currentRole) {
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700123 case MASTER:
Ayaka Koshibec4047702014-10-07 14:43:52 -0700124 //reinforce mastership
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700125 // RoleInfo integrity check
126 boolean modified = rv.reassign(newMaster, STANDBY, NONE);
127 if (modified) {
128 roleMap.put(deviceId, rv);
129 // should never reach here.
130 log.warn("{} was in both MASTER and STANDBY for {}", newMaster, deviceId);
131 // trigger BACKUPS_CHANGED?
132 }
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700133 return null;
134 case STANDBY:
Ayaka Koshibea7384a82014-10-22 18:59:06 -0700135 case NONE:
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700136 final NodeId currentMaster = rv.get(MASTER);
137 if (currentMaster != null) {
138 // place current master in STANDBY
139 rv.reassign(currentMaster, NONE, STANDBY);
140 rv.replace(currentMaster, newMaster, MASTER);
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700141 } else {
142 //no master before so just add.
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700143 rv.add(MASTER, newMaster);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700144 }
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700145 // remove newMaster from STANDBY
146 rv.reassign(newMaster, STANDBY, NONE);
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700147 updateTerm(deviceId);
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700148 roleMap.put(deviceId, rv);
Ayaka Koshibefc981cf2014-10-21 12:44:17 -0700149 return new MastershipEvent(MASTER_CHANGED, deviceId, rv.roleInfo());
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700150 default:
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700151 log.warn("unknown Mastership Role {}", currentRole);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700152 return null;
153 }
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700154 } finally {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700155 roleMap.unlock(deviceId);
tomb41d1ac2014-09-24 01:51:24 -0700156 }
157 }
158
159 @Override
160 public NodeId getMaster(DeviceId deviceId) {
Ayaka Koshibee8e45352014-10-16 00:37:19 -0700161 return getNode(MASTER, deviceId);
tomb41d1ac2014-09-24 01:51:24 -0700162 }
163
Ayaka Koshibe45503ce2014-10-14 11:26:45 -0700164
165 @Override
Ayaka Koshibeabedb092014-10-20 17:01:31 -0700166 public RoleInfo getNodes(DeviceId deviceId) {
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700167 RoleValue rv = roleMap.get(deviceId);
168 if (rv != null) {
Ayaka Koshibeabedb092014-10-20 17:01:31 -0700169 return rv.roleInfo();
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700170 } else {
171 return new RoleInfo();
Ayaka Koshibe45503ce2014-10-14 11:26:45 -0700172 }
173 }
174
tomb41d1ac2014-09-24 01:51:24 -0700175 @Override
176 public Set<DeviceId> getDevices(NodeId nodeId) {
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700177 Set<DeviceId> devices = new HashSet<>();
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700178
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700179 for (Map.Entry<DeviceId, RoleValue> el : roleMap.entrySet()) {
180 if (nodeId.equals(el.getValue().get(MASTER))) {
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700181 devices.add(el.getKey());
tomb41d1ac2014-09-24 01:51:24 -0700182 }
183 }
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700184
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700185 return devices;
tomb41d1ac2014-09-24 01:51:24 -0700186 }
187
188 @Override
189 public MastershipRole requestRole(DeviceId deviceId) {
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700190
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700191 // if no master => become master
192 // if there already exists a master:
193 // if I was the master return MASTER
194 // else put myself in STANDBY and return STANDBY
195
196 final NodeId local = clusterService.getLocalNode().id();
197 boolean modified = false;
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700198 roleMap.lock(deviceId);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700199 try {
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700200 final RoleValue rv = getRoleValue(deviceId);
201 if (rv.get(MASTER) == null) {
202 // there's no master become one
203 // move out from STANDBY
204 rv.reassign(local, STANDBY, NONE);
205 rv.add(MASTER, local);
206
207 updateTerm(deviceId);
208 roleMap.put(deviceId, rv);
209 return MASTER;
210 }
211 final MastershipRole currentRole = rv.getRole(local);
212 switch (currentRole) {
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700213 case MASTER:
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700214 // RoleInfo integrity check
215 modified = rv.reassign(local, STANDBY, NONE);
216 if (modified) {
217 log.warn("{} was in both MASTER and STANDBY for {}", local, deviceId);
218 // should never reach here,
219 // but heal if we happened to be there
220 roleMap.put(deviceId, rv);
221 // trigger BACKUPS_CHANGED?
222 }
223 return currentRole;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700224 case STANDBY:
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700225 // RoleInfo integrity check
226 modified = rv.reassign(local, NONE, STANDBY);
227 if (modified) {
228 log.warn("{} was in both NONE and STANDBY for {}", local, deviceId);
229 // should never reach here,
230 // but heal if we happened to be there
231 roleMap.put(deviceId, rv);
232 // trigger BACKUPS_CHANGED?
233 }
234 return currentRole;
235 case NONE:
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700236 rv.reassign(local, NONE, STANDBY);
Ayaka Koshibee8e45352014-10-16 00:37:19 -0700237 roleMap.put(deviceId, rv);
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700238 // TODO: notifyDelegate BACKUPS_CHANGED
239 return STANDBY;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700240 default:
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700241 log.warn("unknown Mastership Role {}", currentRole);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700242 }
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700243 return currentRole;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700244 } finally {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700245 roleMap.unlock(deviceId);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700246 }
tomb41d1ac2014-09-24 01:51:24 -0700247 }
248
249 @Override
Ayaka Koshibeb70d34b2014-09-25 15:43:01 -0700250 public MastershipTerm getTermFor(DeviceId deviceId) {
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700251 // term information and role must be read atomically
252 // acquiring write lock for the device
253 roleMap.lock(deviceId);
254 try {
255 RoleValue rv = getRoleValue(deviceId);
256 final Integer term = terms.get(deviceId);
257 final NodeId master = rv.get(MASTER);
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700258 if (term == null) {
259 return MastershipTerm.of(null, NOTHING);
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700260 }
261 return MastershipTerm.of(master, term);
262 } finally {
263 roleMap.unlock(deviceId);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700264 }
Ayaka Koshibeb70d34b2014-09-25 15:43:01 -0700265 }
266
Ayaka Koshibed9f693e2014-09-29 18:04:54 -0700267 @Override
Ayaka Koshibec4047702014-10-07 14:43:52 -0700268 public MastershipEvent setStandby(NodeId nodeId, DeviceId deviceId) {
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700269 // if nodeId was MASTER, rotate STANDBY
270 // if nodeId was STANDBY no-op
271 // if nodeId was NONE, add to STANDBY
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700272
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700273 roleMap.lock(deviceId);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700274 try {
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700275 final RoleValue rv = getRoleValue(deviceId);
276 final MastershipRole currentRole = getRole(nodeId, deviceId);
277 switch (currentRole) {
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700278 case MASTER:
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700279 NodeId newMaster = reelect(nodeId, deviceId, rv);
280 rv.reassign(nodeId, NONE, STANDBY);
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800281 updateTerm(deviceId);
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700282 if (newMaster != null) {
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700283 roleMap.put(deviceId, rv);
284 return new MastershipEvent(MASTER_CHANGED, deviceId, rv.roleInfo());
285 } else {
286 // no master candidate
287 roleMap.put(deviceId, rv);
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800288 // TODO: Should there be new event type for no MASTER?
289 return new MastershipEvent(MASTER_CHANGED, deviceId, rv.roleInfo());
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700290 }
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700291 case STANDBY:
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700292 return null;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700293 case NONE:
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700294 rv.reassign(nodeId, NONE, STANDBY);
Ayaka Koshibee8e45352014-10-16 00:37:19 -0700295 roleMap.put(deviceId, rv);
Ayaka Koshibe98bd12f2014-11-01 20:13:37 -0700296 return new MastershipEvent(BACKUPS_CHANGED, deviceId, rv.roleInfo());
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700297 default:
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700298 log.warn("unknown Mastership Role {}", currentRole);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700299 }
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700300 return null;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700301 } finally {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700302 roleMap.unlock(deviceId);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700303 }
304 }
305
Ayaka Koshibec4047702014-10-07 14:43:52 -0700306 @Override
307 public MastershipEvent relinquishRole(NodeId nodeId, DeviceId deviceId) {
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700308 // relinquishRole is basically set to None
309
310 // If nodeId was master reelect next and remove nodeId
311 // else remove from STANDBY
Ayaka Koshibe25fd23a2014-10-03 15:50:43 -0700312
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700313 roleMap.lock(deviceId);
Ayaka Koshibec4047702014-10-07 14:43:52 -0700314 try {
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700315 final RoleValue rv = getRoleValue(deviceId);
316 final MastershipRole currentRole = rv.getRole(nodeId);
317 switch (currentRole) {
Ayaka Koshibec4047702014-10-07 14:43:52 -0700318 case MASTER:
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700319 NodeId newMaster = reelect(nodeId, deviceId, rv);
320 if (newMaster != null) {
Yuta HIGUCHIf1d159a2014-10-29 23:31:40 -0700321 updateTerm(deviceId);
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700322 roleMap.put(deviceId, rv);
323 return new MastershipEvent(MASTER_CHANGED, deviceId, rv.roleInfo());
324 } else {
Ayaka Koshibe98bd12f2014-11-01 20:13:37 -0700325 // No master candidate - no more backups, device is likely
326 // fully disconnected
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700327 roleMap.put(deviceId, rv);
328 // Should there be new event type?
329 return null;
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700330 }
Ayaka Koshibec4047702014-10-07 14:43:52 -0700331 case STANDBY:
332 //fall through to reinforce relinquishment
333 case NONE:
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700334 boolean modified = rv.reassign(nodeId, STANDBY, NONE);
335 if (modified) {
336 roleMap.put(deviceId, rv);
Ayaka Koshibe98bd12f2014-11-01 20:13:37 -0700337 return new MastershipEvent(BACKUPS_CHANGED, deviceId, rv.roleInfo());
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700338 }
339 return null;
Ayaka Koshibec4047702014-10-07 14:43:52 -0700340 default:
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700341 log.warn("unknown Mastership Role {}", currentRole);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700342 }
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700343 return null;
Ayaka Koshibec4047702014-10-07 14:43:52 -0700344 } finally {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700345 roleMap.unlock(deviceId);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700346 }
Ayaka Koshibed9f693e2014-09-29 18:04:54 -0700347 }
348
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700349 // TODO: Consider moving this to RoleValue method
Ayaka Koshibec4047702014-10-07 14:43:52 -0700350 //helper to fetch a new master candidate for a given device.
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700351 private NodeId reelect(
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700352 NodeId current, DeviceId deviceId, RoleValue rv) {
Ayaka Koshibec4047702014-10-07 14:43:52 -0700353
354 //if this is an queue it'd be neater.
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700355 NodeId candidate = null;
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700356 for (NodeId n : rv.nodesOfRole(STANDBY)) {
357 if (!current.equals(n)) {
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700358 candidate = n;
Ayaka Koshibec4047702014-10-07 14:43:52 -0700359 break;
360 }
361 }
362
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700363 if (candidate == null) {
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700364 log.info("{} giving up and going to NONE for {}", current, deviceId);
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700365 rv.remove(MASTER, current);
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700366 // master did change, but there is no master candidate.
Ayaka Koshibec4047702014-10-07 14:43:52 -0700367 return null;
368 } else {
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700369 log.info("{} trying to pass mastership for {} to {}", current, deviceId, candidate);
370 rv.replace(current, candidate, MASTER);
371 rv.reassign(candidate, STANDBY, NONE);
372 return candidate;
Ayaka Koshibec4047702014-10-07 14:43:52 -0700373 }
374 }
375
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700376 //return the RoleValue structure for a device, or create one
377 private RoleValue getRoleValue(DeviceId deviceId) {
378 RoleValue value = roleMap.get(deviceId);
379 if (value == null) {
380 value = new RoleValue();
Ayaka Koshibea7384a82014-10-22 18:59:06 -0700381 RoleValue concurrentlyAdded = roleMap.putIfAbsent(deviceId, value);
382 if (concurrentlyAdded != null) {
383 return concurrentlyAdded;
384 }
Ayaka Koshibec4047702014-10-07 14:43:52 -0700385 }
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700386 return value;
Ayaka Koshibec4047702014-10-07 14:43:52 -0700387 }
388
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700389 //get first applicable node out of store-unique structure.
390 private NodeId getNode(MastershipRole role, DeviceId deviceId) {
391 RoleValue value = roleMap.get(deviceId);
392 if (value != null) {
393 return value.get(role);
Ayaka Koshibec4047702014-10-07 14:43:52 -0700394 }
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700395 return null;
396 }
397
Ayaka Koshibec4047702014-10-07 14:43:52 -0700398 //adds or updates term information.
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700399 // must be guarded by roleMap.lock(deviceId)
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700400 private void updateTerm(DeviceId deviceId) {
Yuta HIGUCHIf1d159a2014-10-29 23:31:40 -0700401 Integer term = terms.get(deviceId);
402 if (term == null) {
403 term = terms.putIfAbsent(deviceId, INIT);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700404 if (term == null) {
Yuta HIGUCHIf1d159a2014-10-29 23:31:40 -0700405 // initial term set successfully
406 return;
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700407 }
Yuta HIGUCHIf1d159a2014-10-29 23:31:40 -0700408 // concurrent initialization detected,
409 // fall through to try incrementing
410 }
411 Integer nextTerm = term + 1;
412 boolean success = terms.replace(deviceId, term, nextTerm);
413 while (!success) {
414 term = terms.get(deviceId);
415 if (term == null) {
416 // something is very wrong, but write something to avoid
417 // infinite loop.
418 log.warn("Term info for {} disappeared.", deviceId);
419 term = putIfAbsent(terms, deviceId, nextTerm);
420 }
421 nextTerm = term + 1;
422 success = terms.replace(deviceId, term, nextTerm);
Ayaka Koshibec4047702014-10-07 14:43:52 -0700423 }
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700424 }
425
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700426 private class RemoteMasterShipEventHandler implements EntryListener<DeviceId, RoleValue> {
alshabib339a3d92014-09-26 17:54:32 -0700427
428 @Override
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700429 public void entryAdded(EntryEvent<DeviceId, RoleValue> event) {
Yuta HIGUCHIeb5a0b92014-10-29 15:45:55 -0700430 entryUpdated(event);
alshabib339a3d92014-09-26 17:54:32 -0700431 }
432
433 @Override
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700434 public void entryRemoved(EntryEvent<DeviceId, RoleValue> event) {
alshabib339a3d92014-09-26 17:54:32 -0700435 }
436
437 @Override
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700438 public void entryUpdated(EntryEvent<DeviceId, RoleValue> event) {
Ayaka Koshibe98bd12f2014-11-01 20:13:37 -0700439 // compare old and current RoleValues. If master is different,
440 // emit MASTER_CHANGED. else, emit BACKUPS_CHANGED.
441 RoleValue oldValue = event.getOldValue();
442 RoleValue newValue = event.getValue();
443
Ayaka Koshibe15f4d602014-11-03 16:26:27 -0800444 // There will be no oldValue at the very first instance of an EntryEvent.
445 // Technically, the progression is: null event -> null master -> some master;
446 // We say a null master and a null oldValue are the same condition.
Yuta HIGUCHI780b1382014-11-03 14:36:39 -0800447 NodeId oldMaster = null;
448 if (oldValue != null) {
449 oldMaster = oldValue.get(MASTER);
450 }
451 NodeId newMaster = newValue.get(MASTER);
452
Ayaka Koshibe15f4d602014-11-03 16:26:27 -0800453 if (!Objects.equal(oldMaster, newMaster)) {
Ayaka Koshibe98bd12f2014-11-01 20:13:37 -0700454 notifyDelegate(new MastershipEvent(
455 MASTER_CHANGED, event.getKey(), event.getValue().roleInfo()));
456 } else {
457 notifyDelegate(new MastershipEvent(
458 BACKUPS_CHANGED, event.getKey(), event.getValue().roleInfo()));
459 }
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700460 }
461
462 @Override
463 public void entryEvicted(EntryEvent<DeviceId, RoleValue> event) {
464 }
465
466 @Override
467 public void mapEvicted(MapEvent event) {
468 }
469
470 @Override
471 public void mapCleared(MapEvent event) {
alshabib339a3d92014-09-26 17:54:32 -0700472 }
473 }
474
tomb41d1ac2014-09-24 01:51:24 -0700475}