Skip to content

Commit f39619d

Browse files
committed
[ML] Don't write timing stats on no-op (elastic#43680)
Similar to elastic/ml-cpp#512, if a job opens and closes and does nothing in between we shouldn't write timing stats to the results index.
1 parent df4b30f commit f39619d

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/TimingStatsReporter.java

+8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ public void reportBucketProcessingTime(long bucketProcessingTimeMs) {
4242
}
4343
}
4444

45+
public void finishReporting() {
46+
// Don't flush if current timing stats are identical to the persisted ones
47+
if (currentTimingStats.equals(persistedTimingStats)) {
48+
return;
49+
}
50+
flush();
51+
}
52+
4553
public void flush() {
4654
persistedTimingStats = new TimingStats(currentTimingStats);
4755
bulkResultsPersister.persistTimingStats(persistedTimingStats);

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/output/AutodetectResultProcessor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public void process() {
134134

135135
try {
136136
if (processKilled == false) {
137-
timingStatsReporter.flush();
137+
timingStatsReporter.finishReporting();
138138
bulkResultsPersister.executeRequest();
139139
}
140140
} catch (Exception e) {

x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/TimingStatsReporterTests.java

+19
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import static org.hamcrest.Matchers.is;
1515
import static org.mockito.Mockito.inOrder;
1616
import static org.mockito.Mockito.mock;
17+
import static org.mockito.Mockito.verify;
1718
import static org.mockito.Mockito.verifyZeroInteractions;
1819

1920
public class TimingStatsReporterTests extends ESTestCase {
@@ -76,6 +77,24 @@ public void testFlush() {
7677
inOrder.verifyNoMoreInteractions();
7778
}
7879

80+
public void testFinishReportingNoChange() {
81+
TimingStatsReporter reporter = new TimingStatsReporter(new TimingStats(JOB_ID), bulkResultsPersister);
82+
83+
reporter.finishReporting();
84+
85+
verifyZeroInteractions(bulkResultsPersister);
86+
}
87+
88+
public void testFinishReportingWithChange() {
89+
TimingStatsReporter reporter = new TimingStatsReporter(new TimingStats(JOB_ID), bulkResultsPersister);
90+
91+
reporter.reportBucketProcessingTime(10);
92+
93+
reporter.finishReporting();
94+
95+
verify(bulkResultsPersister).persistTimingStats(new TimingStats(JOB_ID, 1, 10.0, 10.0, 10.0, 10.0));
96+
}
97+
7998
public void testTimingStatsDifferSignificantly() {
8099
assertThat(
81100
TimingStatsReporter.differSignificantly(

x-pack/plugin/src/test/resources/rest-api-spec/test/ml/index_layout.yml

+8-8
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ setup:
124124
Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
125125
count:
126126
index: .ml-anomalies-shared
127-
- match: {count: 8}
127+
- match: {count: 6}
128128

129129
- do:
130130
headers:
@@ -138,7 +138,7 @@ setup:
138138
term:
139139
job_id: index-layout-job
140140

141-
- match: {count: 4}
141+
- match: {count: 3}
142142

143143
- do:
144144
headers:
@@ -152,7 +152,7 @@ setup:
152152
term:
153153
job_id: index-layout-job
154154

155-
- match: {count: 4}
155+
- match: {count: 3}
156156

157157
- do:
158158
headers:
@@ -166,7 +166,7 @@ setup:
166166
term:
167167
job_id: index-layout-job2
168168

169-
- match: {count: 4}
169+
- match: {count: 3}
170170

171171
- do:
172172
headers:
@@ -179,7 +179,7 @@ setup:
179179
filter:
180180
term:
181181
job_id: index-layout-job2
182-
- match: {count: 4}
182+
- match: {count: 3}
183183

184184
- do:
185185
headers:
@@ -236,7 +236,7 @@ setup:
236236
Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
237237
count:
238238
index: .ml-anomalies-shared
239-
- match: {count: 4}
239+
- match: {count: 3}
240240

241241

242242
- do:
@@ -251,7 +251,7 @@ setup:
251251
term:
252252
job_id: index-layout-job2
253253

254-
- match: {count: 4}
254+
- match: {count: 3}
255255

256256
- do:
257257
headers:
@@ -265,7 +265,7 @@ setup:
265265
term:
266266
job_id: index-layout-job2
267267

268-
- match: {count: 4}
268+
- match: {count: 3}
269269

270270
- do:
271271
ml.delete_job:

0 commit comments

Comments
 (0)