Skip to content

Commit b1f891b

Browse files
committed
Check new job’s groups are not job Ids
1 parent 748706a commit b1f891b

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/JobManager.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,15 @@ public void putJob(PutJobAction.Request request, AnalysisRegistry analysisRegist
348348
return;
349349
}
350350

351+
// and that the new job's groups are not job Ids
352+
for (String group : job.getGroups()) {
353+
if (currentMlMetadata.getJobs().containsKey(group)) {
354+
actionListener.onFailure(new
355+
ResourceAlreadyExistsException(Messages.getMessage(Messages.JOB_AND_GROUP_NAMES_MUST_BE_UNIQUE, group)));
356+
return;
357+
}
358+
}
359+
351360
ActionListener<Boolean> putJobListener = new ActionListener<Boolean>() {
352361
@Override
353362
public void onResponse(Boolean indicesCreated) {

x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/JobManagerTests.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,6 @@ public void testPutJob_ThrowsIfIdIsTheSameAsAGroup() throws IOException {
653653
MockClientBuilder mockClientBuilder = new MockClientBuilder("jobmanager-test");
654654
JobManager jobManager = createJobManager(mockClientBuilder.build());
655655

656-
PutJobAction.Request putJobRequest = new PutJobAction.Request(createJobFoo());
657656

658657
MlMetadata.Builder mlMetadata = new MlMetadata.Builder();
659658
Job.Builder jobBuilder = buildJobBuilder("job-with-group-foo");
@@ -662,6 +661,8 @@ public void testPutJob_ThrowsIfIdIsTheSameAsAGroup() throws IOException {
662661
ClusterState clusterState = ClusterState.builder(new ClusterName("name"))
663662
.metaData(MetaData.builder().putCustom(MlMetadata.TYPE, mlMetadata.build())).build();
664663

664+
// job id cannot be a group
665+
PutJobAction.Request putJobRequest = new PutJobAction.Request(createJobFoo());
665666
jobManager.putJob(putJobRequest, analysisRegistry, clusterState, new ActionListener<PutJobAction.Response>() {
666667
@Override
667668
public void onResponse(PutJobAction.Response response) {
@@ -674,6 +675,26 @@ public void onFailure(Exception e) {
674675
assertEquals("job and group names must be unique but job [foo] and group [foo] have the same name", e.getMessage());
675676
}
676677
});
678+
679+
// the job's groups cannot be job Ids
680+
jobBuilder = buildJobBuilder("job-with-clashing-group-name");
681+
jobBuilder.setCreateTime(null);
682+
jobBuilder.setGroups(Collections.singletonList("job-with-group-foo"));
683+
putJobRequest = new PutJobAction.Request(jobBuilder);
684+
685+
jobManager.putJob(putJobRequest, analysisRegistry, clusterState, new ActionListener<PutJobAction.Response>() {
686+
@Override
687+
public void onResponse(PutJobAction.Response response) {
688+
fail("should have got an error");
689+
}
690+
691+
@Override
692+
public void onFailure(Exception e) {
693+
assertTrue(e instanceof ResourceAlreadyExistsException);
694+
assertEquals("job and group names must be unique but job [job-with-group-foo] and " +
695+
"group [job-with-group-foo] have the same name", e.getMessage());
696+
}
697+
});
677698
}
678699

679700
public void testNotifyFilterChangedGivenNoop() {

0 commit comments

Comments
 (0)