use of co.cask.cdap.common.zookeeper.coordination.ResourceHandler in project cdap by caskdata.
the class ResourceBalancerService method createResourceHandler.
private ResourceHandler createResourceHandler(Discoverable discoverable) {
return new ResourceHandler(discoverable) {
private Service service;
@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();
}
}
@Override
public void finished(Throwable failureCause) {
try {
if (service != null) {
service.stopAndWait();
service = null;
}
completion.set(null);
} catch (Throwable t) {
LOG.error("Exception when stopping service {}", service, t);
Throwable cause = failureCause == null ? t : failureCause;
if (cause != t) {
cause.addSuppressed(t);
}
completion.setException(t);
// No need to call stop as this callback only happen during shutdown of this service
}
}
};
}
Aggregations