@@ -187,6 +187,16 @@ private void undeployModels(
187187 mlUndeployModelNodesRequest .setTenantId (tenantId );
188188
189189 client .execute (MLUndeployModelAction .INSTANCE , mlUndeployModelNodesRequest , ActionListener .wrap (response -> {
190+ /*
191+ * The method TransportUndeployModelsAction.processUndeployModelResponseAndUpdate(...) performs
192+ * undeploy action of models by removing the models from the nodes cache and updating the index when it's able to find it.
193+ *
194+ * The problem becomes when the models index is incorrect and no node(s) are servicing the model. This results in
195+ * `{}` responses (on undeploy action), with no update to the model index thus, causing incorrect model state status.
196+ *
197+ * Having this change enables a check that this edge case occurs along with having access to the model id
198+ * allowing us to update the stale model index correctly to `UNDEPLOYED` since no nodes service the model.
199+ */
190200 if (response .getNodes ().isEmpty ()) {
191201 bulkSetModelIndexToUndeploy (modelIds , listener , response );
192202 return ;
@@ -203,14 +213,14 @@ private void bulkSetModelIndexToUndeploy(
203213 BulkRequest bulkUpdateRequest = new BulkRequest ();
204214 for (String modelId : modelIds ) {
205215 UpdateRequest updateRequest = new UpdateRequest ();
206- Instant now = Instant . now ();
216+
207217 ImmutableMap .Builder <String , Object > builder = ImmutableMap .builder ();
208218 builder .put (MLModel .MODEL_STATE_FIELD , MLModelState .UNDEPLOYED .name ());
209219
210220 builder .put (MLModel .PLANNING_WORKER_NODES_FIELD , List .of ());
211221 builder .put (MLModel .PLANNING_WORKER_NODE_COUNT_FIELD , 0 );
212222
213- builder .put (MLModel .LAST_UPDATED_TIME_FIELD , now .toEpochMilli ());
223+ builder .put (MLModel .LAST_UPDATED_TIME_FIELD , Instant . now () .toEpochMilli ());
214224 builder .put (MLModel .CURRENT_WORKER_NODE_COUNT_FIELD , 0 );
215225 updateRequest .index (ML_MODEL_INDEX ).id (modelId ).doc (builder .build ());
216226 bulkUpdateRequest .add (updateRequest );
0 commit comments