Search in sources :

Example 1 with GremlinExecutor

use of org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutor in project incubator-hugegraph by apache.

the class ContextGremlinServer method removeGraph.

private void removeGraph(String name) {
    GraphManager manager = this.getServerGremlinExecutor().getGraphManager();
    GremlinExecutor executor = this.getServerGremlinExecutor().getGremlinExecutor();
    try {
        manager.removeGraph(name);
        manager.removeTraversalSource(G_PREFIX + name);
        Whitebox.invoke(executor, "globalBindings", new Class<?>[] { Object.class }, "remove", name);
    } catch (Exception e) {
        throw new HugeException("Failed to remove graph '%s' from " + "gremlin server context", e, name);
    }
}
Also used : GraphManager(org.apache.tinkerpop.gremlin.server.GraphManager) GremlinExecutor(org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutor) HugeException(com.baidu.hugegraph.HugeException) HugeException(com.baidu.hugegraph.HugeException)

Example 2 with GremlinExecutor

use of org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutor in project incubator-hugegraph by apache.

the class ContextGremlinServer method injectGraph.

private void injectGraph(HugeGraph graph) {
    String name = graph.name();
    GraphManager manager = this.getServerGremlinExecutor().getGraphManager();
    GremlinExecutor executor = this.getServerGremlinExecutor().getGremlinExecutor();
    manager.putGraph(name, graph);
    GraphTraversalSource g = manager.getGraph(name).traversal();
    manager.putTraversalSource(G_PREFIX + name, g);
    Whitebox.invoke(executor, "globalBindings", new Class<?>[] { String.class, Object.class }, "put", name, graph);
}
Also used : GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) GraphManager(org.apache.tinkerpop.gremlin.server.GraphManager) GremlinExecutor(org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutor)

Example 3 with GremlinExecutor

use of org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutor in project act-platform by mnemonic-no.

the class TraverseGraphDelegate method executeTraversal.

private void executeTraversal(Collection<UUID> startingObjects, String query) throws InvalidArgumentException, OperationTimeoutException {
    try (Graph graph = createGraph();
        GremlinExecutor executor = createExecutor()) {
        // Create the first step of the graph traversal, i.e. starting the traversal at the Object(s) specified in the request.
        // This is injected into the script execution as variable 'g'. Every query has to start from 'g'.
        GraphTraversal<Vertex, Vertex> startingPoint = graph.traversal().V(startingObjects.toArray());
        Map<String, java.lang.Object> bindings = MapUtils.map(T("g", startingPoint));
        // Start script execution and wait until result arrived or execution is aborted.
        // Use 'withResult' callback here because the graph will then be iterated inside the 'eval' thread, thus, every
        // exception caused by the traversal will be handled inside that thread as well which will result in an ExecutionException.
        executor.eval(query, SCRIPT_ENGINE, bindings, this::produceTraversalResult).get();
    } catch (ExecutionException ex) {
        // Exceptions causing the script execution to fail are wrapped inside an ExecutionException. Need to unwrap them.
        Throwable cause = ObjectUtils.ifNull(ex.getCause(), ex);
        // In both cases throw an own OperationTimeoutException in order to signal the timeout to the user.
        if (cause instanceof TimeoutException) {
            throw new OperationTimeoutException("The performed graph traversal query timed out.", "graph.traversal.timeout");
        }
        // e.g. invalid syntax, an unsupported operation such as 'addE()', or an operation not allowed by the sandbox.
        throw new InvalidArgumentException().addValidationError(cause.getMessage(), "graph.traversal.failure", "query", query);
    } catch (Exception ex) {
        // Something bad happened, abort method.
        throw new IllegalStateException("Could not perform graph traversal.", ex);
    }
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) ObjectVertex(no.mnemonic.act.platform.dao.tinkerpop.ObjectVertex) OperationTimeoutException(no.mnemonic.act.platform.api.exceptions.OperationTimeoutException) GremlinExecutor(org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutor) TimeoutException(java.util.concurrent.TimeoutException) InvalidArgumentException(no.mnemonic.act.platform.api.exceptions.InvalidArgumentException) AccessDeniedException(no.mnemonic.act.platform.api.exceptions.AccessDeniedException) AuthenticationFailedException(no.mnemonic.act.platform.api.exceptions.AuthenticationFailedException) ExecutionException(java.util.concurrent.ExecutionException) OperationTimeoutException(no.mnemonic.act.platform.api.exceptions.OperationTimeoutException) Graph(org.apache.tinkerpop.gremlin.structure.Graph) ActGraph(no.mnemonic.act.platform.dao.tinkerpop.ActGraph) InvalidArgumentException(no.mnemonic.act.platform.api.exceptions.InvalidArgumentException) Object(no.mnemonic.act.platform.api.model.v1.Object) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) OperationTimeoutException(no.mnemonic.act.platform.api.exceptions.OperationTimeoutException)

Example 4 with GremlinExecutor

use of org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutor in project act-platform by mnemonic-no.

the class TraverseGraphHandler method executeTraversal.

private Collection<Object> executeTraversal(Collection<UUID> startingObjects, String query, TraverseParams traverseParams) throws InvalidArgumentException, OperationTimeoutException {
    // The result will be written into this collection.
    Collection<Object> traversalResult = new ArrayList<>();
    try (Graph graph = createGraph(traverseParams);
        GremlinExecutor executor = createExecutor()) {
        // Create the first step of the graph traversal, i.e. starting the traversal at the Object(s) specified in the request.
        // This is injected into the script execution as variable 'g'. Every query has to start from 'g'.
        GraphTraversal<Vertex, Vertex> startingPoint = graph.traversal().V(startingObjects.toArray());
        Map<String, Object> bindings = MapUtils.map(T("g", startingPoint));
        // Start script execution and wait until result arrived or execution is aborted.
        // Use 'withResult' callback here because the graph will then be iterated inside the 'eval' thread, thus, every
        // exception caused by the traversal will be handled inside that thread as well which will result in an ExecutionException.
        executor.eval(query, SCRIPT_ENGINE, bindings, createResultConsumer(traversalResult, traverseParams)).get();
    } catch (ExecutionException ex) {
        // Exceptions causing the script execution to fail are wrapped inside an ExecutionException. Need to unwrap them.
        Throwable cause = ObjectUtils.ifNull(ex.getCause(), ex);
        // In both cases throw an own OperationTimeoutException in order to signal the timeout to the user.
        if (cause instanceof TimeoutException) {
            throw new OperationTimeoutException("The performed graph traversal query timed out.", "graph.traversal.timeout");
        }
        // e.g. invalid syntax, an unsupported operation such as 'addE()', or an operation not allowed by the sandbox.
        throw new InvalidArgumentException().addValidationError(cause.getMessage(), "graph.traversal.failure", "query", query);
    } catch (Exception ex) {
        // Something bad happened, abort method.
        throw new IllegalStateException("Could not perform graph traversal.", ex);
    }
    return traversalResult;
}
Also used : ObjectVertex(no.mnemonic.act.platform.service.ti.tinkerpop.ObjectVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) OperationTimeoutException(no.mnemonic.act.platform.api.exceptions.OperationTimeoutException) GremlinExecutor(org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutor) TimeoutException(java.util.concurrent.TimeoutException) InvalidArgumentException(no.mnemonic.act.platform.api.exceptions.InvalidArgumentException) ExecutionException(java.util.concurrent.ExecutionException) OperationTimeoutException(no.mnemonic.act.platform.api.exceptions.OperationTimeoutException) Graph(org.apache.tinkerpop.gremlin.structure.Graph) ActGraph(no.mnemonic.act.platform.service.ti.tinkerpop.ActGraph) InvalidArgumentException(no.mnemonic.act.platform.api.exceptions.InvalidArgumentException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) OperationTimeoutException(no.mnemonic.act.platform.api.exceptions.OperationTimeoutException)

Example 5 with GremlinExecutor

use of org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutor in project GraphScope by alibaba.

the class GaiaGraphOpProcessor method createLifeCycle.

@Override
protected GremlinExecutor.LifeCycle createLifeCycle(Context ctx, Supplier<GremlinExecutor> gremlinExecutorSupplier, BindingSupplier bindingsSupplier) {
    final RequestMessage msg = ctx.getRequestMessage();
    final Settings settings = ctx.getSettings();
    final Map<String, Object> args = msg.getArgs();
    final long seto = args.containsKey(Tokens.ARGS_SCRIPT_EVAL_TIMEOUT) ? ((Number) args.get(Tokens.ARGS_SCRIPT_EVAL_TIMEOUT)).longValue() : settings.scriptEvaluationTimeout;
    if (config.getGraphType() == GraphType.MAXGRAPH) {
        graphStore.updateSnapShotId();
    }
    return GremlinExecutor.LifeCycle.build().scriptEvaluationTimeoutOverride(seto).beforeEval(b -> {
        try {
            b.putAll(bindingsSupplier.get());
        } catch (OpProcessorException ope) {
            throw new RuntimeException(ope);
        }
    }).transformResult(o -> {
        if (o != null && o instanceof Traversal) {
            applyStrategy((Traversal) o, config, graphStore);
        }
        return o;
    }).withResult(o -> {
        if (o != null && o instanceof Traversal) {
            long queryId = (long) queryIdMaker.getId(o);
            TraversalBuilder traversalBuilder = new TraversalBuilder((Traversal.Admin) o).addConfig(PlanConfig.QUERY_ID, queryId).addConfig(PlanConfig.TAG_ID_MAKER, new TagIdMaker((Traversal.Admin) o)).addConfig(PlanConfig.QUERY_CONFIG, PlanUtils.getDefaultConfig(queryId, config));
            if (config.getGraphType() == GraphType.MAXGRAPH) {
                traversalBuilder.addConfig(PlanConfig.SNAPSHOT_ID, Long.valueOf(graphStore.getSnapShotId()));
            }
            AbstractBuilder jobReqBuilder = new TraversalTranslator(traversalBuilder).translate();
            PlanUtils.print(jobReqBuilder);
            broadcastProcessor.broadcast(jobReqBuilder.build(), new GremlinResultProcessor(ctx, new DefaultResultParser(traversalBuilder, graphStore, config)));
        } else {
            List<Object> results = new ArrayList<>();
            if (o != null) {
                results.add(o);
            }
            writeResultList(ctx, results, ResponseStatusCode.SUCCESS);
        }
    }).create();
}
Also used : TraversalBuilder(com.alibaba.graphscope.gaia.plan.translator.builder.TraversalBuilder) AbstractBroadcastProcessor(com.alibaba.graphscope.gaia.broadcast.AbstractBroadcastProcessor) TraversalTranslator(com.alibaba.graphscope.gaia.plan.translator.TraversalTranslator) Settings(org.apache.tinkerpop.gremlin.server.Settings) Tokens(org.apache.tinkerpop.gremlin.driver.Tokens) LoggerFactory(org.slf4j.LoggerFactory) DefaultResultParser(com.alibaba.graphscope.gaia.result.DefaultResultParser) Supplier(java.util.function.Supplier) RequestMessage(org.apache.tinkerpop.gremlin.driver.message.RequestMessage) ArrayList(java.util.ArrayList) GremlinResultProcessor(com.alibaba.graphscope.gaia.result.GremlinResultProcessor) Map(java.util.Map) OpProcessorException(org.apache.tinkerpop.gremlin.server.op.OpProcessorException) GraphStoreService(com.alibaba.graphscope.gaia.store.GraphStoreService) Logger(org.slf4j.Logger) GaiaConfig(com.alibaba.graphscope.gaia.config.GaiaConfig) PlanUtils(com.alibaba.graphscope.gaia.plan.PlanUtils) AbstractBuilder(com.alibaba.pegasus.builder.AbstractBuilder) List(java.util.List) TagIdMaker(com.alibaba.graphscope.gaia.idmaker.TagIdMaker) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) PlanConfig(com.alibaba.graphscope.gaia.plan.translator.builder.PlanConfig) ResponseStatusCode(org.apache.tinkerpop.gremlin.driver.message.ResponseStatusCode) GraphType(com.alibaba.graphscope.gaia.store.GraphType) Context(org.apache.tinkerpop.gremlin.server.Context) GremlinExecutor(org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutor) OpProcessorException(org.apache.tinkerpop.gremlin.server.op.OpProcessorException) AbstractBuilder(com.alibaba.pegasus.builder.AbstractBuilder) TraversalBuilder(com.alibaba.graphscope.gaia.plan.translator.builder.TraversalBuilder) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) GremlinResultProcessor(com.alibaba.graphscope.gaia.result.GremlinResultProcessor) RequestMessage(org.apache.tinkerpop.gremlin.driver.message.RequestMessage) TagIdMaker(com.alibaba.graphscope.gaia.idmaker.TagIdMaker) TraversalTranslator(com.alibaba.graphscope.gaia.plan.translator.TraversalTranslator) ArrayList(java.util.ArrayList) List(java.util.List) Settings(org.apache.tinkerpop.gremlin.server.Settings) DefaultResultParser(com.alibaba.graphscope.gaia.result.DefaultResultParser)

Aggregations

GremlinExecutor (org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutor)15 RequestMessage (org.apache.tinkerpop.gremlin.driver.message.RequestMessage)8 TimeoutException (java.util.concurrent.TimeoutException)7 OpProcessorException (org.apache.tinkerpop.gremlin.server.op.OpProcessorException)7 Bindings (javax.script.Bindings)6 SimpleBindings (javax.script.SimpleBindings)6 Settings (org.apache.tinkerpop.gremlin.server.Settings)6 Logger (org.slf4j.Logger)6 LoggerFactory (org.slf4j.LoggerFactory)6 Traversal (org.apache.tinkerpop.gremlin.process.traversal.Traversal)5 ResponseStatusCode (org.apache.tinkerpop.gremlin.driver.message.ResponseStatusCode)4 TimedInterruptTimeoutException (org.apache.tinkerpop.gremlin.groovy.jsr223.TimedInterruptTimeoutException)4 Context (org.apache.tinkerpop.gremlin.server.Context)4 Graph (org.apache.tinkerpop.gremlin.structure.Graph)4 GaiaConfig (com.alibaba.graphscope.gaia.config.GaiaConfig)3 TagIdMaker (com.alibaba.graphscope.gaia.idmaker.TagIdMaker)3 PlanUtils (com.alibaba.graphscope.gaia.plan.PlanUtils)3 TraversalTranslator (com.alibaba.graphscope.gaia.plan.translator.TraversalTranslator)3 PlanConfig (com.alibaba.graphscope.gaia.plan.translator.builder.PlanConfig)3 TraversalBuilder (com.alibaba.graphscope.gaia.plan.translator.builder.TraversalBuilder)3