Search in sources :

Example 51 with IndexMetadata

use of org.elasticsearch.cluster.metadata.IndexMetadata in project elasticsearch by elastic.

the class LocalAllocateDangledIndices method allocateDangled.

public void allocateDangled(Collection<IndexMetaData> indices, final Listener listener) {
    ClusterState clusterState = clusterService.state();
    DiscoveryNode masterNode = clusterState.nodes().getMasterNode();
    if (masterNode == null) {
        listener.onFailure(new MasterNotDiscoveredException("no master to send allocate dangled request"));
        return;
    }
    AllocateDangledRequest request = new AllocateDangledRequest(clusterService.localNode(), indices.toArray(new IndexMetaData[indices.size()]));
    transportService.sendRequest(masterNode, ACTION_NAME, request, new TransportResponseHandler<AllocateDangledResponse>() {

        @Override
        public AllocateDangledResponse newInstance() {
            return new AllocateDangledResponse();
        }

        @Override
        public void handleResponse(AllocateDangledResponse response) {
            listener.onResponse(response);
        }

        @Override
        public void handleException(TransportException exp) {
            listener.onFailure(exp);
        }

        @Override
        public String executor() {
            return ThreadPool.Names.SAME;
        }
    });
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) MasterNotDiscoveredException(org.elasticsearch.discovery.MasterNotDiscoveredException) TransportException(org.elasticsearch.transport.TransportException) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 52 with IndexMetadata

use of org.elasticsearch.cluster.metadata.IndexMetadata 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 53 with IndexMetadata

use of org.elasticsearch.cluster.metadata.IndexMetadata in project elasticsearch by elastic.

the class MetaStateService method loadFullState.

/**
     * Loads the full state, which includes both the global state and all the indices
     * meta state.
     */
MetaData loadFullState() throws Exception {
    MetaData globalMetaData = loadGlobalState();
    MetaData.Builder metaDataBuilder;
    if (globalMetaData != null) {
        metaDataBuilder = MetaData.builder(globalMetaData);
    } else {
        metaDataBuilder = MetaData.builder();
    }
    for (String indexFolderName : nodeEnv.availableIndexFolders()) {
        IndexMetaData indexMetaData = IndexMetaData.FORMAT.loadLatestState(logger, namedXContentRegistry, nodeEnv.resolveIndexFolder(indexFolderName));
        if (indexMetaData != null) {
            metaDataBuilder.put(indexMetaData, false);
        } else {
            logger.debug("[{}] failed to find metadata for existing index location", indexFolderName);
        }
    }
    return metaDataBuilder.build();
}
Also used : MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 54 with IndexMetadata

use of org.elasticsearch.cluster.metadata.IndexMetadata in project elasticsearch by elastic.

the class DanglingIndicesState method findNewDanglingIndices.

/**
     * Finds new dangling indices by iterating over the indices and trying to find indices
     * that have state on disk, but are not part of the provided meta data, or not detected
     * as dangled already.
     */
Map<Index, IndexMetaData> findNewDanglingIndices(final MetaData metaData) {
    final Set<String> excludeIndexPathIds = new HashSet<>(metaData.indices().size() + danglingIndices.size());
    for (ObjectCursor<IndexMetaData> cursor : metaData.indices().values()) {
        excludeIndexPathIds.add(cursor.value.getIndex().getUUID());
    }
    excludeIndexPathIds.addAll(danglingIndices.keySet().stream().map(Index::getUUID).collect(Collectors.toList()));
    try {
        final List<IndexMetaData> indexMetaDataList = metaStateService.loadIndicesStates(excludeIndexPathIds::contains);
        Map<Index, IndexMetaData> newIndices = new HashMap<>(indexMetaDataList.size());
        final IndexGraveyard graveyard = metaData.indexGraveyard();
        for (IndexMetaData indexMetaData : indexMetaDataList) {
            if (metaData.hasIndex(indexMetaData.getIndex().getName())) {
                logger.warn("[{}] can not be imported as a dangling index, as index with same name already exists in cluster metadata", indexMetaData.getIndex());
            } else if (graveyard.containsIndex(indexMetaData.getIndex())) {
                logger.warn("[{}] can not be imported as a dangling index, as an index with the same name and UUID exist in the " + "index tombstones.  This situation is likely caused by copying over the data directory for an index " + "that was previously deleted.", indexMetaData.getIndex());
            } else {
                logger.info("[{}] dangling index exists on local file system, but not in cluster metadata, " + "auto import to cluster state", indexMetaData.getIndex());
                newIndices.put(indexMetaData.getIndex(), indexMetaData);
            }
        }
        return newIndices;
    } catch (IOException e) {
        logger.warn("failed to list dangling indices", e);
        return emptyMap();
    }
}
Also used : HashMap(java.util.HashMap) IndexGraveyard(org.elasticsearch.cluster.metadata.IndexGraveyard) Index(org.elasticsearch.index.Index) IOException(java.io.IOException) HashSet(java.util.HashSet) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 55 with IndexMetadata

use of org.elasticsearch.cluster.metadata.IndexMetadata in project elasticsearch by elastic.

the class IndicesService method deleteShardStore.

/**
     * This method deletes the shard contents on disk for the given shard ID. This method will fail if the shard deleting
     * is prevented by {@link #canDeleteShardContent(ShardId, IndexSettings)}
     * of if the shards lock can not be acquired.
     *
     * On data nodes, if the deleted shard is the last shard folder in its index, the method will attempt to remove the index folder as well.
     *
     * @param reason the reason for the shard deletion
     * @param shardId the shards ID to delete
     * @param clusterState . This is required to access the indexes settings etc.
     * @throws IOException if an IOException occurs
     */
public void deleteShardStore(String reason, ShardId shardId, ClusterState clusterState) throws IOException, ShardLockObtainFailedException {
    final IndexMetaData metaData = clusterState.getMetaData().indices().get(shardId.getIndexName());
    final IndexSettings indexSettings = buildIndexSettings(metaData);
    ShardDeletionCheckResult shardDeletionCheckResult = canDeleteShardContent(shardId, indexSettings);
    if (shardDeletionCheckResult != ShardDeletionCheckResult.FOLDER_FOUND_CAN_DELETE) {
        throw new IllegalStateException("Can't delete shard " + shardId + " (cause: " + shardDeletionCheckResult + ")");
    }
    nodeEnv.deleteShardDirectorySafe(shardId, indexSettings);
    logger.debug("{} deleted shard reason [{}]", shardId, reason);
    if (// master nodes keep the index meta data, even if having no shards..
    clusterState.nodes().getLocalNode().isMasterNode() == false && canDeleteIndexContents(shardId.getIndex(), indexSettings)) {
        if (nodeEnv.findAllShardIds(shardId.getIndex()).isEmpty()) {
            try {
                // note that deleteIndexStore have more safety checks and may throw an exception if index was concurrently created.
                deleteIndexStore("no longer used", metaData, clusterState);
            } catch (Exception e) {
                // wrap the exception to indicate we already deleted the shard
                throw new ElasticsearchException("failed to delete unused index after deleting its last shard (" + shardId + ")", e);
            }
        } else {
            logger.trace("[{}] still has shard stores, leaving as is", shardId.getIndex());
        }
    }
}
Also used : IndexSettings(org.elasticsearch.index.IndexSettings) ElasticsearchException(org.elasticsearch.ElasticsearchException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) ShardLockObtainFailedException(org.elasticsearch.env.ShardLockObtainFailedException) IllegalIndexShardStateException(org.elasticsearch.index.shard.IllegalIndexShardStateException) ResourceAlreadyExistsException(org.elasticsearch.ResourceAlreadyExistsException) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Aggregations

IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)253 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)194 ClusterState (org.elasticsearch.cluster.ClusterState)124 Settings (org.elasticsearch.common.settings.Settings)104 Index (org.elasticsearch.index.Index)100 Test (org.junit.Test)90 ShardId (org.elasticsearch.index.shard.ShardId)71 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)68 IOException (java.io.IOException)65 Metadata (org.elasticsearch.cluster.metadata.Metadata)62 IndexSettings (org.elasticsearch.index.IndexSettings)62 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)60 MetaData (org.elasticsearch.cluster.metadata.MetaData)58 HashSet (java.util.HashSet)56 HashMap (java.util.HashMap)54 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)54 Map (java.util.Map)50 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)49 ArrayList (java.util.ArrayList)47 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)44