diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/MlMetadata.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/MlMetadata.java index 1005dfb33f7..37e4c35575e 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/MlMetadata.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/MlMetadata.java @@ -276,7 +276,7 @@ public class MlMetadata implements MetaData.Custom { private void checkJobIsAvailableForDatafeed(String jobId) { Job job = jobs.get(jobId); - if (job == null) { + if (job == null || job.isDeleted()) { throw ExceptionsHelper.missingJobException(jobId); } Optional existingDatafeed = getDatafeedByJobId(jobId); diff --git a/plugin/src/test/java/org/elasticsearch/xpack/ml/MlMetadataTests.java b/plugin/src/test/java/org/elasticsearch/xpack/ml/MlMetadataTests.java index e7c1e9591da..cd504f8a277 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/ml/MlMetadataTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/ml/MlMetadataTests.java @@ -203,6 +203,15 @@ public class MlMetadataTests extends AbstractSerializingTestCase { expectThrows(ResourceNotFoundException.class, () -> builder.putDatafeed(datafeedConfig1)); } + public void testPutDatafeed_failBecauseJobIsBeingDeleted() { + Job job1 = createDatafeedJob().setDeleted(true).build(new Date()); + DatafeedConfig datafeedConfig1 = createDatafeedConfig("datafeed1", job1.getId()).build(); + MlMetadata.Builder builder = new MlMetadata.Builder(); + builder.putJob(job1, false); + + expectThrows(ResourceNotFoundException.class, () -> builder.putDatafeed(datafeedConfig1)); + } + public void testPutDatafeed_failBecauseDatafeedIdIsAlreadyTaken() { Job job1 = createDatafeedJob().build(new Date()); DatafeedConfig datafeedConfig1 = createDatafeedConfig("datafeed1", job1.getId()).build();