use of co.cask.cdap.common.zookeeper.coordination.ResourceRequirement 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);
}
}
};
}
use of co.cask.cdap.common.zookeeper.coordination.ResourceRequirement 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();
}
}
});
}
use of co.cask.cdap.common.zookeeper.coordination.ResourceRequirement in project cdap by caskdata.
the class ResourceBalancerService method startUp.
@Override
protected void startUp() throws Exception {
LOG.info("Starting ResourceBalancer {} service...", serviceName);
// We first submit requirement before starting coordinator to make sure all needed paths in ZK are created
ResourceRequirement requirement = ResourceRequirement.builder(serviceName).addPartitions("", partitionCount, 1).build();
resourceClient.submitRequirement(requirement).get();
Discoverable discoverable = createDiscoverable(serviceName);
cancelDiscoverable = discoveryService.register(ResolvingDiscoverable.of(discoverable));
election.start();
resourceClient.startAndWait();
cancelResourceHandler = resourceClient.subscribe(serviceName, createResourceHandler(discoverable));
LOG.info("Started ResourceBalancer {} service...", serviceName);
}
Aggregations