Skip to content

Commit 2ec0488

Browse files
githubgxllguojn1
authored andcommitted
[fix][dingo-sdk] Added preWrite to the problem of leader switching, reporting an error instead of SDK side retry
1 parent e96a80c commit 2ec0488

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

java/dingo-sdk/src/main/java/io/dingodb/sdk/common/utils/ErrorCodeUtils.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,10 @@ public static Strategy errorToStrategy(int code) {
9090
return Strategy.FAILED;
9191
}
9292

93+
public static boolean isPreWriteRequestFailed(int code, boolean isPreWrite) {
94+
return isPreWrite && (code == ERAFT_NOTLEADER_VALUE || code == ERAFT_COMMITLOG_VALUE);
95+
}
96+
97+
9398
public static final Function<Integer, Strategy> errorToStrategyFunc = ErrorCodeUtils::errorToStrategy;
9499
}

java/dingo-sdk/src/main/java/io/dingodb/sdk/service/caller/ServiceCaller.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.dingodb.sdk.common.DingoClientException.ExhaustedRetryException;
44
import io.dingodb.sdk.common.DingoClientException.InvalidRouteTableException;
55
import io.dingodb.sdk.common.DingoClientException.RequestErrorException;
6+
import io.dingodb.sdk.common.utils.ErrorCodeUtils;
67
import io.dingodb.sdk.service.JsonMessageUtils;
78
import io.dingodb.sdk.service.Caller;
89
import io.dingodb.sdk.service.ChannelProvider;
@@ -12,6 +13,7 @@
1213
import io.dingodb.sdk.service.entity.Message.Response;
1314
import io.dingodb.sdk.service.entity.error.Errno;
1415
import io.dingodb.sdk.service.entity.error.Error;
16+
import io.dingodb.sdk.service.entity.store.TxnPrewriteRequest;
1517
import io.grpc.CallOptions;
1618
import io.grpc.Channel;
1719
import io.grpc.MethodDescriptor;
@@ -25,6 +27,7 @@
2527
import java.util.function.Function;
2628

2729
import static io.dingodb.sdk.common.utils.ErrorCodeUtils.errorToStrategy;
30+
import static io.dingodb.sdk.common.utils.ErrorCodeUtils.isPreWriteRequestFailed;
2831

2932
@Slf4j
3033
public class ServiceCaller<S extends Service<S>> implements Caller<S> {
@@ -96,8 +99,10 @@ public <REQ extends Request, RES extends Response> RES call(
9699
errMsgs.compute(
97100
channel.authority() + ">>" + error.getErrmsg(), (k, v) -> v == null ? 1 : v + 1
98101
);
102+
boolean isPreWriteFailed = isPreWriteFailed(request);
99103
switch (handler.onErrStrategy(
100-
errorToStrategy(errCode),
104+
isPreWriteRequestFailed(errCode, isPreWriteFailed) ?
105+
ErrorCodeUtils.Strategy.FAILED : errorToStrategy(errCode),
101106
this.retry, retry, request, response, options, channel.authority(), requestId
102107
)) {
103108
case RETRY:
@@ -141,6 +146,13 @@ public <REQ extends Request, RES extends Response> RES call(
141146
throw generateException(methodName, requestId, lastRequest, connected, errMsgs, handler);
142147
}
143148

149+
private static <REQ extends Request> boolean isPreWriteFailed(REQ request) {
150+
if (request instanceof TxnPrewriteRequest) {
151+
return (((TxnPrewriteRequest) request).isTryOnePc() || ((TxnPrewriteRequest) request).isUseAsyncCommit());
152+
}
153+
return false;
154+
}
155+
144156
private void waitRetry() {
145157
LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(1));
146158
}

0 commit comments

Comments
 (0)