Search in sources :

Example 1 with RecoveryResponse

use of org.elasticsearch.action.admin.indices.recovery.RecoveryResponse in project elasticsearch by elastic.

the class IndexRecoveryIT method testSnapshotRecovery.

public void testSnapshotRecovery() throws Exception {
    logger.info("--> start node A");
    String nodeA = internalCluster().startNode();
    logger.info("--> create repository");
    assertAcked(client().admin().cluster().preparePutRepository(REPO_NAME).setType("fs").setSettings(Settings.builder().put("location", randomRepoPath()).put("compress", false)).get());
    ensureGreen();
    logger.info("--> create index on node: {}", nodeA);
    createAndPopulateIndex(INDEX_NAME, 1, SHARD_COUNT, REPLICA_COUNT);
    logger.info("--> snapshot");
    CreateSnapshotResponse createSnapshotResponse = client().admin().cluster().prepareCreateSnapshot(REPO_NAME, SNAP_NAME).setWaitForCompletion(true).setIndices(INDEX_NAME).get();
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));
    assertThat(client().admin().cluster().prepareGetSnapshots(REPO_NAME).setSnapshots(SNAP_NAME).get().getSnapshots().get(0).state(), equalTo(SnapshotState.SUCCESS));
    client().admin().indices().prepareClose(INDEX_NAME).execute().actionGet();
    logger.info("--> restore");
    RestoreSnapshotResponse restoreSnapshotResponse = client().admin().cluster().prepareRestoreSnapshot(REPO_NAME, SNAP_NAME).setWaitForCompletion(true).execute().actionGet();
    int totalShards = restoreSnapshotResponse.getRestoreInfo().totalShards();
    assertThat(totalShards, greaterThan(0));
    ensureGreen();
    logger.info("--> request recoveries");
    RecoveryResponse response = client().admin().indices().prepareRecoveries(INDEX_NAME).execute().actionGet();
    for (Map.Entry<String, List<RecoveryState>> indexRecoveryStates : response.shardRecoveryStates().entrySet()) {
        assertThat(indexRecoveryStates.getKey(), equalTo(INDEX_NAME));
        List<RecoveryState> recoveryStates = indexRecoveryStates.getValue();
        assertThat(recoveryStates.size(), equalTo(totalShards));
        for (RecoveryState recoveryState : recoveryStates) {
            SnapshotRecoverySource recoverySource = new SnapshotRecoverySource(new Snapshot(REPO_NAME, createSnapshotResponse.getSnapshotInfo().snapshotId()), Version.CURRENT, INDEX_NAME);
            assertRecoveryState(recoveryState, 0, recoverySource, true, Stage.DONE, null, nodeA);
            validateIndexRecoveryState(recoveryState.getIndex());
        }
    }
}
Also used : Snapshot(org.elasticsearch.snapshots.Snapshot) SnapshotRecoverySource(org.elasticsearch.cluster.routing.RecoverySource.SnapshotRecoverySource) CreateSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) RestoreSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse) RecoveryResponse(org.elasticsearch.action.admin.indices.recovery.RecoveryResponse)

Example 2 with RecoveryResponse

use of org.elasticsearch.action.admin.indices.recovery.RecoveryResponse 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 3 with RecoveryResponse

use of org.elasticsearch.action.admin.indices.recovery.RecoveryResponse 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)

Example 4 with RecoveryResponse

use of org.elasticsearch.action.admin.indices.recovery.RecoveryResponse in project elasticsearch by elastic.

the class OldIndexBackwardsCompatibilityIT method assertIndexSanity.

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));
}
Also used : IndicesSegmentResponse(org.elasticsearch.action.admin.indices.segments.IndicesSegmentResponse) IndexShardSegments(org.elasticsearch.action.admin.indices.segments.IndexShardSegments) IndexSegments(org.elasticsearch.action.admin.indices.segments.IndexSegments) Segment(org.elasticsearch.index.engine.Segment) RecoveryResponse(org.elasticsearch.action.admin.indices.recovery.RecoveryResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse) Version(org.elasticsearch.Version) GetIndexResponse(org.elasticsearch.action.admin.indices.get.GetIndexResponse) ShardSegments(org.elasticsearch.action.admin.indices.segments.ShardSegments) IndexShardSegments(org.elasticsearch.action.admin.indices.segments.IndexShardSegments) RecoveryState(org.elasticsearch.indices.recovery.RecoveryState)

Example 5 with RecoveryResponse

use of org.elasticsearch.action.admin.indices.recovery.RecoveryResponse in project elasticsearch by elastic.

the class ZenDiscoveryIT method testNoShardRelocationsOccurWhenElectedMasterNodeFails.

public void testNoShardRelocationsOccurWhenElectedMasterNodeFails() throws Exception {
    Settings defaultSettings = Settings.builder().put(FaultDetection.PING_TIMEOUT_SETTING.getKey(), "1s").put(FaultDetection.PING_RETRIES_SETTING.getKey(), "1").build();
    Settings masterNodeSettings = Settings.builder().put(Node.NODE_DATA_SETTING.getKey(), false).put(defaultSettings).build();
    internalCluster().startNodes(2, masterNodeSettings);
    Settings dateNodeSettings = Settings.builder().put(Node.NODE_MASTER_SETTING.getKey(), false).put(defaultSettings).build();
    internalCluster().startNodes(2, dateNodeSettings);
    ClusterHealthResponse clusterHealthResponse = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForNodes("4").setWaitForNoRelocatingShards(true).get();
    assertThat(clusterHealthResponse.isTimedOut(), is(false));
    createIndex("test");
    ensureSearchable("test");
    RecoveryResponse r = client().admin().indices().prepareRecoveries("test").get();
    int numRecoveriesBeforeNewMaster = r.shardRecoveryStates().get("test").size();
    final String oldMaster = internalCluster().getMasterName();
    internalCluster().stopCurrentMasterNode();
    assertBusy(() -> {
        String current = internalCluster().getMasterName();
        assertThat(current, notNullValue());
        assertThat(current, not(equalTo(oldMaster)));
    });
    ensureSearchable("test");
    r = client().admin().indices().prepareRecoveries("test").get();
    int numRecoveriesAfterNewMaster = r.shardRecoveryStates().get("test").size();
    assertThat(numRecoveriesAfterNewMaster, equalTo(numRecoveriesBeforeNewMaster));
}
Also used : ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) Settings(org.elasticsearch.common.settings.Settings) RecoveryResponse(org.elasticsearch.action.admin.indices.recovery.RecoveryResponse)

Aggregations

RecoveryResponse (org.elasticsearch.action.admin.indices.recovery.RecoveryResponse)20 Settings (org.elasticsearch.common.settings.Settings)9 RecoveryRequest (org.elasticsearch.action.admin.indices.recovery.RecoveryRequest)8 List (java.util.List)6 RecoveryState (org.elasticsearch.indices.recovery.RecoveryState)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 ClusterState (org.elasticsearch.cluster.ClusterState)4 RecoverySource (org.elasticsearch.cluster.routing.RecoverySource)4 IOException (java.io.IOException)3 Map (java.util.Map)3 SnapshotRecoverySource (org.elasticsearch.cluster.routing.RecoverySource.SnapshotRecoverySource)3 Index (org.elasticsearch.index.Index)3 Snapshot (org.elasticsearch.snapshots.Snapshot)3 Collections.singletonMap (java.util.Collections.singletonMap)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Version (org.elasticsearch.Version)2 ClusterHealthResponse (org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse)2 GetSnapshotsRequest (org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsRequest)2 ClusterStateResponse (org.elasticsearch.action.admin.cluster.state.ClusterStateResponse)2