use of io.zeebe.util.sched.future.ActorFuture 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.util.sched.future.ActorFuture 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());
}
}));
}
use of io.zeebe.util.sched.future.ActorFuture in project zeebe by zeebe-io.
the class TopicSubscriptionTest method shouldCloseClientAfterSubscriptionCloseIsCalled.
@Test
public void shouldCloseClientAfterSubscriptionCloseIsCalled() throws Exception {
// given
broker.stubTopicSubscriptionApi(0L);
final ResponseController responseController = broker.onControlMessageRequest((r) -> r.messageType() == ControlMessageType.REMOVE_TOPIC_SUBSCRIPTION).respondWith().data().allOf((r) -> r.getData()).done().registerControlled();
final TopicSubscription foo = client.topics().newSubscription(clientRule.getDefaultTopicName()).handler(DO_NOTHING).name("foo").open();
final ActorFuture<Void> future = ((TopicSubscriberGroup) foo).closeAsync();
waitUntil(() -> broker.getReceivedControlMessageRequests().stream().filter(r -> r.messageType() == ControlMessageType.REMOVE_TOPIC_SUBSCRIPTION).count() == 1);
final Thread closingThread = new Thread(client::close);
closingThread.start();
// when
responseController.unblockNextResponse();
// then
closingThread.join();
waitUntil(() -> future.isDone());
assertThat(future).isDone();
}
use of io.zeebe.util.sched.future.ActorFuture in project zeebe by zeebe-io.
the class CreatePartitionProcessor method executeSideEffects.
@Override
public boolean executeSideEffects(TypedEvent<PartitionEvent> event, TypedResponseWriter responseWriter) {
final PartitionEvent value = event.getValue();
final TopologyBroker creator = value.getCreator();
final DirectBuffer creatorHost = creator.getHost();
creatorAddress.host(creatorHost, 0, creatorHost.capacity());
creatorAddress.port(creator.getPort());
final ActorFuture<ClientResponse> partitionRemote = partitionManager.createPartitionRemote(creatorAddress, value.getTopicName(), value.getId());
actor.runOnCompletion(partitionRemote, ((clientRequest, throwable) -> {
if (throwable == null) {
clientRequest.close();
} else {
Loggers.SYSTEM_LOGGER.error("Failed to create partitions request.", throwable);
}
}));
return true;
}
use of io.zeebe.util.sched.future.ActorFuture in project zeebe by zeebe-io.
the class RemoveTopicSubscriptionHandler method handle.
@Override
public void handle(final ActorControl actor, final int partitionId, final DirectBuffer buffer, final BrokerEventMetadata metadata) {
final int requestStreamId = metadata.getRequestStreamId();
final long requestId = metadata.getRequestId();
final CloseSubscriptionRequest request = new CloseSubscriptionRequest();
request.wrap(cloneBuffer(buffer));
final ActorFuture<Void> future = subscriptionService.closeSubscriptionAsync(partitionId, request.getSubscriberKey());
actor.runOnCompletion(future, ((aVoid, throwable) -> {
if (throwable == null) {
sendResponse(actor, requestStreamId, requestId, request);
} else {
sendErrorResponse(actor, requestStreamId, requestId, "Cannot close topic subscription. %s", throwable.getMessage());
}
}));
}
Aggregations