Skip to content

Commit 457939e

Browse files
authored
Do not throw exceptions resulting from persisting datafeed timing stats. (#49044) (#49051)
1 parent 1cc9878 commit 457939e

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,13 @@ public Integer getMaxEmptySearches() {
117117
}
118118

119119
public void finishReportingTimingStats() {
120-
timingStatsReporter.finishReporting();
120+
try {
121+
timingStatsReporter.finishReporting();
122+
} catch (Exception e) {
123+
// We don't want the exception to propagate out of this method as it can leave the datafeed in the "stopping" state forever.
124+
// Since persisting datafeed timing stats is not critical, we just log a warning here.
125+
LOGGER.warn("[{}] Datafeed timing stats could not be reported due to: {}", jobId, e);
126+
}
121127
}
122128

123129
Long runLookBack(long startTime, Long endTime) throws Exception {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ public void stop(String source, TimeValue timeout, Exception e, boolean autoClos
361361
acquired = datafeedJobLock.tryLock(timeout.millis(), TimeUnit.MILLISECONDS);
362362
} catch (InterruptedException e1) {
363363
Thread.currentThread().interrupt();
364-
} finally {
364+
} finally { // It is crucial that none of the calls this "finally" block makes throws an exception for minor problems.
365365
logger.info("[{}] stopping datafeed [{}] for job [{}], acquired [{}]...", source, datafeedId,
366366
datafeedJob.getJobId(), acquired);
367367
runningDatafeedsOnThisNode.remove(allocationId);

x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedJobTests.java

+11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.elasticsearch.client.Client;
1313
import org.elasticsearch.common.bytes.BytesArray;
1414
import org.elasticsearch.common.settings.Settings;
15+
import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException;
1516
import org.elasticsearch.common.util.concurrent.ThreadContext;
1617
import org.elasticsearch.common.xcontent.ToXContent;
1718
import org.elasticsearch.common.xcontent.XContentBuilder;
@@ -60,6 +61,7 @@
6061
import static org.mockito.Matchers.eq;
6162
import static org.mockito.Matchers.same;
6263
import static org.mockito.Mockito.atMost;
64+
import static org.mockito.Mockito.doThrow;
6365
import static org.mockito.Mockito.mock;
6466
import static org.mockito.Mockito.never;
6567
import static org.mockito.Mockito.times;
@@ -454,6 +456,15 @@ public void testFlushAnalysisProblemIsConflict() {
454456
assertThat(analysisProblemException.shouldStop, is(true));
455457
}
456458

459+
public void testFinishReportingTimingStats() {
460+
doThrow(new EsRejectedExecutionException()).when(timingStatsReporter).finishReporting();
461+
462+
long frequencyMs = 100;
463+
long queryDelayMs = 1000;
464+
DatafeedJob datafeedJob = createDatafeedJob(frequencyMs, queryDelayMs, 1000, -1, randomBoolean());
465+
datafeedJob.finishReportingTimingStats();
466+
}
467+
457468
private DatafeedJob createDatafeedJob(long frequencyMs, long queryDelayMs, long latestFinalBucketEndTimeMs,
458469
long latestRecordTimeMs, boolean haveSeenDataPreviously) {
459470
Supplier<Long> currentTimeSupplier = () -> currentTime;

0 commit comments

Comments
 (0)