Search in sources :

Example 1 with ResponseHandlerContext

use of org.apache.tinkerpop.gremlin.server.ResponseHandlerContext in project GraphScope by alibaba.

the class AbstractMixedOpProcessor method handleIterator.

@Override
protected void handleIterator(final ResponseHandlerContext rhc, final Iterator itty) throws InterruptedException {
    Context context = rhc.getContext();
    ChannelHandlerContext ctx = context.getChannelHandlerContext();
    RequestMessage msg = context.getRequestMessage();
    Settings settings = context.getSettings();
    MessageSerializer serializer = ctx.channel().attr(StateKey.SERIALIZER).get();
    boolean useBinary = ctx.channel().attr(StateKey.USE_BINARY).get();
    boolean warnOnce = false;
    boolean managedTransactionsForRequest = this.manageTransactions ? true : (Boolean) msg.getArgs().getOrDefault("manageTransaction", false);
    if (!itty.hasNext()) {
        if (managedTransactionsForRequest) {
            attemptCommit(msg, context.getGraphManager(), settings.strictTransactionManagement);
        }
        rhc.writeAndFlush(ResponseMessage.build(msg).code(ResponseStatusCode.NO_CONTENT).statusAttributes(this.generateStatusAttributes(ctx, msg, ResponseStatusCode.NO_CONTENT, itty, settings)).create());
    } else {
        int resultIterationBatchSize = (Integer) msg.optionalArgs("batchSize").orElse(settings.resultIterationBatchSize);
        List<Object> aggregate = new ArrayList(resultIterationBatchSize);
        boolean hasMore = itty.hasNext();
        while (hasMore) {
            if (Thread.interrupted()) {
                throw new InterruptedException();
            }
            boolean forceFlush = this.isForceFlushed(ctx, msg, itty);
            if (aggregate.size() < resultIterationBatchSize && itty.hasNext() && !forceFlush) {
                Object object = itty.next();
                if (object instanceof Path) {
                    object = DetachedFactory.detach((Path) object, true);
                }
                aggregate.add(object);
            }
            if (!ctx.channel().isWritable()) {
                if (!warnOnce) {
                    logger.warn("Pausing response writing as writeBufferHighWaterMark exceeded on" + " {} - writing will continue once client has caught up", msg);
                    warnOnce = true;
                }
                TimeUnit.MILLISECONDS.sleep(10L);
            } else if (forceFlush || aggregate.size() == resultIterationBatchSize || !itty.hasNext()) {
                ResponseStatusCode code = itty.hasNext() ? ResponseStatusCode.PARTIAL_CONTENT : ResponseStatusCode.SUCCESS;
                Frame frame = null;
                try {
                    frame = makeFrame(rhc, msg, serializer, useBinary, aggregate, code, this.generateResultMetaData(ctx, msg, code, itty, settings), this.generateStatusAttributes(ctx, msg, code, itty, settings));
                } catch (Exception var20) {
                    if (frame != null) {
                        frame.tryRelease();
                    }
                    if (managedTransactionsForRequest) {
                        attemptRollback(msg, context.getGraphManager(), settings.strictTransactionManagement);
                    }
                    return;
                }
                boolean moreInIterator = itty.hasNext();
                try {
                    if (moreInIterator) {
                        aggregate = new ArrayList(resultIterationBatchSize);
                    } else {
                        if (managedTransactionsForRequest) {
                            attemptCommit(msg, context.getGraphManager(), settings.strictTransactionManagement);
                        }
                        hasMore = false;
                    }
                } catch (Exception var19) {
                    if (frame != null) {
                        frame.tryRelease();
                    }
                    throw var19;
                }
                if (!moreInIterator) {
                    this.iterateComplete(ctx, msg, itty);
                }
                rhc.writeAndFlush(code, frame);
            }
        }
    }
}
Also used : ResponseHandlerContext(org.apache.tinkerpop.gremlin.server.ResponseHandlerContext) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Context(org.apache.tinkerpop.gremlin.server.Context) Path(org.apache.tinkerpop.gremlin.process.traversal.Path) Frame(org.apache.tinkerpop.gremlin.server.handler.Frame) MessageSerializer(org.apache.tinkerpop.gremlin.driver.MessageSerializer) ArrayList(java.util.ArrayList) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) TimedInterruptTimeoutException(org.apache.tinkerpop.gremlin.groovy.jsr223.TimedInterruptTimeoutException) TimeoutException(java.util.concurrent.TimeoutException) RetryGremlinException(com.alibaba.maxgraph.compiler.exception.RetryGremlinException) OpProcessorException(org.apache.tinkerpop.gremlin.server.op.OpProcessorException) ResponseStatusCode(org.apache.tinkerpop.gremlin.driver.message.ResponseStatusCode) RequestMessage(org.apache.tinkerpop.gremlin.driver.message.RequestMessage) Settings(org.apache.tinkerpop.gremlin.server.Settings)

Example 2 with ResponseHandlerContext

use of org.apache.tinkerpop.gremlin.server.ResponseHandlerContext 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

TimeoutException (java.util.concurrent.TimeoutException)2 RequestMessage (org.apache.tinkerpop.gremlin.driver.message.RequestMessage)2 TimedInterruptTimeoutException (org.apache.tinkerpop.gremlin.groovy.jsr223.TimedInterruptTimeoutException)2 ResponseHandlerContext (org.apache.tinkerpop.gremlin.server.ResponseHandlerContext)2 OpProcessorException (org.apache.tinkerpop.gremlin.server.op.OpProcessorException)2 SchemaNotFoundException (com.alibaba.graphscope.gaia.store.SchemaNotFoundException)1 RetryGremlinException (com.alibaba.maxgraph.compiler.exception.RetryGremlinException)1 Timer (com.codahale.metrics.Timer)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ArrayList (java.util.ArrayList)1 Bindings (javax.script.Bindings)1 SimpleBindings (javax.script.SimpleBindings)1 MessageSerializer (org.apache.tinkerpop.gremlin.driver.MessageSerializer)1 ResponseStatusCode (org.apache.tinkerpop.gremlin.driver.message.ResponseStatusCode)1 GremlinExecutor (org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutor)1 Path (org.apache.tinkerpop.gremlin.process.traversal.Path)1 Context (org.apache.tinkerpop.gremlin.server.Context)1 Settings (org.apache.tinkerpop.gremlin.server.Settings)1 Frame (org.apache.tinkerpop.gremlin.server.handler.Frame)1 MultipleCompilationErrorsException (org.codehaus.groovy.control.MultipleCompilationErrorsException)1