/** * Creates a new {@link LeaderElection} for the given arguments, starts the service, and adds it to the registry. * @param name Name for the election. * @param handler Callback to handle leader and follower transitions. * @return An object to cancel the election participation. */ public Cancellable register(String name, ElectionHandler handler) { LeaderElection election = new LeaderElection(zkClient, name, handler); election.start(); registry.put(name, election); return new CancellableElection(name, election); }
/** * Creates a new {@link LeaderElection} for the given arguments, starts the service, and adds it to the registry. * @param name Name for the election. * @param handler Callback to handle leader and follower transitions. * @return An object to cancel the election participation. */ public Cancellable register(String name, ElectionHandler handler) { LeaderElection election = new LeaderElection(zkClient, name, handler); election.start(); registry.put(name, election); return new CancellableElection(name, election); }
/** * 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(); }
leaderElection.start();
leaderElection.start();
leaderElection.start();
@Override protected void doInit() throws IOException { this.keyCache.addListener(this); try { keyCache.init(); } catch (InterruptedException ie) { throw Throwables.propagate(ie); } this.leaderElection = new LeaderElection(zookeeper, "/leader", new ElectionHandler() { @Override public void leader() { leader.set(true); LOG.debug("Transitioned to leader"); if (currentKey == null) { rotateKey(); } } @Override public void follower() { leader.set(false); LOG.debug("Transitioned to follower"); } }); this.leaderElection.start(); startExpirationThread(); }
@Override protected void startUp() throws Exception { LOG.info("Starting ResourceBalancer {} service...", serviceName); // We first submit requirement before starting coordinator to make sure all needed paths in ZK are created ResourceRequirement requirement = ResourceRequirement.builder(serviceName).addPartitions("", partitionCount, 1).build(); resourceClient.submitRequirement(requirement).get(); Discoverable discoverable = createDiscoverable(serviceName); cancelDiscoverable = discoveryService.register(ResolvingDiscoverable.of(discoverable)); election.start(); resourceClient.startAndWait(); cancelResourceHandler = resourceClient.subscribe(serviceName, createResourceHandler(discoverable)); LOG.info("Started ResourceBalancer {} service...", serviceName); }
@Override protected void startUp() throws Exception { LOG.info("Starting ResourceBalancer {} service...", serviceName); // We first submit requirement before starting coordinator to make sure all needed paths in ZK are created ResourceRequirement requirement = ResourceRequirement.builder(serviceName).addPartitions("", partitionCount, 1).build(); resourceClient.submitRequirement(requirement).get(); Discoverable discoverable = createDiscoverable(serviceName); cancelDiscoverable = discoveryService.register(ResolvingDiscoverable.of(discoverable)); election.start(); resourceClient.startAndWait(); cancelResourceHandler = resourceClient.subscribe(serviceName, createResourceHandler(discoverable)); LOG.info("Started ResourceBalancer {} service...", serviceName); }
leaderElection.start(); leaderElections.add(leaderElection);