Search in sources :

Example 16 with Connection

use of com.google.cloud.spanner.connection.Connection in project java-spanner by googleapis.

the class ITAsyncTransactionRetryTest method testAbortWithConcurrentDelete.

@Test
public void testAbortWithConcurrentDelete() {
    assumeFalse("concurrent transactions are not supported on the emulator", isUsingEmulator());
    AbortInterceptor interceptor = new AbortInterceptor(0);
    // first insert two test records
    try (ITConnection connection = createConnection()) {
        connection.executeUpdateAsync(Statement.of("INSERT INTO TEST (ID, NAME) VALUES (1, 'test 1')"));
        connection.executeUpdateAsync(Statement.of("INSERT INTO TEST (ID, NAME) VALUES (2, 'test 2')"));
        get(connection.commitAsync());
    }
    // open a new connection and select the two test records
    try (ITConnection connection = createConnection(interceptor, new CountTransactionRetryListener())) {
        // select the test records and consume the entire result set
        try (AsyncResultSet rs = connection.executeQueryAsync(Statement.of("SELECT * FROM TEST ORDER BY ID"))) {
            get(rs.setCallback(executor, resultSet -> {
                while (true) {
                    switch(resultSet.tryNext()) {
                        case DONE:
                            return CallbackResponse.DONE;
                        case NOT_READY:
                            return CallbackResponse.CONTINUE;
                        case OK:
                            break;
                    }
                }
            }));
        }
        // open a new connection and transaction and remove one of the test records
        try (ITConnection connection2 = createConnection()) {
            connection2.executeUpdateAsync(Statement.of("DELETE FROM TEST WHERE ID=1"));
            get(connection2.commitAsync());
        }
        // now try to do an insert that will abort. The retry should now fail as there has been a
        // concurrent modification
        interceptor.setProbability(1.0);
        interceptor.setOnlyInjectOnce(true);
        try {
            get(connection.executeUpdateAsync(Statement.of("INSERT INTO TEST (ID, NAME) VALUES (3, 'test 3')")));
            fail("Missing expected exception");
        } catch (AbortedDueToConcurrentModificationException e) {
            assertRetryStatistics(1, 1, 0);
        }
    }
}
Also used : Assume.assumeFalse(org.junit.Assume.assumeFalse) RunWith(org.junit.runner.RunWith) SpannerExceptionFactory(com.google.cloud.spanner.SpannerExceptionFactory) TransactionRetryListener(com.google.cloud.spanner.connection.TransactionRetryListener) Timestamp(com.google.cloud.Timestamp) Connection(com.google.cloud.spanner.connection.Connection) Level(java.util.logging.Level) ResultSet(com.google.cloud.spanner.ResultSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestName(org.junit.rules.TestName) After(org.junit.After) Assert.fail(org.junit.Assert.fail) AbortedDueToConcurrentModificationException(com.google.cloud.spanner.AbortedDueToConcurrentModificationException) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) AbortedException(com.google.cloud.spanner.AbortedException) AsyncResultSet(com.google.cloud.spanner.AsyncResultSet) ParallelIntegrationTest(com.google.cloud.spanner.ParallelIntegrationTest) AfterClass(org.junit.AfterClass) CallbackResponse(com.google.cloud.spanner.AsyncResultSet.CallbackResponse) SpannerApiFutures.get(com.google.cloud.spanner.SpannerApiFutures.get) EmulatorSpannerHelper.isUsingEmulator(com.google.cloud.spanner.testing.EmulatorSpannerHelper.isUsingEmulator) Test(org.junit.Test) Mutation(com.google.cloud.spanner.Mutation) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) ITAbstractSpannerTest(com.google.cloud.spanner.connection.ITAbstractSpannerTest) Logger(java.util.logging.Logger) Category(org.junit.experimental.categories.Category) Options(com.google.cloud.spanner.Options) Executors(java.util.concurrent.Executors) ApiFuture(com.google.api.core.ApiFuture) SettableApiFuture(com.google.api.core.SettableApiFuture) KeySet(com.google.cloud.spanner.KeySet) ErrorCode(com.google.cloud.spanner.ErrorCode) Statement(com.google.cloud.spanner.Statement) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) Rule(org.junit.Rule) Struct(com.google.cloud.spanner.Struct) AsyncResultSet(com.google.cloud.spanner.AsyncResultSet) AbortedDueToConcurrentModificationException(com.google.cloud.spanner.AbortedDueToConcurrentModificationException) ParallelIntegrationTest(com.google.cloud.spanner.ParallelIntegrationTest) Test(org.junit.Test) ITAbstractSpannerTest(com.google.cloud.spanner.connection.ITAbstractSpannerTest)

Example 17 with Connection

use of com.google.cloud.spanner.connection.Connection in project java-spanner by googleapis.

the class ITAsyncTransactionRetryTest method testAbortWithConcurrentInsert.

@Test
public void testAbortWithConcurrentInsert() {
    assumeFalse("concurrent transactions are not supported on the emulator", isUsingEmulator());
    AbortInterceptor interceptor = new AbortInterceptor(0);
    try (ITConnection connection = createConnection(interceptor, new CountTransactionRetryListener())) {
        // insert two test records
        connection.executeUpdateAsync(Statement.of("INSERT INTO TEST (ID, NAME) VALUES (1, 'test 1')"));
        connection.executeUpdateAsync(Statement.of("INSERT INTO TEST (ID, NAME) VALUES (2, 'test 2')"));
        // select the test records and consume the entire result set
        try (AsyncResultSet rs = connection.executeQueryAsync(Statement.of("SELECT * FROM TEST ORDER BY ID"))) {
            get(rs.setCallback(executor, resultSet -> {
                while (true) {
                    switch(resultSet.tryNext()) {
                        case DONE:
                            return CallbackResponse.DONE;
                        case NOT_READY:
                            return CallbackResponse.CONTINUE;
                        case OK:
                            break;
                    }
                }
            }));
        }
        // open a new connection and transaction and do an additional insert
        try (ITConnection connection2 = createConnection()) {
            connection2.executeUpdateAsync(Statement.of("INSERT INTO TEST (ID, NAME) VALUES (3, 'test 3')"));
            get(connection2.commitAsync());
        }
        // now try to do an insert that will abort. The retry should now fail as there has been a
        // concurrent modification
        interceptor.setProbability(1.0);
        interceptor.setOnlyInjectOnce(true);
        ApiFuture<Long> updateCount = connection.executeUpdateAsync(Statement.of("INSERT INTO TEST (ID, NAME) VALUES (4, 'test 4')"));
        try {
            get(updateCount);
            fail("Missing expected exception");
        } catch (AbortedDueToConcurrentModificationException e) {
            assertRetryStatistics(1, 1, 0);
        }
    }
}
Also used : Assume.assumeFalse(org.junit.Assume.assumeFalse) RunWith(org.junit.runner.RunWith) SpannerExceptionFactory(com.google.cloud.spanner.SpannerExceptionFactory) TransactionRetryListener(com.google.cloud.spanner.connection.TransactionRetryListener) Timestamp(com.google.cloud.Timestamp) Connection(com.google.cloud.spanner.connection.Connection) Level(java.util.logging.Level) ResultSet(com.google.cloud.spanner.ResultSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestName(org.junit.rules.TestName) After(org.junit.After) Assert.fail(org.junit.Assert.fail) AbortedDueToConcurrentModificationException(com.google.cloud.spanner.AbortedDueToConcurrentModificationException) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) AbortedException(com.google.cloud.spanner.AbortedException) AsyncResultSet(com.google.cloud.spanner.AsyncResultSet) ParallelIntegrationTest(com.google.cloud.spanner.ParallelIntegrationTest) AfterClass(org.junit.AfterClass) CallbackResponse(com.google.cloud.spanner.AsyncResultSet.CallbackResponse) SpannerApiFutures.get(com.google.cloud.spanner.SpannerApiFutures.get) EmulatorSpannerHelper.isUsingEmulator(com.google.cloud.spanner.testing.EmulatorSpannerHelper.isUsingEmulator) Test(org.junit.Test) Mutation(com.google.cloud.spanner.Mutation) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) ITAbstractSpannerTest(com.google.cloud.spanner.connection.ITAbstractSpannerTest) Logger(java.util.logging.Logger) Category(org.junit.experimental.categories.Category) Options(com.google.cloud.spanner.Options) Executors(java.util.concurrent.Executors) ApiFuture(com.google.api.core.ApiFuture) SettableApiFuture(com.google.api.core.SettableApiFuture) KeySet(com.google.cloud.spanner.KeySet) ErrorCode(com.google.cloud.spanner.ErrorCode) Statement(com.google.cloud.spanner.Statement) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) Rule(org.junit.Rule) Struct(com.google.cloud.spanner.Struct) AsyncResultSet(com.google.cloud.spanner.AsyncResultSet) AtomicLong(java.util.concurrent.atomic.AtomicLong) AbortedDueToConcurrentModificationException(com.google.cloud.spanner.AbortedDueToConcurrentModificationException) ParallelIntegrationTest(com.google.cloud.spanner.ParallelIntegrationTest) Test(org.junit.Test) ITAbstractSpannerTest(com.google.cloud.spanner.connection.ITAbstractSpannerTest)

Example 18 with Connection

use of com.google.cloud.spanner.connection.Connection in project pgadapter by GoogleCloudPlatform.

the class MutationWriter method writeToSpanner.

/**
 * Write list of mutations to Spanner and reset for next batch
 *
 * @return count of the number of rows updated.
 */
public int writeToSpanner(ConnectionHandler connectionHandler) {
    if (this.mutations.isEmpty()) {
        return 0;
    }
    Connection connection = connectionHandler.getSpannerConnection();
    // Write mutation to spanner
    connection.write(this.mutations);
    // Reset mutations, mutation counter, and batch size count for a new batch
    this.mutations = new ArrayList<>();
    this.mutationCount = 0;
    return this.rowCount;
}
Also used : Connection(com.google.cloud.spanner.connection.Connection)

Aggregations

Connection (com.google.cloud.spanner.connection.Connection)18 Test (org.junit.Test)16 TypeCode (com.google.spanner.v1.TypeCode)6 CSVFormat (org.apache.commons.csv.CSVFormat)6 DatabaseClient (com.google.cloud.spanner.DatabaseClient)5 ErrorCode (com.google.cloud.spanner.ErrorCode)5 Mutation (com.google.cloud.spanner.Mutation)5 ResultSet (com.google.cloud.spanner.ResultSet)5 RunWith (org.junit.runner.RunWith)5 SpannerException (com.google.cloud.spanner.SpannerException)4 SpannerExceptionFactory (com.google.cloud.spanner.SpannerExceptionFactory)4 Statement (com.google.cloud.spanner.Statement)4 Truth.assertThat (com.google.common.truth.Truth.assertThat)4 ExecutorService (java.util.concurrent.ExecutorService)4 TimeUnit (java.util.concurrent.TimeUnit)4 AfterClass (org.junit.AfterClass)4 Assert.fail (org.junit.Assert.fail)4 Rule (org.junit.Rule)4 JUnit4 (org.junit.runners.JUnit4)4 ApiFuture (com.google.api.core.ApiFuture)3