use of io.zeebe.broker.task.processor.TaskSubscription in project zeebe by zeebe-io.
the class TaskSubscriptionManager method addSubscription.
public ActorFuture<Void> addSubscription(final TaskSubscription subscription) {
final CompletableActorFuture<Void> future = new CompletableActorFuture<>();
actor.call(() -> {
final DirectBuffer taskType = subscription.getLockTaskType();
final int partitionId = subscription.getPartitionId();
final LogStreamBucket logStreamBucket = logStreamBuckets.get(partitionId);
if (logStreamBucket == null) {
future.completeExceptionally(new RuntimeException(String.format("Partition with id '%d' not found.", partitionId)));
return;
}
final long subscriptionId = nextSubscriptionId++;
subscription.setSubscriberKey(subscriptionId);
final LockTaskStreamProcessor streamProcessor = logStreamBucket.getStreamProcessorByTaskType(taskType);
if (streamProcessor != null) {
streamProcessorBySubscriptionId.put(subscriptionId, streamProcessor);
final ActorFuture<Void> addFuture = streamProcessor.addSubscription(subscription);
actor.runOnCompletion(addFuture, (aVoid, throwable) -> {
if (throwable == null) {
actor.submit(this::handleCreditRequests);
future.complete(null);
} else {
future.completeExceptionally(throwable);
}
});
} else {
final LockTaskStreamProcessor processor = new LockTaskStreamProcessor(taskType);
final ActorFuture<Void> processorFuture = createStreamProcessorService(processor, taskType, logStreamBucket, taskType);
actor.runOnCompletion(processorFuture, (v, t) -> {
if (t == null) {
streamProcessorBySubscriptionId.put(subscriptionId, processor);
logStreamBucket.addStreamProcessor(processor);
final ActorFuture<Void> addFuture = processor.addSubscription(subscription);
actor.runOnCompletion(addFuture, ((aVoid, throwable) -> {
if (throwable == null) {
actor.submit(this::handleCreditRequests);
future.complete(null);
} else {
future.completeExceptionally(throwable);
}
}));
} else {
future.completeExceptionally(t);
}
});
}
});
return future;
}
use of io.zeebe.broker.task.processor.TaskSubscription in project zeebe by zeebe-io.
the class AddTaskSubscriptionHandler method handle.
@Override
public void handle(final ActorControl actor, final int partitionId, final DirectBuffer buffer, final BrokerEventMetadata eventMetada) {
final TaskSubscriptionRequest request = new TaskSubscriptionRequest();
request.wrap(cloneBuffer(buffer));
final long requestId = eventMetada.getRequestId();
final int requestStreamId = eventMetada.getRequestStreamId();
final TaskSubscription taskSubscription = new TaskSubscription(partitionId, request.getLockTaskType(), request.getLockDuration(), request.getLockOwner(), requestStreamId);
taskSubscription.setCredits(request.getCredits());
final ActorFuture<Void> future = manager.addSubscription(taskSubscription);
actor.runOnCompletion(future, ((aVoid, throwable) -> {
if (throwable == null) {
final long subscriberKey = taskSubscription.getSubscriberKey();
request.setSubscriberKey(subscriberKey);
sendResponse(actor, requestStreamId, requestId, request);
} else {
sendErrorResponse(actor, requestStreamId, requestId, "Cannot add task subscription. %s", throwable.getMessage());
}
}));
}
Aggregations