Search in sources :

Example 1 with JavaPlatformImplementation

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

the class ExecutionNodeExecutor method visit.

@Override
public Result visit(PureExpressionPlatformExecutionNode pureExpressionPlatformExecutionNode) {
    if (!(pureExpressionPlatformExecutionNode.implementation instanceof JavaPlatformImplementation)) {
        throw new RuntimeException("Only Java implementations are currently supported, found: " + pureExpressionPlatformExecutionNode.implementation);
    }
    JavaPlatformImplementation javaPlatformImpl = (JavaPlatformImplementation) pureExpressionPlatformExecutionNode.implementation;
    String executionClassName = JavaHelper.getExecutionClassFullName(javaPlatformImpl);
    Class<?> clazz = ExecutionNodeJavaPlatformHelper.getClassToExecute(pureExpressionPlatformExecutionNode, executionClassName, this.executionState, this.profiles);
    if (Arrays.asList(clazz.getInterfaces()).contains(IPlatformPureExpressionExecutionNodeSerializeSpecifics.class)) {
        try {
            org.finos.legend.engine.plan.dependencies.store.platform.IPlatformPureExpressionExecutionNodeSerializeSpecifics nodeSpecifics = (org.finos.legend.engine.plan.dependencies.store.platform.IPlatformPureExpressionExecutionNodeSerializeSpecifics) clazz.newInstance();
            Result childResult = pureExpressionPlatformExecutionNode.executionNodes().getFirst().accept(new ExecutionNodeExecutor(profiles, executionState));
            IExecutionNodeContext context = new DefaultExecutionNodeContext(this.executionState, childResult);
            AppliedFunction f = (AppliedFunction) pureExpressionPlatformExecutionNode.pure;
            SerializationConfig config = f.parameters.size() == 3 ? (SerializationConfig) f.parameters.get(2) : null;
            return ExecutionNodeSerializerHelper.executeSerialize(nodeSpecifics, config, childResult, context);
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    if (Arrays.asList(clazz.getInterfaces()).contains(IPlatformPureExpressionExecutionNodeGraphFetchUnionSpecifics.class)) {
        StreamingObjectResult<?> streamResult1 = (StreamingObjectResult) pureExpressionPlatformExecutionNode.executionNodes.get(0).accept(new ExecutionNodeExecutor(this.profiles, this.executionState));
        StreamingObjectResult<?> streamResult2 = (StreamingObjectResult) pureExpressionPlatformExecutionNode.executionNodes.get(1).accept(new ExecutionNodeExecutor(this.profiles, this.executionState));
        Result childResult = new Result("success") {

            @Override
            public <T> T accept(ResultVisitor<T> resultVisitor) {
                throw new RuntimeException("Not implemented");
            }

            @Override
            public void close() {
                streamResult1.close();
                streamResult2.close();
            }
        };
        return new StreamingObjectResult<>(Stream.concat(streamResult1.getObjectStream(), streamResult2.getObjectStream()), streamResult1.getResultBuilder(), childResult);
    }
    if (Arrays.asList(clazz.getInterfaces()).contains(IPlatformPureExpressionExecutionNodeGraphFetchMergeSpecifics.class)) {
        StreamingObjectResult<?> streamResult = (StreamingObjectResult) pureExpressionPlatformExecutionNode.executionNodes.get(0).accept(new ExecutionNodeExecutor(this.profiles, this.executionState));
        return streamResult;
    } else {
        return ExecutionNodeJavaPlatformHelper.executeJavaImplementation(pureExpressionPlatformExecutionNode, DefaultExecutionNodeContext.factory(), this.profiles, this.executionState);
    }
}
Also used : 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) IPlatformPureExpressionExecutionNodeSerializeSpecifics(org.finos.legend.engine.plan.dependencies.store.platform.IPlatformPureExpressionExecutionNodeSerializeSpecifics) JavaPlatformImplementation(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.JavaPlatformImplementation) SerializationConfig(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.SerializationConfig) StreamingObjectResult(org.finos.legend.engine.plan.execution.result.object.StreamingObjectResult) IPlatformPureExpressionExecutionNodeSerializeSpecifics(org.finos.legend.engine.plan.dependencies.store.platform.IPlatformPureExpressionExecutionNodeSerializeSpecifics) IExecutionNodeContext(org.finos.legend.engine.plan.dependencies.store.shared.IExecutionNodeContext) DefaultExecutionNodeContext(org.finos.legend.engine.plan.execution.nodes.helpers.platform.DefaultExecutionNodeContext) AppliedFunction(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.application.AppliedFunction) ResultVisitor(org.finos.legend.engine.plan.execution.result.ResultVisitor)

Example 2 with JavaPlatformImplementation

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

the class ExecutionNodeJavaPlatformHelper method getClassToExecute.

public static Class<?> getClassToExecute(ExecutionNode node, String _class, ExecutionState executionState, MutableList<CommonProfile> pm) {
    if (executionState.isJavaCompilationForbidden()) {
        try {
            return Thread.currentThread().getContextClassLoader().loadClass(_class);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    JavaPlatformImplementation j = (JavaPlatformImplementation) node.implementation;
    List<JavaClass> javaClasses = FastList.newList();
    if (j.code != null) {
        JavaClass executeClass = JavaHelper.newJavaClass(JavaHelper.getExecutionClassFullName(j));
        executeClass.source = j.code;
        javaClasses.add(executeClass);
    }
    javaClasses.addAll(getLocalImplementationSupportClasses(node));
    if (javaClasses.isEmpty()) {
        ClassLoader classLoader = executionState.hasJavaCompiler() ? executionState.getJavaCompiler().getClassLoader() : Thread.currentThread().getContextClassLoader();
        try {
            return classLoader.loadClass(_class);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    try {
        // TODO Confirm we can delete this
        MutableMap<String, String> compiledClassesWithByteCode = getCompiledClasses(node);
        List<StringJavaSource> classesToCompile = javaClasses.stream().filter(c -> !compiledClassesWithByteCode.containsKey(JavaHelper.getJavaClassFullName(c))).map(JavaHelper::buildStringJavaSource).collect(Collectors.toList());
        EngineJavaCompiler compiler = new EngineJavaCompiler(executionState.getJavaCompiler());
        long start = System.currentTimeMillis();
        try {
            if (!compiledClassesWithByteCode.isEmpty()) {
                compiler.load(compiledClassesWithByteCode);
            }
            if (!classesToCompile.isEmpty()) {
                LOGGER.info(new LogInfo(pm, LoggingEventType.JAVA_COMPILATION_START, "Node: " + node.getClass().getName()).toString());
                compiler.compile(classesToCompile);
                LOGGER.info(new LogInfo(pm, LoggingEventType.JAVA_COMPILATION_STOP, (double) System.currentTimeMillis() - start).toString());
            }
        } catch (Exception jce) {
            LOGGER.info(new LogInfo(pm, LoggingEventType.JAVA_COMPILATION_ERROR, new ErrorResult(1, jce).getMessage()).toString());
            throw jce;
        }
        return compiler.getClassLoader().loadClass(_class);
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : LogInfo(org.finos.legend.engine.shared.core.operational.logs.LogInfo) EngineJavaCompiler(org.finos.legend.engine.shared.javaCompiler.EngineJavaCompiler) ErrorResult(org.finos.legend.engine.plan.execution.result.ErrorResult) InvocationTargetException(java.lang.reflect.InvocationTargetException) JavaClass(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.JavaClass) JavaPlatformImplementation(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.JavaPlatformImplementation) StringJavaSource(org.finos.legend.engine.shared.javaCompiler.StringJavaSource)

Example 3 with JavaPlatformImplementation

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

the class JavaHelper method compilePlanFast.

private static EngineJavaCompiler compilePlanFast(SingleExecutionPlan singleExecutionPlan) throws JavaCompileException, IOException, CompileException {
    Map<JavaPlatformImplementation, List<JavaClass>> javaClassesMap = Maps.mutable.empty();
    if (singleExecutionPlan.globalImplementationSupport != null) {
        JavaPlatformImplementation platformImplementation = (JavaPlatformImplementation) singleExecutionPlan.globalImplementationSupport;
        List<JavaClass> globalImplementationSupportClasses = collectJavaClasses(platformImplementation);
        if (!globalImplementationSupportClasses.isEmpty()) {
            javaClassesMap.put(platformImplementation, globalImplementationSupportClasses);
        }
    }
    collectJavaClasses(singleExecutionPlan.rootExecutionNode, javaClassesMap);
    if (javaClassesMap.isEmpty()) {
        return null;
    }
    EngineJavaCompiler javaCompiler = createNewJavaCompiler();
    Map<JavaClass, JavaPlatformImplementation> reverseClassMap = Maps.mutable.empty();
    List<JavaClass> executeClasses = new ArrayList<>();
    List<JavaClass> nonExecuteClasses = new ArrayList<>();
    javaClassesMap.forEach((jimpl, jclasses) -> jclasses.forEach(jclass -> {
        reverseClassMap.put(jclass, jimpl);
        (javaClassHasFullName(jclass, jimpl.executionClassFullName) ? executeClasses : nonExecuteClasses).add(jclass);
    }));
    Map<String, JavaClass> classMap = nonExecuteClasses.stream().collect(Collectors.toMap(JavaHelper::getJavaClassFullName, x -> x, (x, y) -> {
        throw new RuntimeException("Conflicting classes compilation in fast mode not supported!");
    }));
    Map<String, String> classToBytecodeMap = compileJavaClasses(nonExecuteClasses, javaCompiler);
    classToBytecodeMap.forEach((name, bytecode) -> {
        JavaClass _class = classMap.get(name);
        if (_class == null) {
            // Handling Inner Classes only
            int firstDollar = name.indexOf('$');
            if (firstDollar == -1) {
                throw new RuntimeException("Non Inner classes are not supported yet!");
            }
            JavaClass topClass = classMap.get(name.substring(0, firstDollar));
            JavaPlatformImplementation impl = reverseClassMap.get(topClass);
            _class = createGeneratedJavaClass(name);
            if (impl.classes == null) {
                impl.classes = FastList.newListWith(_class);
            } else {
                impl.classes.add(_class);
            }
        }
        _class.byteCode = bytecode;
    });
    ClassLoader globalClassLoader = javaCompiler.getClassLoader();
    for (JavaClass executeClass : executeClasses) {
        if (executeClass.byteCode == null) {
            Map<String, byte[]> classes = SingleFileCompiler.compileFile(buildStringJavaSource(executeClass), globalClassLoader);
            executeClass.byteCode = Base64.getEncoder().encodeToString(classes.get(getJavaClassFullName(executeClass)));
        }
    }
    return javaCompiler;
}
Also used : JavaClass(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.JavaClass) LoggingEventType(org.finos.legend.engine.shared.core.operational.logs.LoggingEventType) SingleFileCompiler(org.finos.legend.engine.shared.javaCompiler.SingleFileCompiler) CommonProfile(org.pac4j.core.profile.CommonProfile) ExecutionPlanDependenciesFilter(org.finos.legend.engine.plan.compilation.ExecutionPlanDependenciesFilter) ClassPathFilter(org.finos.legend.engine.shared.javaCompiler.ClassPathFilter) CompositeClassPathFilter(org.finos.legend.engine.shared.javaCompiler.CompositeClassPathFilter) MutableList(org.eclipse.collections.api.list.MutableList) FastList(org.eclipse.collections.impl.list.mutable.FastList) ArrayList(java.util.ArrayList) Maps(org.eclipse.collections.api.factory.Maps) CompileException(org.codehaus.commons.compiler.CompileException) Map(java.util.Map) ExecutionNode(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.ExecutionNode) JavaPlatformImplementation(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.JavaPlatformImplementation) ErrorResult(org.finos.legend.engine.plan.execution.result.ErrorResult) UnifiedMap(org.eclipse.collections.impl.map.mutable.UnifiedMap) Logger(org.slf4j.Logger) EngineJavaCompiler(org.finos.legend.engine.shared.javaCompiler.EngineJavaCompiler) SingleExecutionPlan(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.SingleExecutionPlan) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) LogInfo(org.finos.legend.engine.shared.core.operational.logs.LogInfo) JavaVersion(org.finos.legend.engine.shared.javaCompiler.JavaVersion) Base64(java.util.Base64) List(java.util.List) JavaCompileException(org.finos.legend.engine.shared.javaCompiler.JavaCompileException) Lists(org.eclipse.collections.impl.factory.Lists) StringJavaSource(org.finos.legend.engine.shared.javaCompiler.StringJavaSource) CompiledClass(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.CompiledClass) Collections(java.util.Collections) EngineJavaCompiler(org.finos.legend.engine.shared.javaCompiler.EngineJavaCompiler) ArrayList(java.util.ArrayList) JavaClass(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.JavaClass) JavaPlatformImplementation(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.JavaPlatformImplementation) MutableList(org.eclipse.collections.api.list.MutableList) FastList(org.eclipse.collections.impl.list.mutable.FastList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with JavaPlatformImplementation

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

the class JavaHelper method compilePlanSlow.

private static EngineJavaCompiler compilePlanSlow(SingleExecutionPlan singleExecutionPlan) throws JavaCompileException {
    EngineJavaCompiler javaCompiler = createNewJavaCompiler();
    if (singleExecutionPlan.globalImplementationSupport != null) {
        JavaPlatformImplementation platformImplementation = (JavaPlatformImplementation) singleExecutionPlan.globalImplementationSupport;
        compileImplementation(platformImplementation, javaCompiler);
    }
    compileNode(singleExecutionPlan.rootExecutionNode, javaCompiler);
    return javaCompiler;
}
Also used : EngineJavaCompiler(org.finos.legend.engine.shared.javaCompiler.EngineJavaCompiler) JavaPlatformImplementation(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.JavaPlatformImplementation)

Example 5 with JavaPlatformImplementation

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

the class JavaHelper method compileNode.

private static void compileNode(ExecutionNode executionNode, EngineJavaCompiler globalJavaCompiler) throws JavaCompileException {
    if (executionNode.implementation != null) {
        JavaPlatformImplementation platformImplementation = (JavaPlatformImplementation) executionNode.implementation;
        if (platformImplementation.classes != null && !platformImplementation.classes.isEmpty()) {
            EngineJavaCompiler javaCompiler = new EngineJavaCompiler(globalJavaCompiler);
            JavaHelper.compileImplementation(platformImplementation, javaCompiler);
        }
    }
    for (ExecutionNode childNode : executionNode.executionNodes()) {
        compileNode(childNode, globalJavaCompiler);
    }
}
Also used : EngineJavaCompiler(org.finos.legend.engine.shared.javaCompiler.EngineJavaCompiler) JavaPlatformImplementation(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.JavaPlatformImplementation) ExecutionNode(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.ExecutionNode)

Aggregations

JavaPlatformImplementation (org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.JavaPlatformImplementation)10 Result (org.finos.legend.engine.plan.execution.result.Result)6 StreamingObjectResult (org.finos.legend.engine.plan.execution.result.object.StreamingObjectResult)6 JavaClass (org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.JavaClass)6 MutableList (org.eclipse.collections.api.list.MutableList)5 ConstantResult (org.finos.legend.engine.plan.execution.result.ConstantResult)5 ErrorResult (org.finos.legend.engine.plan.execution.result.ErrorResult)5 ExecutionNode (org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.ExecutionNode)5 ExecutionNodeExecutor (org.finos.legend.engine.plan.execution.nodes.ExecutionNodeExecutor)4 GraphFetchResult (org.finos.legend.engine.plan.execution.result.graphFetch.GraphFetchResult)4 EngineJavaCompiler (org.finos.legend.engine.shared.javaCompiler.EngineJavaCompiler)4 Span (io.opentracing.Span)3 List (java.util.List)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 Collectors (java.util.stream.Collectors)3 FastList (org.eclipse.collections.impl.list.mutable.FastList)3 DefaultExecutionNodeContext (org.finos.legend.engine.plan.execution.nodes.helpers.platform.DefaultExecutionNodeContext)3 CompiledClass (org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.CompiledClass)3 Scope (io.opentracing.Scope)2 IOException (java.io.IOException)2