Madan Jampani
Committed by Gerrit Code Review

When relinquishing mastership, wait for LeadershipService::withdraw to complete …

…before returning a MastershipEvent

Change-Id: Ieb0816bf3d17fee28815ea59959ed6d4a4efb478
...@@ -127,7 +127,7 @@ public class ConsistentDeviceMastershipStore ...@@ -127,7 +127,7 @@ public class ConsistentDeviceMastershipStore
127 transferExecutor = 127 transferExecutor =
128 Executors.newSingleThreadScheduledExecutor( 128 Executors.newSingleThreadScheduledExecutor(
129 groupedThreads("onos/store/device/mastership", "mastership-transfer-executor")); 129 groupedThreads("onos/store/device/mastership", "mastership-transfer-executor"));
130 - clusterCommunicator.<DeviceId, MastershipEvent>addSubscriber(ROLE_RELINQUISH_SUBJECT, 130 + clusterCommunicator.addSubscriber(ROLE_RELINQUISH_SUBJECT,
131 SERIALIZER::decode, 131 SERIALIZER::decode,
132 this::relinquishLocalRole, 132 this::relinquishLocalRole,
133 SERIALIZER::encode, 133 SERIALIZER::encode,
...@@ -310,7 +310,10 @@ public class ConsistentDeviceMastershipStore ...@@ -310,7 +310,10 @@ public class ConsistentDeviceMastershipStore
310 checkArgument(nodeId != null, NODE_ID_NULL); 310 checkArgument(nodeId != null, NODE_ID_NULL);
311 checkArgument(deviceId != null, DEVICE_ID_NULL); 311 checkArgument(deviceId != null, DEVICE_ID_NULL);
312 312
313 - if (!nodeId.equals(localNodeId)) { 313 + if (nodeId.equals(localNodeId)) {
314 + return relinquishLocalRole(deviceId);
315 + }
316 +
314 log.debug("Forwarding request to relinquish " 317 log.debug("Forwarding request to relinquish "
315 + "role for device {} to {}", deviceId, nodeId); 318 + "role for device {} to {}", deviceId, nodeId);
316 return clusterCommunicator.sendAndReceive( 319 return clusterCommunicator.sendAndReceive(
...@@ -320,15 +323,13 @@ public class ConsistentDeviceMastershipStore ...@@ -320,15 +323,13 @@ public class ConsistentDeviceMastershipStore
320 SERIALIZER::decode, 323 SERIALIZER::decode,
321 nodeId); 324 nodeId);
322 } 325 }
323 - return CompletableFuture.completedFuture(relinquishLocalRole(deviceId));
324 - }
325 326
326 - private MastershipEvent relinquishLocalRole(DeviceId deviceId) { 327 + private CompletableFuture<MastershipEvent> relinquishLocalRole(DeviceId deviceId) {
327 checkArgument(deviceId != null, DEVICE_ID_NULL); 328 checkArgument(deviceId != null, DEVICE_ID_NULL);
328 329
329 // Check if this node is can be managed by this node. 330 // Check if this node is can be managed by this node.
330 if (!connectedDevices.contains(deviceId)) { 331 if (!connectedDevices.contains(deviceId)) {
331 - return null; 332 + return CompletableFuture.completedFuture(null);
332 } 333 }
333 334
334 String leadershipTopic = createDeviceMastershipTopic(deviceId); 335 String leadershipTopic = createDeviceMastershipTopic(deviceId);
...@@ -339,9 +340,8 @@ public class ConsistentDeviceMastershipStore ...@@ -339,9 +340,8 @@ public class ConsistentDeviceMastershipStore
339 : MastershipEvent.Type.BACKUPS_CHANGED; 340 : MastershipEvent.Type.BACKUPS_CHANGED;
340 341
341 connectedDevices.remove(deviceId); 342 connectedDevices.remove(deviceId);
342 - leadershipService.withdraw(leadershipTopic); 343 + return leadershipService.withdraw(leadershipTopic)
343 - 344 + .thenApply(v -> new MastershipEvent(eventType, deviceId, getNodes(deviceId)));
344 - return new MastershipEvent(eventType, deviceId, getNodes(deviceId));
345 } 345 }
346 346
347 private MastershipEvent transitionFromMasterToStandby(DeviceId deviceId) { 347 private MastershipEvent transitionFromMasterToStandby(DeviceId deviceId) {
......