Search in sources :

Example 1 with CommitResponse

use of com.google.cloud.spanner.CommitResponse in project beam by apache.

the class SpannerIOWriteTest method retryOnAbortedAndDeadlineExceeded.

@Test
public void retryOnAbortedAndDeadlineExceeded() throws InterruptedException {
    List<Mutation> mutationList = Arrays.asList(m((long) 1));
    String errString = "Transaction aborted. " + "Database schema probably changed during transaction, retry may succeed.";
    // mock sleeper so that it does not actually sleep.
    WriteToSpannerFn.sleeper = Mockito.mock(Sleeper.class);
    // Respond with (1) Aborted transaction a couple of times (2) deadline exceeded
    // (3) Aborted transaction 3 times (4)  deadline exceeded and finally return success.
    when(serviceFactory.mockDatabaseClient().writeAtLeastOnceWithOptions(any(), any(ReadQueryUpdateTransactionOption.class))).thenThrow(SpannerExceptionFactory.newSpannerException(ErrorCode.ABORTED, errString)).thenThrow(SpannerExceptionFactory.newSpannerException(ErrorCode.ABORTED, errString)).thenThrow(SpannerExceptionFactory.newSpannerException(ErrorCode.DEADLINE_EXCEEDED, "simulated Timeout 1")).thenThrow(SpannerExceptionFactory.newSpannerException(ErrorCode.ABORTED, errString)).thenThrow(SpannerExceptionFactory.newSpannerException(ErrorCode.ABORTED, errString)).thenThrow(SpannerExceptionFactory.newSpannerException(ErrorCode.ABORTED, errString)).thenThrow(SpannerExceptionFactory.newSpannerException(ErrorCode.DEADLINE_EXCEEDED, "simulated Timeout 2")).thenReturn(new CommitResponse(Timestamp.now()));
    SpannerWriteResult result = pipeline.apply(Create.of(mutationList)).apply(SpannerIO.write().withProjectId("test-project").withInstanceId("test-instance").withDatabaseId("test-database").withServiceFactory(serviceFactory).withBatchSizeBytes(0).withFailureMode(FailureMode.FAIL_FAST));
    // Zero error
    PAssert.that(result.getFailedMutations()).satisfies(m -> {
        assertEquals(0, Iterables.size(m));
        return null;
    });
    pipeline.run().waitUntilFinish();
    // 2 calls to sleeper
    verify(WriteToSpannerFn.sleeper, times(2)).sleep(anyLong());
    // 8 write attempts for the single mutationGroup.
    verify(serviceFactory.mockDatabaseClient(), times(8)).writeAtLeastOnceWithOptions(any(), any(ReadQueryUpdateTransactionOption.class));
}
Also used : ReadQueryUpdateTransactionOption(com.google.cloud.spanner.Options.ReadQueryUpdateTransactionOption) CommitResponse(com.google.cloud.spanner.CommitResponse) Mutation(com.google.cloud.spanner.Mutation) Sleeper(org.apache.beam.sdk.util.Sleeper) Test(org.junit.Test)

Example 2 with CommitResponse

use of com.google.cloud.spanner.CommitResponse in project beam by apache.

the class SpannerIOWriteTest method retryOnSchemaChangeException.

@Test
public void retryOnSchemaChangeException() throws InterruptedException {
    List<Mutation> mutationList = Arrays.asList(m((long) 1));
    String errString = "Transaction aborted. " + "Database schema probably changed during transaction, retry may succeed.";
    // mock sleeper so that it does not actually sleep.
    WriteToSpannerFn.sleeper = Mockito.mock(Sleeper.class);
    // respond with 2 timeouts and a success.
    when(serviceFactory.mockDatabaseClient().writeAtLeastOnceWithOptions(any(), any(ReadQueryUpdateTransactionOption.class))).thenThrow(SpannerExceptionFactory.newSpannerException(ErrorCode.ABORTED, errString)).thenThrow(SpannerExceptionFactory.newSpannerException(ErrorCode.ABORTED, errString)).thenReturn(new CommitResponse(Timestamp.now()));
    SpannerWriteResult result = pipeline.apply(Create.of(mutationList)).apply(SpannerIO.write().withProjectId("test-project").withInstanceId("test-instance").withDatabaseId("test-database").withServiceFactory(serviceFactory).withBatchSizeBytes(0).withFailureMode(FailureMode.FAIL_FAST));
    // all success, so veryify no errors
    PAssert.that(result.getFailedMutations()).satisfies(m -> {
        assertEquals(0, Iterables.size(m));
        return null;
    });
    pipeline.run().waitUntilFinish();
    // 0 calls to sleeper
    verify(WriteToSpannerFn.sleeper, times(0)).sleep(anyLong());
    // 3 write attempts for the single mutationGroup.
    verify(serviceFactory.mockDatabaseClient(), times(3)).writeAtLeastOnceWithOptions(any(), any(ReadQueryUpdateTransactionOption.class));
}
Also used : ReadQueryUpdateTransactionOption(com.google.cloud.spanner.Options.ReadQueryUpdateTransactionOption) CommitResponse(com.google.cloud.spanner.CommitResponse) Mutation(com.google.cloud.spanner.Mutation) Sleeper(org.apache.beam.sdk.util.Sleeper) Test(org.junit.Test)

Example 3 with CommitResponse

use of com.google.cloud.spanner.CommitResponse in project beam by apache.

the class SpannerIOWriteTest method testSpannerWriteMetricIsSet.

@Test
public void testSpannerWriteMetricIsSet() {
    Mutation mutation = m(2L);
    PCollection<Mutation> mutations = pipeline.apply(Create.of(mutation));
    // respond with 2 error codes and a success.
    when(serviceFactory.mockDatabaseClient().writeAtLeastOnceWithOptions(any(), any(ReadQueryUpdateTransactionOption.class))).thenThrow(SpannerExceptionFactory.newSpannerException(ErrorCode.DEADLINE_EXCEEDED, "Simulated Timeout 1")).thenThrow(SpannerExceptionFactory.newSpannerException(ErrorCode.DEADLINE_EXCEEDED, "Simulated Timeout 2")).thenReturn(new CommitResponse(Timestamp.now()));
    mutations.apply(SpannerIO.write().withProjectId("test-project").withInstanceId("test-instance").withDatabaseId("test-database").withFailureMode(FailureMode.FAIL_FAST).withServiceFactory(serviceFactory));
    pipeline.run();
    verifyMetricWasSet("test-project", "test-database", "test-instance", "Write", "deadline_exceeded", 2);
    verifyMetricWasSet("test-project", "test-database", "test-instance", "Write", "ok", 1);
}
Also used : ReadQueryUpdateTransactionOption(com.google.cloud.spanner.Options.ReadQueryUpdateTransactionOption) CommitResponse(com.google.cloud.spanner.CommitResponse) Mutation(com.google.cloud.spanner.Mutation) Test(org.junit.Test)

Example 4 with CommitResponse

use of com.google.cloud.spanner.CommitResponse in project beam by apache.

the class SpannerIOWriteTest method deadlineExceededRetries.

@Test
public void deadlineExceededRetries() throws InterruptedException {
    List<Mutation> mutationList = Arrays.asList(m((long) 1));
    // mock sleeper so that it does not actually sleep.
    WriteToSpannerFn.sleeper = Mockito.mock(Sleeper.class);
    // respond with 2 timeouts and a success.
    when(serviceFactory.mockDatabaseClient().writeAtLeastOnceWithOptions(any(), any(ReadQueryUpdateTransactionOption.class))).thenThrow(SpannerExceptionFactory.newSpannerException(ErrorCode.DEADLINE_EXCEEDED, "simulated Timeout 1")).thenThrow(SpannerExceptionFactory.newSpannerException(ErrorCode.DEADLINE_EXCEEDED, "simulated Timeout 2")).thenReturn(new CommitResponse(Timestamp.now()));
    SpannerWriteResult result = pipeline.apply(Create.of(mutationList)).apply(SpannerIO.write().withProjectId("test-project").withInstanceId("test-instance").withDatabaseId("test-database").withServiceFactory(serviceFactory).withBatchSizeBytes(0).withFailureMode(SpannerIO.FailureMode.REPORT_FAILURES));
    // all success, so veryify no errors
    PAssert.that(result.getFailedMutations()).satisfies(m -> {
        assertEquals(0, Iterables.size(m));
        return null;
    });
    pipeline.run().waitUntilFinish();
    // 2 calls to sleeper
    verify(WriteToSpannerFn.sleeper, times(2)).sleep(anyLong());
    // 3 write attempts for the single mutationGroup.
    verify(serviceFactory.mockDatabaseClient(), times(3)).writeAtLeastOnceWithOptions(any(), any(ReadQueryUpdateTransactionOption.class));
}
Also used : ReadQueryUpdateTransactionOption(com.google.cloud.spanner.Options.ReadQueryUpdateTransactionOption) CommitResponse(com.google.cloud.spanner.CommitResponse) Mutation(com.google.cloud.spanner.Mutation) Sleeper(org.apache.beam.sdk.util.Sleeper) Test(org.junit.Test)

Aggregations

CommitResponse (com.google.cloud.spanner.CommitResponse)4 Mutation (com.google.cloud.spanner.Mutation)4 ReadQueryUpdateTransactionOption (com.google.cloud.spanner.Options.ReadQueryUpdateTransactionOption)4 Test (org.junit.Test)4 Sleeper (org.apache.beam.sdk.util.Sleeper)3