Skip to content

Commit 83eaf5d

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

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
@@ -16,6 +16,7 @@
1616
import org.opensearch.OpenSearchStatusException;
1717
import org.opensearch.action.ActionRequest;
1818
import org.opensearch.action.bulk.BulkRequest;
19+
import org.opensearch.action.bulk.BulkResponse;
1920
import org.opensearch.action.search.SearchRequest;
2021
import org.opensearch.action.search.SearchResponse;
2122
import org.opensearch.action.support.ActionFilters;
@@ -229,13 +230,22 @@ private void bulkSetModelIndexToUndeploy(
229230
bulkUpdateRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
230231
log.info("No nodes service: {}", Arrays.toString(modelIds));
231232

232-
client.bulk(bulkUpdateRequest, ActionListener.wrap(br -> {
233-
log.debug("Successfully set modelIds to UNDEPLOY in index");
234-
listener.onResponse(new MLUndeployModelsResponse(response));
235-
}, e -> {
236-
log.error("Failed to set modelIds to UNDEPLOY in index", e);
233+
try (ThreadContext.StoredContext threadContext = client.threadPool().getThreadContext().stashContext()) {
234+
ActionListener<MLUndeployModelsResponse> listenerWithContextRestoration = ActionListener.runBefore(listener, () -> threadContext.restore());
235+
ActionListener<BulkResponse> bulkResponseListener = ActionListener.wrap(br -> {
236+
log.debug("Successfully set modelIds to UNDEPLOY in index");
237+
listenerWithContextRestoration.onResponse(new MLUndeployModelsResponse(response));
238+
}, e -> {
239+
log.error("Failed to set modelIds to UNDEPLOY in index", e);
240+
listenerWithContextRestoration.onFailure(e);
241+
});
242+
243+
client.bulk(bulkUpdateRequest, bulkResponseListener);
244+
} catch (Exception e) {
245+
log.error("Unexpected error while setting modelIds to UNDEPLOY status to index", e);
237246
listener.onFailure(e);
238-
}));
247+
}
248+
239249
}
240250

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

0 commit comments

Comments
 (0)