Search in sources :

Example 1 with SUPPORTS_UPDATE

use of io.trino.testing.TestingConnectorBehavior.SUPPORTS_UPDATE in project trino by trinodb.

the class BaseConnectorTest method testUpdateRowConcurrently.

// Repeat test with invocationCount for better test coverage, since the tested aspect is inherently non-deterministic.
@Test(timeOut = 60_000, invocationCount = 4)
public void testUpdateRowConcurrently() throws Exception {
    if (!hasBehavior(SUPPORTS_UPDATE)) {
        // Covered by testUpdate
        return;
    }
    int threads = 4;
    CyclicBarrier barrier = new CyclicBarrier(threads);
    ExecutorService executor = newFixedThreadPool(threads);
    try (TestTable table = new TestTable(getQueryRunner()::execute, "test_concurrent_update", IntStream.range(0, threads).mapToObj(i -> format("col%s integer", i)).collect(joining(", ", "(", ")")))) {
        String tableName = table.getName();
        assertUpdate(format("INSERT INTO %s VALUES (%s)", tableName, join(",", nCopies(threads, "0"))), 1);
        List<Future<Boolean>> futures = IntStream.range(0, threads).mapToObj(threadNumber -> executor.submit(() -> {
            barrier.await(10, SECONDS);
            try {
                String columnName = "col" + threadNumber;
                getQueryRunner().execute(format("UPDATE %s SET %s = %s + 1", tableName, columnName, columnName));
                return true;
            } catch (Exception e) {
                RuntimeException trinoException = getTrinoExceptionCause(e);
                try {
                    verifyConcurrentUpdateFailurePermissible(trinoException);
                } catch (Throwable verifyFailure) {
                    if (trinoException != e && verifyFailure != e) {
                        verifyFailure.addSuppressed(e);
                    }
                    throw verifyFailure;
                }
                return false;
            }
        })).collect(toImmutableList());
        String expected = futures.stream().map(future -> tryGetFutureValue(future, 10, SECONDS).orElseThrow(() -> new RuntimeException("Wait timed out"))).map(success -> success ? "1" : "0").collect(joining(",", "VALUES (", ")"));
        assertThat(query("TABLE " + tableName)).matches(expected);
    } finally {
        executor.shutdownNow();
        executor.awaitTermination(10, SECONDS);
    }
}
Also used : SkipException(org.testng.SkipException) SUPPORTS_MULTI_STATEMENT_WRITES(io.trino.testing.TestingConnectorBehavior.SUPPORTS_MULTI_STATEMENT_WRITES) QueryManager(io.trino.execution.QueryManager) SUPPORTS_ARRAY(io.trino.testing.TestingConnectorBehavior.SUPPORTS_ARRAY) Test(org.testng.annotations.Test) TestTable(io.trino.testing.sql.TestTable) Thread.currentThread(java.lang.Thread.currentThread) Future(java.util.concurrent.Future) DataProviders.toDataProvider(io.trino.testing.DataProviders.toDataProvider) Duration.nanosSince(io.airlift.units.Duration.nanosSince) ENGLISH(java.util.Locale.ENGLISH) Assert.assertFalse(org.testng.Assert.assertFalse) Assert.assertEquals(io.trino.testing.assertions.Assert.assertEquals) PlanPrinter.textLogicalPlan(io.trino.sql.planner.planprinter.PlanPrinter.textLogicalPlan) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Uninterruptibles.sleepUninterruptibly(com.google.common.util.concurrent.Uninterruptibles.sleepUninterruptibly) Collectors.joining(java.util.stream.Collectors.joining) CountDownLatch(java.util.concurrent.CountDownLatch) Stream(java.util.stream.Stream) SUPPORTS_RENAME_TABLE(io.trino.testing.TestingConnectorBehavior.SUPPORTS_RENAME_TABLE) INFORMATION_SCHEMA(io.trino.connector.informationschema.InformationSchemaTable.INFORMATION_SCHEMA) Session(io.trino.Session) Verify.verifyNotNull(com.google.common.base.Verify.verifyNotNull) PlanNodeSearcher.searchFrom(io.trino.sql.planner.optimizations.PlanNodeSearcher.searchFrom) LimitNode(io.trino.sql.planner.plan.LimitNode) SUPPORTS_RENAME_SCHEMA(io.trino.testing.TestingConnectorBehavior.SUPPORTS_RENAME_SCHEMA) Callable(java.util.concurrent.Callable) MINUTES(java.util.concurrent.TimeUnit.MINUTES) SUPPORTS_TOPN_PUSHDOWN(io.trino.testing.TestingConnectorBehavior.SUPPORTS_TOPN_PUSHDOWN) Supplier(java.util.function.Supplier) SUPPORTS_RENAME_MATERIALIZED_VIEW_ACROSS_SCHEMAS(io.trino.testing.TestingConnectorBehavior.SUPPORTS_RENAME_MATERIALIZED_VIEW_ACROSS_SCHEMAS) StatsAndCosts(io.trino.cost.StatsAndCosts) ArrayList(java.util.ArrayList) SUPPORTS_CREATE_VIEW(io.trino.testing.TestingConnectorBehavior.SUPPORTS_CREATE_VIEW) SUPPORTS_TRUNCATE(io.trino.testing.TestingConnectorBehavior.SUPPORTS_TRUNCATE) VARCHAR(io.trino.spi.type.VarcharType.VARCHAR) String.join(java.lang.String.join) UncheckedTimeoutException(com.google.common.util.concurrent.UncheckedTimeoutException) SUPPORTS_COMMENT_ON_COLUMN(io.trino.testing.TestingConnectorBehavior.SUPPORTS_COMMENT_ON_COLUMN) SUPPORTS_CREATE_SCHEMA(io.trino.testing.TestingConnectorBehavior.SUPPORTS_CREATE_SCHEMA) Language(org.intellij.lang.annotations.Language) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) QueryInfo(io.trino.execution.QueryInfo) JoinDistributionType(io.trino.sql.planner.OptimizerConfig.JoinDistributionType) SUPPORTS_CREATE_MATERIALIZED_VIEW(io.trino.testing.TestingConnectorBehavior.SUPPORTS_CREATE_MATERIALIZED_VIEW) QualifiedObjectName(io.trino.metadata.QualifiedObjectName) ArrayDeque(java.util.ArrayDeque) SUPPORTS_ROW_LEVEL_DELETE(io.trino.testing.TestingConnectorBehavior.SUPPORTS_ROW_LEVEL_DELETE) BasicQueryInfo(io.trino.server.BasicQueryInfo) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SUPPORTS_DROP_COLUMN(io.trino.testing.TestingConnectorBehavior.SUPPORTS_DROP_COLUMN) SUPPORTS_CREATE_TABLE_WITH_DATA(io.trino.testing.TestingConnectorBehavior.SUPPORTS_CREATE_TABLE_WITH_DATA) CompletionService(java.util.concurrent.CompletionService) Duration(io.airlift.units.Duration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestTable.randomTableSuffix(io.trino.testing.sql.TestTable.randomTableSuffix) SUPPORTS_CREATE_TABLE(io.trino.testing.TestingConnectorBehavior.SUPPORTS_CREATE_TABLE) CyclicBarrier(java.util.concurrent.CyclicBarrier) ImmutableSet(com.google.common.collect.ImmutableSet) SUPPORTS_RENAME_COLUMN(io.trino.testing.TestingConnectorBehavior.SUPPORTS_RENAME_COLUMN) Collections.nCopies(java.util.Collections.nCopies) SUPPORTS_RENAME_TABLE_ACROSS_SCHEMAS(io.trino.testing.TestingConnectorBehavior.SUPPORTS_RENAME_TABLE_ACROSS_SCHEMAS) String.format(java.lang.String.format) Preconditions.checkState(com.google.common.base.Preconditions.checkState) SUPPORTS_DELETE(io.trino.testing.TestingConnectorBehavior.SUPPORTS_DELETE) List(java.util.List) SUPPORTS_INSERT(io.trino.testing.TestingConnectorBehavior.SUPPORTS_INSERT) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) IntStream(java.util.stream.IntStream) DataProvider(org.testng.annotations.DataProvider) Assert.assertNull(org.testng.Assert.assertNull) Stopwatch(com.google.common.base.Stopwatch) Deque(java.util.Deque) QueryAssertions.getTrinoExceptionCause(io.trino.testing.QueryAssertions.getTrinoExceptionCause) ImmutableList(com.google.common.collect.ImmutableList) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) SUPPORTS_UPDATE(io.trino.testing.TestingConnectorBehavior.SUPPORTS_UPDATE) Objects.requireNonNull(java.util.Objects.requireNonNull) ExecutorService(java.util.concurrent.ExecutorService) DispatchManager(io.trino.dispatcher.DispatchManager) Assert.fail(org.testng.Assert.fail) SUPPORTS_RENAME_MATERIALIZED_VIEW(io.trino.testing.TestingConnectorBehavior.SUPPORTS_RENAME_MATERIALIZED_VIEW) MoreFutures.tryGetFutureValue(io.airlift.concurrent.MoreFutures.tryGetFutureValue) Executors.newFixedThreadPool(java.util.concurrent.Executors.newFixedThreadPool) FunctionManager(io.trino.metadata.FunctionManager) SUPPORTS_NEGATIVE_DATE(io.trino.testing.TestingConnectorBehavior.SUPPORTS_NEGATIVE_DATE) Consumer(java.util.function.Consumer) SUPPORTS_ADD_COLUMN(io.trino.testing.TestingConnectorBehavior.SUPPORTS_ADD_COLUMN) QueryAssertions.assertContains(io.trino.testing.QueryAssertions.assertContains) Collectors.toList(java.util.stream.Collectors.toList) IGNORE_STATS_CALCULATOR_FAILURES(io.trino.SystemSessionProperties.IGNORE_STATS_CALCULATOR_FAILURES) Assert.assertEventually(io.trino.testing.assertions.Assert.assertEventually) SUPPORTS_COMMENT_ON_TABLE(io.trino.testing.TestingConnectorBehavior.SUPPORTS_COMMENT_ON_TABLE) Metadata(io.trino.metadata.Metadata) Assert.assertTrue(org.testng.Assert.assertTrue) Plan(io.trino.sql.planner.Plan) SUPPORTS_NOT_NULL_CONSTRAINT(io.trino.testing.TestingConnectorBehavior.SUPPORTS_NOT_NULL_CONSTRAINT) SECONDS(java.util.concurrent.TimeUnit.SECONDS) MaterializedResult.resultBuilder(io.trino.testing.MaterializedResult.resultBuilder) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) TestTable(io.trino.testing.sql.TestTable) SkipException(org.testng.SkipException) UncheckedTimeoutException(com.google.common.util.concurrent.UncheckedTimeoutException) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.testng.annotations.Test)

Aggregations

Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 Stopwatch (com.google.common.base.Stopwatch)1 Verify.verifyNotNull (com.google.common.base.Verify.verifyNotNull)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Iterables.getOnlyElement (com.google.common.collect.Iterables.getOnlyElement)1 UncheckedTimeoutException (com.google.common.util.concurrent.UncheckedTimeoutException)1 Uninterruptibles.sleepUninterruptibly (com.google.common.util.concurrent.Uninterruptibles.sleepUninterruptibly)1 MoreFutures.tryGetFutureValue (io.airlift.concurrent.MoreFutures.tryGetFutureValue)1 Duration (io.airlift.units.Duration)1 Duration.nanosSince (io.airlift.units.Duration.nanosSince)1 Session (io.trino.Session)1 IGNORE_STATS_CALCULATOR_FAILURES (io.trino.SystemSessionProperties.IGNORE_STATS_CALCULATOR_FAILURES)1 INFORMATION_SCHEMA (io.trino.connector.informationschema.InformationSchemaTable.INFORMATION_SCHEMA)1 StatsAndCosts (io.trino.cost.StatsAndCosts)1 DispatchManager (io.trino.dispatcher.DispatchManager)1 QueryInfo (io.trino.execution.QueryInfo)1 QueryManager (io.trino.execution.QueryManager)1 FunctionManager (io.trino.metadata.FunctionManager)1