Search in sources :

Example 1 with ErrorCode

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

the class ConnectionImplTest method testExecuteSetAutocommitDmlModeInvalidValue.

@Test
public void testExecuteSetAutocommitDmlModeInvalidValue() {
    try (ConnectionImpl subject = createConnection(ConnectionOptions.newBuilder().setCredentials(NoCredentials.getInstance()).setUri(URI).build())) {
        assertThat(subject.isAutocommit(), is(true));
        assertThat(subject.getAutocommitDmlMode(), is(equalTo(AutocommitDmlMode.TRANSACTIONAL)));
        ErrorCode expected = null;
        try {
            subject.execute(Statement.of("set autocommit_dml_mode='NON_EXISTENT_VALUE'"));
        } catch (SpannerException e) {
            expected = e.getErrorCode();
        }
        assertThat(expected, is(equalTo(ErrorCode.INVALID_ARGUMENT)));
    }
}
Also used : ErrorCode(com.google.cloud.spanner.ErrorCode) AbstractConnectionImplTest.expectSpannerException(com.google.cloud.spanner.connection.AbstractConnectionImplTest.expectSpannerException) SpannerException(com.google.cloud.spanner.SpannerException) Test(org.junit.Test)

Example 2 with ErrorCode

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

the class ConnectionImplTest method testExecuteSetStatementTimeoutInvalidValue.

@Test
public void testExecuteSetStatementTimeoutInvalidValue() {
    try (ConnectionImpl subject = createConnection(ConnectionOptions.newBuilder().setCredentials(NoCredentials.getInstance()).setUri(URI).build())) {
        assertThat(subject.getStatementTimeout(TimeUnit.MILLISECONDS), is(equalTo(0L)));
        ErrorCode expected = null;
        try {
            subject.execute(Statement.of("set statement_timeout=-1"));
        } catch (SpannerException e) {
            expected = e.getErrorCode();
        }
        assertThat(expected, is(equalTo(ErrorCode.INVALID_ARGUMENT)));
    }
}
Also used : ErrorCode(com.google.cloud.spanner.ErrorCode) AbstractConnectionImplTest.expectSpannerException(com.google.cloud.spanner.connection.AbstractConnectionImplTest.expectSpannerException) SpannerException(com.google.cloud.spanner.SpannerException) Test(org.junit.Test)

Example 3 with ErrorCode

use of com.google.cloud.spanner.ErrorCode in project datarouter by hotpads.

the class SpannerSessionPoolIntegrationTester method scanWithInterrupts.

@Test
public void scanWithInterrupts() {
    // init client in parent thread without timeout
    node.scan().findFirst();
    var counts = new Counts();
    var callCount = counts.add("call");
    var cancelledCount = counts.add("cancelled");
    var resourceExhaustedCount = counts.add("resourceExhausted");
    var otherErrorCount = counts.add("otherError");
    var successCount = counts.add("success");
    int numIterations = maxSessions + 2;
    int numThreads = 1;
    boolean paralleScan = false;
    var config = new Config().setResponseBatchSize(2_000);
    int timeoutMs = 300;
    boolean cancelFutures = true;
    boolean mayInterruptIfRunning = true;
    int logEveryN = 20;
    Scanner.iterate(0, i -> i + 1).limit(numIterations).parallel(new ParallelScannerContext(scannerExec, numThreads, false, paralleScan)).each(i -> {
        var future = opExec.submit(() -> {
            try {
                callCount.increment();
                node.scan(config).count();
                successCount.increment();
                return null;
            } catch (SpannerException spannerException) {
                if (spannerException.getErrorCode().equals(ErrorCode.CANCELLED)) {
                    cancelledCount.increment();
                } else if (spannerException.getErrorCode().equals(ErrorCode.RESOURCE_EXHAUSTED)) {
                    resourceExhaustedCount.increment();
                } else {
                    otherErrorCount.increment();
                }
                logger.info("spannerException errorCode={} {}", spannerException.getErrorCode(), counts);
                logger.warn("spannerException errorCode={} {}", spannerException.getErrorCode(), counts, spannerException);
                throw spannerException;
            }
        });
        try {
            future.get(timeoutMs, TimeUnit.MILLISECONDS);
        } catch (ExecutionException e) {
            logger.warn("", e);
        } catch (TimeoutException e) {
            logger.warn("", e);
            if (cancelFutures) {
                future.cancel(mayInterruptIfRunning);
            }
        } catch (InterruptedException e) {
            logger.warn("", e);
        }
    }).sample(logEveryN, true).forEach($ -> logger.warn("{}", counts));
    Assert.assertEquals(resourceExhaustedCount.value(), 0);
}
Also used : Scanner(io.datarouter.scanner.Scanner) ParallelScannerContext(io.datarouter.scanner.ParallelScannerContext) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) Test(org.testng.annotations.Test) TestDatabeanKey(io.datarouter.storage.test.TestDatabeanKey) Inject(javax.inject.Inject) Counts(io.datarouter.util.Count.Counts) Assert(org.testng.Assert) ClientId(io.datarouter.storage.client.ClientId) Config(io.datarouter.storage.config.Config) SortedMapStorageNode(io.datarouter.storage.node.op.combo.SortedMapStorage.SortedMapStorageNode) ExecutorService(java.util.concurrent.ExecutorService) AfterClass(org.testng.annotations.AfterClass) Logger(org.slf4j.Logger) Executors(java.util.concurrent.Executors) Guice(org.testng.annotations.Guice) NodeFactory(io.datarouter.storage.node.factory.NodeFactory) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) ErrorCode(com.google.cloud.spanner.ErrorCode) SpannerException(com.google.cloud.spanner.SpannerException) SpannerTestNgModuleFactory(io.datarouter.gcp.spanner.SpannerTestNgModuleFactory) TestDatabeanFielder(io.datarouter.storage.test.TestDatabeanFielder) SpannerClientOptions(io.datarouter.gcp.spanner.client.SpannerClientOptions) TestDatabean(io.datarouter.storage.test.TestDatabean) Counts(io.datarouter.util.Count.Counts) Config(io.datarouter.storage.config.Config) ParallelScannerContext(io.datarouter.scanner.ParallelScannerContext) SpannerException(com.google.cloud.spanner.SpannerException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) Test(org.testng.annotations.Test)

Aggregations

ErrorCode (com.google.cloud.spanner.ErrorCode)3 SpannerException (com.google.cloud.spanner.SpannerException)3 AbstractConnectionImplTest.expectSpannerException (com.google.cloud.spanner.connection.AbstractConnectionImplTest.expectSpannerException)2 Test (org.junit.Test)2 SpannerTestNgModuleFactory (io.datarouter.gcp.spanner.SpannerTestNgModuleFactory)1 SpannerClientOptions (io.datarouter.gcp.spanner.client.SpannerClientOptions)1 ParallelScannerContext (io.datarouter.scanner.ParallelScannerContext)1 Scanner (io.datarouter.scanner.Scanner)1 ClientId (io.datarouter.storage.client.ClientId)1 Config (io.datarouter.storage.config.Config)1 NodeFactory (io.datarouter.storage.node.factory.NodeFactory)1 SortedMapStorageNode (io.datarouter.storage.node.op.combo.SortedMapStorage.SortedMapStorageNode)1 TestDatabean (io.datarouter.storage.test.TestDatabean)1 TestDatabeanFielder (io.datarouter.storage.test.TestDatabeanFielder)1 TestDatabeanKey (io.datarouter.storage.test.TestDatabeanKey)1 Counts (io.datarouter.util.Count.Counts)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 TimeUnit (java.util.concurrent.TimeUnit)1