-
Notifications
You must be signed in to change notification settings - Fork 133
IGNITE-22303 Retry operation when plan gets outdated by the time of execution #7309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cce3ef9
IGNITE-22303 Re-plan outdated SQL multi-step plan
xtern a0e457c
IGNITE-22303 Replace instanceof with something meaningful
xtern 97a1f04
IGNITE-22303 Review notes.
xtern 63458aa
IGNITE-22303 (minor) Comment fix.
xtern f0f4b46
IGNITE-27243 Reset retryTx after retry.
xtern 48d40de
IGNITE-27243 (minor) Test simplified.
xtern 77edec6
Merge remote-tracking branch 'origin/main' into ignite-22303
xtern da6f39a
Merge remote-tracking branch 'origin/main' into ignite-22303
xtern 6818db2
IGNITE-22303 (minor) Javadoc formatting
xtern File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
126 changes: 126 additions & 0 deletions
126
...est/java/org/apache/ignite/internal/sql/engine/ItSqlConcurrentSchemaModificationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.ignite.internal.sql.engine; | ||
|
|
||
| import static org.apache.ignite.internal.TestWrappers.unwrapIgniteImpl; | ||
| import static org.apache.ignite.internal.lang.IgniteStringFormatter.format; | ||
| import static org.apache.ignite.internal.testframework.IgniteTestUtils.await; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import java.util.Set; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.ThreadLocalRandom; | ||
| import org.apache.ignite.internal.sql.BaseSqlIntegrationTest; | ||
| import org.apache.ignite.internal.sql.engine.util.Commons; | ||
| import org.apache.ignite.sql.IgniteSql; | ||
| import org.apache.ignite.sql.SqlRow; | ||
| import org.apache.ignite.sql.async.AsyncResultSet; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.ValueSource; | ||
|
|
||
| /** | ||
| * Integration tests to verify SQL query execution during concurrent schema updates. | ||
| */ | ||
| public class ItSqlConcurrentSchemaModificationTest extends BaseSqlIntegrationTest { | ||
| @Override | ||
| protected int initialNodes() { | ||
| return 1; | ||
| } | ||
|
|
||
| @Override | ||
| protected String getNodeBootstrapConfigTemplate() { | ||
| return "ignite.sql.execution.threadCount: 64"; | ||
| } | ||
|
|
||
| @AfterEach | ||
| void dropTables() { | ||
| System.clearProperty("FAST_QUERY_OPTIMIZATION_ENABLED"); | ||
| Commons.resetFastQueryOptimizationFlag(); | ||
| unwrapIgniteImpl(CLUSTER.aliveNode()).queryEngine().invalidatePlannerCache(Set.of()); | ||
|
|
||
| dropAllTables(); | ||
| } | ||
|
|
||
| @ParameterizedTest(name = "FastQueryOptimization={0}") | ||
| @ValueSource(booleans = {true, false}) | ||
| void dmlWithConcurrentDdl(Boolean fastPlan) throws InterruptedException { | ||
| System.setProperty("FAST_QUERY_OPTIMIZATION_ENABLED", fastPlan.toString()); | ||
|
|
||
| IgniteSql sql = CLUSTER.aliveNode().sql(); | ||
|
|
||
| sql("CREATE TABLE t(id INT PRIMARY KEY)"); | ||
|
|
||
| int iterations = 20; | ||
|
|
||
| for (int i = 0; i < iterations; i++) { | ||
| log.info("iteration #" + i); | ||
|
|
||
| String ddlQuery = i % 2 == 0 | ||
| ? "ALTER TABLE t ADD COLUMN val VARCHAR DEFAULT 'abc'" | ||
| : "ALTER TABLE t DROP COLUMN val"; | ||
|
|
||
| CompletableFuture<AsyncResultSet<SqlRow>> ddlFut = sql.executeAsync(null, ddlQuery); | ||
|
|
||
| Thread.sleep(ThreadLocalRandom.current().nextInt(15) * 10); | ||
|
|
||
| assertQuery("INSERT INTO t (id) VALUES (?)") | ||
| .withParam(i) | ||
| .returns(1L) | ||
| .check(); | ||
|
|
||
| await(await(ddlFut).closeAsync()); | ||
|
|
||
| assertEquals(0, txManager().pending()); | ||
| } | ||
| } | ||
|
|
||
| @ParameterizedTest(name = "FastQueryOptimization={0}") | ||
| @ValueSource(booleans = {true, false}) | ||
| void selectWithConcurrentDdl(boolean fastPlan) throws InterruptedException { | ||
| System.setProperty("FAST_QUERY_OPTIMIZATION_ENABLED", String.valueOf(fastPlan)); | ||
|
|
||
| IgniteSql sql = CLUSTER.aliveNode().sql(); | ||
|
|
||
| sql("CREATE TABLE t(id INT PRIMARY KEY, id2 INT)"); | ||
| sql("INSERT INTO t VALUES (0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9)"); | ||
|
|
||
| int iterations = 20; | ||
|
|
||
| for (int i = 0; i < iterations; i++) { | ||
| log.info("iteration #" + i); | ||
|
|
||
| String ddlQuery = i % 2 == 0 | ||
| ? "ALTER TABLE t ADD COLUMN val VARCHAR DEFAULT 'abc'" | ||
| : "ALTER TABLE t DROP COLUMN val"; | ||
|
|
||
| CompletableFuture<AsyncResultSet<SqlRow>> ddlFut = sql.executeAsync(null, ddlQuery); | ||
| Thread.sleep(ThreadLocalRandom.current().nextInt(15) * 10); | ||
|
|
||
| int id = i % 10; | ||
|
|
||
| assertQuery(format("SELECT id2 FROM t WHERE id={}", id)) | ||
| .returns(id) | ||
| .check(); | ||
|
|
||
| await(await(ddlFut).closeAsync()); | ||
|
|
||
| assertEquals(0, txManager().pending()); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...rc/main/java/org/apache/ignite/internal/sql/engine/SqlPlanToTxSchemaVersionValidator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.ignite.internal.sql.engine; | ||
|
|
||
| import static org.apache.ignite.internal.util.CompletableFutures.nullCompletedFuture; | ||
|
|
||
| import java.util.concurrent.CompletableFuture; | ||
| import org.apache.ignite.internal.catalog.CatalogService; | ||
| import org.apache.ignite.internal.hlc.HybridTimestamp; | ||
| import org.apache.ignite.internal.schema.SchemaSyncService; | ||
| import org.apache.ignite.internal.sql.engine.exec.SqlPlanOutdatedException; | ||
| import org.apache.ignite.internal.sql.engine.prepare.MultiStepPlan; | ||
| import org.apache.ignite.internal.sql.engine.tx.QueryTransactionWrapper; | ||
| import org.apache.ignite.internal.tx.InternalTransaction; | ||
| import org.jetbrains.annotations.TestOnly; | ||
|
|
||
| /** | ||
| * SQL query execution plan validator. | ||
| * | ||
| * <p>Performs validation of the catalog version from the {@link MultiStepPlan multi-step plan} relative to the started transaction. | ||
| */ | ||
| public class SqlPlanToTxSchemaVersionValidator { | ||
| @TestOnly | ||
| public static final SqlPlanToTxSchemaVersionValidator NOOP = new NoopSqlPlanToTxSchemaVersionValidator(); | ||
|
|
||
| private final SchemaSyncService schemaSyncService; | ||
| private final CatalogService catalogService; | ||
|
|
||
| /** | ||
| * Compares the catalog version from the plan with the version of the active catalog | ||
| * at the {@link InternalTransaction#schemaTimestamp() schema time} of the transaction. | ||
| * | ||
| * @param plan {@link MultiStepPlan multi-step} execution plan. | ||
| * @param tx Query transaction wrapper. | ||
| * @return Successfully completed future if the provided transaction is explicit or the catalog versions match. | ||
| * Otherwise returns a future completed with an exception {@link SqlPlanOutdatedException}. | ||
| */ | ||
| public CompletableFuture<Void> validate(MultiStepPlan plan, QueryTransactionWrapper tx) { | ||
| if (!tx.implicit()) { | ||
| return nullCompletedFuture(); | ||
| } | ||
|
|
||
| HybridTimestamp ts = tx.unwrap().schemaTimestamp(); | ||
|
|
||
| return schemaSyncService.waitForMetadataCompleteness(ts) | ||
| .thenRun(() -> { | ||
| // TODO https://issues.apache.org/jira/browse/IGNITE-27491 Avoid re-planning in case of unrelated catalog changes | ||
| int requiredCatalog = catalogService.activeCatalogVersion(ts.longValue()); | ||
|
|
||
| if (requiredCatalog != plan.catalogVersion()) { | ||
| throw new SqlPlanOutdatedException(); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private SqlPlanToTxSchemaVersionValidator(SchemaSyncService schemaSyncService, CatalogService catalogService) { | ||
| this.schemaSyncService = schemaSyncService; | ||
| this.catalogService = catalogService; | ||
| } | ||
|
|
||
| public static SqlPlanToTxSchemaVersionValidator create(SchemaSyncService schemaSyncService, CatalogService catalogService) { | ||
| return new SqlPlanToTxSchemaVersionValidator(schemaSyncService, catalogService); | ||
| } | ||
|
|
||
| private static class NoopSqlPlanToTxSchemaVersionValidator extends SqlPlanToTxSchemaVersionValidator { | ||
| @SuppressWarnings("DataFlowIssue") | ||
| private NoopSqlPlanToTxSchemaVersionValidator() { | ||
| super(null, null); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<Void> validate(MultiStepPlan plan, QueryTransactionWrapper tx) { | ||
| return nullCompletedFuture(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.