Search in sources :

Example 1 with TaskSubscription

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;
}
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 TaskSubscription

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());
        }
    }));
}
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)

Aggregations

TaskSubscription (io.zeebe.broker.task.processor.TaskSubscription)2 ActorFuture (io.zeebe.util.sched.future.ActorFuture)2 DirectBuffer (org.agrona.DirectBuffer)2 Loggers (io.zeebe.broker.Loggers)1 SNAPSHOT_STORAGE_SERVICE (io.zeebe.broker.logstreams.LogStreamServiceNames.SNAPSHOT_STORAGE_SERVICE)1 TASK_LOCK_STREAM_PROCESSOR_ID (io.zeebe.broker.logstreams.processor.StreamProcessorIds.TASK_LOCK_STREAM_PROCESSOR_ID)1 StreamProcessorService (io.zeebe.broker.logstreams.processor.StreamProcessorService)1 TypedStreamEnvironment (io.zeebe.broker.logstreams.processor.TypedStreamEnvironment)1 TypedStreamProcessor (io.zeebe.broker.logstreams.processor.TypedStreamProcessor)1 TaskQueueServiceNames.taskQueueLockStreamProcessorServiceName (io.zeebe.broker.task.TaskQueueServiceNames.taskQueueLockStreamProcessorServiceName)1 TaskSubscriptionManager (io.zeebe.broker.task.TaskSubscriptionManager)1 LockTaskStreamProcessor (io.zeebe.broker.task.processor.LockTaskStreamProcessor)1 TaskSubscriptionRequest (io.zeebe.broker.task.processor.TaskSubscriptionRequest)1 LogStream (io.zeebe.logstreams.log.LogStream)1 StreamProcessorController (io.zeebe.logstreams.processor.StreamProcessorController)1 ControlMessageType (io.zeebe.protocol.clientapi.ControlMessageType)1 BrokerEventMetadata (io.zeebe.protocol.impl.BrokerEventMetadata)1 ServiceName (io.zeebe.servicecontainer.ServiceName)1 ServiceStartContext (io.zeebe.servicecontainer.ServiceStartContext)1 RemoteAddress (io.zeebe.transport.RemoteAddress)1