This is an automated email from the ASF dual-hosted git repository.

alexpl pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ignite.git


The following commit(s) were added to refs/heads/master by this push:
     new 2981aebfba5 IGNITE-26596 Add the DURATION_TOTAL field to the 
SQL_QUERIES_HISTORY system view - Fixes #12544.
2981aebfba5 is described below

commit 2981aebfba5071bdd665d5865c2514a53487bd6e
Author: oleg-vlsk <[email protected]>
AuthorDate: Wed Dec 3 09:54:49 2025 +0300

    IGNITE-26596 Add the DURATION_TOTAL field to the SQL_QUERIES_HISTORY system 
view - Fixes #12544.
    
    Signed-off-by: Aleksey Plekhanov <[email protected]>
---
 docs/_docs/monitoring-metrics/system-views.adoc    |  1 +
 .../integration/SqlDiagnosticIntegrationTest.java  | 55 ++++++++++++++++++++++
 .../ignite/jdbc/thin/JdbcThinMetadataSelfTest.java |  1 +
 .../walker/SqlQueryHistoryViewWalker.java          |  8 ++--
 .../processors/query/running/QueryHistory.java     | 12 ++++-
 .../query/running/QueryHistoryMetricsValue.java    | 15 ++++++
 .../spi/systemview/view/SqlQueryHistoryView.java   |  8 +++-
 .../processors/query/SqlQueryHistorySelfTest.java  | 47 ++++++++++++++++++
 8 files changed, 142 insertions(+), 5 deletions(-)

diff --git a/docs/_docs/monitoring-metrics/system-views.adoc 
b/docs/_docs/monitoring-metrics/system-views.adoc
index 170e27d830a..f9afb0ccc90 100644
--- a/docs/_docs/monitoring-metrics/system-views.adoc
+++ b/docs/_docs/monitoring-metrics/system-views.adoc
@@ -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
 |===
 
diff --git 
a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/SqlDiagnosticIntegrationTest.java
 
b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/SqlDiagnosticIntegrationTest.java
index 31067506281..00bf5ebd4d1 100644
--- 
a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/SqlDiagnosticIntegrationTest.java
+++ 
b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/SqlDiagnosticIntegrationTest.java
@@ -71,6 +71,7 @@ import 
org.apache.ignite.internal.processors.query.calcite.exec.task.StripedQuer
 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;
@@ -1028,6 +1029,59 @@ public class SqlDiagnosticIntegrationTest extends 
AbstractBasicIntegrationTest {
         }
     }
 
+    /**
+     * 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));
@@ -1049,6 +1103,7 @@ public class SqlDiagnosticIntegrationTest extends 
AbstractBasicIntegrationTest {
     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)
diff --git 
a/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinMetadataSelfTest.java
 
b/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinMetadataSelfTest.java
index 2425aa23134..bebf1e48861 100644
--- 
a/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinMetadataSelfTest.java
+++ 
b/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinMetadataSelfTest.java
@@ -775,6 +775,7 @@ public class JdbcThinMetadataSelfTest extends 
JdbcThinAbstractSelfTest {
                 "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",
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/managers/systemview/walker/SqlQueryHistoryViewWalker.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/managers/systemview/walker/SqlQueryHistoryViewWalker.java
index e4b3c52344e..5384928f315 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/managers/systemview/walker/SqlQueryHistoryViewWalker.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/managers/systemview/walker/SqlQueryHistoryViewWalker.java
@@ -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} */
@@ -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;
     }
 }
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/running/QueryHistory.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/running/QueryHistory.java
index 289204ae7f0..a7aa97a0dba 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/running/QueryHistory.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/running/QueryHistory.java
@@ -62,7 +62,7 @@ public class 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<>();
     }
@@ -93,6 +93,7 @@ public class QueryHistory {
             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);
 
@@ -156,6 +157,15 @@ public class QueryHistory {
         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.
      *
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/running/QueryHistoryMetricsValue.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/running/QueryHistoryMetricsValue.java
index 5213c47dee3..db7b0b939db 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/running/QueryHistoryMetricsValue.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/running/QueryHistoryMetricsValue.java
@@ -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;
 
@@ -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.
      */
@@ -55,6 +59,7 @@ class QueryHistoryMetricsValue {
         long failures,
         long minTime,
         long maxTime,
+        long totalTime,
         long lastStartTime,
         @Nullable String initId
     ) {
@@ -62,6 +67,7 @@ class QueryHistoryMetricsValue {
         this.failures = failures;
         this.minTime = minTime;
         this.maxTime = maxTime;
+        this.totalTime = totalTime;
         this.lastStartTime = lastStartTime;
         this.initId = initId;
     }
@@ -102,6 +108,15 @@ class QueryHistoryMetricsValue {
         return maxTime;
     }
 
+    /**
+     * Gets total execution time of query.
+     *
+     * @return Total execution time of query.
+     */
+    public long totalTime() {
+        return totalTime;
+    }
+
     /**
      * Gets latest query start time.
      *
diff --git 
a/modules/core/src/main/java/org/apache/ignite/spi/systemview/view/SqlQueryHistoryView.java
 
b/modules/core/src/main/java/org/apache/ignite/spi/systemview/view/SqlQueryHistoryView.java
index cefe4d430fe..ce5cea4fdfa 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/spi/systemview/view/SqlQueryHistoryView.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/spi/systemview/view/SqlQueryHistoryView.java
@@ -83,8 +83,14 @@ public class SqlQueryHistoryView {
         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());
     }
diff --git 
a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/SqlQueryHistorySelfTest.java
 
b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/SqlQueryHistorySelfTest.java
index 6d9f1ed97ef..2b97ef780bf 100644
--- 
a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/SqlQueryHistorySelfTest.java
+++ 
b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/SqlQueryHistorySelfTest.java
@@ -41,6 +41,7 @@ import org.apache.ignite.configuration.SqlConfiguration;
 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;
@@ -245,6 +246,7 @@ public class SqlQueryHistorySelfTest extends 
GridCommonAbstractTest {
             .queryHistoryMetrics().values();
 
         assertFalse(F.isEmpty(historyCol));
+        assertEquals(1, historyCol.size());
 
         QueryHistory history = first(historyCol);
 
@@ -253,6 +255,39 @@ public class SqlQueryHistorySelfTest extends 
GridCommonAbstractTest {
         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.
      *
@@ -669,6 +704,18 @@ public class SqlQueryHistorySelfTest extends 
GridCommonAbstractTest {
         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;
+        }
     }
 
     /**

Reply via email to