@@ -164,6 +164,16 @@ private void undeployModels(String[] targetNodeIds, String[] modelIds, ActionLis
164164 MLUndeployModelNodesRequest mlUndeployModelNodesRequest = new MLUndeployModelNodesRequest (targetNodeIds , modelIds );
165165
166166 client .execute (MLUndeployModelAction .INSTANCE , mlUndeployModelNodesRequest , ActionListener .wrap (response -> {
167+ /*
168+ * The method TransportUndeployModelsAction.processUndeployModelResponseAndUpdate(...) performs
169+ * undeploy action of models by removing the models from the nodes cache and updating the index when it's able to find it.
170+ *
171+ * The problem becomes when the models index is incorrect and no node(s) are servicing the model. This results in
172+ * `{}` responses (on undeploy action), with no update to the model index thus, causing incorrect model state status.
173+ *
174+ * Having this change enables a check that this edge case occurs along with having access to the model id
175+ * allowing us to update the stale model index correctly to `UNDEPLOYED` since no nodes service the model.
176+ */
167177 if (response .getNodes ().isEmpty ()) {
168178 bulkSetModelIndexToUndeploy (modelIds , listener , response );
169179 return ;
@@ -180,14 +190,14 @@ private void bulkSetModelIndexToUndeploy(
180190 BulkRequest bulkUpdateRequest = new BulkRequest ();
181191 for (String modelId : modelIds ) {
182192 UpdateRequest updateRequest = new UpdateRequest ();
183- Instant now = Instant . now ();
193+
184194 ImmutableMap .Builder <String , Object > builder = ImmutableMap .builder ();
185195 builder .put (MLModel .MODEL_STATE_FIELD , MLModelState .UNDEPLOYED .name ());
186196
187197 builder .put (MLModel .PLANNING_WORKER_NODES_FIELD , List .of ());
188198 builder .put (MLModel .PLANNING_WORKER_NODE_COUNT_FIELD , 0 );
189199
190- builder .put (MLModel .LAST_UPDATED_TIME_FIELD , now .toEpochMilli ());
200+ builder .put (MLModel .LAST_UPDATED_TIME_FIELD , Instant . now () .toEpochMilli ());
191201 builder .put (MLModel .CURRENT_WORKER_NODE_COUNT_FIELD , 0 );
192202 updateRequest .index (ML_MODEL_INDEX ).id (modelId ).doc (builder .build ());
193203 bulkUpdateRequest .add (updateRequest );
0 commit comments