Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ML] Change JobManager to work with Job config in index #33064

Merged
merged 5 commits into from
Aug 29, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@
import org.elasticsearch.xpack.ml.MachineLearning;
import org.elasticsearch.xpack.ml.job.categorization.CategorizationAnalyzer;
import org.elasticsearch.xpack.ml.job.persistence.JobConfigProvider;
import org.elasticsearch.xpack.ml.job.persistence.JobResultsProvider;
import org.elasticsearch.xpack.ml.job.persistence.JobResultsPersister;
import org.elasticsearch.xpack.ml.job.persistence.JobResultsProvider;
import org.elasticsearch.xpack.ml.job.process.autodetect.UpdateParams;
import org.elasticsearch.xpack.ml.notifications.Auditor;
import org.elasticsearch.xpack.ml.utils.ChainTaskExecutor;
Expand Down Expand Up @@ -419,26 +419,22 @@ public void notifyFilterChanged(MlFilter filter, Set<String> addedItems, Set<Str
return;
}

Set<String> openJobIds = openJobIds(clusterService.state());
if (openJobIds.isEmpty()) {
updatedListener.onResponse(Boolean.TRUE);
return;
}

String jobsExpression = Strings.collectionToCommaDelimitedString(openJobIds);

// TODO JIndex probably better to query for the filter ID
jobConfigProvider.expandJobs(jobsExpression, false, ActionListener.wrap(
jobConfigProvider.findJobsWithCustomRules(ActionListener.wrap(
jobBuilders -> {
threadPool.executor(MachineLearning.UTILITY_THREAD_POOL_NAME).execute(() -> {
for (Job.Builder builder: jobBuilders) {
// TODO JIndex - sneaky because the job isn't being built here
Set<String> jobFilters = builder.getAnalysisConfig().extractReferencedFilters();
for (Job job: jobBuilders) {
Set<String> jobFilters = job.getAnalysisConfig().extractReferencedFilters();
ClusterState clusterState = clusterService.state();
if (jobFilters.contains(filter.getId())) {
updateJobProcessNotifier.submitJobUpdate(UpdateParams.filterUpdate(builder.getId(), filter),
if (isJobOpen(clusterState, job.getId())) {
updateJobProcessNotifier.submitJobUpdate(UpdateParams.filterUpdate(job.getId(), filter),
ActionListener.wrap(isUpdated -> {
auditFilterChanges(builder.getId(), filter.getId(), addedItems, removedItems);
}, e -> {}));
auditFilterChanges(job.getId(), filter.getId(), addedItems, removedItems);
}, e -> {
}));
} else {
auditFilterChanges(job.getId(), filter.getId(), addedItems, removedItems);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
package org.elasticsearch.xpack.ml.job.persistence;

import org.apache.lucene.search.join.ScoreMode;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.DocWriteRequest;
Expand Down Expand Up @@ -37,12 +38,15 @@
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.elasticsearch.index.query.WildcardQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.elasticsearch.xpack.core.ml.job.config.AnalysisConfig;
import org.elasticsearch.xpack.core.ml.job.config.Detector;
import org.elasticsearch.xpack.core.ml.job.config.Job;
import org.elasticsearch.xpack.core.ml.job.config.JobUpdate;
import org.elasticsearch.xpack.core.ml.job.persistence.AnomalyDetectorsIndex;
Expand All @@ -52,6 +56,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -489,6 +494,40 @@ public void expandGroupIds(List<String> groupIds, ActionListener<Set<String>> l
, client::search);
}

public void findJobsWithCustomRules(ActionListener<List<Job>> listener) {
String customRulesPath = Strings.collectionToDelimitedString(Arrays.asList(Job.ANALYSIS_CONFIG.getPreferredName(),
AnalysisConfig.DETECTORS.getPreferredName(), Detector.CUSTOM_RULES_FIELD.getPreferredName()), ".");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder()
.query(QueryBuilders.nestedQuery(customRulesPath, QueryBuilders.existsQuery(customRulesPath), ScoreMode.None))
.size(10000);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will have to be extracted to a constant and be used by all searches. I think we should also think how to refactor the code that does the job search into something that is reused as a few methods are doing such searches.


SearchRequest searchRequest = client.prepareSearch(AnomalyDetectorsIndex.configIndexName())
.setIndicesOptions(IndicesOptions.lenientExpandOpen())
.setSource(sourceBuilder).request();

executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, searchRequest,
ActionListener.<SearchResponse>wrap(
response -> {
List<Job> jobs = new ArrayList<>();

SearchHit[] hits = response.getHits().getHits();
for (SearchHit hit : hits) {
try {
BytesReference source = hit.getSourceRef();
Job job = parseJobLenientlyFromSource(source).build();
jobs.add(job);
} catch (IOException e) {
// TODO A better way to handle this rather than just ignoring the error?
logger.error("Error parsing anomaly detector job configuration [" + hit.getId() + "]", e);
}
}

listener.onResponse(jobs);
},
listener::onFailure)
, client::search);
}

private void parseJobLenientlyFromSource(BytesReference source, ActionListener<Job.Builder> jobListener) {
try (InputStream stream = source.streamInput();
XContentParser parser = XContentFactory.xContent(XContentType.JSON)
Expand Down