Search in sources :

Example 1 with ResourceModifier

use of co.cask.cdap.common.zookeeper.coordination.ResourceModifier in project cdap by caskdata.

the class DistributedStreamService method createRequirementModifier.

/**
 * Creates a {@link ResourceModifier} that updates stream resource requirement by consulting stream meta store.
 */
private ResourceModifier createRequirementModifier() {
    return new ResourceModifier() {

        @Nullable
        @Override
        public ResourceRequirement apply(@Nullable ResourceRequirement existingRequirement) {
            try {
                // Create one requirement for the resource coordinator for all the streams.
                // One stream is identified by one partition
                ResourceRequirement.Builder builder = ResourceRequirement.builder(Constants.Service.STREAMS);
                for (Map.Entry<NamespaceId, StreamSpecification> streamSpecEntry : streamMetaStore.listStreams().entries()) {
                    StreamId streamId = streamSpecEntry.getKey().stream(streamSpecEntry.getValue().getName());
                    LOG.debug("Adding {} stream as a resource to the coordinator to manager streams leaders.", streamId);
                    builder.addPartition(new ResourceRequirement.Partition(streamId.toString(), 1));
                }
                return builder.build();
            } catch (Throwable e) {
                LOG.warn("Could not create requirement for coordinator in Stream handler leader: " + e.getMessage());
                LOG.debug("Could not create requirement for coordinator in Stream handler leader", e);
                throw Throwables.propagate(e);
            }
        }
    };
}
Also used : ResourceModifier(co.cask.cdap.common.zookeeper.coordination.ResourceModifier) StreamId(co.cask.cdap.proto.id.StreamId) StreamSpecification(co.cask.cdap.api.data.stream.StreamSpecification) ResourceRequirement(co.cask.cdap.common.zookeeper.coordination.ResourceRequirement) NamespaceId(co.cask.cdap.proto.id.NamespaceId) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Nullable(javax.annotation.Nullable)

Example 2 with ResourceModifier

use of co.cask.cdap.common.zookeeper.coordination.ResourceModifier in project cdap by caskdata.

the class DistributedStreamService method updateRequirement.

/**
 * Updates stream resource requirement. It will retry if failed to do so.
 */
private void updateRequirement() {
    final ResourceModifier modifier = createRequirementModifier();
    Futures.addCallback(resourceCoordinatorClient.modifyRequirement(Constants.Service.STREAMS, modifier), new FutureCallback<ResourceRequirement>() {

        @Override
        public void onSuccess(ResourceRequirement result) {
            // No-op
            LOG.info("Stream resource requirement updated to {}", result);
        }

        @Override
        public void onFailure(Throwable t) {
            LOG.warn("Failed to update stream resource requirement: {}", t.getMessage());
            LOG.debug("Failed to update stream resource requirement.", t);
            if (isRunning()) {
                final FutureCallback<ResourceRequirement> callback = this;
                // Retry in 2 seconds. Shouldn't sleep in this callback thread. Should start a new thread for the retry.
                Thread retryThread = new Thread("stream-resource-update") {

                    @Override
                    public void run() {
                        try {
                            TimeUnit.SECONDS.sleep(2);
                            LOG.info("Retrying update stream resource requirement");
                            Futures.addCallback(resourceCoordinatorClient.modifyRequirement(Constants.Service.STREAMS, modifier), callback);
                        } catch (InterruptedException e) {
                            LOG.warn("Stream resource retry thread interrupted", e);
                        }
                    }
                };
                retryThread.setDaemon(true);
                retryThread.start();
            }
        }
    });
}
Also used : ResourceModifier(co.cask.cdap.common.zookeeper.coordination.ResourceModifier) ResourceRequirement(co.cask.cdap.common.zookeeper.coordination.ResourceRequirement) FutureCallback(com.google.common.util.concurrent.FutureCallback)

Aggregations

ResourceModifier (co.cask.cdap.common.zookeeper.coordination.ResourceModifier)2 ResourceRequirement (co.cask.cdap.common.zookeeper.coordination.ResourceRequirement)2 StreamSpecification (co.cask.cdap.api.data.stream.StreamSpecification)1 NamespaceId (co.cask.cdap.proto.id.NamespaceId)1 StreamId (co.cask.cdap.proto.id.StreamId)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 FutureCallback (com.google.common.util.concurrent.FutureCallback)1 Map (java.util.Map)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 Nullable (javax.annotation.Nullable)1