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