Search in sources :

Example 16 with Repository

use of org.elasticsearch.repositories.Repository in project crate by crate.

the class BlobStoreRepositoryRestoreTests method testSnapshotWithConflictingName.

public void testSnapshotWithConflictingName() throws IOException {
    final IndexId indexId = new IndexId(randomAlphaOfLength(10), UUIDs.randomBase64UUID());
    final ShardId shardId = new ShardId(indexId.getName(), indexId.getId(), 0);
    IndexShard shard = newShard(shardId, true);
    try {
        // index documents in the shards
        final int numDocs = scaledRandomIntBetween(1, 500);
        recoverShardFromStore(shard);
        for (int i = 0; i < numDocs; i++) {
            indexDoc(shard, Integer.toString(i));
            if (rarely()) {
                flushShard(shard, false);
            }
        }
        assertDocCount(shard, numDocs);
        // snapshot the shard
        final Repository repository = createRepository();
        final Snapshot snapshot = new Snapshot(repository.getMetadata().name(), new SnapshotId(randomAlphaOfLength(10), "_uuid"));
        final String shardGen = snapshotShard(shard, snapshot, repository);
        assertNotNull(shardGen);
        final Snapshot snapshotWithSameName = new Snapshot(repository.getMetadata().name(), new SnapshotId(snapshot.getSnapshotId().getName(), "_uuid2"));
        final PlainActionFuture<SnapshotInfo> future = PlainActionFuture.newFuture();
        repository.finalizeSnapshot(snapshot.getSnapshotId(), ShardGenerations.builder().put(indexId, 0, shardGen).build(), 0L, null, 1, Collections.emptyList(), -1L, false, Metadata.builder().put(shard.indexSettings().getIndexMetadata(), false).build(), true, future);
        future.actionGet();
        IndexShardSnapshotFailedException isfe = expectThrows(IndexShardSnapshotFailedException.class, () -> snapshotShard(shard, snapshotWithSameName, repository));
        assertThat(isfe.getMessage(), containsString("Duplicate snapshot name"));
    } finally {
        if (shard != null && shard.state() != IndexShardState.CLOSED) {
            try {
                shard.close("test", false);
            } finally {
                IOUtils.close(shard.store());
            }
        }
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) IndexId(org.elasticsearch.repositories.IndexId) Snapshot(org.elasticsearch.snapshots.Snapshot) SnapshotId(org.elasticsearch.snapshots.SnapshotId) Repository(org.elasticsearch.repositories.Repository) FsRepository(org.elasticsearch.repositories.fs.FsRepository) SnapshotInfo(org.elasticsearch.snapshots.SnapshotInfo) IndexShard(org.elasticsearch.index.shard.IndexShard) IndexShardSnapshotFailedException(org.elasticsearch.index.snapshots.IndexShardSnapshotFailedException) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 17 with Repository

use of org.elasticsearch.repositories.Repository in project crate by crate.

the class CorruptedBlobStoreRepositoryIT method testFindDanglingLatestGeneration.

public void testFindDanglingLatestGeneration() throws Exception {
    Path repo = randomRepoPath();
    final String repoName = "test";
    logger.info("-->  creating repository at {}", repo.toAbsolutePath());
    execute("CREATE REPOSITORY test TYPE fs with (location=?, compress=false, chunk_size=?)", new Object[] { repo.toAbsolutePath().toString(), randomIntBetween(100, 1000) + ByteSizeUnit.BYTES.getSuffix() });
    execute("create table doc.test1(x integer)");
    execute("create table doc.test2(x integer)");
    logger.info("--> indexing some data");
    execute("insert into doc.test1 values(1),(2)");
    execute("insert into doc.test2 values(1),(2)");
    final String snapshot = "snapshot1";
    logger.info("--> creating snapshot");
    CreateSnapshotResponse createSnapshotResponse = client().admin().cluster().prepareCreateSnapshot(repoName, snapshot).setWaitForCompletion(true).setIndices("test*").get();
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));
    final Repository repository = internalCluster().getCurrentMasterNodeInstance(RepositoriesService.class).repository(repoName);
    logger.info("--> move index-N blob to next generation");
    final RepositoryData repositoryData = getRepositoryData(repository);
    final long beforeMoveGen = repositoryData.getGenId();
    Files.move(repo.resolve("index-" + beforeMoveGen), repo.resolve("index-" + (beforeMoveGen + 1)));
    logger.info("--> set next generation as pending in the cluster state");
    final PlainActionFuture<Void> csUpdateFuture = PlainActionFuture.newFuture();
    internalCluster().getCurrentMasterNodeInstance(ClusterService.class).submitStateUpdateTask("set pending generation", new ClusterStateUpdateTask() {

        @Override
        public ClusterState execute(ClusterState currentState) {
            return ClusterState.builder(currentState).metadata(Metadata.builder(currentState.getMetadata()).putCustom(RepositoriesMetadata.TYPE, currentState.metadata().<RepositoriesMetadata>custom(RepositoriesMetadata.TYPE).withUpdatedGeneration(repository.getMetadata().name(), beforeMoveGen, beforeMoveGen + 1)).build()).build();
        }

        @Override
        public void onFailure(String source, Exception e) {
            csUpdateFuture.onFailure(e);
        }

        @Override
        public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
            csUpdateFuture.onResponse(null);
        }
    });
    csUpdateFuture.get();
    logger.info("--> full cluster restart");
    internalCluster().fullRestart();
    ensureGreen();
    Repository repositoryAfterRestart = internalCluster().getCurrentMasterNodeInstance(RepositoriesService.class).repository(repoName);
    logger.info("--> verify index-N blob is found at the new location");
    assertThat(getRepositoryData(repositoryAfterRestart).getGenId(), is(beforeMoveGen + 1));
    logger.info("--> delete snapshot");
    execute("drop snapshot test.snapshot1");
    logger.info("--> verify index-N blob is found at the expected location");
    assertThat(getRepositoryData(repositoryAfterRestart).getGenId(), is(beforeMoveGen + 2));
    logger.info("--> make sure snapshot doesn't exist");
    expectThrows(SnapshotMissingException.class, () -> client().admin().cluster().prepareGetSnapshots(repoName).addSnapshots(snapshot).get().getSnapshots());
}
Also used : Path(java.nio.file.Path) ClusterState(org.elasticsearch.cluster.ClusterState) ClusterStateUpdateTask(org.elasticsearch.cluster.ClusterStateUpdateTask) Matchers.containsString(org.hamcrest.Matchers.containsString) RepositoryException(org.elasticsearch.repositories.RepositoryException) RepositoryData(org.elasticsearch.repositories.RepositoryData) RepositoriesMetadata(org.elasticsearch.cluster.metadata.RepositoriesMetadata) Repository(org.elasticsearch.repositories.Repository) BlobStoreRepository(org.elasticsearch.repositories.blobstore.BlobStoreRepository) ClusterService(org.elasticsearch.cluster.service.ClusterService) CreateSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) RepositoriesService(org.elasticsearch.repositories.RepositoriesService)

Example 18 with Repository

use of org.elasticsearch.repositories.Repository in project crate by crate.

the class CorruptedBlobStoreRepositoryIT method testHandlingMissingRootLevelSnapshotMetadata.

public void testHandlingMissingRootLevelSnapshotMetadata() throws Exception {
    Path repo = randomRepoPath();
    final String repoName = "test";
    logger.info("-->  creating repository at {}", repo.toAbsolutePath());
    execute("CREATE REPOSITORY test TYPE fs with (location=?, compress=false, chunk_size=?)", new Object[] { repo.toAbsolutePath().toString(), randomIntBetween(100, 1000) + ByteSizeUnit.BYTES.getSuffix() });
    final String snapshotPrefix = "test-snap-";
    final int snapshots = randomIntBetween(1, 2);
    logger.info("--> creating [{}] snapshots", snapshots);
    for (int i = 0; i < snapshots; ++i) {
        // Workaround to simulate BwC situation: taking a snapshot without indices here so that we don't create any new version shard
        // generations (the existence of which would short-circuit checks for the repo containing old version snapshots)
        CreateSnapshotResponse createSnapshotResponse = client().admin().cluster().prepareCreateSnapshot(repoName, snapshotPrefix + i).setIndices().setWaitForCompletion(true).get();
        assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), is(0));
        assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));
    }
    final Repository repository = internalCluster().getCurrentMasterNodeInstance(RepositoriesService.class).repository(repoName);
    final RepositoryData repositoryData = getRepositoryData(repository);
    final SnapshotId snapshotToCorrupt = randomFrom(repositoryData.getSnapshotIds());
    logger.info("--> delete root level snapshot metadata blob for snapshot [{}]", snapshotToCorrupt);
    Files.delete(repo.resolve(String.format(Locale.ROOT, BlobStoreRepository.SNAPSHOT_NAME_FORMAT, snapshotToCorrupt.getUUID())));
    logger.info("--> strip version information from index-N blob");
    final RepositoryData withoutVersions = new RepositoryData(repositoryData.getGenId(), repositoryData.getSnapshotIds().stream().collect(Collectors.toMap(SnapshotId::getUUID, Function.identity())), repositoryData.getSnapshotIds().stream().collect(Collectors.toMap(SnapshotId::getUUID, repositoryData::getSnapshotState)), Collections.emptyMap(), Collections.emptyMap(), ShardGenerations.EMPTY);
    Files.write(repo.resolve(BlobStoreRepository.INDEX_FILE_PREFIX + withoutVersions.getGenId()), BytesReference.toBytes(BytesReference.bytes(withoutVersions.snapshotsToXContent(XContentFactory.jsonBuilder(), true))), StandardOpenOption.TRUNCATE_EXISTING);
    logger.info("--> verify that repo is assumed in old metadata format");
    final SnapshotsService snapshotsService = internalCluster().getCurrentMasterNodeInstance(SnapshotsService.class);
    final ThreadPool threadPool = internalCluster().getCurrentMasterNodeInstance(ThreadPool.class);
    assertThat(PlainActionFuture.get(f -> threadPool.generic().execute(ActionRunnable.wrap(f, (d) -> snapshotsService.hasOldVersionSnapshots(repoName, getRepositoryData(repository), d, null)))), is(true));
    logger.info("--> verify that snapshot with missing root level metadata can be deleted");
    client().admin().cluster().prepareDeleteSnapshot(repoName, snapshotToCorrupt.getName()).get();
    logger.info("--> verify that repository is assumed in new metadata format after removing corrupted snapshot");
    assertThat(PlainActionFuture.get(f -> threadPool.generic().execute(ActionRunnable.wrap(f, (d) -> snapshotsService.hasOldVersionSnapshots(repoName, getRepositoryData(repository), d, null)))), is(false));
    final RepositoryData finalRepositoryData = getRepositoryData(repository);
    for (SnapshotId snapshotId : finalRepositoryData.getSnapshotIds()) {
        assertThat(finalRepositoryData.getVersion(snapshotId), is(Version.CURRENT));
    }
}
Also used : Path(java.nio.file.Path) XContentFactory(org.elasticsearch.common.xcontent.XContentFactory) ByteSizeUnit(org.elasticsearch.common.unit.ByteSizeUnit) ShardGenerations(org.elasticsearch.repositories.ShardGenerations) ClusterService(org.elasticsearch.cluster.service.ClusterService) Function(java.util.function.Function) ClusterState(org.elasticsearch.cluster.ClusterState) Metadata(org.elasticsearch.cluster.metadata.Metadata) ClusterStateUpdateTask(org.elasticsearch.cluster.ClusterStateUpdateTask) RepositoriesMetadata(org.elasticsearch.cluster.metadata.RepositoriesMetadata) Locale(java.util.Locale) ThreadPool(org.elasticsearch.threadpool.ThreadPool) ActionRunnable(org.elasticsearch.action.ActionRunnable) Path(java.nio.file.Path) RepositoryException(org.elasticsearch.repositories.RepositoryException) Repository(org.elasticsearch.repositories.Repository) Files(java.nio.file.Files) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) Client(org.elasticsearch.client.Client) StandardOpenOption(java.nio.file.StandardOpenOption) BytesReference(org.elasticsearch.common.bytes.BytesReference) RepositoriesService(org.elasticsearch.repositories.RepositoriesService) Collectors(java.util.stream.Collectors) BlobStoreRepository(org.elasticsearch.repositories.blobstore.BlobStoreRepository) Version(org.elasticsearch.Version) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) CreateSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) RepositoryData(org.elasticsearch.repositories.RepositoryData) Repository(org.elasticsearch.repositories.Repository) BlobStoreRepository(org.elasticsearch.repositories.blobstore.BlobStoreRepository) CreateSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) RepositoriesService(org.elasticsearch.repositories.RepositoriesService) ThreadPool(org.elasticsearch.threadpool.ThreadPool) Matchers.containsString(org.hamcrest.Matchers.containsString) RepositoryData(org.elasticsearch.repositories.RepositoryData)

Example 19 with Repository

use of org.elasticsearch.repositories.Repository in project crate by crate.

the class CorruptedBlobStoreRepositoryIT method testCorruptedSnapshotIsIgnored.

public void testCorruptedSnapshotIsIgnored() throws Exception {
    Path repo = randomRepoPath();
    final String repoName = "test";
    logger.info("-->  creating repository at {}", repo.toAbsolutePath());
    execute("CREATE REPOSITORY test TYPE fs with (location=?, compress=false, chunk_size=?)", new Object[] { repo.toAbsolutePath().toString(), randomIntBetween(100, 1000) + ByteSizeUnit.BYTES.getSuffix() });
    final String snapshotPrefix = "test-snap-";
    final int snapshots = randomIntBetween(2, 2);
    logger.info("--> creating [{}] snapshots", snapshots);
    for (int i = 0; i < snapshots; ++i) {
        CreateSnapshotResponse createSnapshotResponse = client().admin().cluster().prepareCreateSnapshot(repoName, snapshotPrefix + i).setIndices().setWaitForCompletion(true).get();
        assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), is(0));
        assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));
    }
    final Repository repository = internalCluster().getCurrentMasterNodeInstance(RepositoriesService.class).repository(repoName);
    final RepositoryData repositoryData = getRepositoryData(repository);
    final SnapshotId snapshotToCorrupt = randomFrom(repositoryData.getSnapshotIds());
    logger.info("--> delete root level snapshot metadata blob for snapshot [{}]", snapshotToCorrupt);
    Files.delete(repo.resolve(String.format(Locale.ROOT, BlobStoreRepository.SNAPSHOT_NAME_FORMAT, snapshotToCorrupt.getUUID())));
    logger.info("--> ensure snapshot list can be retrieved without any error if ignoreUnavailable is set");
    var resp = client().admin().cluster().prepareGetSnapshots(repoName).setIgnoreUnavailable(true).get();
    assertThat(resp.getSnapshots().size(), is(snapshots - 1));
    client().admin().cluster().prepareDeleteSnapshot(repoName, snapshotToCorrupt.getName()).get();
}
Also used : Path(java.nio.file.Path) Repository(org.elasticsearch.repositories.Repository) BlobStoreRepository(org.elasticsearch.repositories.blobstore.BlobStoreRepository) CreateSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) RepositoriesService(org.elasticsearch.repositories.RepositoriesService) Matchers.containsString(org.hamcrest.Matchers.containsString) RepositoryData(org.elasticsearch.repositories.RepositoryData)

Example 20 with Repository

use of org.elasticsearch.repositories.Repository in project crate by crate.

the class SysSnapshotsTest method test_current_snapshot_does_not_fail_if_get_repository_data_raises_exception.

@Test
public void test_current_snapshot_does_not_fail_if_get_repository_data_raises_exception() throws Exception {
    Repository r1 = mock(Repository.class);
    Mockito.doThrow(new IllegalStateException("some error")).when(r1).getRepositoryData(any());
    SysSnapshots sysSnapshots = new SysSnapshots(() -> List.of(r1));
    CompletableFuture<Iterable<SysSnapshot>> currentSnapshots = sysSnapshots.currentSnapshots();
    Iterable<SysSnapshot> iterable = currentSnapshots.get(5, TimeUnit.SECONDS);
    assertThat(iterable.iterator().hasNext(), is(false));
}
Also used : Repository(org.elasticsearch.repositories.Repository) Test(org.junit.Test)

Aggregations

Repository (org.elasticsearch.repositories.Repository)33 RepositoryData (org.elasticsearch.repositories.RepositoryData)21 ArrayList (java.util.ArrayList)20 ShardId (org.elasticsearch.index.shard.ShardId)20 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)18 List (java.util.List)17 Collectors (java.util.stream.Collectors)17 ClusterState (org.elasticsearch.cluster.ClusterState)17 ClusterStateUpdateTask (org.elasticsearch.cluster.ClusterStateUpdateTask)17 IndexId (org.elasticsearch.repositories.IndexId)17 IOException (java.io.IOException)16 Collections (java.util.Collections)16 Set (java.util.Set)16 ActionListener (org.elasticsearch.action.ActionListener)16 ClusterService (org.elasticsearch.cluster.service.ClusterService)16 UUIDs (org.elasticsearch.common.UUIDs)16 Index (org.elasticsearch.index.Index)16 Settings (org.elasticsearch.common.settings.Settings)15 Map (java.util.Map)14 Logger (org.apache.logging.log4j.Logger)14