Madan Jampani
Committed by Gerrit Code Review

ONOS-1798: More appropriate method name + better pre-condition checks

Change-Id: I5d277967c22b9db48097288ea072cbc4d1c5a2a8
...@@ -74,7 +74,8 @@ public interface LeadershipService { ...@@ -74,7 +74,8 @@ public interface LeadershipService {
74 * potentially become the leader again if and when it becomes the highest 74 * potentially become the leader again if and when it becomes the highest
75 * priority candidate 75 * priority candidate
76 * <p> 76 * <p>
77 - * If the local nodeId is not the leader, this method will be a noop. 77 + * If the local nodeId is not the leader, this method will make no changes and
78 + * simply return false.
78 * 79 *
79 * @param path topic for which this controller node should give up leadership 80 * @param path topic for which this controller node should give up leadership
80 * @return true if this node stepped down from leadership, false otherwise 81 * @return true if this node stepped down from leadership, false otherwise
......
...@@ -271,7 +271,7 @@ public class DistributedLeadershipManager implements LeadershipService { ...@@ -271,7 +271,7 @@ public class DistributedLeadershipManager implements LeadershipService {
271 271
272 @Override 272 @Override
273 public boolean stepdown(String path) { 273 public boolean stepdown(String path) {
274 - if (!activeTopics.contains(path)) { 274 + if (!activeTopics.contains(path) || !Objects.equals(localNodeId, getLeader(path))) {
275 return false; 275 return false;
276 } 276 }
277 277
......
...@@ -93,7 +93,7 @@ public class ConsistentDeviceMastershipStore ...@@ -93,7 +93,7 @@ public class ConsistentDeviceMastershipStore
93 new MessageSubject("mastership-store-device-role-query"); 93 new MessageSubject("mastership-store-device-role-query");
94 private static final MessageSubject ROLE_RELINQUISH_SUBJECT = 94 private static final MessageSubject ROLE_RELINQUISH_SUBJECT =
95 new MessageSubject("mastership-store-device-role-relinquish"); 95 new MessageSubject("mastership-store-device-role-relinquish");
96 - private static final MessageSubject MASTERSHIP_RELINQUISH_SUBJECT = 96 + private static final MessageSubject TRANSITION_FROM_MASTER_TO_STANDBY_SUBJECT =
97 new MessageSubject("mastership-store-device-mastership-relinquish"); 97 new MessageSubject("mastership-store-device-mastership-relinquish");
98 98
99 private static final Pattern DEVICE_MASTERSHIP_TOPIC_PATTERN = 99 private static final Pattern DEVICE_MASTERSHIP_TOPIC_PATTERN =
...@@ -128,9 +128,9 @@ public class ConsistentDeviceMastershipStore ...@@ -128,9 +128,9 @@ public class ConsistentDeviceMastershipStore
128 clusterCommunicator.addSubscriber(ROLE_RELINQUISH_SUBJECT, 128 clusterCommunicator.addSubscriber(ROLE_RELINQUISH_SUBJECT,
129 new RoleRelinquishHandler(), 129 new RoleRelinquishHandler(),
130 messageHandlingExecutor); 130 messageHandlingExecutor);
131 - clusterCommunicator.addSubscriber(MASTERSHIP_RELINQUISH_SUBJECT, 131 + clusterCommunicator.addSubscriber(TRANSITION_FROM_MASTER_TO_STANDBY_SUBJECT,
132 SERIALIZER::decode, 132 SERIALIZER::decode,
133 - this::relinquishMastership, 133 + this::transitionFromMasterToStandby,
134 SERIALIZER::encode, 134 SERIALIZER::encode,
135 messageHandlingExecutor); 135 messageHandlingExecutor);
136 localNodeId = clusterService.getLocalNode().id(); 136 localNodeId = clusterService.getLocalNode().id();
...@@ -143,7 +143,7 @@ public class ConsistentDeviceMastershipStore ...@@ -143,7 +143,7 @@ public class ConsistentDeviceMastershipStore
143 public void deactivate() { 143 public void deactivate() {
144 clusterCommunicator.removeSubscriber(ROLE_QUERY_SUBJECT); 144 clusterCommunicator.removeSubscriber(ROLE_QUERY_SUBJECT);
145 clusterCommunicator.removeSubscriber(ROLE_RELINQUISH_SUBJECT); 145 clusterCommunicator.removeSubscriber(ROLE_RELINQUISH_SUBJECT);
146 - clusterCommunicator.removeSubscriber(MASTERSHIP_RELINQUISH_SUBJECT); 146 + clusterCommunicator.removeSubscriber(TRANSITION_FROM_MASTER_TO_STANDBY_SUBJECT);
147 messageHandlingExecutor.shutdown(); 147 messageHandlingExecutor.shutdown();
148 leadershipService.removeListener(leadershipEventListener); 148 leadershipService.removeListener(leadershipEventListener);
149 149
...@@ -256,7 +256,7 @@ public class ConsistentDeviceMastershipStore ...@@ -256,7 +256,7 @@ public class ConsistentDeviceMastershipStore
256 return null; 256 return null;
257 } 257 }
258 if (leadershipService.makeTopCandidate(leadershipTopic, nodeId)) { 258 if (leadershipService.makeTopCandidate(leadershipTopic, nodeId)) {
259 - return relinquishMastership(deviceId); 259 + return transitionFromMasterToStandby(deviceId);
260 } else { 260 } else {
261 log.warn("Failed to promote {} to mastership for {}", nodeId, deviceId); 261 log.warn("Failed to promote {} to mastership for {}", nodeId, deviceId);
262 } 262 }
...@@ -282,9 +282,9 @@ public class ConsistentDeviceMastershipStore ...@@ -282,9 +282,9 @@ public class ConsistentDeviceMastershipStore
282 if (!nodeId.equals(currentMaster)) { 282 if (!nodeId.equals(currentMaster)) {
283 return null; 283 return null;
284 } 284 }
285 - // FIXME: This can becomes the master again unless it 285 + // FIXME: This can become the master again unless it
286 - // is demoted to the end of candidates list. 286 + // is first demoted to the end of candidates list.
287 - return relinquishMastership(deviceId); 287 + return transitionFromMasterToStandby(deviceId);
288 } 288 }
289 289
290 @Override 290 @Override
...@@ -309,14 +309,11 @@ public class ConsistentDeviceMastershipStore ...@@ -309,14 +309,11 @@ public class ConsistentDeviceMastershipStore
309 } 309 }
310 310
311 String leadershipTopic = createDeviceMastershipTopic(deviceId); 311 String leadershipTopic = createDeviceMastershipTopic(deviceId);
312 - Leadership currentLeadership = leadershipService.getLeadership(leadershipTopic); 312 + NodeId currentLeader = leadershipService.getLeader(leadershipTopic);
313 313
314 - MastershipEvent.Type eventType = null; 314 + MastershipEvent.Type eventType = Objects.equal(currentLeader, localNodeId)
315 - if (currentLeadership != null && currentLeadership.leader().equals(localNodeId)) { 315 + ? MastershipEvent.Type.MASTER_CHANGED
316 - eventType = MastershipEvent.Type.MASTER_CHANGED; 316 + : MastershipEvent.Type.BACKUPS_CHANGED;
317 - } else {
318 - eventType = MastershipEvent.Type.BACKUPS_CHANGED;
319 - }
320 317
321 connectedDevices.remove(deviceId); 318 connectedDevices.remove(deviceId);
322 leadershipService.withdraw(leadershipTopic); 319 leadershipService.withdraw(leadershipTopic);
...@@ -324,7 +321,7 @@ public class ConsistentDeviceMastershipStore ...@@ -324,7 +321,7 @@ public class ConsistentDeviceMastershipStore
324 return new MastershipEvent(eventType, deviceId, getNodes(deviceId)); 321 return new MastershipEvent(eventType, deviceId, getNodes(deviceId));
325 } 322 }
326 323
327 - private MastershipEvent relinquishMastership(DeviceId deviceId) { 324 + private MastershipEvent transitionFromMasterToStandby(DeviceId deviceId) {
328 checkArgument(deviceId != null, DEVICE_ID_NULL); 325 checkArgument(deviceId != null, DEVICE_ID_NULL);
329 326
330 NodeId currentMaster = getMaster(deviceId); 327 NodeId currentMaster = getMaster(deviceId);
...@@ -337,22 +334,14 @@ public class ConsistentDeviceMastershipStore ...@@ -337,22 +334,14 @@ public class ConsistentDeviceMastershipStore
337 + "mastership for device {} to {}", deviceId, currentMaster); 334 + "mastership for device {} to {}", deviceId, currentMaster);
338 return futureGetOrElse(clusterCommunicator.sendAndReceive( 335 return futureGetOrElse(clusterCommunicator.sendAndReceive(
339 deviceId, 336 deviceId,
340 - MASTERSHIP_RELINQUISH_SUBJECT, 337 + TRANSITION_FROM_MASTER_TO_STANDBY_SUBJECT,
341 SERIALIZER::encode, 338 SERIALIZER::encode,
342 SERIALIZER::decode, 339 SERIALIZER::decode,
343 currentMaster), null); 340 currentMaster), null);
344 } 341 }
345 342
346 - String leadershipTopic = createDeviceMastershipTopic(deviceId); 343 + return leadershipService.stepdown(createDeviceMastershipTopic(deviceId))
347 - Leadership currentLeadership = leadershipService.getLeadership(leadershipTopic); 344 + ? new MastershipEvent(MastershipEvent.Type.MASTER_CHANGED, deviceId, getNodes(deviceId)) : null;
348 -
349 - MastershipEvent.Type eventType = null;
350 - if (currentLeadership != null && currentLeadership.leader().equals(localNodeId)) {
351 - eventType = MastershipEvent.Type.MASTER_CHANGED;
352 - }
353 -
354 - return leadershipService.stepdown(leadershipTopic)
355 - ? new MastershipEvent(eventType, deviceId, getNodes(deviceId)) : null;
356 } 345 }
357 346
358 private class RoleQueryHandler implements ClusterMessageHandler { 347 private class RoleQueryHandler implements ClusterMessageHandler {
......