Madan Jampani
Committed by Gerrit Code Review

Added couple of methods to LeadershipService.

Change-Id: I259b1a282a51af9425e941a720336f89d66f1097
......@@ -16,6 +16,7 @@
package org.onosproject.cluster;
import java.util.Map;
import java.util.Set;
/**
* Service for leader election.
......@@ -27,13 +28,27 @@ import java.util.Map;
public interface LeadershipService {
/**
* Gets the most recent leader for the topic.
* Returns the current leader for the topic.
* @param path topic
* @return nodeId of the leader, null if so such topic exists.
*/
NodeId getLeader(String path);
/**
* Returns the current leadership info for the topic.
* @param path topic
* @return leadership info or null if so such topic exists.
*/
Leadership getLeadership(String path);
/**
* Returns the set of topics owned by the specified node.
* @param nodeId node Id.
* @return set of topics for which this node is the current leader.
*/
Set<String> ownedTopics(NodeId nodeId);
/**
* Joins the leadership contest.
* @param path topic for which this controller node wishes to be a leader.
*/
......@@ -45,6 +60,10 @@ public interface LeadershipService {
*/
void withdraw(String path);
/**
* Returns the current leader board.
* @return mapping from topic to leadership info.
*/
Map<String, Leadership> getLeaderBoard();
/**
......
......@@ -46,10 +46,12 @@ import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.locks.Lock;
import java.util.stream.Collectors;
import static com.google.common.base.Preconditions.checkArgument;
import static org.onlab.util.Tools.groupedThreads;
......@@ -162,6 +164,28 @@ public class HazelcastLeadershipService implements LeadershipService {
}
@Override
public Leadership getLeadership(String path) {
checkArgument(path != null);
Topic topic = topics.get(path);
if (topic != null) {
return new Leadership(topic.topicName(),
topic.leader(),
topic.term());
}
return null;
}
@Override
public Set<String> ownedTopics(NodeId nodeId) {
checkArgument(nodeId != null);
return topics.values()
.stream()
.filter(topic -> nodeId.equals(topic.leader()))
.map(topic -> topic.topicName)
.collect(Collectors.toSet());
}
@Override
public void runForLeadership(String path) {
checkArgument(path != null);
Topic topic = new Topic(path);
......
......@@ -2,6 +2,7 @@ package org.onosproject.store.consistent.impl;
import static org.onlab.util.Tools.groupedThreads;
import static org.slf4j.LoggerFactory.getLogger;
import static com.google.common.base.Preconditions.checkArgument;
import java.util.Map;
import java.util.Map.Entry;
......@@ -10,6 +11,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
......@@ -171,6 +173,22 @@ public class DistributedLeadershipManager implements LeadershipService {
}
@Override
public Leadership getLeadership(String path) {
checkArgument(path != null);
return leaderBoard.get(path);
}
@Override
public Set<String> ownedTopics(NodeId nodeId) {
checkArgument(nodeId != null);
return leaderBoard.entrySet()
.stream()
.filter(entry -> nodeId.equals(entry.getValue().leader()))
.map(Entry::getKey)
.collect(Collectors.toSet());
}
@Override
public void runForLeadership(String path) {
log.info("Running for leadership for topic: {}", path);
activeTopics.add(path);
......
......@@ -15,10 +15,14 @@
*/
package org.onosproject.store.trivial.impl;
import static com.google.common.base.Preconditions.checkArgument;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.stream.Collectors;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
......@@ -55,6 +59,22 @@ public class SimpleLeadershipManager implements LeadershipService {
}
@Override
public Leadership getLeadership(String path) {
checkArgument(path != null);
return elections.get(path) ? new Leadership(path, clusterService.getLocalNode().id(), 0) : null;
}
@Override
public Set<String> ownedTopics(NodeId nodeId) {
checkArgument(nodeId != null);
return elections.entrySet()
.stream()
.filter(Entry::getValue)
.map(Entry::getKey)
.collect(Collectors.toSet());
}
@Override
public void runForLeadership(String path) {
elections.put(path, true);
for (LeadershipEventListener listener : listeners) {
......@@ -88,5 +108,4 @@ public class SimpleLeadershipManager implements LeadershipService {
public void removeListener(LeadershipEventListener listener) {
listeners.remove(listener);
}
}
......