Skip to content

Commit f9c2471

Browse files
authored
Merge pull request #1235 from mziccard/datastore-aborted
Retry ABORTED Datastore commit only when NON_TRANSACTIONAL
2 parents 4d5a86c + 4015094 commit f9c2471

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

google-cloud-datastore/src/main/java/com/google/cloud/datastore/DatastoreException.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,24 @@ public final class DatastoreException extends BaseServiceException {
3434

3535
// see https://cloud.google.com/datastore/docs/concepts/errors#Error_Codes"
3636
private static final Set<Error> RETRYABLE_ERRORS = ImmutableSet.of(
37-
new Error(10, "ABORTED"), new Error(4, "DEADLINE_EXCEEDED"), new Error(14, "UNAVAILABLE"));
37+
new Error(10, "ABORTED", true),
38+
new Error(4, "DEADLINE_EXCEEDED", false),
39+
new Error(14, "UNAVAILABLE", true));
3840
private static final long serialVersionUID = 2663750991205874435L;
3941

4042
public DatastoreException(int code, String message, String reason) {
41-
this(code, message, reason, null);
43+
this(code, message, reason, true, null);
4244
}
4345

4446
public DatastoreException(int code, String message, String reason, Throwable cause) {
4547
super(code, message, reason, true, cause);
4648
}
4749

50+
public DatastoreException(int code, String message, String reason, boolean idempotent,
51+
Throwable cause) {
52+
super(code, message, reason, idempotent, cause);
53+
}
54+
4855
public DatastoreException(IOException exception) {
4956
super(exception, true);
5057
}

google-cloud-datastore/src/main/java/com/google/cloud/datastore/spi/DefaultDatastoreRpc.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ private static String removeScheme(String url) {
9393

9494
private static DatastoreException translate(
9595
com.google.datastore.v1.client.DatastoreException exception) {
96+
return translate(exception, true);
97+
}
98+
99+
private static DatastoreException translate(
100+
com.google.datastore.v1.client.DatastoreException exception, boolean idempotent) {
96101
String reason = "";
97102
if (exception.getCode() != null) {
98103
reason = exception.getCode().name();
@@ -103,15 +108,14 @@ private static DatastoreException translate(
103108
}
104109
}
105110
return new DatastoreException(
106-
exception.getCode().getNumber(), exception.getMessage(), reason, exception);
111+
exception.getCode().getNumber(), exception.getMessage(), reason, idempotent, exception);
107112
}
108113

109114
@Override
110115
public AllocateIdsResponse allocateIds(AllocateIdsRequest request) {
111116
try {
112117
return client.allocateIds(request);
113118
} catch (com.google.datastore.v1.client.DatastoreException ex) {
114-
115119
throw translate(ex);
116120
}
117121
}
@@ -130,7 +134,7 @@ public CommitResponse commit(CommitRequest request) {
130134
try {
131135
return client.commit(request);
132136
} catch (com.google.datastore.v1.client.DatastoreException ex) {
133-
throw translate(ex);
137+
throw translate(ex, request.getMode() == CommitRequest.Mode.NON_TRANSACTIONAL);
134138
}
135139
}
136140

0 commit comments

Comments
 (0)