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 cae599bab84 IGNITE-29178 SQL Calcite: SELECT hangs during a topology 
change within a transaction (#13736)
cae599bab84 is described below

commit cae599bab841f638cb8a8139f178a5b2eb6ff9a9
Author: Kirill Tkalenko <[email protected]>
AuthorDate: Wed Nov 11 10:33:32 2026 +0300

    IGNITE-29178 SQL Calcite: SELECT hangs during a topology change within a 
transaction (#13736)
---
 .../query/calcite/schema/CacheTableImpl.java       |   6 +-
 .../query/calcite/schema/IgniteStatisticsImpl.java |  47 +++++-
 .../calcite/integration/SetOpIntegrationTest.java  |   6 +-
 .../IgniteStatisticsImplRollingUpgradeTest.java    | 119 ++++++++++++++
 .../calcite/schema/IgniteStatisticsImplTest.java   | 171 +++++++++++++++++++++
 .../tx/SqlTransactionsTopologyChangeTest.java      | 118 ++++++++++++++
 .../ignite/testsuites/IgniteCalciteTestSuite.java  |   6 +
 .../query/stat/StatisticsCacheSizeTask.java        | 122 +++++++++++++++
 .../feature/CoreFeatureRegistry.java               |   3 +
 9 files changed, 590 insertions(+), 8 deletions(-)

diff --git 
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/schema/CacheTableImpl.java
 
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/schema/CacheTableImpl.java
index 2f06b666660..8f04d2bd8c6 100644
--- 
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/schema/CacheTableImpl.java
+++ 
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/schema/CacheTableImpl.java
@@ -50,6 +50,9 @@ public class CacheTableImpl extends AbstractTable implements 
IgniteCacheTable {
     /** */
     private final GridKernalContext ctx;
 
+    /** */
+    private final IgniteStatisticsImpl fallbackStats;
+
     /** */
     private final Map<String, IgniteIndex> indexes = new ConcurrentHashMap<>();
 
@@ -63,6 +66,7 @@ public class CacheTableImpl extends AbstractTable implements 
IgniteCacheTable {
     public CacheTableImpl(GridKernalContext ctx, CacheTableDescriptor desc) {
         this.ctx = ctx;
         this.desc = desc;
+        fallbackStats = new IgniteStatisticsImpl(desc);
     }
 
     /** {@inheritDoc} */
@@ -81,7 +85,7 @@ public class CacheTableImpl extends AbstractTable implements 
IgniteCacheTable {
         if (statistics != null)
             return new IgniteStatisticsImpl(statistics);
 
-        return new IgniteStatisticsImpl(desc);
+        return fallbackStats;
     }
 
     /** {@inheritDoc} */
diff --git 
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/schema/IgniteStatisticsImpl.java
 
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/schema/IgniteStatisticsImpl.java
index e06820adcd8..d4b6fee4961 100644
--- 
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/schema/IgniteStatisticsImpl.java
+++ 
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/schema/IgniteStatisticsImpl.java
@@ -17,6 +17,8 @@
 
 package org.apache.ignite.internal.processors.query.calcite.schema;
 
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
 import java.util.concurrent.atomic.AtomicInteger;
 import com.google.common.collect.ImmutableList;
@@ -25,21 +27,34 @@ import org.apache.calcite.rel.RelReferentialConstraint;
 import org.apache.calcite.schema.Statistic;
 import org.apache.calcite.util.ImmutableBitSet;
 import org.apache.ignite.IgniteCheckedException;
-import org.apache.ignite.cache.CachePeekMode;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.internal.IgniteInternalFuture;
+import org.apache.ignite.internal.processors.cache.GridCacheContext;
 import 
org.apache.ignite.internal.processors.query.calcite.trait.IgniteDistribution;
 import org.apache.ignite.internal.processors.query.stat.ColumnStatistics;
 import org.apache.ignite.internal.processors.query.stat.ObjectStatisticsImpl;
+import 
org.apache.ignite.internal.processors.query.stat.StatisticsCacheSizeTask;
+import org.jetbrains.annotations.Nullable;
+
+import static 
org.apache.ignite.internal.processors.rollingupgrade.feature.CoreFeatureRegistry.STATISTICS_CACHE_SIZE_TASK_FEATURE;
+import static 
org.apache.ignite.internal.processors.task.TaskExecutionOptions.options;
 
 /** Calcite statistics wrapper. */
 public class IgniteStatisticsImpl implements Statistic {
     /** */
     private static final int STATS_CLI_UPDATE_THRESHOLD = 200;
 
+    /** */
+    private static final long LOCAL_CACHE_SIZE_TIMEOUT = 1_000;
+
+    /** */
+    private static final long DFLT_ROW_CNT = 1_000;
+
     /** */
     AtomicInteger cliReqCnt = new AtomicInteger();
 
     /** */
-    private volatile long primaryRowCnt;
+    private volatile long primaryRowCnt = DFLT_ROW_CNT;
 
     /** Internal statistics implementation. */
     private final ObjectStatisticsImpl statistics;
@@ -76,7 +91,7 @@ public class IgniteStatisticsImpl implements Statistic {
         else if (desc != null)
             rows = getRowCountApproximation();
         else
-            rows = 1000;
+            rows = DFLT_ROW_CNT;
 
         return (double)rows;
     }
@@ -112,7 +127,7 @@ public class IgniteStatisticsImpl implements Statistic {
      * @param colName Column name.
      * @return Column statistics or {@code null} if there are no statistics 
for specified column.
      */
-    public ColumnStatistics getColumnStatistics(String colName) {
+    public @Nullable ColumnStatistics getColumnStatistics(String colName) {
         return (statistics == null) ? null : 
statistics.columnStatistics(colName);
     }
 
@@ -122,7 +137,10 @@ public class IgniteStatisticsImpl implements Statistic {
     private void refreshStatsIfNeededEx() {
         if (cliReqCnt.getAndIncrement() % STATS_CLI_UPDATE_THRESHOLD == 0) {
             try {
-                primaryRowCnt = 
desc.cacheInfo().cacheContext().cache().sizeLong(new CachePeekMode[] 
{CachePeekMode.PRIMARY});
+                Long size = localCacheSize(desc.cacheInfo().cacheContext());
+
+                if (size != null)
+                    primaryRowCnt = size;
             }
             catch (IgniteCheckedException ignore) {
                 // No-op.
@@ -136,4 +154,23 @@ public class IgniteStatisticsImpl implements Statistic {
 
         return primaryRowCnt;
     }
+
+    /** */
+    private static @Nullable Long localCacheSize(@Nullable GridCacheContext<?, 
?> cctx) throws IgniteCheckedException {
+        if (cctx == null || 
!cctx.kernalContext().rollingUpgrade().features().isActive(STATISTICS_CACHE_SIZE_TASK_FEATURE))
+            return null;
+
+        Collection<ClusterNode> nodes = new 
ArrayList<>(cctx.grid().cluster().forDataNodes(cctx.name()).nodes());
+
+        if (nodes.isEmpty())
+            return null;
+
+        IgniteInternalFuture<Long> fut = cctx.kernalContext().task().execute(
+            new StatisticsCacheSizeTask(cctx.name()),
+            null,
+            
options(nodes).withTimeout(LOCAL_CACHE_SIZE_TIMEOUT).withFailoverDisabled()
+        );
+
+        return fut.get();
+    }
 }
diff --git 
a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/SetOpIntegrationTest.java
 
b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/SetOpIntegrationTest.java
index fee789bbe94..4beb2ba4059 100644
--- 
a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/SetOpIntegrationTest.java
+++ 
b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/SetOpIntegrationTest.java
@@ -570,8 +570,6 @@ public class SetOpIntegrationTest extends 
AbstractBasicIntegrationTransactionalT
 
                     clearTransaction();
 
-                    sql("DROP TABLE t1");
-
                     assertEquals(expected.length, res.size());
 
                     for (int i = 0; i < expected.length; ++i) {
@@ -583,6 +581,10 @@ public class SetOpIntegrationTest extends 
AbstractBasicIntegrationTransactionalT
                     sql("DELETE FROM t0");
 
                     clearTransaction();
+
+                    sql("DROP TABLE t1");
+
+                    awaitPartitionMapExchange();
                 }
             }
         }
diff --git 
a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/schema/IgniteStatisticsImplRollingUpgradeTest.java
 
b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/schema/IgniteStatisticsImplRollingUpgradeTest.java
new file mode 100644
index 00000000000..e2be385ca17
--- /dev/null
+++ 
b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/schema/IgniteStatisticsImplRollingUpgradeTest.java
@@ -0,0 +1,119 @@
+/*
+ * 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.schema;
+
+import java.util.List;
+import org.apache.ignite.cache.query.FieldsQueryCursor;
+import org.apache.ignite.cache.query.SqlFieldsQuery;
+import org.apache.ignite.calcite.CalciteQueryEngineConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.GridJobExecuteRequest;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.TestRecordingCommunicationSpi;
+import 
org.apache.ignite.internal.processors.query.calcite.CalciteQueryProcessor;
+import org.apache.ignite.internal.processors.query.calcite.util.Commons;
+import 
org.apache.ignite.internal.processors.query.stat.StatisticsCacheSizeTask;
+import 
org.apache.ignite.internal.processors.rollingupgrade.AbstractRollingUpgradeTest;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/** */
+public class IgniteStatisticsImplRollingUpgradeTest extends 
AbstractRollingUpgradeTest {
+    /** */
+    @BeforeAll
+    static void init() {
+        beforeFirstTest0();
+    }
+
+    /** {@inheritDoc} */
+    @BeforeEach
+    @Override protected void beforeTest() throws Exception {
+        super.beforeTest();
+    }
+
+    /** {@inheritDoc} */
+    @AfterEach
+    @Override protected void afterTest() throws Exception {
+        super.afterTest();
+    }
+
+    /** {@inheritDoc} */
+    @Override protected IgniteConfiguration getConfiguration(String name, 
String ver) throws Exception {
+        IgniteConfiguration cfg = super.getConfiguration(name, ver);
+
+        cfg.getSqlConfiguration().setQueryEnginesConfiguration(new 
CalciteQueryEngineConfiguration());
+
+        return cfg;
+    }
+
+    /** */
+    @Test
+    public void testRefreshAfterFinalization() throws Exception {
+        startGrid(0, TEST_DEFAULT_VER);
+        startGrid(1, TEST_DEFAULT_VER);
+
+        ru(0).enableVersionUpgrade();
+
+        upgradeNodeVersion(0, MASTER_VER_REF);
+
+        IgniteEx node = grid(0);
+        TestRecordingCommunicationSpi spi = 
TestRecordingCommunicationSpi.spi(node);
+
+        spi.record((remote, msg) -> msg instanceof GridJobExecuteRequest req
+            && 
StatisticsCacheSizeTask.class.getName().equals(req.taskClassName()));
+
+        sql(node, "CREATE TABLE TEST (id INT PRIMARY KEY, val INT) WITH 
template=replicated");
+        sql(node, "INSERT INTO TEST VALUES (1, 42)");
+
+        assertEquals(1_000, refreshRowCount(node), 0.0);
+        assertTrue(spi.recordedMessages(false).isEmpty());
+
+        upgradeNodeVersion(1, MASTER_VER_REF);
+        awaitPartitionMapExchange();
+
+        assertEquals(1_000, refreshRowCount(node), 0.0);
+        assertTrue(spi.recordedMessages(false).isEmpty());
+
+        finalizeClusterVersion(0, MASTER_VER_REF);
+
+        assertEquals(1, refreshRowCount(node), 0.0);
+        assertFalse(spi.recordedMessages(false).isEmpty());
+    }
+
+    /** */
+    private static double refreshRowCount(IgniteEx node) {
+        CalciteQueryProcessor proc = Commons.lookupComponent(node.context(), 
CalciteQueryProcessor.class);
+        CacheTableImpl tbl = 
(CacheTableImpl)proc.schemaHolder().schema("PUBLIC").getTable("TEST");
+        IgniteStatisticsImpl stat = (IgniteStatisticsImpl)tbl.getStatistic();
+
+        stat.cliReqCnt.set(0);
+
+        return stat.getRowCount();
+    }
+
+    /** */
+    private static void sql(IgniteEx node, String sql) {
+        SqlFieldsQuery qry = new SqlFieldsQuery(sql).setSchema("PUBLIC");
+
+        try (FieldsQueryCursor<List<?>> cur = 
node.context().query().querySqlFields(qry, false)) {
+            cur.getAll();
+        }
+    }
+}
diff --git 
a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/schema/IgniteStatisticsImplTest.java
 
b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/schema/IgniteStatisticsImplTest.java
new file mode 100644
index 00000000000..7fd45afda28
--- /dev/null
+++ 
b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/schema/IgniteStatisticsImplTest.java
@@ -0,0 +1,171 @@
+/*
+ * 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.schema;
+
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import org.apache.ignite.cache.CacheMode;
+import org.apache.ignite.cache.query.FieldsQueryCursor;
+import org.apache.ignite.cache.query.SqlFieldsQuery;
+import org.apache.ignite.calcite.CalciteQueryEngineConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.GridJobExecuteRequest;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.IgniteInternalFuture;
+import org.apache.ignite.internal.TestRecordingCommunicationSpi;
+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.util.Commons;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.EnumSource;
+
+import static org.apache.ignite.testframework.GridTestUtils.runAsync;
+import static org.apache.ignite.testframework.GridTestUtils.waitForCondition;
+
+/** */
+public class IgniteStatisticsImplTest extends GridCommonAbstractWrapperTest {
+    /** */
+    private static final String TBL_NAME = "TEST";
+
+    /** */
+    private static final int ROW_CNT = 100;
+
+    /** */
+    private IgniteEx client;
+
+    /** {@inheritDoc} */
+    @Override protected IgniteConfiguration getConfiguration(String name) 
throws Exception {
+        IgniteConfiguration cfg = super.getConfiguration(name);
+
+        cfg.getSqlConfiguration().setQueryEnginesConfiguration(new 
CalciteQueryEngineConfiguration());
+        cfg.setCommunicationSpi(new TestRecordingCommunicationSpi());
+
+        return cfg;
+    }
+
+    /** */
+    @BeforeEach
+    public void startNodes() throws Exception {
+        startGrids(2);
+
+        client = startClientGrid(2);
+    }
+
+    /** */
+    @AfterEach
+    public void stopNodes() {
+        stopAllGrids();
+    }
+
+    /** */
+    @ParameterizedTest
+    @EnumSource(value = CacheMode.class, names = {"PARTITIONED", "REPLICATED"})
+    public void testPrimaryRowsOnly(CacheMode mode) {
+        createAndFillTable(mode);
+
+        assertEquals(ROW_CNT, statistics().getRowCount(), 0.0);
+    }
+
+    /** */
+    @Test
+    public void testTimeoutKeepsPreviousEstimate() throws Exception {
+        createAndFillTable(CacheMode.PARTITIONED);
+
+        IgniteStatisticsImpl stat = statistics();
+
+        assertEquals(ROW_CNT, stat.getRowCount(), 0.0);
+
+        TestRecordingCommunicationSpi spi = 
TestRecordingCommunicationSpi.spi(client);
+
+        spi.blockMessages(GridJobExecuteRequest.class, grid(1).name());
+
+        try {
+            assertEquals(ROW_CNT, statistics().getRowCount(), 0.0);
+            assertFalse(spi.hasBlockedMessages());
+
+            stat.cliReqCnt.set(0);
+
+            assertEquals(ROW_CNT, runAsync(() -> 
statistics().getRowCount()).get(5_000), 0.0);
+            assertTrue(spi.hasBlockedMessages());
+        }
+        finally {
+            spi.stopBlock(false);
+        }
+    }
+
+    /** */
+    @Test
+    public void testNodeLeftKeepsPreviousEstimate() throws Exception {
+        createAndFillTable(CacheMode.PARTITIONED);
+
+        IgniteStatisticsImpl stat = statistics();
+
+        assertEquals(ROW_CNT, stat.getRowCount(), 0.0);
+
+        TestRecordingCommunicationSpi spi = 
TestRecordingCommunicationSpi.spi(client);
+
+        spi.blockMessages(GridJobExecuteRequest.class, grid(1).name());
+
+        try {
+            stat.cliReqCnt.set(0);
+
+            IgniteInternalFuture<Double> fut = runAsync(stat::getRowCount);
+
+            assertTrue(waitForCondition(spi::hasBlockedMessages, 5_000));
+
+            stopGrid(1);
+
+            assertEquals(ROW_CNT, fut.get(5_000), 0.0);
+        }
+        finally {
+            spi.stopBlock(false);
+        }
+    }
+
+    /** */
+    private IgniteStatisticsImpl statistics() {
+        CalciteQueryProcessor proc = Commons.lookupComponent(client.context(), 
CalciteQueryProcessor.class);
+        CacheTableImpl table = 
(CacheTableImpl)proc.schemaHolder().schema("PUBLIC").getTable(TBL_NAME);
+
+        return (IgniteStatisticsImpl)table.getStatistic();
+    }
+
+    /**  */
+    private void createAndFillTable(CacheMode mode) {
+        sql(String.format("CREATE TABLE %s(id INT PRIMARY KEY, val INT) WITH 
template=%s, backups=1", TBL_NAME, mode.name()));
+
+        String values = IntStream.range(0, ROW_CNT)
+            .mapToObj(i -> "(" + i + "," + i + ")")
+            .collect(Collectors.joining(","));
+
+        sql(String.format("INSERT INTO %s VALUES %s", TBL_NAME, values));
+    }
+
+    /**  */
+    private void sql(String sql) {
+        SqlFieldsQuery qry = new SqlFieldsQuery(sql).setSchema("PUBLIC");
+
+        try (FieldsQueryCursor<List<?>> cur = 
client.context().query().querySqlFields(qry, false)) {
+            cur.getAll();
+        }
+    }
+}
diff --git 
a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/tx/SqlTransactionsTopologyChangeTest.java
 
b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/tx/SqlTransactionsTopologyChangeTest.java
new file mode 100644
index 00000000000..12be93d9f16
--- /dev/null
+++ 
b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/tx/SqlTransactionsTopologyChangeTest.java
@@ -0,0 +1,118 @@
+/*
+ * 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.tx;
+
+import java.util.List;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.cache.QueryEntity;
+import org.apache.ignite.cache.query.FieldsQueryCursor;
+import org.apache.ignite.cache.query.SqlFieldsQuery;
+import org.apache.ignite.calcite.CalciteQueryEngineConfiguration;
+import org.apache.ignite.cluster.ClusterTopologyException;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.IgniteInternalFuture;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import 
org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTopologyFuture;
+import 
org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionTopology;
+import 
org.apache.ignite.internal.processors.query.calcite.GridCommonAbstractWrapperTest;
+import org.apache.ignite.transactions.Transaction;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import static java.util.concurrent.TimeUnit.SECONDS;
+import static org.apache.ignite.cache.CacheAtomicityMode.TRANSACTIONAL;
+import static 
org.apache.ignite.testframework.GridTestUtils.assertThrowsWithCause;
+import static org.apache.ignite.testframework.GridTestUtils.runAsync;
+import static org.apache.ignite.testframework.GridTestUtils.waitForCondition;
+import static 
org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC;
+import static 
org.apache.ignite.transactions.TransactionIsolation.READ_COMMITTED;
+
+/** */
+public class SqlTransactionsTopologyChangeTest extends 
GridCommonAbstractWrapperTest {
+    /** {@inheritDoc} */
+    @Override protected IgniteConfiguration getConfiguration(String 
igniteInstanceName) throws Exception {
+        IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
+
+        cfg.getSqlConfiguration().setQueryEnginesConfiguration(new 
CalciteQueryEngineConfiguration());
+        cfg.getTransactionConfiguration().setTxAwareQueriesEnabled(true);
+
+        return cfg;
+    }
+
+    /** */
+    @AfterEach
+    public void stopNodes() {
+        stopAllGrids();
+    }
+
+    /** */
+    @ParameterizedTest
+    @ValueSource(booleans = {false, true})
+    public void testSelectDuringTopologyChange(boolean client) throws 
Exception {
+        IgniteEx server = startGrid(0);
+        IgniteEx node = client ? startClientGrid(1) : server;
+
+        IgniteCache<Integer, Integer> cache = node.createCache(new 
CacheConfiguration<Integer, Integer>("test-cache")
+            .setAtomicityMode(TRANSACTIONAL)
+            .setSqlSchema("PUBLIC")
+            .setQueryEntities(List.of(new QueryEntity(Integer.class, 
Integer.class).setTableName("TEST"))));
+
+        node.createCache(new CacheConfiguration<>(DEFAULT_CACHE_NAME));
+
+        GridDhtPartitionTopology top = 
server.cachex(cache.getName()).context().topology();
+        AffinityTopologyVersion topVer = top.readyTopologyVersion();
+        IgniteInternalFuture<?> destroyFut;
+        String qry = "SELECT _KEY, _VAL FROM test";
+
+        // Bound the transaction timeout so a regression cannot hang the test 
indefinitely during planning.
+        try (Transaction tx = node.transactions().txStart(PESSIMISTIC, 
READ_COMMITTED, 10_000, 1)) {
+            // Hold the exchange without planning a query and warming the 
table statistics.
+            cache.put(1, 42);
+            destroyFut = runAsync(() -> node.destroyCache(DEFAULT_CACHE_NAME));
+
+            assertTrue("Cache stop exchange did not start", 
waitForCondition(() -> {
+                GridDhtTopologyFuture fut = top.topologyVersionFuture();
+
+                return fut.initialVersion().after(topVer) && !fut.isDone();
+            }, 5_000));
+
+            // The query must reach the scan while the exchange is still 
blocked by the transaction.
+            assertThrowsWithCause(() -> sql(node, qry), 
ClusterTopologyException.class);
+
+            assertFalse(destroyFut.isDone());
+
+            tx.commit();
+        }
+
+        destroyFut.get(5_000);
+
+        assertEquals(List.of(List.of(1, 42)), sql(node, qry));
+    }
+
+    /** */
+    private static List<List<?>> sql(IgniteEx node, String sql) {
+        SqlFieldsQuery qry = new 
SqlFieldsQuery(sql).setSchema("PUBLIC").setTimeout(5, SECONDS);
+
+        try (FieldsQueryCursor<List<?>> cur = 
node.context().query().querySqlFields(qry, false)) {
+            return cur.getAll();
+        }
+    }
+}
diff --git 
a/modules/calcite/src/test/java/org/apache/ignite/testsuites/IgniteCalciteTestSuite.java
 
b/modules/calcite/src/test/java/org/apache/ignite/testsuites/IgniteCalciteTestSuite.java
index 2de7fb99383..71f17035e1c 100644
--- 
a/modules/calcite/src/test/java/org/apache/ignite/testsuites/IgniteCalciteTestSuite.java
+++ 
b/modules/calcite/src/test/java/org/apache/ignite/testsuites/IgniteCalciteTestSuite.java
@@ -20,12 +20,15 @@ package org.apache.ignite.testsuites;
 import 
org.apache.ignite.internal.processors.query.calcite.exec.LogicalRelImplementorTest;
 import 
org.apache.ignite.internal.processors.query.calcite.exec.NumericTypesPrecisionsTest;
 import 
org.apache.ignite.internal.processors.query.calcite.message.CalciteCommunicationMessageSerializationTest;
+import 
org.apache.ignite.internal.processors.query.calcite.schema.IgniteStatisticsImplRollingUpgradeTest;
+import 
org.apache.ignite.internal.processors.query.calcite.schema.IgniteStatisticsImplTest;
 import 
org.apache.ignite.internal.processors.query.calcite.sql.ParserCodegenResourcesTest;
 import 
org.apache.ignite.internal.processors.query.calcite.sql.SqlCustomParserTest;
 import 
org.apache.ignite.internal.processors.query.calcite.sql.SqlReservedWordsTest;
 import 
org.apache.ignite.internal.processors.query.calcite.sql.SqlSelectForUpdateParserTest;
 import org.apache.ignite.internal.processors.tx.SqlTransactionsIsolationTest;
 import org.apache.ignite.internal.processors.tx.SqlTransactionsSavepointTest;
+import 
org.apache.ignite.internal.processors.tx.SqlTransactionsTopologyChangeTest;
 import 
org.apache.ignite.internal.processors.tx.SqlTransactionsUnsupportedModesTest;
 import org.junit.platform.suite.api.SelectClasses;
 import org.junit.platform.suite.api.Suite;
@@ -50,6 +53,9 @@ import org.junit.platform.suite.api.Suite;
     SqlTransactionsIsolationTest.class,
     SqlTransactionsUnsupportedModesTest.class,
     SqlTransactionsSavepointTest.class,
+    SqlTransactionsTopologyChangeTest.class,
+    IgniteStatisticsImplTest.class,
+    IgniteStatisticsImplRollingUpgradeTest.class,
 })
 public class IgniteCalciteTestSuite {
 }
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/stat/StatisticsCacheSizeTask.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/stat/StatisticsCacheSizeTask.java
new file mode 100644
index 00000000000..86855e8d8cd
--- /dev/null
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/stat/StatisticsCacheSizeTask.java
@@ -0,0 +1,122 @@
+/*
+ * 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.stat;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.cache.CachePeekMode;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.compute.ComputeJob;
+import org.apache.ignite.compute.ComputeJobAdapter;
+import org.apache.ignite.compute.ComputeJobResult;
+import org.apache.ignite.compute.ComputeJobResultPolicy;
+import org.apache.ignite.compute.ComputeTaskAdapter;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.processors.cache.GridCacheAdapter;
+import org.apache.ignite.internal.processors.task.GridInternal;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.resources.IgniteInstanceResource;
+import org.jetbrains.annotations.Nullable;
+
+import static org.apache.ignite.cache.CachePeekMode.PRIMARY;
+import static org.apache.ignite.compute.ComputeJobResultPolicy.WAIT;
+
+/**
+ * Estimates cache size using the currently available local primary partitions 
on each data node.
+ * Nodes need not agree on a topology version: missed or double-counted rows 
during rebalance are acceptable
+ * for planning statistics. An incomplete request returns {@code null} so the 
previous estimate can be retained.
+ */
+@GridInternal
+public class StatisticsCacheSizeTask extends ComputeTaskAdapter<Void, Long> {
+    /** */
+    private static final long serialVersionUID = 0L;
+
+    /** */
+    private final String cacheName;
+
+    /** @param cacheName Cache name. */
+    public StatisticsCacheSizeTask(String cacheName) {
+        this.cacheName = cacheName;
+    }
+
+    /** {@inheritDoc} */
+    @Override public Map<? extends ComputeJob, ClusterNode> 
map(List<ClusterNode> subgrid, @Nullable Void arg) {
+        Map<ComputeJob, ClusterNode> jobs = new HashMap<>();
+
+        for (ClusterNode node : subgrid)
+            jobs.put(new SizeJob(cacheName), node);
+
+        return jobs;
+    }
+
+    /** {@inheritDoc} */
+    @Override public ComputeJobResultPolicy result(ComputeJobResult res, 
List<ComputeJobResult> rcvd) {
+        return WAIT;
+    }
+
+    /** {@inheritDoc} */
+    @Override public @Nullable Long reduce(List<ComputeJobResult> results) {
+        long size = 0;
+
+        for (ComputeJobResult res : results) {
+            Long locSize = res.getData();
+
+            if (res.getException() != null || locSize == null)
+                return null;
+
+            size += locSize;
+        }
+
+        return size;
+    }
+
+    /** Reads local counters directly, bypassing cache proxy initialization 
and topology readiness waits. */
+    private static class SizeJob extends ComputeJobAdapter {
+        /** */
+        private static final long serialVersionUID = 0L;
+
+        /** */
+        @IgniteInstanceResource
+        private IgniteEx ignite;
+
+        /** */
+        private final String cacheName;
+
+        /** @param cacheName Cache name. */
+        SizeJob(String cacheName) {
+            this.cacheName = cacheName;
+        }
+
+        /** {@inheritDoc} */
+        @Override public @Nullable Object execute() {
+            GridCacheAdapter<?, ?> cache = 
ignite.context().cache().internalCache(cacheName);
+
+            if (cache == null || !cache.context().started())
+                return null;
+
+            try {
+                return cache.localSizeLong(new CachePeekMode[] {PRIMARY});
+            }
+            catch (IgniteCheckedException e) {
+                throw U.convertException(e);
+            }
+        }
+    }
+}
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/rollingupgrade/feature/CoreFeatureRegistry.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/rollingupgrade/feature/CoreFeatureRegistry.java
index 4d00489327f..47d104a9d17 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/rollingupgrade/feature/CoreFeatureRegistry.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/rollingupgrade/feature/CoreFeatureRegistry.java
@@ -111,4 +111,7 @@ public class CoreFeatureRegistry {
 
     /** Feature that introduces the snapshot list command. */
     public static final IgniteFeature SNAPSHOT_LIST_FEATURE = new 
IgniteCoreFeature(4);
+
+    /** Feature that introduces the cache size estimation task for SQL 
statistics. */
+    public static final IgniteFeature STATISTICS_CACHE_SIZE_TASK_FEATURE = new 
IgniteCoreFeature(5);
 }

Reply via email to