This is an automated email from the ASF dual-hosted git repository. asf-gitbox-commits pushed a commit to branch ignite-29320-2 in repository https://gitbox.apache.org/repos/asf/ignite.git
commit 5e9ce82c472b63587b53a8430c6a179a2a085462 Author: zstan <[email protected]> AuthorDate: Thu Nov 19 11:51:07 2026 +0300 IGNITE-29320 Calcite. Fix performance regression introduced by IGNITE-29178 --- .../query/calcite/schema/IgniteStatisticsImpl.java | 26 +++-- .../IgniteStatisticsImplRollingUpgradeTest.java | 2 +- .../calcite/schema/IgniteStatisticsImplTest.java | 130 ++++++++++++++++++++- 3 files changed, 145 insertions(+), 13 deletions(-) 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 d4b6fee4961..4b3388c021f 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 @@ -20,7 +20,7 @@ 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 java.util.concurrent.atomic.AtomicBoolean; import com.google.common.collect.ImmutableList; import org.apache.calcite.rel.RelCollation; import org.apache.calcite.rel.RelReferentialConstraint; @@ -34,6 +34,7 @@ import org.apache.ignite.internal.processors.query.calcite.trait.IgniteDistribut 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.apache.ignite.internal.util.typedef.internal.U; import org.jetbrains.annotations.Nullable; import static org.apache.ignite.internal.processors.rollingupgrade.feature.CoreFeatureRegistry.STATISTICS_CACHE_SIZE_TASK_FEATURE; @@ -41,17 +42,20 @@ import static org.apache.ignite.internal.processors.task.TaskExecutionOptions.op /** Calcite statistics wrapper. */ public class IgniteStatisticsImpl implements Statistic { - /** */ - private static final int STATS_CLI_UPDATE_THRESHOLD = 200; + /** Minimal interval between row count refresh attempts, in milliseconds. */ + public static final long STATS_REFRESH_INTERVAL = 2_000; /** */ private static final long LOCAL_CACHE_SIZE_TIMEOUT = 1_000; /** */ - private static final long DFLT_ROW_CNT = 1_000; + public static final long DFLT_ROW_CNT = 1_000; /** */ - AtomicInteger cliReqCnt = new AtomicInteger(); + private final AtomicBoolean updateInProgress = new AtomicBoolean(); + + /** Time of the last row count refresh attempt, {@code 0} if there were none. */ + volatile long lastRefreshTs; /** */ private volatile long primaryRowCnt = DFLT_ROW_CNT; @@ -135,16 +139,24 @@ public class IgniteStatisticsImpl implements Statistic { * Refreshes table stats if they are possibly outdated, must be called only in client mode. */ private void refreshStatsIfNeededEx() { - if (cliReqCnt.getAndIncrement() % STATS_CLI_UPDATE_THRESHOLD == 0) { + if (updateInProgress.compareAndSet(false, true)) { try { + if (U.currentTimeMillis() - lastRefreshTs < STATS_REFRESH_INTERVAL) + return; + Long size = localCacheSize(desc.cacheInfo().cacheContext()); if (size != null) - primaryRowCnt = size; + primaryRowCnt = Math.max(size, DFLT_ROW_CNT); + + lastRefreshTs = U.currentTimeMillis(); } catch (IgniteCheckedException ignore) { // No-op. } + finally { + updateInProgress.set(false); + } } } 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 index e2be385ca17..b352593a894 100644 --- 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 @@ -103,7 +103,7 @@ public class IgniteStatisticsImplRollingUpgradeTest extends AbstractRollingUpgra CacheTableImpl tbl = (CacheTableImpl)proc.schemaHolder().schema("PUBLIC").getTable("TEST"); IgniteStatisticsImpl stat = (IgniteStatisticsImpl)tbl.getStatistic(); - stat.cliReqCnt.set(0); + stat.lastRefreshTs = 0; return stat.getRowCount(); } 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 index 7fd45afda28..427655ee842 100644 --- 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 @@ -18,12 +18,16 @@ package org.apache.ignite.internal.processors.query.calcite.schema; import java.util.List; +import java.util.concurrent.CyclicBarrier; import java.util.stream.Collectors; import java.util.stream.IntStream; +import org.apache.ignite.IgniteCache; import org.apache.ignite.cache.CacheMode; import org.apache.ignite.cache.query.FieldsQueryCursor; import org.apache.ignite.cache.query.SqlFieldsQuery; +import org.apache.ignite.cache.query.annotations.QuerySqlField; import org.apache.ignite.calcite.CalciteQueryEngineConfiguration; +import org.apache.ignite.configuration.CacheConfiguration; import org.apache.ignite.configuration.IgniteConfiguration; import org.apache.ignite.internal.GridJobExecuteRequest; import org.apache.ignite.internal.IgniteEx; @@ -31,6 +35,7 @@ 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.rel.IgniteTableScan; import org.apache.ignite.internal.processors.query.calcite.util.Commons; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -38,7 +43,10 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.EnumSource; +import static org.apache.ignite.internal.processors.query.calcite.schema.IgniteStatisticsImpl.DFLT_ROW_CNT; +import static org.apache.ignite.internal.processors.query.calcite.schema.IgniteStatisticsImpl.STATS_REFRESH_INTERVAL; import static org.apache.ignite.testframework.GridTestUtils.runAsync; +import static org.apache.ignite.testframework.GridTestUtils.runMultiThreaded; import static org.apache.ignite.testframework.GridTestUtils.waitForCondition; /** */ @@ -47,7 +55,7 @@ public class IgniteStatisticsImplTest extends GridCommonAbstractWrapperTest { private static final String TBL_NAME = "TEST"; /** */ - private static final int ROW_CNT = 100; + private static final int ROW_CNT = (int)DFLT_ROW_CNT + 1; /** */ private IgniteEx client; @@ -102,7 +110,7 @@ public class IgniteStatisticsImplTest extends GridCommonAbstractWrapperTest { assertEquals(ROW_CNT, statistics().getRowCount(), 0.0); assertFalse(spi.hasBlockedMessages()); - stat.cliReqCnt.set(0); + stat.lastRefreshTs = 0; assertEquals(ROW_CNT, runAsync(() -> statistics().getRowCount()).get(5_000), 0.0); assertTrue(spi.hasBlockedMessages()); @@ -126,7 +134,7 @@ public class IgniteStatisticsImplTest extends GridCommonAbstractWrapperTest { spi.blockMessages(GridJobExecuteRequest.class, grid(1).name()); try { - stat.cliReqCnt.set(0); + stat.lastRefreshTs = 0; IgniteInternalFuture<Double> fut = runAsync(stat::getRowCount); @@ -141,6 +149,101 @@ public class IgniteStatisticsImplTest extends GridCommonAbstractWrapperTest { } } + /** */ + @Test + public void testRefreshByTime() throws Exception { + createAndFillTable(CacheMode.PARTITIONED); + + assertEquals(ROW_CNT, statistics().getRowCount(), 0.0); + + sql(String.format("INSERT INTO %s VALUES (%d, %d)", TBL_NAME, ROW_CNT, ROW_CNT)); + + assertTrue(waitForCondition(() -> statistics().getRowCount() == ROW_CNT + 1, 2 * STATS_REFRESH_INTERVAL)); + } + + /** */ + @Test + public void testIndexScanPlan() throws Exception { + createAndFillTable(CacheMode.PARTITIONED, 0, 1); + sql("CREATE INDEX idxVal ON %s(val)".formatted(TBL_NAME)); + + String sql = "EXPLAIN PLAN FOR SELECT * FROM %s WHERE val > 100 AND val < 200".formatted(TBL_NAME); + SqlFieldsQuery qry = new SqlFieldsQuery(sql).setSchema("PUBLIC"); + + try (FieldsQueryCursor<List<?>> cur = client.context().query().querySqlFields(qry, false)) { + List<List<?>> curr = cur.getAll(); + + String plan = (String)curr.get(0).get(0); + + // Plan for single item table need to goes through table scan, + // but due to IgniteStatisticsImpl.DFLT_ROW_CNT hack it plans through index scan, this part + // need to be fixed after cache for plans invalidation is done. + assertTrue(plan, !plan.contains(IgniteTableScan.class.getSimpleName())); + } + + createAndFillTable(CacheMode.PARTITIONED, 1, ROW_CNT); + + assertTrue(waitForCondition(() -> { + try (FieldsQueryCursor<List<?>> cur = client.context().query().querySqlFields(qry, false)) { + List<List<?>> curr = cur.getAll(); + + String plan = (String)curr.get(0).get(0); + return !plan.contains(IgniteTableScan.class.getSimpleName()); + } + }, 2 * STATS_REFRESH_INTERVAL)); + } + + /** */ + @Test + public void testConcurrentSelectsUseIndex() throws Exception { + String query = "select _key, _val from Person where salary >= ? and salary <= ?"; + + IgniteEx client = startClientGrid(3); + + int tableScans = 0; + int tables = 20; + String tableScanPlan = null; + + for (int t = 0; t < tables; t++) { + String cacheName = "person" + t; + + IgniteCache<Integer, Person> cache = client.createCache(new CacheConfiguration<Integer, Person>(cacheName) + .setIndexedTypes(Integer.class, Person.class)); + + for (int i = 0; i < 50; i++) + cache.put(i, new Person(i * 1000)); + + CyclicBarrier start = new CyclicBarrier(2); + + runMultiThreaded(() -> { + start.await(); + + return cache.query(new SqlFieldsQuery(query).setArgs(0, 1000)).getAll(); + }, 2, "select"); + + SqlFieldsQuery qry = new SqlFieldsQuery("EXPLAIN PLAN FOR " + query).setArgs(0, 1000); + String plan; + + try (FieldsQueryCursor<List<?>> cur = cache.withKeepBinary().query(new SqlFieldsQuery(qry).setArgs(0, 1000))) { + List<List<?>> curr = cur.getAll(); + + plan = (String)curr.get(0).get(0); + } + + boolean tableScan = plan.contains(IgniteTableScan.class.getSimpleName()); + + if (tableScan) { + tableScans++; + tableScanPlan = plan; + } + + client.destroyCache(cacheName); + } + + assertEquals("Tables where the select chose a full table scan: " + tableScans + " of " + tables + ", e.g.\n" + + tableScanPlan, 0, tableScans); + } + /** */ private IgniteStatisticsImpl statistics() { CalciteQueryProcessor proc = Commons.lookupComponent(client.context(), CalciteQueryProcessor.class); @@ -151,9 +254,14 @@ public class IgniteStatisticsImplTest extends GridCommonAbstractWrapperTest { /** */ 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())); + createAndFillTable(mode, 0, ROW_CNT); + } - String values = IntStream.range(0, ROW_CNT) + /** */ + private void createAndFillTable(CacheMode mode, int begin, int end) { + sql(String.format("CREATE TABLE IF NOT EXISTS %s(id INT PRIMARY KEY, val INT) WITH template=%s, backups=1", TBL_NAME, mode.name())); + + String values = IntStream.range(begin, end) .mapToObj(i -> "(" + i + "," + i + ")") .collect(Collectors.joining(",")); @@ -168,4 +276,16 @@ public class IgniteStatisticsImplTest extends GridCommonAbstractWrapperTest { cur.getAll(); } } + + /** */ + private static class Person { + /** */ + @QuerySqlField(index = true) + private final double salary; + + /** */ + Person(double salary) { + this.salary = salary; + } + } }
