Madan Jampani
Committed by Gerrit Code Review

MastershipService.relinquishRole returns CompletableFuture + Block deviceDiconne…

…cted until role relinquish is complete

Change-Id: I081df48fc05fdca2e452a937a093d5caa16091ed
...@@ -55,8 +55,9 @@ public interface MastershipService { ...@@ -55,8 +55,9 @@ public interface MastershipService {
55 * for this device, no master selection will occur. 55 * for this device, no master selection will occur.
56 * 56 *
57 * @param deviceId the identifier of the device 57 * @param deviceId the identifier of the device
58 + * @return future that is completed when relinquish is complete
58 */ 59 */
59 - void relinquishMastership(DeviceId deviceId); 60 + CompletableFuture<Void> relinquishMastership(DeviceId deviceId);
60 61
61 /** 62 /**
62 * Returns the current master for a given device. 63 * Returns the current master for a given device.
......
...@@ -38,7 +38,8 @@ public class MastershipServiceAdapter implements MastershipService { ...@@ -38,7 +38,8 @@ public class MastershipServiceAdapter implements MastershipService {
38 } 38 }
39 39
40 @Override 40 @Override
41 - public void relinquishMastership(DeviceId deviceId) { 41 + public CompletableFuture<Void> relinquishMastership(DeviceId deviceId) {
42 + return null;
42 } 43 }
43 44
44 @Override 45 @Override
......
...@@ -95,12 +95,13 @@ public class MastershipManager ...@@ -95,12 +95,13 @@ public class MastershipManager
95 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 95 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
96 protected MetricsService metricsService; 96 protected MetricsService metricsService;
97 97
98 + private NodeId localNodeId;
98 private Timer requestRoleTimer; 99 private Timer requestRoleTimer;
99 100
100 @Activate 101 @Activate
101 public void activate() { 102 public void activate() {
102 requestRoleTimer = createTimer("Mastership", "requestRole", "responseTime"); 103 requestRoleTimer = createTimer("Mastership", "requestRole", "responseTime");
103 - 104 + localNodeId = clusterService.getLocalNode().id();
104 eventDispatcher.addSink(MastershipEvent.class, listenerRegistry); 105 eventDispatcher.addSink(MastershipEvent.class, listenerRegistry);
105 store.setDelegate(delegate); 106 store.setDelegate(delegate);
106 log.info("Started"); 107 log.info("Started");
...@@ -136,11 +137,8 @@ public class MastershipManager ...@@ -136,11 +137,8 @@ public class MastershipManager
136 return CompletableFuture.completedFuture(null); 137 return CompletableFuture.completedFuture(null);
137 } 138 }
138 139
139 - return eventFuture.whenComplete((event, error) -> { 140 + return eventFuture.thenAccept(this::post)
140 - if (event != null) { 141 + .thenApply(v -> null);
141 - post(event);
142 - }
143 - }).thenApply(v -> null);
144 } 142 }
145 143
146 @Override 144 @Override
...@@ -152,15 +150,11 @@ public class MastershipManager ...@@ -152,15 +150,11 @@ public class MastershipManager
152 } 150 }
153 151
154 @Override 152 @Override
155 - public void relinquishMastership(DeviceId deviceId) { 153 + public CompletableFuture<Void> relinquishMastership(DeviceId deviceId) {
156 checkPermission(Permission.CLUSTER_WRITE); 154 checkPermission(Permission.CLUSTER_WRITE);
157 - 155 + return store.relinquishRole(localNodeId, deviceId)
158 - store.relinquishRole(clusterService.getLocalNode().id(), deviceId) 156 + .thenAccept(this::post)
159 - .whenComplete((event, error) -> { 157 + .thenApply(v -> null);
160 - if (event != null) {
161 - post(event);
162 - }
163 - });
164 } 158 }
165 159
166 @Override 160 @Override
......
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
16 package org.onosproject.net.device.impl; 16 package org.onosproject.net.device.impl;
17 17
18 import com.google.common.collect.Lists; 18 import com.google.common.collect.Lists;
19 -
20 import org.apache.felix.scr.annotations.Activate; 19 import org.apache.felix.scr.annotations.Activate;
21 import org.apache.felix.scr.annotations.Component; 20 import org.apache.felix.scr.annotations.Component;
22 import org.apache.felix.scr.annotations.Deactivate; 21 import org.apache.felix.scr.annotations.Deactivate;
...@@ -411,8 +410,15 @@ public class DeviceManager ...@@ -411,8 +410,15 @@ public class DeviceManager
411 } 410 }
412 }); 411 });
413 } finally { 412 } finally {
414 - //relinquish master role and ability to be backup. 413 + try {
415 - mastershipService.relinquishMastership(deviceId); 414 + //relinquish master role and ability to be backup.
415 + mastershipService.relinquishMastership(deviceId).get();
416 + } catch (InterruptedException e) {
417 + log.warn("Interrupted while reliquishing role for {}", deviceId);
418 + Thread.currentThread().interrupt();
419 + } catch (ExecutionException e) {
420 + log.error("Exception thrown while relinquishing role for {}", deviceId, e);
421 + }
416 } 422 }
417 } 423 }
418 424
......
...@@ -311,6 +311,11 @@ public class DeviceManagerTest { ...@@ -311,6 +311,11 @@ public class DeviceManagerTest {
311 } 311 }
312 312
313 @Override 313 @Override
314 + public CompletableFuture<Void> relinquishMastership(DeviceId deviceId) {
315 + return CompletableFuture.completedFuture(null);
316 + }
317 +
318 + @Override
314 public MastershipTerm getMastershipTerm(DeviceId deviceId) { 319 public MastershipTerm getMastershipTerm(DeviceId deviceId) {
315 // FIXME: just returning something not null 320 // FIXME: just returning something not null
316 return MastershipTerm.of(NID_LOCAL, 1); 321 return MastershipTerm.of(NID_LOCAL, 1);
......
...@@ -37,6 +37,7 @@ import org.onosproject.net.Annotations; ...@@ -37,6 +37,7 @@ import org.onosproject.net.Annotations;
37 import org.onosproject.net.DefaultAnnotations; 37 import org.onosproject.net.DefaultAnnotations;
38 import org.onosproject.net.Device; 38 import org.onosproject.net.Device;
39 import org.onosproject.net.DeviceId; 39 import org.onosproject.net.DeviceId;
40 +import org.onosproject.net.MastershipRole;
40 import org.onosproject.net.Port; 41 import org.onosproject.net.Port;
41 import org.onosproject.net.PortNumber; 42 import org.onosproject.net.PortNumber;
42 import org.onosproject.net.SparseAnnotations; 43 import org.onosproject.net.SparseAnnotations;
...@@ -62,6 +63,7 @@ import java.util.HashMap; ...@@ -62,6 +63,7 @@ import java.util.HashMap;
62 import java.util.List; 63 import java.util.List;
63 import java.util.Map; 64 import java.util.Map;
64 import java.util.Set; 65 import java.util.Set;
66 +import java.util.concurrent.CompletableFuture;
65 import java.util.concurrent.CountDownLatch; 67 import java.util.concurrent.CountDownLatch;
66 import java.util.concurrent.ExecutorService; 68 import java.util.concurrent.ExecutorService;
67 import java.util.concurrent.TimeUnit; 69 import java.util.concurrent.TimeUnit;
...@@ -854,6 +856,10 @@ public class GossipDeviceStoreTest { ...@@ -854,6 +856,10 @@ public class GossipDeviceStoreTest {
854 public NodeId getMasterFor(DeviceId deviceId) { 856 public NodeId getMasterFor(DeviceId deviceId) {
855 return NID1; 857 return NID1;
856 } 858 }
859 + @Override
860 + public CompletableFuture<MastershipRole> requestRoleFor(DeviceId deviceId) {
861 + return CompletableFuture.completedFuture(null);
862 + }
857 } 863 }
858 864
859 private static final class TestGossipDeviceStore extends GossipDeviceStore { 865 private static final class TestGossipDeviceStore extends GossipDeviceStore {
......
...@@ -502,8 +502,8 @@ public class LLDPLinkProviderTest { ...@@ -502,8 +502,8 @@ public class LLDPLinkProviderTest {
502 } 502 }
503 503
504 @Override 504 @Override
505 - public void relinquishMastership(DeviceId deviceId) { 505 + public CompletableFuture<Void> relinquishMastership(DeviceId deviceId) {
506 - 506 + return null;
507 } 507 }
508 508
509 @Override 509 @Override
......