Search in sources :

Example 1 with ExceptionHolder

use of com.alibaba.maxgraph.sdk.exception.ExceptionHolder in project GraphScope by alibaba.

the class RpcConnector method query.

public void query(QueryFlowOuterClass.QueryFlow.Builder queryFlow, TimelyResultProcessor resultProcessor, GraphSchema schema, Graph graph, long timeout, boolean isAsync) throws Exception {
    ExceptionHolder exceptionHolder = new ExceptionHolder();
    List<Endpoint> executorAddrList = getTargetExecutorAddrs();
    int executorCount = executorAddrList.size();
    CountDownLatch latch = new CountDownLatch(executorCount);
    Stopwatch stopwatch = Stopwatch.createStarted();
    LOG.info("Query plan=>" + TextFormat.printToString(queryFlow));
    List<StreamObserver> streamObserverList = new ArrayList<>();
    for (int i = 0; i < executorAddrList.size(); i++) {
        streamObserverList.add(new QueryStreamObserver(resultProcessor, schema, graph, exceptionHolder, latch, queryFlow.getQueryId()));
    }
    long currentTimeOut = queryFlow.getTimeoutMs();
    if (currentTimeOut == 0) {
        queryFlow.setTimeoutMs(timeout);
        currentTimeOut = timeout;
    }
    queryFlow.setStartTimestampMs(System.currentTimeMillis());
    QueryFlowOuterClass.QueryFlow queryFlowPlan = queryFlow.build();
    if (isAsync) {
        for (int i = 0; i < executorAddrList.size(); i++) {
            IpPort address = IpPort.fromCtrlAndAsyncEndpoint(executorAddrList.get(i));
            ManagedChannel randomChannel = channels.get(address).get(RandomUtils.nextInt(0, channelCount)).getRight();
            ConnectivityState connectivityState = randomChannel.getState(true);
            if (connectivityState != ConnectivityState.IDLE && connectivityState != ConnectivityState.READY) {
                LOG.warn("refresh connection for " + address + " with connectivity state " + connectivityState);
                channels.refresh(address);
                randomChannel = channels.get(address).get(RandomUtils.nextInt(0, channelCount)).getRight();
            }
            connectivityState = randomChannel.getState(true);
            LOG.info("Connectivity state from " + address + " " + connectivityState);
            AsyncMaxGraphServiceGrpc.newStub(randomChannel).asyncExecute(queryFlowPlan, streamObserverList.get(i));
        }
    } else {
        for (int i = 0; i < executorAddrList.size(); i++) {
            IpPort address = IpPort.fromGremlinEndpoint(executorAddrList.get(i));
            MaxGraphServiceStub gremlinServiceStub = channels.get(address).get(RandomUtils.nextInt(0, channelCount)).getLeft();
            gremlinServiceStub.execute(queryFlowPlan, streamObserverList.get(i));
        }
    }
    long sendTime = stopwatch.elapsed(TimeUnit.MILLISECONDS);
    long awaitStart = System.currentTimeMillis();
    long waitTimeOut = currentTimeOut;
    while (true) {
        try {
            if (!latch.await(waitTimeOut, TimeUnit.MILLISECONDS)) {
                // try to check if executor is exit
                getTargetExecutorAddrs();
                if (exceptionHolder.getException() != null) {
                    LOG.error("query from timely fail", exceptionHolder.getException());
                    throw new RuntimeException(exceptionHolder.getException());
                }
                throw new RuntimeException("Wait query complete time out for " + (currentTimeOut / 1000.0) + " secs");
            }
            break;
        } catch (InterruptedException e) {
            if (exceptionHolder.getException() != null) {
                LOG.error("query from timely fail", exceptionHolder.getException());
                throw new RuntimeException(exceptionHolder.getException());
            }
            long currentTime = System.currentTimeMillis();
            waitTimeOut = currentTimeOut - (currentTime - awaitStart);
            if (waitTimeOut <= 0) {
                break;
            }
            LOG.error("thread interrupted, continue to wait " + waitTimeOut + "ms");
        }
    }
    if (exceptionHolder.getException() != null) {
        LOG.error("query from timely fail", exceptionHolder.getException());
        throw new RuntimeException(exceptionHolder.getException());
    }
    processEmptyResult(resultProcessor, queryFlowPlan);
    long queryTime = stopwatch.elapsed(TimeUnit.MILLISECONDS);
    resultProcessor.finish();
    long finishTime = stopwatch.elapsed(TimeUnit.MILLISECONDS);
    LOG.info("finish query " + queryFlow.getQueryId() + " send cost=>" + sendTime + ", query cost=>" + (queryTime - sendTime) + "ms, finish cost=>" + (finishTime - queryTime) + "ms, total cost=>" + finishTime + "ms, query count=> " + resultProcessor.total());
    stopwatch.stop();
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) ExceptionHolder(com.alibaba.maxgraph.sdk.exception.ExceptionHolder) Stopwatch(com.google.common.base.Stopwatch) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) QueryFlowOuterClass(com.alibaba.maxgraph.QueryFlowOuterClass) Endpoint(com.alibaba.maxgraph.sdkcommon.client.Endpoint) ConnectivityState(io.grpc.ConnectivityState) Endpoint(com.alibaba.maxgraph.sdkcommon.client.Endpoint) MaxGraphServiceStub(com.alibaba.maxgraph.rpc.MaxGraphServiceGrpc.MaxGraphServiceStub) ManagedChannel(io.grpc.ManagedChannel)

Example 2 with ExceptionHolder

use of com.alibaba.maxgraph.sdk.exception.ExceptionHolder in project GraphScope by alibaba.

the class RpcConnector method executePrepare.

public void executePrepare(QueryFlowOuterClass.Query.Builder query, TimelyResultProcessor resultProcessor, GraphSchema schema, Graph graph, boolean isAsync) throws Exception {
    ExceptionHolder exceptionHolder = new ExceptionHolder();
    CountDownLatch latch = new CountDownLatch(1);
    Stopwatch stopwatch = Stopwatch.createStarted();
    StreamObserver streamObserver = new QueryStreamObserver(resultProcessor, schema, graph, exceptionHolder, latch, query.getQueryId());
    query.setTimeoutSeconds(rpcConfig.getQueryTimeoutSec());
    if (isAsync) {
        IpPort address = IpPort.fromCtrlAndAsyncEndpoint(addressFetcher.getServiceAddress().get(0));
        ManagedChannel randomChannel = channels.get(address).get(RandomUtils.nextInt(0, channelCount)).getRight();
        AsyncMaxGraphServiceGrpc.newStub(randomChannel).asyncQuery2(query.build(), streamObserver);
    } else {
        IpPort address = IpPort.fromGremlinEndpoint(addressFetcher.getServiceAddress().get(0));
        ManagedChannel randomChannel = channels.get(address).get(RandomUtils.nextInt(0, channelCount)).getRight();
        MaxGraphServiceStub gremlinServiceStub = MaxGraphServiceGrpc.newStub(randomChannel);
        gremlinServiceStub.query2(query.build(), streamObserver);
    }
    if (!latch.await(rpcConfig.getQueryTimeoutSec(), TimeUnit.SECONDS)) {
        throw new RuntimeException("Wait query complete time out " + rpcConfig.getQueryTimeoutSec() + " secs");
    }
    if (exceptionHolder.getException() != null) {
        LOG.error("query from timely fail", exceptionHolder.getException());
        throw new RuntimeException(exceptionHolder.getException());
    }
    resultProcessor.finish();
    LOG.info("execute prepare query " + query.getQueryId() + " finish cost=>" + stopwatch.elapsed(TimeUnit.MILLISECONDS) + " ms, count=> " + resultProcessor.total());
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) ExceptionHolder(com.alibaba.maxgraph.sdk.exception.ExceptionHolder) MaxGraphServiceStub(com.alibaba.maxgraph.rpc.MaxGraphServiceGrpc.MaxGraphServiceStub) Stopwatch(com.google.common.base.Stopwatch) ManagedChannel(io.grpc.ManagedChannel) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 3 with ExceptionHolder

use of com.alibaba.maxgraph.sdk.exception.ExceptionHolder in project GraphScope by alibaba.

the class RemoteRpcConnector method queryVertices.

public void queryVertices(Map<Integer, Map<ElementId, Integer>> classified, GraphSchema schema, TinkerMaxGraph graph, Context context, int batchSize, List<Object> resultList, boolean vertexCacheFlag, RpcProcessorType rpcProcessorType, Map<CompositeId, Map<String, Object>> existPropMap) {
    List<RpcAddress> remoteAddressList = addressFetcher.getAddressList();
    if (classified.size() > remoteAddressList.size()) {
        throw new IllegalArgumentException("classified=>" + classified.keySet() + " count > remote address list=>" + remoteAddressList.toString());
    }
    int classifiedCount = classified.size();
    CountDownLatch latch = new CountDownLatch(classifiedCount);
    ExceptionHolder exceptionHolder = new ExceptionHolder();
    List<RemoteRpcProcessor> remoteRpcProcessorList = Lists.newArrayListWithCapacity(classifiedCount);
    for (Map.Entry<Integer, Map<ElementId, Integer>> entry : classified.entrySet()) {
        if (entry.getKey() < 0) {
            throw new RuntimeException("Invalid key in " + classified + " when get detail of vertex");
        }
        RpcAddress rpcAddress = remoteAddressList.get(entry.getKey());
        LOG.info("Get rpc address " + rpcAddress + " for store id " + entry.getKey());
        try {
            ManagedChannel managedChannel = channels.get(rpcAddress);
            GremlinServiceGrpc.GremlinServiceStub gremlinServiceStub = GremlinServiceGrpc.newStub(managedChannel);
            GremlinQuery.VertexRequest.Builder reqBuilder = GremlinQuery.VertexRequest.newBuilder();
            Map<ElementId, Integer> vertexCountList = entry.getValue();
            for (ElementId elementId : vertexCountList.keySet()) {
                reqBuilder.addIds(GremlinQuery.VertexId.newBuilder().setId(elementId.id()).setTypeId(elementId.typeId()).build());
            }
            RemoteRpcProcessor remoteRpcProcessor = createRemoteRpcProcessor(rpcProcessorType, context, batchSize);
            remoteRpcProcessorList.add(remoteRpcProcessor);
            VertexStreamObserver vertexStreamObserver = new VertexStreamObserver(remoteRpcProcessor, schema, graph, exceptionHolder, latch, vertexCountList, vertexCacheFlag, existPropMap);
            gremlinServiceStub.getVertexs(reqBuilder.build(), vertexStreamObserver);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
    }
    try {
        if (!latch.await(rpcConfig.getQueryTimeoutSec(), TimeUnit.SECONDS)) {
            throw new RuntimeException("Wait query vertex complete time out");
        }
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
    if (exceptionHolder.getException() != null) {
        LOG.error("query vertex from store service fail", exceptionHolder.getException());
        throw new RuntimeException(exceptionHolder.getException());
    }
    if (null != resultList) {
        for (RemoteRpcProcessor remoteRpcProcessor : remoteRpcProcessorList) {
            resultList.addAll(remoteRpcProcessor.getResultList());
        }
    }
}
Also used : ExceptionHolder(com.alibaba.maxgraph.sdk.exception.ExceptionHolder) CountDownLatch(java.util.concurrent.CountDownLatch) RpcAddress(com.alibaba.maxgraph.common.rpc.RpcAddress) ManagedChannel(io.grpc.ManagedChannel) ExecutionException(java.util.concurrent.ExecutionException) Map(java.util.Map) ElementId(com.alibaba.maxgraph.sdkcommon.graph.ElementId) GremlinServiceGrpc(com.alibaba.maxgraph.proto.GremlinServiceGrpc)

Aggregations

ExceptionHolder (com.alibaba.maxgraph.sdk.exception.ExceptionHolder)3 ManagedChannel (io.grpc.ManagedChannel)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 MaxGraphServiceStub (com.alibaba.maxgraph.rpc.MaxGraphServiceGrpc.MaxGraphServiceStub)2 Stopwatch (com.google.common.base.Stopwatch)2 StreamObserver (io.grpc.stub.StreamObserver)2 QueryFlowOuterClass (com.alibaba.maxgraph.QueryFlowOuterClass)1 RpcAddress (com.alibaba.maxgraph.common.rpc.RpcAddress)1 GremlinServiceGrpc (com.alibaba.maxgraph.proto.GremlinServiceGrpc)1 Endpoint (com.alibaba.maxgraph.sdkcommon.client.Endpoint)1 ElementId (com.alibaba.maxgraph.sdkcommon.graph.ElementId)1 ConnectivityState (io.grpc.ConnectivityState)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 ExecutionException (java.util.concurrent.ExecutionException)1