Search in sources :

Example 1 with ResourceCoordinator

use of co.cask.cdap.common.zookeeper.coordination.ResourceCoordinator in project cdap by caskdata.

the class DistributedStreamService method performLeaderElection.

/**
   * Elect one leader among the {@link DistributedStreamService}s running in different Twill runnables.
   */
private void performLeaderElection() {
    // Start the resource coordinator that will map Streams to Stream handlers
    leaderElection = new LeaderElection(// TODO: Should unify this leader election with DistributedStreamFileJanitorService
    zkClient, "/election/" + STREAMS_COORDINATOR, new ElectionHandler() {

        @Override
        public void leader() {
            LOG.info("Became Stream handler leader. Starting resource coordinator.");
            resourceCoordinator = new ResourceCoordinator(getCoordinatorZKClient(), discoveryServiceClient, new BalancedAssignmentStrategy());
            resourceCoordinator.startAndWait();
            updateRequirement();
        }

        @Override
        public void follower() {
            LOG.info("Became Stream handler follower.");
            if (resourceCoordinator != null) {
                resourceCoordinator.stopAndWait();
            }
        }
    });
    leaderElection.start();
}
Also used : LeaderElection(org.apache.twill.internal.zookeeper.LeaderElection) BalancedAssignmentStrategy(co.cask.cdap.common.zookeeper.coordination.BalancedAssignmentStrategy) ElectionHandler(org.apache.twill.api.ElectionHandler) ResourceCoordinator(co.cask.cdap.common.zookeeper.coordination.ResourceCoordinator)

Aggregations

BalancedAssignmentStrategy (co.cask.cdap.common.zookeeper.coordination.BalancedAssignmentStrategy)1 ResourceCoordinator (co.cask.cdap.common.zookeeper.coordination.ResourceCoordinator)1 ElectionHandler (org.apache.twill.api.ElectionHandler)1 LeaderElection (org.apache.twill.internal.zookeeper.LeaderElection)1