[ML] More informative logging for condition that prevents job creation (elastic/x-pack-elasticsearch#1811)

Original commit: elastic/x-pack-elasticsearch@fb223d3406
This commit is contained in:
David Kyle 2017-06-22 09:46:19 +01:00
parent 5dfccb0d64
commit 2eb939842d
2 changed files with 42 additions and 12 deletions

View File

@ -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<Boolean> 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<MultiSearchResponse> searchResponseActionListener = new ActionListener<MultiSearchResponse>() {
@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<SearchHit> 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

View File

@ -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() {