use of com.google.cloud.spanner.Mutation in project beam by apache.
the class MutationSizeEstimatorTest method bytes.
@Test
public void bytes() throws Exception {
Mutation empty = Mutation.newInsertOrUpdateBuilder("test").set("one").to(ByteArray.fromBase64("")).build();
Mutation nullValue = Mutation.newInsertOrUpdateBuilder("test").set("one").to((ByteArray) null).build();
Mutation sample = Mutation.newInsertOrUpdateBuilder("test").set("one").to(ByteArray.fromBase64("abcdabcd")).build();
Mutation nullArray = Mutation.newInsertOrUpdateBuilder("test").set("one").toBytesArray(null).build();
assertThat(MutationSizeEstimator.sizeOf(empty), is(0L));
assertThat(MutationSizeEstimator.sizeOf(nullValue), is(0L));
assertThat(MutationSizeEstimator.sizeOf(sample), is(6L));
assertThat(MutationSizeEstimator.sizeOf(nullArray), is(0L));
}
use of com.google.cloud.spanner.Mutation 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));
}
use of com.google.cloud.spanner.Mutation 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);
}
use of com.google.cloud.spanner.Mutation in project beam by apache.
the class SpannerIOWriteTest method streamingWritesWithPriority.
@Test
public void streamingWritesWithPriority() throws Exception {
TestStream<Mutation> testStream = TestStream.create(SerializableCoder.of(Mutation.class)).addElements(m(1L), m(2L)).advanceProcessingTime(Duration.standardMinutes(1)).addElements(m(3L), m(4L)).advanceProcessingTime(Duration.standardMinutes(1)).addElements(m(5L), m(6L)).advanceWatermarkToInfinity();
Write write = SpannerIO.write().withProjectId("test-project").withInstanceId("test-instance").withDatabaseId("test-database").withServiceFactory(serviceFactory).withHighPriority();
pipeline.apply(testStream).apply(write);
pipeline.run();
assertEquals(RpcPriority.HIGH, write.getSpannerConfig().getRpcPriority().get());
verifyBatches(batch(m(1L), m(2L)), batch(m(3L), m(4L)), batch(m(5L), m(6L)));
}
use of com.google.cloud.spanner.Mutation in project beam by apache.
the class SpannerIOWriteTest method singleMutationPipeline.
@Test
public void singleMutationPipeline() throws Exception {
Mutation mutation = m(2L);
PCollection<Mutation> mutations = pipeline.apply(Create.of(mutation));
mutations.apply(SpannerIO.write().withProjectId("test-project").withInstanceId("test-instance").withDatabaseId("test-database").withServiceFactory(serviceFactory));
pipeline.run();
verifyBatches(batch(m(2L)));
}
Aggregations