Search in sources :

Example 31 with IndexId

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

the class SnapshotsService method snapshotShards.

/**
     * Returns status of shards  currently finished snapshots
     * <p>
     * This method is executed on master node and it's complimentary to the {@link SnapshotShardsService#currentSnapshotShards(Snapshot)} because it
     * returns similar information but for already finished snapshots.
     * </p>
     *
     * @param repositoryName  repository name
     * @param snapshotInfo    snapshot info
     * @return map of shard id to snapshot status
     */
public Map<ShardId, IndexShardSnapshotStatus> snapshotShards(final String repositoryName, final SnapshotInfo snapshotInfo) throws IOException {
    Map<ShardId, IndexShardSnapshotStatus> shardStatus = new HashMap<>();
    Repository repository = repositoriesService.repository(repositoryName);
    RepositoryData repositoryData = repository.getRepositoryData();
    MetaData metaData = repository.getSnapshotMetaData(snapshotInfo, repositoryData.resolveIndices(snapshotInfo.indices()));
    for (String index : snapshotInfo.indices()) {
        IndexId indexId = repositoryData.resolveIndexId(index);
        IndexMetaData indexMetaData = metaData.indices().get(index);
        if (indexMetaData != null) {
            int numberOfShards = indexMetaData.getNumberOfShards();
            for (int i = 0; i < numberOfShards; i++) {
                ShardId shardId = new ShardId(indexMetaData.getIndex(), i);
                SnapshotShardFailure shardFailure = findShardFailure(snapshotInfo.shardFailures(), shardId);
                if (shardFailure != null) {
                    IndexShardSnapshotStatus shardSnapshotStatus = new IndexShardSnapshotStatus();
                    shardSnapshotStatus.updateStage(IndexShardSnapshotStatus.Stage.FAILURE);
                    shardSnapshotStatus.failure(shardFailure.reason());
                    shardStatus.put(shardId, shardSnapshotStatus);
                } else {
                    IndexShardSnapshotStatus shardSnapshotStatus = repository.getShardSnapshotStatus(snapshotInfo.snapshotId(), snapshotInfo.version(), indexId, shardId);
                    shardStatus.put(shardId, shardSnapshotStatus);
                }
            }
        }
    }
    return unmodifiableMap(shardStatus);
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) IndexShardSnapshotStatus(org.elasticsearch.index.snapshots.IndexShardSnapshotStatus) IndexId(org.elasticsearch.repositories.IndexId) Repository(org.elasticsearch.repositories.Repository) HashMap(java.util.HashMap) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) RepositoriesMetaData(org.elasticsearch.cluster.metadata.RepositoriesMetaData) RepositoryData(org.elasticsearch.repositories.RepositoryData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 32 with IndexId

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

the class SnapshotsService method shards.

/**
     * Calculates the list of shards that should be included into the current snapshot
     *
     * @param clusterState cluster state
     * @param indices      list of indices to be snapshotted
     * @return list of shard to be included into current snapshot
     */
private ImmutableOpenMap<ShardId, SnapshotsInProgress.ShardSnapshotStatus> shards(ClusterState clusterState, List<IndexId> indices) {
    ImmutableOpenMap.Builder<ShardId, SnapshotsInProgress.ShardSnapshotStatus> builder = ImmutableOpenMap.builder();
    MetaData metaData = clusterState.metaData();
    for (IndexId index : indices) {
        final String indexName = index.getName();
        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, State.MISSING, "missing index"));
        } else if (indexMetaData.getState() == IndexMetaData.State.CLOSE) {
            for (int i = 0; i < indexMetaData.getNumberOfShards(); i++) {
                ShardId shardId = new ShardId(indexMetaData.getIndex(), i);
                builder.put(shardId, new SnapshotsInProgress.ShardSnapshotStatus(null, State.MISSING, "index is closed"));
            }
        } else {
            IndexRoutingTable indexRoutingTable = clusterState.getRoutingTable().index(indexName);
            for (int i = 0; i < indexMetaData.getNumberOfShards(); i++) {
                ShardId shardId = new ShardId(indexMetaData.getIndex(), i);
                if (indexRoutingTable != null) {
                    ShardRouting primary = indexRoutingTable.shard(i).primaryShard();
                    if (primary == null || !primary.assignedToNode()) {
                        builder.put(shardId, new SnapshotsInProgress.ShardSnapshotStatus(null, State.MISSING, "primary shard is not allocated"));
                    } else if (primary.relocating() || primary.initializing()) {
                        // The WAITING state was introduced in V1.2.0 - don't use it if there are nodes with older version in the cluster
                        builder.put(shardId, new SnapshotsInProgress.ShardSnapshotStatus(primary.currentNodeId(), State.WAITING));
                    } else if (!primary.started()) {
                        builder.put(shardId, new SnapshotsInProgress.ShardSnapshotStatus(primary.currentNodeId(), State.MISSING, "primary shard hasn't been started yet"));
                    } else {
                        builder.put(shardId, new SnapshotsInProgress.ShardSnapshotStatus(primary.currentNodeId()));
                    }
                } else {
                    builder.put(shardId, new SnapshotsInProgress.ShardSnapshotStatus(null, State.MISSING, "missing routing table"));
                }
            }
        }
    }
    return builder.build();
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) IndexId(org.elasticsearch.repositories.IndexId) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) ShardSnapshotStatus(org.elasticsearch.cluster.SnapshotsInProgress.ShardSnapshotStatus) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) RepositoriesMetaData(org.elasticsearch.cluster.metadata.RepositoriesMetaData) SnapshotsInProgress(org.elasticsearch.cluster.SnapshotsInProgress) ShardSnapshotStatus(org.elasticsearch.cluster.SnapshotsInProgress.ShardSnapshotStatus) IndexShardSnapshotStatus(org.elasticsearch.index.snapshots.IndexShardSnapshotStatus) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 33 with IndexId

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

the class MetaDataDeleteIndexServiceTests method testDeleteSnapshotting.

public void testDeleteSnapshotting() {
    String index = randomAsciiOfLength(5);
    Snapshot snapshot = new Snapshot("doesn't matter", new SnapshotId("snapshot name", "snapshot uuid"));
    SnapshotsInProgress snaps = new SnapshotsInProgress(new SnapshotsInProgress.Entry(snapshot, true, false, SnapshotsInProgress.State.INIT, singletonList(new IndexId(index, "doesn't matter")), System.currentTimeMillis(), (long) randomIntBetween(0, 1000), ImmutableOpenMap.of()));
    ClusterState state = ClusterState.builder(clusterState(index)).putCustom(SnapshotsInProgress.TYPE, snaps).build();
    Exception e = expectThrows(IllegalArgumentException.class, () -> service.deleteIndices(state, singleton(state.metaData().getIndices().get(index).getIndex())));
    assertEquals("Cannot delete indices that are being snapshotted: [[" + index + "]]. Try again after snapshot finishes " + "or cancel the currently running snapshot.", e.getMessage());
}
Also used : Snapshot(org.elasticsearch.snapshots.Snapshot) SnapshotId(org.elasticsearch.snapshots.SnapshotId) IndexId(org.elasticsearch.repositories.IndexId) ClusterState(org.elasticsearch.cluster.ClusterState) SnapshotsInProgress(org.elasticsearch.cluster.SnapshotsInProgress) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException)

Example 34 with IndexId

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

the class SnapshotsInProgressSerializationTests method randomSnapshot.

private Entry randomSnapshot() {
    Snapshot snapshot = new Snapshot(randomAsciiOfLength(10), new SnapshotId(randomAsciiOfLength(10), randomAsciiOfLength(10)));
    boolean includeGlobalState = randomBoolean();
    boolean partial = randomBoolean();
    State state = randomFrom(State.values());
    int numberOfIndices = randomIntBetween(0, 10);
    List<IndexId> indices = new ArrayList<>();
    for (int i = 0; i < numberOfIndices; i++) {
        indices.add(new IndexId(randomAsciiOfLength(10), randomAsciiOfLength(10)));
    }
    long startTime = randomLong();
    long repositoryStateId = randomLong();
    ImmutableOpenMap.Builder<ShardId, SnapshotsInProgress.ShardSnapshotStatus> builder = ImmutableOpenMap.builder();
    int shardsCount = randomIntBetween(0, 10);
    for (int j = 0; j < shardsCount; j++) {
        ShardId shardId = new ShardId(new Index(randomAsciiOfLength(10), randomAsciiOfLength(10)), randomIntBetween(0, 10));
        String nodeId = randomAsciiOfLength(10);
        State shardState = randomFrom(State.values());
        builder.put(shardId, new SnapshotsInProgress.ShardSnapshotStatus(nodeId, shardState));
    }
    ImmutableOpenMap<ShardId, SnapshotsInProgress.ShardSnapshotStatus> shards = builder.build();
    return new Entry(snapshot, includeGlobalState, partial, state, indices, startTime, repositoryStateId, shards);
}
Also used : IndexId(org.elasticsearch.repositories.IndexId) ArrayList(java.util.ArrayList) Index(org.elasticsearch.index.Index) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) ShardId(org.elasticsearch.index.shard.ShardId) Entry(org.elasticsearch.cluster.SnapshotsInProgress.Entry) State(org.elasticsearch.cluster.SnapshotsInProgress.State) SnapshotsInProgress(org.elasticsearch.cluster.SnapshotsInProgress)

Example 35 with IndexId

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

the class SnapshotsInProgress method writeTo.

@Override
public void writeTo(StreamOutput out) throws IOException {
    out.writeVInt(entries.size());
    for (Entry entry : entries) {
        entry.snapshot().writeTo(out);
        out.writeBoolean(entry.includeGlobalState());
        out.writeBoolean(entry.partial());
        out.writeByte(entry.state().value());
        out.writeVInt(entry.indices().size());
        for (IndexId index : entry.indices()) {
            index.writeTo(out);
        }
        if (out.getVersion().after(Version.V_4_5_1)) {
            out.writeStringArray(entry.templates.toArray(new String[0]));
        }
        out.writeLong(entry.startTime());
        out.writeVInt(entry.shards().size());
        for (ObjectObjectCursor<ShardId, ShardSnapshotStatus> shardEntry : entry.shards()) {
            shardEntry.key.writeTo(out);
            shardEntry.value.writeTo(out);
        }
        out.writeLong(entry.repositoryStateId);
        out.writeOptionalString(entry.failure);
        if (out.getVersion().onOrAfter(SnapshotsService.SHARD_GEN_IN_REPO_DATA_VERSION)) {
            out.writeBoolean(entry.useShardGenerations);
        }
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) IndexId(org.elasticsearch.repositories.IndexId)

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