Skip to content

Commit dee59bf

Browse files
oleg-vlskalex-plekhanov
authored andcommitted
IGNITE-24669 Set H2 default SQL plan history to zero - Fixes #11903.
Signed-off-by: Aleksey Plekhanov <plehanov.alex@gmail.com>
1 parent eb266bd commit dee59bf

File tree

4 files changed

+115
-12
lines changed

4 files changed

+115
-12
lines changed

modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/SqlPlanHistoryIntegrationTest.java

Lines changed: 96 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import org.junit.runner.RunWith;
5858
import org.junit.runners.Parameterized;
5959

60+
import static org.apache.ignite.configuration.SqlConfiguration.DFLT_SQL_PLAN_HISTORY_SIZE;
6061
import static org.apache.ignite.internal.processors.query.running.RunningQueryManager.SQL_PLAN_HIST_VIEW;
6162
import static org.apache.ignite.testframework.GridTestUtils.waitForCondition;
6263
import static org.junit.Assert.assertNotEquals;
@@ -101,6 +102,18 @@ public class SqlPlanHistoryIntegrationTest extends GridCommonAbstractTest {
101102
), false
102103
);
103104

105+
/** Flag indicating whether SQL is configured by using {@link IgniteConfiguration#setSqlConfiguration(SqlConfiguration)}. */
106+
private boolean isSqlConfigured = true;
107+
108+
/** Flag indicating whether the SQL engine is configured within {@link SqlConfiguration}. */
109+
private boolean isSqlEngineConfigured = true;
110+
111+
/**
112+
* Flag indicating whether a custom SQL plan history size is explicitly set within {@link SqlConfiguration}.
113+
* If {@code false}, the default SQL plan history size will be used.
114+
*/
115+
private boolean isPlanHistorySizeSet = true;
116+
104117
/** SQL plan history size. */
105118
private int planHistorySize = 10;
106119

@@ -140,10 +153,18 @@ public static Collection<Object[]> params() {
140153
@Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
141154
IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
142155

143-
cfg.setSqlConfiguration(new SqlConfiguration()
144-
.setSqlPlanHistorySize(planHistorySize)
145-
.setQueryEnginesConfiguration(configureSqlEngine())
146-
);
156+
if (isSqlConfigured) {
157+
SqlConfiguration sqlCfg = new SqlConfiguration();
158+
159+
if (isSqlEngineConfigured) {
160+
sqlCfg.setQueryEnginesConfiguration(configureSqlEngine());
161+
162+
if (isPlanHistorySizeSet)
163+
sqlCfg.setSqlPlanHistorySize(planHistorySize);
164+
}
165+
166+
cfg.setSqlConfiguration(sqlCfg);
167+
}
147168

148169
return cfg.setCacheConfiguration(
149170
configureCache("A", Integer.class, String.class),
@@ -425,6 +446,45 @@ public void testResetPlanHistoryMetrics() throws Exception {
425446
checkReset(() -> queryNode().context().query().runningQueryManager().resetPlanHistoryMetrics());
426447
}
427448

449+
/**
450+
* Checks that the actual SQL plan history remains empty and its size in {@link SqlConfiguration} equals {@code -1}
451+
* when SQL is not explicitly configured. In such cases, H2 is used as the default engine.
452+
*/
453+
@Test
454+
public void testNoSqlConfiguration() throws Exception {
455+
isSqlConfigured = false;
456+
457+
checkDefaultHistorySize(true, -1, 0, 1);
458+
}
459+
460+
/**
461+
* Checks that the actual SQL plan history remains empty and its size in {@link SqlConfiguration} equals {@code -1}
462+
* when the SQL engine is not configured. In such cases, H2 is used as the default engine.
463+
*/
464+
@Test
465+
public void testNoSqlEngineConfiguration() throws Exception {
466+
isSqlEngineConfigured = false;
467+
468+
checkDefaultHistorySize(true, -1, 0, 1);
469+
}
470+
471+
/**
472+
* Checks that the actual SQL plan history size and its value in {@link SqlConfiguration} are correct for both
473+
* Calcite and H2 engines when the history size is not explicitly set.
474+
*/
475+
@Test
476+
public void testDefaultHistorySize() throws Exception {
477+
isPlanHistorySizeSet = false;
478+
479+
boolean isCalcite = sqlEngine.equals(CalciteQueryEngineConfiguration.ENGINE_NAME);
480+
481+
int expConfHistSize = isCalcite ? DFLT_SQL_PLAN_HISTORY_SIZE : -1;
482+
int expHistSize = isCalcite ? DFLT_SQL_PLAN_HISTORY_SIZE : 0;
483+
int qrys = isCalcite ? DFLT_SQL_PLAN_HISTORY_SIZE : 1;
484+
485+
checkDefaultHistorySize(false, expConfHistSize, expHistSize, qrys);
486+
}
487+
428488
/**
429489
* Checks that there is no 'scanCount' suffix in H2 local query plans even if identical queries are not
430490
* executed one after another (when there are other queries executed between them).
@@ -709,6 +769,38 @@ public void checkEmptyHistory(Runnable setup, boolean startGridFirst) throws Exc
709769
assertTrue(getSqlPlanHistory().isEmpty());
710770
}
711771

772+
/**
773+
* @param isSingleEngineCheck Flag indicating whether the test should be run for one SQL engine or for both of them.
774+
* @param expConfHistSize Expected SQL plan history size in {@link SqlConfiguration}.
775+
* @param expHistSize Expected actual SQL plan history size.
776+
* @param qrys Nubmer of queries to be run in the test.
777+
*/
778+
public void checkDefaultHistorySize(
779+
boolean isSingleEngineCheck,
780+
int expConfHistSize,
781+
int expHistSize,
782+
int qrys
783+
) throws Exception {
784+
if (isSingleEngineCheck)
785+
assumeFalse(sqlEngine == CalciteQueryEngineConfiguration.ENGINE_NAME);
786+
787+
assumeFalse(isClient || loc || isFullyFetched);
788+
789+
startTestGrid();
790+
791+
int confHistSize = queryNode().configuration().getSqlConfiguration().getSqlPlanHistorySize();
792+
793+
assertEquals(expConfHistSize, confHistSize);
794+
795+
for (int i = 0; i < qrys; i++) {
796+
String sql = "select * from A.String where _key <= " + i;
797+
798+
cacheQuery(new SqlFieldsQuery(sql), "A");
799+
}
800+
801+
assertEquals(expHistSize, getSqlPlanHistory().size());
802+
}
803+
712804
/** */
713805
private static class Person {
714806
/** */

modules/core/src/main/java/org/apache/ignite/configuration/SqlConfiguration.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class SqlConfiguration {
4949
private int sqlQryHistSize = DFLT_SQL_QUERY_HISTORY_SIZE;
5050

5151
/** SQL plan history size. */
52-
private int sqlPlanHistSize = DFLT_SQL_PLAN_HISTORY_SIZE;
52+
private int sqlPlanHistSize = -1;
5353

5454
/** Enable validation of key & values against sql schema. */
5555
private boolean validationEnabled;
@@ -114,9 +114,9 @@ public SqlConfiguration setSqlQueryHistorySize(int size) {
114114
}
115115

116116
/**
117-
* Number of SQL plan history elements to keep in memory. If not provided, then default value {@link
118-
* #DFLT_SQL_PLAN_HISTORY_SIZE} is used. If provided value is less or equals 0, then gathering SQL plan history
119-
* will be switched off.
117+
* Number of SQL plan history elements to keep in memory. If not provided, then default value {@code -1} applies when
118+
* the indexing module is in use and {@link #DFLT_SQL_PLAN_HISTORY_SIZE} is used for other engines. If provided value
119+
* is less or equals 0, then gathering SQL plan history will be switched off.
120120
*
121121
* @return SQL plan history size.
122122
*/
@@ -125,8 +125,8 @@ public int getSqlPlanHistorySize() {
125125
}
126126

127127
/**
128-
* Sets number of SQL plan history elements kept in memory. If not explicitly set, then default value is {@link
129-
* #DFLT_SQL_PLAN_HISTORY_SIZE}.
128+
* Sets number of SQL plan history elements kept in memory. If not explicitly set, then default value {@code -1}
129+
* applies when the indexing module is in use and {@link #DFLT_SQL_PLAN_HISTORY_SIZE} is used for other engines.
130130
*
131131
* @param size Number of SQL plan history elements kept in memory.
132132
* @return {@code this} for chaining.

modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryProcessor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4441,4 +4441,9 @@ public SchemaSqlViewManager sqlViewManager() {
44414441
public IgniteStatisticsManager statsManager() {
44424442
return statsMgr;
44434443
}
4444+
4445+
/** @return Default query engine. */
4446+
public QueryEngine defaultQueryEngine() {
4447+
return dfltQryEngine;
4448+
}
44444449
}

modules/core/src/main/java/org/apache/ignite/internal/processors/query/running/RunningQueryManager.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
import org.apache.ignite.spi.systemview.view.SqlQueryView;
7373
import org.jetbrains.annotations.Nullable;
7474

75+
import static org.apache.ignite.configuration.SqlConfiguration.DFLT_SQL_PLAN_HISTORY_SIZE;
7576
import static org.apache.ignite.internal.processors.cache.query.GridCacheQueryType.SQL;
7677
import static org.apache.ignite.internal.processors.cache.query.GridCacheQueryType.SQL_FIELDS;
7778
import static org.apache.ignite.internal.processors.metric.impl.MetricUtils.metricName;
@@ -192,14 +193,19 @@ public RunningQueryManager(GridKernalContext ctx) {
192193

193194
localNodeId = ctx.localNodeId();
194195

195-
histSz = ctx.config().getSqlConfiguration().getSqlQueryHistorySize();
196+
final SqlConfiguration sqlCfg = ctx.config().getSqlConfiguration();
197+
198+
histSz = sqlCfg.getSqlQueryHistorySize();
196199
closure = ctx.closure();
197200

198201
qryHistTracker = new QueryHistoryTracker(histSz);
199202

200203
heavyQrysTracker = ctx.query().moduleEnabled() ? new HeavyQueriesTracker(ctx) : null;
201204

202-
planHistSz = ctx.config().getSqlConfiguration().getSqlPlanHistorySize();
205+
if (sqlCfg.getSqlPlanHistorySize() < 0 && ctx.query().defaultQueryEngine() != null)
206+
sqlCfg.setSqlPlanHistorySize(DFLT_SQL_PLAN_HISTORY_SIZE);
207+
208+
planHistSz = sqlCfg.getSqlPlanHistorySize();
203209

204210
planHistTracker = new SqlPlanHistoryTracker(planHistSz);
205211

0 commit comments

Comments
 (0)