Skip to content

Commit

Permalink
Simplify restoring context with runBefore
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Widdis <[email protected]>
  • Loading branch information
dbwiddis committed Sep 17, 2024
1 parent 4f5510a commit 8351004
Showing 1 changed file with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,19 @@ protected void doExecute(Task task, ActionRequest request, ActionListener<Delete
.build();

try (ThreadContext.StoredContext context = client.threadPool().getThreadContext().stashContext()) {

ActionListener<DeleteResponse> wrappedListener = ActionListener.runBefore(actionListener, context::restore);
sdkClient
.getDataObjectAsync(getDataObjectRequest, client.threadPool().executor(GENERAL_THREAD_POOL))
.whenComplete((r, throwable) -> {
log.debug("Completed Get Agent Request, Agent id:{}", agentId);
if (throwable != null) {
context.restore();
Exception cause = SdkClientUtils.unwrapAndConvertToException(throwable);
if (cause instanceof IndexNotFoundException) {
log.info("Failed to get Agent index", cause);
actionListener.onFailure(new OpenSearchStatusException("Failed to get agent index", RestStatus.NOT_FOUND));
wrappedListener.onFailure(new OpenSearchStatusException("Failed to get agent index", RestStatus.NOT_FOUND));
} else {
log.error("Failed to get ML Agent {}", agentId, cause);
actionListener.onFailure(cause);
wrappedListener.onFailure(cause);
}
} else {
try {
Expand All @@ -122,10 +121,14 @@ protected void doExecute(Task task, ActionRequest request, ActionListener<Delete
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
MLAgent mlAgent = MLAgent.parse(parser);
if (TenantAwareHelper
.validateTenantResource(mlFeatureEnabledSetting, tenantId, mlAgent.getTenantId(), actionListener)) {
.validateTenantResource(
mlFeatureEnabledSetting,
tenantId,
mlAgent.getTenantId(),
wrappedListener
)) {
if (mlAgent.getIsHidden() && !isSuperAdmin) {
context.restore();
actionListener
wrappedListener
.onFailure(
new OpenSearchStatusException(
"User doesn't have privilege to perform this operation on this agent",
Expand All @@ -146,26 +149,24 @@ protected void doExecute(Task task, ActionRequest request, ActionListener<Delete
client.threadPool().executor(GENERAL_THREAD_POOL)
)
.whenComplete((response, delThrowable) -> {
context.restore();
handleDeleteResponse(response, delThrowable, tenantId, actionListener);
handleDeleteResponse(response, delThrowable, tenantId, wrappedListener);
});
} catch (Exception e) {
context.restore();
log.error("Failed to delete ML agent: {}", agentId, e);
actionListener.onFailure(e);
wrappedListener.onFailure(e);
}
}
}
} catch (Exception e) {
log.error("Failed to parse ml agent {}", agentId, e);
actionListener.onFailure(e);
wrappedListener.onFailure(e);
}
} else {
actionListener.onFailure(new OpenSearchStatusException("Fail to find ml agent", RestStatus.NOT_FOUND));
wrappedListener.onFailure(new OpenSearchStatusException("Fail to find ml agent", RestStatus.NOT_FOUND));
}
} catch (Exception e) {
log.error("Failed to delete ML agent: {}", agentId, e);
actionListener.onFailure(e);
wrappedListener.onFailure(e);
}
}
});
Expand Down

0 comments on commit 8351004

Please sign in to comment.