com.google.appengine.api.datastore.DatastoreTimeoutException Java Examples

The following examples show how to use com.google.appengine.api.datastore.DatastoreTimeoutException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WhoisActionTest.java    From nomulus with Apache License 2.0 6 votes vote down vote up
@Test
public void testRun_retryOnTransientFailure() throws Exception {
  persistResource(loadRegistrar("TheRegistrar").asBuilder().setUrl("http://my.fake.url").build());
  persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4"));
  WhoisAction action = newWhoisAction("ns1.cat.lol");
  WhoisResponse expectedResponse =
      action
          .whoisReader
          .readCommand(action.input, false, clock.nowUtc())
          .executeQuery(clock.nowUtc());

  WhoisReader mockReader = mock(WhoisReader.class);
  WhoisCommand mockCommand = mock(WhoisCommand.class);
  when(mockReader.readCommand(any(Reader.class), eq(false), any(DateTime.class)))
      .thenReturn(mockCommand);
  when(mockCommand.executeQuery(any(DateTime.class)))
      .thenThrow(new DatastoreFailureException("Expected transient exception #1"))
      .thenThrow(new DatastoreTimeoutException("Expected transient exception #2"))
      .thenReturn(expectedResponse);

  action.whoisReader = mockReader;
  action.run();
  assertThat(response.getPayload()).isEqualTo(loadFile("whois_action_nameserver.txt"));
}
 
Example #2
Source File: OfyTest.java    From nomulus with Apache License 2.0 6 votes vote down vote up
@Test
public void testTransact_datastoreTimeoutException_noManifest_retries() {
  assertThat(
          tm().transact(
                  new Supplier<Integer>() {

                    int count = 0;

                    @Override
                    public Integer get() {
                      // We don't write anything in this transaction, so there is no commit log
                      // manifest.
                      // Therefore it's always safe to retry since nothing got written.
                      count++;
                      if (count == 3) {
                        return count;
                      }
                      throw new DatastoreTimeoutException("");
                    }
                  }))
      .isEqualTo(3);
}
 
Example #3
Source File: OfyTest.java    From nomulus with Apache License 2.0 6 votes vote down vote up
@Test
public void testTransact_datastoreTimeoutException_manifestNotWrittenToDatastore_retries() {
  assertThat(
          tm().transact(
                  new Supplier<Integer>() {

                    int count = 0;

                    @Override
                    public Integer get() {
                      // There will be something in the manifest now, but it won't be committed if
                      // we throw.
                      ofy().save().entity(someObject);
                      count++;
                      if (count == 3) {
                        return count;
                      }
                      throw new DatastoreTimeoutException("");
                    }
                  }))
      .isEqualTo(3);
}
 
Example #4
Source File: AutomatedActionExceptionTestAction.java    From teammates with GNU General Public License v2.0 6 votes vote down vote up
@Override
@SuppressWarnings("PMD.AvoidThrowingNullPointerException") // deliberately done for testing
public void execute() {
    String error = getNonNullRequestParamValue(Const.ParamsNames.ERROR);
    if (error.equals(AssertionError.class.getSimpleName())) {
        throw new AssertionError("AssertionError testing");
    }
    if (error.equals(NullPointerException.class.getSimpleName())) {
        throw new NullPointerException("NullPointerException testing");
    }
    if (error.equals(DeadlineExceededException.class.getSimpleName())) {
        throw new DeadlineExceededException("DeadlineExceededException testing");
    }
    if (error.equals(DatastoreTimeoutException.class.getSimpleName())) {
        throw new DatastoreTimeoutException("DatastoreTimeoutException testing");
    }
    if (error.equals(InvalidHttpParameterException.class.getSimpleName())) {
        throw new InvalidHttpParameterException("InvalidHttpParameterException testing");
    }
}
 
Example #5
Source File: Ofy.java    From nomulus with Apache License 2.0 5 votes vote down vote up
/**
 * Transact with commit logs and retry with exponential backoff.
 *
 * <p>This method is broken out from {@link #transactNew(Work)} for testing purposes.
 */
@VisibleForTesting
<R> R transactCommitLoggedWork(CommitLoggedWork<R> work) {
  long baseRetryMillis = getBaseOfyRetryDuration().getMillis();
  for (long attempt = 0, sleepMillis = baseRetryMillis;
      true;
      attempt++, sleepMillis *= 2) {
    try {
      ofy().transactNew(() -> {
        work.run();
        return null;
      });
      return work.getResult();
    } catch (TransientFailureException
        | TimestampInversionException
        | DatastoreTimeoutException
        | DatastoreFailureException e) {
      // TransientFailureExceptions come from task queues and always mean nothing committed.
      // TimestampInversionExceptions are thrown by our code and are always retryable as well.
      // However, Datastore exceptions might get thrown even if the transaction succeeded.
      if ((e instanceof DatastoreTimeoutException || e instanceof DatastoreFailureException)
          && checkIfAlreadySucceeded(work)) {
        return work.getResult();
      }
      if (attempt == NUM_RETRIES) {
        throw e;  // Give up.
      }
      sleeper.sleepUninterruptibly(Duration.millis(sleepMillis));
      logger.atInfo().withCause(e).log(
          "Retrying %s, attempt %d", e.getClass().getSimpleName(), attempt);
    }
  }
}
 
Example #6
Source File: OfyTest.java    From nomulus with Apache License 2.0 5 votes vote down vote up
@Test
public void testTransact_datastoreTimeoutException_manifestWrittenToDatastore_returnsSuccess() {
  // A work unit that throws if it is ever retried.
  Supplier work =
      new Supplier<Void>() {
        boolean firstCallToVrun = true;

        @Override
        public Void get() {
          if (firstCallToVrun) {
            firstCallToVrun = false;
            ofy().save().entity(someObject);
            return null;
          }
          fail("Shouldn't have retried.");
          return null;
        }
      };
  // A commit logged work that throws on the first attempt to get its result.
  CommitLoggedWork<Void> commitLoggedWork = new CommitLoggedWork<Void>(work, new SystemClock()) {
    boolean firstCallToGetResult = true;

    @Override
    public Void getResult() {
      if (firstCallToGetResult) {
        firstCallToGetResult = false;
        throw new DatastoreTimeoutException("");
      }
      return null;
    }};
  // Despite the DatastoreTimeoutException in the first call to getResult(), this should succeed
  // without retrying. If a retry is triggered, the test should fail due to the call to fail().
  ofy().transactCommitLoggedWork(commitLoggedWork);
}
 
Example #7
Source File: AdminExceptionTestAction.java    From teammates with GNU General Public License v2.0 5 votes vote down vote up
@Override
@SuppressWarnings("PMD.AvoidThrowingNullPointerException") // deliberately done for testing
public JsonResult execute() {
    String error = getNonNullRequestParamValue(Const.ParamsNames.ERROR);
    if (error.equals(AssertionError.class.getSimpleName())) {
        throw new AssertionError("AssertionError testing");
    }
    if (error.equals(NullPointerException.class.getSimpleName())) {
        throw new NullPointerException("NullPointerException testing");
    }
    if (error.equals(DeadlineExceededException.class.getSimpleName())) {
        throw new DeadlineExceededException("DeadlineExceededException testing");
    }
    if (error.equals(DatastoreTimeoutException.class.getSimpleName())) {
        throw new DatastoreTimeoutException("DatastoreTimeoutException testing");
    }
    if (error.equals(InvalidHttpParameterException.class.getSimpleName())) {
        throw new InvalidHttpParameterException("InvalidHttpParameterException testing");
    }
    if (error.equals(UnauthorizedAccessException.class.getSimpleName())) {
        throw new UnauthorizedAccessException("UnauthorizedAccessException testing");
    }
    if (error.equals(EntityNotFoundException.class.getSimpleName())) {
        throw new EntityNotFoundException(new EntityDoesNotExistException("EntityNotFoundException testing"));
    }
    return new JsonResult("Test output");
}
 
Example #8
Source File: DatastoreSessionStore.java    From appengine-java-vm-runtime with Apache License 2.0 5 votes vote down vote up
@Override
public void saveSession(String key, SessionData data) throws Retryable {
  try {
    datastore.put(createEntityForSession(key, data));
  } catch (DatastoreTimeoutException e) {
    throw new Retryable(e);
  }
}
 
Example #9
Source File: SessionManagerTest.java    From appengine-java-vm-runtime with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public byte[] makeSyncCall(
    ApiProxy.Environment environment, String packageName, String methodName, byte[] request)
    throws ApiProxyException {
  if (packageName.equals("datastore_v3") && timeoutCount > 0) {
    timeoutCount--;
    throw new DatastoreTimeoutException("Timeout");
  }
  return delegate.makeSyncCall(environment, packageName, methodName, request);
}
 
Example #10
Source File: SessionManagerTest.java    From appengine-java-vm-runtime with Apache License 2.0 5 votes vote down vote up
public Future<byte[]> makeAsyncCall(
    ApiProxy.Environment environment,
    String packageName,
    String methodName,
    byte[] request,
    ApiProxy.ApiConfig apiConfig) {
  if (packageName.equals("datastore_v3") && timeoutCount > 0) {
    timeoutCount--;
    throw new DatastoreTimeoutException("Timeout");
  }
  return delegate.makeAsyncCall(environment, packageName, methodName, request, apiConfig);
}
 
Example #11
Source File: OfyTest.java    From nomulus with Apache License 2.0 4 votes vote down vote up
@Test
public void testTransactNewReadOnly_datastoreTimeoutException_retries() {
  doReadOnlyRetryTest(new DatastoreTimeoutException(""));
}
 
Example #12
Source File: SystemErrorEmailReportE2ETest.java    From teammates with GNU General Public License v2.0 3 votes vote down vote up
private void testDatastoreTimeoutException() {

        ______TS("DatastoreTimeoutException testing");

        String url = createUrl(Const.ResourceURIs.EXCEPTION)
                .withParam(Const.ParamsNames.ERROR, DatastoreTimeoutException.class.getSimpleName())
                .toString();

        BackDoor.executeGetRequest(url, null);

        print("DatastoreTimeoutException triggered, please check your crash report at " + Config.SUPPORT_EMAIL);

    }
 
Example #13
Source File: WebApiServletTest.java    From teammates with GNU General Public License v2.0 2 votes vote down vote up
@Test
public void allTests() throws Exception {

    ______TS("Typical case: valid action mapping");

    setupMocks(HttpGet.METHOD_NAME, Const.ResourceURIs.EXCEPTION);
    mockRequest.addParam(Const.ParamsNames.ERROR, "NoException");

    SERVLET.doGet(mockRequest, mockResponse);
    assertEquals(HttpStatus.SC_OK, mockResponse.getStatus());

    ______TS("Failure case: invalid action mapping");

    setupMocks(HttpGet.METHOD_NAME, "nonexistent");

    SERVLET.doGet(mockRequest, mockResponse);
    assertEquals(HttpStatus.SC_NOT_FOUND, mockResponse.getStatus());

    setupMocks(HttpPost.METHOD_NAME, Const.ResourceURIs.EXCEPTION);

    SERVLET.doGet(mockRequest, mockResponse);
    assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, mockResponse.getStatus());

    ______TS("Failure case: NullHttpParameterException");

    setupMocks(HttpGet.METHOD_NAME, Const.ResourceURIs.EXCEPTION);

    SERVLET.doGet(mockRequest, mockResponse);
    assertEquals(HttpStatus.SC_BAD_REQUEST, mockResponse.getStatus());

    ______TS("Failure case: InvalidHttpParameterException");

    setupMocks(HttpGet.METHOD_NAME, Const.ResourceURIs.EXCEPTION);
    mockRequest.addParam(Const.ParamsNames.ERROR, InvalidHttpParameterException.class.getSimpleName());

    SERVLET.doGet(mockRequest, mockResponse);
    assertEquals(HttpStatus.SC_BAD_REQUEST, mockResponse.getStatus());

    ______TS("Failure case: DeadlineExceededException");

    setupMocks(HttpGet.METHOD_NAME, Const.ResourceURIs.EXCEPTION);
    mockRequest.addParam(Const.ParamsNames.ERROR, DeadlineExceededException.class.getSimpleName());

    SERVLET.doGet(mockRequest, mockResponse);
    assertEquals(HttpStatus.SC_GATEWAY_TIMEOUT, mockResponse.getStatus());

    ______TS("Failure case: DatastoreTimeoutException");

    setupMocks(HttpGet.METHOD_NAME, Const.ResourceURIs.EXCEPTION);
    mockRequest.addParam(Const.ParamsNames.ERROR, DatastoreTimeoutException.class.getSimpleName());

    SERVLET.doGet(mockRequest, mockResponse);
    assertEquals(HttpStatus.SC_GATEWAY_TIMEOUT, mockResponse.getStatus());

    ______TS("Failure case: UnauthorizedAccessException");

    setupMocks(HttpGet.METHOD_NAME, Const.ResourceURIs.EXCEPTION);
    mockRequest.addParam(Const.ParamsNames.ERROR, UnauthorizedAccessException.class.getSimpleName());

    SERVLET.doGet(mockRequest, mockResponse);
    assertEquals(HttpStatus.SC_FORBIDDEN, mockResponse.getStatus());

    ______TS("Failure case: EntityNotFoundException");

    setupMocks(HttpGet.METHOD_NAME, Const.ResourceURIs.EXCEPTION);
    mockRequest.addParam(Const.ParamsNames.ERROR, EntityNotFoundException.class.getSimpleName());

    SERVLET.doGet(mockRequest, mockResponse);
    assertEquals(HttpStatus.SC_NOT_FOUND, mockResponse.getStatus());

    ______TS("Failure case: NullPointerException");

    setupMocks(HttpGet.METHOD_NAME, Const.ResourceURIs.EXCEPTION);
    mockRequest.addParam(Const.ParamsNames.ERROR, NullPointerException.class.getSimpleName());

    SERVLET.doGet(mockRequest, mockResponse);
    assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, mockResponse.getStatus());

    ______TS("Failure case: AssertionError");

    setupMocks(HttpGet.METHOD_NAME, Const.ResourceURIs.EXCEPTION);
    mockRequest.addParam(Const.ParamsNames.ERROR, AssertionError.class.getSimpleName());

    SERVLET.doGet(mockRequest, mockResponse);
    assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, mockResponse.getStatus());

}
 
Example #14
Source File: AutomatedServletTest.java    From teammates with GNU General Public License v2.0 2 votes vote down vote up
@Test
public void allTests() {

    ______TS("Typical case: valid action mapping");

    setupMocks(Const.CronJobURIs.AUTOMATED_FEEDBACK_OPENING_REMINDERS);

    SERVLET.doGet(mockRequest, mockResponse);
    assertEquals(HttpStatus.SC_OK, mockResponse.getStatus());

    ______TS("\"Successful\" case: invalid action mapping");

    setupMocks("/auto/mappingDoesNotExist");

    SERVLET.doGet(mockRequest, mockResponse);
    assertEquals(HttpStatus.SC_OK, mockResponse.getStatus());

    ______TS("\"Successful\" case: NullHttpParameterException");

    setupMocks(Const.CronJobURIs.AUTOMATED_EXCEPTION_TEST);

    SERVLET.doGet(mockRequest, mockResponse);
    assertEquals(HttpStatus.SC_OK, mockResponse.getStatus());

    ______TS("\"Successful\" case: InvalidHttpParameterException");

    setupMocks(Const.CronJobURIs.AUTOMATED_EXCEPTION_TEST);
    mockRequest.addParam(Const.ParamsNames.ERROR, InvalidHttpParameterException.class.getSimpleName());

    SERVLET.doGet(mockRequest, mockResponse);
    assertEquals(HttpStatus.SC_OK, mockResponse.getStatus());

    ______TS("Failure case: DeadlineExceededException");

    setupMocks(Const.CronJobURIs.AUTOMATED_EXCEPTION_TEST);
    mockRequest.addParam(Const.ParamsNames.ERROR, DeadlineExceededException.class.getSimpleName());

    SERVLET.doGet(mockRequest, mockResponse);
    assertEquals(HttpStatus.SC_GATEWAY_TIMEOUT, mockResponse.getStatus());

    ______TS("Failure case: DatastoreTimeoutException");

    setupMocks(Const.CronJobURIs.AUTOMATED_EXCEPTION_TEST);
    mockRequest.addParam(Const.ParamsNames.ERROR, DatastoreTimeoutException.class.getSimpleName());

    SERVLET.doGet(mockRequest, mockResponse);
    assertEquals(HttpStatus.SC_GATEWAY_TIMEOUT, mockResponse.getStatus());

    ______TS("Failure case: NullPointerException");

    setupMocks(Const.CronJobURIs.AUTOMATED_EXCEPTION_TEST);
    mockRequest.addParam(Const.ParamsNames.ERROR, NullPointerException.class.getSimpleName());

    SERVLET.doGet(mockRequest, mockResponse);
    assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, mockResponse.getStatus());

    ______TS("Failure case: AssertionError");

    setupMocks(Const.CronJobURIs.AUTOMATED_EXCEPTION_TEST);
    mockRequest.addParam(Const.ParamsNames.ERROR, AssertionError.class.getSimpleName());

    SERVLET.doGet(mockRequest, mockResponse);
    assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, mockResponse.getStatus());

}