Skip to content

Commit

Permalink
[Feature/multi_tenancy] Delay context restoring for initializing mast…
Browse files Browse the repository at this point in the history
…er key and deleting agent (#2866)

* Delay context restoring for initializing master key and deleting agent

Signed-off-by: Daniel Widdis <[email protected]>

* Simplify restoring context with runBefore

Signed-off-by: Daniel Widdis <[email protected]>

---------

Signed-off-by: Daniel Widdis <[email protected]>
  • Loading branch information
dbwiddis authored Sep 23, 2024
1 parent d59fced commit 8d9786b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,14 @@ private void handleGetDataObjectResponse(
AtomicReference<Exception> exceptionRef,
CountDownLatch latch
) {
context.restore();
log.debug("Completed Get MASTER_KEY Request, for tenant id:{}", tenantId);

if (throwable != null) {
handleGetDataObjectFailure(throwable, exceptionRef, latch);
} else {
handleGetDataObjectSuccess(response, tenantId, exceptionRef, latch, context);
}
context.restore();
}

private void handleGetDataObjectFailure(Throwable throwable, AtomicReference<Exception> exceptionRef, CountDownLatch latch) {
Expand Down
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) -> {
context.restore();
log.debug("Completed Get Agent Request, Agent id:{}", agentId);
if (throwable != null) {
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,9 +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) {
actionListener
wrappedListener
.onFailure(
new OpenSearchStatusException(
"User doesn't have privilege to perform this operation on this agent",
Expand All @@ -144,30 +148,25 @@ protected void doExecute(Task task, ActionRequest request, ActionListener<Delete
.build(),
client.threadPool().executor(GENERAL_THREAD_POOL)
)
.whenComplete(
(response, delThrowable) -> handleDeleteResponse(
response,
delThrowable,
tenantId,
actionListener
)
);
.whenComplete((response, delThrowable) -> {
handleDeleteResponse(response, delThrowable, tenantId, wrappedListener);
});
} catch (Exception e) {
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 8d9786b

Please sign in to comment.