Search in sources :

Example 6 with ReactorGraph

use of ru.fix.completable.reactor.runtime.ReactorGraph in project completable-reactor by ru-fix.

the class CompletableReactorHandlerArgumentsTest method arguments_test_4.

@Test
public void arguments_test_4() throws Exception {
    @Reactored("Payload with string")
    @Data
    class Payload {

        String data;
    }
    class Service {

        @Reactored("method with 6 arguments")
        public CompletableFuture<String> foo(short arg1, boolean arg2, BigInteger arg3, long arg4) {
            return CompletableFuture.completedFuture("" + arg1 + arg2 + arg3 + arg4);
        }
    }
    final Service service = new Service();
    class Config {

        ReactorGraphBuilder graphBuilder = new ReactorGraphBuilder(this);

        Processor<Payload> processor = graphBuilder.processor().forPayload(Payload.class).passArg(pld -> (short) 3).passArg(pld -> true).passArg(pld -> BigInteger.valueOf(6L)).passArg(pld -> 7L).withHandler(service::foo).withMerger((payload, result) -> {
            payload.data = result;
            return CompletableReactorTest.Status.OK;
        }).buildProcessor();

        ReactorGraph<Payload> graph() {
            return graphBuilder.payload(Payload.class).handle(processor).mergePoint(processor).onAny().complete().coordinates().buildGraph();
        }
    }
    val graph = new Config().graph();
    reactor.registerReactorGraph(graph);
    assertEquals("3true67", reactor.submit(new Payload()).getResultFuture().get(5, TimeUnit.SECONDS).getData());
}
Also used : Reactored(ru.fix.completable.reactor.api.Reactored) lombok.val(lombok.val) Test(org.junit.Test) CompletableFuture(java.util.concurrent.CompletableFuture) CompletableReactor(ru.fix.completable.reactor.runtime.CompletableReactor) TimeUnit(java.util.concurrent.TimeUnit) ReactorGraph(ru.fix.completable.reactor.runtime.ReactorGraph) Processor(ru.fix.completable.reactor.runtime.dsl.Processor) ReactorGraphBuilder(ru.fix.completable.reactor.runtime.ReactorGraphBuilder) Data(lombok.Data) BigInteger(java.math.BigInteger) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) SimpleProfiler(ru.fix.commons.profiler.impl.SimpleProfiler) lombok.val(lombok.val) ReactorGraphBuilder(ru.fix.completable.reactor.runtime.ReactorGraphBuilder) Processor(ru.fix.completable.reactor.runtime.dsl.Processor) Reactored(ru.fix.completable.reactor.api.Reactored) BigInteger(java.math.BigInteger) Data(lombok.Data) Test(org.junit.Test)

Example 7 with ReactorGraph

use of ru.fix.completable.reactor.runtime.ReactorGraph in project completable-reactor by ru-fix.

the class CompletableReactorHandlerArgumentsTest method arguments_test_5.

@Test
public void arguments_test_5() throws Exception {
    @Reactored("Payload with string")
    @Data
    class Payload {

        String data;
    }
    class Service {

        @Reactored("method with 6 arguments")
        public CompletableFuture<String> foo(String arg1, short arg2, boolean arg3, BigInteger arg4, long arg5) {
            return CompletableFuture.completedFuture(arg1 + arg2 + arg3 + arg4 + arg5);
        }
    }
    final Service service = new Service();
    class Config {

        ReactorGraphBuilder graphBuilder = new ReactorGraphBuilder(this);

        Processor<Payload> processor = graphBuilder.processor().forPayload(Payload.class).passArg(pld -> "1").passArg(pld -> (short) 3).passArg(pld -> true).passArg(pld -> BigInteger.valueOf(6L)).passArg(pld -> 7L).withHandler(service::foo).withMerger((payload, result) -> {
            payload.data = result;
            return CompletableReactorTest.Status.OK;
        }).buildProcessor();

        ReactorGraph<Payload> graph() {
            return graphBuilder.payload(Payload.class).handle(processor).mergePoint(processor).onAny().complete().coordinates().buildGraph();
        }
    }
    val graph = new Config().graph();
    reactor.registerReactorGraph(graph);
    assertEquals("13true67", reactor.submit(new Payload()).getResultFuture().get(5, TimeUnit.SECONDS).getData());
}
Also used : Reactored(ru.fix.completable.reactor.api.Reactored) lombok.val(lombok.val) Test(org.junit.Test) CompletableFuture(java.util.concurrent.CompletableFuture) CompletableReactor(ru.fix.completable.reactor.runtime.CompletableReactor) TimeUnit(java.util.concurrent.TimeUnit) ReactorGraph(ru.fix.completable.reactor.runtime.ReactorGraph) Processor(ru.fix.completable.reactor.runtime.dsl.Processor) ReactorGraphBuilder(ru.fix.completable.reactor.runtime.ReactorGraphBuilder) Data(lombok.Data) BigInteger(java.math.BigInteger) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) SimpleProfiler(ru.fix.commons.profiler.impl.SimpleProfiler) lombok.val(lombok.val) ReactorGraphBuilder(ru.fix.completable.reactor.runtime.ReactorGraphBuilder) Processor(ru.fix.completable.reactor.runtime.dsl.Processor) Reactored(ru.fix.completable.reactor.api.Reactored) BigInteger(java.math.BigInteger) Data(lombok.Data) Test(org.junit.Test)

Example 8 with ReactorGraph

use of ru.fix.completable.reactor.runtime.ReactorGraph in project completable-reactor by ru-fix.

the class CompletableReactorTest method run_subgraph.

@Test
public void run_subgraph() throws Exception {
    class Config {

        ReactorGraphBuilder builder = new ReactorGraphBuilder(this);

        Processor<IdListPayload> idProcessor11 = buildProcessor(builder, new IdProcessor(11));

        Processor<IdListPayload> idProcessor12 = buildProcessor(builder, new IdProcessor(12));

        Processor<IdListPayload> idProcessor13 = buildProcessor(builder, new IdProcessor(13));

        ReactorGraph<SubgraphPayload> childGraph() {
            return builder.payload(SubgraphPayload.class).handle(idProcessor11).handle(idProcessor12).mergePoint(idProcessor11).onAny().merge(idProcessor12).mergePoint(idProcessor12).onAny().handle(idProcessor13).mergePoint(idProcessor13).onAny().complete().coordinates().proc(idProcessor11, 306, 216).proc(idProcessor12, 612, 218).proc(idProcessor13, 539, 596).merge(idProcessor11, 430, 365).merge(idProcessor12, 620, 421).merge(idProcessor13, 613, 693).start(500, 100).complete(idProcessor13, 587, 776).buildGraph();
        }

        Processor<IdListPayload> idProcessor1 = buildProcessor(builder, new IdProcessor(1));

        Processor<IdListPayload> idProcessor2 = buildProcessor(builder, new IdProcessor(2));

        Processor<IdListPayload> idProcessor3 = buildProcessor(builder, new IdProcessor(3));

        Subgraph<ParentGraphPayload> subgraphProcessor = builder.subgraph(SubgraphPayload.class).forPayload(ParentGraphPayload.class).passArg(payload -> new SubgraphPayload()).withMerger((payload, result) -> {
            payload.getIdSequence().addAll(result.getIdSequence());
            return Status.OK;
        }).buildSubgraph();

        ReactorGraph<ParentGraphPayload> parentGraph() {
            return builder.payload(ParentGraphPayload.class).handle(idProcessor1).mergePoint(idProcessor1).onAny().handle(idProcessor2).onAny().handle(subgraphProcessor).mergePoint(subgraphProcessor).onAny().merge(idProcessor2).mergePoint(idProcessor2).onAny().handle(idProcessor3).mergePoint(idProcessor3).onAny().complete().coordinates().proc(idProcessor1, 406, 228).proc(idProcessor2, 626, 408).proc(idProcessor3, 415, 730).proc(subgraphProcessor, 195, 418).merge(idProcessor1, 475, 342).merge(subgraphProcessor, 341, 565).merge(idProcessor2, 488, 620).merge(idProcessor3, 490, 840).start(460, 120).complete(idProcessor3, 460, 930).buildGraph();
        }
    }
    Config config = new Config();
    val childGraph = config.childGraph();
    val parentGraph = config.parentGraph();
    reactor.registerReactorGraph(childGraph);
    printGraph(childGraph, parentGraph);
    reactor.registerReactorGraph(parentGraph);
    ParentGraphPayload resultPaylaod = reactor.submit(new ParentGraphPayload()).getResultFuture().get(5, TimeUnit.SECONDS);
    assertEquals(Arrays.asList(1, 11, 12, 13, 2, 3), resultPaylaod.getIdSequence());
}
Also used : Reactored(ru.fix.completable.reactor.api.Reactored) Arrays(java.util.Arrays) Accessors(lombok.experimental.Accessors) TimeoutException(java.util.concurrent.TimeoutException) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) ReactorGraph(ru.fix.completable.reactor.runtime.ReactorGraph) Processor(ru.fix.completable.reactor.runtime.dsl.Processor) ReactorGraphBuilder(ru.fix.completable.reactor.runtime.ReactorGraphBuilder) lombok.experimental.var(lombok.experimental.var) MergePoint(ru.fix.completable.reactor.runtime.dsl.MergePoint) Before(org.junit.Before) Semaphore(java.util.concurrent.Semaphore) lombok.val(lombok.val) Test(org.junit.Test) EqualsAndHashCode(lombok.EqualsAndHashCode) CompletableReactor(ru.fix.completable.reactor.runtime.CompletableReactor) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Subgraph(ru.fix.completable.reactor.runtime.dsl.Subgraph) Data(lombok.Data) Assert(org.junit.Assert) SimpleProfiler(ru.fix.commons.profiler.impl.SimpleProfiler) lombok.val(lombok.val) ReactorGraphBuilder(ru.fix.completable.reactor.runtime.ReactorGraphBuilder) Processor(ru.fix.completable.reactor.runtime.dsl.Processor) Subgraph(ru.fix.completable.reactor.runtime.dsl.Subgraph) Test(org.junit.Test)

Example 9 with ReactorGraph

use of ru.fix.completable.reactor.runtime.ReactorGraph in project completable-reactor by ru-fix.

the class ReactorGraphExecutionBuilder method build.

/**
 * @param reactorGraph
 * @param <PayloadType>
 * @return
 */
public <PayloadType> ReactorGraphExecution<PayloadType> build(ReactorGraph<PayloadType> reactorGraph) {
    val crReactorGraph = (CRReactorGraph<PayloadType>) reactorGraph;
    /**
     * Internal representation of processing graph based on processing vertices.
     */
    final Map<CRProcessingItem, ProcessingVertex> processingVertices = new HashMap<>();
    final CompletableFuture<PayloadType> submitFuture = new CompletableFuture<>();
    /**
     * Will be completed on payload submission to processor chain
     */
    final CompletableFuture<ReactorGraphExecutionBuilder.TransitionPayloadContext> startPointTransitionFuture = submitFuture.thenApplyAsync(payload -> new TransitionPayloadContext().setPayload(payload));
    /**
     * Will be completed with payload when terminal graph state would be reached.
     */
    final CompletableFuture<PayloadType> executionResultFuture = new CompletableFuture<>();
    /**
     * Init Processing Vertices.
     */
    crReactorGraph.getProcessingItems().forEach((proc, info) -> {
        ProcessingVertex vertex = new ProcessingVertex().setProcessingItem(proc).setProcessingItemInfo(info);
        if (info.getProcessingItemType() == CRReactorGraph.ProcessingItemType.MERGE_POINT) {
            /**
             * Detached merge point does not uses {@code {@link ProcessingVertex#getProcessorFuture()}
             */
            vertex.getProcessorFuture().completeExceptionally(new IllegalStateException(String.format("Detached Merge Point %s should not use processorFuture.", vertex.getProcessingItem().getDebugName())));
        }
        processingVertices.put(proc, vertex);
    });
    /**
     * Populate start point transition
     */
    for (CRProcessingItem processor : crReactorGraph.getStartPoint().getProcessingItems()) {
        ProcessingVertex vertex = processingVertices.get(processor);
        if (vertex.getProcessingItemInfo().getProcessingItemType() == CRReactorGraph.ProcessingItemType.MERGE_POINT) {
            /**
             * In case of Detached merge point transition from start point is being converted
             * to a {@link MergePayloadContext}
             */
            vertex.getIncomingMergeFlows().add(new TransitionFuture<>(startPointTransitionFuture.thenApplyAsync(transitionPayloadContext -> new MergePayloadContext().setDeadTransition(transitionPayloadContext.isDeadTransition()).setTerminal(transitionPayloadContext.isTerminal()).setPayload(transitionPayloadContext.getPayload()).setMergeResult(null))));
        } else {
            vertex.getIncomingProcessorFlows().add(new TransitionFuture<>(startPointTransitionFuture));
        }
    }
    /**
     * Populate MergePoints
     */
    crReactorGraph.getMergePoints().forEach(mergePoint -> {
        ProcessingVertex mergePointVertex = processingVertices.get(mergePoint.asProcessingItem());
        mergePoint.getTransitions().forEach(mergePointVertex.getMergePointTransition()::add);
    });
    /**
     * Populate transitions to all MergePoints
     */
    crReactorGraph.getMergePoints().forEach(mergePoint -> {
        ProcessingVertex mergePointVertex = processingVertices.get(mergePoint.asProcessingItem());
        mergePoint.getTransitions().forEach(mergePointVertex.getMergePointTransition()::add);
    });
    /**
     * Populate outgoing flows
     */
    for (CRReactorGraph.MergePoint mergePoint : crReactorGraph.getMergePoints()) {
        /**
         * MergeGroup can contain more that one MergePoint, so we have to wait all merge points to complete before
         * invoke processors.
         * We should not wait all merge point to complete if we are invoking other merge point within mergeGroup.
         */
        ProcessingVertex mergePointVertex = processingVertices.get(mergePoint.asProcessingItem());
        CompletableFuture<MergePayloadContext> mergePointFuture = mergePointVertex.getMergePointFuture();
        for (CRReactorGraph.Transition transition : mergePoint.getTransitions()) {
            /**
             * Terminal merge point vertex handled synchronously with merging process
             * Skip that kind of transition during pre-processing.
             */
            if (!transition.isComplete()) {
                /**
                 * Populate outgoing processor flows
                 * activates when all merge points within merge group is completed
                 */
                if (transition.getHandleBy() != null) {
                    CRProcessingItem proc = transition.getHandleBy();
                    processingVertices.get(proc).getIncomingProcessorFlows().add(new TransitionFuture<>(mergePointFuture.thenApplyAsync(context -> {
                        if (context.isTerminal()) {
                            return new TransitionPayloadContext().setTerminal(true);
                        } else if (context.isDeadTransition()) {
                            return new TransitionPayloadContext().setDeadTransition(true);
                        } else if (transition.isOnAny() || transition.getMergeStatuses().contains(context.mergeResult)) {
                            return new TransitionPayloadContext().setPayload(context.payload);
                        } else {
                            return new TransitionPayloadContext().setPayload(context.payload).setDeadTransition(true);
                        }
                    }).exceptionally(exc -> {
                        log.error("Failed to activate");
                        return null;
                    })));
                }
                /**
                 * Populate outgoing merge flows
                 */
                if (transition.getMerge() != null) {
                    CRProcessingItem proc = transition.getMerge();
                    ProcessingVertex transitionDestinationVertex = processingVertices.get(proc);
                    transitionDestinationVertex.getIncomingMergeFlows().add(new TransitionFuture<>(mergePointFuture.thenApplyAsync(context -> {
                        if (context.isTerminal()) {
                            return new MergePayloadContext().setTerminal(true);
                        } else if (context.isDeadTransition()) {
                            return new MergePayloadContext().setDeadTransition(true);
                        } else if (transition.isOnAny() || transition.getMergeStatuses().contains(context.mergeResult)) {
                            return new MergePayloadContext().setPayload(context.payload).setMergeResult(context.mergeResult);
                        } else {
                            return new MergePayloadContext().setDeadTransition(true);
                        }
                    })));
                }
            }
        }
    }
    // TODO FIX!! allow detached processor to read data from payload and only after that execute merge point
    // down the flow, so merge point and detached processor would not run concurrently
    /**
     * Join incoming processor flows to single processor invocation
     */
    processingVertices.forEach((processor, processingItem) -> {
        if (processingItem.getProcessingItemInfo().getProcessingItemType() == CRReactorGraph.ProcessingItemType.MERGE_POINT) {
            /**
             * Detached merge point does not have graph processor, only merge point.
             * No processor invocation is needed
             */
            if (processingItem.getIncomingProcessorFlows().size() != 0) {
                throw new IllegalStateException(String.format("Invalid graph state. Detached merge point %s have more than 0 incoming flows.", processor.getDebugName()));
            }
            return;
        }
        if (processingItem.getIncomingProcessorFlows().size() <= 0) {
            throw new IllegalArgumentException(String.format("Invalid graph descriptor. Processor %s does not have incoming flows." + " Probably missing handleBy directive for this processor.", processor.getDebugName()));
        }
        CompletableFuture.allOf(processingItem.getIncomingProcessorFlows().stream().map(TransitionFuture::getFuture).toArray(CompletableFuture[]::new)).thenRunAsync(() -> {
            List<TransitionPayloadContext> incomingFlows = processingItem.getIncomingProcessorFlows().stream().map(future -> {
                try {
                    /**
                     * Future should be already complete
                     */
                    if (!future.getFuture().isDone()) {
                        Exception resultException = new Exception(String.format("Illegal graph execution state." + " Future is not completed. Processor: %s", processingItem.getProcessingItem().getDebugName()));
                        log.error(resultException.getMessage(), resultException);
                        executionResultFuture.completeExceptionally(resultException);
                        return INVALID_TRANSITION_PAYLOAD_CONTEXT;
                    } else {
                        return future.getFuture().get();
                    }
                } catch (Exception exc) {
                    Exception resultException = new Exception(String.format("Failed to get incoming processor flow future result" + " for processor: %s", processingItem.getProcessingItem().getDebugName()), exc);
                    log.error(resultException.getMessage(), resultException);
                    executionResultFuture.completeExceptionally(resultException);
                    return INVALID_TRANSITION_PAYLOAD_CONTEXT;
                }
            }).collect(Collectors.toList());
            if (incomingFlows.stream().anyMatch(context -> context == INVALID_TRANSITION_PAYLOAD_CONTEXT)) {
                /**
                 * Invalid graph execution state
                 * Mark as terminal all outgoing flows from processor
                 */
                processingItem.getProcessorFuture().complete(new HandlePayloadContext().setTerminal(true));
            } else if (incomingFlows.stream().anyMatch(TransitionPayloadContext::isTerminal)) {
                /**
                 * Terminal state reached.
                 * Mark as terminal all outgoing flows from processor
                 */
                processingItem.getProcessorFuture().complete(new HandlePayloadContext().setTerminal(true));
            } else {
                List<TransitionPayloadContext> activeIncomingFlows = incomingFlows.stream().filter(context -> !context.isDeadTransition()).collect(Collectors.toList());
                if (activeIncomingFlows.size() <= 0) {
                    /**
                     * There is no active incoming flow for given processor.
                     * Processor will not be invoked.
                     * All outgoing flows from processor will be marked as dead.
                     */
                    processingItem.getProcessorFuture().complete(new HandlePayloadContext().setDeadTransition(true));
                } else {
                    if (activeIncomingFlows.size() > 1) {
                        /**
                         * Illegal graph state. Too many active incoming flows.
                         * Mark as terminal all outgoing flows
                         * Complete graph with exception
                         */
                        Exception tooManyActiveIncomingFlowsExc = new Exception(String.format("There is more than one active incoming flow for processor %s." + " Reactor can not determinate from which of transitions" + " take payload." + " Possible loss of computation results." + " Possible concurrent modifications of payload.", processingItem.getProcessingItem().getDebugName()));
                        executionResultFuture.completeExceptionally(tooManyActiveIncomingFlowsExc);
                        processingItem.getProcessorFuture().complete(new HandlePayloadContext().setTerminal(true));
                    } else {
                        handle(processingItem, activeIncomingFlows.get(0), executionResultFuture);
                    }
                }
            }
        }).exceptionally(throwable -> {
            log.error("Join incoming processor flows failed.", throwable);
            return null;
        });
    });
    // processingVertices
    /**
     * Join incoming merge flows and processor handing future with single merging invocation
     */
    processingVertices.forEach((processor, vertex) -> {
        List<CompletableFuture> incomingFlows = new ArrayList<>();
        vertex.getIncomingMergeFlows().stream().map(TransitionFuture::getFuture).forEach(incomingFlows::add);
        if (vertex.getProcessingItemInfo().getProcessingItemType() != CRReactorGraph.ProcessingItemType.MERGE_POINT) {
            /**
             * Ignore processor future for detached merge point
             * And use it for all other cases
             */
            incomingFlows.add(vertex.getProcessorFuture());
        }
        CompletableFuture.allOf(incomingFlows.toArray(new CompletableFuture[incomingFlows.size()])).thenRunAsync(() -> {
            /**
             * Processor result, could be INVALID_HANDLE_PAYLOAD_CONTEXT in case of exception
             * Could be NULL in case of detached merge point
             */
            HandlePayloadContext handlePayloadContext = null;
            if (vertex.getProcessingItemInfo().getProcessingItemType() != CRReactorGraph.ProcessingItemType.MERGE_POINT) {
                handlePayloadContext = Optional.of(vertex.getProcessorFuture()).map(future -> {
                    try {
                        if (!future.isDone()) {
                            RuntimeException resultException = new RuntimeException(String.format("Illegal graph execution state. Processor future" + " is not completed. Processor %s", vertex.getProcessingItem().getDebugName()));
                            log.error(resultException.getMessage(), resultException);
                            executionResultFuture.completeExceptionally(resultException);
                            return INVALID_HANDLE_PAYLOAD_CONTEXT;
                        } else {
                            return future.get();
                        }
                    } catch (Exception exc) {
                        RuntimeException resultException = new RuntimeException(String.format("Failed to get processor future result for processor: %s", vertex.getProcessingItem().getDebugName()), exc);
                        log.error(resultException.getMessage(), resultException);
                        executionResultFuture.completeExceptionally(resultException);
                        return INVALID_HANDLE_PAYLOAD_CONTEXT;
                    }
                }).orElse(INVALID_HANDLE_PAYLOAD_CONTEXT);
                if (handlePayloadContext == INVALID_HANDLE_PAYLOAD_CONTEXT) {
                    /**
                     * Failed to get processor result.
                     * Merging will not be applied to payload.
                     * All outgoing flows from merge point will be marked as terminal.
                     * executionResult completed by exception
                     */
                    vertex.getMergePointFuture().complete(new MergePayloadContext().setTerminal(true));
                    return;
                } else if (handlePayloadContext.isTerminal()) {
                    /**
                     * Processor was marked as terminal during flow by terminal transition.
                     * Merging will not be applied to payload.
                     * All outgoing flows from merge point will be marked as terminal.
                     */
                    vertex.getMergePointFuture().complete(new MergePayloadContext().setTerminal(true));
                    return;
                } else if (handlePayloadContext.isDeadTransition()) {
                    /**
                     * Processor was disabled during flow by dead transition.
                     * Merging will not be applied to payload.
                     * All outgoing flows from merge point will be marked as dead.
                     */
                    vertex.getMergePointFuture().complete(new MergePayloadContext().setDeadTransition(true));
                    return;
                }
            }
            /**
             * Incoming merge flows, could be empty for processors Merge Point
             */
            List<MergePayloadContext> incomingMergeFlows = vertex.getIncomingMergeFlows().stream().map(future -> {
                try {
                    if (!future.getFuture().isDone()) {
                        RuntimeException resultException = new RuntimeException(String.format("Illegal graph execution state. Incoming merge future" + " is not complete." + " ProcessingVertex: %s", vertex));
                        log.error(resultException.getMessage(), resultException);
                        executionResultFuture.completeExceptionally(resultException);
                        return INVALID_MERGE_PAYLOAD_CONTEXT;
                    } else {
                        return future.getFuture().get();
                    }
                } catch (Exception exc) {
                    RuntimeException resultException = new RuntimeException(String.format("Failed to get incoming merge flow future result for processor: %s", vertex.getProcessingItem().getDebugName()), exc);
                    log.error(resultException.getMessage(), resultException);
                    executionResultFuture.completeExceptionally(resultException);
                    return INVALID_MERGE_PAYLOAD_CONTEXT;
                }
            }).collect(Collectors.toList());
            if (incomingMergeFlows.stream().anyMatch(context -> context == INVALID_MERGE_PAYLOAD_CONTEXT)) {
                /**
                 * Exception during merging
                 * Mark as terminal all outgoing flows from merge point
                 */
                vertex.getMergePointFuture().complete(new MergePayloadContext().setTerminal(true));
            } else if (incomingMergeFlows.stream().anyMatch(MergePayloadContext::isTerminal)) {
                /**
                 * Terminal state reached.
                 * Mark as terminal all outgoing flows from merge point
                 */
                vertex.getMergePointFuture().complete(new MergePayloadContext().setTerminal(true));
            } else {
                final List<MergePayloadContext> activeIncomingMergeFlows = incomingMergeFlows.stream().filter(context -> !context.isDeadTransition()).collect(Collectors.toList());
                if (vertex.getProcessingItemInfo().getProcessingItemType() == CRReactorGraph.ProcessingItemType.MERGE_POINT) {
                    /**
                     * Detached merge point
                     */
                    if (activeIncomingMergeFlows.size() == 0) {
                        /**
                         * Check that there are at least one incoming transition that marked as dead
                         */
                        if (incomingMergeFlows.stream().anyMatch(MergePayloadContext::isDeadTransition)) {
                            /**
                             * Detached MergePoint marked as Dead, because there are no active incoming
                             * flows and there is at least one incoming dead transition
                             * Mark as dead all outgoing flows from merge point
                             */
                            vertex.getMergePointFuture().complete(new MergePayloadContext().setDeadTransition(true));
                        } else {
                            throw new IllegalStateException(String.format("There is no incoming merge flows for detached merge point %s." + " At least dead incoming transition expected.", vertex.getProcessingItem().getDebugName()));
                        }
                    } else {
                        if (activeIncomingMergeFlows.size() > 1) {
                            /**
                             * Illegal graph state. Too many active incoming flows.
                             * Mark as terminal all outgoing flows from merge point
                             * Complete graph with exception
                             */
                            Exception tooManyActiveIncomingFlowsExc = new Exception(String.format("There is more than one active incoming flow for routing point %s." + " Reactor can not determinate from which of transitions take" + " payload." + " Possible loss of computation results." + " Possible concurrent modifications of payload.", vertex.getProcessingItem().getDebugName()));
                            executionResultFuture.completeExceptionally(tooManyActiveIncomingFlowsExc);
                            vertex.getMergePointFuture().complete(new MergePayloadContext().setTerminal(true));
                        } else {
                            /**
                             * Single active incoming merge flow
                             */
                            merge(vertex, Optional.empty(), activeIncomingMergeFlows.get(0).getPayload(), executionResultFuture);
                        }
                    }
                } else {
                    /**
                     * Processors MergePoint
                     */
                    if (incomingMergeFlows.size() == 0) {
                        /**
                         * No incoming merge flows, only one flow from processors handle
                         */
                        merge(vertex, handlePayloadContext.getProcessorResult(), handlePayloadContext.getPayload(), executionResultFuture);
                    } else {
                        /**
                         * Incoming merge flows exists. But some of them can be marked as dead.
                         */
                        if (activeIncomingMergeFlows.size() == 0) {
                            /**
                             * There is no active incoming merge flow for given merge point.
                             * Mark merge point as dead.
                             */
                            vertex.getMergePointFuture().complete(new MergePayloadContext().setDeadTransition(true));
                        } else {
                            if (activeIncomingMergeFlows.size() > 1) {
                                /**
                                 * Illegal graph state. Too many active incoming flows.
                                 * Mark as terminal all outgoing flows from merge point
                                 * Complete graph with exception
                                 */
                                Exception tooManyActiveIncomingFlowsExc = new Exception(String.format("There is more than one active incoming flow for merge point for" + " processor %s." + " Reactor can not determinate from which of transitions" + " take payload." + " Possible loss of computation results." + " Possible concurrent modifications of payload.", vertex.getProcessingItem().getDebugName()));
                                executionResultFuture.completeExceptionally(tooManyActiveIncomingFlowsExc);
                                vertex.getMergePointFuture().complete(new MergePayloadContext().setTerminal(true));
                            } else {
                                merge(vertex, handlePayloadContext.getProcessorResult(), activeIncomingMergeFlows.get(0).getPayload(), executionResultFuture);
                            }
                        }
                    }
                }
            }
        }).exceptionally(throwable -> {
            log.error("Joining incoming merge flows failed.", throwable);
            return null;
        });
    });
    // processingVertices
    /**
     * Handle terminal vertices.
     * When execution reaches 'complete' vertex all transitions should be marked dead and complete.
     */
    executionResultFuture.thenRunAsync(() -> {
        processingVertices.entrySet().stream().map(Map.Entry::getValue).map(ProcessingVertex::getIncomingProcessorFlows).flatMap(Collection::stream).map(TransitionFuture::getFuture).forEach(future -> future.complete(new TransitionPayloadContext().setDeadTransition(true)));
        processingVertices.entrySet().stream().map(Map.Entry::getValue).map(ProcessingVertex::getIncomingProcessorFlows).flatMap(Collection::stream).map(TransitionFuture::getFuture).forEach(future -> future.complete(new TransitionPayloadContext().setDeadTransition(true)));
    }).exceptionally(throwable -> {
        log.error("Marking transitions as dead is failed.", throwable);
        return null;
    });
    /**
     * Collect chain execution processor handling futures
     * Processors futures holds handle invocation result or dead context.
     * Exception is an detached merge point which processorFuture is not used
     * Then all processors futures completes chainExecutionFuture completes too.
     */
    CompletableFuture<Void> chainExecutionFuture = CompletableFuture.allOf(processingVertices.entrySet().stream().map(Map.Entry::getValue).filter(vertex -> vertex.getProcessingItemInfo().getProcessingItemType() != CRReactorGraph.ProcessingItemType.MERGE_POINT).map(ProcessingVertex::getProcessorFuture).toArray(CompletableFuture[]::new));
    return new ReactorGraphExecution<PayloadType>(submitFuture, executionResultFuture, chainExecutionFuture, debugProcessingVertexGraphState ? processingVertices.values() : null);
}
Also used : ReactorGraphModel(ru.fix.completable.reactor.api.ReactorGraphModel) java.util(java.util) Accessors(lombok.experimental.Accessors) ProfiledCall(ru.fix.commons.profiler.ProfiledCall) lombok.val(lombok.val) CompletableFuture(java.util.concurrent.CompletableFuture) CRProcessingItem(ru.fix.completable.reactor.runtime.internal.CRProcessingItem) ThreadsafeCopyMaker(ru.fix.completable.reactor.runtime.cloning.ThreadsafeCopyMaker) ImmutabilityChecker(ru.fix.completable.reactor.runtime.immutability.ImmutabilityChecker) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) CRProcessorDescription(ru.fix.completable.reactor.runtime.internal.dsl.CRProcessorDescription) Slf4j(lombok.extern.slf4j.Slf4j) ReactorGraph(ru.fix.completable.reactor.runtime.ReactorGraph) ImmutabilityControlLevel(ru.fix.completable.reactor.runtime.immutability.ImmutabilityControlLevel) DebugSerializer(ru.fix.completable.reactor.runtime.debug.DebugSerializer) Tracer(ru.fix.completable.reactor.runtime.tracing.Tracer) CRReactorGraph(ru.fix.completable.reactor.runtime.internal.CRReactorGraph) ProfilerNames(ru.fix.completable.reactor.runtime.ProfilerNames) Data(lombok.Data) Profiler(ru.fix.commons.profiler.Profiler) CompletableFuture(java.util.concurrent.CompletableFuture) lombok.val(lombok.val) CRReactorGraph(ru.fix.completable.reactor.runtime.internal.CRReactorGraph) CRProcessingItem(ru.fix.completable.reactor.runtime.internal.CRProcessingItem)

Aggregations

CompletableFuture (java.util.concurrent.CompletableFuture)9 Data (lombok.Data)9 lombok.val (lombok.val)9 ReactorGraph (ru.fix.completable.reactor.runtime.ReactorGraph)9 TimeUnit (java.util.concurrent.TimeUnit)8 Before (org.junit.Before)8 Test (org.junit.Test)8 SimpleProfiler (ru.fix.commons.profiler.impl.SimpleProfiler)8 Reactored (ru.fix.completable.reactor.api.Reactored)8 CompletableReactor (ru.fix.completable.reactor.runtime.CompletableReactor)8 ReactorGraphBuilder (ru.fix.completable.reactor.runtime.ReactorGraphBuilder)8 Processor (ru.fix.completable.reactor.runtime.dsl.Processor)8 Assert.assertEquals (org.junit.Assert.assertEquals)7 BigInteger (java.math.BigInteger)6 Accessors (lombok.experimental.Accessors)3 Slf4j (lombok.extern.slf4j.Slf4j)2 Profiler (ru.fix.commons.profiler.Profiler)2 Subgraph (ru.fix.completable.reactor.runtime.dsl.Subgraph)2 java.util (java.util)1 ArrayList (java.util.ArrayList)1