Search in sources :

Example 1 with SchemaNotFoundException

use of com.alibaba.graphscope.gaia.store.SchemaNotFoundException in project GraphScope by alibaba.

the class SchemaIdMakerStrategy method apply.

@Override
public void apply(Traversal.Admin<?, ?> traversal) {
    try {
        // label string -> label id
        List<Step> steps = traversal.getSteps();
        for (int i = 0; i < steps.size(); ++i) {
            Step step = steps.get(i);
            if (step instanceof HasContainerHolder) {
                List<HasContainer> containers = ((HasContainerHolder) step).getHasContainers();
                for (HasContainer container : containers) {
                    if (container.getKey().equals(T.label.getAccessor())) {
                        P predicate = container.getPredicate();
                        if (predicate.getValue() instanceof List && ((List) predicate.getValue()).get(0) instanceof String) {
                            List<String> values = (List<String>) predicate.getValue();
                            predicate.setValue(values.stream().map(k -> {
                                if (StringUtils.isNumeric(k)) {
                                    return k;
                                } else {
                                    long labelId = graphStore.getLabelId(k);
                                    return String.valueOf(labelId);
                                }
                            }).collect(Collectors.toList()));
                        } else if (predicate.getValue() instanceof String) {
                            String value = (String) predicate.getValue();
                            if (StringUtils.isNumeric(value)) {
                                predicate.setValue(value);
                            } else {
                                long labelId = graphStore.getLabelId(value);
                                predicate.setValue(String.valueOf(labelId));
                            }
                        } else {
                            throw new UnsupportedOperationException("hasLabel value type not support " + predicate.getValue().getClass());
                        }
                    }
                }
            } else if (step instanceof VertexStep) {
                String[] edgeLabels = ((VertexStep) step).getEdgeLabels();
                for (int j = 0; j < edgeLabels.length; ++j) {
                    if (StringUtils.isNumeric(edgeLabels[j])) {
                    // do nothing
                    } else {
                        long labelId = graphStore.getLabelId(edgeLabels[j]);
                        edgeLabels[j] = String.valueOf(labelId);
                    }
                }
            }
        }
        GraphType graphType = config.getGraphType();
        // property string -> property id
        if (graphType == GraphType.MAXGRAPH) {
            for (int i = 0; i < steps.size(); ++i) {
                Step step = steps.get(i);
                if (step instanceof HasContainerHolder) {
                    List<HasContainer> containers = ((HasContainerHolder) step).getHasContainers();
                    for (HasContainer container : containers) {
                        container.setKey(PlanUtils.convertToPropertyId(graphStore, container.getKey()));
                    }
                } else if (step instanceof PropertiesStep || step instanceof PropertyMapStep) {
                    String[] oldKeys;
                    if (step instanceof PropertiesStep) {
                        oldKeys = ((PropertiesStep) step).getPropertyKeys();
                    } else {
                        oldKeys = ((PropertyMapStep) step).getPropertyKeys();
                    }
                    String[] newKeys = Arrays.stream(oldKeys).map(k -> PlanUtils.convertToPropertyId(graphStore, k)).toArray(String[]::new);
                    FieldUtils.writeField(step, "propertyKeys", newKeys, true);
                } else if (step instanceof ByModulating) {
                    TraversalParent byParent = (TraversalParent) step;
                    for (Traversal.Admin k : byParent.getLocalChildren()) {
                        if (k instanceof ElementValueTraversal) {
                            ElementValueTraversal value = (ElementValueTraversal) k;
                            String propertyId = PlanUtils.convertToPropertyId(graphStore, value.getPropertyKey());
                            FieldUtils.writeField(value, "propertyKey", propertyId, true);
                        }
                    }
                }
            }
        }
    } catch (SchemaNotFoundException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ByModulating(org.apache.tinkerpop.gremlin.process.traversal.step.ByModulating) PropertiesStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertiesStep) PropertyMapStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertyMapStep) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) ElementValueTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.ElementValueTraversal) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) PropertiesStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertiesStep) Step(org.apache.tinkerpop.gremlin.process.traversal.Step) PropertyMapStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertyMapStep) VertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep) ElementValueTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.ElementValueTraversal) SchemaNotFoundException(com.alibaba.graphscope.gaia.store.SchemaNotFoundException) P(org.apache.tinkerpop.gremlin.process.traversal.P) VertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep) TraversalParent(org.apache.tinkerpop.gremlin.process.traversal.step.TraversalParent) GraphType(com.alibaba.graphscope.gaia.store.GraphType) HasContainerHolder(org.apache.tinkerpop.gremlin.process.traversal.step.HasContainerHolder) List(java.util.List) SchemaNotFoundException(com.alibaba.graphscope.gaia.store.SchemaNotFoundException)

Example 2 with SchemaNotFoundException

use of com.alibaba.graphscope.gaia.store.SchemaNotFoundException in project GraphScope by alibaba.

the class TraversalOpProcessor method select.

@Override
public ThrowingConsumer<Context> select(Context ctx) throws OpProcessorException {
    final RequestMessage message = ctx.getRequestMessage();
    final ThrowingConsumer<Context> op;
    final GremlinExecutor executor = ctx.getGremlinExecutor();
    final SimpleBindings b = new SimpleBindings();
    final Map<String, String> aliases = (Map<String, String>) message.optionalArgs(Tokens.ARGS_ALIASES).get();
    final String traversalSourceName = aliases.entrySet().iterator().next().getValue();
    logger.info("tokens ops is {}", message.getOp());
    if (config.getGraphType() == GraphType.MAXGRAPH) {
        graphStore.updateSnapShotId();
    }
    switch(message.getOp()) {
        case Tokens.OPS_BYTECODE:
            op = (context -> {
                try {
                    Object byteCode = message.getArgs().get(Tokens.ARGS_GREMLIN);
                    Traversal traversal = executor.eval((Bytecode) byteCode, new SimpleBindings(), null, traversalSourceName);
                    GaiaGraphOpProcessor.applyStrategy(traversal, config, graphStore);
                    long queryId = (long) queryIdMaker.getId(traversal);
                    TraversalBuilder traversalBuilder = new TraversalBuilder((Traversal.Admin) traversal).addConfig(PlanConfig.QUERY_ID, queryId).addConfig(PlanConfig.TAG_ID_MAKER, new TagIdMaker((Traversal.Admin) traversal)).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 RemoteTraverserResultParser(traversalBuilder, graphStore, config)));
                    logger.info("query-{} finish", queryId);
                } catch (SchemaNotFoundException e) {
                    throw new OpProcessorException("schema not found error", ResponseMessage.build(message).code(ResponseStatusCode.SUCCESS).result(Collections.EMPTY_LIST).create());
                }
            });
            return op;
        case Tokens.OPS_KEYS:
            GaiaGraphOpProcessor.writeResultList(ctx, Collections.EMPTY_LIST, ResponseStatusCode.SUCCESS);
            return null;
        default:
            String errorMsg = "not support " + message.getOp();
            GaiaGraphOpProcessor.writeResultList(ctx, Collections.singletonList(errorMsg), ResponseStatusCode.REQUEST_ERROR_INVALID_REQUEST_ARGUMENTS);
            return null;
    }
}
Also used : Context(org.apache.tinkerpop.gremlin.server.Context) TraversalBuilder(com.alibaba.graphscope.gaia.plan.translator.builder.TraversalBuilder) Bytecode(org.apache.tinkerpop.gremlin.process.traversal.Bytecode) AbstractBroadcastProcessor(com.alibaba.graphscope.gaia.broadcast.AbstractBroadcastProcessor) TraversalTranslator(com.alibaba.graphscope.gaia.plan.translator.TraversalTranslator) ThrowingConsumer(org.apache.tinkerpop.gremlin.util.function.ThrowingConsumer) Tokens(org.apache.tinkerpop.gremlin.driver.Tokens) LoggerFactory(org.slf4j.LoggerFactory) IdMaker(com.alibaba.graphscope.gaia.idmaker.IdMaker) RequestMessage(org.apache.tinkerpop.gremlin.driver.message.RequestMessage) ResponseMessage(org.apache.tinkerpop.gremlin.driver.message.ResponseMessage) GremlinResultProcessor(com.alibaba.graphscope.gaia.result.GremlinResultProcessor) Map(java.util.Map) OpProcessorException(org.apache.tinkerpop.gremlin.server.op.OpProcessorException) IncrementalQueryIdMaker(com.alibaba.graphscope.gaia.idmaker.IncrementalQueryIdMaker) GraphStoreService(com.alibaba.graphscope.gaia.store.GraphStoreService) Logger(org.slf4j.Logger) SchemaNotFoundException(com.alibaba.graphscope.gaia.store.SchemaNotFoundException) GaiaConfig(com.alibaba.graphscope.gaia.config.GaiaConfig) PlanUtils(com.alibaba.graphscope.gaia.plan.PlanUtils) RemoteTraverserResultParser(com.alibaba.graphscope.gaia.result.RemoteTraverserResultParser) AbstractBuilder(com.alibaba.pegasus.builder.AbstractBuilder) SimpleBindings(javax.script.SimpleBindings) 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) AbstractOpProcessor(org.apache.tinkerpop.gremlin.server.op.AbstractOpProcessor) Collections(java.util.Collections) OpProcessorException(org.apache.tinkerpop.gremlin.server.op.OpProcessorException) AbstractBuilder(com.alibaba.pegasus.builder.AbstractBuilder) GremlinExecutor(org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutor) TraversalBuilder(com.alibaba.graphscope.gaia.plan.translator.builder.TraversalBuilder) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) GremlinResultProcessor(com.alibaba.graphscope.gaia.result.GremlinResultProcessor) SimpleBindings(javax.script.SimpleBindings) RequestMessage(org.apache.tinkerpop.gremlin.driver.message.RequestMessage) TagIdMaker(com.alibaba.graphscope.gaia.idmaker.TagIdMaker) Bytecode(org.apache.tinkerpop.gremlin.process.traversal.Bytecode) TraversalTranslator(com.alibaba.graphscope.gaia.plan.translator.TraversalTranslator) SchemaNotFoundException(com.alibaba.graphscope.gaia.store.SchemaNotFoundException) Map(java.util.Map) RemoteTraverserResultParser(com.alibaba.graphscope.gaia.result.RemoteTraverserResultParser)

Example 3 with SchemaNotFoundException

use of com.alibaba.graphscope.gaia.store.SchemaNotFoundException in project GraphScope by alibaba.

the class AbstractGraphOpProcessor method evalOpInternal.

@Override
protected void evalOpInternal(Context ctx, Supplier<GremlinExecutor> gremlinExecutorSupplier, BindingSupplier bindingsSupplier) throws OpProcessorException {
    final Timer.Context timerContext = evalOpTimer.time();
    final RequestMessage msg = ctx.getRequestMessage();
    final GremlinExecutor gremlinExecutor = gremlinExecutorSupplier.get();
    final Map<String, Object> args = msg.getArgs();
    final String script = (String) args.get(Tokens.ARGS_GREMLIN);
    logger.info("script is {}", script);
    final String language = args.containsKey(Tokens.ARGS_LANGUAGE) ? (String) args.get(Tokens.ARGS_LANGUAGE) : null;
    final Bindings bindings = new SimpleBindings();
    final GremlinExecutor.LifeCycle lifeCycle = createLifeCycle(ctx, gremlinExecutorSupplier, bindingsSupplier);
    final CompletableFuture<Object> evalFuture = gremlinExecutor.eval(script, language, bindings, lifeCycle);
    evalFuture.handle((v, t) -> {
        long elapsed = timerContext.stop();
        logger.info("query {} total execution time is {} ms", script, elapsed / 1000000.0f);
        ResponseHandlerContext rhc = new ResponseHandlerContext(ctx);
        if (t != null) {
            if (t instanceof OpProcessorException) {
                rhc.writeAndFlush(((OpProcessorException) t).getResponseMessage());
            } else if (t instanceof TimedInterruptTimeoutException) {
                // occurs when the TimedInterruptCustomizerProvider is in play
                final String errorMessage = String.format("A timeout occurred within the script during evaluation" + " of [%s] - consider increasing the limit given" + " to TimedInterruptCustomizerProvider", msg);
                logger.warn(errorMessage);
                rhc.writeAndFlush(ResponseMessage.build(msg).code(ResponseStatusCode.SERVER_ERROR_TIMEOUT).statusMessage("Timeout during script evaluation triggered by" + " TimedInterruptCustomizerProvider").statusAttributeException(t).create());
            } else if (t instanceof TimeoutException) {
                final String errorMessage = String.format("Script evaluation exceeded the configured threshold" + " for request [%s]", msg);
                logger.warn(errorMessage, t);
                rhc.writeAndFlush(ResponseMessage.build(msg).code(ResponseStatusCode.SERVER_ERROR_TIMEOUT).statusMessage(t.getMessage()).statusAttributeException(t).create());
            } else if (t instanceof SchemaNotFoundException) {
                writeResultList(ctx, Collections.EMPTY_LIST, ResponseStatusCode.SUCCESS);
            } else {
                if (t instanceof MultipleCompilationErrorsException && t.getMessage().contains("Method too large") && ((MultipleCompilationErrorsException) t).getErrorCollector().getErrorCount() == 1) {
                    final String errorMessage = String.format("The Gremlin statement that was submitted exceeds" + " the maximum compilation size allowed by the" + " JVM, please split it into multiple smaller" + " statements");
                    logger.warn(errorMessage);
                    rhc.writeAndFlush(ResponseMessage.build(msg).code(ResponseStatusCode.SERVER_ERROR_SCRIPT_EVALUATION).statusMessage(errorMessage).statusAttributeException(t).create());
                } else {
                    final String errorMessage = (t.getMessage() == null) ? t.toString() : t.getMessage();
                    logger.warn(String.format("Exception processing a script on request [%s].", msg), t);
                    rhc.writeAndFlush(ResponseMessage.build(msg).code(ResponseStatusCode.SERVER_ERROR_SCRIPT_EVALUATION).statusMessage(errorMessage).statusAttributeException(t).create());
                }
            }
        }
        return null;
    });
}
Also used : ResponseHandlerContext(org.apache.tinkerpop.gremlin.server.ResponseHandlerContext) OpProcessorException(org.apache.tinkerpop.gremlin.server.op.OpProcessorException) GremlinExecutor(org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutor) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) Timer(com.codahale.metrics.Timer) SimpleBindings(javax.script.SimpleBindings) RequestMessage(org.apache.tinkerpop.gremlin.driver.message.RequestMessage) SchemaNotFoundException(com.alibaba.graphscope.gaia.store.SchemaNotFoundException) MultipleCompilationErrorsException(org.codehaus.groovy.control.MultipleCompilationErrorsException) TimedInterruptTimeoutException(org.apache.tinkerpop.gremlin.groovy.jsr223.TimedInterruptTimeoutException) TimedInterruptTimeoutException(org.apache.tinkerpop.gremlin.groovy.jsr223.TimedInterruptTimeoutException) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

SchemaNotFoundException (com.alibaba.graphscope.gaia.store.SchemaNotFoundException)3 GraphType (com.alibaba.graphscope.gaia.store.GraphType)2 SimpleBindings (javax.script.SimpleBindings)2 RequestMessage (org.apache.tinkerpop.gremlin.driver.message.RequestMessage)2 GremlinExecutor (org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutor)2 Traversal (org.apache.tinkerpop.gremlin.process.traversal.Traversal)2 OpProcessorException (org.apache.tinkerpop.gremlin.server.op.OpProcessorException)2 AbstractBroadcastProcessor (com.alibaba.graphscope.gaia.broadcast.AbstractBroadcastProcessor)1 GaiaConfig (com.alibaba.graphscope.gaia.config.GaiaConfig)1 IdMaker (com.alibaba.graphscope.gaia.idmaker.IdMaker)1 IncrementalQueryIdMaker (com.alibaba.graphscope.gaia.idmaker.IncrementalQueryIdMaker)1 TagIdMaker (com.alibaba.graphscope.gaia.idmaker.TagIdMaker)1 PlanUtils (com.alibaba.graphscope.gaia.plan.PlanUtils)1 TraversalTranslator (com.alibaba.graphscope.gaia.plan.translator.TraversalTranslator)1 PlanConfig (com.alibaba.graphscope.gaia.plan.translator.builder.PlanConfig)1 TraversalBuilder (com.alibaba.graphscope.gaia.plan.translator.builder.TraversalBuilder)1 GremlinResultProcessor (com.alibaba.graphscope.gaia.result.GremlinResultProcessor)1 RemoteTraverserResultParser (com.alibaba.graphscope.gaia.result.RemoteTraverserResultParser)1 GraphStoreService (com.alibaba.graphscope.gaia.store.GraphStoreService)1 AbstractBuilder (com.alibaba.pegasus.builder.AbstractBuilder)1