Search in sources :

Example 6 with RecoveryState

use of org.elasticsearch.indices.recovery.RecoveryState in project elasticsearch by elastic.

the class IndexShardTests method testFailIfIndexNotPresentInRecoverFromStore.

public void testFailIfIndexNotPresentInRecoverFromStore() throws Exception {
    final IndexShard shard = newStartedShard(true);
    indexDoc(shard, "test", "0");
    if (randomBoolean()) {
        flushShard(shard);
    }
    Store store = shard.store();
    store.incRef();
    closeShards(shard);
    cleanLuceneIndex(store.directory());
    store.decRef();
    IndexShard newShard = reinitShard(shard);
    DiscoveryNode localNode = new DiscoveryNode("foo", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT);
    ShardRouting routing = newShard.routingEntry();
    newShard.markAsRecovering("store", new RecoveryState(routing, localNode, null));
    try {
        newShard.recoverFromStore();
        fail("index not there!");
    } catch (IndexShardRecoveryException ex) {
        assertTrue(ex.getMessage().contains("failed to fetch index version after copying it over"));
    }
    routing = ShardRoutingHelper.moveToUnassigned(routing, new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "because I say so"));
    routing = ShardRoutingHelper.initialize(routing, newShard.routingEntry().currentNodeId());
    assertTrue("it's already recovering, we should ignore new ones", newShard.ignoreRecoveryAttempt());
    try {
        newShard.markAsRecovering("store", new RecoveryState(routing, localNode, null));
        fail("we are already recovering, can't mark again");
    } catch (IllegalIndexShardStateException e) {
    // OK!
    }
    newShard = reinitShard(newShard, ShardRoutingHelper.initWithSameId(routing, RecoverySource.StoreRecoverySource.EMPTY_STORE_INSTANCE));
    newShard.markAsRecovering("store", new RecoveryState(newShard.routingEntry(), localNode, null));
    assertTrue("recover even if there is nothing to recover", newShard.recoverFromStore());
    newShard.updateRoutingEntry(newShard.routingEntry().moveToStarted());
    assertDocCount(newShard, 0);
    // we can't issue this request through a client because of the inconsistencies we created with the cluster state
    // doing it directly instead
    indexDoc(newShard, "test", "0");
    newShard.refresh("test");
    assertDocCount(newShard, 1);
    closeShards(newShard);
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) UnassignedInfo(org.elasticsearch.cluster.routing.UnassignedInfo) Store(org.elasticsearch.index.store.Store) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) RecoveryState(org.elasticsearch.indices.recovery.RecoveryState)

Example 7 with RecoveryState

use of org.elasticsearch.indices.recovery.RecoveryState in project elasticsearch by elastic.

the class IndexShardIT method recoverShard.

public static final IndexShard recoverShard(IndexShard newShard) throws IOException {
    DiscoveryNode localNode = new DiscoveryNode("foo", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT);
    newShard.markAsRecovering("store", new RecoveryState(newShard.routingEntry(), localNode, null));
    assertTrue(newShard.recoverFromStore());
    newShard.updateRoutingEntry(newShard.routingEntry().moveToStarted());
    return newShard;
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) RecoveryState(org.elasticsearch.indices.recovery.RecoveryState)

Example 8 with RecoveryState

use of org.elasticsearch.indices.recovery.RecoveryState in project elasticsearch by elastic.

the class IndexShardTests method testSnapshotStore.

/***
     * test one can snapshot the store at various lifecycle stages
     */
public void testSnapshotStore() throws IOException {
    final IndexShard shard = newStartedShard(true);
    indexDoc(shard, "test", "0");
    flushShard(shard);
    final IndexShard newShard = reinitShard(shard);
    DiscoveryNode localNode = new DiscoveryNode("foo", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT);
    Store.MetadataSnapshot snapshot = newShard.snapshotStoreMetadata();
    assertThat(snapshot.getSegmentsFile().name(), equalTo("segments_2"));
    newShard.markAsRecovering("store", new RecoveryState(newShard.routingEntry(), localNode, null));
    snapshot = newShard.snapshotStoreMetadata();
    assertThat(snapshot.getSegmentsFile().name(), equalTo("segments_2"));
    assertTrue(newShard.recoverFromStore());
    snapshot = newShard.snapshotStoreMetadata();
    assertThat(snapshot.getSegmentsFile().name(), equalTo("segments_2"));
    newShard.updateRoutingEntry(newShard.routingEntry().moveToStarted());
    snapshot = newShard.snapshotStoreMetadata();
    assertThat(snapshot.getSegmentsFile().name(), equalTo("segments_2"));
    newShard.close("test", false);
    snapshot = newShard.snapshotStoreMetadata();
    assertThat(snapshot.getSegmentsFile().name(), equalTo("segments_2"));
    closeShards(newShard);
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) Store(org.elasticsearch.index.store.Store) RecoveryState(org.elasticsearch.indices.recovery.RecoveryState)

Example 9 with RecoveryState

use of org.elasticsearch.indices.recovery.RecoveryState in project elasticsearch by elastic.

the class RestRecoveryActionTests method testRestRecoveryAction.

public void testRestRecoveryAction() {
    final Settings settings = Settings.EMPTY;
    final RestController restController = new RestController(settings, Collections.emptySet(), null, null, null);
    final RestRecoveryAction action = new RestRecoveryAction(settings, restController);
    final int totalShards = randomIntBetween(1, 32);
    final int successfulShards = Math.max(0, totalShards - randomIntBetween(1, 2));
    final int failedShards = totalShards - successfulShards;
    final boolean detailed = randomBoolean();
    final Map<String, List<RecoveryState>> shardRecoveryStates = new HashMap<>();
    final List<RecoveryState> recoveryStates = new ArrayList<>();
    for (int i = 0; i < successfulShards; i++) {
        final RecoveryState state = mock(RecoveryState.class);
        when(state.getShardId()).thenReturn(new ShardId(new Index("index", "_na_"), i));
        final RecoveryState.Timer timer = mock(RecoveryState.Timer.class);
        when(timer.time()).thenReturn((long) randomIntBetween(1000000, 10 * 1000000));
        when(state.getTimer()).thenReturn(timer);
        when(state.getRecoverySource()).thenReturn(TestShardRouting.randomRecoverySource());
        when(state.getStage()).thenReturn(randomFrom(RecoveryState.Stage.values()));
        final DiscoveryNode sourceNode = randomBoolean() ? mock(DiscoveryNode.class) : null;
        if (sourceNode != null) {
            when(sourceNode.getHostName()).thenReturn(randomAsciiOfLength(8));
        }
        when(state.getSourceNode()).thenReturn(sourceNode);
        final DiscoveryNode targetNode = mock(DiscoveryNode.class);
        when(targetNode.getHostName()).thenReturn(randomAsciiOfLength(8));
        when(state.getTargetNode()).thenReturn(targetNode);
        RecoveryState.Index index = mock(RecoveryState.Index.class);
        final int totalRecoveredFiles = randomIntBetween(1, 64);
        when(index.totalRecoverFiles()).thenReturn(totalRecoveredFiles);
        final int recoveredFileCount = randomIntBetween(0, totalRecoveredFiles);
        when(index.recoveredFileCount()).thenReturn(recoveredFileCount);
        when(index.recoveredFilesPercent()).thenReturn((100f * recoveredFileCount) / totalRecoveredFiles);
        when(index.totalFileCount()).thenReturn(randomIntBetween(totalRecoveredFiles, 2 * totalRecoveredFiles));
        final int totalRecoveredBytes = randomIntBetween(1, 1 << 24);
        when(index.totalRecoverBytes()).thenReturn((long) totalRecoveredBytes);
        final int recoveredBytes = randomIntBetween(0, totalRecoveredBytes);
        when(index.recoveredBytes()).thenReturn((long) recoveredBytes);
        when(index.recoveredBytesPercent()).thenReturn((100f * recoveredBytes) / totalRecoveredBytes);
        when(index.totalRecoverBytes()).thenReturn((long) randomIntBetween(totalRecoveredBytes, 2 * totalRecoveredBytes));
        when(state.getIndex()).thenReturn(index);
        final RecoveryState.Translog translog = mock(RecoveryState.Translog.class);
        final int translogOps = randomIntBetween(0, 1 << 18);
        when(translog.totalOperations()).thenReturn(translogOps);
        final int translogOpsRecovered = randomIntBetween(0, translogOps);
        when(translog.recoveredOperations()).thenReturn(translogOpsRecovered);
        when(translog.recoveredPercent()).thenReturn(translogOps == 0 ? 100f : (100f * translogOpsRecovered / translogOps));
        when(state.getTranslog()).thenReturn(translog);
        recoveryStates.add(state);
    }
    final List<RecoveryState> shuffle = new ArrayList<>(recoveryStates);
    Randomness.shuffle(shuffle);
    shardRecoveryStates.put("index", shuffle);
    final List<ShardOperationFailedException> shardFailures = new ArrayList<>();
    final RecoveryResponse response = new RecoveryResponse(totalShards, successfulShards, failedShards, detailed, shardRecoveryStates, shardFailures);
    final Table table = action.buildRecoveryTable(null, response);
    assertNotNull(table);
    List<Table.Cell> headers = table.getHeaders();
    assertThat(headers.get(0).value, equalTo("index"));
    assertThat(headers.get(1).value, equalTo("shard"));
    assertThat(headers.get(2).value, equalTo("time"));
    assertThat(headers.get(3).value, equalTo("type"));
    assertThat(headers.get(4).value, equalTo("stage"));
    assertThat(headers.get(5).value, equalTo("source_host"));
    assertThat(headers.get(6).value, equalTo("source_node"));
    assertThat(headers.get(7).value, equalTo("target_host"));
    assertThat(headers.get(8).value, equalTo("target_node"));
    assertThat(headers.get(9).value, equalTo("repository"));
    assertThat(headers.get(10).value, equalTo("snapshot"));
    assertThat(headers.get(11).value, equalTo("files"));
    assertThat(headers.get(12).value, equalTo("files_recovered"));
    assertThat(headers.get(13).value, equalTo("files_percent"));
    assertThat(headers.get(14).value, equalTo("files_total"));
    assertThat(headers.get(15).value, equalTo("bytes"));
    assertThat(headers.get(16).value, equalTo("bytes_recovered"));
    assertThat(headers.get(17).value, equalTo("bytes_percent"));
    assertThat(headers.get(18).value, equalTo("bytes_total"));
    assertThat(headers.get(19).value, equalTo("translog_ops"));
    assertThat(headers.get(20).value, equalTo("translog_ops_recovered"));
    assertThat(headers.get(21).value, equalTo("translog_ops_percent"));
    assertThat(table.getRows().size(), equalTo(successfulShards));
    for (int i = 0; i < successfulShards; i++) {
        final RecoveryState state = recoveryStates.get(i);
        List<Table.Cell> cells = table.getRows().get(i);
        assertThat(cells.get(0).value, equalTo("index"));
        assertThat(cells.get(1).value, equalTo(i));
        assertThat(cells.get(2).value, equalTo(new TimeValue(state.getTimer().time())));
        assertThat(cells.get(3).value, equalTo(state.getRecoverySource().getType().name().toLowerCase(Locale.ROOT)));
        assertThat(cells.get(4).value, equalTo(state.getStage().name().toLowerCase(Locale.ROOT)));
        assertThat(cells.get(5).value, equalTo(state.getSourceNode() == null ? "n/a" : state.getSourceNode().getHostName()));
        assertThat(cells.get(6).value, equalTo(state.getSourceNode() == null ? "n/a" : state.getSourceNode().getName()));
        assertThat(cells.get(7).value, equalTo(state.getTargetNode().getHostName()));
        assertThat(cells.get(8).value, equalTo(state.getTargetNode().getName()));
        assertThat(cells.get(9).value, equalTo(state.getRecoverySource() == null || state.getRecoverySource().getType() != RecoverySource.Type.SNAPSHOT ? "n/a" : ((SnapshotRecoverySource) state.getRecoverySource()).snapshot().getRepository()));
        assertThat(cells.get(10).value, equalTo(state.getRecoverySource() == null || state.getRecoverySource().getType() != RecoverySource.Type.SNAPSHOT ? "n/a" : ((SnapshotRecoverySource) state.getRecoverySource()).snapshot().getSnapshotId().getName()));
        assertThat(cells.get(11).value, equalTo(state.getIndex().totalRecoverFiles()));
        assertThat(cells.get(12).value, equalTo(state.getIndex().recoveredFileCount()));
        assertThat(cells.get(13).value, equalTo(percent(state.getIndex().recoveredFilesPercent())));
        assertThat(cells.get(14).value, equalTo(state.getIndex().totalFileCount()));
        assertThat(cells.get(15).value, equalTo(state.getIndex().totalRecoverBytes()));
        assertThat(cells.get(16).value, equalTo(state.getIndex().recoveredBytes()));
        assertThat(cells.get(17).value, equalTo(percent(state.getIndex().recoveredBytesPercent())));
        assertThat(cells.get(18).value, equalTo(state.getIndex().totalBytes()));
        assertThat(cells.get(19).value, equalTo(state.getTranslog().totalOperations()));
        assertThat(cells.get(20).value, equalTo(state.getTranslog().recoveredOperations()));
        assertThat(cells.get(21).value, equalTo(percent(state.getTranslog().recoveredPercent())));
    }
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RestController(org.elasticsearch.rest.RestController) Index(org.elasticsearch.index.Index) RecoveryResponse(org.elasticsearch.action.admin.indices.recovery.RecoveryResponse) ShardId(org.elasticsearch.index.shard.ShardId) ArrayList(java.util.ArrayList) List(java.util.List) ShardOperationFailedException(org.elasticsearch.action.ShardOperationFailedException) RecoveryState(org.elasticsearch.indices.recovery.RecoveryState) Settings(org.elasticsearch.common.settings.Settings) TimeValue(org.elasticsearch.common.unit.TimeValue) Table(org.elasticsearch.common.Table) SnapshotRecoverySource(org.elasticsearch.cluster.routing.RecoverySource.SnapshotRecoverySource)

Example 10 with RecoveryState

use of org.elasticsearch.indices.recovery.RecoveryState in project elasticsearch by elastic.

the class FullRollingRestartIT method testNoRebalanceOnRollingRestart.

public void testNoRebalanceOnRollingRestart() throws Exception {
    // see https://github.com/elastic/elasticsearch/issues/14387
    internalCluster().startMasterOnlyNode(Settings.EMPTY);
    internalCluster().startDataOnlyNodes(3);
    /**
         * We start 3 nodes and a dedicated master. Restart on of the data-nodes and ensure that we got no relocations.
         * Yet we have 6 shards 0 replica so that means if the restarting node comes back both other nodes are subject
         * to relocating to the restarting node since all had 2 shards and now one node has nothing allocated.
         * We have a fix for this to wait until we have allocated unallocated shards now so this shouldn't happen.
         */
    prepareCreate("test").setSettings(Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, "6").put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, "0").put(UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), TimeValue.timeValueMinutes(1))).get();
    for (int i = 0; i < 100; i++) {
        client().prepareIndex("test", "type1", Long.toString(i)).setSource(MapBuilder.<String, Object>newMapBuilder().put("test", "value" + i).map()).execute().actionGet();
    }
    ensureGreen();
    ClusterState state = client().admin().cluster().prepareState().get().getState();
    RecoveryResponse recoveryResponse = client().admin().indices().prepareRecoveries("test").get();
    for (RecoveryState recoveryState : recoveryResponse.shardRecoveryStates().get("test")) {
        assertTrue("relocated from: " + recoveryState.getSourceNode() + " to: " + recoveryState.getTargetNode() + "\n" + state, recoveryState.getRecoverySource().getType() != RecoverySource.Type.PEER || recoveryState.getPrimary() == false);
    }
    internalCluster().restartRandomDataNode();
    ensureGreen();
    ClusterState afterState = client().admin().cluster().prepareState().get().getState();
    recoveryResponse = client().admin().indices().prepareRecoveries("test").get();
    for (RecoveryState recoveryState : recoveryResponse.shardRecoveryStates().get("test")) {
        assertTrue("relocated from: " + recoveryState.getSourceNode() + " to: " + recoveryState.getTargetNode() + "-- \nbefore: \n" + state, recoveryState.getRecoverySource().getType() != RecoverySource.Type.PEER || recoveryState.getPrimary() == false);
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) RecoveryState(org.elasticsearch.indices.recovery.RecoveryState) RecoveryResponse(org.elasticsearch.action.admin.indices.recovery.RecoveryResponse)

Aggregations

RecoveryState (org.elasticsearch.indices.recovery.RecoveryState)29 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)16 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)9 Settings (org.elasticsearch.common.settings.Settings)7 Store (org.elasticsearch.index.store.Store)6 IOException (java.io.IOException)5 TestShardRouting (org.elasticsearch.cluster.routing.TestShardRouting)5 RecoveryResponse (org.elasticsearch.action.admin.indices.recovery.RecoveryResponse)4 UnassignedInfo (org.elasticsearch.cluster.routing.UnassignedInfo)4 EngineException (org.elasticsearch.index.engine.EngineException)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 List (java.util.List)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Version (org.elasticsearch.Version)3 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)3 IndexService (org.elasticsearch.index.IndexService)3 IndexShard (org.elasticsearch.index.shard.IndexShard)3 Path (java.nio.file.Path)2