From 99a374f6cb64c8194a7586592e60d7c167eb70b2 Mon Sep 17 00:00:00 2001 From: Anastasia Iakimova <94782753+Nastya828@users.noreply.github.com> Date: Thu, 28 Sep 2023 22:17:21 +0300 Subject: [PATCH] IGNITE-19880 Fixed negative duration in the SQL query system view (#10958) --- .../query/running/QueryHistoryTracker.java | 3 ++- .../query/running/RunningQueryManager.java | 6 +++--- .../query/SqlSystemViewsSelfTest.java | 21 +++++++++++++++++++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/running/QueryHistoryTracker.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/running/QueryHistoryTracker.java index 33b986590a3f1c..6a2650f48a456e 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/running/QueryHistoryTracker.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/running/QueryHistoryTracker.java @@ -22,6 +22,7 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import org.apache.ignite.configuration.SqlConfiguration; +import org.apache.ignite.internal.util.typedef.internal.U; import org.jsr166.ConcurrentLinkedDeque8; import org.jsr166.ConcurrentLinkedDeque8.Node; @@ -58,7 +59,7 @@ void collectHistory(GridRunningQueryInfo runningQryInfo, boolean failed) { String schema = runningQryInfo.schemaName(); boolean loc = runningQryInfo.local(); long startTime = runningQryInfo.startTime(); - long duration = System.currentTimeMillis() - startTime; + long duration = U.currentTimeMillis() - startTime; QueryHistory hist = new QueryHistory(qry, schema, loc, startTime, duration, failed); diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/running/RunningQueryManager.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/running/RunningQueryManager.java index 50617b288e3a8b..52f8cc87f67c52 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/running/RunningQueryManager.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/running/RunningQueryManager.java @@ -270,7 +270,7 @@ public long register(String qry, GridCacheQueryType qryType, String schemaName, qry, qryType, schemaName, - System.currentTimeMillis(), + U.currentTimeMillis(), ctx.performanceStatistics().enabled() ? System.nanoTime() : 0, cancel, loc, @@ -411,7 +411,7 @@ public void unregister(long qryId, @Nullable Throwable failReason) { qry.queryType(), qry.schemaName(), qry.startTime(), - System.currentTimeMillis(), + U.currentTimeMillis(), qry.local(), qry.enforceJoinOrder(), qry.lazy(), @@ -523,7 +523,7 @@ private boolean isSqlQuery(GridRunningQueryInfo runningQryInfo) { public Collection runningQueries(long duration) { Collection res = new ArrayList<>(); - long curTime = System.currentTimeMillis(); + long curTime = U.currentTimeMillis(); for (GridRunningQueryInfo runningQryInfo : runs.values()) { if (curTime - runningQryInfo.startTime() > duration) diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/SqlSystemViewsSelfTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/SqlSystemViewsSelfTest.java index 1bbb15ac9358e8..62ef13a69a29fa 100644 --- a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/SqlSystemViewsSelfTest.java +++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/SqlSystemViewsSelfTest.java @@ -84,6 +84,8 @@ import org.apache.ignite.lang.IgnitePredicate; import org.apache.ignite.lang.IgniteRunnable; import org.apache.ignite.spi.discovery.tcp.internal.TcpDiscoveryNode; +import org.apache.ignite.spi.systemview.view.SqlQueryView; +import org.apache.ignite.spi.systemview.view.SystemView; import org.apache.ignite.spi.systemview.view.sql.SqlTableView; import org.apache.ignite.testframework.GridTestUtils; import org.junit.Assert; @@ -93,6 +95,7 @@ import static java.util.stream.Collectors.toSet; import static org.apache.ignite.events.EventType.EVT_CONSISTENCY_VIOLATION; import static org.apache.ignite.internal.processors.cache.persistence.metastorage.MetaStorage.METASTORAGE_CACHE_NAME; +import static org.apache.ignite.internal.processors.query.running.RunningQueryManager.SQL_QRY_VIEW; import static org.apache.ignite.internal.util.IgniteUtils.MB; import static org.apache.ignite.testframework.GridTestUtils.waitForCondition; import static org.junit.Assert.assertNotEquals; @@ -668,6 +671,24 @@ public void testRunningQueriesView() throws Exception { assertTrue(cache.query(new SqlFieldsQuery(sql)).getAll().isEmpty()); } + /** + * Test running queries duration in system view. + */ + @Test + public void testRunningQueriesViewDuration() throws Exception { + IgniteEx ignite = startGrid(0); + + SqlFieldsQuery sql = new SqlFieldsQuery("SELECT * FROM (VALUES (1),(2))").setPageSize(1); + + for (int i = 0; i < 5; i++) { + ignite.context().query().querySqlFields(sql, true).iterator().hasNext(); + + SystemView view = ignite.context().systemView().view(SQL_QRY_VIEW); + + view.forEach(v -> assertTrue(v.duration() >= 0)); + } + } + /** * Test that we can't use cache tables and system views in the same query. */