Yuta HIGUCHI

DistributedMastershipStore

- try to avoid unnecessary remote writes
- avoid reads from blocking if possible
- atomically read term info

Change-Id: I50badc718726261ccb14a6feefc578b420d28923
...@@ -15,6 +15,10 @@ ...@@ -15,6 +15,10 @@
15 */ 15 */
16 package org.onlab.onos.store.mastership.impl; 16 package org.onlab.onos.store.mastership.impl;
17 17
18 +import static org.onlab.onos.net.MastershipRole.MASTER;
19 +import static org.onlab.onos.net.MastershipRole.NONE;
20 +import static org.onlab.onos.net.MastershipRole.STANDBY;
21 +
18 import java.util.Collections; 22 import java.util.Collections;
19 import java.util.EnumMap; 23 import java.util.EnumMap;
20 import java.util.LinkedList; 24 import java.util.LinkedList;
...@@ -59,18 +63,30 @@ final class RoleValue { ...@@ -59,18 +63,30 @@ final class RoleValue {
59 return value.get(type).contains(nodeId); 63 return value.get(type).contains(nodeId);
60 } 64 }
61 65
66 + public MastershipRole getRole(NodeId nodeId) {
67 + if (contains(MASTER, nodeId)) {
68 + return MASTER;
69 + }
70 + if (contains(STANDBY, nodeId)) {
71 + return STANDBY;
72 + }
73 + return NONE;
74 + }
75 +
62 /** 76 /**
63 * Associates a node to a certain role. 77 * Associates a node to a certain role.
64 * 78 *
65 * @param type the role 79 * @param type the role
66 * @param nodeId the node ID of the node to associate 80 * @param nodeId the node ID of the node to associate
81 + * @return true if modified
67 */ 82 */
68 - public void add(MastershipRole type, NodeId nodeId) { 83 + public boolean add(MastershipRole type, NodeId nodeId) {
69 List<NodeId> nodes = value.get(type); 84 List<NodeId> nodes = value.get(type);
70 85
71 if (!nodes.contains(nodeId)) { 86 if (!nodes.contains(nodeId)) {
72 - nodes.add(nodeId); 87 + return nodes.add(nodeId);
73 } 88 }
89 + return false;
74 } 90 }
75 91
76 /** 92 /**
...@@ -78,7 +94,7 @@ final class RoleValue { ...@@ -78,7 +94,7 @@ final class RoleValue {
78 * 94 *
79 * @param type the role 95 * @param type the role
80 * @param nodeId the ID of the node to remove 96 * @param nodeId the ID of the node to remove
81 - * @return 97 + * @return true if modified
82 */ 98 */
83 public boolean remove(MastershipRole type, NodeId nodeId) { 99 public boolean remove(MastershipRole type, NodeId nodeId) {
84 List<NodeId> nodes = value.get(type); 100 List<NodeId> nodes = value.get(type);
...@@ -96,10 +112,12 @@ final class RoleValue { ...@@ -96,10 +112,12 @@ final class RoleValue {
96 * @param nodeId the Node ID of node changing roles 112 * @param nodeId the Node ID of node changing roles
97 * @param from the old role 113 * @param from the old role
98 * @param to the new role 114 * @param to the new role
115 + * @return true if modified
99 */ 116 */
100 - public void reassign(NodeId nodeId, MastershipRole from, MastershipRole to) { 117 + public boolean reassign(NodeId nodeId, MastershipRole from, MastershipRole to) {
101 - remove(from, nodeId); 118 + boolean modified = remove(from, nodeId);
102 - add(to, nodeId); 119 + modified |= add(to, nodeId);
120 + return modified;
103 } 121 }
104 122
105 /** 123 /**
...@@ -109,10 +127,12 @@ final class RoleValue { ...@@ -109,10 +127,12 @@ final class RoleValue {
109 * @param from the old NodeId to replace 127 * @param from the old NodeId to replace
110 * @param to the new NodeId 128 * @param to the new NodeId
111 * @param type the role associated with the old NodeId 129 * @param type the role associated with the old NodeId
130 + * @return true if modified
112 */ 131 */
113 - public void replace(NodeId from, NodeId to, MastershipRole type) { 132 + public boolean replace(NodeId from, NodeId to, MastershipRole type) {
114 - remove(type, from); 133 + boolean modified = remove(type, from);
115 - add(type, to); 134 + modified |= add(type, to);
135 + return modified;
116 } 136 }
117 137
118 /** 138 /**
......
...@@ -142,7 +142,7 @@ public class DistributedMastershipStoreTest { ...@@ -142,7 +142,7 @@ public class DistributedMastershipStoreTest {
142 testStore.setCurrent(CN1); 142 testStore.setCurrent(CN1);
143 143
144 //if already MASTER, nothing should happen 144 //if already MASTER, nothing should happen
145 - testStore.put(DID2, N1, true, false, false); 145 + testStore.put(DID2, N1, true, false, true);
146 assertEquals("wrong role for MASTER:", MASTER, dms.requestRole(DID2)); 146 assertEquals("wrong role for MASTER:", MASTER, dms.requestRole(DID2));
147 147
148 //populate maps with DID1, N1 thru NONE case 148 //populate maps with DID1, N1 thru NONE case
......