Skip to content

Commit 6323086

Browse files
committed
add context stash/restore to write operation
Signed-off-by: Brian Flores <[email protected]>
1 parent fc8ded6 commit 6323086

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

plugin/src/main/java/org/opensearch/ml/action/undeploy/TransportUndeployModelsAction.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.opensearch.OpenSearchStatusException;
1616
import org.opensearch.action.ActionRequest;
1717
import org.opensearch.action.bulk.BulkRequest;
18+
import org.opensearch.action.bulk.BulkResponse;
1819
import org.opensearch.action.search.SearchRequest;
1920
import org.opensearch.action.search.SearchResponse;
2021
import org.opensearch.action.support.ActionFilters;
@@ -206,13 +207,22 @@ private void bulkSetModelIndexToUndeploy(
206207
bulkUpdateRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
207208
log.info("No nodes service: {}", Arrays.toString(modelIds));
208209

209-
client.bulk(bulkUpdateRequest, ActionListener.wrap(br -> {
210-
log.debug("Successfully set modelIds to UNDEPLOY in index");
211-
listener.onResponse(new MLUndeployModelsResponse(response));
212-
}, e -> {
213-
log.error("Failed to set modelIds to UNDEPLOY in index", e);
210+
try (ThreadContext.StoredContext threadContext = client.threadPool().getThreadContext().stashContext()) {
211+
ActionListener<MLUndeployModelsResponse> listenerWithContextRestoration = ActionListener.runBefore(listener, () -> threadContext.restore());
212+
ActionListener<BulkResponse> bulkResponseListener = ActionListener.wrap(br -> {
213+
log.debug("Successfully set modelIds to UNDEPLOY in index");
214+
listenerWithContextRestoration.onResponse(new MLUndeployModelsResponse(response));
215+
}, e -> {
216+
log.error("Failed to set modelIds to UNDEPLOY in index", e);
217+
listenerWithContextRestoration.onFailure(e);
218+
});
219+
220+
client.bulk(bulkUpdateRequest, bulkResponseListener);
221+
} catch (Exception e) {
222+
log.error("Unexpected error while setting modelIds to UNDEPLOY status to index", e);
214223
listener.onFailure(e);
215-
}));
224+
}
225+
216226
}
217227

218228
private void validateAccess(String modelId, ActionListener<Boolean> listener) {

0 commit comments

Comments
 (0)