3838import org .elasticsearch .action .admin .indices .stats .CommonStats ;
3939import org .elasticsearch .action .admin .indices .stats .CommonStatsFlags ;
4040import org .elasticsearch .action .admin .indices .stats .ShardStats ;
41+ import org .elasticsearch .action .index .IndexRequest ;
4142import org .elasticsearch .action .support .PlainActionFuture ;
4243import org .elasticsearch .cluster .metadata .IndexMetaData ;
4344import org .elasticsearch .cluster .metadata .MappingMetaData ;
7778import org .elasticsearch .index .mapper .Mapping ;
7879import org .elasticsearch .index .mapper .ParseContext ;
7980import org .elasticsearch .index .mapper .ParsedDocument ;
81+ import org .elasticsearch .index .mapper .SourceToParse ;
8082import org .elasticsearch .index .mapper .Uid ;
8183import org .elasticsearch .index .mapper .UidFieldMapper ;
8284import org .elasticsearch .index .snapshots .IndexShardSnapshotStatus ;
123125
124126import static java .util .Collections .emptyMap ;
125127import static java .util .Collections .emptySet ;
128+ import static org .elasticsearch .cluster .routing .TestShardRouting .newShardRouting ;
126129import static org .elasticsearch .common .lucene .Lucene .cleanLuceneIndex ;
127130import static org .elasticsearch .common .xcontent .ToXContent .EMPTY_PARAMS ;
128131import static org .elasticsearch .common .xcontent .XContentFactory .jsonBuilder ;
@@ -282,14 +285,14 @@ public void testOperationLocksOnPrimaryShards() throws InterruptedException, Exe
282285
283286 if (randomBoolean ()) {
284287 // relocation target
285- indexShard = newShard (TestShardRouting . newShardRouting (shardId , "local_node" , "other node" ,
288+ indexShard = newShard (newShardRouting (shardId , "local_node" , "other node" ,
286289 true , ShardRoutingState .INITIALIZING , AllocationId .newRelocation (AllocationId .newInitializing ())));
287290 } else if (randomBoolean ()) {
288291 // simulate promotion
289292 indexShard = newStartedShard (false );
290293 ShardRouting replicaRouting = indexShard .routingEntry ();
291294 indexShard .updatePrimaryTerm (indexShard .getPrimaryTerm () + 1 );
292- ShardRouting primaryRouting = TestShardRouting . newShardRouting (replicaRouting .shardId (), replicaRouting .currentNodeId (), null ,
295+ ShardRouting primaryRouting = newShardRouting (replicaRouting .shardId (), replicaRouting .currentNodeId (), null ,
293296 true , ShardRoutingState .STARTED , replicaRouting .allocationId ());
294297 indexShard .updateRoutingEntry (primaryRouting );
295298 } else {
@@ -341,7 +344,7 @@ public void testOperationLocksOnReplicaShards() throws InterruptedException, Exe
341344 case 1 : {
342345 // initializing replica / primary
343346 final boolean relocating = randomBoolean ();
344- ShardRouting routing = TestShardRouting . newShardRouting (shardId , "local_node" ,
347+ ShardRouting routing = newShardRouting (shardId , "local_node" ,
345348 relocating ? "sourceNode" : null ,
346349 relocating ? randomBoolean () : false ,
347350 ShardRoutingState .INITIALIZING ,
@@ -353,7 +356,7 @@ public void testOperationLocksOnReplicaShards() throws InterruptedException, Exe
353356 // relocation source
354357 indexShard = newStartedShard (true );
355358 ShardRouting routing = indexShard .routingEntry ();
356- routing = TestShardRouting . newShardRouting (routing .shardId (), routing .currentNodeId (), "otherNode" ,
359+ routing = newShardRouting (routing .shardId (), routing .currentNodeId (), "otherNode" ,
357360 true , ShardRoutingState .RELOCATING , AllocationId .newRelocation (routing .allocationId ()));
358361 indexShard .updateRoutingEntry (routing );
359362 indexShard .relocated ("test" );
@@ -914,6 +917,38 @@ public void testRecoverFromStore() throws IOException {
914917 closeShards (newShard );
915918 }
916919
920+ public void testRecoverFromStoreWithOutOfOrderDelete () throws IOException {
921+ final IndexShard shard = newStartedShard (false );
922+ final Engine .Index index = shard .prepareIndexOnReplica (
923+ SourceToParse .source (SourceToParse .Origin .REPLICA , shard .shardId ().getIndexName (), "type" , "id" , new BytesArray ("{}" ),
924+ XContentType .JSON ), 1 , VersionType .EXTERNAL , IndexRequest .UNSET_AUTO_GENERATED_TIMESTAMP , false );
925+ final Engine .Delete delete = shard .prepareDeleteOnReplica ("type" , "id" , 2 , VersionType .EXTERNAL );
926+ shard .delete (delete );
927+ final int translogOps ;
928+ if (randomBoolean ()) {
929+ flushShard (shard , true ); // lucene won't flush due to just one pending delete
930+ translogOps = 0 ;
931+ } else {
932+ translogOps = 1 ;
933+ }
934+ final Engine .IndexResult result = shard .index (index );
935+ assertThat (result .getTranslogLocation (), nullValue ());
936+ final ShardRouting replicaRouting = shard .routingEntry ();
937+ IndexShard newShard = reinitShard (shard ,
938+ newShardRouting (replicaRouting .shardId (), replicaRouting .currentNodeId (), true , ShardRoutingState .INITIALIZING ,
939+ RecoverySource .StoreRecoverySource .EXISTING_STORE_INSTANCE ));
940+ DiscoveryNode localNode = new DiscoveryNode ("foo" , LocalTransportAddress .buildUnique (), emptyMap (), emptySet (), Version .CURRENT );
941+ newShard .markAsRecovering ("store" , new RecoveryState (newShard .routingEntry (), localNode , null ));
942+ assertTrue (newShard .recoverFromStore ());
943+ assertEquals (translogOps , newShard .recoveryState ().getTranslog ().recoveredOperations ());
944+ assertEquals (translogOps , newShard .recoveryState ().getTranslog ().totalOperations ());
945+ assertEquals (translogOps , newShard .recoveryState ().getTranslog ().totalOperationsOnStart ());
946+ assertEquals (100.0f , newShard .recoveryState ().getTranslog ().recoveredPercent (), 0.01f );
947+ newShard .updateRoutingEntry (newShard .routingEntry ().moveToStarted ());
948+ assertDocCount (newShard , 0 );
949+ closeShards (newShard );
950+ }
951+
917952 public void testRecoverFromCleanStore () throws IOException {
918953 final IndexShard shard = newStartedShard (true );
919954 indexDoc (shard , "test" , "0" );
@@ -1336,7 +1371,7 @@ public void testRecoverFromLocalShard() throws IOException {
13361371 sourceShard .refresh ("test" );
13371372
13381373
1339- ShardRouting targetRouting = TestShardRouting . newShardRouting (new ShardId ("index_1" , "index_1" , 0 ), "n1" , true ,
1374+ ShardRouting targetRouting = newShardRouting (new ShardId ("index_1" , "index_1" , 0 ), "n1" , true ,
13401375 ShardRoutingState .INITIALIZING , RecoverySource .LocalShardsRecoverySource .INSTANCE );
13411376
13421377 final IndexShard targetShard ;
0 commit comments