Search in sources :

Example 1 with DfsTraversal

use of com.alibaba.maxgraph.compiler.dfs.DfsTraversal in project GraphScope by alibaba.

the class MixedTraversalOpProcessor method doProcessTraversal.

private Long doProcessTraversal(Context context, Object object, Graph graph, long timeout, String queryId, Stopwatch timer) {
    Pair<GraphSchema, Long> snapshotSchema;
    GraphSchema schema;
    Long resultNum = 0L;
    if (object instanceof GraphTraversal.Admin || object instanceof DfsTraversal) {
        QueryStatus queryStatus;
        GraphTraversal.Admin traversal = (object instanceof GraphTraversal.Admin) ? GraphTraversal.Admin.class.cast(object) : (DfsTraversal.class.cast(object)).getTraversal();
        String queryString = traversal.toString();
        logger.info("Receive traversal query=>" + queryString);
        if (!traversal.isLocked()) {
            traversal.getStrategies().removeStrategies(ProfileStrategy.class, MxGraphStepStrategy.class, FilterRankingStrategy.class);
        }
        traversal.applyStrategies();
        NettyVertexRpcProcessor nettyVertexRpcProcessor;
        QueryFlowManager queryFlowManager;
        // 保证一查看到snapshotId就开始维护query_status
        synchronized (queryCallbackManager) {
            snapshotSchema = schemaFetcher.getSchemaSnapshotPair();
            queryStatus = queryCallbackManager.beforeExecution(snapshotSchema.getRight());
        }
        schema = snapshotSchema.getLeft();
        LogicalPlanOptimizer planOptimizer = new LogicalPlanOptimizer(new OptimizeConfig(), this.globalPullGraphFlag, schema, snapshotSchema.getRight(), this.lambdaEnableFlag);
        final int resultIterationBatchSize = (Integer) context.getRequestMessage().optionalArgs(Tokens.ARGS_BATCH_SIZE).orElse(this.resultIterationBatchSize);
        nettyVertexRpcProcessor = new NettyTraverserVertexProcessor(context, resultIterationBatchSize, false);
        try {
            queryFlowManager = (object instanceof GraphTraversal.Admin) ? planOptimizer.build(GraphTraversal.class.cast(traversal)) : planOptimizer.build(DfsTraversal.class.cast(object));
        } catch (IllegalArgumentException iae) {
            if (iae.getMessage().contains("MaxGraphIoStep")) {
                logger.info("do maxgraph io step");
                while (traversal.hasNext()) {
                    logger.info("maxgraph io hasNext");
                }
                nettyVertexRpcProcessor.finish(ResponseStatusCode.SUCCESS);
                return 0L;
            }
            throw iae;
        }
        try {
            boolean isLambdaExisted = TraversalHelper.anyStepRecursively(s -> s instanceof LambdaHolder, (Traversal.Admin<?, ?>) traversal);
            queryFlowManager.getQueryFlow().setFrontId(serverId);
            if (this.lambdaEnableFlag && isLambdaExisted) {
                final ObjectMapper mapper = GraphSONMapper.build().version(GraphSONVersion.V3_0).addCustomModule(GraphSONXModuleV3d0.build().create(false)).create().createMapper();
                Bytecode bytecode = (Bytecode) context.getRequestMessage().getArgs().get(Tokens.ARGS_GREMLIN);
                byte[] bytecodeByte = mapper.writeValueAsBytes(bytecode);
                queryFlowManager.getQueryFlow().setLambdaExisted(isLambdaExisted).setBytecode(ByteString.copyFrom(bytecodeByte));
            }
            GremlinResultTransform gremlinResultTransform = new GremlinResultTransform(remoteRpcConnector, nettyVertexRpcProcessor, this.graph, queryFlowManager.getResultValueType(), vertexCacheFlag);
            NettyResultProcessor nettyResultProcessor = new NettyResultProcessor(queryId, traversal.toString(), context, new ExecuteConfig().getBatchQuerySize(), resultIterationBatchSize, false);
            nettyResultProcessor.setSchema(schema);
            nettyResultProcessor.setResultTransform(gremlinResultTransform);
            nettyResultProcessor.setLabelIndexNameList(queryFlowManager.getTreeNodeLabelManager().getUserIndexLabelList());
            TimelyQuery timelyQuery = new TimelyQuery(queryFlowManager, nettyResultProcessor, this.graph);
            Logging.query(this.graphName, FRONTEND, this.serverId, queryId, QueryType.EXECUTE, QueryEvent.PLAN_GENERATED, timer.elapsed(TimeUnit.NANOSECONDS), null, null, "");
            timelyExecutor.execute(timelyQuery, schema, timeout, queryId);
            resultNum = nettyResultProcessor.total();
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            queryCallbackManager.afterExecution(queryStatus);
        }
    } else {
        throw new IllegalArgumentException("Not support to process=>" + object);
    }
    return resultNum;
}
Also used : TimelyQuery(com.alibaba.maxgraph.compiler.query.TimelyQuery) DfsTraversal(com.alibaba.maxgraph.compiler.dfs.DfsTraversal) LambdaHolder(org.apache.tinkerpop.gremlin.process.traversal.step.LambdaHolder) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) DfsTraversal(com.alibaba.maxgraph.compiler.dfs.DfsTraversal) QueryFlowManager(com.alibaba.maxgraph.compiler.optimizer.QueryFlowManager) ByteString(com.google.protobuf.ByteString) QueryStatus(com.alibaba.maxgraph.api.query.QueryStatus) GraphSchema(com.alibaba.maxgraph.compiler.api.schema.GraphSchema) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Bytecode(org.apache.tinkerpop.gremlin.process.traversal.Bytecode) JsonProcessingException(org.apache.tinkerpop.shaded.jackson.core.JsonProcessingException) ObjectMapper(org.apache.tinkerpop.shaded.jackson.databind.ObjectMapper) OptimizeConfig(com.alibaba.maxgraph.compiler.optimizer.OptimizeConfig) LogicalPlanOptimizer(com.alibaba.maxgraph.compiler.optimizer.LogicalPlanOptimizer) ExecuteConfig(com.alibaba.maxgraph.compiler.executor.ExecuteConfig)

Example 2 with DfsTraversal

use of com.alibaba.maxgraph.compiler.dfs.DfsTraversal in project GraphScope by alibaba.

the class MixedOpProcessor method processHttpGraphTraversal.

@Override
public List<Object> processHttpGraphTraversal(String script, Object traversal, long timeout, FullHttpRequest request) throws Exception {
    if (traversal instanceof GraphTraversal || traversal instanceof DfsTraversal) {
        String queryId = String.valueOf(ThreadLocalRandom.current().nextLong());
        Stopwatch timer = Stopwatch.createStarted();
        RemoteRpcProcessor remoteRpcProcessor = new DefaultVertexRpcProcessor();
        MemoryResultProcessor resultProcessor = new MemoryResultProcessor(executeConfig.getBatchQuerySize(), resultIterationBatchSize, queryId);
        processQueryTraversal(script, traversal, timeout, queryId, timer, this.httpRpcConnector, remoteRpcProcessor, resultProcessor);
        return resultProcessor.getResultList();
    } else {
        throw new IllegalArgumentException("Not support " + script + " in http request");
    }
}
Also used : GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Stopwatch(com.google.common.base.Stopwatch) DfsTraversal(com.alibaba.maxgraph.compiler.dfs.DfsTraversal)

Example 3 with DfsTraversal

use of com.alibaba.maxgraph.compiler.dfs.DfsTraversal in project GraphScope by alibaba.

the class MixedOpProcessor method doProcessGraphTraversal.

private Long doProcessGraphTraversal(String script, Context context, Object traversal, long timeout, String queryId, Stopwatch timer) throws Exception {
    Long totalResultNum = 0L;
    Pair<GraphSchema, Long> snapshotSchema;
    GraphSchema schema;
    if (traversal instanceof GraphTraversal || traversal instanceof DfsTraversal) {
        final int resultIterationBatchSize = (Integer) context.getRequestMessage().optionalArgs(Tokens.ARGS_BATCH_SIZE).orElse(this.resultIterationBatchSize);
        NettyVertexRpcProcessor nettyVertexRpcProcessor = new NettyVertexRpcProcessor(context, resultIterationBatchSize, false);
        AbstractResultProcessor nettyResultProcessor = new NettyResultProcessor(queryId, script, context, executeConfig.getBatchQuerySize(), resultIterationBatchSize, false);
        totalResultNum = processQueryTraversal(script, traversal, timeout, queryId, timer, this.remoteRpcConnector, nettyVertexRpcProcessor, nettyResultProcessor);
    } else {
        snapshotSchema = this.schemaFetcher.getSchemaSnapshotPair();
        schema = snapshotSchema.getLeft();
        if (traversal instanceof PreparedTraversal) {
            throw new UnsupportedOperationException();
        } else if (traversal instanceof PreparedExecuteParam) {
            throw new UnsupportedOperationException();
        } else if (traversal instanceof ShowProcessListQuery) {
            TimelyResultProcessor nettyResultProcessor = newNettyResultProcessor(queryId, script, context, graph, schema);
            timelyExecutor.showProcessList(nettyResultProcessor);
        } else if (traversal instanceof CancelDataflow) {
            NettyResultProcessor nettyResultProcessor = newNettyResultProcessor(queryId, script, context, graph, schema);
            timelyExecutor.cancelDataflow(nettyResultProcessor, ((CancelDataflow) traversal).queryId);
        } else if (traversal instanceof RecordManager) {
            Object result = processRecordManager(RecordManager.class.cast(traversal));
            writeResultList(context, Lists.newArrayList(result), ResponseStatusCode.SUCCESS);
        } else if (traversal instanceof EstimateRequest) {
            writeResultList(context, Lists.newArrayList(processEstimateManager((EstimateRequest) traversal, timeout)), ResponseStatusCode.SUCCESS);
        } else if (traversal instanceof StatisticsRequest) {
            CostDataStatistics costDataStatistics = CostDataStatistics.getInstance();
            writeResultList(context, Lists.newArrayList(costDataStatistics.formatJson()), ResponseStatusCode.SUCCESS);
        } else if (traversal instanceof ShowPlanPathListRequest) {
            ShowPlanPathListRequest showPlanPathListRequest = (ShowPlanPathListRequest) traversal;
            writeResultList(context, Lists.newArrayList(buildCostPathList(showPlanPathListRequest.getTraversal())), ResponseStatusCode.SUCCESS);
        } else if (traversal instanceof Element) {
            writeResultList(context, Lists.newArrayList((Element) traversal), ResponseStatusCode.SUCCESS);
        } else if (traversal instanceof List) {
            writeResultList(context, (List) traversal, ResponseStatusCode.SUCCESS);
        } else if (traversal instanceof GraphSchema) {
            writeResultList(context, Lists.newArrayList(((GraphSchema) traversal).formatJson()), ResponseStatusCode.SUCCESS);
        } else if (traversal instanceof String) {
            writeResultList(context, Lists.newArrayList(traversal), ResponseStatusCode.SUCCESS);
        } else if (traversal != null && (!(traversal instanceof String) || !StringUtils.isEmpty(traversal.toString()))) {
            throw new IllegalArgumentException(traversal.toString());
        }
    }
    return totalResultNum;
}
Also used : PreparedTraversal(com.alibaba.maxgraph.compiler.prepare.PreparedTraversal) CancelDataflow(com.alibaba.maxgraph.sdkcommon.graph.CancelDataflow) Element(org.apache.tinkerpop.gremlin.structure.Element) GraphElement(com.alibaba.maxgraph.compiler.api.schema.GraphElement) DfsTraversal(com.alibaba.maxgraph.compiler.dfs.DfsTraversal) TimelyResultProcessor(com.alibaba.maxgraph.rpc.TimelyResultProcessor) GraphSchema(com.alibaba.maxgraph.compiler.api.schema.GraphSchema) CostDataStatistics(com.alibaba.maxgraph.compiler.cost.statistics.CostDataStatistics) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) ShowProcessListQuery(com.alibaba.maxgraph.sdkcommon.graph.ShowProcessListQuery) List(java.util.List) ShowPlanPathListRequest(com.alibaba.maxgraph.sdkcommon.graph.ShowPlanPathListRequest) EstimateRequest(com.alibaba.maxgraph.sdkcommon.graph.EstimateRequest) StatisticsRequest(com.alibaba.maxgraph.sdkcommon.graph.StatisticsRequest) RecordManager(com.alibaba.maxgraph.structure.manager.record.RecordManager) PreparedExecuteParam(com.alibaba.maxgraph.compiler.prepare.PreparedExecuteParam)

Aggregations

DfsTraversal (com.alibaba.maxgraph.compiler.dfs.DfsTraversal)3 GraphTraversal (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal)3 GraphSchema (com.alibaba.maxgraph.compiler.api.schema.GraphSchema)2 QueryStatus (com.alibaba.maxgraph.api.query.QueryStatus)1 GraphElement (com.alibaba.maxgraph.compiler.api.schema.GraphElement)1 CostDataStatistics (com.alibaba.maxgraph.compiler.cost.statistics.CostDataStatistics)1 ExecuteConfig (com.alibaba.maxgraph.compiler.executor.ExecuteConfig)1 LogicalPlanOptimizer (com.alibaba.maxgraph.compiler.optimizer.LogicalPlanOptimizer)1 OptimizeConfig (com.alibaba.maxgraph.compiler.optimizer.OptimizeConfig)1 QueryFlowManager (com.alibaba.maxgraph.compiler.optimizer.QueryFlowManager)1 PreparedExecuteParam (com.alibaba.maxgraph.compiler.prepare.PreparedExecuteParam)1 PreparedTraversal (com.alibaba.maxgraph.compiler.prepare.PreparedTraversal)1 TimelyQuery (com.alibaba.maxgraph.compiler.query.TimelyQuery)1 TimelyResultProcessor (com.alibaba.maxgraph.rpc.TimelyResultProcessor)1 CancelDataflow (com.alibaba.maxgraph.sdkcommon.graph.CancelDataflow)1 EstimateRequest (com.alibaba.maxgraph.sdkcommon.graph.EstimateRequest)1 ShowPlanPathListRequest (com.alibaba.maxgraph.sdkcommon.graph.ShowPlanPathListRequest)1 ShowProcessListQuery (com.alibaba.maxgraph.sdkcommon.graph.ShowProcessListQuery)1 StatisticsRequest (com.alibaba.maxgraph.sdkcommon.graph.StatisticsRequest)1 RecordManager (com.alibaba.maxgraph.structure.manager.record.RecordManager)1