[ML] Create calendars with job groups (elastic/x-pack-elasticsearch#4308)
Original commit: elastic/x-pack-elasticsearch@dc42dccb1f
This commit is contained in:
parent
6bd5e9ef91
commit
11a6cd18ac
|
@ -60,10 +60,6 @@ public class TransportPutCalendarAction extends HandledTransportAction<PutCalend
|
||||||
protected void doExecute(PutCalendarAction.Request request, ActionListener<PutCalendarAction.Response> listener) {
|
protected void doExecute(PutCalendarAction.Request request, ActionListener<PutCalendarAction.Response> listener) {
|
||||||
Calendar calendar = request.getCalendar();
|
Calendar calendar = request.getCalendar();
|
||||||
|
|
||||||
if (checkJobsOrGroupsExist(calendar.getJobIds(), listener::onFailure) == false) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
IndexRequest indexRequest = new IndexRequest(MlMetaIndex.INDEX_NAME, MlMetaIndex.TYPE, calendar.documentId());
|
IndexRequest indexRequest = new IndexRequest(MlMetaIndex.INDEX_NAME, MlMetaIndex.TYPE, calendar.documentId());
|
||||||
try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
|
try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
|
||||||
indexRequest.source(calendar.toXContent(builder,
|
indexRequest.source(calendar.toXContent(builder,
|
||||||
|
@ -95,17 +91,4 @@ public class TransportPutCalendarAction extends HandledTransportAction<PutCalend
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean checkJobsOrGroupsExist(List<String> jobIds, Consumer<Exception> errorHandler) {
|
|
||||||
ClusterState state = clusterService.state();
|
|
||||||
MlMetadata mlMetadata = state.getMetaData().custom(MLMetadataField.TYPE);
|
|
||||||
for (String jobId: jobIds) {
|
|
||||||
if (mlMetadata.isGroupOrJob(jobId) == false) {
|
|
||||||
errorHandler.accept(ExceptionsHelper.missingJobException(jobId));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,8 +23,9 @@ import org.elasticsearch.xpack.core.ml.action.UpdateCalendarJobAction;
|
||||||
import org.elasticsearch.xpack.ml.job.JobManager;
|
import org.elasticsearch.xpack.ml.job.JobManager;
|
||||||
import org.elasticsearch.xpack.ml.job.persistence.JobProvider;
|
import org.elasticsearch.xpack.ml.job.persistence.JobProvider;
|
||||||
|
|
||||||
import java.util.HashSet;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class TransportUpdateCalendarJobAction extends HandledTransportAction<UpdateCalendarJobAction.Request, PutCalendarAction.Response> {
|
public class TransportUpdateCalendarJobAction extends HandledTransportAction<UpdateCalendarJobAction.Request, PutCalendarAction.Response> {
|
||||||
|
|
||||||
|
@ -47,17 +48,17 @@ public class TransportUpdateCalendarJobAction extends HandledTransportAction<Upd
|
||||||
@Override
|
@Override
|
||||||
protected void doExecute(UpdateCalendarJobAction.Request request, ActionListener<PutCalendarAction.Response> listener) {
|
protected void doExecute(UpdateCalendarJobAction.Request request, ActionListener<PutCalendarAction.Response> listener) {
|
||||||
ClusterState clusterState = clusterService.state();
|
ClusterState clusterState = clusterService.state();
|
||||||
MlMetadata mlMetadata = clusterState.getMetaData().custom(MLMetadataField.TYPE);
|
MlMetadata maybeNullMetaData = clusterState.getMetaData().custom(MLMetadataField.TYPE);
|
||||||
if (mlMetadata == null) {
|
final MlMetadata mlMetadata = maybeNullMetaData == null ? MlMetadata.EMPTY_METADATA : maybeNullMetaData;
|
||||||
mlMetadata = MlMetadata.EMPTY_METADATA;
|
|
||||||
}
|
|
||||||
|
|
||||||
Set<String> jobIdsToAdd = Strings.tokenizeByCommaToSet(request.getJobIdsToAddExpression());
|
Set<String> jobIdsToAdd = Strings.tokenizeByCommaToSet(request.getJobIdsToAddExpression());
|
||||||
Set<String> jobIdsToRemove = Strings.tokenizeByCommaToSet(request.getJobIdsToRemoveExpression());
|
Set<String> jobIdsToRemove = Strings.tokenizeByCommaToSet(request.getJobIdsToRemoveExpression());
|
||||||
|
|
||||||
jobProvider.updateCalendar(request.getCalendarId(), jobIdsToAdd, jobIdsToRemove, mlMetadata,
|
jobProvider.updateCalendar(request.getCalendarId(), jobIdsToAdd, jobIdsToRemove, mlMetadata,
|
||||||
c -> {
|
c -> {
|
||||||
jobManager.updateProcessOnCalendarChanged(c.getJobIds());
|
List<String> existingJobsOrGroups =
|
||||||
|
c.getJobIds().stream().filter(mlMetadata::isGroupOrJob).collect(Collectors.toList());
|
||||||
|
jobManager.updateProcessOnCalendarChanged(existingJobsOrGroups);
|
||||||
listener.onResponse(new PutCalendarAction.Response(c));
|
listener.onResponse(new PutCalendarAction.Response(c));
|
||||||
}, listener::onFailure);
|
}, listener::onFailure);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1105,13 +1105,6 @@ public class JobProvider {
|
||||||
calendar -> {
|
calendar -> {
|
||||||
Set<String> currentJobs = new HashSet<>(calendar.getJobIds());
|
Set<String> currentJobs = new HashSet<>(calendar.getJobIds());
|
||||||
|
|
||||||
for (String jobToAdd : jobIdsToAdd) {
|
|
||||||
if (mlMetadata.isGroupOrJob(jobToAdd) == false) {
|
|
||||||
errorHandler.accept(ExceptionsHelper.missingJobException(jobToAdd));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (String jobToRemove : jobIdsToRemove) {
|
for (String jobToRemove : jobIdsToRemove) {
|
||||||
if (currentJobs.contains(jobToRemove) == false) {
|
if (currentJobs.contains(jobToRemove) == false) {
|
||||||
errorHandler.accept(ExceptionsHelper.badRequestException("Cannot remove [" + jobToRemove
|
errorHandler.accept(ExceptionsHelper.badRequestException("Cannot remove [" + jobToRemove
|
||||||
|
|
|
@ -87,14 +87,22 @@
|
||||||
calendar_id: "dogs_of_the_year"
|
calendar_id: "dogs_of_the_year"
|
||||||
|
|
||||||
- do:
|
- do:
|
||||||
catch: missing
|
|
||||||
xpack.ml.put_calendar:
|
xpack.ml.put_calendar:
|
||||||
calendar_id: "new_cal_with_unknown_job"
|
calendar_id: "new_cal_with_unknown_job_group"
|
||||||
body: >
|
body: >
|
||||||
{
|
{
|
||||||
"job_ids": ["cal-job", "unknown-job"]
|
"job_ids": ["cal-job", "unknown-job-group"]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- do:
|
||||||
|
xpack.ml.get_calendars:
|
||||||
|
calendar_id: "new_cal_with_unknown_job_group"
|
||||||
|
- match: { count: 1 }
|
||||||
|
- match:
|
||||||
|
calendars.0:
|
||||||
|
calendar_id: "new_cal_with_unknown_job_group"
|
||||||
|
job_ids: ["cal-job", "unknown-job-group"]
|
||||||
|
|
||||||
---
|
---
|
||||||
"Test get calendar given missing":
|
"Test get calendar given missing":
|
||||||
- do:
|
- do:
|
||||||
|
@ -266,12 +274,6 @@
|
||||||
- match: { calendars.0.calendar_id: "wildlife" }
|
- match: { calendars.0.calendar_id: "wildlife" }
|
||||||
- length: { calendars.0.job_ids: 0 }
|
- length: { calendars.0.job_ids: 0 }
|
||||||
|
|
||||||
- do:
|
|
||||||
catch: missing
|
|
||||||
xpack.ml.put_calendar_job:
|
|
||||||
calendar_id: "wildlife"
|
|
||||||
job_id: "missing_job"
|
|
||||||
|
|
||||||
- do:
|
- do:
|
||||||
catch: /Cannot remove \[missing_job\] as it is not present in calendar \[wildlife\]/
|
catch: /Cannot remove \[missing_job\] as it is not present in calendar \[wildlife\]/
|
||||||
xpack.ml.delete_calendar_job:
|
xpack.ml.delete_calendar_job:
|
||||||
|
|
Loading…
Reference in New Issue