Madan Jampani
Committed by Gerrit Code Review

Using AsyncLeaderElector primitive for Leadership

Change-Id: I0ff91ddaa3b5dec79e073496044c2f860ae94616
......@@ -18,9 +18,6 @@ package org.onosproject.cluster.impl;
import static org.slf4j.LoggerFactory.getLogger;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
......@@ -28,9 +25,7 @@ import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.Service;
import org.onlab.util.Tools;
import org.onosproject.cluster.ClusterService;
import org.onosproject.cluster.ControllerNode;
import org.onosproject.cluster.Leadership;
import org.onosproject.cluster.LeadershipAdminService;
import org.onosproject.cluster.LeadershipEvent;
......@@ -65,25 +60,16 @@ public class LeadershipManager
private NodeId localNodeId;
private final ScheduledExecutorService deadlockDetector =
Executors.newSingleThreadScheduledExecutor(Tools.groupedThreads("onos/leadership", ""));
@Activate
public void activate() {
localNodeId = clusterService.getLocalNode().id();
store.setDelegate(delegate);
eventDispatcher.addSink(LeadershipEvent.class, listenerRegistry);
deadlockDetector.scheduleWithFixedDelay(() -> clusterService.getNodes()
.stream()
.map(ControllerNode::id)
.filter(id -> !clusterService.getState(id).isActive())
.forEach(this::unregister), 0, 2, TimeUnit.SECONDS);
log.info("Started");
}
@Deactivate
public void deactivate() {
deadlockDetector.shutdown();
Maps.filterValues(store.getLeaderships(), v -> v.candidates().contains(localNodeId))
.keySet()
.forEach(this::withdraw);
......
......@@ -56,7 +56,7 @@ import com.google.common.collect.Sets;
* Implementation of {@code LeadershipStore} backed by {@link ConsistentMap}.
*/
@Service
@Component(immediate = true, enabled = true)
@Component(immediate = true, enabled = false)
public class DistributedLeadershipStore
extends AbstractStore<LeadershipEvent, LeadershipStoreDelegate>
implements LeadershipStore {
......
......@@ -47,7 +47,7 @@ import com.google.common.collect.Maps;
* primitive.
*/
@Service
@Component(immediate = true, enabled = false)
@Component(immediate = true, enabled = true)
public class NewDistributedLeadershipStore
extends AbstractStore<LeadershipEvent, LeadershipStoreDelegate>
implements LeadershipStore {
......