/**
  * Restores shard from {@link RestoreSource} associated with this shard in routing table
  *
  * @param recoveryState recovery state
  */
 public void restore(final RecoveryState recoveryState) {
   RestoreSource restoreSource = indexShard.routingEntry().restoreSource();
   if (restoreSource == null) {
     throw new IndexShardRestoreFailedException(shardId, "empty restore source");
   }
   if (logger.isTraceEnabled()) {
     logger.trace("[{}] restoring shard  [{}]", restoreSource.snapshotId(), shardId);
   }
   try {
     recoveryState.getTranslog().totalOperations(0);
     recoveryState.getTranslog().totalOperationsOnStart(0);
     indexShard.prepareForIndexRecovery();
     IndexShardRepository indexShardRepository =
         repositoriesService.indexShardRepository(restoreSource.snapshotId().getRepository());
     ShardId snapshotShardId = shardId;
     if (!shardId.getIndex().equals(restoreSource.index())) {
       snapshotShardId = new ShardId(restoreSource.index(), shardId.id());
     }
     indexShardRepository.restore(
         restoreSource.snapshotId(), shardId, snapshotShardId, recoveryState);
     indexShard.prepareForTranslogRecovery();
     indexShard.finalizeRecovery();
     indexShard.postRecovery("restore done");
     restoreService.indexShardRestoreCompleted(restoreSource.snapshotId(), shardId);
   } catch (Throwable t) {
     if (Lucene.isCorruptionException(t)) {
       restoreService.failRestore(restoreSource.snapshotId(), shardId());
     }
     throw new IndexShardRestoreFailedException(shardId, "restore failed", t);
   }
 }
예제 #2
0
 void assertIndexSanity(String indexName, Version indexCreated) {
   GetIndexResponse getIndexResponse =
       client().admin().indices().prepareGetIndex().addIndices(indexName).get();
   assertEquals(1, getIndexResponse.indices().length);
   assertEquals(indexName, getIndexResponse.indices()[0]);
   Version actualVersionCreated =
       Version.indexCreated(getIndexResponse.getSettings().get(indexName));
   assertEquals(indexCreated, actualVersionCreated);
   ensureYellow(indexName);
   RecoveryResponse recoveryResponse =
       client()
           .admin()
           .indices()
           .prepareRecoveries(indexName)
           .setDetailed(true)
           .setActiveOnly(false)
           .get();
   boolean foundTranslog = false;
   for (List<RecoveryState> states : recoveryResponse.shardRecoveryStates().values()) {
     for (RecoveryState state : states) {
       if (state.getStage() == RecoveryState.Stage.DONE
           && state.getPrimary()
           && state.getRecoverySource().getType() == RecoverySource.Type.EXISTING_STORE) {
         assertFalse("more than one primary recoverd?", foundTranslog);
         assertNotEquals(0, state.getTranslog().recoveredOperations());
         foundTranslog = true;
       }
     }
   }
   assertTrue("expected translog but nothing was recovered", foundTranslog);
   IndicesSegmentResponse segmentsResponse =
       client().admin().indices().prepareSegments(indexName).get();
   IndexSegments segments = segmentsResponse.getIndices().get(indexName);
   int numCurrent = 0;
   int numBWC = 0;
   for (IndexShardSegments indexShardSegments : segments) {
     for (ShardSegments shardSegments : indexShardSegments) {
       for (Segment segment : shardSegments) {
         if (indexCreated.luceneVersion.equals(segment.version)) {
           numBWC++;
           if (Version.CURRENT.luceneVersion.equals(segment.version)) {
             numCurrent++;
           }
         } else if (Version.CURRENT.luceneVersion.equals(segment.version)) {
           numCurrent++;
         } else {
           fail("unexpected version " + segment.version);
         }
       }
     }
   }
   assertNotEquals("expected at least 1 current segment after translog recovery", 0, numCurrent);
   assertNotEquals("expected at least 1 old segment", 0, numBWC);
   SearchResponse test = client().prepareSearch(indexName).get();
   assertThat(test.getHits().getTotalHits(), greaterThanOrEqualTo(1L));
 }