/** * 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(); }
@Override public void cancel() { election.stop(); registry.remove(name, election); } }
@Override protected void shutDown() throws Exception { leaderElection.stopAndWait(); } }
LeaderElection leaderElection = new LeaderElection(zkClient, prefix, new ElectionHandler() { @Override public void leader() { leaderElection.start(); leaderElections.add(leaderElection); leaderElection.stopAndWait(); Tasks.waitFor(--expectedSize, new Callable<Integer>() { @Override
leaderElection = new LeaderElection(zkClient, Constants.Service.MESSAGING_SERVICE, new ElectionHandler() { @Override public void leader() { leaderElection.startAndWait(); latch.await();
@Inject public DistributedStreamFileJanitorService(ZKClient zkClient, final StreamFileJanitor janitor, final CConfiguration cConf) { String electionPrefix = "/" + Constants.Service.STREAMS + "/leader"; leaderElection = new LeaderElection(zkClient, electionPrefix, new ElectionHandler() { Service janitorService; @Override public void leader() { LOG.info("Leader of stream file janitor service"); janitorService = new LocalStreamFileJanitorService(janitor, cConf); janitorService.start(); } @Override public void follower() { LOG.info("Follower of stream file janitor service"); if (janitorService != null) { janitorService.stop(); janitorService = null; } } }); }
this.electionInfoService = new LeaderElectionInfoService(zkClient, electionPath); this.electionHandler = new MasterLeaderElectionHandler(cConf, hConf, zkClient, electionInfoService); this.leaderElection = new LeaderElection(zkClient, electionPath, electionHandler); this.leaderElection.addListener(new ServiceListenerAdapter() { @Override public void terminated(Service.State from) {
@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 { leaderElection.startAndWait(); }
@Override public void onChange(Collection<PartitionReplica> partitionReplicas) { Set<Integer> partitions = Sets.newHashSet(); for (PartitionReplica replica : partitionReplicas) { partitions.add(Integer.valueOf(replica.getName())); } LOG.info("Partitions changed {}, service: {}", partitions, serviceName); try { if (service != null) { service.stopAndWait(); } if (partitions.isEmpty() || !election.isRunning()) { service = null; } else { service = createService(partitions); service.startAndWait(); } } catch (Throwable t) { LOG.error("Failed to change partitions, service: {}.", serviceName, t); completion.setException(t); stop(); } }
leaderElection = new LeaderElection(zkClient, Constants.Service.MESSAGING_SERVICE, new ElectionHandler() { @Override public void leader() { leaderElection.startAndWait(); latch.await();
new LeaderElection(zk, serviceName, new ElectionHandler() { private ResourceCoordinator coordinator;
@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 public void start() throws Exception { logAppenderInitializer.initialize(); resetShutdownTime(); createDirectory("twill"); createSystemHBaseNamespace(); updateConfigurationTable(); Services.startAndWait(zkClient, cConf.getLong(Constants.Zookeeper.CLIENT_STARTUP_TIMEOUT_MILLIS), TimeUnit.MILLISECONDS, String.format("Connection timed out while trying to start ZooKeeper client. Please " + "verify that the ZooKeeper quorum settings are correct in cdap-site.xml. " + "Currently configured as: %s", cConf.get(Constants.Zookeeper.QUORUM))); // Tries to create the ZK root node (which can be namespaced through the zk connection string) Futures.getUnchecked(ZKOperations.ignoreError(zkClient.create("/", null, CreateMode.PERSISTENT), KeeperException.NodeExistsException.class, null)); electionInfoService.startAndWait(); leaderElection.startAndWait(); }
@Override public void onChange(Collection<PartitionReplica> partitionReplicas) { Set<Integer> partitions = Sets.newHashSet(); for (PartitionReplica replica : partitionReplicas) { partitions.add(Integer.valueOf(replica.getName())); } LOG.info("Partitions changed {}, service: {}", partitions, serviceName); try { if (service != null) { service.stopAndWait(); } if (partitions.isEmpty() || !election.isRunning()) { service = null; } else { service = createService(partitions); service.startAndWait(); } } catch (Throwable t) { LOG.error("Failed to change partitions, service: {}.", serviceName, t); completion.setException(t); stop(); } }
/** * 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); }
/** * Stops all active {@link LeaderElection} processes. */ public void shutdown() { for (LeaderElection election : registry.values()) { election.stop(); } }
new LeaderElection(zk, serviceName, new ElectionHandler() { private ResourceCoordinator coordinator;
@Override public void shutDown() { leaderElection.stopAndWait(); }
if (leaderElection.isRunning()) { stopQuietly(leaderElection);