18
18
*/
19
19
package org .elasticsearch .index .replication ;
20
20
21
- import org .apache .lucene .document .Field ;
22
21
import org .apache .lucene .index .IndexWriter ;
23
22
import org .apache .lucene .index .IndexableField ;
24
23
import org .apache .lucene .index .Term ;
58
57
import org .hamcrest .Matcher ;
59
58
60
59
import java .io .IOException ;
61
- import java .nio .charset .StandardCharsets ;
62
60
import java .util .ArrayList ;
63
61
import java .util .Collections ;
64
62
import java .util .List ;
@@ -418,10 +416,8 @@ public void testReplicaOperationWithConcurrentPrimaryPromotion() throws Exceptio
418
416
*/
419
417
public void testDocumentFailureReplication () throws Exception {
420
418
final IOException indexException = new IOException ("simulated indexing failure" );
421
- final IOException deleteException = new IOException ("simulated deleting failure" );
422
419
final EngineFactory engineFactory = config -> InternalEngineTests .createInternalEngine ((dir , iwc ) ->
423
420
new IndexWriter (dir , iwc ) {
424
- final AtomicBoolean throwAfterIndexedOneDoc = new AtomicBoolean (); // need one document to trigger delete in IW.
425
421
@ Override
426
422
public long addDocument (Iterable <? extends IndexableField > doc ) throws IOException {
427
423
boolean isTombstone = false ;
@@ -430,20 +426,12 @@ public long addDocument(Iterable<? extends IndexableField> doc) throws IOExcepti
430
426
isTombstone = true ;
431
427
}
432
428
}
433
- if (isTombstone == false && throwAfterIndexedOneDoc . getAndSet ( true ) ) {
434
- throw indexException ;
429
+ if (isTombstone ) {
430
+ return super . addDocument ( doc ); // allow to add Noop
435
431
} else {
436
- return super . addDocument ( doc ) ;
432
+ throw indexException ;
437
433
}
438
434
}
439
- @ Override
440
- public long deleteDocuments (Term ... terms ) throws IOException {
441
- throw deleteException ;
442
- }
443
- @ Override
444
- public long softUpdateDocument (Term term , Iterable <? extends IndexableField > doc , Field ...fields ) throws IOException {
445
- throw deleteException ; // a delete uses softUpdateDocument API if soft-deletes enabled
446
- }
447
435
}, null , null , config );
448
436
try (ReplicationGroup shards = new ReplicationGroup (buildIndexMetaData (0 )) {
449
437
@ Override
@@ -454,20 +442,13 @@ public long softUpdateDocument(Term term, Iterable<? extends IndexableField> doc
454
442
long primaryTerm = shards .getPrimary ().getPendingPrimaryTerm ();
455
443
List <Translog .Operation > expectedTranslogOps = new ArrayList <>();
456
444
BulkItemResponse indexResp = shards .index (new IndexRequest (index .getName (), "type" , "1" ).source ("{}" , XContentType .JSON ));
457
- assertThat (indexResp .isFailed (), equalTo (false ));
458
- expectedTranslogOps .add (new Translog .Index ("type" , "1" , 0 , primaryTerm , 1 , "{}" .getBytes (StandardCharsets .UTF_8 ), null , -1 ));
445
+ assertThat (indexResp .isFailed (), equalTo (true ));
446
+ assertThat (indexResp .getFailure ().getCause (), equalTo (indexException ));
447
+ expectedTranslogOps .add (new Translog .NoOp (0 , primaryTerm , indexException .toString ()));
459
448
try (Translog .Snapshot snapshot = getTranslog (shards .getPrimary ()).newSnapshot ()) {
460
449
assertThat (snapshot , SnapshotMatchers .containsOperationsInAnyOrder (expectedTranslogOps ));
461
450
}
462
-
463
- indexResp = shards .index (new IndexRequest (index .getName (), "type" , "any" ).source ("{}" , XContentType .JSON ));
464
- assertThat (indexResp .getFailure ().getCause (), equalTo (indexException ));
465
- expectedTranslogOps .add (new Translog .NoOp (1 , primaryTerm , indexException .toString ()));
466
-
467
- BulkItemResponse deleteResp = shards .delete (new DeleteRequest (index .getName (), "type" , "1" ));
468
- assertThat (deleteResp .getFailure ().getCause (), equalTo (deleteException ));
469
- expectedTranslogOps .add (new Translog .NoOp (2 , primaryTerm , deleteException .toString ()));
470
- shards .assertAllEqual (1 );
451
+ shards .assertAllEqual (0 );
471
452
472
453
int nReplica = randomIntBetween (1 , 3 );
473
454
for (int i = 0 ; i < nReplica ; i ++) {
@@ -482,14 +463,10 @@ public long softUpdateDocument(Term term, Iterable<? extends IndexableField> doc
482
463
assertThat (snapshot , SnapshotMatchers .containsOperationsInAnyOrder (expectedTranslogOps ));
483
464
}
484
465
}
485
- // unlike previous failures, these two failures replicated directly from the replication channel.
466
+ // the failure replicated directly from the replication channel.
486
467
indexResp = shards .index (new IndexRequest (index .getName (), "type" , "any" ).source ("{}" , XContentType .JSON ));
487
468
assertThat (indexResp .getFailure ().getCause (), equalTo (indexException ));
488
- expectedTranslogOps .add (new Translog .NoOp (3 , primaryTerm , indexException .toString ()));
489
-
490
- deleteResp = shards .delete (new DeleteRequest (index .getName (), "type" , "1" ));
491
- assertThat (deleteResp .getFailure ().getCause (), equalTo (deleteException ));
492
- expectedTranslogOps .add (new Translog .NoOp (4 , primaryTerm , deleteException .toString ()));
469
+ expectedTranslogOps .add (new Translog .NoOp (1 , primaryTerm , indexException .toString ()));
493
470
494
471
for (IndexShard shard : shards ) {
495
472
try (Translog .Snapshot snapshot = getTranslog (shard ).newSnapshot ()) {
@@ -499,7 +476,7 @@ public long softUpdateDocument(Term term, Iterable<? extends IndexableField> doc
499
476
assertThat (snapshot , SnapshotMatchers .containsOperationsInAnyOrder (expectedTranslogOps ));
500
477
}
501
478
}
502
- shards .assertAllEqual (1 );
479
+ shards .assertAllEqual (0 );
503
480
}
504
481
}
505
482
0 commit comments