Search in sources :

Example 1 with ShardStateMetadata

use of org.elasticsearch.index.shard.ShardStateMetadata in project elasticsearch by elastic.

the class TransportNodesListGatewayStartedShards method nodeOperation.

@Override
protected NodeGatewayStartedShards nodeOperation(NodeRequest request) {
    try {
        final ShardId shardId = request.getShardId();
        logger.trace("{} loading local shard state info", shardId);
        ShardStateMetaData shardStateMetaData = ShardStateMetaData.FORMAT.loadLatestState(logger, NamedXContentRegistry.EMPTY, nodeEnv.availableShardPaths(request.shardId));
        if (shardStateMetaData != null) {
            IndexMetaData metaData = clusterService.state().metaData().index(shardId.getIndex());
            if (metaData == null) {
                // we may send this requests while processing the cluster state that recovered the index
                // sometimes the request comes in before the local node processed that cluster state
                // in such cases we can load it from disk
                metaData = IndexMetaData.FORMAT.loadLatestState(logger, NamedXContentRegistry.EMPTY, nodeEnv.indexPaths(shardId.getIndex()));
            }
            if (metaData == null) {
                ElasticsearchException e = new ElasticsearchException("failed to find local IndexMetaData");
                e.setShard(request.shardId);
                throw e;
            }
            if (indicesService.getShardOrNull(shardId) == null) {
                // we don't have an open shard on the store, validate the files on disk are openable
                ShardPath shardPath = null;
                try {
                    IndexSettings indexSettings = new IndexSettings(metaData, settings);
                    shardPath = ShardPath.loadShardPath(logger, nodeEnv, shardId, indexSettings);
                    if (shardPath == null) {
                        throw new IllegalStateException(shardId + " no shard path found");
                    }
                    Store.tryOpenIndex(shardPath.resolveIndex(), shardId, nodeEnv::shardLock, logger);
                } catch (Exception exception) {
                    final ShardPath finalShardPath = shardPath;
                    logger.trace((Supplier<?>) () -> new ParameterizedMessage("{} can't open index for shard [{}] in path [{}]", shardId, shardStateMetaData, (finalShardPath != null) ? finalShardPath.resolveIndex() : ""), exception);
                    String allocationId = shardStateMetaData.allocationId != null ? shardStateMetaData.allocationId.getId() : null;
                    return new NodeGatewayStartedShards(clusterService.localNode(), allocationId, shardStateMetaData.primary, exception);
                }
            }
            logger.debug("{} shard state info found: [{}]", shardId, shardStateMetaData);
            String allocationId = shardStateMetaData.allocationId != null ? shardStateMetaData.allocationId.getId() : null;
            return new NodeGatewayStartedShards(clusterService.localNode(), allocationId, shardStateMetaData.primary);
        }
        logger.trace("{} no local shard info found", shardId);
        return new NodeGatewayStartedShards(clusterService.localNode(), null, false);
    } catch (Exception e) {
        throw new ElasticsearchException("failed to load started shards", e);
    }
}
Also used : IndexSettings(org.elasticsearch.index.IndexSettings) ElasticsearchException(org.elasticsearch.ElasticsearchException) ElasticsearchException(org.elasticsearch.ElasticsearchException) FailedNodeException(org.elasticsearch.action.FailedNodeException) IOException(java.io.IOException) ShardStateMetaData(org.elasticsearch.index.shard.ShardStateMetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) ShardId(org.elasticsearch.index.shard.ShardId) ShardPath(org.elasticsearch.index.shard.ShardPath) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage)

Example 2 with ShardStateMetadata

use of org.elasticsearch.index.shard.ShardStateMetadata in project elasticsearch by elastic.

the class IndexFolderUpgraderTests method writeIndex.

private void writeIndex(NodeEnvironment nodeEnv, IndexSettings indexSettings, int numIdxFiles, int numTranslogFiles) throws IOException {
    NodeEnvironment.NodePath[] nodePaths = nodeEnv.nodePaths();
    Path[] oldIndexPaths = new Path[nodePaths.length];
    for (int i = 0; i < nodePaths.length; i++) {
        oldIndexPaths[i] = nodePaths[i].indicesPath.resolve(indexSettings.getIndex().getName());
    }
    IndexMetaData.FORMAT.write(indexSettings.getIndexMetaData(), oldIndexPaths);
    for (int id = 0; id < indexSettings.getNumberOfShards(); id++) {
        Path oldIndexPath = randomFrom(oldIndexPaths);
        ShardId shardId = new ShardId(indexSettings.getIndex(), id);
        if (indexSettings.hasCustomDataPath()) {
            Path customIndexPath = nodeEnv.resolveBaseCustomLocation(indexSettings).resolve(indexSettings.getIndex().getName());
            writeShard(shardId, customIndexPath, numIdxFiles, numTranslogFiles);
        } else {
            writeShard(shardId, oldIndexPath, numIdxFiles, numTranslogFiles);
        }
        ShardStateMetaData state = new ShardStateMetaData(true, indexSettings.getUUID(), AllocationId.newInitializing());
        ShardStateMetaData.FORMAT.write(state, oldIndexPath.resolve(String.valueOf(shardId.getId())));
    }
}
Also used : Path(java.nio.file.Path) ShardPath(org.elasticsearch.index.shard.ShardPath) ShardId(org.elasticsearch.index.shard.ShardId) ShardStateMetaData(org.elasticsearch.index.shard.ShardStateMetaData)

Example 3 with ShardStateMetadata

use of org.elasticsearch.index.shard.ShardStateMetadata in project crate by crate.

the class TransportNodesListGatewayStartedShards method nodeOperation.

@Override
protected NodeGatewayStartedShards nodeOperation(NodeRequest request) {
    try {
        final ShardId shardId = request.getShardId();
        logger.trace("{} loading local shard state info", shardId);
        ShardStateMetadata shardStateMetadata = ShardStateMetadata.FORMAT.loadLatestState(logger, namedXContentRegistry, nodeEnv.availableShardPaths(request.shardId));
        if (shardStateMetadata != null) {
            if (indicesService.getShardOrNull(shardId) == null) {
                final String customDataPath;
                if (request.getCustomDataPath() != null) {
                    customDataPath = request.getCustomDataPath();
                } else {
                    // TODO: Fallback for BWC with older ES versions. Remove once request.getCustomDataPath() always returns non-null
                    final IndexMetadata metadata = clusterService.state().metadata().index(shardId.getIndex());
                    if (metadata != null) {
                        customDataPath = new IndexSettings(metadata, settings).customDataPath();
                    } else {
                        logger.trace("{} node doesn't have meta data for the requests index", shardId);
                        throw new ElasticsearchException("node doesn't have meta data for index " + shardId.getIndex());
                    }
                }
                // we don't have an open shard on the store, validate the files on disk are openable
                ShardPath shardPath = null;
                try {
                    shardPath = ShardPath.loadShardPath(logger, nodeEnv, shardId, customDataPath);
                    if (shardPath == null) {
                        throw new IllegalStateException(shardId + " no shard path found");
                    }
                    Store.tryOpenIndex(shardPath.resolveIndex(), shardId, nodeEnv::shardLock, logger);
                } catch (Exception exception) {
                    final ShardPath finalShardPath = shardPath;
                    logger.trace(() -> new ParameterizedMessage("{} can't open index for shard [{}] in path [{}]", shardId, shardStateMetadata, (finalShardPath != null) ? finalShardPath.resolveIndex() : ""), exception);
                    String allocationId = shardStateMetadata.allocationId != null ? shardStateMetadata.allocationId.getId() : null;
                    return new NodeGatewayStartedShards(clusterService.localNode(), allocationId, shardStateMetadata.primary, exception);
                }
            }
            logger.debug("{} shard state info found: [{}]", shardId, shardStateMetadata);
            String allocationId = shardStateMetadata.allocationId != null ? shardStateMetadata.allocationId.getId() : null;
            return new NodeGatewayStartedShards(clusterService.localNode(), allocationId, shardStateMetadata.primary);
        }
        logger.trace("{} no local shard info found", shardId);
        return new NodeGatewayStartedShards(clusterService.localNode(), null, false);
    } catch (Exception e) {
        throw new ElasticsearchException("failed to load started shards", e);
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) ShardPath(org.elasticsearch.index.shard.ShardPath) IndexSettings(org.elasticsearch.index.IndexSettings) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ElasticsearchException(org.elasticsearch.ElasticsearchException) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) ShardStateMetadata(org.elasticsearch.index.shard.ShardStateMetadata) ElasticsearchException(org.elasticsearch.ElasticsearchException) FailedNodeException(org.elasticsearch.action.FailedNodeException) IOException(java.io.IOException)

Example 4 with ShardStateMetadata

use of org.elasticsearch.index.shard.ShardStateMetadata in project crate by crate.

the class CrateMetaDataUpgradeService method upgradeShardMetaData.

private static void upgradeShardMetaData(NodeEnvironment nodeEnv, String indexName, String indexUUID, ESLogger logger) throws IOException {
    for (ShardId shardId : findAllShardIds(nodeEnv.indexPaths(new Index(indexName)))) {
        try (ShardLock lock = nodeEnv.shardLock(shardId, 0)) {
            final Path[] paths = nodeEnv.availableShardPaths(shardId);
            final ShardStateMetaData loaded = ShardStateMetaData.FORMAT.loadLatestState(logger, paths);
            if (loaded == null) {
                throw new IllegalStateException("[" + shardId + "] no shard state found in any of: " + Arrays.toString(paths) + " please check and remove them if possible");
            }
            for (Path path : paths) {
                ShardStateMetaData newShardStateMetaData = new ShardStateMetaData(loaded.version, loaded.primary, indexUUID);
                ShardStateMetaData.FORMAT.write(newShardStateMetaData, newShardStateMetaData.version, path);
                logger.trace("new UUID={} was set for shard={} of index={} in path={}", indexUUID, shardId, indexName, path);
            }
        }
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) Path(java.nio.file.Path) Index(org.elasticsearch.index.Index) ShardLock(org.elasticsearch.env.ShardLock) ShardStateMetaData(org.elasticsearch.index.shard.ShardStateMetaData)

Aggregations

ShardId (org.elasticsearch.index.shard.ShardId)4 ShardPath (org.elasticsearch.index.shard.ShardPath)3 ShardStateMetaData (org.elasticsearch.index.shard.ShardStateMetaData)3 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)2 ElasticsearchException (org.elasticsearch.ElasticsearchException)2 FailedNodeException (org.elasticsearch.action.FailedNodeException)2 IndexSettings (org.elasticsearch.index.IndexSettings)2 Supplier (org.apache.logging.log4j.util.Supplier)1 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)1 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)1 ShardLock (org.elasticsearch.env.ShardLock)1 Index (org.elasticsearch.index.Index)1 ShardStateMetadata (org.elasticsearch.index.shard.ShardStateMetadata)1