[ML] Create calendars with job groups (elastic/x-pack-elasticsearch#4308)

Original commit: elastic/x-pack-elasticsearch@dc42dccb1f
This commit is contained in:
David Kyle 2018-04-09 16:18:01 +01:00 committed by GitHub
parent 6bd5e9ef91
commit 11a6cd18ac
4 changed files with 18 additions and 39 deletions

View File

@ -60,10 +60,6 @@ public class TransportPutCalendarAction extends HandledTransportAction<PutCalend
protected void doExecute(PutCalendarAction.Request request, ActionListener<PutCalendarAction.Response> listener) {
Calendar calendar = request.getCalendar();
if (checkJobsOrGroupsExist(calendar.getJobIds(), listener::onFailure) == false) {
return;
}
IndexRequest indexRequest = new IndexRequest(MlMetaIndex.INDEX_NAME, MlMetaIndex.TYPE, calendar.documentId());
try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
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;
}
}

View File

@ -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.persistence.JobProvider;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class TransportUpdateCalendarJobAction extends HandledTransportAction<UpdateCalendarJobAction.Request, PutCalendarAction.Response> {
@ -47,17 +48,17 @@ public class TransportUpdateCalendarJobAction extends HandledTransportAction<Upd
@Override
protected void doExecute(UpdateCalendarJobAction.Request request, ActionListener<PutCalendarAction.Response> listener) {
ClusterState clusterState = clusterService.state();
MlMetadata mlMetadata = clusterState.getMetaData().custom(MLMetadataField.TYPE);
if (mlMetadata == null) {
mlMetadata = MlMetadata.EMPTY_METADATA;
}
MlMetadata maybeNullMetaData = clusterState.getMetaData().custom(MLMetadataField.TYPE);
final MlMetadata mlMetadata = maybeNullMetaData == null ? MlMetadata.EMPTY_METADATA : maybeNullMetaData;
Set<String> jobIdsToAdd = Strings.tokenizeByCommaToSet(request.getJobIdsToAddExpression());
Set<String> jobIdsToRemove = Strings.tokenizeByCommaToSet(request.getJobIdsToRemoveExpression());
jobProvider.updateCalendar(request.getCalendarId(), jobIdsToAdd, jobIdsToRemove, mlMetadata,
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::onFailure);
}

View File

@ -1105,13 +1105,6 @@ public class JobProvider {
calendar -> {
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) {
if (currentJobs.contains(jobToRemove) == false) {
errorHandler.accept(ExceptionsHelper.badRequestException("Cannot remove [" + jobToRemove

View File

@ -87,14 +87,22 @@
calendar_id: "dogs_of_the_year"
- do:
catch: missing
xpack.ml.put_calendar:
calendar_id: "new_cal_with_unknown_job"
calendar_id: "new_cal_with_unknown_job_group"
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":
- do:
@ -266,12 +274,6 @@
- match: { calendars.0.calendar_id: "wildlife" }
- length: { calendars.0.job_ids: 0 }
- do:
catch: missing
xpack.ml.put_calendar_job:
calendar_id: "wildlife"
job_id: "missing_job"
- do:
catch: /Cannot remove \[missing_job\] as it is not present in calendar \[wildlife\]/
xpack.ml.delete_calendar_job: