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