10
10
import org .apache .lucene .search .join .ScoreMode ;
11
11
import org .elasticsearch .ElasticsearchException ;
12
12
import org .elasticsearch .ElasticsearchParseException ;
13
+ import org .elasticsearch .Version ;
13
14
import org .elasticsearch .action .ActionListener ;
14
15
import org .elasticsearch .action .DocWriteRequest ;
15
16
import org .elasticsearch .action .DocWriteResponse ;
24
25
import org .elasticsearch .action .get .MultiGetResponse ;
25
26
import org .elasticsearch .action .index .IndexAction ;
26
27
import org .elasticsearch .action .index .IndexRequest ;
28
+ import org .elasticsearch .action .index .IndexRequestBuilder ;
27
29
import org .elasticsearch .action .index .IndexResponse ;
28
30
import org .elasticsearch .action .search .SearchRequest ;
29
31
import org .elasticsearch .action .search .SearchResponse ;
@@ -282,9 +284,12 @@ public void onFailure(Exception e) {
282
284
* @param maxModelMemoryLimit The maximum model memory allowed. This can be {@code null}
283
285
* if the job's {@link org.elasticsearch.xpack.core.ml.job.config.AnalysisLimits}
284
286
* are not changed.
287
+ * @param minClusterNodeVersion the minimum version of nodes in the cluster
285
288
* @param updatedJobListener Updated job listener
286
289
*/
287
- public void updateJob (String jobId , JobUpdate update , ByteSizeValue maxModelMemoryLimit , ActionListener <Job > updatedJobListener ) {
290
+ public void updateJob (String jobId , JobUpdate update , ByteSizeValue maxModelMemoryLimit ,
291
+ Version minClusterNodeVersion ,
292
+ ActionListener <Job > updatedJobListener ) {
288
293
GetRequest getRequest = new GetRequest (AnomalyDetectorsIndex .configIndexName (),
289
294
ElasticsearchMappings .DOC_TYPE , Job .documentId (jobId ));
290
295
@@ -296,7 +301,9 @@ public void onResponse(GetResponse getResponse) {
296
301
return ;
297
302
}
298
303
299
- long version = getResponse .getVersion ();
304
+ final long version = getResponse .getVersion ();
305
+ final long seqNo = getResponse .getSeqNo ();
306
+ final long primaryTerm = getResponse .getPrimaryTerm ();
300
307
BytesReference source = getResponse .getSourceAsBytesRef ();
301
308
Job .Builder jobBuilder ;
302
309
try {
@@ -316,7 +323,7 @@ public void onResponse(GetResponse getResponse) {
316
323
return ;
317
324
}
318
325
319
- indexUpdatedJob (updatedJob , version , updatedJobListener );
326
+ indexUpdatedJob (updatedJob , version , seqNo , primaryTerm , minClusterNodeVersion , updatedJobListener );
320
327
}
321
328
322
329
@ Override
@@ -341,17 +348,18 @@ public interface UpdateValidator {
341
348
}
342
349
343
350
/**
344
- * Similar to {@link #updateJob(String, JobUpdate, ByteSizeValue, ActionListener)} but
351
+ * Similar to {@link #updateJob(String, JobUpdate, ByteSizeValue, Version, ActionListener)} but
345
352
* with an extra validation step which is called before the updated is applied.
346
353
*
347
354
* @param jobId The Id of the job to update
348
355
* @param update The job update
349
356
* @param maxModelMemoryLimit The maximum model memory allowed
350
357
* @param validator The job update validator
358
+ * @param minClusterNodeVersion the minimum version of a node ifn the cluster
351
359
* @param updatedJobListener Updated job listener
352
360
*/
353
361
public void updateJobWithValidation (String jobId , JobUpdate update , ByteSizeValue maxModelMemoryLimit ,
354
- UpdateValidator validator , ActionListener <Job > updatedJobListener ) {
362
+ UpdateValidator validator , Version minClusterNodeVersion , ActionListener <Job > updatedJobListener ) {
355
363
GetRequest getRequest = new GetRequest (AnomalyDetectorsIndex .configIndexName (),
356
364
ElasticsearchMappings .DOC_TYPE , Job .documentId (jobId ));
357
365
@@ -363,7 +371,9 @@ public void onResponse(GetResponse getResponse) {
363
371
return ;
364
372
}
365
373
366
- long version = getResponse .getVersion ();
374
+ final long version = getResponse .getVersion ();
375
+ final long seqNo = getResponse .getSeqNo ();
376
+ final long primaryTerm = getResponse .getPrimaryTerm ();
367
377
BytesReference source = getResponse .getSourceAsBytesRef ();
368
378
Job originalJob ;
369
379
try {
@@ -385,7 +395,7 @@ public void onResponse(GetResponse getResponse) {
385
395
return ;
386
396
}
387
397
388
- indexUpdatedJob (updatedJob , version , updatedJobListener );
398
+ indexUpdatedJob (updatedJob , version , seqNo , primaryTerm , minClusterNodeVersion , updatedJobListener );
389
399
},
390
400
updatedJobListener ::onFailure
391
401
));
@@ -402,17 +412,22 @@ public void onFailure(Exception e) {
402
412
});
403
413
}
404
414
405
- private void indexUpdatedJob (Job updatedJob , long version , ActionListener <Job > updatedJobListener ) {
415
+ private void indexUpdatedJob (Job updatedJob , long version , long seqNo , long primaryTerm , Version minClusterNodeVersion ,
416
+ ActionListener <Job > updatedJobListener ) {
406
417
try (XContentBuilder builder = XContentFactory .jsonBuilder ()) {
407
418
XContentBuilder updatedSource = updatedJob .toXContent (builder , ToXContent .EMPTY_PARAMS );
408
- IndexRequest indexRequest = client .prepareIndex (AnomalyDetectorsIndex .configIndexName (),
419
+ IndexRequestBuilder indexRequest = client .prepareIndex (AnomalyDetectorsIndex .configIndexName (),
409
420
ElasticsearchMappings .DOC_TYPE , Job .documentId (updatedJob .getId ()))
410
421
.setSource (updatedSource )
411
- .setVersion (version )
412
- .setRefreshPolicy (WriteRequest .RefreshPolicy .IMMEDIATE )
413
- .request ();
422
+ .setRefreshPolicy (WriteRequest .RefreshPolicy .IMMEDIATE );
423
+ if (minClusterNodeVersion .onOrAfter (Version .V_6_7_0 )) {
424
+ indexRequest .setIfSeqNo (seqNo );
425
+ indexRequest .setIfPrimaryTerm (primaryTerm );
426
+ } else {
427
+ indexRequest .setVersion (version );
428
+ }
414
429
415
- executeAsyncWithOrigin (client , ML_ORIGIN , IndexAction .INSTANCE , indexRequest , ActionListener .wrap (
430
+ executeAsyncWithOrigin (client , ML_ORIGIN , IndexAction .INSTANCE , indexRequest . request () , ActionListener .wrap (
416
431
indexResponse -> {
417
432
assert indexResponse .getResult () == DocWriteResponse .Result .UPDATED ;
418
433
updatedJobListener .onResponse (updatedJob );
0 commit comments