Search in sources :

Example 41 with IndexId

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

the class SnapshotsService method shards.

/**
 * Calculates the list of shards that should be included into the current snapshot
 *
 * @param clusterState cluster state
 * @param snapshot     SnapshotsInProgress Entry
 * @return list of shard to be included into current snapshot
 */
private static ImmutableOpenMap<ShardId, SnapshotsInProgress.ShardSnapshotStatus> shards(ClusterState clusterState, SnapshotsInProgress.Entry snapshot, RepositoryData repositoryData) {
    ImmutableOpenMap.Builder<ShardId, SnapshotsInProgress.ShardSnapshotStatus> builder = ImmutableOpenMap.builder();
    Metadata metadata = clusterState.metadata();
    final ShardGenerations shardGenerations = repositoryData.shardGenerations();
    for (IndexId index : snapshot.indices()) {
        final String indexName = index.getName();
        final boolean isNewIndex = repositoryData.getIndices().containsKey(indexName) == false;
        IndexMetadata indexMetadata = metadata.index(indexName);
        if (indexMetadata == null) {
            // The index was deleted before we managed to start the snapshot - mark it as missing.
            builder.put(new ShardId(indexName, IndexMetadata.INDEX_UUID_NA_VALUE, 0), new SnapshotsInProgress.ShardSnapshotStatus(null, ShardState.MISSING, "missing index", null));
        } else {
            IndexRoutingTable indexRoutingTable = clusterState.getRoutingTable().index(indexName);
            for (int i = 0; i < indexMetadata.getNumberOfShards(); i++) {
                ShardId shardId = new ShardId(indexMetadata.getIndex(), i);
                final String shardRepoGeneration;
                if (snapshot.useShardGenerations()) {
                    if (isNewIndex) {
                        assert shardGenerations.getShardGen(index, shardId.getId()) == null : "Found shard generation for new index [" + index + "]";
                        shardRepoGeneration = ShardGenerations.NEW_SHARD_GEN;
                    } else {
                        shardRepoGeneration = shardGenerations.getShardGen(index, shardId.getId());
                    }
                } else {
                    shardRepoGeneration = null;
                }
                if (indexRoutingTable != null) {
                    ShardRouting primary = indexRoutingTable.shard(i).primaryShard();
                    if (primary == null || !primary.assignedToNode()) {
                        builder.put(shardId, new SnapshotsInProgress.ShardSnapshotStatus(null, ShardState.MISSING, "primary shard is not allocated", shardRepoGeneration));
                    } else if (primary.relocating() || primary.initializing()) {
                        builder.put(shardId, new SnapshotsInProgress.ShardSnapshotStatus(primary.currentNodeId(), ShardState.WAITING, shardRepoGeneration));
                    } else if (!primary.started()) {
                        builder.put(shardId, new SnapshotsInProgress.ShardSnapshotStatus(primary.currentNodeId(), ShardState.MISSING, "primary shard hasn't been started yet", shardRepoGeneration));
                    } else {
                        builder.put(shardId, new SnapshotsInProgress.ShardSnapshotStatus(primary.currentNodeId(), shardRepoGeneration));
                    }
                } else {
                    builder.put(shardId, new SnapshotsInProgress.ShardSnapshotStatus(null, ShardState.MISSING, "missing routing table"));
                }
            }
        }
    }
    return builder.build();
}
Also used : IndexId(org.elasticsearch.repositories.IndexId) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) ShardSnapshotStatus(org.elasticsearch.cluster.SnapshotsInProgress.ShardSnapshotStatus) ShardGenerations(org.elasticsearch.repositories.ShardGenerations) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) Metadata(org.elasticsearch.cluster.metadata.Metadata) RepositoriesMetadata(org.elasticsearch.cluster.metadata.RepositoriesMetadata) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) ShardId(org.elasticsearch.index.shard.ShardId) SnapshotsInProgress(org.elasticsearch.cluster.SnapshotsInProgress) ShardSnapshotStatus(org.elasticsearch.cluster.SnapshotsInProgress.ShardSnapshotStatus) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 42 with IndexId

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

the class SnapshotsService method buildGenerations.

private static ShardGenerations buildGenerations(SnapshotsInProgress.Entry snapshot, Metadata metadata) {
    ShardGenerations.Builder builder = ShardGenerations.builder();
    final Map<String, IndexId> indexLookup = new HashMap<>();
    snapshot.indices().forEach(idx -> indexLookup.put(idx.getName(), idx));
    snapshot.shards().forEach(c -> {
        if (metadata.index(c.key.getIndex()) == null) {
            assert snapshot.partial() : "Index [" + c.key.getIndex() + "] was deleted during a snapshot but snapshot was not partial.";
            return;
        }
        final IndexId indexId = indexLookup.get(c.key.getIndexName());
        if (indexId != null) {
            builder.put(indexId, c.key.id(), c.value.generation());
        }
    });
    return builder.build();
}
Also used : IndexId(org.elasticsearch.repositories.IndexId) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ShardGenerations(org.elasticsearch.repositories.ShardGenerations)

Example 43 with IndexId

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

the class BlobStoreTestUtil method assertSnapshotUUIDs.

private static void assertSnapshotUUIDs(BlobStoreRepository repository, RepositoryData repositoryData) throws IOException {
    final BlobContainer repoRoot = repository.blobContainer();
    final Collection<SnapshotId> snapshotIds = repositoryData.getSnapshotIds();
    final List<String> expectedSnapshotUUIDs = snapshotIds.stream().map(SnapshotId::getUUID).collect(Collectors.toList());
    for (String prefix : new String[] { "snap-", "meta-" }) {
        final Collection<String> foundSnapshotUUIDs = repoRoot.listBlobs().keySet().stream().filter(p -> p.startsWith(prefix)).map(p -> p.replace(prefix, "").replace(".dat", "")).collect(Collectors.toSet());
        assertThat(foundSnapshotUUIDs, containsInAnyOrder(expectedSnapshotUUIDs.toArray(Strings.EMPTY_ARRAY)));
    }
    final BlobContainer indicesContainer = repository.blobContainer().children().get("indices");
    final Map<String, BlobContainer> indices;
    if (indicesContainer == null) {
        indices = Collections.emptyMap();
    } else {
        indices = indicesContainer.children();
    }
    final Map<IndexId, Integer> maxShardCountsExpected = new HashMap<>();
    final Map<IndexId, Integer> maxShardCountsSeen = new HashMap<>();
    // Assert that for each snapshot, the relevant metadata was written to index and shard folders
    for (SnapshotId snapshotId : snapshotIds) {
        repository.getSnapshotInfo(snapshotId, ActionListener.wrap(snapshotInfo -> {
            for (String index : snapshotInfo.indices()) {
                final IndexId indexId = repositoryData.resolveIndexId(index);
                assertThat(indices, hasKey(indexId.getId()));
                final BlobContainer indexContainer = indices.get(indexId.getId());
                assertThat(indexContainer.listBlobs(), hasKey(String.format(Locale.ROOT, BlobStoreRepository.METADATA_NAME_FORMAT, snapshotId.getUUID())));
                final IndexMetadata indexMetadata = PlainActionFuture.get(x -> repository.getSnapshotIndexMetadata(snapshotId, indexId, x));
                for (Map.Entry<String, BlobContainer> entry : indexContainer.children().entrySet()) {
                    // Skip Lucene MockFS extraN directory
                    if (entry.getKey().startsWith("extra")) {
                        continue;
                    }
                    final int shardId = Integer.parseInt(entry.getKey());
                    final int shardCount = indexMetadata.getNumberOfShards();
                    maxShardCountsExpected.compute(indexId, (i, existing) -> existing == null || existing < shardCount ? shardCount : existing);
                    final BlobContainer shardContainer = entry.getValue();
                    // becomes unreferenced. We should fix that and remove this conditional once its fixed.
                    if (shardContainer.listBlobs().keySet().stream().anyMatch(blob -> blob.startsWith("extra") == false)) {
                        final int impliedCount = shardId - 1;
                        maxShardCountsSeen.compute(indexId, (i, existing) -> existing == null || existing < impliedCount ? impliedCount : existing);
                    }
                    if (shardId < shardCount && snapshotInfo.shardFailures().stream().noneMatch(shardFailure -> shardFailure.index().equals(index) && shardFailure.shardId() == shardId)) {
                        final Map<String, BlobMetadata> shardPathContents = shardContainer.listBlobs();
                        assertThat(shardPathContents, hasKey(String.format(Locale.ROOT, BlobStoreRepository.SNAPSHOT_NAME_FORMAT, snapshotId.getUUID())));
                        assertThat(shardPathContents.keySet().stream().filter(name -> name.startsWith(BlobStoreRepository.INDEX_FILE_PREFIX)).count(), lessThanOrEqualTo(2L));
                    }
                }
            }
        }, (e) -> {
        }));
    }
    ;
    maxShardCountsSeen.forEach(((indexId, count) -> assertThat("Found unreferenced shard paths for index [" + indexId + "]", count, lessThanOrEqualTo(maxShardCountsExpected.get(indexId)))));
}
Also used : NoSuchFileException(java.nio.file.NoSuchFileException) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) ClusterApplierService(org.elasticsearch.cluster.service.ClusterApplierService) Matchers.not(org.hamcrest.Matchers.not) Matchers.hasKey(org.hamcrest.Matchers.hasKey) RepositoryMetadata(org.elasticsearch.cluster.metadata.RepositoryMetadata) Assert.assertThat(org.junit.Assert.assertThat) ClusterState(org.elasticsearch.cluster.ClusterState) ClusterStateUpdateTask(org.elasticsearch.cluster.ClusterStateUpdateTask) Locale(java.util.Locale) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) BlobContainer(org.elasticsearch.common.blobstore.BlobContainer) ThreadPool(org.elasticsearch.threadpool.ThreadPool) ActionRunnable(org.elasticsearch.action.ActionRunnable) NamedXContentRegistry(org.elasticsearch.common.xcontent.NamedXContentRegistry) Matchers.lessThanOrEqualTo(org.hamcrest.Matchers.lessThanOrEqualTo) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) Collection(java.util.Collection) BlobMetadata(org.elasticsearch.common.blobstore.BlobMetadata) ClusterChangedEvent(org.elasticsearch.cluster.ClusterChangedEvent) Collectors(java.util.stream.Collectors) Matchers.any(org.mockito.Matchers.any) List(java.util.List) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) TimeValue(io.crate.common.unit.TimeValue) RepositoryData(org.elasticsearch.repositories.RepositoryData) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Mockito.mock(org.mockito.Mockito.mock) InternalTestCluster(org.elasticsearch.test.InternalTestCluster) DataInputStream(java.io.DataInputStream) SnapshotId(org.elasticsearch.snapshots.SnapshotId) ShardGenerations(org.elasticsearch.repositories.ShardGenerations) XContentType(org.elasticsearch.common.xcontent.XContentType) ClusterService(org.elasticsearch.cluster.service.ClusterService) HashMap(java.util.HashMap) LoggingDeprecationHandler(org.elasticsearch.common.xcontent.LoggingDeprecationHandler) IndexId(org.elasticsearch.repositories.IndexId) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.anyString(org.mockito.Matchers.anyString) Strings(org.elasticsearch.common.Strings) Metadata(org.elasticsearch.cluster.metadata.Metadata) RepositoriesMetadata(org.elasticsearch.cluster.metadata.RepositoriesMetadata) ClusterStateApplier(org.elasticsearch.cluster.ClusterStateApplier) Executor(java.util.concurrent.Executor) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) RepositoriesService(org.elasticsearch.repositories.RepositoriesService) XContentParser(org.elasticsearch.common.xcontent.XContentParser) SameThreadExecutorService(org.apache.lucene.util.SameThreadExecutorService) Collections(java.util.Collections) ActionListener(org.elasticsearch.action.ActionListener) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) IndexId(org.elasticsearch.repositories.IndexId) HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString) SnapshotId(org.elasticsearch.snapshots.SnapshotId) BlobContainer(org.elasticsearch.common.blobstore.BlobContainer) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

IndexId (org.elasticsearch.repositories.IndexId)43 ShardId (org.elasticsearch.index.shard.ShardId)22 IOException (java.io.IOException)18 SnapshotsInProgress (org.elasticsearch.cluster.SnapshotsInProgress)16 SnapshotId (org.elasticsearch.snapshots.SnapshotId)16 RepositoryData (org.elasticsearch.repositories.RepositoryData)14 Index (org.elasticsearch.index.Index)13 ArrayList (java.util.ArrayList)12 List (java.util.List)12 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)11 BlobContainer (org.elasticsearch.common.blobstore.BlobContainer)11 Snapshot (org.elasticsearch.snapshots.Snapshot)11 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)10 ClusterState (org.elasticsearch.cluster.ClusterState)10 Map (java.util.Map)9 Collectors (java.util.stream.Collectors)9 RepositoryException (org.elasticsearch.repositories.RepositoryException)9 SnapshotInfo (org.elasticsearch.snapshots.SnapshotInfo)9 Metadata (org.elasticsearch.cluster.metadata.Metadata)8 RepositoriesMetadata (org.elasticsearch.cluster.metadata.RepositoriesMetadata)8