boolean hasLeadership() { if (!_leaderService.isRunning() || !_leaderService.hasLeadership()) { return false; } // It's possible we technically have leadership but are in the process of giving it up because we already // are leading the maximum number of partitions. Verify that this isn't a rejected service. Service delegateService = _leaderService.getCurrentDelegateService().orNull(); return delegateService == null || !(delegateService instanceof RelinquishService); }
public void start() throws Exception { _leaderServiceList = getLeaderServices(_timestampCache, _curator, _self, _cqlSessionForHintsPollerMap, _clusterHintsPoller, _dropwizardTask, _metricRegistry); for (LeaderService leaderService : _leaderServiceList) { leaderService.startAsync(); leaderService.awaitRunning(); } }
public void stop() throws Exception { for (LeaderService leaderService : _leaderServiceList) { leaderService.stopAsync(); leaderService.awaitTerminated(); } }
@Override public LeaderService get() { LeaderService service = new LeaderService( curator, "/leader/table-maintenance", self.toString(), "Leader-TableMaintenance-" + scope, 1, TimeUnit.MINUTES, maintenanceServiceFactory); ServiceFailureListener.listenTo(service, metricRegistry); dropwizardTask.register(scope.toLowerCase() + "-maintenance", service); return service; } };
PartitionLeader(int partition) { _partition = partition; String leaderPath = ZKPaths.makePath(_basePath, String.format(LEADER_PATH_PATTERN, partition)); _leaderService = new LeaderService(_curator, leaderPath, _instanceId, _serviceName, _reacquireDelay, _delayUnit, this::leadershipAcquired); _leaderService.addListener(new Service.Listener() { @Override public void running() { _startedFuture.set(null); } @Override public void terminated(Service.State from) { _terminatedFuture.set(null); } }, MoreExecutors.sameThreadExecutor()); }
Service actualService = leaderService.getCurrentDelegateService().orNull(); if (actualService == null || !actualService.isRunning()) { out.printf("Process is not currently elected leader: %s%n", name); leaderService.stopAndWait(); describeState(leaderService.state(), leaderService.hasLeadership()), getLeaderId(leaderService));
private Optional<LeaderService> startService(final String name) { if (!isOwner(name)) { return Optional.absent(); } _log.info("Starting owned service {}: {}", _group, name); String zkLeaderPath = String.format("/leader/%s/%s", _group.toLowerCase(), name); String threadName = String.format("Leader-%s-%s", _group, name); String taskName = String.format("%s-%s", _group.toLowerCase(), name); LeaderService leaderService = new LeaderService(_curator, zkLeaderPath, _selfId, threadName, 1, TimeUnit.MINUTES, new Supplier<Service>() { @Override public Service get() { return _factory.create(name); } }); ServiceFailureListener.listenTo(leaderService, _metricRegistry); _dropwizardTask.register(taskName, leaderService); leaderService.start(); return Optional.of(leaderService); }
@Override public Map<String, T> getServices() { Map<String, T> snapshotMap = Maps.newLinkedHashMap(); for (Map.Entry<String, Optional<LeaderService>> entry : _leaderMap.asMap().entrySet()) { String name = entry.getKey(); Optional<LeaderService> ref = entry.getValue(); if (!ref.isPresent()) { continue; } Optional<Service> service = ref.get().getCurrentDelegateService(); if (!service.isPresent()) { continue; } //noinspection unchecked snapshotMap.put(name, (T) service.get()); } return snapshotMap; }
Future<Void> startAsync() { _leaderService.startAsync(); return _startedFuture; }
Future<Void> stopAsync() { _leaderService.stopAsync(); return _terminatedFuture; }
public void register(final String name, final LeaderService leaderService) { _selectorMap.put(name, leaderService); // Unregister automatically to avoid memory leaks. leaderService.addListener(new AbstractServiceListener() { @Override public void terminated(Service.State from) { unregister(name, leaderService); } @Override public void failed(Service.State from, Throwable failure) { unregister(name, leaderService); } }, MoreExecutors.sameThreadExecutor()); }
@Override public LeaderService get() { LeaderService service = new LeaderService( curator, "/leader/table-maintenance", self.toString(), "Leader-TableMaintenance-" + scope, 1, TimeUnit.MINUTES, maintenanceServiceFactory); ServiceFailureListener.listenTo(service, metricRegistry); dropwizardTask.register(scope.toLowerCase() + "-maintenance", service); return service; } };
PartitionLeader(int partition) { _partition = partition; String leaderPath = ZKPaths.makePath(_basePath, String.format(LEADER_PATH_PATTERN, partition)); _leaderService = new LeaderService(_curator, leaderPath, _instanceId, _serviceName, _reacquireDelay, _delayUnit, this::leadershipAcquired); _leaderService.addListener(new Service.Listener() { @Override public void running() { _startedFuture.set(null); } @Override public void terminated(Service.State from) { _terminatedFuture.set(null); } }, MoreExecutors.sameThreadExecutor()); }
Service actualService = leaderService.getCurrentDelegateService().orNull(); if (actualService == null || !actualService.isRunning()) { out.printf("Process is not currently elected leader: %s%n", name); leaderService.stopAndWait(); describeState(leaderService.state(), leaderService.hasLeadership()), getLeaderId(leaderService));
private Optional<LeaderService> startService(final String name) { if (!isOwner(name)) { return Optional.absent(); } _log.info("Starting owned service {}: {}", _group, name); String zkLeaderPath = String.format("/leader/%s/%s", _group.toLowerCase(), name); String threadName = String.format("Leader-%s-%s", _group, name); String taskName = String.format("%s-%s", _group.toLowerCase(), name); LeaderService leaderService = new LeaderService(_curator, zkLeaderPath, _selfId, threadName, 1, TimeUnit.MINUTES, new Supplier<Service>() { @Override public Service get() { return _factory.create(name); } }); ServiceFailureListener.listenTo(leaderService, _metricRegistry); _dropwizardTask.register(taskName, leaderService); leaderService.start(); return Optional.of(leaderService); }
@Override public Map<String, T> getServices() { Map<String, T> snapshotMap = Maps.newLinkedHashMap(); for (Map.Entry<String, Optional<LeaderService>> entry : _leaderMap.asMap().entrySet()) { String name = entry.getKey(); Optional<LeaderService> ref = entry.getValue(); if (!ref.isPresent()) { continue; } Optional<Service> service = ref.get().getCurrentDelegateService(); if (!service.isPresent()) { continue; } //noinspection unchecked snapshotMap.put(name, (T) service.get()); } return snapshotMap; }
Future<Void> startAsync() { _leaderService.startAsync(); return _startedFuture; }
Future<Void> stopAsync() { _leaderService.stopAsync(); return _terminatedFuture; }
public void register(final String name, final LeaderService leaderService) { _selectorMap.put(name, leaderService); // Unregister automatically to avoid memory leaks. leaderService.addListener(new AbstractServiceListener() { @Override public void terminated(Service.State from) { unregister(name, leaderService); } @Override public void failed(Service.State from, Throwable failure) { unregister(name, leaderService); } }, MoreExecutors.sameThreadExecutor()); }
String zkLeaderPath = "/leader/hints/" + clusterName; String threadName = "Leader-HintsPoller-" + clusterName; LeaderService leaderService = new LeaderService( curator, zkLeaderPath, serverId, threadName, 1, TimeUnit.MINUTES, new Supplier<Service>() {