This is an automated email from the ASF dual-hosted git repository.

tkalkirill pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ignite.git


The following commit(s) were added to refs/heads/master by this push:
     new cb43c3d1d91 IGNITE-29281 SQL Calcite: Fix Query cleanup race (#13819)
cb43c3d1d91 is described below

commit cb43c3d1d91cef67ccbb11be455abf9c003e36f8
Author: Kirill Tkalenko <[email protected]>
AuthorDate: Mon Nov 16 22:29:04 2026 +0300

    IGNITE-29281 SQL Calcite: Fix Query cleanup race (#13819)
---
 .../query/calcite/CalciteQueryProcessor.java       |  11 +-
 .../processors/query/calcite/RootQuery.java        |  10 +-
 .../integration/AbstractBasicIntegrationTest.java  |  42 +++++-
 .../QueryCleanupRaceIntegrationTest.java           | 153 +++++++++++++++++++++
 .../integration/RunningQueriesIntegrationTest.java |   4 +-
 .../ignite/testsuites/IntegrationTestSuite.java    |   2 +
 6 files changed, 206 insertions(+), 16 deletions(-)

diff --git 
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/CalciteQueryProcessor.java
 
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/CalciteQueryProcessor.java
index 3a14a5e3433..154ce772d40 100644
--- 
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/CalciteQueryProcessor.java
+++ 
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/CalciteQueryProcessor.java
@@ -753,20 +753,19 @@ public class CalciteQueryProcessor extends 
GridProcessorAdapter implements Query
         catch (Throwable e) {
             boolean isCanceled = qry.isCancelled();
 
+            // Keep the query registered until asynchronous fragment cleanup 
has completed. In particular,
+            // late start responses must still be able to find a query that is 
waiting to close.
+            qry.onError(isCanceled ? new QueryCancelledException() : e);
+
             if (qrys != null)
                 qrys.forEach(RootQuery::cancel);
 
             if (isCanceled) {
-                qryReg.unregister(qry.id(), new QueryCancelledException());
-
                 throw new IgniteSQLException("The query was cancelled while 
planning",
                     IgniteQueryErrorCode.QUERY_CANCELED, e);
             }
-            else {
-                qryReg.unregister(qry.id(), e);
-
+            else
                 throw e;
-            }
         }
     }
 
diff --git 
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/RootQuery.java
 
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/RootQuery.java
index d7727df9aaf..3d6aab68ead 100644
--- 
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/RootQuery.java
+++ 
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/RootQuery.java
@@ -297,9 +297,8 @@ public class RootQuery<RowT> extends Query<RowT> implements 
TrackableQuery {
                 return;
 
             if (state == QueryState.INITED || state == QueryState.PLANNING || 
state == QueryState.MAPPING) {
-                state = QueryState.CLOSED;
-
-                return;
+                // Even a query that failed before execution must run the 
unregister callback.
+                state0 = state = QueryState.CLOSED;
             }
 
             if (state == QueryState.EXECUTING) {
@@ -437,7 +436,10 @@ public class RootQuery<RowT> extends Query<RowT> 
implements TrackableQuery {
 
     /** */
     @Override public void onError(Throwable error) {
-        root.onError(error);
+        // The iterator can rethrow a wrapper whose cause is the failure 
already stored in the root.
+        // Feeding it back would make that failure suppress its own wrapper 
and create an exception cycle.
+        if (root != null && root.failure() == null)
+            root.onError(error);
 
         tryClose(error);
     }
diff --git 
a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/AbstractBasicIntegrationTest.java
 
b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/AbstractBasicIntegrationTest.java
index d5ebda2f85f..96388e386d2 100644
--- 
a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/AbstractBasicIntegrationTest.java
+++ 
b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/AbstractBasicIntegrationTest.java
@@ -17,10 +17,12 @@
 
 package org.apache.ignite.internal.processors.query.calcite.integration;
 
+import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicReference;
 import org.apache.calcite.plan.RelOptCluster;
 import org.apache.calcite.rel.RelCollation;
 import org.apache.calcite.rex.RexNode;
@@ -40,7 +42,9 @@ import 
org.apache.ignite.internal.processors.query.QueryContext;
 import org.apache.ignite.internal.processors.query.QueryEngine;
 import 
org.apache.ignite.internal.processors.query.calcite.CalciteQueryProcessor;
 import 
org.apache.ignite.internal.processors.query.calcite.GridCommonAbstractWrapperTest;
+import org.apache.ignite.internal.processors.query.calcite.Query;
 import org.apache.ignite.internal.processors.query.calcite.QueryChecker;
+import org.apache.ignite.internal.processors.query.calcite.RootQuery;
 import 
org.apache.ignite.internal.processors.query.calcite.exec.ExecutionContext;
 import 
org.apache.ignite.internal.processors.query.calcite.exec.ExecutionServiceImpl;
 import 
org.apache.ignite.internal.processors.query.calcite.exec.exp.RangeIterable;
@@ -59,6 +63,7 @@ import org.jetbrains.annotations.Nullable;
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeAll;
 
+import static java.util.stream.Collectors.joining;
 import static 
org.apache.ignite.internal.processors.authentication.AuthenticationProcessorSelfTest.authenticate;
 import static 
org.apache.ignite.internal.processors.authentication.User.DFAULT_USER_NAME;
 import static 
org.apache.ignite.internal.processors.query.calcite.exec.ExchangeServiceImpl.INBOX_INITIALIZATION_TIMEOUT;
@@ -117,8 +122,7 @@ public class AbstractBasicIntegrationTest extends 
GridCommonAbstractWrapperTest
         // Wait for pending queries before destroying caches. If some error 
occurs during query execution, client code
         // can get control earlier than query leave the running queries 
registry (need some time for async message
         // exchange), but eventually, all queries should be closed.
-        assertTrue("Not finished queries found on client", waitForCondition(
-            () -> 
queryProcessor(client).queryRegistry().runningQueries().isEmpty(), 1_000L));
+        assertNoRunningQueries(client);
 
         for (Ignite ign : G.allGrids()) {
             if (destroyCachesAfterTest()) {
@@ -128,8 +132,7 @@ public class AbstractBasicIntegrationTest extends 
GridCommonAbstractWrapperTest
 
             CalciteQueryProcessor qryProc = queryProcessor(ign);
 
-            assertTrue("Not finished queries found [ignite=" + ign.name() + 
']',
-                waitForCondition(() -> 
qryProc.queryRegistry().runningQueries().isEmpty(), 1_000));
+            assertNoRunningQueries(ign);
 
             ExecutionServiceImpl<Object[]> execSvc = 
(ExecutionServiceImpl<Object[]>)qryProc.executionService();
             assertEquals("Tracked memory must be 0 after test [ignite=" + 
ign.name() + ']',
@@ -273,7 +276,7 @@ public class AbstractBasicIntegrationTest extends 
GridCommonAbstractWrapperTest
     }
 
     /** */
-    protected CalciteQueryProcessor queryProcessor(Ignite ignite) {
+    protected static CalciteQueryProcessor queryProcessor(Ignite ignite) {
         return Commons.lookupComponent(((IgniteEx)ignite).context(), 
CalciteQueryProcessor.class);
     }
 
@@ -443,4 +446,33 @@ public class AbstractBasicIntegrationTest extends 
GridCommonAbstractWrapperTest
             return name.equals(employer.name) && 
salary.equals(employer.salary);
         }
     }
+
+    /** */
+    protected static void assertNoRunningQueries(Ignite n) throws Exception {
+        AtomicReference<Collection<Query<?>>> runningQrysRef = new 
AtomicReference<>(List.of());
+
+        if (waitForCondition(() -> isAbsentRunningQueries(n, runningQrysRef), 
1_000L))
+            return;
+
+        String runningQrysInfo = runningQrysRef.get().stream()
+            .map(AbstractBasicIntegrationTest::toQueryInfoString)
+            .collect(joining(", ", "[", "]"));
+
+        fail(String.format("Not finished queries found: [ignite=%s, 
queries=%s]", n.name(), runningQrysInfo));
+    }
+
+    /** */
+    private static boolean isAbsentRunningQueries(Ignite n, 
AtomicReference<Collection<Query<?>>> runningQrysRef) {
+        List<Query<?>> qrys = 
List.copyOf(queryProcessor(n).queryRegistry().runningQueries());
+        runningQrysRef.set(qrys);
+
+        return qrys.isEmpty();
+    }
+
+    /** */
+    private static String toQueryInfoString(Query<?> qry) {
+        String qryStr = qry instanceof RootQuery ? 
((RootQuery<?>)qry).queryInfo(null) : qry.toString();
+
+        return String.format("queryId=%s, state=%s, query=%s", qry.id(), 
qry.state(), qryStr);
+    }
 }
diff --git 
a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/QueryCleanupRaceIntegrationTest.java
 
b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/QueryCleanupRaceIntegrationTest.java
new file mode 100644
index 00000000000..be8836dcd4a
--- /dev/null
+++ 
b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/QueryCleanupRaceIntegrationTest.java
@@ -0,0 +1,153 @@
+/*
+ * 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.processors.query.calcite.integration;
+
+import java.util.Collections;
+import java.util.IdentityHashMap;
+import java.util.Set;
+import java.util.UUID;
+import java.util.concurrent.atomic.AtomicReference;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.TestRecordingCommunicationSpi;
+import org.apache.ignite.internal.processors.query.IgniteSQLException;
+import org.apache.ignite.internal.processors.query.calcite.Query;
+import org.apache.ignite.internal.processors.query.calcite.QueryState;
+import 
org.apache.ignite.internal.processors.query.calcite.message.QueryStartResponse;
+import org.apache.ignite.internal.util.typedef.G;
+import org.apache.ignite.internal.util.typedef.X;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/** Tests query cleanup when a fragment start response arrives after a DELETE 
failure. */
+public class QueryCleanupRaceIntegrationTest extends 
AbstractBasicIntegrationTest {
+    /** {@inheritDoc} */
+    @Override protected int nodeCount() {
+        return 1;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected IgniteConfiguration getConfiguration(String 
igniteInstanceName) throws Exception {
+        return super.getConfiguration(igniteInstanceName)
+            .setCommunicationSpi(new TestRecordingCommunicationSpi());
+    }
+
+    /** Delayed start acknowledgement must not leave the failed DELETE 
registered on the server. */
+    @Test
+    void testDeleteFailureWithDelayedQueryStartResponse() throws Exception {
+        sql("CREATE TABLE test (id INT PRIMARY KEY, val INT)");
+        sql("INSERT INTO test (id, val) VALUES (1, 0)");
+
+        // The second execution exercises the cached-plan path, which has no 
list of sibling queries.
+        for (int i = 0; i < 2; i++)
+            checkDeleteFailureWithDelayedQueryStartResponse();
+
+        assertNoRunningQueriesOnAllNodes();
+    }
+
+    /** */
+    private void checkDeleteFailureWithDelayedQueryStartResponse() throws 
Exception {
+        TestRecordingCommunicationSpi spi = 
TestRecordingCommunicationSpi.spi(grid(0));
+        AtomicReference<UUID> qryId = new AtomicReference<>();
+
+        // Setup queries have completed; only the following DELETE is executed 
while blocking is enabled.
+        spi.blockMessages((node, msg) -> {
+            if (node.id().equals(client.localNode().id()) && msg instanceof 
QueryStartResponse) {
+                qryId.set(((QueryStartResponse)msg).queryId());
+
+                return true;
+            }
+
+            return false;
+        });
+
+        try {
+            // Error messages remain deliverable, so the caller observes the 
error before the start response.
+            // Error sql must necessarily be executed through the rows of the 
table.
+            assertThrows("DELETE FROM test WHERE 100 / val > 0", 
ArithmeticException.class, "/ by zero");
+
+            assertTrue("No QueryStartResponse was intercepted", 
spi.waitForBlocked(1, 5_000));
+
+            Query<?> root = 
queryProcessor(client).queryRegistry().query(qryId.get());
+
+            assertNotNull("The query must remain registered while its start 
response is pending", root);
+            assertEquals(QueryState.CLOSING, root.state());
+        }
+        finally {
+            spi.stopBlock();
+        }
+    }
+
+    /** A propagated execution error must not be added back to its own cause 
as a suppressed exception. */
+    @Test
+    void testExecutionFailureHasAcyclicExceptionGraph() throws Exception {
+        sql("CREATE TABLE test (id INT PRIMARY KEY, val INT)");
+        sql("INSERT INTO test (id, val) VALUES (1, 0)");
+
+        for (int i = 0; i < 2; i++) {
+            IgniteSQLException error = 
Assertions.assertThrows(IgniteSQLException.class,
+                () -> sql("DELETE FROM test WHERE 100 / val > 0"));
+
+            assertTrue(X.hasCause(error, ArithmeticException.class));
+            assertNoExceptionCycle(error, Collections.newSetFromMap(new 
IdentityHashMap<>()));
+        }
+
+        assertNoRunningQueriesOnAllNodes();
+    }
+
+    /** Check paths rather than all visited nodes: sharing a cause does not 
constitute a cycle. */
+    private void assertNoExceptionCycle(Throwable error, Set<Throwable> 
ancestors) {
+        if (error == null)
+            return;
+
+        assertTrue("Cycle in the cause/suppressed exception graph", 
ancestors.add(error));
+
+        assertNoExceptionCycle(error.getCause(), ancestors);
+
+        for (Throwable suppressed : error.getSuppressed())
+            assertNoExceptionCycle(suppressed, ancestors);
+
+        ancestors.remove(error);
+    }
+
+    /** Errors before creation of a root execution node must also unregister 
the query. */
+    @Test
+    void testPlanningFailureClosesQuery() throws Exception {
+        sql("CREATE TABLE test (id INT PRIMARY KEY, val INT)");
+
+        assertThrowsSqlException("SELECT missing FROM test", "not found");
+        assertNoRunningQueriesOnAllNodes();
+    }
+
+    /** A later statement's planning failure must close earlier statements in 
the same call. */
+    @Test
+    void testPlanningFailureClosesEarlierStatement() throws Exception {
+        sql("CREATE TABLE test (id INT PRIMARY KEY, val INT)");
+        sql("INSERT INTO test (id, val) VALUES (1, 0)");
+
+        assertThrowsSqlException("SELECT * FROM test; SELECT missing FROM 
test", "not found");
+        assertNoRunningQueriesOnAllNodes();
+    }
+
+    /** */
+    private static void assertNoRunningQueriesOnAllNodes() throws Exception {
+        // Query cleanup is asynchronous, so wait for it to complete on every 
node.
+        for (Ignite node : G.allGrids())
+            assertNoRunningQueries(node);
+    }
+}
diff --git 
a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/RunningQueriesIntegrationTest.java
 
b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/RunningQueriesIntegrationTest.java
index 3f5d6a21e66..cda8f83323c 100644
--- 
a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/RunningQueriesIntegrationTest.java
+++ 
b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/RunningQueriesIntegrationTest.java
@@ -168,7 +168,9 @@ public class RunningQueriesIntegrationTest extends 
AbstractBasicIntegrationTest
 
         assertTrue(GridTestUtils.waitForCondition(() -> 
engine.runningQueries().isEmpty(), TIMEOUT_IN_MS));
 
-        GridTestUtils.assertThrowsAnyCause(log, () -> fut.get(0), 
IgniteSQLException.class, "The query was cancelled while planning");
+        // Registry cleanup can finish before the planning task observes 
cancellation and completes its future.
+        GridTestUtils.assertThrowsAnyCause(log, () -> fut.get(TIMEOUT_IN_MS), 
IgniteSQLException.class,
+            "The query was cancelled while planning");
 
         assertEquals(1, ((LongMetric)mreg.findMetric("canceled")).value());
     }
diff --git 
a/modules/calcite/src/test/java/org/apache/ignite/testsuites/IntegrationTestSuite.java
 
b/modules/calcite/src/test/java/org/apache/ignite/testsuites/IntegrationTestSuite.java
index d8a15f64ee6..2d972610b94 100644
--- 
a/modules/calcite/src/test/java/org/apache/ignite/testsuites/IntegrationTestSuite.java
+++ 
b/modules/calcite/src/test/java/org/apache/ignite/testsuites/IntegrationTestSuite.java
@@ -65,6 +65,7 @@ import 
org.apache.ignite.internal.processors.query.calcite.integration.Operators
 import 
org.apache.ignite.internal.processors.query.calcite.integration.PartitionPruneTest;
 import 
org.apache.ignite.internal.processors.query.calcite.integration.PartitionsReservationIntegrationTest;
 import 
org.apache.ignite.internal.processors.query.calcite.integration.QueryBlockingTaskExecutorIntegrationTest;
+import 
org.apache.ignite.internal.processors.query.calcite.integration.QueryCleanupRaceIntegrationTest;
 import 
org.apache.ignite.internal.processors.query.calcite.integration.QueryMetadataIntegrationTest;
 import 
org.apache.ignite.internal.processors.query.calcite.integration.QueryWithPartitionsIntegrationTest;
 import 
org.apache.ignite.internal.processors.query.calcite.integration.RecursiveCteIntegrationTest;
@@ -189,6 +190,7 @@ import org.junit.platform.suite.api.Suite;
     BulkOperationDeadlockIntegrationTest.class,
     SelectForUpdateIntegrationTest.class,
     LitmusCheckIntegrationTest.class,
+    QueryCleanupRaceIntegrationTest.class,
 })
 public class IntegrationTestSuite {
 }

Reply via email to