Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix TuGraphDbRpcClient #24

Merged
merged 2 commits into from
Jan 19, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.google.protobuf.InvalidProtocolBufferException;
import lgraph.Lgraph;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

Expand All @@ -24,6 +25,7 @@
import java.nio.file.Files;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;

/**
* @Author: [email protected]
Expand Down Expand Up @@ -53,6 +55,7 @@ enum ClientType{
// Attributes common to all types of clients
private TuGraphSingleRpcClient leaderClient;
private final List<TuGraphSingleRpcClient> rpcClientPool = new CopyOnWriteArrayList<>();
private final Set<String> failUrls = new CopyOnWriteArraySet<>();
private List<UserDefinedProcedure> userDefinedProcedures = new CopyOnWriteArrayList<>();
private List<BuiltInProcedure> builtInProcedures = new CopyOnWriteArrayList<>();

Expand Down Expand Up @@ -154,7 +157,7 @@ public String callGqlToLeader(String gql, String graph, double timeout, boolean
}

public String callProcedure(String procedureType, String procedureName, String param, double procedureTimeOut,
boolean inProcess, String graph) throws Exception {
boolean inProcess, String graph) throws Exception {
return callProcedure(procedureType, procedureName, param, procedureTimeOut, inProcess, graph, false);
}

Expand Down Expand Up @@ -188,7 +191,7 @@ public String callProcedure(String procedureType, String procedureName, String p
}

public String callProcedureToLeader(String procedureType, String procedureName, String param, double procedureTimeOut,
boolean inProcess, String graph) throws Exception {
boolean inProcess, String graph) throws Exception {
return callProcedureToLeader(procedureType, procedureName, param, procedureTimeOut, inProcess, graph, false);
}

Expand All @@ -202,7 +205,7 @@ public String callProcedureToLeader(String procedureType, String procedureName,
}

public boolean loadProcedure(String sourceFile, String procedureType, String procedureName, String codeType,
String procedureDescription, boolean readOnly, String version, String graph) throws Exception {
String procedureDescription, boolean readOnly, String version, String graph) throws Exception {
if (clientType == ClientType.SINGLE_CONNECTION) {
return baseClient.loadProcedure(sourceFile, procedureType, procedureName, codeType, procedureDescription, readOnly, version, graph);
} else {
Expand Down Expand Up @@ -356,7 +359,10 @@ private TuGraphSingleRpcClient getClient(Lgraph.ProtoGraphQueryType type, String

private TuGraphSingleRpcClient getClient(boolean isReadQuery) throws Exception {
if (isReadQuery) {
if (rpcClientPool.size() == 0){
if (CollectionUtils.isNotEmpty(failUrls)) {
loadRpcClient(failUrls);
}
if (CollectionUtils.isEmpty(rpcClientPool)) {
throw new Exception("all instance is down, refuse req!");
}
TuGraphSingleRpcClient rpcClient = rpcClientPool.get(rpcClientPool.size() - 1);
Expand Down Expand Up @@ -395,16 +401,27 @@ private void refreshClientPool() {
}
});
} else {
for (String url : urls) {
loadRpcClient(new HashSet<>(urls));
}
}

private void loadRpcClient(Set<String> urlList) {
Set<String> failList = new HashSet<>();
for (String url: urlList) {
try {
TuGraphSingleRpcClient rpcClient = new TuGraphSingleRpcClient("list://" + url, user, password);
String result = rpcClient.callCypher("CALL dbms.ha.clusterInfo()", "default", 10);
ClusterInfo clusterInfo = JSON.parseObject(JSON.parseArray(result).get(0).toString(), new TypeReference<ClusterInfo>(){});
if (clusterInfo.isMaster()) {
leaderClient = rpcClient;
}
rpcClientPool.add(rpcClient);
} catch (Exception e) {
failList.add(url);
}
}
failUrls.clear();
failUrls.addAll(failList);
}

/**
Expand Down Expand Up @@ -438,9 +455,9 @@ private <E> E doubleCheckQuery(QueryInterface<E> queryInterface) throws Exceptio
return queryInterface.method();
} catch (Exception e2) {
log.error(e2.getMessage());
throw e2;
}
}
return null;
}

private static class TuGraphSingleRpcClient {
Expand Down
Loading