4040import org .opensearch .ml .common .transport .deploy .MLDeployModelRequest ;
4141import org .opensearch .ml .common .transport .undeploy .MLUndeployModelAction ;
4242import org .opensearch .ml .common .transport .undeploy .MLUndeployModelNodesRequest ;
43+ import org .opensearch .ml .common .transport .undeploy .MLUndeployModelNodesResponse ;
4344import org .opensearch .ml .common .transport .undeploy .MLUndeployModelsAction ;
4445import org .opensearch .ml .common .transport .undeploy .MLUndeployModelsRequest ;
4546import org .opensearch .ml .common .transport .undeploy .MLUndeployModelsResponse ;
@@ -162,15 +163,20 @@ protected void doExecute(Task task, ActionRequest request, ActionListener<MLUnde
162163 private void undeployModels (String [] targetNodeIds , String [] modelIds , ActionListener <MLUndeployModelsResponse > listener ) {
163164 MLUndeployModelNodesRequest mlUndeployModelNodesRequest = new MLUndeployModelNodesRequest (targetNodeIds , modelIds );
164165
165- client .execute (MLUndeployModelAction .INSTANCE , mlUndeployModelNodesRequest , ActionListener .wrap (r -> {
166- if (r .getNodes ().isEmpty ()) {
167- bulkSetModelIndexToUndeploy (modelIds );
166+ client .execute (MLUndeployModelAction .INSTANCE , mlUndeployModelNodesRequest , ActionListener .wrap (response -> {
167+ if (response .getNodes ().isEmpty ()) {
168+ bulkSetModelIndexToUndeploy (modelIds , listener , response );
169+ return ;
168170 }
169- listener .onResponse (new MLUndeployModelsResponse (r ));
171+ listener .onResponse (new MLUndeployModelsResponse (response ));
170172 }, listener ::onFailure ));
171173 }
172174
173- private void bulkSetModelIndexToUndeploy (String [] modelIds ) {
175+ private void bulkSetModelIndexToUndeploy (
176+ String [] modelIds ,
177+ ActionListener <MLUndeployModelsResponse > listener ,
178+ MLUndeployModelNodesResponse response
179+ ) {
174180 BulkRequest bulkUpdateRequest = new BulkRequest ();
175181 for (String modelId : modelIds ) {
176182 UpdateRequest updateRequest = new UpdateRequest ();
@@ -186,10 +192,16 @@ private void bulkSetModelIndexToUndeploy(String[] modelIds) {
186192 updateRequest .index (ML_MODEL_INDEX ).id (modelId ).doc (builder .build ());
187193 bulkUpdateRequest .add (updateRequest );
188194 }
195+
189196 bulkUpdateRequest .setRefreshPolicy (WriteRequest .RefreshPolicy .IMMEDIATE );
190- log .info ("No models service: {}" , modelIds .toString ());
191- client .bulk (bulkUpdateRequest , ActionListener .wrap (br -> { log .debug ("Successfully set modelIds to UNDEPLOY in index" ); }, e -> {
197+ log .info ("No nodes service: {}" , modelIds .toString ());
198+
199+ client .bulk (bulkUpdateRequest , ActionListener .wrap (br -> {
200+ log .debug ("Successfully set modelIds to UNDEPLOY in index" );
201+ listener .onResponse (new MLUndeployModelsResponse (response ));
202+ }, e -> {
192203 log .error ("Failed to set modelIds to UNDEPLOY in index" , e );
204+ listener .onFailure (e );
193205 }));
194206 }
195207
0 commit comments