Skip to content

Commit 94ec940

Browse files
oleg-vlskalex-plekhanov
authored andcommitted
IGNITE-26598 Add initiatorId to the SQL_QUERY_HISTORY system view - Fixes #12518.
Signed-off-by: Aleksey Plekhanov <[email protected]>
1 parent 35f9bbd commit 94ec940

File tree

9 files changed

+191
-38
lines changed

9 files changed

+191
-38
lines changed

docs/_docs/monitoring-metrics/system-views.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ control.bat --system-view NODE_METRICS --node-id a1b77663-b37f-4ddf-87a6-1e2d684
115115
|EVICTION_POLICY_FACTORY | string | String representation of eviction policy factory
116116
|EXPIRY_POLICY_FACTORY | string | String representation of expiry policy factory
117117
|CONFLICT_RESOLVER | string | String representation of cache conflict resolver
118-
|HAS_EXPIRING_ENTRIES | string | Yes if cache has entries pending expire
118+
|HAS_EXPIRING_ENTRIES | string | Yes if cache has entries pending expire
119119
|INTERCEPTOR | string | String representation of interceptor
120120
|IS_COPY_ON_READ | boolean | Flag indicating whether a copy of the value stored in the on-heap cache
121121
|IS_EAGER_TTL | boolean | Flag indicating whether expired cache entries will be eagerly removed from cache
@@ -512,6 +512,7 @@ This view exposes information about currently running SQL queries.
512512
|SCHEMA_NAME | string | Schema name
513513
|SQL | string | Query text
514514
|LOCAL | boolean | True if local only
515+
|INITIATOR_ID | string | Latest user-defined initiator ID
515516
|EXECUTIONS | long | Count of executions
516517
|FAILURES | long | Count of failures
517518
|DURATION_MIN | long | Minimal duration of execution

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

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@
7676
import org.apache.ignite.internal.util.typedef.internal.U;
7777
import org.apache.ignite.metric.MetricRegistry;
7878
import org.apache.ignite.spi.metric.LongMetric;
79+
import org.apache.ignite.spi.systemview.view.SqlQueryHistoryView;
80+
import org.apache.ignite.spi.systemview.view.SystemView;
7981
import org.apache.ignite.testframework.GridTestUtils;
8082
import org.apache.ignite.testframework.ListeningTestLogger;
8183
import org.apache.ignite.testframework.LogListener;
@@ -100,7 +102,9 @@
100102
import static org.apache.ignite.internal.processors.query.running.HeavyQueriesTracker.LONG_QUERY_ERROR_MSG;
101103
import static org.apache.ignite.internal.processors.query.running.HeavyQueriesTracker.LONG_QUERY_EXEC_MSG;
102104
import static org.apache.ignite.internal.processors.query.running.HeavyQueriesTracker.LONG_QUERY_FINISHED_MSG;
105+
import static org.apache.ignite.internal.processors.query.running.RunningQueryManager.SQL_QRY_HIST_VIEW;
103106
import static org.apache.ignite.internal.processors.query.running.RunningQueryManager.SQL_USER_QUERIES_REG_NAME;
107+
import static org.apache.ignite.internal.util.lang.GridFunc.first;
104108
import static org.apache.ignite.testframework.GridTestUtils.waitForCondition;
105109

106110
/**
@@ -997,19 +1001,36 @@ private void cancelAllQueriesAndWaitForCompletion(IgniteEx ignite, IgniteInterna
9971001
}
9981002
}
9991003

1004+
/** Verifies that user-defined query initiator ID is present in the SQL_QUERY_HISTORY system view. */
1005+
@Test
1006+
public void testSqlFieldsQueryWithInitiatorId() throws Exception {
1007+
IgniteEx grid = grid(0);
1008+
1009+
IgniteCache<Long, Long> cache = prepareTestCache(grid);
1010+
1011+
for (String testId : new String[] {"testId0", "testId1"}) {
1012+
cache.query(new SqlFieldsQuery("select * from test").setQueryInitiatorId(testId)).getAll();
1013+
1014+
assertTrue(waitForCondition(() -> {
1015+
SystemView<SqlQueryHistoryView> history = grid.context().systemView().view(SQL_QRY_HIST_VIEW);
1016+
1017+
assertNotNull(history);
1018+
1019+
if (history.size() != 1)
1020+
return false;
1021+
1022+
SqlQueryHistoryView view = first(history);
1023+
1024+
assertNotNull(view);
1025+
1026+
return testId.equals(view.initiatorId());
1027+
}, 3_000));
1028+
}
1029+
}
1030+
10001031
/** */
10011032
private FieldsQueryCursor<List<?>> runNotFullyFetchedQuery(boolean loc) {
1002-
IgniteCache<Long, Long> cache = grid(0).createCache(new CacheConfiguration<Long, Long>()
1003-
.setName("test")
1004-
.setQueryEntities(Collections.singleton(new QueryEntity(Long.class, Long.class)
1005-
.setTableName("test")
1006-
.addQueryField("id", Long.class.getName(), null)
1007-
.addQueryField("val", Long.class.getName(), null)
1008-
.setKeyFieldName("id")
1009-
.setValueFieldName("val"))));
1010-
1011-
for (long i = 0; i < 10; ++i)
1012-
cache.put(i, i);
1033+
IgniteCache<Long, Long> cache = prepareTestCache(grid(0));
10131034

10141035
return cache.query(new SqlFieldsQuery("select * from test").setLocal(loc).setPageSize(1));
10151036
}
@@ -1024,6 +1045,23 @@ private boolean isHeavyQueriesTrackerEmpty() {
10241045
return heavyQueriesTracker().getQueries().isEmpty();
10251046
}
10261047

1048+
/** */
1049+
private static IgniteCache<Long, Long> prepareTestCache(IgniteEx grid) {
1050+
IgniteCache<Long, Long> cache = grid.createCache(new CacheConfiguration<Long, Long>()
1051+
.setName("test")
1052+
.setQueryEntities(Collections.singleton(new QueryEntity(Long.class, Long.class)
1053+
.setTableName("test")
1054+
.addQueryField("id", Long.class.getName(), null)
1055+
.addQueryField("val", Long.class.getName(), null)
1056+
.setKeyFieldName("id")
1057+
.setValueFieldName("val"))));
1058+
1059+
for (long i = 0; i < 10; ++i)
1060+
cache.put(i, i);
1061+
1062+
return cache;
1063+
}
1064+
10271065
/** */
10281066
public static class FunctionsLibrary {
10291067
/** */

modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinMetadataSelfTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,7 @@ public void testGetAllColumns() throws Exception {
770770
"SYS.SQL_QUERIES_HISTORY.SCHEMA_NAME.null",
771771
"SYS.SQL_QUERIES_HISTORY.SQL.null",
772772
"SYS.SQL_QUERIES_HISTORY.LOCAL.null",
773+
"SYS.SQL_QUERIES_HISTORY.INITIATOR_ID.null",
773774
"SYS.SQL_QUERIES_HISTORY.EXECUTIONS.null",
774775
"SYS.SQL_QUERIES_HISTORY.FAILURES.null",
775776
"SYS.SQL_QUERIES_HISTORY.DURATION_MIN.null",

modules/core/src/main/java/org/apache/ignite/internal/managers/systemview/walker/SqlQueryHistoryViewWalker.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
/**
2525
* Generated by {@code org.apache.ignite.codegen.SystemViewRowAttributeWalkerGenerator}.
2626
* {@link SqlQueryHistoryView} attributes walker.
27-
*
27+
*
2828
* @see SqlQueryHistoryView
2929
*/
3030
public class SqlQueryHistoryViewWalker implements SystemViewRowAttributeWalker<SqlQueryHistoryView> {
@@ -33,27 +33,29 @@ public class SqlQueryHistoryViewWalker implements SystemViewRowAttributeWalker<S
3333
v.accept(0, "schemaName", String.class);
3434
v.accept(1, "sql", String.class);
3535
v.accept(2, "local", boolean.class);
36-
v.accept(3, "executions", long.class);
37-
v.accept(4, "failures", long.class);
38-
v.accept(5, "durationMin", long.class);
39-
v.accept(6, "durationMax", long.class);
40-
v.accept(7, "lastStartTime", Date.class);
36+
v.accept(3, "initiatorId", String.class);
37+
v.accept(4, "executions", long.class);
38+
v.accept(5, "failures", long.class);
39+
v.accept(6, "durationMin", long.class);
40+
v.accept(7, "durationMax", long.class);
41+
v.accept(8, "lastStartTime", Date.class);
4142
}
4243

4344
/** {@inheritDoc} */
4445
@Override public void visitAll(SqlQueryHistoryView row, AttributeWithValueVisitor v) {
4546
v.accept(0, "schemaName", String.class, row.schemaName());
4647
v.accept(1, "sql", String.class, row.sql());
4748
v.acceptBoolean(2, "local", row.local());
48-
v.acceptLong(3, "executions", row.executions());
49-
v.acceptLong(4, "failures", row.failures());
50-
v.acceptLong(5, "durationMin", row.durationMin());
51-
v.acceptLong(6, "durationMax", row.durationMax());
52-
v.accept(7, "lastStartTime", Date.class, row.lastStartTime());
49+
v.accept(3, "initiatorId", String.class, row.initiatorId());
50+
v.acceptLong(4, "executions", row.executions());
51+
v.acceptLong(5, "failures", row.failures());
52+
v.acceptLong(6, "durationMin", row.durationMin());
53+
v.acceptLong(7, "durationMax", row.durationMax());
54+
v.accept(8, "lastStartTime", Date.class, row.lastStartTime());
5355
}
5456

5557
/** {@inheritDoc} */
5658
@Override public int count() {
57-
return 8;
59+
return 9;
5860
}
5961
}

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,22 @@ public class QueryHistory {
4747
* @param startTime Start time of query execution.
4848
* @param duration Duration of query execution.
4949
* @param failed {@code True} query executed unsuccessfully {@code false} otherwise.
50+
* @param initId Initiator ID.
5051
*/
51-
public QueryHistory(String qry, String schema, boolean loc, long startTime, long duration, boolean failed) {
52+
public QueryHistory(
53+
String qry,
54+
String schema,
55+
boolean loc,
56+
long startTime,
57+
long duration,
58+
boolean failed,
59+
@Nullable String initId
60+
) {
5261
key = new QueryHistoryKey(qry, schema, loc);
5362

5463
long failures = failed ? 1 : 0;
5564

56-
val = new QueryHistoryMetricsValue(1, failures, duration, duration, startTime);
65+
val = new QueryHistoryMetricsValue(1, failures, duration, duration, startTime, initId);
5766

5867
linkRef = new AtomicReference<>();
5968
}
@@ -72,12 +81,20 @@ public QueryHistoryKey key() {
7281
* @return Aggregated metrics.
7382
*/
7483
public QueryHistory aggregateWithNew(QueryHistory m) {
84+
long curLastStart = val.lastStartTime();
85+
long newLastStart = m.lastStartTime();
86+
87+
String initiatorId = curLastStart > newLastStart
88+
? val.initiatorId()
89+
: m.initiatorId();
90+
7591
val = new QueryHistoryMetricsValue(
7692
val.execs() + m.executions(),
7793
val.failures() + m.failures(),
7894
Math.min(val.minTime(), m.minimumTime()),
7995
Math.max(val.maxTime(), m.maximumTime()),
80-
Math.max(val.lastStartTime(), m.lastStartTime()));
96+
Math.max(curLastStart, newLastStart),
97+
initiatorId);
8198

8299
return this;
83100
}
@@ -148,6 +165,15 @@ public long lastStartTime() {
148165
return val.lastStartTime();
149166
}
150167

168+
/**
169+
* Gets latest initiator ID.
170+
*
171+
* @return Latest initiator ID.
172+
*/
173+
public String initiatorId() {
174+
return val.initiatorId();
175+
}
176+
151177
/**
152178
* @return Link to internal node in eviction deque.
153179
*/

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
package org.apache.ignite.internal.processors.query.running;
2020

21+
import org.jetbrains.annotations.Nullable;
22+
2123
/**
2224
* Immutable query metrics.
2325
*/
@@ -37,19 +39,31 @@ class QueryHistoryMetricsValue {
3739
/** Last start time of execution. */
3840
private final long lastStartTime;
3941

42+
/** Latest user-defined initiator ID. */
43+
private final String initId;
44+
4045
/**
4146
* @param execs Number of executions.
4247
* @param failures Number of failure.
4348
* @param minTime Min time of execution.
4449
* @param maxTime Max time of execution.
4550
* @param lastStartTime Last start time of execution.
51+
* @param initId Latest initiator ID.
4652
*/
47-
public QueryHistoryMetricsValue(long execs, long failures, long minTime, long maxTime, long lastStartTime) {
53+
public QueryHistoryMetricsValue(
54+
long execs,
55+
long failures,
56+
long minTime,
57+
long maxTime,
58+
long lastStartTime,
59+
@Nullable String initId
60+
) {
4861
this.execs = execs;
4962
this.failures = failures;
5063
this.minTime = minTime;
5164
this.maxTime = maxTime;
5265
this.lastStartTime = lastStartTime;
66+
this.initId = initId;
5367
}
5468

5569
/**
@@ -96,4 +110,13 @@ public long maxTime() {
96110
public long lastStartTime() {
97111
return lastStartTime;
98112
}
113+
114+
/**
115+
* Gets latest initiator ID.
116+
*
117+
* @return Latest initiator ID.
118+
*/
119+
public String initiatorId() {
120+
return initId;
121+
}
99122
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,12 @@ void collectHistory(GridRunningQueryInfo runningQryInfo, boolean failed) {
5757

5858
String qry = runningQryInfo.query();
5959
String schema = runningQryInfo.schemaName();
60+
String initId = runningQryInfo.queryInitiatorId();
6061
boolean loc = runningQryInfo.local();
6162
long startTime = runningQryInfo.startTime();
6263
long duration = U.currentTimeMillis() - startTime;
6364

64-
QueryHistory hist = new QueryHistory(qry, schema, loc, startTime, duration, failed);
65+
QueryHistory hist = new QueryHistory(qry, schema, loc, startTime, duration, failed, initId);
6566

6667
QueryHistory mergedHist = qryHist.merge(hist.key(), hist, QueryHistory::aggregateWithNew);
6768

modules/core/src/main/java/org/apache/ignite/spi/systemview/view/SqlQueryHistoryView.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,32 +53,38 @@ public boolean local() {
5353
return qry.local();
5454
}
5555

56-
/** @return Number of executions of the query. */
56+
/** @return Latest initiator ID. */
5757
@Order(3)
58+
public String initiatorId() {
59+
return qry.initiatorId();
60+
}
61+
62+
/** @return Number of executions of the query. */
63+
@Order(4)
5864
public long executions() {
5965
return qry.executions();
6066
}
6167

6268
/** @return Number of failed execution of the query. */
63-
@Order(4)
69+
@Order(5)
6470
public long failures() {
6571
return qry.failures();
6672
}
6773

6874
/** @return Minimal query duration. */
69-
@Order(5)
75+
@Order(6)
7076
public long durationMin() {
7177
return qry.minimumTime();
7278
}
7379

7480
/** @return Maximum query duration. */
75-
@Order(6)
81+
@Order(7)
7682
public long durationMax() {
7783
return qry.maximumTime();
7884
}
7985

8086
/** @return Last start time. */
81-
@Order(7)
87+
@Order(8)
8288
public Date lastStartTime() {
8389
return new Date(qry.lastStartTime());
8490
}

0 commit comments

Comments
 (0)