Search in sources :

Example 1 with SnapshotState

use of org.elasticsearch.snapshots.SnapshotState in project elasticsearch by elastic.

the class URLSnapshotRestoreTests method testUrlRepository.

public void testUrlRepository() throws Exception {
    Client client = client();
    logger.info("-->  creating repository");
    Path repositoryLocation = randomRepoPath();
    assertAcked(client.admin().cluster().preparePutRepository("test-repo").setType(FsRepository.TYPE).setSettings(Settings.builder().put(FsRepository.LOCATION_SETTING.getKey(), repositoryLocation).put(FsRepository.COMPRESS_SETTING.getKey(), randomBoolean()).put(FsRepository.CHUNK_SIZE_SETTING.getKey(), 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();
    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().successfulShards(), greaterThan(0));
    int actualTotalShards = createSnapshotResponse.getSnapshotInfo().totalShards();
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(actualTotalShards));
    SnapshotState state = client.admin().cluster().prepareGetSnapshots("test-repo").setSnapshots("test-snap").get().getSnapshots().get(0).state();
    assertThat(state, equalTo(SnapshotState.SUCCESS));
    logger.info("--> delete index");
    cluster().wipeIndices("test-idx");
    logger.info("--> create read-only URL repository");
    assertAcked(client.admin().cluster().preparePutRepository("url-repo").setType(URLRepository.TYPE).setSettings(Settings.builder().put(URLRepository.URL_SETTING.getKey(), repositoryLocation.toUri().toURL()).put("list_directories", randomBoolean())));
    logger.info("--> restore index after deletion");
    RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("url-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("url-repo").get();
    assertThat(getSnapshotsResponse.getSnapshots(), notNullValue());
    assertThat(getSnapshotsResponse.getSnapshots().size(), equalTo(1));
    logger.info("--> delete snapshot");
    DeleteSnapshotResponse deleteSnapshotResponse = client.admin().cluster().prepareDeleteSnapshot("test-repo", "test-snap").get();
    assertAcked(deleteSnapshotResponse);
    logger.info("--> list available shapshot again, no snapshots should be returned");
    getSnapshotsResponse = client.admin().cluster().prepareGetSnapshots("url-repo").get();
    assertThat(getSnapshotsResponse.getSnapshots(), notNullValue());
    assertThat(getSnapshotsResponse.getSnapshots().size(), equalTo(0));
}
Also used : Path(java.nio.file.Path) SnapshotState(org.elasticsearch.snapshots.SnapshotState) GetSnapshotsResponse(org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsResponse) CreateSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) Client(org.elasticsearch.client.Client) DeleteSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.delete.DeleteSnapshotResponse) RestoreSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)

Example 2 with SnapshotState

use of org.elasticsearch.snapshots.SnapshotState in project crate by crate.

the class RepositoryData method snapshotsFromXContent.

/**
 * Reads an instance of {@link RepositoryData} from x-content, loading the snapshots and indices metadata.
 */
public static RepositoryData snapshotsFromXContent(final XContentParser parser, long genId) throws IOException {
    final Map<String, SnapshotId> snapshots = new HashMap<>();
    final Map<String, SnapshotState> snapshotStates = new HashMap<>();
    final Map<String, Version> snapshotVersions = new HashMap<>();
    final Map<IndexId, Set<SnapshotId>> indexSnapshots = new HashMap<>();
    final ShardGenerations.Builder shardGenerations = ShardGenerations.builder();
    if (parser.nextToken() == XContentParser.Token.START_OBJECT) {
        while (parser.nextToken() == XContentParser.Token.FIELD_NAME) {
            String field = parser.currentName();
            if (SNAPSHOTS.equals(field)) {
                if (parser.nextToken() == XContentParser.Token.START_ARRAY) {
                    while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
                        String name = null;
                        String uuid = null;
                        SnapshotState state = null;
                        Version version = null;
                        while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
                            String currentFieldName = parser.currentName();
                            parser.nextToken();
                            if (NAME.equals(currentFieldName)) {
                                name = parser.text();
                            } else if (UUID.equals(currentFieldName)) {
                                uuid = parser.text();
                            } else if (STATE.equals(currentFieldName)) {
                                state = SnapshotState.fromValue(parser.numberValue().byteValue());
                            } else if (VERSION.equals(currentFieldName)) {
                                version = Version.fromString(parser.text());
                            }
                        }
                        final SnapshotId snapshotId = new SnapshotId(name, uuid);
                        if (state != null) {
                            snapshotStates.put(uuid, state);
                        }
                        if (version != null) {
                            snapshotVersions.put(uuid, version);
                        }
                        snapshots.put(snapshotId.getUUID(), snapshotId);
                    }
                } else {
                    throw new ElasticsearchParseException("expected array for [" + field + "]");
                }
            } else if (INDICES.equals(field)) {
                if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
                    throw new ElasticsearchParseException("start object expected [indices]");
                }
                while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
                    final String indexName = parser.currentName();
                    final Set<SnapshotId> snapshotIds = new LinkedHashSet<>();
                    final List<String> gens = new ArrayList<>();
                    IndexId indexId = null;
                    if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
                        throw new ElasticsearchParseException("start object expected index[" + indexName + "]");
                    }
                    while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
                        final String indexMetaFieldName = parser.currentName();
                        parser.nextToken();
                        if (INDEX_ID.equals(indexMetaFieldName)) {
                            indexId = new IndexId(indexName, parser.text());
                        } else if (SNAPSHOTS.equals(indexMetaFieldName)) {
                            if (parser.currentToken() != XContentParser.Token.START_ARRAY) {
                                throw new ElasticsearchParseException("start array expected [snapshots]");
                            }
                            while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
                                String uuid = null;
                                // the old format pre 5.4.1 which contains the snapshot name and uuid
                                if (parser.currentToken() == XContentParser.Token.START_OBJECT) {
                                    while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
                                        String currentFieldName = parser.currentName();
                                        parser.nextToken();
                                        if (UUID.equals(currentFieldName)) {
                                            uuid = parser.text();
                                        }
                                    }
                                } else {
                                    // the new format post 5.4.1 that only contains the snapshot uuid,
                                    // since we already have the name/uuid combo in the snapshots array
                                    uuid = parser.text();
                                }
                                SnapshotId snapshotId = snapshots.get(uuid);
                                if (snapshotId != null) {
                                    snapshotIds.add(snapshotId);
                                } else {
                                    // different versions create or delete snapshot in the same repository.
                                    throw new ElasticsearchParseException("Detected a corrupted repository, index " + indexId + " references an unknown snapshot uuid [" + uuid + "]");
                                }
                            }
                        } else if (SHARD_GENERATIONS.equals(indexMetaFieldName)) {
                            XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_ARRAY, parser.currentToken(), parser::getTokenLocation);
                            while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
                                gens.add(parser.textOrNull());
                            }
                        }
                    }
                    assert indexId != null;
                    indexSnapshots.put(indexId, snapshotIds);
                    for (int i = 0; i < gens.size(); i++) {
                        shardGenerations.put(indexId, i, gens.get(i));
                    }
                }
            } else {
                throw new ElasticsearchParseException("unknown field name  [" + field + "]");
            }
        }
    } else {
        throw new ElasticsearchParseException("start object expected");
    }
    return new RepositoryData(genId, snapshots, snapshotStates, snapshotVersions, indexSnapshots, shardGenerations.build());
}
Also used : Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) SnapshotId(org.elasticsearch.snapshots.SnapshotId) SnapshotState(org.elasticsearch.snapshots.SnapshotState) Version(org.elasticsearch.Version) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with SnapshotState

use of org.elasticsearch.snapshots.SnapshotState in project crate by crate.

the class RepositoryDataTests method testGetSnapshotState.

public void testGetSnapshotState() {
    final SnapshotId snapshotId = new SnapshotId(randomAlphaOfLength(8), UUIDs.randomBase64UUID());
    final SnapshotState state = randomFrom(SnapshotState.values());
    final RepositoryData repositoryData = RepositoryData.EMPTY.addSnapshot(snapshotId, state, randomFrom(Version.CURRENT, Version.CURRENT.minimumCompatibilityVersion()), ShardGenerations.EMPTY);
    assertEquals(state, repositoryData.getSnapshotState(snapshotId));
    assertNull(repositoryData.getSnapshotState(new SnapshotId(randomAlphaOfLength(8), UUIDs.randomBase64UUID())));
}
Also used : SnapshotId(org.elasticsearch.snapshots.SnapshotId) SnapshotState(org.elasticsearch.snapshots.SnapshotState)

Example 4 with SnapshotState

use of org.elasticsearch.snapshots.SnapshotState in project crate by crate.

the class RepositoryDataTests method testInitIndices.

public void testInitIndices() {
    final int numSnapshots = randomIntBetween(1, 30);
    final Map<String, SnapshotId> snapshotIds = new HashMap<>(numSnapshots);
    final Map<String, SnapshotState> snapshotStates = new HashMap<>(numSnapshots);
    final Map<String, Version> snapshotVersions = new HashMap<>(numSnapshots);
    for (int i = 0; i < numSnapshots; i++) {
        final SnapshotId snapshotId = new SnapshotId(randomAlphaOfLength(8), UUIDs.randomBase64UUID());
        snapshotIds.put(snapshotId.getUUID(), snapshotId);
        snapshotStates.put(snapshotId.getUUID(), randomFrom(SnapshotState.values()));
        snapshotVersions.put(snapshotId.getUUID(), randomFrom(Version.CURRENT, Version.CURRENT.minimumCompatibilityVersion()));
    }
    RepositoryData repositoryData = new RepositoryData(EMPTY_REPO_GEN, snapshotIds, Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap(), ShardGenerations.EMPTY);
    // test that initializing indices works
    Map<IndexId, Set<SnapshotId>> indices = randomIndices(snapshotIds);
    RepositoryData newRepoData = new RepositoryData(repositoryData.getGenId(), snapshotIds, snapshotStates, snapshotVersions, indices, ShardGenerations.EMPTY);
    List<SnapshotId> expected = new ArrayList<>(repositoryData.getSnapshotIds());
    Collections.sort(expected);
    List<SnapshotId> actual = new ArrayList<>(newRepoData.getSnapshotIds());
    Collections.sort(actual);
    assertEquals(expected, actual);
    for (IndexId indexId : indices.keySet()) {
        assertEquals(indices.get(indexId), newRepoData.getSnapshots(indexId));
    }
}
Also used : HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SnapshotId(org.elasticsearch.snapshots.SnapshotId) SnapshotState(org.elasticsearch.snapshots.SnapshotState) Version(org.elasticsearch.Version)

Example 5 with SnapshotState

use of org.elasticsearch.snapshots.SnapshotState in project crate by crate.

the class RepositoryDataTests method testIndexThatReferencesAnUnknownSnapshot.

public void testIndexThatReferencesAnUnknownSnapshot() throws IOException {
    final XContent xContent = randomFrom(XContentType.values()).xContent();
    final RepositoryData repositoryData = generateRandomRepoData();
    XContentBuilder builder = XContentBuilder.builder(xContent);
    repositoryData.snapshotsToXContent(builder, true);
    RepositoryData parsedRepositoryData;
    try (XContentParser xParser = createParser(builder)) {
        parsedRepositoryData = RepositoryData.snapshotsFromXContent(xParser, repositoryData.getGenId());
    }
    assertEquals(repositoryData, parsedRepositoryData);
    Map<String, SnapshotId> snapshotIds = new HashMap<>();
    Map<String, SnapshotState> snapshotStates = new HashMap<>();
    Map<String, Version> snapshotVersions = new HashMap<>();
    for (SnapshotId snapshotId : parsedRepositoryData.getSnapshotIds()) {
        snapshotIds.put(snapshotId.getUUID(), snapshotId);
        snapshotStates.put(snapshotId.getUUID(), parsedRepositoryData.getSnapshotState(snapshotId));
        snapshotVersions.put(snapshotId.getUUID(), parsedRepositoryData.getVersion(snapshotId));
    }
    final IndexId corruptedIndexId = randomFrom(parsedRepositoryData.getIndices().values());
    Map<IndexId, Set<SnapshotId>> indexSnapshots = new HashMap<>();
    final ShardGenerations.Builder shardGenBuilder = ShardGenerations.builder();
    for (Map.Entry<String, IndexId> snapshottedIndex : parsedRepositoryData.getIndices().entrySet()) {
        IndexId indexId = snapshottedIndex.getValue();
        Set<SnapshotId> snapshotsIds = new LinkedHashSet<>(parsedRepositoryData.getSnapshots(indexId));
        if (corruptedIndexId.equals(indexId)) {
            snapshotsIds.add(new SnapshotId("_uuid", "_does_not_exist"));
        }
        indexSnapshots.put(indexId, snapshotsIds);
        final int shardCount = randomIntBetween(1, 10);
        for (int i = 0; i < shardCount; ++i) {
            shardGenBuilder.put(indexId, i, UUIDs.randomBase64UUID(random()));
        }
    }
    assertNotNull(corruptedIndexId);
    RepositoryData corruptedRepositoryData = new RepositoryData(parsedRepositoryData.getGenId(), snapshotIds, snapshotStates, snapshotVersions, indexSnapshots, shardGenBuilder.build());
    final XContentBuilder corruptedBuilder = XContentBuilder.builder(xContent);
    corruptedRepositoryData.snapshotsToXContent(corruptedBuilder, true);
    try (XContentParser xParser = createParser(corruptedBuilder)) {
        ElasticsearchParseException e = expectThrows(ElasticsearchParseException.class, () -> RepositoryData.snapshotsFromXContent(xParser, corruptedRepositoryData.getGenId()));
        assertThat(e.getMessage(), equalTo("Detected a corrupted repository, index " + corruptedIndexId + " references an unknown " + "snapshot uuid [_does_not_exist]"));
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) HashMap(java.util.HashMap) SnapshotId(org.elasticsearch.snapshots.SnapshotId) SnapshotState(org.elasticsearch.snapshots.SnapshotState) Version(org.elasticsearch.Version) XContent(org.elasticsearch.common.xcontent.XContent) JsonXContent(org.elasticsearch.common.xcontent.json.JsonXContent) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) HashMap(java.util.HashMap) Map(java.util.Map) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Aggregations

SnapshotState (org.elasticsearch.snapshots.SnapshotState)5 SnapshotId (org.elasticsearch.snapshots.SnapshotId)4 HashMap (java.util.HashMap)3 LinkedHashSet (java.util.LinkedHashSet)3 Set (java.util.Set)3 Version (org.elasticsearch.Version)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)2 Path (java.nio.file.Path)1 List (java.util.List)1 Map (java.util.Map)1 CreateSnapshotResponse (org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse)1 DeleteSnapshotResponse (org.elasticsearch.action.admin.cluster.snapshots.delete.DeleteSnapshotResponse)1 GetSnapshotsResponse (org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsResponse)1 RestoreSnapshotResponse (org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)1 Client (org.elasticsearch.client.Client)1 XContent (org.elasticsearch.common.xcontent.XContent)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1 XContentParser (org.elasticsearch.common.xcontent.XContentParser)1