Search in sources :

Example 16 with Client

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

the class SharedClusterSnapshotRestoreIT method testReadonlyRepository.

// this fails every now and then: https://github.com/elastic/elasticsearch/issues/18121 but without
@TestLogging("_root:DEBUG")
public // more logs we cannot find out why
void testReadonlyRepository() throws Exception {
    Client client = client();
    logger.info("-->  creating repository");
    Path repositoryLocation = randomRepoPath();
    assertAcked(client.admin().cluster().preparePutRepository("test-repo").setType("fs").setSettings(Settings.builder().put("location", repositoryLocation).put("compress", randomBoolean()).put("chunk_size", randomIntBetween(100, 1000), ByteSizeUnit.BYTES)));
    createIndex("test-idx");
    ensureGreen();
    logger.info("--> indexing some data");
    for (int i = 0; i < 100; i++) {
        index("test-idx", "doc", Integer.toString(i), "foo", "bar" + i);
    }
    refresh();
    logger.info("--> snapshot");
    CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndices("test-idx").get();
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));
    assertThat(client.admin().cluster().prepareGetSnapshots("test-repo").setSnapshots("test-snap").get().getSnapshots().get(0).state(), equalTo(SnapshotState.SUCCESS));
    logger.info("--> delete index");
    cluster().wipeIndices("test-idx");
    logger.info("--> create read-only URL repository");
    assertAcked(client.admin().cluster().preparePutRepository("readonly-repo").setType("fs").setSettings(Settings.builder().put("location", repositoryLocation).put("compress", randomBoolean()).put("readonly", true).put("chunk_size", randomIntBetween(100, 1000), ByteSizeUnit.BYTES)));
    logger.info("--> restore index after deletion");
    RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("readonly-repo", "test-snap").setWaitForCompletion(true).setIndices("test-idx").execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    assertThat(client.prepareSearch("test-idx").setSize(0).get().getHits().getTotalHits(), equalTo(100L));
    logger.info("--> list available shapshots");
    GetSnapshotsResponse getSnapshotsResponse = client.admin().cluster().prepareGetSnapshots("readonly-repo").get();
    assertThat(getSnapshotsResponse.getSnapshots(), notNullValue());
    assertThat(getSnapshotsResponse.getSnapshots().size(), equalTo(1));
    logger.info("--> try deleting snapshot");
    assertThrows(client.admin().cluster().prepareDeleteSnapshot("readonly-repo", "test-snap"), RepositoryException.class, "cannot delete snapshot from a readonly repository");
    logger.info("--> try making another snapshot");
    assertThrows(client.admin().cluster().prepareCreateSnapshot("readonly-repo", "test-snap-2").setWaitForCompletion(true).setIndices("test-idx"), RepositoryException.class, "cannot create snapshot in a readonly repository");
}
Also used : Path(java.nio.file.Path) GetSnapshotsResponse(org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsResponse) CreateSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) Client(org.elasticsearch.client.Client) RestoreSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse) TestLogging(org.elasticsearch.test.junit.annotations.TestLogging)

Example 17 with Client

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

the class SharedClusterSnapshotRestoreIT method testRecreateBlocksOnRestore.

public void testRecreateBlocksOnRestore() 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)));
    Settings.Builder indexSettings = Settings.builder().put(indexSettings()).put(SETTING_NUMBER_OF_REPLICAS, between(0, 1)).put(INDEX_REFRESH_INTERVAL_SETTING.getKey(), "10s");
    logger.info("--> create index");
    assertAcked(prepareCreate("test-idx", 2, indexSettings));
    try {
        List<String> initialBlockSettings = randomSubsetOf(randomInt(3), IndexMetaData.SETTING_BLOCKS_WRITE, IndexMetaData.SETTING_BLOCKS_METADATA, IndexMetaData.SETTING_READ_ONLY);
        Settings.Builder initialSettingsBuilder = Settings.builder();
        for (String blockSetting : initialBlockSettings) {
            initialSettingsBuilder.put(blockSetting, true);
        }
        Settings initialSettings = initialSettingsBuilder.build();
        logger.info("--> using initial block settings {}", initialSettings.getAsMap());
        if (!initialSettings.isEmpty()) {
            logger.info("--> apply initial blocks to index");
            client().admin().indices().prepareUpdateSettings("test-idx").setSettings(initialSettingsBuilder).get();
        }
        logger.info("--> snapshot index");
        CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndices("test-idx").get();
        assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
        assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));
        logger.info("--> remove blocks and delete index");
        disableIndexBlock("test-idx", IndexMetaData.SETTING_BLOCKS_METADATA);
        disableIndexBlock("test-idx", IndexMetaData.SETTING_READ_ONLY);
        disableIndexBlock("test-idx", IndexMetaData.SETTING_BLOCKS_WRITE);
        disableIndexBlock("test-idx", IndexMetaData.SETTING_BLOCKS_READ);
        cluster().wipeIndices("test-idx");
        logger.info("--> restore index with additional block changes");
        List<String> changeBlockSettings = randomSubsetOf(randomInt(4), IndexMetaData.SETTING_BLOCKS_METADATA, IndexMetaData.SETTING_BLOCKS_WRITE, IndexMetaData.SETTING_READ_ONLY, IndexMetaData.SETTING_BLOCKS_READ);
        Settings.Builder changedSettingsBuilder = Settings.builder();
        for (String blockSetting : changeBlockSettings) {
            changedSettingsBuilder.put(blockSetting, randomBoolean());
        }
        Settings changedSettings = changedSettingsBuilder.build();
        logger.info("--> applying changed block settings {}", changedSettings.getAsMap());
        RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setIndexSettings(changedSettings).setWaitForCompletion(true).execute().actionGet();
        assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
        ClusterBlocks blocks = client.admin().cluster().prepareState().clear().setBlocks(true).get().getState().blocks();
        // compute current index settings (as we cannot query them if they contain SETTING_BLOCKS_METADATA)
        Settings mergedSettings = Settings.builder().put(initialSettings).put(changedSettings).build();
        logger.info("--> merged block settings {}", mergedSettings.getAsMap());
        logger.info("--> checking consistency between settings and blocks");
        assertThat(mergedSettings.getAsBoolean(IndexMetaData.SETTING_BLOCKS_METADATA, false), is(blocks.hasIndexBlock("test-idx", IndexMetaData.INDEX_METADATA_BLOCK)));
        assertThat(mergedSettings.getAsBoolean(IndexMetaData.SETTING_BLOCKS_READ, false), is(blocks.hasIndexBlock("test-idx", IndexMetaData.INDEX_READ_BLOCK)));
        assertThat(mergedSettings.getAsBoolean(IndexMetaData.SETTING_BLOCKS_WRITE, false), is(blocks.hasIndexBlock("test-idx", IndexMetaData.INDEX_WRITE_BLOCK)));
        assertThat(mergedSettings.getAsBoolean(IndexMetaData.SETTING_READ_ONLY, false), is(blocks.hasIndexBlock("test-idx", IndexMetaData.INDEX_READ_ONLY_BLOCK)));
    } finally {
        logger.info("--> cleaning up blocks");
        disableIndexBlock("test-idx", IndexMetaData.SETTING_BLOCKS_METADATA);
        disableIndexBlock("test-idx", IndexMetaData.SETTING_READ_ONLY);
        disableIndexBlock("test-idx", IndexMetaData.SETTING_BLOCKS_WRITE);
        disableIndexBlock("test-idx", IndexMetaData.SETTING_BLOCKS_READ);
    }
}
Also used : CreateSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) ClusterBlocks(org.elasticsearch.cluster.block.ClusterBlocks) 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 18 with Client

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

the class SharedClusterSnapshotRestoreIT method testDataFileCorruptionDuringRestore.

public void testDataFileCorruptionDuringRestore() throws Exception {
    Path repositoryLocation = randomRepoPath();
    Client client = client();
    logger.info("-->  creating repository");
    assertAcked(client.admin().cluster().preparePutRepository("test-repo").setType("fs").setSettings(Settings.builder().put("location", repositoryLocation)));
    createIndex("test-idx");
    ensureGreen();
    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));
    logger.info("--> snapshot");
    CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndices("test-idx").get();
    assertThat(createSnapshotResponse.getSnapshotInfo().state(), equalTo(SnapshotState.SUCCESS));
    assertThat(createSnapshotResponse.getSnapshotInfo().totalShards(), equalTo(createSnapshotResponse.getSnapshotInfo().successfulShards()));
    logger.info("-->  update repository with mock version");
    assertAcked(client.admin().cluster().preparePutRepository("test-repo").setType("mock").setSettings(Settings.builder().put("location", repositoryLocation).put("random", randomAsciiOfLength(10)).put("use_lucene_corruption", true).put("max_failure_number", 10000000L).put("random_data_file_io_exception_rate", 1.0)));
    // Test restore after index deletion
    logger.info("--> delete index");
    cluster().wipeIndices("test-idx");
    logger.info("--> restore corrupt index");
    RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setWaitForCompletion(true).execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    assertThat(restoreSnapshotResponse.getRestoreInfo().failedShards(), equalTo(restoreSnapshotResponse.getRestoreInfo().totalShards()));
}
Also used : Path(java.nio.file.Path) CreateSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) Client(org.elasticsearch.client.Client) RestoreSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)

Example 19 with Client

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

the class SharedClusterSnapshotRestoreIT method testSnapshotSingleClosedIndex.

public void testSnapshotSingleClosedIndex() 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");
    ensureGreen();
    logger.info("-->  closing index test-idx");
    assertAcked(client.admin().indices().prepareClose("test-idx"));
    logger.info("--> snapshot");
    assertBlocked(client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap-1").setWaitForCompletion(true).setIndices("test-idx"), MetaDataIndexStateService.INDEX_CLOSED_BLOCK);
}
Also used : Client(org.elasticsearch.client.Client)

Example 20 with Client

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

the class SharedClusterSnapshotRestoreIT method testSingleGetAfterRestore.

public void testSingleGetAfterRestore() throws Exception {
    String indexName = "testindex";
    String repoName = "test-restore-snapshot-repo";
    String snapshotName = "test-restore-snapshot";
    String absolutePath = randomRepoPath().toAbsolutePath().toString();
    logger.info("Path [{}]", absolutePath);
    String restoredIndexName = indexName + "-restored";
    String typeName = "actions";
    String expectedValue = "expected";
    Client client = client();
    // Write a document
    String docId = Integer.toString(randomInt());
    index(indexName, typeName, docId, "value", expectedValue);
    logger.info("-->  creating repository");
    assertAcked(client.admin().cluster().preparePutRepository(repoName).setType("fs").setSettings(Settings.builder().put("location", absolutePath)));
    logger.info("--> snapshot");
    CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot(repoName, snapshotName).setWaitForCompletion(true).setIndices(indexName).get();
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));
    assertThat(createSnapshotResponse.getSnapshotInfo().state(), equalTo(SnapshotState.SUCCESS));
    RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot(repoName, snapshotName).setWaitForCompletion(true).setRenamePattern(indexName).setRenameReplacement(restoredIndexName).get();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    assertThat(client.prepareGet(restoredIndexName, typeName, docId).get().isExists(), equalTo(true));
}
Also used : 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)

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