Search in sources :

Example 1 with ResourceHandler

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
            }
        }
    };
}
Also used : PartitionReplica(co.cask.cdap.common.zookeeper.coordination.PartitionReplica) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) DiscoveryService(org.apache.twill.discovery.DiscoveryService) Service(com.google.common.util.concurrent.Service) Collection(java.util.Collection) ResourceHandler(co.cask.cdap.common.zookeeper.coordination.ResourceHandler)

Aggregations

PartitionReplica (co.cask.cdap.common.zookeeper.coordination.PartitionReplica)1 ResourceHandler (co.cask.cdap.common.zookeeper.coordination.ResourceHandler)1 AbstractIdleService (com.google.common.util.concurrent.AbstractIdleService)1 Service (com.google.common.util.concurrent.Service)1 Collection (java.util.Collection)1 DiscoveryService (org.apache.twill.discovery.DiscoveryService)1