|
49 | 49 | import org.elasticsearch.xpack.ml.dataframe.stats.ProgressTracker; |
50 | 50 | import org.elasticsearch.xpack.ml.dataframe.stats.StatsHolder; |
51 | 51 | import org.elasticsearch.xpack.ml.notifications.DataFrameAnalyticsAuditor; |
| 52 | +import org.elasticsearch.xpack.ml.utils.persistence.MlParserUtils; |
52 | 53 |
|
53 | 54 | import java.util.List; |
54 | 55 | import java.util.Map; |
@@ -309,17 +310,31 @@ void persistProgress(Client client, String jobId, Runnable runnable) { |
309 | 310 | ActionListener<SearchResponse> searchFormerProgressDocListener = ActionListener.wrap( |
310 | 311 | searchResponse -> { |
311 | 312 | String indexOrAlias = AnomalyDetectorsIndex.jobStateIndexWriteAlias(); |
| 313 | + StoredProgress previous = null; |
312 | 314 | if (searchResponse.getHits().getHits().length > 0) { |
313 | 315 | indexOrAlias = searchResponse.getHits().getHits()[0].getIndex(); |
| 316 | + try { |
| 317 | + previous = MlParserUtils.parse(searchResponse.getHits().getHits()[0], StoredProgress.PARSER); |
| 318 | + } catch (Exception ex) { |
| 319 | + LOGGER.warn(new ParameterizedMessage("[{}] failed to parse previously stored progress", jobId), ex); |
| 320 | + } |
314 | 321 | } |
| 322 | + |
| 323 | + List<PhaseProgress> progress = statsHolder.getProgressTracker().report(); |
| 324 | + final StoredProgress progressToStore = new StoredProgress(progress); |
| 325 | + if (progressToStore.equals(previous)) { |
| 326 | + LOGGER.debug("[{}] new progress is the same as previously persisted progress. Skipping storage.", jobId); |
| 327 | + runnable.run(); |
| 328 | + return; |
| 329 | + } |
| 330 | + |
315 | 331 | IndexRequest indexRequest = new IndexRequest(indexOrAlias) |
316 | 332 | .id(progressDocId) |
317 | 333 | .setRequireAlias(AnomalyDetectorsIndex.jobStateIndexWriteAlias().equals(indexOrAlias)) |
318 | 334 | .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); |
319 | | - List<PhaseProgress> progress = statsHolder.getProgressTracker().report(); |
320 | 335 | try (XContentBuilder jsonBuilder = JsonXContent.contentBuilder()) { |
321 | 336 | LOGGER.debug("[{}] Persisting progress is: {}", jobId, progress); |
322 | | - new StoredProgress(progress).toXContent(jsonBuilder, Payload.XContent.EMPTY_PARAMS); |
| 337 | + progressToStore.toXContent(jsonBuilder, Payload.XContent.EMPTY_PARAMS); |
323 | 338 | indexRequest.source(jsonBuilder); |
324 | 339 | } |
325 | 340 | executeAsyncWithOrigin(client, ML_ORIGIN, IndexAction.INSTANCE, indexRequest, indexProgressDocListener); |
|
0 commit comments