Search in sources :

Example 1 with GraphFetchExecutionNode

use of org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.graphFetch.GraphFetchExecutionNode in project legend-engine by finos.

the class ExecutionNodeExecutor method visit.

@Deprecated
@Override
public Result visit(GraphFetchExecutionNode graphFetchExecutionNode) {
    final Span topSpan = GlobalTracer.get().activeSpan();
    try (Scope ignored1 = GlobalTracer.get().buildSpan("Graph Query: Execute").startActive(true)) {
        Result rootResult;
        try (Scope ignored2 = GlobalTracer.get().buildSpan("Graph Query: Execute Root").startActive(true)) {
            rootResult = graphFetchExecutionNode.rootExecutionNode.accept(new ExecutionNodeExecutor(profiles, executionState));
        }
        if (graphFetchExecutionNode.implementation != null) {
            if (!(rootResult instanceof StreamingObjectResult)) {
                throw new RuntimeException("Unexpected result : " + rootResult.getClass().getName());
            }
            StreamingObjectResult<?> objectResult = (StreamingObjectResult<?>) rootResult;
            try {
                if (!(graphFetchExecutionNode.implementation instanceof JavaPlatformImplementation)) {
                    throw new RuntimeException("Only Java implementations are currently supported, found: " + graphFetchExecutionNode.implementation);
                }
                JavaPlatformImplementation javaPlatformImpl = (JavaPlatformImplementation) graphFetchExecutionNode.implementation;
                String executionClassName = JavaHelper.getExecutionClassFullName(javaPlatformImpl);
                String executionMethodName = JavaHelper.getExecutionMethodName(javaPlatformImpl);
                Stream<?> transformedResult = ExecutionNodeJavaPlatformHelper.executeStaticJavaMethod(graphFetchExecutionNode, executionClassName, executionMethodName, Arrays.asList(StreamingObjectResult.class, ExecutionNode.class, ExecutionState.class, ProfileManager.class), Arrays.asList(objectResult, graphFetchExecutionNode, this.executionState, this.profiles), this.executionState, this.profiles);
                return new StreamingObjectResult<>(transformedResult, objectResult.getResultBuilder(), objectResult);
            } catch (Exception e) {
                objectResult.close();
                throw e;
            }
        } else {
            final long maxMemoryBytesForGraph = 52_428_800L;
            /* 50MB - 50 * 1024 * 1024 */
            final int batchSize = graphFetchExecutionNode.batchSize;
            final GlobalGraphFetchExecutionNode rootGlobalNode = graphFetchExecutionNode.globalGraphFetchExecutionNode;
            final LocalGraphFetchExecutionNode rootLocalNode = rootGlobalNode.localGraphFetchExecutionNode;
            final AtomicInteger batchIndex = new AtomicInteger(1);
            final AtomicLong rowCount = new AtomicLong(0);
            try {
                Stream<List<?>> batchedStoreLocalObjectStream = StreamSupport.stream(new Spliterators.AbstractSpliterator<List<?>>(Long.MAX_VALUE, Spliterator.ORDERED) {

                    @Override
                    public boolean tryAdvance(Consumer<? super List<?>> action) {
                        int currentBatch = batchIndex.getAndIncrement();
                        try (Scope scope = GlobalTracer.get().buildSpan("Graph Query: Execute Batch " + currentBatch).startActive(true)) {
                            GraphExecutionState graphExecutionState = new GraphExecutionState(executionState, batchSize, rootResult, maxMemoryBytesForGraph);
                            ConstantResult constantResult = (ConstantResult) rootLocalNode.accept(new ExecutionNodeExecutor(ExecutionNodeExecutor.this.profiles, graphExecutionState));
                            List<?> objects = (List<?>) constantResult.getValue();
                            boolean nonEmptyObjectList = !objects.isEmpty();
                            if (rootGlobalNode.children != null && (rootGlobalNode.children.size() > 0) && nonEmptyObjectList) {
                                try (Scope ignored3 = GlobalTracer.get().buildSpan("Graph Query: Execute Cross Store Children").startActive(true)) {
                                    for (GlobalGraphFetchExecutionNode crossChild : rootGlobalNode.children) {
                                        try (Scope ignored4 = GlobalTracer.get().buildSpan("Graph Query: Execute Cross Store Child").startActive(true)) {
                                            graphExecutionState.setObjectsToGraphFetch(objects);
                                            ExecutionNodeExecutor.this.executeGlobalGraphOperation(crossChild, graphExecutionState);
                                        }
                                    }
                                }
                            }
                            rowCount.addAndGet(graphExecutionState.getRowCount());
                            action.accept(objects);
                            if (nonEmptyObjectList) {
                                scope.span().setTag("batchObjectCount", objects.size());
                                scope.span().setTag("batchMemoryUtilization", graphExecutionState.getTotalObjectMemoryUtilization());
                            } else {
                                if (topSpan != null) {
                                    topSpan.setTag("lastQueryRowCount", rowCount);
                                }
                            }
                            return nonEmptyObjectList && (objects.size() >= batchSize);
                        }
                    }
                }, false);
                Stream<?> globalObjectStream = batchedStoreLocalObjectStream.flatMap(Collection::stream);
                return new StreamingObjectResult<>(globalObjectStream, new PartialClassBuilder(graphFetchExecutionNode), rootResult);
            } catch (Exception e) {
                rootResult.close();
                throw e;
            }
        }
    }
}
Also used : ProfileManager(org.pac4j.core.profile.ProfileManager) GraphExecutionState(org.finos.legend.engine.plan.execution.nodes.state.GraphExecutionState) ExecutionState(org.finos.legend.engine.plan.execution.nodes.state.ExecutionState) PartialClassBuilder(org.finos.legend.engine.plan.execution.result.builder._class.PartialClassBuilder) LocalGraphFetchExecutionNode(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.graphFetch.LocalGraphFetchExecutionNode) InMemoryCrossStoreGraphFetchExecutionNode(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.graphFetch.store.inMemory.InMemoryCrossStoreGraphFetchExecutionNode) ErrorExecutionNode(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.ErrorExecutionNode) StoreStreamReadingExecutionNode(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.graphFetch.store.inMemory.StoreStreamReadingExecutionNode) ExecutionNode(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.ExecutionNode) AllocationExecutionNode(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.AllocationExecutionNode) ConstantExecutionNode(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.ConstantExecutionNode) InMemoryRootGraphFetchExecutionNode(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.graphFetch.store.inMemory.InMemoryRootGraphFetchExecutionNode) FreeMarkerConditionalExecutionNode(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.FreeMarkerConditionalExecutionNode) MultiResultSequenceExecutionNode(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.MultiResultSequenceExecutionNode) AggregationAwareExecutionNode(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.AggregationAwareExecutionNode) GraphFetchM2MExecutionNode(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.GraphFetchM2MExecutionNode) PureExpressionPlatformExecutionNode(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.PureExpressionPlatformExecutionNode) GraphFetchExecutionNode(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.graphFetch.GraphFetchExecutionNode) SequenceExecutionNode(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.SequenceExecutionNode) InMemoryPropertyGraphFetchExecutionNode(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.graphFetch.store.inMemory.InMemoryPropertyGraphFetchExecutionNode) GlobalGraphFetchExecutionNode(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.graphFetch.GlobalGraphFetchExecutionNode) LocalGraphFetchExecutionNode(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.graphFetch.LocalGraphFetchExecutionNode) Span(io.opentracing.Span) ConstantResult(org.finos.legend.engine.plan.execution.result.ConstantResult) GraphFetchResult(org.finos.legend.engine.plan.execution.result.graphFetch.GraphFetchResult) StreamingObjectResult(org.finos.legend.engine.plan.execution.result.object.StreamingObjectResult) ErrorResult(org.finos.legend.engine.plan.execution.result.ErrorResult) Result(org.finos.legend.engine.plan.execution.result.Result) MultiResult(org.finos.legend.engine.plan.execution.result.MultiResult) JavaPlatformImplementation(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.JavaPlatformImplementation) MutableList(org.eclipse.collections.api.list.MutableList) List(java.util.List) GraphExecutionState(org.finos.legend.engine.plan.execution.nodes.state.GraphExecutionState) StreamingObjectResult(org.finos.legend.engine.plan.execution.result.object.StreamingObjectResult) ConstantResult(org.finos.legend.engine.plan.execution.result.ConstantResult) Spliterators(java.util.Spliterators) AtomicLong(java.util.concurrent.atomic.AtomicLong) Scope(io.opentracing.Scope) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Collection(java.util.Collection) GlobalGraphFetchExecutionNode(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.graphFetch.GlobalGraphFetchExecutionNode)

Aggregations

Scope (io.opentracing.Scope)1 Span (io.opentracing.Span)1 Collection (java.util.Collection)1 List (java.util.List)1 Spliterators (java.util.Spliterators)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 MutableList (org.eclipse.collections.api.list.MutableList)1 ExecutionState (org.finos.legend.engine.plan.execution.nodes.state.ExecutionState)1 GraphExecutionState (org.finos.legend.engine.plan.execution.nodes.state.GraphExecutionState)1 ConstantResult (org.finos.legend.engine.plan.execution.result.ConstantResult)1 ErrorResult (org.finos.legend.engine.plan.execution.result.ErrorResult)1 MultiResult (org.finos.legend.engine.plan.execution.result.MultiResult)1 Result (org.finos.legend.engine.plan.execution.result.Result)1 PartialClassBuilder (org.finos.legend.engine.plan.execution.result.builder._class.PartialClassBuilder)1 GraphFetchResult (org.finos.legend.engine.plan.execution.result.graphFetch.GraphFetchResult)1 StreamingObjectResult (org.finos.legend.engine.plan.execution.result.object.StreamingObjectResult)1 AggregationAwareExecutionNode (org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.AggregationAwareExecutionNode)1 AllocationExecutionNode (org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.AllocationExecutionNode)1 ConstantExecutionNode (org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.ConstantExecutionNode)1