com.google.cloud.spanner.SpannerExceptionFactory Java Examples
The following examples show how to use
com.google.cloud.spanner.SpannerExceptionFactory.
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: SpannerTransactionManagerTests.java From spring-cloud-gcp with Apache License 2.0 | 6 votes |
@Test public void testDoCommitDupeException() { this.expectedEx.expect(DuplicateKeyException.class); this.expectedEx.expectMessage("ALREADY_EXISTS; nested exception is " + "com.google.cloud.spanner.SpannerException: ALREADY_EXISTS: this is from a test"); SpannerException exception = SpannerExceptionFactory.newSpannerException( ErrorCode.ALREADY_EXISTS, "this is from a test"); when(transactionManager.getState()).thenReturn(TransactionState.STARTED); Mockito.doThrow(exception).when(transactionManager).commit(); tx.transactionManager = transactionManager; manager.doCommit(status); }
Example #2
Source File: RunningOperationsStoreTest.java From spanner-jdbc with MIT License | 6 votes |
private Operation<Void, UpdateDatabaseDdlMetadata> mockOperation(boolean error) { @SuppressWarnings("unchecked") Operation<Void, UpdateDatabaseDdlMetadata> op = mock(Operation.class); when(op.getName()).then(new Returns("TEST_OPERATION")); when(op.isDone()).then(new Answer<Boolean>() { @Override public Boolean answer(InvocationOnMock invocation) throws Throwable { return reportDone; } }); when(op.reload()).then(new Returns(op)); if (error) when(op.getResult()).thenThrow(SpannerExceptionFactory .newSpannerException(ErrorCode.INVALID_ARGUMENT, "Some exception")); else when(op.getResult()).then(new Returns(null)); UpdateDatabaseDdlMetadata metadata = UpdateDatabaseDdlMetadata.getDefaultInstance(); when(op.getMetadata()).then(new Returns(metadata)); return op; }
Example #3
Source File: CloudSpannerDriver.java From spanner-jdbc with MIT License | 6 votes |
/** * Closes all connections to Google Cloud Spanner that have been opened by this driver during the * lifetime of this application. You should call this method when you want to shutdown your * application, as this frees up all connections and sessions to Google Cloud Spanner. Failure to * do so, will keep sessions open server side and can eventually lead to resource exhaustion. Any * open JDBC connection to Cloud Spanner opened by this driver will also be closed by this method. * This method is also called automatically in a shutdown hook when the JVM is stopped orderly. */ public synchronized void closeSpanner() { try { for (Entry<Spanner, List<CloudSpannerConnection>> entry : connections.entrySet()) { List<CloudSpannerConnection> list = entry.getValue(); for (CloudSpannerConnection con : list) { if (!con.isClosed()) { con.rollback(); con.markClosed(); } } entry.getKey().close(); } connections.clear(); spanners.clear(); } catch (SQLException e) { throw SpannerExceptionFactory.newSpannerException(e); } }
Example #4
Source File: HelloSpannerTest.java From java-docs-samples with Apache License 2.0 | 5 votes |
private void setupFailedMockQuery() { ReadContext readContext = mock(ReadContext.class); when(readContext.executeQuery(Statement.of("SELECT SingerId, AlbumId, AlbumTitle FROM Albums"))) .thenThrow( SpannerExceptionFactory.newSpannerException( ErrorCode.NOT_FOUND, "Table `Albums` not found")); when(client.singleUse()).thenReturn(readContext); }
Example #5
Source File: CloudSpannerXAExceptionTest.java From spanner-jdbc with MIT License | 5 votes |
@Test public void testConstructors() { validateException( new CloudSpannerXAException("TEST", Code.FAILED_PRECONDITION, XAException.XAER_INVAL), null); validateException(new CloudSpannerXAException("TEST", new SQLException("TEST"), Code.FAILED_PRECONDITION, XAException.XAER_INVAL), SQLException.class); validateException(new CloudSpannerXAException("TEST", new CloudSpannerSQLException("TEST", SpannerExceptionFactory.newSpannerException(ErrorCode.FAILED_PRECONDITION, "TEST")), XAException.XAER_INVAL), CloudSpannerSQLException.class); }
Example #6
Source File: CloudSpannerTransaction.java From spanner-jdbc with MIT License | 5 votes |
@Override public BatchTransactionId getBatchTransactionId() { checkTransaction(); if (batchReadOnlyTransaction != null) { return batchReadOnlyTransaction.getBatchTransactionId(); } throw SpannerExceptionFactory.newSpannerException(ErrorCode.FAILED_PRECONDITION, METHOD_ONLY_IN_BATCH_READONLY); }
Example #7
Source File: CloudSpannerTransaction.java From spanner-jdbc with MIT License | 5 votes |
@Override public ResultSet execute(Partition partition) throws SpannerException { checkTransaction(); if (batchReadOnlyTransaction != null) { return batchReadOnlyTransaction.execute(partition); } throw SpannerExceptionFactory.newSpannerException(ErrorCode.FAILED_PRECONDITION, METHOD_ONLY_IN_BATCH_READONLY); }
Example #8
Source File: CloudSpannerTransaction.java From spanner-jdbc with MIT License | 5 votes |
@Override public List<Partition> partitionQuery(PartitionOptions partitionOptions, Statement statement, QueryOption... options) throws SpannerException { checkTransaction(); if (batchReadOnlyTransaction != null) { return batchReadOnlyTransaction.partitionQuery(partitionOptions, statement, options); } throw SpannerExceptionFactory.newSpannerException(ErrorCode.FAILED_PRECONDITION, METHOD_ONLY_IN_BATCH_READONLY); }
Example #9
Source File: CloudSpannerTransaction.java From spanner-jdbc with MIT License | 5 votes |
@Override public List<Partition> partitionReadUsingIndex(PartitionOptions partitionOptions, String table, String index, KeySet keys, Iterable<String> columns, ReadOption... options) throws SpannerException { throw SpannerExceptionFactory.newSpannerException(ErrorCode.UNIMPLEMENTED, METHOD_NOT_IMPLEMENTED); }
Example #10
Source File: CloudSpannerTransaction.java From spanner-jdbc with MIT License | 4 votes |
@Override public List<Partition> partitionRead(PartitionOptions partitionOptions, String table, KeySet keys, Iterable<String> columns, ReadOption... options) throws SpannerException { throw SpannerExceptionFactory.newSpannerException(ErrorCode.UNIMPLEMENTED, METHOD_NOT_IMPLEMENTED); }
Example #11
Source File: CloudSpannerTransaction.java From spanner-jdbc with MIT License | 4 votes |
@Override public ResultSet analyzeQuery(Statement statement, QueryAnalyzeMode queryMode) { throw SpannerExceptionFactory.newSpannerException(ErrorCode.UNIMPLEMENTED, METHOD_NOT_IMPLEMENTED); }
Example #12
Source File: CloudSpannerTransaction.java From spanner-jdbc with MIT License | 4 votes |
@Override public Struct readRowUsingIndex(String table, String index, Key key, Iterable<String> columns) { throw SpannerExceptionFactory.newSpannerException(ErrorCode.UNIMPLEMENTED, METHOD_NOT_IMPLEMENTED); }
Example #13
Source File: CloudSpannerTransaction.java From spanner-jdbc with MIT License | 4 votes |
@Override public Struct readRow(String table, Key key, Iterable<String> columns) { throw SpannerExceptionFactory.newSpannerException(ErrorCode.UNIMPLEMENTED, METHOD_NOT_IMPLEMENTED); }
Example #14
Source File: CloudSpannerTransaction.java From spanner-jdbc with MIT License | 4 votes |
@Override public ResultSet readUsingIndex(String table, String index, KeySet keys, Iterable<String> columns, ReadOption... options) { throw SpannerExceptionFactory.newSpannerException(ErrorCode.UNIMPLEMENTED, METHOD_NOT_IMPLEMENTED); }
Example #15
Source File: SpannerIOWriteTest.java From beam with Apache License 2.0 | 4 votes |
@Test public void reportFailures() throws Exception { MutationGroup[] mutationGroups = new MutationGroup[10]; for (int i = 0; i < mutationGroups.length; i++) { mutationGroups[i] = g(m((long) i)); } List<MutationGroup> mutationGroupList = Arrays.asList(mutationGroups); when(serviceFactory.mockDatabaseClient().writeAtLeastOnce(any())) .thenAnswer( invocationOnMock -> { Preconditions.checkNotNull(invocationOnMock.getArguments()[0]); throw SpannerExceptionFactory.newSpannerException(ErrorCode.ALREADY_EXISTS, "oops"); }); SpannerWriteResult result = pipeline .apply(Create.of(mutationGroupList)) .apply( SpannerIO.write() .withProjectId("test-project") .withInstanceId("test-instance") .withDatabaseId("test-database") .withServiceFactory(serviceFactory) .withBatchSizeBytes(0) .withFailureMode(SpannerIO.FailureMode.REPORT_FAILURES) .grouped()); PAssert.that(result.getFailedMutations()) .satisfies( m -> { assertEquals(mutationGroups.length, Iterables.size(m)); return null; }); PAssert.that(result.getFailedMutations()).containsInAnyOrder(mutationGroupList); pipeline.run().waitUntilFinish(); // writeAtLeastOnce called once for the batch of mutations // (which as they are unbatched = each mutation group) then again for the individual retry. verify(serviceFactory.mockDatabaseClient(), times(20)).writeAtLeastOnce(any()); }
Example #16
Source File: SpannerIOWriteTest.java From beam with Apache License 2.0 | 4 votes |
@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().writeAtLeastOnce(any())) .thenThrow( SpannerExceptionFactory.newSpannerException( ErrorCode.DEADLINE_EXCEEDED, "simulated Timeout 1")) .thenThrow( SpannerExceptionFactory.newSpannerException( ErrorCode.DEADLINE_EXCEEDED, "simulated Timeout 2")) .thenReturn(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)).writeAtLeastOnce(any()); }
Example #17
Source File: SpannerIOWriteTest.java From beam with Apache License 2.0 | 4 votes |
@Test public void deadlineExceededFailsAfterRetries() 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 all timeouts. when(serviceFactory.mockDatabaseClient().writeAtLeastOnce(any())) .thenThrow( SpannerExceptionFactory.newSpannerException( ErrorCode.DEADLINE_EXCEEDED, "simulated Timeout")); SpannerWriteResult result = pipeline .apply(Create.of(mutationList)) .apply( SpannerIO.write() .withProjectId("test-project") .withInstanceId("test-instance") .withDatabaseId("test-database") .withServiceFactory(serviceFactory) .withBatchSizeBytes(0) .withMaxCumulativeBackoff(Duration.standardHours(2)) .withFailureMode(SpannerIO.FailureMode.REPORT_FAILURES)); // One error PAssert.that(result.getFailedMutations()) .satisfies( m -> { assertEquals(1, Iterables.size(m)); return null; }); pipeline.run().waitUntilFinish(); // Due to jitter in backoff algorithm, we cannot test for an exact number of retries, // but there will be more than 16 (normally 18). int numSleeps = Mockito.mockingDetails(WriteToSpannerFn.sleeper).getInvocations().size(); assertTrue(String.format("Should be least 16 sleeps, got %d", numSleeps), numSleeps > 16); long totalSleep = Mockito.mockingDetails(WriteToSpannerFn.sleeper).getInvocations().stream() .mapToLong(i -> i.getArgument(0)) .reduce(0L, Long::sum); // Total sleep should be greater then 2x maxCumulativeBackoff: 120m, // because the batch is repeated inidividually due REPORT_FAILURES. assertTrue( String.format("Should be least 7200s of sleep, got %d", totalSleep), totalSleep >= Duration.standardHours(2).getMillis()); // Number of write attempts should be numSleeps + 2 write attempts: // 1 batch attempt, numSleeps/2 batch retries, // then 1 individual attempt + numSleeps/2 individual retries verify(serviceFactory.mockDatabaseClient(), times(numSleeps + 2)).writeAtLeastOnce(any()); }
Example #18
Source File: SpannerIOWriteTest.java From beam with Apache License 2.0 | 4 votes |
@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().writeAtLeastOnce(any())) .thenThrow(SpannerExceptionFactory.newSpannerException(ErrorCode.ABORTED, errString)) .thenThrow(SpannerExceptionFactory.newSpannerException(ErrorCode.ABORTED, errString)) .thenReturn(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)).writeAtLeastOnce(any()); }
Example #19
Source File: SpannerIOWriteTest.java From beam with Apache License 2.0 | 4 votes |
@Test public void retryMaxOnSchemaChangeException() 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 Aborted transaction when(serviceFactory.mockDatabaseClient().writeAtLeastOnce(any())) .thenThrow(SpannerExceptionFactory.newSpannerException(ErrorCode.ABORTED, errString)); // When spanner aborts transaction for more than 5 time, pipeline execution stops with // PipelineExecutionException thrown.expect(PipelineExecutionException.class); thrown.expectMessage(errString); 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)); // One error PAssert.that(result.getFailedMutations()) .satisfies( m -> { assertEquals(1, Iterables.size(m)); return null; }); pipeline.run().waitUntilFinish(); // 0 calls to sleeper verify(WriteToSpannerFn.sleeper, times(0)).sleep(anyLong()); // 5 write attempts for the single mutationGroup. verify(serviceFactory.mockDatabaseClient(), times(5)).writeAtLeastOnce(any()); }
Example #20
Source File: SpannerIOWriteTest.java From beam with Apache License 2.0 | 4 votes |
@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().writeAtLeastOnce(any())) .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(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)).writeAtLeastOnce(any()); }
Example #21
Source File: CloudSpannerTransaction.java From spanner-jdbc with MIT License | 4 votes |
@Override public ResultSet read(String table, KeySet keys, Iterable<String> columns, ReadOption... options) { throw SpannerExceptionFactory.newSpannerException(ErrorCode.UNIMPLEMENTED, METHOD_NOT_IMPLEMENTED); }