public void takeLeadership(CuratorFramework client) throws Exception {
logger.info("Leader Admin {} is watching for stream/job deployment requests.", getId());
cleanupDeployments(client);
PathChildrenCache containers = null;
PathChildrenCache streamDeployments = null;
PathChildrenCache jobDeployments = null;
PathChildrenCache moduleDeploymentRequests = null;
StreamDeploymentListener streamDeploymentListener;
JobDeploymentListener jobDeploymentListener;
ContainerListener containerListener;
try {
StreamFactory streamFactory = new StreamFactory(streamDefinitionRepository, moduleRegistry,
moduleOptionsMetadataResolver);
JobFactory jobFactory = new JobFactory(jobDefinitionRepository, moduleRegistry,
moduleOptionsMetadataResolver);
String requestedModulesPath = Paths.build(Paths.MODULE_DEPLOYMENTS, Paths.REQUESTED);
Paths.ensurePath(client, requestedModulesPath);
String allocatedModulesPath = Paths.build(Paths.MODULE_DEPLOYMENTS, Paths.ALLOCATED);
Paths.ensurePath(client, allocatedModulesPath);
moduleDeploymentRequests = instantiatePathChildrenCache(client, requestedModulesPath);
moduleDeploymentRequests.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
streamDeploymentListener = new StreamDeploymentListener(zkConnection,
moduleDeploymentRequests,
containerRepository,
streamFactory,
containerMatcher,
stateCalculator);
streamDeployments = instantiatePathChildrenCache(client, Paths.STREAM_DEPLOYMENTS);
streamDeployments.getListenable().addListener(streamDeploymentListener);
// using BUILD_INITIAL_CACHE so that all known streams are populated
// in the cache before invoking recalculateStreamStates; same for
// jobs below
streamDeployments.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
streamDeploymentListener.recalculateStreamStates(client, streamDeployments);
jobDeploymentListener = new JobDeploymentListener(zkConnection,
moduleDeploymentRequests,
containerRepository,
jobFactory,
containerMatcher,
stateCalculator);
jobDeployments = instantiatePathChildrenCache(client, Paths.JOB_DEPLOYMENTS);
jobDeployments.getListenable().addListener(jobDeploymentListener);
jobDeployments.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
jobDeploymentListener.recalculateJobStates(client, jobDeployments);
containerListener = new ContainerListener(zkConnection,
containerRepository,
streamFactory,
jobFactory,
streamDeployments,
jobDeployments,
moduleDeploymentRequests,
containerMatcher,
stateCalculator,
executorService,
quietPeriod);
containers = instantiatePathChildrenCache(client, Paths.CONTAINERS);
containers.getListenable().addListener(containerListener);
containers.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);
Thread.sleep(Long.MAX_VALUE);
}
catch (InterruptedException e) {
logger.info("Leadership canceled due to thread interrupt");
Thread.currentThread().interrupt();
}
finally {
if (containers != null) {
containers.close();
}
if (streamDeployments != null) {
streamDeployments.close();
}
if (jobDeployments != null) {
jobDeployments.close();
}
if (moduleDeploymentRequests != null) {
moduleDeploymentRequests.close();
}
}
}