Search in sources :

Example 96 with Client

use of org.elasticsearch.client.Client in project elasticsearch by elastic.

the class DedicatedClusterSnapshotRestoreIT method testRestorePersistentSettings.

public void testRestorePersistentSettings() throws Exception {
    logger.info("--> start 2 nodes");
    Settings nodeSettings = Settings.builder().put("discovery.type", "zen").put(ZenDiscovery.PING_TIMEOUT_SETTING.getKey(), "200ms").put("discovery.initial_state_timeout", "500ms").build();
    internalCluster().startNode(nodeSettings);
    Client client = client();
    String secondNode = internalCluster().startNode(nodeSettings);
    logger.info("--> wait for the second node to join the cluster");
    assertThat(client.admin().cluster().prepareHealth().setWaitForNodes("2").get().isTimedOut(), equalTo(false));
    int random = randomIntBetween(10, 42);
    logger.info("--> set test persistent setting");
    client.admin().cluster().prepareUpdateSettings().setPersistentSettings(Settings.builder().put(ElectMasterService.DISCOVERY_ZEN_MINIMUM_MASTER_NODES_SETTING.getKey(), 2)).execute().actionGet();
    assertThat(client.admin().cluster().prepareState().setRoutingTable(false).setNodes(false).execute().actionGet().getState().getMetaData().persistentSettings().getAsInt(ElectMasterService.DISCOVERY_ZEN_MINIMUM_MASTER_NODES_SETTING.getKey(), -1), equalTo(2));
    logger.info("--> create repository");
    PutRepositoryResponse putRepositoryResponse = client.admin().cluster().preparePutRepository("test-repo").setType("fs").setSettings(Settings.builder().put("location", randomRepoPath())).execute().actionGet();
    assertThat(putRepositoryResponse.isAcknowledged(), equalTo(true));
    logger.info("--> start snapshot");
    CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(true).execute().actionGet();
    assertThat(createSnapshotResponse.getSnapshotInfo().totalShards(), equalTo(0));
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(0));
    assertThat(client.admin().cluster().prepareGetSnapshots("test-repo").setSnapshots("test-snap").execute().actionGet().getSnapshots().get(0).state(), equalTo(SnapshotState.SUCCESS));
    logger.info("--> clean the test persistent setting");
    client.admin().cluster().prepareUpdateSettings().setPersistentSettings(Settings.builder().put(ElectMasterService.DISCOVERY_ZEN_MINIMUM_MASTER_NODES_SETTING.getKey(), 1)).execute().actionGet();
    assertThat(client.admin().cluster().prepareState().setRoutingTable(false).setNodes(false).execute().actionGet().getState().getMetaData().persistentSettings().getAsInt(ElectMasterService.DISCOVERY_ZEN_MINIMUM_MASTER_NODES_SETTING.getKey(), -1), equalTo(1));
    stopNode(secondNode);
    assertThat(client.admin().cluster().prepareHealth().setWaitForNodes("1").get().isTimedOut(), equalTo(false));
    logger.info("--> restore snapshot");
    try {
        client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setRestoreGlobalState(true).setWaitForCompletion(true).execute().actionGet();
        fail("can't restore minimum master nodes");
    } catch (IllegalArgumentException ex) {
        assertEquals("illegal value can't update [discovery.zen.minimum_master_nodes] from [1] to [2]", ex.getMessage());
        assertEquals("cannot set discovery.zen.minimum_master_nodes to more than the current master nodes count [1]", ex.getCause().getMessage());
    }
    logger.info("--> ensure that zen discovery minimum master nodes wasn't restored");
    assertThat(client.admin().cluster().prepareState().setRoutingTable(false).setNodes(false).execute().actionGet().getState().getMetaData().persistentSettings().getAsInt(ElectMasterService.DISCOVERY_ZEN_MINIMUM_MASTER_NODES_SETTING.getKey(), -1), not(equalTo(2)));
}
Also used : CreateSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) PutRepositoryResponse(org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryResponse) NodeClient(org.elasticsearch.client.node.NodeClient) Client(org.elasticsearch.client.Client) Settings(org.elasticsearch.common.settings.Settings)

Example 97 with Client

use of org.elasticsearch.client.Client in project elasticsearch by elastic.

the class SharedClusterSnapshotRestoreIT method testMoveShardWhileSnapshotting.

public void testMoveShardWhileSnapshotting() throws Exception {
    Client client = client();
    Path repositoryLocation = randomRepoPath();
    logger.info("-->  creating repository");
    assertAcked(client.admin().cluster().preparePutRepository("test-repo").setType("mock").setSettings(Settings.builder().put("location", repositoryLocation).put("random", randomAsciiOfLength(10)).put("wait_after_unblock", 200)));
    // Create index on 2 nodes and make sure each node has a primary by setting no replicas
    assertAcked(prepareCreate("test-idx", 2, Settings.builder().put("number_of_replicas", 0)));
    logger.info("--> indexing some data");
    for (int i = 0; i < 100; i++) {
        index("test-idx", "doc", Integer.toString(i), "foo", "bar" + i);
    }
    refresh();
    assertThat(client.prepareSearch("test-idx").setSize(0).get().getHits().getTotalHits(), equalTo(100L));
    // Pick one node and block it
    String blockedNode = blockNodeWithIndex("test-repo", "test-idx");
    logger.info("--> snapshot");
    client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(false).setIndices("test-idx").get();
    logger.info("--> waiting for block to kick in");
    waitForBlock(blockedNode, "test-repo", TimeValue.timeValueSeconds(60));
    logger.info("--> execution was blocked on node [{}], moving shards away from this node", blockedNode);
    Settings.Builder excludeSettings = Settings.builder().put("index.routing.allocation.exclude._name", blockedNode);
    client().admin().indices().prepareUpdateSettings("test-idx").setSettings(excludeSettings).get();
    logger.info("--> unblocking blocked node");
    unblockNode("test-repo", blockedNode);
    logger.info("--> waiting for completion");
    SnapshotInfo snapshotInfo = waitForCompletion("test-repo", "test-snap", TimeValue.timeValueSeconds(600));
    logger.info("Number of failed shards [{}]", snapshotInfo.shardFailures().size());
    logger.info("--> done");
    List<SnapshotInfo> snapshotInfos = client().admin().cluster().prepareGetSnapshots("test-repo").setSnapshots("test-snap").get().getSnapshots();
    assertThat(snapshotInfos.size(), equalTo(1));
    assertThat(snapshotInfos.get(0).state(), equalTo(SnapshotState.SUCCESS));
    assertThat(snapshotInfos.get(0).shardFailures().size(), equalTo(0));
    logger.info("--> delete index");
    cluster().wipeIndices("test-idx");
    logger.info("--> replace mock repository with real one at the same location");
    assertAcked(client.admin().cluster().preparePutRepository("test-repo").setType("fs").setSettings(Settings.builder().put("location", repositoryLocation)));
    logger.info("--> restore index");
    RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setWaitForCompletion(true).execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    assertThat(client.prepareSearch("test-idx").setSize(0).get().getHits().getTotalHits(), equalTo(100L));
}
Also used : Path(java.nio.file.Path) Matchers.containsString(org.hamcrest.Matchers.containsString) Client(org.elasticsearch.client.Client) Settings(org.elasticsearch.common.settings.Settings) RestoreSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)

Example 98 with Client

use of org.elasticsearch.client.Client in project elasticsearch by elastic.

the class SharedClusterSnapshotRestoreIT method testDeleteSnapshot.

public void testDeleteSnapshot() throws Exception {
    final int numberOfSnapshots = between(5, 15);
    Client client = client();
    Path repo = randomRepoPath();
    logger.info("-->  creating repository at {}", repo.toAbsolutePath());
    assertAcked(client.admin().cluster().preparePutRepository("test-repo").setType("fs").setSettings(Settings.builder().put("location", repo).put("compress", false).put("chunk_size", randomIntBetween(100, 1000), ByteSizeUnit.BYTES)));
    createIndex("test-idx");
    ensureGreen();
    int[] numberOfFiles = new int[numberOfSnapshots];
    logger.info("--> creating {} snapshots ", numberOfSnapshots);
    for (int i = 0; i < numberOfSnapshots; i++) {
        for (int j = 0; j < 10; j++) {
            index("test-idx", "doc", Integer.toString(i * 10 + j), "foo", "bar" + i * 10 + j);
        }
        refresh();
        logger.info("--> snapshot {}", i);
        CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap-" + i).setWaitForCompletion(true).setIndices("test-idx").get();
        assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
        assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));
        // Store number of files after each snapshot
        numberOfFiles[i] = numberOfFiles(repo);
    }
    assertThat(client.prepareSearch("test-idx").setSize(0).get().getHits().getTotalHits(), equalTo(10L * numberOfSnapshots));
    int numberOfFilesBeforeDeletion = numberOfFiles(repo);
    logger.info("--> delete all snapshots except the first one and last one");
    for (int i = 1; i < numberOfSnapshots - 1; i++) {
        client.admin().cluster().prepareDeleteSnapshot("test-repo", "test-snap-" + i).get();
    }
    int numberOfFilesAfterDeletion = numberOfFiles(repo);
    assertThat(numberOfFilesAfterDeletion, lessThan(numberOfFilesBeforeDeletion));
    logger.info("--> delete index");
    cluster().wipeIndices("test-idx");
    logger.info("--> restore index");
    String lastSnapshot = "test-snap-" + (numberOfSnapshots - 1);
    RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", lastSnapshot).setWaitForCompletion(true).execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    assertThat(client.prepareSearch("test-idx").setSize(0).get().getHits().getTotalHits(), equalTo(10L * numberOfSnapshots));
    logger.info("--> delete the last snapshot");
    client.admin().cluster().prepareDeleteSnapshot("test-repo", lastSnapshot).get();
    logger.info("--> make sure that number of files is back to what it was when the first snapshot was made, " + "plus one because one backup index-N file should remain");
    assertThat(numberOfFiles(repo), equalTo(numberOfFiles[0] + 1));
}
Also used : Path(java.nio.file.Path) CreateSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) Client(org.elasticsearch.client.Client) RestoreSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)

Example 99 with Client

use of org.elasticsearch.client.Client in project elasticsearch by elastic.

the class SharedClusterSnapshotRestoreIT method testSnapshotClosedIndex.

public void testSnapshotClosedIndex() throws Exception {
    Client client = client();
    logger.info("-->  creating repository");
    assertAcked(client.admin().cluster().preparePutRepository("test-repo").setType("fs").setSettings(Settings.builder().put("location", randomRepoPath())));
    createIndex("test-idx", "test-idx-closed");
    ensureGreen();
    logger.info("-->  closing index test-idx-closed");
    assertAcked(client.admin().indices().prepareClose("test-idx-closed"));
    ClusterStateResponse stateResponse = client.admin().cluster().prepareState().get();
    assertThat(stateResponse.getState().metaData().index("test-idx-closed").getState(), equalTo(IndexMetaData.State.CLOSE));
    assertThat(stateResponse.getState().routingTable().index("test-idx-closed"), nullValue());
    logger.info("--> snapshot");
    CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndices("test-idx*").get();
    assertThat(createSnapshotResponse.getSnapshotInfo().indices().size(), equalTo(1));
    assertThat(createSnapshotResponse.getSnapshotInfo().shardFailures().size(), equalTo(0));
    logger.info("-->  deleting snapshot");
    client.admin().cluster().prepareDeleteSnapshot("test-repo", "test-snap").get();
    logger.info("--> snapshot with closed index");
    assertBlocked(client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndices("test-idx", "test-idx-closed"), MetaDataIndexStateService.INDEX_CLOSED_BLOCK);
}
Also used : CreateSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) Client(org.elasticsearch.client.Client)

Example 100 with Client

use of org.elasticsearch.client.Client in project elasticsearch by elastic.

the class SharedClusterSnapshotRestoreIT method testBasicWorkFlow.

public void testBasicWorkFlow() throws Exception {
    Client client = client();
    logger.info("-->  creating repository");
    assertAcked(client.admin().cluster().preparePutRepository("test-repo").setType("fs").setSettings(Settings.builder().put("location", randomRepoPath()).put("compress", randomBoolean()).put("chunk_size", randomIntBetween(100, 1000), ByteSizeUnit.BYTES)));
    createIndex("test-idx-1", "test-idx-2", "test-idx-3");
    ensureGreen();
    logger.info("--> indexing some data");
    for (int i = 0; i < 100; i++) {
        index("test-idx-1", "doc", Integer.toString(i), "foo", "bar" + i);
        index("test-idx-2", "doc", Integer.toString(i), "foo", "baz" + i);
        index("test-idx-3", "doc", Integer.toString(i), "foo", "baz" + i);
    }
    refresh();
    assertHitCount(client.prepareSearch("test-idx-1").setSize(0).get(), 100L);
    assertHitCount(client.prepareSearch("test-idx-2").setSize(0).get(), 100L);
    assertHitCount(client.prepareSearch("test-idx-3").setSize(0).get(), 100L);
    ListenableActionFuture<FlushResponse> flushResponseFuture = null;
    if (randomBoolean()) {
        ArrayList<String> indicesToFlush = new ArrayList<>();
        for (int i = 1; i < 4; i++) {
            if (randomBoolean()) {
                indicesToFlush.add("test-idx-" + i);
            }
        }
        if (!indicesToFlush.isEmpty()) {
            String[] indices = indicesToFlush.toArray(new String[indicesToFlush.size()]);
            logger.info("--> starting asynchronous flush for indices {}", Arrays.toString(indices));
            flushResponseFuture = client.admin().indices().prepareFlush(indices).execute();
        }
    }
    logger.info("--> snapshot");
    CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndices("test-idx-*", "-test-idx-3").get();
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));
    List<SnapshotInfo> snapshotInfos = client.admin().cluster().prepareGetSnapshots("test-repo").setSnapshots(randomFrom("test-snap", "_all", "*", "*-snap", "test*")).get().getSnapshots();
    assertThat(snapshotInfos.size(), equalTo(1));
    SnapshotInfo snapshotInfo = snapshotInfos.get(0);
    assertThat(snapshotInfo.state(), equalTo(SnapshotState.SUCCESS));
    assertThat(snapshotInfo.version(), equalTo(Version.CURRENT));
    logger.info("--> delete some data");
    for (int i = 0; i < 50; i++) {
        client.prepareDelete("test-idx-1", "doc", Integer.toString(i)).get();
    }
    for (int i = 50; i < 100; i++) {
        client.prepareDelete("test-idx-2", "doc", Integer.toString(i)).get();
    }
    for (int i = 0; i < 100; i += 2) {
        client.prepareDelete("test-idx-3", "doc", Integer.toString(i)).get();
    }
    assertAllSuccessful(refresh());
    assertHitCount(client.prepareSearch("test-idx-1").setSize(0).get(), 50L);
    assertHitCount(client.prepareSearch("test-idx-2").setSize(0).get(), 50L);
    assertHitCount(client.prepareSearch("test-idx-3").setSize(0).get(), 50L);
    logger.info("--> close indices");
    client.admin().indices().prepareClose("test-idx-1", "test-idx-2").get();
    logger.info("--> restore all indices from the snapshot");
    RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setWaitForCompletion(true).execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    ensureGreen();
    for (int i = 0; i < 5; i++) {
        assertHitCount(client.prepareSearch("test-idx-1").setSize(0).get(), 100L);
        assertHitCount(client.prepareSearch("test-idx-2").setSize(0).get(), 100L);
        assertHitCount(client.prepareSearch("test-idx-3").setSize(0).get(), 50L);
    }
    // Test restore after index deletion
    logger.info("--> delete indices");
    cluster().wipeIndices("test-idx-1", "test-idx-2");
    logger.info("--> restore one index after deletion");
    restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndices("test-idx-*", "-test-idx-2").execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    ensureGreen();
    for (int i = 0; i < 5; i++) {
        assertHitCount(client.prepareSearch("test-idx-1").setSize(0).get(), 100L);
    }
    ClusterState clusterState = client.admin().cluster().prepareState().get().getState();
    assertThat(clusterState.getMetaData().hasIndex("test-idx-1"), equalTo(true));
    assertThat(clusterState.getMetaData().hasIndex("test-idx-2"), equalTo(false));
    if (flushResponseFuture != null) {
        // Finish flush
        flushResponseFuture.actionGet();
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) FlushResponse(org.elasticsearch.action.admin.indices.flush.FlushResponse) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) RestoreSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse) CreateSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) Client(org.elasticsearch.client.Client)

Aggregations

Client (org.elasticsearch.client.Client)163 CreateSnapshotResponse (org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse)42 Settings (org.elasticsearch.common.settings.Settings)37 Path (java.nio.file.Path)30 RestoreSnapshotResponse (org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)28 PutRepositoryResponse (org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryResponse)24 ArrayList (java.util.ArrayList)23 ClusterHealthResponse (org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse)23 IOException (java.io.IOException)20 Matchers.containsString (org.hamcrest.Matchers.containsString)20 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)18 ClusterState (org.elasticsearch.cluster.ClusterState)17 ExecutionException (java.util.concurrent.ExecutionException)16 SearchResponse (org.elasticsearch.action.search.SearchResponse)14 ClusterAdminClient (org.elasticsearch.client.ClusterAdminClient)13 ClusterService (org.elasticsearch.cluster.service.ClusterService)10 List (java.util.List)9 ActionRequestValidationException (org.elasticsearch.action.ActionRequestValidationException)9 CloseIndexResponse (org.elasticsearch.action.admin.indices.close.CloseIndexResponse)9 OpenIndexResponse (org.elasticsearch.action.admin.indices.open.OpenIndexResponse)9