Search in sources :

Example 1 with ActorFuture

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;
}
Also used : DirectBuffer(org.agrona.DirectBuffer) StreamProcessorService(io.zeebe.broker.logstreams.processor.StreamProcessorService) TransportListener(io.zeebe.transport.TransportListener) TaskSubscription(io.zeebe.broker.task.processor.TaskSubscription) LogStream(io.zeebe.logstreams.log.LogStream) ArrayList(java.util.ArrayList) LockTaskStreamProcessor(io.zeebe.broker.task.processor.LockTaskStreamProcessor) RemoteAddress(io.zeebe.transport.RemoteAddress) Long2ObjectHashMap(org.agrona.collections.Long2ObjectHashMap) HeapBufferAllocator(io.zeebe.util.allocation.HeapBufferAllocator) CompletableActorFuture(io.zeebe.util.sched.future.CompletableActorFuture) TypedStreamProcessor(io.zeebe.broker.logstreams.processor.TypedStreamProcessor) ServiceName(io.zeebe.servicecontainer.ServiceName) StreamProcessorController(io.zeebe.logstreams.processor.StreamProcessorController) CompactList(io.zeebe.util.collection.CompactList) Iterator(java.util.Iterator) Int2ObjectHashMap(org.agrona.collections.Int2ObjectHashMap) Set(java.util.Set) ServerTransport(io.zeebe.transport.ServerTransport) TASK_LOCK_STREAM_PROCESSOR_ID(io.zeebe.broker.logstreams.processor.StreamProcessorIds.TASK_LOCK_STREAM_PROCESSOR_ID) TaskQueueServiceNames.taskQueueLockStreamProcessorServiceName(io.zeebe.broker.task.TaskQueueServiceNames.taskQueueLockStreamProcessorServiceName) ActorFuture(io.zeebe.util.sched.future.ActorFuture) List(java.util.List) TypedStreamEnvironment(io.zeebe.broker.logstreams.processor.TypedStreamEnvironment) Actor(io.zeebe.util.sched.Actor) ServiceStartContext(io.zeebe.servicecontainer.ServiceStartContext) BufferUtil(io.zeebe.util.buffer.BufferUtil) BufferUtil.bufferAsString(io.zeebe.util.buffer.BufferUtil.bufferAsString) SNAPSHOT_STORAGE_SERVICE(io.zeebe.broker.logstreams.LogStreamServiceNames.SNAPSHOT_STORAGE_SERVICE) Entry(java.util.Map.Entry) Loggers(io.zeebe.broker.Loggers) DirectBuffer(org.agrona.DirectBuffer) CompletableActorFuture(io.zeebe.util.sched.future.CompletableActorFuture) LockTaskStreamProcessor(io.zeebe.broker.task.processor.LockTaskStreamProcessor)

Example 2 with ActorFuture

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());
        }
    }));
}
Also used : TaskSubscriptionRequest(io.zeebe.broker.task.processor.TaskSubscriptionRequest) ActorControl(io.zeebe.util.sched.ActorControl) ActorFuture(io.zeebe.util.sched.future.ActorFuture) BrokerEventMetadata(io.zeebe.protocol.impl.BrokerEventMetadata) TaskSubscription(io.zeebe.broker.task.processor.TaskSubscription) ControlMessageType(io.zeebe.protocol.clientapi.ControlMessageType) TaskSubscriptionManager(io.zeebe.broker.task.TaskSubscriptionManager) ServerOutput(io.zeebe.transport.ServerOutput) TaskSubscriptionRequest(io.zeebe.broker.task.processor.TaskSubscriptionRequest) BufferUtil.cloneBuffer(io.zeebe.util.buffer.BufferUtil.cloneBuffer) DirectBuffer(org.agrona.DirectBuffer) TaskSubscription(io.zeebe.broker.task.processor.TaskSubscription)

Example 3 with ActorFuture

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();
}
Also used : SubscriptionManager(io.zeebe.client.task.impl.subscription.SubscriptionManager) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ControlMessageType(io.zeebe.protocol.clientapi.ControlMessageType) TimeoutException(java.util.concurrent.TimeoutException) ZeebeClientImpl(io.zeebe.client.impl.ZeebeClientImpl) ControlMessageRequest(io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest) ZeebeClient(io.zeebe.client.ZeebeClient) StubBrokerRule(io.zeebe.test.broker.protocol.brokerapi.StubBrokerRule) Future(java.util.concurrent.Future) ErrorCode(io.zeebe.protocol.clientapi.ErrorCode) RemoteAddress(io.zeebe.transport.RemoteAddress) ClientRule(io.zeebe.client.util.ClientRule) TopicSubscriberGroup(io.zeebe.client.event.impl.TopicSubscriberGroup) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) TopicSubscriptionBuilderImpl(io.zeebe.client.event.impl.TopicSubscriptionBuilderImpl) TestUtil.waitUntil(io.zeebe.test.util.TestUtil.waitUntil) Set(java.util.Set) Test(org.junit.Test) Collectors(java.util.stream.Collectors) ResponseController(io.zeebe.test.broker.protocol.brokerapi.ResponseController) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) RuleChain(org.junit.rules.RuleChain) ActorFuture(io.zeebe.util.sched.future.ActorFuture) List(java.util.List) Stream(java.util.stream.Stream) Rule(org.junit.Rule) EventType(io.zeebe.protocol.clientapi.EventType) Conditions(io.zeebe.test.util.Conditions) Optional(java.util.Optional) ExecuteCommandRequest(io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) TestUtil(io.zeebe.test.util.TestUtil) TopicSubscriberGroup(io.zeebe.client.event.impl.TopicSubscriberGroup) ResponseController(io.zeebe.test.broker.protocol.brokerapi.ResponseController) Test(org.junit.Test)

Example 4 with ActorFuture

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;
}
Also used : DirectBuffer(org.agrona.DirectBuffer) ActorControl(io.zeebe.util.sched.ActorControl) ActorClock(io.zeebe.util.sched.clock.ActorClock) ActorFuture(io.zeebe.util.sched.future.ActorFuture) io.zeebe.broker.logstreams.processor(io.zeebe.broker.logstreams.processor) io.zeebe.transport(io.zeebe.transport) Duration(java.time.Duration) Loggers(io.zeebe.broker.Loggers) PartitionManager(io.zeebe.broker.clustering.management.PartitionManager) TopologyBroker(io.zeebe.broker.clustering.handler.TopologyBroker) DirectBuffer(org.agrona.DirectBuffer) TopologyBroker(io.zeebe.broker.clustering.handler.TopologyBroker)

Example 5 with ActorFuture

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());
        }
    }));
}
Also used : AbstractControlMessageHandler(io.zeebe.broker.transport.controlmessage.AbstractControlMessageHandler) ActorControl(io.zeebe.util.sched.ActorControl) ActorFuture(io.zeebe.util.sched.future.ActorFuture) BrokerEventMetadata(io.zeebe.protocol.impl.BrokerEventMetadata) TopicSubscriptionService(io.zeebe.broker.event.processor.TopicSubscriptionService) ControlMessageType(io.zeebe.protocol.clientapi.ControlMessageType) ServerOutput(io.zeebe.transport.ServerOutput) BufferUtil.cloneBuffer(io.zeebe.util.buffer.BufferUtil.cloneBuffer) CloseSubscriptionRequest(io.zeebe.broker.event.processor.CloseSubscriptionRequest) DirectBuffer(org.agrona.DirectBuffer) CloseSubscriptionRequest(io.zeebe.broker.event.processor.CloseSubscriptionRequest)

Aggregations

ActorFuture (io.zeebe.util.sched.future.ActorFuture)6 ControlMessageType (io.zeebe.protocol.clientapi.ControlMessageType)4 DirectBuffer (org.agrona.DirectBuffer)4 RemoteAddress (io.zeebe.transport.RemoteAddress)3 List (java.util.List)3 Set (java.util.Set)3 Loggers (io.zeebe.broker.Loggers)2 TaskSubscription (io.zeebe.broker.task.processor.TaskSubscription)2 ZeebeClient (io.zeebe.client.ZeebeClient)2 TopicSubscriberGroup (io.zeebe.client.event.impl.TopicSubscriberGroup)2 TopicSubscriptionBuilderImpl (io.zeebe.client.event.impl.TopicSubscriptionBuilderImpl)2 ZeebeClientImpl (io.zeebe.client.impl.ZeebeClientImpl)2 SubscriptionManager (io.zeebe.client.task.impl.subscription.SubscriptionManager)2 ClientRule (io.zeebe.client.util.ClientRule)2 ErrorCode (io.zeebe.protocol.clientapi.ErrorCode)2 EventType (io.zeebe.protocol.clientapi.EventType)2 BrokerEventMetadata (io.zeebe.protocol.impl.BrokerEventMetadata)2 ControlMessageRequest (io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest)2 ExecuteCommandRequest (io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest)2 ResponseController (io.zeebe.test.broker.protocol.brokerapi.ResponseController)2