From 2eb939842d0a8685eb1283ac2aec9649436b3352 Mon Sep 17 00:00:00 2001 From: David Kyle Date: Thu, 22 Jun 2017 09:46:19 +0100 Subject: [PATCH] [ML] More informative logging for condition that prevents job creation (elastic/x-pack-elasticsearch#1811) Original commit: elastic/x-pack-elasticsearch@fb223d3406d5204f4b4f35f0c531f17bbd6d7f93 --- .../xpack/ml/job/persistence/JobProvider.java | 42 ++++++++++++++----- .../autodetect/state/CategorizerState.java | 12 +++++- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobProvider.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobProvider.java index 2178e94243b..50d3e44dcad 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobProvider.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobProvider.java @@ -84,6 +84,7 @@ import java.util.Objects; import java.util.function.BiFunction; import java.util.function.Consumer; import java.util.function.Supplier; +import java.util.stream.Collectors; public class JobProvider { private static final Logger LOGGER = Loggers.getLogger(JobProvider.class); @@ -121,7 +122,6 @@ public class JobProvider { */ public void checkForLeftOverDocuments(Job job, ActionListener listener) { - String resultsIndexName = job.getResultsIndexName(); SearchRequestBuilder stateDocSearch = client.prepareSearch(AnomalyDetectorsIndex.jobStateIndexName()) .setQuery(QueryBuilders.idsQuery().addIds(CategorizerState.documentId(job.getId(), 1), CategorizerState.v54DocumentId(job.getId(), 1))) @@ -131,6 +131,7 @@ public class JobProvider { .setQuery(QueryBuilders.idsQuery().addIds(Quantiles.documentId(job.getId()), Quantiles.v54DocumentId(job.getId()))) .setIndicesOptions(IndicesOptions.lenientExpandOpen()); + String resultsIndexName = job.getResultsIndexName(); SearchRequestBuilder resultDocSearch = client.prepareSearch(resultsIndexName) .setIndicesOptions(IndicesOptions.lenientExpandOpen()) .setQuery(QueryBuilders.termQuery(Job.ID.getPreferredName(), job.getId())) @@ -140,16 +141,37 @@ public class JobProvider { ActionListener searchResponseActionListener = new ActionListener() { @Override public void onResponse(MultiSearchResponse searchResponse) { - for (MultiSearchResponse.Item itemResponse : searchResponse.getResponses()) { - if (itemResponse.getResponse().getHits().getTotalHits() > 0) { - listener.onFailure(ExceptionsHelper.conflictStatusException( - "Result and/or state documents exist for a prior job with Id [" + job.getId() + "]. " + - "Please create the job with a different Id")); - return; - } - } + List searchHits = Arrays.stream(searchResponse.getResponses()) + .flatMap(item -> Arrays.stream(item.getResponse().getHits().getHits())) + .collect(Collectors.toList()); - listener.onResponse(true); + if (searchHits.isEmpty() == false) { + int quantileDocCount = 0; + int categorizerStateDocCount = 0; + int resultDocCount = 0; + for (SearchHit hit : searchHits) { + if (hit.getId().equals(Quantiles.documentId(job.getId())) || + hit.getId().equals(Quantiles.v54DocumentId(job.getId()))) { + quantileDocCount++; + } else if (hit.getId().startsWith(CategorizerState.documentPrefix(job.getId())) || + hit.getId().startsWith(CategorizerState.v54DocumentPrefix(job.getId()))) { + categorizerStateDocCount++; + } else { + resultDocCount++; + } + } + + LOGGER.warn("{} result, {} quantile state and {} categorizer state documents exist for a prior job with Id [{}]", + resultDocCount, quantileDocCount, categorizerStateDocCount, job.getId()); + + listener.onFailure(ExceptionsHelper.conflictStatusException( + "[" + resultDocCount + "] result and [" + (quantileDocCount + categorizerStateDocCount) + + "] state documents exist for a prior job with Id [" + job.getId() + "]. " + + "Please create the job with a different Id")); + return; + } else { + listener.onResponse(true); + } } @Override diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/state/CategorizerState.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/state/CategorizerState.java index 750a95c62d6..f896badc567 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/state/CategorizerState.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/state/CategorizerState.java @@ -19,14 +19,22 @@ public class CategorizerState { public static final String TYPE = "categorizer_state"; public static final String documentId(String jobId, int docNum) { - return jobId + "_" + TYPE + "#" + docNum; + return documentPrefix(jobId) + docNum; + } + + public static final String documentPrefix(String jobId) { + return jobId + "_" + TYPE + "#"; } /** * This is how the IDs were formed in v5.4 */ public static final String v54DocumentId(String jobId, int docNum) { - return jobId + "#" + docNum; + return v54DocumentPrefix(jobId) + docNum; + } + + public static final String v54DocumentPrefix(String jobId) { + return jobId + "#"; } private CategorizerState() {