Madan Jampani
Committed by Gerrit Code Review

Added couple of methods to LeadershipService.

Change-Id: I259b1a282a51af9425e941a720336f89d66f1097
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
16 package org.onosproject.cluster; 16 package org.onosproject.cluster;
17 17
18 import java.util.Map; 18 import java.util.Map;
19 +import java.util.Set;
19 20
20 /** 21 /**
21 * Service for leader election. 22 * Service for leader election.
...@@ -27,13 +28,27 @@ import java.util.Map; ...@@ -27,13 +28,27 @@ import java.util.Map;
27 public interface LeadershipService { 28 public interface LeadershipService {
28 29
29 /** 30 /**
30 - * Gets the most recent leader for the topic. 31 + * Returns the current leader for the topic.
31 * @param path topic 32 * @param path topic
32 * @return nodeId of the leader, null if so such topic exists. 33 * @return nodeId of the leader, null if so such topic exists.
33 */ 34 */
34 NodeId getLeader(String path); 35 NodeId getLeader(String path);
35 36
36 /** 37 /**
38 + * Returns the current leadership info for the topic.
39 + * @param path topic
40 + * @return leadership info or null if so such topic exists.
41 + */
42 + Leadership getLeadership(String path);
43 +
44 + /**
45 + * Returns the set of topics owned by the specified node.
46 + * @param nodeId node Id.
47 + * @return set of topics for which this node is the current leader.
48 + */
49 + Set<String> ownedTopics(NodeId nodeId);
50 +
51 + /**
37 * Joins the leadership contest. 52 * Joins the leadership contest.
38 * @param path topic for which this controller node wishes to be a leader. 53 * @param path topic for which this controller node wishes to be a leader.
39 */ 54 */
...@@ -45,6 +60,10 @@ public interface LeadershipService { ...@@ -45,6 +60,10 @@ public interface LeadershipService {
45 */ 60 */
46 void withdraw(String path); 61 void withdraw(String path);
47 62
63 + /**
64 + * Returns the current leader board.
65 + * @return mapping from topic to leadership info.
66 + */
48 Map<String, Leadership> getLeaderBoard(); 67 Map<String, Leadership> getLeaderBoard();
49 68
50 /** 69 /**
......
...@@ -46,10 +46,12 @@ import org.slf4j.LoggerFactory; ...@@ -46,10 +46,12 @@ import org.slf4j.LoggerFactory;
46 46
47 import java.util.HashMap; 47 import java.util.HashMap;
48 import java.util.Map; 48 import java.util.Map;
49 +import java.util.Set;
49 import java.util.concurrent.ExecutorService; 50 import java.util.concurrent.ExecutorService;
50 import java.util.concurrent.Executors; 51 import java.util.concurrent.Executors;
51 import java.util.concurrent.Future; 52 import java.util.concurrent.Future;
52 import java.util.concurrent.locks.Lock; 53 import java.util.concurrent.locks.Lock;
54 +import java.util.stream.Collectors;
53 55
54 import static com.google.common.base.Preconditions.checkArgument; 56 import static com.google.common.base.Preconditions.checkArgument;
55 import static org.onlab.util.Tools.groupedThreads; 57 import static org.onlab.util.Tools.groupedThreads;
...@@ -162,6 +164,28 @@ public class HazelcastLeadershipService implements LeadershipService { ...@@ -162,6 +164,28 @@ public class HazelcastLeadershipService implements LeadershipService {
162 } 164 }
163 165
164 @Override 166 @Override
167 + public Leadership getLeadership(String path) {
168 + checkArgument(path != null);
169 + Topic topic = topics.get(path);
170 + if (topic != null) {
171 + return new Leadership(topic.topicName(),
172 + topic.leader(),
173 + topic.term());
174 + }
175 + return null;
176 + }
177 +
178 + @Override
179 + public Set<String> ownedTopics(NodeId nodeId) {
180 + checkArgument(nodeId != null);
181 + return topics.values()
182 + .stream()
183 + .filter(topic -> nodeId.equals(topic.leader()))
184 + .map(topic -> topic.topicName)
185 + .collect(Collectors.toSet());
186 + }
187 +
188 + @Override
165 public void runForLeadership(String path) { 189 public void runForLeadership(String path) {
166 checkArgument(path != null); 190 checkArgument(path != null);
167 Topic topic = new Topic(path); 191 Topic topic = new Topic(path);
......
...@@ -2,6 +2,7 @@ package org.onosproject.store.consistent.impl; ...@@ -2,6 +2,7 @@ package org.onosproject.store.consistent.impl;
2 2
3 import static org.onlab.util.Tools.groupedThreads; 3 import static org.onlab.util.Tools.groupedThreads;
4 import static org.slf4j.LoggerFactory.getLogger; 4 import static org.slf4j.LoggerFactory.getLogger;
5 +import static com.google.common.base.Preconditions.checkArgument;
5 6
6 import java.util.Map; 7 import java.util.Map;
7 import java.util.Map.Entry; 8 import java.util.Map.Entry;
...@@ -10,6 +11,7 @@ import java.util.concurrent.ExecutorService; ...@@ -10,6 +11,7 @@ import java.util.concurrent.ExecutorService;
10 import java.util.concurrent.Executors; 11 import java.util.concurrent.Executors;
11 import java.util.concurrent.ScheduledExecutorService; 12 import java.util.concurrent.ScheduledExecutorService;
12 import java.util.concurrent.TimeUnit; 13 import java.util.concurrent.TimeUnit;
14 +import java.util.stream.Collectors;
13 15
14 import org.apache.felix.scr.annotations.Activate; 16 import org.apache.felix.scr.annotations.Activate;
15 import org.apache.felix.scr.annotations.Component; 17 import org.apache.felix.scr.annotations.Component;
...@@ -171,6 +173,22 @@ public class DistributedLeadershipManager implements LeadershipService { ...@@ -171,6 +173,22 @@ public class DistributedLeadershipManager implements LeadershipService {
171 } 173 }
172 174
173 @Override 175 @Override
176 + public Leadership getLeadership(String path) {
177 + checkArgument(path != null);
178 + return leaderBoard.get(path);
179 + }
180 +
181 + @Override
182 + public Set<String> ownedTopics(NodeId nodeId) {
183 + checkArgument(nodeId != null);
184 + return leaderBoard.entrySet()
185 + .stream()
186 + .filter(entry -> nodeId.equals(entry.getValue().leader()))
187 + .map(Entry::getKey)
188 + .collect(Collectors.toSet());
189 + }
190 +
191 + @Override
174 public void runForLeadership(String path) { 192 public void runForLeadership(String path) {
175 log.info("Running for leadership for topic: {}", path); 193 log.info("Running for leadership for topic: {}", path);
176 activeTopics.add(path); 194 activeTopics.add(path);
......
...@@ -15,10 +15,14 @@ ...@@ -15,10 +15,14 @@
15 */ 15 */
16 package org.onosproject.store.trivial.impl; 16 package org.onosproject.store.trivial.impl;
17 17
18 +import static com.google.common.base.Preconditions.checkArgument;
19 +
18 import java.util.Map; 20 import java.util.Map;
21 +import java.util.Map.Entry;
19 import java.util.Set; 22 import java.util.Set;
20 import java.util.concurrent.ConcurrentHashMap; 23 import java.util.concurrent.ConcurrentHashMap;
21 import java.util.concurrent.CopyOnWriteArraySet; 24 import java.util.concurrent.CopyOnWriteArraySet;
25 +import java.util.stream.Collectors;
22 26
23 import org.apache.felix.scr.annotations.Component; 27 import org.apache.felix.scr.annotations.Component;
24 import org.apache.felix.scr.annotations.Reference; 28 import org.apache.felix.scr.annotations.Reference;
...@@ -55,6 +59,22 @@ public class SimpleLeadershipManager implements LeadershipService { ...@@ -55,6 +59,22 @@ public class SimpleLeadershipManager implements LeadershipService {
55 } 59 }
56 60
57 @Override 61 @Override
62 + public Leadership getLeadership(String path) {
63 + checkArgument(path != null);
64 + return elections.get(path) ? new Leadership(path, clusterService.getLocalNode().id(), 0) : null;
65 + }
66 +
67 + @Override
68 + public Set<String> ownedTopics(NodeId nodeId) {
69 + checkArgument(nodeId != null);
70 + return elections.entrySet()
71 + .stream()
72 + .filter(Entry::getValue)
73 + .map(Entry::getKey)
74 + .collect(Collectors.toSet());
75 + }
76 +
77 + @Override
58 public void runForLeadership(String path) { 78 public void runForLeadership(String path) {
59 elections.put(path, true); 79 elections.put(path, true);
60 for (LeadershipEventListener listener : listeners) { 80 for (LeadershipEventListener listener : listeners) {
...@@ -88,5 +108,4 @@ public class SimpleLeadershipManager implements LeadershipService { ...@@ -88,5 +108,4 @@ public class SimpleLeadershipManager implements LeadershipService {
88 public void removeListener(LeadershipEventListener listener) { 108 public void removeListener(LeadershipEventListener listener) {
89 listeners.remove(listener); 109 listeners.remove(listener);
90 } 110 }
91 -
92 } 111 }
......