Jian Li
Committed by Gerrit Code Review

Add a set of synchronous version of methods for MastershipService

Change-Id: I61da650eb545c14b5fa824ac92d1ebe62126dec3
...@@ -16,7 +16,9 @@ ...@@ -16,7 +16,9 @@
16 package org.onosproject.mastership; 16 package org.onosproject.mastership;
17 17
18 import java.util.concurrent.CompletableFuture; 18 import java.util.concurrent.CompletableFuture;
19 +import java.util.concurrent.TimeUnit;
19 20
21 +import org.onlab.util.Tools;
20 import org.onosproject.cluster.NodeId; 22 import org.onosproject.cluster.NodeId;
21 import org.onosproject.net.DeviceId; 23 import org.onosproject.net.DeviceId;
22 import org.onosproject.net.MastershipRole; 24 import org.onosproject.net.MastershipRole;
...@@ -26,6 +28,8 @@ import org.onosproject.net.MastershipRole; ...@@ -26,6 +28,8 @@ import org.onosproject.net.MastershipRole;
26 */ 28 */
27 public interface MastershipAdminService { 29 public interface MastershipAdminService {
28 30
31 + long TIMEOUT_MILLIS = 3000;
32 +
29 /** 33 /**
30 * Applies the current mastership role for the specified device. 34 * Applies the current mastership role for the specified device.
31 * 35 *
...@@ -37,6 +41,18 @@ public interface MastershipAdminService { ...@@ -37,6 +41,18 @@ public interface MastershipAdminService {
37 CompletableFuture<Void> setRole(NodeId instance, DeviceId deviceId, MastershipRole role); 41 CompletableFuture<Void> setRole(NodeId instance, DeviceId deviceId, MastershipRole role);
38 42
39 /** 43 /**
44 + * Synchronous version of setRole.
45 + * Applies the current mastership role for the specified device.
46 + *
47 + * @param instance controller instance identifier
48 + * @param deviceId device identifier
49 + * @param role requested role
50 + */
51 + default void setRoleSync(NodeId instance, DeviceId deviceId, MastershipRole role) {
52 + Tools.futureGetOrElse(setRole(instance, deviceId, role), TIMEOUT_MILLIS, TimeUnit.MILLISECONDS, null);
53 + }
54 +
55 + /**
40 * Balances the mastership to be shared as evenly as possibly by all 56 * Balances the mastership to be shared as evenly as possibly by all
41 * online instances. 57 * online instances.
42 */ 58 */
......
...@@ -16,10 +16,13 @@ ...@@ -16,10 +16,13 @@
16 package org.onosproject.mastership; 16 package org.onosproject.mastership;
17 17
18 import static org.onosproject.net.MastershipRole.MASTER; 18 import static org.onosproject.net.MastershipRole.MASTER;
19 +import static org.onosproject.net.MastershipRole.NONE;
19 20
20 import java.util.Set; 21 import java.util.Set;
21 import java.util.concurrent.CompletableFuture; 22 import java.util.concurrent.CompletableFuture;
23 +import java.util.concurrent.TimeUnit;
22 24
25 +import org.onlab.util.Tools;
23 import org.onosproject.cluster.NodeId; 26 import org.onosproject.cluster.NodeId;
24 import org.onosproject.cluster.RoleInfo; 27 import org.onosproject.cluster.RoleInfo;
25 import org.onosproject.event.ListenerService; 28 import org.onosproject.event.ListenerService;
...@@ -35,11 +38,13 @@ import org.onosproject.net.MastershipRole; ...@@ -35,11 +38,13 @@ import org.onosproject.net.MastershipRole;
35 public interface MastershipService 38 public interface MastershipService
36 extends ListenerService<MastershipEvent, MastershipListener> { 39 extends ListenerService<MastershipEvent, MastershipListener> {
37 40
41 + long TIMEOUT_MILLIS = 3000;
42 +
38 /** 43 /**
39 * Returns the role of the local node for the specified device, without 44 * Returns the role of the local node for the specified device, without
40 * triggering master selection. 45 * triggering master selection.
41 * 46 *
42 - * @param deviceId the the identifier of the device 47 + * @param deviceId the identifier of the device
43 * @return role of the current node 48 * @return role of the current node
44 */ 49 */
45 MastershipRole getLocalRole(DeviceId deviceId); 50 MastershipRole getLocalRole(DeviceId deviceId);
...@@ -47,7 +52,7 @@ public interface MastershipService ...@@ -47,7 +52,7 @@ public interface MastershipService
47 /** 52 /**
48 * Returns true if the local controller is the Master for the specified deviceId. 53 * Returns true if the local controller is the Master for the specified deviceId.
49 * 54 *
50 - * @param deviceId the the identifier of the device 55 + * @param deviceId the identifier of the device
51 * @return true if local node is master; false otherwise 56 * @return true if local node is master; false otherwise
52 */ 57 */
53 default boolean isLocalMaster(DeviceId deviceId) { 58 default boolean isLocalMaster(DeviceId deviceId) {
...@@ -58,12 +63,24 @@ public interface MastershipService ...@@ -58,12 +63,24 @@ public interface MastershipService
58 * Returns the mastership status of the local controller for a given 63 * Returns the mastership status of the local controller for a given
59 * device forcing master selection if necessary. 64 * device forcing master selection if necessary.
60 * 65 *
61 - * @param deviceId the the identifier of the device 66 + * @param deviceId the identifier of the device
62 - * @return the role of this controller instance 67 + * @return future object of this controller instance role
63 */ 68 */
64 CompletableFuture<MastershipRole> requestRoleFor(DeviceId deviceId); 69 CompletableFuture<MastershipRole> requestRoleFor(DeviceId deviceId);
65 70
66 /** 71 /**
72 + * Synchronous version of requestRoleFor. Returns the mastership status of
73 + * the local controller for a given device forcing master selection if necessary.
74 + *
75 + * @param deviceId the identifier of the device
76 + * @return the role of this controller instance
77 + */
78 + default MastershipRole requestRoleForSync(DeviceId deviceId) {
79 + return Tools.futureGetOrElse(requestRoleFor(deviceId),
80 + TIMEOUT_MILLIS, TimeUnit.MILLISECONDS, NONE);
81 + }
82 +
83 + /**
67 * Abandons mastership of the specified device on the local node thus 84 * Abandons mastership of the specified device on the local node thus
68 * forcing selection of a new master. If the local node is not a master 85 * forcing selection of a new master. If the local node is not a master
69 * for this device, no master selection will occur. 86 * for this device, no master selection will occur.
...@@ -74,6 +91,18 @@ public interface MastershipService ...@@ -74,6 +91,18 @@ public interface MastershipService
74 CompletableFuture<Void> relinquishMastership(DeviceId deviceId); 91 CompletableFuture<Void> relinquishMastership(DeviceId deviceId);
75 92
76 /** 93 /**
94 + * Synchronous version of relinquishMastership. Abandons mastership of the
95 + * specified device on the local node thus forcing selection of a new master.
96 + * If the local node is not a master for this device, no master selection will occur.
97 + *
98 + * @param deviceId the identifier of the device
99 + */
100 + default void relinquishMastershipSync(DeviceId deviceId) {
101 + Tools.futureGetOrElse(relinquishMastership(deviceId),
102 + TIMEOUT_MILLIS, TimeUnit.MILLISECONDS, null);
103 + }
104 +
105 + /**
77 * Returns the current master for a given device. 106 * Returns the current master for a given device.
78 * 107 *
79 * @param deviceId the identifier of the device 108 * @param deviceId the identifier of the device
......