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