Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/_docs/monitoring-metrics/system-views.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ This view exposes information about currently running SQL queries.
|FAILURES | long | Count of failures
|DURATION_MIN | long | Minimal duration of execution
|DURATION_MAX | long | Maximum duration of execution
|DURATION_TOTAL | long | Total execution duration for this query over all executions
|LAST_START_TIME | date | Last execution date
|===

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import org.apache.ignite.internal.processors.query.running.GridRunningQueryInfo;
import org.apache.ignite.internal.processors.query.running.HeavyQueriesTracker;
import org.apache.ignite.internal.processors.security.SecurityContext;
import org.apache.ignite.internal.util.GridTestClockTimer;
import org.apache.ignite.internal.util.future.GridCompoundFuture;
import org.apache.ignite.internal.util.typedef.F;
import org.apache.ignite.internal.util.typedef.internal.U;
Expand Down Expand Up @@ -1028,6 +1029,59 @@ public void testSqlFieldsQueryWithInitiatorId() throws Exception {
}
}

/**
* Verifies that query total execution time is correctly accumulated in the DURATION_TOTAL field of the
* SQL_QUERIES_HISTORY system view.
*/
@Test
public void testSqlQueryTotalDuration() throws Exception {
IgniteEx grid = grid(0);

IgniteCache<Long, Long> cache = prepareTestCache(grid);

AtomicLong curTotalTime = new AtomicLong();

int sleepTime = 500;

for (int i = 0; i < 2; i++) {
FunctionsLibrary.latch = new CountDownLatch(1);

IgniteInternalFuture<?> fut = GridTestUtils.runAsync(
() -> cache.query(new SqlFieldsQuery("select * from test where waitLatch(10000)")).getAll());

U.sleep(sleepTime);

GridTestClockTimer.update();

FunctionsLibrary.latch.countDown();

fut.get();

assertTrue(waitForCondition(() -> {
SystemView<SqlQueryHistoryView> history = grid.context().systemView().view(SQL_QRY_HIST_VIEW);

assertNotNull(history);

if (history.size() != 1)
return false;

SqlQueryHistoryView view = first(grid.context().systemView().view(SQL_QRY_HIST_VIEW));

assertNotNull(view);

long totalTime = view.durationTotal();

if (totalTime >= curTotalTime.get() + sleepTime) {
curTotalTime.set(totalTime);

return true;
}

return false;
}, 5_000));
}
}

/** */
private FieldsQueryCursor<List<?>> runNotFullyFetchedQuery(boolean loc) {
IgniteCache<Long, Long> cache = prepareTestCache(grid(0));
Expand All @@ -1049,6 +1103,7 @@ private boolean isHeavyQueriesTrackerEmpty() {
private static IgniteCache<Long, Long> prepareTestCache(IgniteEx grid) {
IgniteCache<Long, Long> cache = grid.createCache(new CacheConfiguration<Long, Long>()
.setName("test")
.setSqlFunctionClasses(FunctionsLibrary.class)
.setQueryEntities(Collections.singleton(new QueryEntity(Long.class, Long.class)
.setTableName("test")
.addQueryField("id", Long.class.getName(), null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,7 @@ public void testGetAllColumns() throws Exception {
"SYS.SQL_QUERIES_HISTORY.FAILURES.null",
"SYS.SQL_QUERIES_HISTORY.DURATION_MIN.null",
"SYS.SQL_QUERIES_HISTORY.DURATION_MAX.null",
"SYS.SQL_QUERIES_HISTORY.DURATION_TOTAL.null",
"SYS.SQL_QUERIES_HISTORY.LAST_START_TIME.null",
"SYS.SQL_QUERIES.QUERY_ID.null",
"SYS.SQL_QUERIES.SQL.null",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public class SqlQueryHistoryViewWalker implements SystemViewRowAttributeWalker<S
v.accept(5, "failures", long.class);
v.accept(6, "durationMin", long.class);
v.accept(7, "durationMax", long.class);
v.accept(8, "lastStartTime", Date.class);
v.accept(8, "durationTotal", long.class);
v.accept(9, "lastStartTime", Date.class);
}

/** {@inheritDoc} */
Expand All @@ -51,11 +52,12 @@ public class SqlQueryHistoryViewWalker implements SystemViewRowAttributeWalker<S
v.acceptLong(5, "failures", row.failures());
v.acceptLong(6, "durationMin", row.durationMin());
v.acceptLong(7, "durationMax", row.durationMax());
v.accept(8, "lastStartTime", Date.class, row.lastStartTime());
v.acceptLong(8, "durationTotal", row.durationTotal());
v.accept(9, "lastStartTime", Date.class, row.lastStartTime());
}

/** {@inheritDoc} */
@Override public int count() {
return 9;
return 10;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public QueryHistory(

long failures = failed ? 1 : 0;

val = new QueryHistoryMetricsValue(1, failures, duration, duration, startTime, initId);
val = new QueryHistoryMetricsValue(1, failures, duration, duration, duration, startTime, initId);

linkRef = new AtomicReference<>();
}
Expand Down Expand Up @@ -93,6 +93,7 @@ public QueryHistory aggregateWithNew(QueryHistory m) {
val.failures() + m.failures(),
Math.min(val.minTime(), m.minimumTime()),
Math.max(val.maxTime(), m.maximumTime()),
val.totalTime() + m.totalTime(),
Math.max(curLastStart, newLastStart),
initiatorId);

Expand Down Expand Up @@ -156,6 +157,15 @@ public long maximumTime() {
return val.maxTime();
}

/**
* Gets total execution time of query.
*
* @return Total execution time of query.
*/
public long totalTime() {
return val.totalTime();
}

/**
* Gets latest query start time.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class QueryHistoryMetricsValue {
/** Maximum time of execution. */
private final long maxTime;

/** Total time of execution. */
private final long totalTime;

/** Last start time of execution. */
private final long lastStartTime;

Expand All @@ -47,6 +50,7 @@ class QueryHistoryMetricsValue {
* @param failures Number of failure.
* @param minTime Min time of execution.
* @param maxTime Max time of execution.
* @param totalTime Total time of execution.
* @param lastStartTime Last start time of execution.
* @param initId Latest initiator ID.
*/
Expand All @@ -55,13 +59,15 @@ public QueryHistoryMetricsValue(
long failures,
long minTime,
long maxTime,
long totalTime,
long lastStartTime,
@Nullable String initId
) {
this.execs = execs;
this.failures = failures;
this.minTime = minTime;
this.maxTime = maxTime;
this.totalTime = totalTime;
this.lastStartTime = lastStartTime;
this.initId = initId;
}
Expand Down Expand Up @@ -102,6 +108,15 @@ public long maxTime() {
return maxTime;
}

/**
* Gets total execution time of query.
*
* @return Total execution time of query.
*/
public long totalTime() {
return totalTime;
}

/**
* Gets latest query start time.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,14 @@ public long durationMax() {
return qry.maximumTime();
}

/** @return Last start time. */
/** @return Total query duration. */
@Order(8)
public long durationTotal() {
return qry.totalTime();
}

/** @return Last start time. */
@Order(9)
public Date lastStartTime() {
return new Date(qry.lastStartTime());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.apache.ignite.internal.IgniteEx;
import org.apache.ignite.internal.IgniteInterruptedCheckedException;
import org.apache.ignite.internal.processors.query.running.QueryHistory;
import org.apache.ignite.internal.util.GridTestClockTimer;
import org.apache.ignite.internal.util.typedef.F;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
import org.apache.ignite.spi.discovery.tcp.ipfinder.TcpDiscoveryIpFinder;
Expand Down Expand Up @@ -245,6 +246,7 @@ public void testSqlFieldsQueryHistoryWithInitiatorId() {
.queryHistoryMetrics().values();

assertFalse(F.isEmpty(historyCol));
assertEquals(1, historyCol.size());

QueryHistory history = first(historyCol);

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

/**
* Test total duration of SQL queries.
*/
@Test
public void testSqlFieldsQueryTotalDuration() {
int sleepTime = 500;

SqlFieldsQuery qry = new SqlFieldsQuery("select * from A.String where _key=0 and sleep_func(?)").setArgs(sleepTime);

IgniteCache<Integer, String> cache = queryNode().context().cache().jcache("A");

long[] totalTimeArr = new long[2];

for (int i = 0; i < totalTimeArr.length; i++) {
cache.query(qry).getAll();

Collection<QueryHistory> historyCol = queryNode().context().query().runningQueryManager()
.queryHistoryMetrics().values();

assertFalse(F.isEmpty(historyCol));
assertEquals(1, historyCol.size());

QueryHistory history = first(historyCol);

assertNotNull(history);

totalTimeArr[i] = history.totalTime();
}

assertTrue(totalTimeArr[0] >= sleepTime);
assertTrue(totalTimeArr[1] >= totalTimeArr[0] + sleepTime);
}

/**
* Test metrics for SQL fields queries.
*
Expand Down Expand Up @@ -669,6 +704,18 @@ public static class Functions {
public static int fail() {
throw new IgniteSQLException("SQL function fail for test purpuses");
}

/**
*
*/
@QuerySqlFunction
public static boolean sleep_func(int sleep) {
doSleep(sleep);

GridTestClockTimer.update();

return true;
}
}

/**
Expand Down
Loading