From c9ee794ffffbc21619ecc60a1936216b1e4dfc6f Mon Sep 17 00:00:00 2001 From: Andrew Purtell Date: Sun, 22 Nov 2026 11:43:55 -0800 Subject: [PATCH] PHOENIX-8102 PhoenixMetricsIT reads task times before the pool thread records them The client thread pool records the task execution and end-to-end times in afterExecute(). ThreadPoolExecutor calls afterExecute() after the task future completes. The client can therefore get the scan result and read the metrics before the pool thread records them. Under CPU load the woken client thread can preempt the pool thread, and the checks then find no sample. Wait, with a bounded poll, until the pool threads record the task times. Then make the existing checks. Co-Authored-By: Claude Opus 5.5 --- .../phoenix/monitoring/PhoenixMetricsIT.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/phoenix-core/src/it/java/org/apache/phoenix/monitoring/PhoenixMetricsIT.java b/phoenix-core/src/it/java/org/apache/phoenix/monitoring/PhoenixMetricsIT.java index 4a457174f01..33999bc3b29 100644 --- a/phoenix-core/src/it/java/org/apache/phoenix/monitoring/PhoenixMetricsIT.java +++ b/phoenix-core/src/it/java/org/apache/phoenix/monitoring/PhoenixMetricsIT.java @@ -209,6 +209,11 @@ public void testGlobalPhoenixMetricsForQueries() throws Exception { assertTrue(GLOBAL_SCAN_BYTES.getMetric().getValue() > 0); assertTrue(GLOBAL_QUERY_TIME.getMetric().getValue() > 0); + // The pool thread records the task times after it gives the scan result to the client. + // Wait until it records them. + getUtility().waitFor(10000, 10, + () -> GLOBAL_TASK_END_TO_END_TIME.getMetric().getNumberOfSamples() > 0 + && GLOBAL_TASK_EXECUTION_TIME.getMetric().getNumberOfSamples() > 0); assertTrue(GLOBAL_TASK_END_TO_END_TIME.getMetric().getValue() > 0); assertTrue(GLOBAL_TASK_EXECUTION_TIME.getMetric().getValue() > 0); @@ -1079,6 +1084,21 @@ private void changeInternalStateForTesting(PhoenixResultSet rs) throws NoSuchFie private void assertReadMetricValuesForSelectSql(ArrayList numRows, ArrayList numExpectedTasks, PhoenixResultSet resultSetBeingTested, Set expectedTableNames) throws SQLException { + // The pool threads record the task times after they give the scan results to the client. + // Wait until each task records its time. The checks below then find any wrong value. + getUtility().waitFor(10000, 10, () -> { + for (Map m : PhoenixRuntime.getRequestReadMetricInfo(resultSetBeingTested) + .values()) { + Long numTasks = m.get(TASK_EXECUTED_COUNTER); + Long taskTime = m.get(TASK_EXECUTION_TIME); + if ( + numTasks == null || taskTime == null || taskTime < numTasks * TASK_EXECUTION_TIME_DELTA + ) { + return false; + } + } + return true; + }); Map> metrics = PhoenixRuntime.getRequestReadMetricInfo(resultSetBeingTested); int counter = 0;