From 2c3e40a6aefd7bd9cdc1ab8f46133b99a2093e0d Mon Sep 17 00:00:00 2001 From: Siddhant Deshmukh Date: Thu, 18 Jul 2024 14:45:44 -0700 Subject: [PATCH] Make singleton Signed-off-by: Siddhant Deshmukh --- .../action/search/TransportSearchAction.java | 2 +- .../tasks/SearchTaskRequestOperationsListener.java | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/org/opensearch/action/search/TransportSearchAction.java b/server/src/main/java/org/opensearch/action/search/TransportSearchAction.java index d5cfcce0fd793..ceab2343091f7 100644 --- a/server/src/main/java/org/opensearch/action/search/TransportSearchAction.java +++ b/server/src/main/java/org/opensearch/action/search/TransportSearchAction.java @@ -435,7 +435,7 @@ private void executeRequest( originalSearchRequest, logger, TraceableSearchRequestOperationsListener.create(tracer, requestSpan), - new SearchTaskRequestOperationsListener(taskResourceTrackingService) + SearchTaskRequestOperationsListener.getInstance(taskResourceTrackingService) ); SearchRequestContext searchRequestContext = new SearchRequestContext( requestOperationsListeners, diff --git a/server/src/main/java/org/opensearch/tasks/SearchTaskRequestOperationsListener.java b/server/src/main/java/org/opensearch/tasks/SearchTaskRequestOperationsListener.java index 7c336fb1c7210..de46ed6a28581 100644 --- a/server/src/main/java/org/opensearch/tasks/SearchTaskRequestOperationsListener.java +++ b/server/src/main/java/org/opensearch/tasks/SearchTaskRequestOperationsListener.java @@ -19,12 +19,20 @@ * */ public final class SearchTaskRequestOperationsListener extends SearchRequestOperationsListener { + private static SearchTaskRequestOperationsListener instance; private final TaskResourceTrackingService taskResourceTrackingService; - public SearchTaskRequestOperationsListener(TaskResourceTrackingService taskResourceTrackingService) { + private SearchTaskRequestOperationsListener(TaskResourceTrackingService taskResourceTrackingService) { this.taskResourceTrackingService = taskResourceTrackingService; } + public static synchronized SearchTaskRequestOperationsListener getInstance(TaskResourceTrackingService taskResourceTrackingService) { + if (instance == null) { + instance = new SearchTaskRequestOperationsListener(taskResourceTrackingService); + } + return instance; + } + @Override public void onRequestEnd(SearchPhaseContext context, SearchRequestContext searchRequestContext) { taskResourceTrackingService.refreshResourceStats(context.getTask());