Skip to content

Commit b647482

Browse files
committed
Fix NPE when date_buckets aggregation is missing in the response
1 parent 2a322ae commit b647482

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/delayeddatacheck/DatafeedDelayedDataDetector.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
*/
77
package org.elasticsearch.xpack.ml.datafeed.delayeddatacheck;
88

9+
import org.apache.logging.log4j.LogManager;
10+
import org.apache.logging.log4j.Logger;
911
import org.elasticsearch.action.search.SearchRequest;
1012
import org.elasticsearch.action.search.SearchResponse;
1113
import org.elasticsearch.action.search.TransportSearchAction;
@@ -39,6 +41,8 @@
3941
*/
4042
public class DatafeedDelayedDataDetector implements DelayedDataDetector {
4143

44+
private static final Logger logger = LogManager.getLogger(DatafeedDelayedDataDetector.class);
45+
4246
private static final String DATE_BUCKETS = "date_buckets";
4347

4448
private final long bucketSpan;
@@ -134,9 +138,19 @@ private Map<Long, Long> checkCurrentBucketEventCount(long start, long end) {
134138

135139
SearchRequest searchRequest = new SearchRequest(datafeedIndices).source(searchSourceBuilder).indicesOptions(indicesOptions);
136140
try (ThreadContext.StoredContext ignore = client.threadPool().getThreadContext().stashWithOrigin(ML_ORIGIN)) {
137-
SearchResponse response = client.execute(TransportSearchAction.TYPE, searchRequest).actionGet();
141+
SearchResponse searchResponse = client.execute(TransportSearchAction.TYPE, searchRequest).actionGet();
138142
try {
139-
List<? extends Histogram.Bucket> buckets = ((Histogram) response.getAggregations().get(DATE_BUCKETS)).getBuckets();
143+
Histogram histogram = searchResponse.getAggregations().get(DATE_BUCKETS);
144+
if (histogram == null) {
145+
logger.debug(
146+
"Missing [{}] aggregation in the search response, request = [{}], response = [{}]",
147+
DATE_BUCKETS,
148+
searchRequest,
149+
searchResponse
150+
);
151+
return Collections.emptyMap();
152+
}
153+
List<? extends Histogram.Bucket> buckets = histogram.getBuckets();
140154
Map<Long, Long> hashMap = Maps.newMapWithExpectedSize(buckets.size());
141155
for (Histogram.Bucket bucket : buckets) {
142156
long bucketTime = toHistogramKeyToEpoch(bucket.getKey());
@@ -147,7 +161,7 @@ private Map<Long, Long> checkCurrentBucketEventCount(long start, long end) {
147161
}
148162
return hashMap;
149163
} finally {
150-
response.decRef();
164+
searchResponse.decRef();
151165
}
152166
}
153167
}

0 commit comments

Comments
 (0)