Skip to content

Commit 2981aeb

Browse files
oleg-vlskalex-plekhanov
authored andcommitted
IGNITE-26596 Add the DURATION_TOTAL field to the SQL_QUERIES_HISTORY system view - Fixes #12544.
Signed-off-by: Aleksey Plekhanov <[email protected]>
1 parent ed18846 commit 2981aeb

File tree

8 files changed

+142
-5
lines changed

8 files changed

+142
-5
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,7 @@ This view exposes information about currently running SQL queries.
517517
|FAILURES | long | Count of failures
518518
|DURATION_MIN | long | Minimal duration of execution
519519
|DURATION_MAX | long | Maximum duration of execution
520+
|DURATION_TOTAL | long | Total execution duration for this query over all executions
520521
|LAST_START_TIME | date | Last execution date
521522
|===
522523

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
import org.apache.ignite.internal.processors.query.running.GridRunningQueryInfo;
7272
import org.apache.ignite.internal.processors.query.running.HeavyQueriesTracker;
7373
import org.apache.ignite.internal.processors.security.SecurityContext;
74+
import org.apache.ignite.internal.util.GridTestClockTimer;
7475
import org.apache.ignite.internal.util.future.GridCompoundFuture;
7576
import org.apache.ignite.internal.util.typedef.F;
7677
import org.apache.ignite.internal.util.typedef.internal.U;
@@ -1028,6 +1029,59 @@ public void testSqlFieldsQueryWithInitiatorId() throws Exception {
10281029
}
10291030
}
10301031

1032+
/**
1033+
* Verifies that query total execution time is correctly accumulated in the DURATION_TOTAL field of the
1034+
* SQL_QUERIES_HISTORY system view.
1035+
*/
1036+
@Test
1037+
public void testSqlQueryTotalDuration() throws Exception {
1038+
IgniteEx grid = grid(0);
1039+
1040+
IgniteCache<Long, Long> cache = prepareTestCache(grid);
1041+
1042+
AtomicLong curTotalTime = new AtomicLong();
1043+
1044+
int sleepTime = 500;
1045+
1046+
for (int i = 0; i < 2; i++) {
1047+
FunctionsLibrary.latch = new CountDownLatch(1);
1048+
1049+
IgniteInternalFuture<?> fut = GridTestUtils.runAsync(
1050+
() -> cache.query(new SqlFieldsQuery("select * from test where waitLatch(10000)")).getAll());
1051+
1052+
U.sleep(sleepTime);
1053+
1054+
GridTestClockTimer.update();
1055+
1056+
FunctionsLibrary.latch.countDown();
1057+
1058+
fut.get();
1059+
1060+
assertTrue(waitForCondition(() -> {
1061+
SystemView<SqlQueryHistoryView> history = grid.context().systemView().view(SQL_QRY_HIST_VIEW);
1062+
1063+
assertNotNull(history);
1064+
1065+
if (history.size() != 1)
1066+
return false;
1067+
1068+
SqlQueryHistoryView view = first(grid.context().systemView().view(SQL_QRY_HIST_VIEW));
1069+
1070+
assertNotNull(view);
1071+
1072+
long totalTime = view.durationTotal();
1073+
1074+
if (totalTime >= curTotalTime.get() + sleepTime) {
1075+
curTotalTime.set(totalTime);
1076+
1077+
return true;
1078+
}
1079+
1080+
return false;
1081+
}, 5_000));
1082+
}
1083+
}
1084+
10311085
/** */
10321086
private FieldsQueryCursor<List<?>> runNotFullyFetchedQuery(boolean loc) {
10331087
IgniteCache<Long, Long> cache = prepareTestCache(grid(0));
@@ -1049,6 +1103,7 @@ private boolean isHeavyQueriesTrackerEmpty() {
10491103
private static IgniteCache<Long, Long> prepareTestCache(IgniteEx grid) {
10501104
IgniteCache<Long, Long> cache = grid.createCache(new CacheConfiguration<Long, Long>()
10511105
.setName("test")
1106+
.setSqlFunctionClasses(FunctionsLibrary.class)
10521107
.setQueryEntities(Collections.singleton(new QueryEntity(Long.class, Long.class)
10531108
.setTableName("test")
10541109
.addQueryField("id", Long.class.getName(), null)

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
@@ -775,6 +775,7 @@ public void testGetAllColumns() throws Exception {
775775
"SYS.SQL_QUERIES_HISTORY.FAILURES.null",
776776
"SYS.SQL_QUERIES_HISTORY.DURATION_MIN.null",
777777
"SYS.SQL_QUERIES_HISTORY.DURATION_MAX.null",
778+
"SYS.SQL_QUERIES_HISTORY.DURATION_TOTAL.null",
778779
"SYS.SQL_QUERIES_HISTORY.LAST_START_TIME.null",
779780
"SYS.SQL_QUERIES.QUERY_ID.null",
780781
"SYS.SQL_QUERIES.SQL.null",

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public class SqlQueryHistoryViewWalker implements SystemViewRowAttributeWalker<S
3838
v.accept(5, "failures", long.class);
3939
v.accept(6, "durationMin", long.class);
4040
v.accept(7, "durationMax", long.class);
41-
v.accept(8, "lastStartTime", Date.class);
41+
v.accept(8, "durationTotal", long.class);
42+
v.accept(9, "lastStartTime", Date.class);
4243
}
4344

4445
/** {@inheritDoc} */
@@ -51,11 +52,12 @@ public class SqlQueryHistoryViewWalker implements SystemViewRowAttributeWalker<S
5152
v.acceptLong(5, "failures", row.failures());
5253
v.acceptLong(6, "durationMin", row.durationMin());
5354
v.acceptLong(7, "durationMax", row.durationMax());
54-
v.accept(8, "lastStartTime", Date.class, row.lastStartTime());
55+
v.acceptLong(8, "durationTotal", row.durationTotal());
56+
v.accept(9, "lastStartTime", Date.class, row.lastStartTime());
5557
}
5658

5759
/** {@inheritDoc} */
5860
@Override public int count() {
59-
return 9;
61+
return 10;
6062
}
6163
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public QueryHistory(
6262

6363
long failures = failed ? 1 : 0;
6464

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

6767
linkRef = new AtomicReference<>();
6868
}
@@ -93,6 +93,7 @@ public QueryHistory aggregateWithNew(QueryHistory m) {
9393
val.failures() + m.failures(),
9494
Math.min(val.minTime(), m.minimumTime()),
9595
Math.max(val.maxTime(), m.maximumTime()),
96+
val.totalTime() + m.totalTime(),
9697
Math.max(curLastStart, newLastStart),
9798
initiatorId);
9899

@@ -156,6 +157,15 @@ public long maximumTime() {
156157
return val.maxTime();
157158
}
158159

160+
/**
161+
* Gets total execution time of query.
162+
*
163+
* @return Total execution time of query.
164+
*/
165+
public long totalTime() {
166+
return val.totalTime();
167+
}
168+
159169
/**
160170
* Gets latest query start time.
161171
*

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ class QueryHistoryMetricsValue {
3636
/** Maximum time of execution. */
3737
private final long maxTime;
3838

39+
/** Total time of execution. */
40+
private final long totalTime;
41+
3942
/** Last start time of execution. */
4043
private final long lastStartTime;
4144

@@ -47,6 +50,7 @@ class QueryHistoryMetricsValue {
4750
* @param failures Number of failure.
4851
* @param minTime Min time of execution.
4952
* @param maxTime Max time of execution.
53+
* @param totalTime Total time of execution.
5054
* @param lastStartTime Last start time of execution.
5155
* @param initId Latest initiator ID.
5256
*/
@@ -55,13 +59,15 @@ public QueryHistoryMetricsValue(
5559
long failures,
5660
long minTime,
5761
long maxTime,
62+
long totalTime,
5863
long lastStartTime,
5964
@Nullable String initId
6065
) {
6166
this.execs = execs;
6267
this.failures = failures;
6368
this.minTime = minTime;
6469
this.maxTime = maxTime;
70+
this.totalTime = totalTime;
6571
this.lastStartTime = lastStartTime;
6672
this.initId = initId;
6773
}
@@ -102,6 +108,15 @@ public long maxTime() {
102108
return maxTime;
103109
}
104110

111+
/**
112+
* Gets total execution time of query.
113+
*
114+
* @return Total execution time of query.
115+
*/
116+
public long totalTime() {
117+
return totalTime;
118+
}
119+
105120
/**
106121
* Gets latest query start time.
107122
*

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,14 @@ public long durationMax() {
8383
return qry.maximumTime();
8484
}
8585

86-
/** @return Last start time. */
86+
/** @return Total query duration. */
8787
@Order(8)
88+
public long durationTotal() {
89+
return qry.totalTime();
90+
}
91+
92+
/** @return Last start time. */
93+
@Order(9)
8894
public Date lastStartTime() {
8995
return new Date(qry.lastStartTime());
9096
}

modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/SqlQueryHistorySelfTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.apache.ignite.internal.IgniteEx;
4242
import org.apache.ignite.internal.IgniteInterruptedCheckedException;
4343
import org.apache.ignite.internal.processors.query.running.QueryHistory;
44+
import org.apache.ignite.internal.util.GridTestClockTimer;
4445
import org.apache.ignite.internal.util.typedef.F;
4546
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
4647
import org.apache.ignite.spi.discovery.tcp.ipfinder.TcpDiscoveryIpFinder;
@@ -245,6 +246,7 @@ public void testSqlFieldsQueryHistoryWithInitiatorId() {
245246
.queryHistoryMetrics().values();
246247

247248
assertFalse(F.isEmpty(historyCol));
249+
assertEquals(1, historyCol.size());
248250

249251
QueryHistory history = first(historyCol);
250252

@@ -253,6 +255,39 @@ public void testSqlFieldsQueryHistoryWithInitiatorId() {
253255
assertEquals(testId1, history.initiatorId());
254256
}
255257

258+
/**
259+
* Test total duration of SQL queries.
260+
*/
261+
@Test
262+
public void testSqlFieldsQueryTotalDuration() {
263+
int sleepTime = 500;
264+
265+
SqlFieldsQuery qry = new SqlFieldsQuery("select * from A.String where _key=0 and sleep_func(?)").setArgs(sleepTime);
266+
267+
IgniteCache<Integer, String> cache = queryNode().context().cache().jcache("A");
268+
269+
long[] totalTimeArr = new long[2];
270+
271+
for (int i = 0; i < totalTimeArr.length; i++) {
272+
cache.query(qry).getAll();
273+
274+
Collection<QueryHistory> historyCol = queryNode().context().query().runningQueryManager()
275+
.queryHistoryMetrics().values();
276+
277+
assertFalse(F.isEmpty(historyCol));
278+
assertEquals(1, historyCol.size());
279+
280+
QueryHistory history = first(historyCol);
281+
282+
assertNotNull(history);
283+
284+
totalTimeArr[i] = history.totalTime();
285+
}
286+
287+
assertTrue(totalTimeArr[0] >= sleepTime);
288+
assertTrue(totalTimeArr[1] >= totalTimeArr[0] + sleepTime);
289+
}
290+
256291
/**
257292
* Test metrics for SQL fields queries.
258293
*
@@ -669,6 +704,18 @@ public static class Functions {
669704
public static int fail() {
670705
throw new IgniteSQLException("SQL function fail for test purpuses");
671706
}
707+
708+
/**
709+
*
710+
*/
711+
@QuerySqlFunction
712+
public static boolean sleep_func(int sleep) {
713+
doSleep(sleep);
714+
715+
GridTestClockTimer.update();
716+
717+
return true;
718+
}
672719
}
673720

674721
/**

0 commit comments

Comments
 (0)