From 08f9ec1cae50a052e2ccaaf03664028f17360b33 Mon Sep 17 00:00:00 2001 From: Akshat Jain Date: Mon, 5 Aug 2024 16:52:56 +0530 Subject: [PATCH] Memoize the redundant calls to overlord in sql statements endpoint (#16839) --- .../sql/resources/SqlStatementResource.java | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/extensions-core/multi-stage-query/src/main/java/org/apache/druid/msq/sql/resources/SqlStatementResource.java b/extensions-core/multi-stage-query/src/main/java/org/apache/druid/msq/sql/resources/SqlStatementResource.java index 322727aea92d..f56622804786 100644 --- a/extensions-core/multi-stage-query/src/main/java/org/apache/druid/msq/sql/resources/SqlStatementResource.java +++ b/extensions-core/multi-stage-query/src/main/java/org/apache/druid/msq/sql/resources/SqlStatementResource.java @@ -113,7 +113,6 @@ import java.util.Map; import java.util.Objects; import java.util.Optional; -import java.util.function.Supplier; import java.util.stream.Collectors; @@ -588,19 +587,19 @@ private Optional getStatementStatus( MSQControllerTask msqControllerTask = getMSQControllerTaskAndCheckPermission(queryId, authenticationResult, forAction); SqlStatementState sqlStatementState = SqlStatementResourceHelper.getSqlStatementState(statusPlus); - Supplier> msqTaskReportPayloadSupplier = () -> { + MSQTaskReportPayload taskReportPayload = null; + if (detail || SqlStatementState.FAILED == sqlStatementState) { try { - return Optional.ofNullable(SqlStatementResourceHelper.getPayload( + taskReportPayload = SqlStatementResourceHelper.getPayload( contactOverlord(overlordClient.taskReportAsMap(queryId), queryId) - )); + ); } catch (DruidException e) { - if (e.getErrorCode().equals("notFound") || e.getMessage().contains("Unable to contact overlord")) { - return Optional.empty(); + if (!e.getErrorCode().equals("notFound") && !e.getMessage().contains("Unable to contact overlord")) { + throw e; } - throw e; } - }; + } if (SqlStatementState.FAILED == sqlStatementState) { return SqlStatementResourceHelper.getExceptionPayload( @@ -608,7 +607,7 @@ private Optional getStatementStatus( taskResponse, statusPlus, sqlStatementState, - msqTaskReportPayloadSupplier.get().orElse(null), + taskReportPayload, jsonMapper, detail ); @@ -627,9 +626,9 @@ private Optional getStatementStatus( msqControllerTask.getQuerySpec().getDestination() ).orElse(null) : null, null, - detail ? SqlStatementResourceHelper.getQueryStagesReport(msqTaskReportPayloadSupplier.get().orElse(null)) : null, - detail ? SqlStatementResourceHelper.getQueryCounters(msqTaskReportPayloadSupplier.get().orElse(null)) : null, - detail ? SqlStatementResourceHelper.getQueryWarningDetails(msqTaskReportPayloadSupplier.get().orElse(null)) : null + SqlStatementResourceHelper.getQueryStagesReport(taskReportPayload), + SqlStatementResourceHelper.getQueryCounters(taskReportPayload), + SqlStatementResourceHelper.getQueryWarningDetails(taskReportPayload) )); } }