Search in sources :

Example 1 with SnapshotException

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

the class TransportSnapshotsStatusAction method buildResponse.

private SnapshotsStatusResponse buildResponse(SnapshotsStatusRequest request, List<SnapshotsInProgress.Entry> currentSnapshotEntries, TransportNodesSnapshotsStatus.NodesSnapshotStatus nodeSnapshotStatuses) throws IOException {
    // First process snapshot that are currently processed
    List<SnapshotStatus> builder = new ArrayList<>();
    Set<String> currentSnapshotNames = new HashSet<>();
    if (!currentSnapshotEntries.isEmpty()) {
        Map<String, TransportNodesSnapshotsStatus.NodeSnapshotStatus> nodeSnapshotStatusMap;
        if (nodeSnapshotStatuses != null) {
            nodeSnapshotStatusMap = nodeSnapshotStatuses.getNodesMap();
        } else {
            nodeSnapshotStatusMap = new HashMap<>();
        }
        for (SnapshotsInProgress.Entry entry : currentSnapshotEntries) {
            currentSnapshotNames.add(entry.snapshot().getSnapshotId().getName());
            List<SnapshotIndexShardStatus> shardStatusBuilder = new ArrayList<>();
            for (ObjectObjectCursor<ShardId, SnapshotsInProgress.ShardSnapshotStatus> shardEntry : entry.shards()) {
                SnapshotsInProgress.ShardSnapshotStatus status = shardEntry.value;
                if (status.nodeId() != null) {
                    // We should have information about this shard from the shard:
                    TransportNodesSnapshotsStatus.NodeSnapshotStatus nodeStatus = nodeSnapshotStatusMap.get(status.nodeId());
                    if (nodeStatus != null) {
                        Map<ShardId, SnapshotIndexShardStatus> shardStatues = nodeStatus.status().get(entry.snapshot());
                        if (shardStatues != null) {
                            SnapshotIndexShardStatus shardStatus = shardStatues.get(shardEntry.key);
                            if (shardStatus != null) {
                                // We have full information about this shard
                                shardStatusBuilder.add(shardStatus);
                                continue;
                            }
                        }
                    }
                }
                final SnapshotIndexShardStage stage;
                switch(shardEntry.value.state()) {
                    case FAILED:
                    case ABORTED:
                    case MISSING:
                        stage = SnapshotIndexShardStage.FAILURE;
                        break;
                    case INIT:
                    case WAITING:
                    case STARTED:
                        stage = SnapshotIndexShardStage.STARTED;
                        break;
                    case SUCCESS:
                        stage = SnapshotIndexShardStage.DONE;
                        break;
                    default:
                        throw new IllegalArgumentException("Unknown snapshot state " + shardEntry.value.state());
                }
                SnapshotIndexShardStatus shardStatus = new SnapshotIndexShardStatus(shardEntry.key, stage);
                shardStatusBuilder.add(shardStatus);
            }
            builder.add(new SnapshotStatus(entry.snapshot(), entry.state(), Collections.unmodifiableList(shardStatusBuilder)));
        }
    }
    // Now add snapshots on disk that are not currently running
    final String repositoryName = request.repository();
    if (Strings.hasText(repositoryName) && request.snapshots() != null && request.snapshots().length > 0) {
        final Set<String> requestedSnapshotNames = Sets.newHashSet(request.snapshots());
        final RepositoryData repositoryData = snapshotsService.getRepositoryData(repositoryName);
        final Map<String, SnapshotId> matchedSnapshotIds = repositoryData.getAllSnapshotIds().stream().filter(s -> requestedSnapshotNames.contains(s.getName())).collect(Collectors.toMap(SnapshotId::getName, Function.identity()));
        for (final String snapshotName : request.snapshots()) {
            if (currentSnapshotNames.contains(snapshotName)) {
                // we've already found this snapshot in the current snapshot entries, so skip over
                continue;
            }
            SnapshotId snapshotId = matchedSnapshotIds.get(snapshotName);
            if (snapshotId == null) {
                // neither in the current snapshot entries nor found in the repository
                if (request.ignoreUnavailable()) {
                    // ignoring unavailable snapshots, so skip over
                    logger.debug("snapshot status request ignoring snapshot [{}], not found in repository [{}]", snapshotName, repositoryName);
                    continue;
                } else {
                    throw new SnapshotMissingException(repositoryName, snapshotName);
                }
            } else if (repositoryData.getIncompatibleSnapshotIds().contains(snapshotId)) {
                throw new SnapshotException(repositoryName, snapshotName, "cannot get the status for an incompatible snapshot");
            }
            SnapshotInfo snapshotInfo = snapshotsService.snapshot(repositoryName, snapshotId);
            List<SnapshotIndexShardStatus> shardStatusBuilder = new ArrayList<>();
            if (snapshotInfo.state().completed()) {
                Map<ShardId, IndexShardSnapshotStatus> shardStatues = snapshotsService.snapshotShards(request.repository(), snapshotInfo);
                for (Map.Entry<ShardId, IndexShardSnapshotStatus> shardStatus : shardStatues.entrySet()) {
                    shardStatusBuilder.add(new SnapshotIndexShardStatus(shardStatus.getKey(), shardStatus.getValue()));
                }
                final SnapshotsInProgress.State state;
                switch(snapshotInfo.state()) {
                    case FAILED:
                        state = SnapshotsInProgress.State.FAILED;
                        break;
                    case SUCCESS:
                    case PARTIAL:
                        // Translating both PARTIAL and SUCCESS to SUCCESS for now
                        // TODO: add the differentiation on the metadata level in the next major release
                        state = SnapshotsInProgress.State.SUCCESS;
                        break;
                    default:
                        throw new IllegalArgumentException("Unknown snapshot state " + snapshotInfo.state());
                }
                builder.add(new SnapshotStatus(new Snapshot(repositoryName, snapshotId), state, Collections.unmodifiableList(shardStatusBuilder)));
            }
        }
    }
    return new SnapshotsStatusResponse(Collections.unmodifiableList(builder));
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) SnapshotMissingException(org.elasticsearch.snapshots.SnapshotMissingException) SnapshotId(org.elasticsearch.snapshots.SnapshotId) Arrays(java.util.Arrays) ClusterService(org.elasticsearch.cluster.service.ClusterService) IndexShardSnapshotStatus(org.elasticsearch.index.snapshots.IndexShardSnapshotStatus) HashMap(java.util.HashMap) SnapshotInfo(org.elasticsearch.snapshots.SnapshotInfo) Function(java.util.function.Function) Strings(org.elasticsearch.common.Strings) Inject(org.elasticsearch.common.inject.Inject) ArrayList(java.util.ArrayList) TransportMasterNodeAction(org.elasticsearch.action.support.master.TransportMasterNodeAction) HashSet(java.util.HashSet) ObjectObjectCursor(com.carrotsearch.hppc.cursors.ObjectObjectCursor) ClusterState(org.elasticsearch.cluster.ClusterState) Settings(org.elasticsearch.common.settings.Settings) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) Map(java.util.Map) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TransportService(org.elasticsearch.transport.TransportService) ClusterBlockLevel(org.elasticsearch.cluster.block.ClusterBlockLevel) ActionFilters(org.elasticsearch.action.support.ActionFilters) SnapshotsService(org.elasticsearch.snapshots.SnapshotsService) Set(java.util.Set) IOException(java.io.IOException) ObjectCursor(com.carrotsearch.hppc.cursors.ObjectCursor) Collectors(java.util.stream.Collectors) Sets(org.elasticsearch.common.util.set.Sets) List(java.util.List) SnapshotException(org.elasticsearch.snapshots.SnapshotException) SnapshotsInProgress(org.elasticsearch.cluster.SnapshotsInProgress) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) Snapshot(org.elasticsearch.snapshots.Snapshot) Collections(java.util.Collections) ActionListener(org.elasticsearch.action.ActionListener) RepositoryData(org.elasticsearch.repositories.RepositoryData) IndexShardSnapshotStatus(org.elasticsearch.index.snapshots.IndexShardSnapshotStatus) ArrayList(java.util.ArrayList) ShardId(org.elasticsearch.index.shard.ShardId) SnapshotMissingException(org.elasticsearch.snapshots.SnapshotMissingException) HashSet(java.util.HashSet) SnapshotException(org.elasticsearch.snapshots.SnapshotException) RepositoryData(org.elasticsearch.repositories.RepositoryData) SnapshotId(org.elasticsearch.snapshots.SnapshotId) Snapshot(org.elasticsearch.snapshots.Snapshot) SnapshotInfo(org.elasticsearch.snapshots.SnapshotInfo) IndexShardSnapshotStatus(org.elasticsearch.index.snapshots.IndexShardSnapshotStatus) SnapshotsInProgress(org.elasticsearch.cluster.SnapshotsInProgress) IndexShardSnapshotStatus(org.elasticsearch.index.snapshots.IndexShardSnapshotStatus) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with SnapshotException

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

the class BlobStoreRepository method deleteSnapshot.

@Override
public void deleteSnapshot(SnapshotId snapshotId, long repositoryStateId) {
    if (isReadOnly()) {
        throw new RepositoryException(metadata.name(), "cannot delete snapshot from a readonly repository");
    }
    final RepositoryData repositoryData = getRepositoryData();
    List<String> indices = Collections.emptyList();
    SnapshotInfo snapshot = null;
    try {
        snapshot = getSnapshotInfo(snapshotId);
        indices = snapshot.indices();
    } catch (SnapshotMissingException ex) {
        throw ex;
    } catch (IllegalStateException | SnapshotException | ElasticsearchParseException ex) {
        logger.warn((Supplier<?>) () -> new ParameterizedMessage("cannot read snapshot file [{}]", snapshotId), ex);
    }
    MetaData metaData = null;
    try {
        if (snapshot != null) {
            metaData = readSnapshotMetaData(snapshotId, snapshot.version(), repositoryData.resolveIndices(indices), true);
        } else {
            metaData = readSnapshotMetaData(snapshotId, null, repositoryData.resolveIndices(indices), true);
        }
    } catch (IOException | SnapshotException ex) {
        logger.warn((Supplier<?>) () -> new ParameterizedMessage("cannot read metadata for snapshot [{}]", snapshotId), ex);
    }
    try {
        // Delete snapshot from the index file, since it is the maintainer of truth of active snapshots
        final RepositoryData updatedRepositoryData = repositoryData.removeSnapshot(snapshotId);
        writeIndexGen(updatedRepositoryData, repositoryStateId);
        // delete the snapshot file
        safeSnapshotBlobDelete(snapshot, snapshotId.getUUID());
        // delete the global metadata file
        safeGlobalMetaDataBlobDelete(snapshot, snapshotId.getUUID());
        // Now delete all indices
        for (String index : indices) {
            final IndexId indexId = repositoryData.resolveIndexId(index);
            BlobPath indexPath = basePath().add("indices").add(indexId.getId());
            BlobContainer indexMetaDataBlobContainer = blobStore().blobContainer(indexPath);
            try {
                indexMetaDataFormat.delete(indexMetaDataBlobContainer, snapshotId.getUUID());
            } catch (IOException ex) {
                logger.warn((Supplier<?>) () -> new ParameterizedMessage("[{}] failed to delete metadata for index [{}]", snapshotId, index), ex);
            }
            if (metaData != null) {
                IndexMetaData indexMetaData = metaData.index(index);
                if (indexMetaData != null) {
                    for (int shardId = 0; shardId < indexMetaData.getNumberOfShards(); shardId++) {
                        try {
                            delete(snapshotId, snapshot.version(), indexId, new ShardId(indexMetaData.getIndex(), shardId));
                        } catch (SnapshotException ex) {
                            final int finalShardId = shardId;
                            logger.warn((Supplier<?>) () -> new ParameterizedMessage("[{}] failed to delete shard data for shard [{}][{}]", snapshotId, index, finalShardId), ex);
                        }
                    }
                }
            }
        }
        // cleanup indices that are no longer part of the repository
        final Collection<IndexId> indicesToCleanUp = Sets.newHashSet(repositoryData.getIndices().values());
        indicesToCleanUp.removeAll(updatedRepositoryData.getIndices().values());
        final BlobContainer indicesBlobContainer = blobStore().blobContainer(basePath().add("indices"));
        for (final IndexId indexId : indicesToCleanUp) {
            try {
                indicesBlobContainer.deleteBlob(indexId.getId());
            } catch (DirectoryNotEmptyException dnee) {
                // if the directory isn't empty for some reason, it will fail to clean up;
                // we'll ignore that and accept that cleanup didn't fully succeed.
                // since we are using UUIDs for path names, this won't be an issue for
                // snapshotting indices of the same name
                logger.debug((Supplier<?>) () -> new ParameterizedMessage("[{}] index [{}] no longer part of any snapshots in the repository, but failed to clean up " + "its index folder due to the directory not being empty.", metadata.name(), indexId), dnee);
            } catch (IOException ioe) {
                // a different IOException occurred while trying to delete - will just log the issue for now
                logger.debug((Supplier<?>) () -> new ParameterizedMessage("[{}] index [{}] no longer part of any snapshots in the repository, but failed to clean up " + "its index folder.", metadata.name(), indexId), ioe);
            }
        }
    } catch (IOException ex) {
        throw new RepositoryException(metadata.name(), "failed to update snapshot in repository", ex);
    }
}
Also used : IndexId(org.elasticsearch.repositories.IndexId) BlobPath(org.elasticsearch.common.blobstore.BlobPath) RepositoryException(org.elasticsearch.repositories.RepositoryException) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) IOException(java.io.IOException) SnapshotException(org.elasticsearch.snapshots.SnapshotException) IndexShardSnapshotException(org.elasticsearch.index.snapshots.IndexShardSnapshotException) RepositoryData(org.elasticsearch.repositories.RepositoryData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) ShardId(org.elasticsearch.index.shard.ShardId) SnapshotInfo(org.elasticsearch.snapshots.SnapshotInfo) SnapshotMissingException(org.elasticsearch.snapshots.SnapshotMissingException) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) StoreFileMetaData(org.elasticsearch.index.store.StoreFileMetaData) BlobMetaData(org.elasticsearch.common.blobstore.BlobMetaData) RepositoryMetaData(org.elasticsearch.cluster.metadata.RepositoryMetaData) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) BlobContainer(org.elasticsearch.common.blobstore.BlobContainer) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage)

Example 3 with SnapshotException

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

the class BlobStoreRepository method readSnapshotMetaData.

private MetaData readSnapshotMetaData(SnapshotId snapshotId, Version snapshotVersion, List<IndexId> indices, boolean ignoreIndexErrors) throws IOException {
    MetaData metaData;
    if (snapshotVersion == null) {
        // We can try detecting the version based on the metadata file format
        assert ignoreIndexErrors;
        if (globalMetaDataFormat.exists(snapshotsBlobContainer, snapshotId.getUUID())) {
            snapshotVersion = Version.CURRENT;
        } else {
            throw new SnapshotMissingException(metadata.name(), snapshotId);
        }
    }
    try {
        metaData = globalMetaDataFormat.read(snapshotsBlobContainer, snapshotId.getUUID());
    } catch (NoSuchFileException ex) {
        throw new SnapshotMissingException(metadata.name(), snapshotId, ex);
    } catch (IOException ex) {
        throw new SnapshotException(metadata.name(), snapshotId, "failed to get snapshots", ex);
    }
    MetaData.Builder metaDataBuilder = MetaData.builder(metaData);
    for (IndexId index : indices) {
        BlobPath indexPath = basePath().add("indices").add(index.getId());
        BlobContainer indexMetaDataBlobContainer = blobStore().blobContainer(indexPath);
        try {
            metaDataBuilder.put(indexMetaDataFormat.read(indexMetaDataBlobContainer, snapshotId.getUUID()), false);
        } catch (ElasticsearchParseException | IOException ex) {
            if (ignoreIndexErrors) {
                logger.warn((Supplier<?>) () -> new ParameterizedMessage("[{}] [{}] failed to read metadata for index", snapshotId, index.getName()), ex);
            } else {
                throw ex;
            }
        }
    }
    return metaDataBuilder.build();
}
Also used : IndexId(org.elasticsearch.repositories.IndexId) BlobPath(org.elasticsearch.common.blobstore.BlobPath) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException) SnapshotException(org.elasticsearch.snapshots.SnapshotException) IndexShardSnapshotException(org.elasticsearch.index.snapshots.IndexShardSnapshotException) SnapshotMissingException(org.elasticsearch.snapshots.SnapshotMissingException) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) StoreFileMetaData(org.elasticsearch.index.store.StoreFileMetaData) BlobMetaData(org.elasticsearch.common.blobstore.BlobMetaData) RepositoryMetaData(org.elasticsearch.cluster.metadata.RepositoryMetaData) BlobContainer(org.elasticsearch.common.blobstore.BlobContainer) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage)

Example 4 with SnapshotException

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

the class ExceptionSerializationTests method testSnapshotException.

public void testSnapshotException() throws IOException {
    final Snapshot snapshot = new Snapshot("repo", new SnapshotId("snap", UUIDs.randomBase64UUID()));
    SnapshotException ex = serialize(new SnapshotException(snapshot, "no such snapshot", new NullPointerException()));
    assertEquals(ex.getRepositoryName(), snapshot.getRepository());
    assertEquals(ex.getSnapshotName(), snapshot.getSnapshotId().getName());
    assertEquals(ex.getMessage(), "[" + snapshot + "] no such snapshot");
    assertTrue(ex.getCause() instanceof NullPointerException);
    ex = serialize(new SnapshotException(null, "no such snapshot", new NullPointerException()));
    assertNull(ex.getRepositoryName());
    assertNull(ex.getSnapshotName());
    assertEquals(ex.getMessage(), "[_na] no such snapshot");
    assertTrue(ex.getCause() instanceof NullPointerException);
}
Also used : Snapshot(org.elasticsearch.snapshots.Snapshot) SnapshotId(org.elasticsearch.snapshots.SnapshotId) SnapshotException(org.elasticsearch.snapshots.SnapshotException)

Aggregations

SnapshotException (org.elasticsearch.snapshots.SnapshotException)4 IOException (java.io.IOException)3 SnapshotMissingException (org.elasticsearch.snapshots.SnapshotMissingException)3 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)2 Supplier (org.apache.logging.log4j.util.Supplier)2 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)2 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)2 MetaData (org.elasticsearch.cluster.metadata.MetaData)2 RepositoryMetaData (org.elasticsearch.cluster.metadata.RepositoryMetaData)2 BlobContainer (org.elasticsearch.common.blobstore.BlobContainer)2 BlobMetaData (org.elasticsearch.common.blobstore.BlobMetaData)2 BlobPath (org.elasticsearch.common.blobstore.BlobPath)2 ShardId (org.elasticsearch.index.shard.ShardId)2 IndexShardSnapshotException (org.elasticsearch.index.snapshots.IndexShardSnapshotException)2 StoreFileMetaData (org.elasticsearch.index.store.StoreFileMetaData)2 IndexId (org.elasticsearch.repositories.IndexId)2 RepositoryData (org.elasticsearch.repositories.RepositoryData)2 Snapshot (org.elasticsearch.snapshots.Snapshot)2 SnapshotId (org.elasticsearch.snapshots.SnapshotId)2 SnapshotInfo (org.elasticsearch.snapshots.SnapshotInfo)2