Ayaka Koshibe

MastershipEvent carries RoleInfo as a subject

Change-Id: I8f850ad1f21bfbdc23fedda1a53f4ccedb1b8d32
...@@ -21,7 +21,7 @@ import org.onlab.onos.net.device.DeviceService; ...@@ -21,7 +21,7 @@ import org.onlab.onos.net.device.DeviceService;
21 description = "Lists mastership roles of nodes for each device.") 21 description = "Lists mastership roles of nodes for each device.")
22 public class RolesCommand extends AbstractShellCommand { 22 public class RolesCommand extends AbstractShellCommand {
23 23
24 - private static final String FMT_HDR = "%s: master=%s, standbys=%s"; 24 + private static final String FMT_HDR = "%s: master=%s, standbys=[ %s]";
25 25
26 @Override 26 @Override
27 protected void execute() { 27 protected void execute() {
......
...@@ -3,10 +3,11 @@ package org.onlab.onos.cluster; ...@@ -3,10 +3,11 @@ package org.onlab.onos.cluster;
3 import java.util.List; 3 import java.util.List;
4 import java.util.Objects; 4 import java.util.Objects;
5 5
6 +import com.google.common.base.MoreObjects;
6 import com.google.common.collect.ImmutableList; 7 import com.google.common.collect.ImmutableList;
7 8
8 /** 9 /**
9 - * A container for detailed role information for a device, 10 + * An immutable container for role information for a device,
10 * within the current cluster. Role attributes include current 11 * within the current cluster. Role attributes include current
11 * master and a preference-ordered list of backup nodes. 12 * master and a preference-ordered list of backup nodes.
12 */ 13 */
...@@ -52,12 +53,9 @@ public class RoleInfo { ...@@ -52,12 +53,9 @@ public class RoleInfo {
52 53
53 @Override 54 @Override
54 public String toString() { 55 public String toString() {
55 - final StringBuilder builder = new StringBuilder(); 56 + return MoreObjects.toStringHelper(this.getClass())
56 - builder.append("master:").append(master).append(","); 57 + .add("master", master)
57 - builder.append("backups:"); 58 + .add("backups", backups)
58 - for (NodeId n : backups) { 59 + .toString();
59 - builder.append(" ").append(n);
60 - }
61 - return builder.toString();
62 } 60 }
63 } 61 }
......
1 package org.onlab.onos.mastership; 1 package org.onlab.onos.mastership;
2 2
3 import org.onlab.onos.cluster.NodeId; 3 import org.onlab.onos.cluster.NodeId;
4 +import org.onlab.onos.cluster.RoleInfo;
4 import org.onlab.onos.event.AbstractEvent; 5 import org.onlab.onos.event.AbstractEvent;
5 import org.onlab.onos.net.DeviceId; 6 import org.onlab.onos.net.DeviceId;
6 7
...@@ -9,9 +10,8 @@ import org.onlab.onos.net.DeviceId; ...@@ -9,9 +10,8 @@ import org.onlab.onos.net.DeviceId;
9 */ 10 */
10 public class MastershipEvent extends AbstractEvent<MastershipEvent.Type, DeviceId> { 11 public class MastershipEvent extends AbstractEvent<MastershipEvent.Type, DeviceId> {
11 12
12 - //do we worry about explicitly setting slaves/equals? probably not, 13 + //Contains master and standby information.
13 - //to keep it simple 14 + RoleInfo roleInfo;
14 - NodeId node;
15 15
16 /** 16 /**
17 * Type of mastership events. 17 * Type of mastership events.
...@@ -29,16 +29,16 @@ public class MastershipEvent extends AbstractEvent<MastershipEvent.Type, DeviceI ...@@ -29,16 +29,16 @@ public class MastershipEvent extends AbstractEvent<MastershipEvent.Type, DeviceI
29 } 29 }
30 30
31 /** 31 /**
32 - * Creates an event of a given type and for the specified device, master, 32 + * Creates an event of a given type and for the specified device,
33 - * and the current time. 33 + * role information, and the current time.
34 * 34 *
35 * @param type device event type 35 * @param type device event type
36 * @param device event device subject 36 * @param device event device subject
37 - * @param node master ID subject 37 + * @param info mastership role information subject
38 */ 38 */
39 - public MastershipEvent(Type type, DeviceId device, NodeId node) { 39 + public MastershipEvent(Type type, DeviceId device, RoleInfo info) {
40 super(type, device); 40 super(type, device);
41 - this.node = node; 41 + this.roleInfo = info;
42 } 42 }
43 43
44 /** 44 /**
...@@ -50,9 +50,9 @@ public class MastershipEvent extends AbstractEvent<MastershipEvent.Type, DeviceI ...@@ -50,9 +50,9 @@ public class MastershipEvent extends AbstractEvent<MastershipEvent.Type, DeviceI
50 * @param master master ID subject 50 * @param master master ID subject
51 * @param time occurrence time 51 * @param time occurrence time
52 */ 52 */
53 - public MastershipEvent(Type type, DeviceId device, NodeId master, long time) { 53 + public MastershipEvent(Type type, DeviceId device, RoleInfo info, long time) {
54 super(type, device, time); 54 super(type, device, time);
55 - this.node = master; 55 + this.roleInfo = info;
56 } 56 }
57 57
58 /** 58 /**
...@@ -63,7 +63,17 @@ public class MastershipEvent extends AbstractEvent<MastershipEvent.Type, DeviceI ...@@ -63,7 +63,17 @@ public class MastershipEvent extends AbstractEvent<MastershipEvent.Type, DeviceI
63 * 63 *
64 * @return node ID as a subject 64 * @return node ID as a subject
65 */ 65 */
66 + //XXX to-be removed - or keep for convenience?
66 public NodeId node() { 67 public NodeId node() {
67 - return node; 68 + return roleInfo.master();
69 + }
70 +
71 + /**
72 + * Returns the current role state for the subject.
73 + *
74 + * @return RoleInfo associated with Device ID subject
75 + */
76 + public RoleInfo roleInfo() {
77 + return roleInfo;
68 } 78 }
69 } 79 }
......
...@@ -5,6 +5,7 @@ import static org.junit.Assert.*; ...@@ -5,6 +5,7 @@ import static org.junit.Assert.*;
5 5
6 import java.util.Collections; 6 import java.util.Collections;
7 import java.util.Map; 7 import java.util.Map;
8 +import java.util.LinkedList;
8 import java.util.concurrent.CountDownLatch; 9 import java.util.concurrent.CountDownLatch;
9 import java.util.concurrent.TimeUnit; 10 import java.util.concurrent.TimeUnit;
10 11
...@@ -12,6 +13,7 @@ import org.junit.After; ...@@ -12,6 +13,7 @@ import org.junit.After;
12 import org.junit.Before; 13 import org.junit.Before;
13 import org.junit.Test; 14 import org.junit.Test;
14 import org.onlab.onos.cluster.NodeId; 15 import org.onlab.onos.cluster.NodeId;
16 +import org.onlab.onos.cluster.RoleInfo;
15 import org.onlab.onos.event.AbstractListenerRegistry; 17 import org.onlab.onos.event.AbstractListenerRegistry;
16 import org.onlab.onos.event.DefaultEventSinkRegistry; 18 import org.onlab.onos.event.DefaultEventSinkRegistry;
17 import org.onlab.onos.event.Event; 19 import org.onlab.onos.event.Event;
...@@ -87,7 +89,8 @@ public class ReplicaInfoManagerTest { ...@@ -87,7 +89,8 @@ public class ReplicaInfoManagerTest {
87 service.addListener(new MasterNodeCheck(latch, DID1, NID1)); 89 service.addListener(new MasterNodeCheck(latch, DID1, NID1));
88 90
89 // fake MastershipEvent 91 // fake MastershipEvent
90 - eventDispatcher.post(new MastershipEvent(Type.MASTER_CHANGED, DID1, NID1)); 92 + eventDispatcher.post(new MastershipEvent(Type.MASTER_CHANGED, DID1,
93 + new RoleInfo(NID1, new LinkedList<NodeId>())));
91 94
92 assertTrue(latch.await(1, TimeUnit.SECONDS)); 95 assertTrue(latch.await(1, TimeUnit.SECONDS));
93 } 96 }
......
...@@ -136,13 +136,13 @@ implements MastershipStore { ...@@ -136,13 +136,13 @@ implements MastershipStore {
136 rv.reassign(nodeId, STANDBY, NONE); 136 rv.reassign(nodeId, STANDBY, NONE);
137 roleMap.put(deviceId, rv); 137 roleMap.put(deviceId, rv);
138 updateTerm(deviceId); 138 updateTerm(deviceId);
139 - return new MastershipEvent(MASTER_CHANGED, deviceId, nodeId); 139 + return new MastershipEvent(MASTER_CHANGED, deviceId, rv.roleInfo());
140 case NONE: 140 case NONE:
141 rv.add(MASTER, nodeId); 141 rv.add(MASTER, nodeId);
142 rv.reassign(nodeId, STANDBY, NONE); 142 rv.reassign(nodeId, STANDBY, NONE);
143 roleMap.put(deviceId, rv); 143 roleMap.put(deviceId, rv);
144 updateTerm(deviceId); 144 updateTerm(deviceId);
145 - return new MastershipEvent(MASTER_CHANGED, deviceId, nodeId); 145 + return new MastershipEvent(MASTER_CHANGED, deviceId, rv.roleInfo());
146 default: 146 default:
147 log.warn("unknown Mastership Role {}", role); 147 log.warn("unknown Mastership Role {}", role);
148 return null; 148 return null;
...@@ -306,7 +306,7 @@ implements MastershipStore { ...@@ -306,7 +306,7 @@ implements MastershipStore {
306 roleMap.put(deviceId, rv); 306 roleMap.put(deviceId, rv);
307 Integer term = terms.get(deviceId); 307 Integer term = terms.get(deviceId);
308 terms.put(deviceId, ++term); 308 terms.put(deviceId, ++term);
309 - return new MastershipEvent(MASTER_CHANGED, deviceId, backup); 309 + return new MastershipEvent(MASTER_CHANGED, deviceId, rv.roleInfo());
310 } 310 }
311 } 311 }
312 312
...@@ -373,7 +373,7 @@ implements MastershipStore { ...@@ -373,7 +373,7 @@ implements MastershipStore {
373 return; 373 return;
374 } 374 }
375 notifyDelegate(new MastershipEvent( 375 notifyDelegate(new MastershipEvent(
376 - MASTER_CHANGED, event.getKey(), event.getValue().get(MASTER))); 376 + MASTER_CHANGED, event.getKey(), event.getValue().roleInfo()));
377 } 377 }
378 378
379 @Override 379 @Override
......
...@@ -10,6 +10,9 @@ import org.onlab.onos.cluster.NodeId; ...@@ -10,6 +10,9 @@ import org.onlab.onos.cluster.NodeId;
10 import org.onlab.onos.cluster.RoleInfo; 10 import org.onlab.onos.cluster.RoleInfo;
11 import org.onlab.onos.net.MastershipRole; 11 import org.onlab.onos.net.MastershipRole;
12 12
13 +import com.google.common.base.MoreObjects;
14 +import com.google.common.base.MoreObjects.ToStringHelper;
15 +
13 /** 16 /**
14 * A structure that holds node mastership roles associated with a 17 * A structure that holds node mastership roles associated with a
15 * {@link DeviceId}. This structure needs to be locked through IMap. 18 * {@link DeviceId}. This structure needs to be locked through IMap.
...@@ -109,14 +112,10 @@ public class RoleValue { ...@@ -109,14 +112,10 @@ public class RoleValue {
109 112
110 @Override 113 @Override
111 public String toString() { 114 public String toString() {
112 - final StringBuilder builder = new StringBuilder(); 115 + ToStringHelper helper = MoreObjects.toStringHelper(this.getClass());
113 for (Map.Entry<MastershipRole, List<NodeId>> el : value.entrySet()) { 116 for (Map.Entry<MastershipRole, List<NodeId>> el : value.entrySet()) {
114 - builder.append(el.getKey().toString()).append(": ["); 117 + helper.add(el.getKey().toString(), el.getValue());
115 - for (NodeId n : el.getValue()) {
116 - builder.append(n);
117 - }
118 - builder.append("]\n");
119 } 118 }
120 - return builder.toString(); 119 + return helper.toString();
121 } 120 }
122 } 121 }
......
...@@ -29,6 +29,8 @@ import org.onlab.onos.store.AbstractStore; ...@@ -29,6 +29,8 @@ import org.onlab.onos.store.AbstractStore;
29 import org.onlab.packet.IpPrefix; 29 import org.onlab.packet.IpPrefix;
30 import org.slf4j.Logger; 30 import org.slf4j.Logger;
31 31
32 +import com.google.common.collect.Lists;
33 +
32 import static org.onlab.onos.mastership.MastershipEvent.Type.*; 34 import static org.onlab.onos.mastership.MastershipEvent.Type.*;
33 35
34 /** 36 /**
...@@ -89,7 +91,8 @@ public class SimpleMastershipStore ...@@ -89,7 +91,8 @@ public class SimpleMastershipStore
89 } 91 }
90 } 92 }
91 93
92 - return new MastershipEvent(MASTER_CHANGED, deviceId, nodeId); 94 + return new MastershipEvent(MASTER_CHANGED, deviceId,
95 + new RoleInfo(nodeId, Lists.newLinkedList(backups)));
93 } 96 }
94 97
95 @Override 98 @Override
...@@ -196,7 +199,8 @@ public class SimpleMastershipStore ...@@ -196,7 +199,8 @@ public class SimpleMastershipStore
196 } else { 199 } else {
197 masterMap.put(deviceId, backup); 200 masterMap.put(deviceId, backup);
198 termMap.get(deviceId).incrementAndGet(); 201 termMap.get(deviceId).incrementAndGet();
199 - return new MastershipEvent(MASTER_CHANGED, deviceId, backup); 202 + return new MastershipEvent(MASTER_CHANGED, deviceId,
203 + new RoleInfo(backup, Lists.newLinkedList(backups)));
200 } 204 }
201 case STANDBY: 205 case STANDBY:
202 case NONE: 206 case NONE:
......