From 81c54a8269faa7729153ef5496579753a8e980dd Mon Sep 17 00:00:00 2001 From: Yang Wang Date: Fri, 9 May 2025 17:03:12 +1000 Subject: [PATCH] Inline TimedRunnable.beforeExecute No need to call the method separately as it can be invoked the first time getQueueTimeNanos is called. Relates: #120488 --- .../TaskExecutionTimeTrackingEsThreadPoolExecutor.java | 1 - .../common/util/concurrent/TimedRunnable.java | 10 +--------- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/common/util/concurrent/TaskExecutionTimeTrackingEsThreadPoolExecutor.java b/server/src/main/java/org/elasticsearch/common/util/concurrent/TaskExecutionTimeTrackingEsThreadPoolExecutor.java index 2b1a5ff6e9c0c..048c87446484a 100644 --- a/server/src/main/java/org/elasticsearch/common/util/concurrent/TaskExecutionTimeTrackingEsThreadPoolExecutor.java +++ b/server/src/main/java/org/elasticsearch/common/util/concurrent/TaskExecutionTimeTrackingEsThreadPoolExecutor.java @@ -163,7 +163,6 @@ protected void beforeExecute(Thread t, Runnable r) { } assert super.unwrap(r) instanceof TimedRunnable : "expected only TimedRunnables in queue"; final TimedRunnable timedRunnable = (TimedRunnable) super.unwrap(r); - timedRunnable.beforeExecute(); final long taskQueueLatency = timedRunnable.getQueueTimeNanos(); assert taskQueueLatency >= 0; queueLatencyMillisHistogram.addObservation(TimeUnit.NANOSECONDS.toMillis(taskQueueLatency)); diff --git a/server/src/main/java/org/elasticsearch/common/util/concurrent/TimedRunnable.java b/server/src/main/java/org/elasticsearch/common/util/concurrent/TimedRunnable.java index de89ad0d8ea3f..9494b926fa802 100644 --- a/server/src/main/java/org/elasticsearch/common/util/concurrent/TimedRunnable.java +++ b/server/src/main/java/org/elasticsearch/common/util/concurrent/TimedRunnable.java @@ -66,8 +66,7 @@ public boolean isForceExecution() { */ long getQueueTimeNanos() { if (beforeExecuteTime == -1) { - assert false : "beforeExecute must be called before getQueueTimeNanos"; - return -1; + beforeExecuteTime = System.nanoTime(); } return beforeExecuteTime - creationTimeNanos; } @@ -84,13 +83,6 @@ long getTotalExecutionNanos() { return Math.max(finishTimeNanos - startTimeNanos, 1); } - /** - * Called when the task has reached the front of the queue and is about to be executed - */ - public void beforeExecute() { - beforeExecuteTime = System.nanoTime(); - } - /** * If the task was failed or rejected, return true. * Otherwise, false.