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] Fix null pointer exception after all datafeed indices deleted #76401

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 @@ -51,7 +51,7 @@
*/
public class ChunkedDataExtractor implements DataExtractor {

private interface DataSummary {
interface DataSummary {
long estimateChunk();
boolean hasData();
long earliestTime();
Expand Down Expand Up @@ -209,11 +209,11 @@ private DataSummary newScrolledDataSummary() {
LOGGER.debug("[{}] Scrolling Data summary response was obtained", context.jobId);
timingStatsReporter.reportSearchDuration(searchResponse.getTook());

Aggregations aggregations = searchResponse.getAggregations();
long earliestTime = 0;
long latestTime = 0;
long totalHits = searchResponse.getHits().getTotalHits().value;
if (totalHits > 0) {
Aggregations aggregations = searchResponse.getAggregations();
Min min = aggregations.get(EARLIEST_TIME);
earliestTime = (long) min.getValue();
Max max = aggregations.get(LATEST_TIME);
Expand All @@ -230,6 +230,11 @@ private DataSummary newAggregatedDataSummary() {
LOGGER.debug("[{}] Aggregating Data summary response was obtained", context.jobId);
timingStatsReporter.reportSearchDuration(searchResponse.getTook());

long totalHits = searchResponse.getHits().getTotalHits().value;
if (totalHits == 0) {
// This can happen if all the indices the datafeed is searching are deleted after it started
return AggregatedDataSummary.noDataSummary(context.histogramInterval);
}
Aggregations aggregations = searchResponse.getAggregations();
Min min = aggregations.get(EARLIEST_TIME);
Max max = aggregations.get(LATEST_TIME);
Expand Down Expand Up @@ -309,13 +314,18 @@ public boolean hasData() {
}
}

private static class AggregatedDataSummary implements DataSummary {
static class AggregatedDataSummary implements DataSummary {

private final double earliestTime;
private final double latestTime;
private final long histogramIntervalMillis;

private AggregatedDataSummary(double earliestTime, double latestTime, long histogramInterval) {
static AggregatedDataSummary noDataSummary(long histogramInterval) {
// hasData() uses infinity to mean no data
return new AggregatedDataSummary(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, histogramInterval);
}

AggregatedDataSummary(double earliestTime, double latestTime, long histogramInterval) {
this.earliestTime = earliestTime;
this.latestTime = latestTime;
this.histogramIntervalMillis = histogramInterval;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public void testExtractionGivenSpecifiedChunk() throws IOException {
public void testExtractionGivenSpecifiedChunkAndAggs() throws IOException {
chunkSpan = TimeValue.timeValueSeconds(1);
TestDataExtractor extractor = new TestDataExtractor(1000L, 2300L, true, 1000L);
extractor.setNextResponse(createSearchResponse(0L, 1000L, 2200L));
extractor.setNextResponse(createSearchResponse(randomLongBetween(2, 10000), 1000L, 2200L));

InputStream inputStream1 = mock(InputStream.class);
InputStream inputStream2 = mock(InputStream.class);
Expand Down Expand Up @@ -200,7 +200,7 @@ public void testExtractionGivenAutoChunkAndAggs() throws IOException {
chunkSpan = null;
TestDataExtractor extractor = new TestDataExtractor(100_000L, 450_000L, true, 200L);

extractor.setNextResponse(createSearchResponse(0L, 100_000L, 400_000L));
extractor.setNextResponse(createSearchResponse(randomLongBetween(2, 10000), 100_000L, 400_000L));

InputStream inputStream1 = mock(InputStream.class);
InputStream inputStream2 = mock(InputStream.class);
Expand Down Expand Up @@ -504,6 +504,11 @@ public void testDataSummaryRequestIsFailed() {
expectThrows(SearchPhaseExecutionException.class, extractor::next);
}

public void testNoDataSummaryHasNoData() {
ChunkedDataExtractor.DataSummary summary = ChunkedDataExtractor.AggregatedDataSummary.noDataSummary(randomNonNegativeLong());
assertFalse(summary.hasData());
}

private SearchResponse createSearchResponse(long totalHits, long earliestTime, long latestTime) {
SearchResponse searchResponse = mock(SearchResponse.class);
when(searchResponse.status()).thenReturn(RestStatus.OK);
Expand Down