Skip to content

Commit

Permalink
Fix ServiceOptions merge bug, remove unnecessary protected modifier f…
Browse files Browse the repository at this point in the history
…rom DateTime, set cursorAfter to endCursor where appropriate, and fix error codes for retries.
  • Loading branch information
Ajay Kannan committed Feb 2, 2016
1 parent fbe5a1d commit d27d567
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.api.client.extensions.appengine.http.UrlFetchTransport;
Expand Down Expand Up @@ -229,8 +228,7 @@ public B clock(Clock clock) {
* @return the builder
*/
public B projectId(String projectId) {
this.projectId =
checkNotNull(projectId, "Project ID cannot be set to null. Leave unset for default.");
this.projectId = projectId;
return self();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ public class DatastoreException extends BaseServiceException {

// see https://cloud.google.com/datastore/docs/concepts/errors#Error_Codes"
private static final Set<Error> RETRYABLE_ERRORS = ImmutableSet.of(
new Error(409, "ABORTED"),
new Error(403, "DEADLINE_EXCEEDED"),
new Error(503, "UNAVAILABLE"));
new Error(10, "ABORTED"), new Error(4, "DEADLINE_EXCEEDED"), new Error(14, "UNAVAILABLE"));
private static final long serialVersionUID = 2663750991205874435L;

public DatastoreException(int code, String message, String reason, Throwable cause) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ Object fromPb(byte[] bytesPb) throws InvalidProtocolBufferException {
com.google.protobuf.Timestamp.parseFrom(bytesPb)));
}

protected static long timestampPbToMicroseconds(com.google.protobuf.Timestamp timestampPb) {
static long timestampPbToMicroseconds(com.google.protobuf.Timestamp timestampPb) {
return timestampPb.getSeconds() * 1000000 + timestampPb.getNanos() / 1000;
}

protected static com.google.protobuf.Timestamp microsecondsToTimestampPb(long microseconds) {
static com.google.protobuf.Timestamp microsecondsToTimestampPb(long microseconds) {
long seconds = microseconds / 1000000;
int nanos = (int) (microseconds % 1000000) * 1000;
return com.google.protobuf.Timestamp.newBuilder().setSeconds(seconds).setNanos(nanos).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ protected T computeNext() {
sendRequest();
}
if (!entityResultPbIter.hasNext()) {
cursor = runQueryResponsePb.getBatch().getEndCursor();
return endOfData();
}
com.google.datastore.v1beta3.EntityResult entityResultPb = entityResultPbIter.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,29 @@ public class DatastoreExceptionTest {

@Test
public void testDatastoreException() throws Exception {
DatastoreException exception = new DatastoreException(409, "message", "ABORTED");
assertEquals(409, exception.code());
DatastoreException exception = new DatastoreException(10, "message", "ABORTED");
assertEquals(10, exception.code());
assertEquals("ABORTED", exception.reason());
assertEquals("message", exception.getMessage());
assertTrue(exception.retryable());
assertTrue(exception.idempotent());

exception = new DatastoreException(403, "message", "DEADLINE_EXCEEDED");
assertEquals(403, exception.code());
exception = new DatastoreException(4, "message", "DEADLINE_EXCEEDED");
assertEquals(4, exception.code());
assertEquals("DEADLINE_EXCEEDED", exception.reason());
assertEquals("message", exception.getMessage());
assertTrue(exception.retryable());
assertTrue(exception.idempotent());

exception = new DatastoreException(503, "message", "UNAVAILABLE");
assertEquals(503, exception.code());
exception = new DatastoreException(14, "message", "UNAVAILABLE");
assertEquals(14, exception.code());
assertEquals("UNAVAILABLE", exception.reason());
assertEquals("message", exception.getMessage());
assertTrue(exception.retryable());
assertTrue(exception.idempotent());

exception = new DatastoreException(500, "message", "INTERNAL");
assertEquals(500, exception.code());
exception = new DatastoreException(2, "message", "INTERNAL");
assertEquals(2, exception.code());
assertEquals("INTERNAL", exception.reason());
assertEquals("message", exception.getMessage());
assertFalse(exception.retryable());
Expand All @@ -77,14 +77,14 @@ public void testDatastoreException() throws Exception {

@Test
public void testTranslateAndThrow() throws Exception {
DatastoreException cause = new DatastoreException(503, "message", "UNAVAILABLE");
DatastoreException cause = new DatastoreException(14, "message", "UNAVAILABLE");
RetryHelper.RetryHelperException exceptionMock = createMock(RetryHelper.RetryHelperException.class);
expect(exceptionMock.getCause()).andReturn(cause).times(2);
replay(exceptionMock);
try {
DatastoreException.translateAndThrow(exceptionMock);
} catch (BaseServiceException ex) {
assertEquals(503, ex.code());
assertEquals(14, ex.code());
assertEquals("message", ex.getMessage());
assertTrue(ex.retryable());
assertTrue(ex.idempotent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,7 @@ public void testRetryableException() throws Exception {
EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(DatastoreOptions.class)))
.andReturn(rpcMock);
EasyMock.expect(rpcMock.lookup(requestPb))
.andThrow(new DatastoreException(503, "UNAVAILABLE", "UNAVAILABLE", null))
.andThrow(new DatastoreException(14, "UNAVAILABLE", "UNAVAILABLE", null))
.andReturn(responsePb);
EasyMock.replay(rpcFactoryMock, rpcMock);
DatastoreOptions options = this.options.toBuilder()
Expand Down

0 comments on commit d27d567

Please sign in to comment.