Search in sources :

Example 1 with SpannerException

use of com.google.cloud.spanner.SpannerException in project google-cloud-java by GoogleCloudPlatform.

the class ITWriteTest method deadline.

@Test
public void deadline() {
    ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
    // Cloud Spanner is fast, but not this fast :-)
    Context.CancellableContext context = Context.current().withDeadlineAfter(10, TimeUnit.NANOSECONDS, executor);
    Runnable work = context.wrap(new Runnable() {

        @Override
        public void run() {
            write(baseInsert().set("BoolValue").to(true).build());
        }
    });
    try {
        work.run();
    } catch (SpannerException e) {
        MatcherAssert.assertThat(e, isSpannerException(ErrorCode.DEADLINE_EXCEEDED));
    } finally {
        executor.shutdown();
    }
}
Also used : Context(io.grpc.Context) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) SpannerMatchers.isSpannerException(com.google.cloud.spanner.SpannerMatchers.isSpannerException) SpannerException(com.google.cloud.spanner.SpannerException) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Example 2 with SpannerException

use of com.google.cloud.spanner.SpannerException in project google-cloud-java by GoogleCloudPlatform.

the class RemoteSpannerHelper method cleanUp.

/** Deletes all the databases created via {@code createTestDatabase}. Shuts down the client. */
public void cleanUp() {
    // Drop all the databases we created explicitly.
    int numDropped = 0;
    for (Database db : dbs) {
        try {
            logger.log(Level.FINE, "Dropping test database {0}", db.getId());
            db.drop();
            ++numDropped;
        } catch (SpannerException e) {
            logger.log(Level.SEVERE, "Failed to drop test database " + db.getId(), e);
        }
    }
    logger.log(Level.INFO, "Dropped {0} test database(s)", numDropped);
    client.close();
}
Also used : Database(com.google.cloud.spanner.Database) SpannerException(com.google.cloud.spanner.SpannerException)

Example 3 with SpannerException

use of com.google.cloud.spanner.SpannerException in project google-cloud-java by GoogleCloudPlatform.

the class ITTransactionTest method userExceptionPreventsCommit.

@Test
public void userExceptionPreventsCommit() {
    class UserException extends Exception {

        UserException(String message) {
            super(message);
        }
    }
    final String key = uniqueKey();
    TransactionCallable<Void> callable = new TransactionCallable<Void>() {

        @Override
        public Void run(TransactionContext transaction) throws UserException {
            transaction.buffer(Mutation.newInsertOrUpdateBuilder("T").set("K").to(key).build());
            throw new UserException("User failure");
        }
    };
    try {
        client.readWriteTransaction().run(callable);
        fail("Expected user exception");
    } catch (SpannerException e) {
        assertThat(e.getErrorCode()).isEqualTo(ErrorCode.UNKNOWN);
        assertThat(e.getMessage()).contains("User failure");
        assertThat(e.getCause()).isInstanceOf(UserException.class);
    }
    Struct row = client.singleUse(TimestampBound.strong()).readRow("T", Key.of(key), Arrays.asList("K"));
    assertThat(row).isNull();
}
Also used : TransactionCallable(com.google.cloud.spanner.TransactionRunner.TransactionCallable) TransactionContext(com.google.cloud.spanner.TransactionContext) SpannerExceptionFactory.newSpannerException(com.google.cloud.spanner.SpannerExceptionFactory.newSpannerException) SpannerException(com.google.cloud.spanner.SpannerException) SpannerExceptionFactory.newSpannerException(com.google.cloud.spanner.SpannerExceptionFactory.newSpannerException) AbortedException(com.google.cloud.spanner.AbortedException) SpannerException(com.google.cloud.spanner.SpannerException) Struct(com.google.cloud.spanner.Struct) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Example 4 with SpannerException

use of com.google.cloud.spanner.SpannerException in project google-cloud-java by GoogleCloudPlatform.

the class ITTransactionTest method readAbort.

@Test
public void readAbort() throws InterruptedException {
    final String key1 = uniqueKey();
    final String key2 = uniqueKey();
    client.write(Arrays.asList(Mutation.newInsertBuilder("T").set("K").to(key1).set("V").to(0).build(), Mutation.newInsertBuilder("T").set("K").to(key2).set("V").to(1).build()));
    final CountDownLatch t1Started = new CountDownLatch(1);
    final CountDownLatch t1Done = new CountDownLatch(1);
    final CountDownLatch t2Running = new CountDownLatch(1);
    final CountDownLatch t2Done = new CountDownLatch(1);
    // Thread 1 performs a read before notifying that it has started and allowing
    // thread 2 to start.  This ensures that it establishes a senior lock priority relative to
    // thread 2.  It then waits for thread 2 to read, so that both threads have shared locks on
    // key1, before continuing to commit; the act of committing means that thread 1's lock is
    // upgraded and thread 2's transaction is aborted.  When thread 1 is done, thread 2 tries a
    // second read, which will abort.  Both threads will mask SpannerExceptions to ensure that
    // the implementation does not require TransactionCallable to propagate them.
    Thread t1 = new Thread() {

        @Override
        public void run() {
            client.readWriteTransaction().run(new TransactionCallable<Void>() {

                @Override
                public Void run(TransactionContext transaction) throws SpannerException {
                    try {
                        Struct row = transaction.readRow("T", Key.of(key1), Arrays.asList("V"));
                        t1Started.countDown();
                        Uninterruptibles.awaitUninterruptibly(t2Running);
                        transaction.buffer(Mutation.newUpdateBuilder("T").set("K").to(key1).set("V").to(row.getLong(0) + 1).build());
                        return null;
                    } catch (SpannerException e) {
                        if (e.getErrorCode() == ErrorCode.ABORTED) {
                            assertThat(e).isInstanceOf(AbortedException.class);
                            assertThat(((AbortedException) e).getRetryDelayInMillis()).isNotEqualTo(-1L);
                        }
                        throw new RuntimeException("Swallowed exception: " + e.getMessage());
                    }
                }
            });
            t1Done.countDown();
        }
    };
    Thread t2 = new Thread() {

        @Override
        public void run() {
            client.readWriteTransaction().run(new TransactionCallable<Void>() {

                @Override
                public Void run(TransactionContext transaction) throws SpannerException {
                    try {
                        Struct r1 = transaction.readRow("T", Key.of(key1), Arrays.asList("V"));
                        t2Running.countDown();
                        Uninterruptibles.awaitUninterruptibly(t1Done);
                        Struct r2 = transaction.readRow("T", Key.of(key2), Arrays.asList("V"));
                        transaction.buffer(Mutation.newUpdateBuilder("T").set("K").to(key2).set("V").to(r1.getLong(0) + r2.getLong(0)).build());
                        return null;
                    } catch (SpannerException e) {
                        if (e.getErrorCode() == ErrorCode.ABORTED) {
                            assertThat(e).isInstanceOf(AbortedException.class);
                            assertThat(((AbortedException) e).getRetryDelayInMillis()).isNotEqualTo(-1L);
                        }
                        throw new RuntimeException("Swallowed exception: " + e.getMessage());
                    }
                }
            });
            t2Done.countDown();
        }
    };
    t1.start();
    Uninterruptibles.awaitUninterruptibly(t1Started);
    // Thread 2 will abort on the first attempt and should retry; wait for completion to confirm.
    t2.start();
    assertThat(t2Done.await(1, TimeUnit.MINUTES)).isTrue();
    // Check that both transactions effects are visible.
    assertThat(client.singleUse(TimestampBound.strong()).readRow("T", Key.of(key1), Arrays.asList("V")).getLong(0)).isEqualTo(1);
    assertThat(client.singleUse(TimestampBound.strong()).readRow("T", Key.of(key2), Arrays.asList("V")).getLong(0)).isEqualTo(2);
}
Also used : TransactionContext(com.google.cloud.spanner.TransactionContext) AbortedException(com.google.cloud.spanner.AbortedException) SpannerExceptionFactory.newSpannerException(com.google.cloud.spanner.SpannerExceptionFactory.newSpannerException) SpannerException(com.google.cloud.spanner.SpannerException) CountDownLatch(java.util.concurrent.CountDownLatch) Struct(com.google.cloud.spanner.Struct) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Example 5 with SpannerException

use of com.google.cloud.spanner.SpannerException in project google-cloud-java by GoogleCloudPlatform.

the class ITReadTest method deadline.

@Test
public void deadline() {
    ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
    Context.CancellableContext context = Context.current().withDeadlineAfter(10, TimeUnit.NANOSECONDS, executor);
    Runnable work = context.wrap(new Runnable() {

        @Override
        public void run() {
            client.singleUse(TimestampBound.strong()).readRow(TABLE_NAME, Key.of("k1"), ALL_COLUMNS);
        }
    });
    try {
        work.run();
    } catch (SpannerException e) {
        MatcherAssert.assertThat(e, isSpannerException(ErrorCode.DEADLINE_EXCEEDED));
    } finally {
        executor.shutdown();
    }
}
Also used : Context(io.grpc.Context) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) SpannerMatchers.isSpannerException(com.google.cloud.spanner.SpannerMatchers.isSpannerException) SpannerException(com.google.cloud.spanner.SpannerException) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Aggregations

SpannerException (com.google.cloud.spanner.SpannerException)6 IntegrationTest (com.google.cloud.spanner.IntegrationTest)5 Test (org.junit.Test)5 SpannerExceptionFactory.newSpannerException (com.google.cloud.spanner.SpannerExceptionFactory.newSpannerException)3 Struct (com.google.cloud.spanner.Struct)3 TransactionContext (com.google.cloud.spanner.TransactionContext)3 AbortedException (com.google.cloud.spanner.AbortedException)2 SpannerMatchers.isSpannerException (com.google.cloud.spanner.SpannerMatchers.isSpannerException)2 TransactionCallable (com.google.cloud.spanner.TransactionRunner.TransactionCallable)2 Context (io.grpc.Context)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 Database (com.google.cloud.spanner.Database)1 CountDownLatch (java.util.concurrent.CountDownLatch)1