Search in sources :

Example 61 with ShardId

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

the class TransportIndicesShardStoresAction method masterOperation.

@Override
protected void masterOperation(IndicesShardStoresRequest request, ClusterState state, ActionListener<IndicesShardStoresResponse> listener) {
    final RoutingTable routingTables = state.routingTable();
    final RoutingNodes routingNodes = state.getRoutingNodes();
    final String[] concreteIndices = indexNameExpressionResolver.concreteIndexNames(state, request);
    final Set<ShardId> shardIdsToFetch = new HashSet<>();
    logger.trace("using cluster state version [{}] to determine shards", state.version());
    // collect relevant shard ids of the requested indices for fetching store infos
    for (String index : concreteIndices) {
        IndexRoutingTable indexShardRoutingTables = routingTables.index(index);
        if (indexShardRoutingTables == null) {
            continue;
        }
        for (IndexShardRoutingTable routing : indexShardRoutingTables) {
            final int shardId = routing.shardId().id();
            ClusterShardHealth shardHealth = new ClusterShardHealth(shardId, routing);
            if (request.shardStatuses().contains(shardHealth.getStatus())) {
                shardIdsToFetch.add(routing.shardId());
            }
        }
    }
    // async fetch store infos from all the nodes
    // NOTE: instead of fetching shard store info one by one from every node (nShards * nNodes requests)
    // we could fetch all shard store info from every node once (nNodes requests)
    // we have to implement a TransportNodesAction instead of using TransportNodesListGatewayStartedShards
    // for fetching shard stores info, that operates on a list of shards instead of a single shard
    new AsyncShardStoresInfoFetches(state.nodes(), routingNodes, shardIdsToFetch, listener).start();
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) RoutingNodes(org.elasticsearch.cluster.routing.RoutingNodes) ClusterShardHealth(org.elasticsearch.cluster.health.ClusterShardHealth) HashSet(java.util.HashSet)

Example 62 with ShardId

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

the class TransportShrinkAction method prepareCreateIndexRequest.

// static for unittesting this method
static CreateIndexClusterStateUpdateRequest prepareCreateIndexRequest(final ShrinkRequest shrinkRequest, final ClusterState state, final IntFunction<DocsStats> perShardDocStats, IndexNameExpressionResolver indexNameExpressionResolver) {
    final String sourceIndex = indexNameExpressionResolver.resolveDateMathExpression(shrinkRequest.getSourceIndex());
    final CreateIndexRequest targetIndex = shrinkRequest.getShrinkIndexRequest();
    final String targetIndexName = indexNameExpressionResolver.resolveDateMathExpression(targetIndex.index());
    final IndexMetaData metaData = state.metaData().index(sourceIndex);
    final Settings targetIndexSettings = Settings.builder().put(targetIndex.settings()).normalizePrefix(IndexMetaData.INDEX_SETTING_PREFIX).build();
    int numShards = 1;
    if (IndexMetaData.INDEX_NUMBER_OF_SHARDS_SETTING.exists(targetIndexSettings)) {
        numShards = IndexMetaData.INDEX_NUMBER_OF_SHARDS_SETTING.get(targetIndexSettings);
    }
    for (int i = 0; i < numShards; i++) {
        Set<ShardId> shardIds = IndexMetaData.selectShrinkShards(i, metaData, numShards);
        long count = 0;
        for (ShardId id : shardIds) {
            DocsStats docsStats = perShardDocStats.apply(id.id());
            if (docsStats != null) {
                count += docsStats.getCount();
            }
            if (count > IndexWriter.MAX_DOCS) {
                throw new IllegalStateException("Can't merge index with more than [" + IndexWriter.MAX_DOCS + "] docs - too many documents in shards " + shardIds);
            }
        }
    }
    if (IndexMetaData.INDEX_ROUTING_PARTITION_SIZE_SETTING.exists(targetIndexSettings)) {
        throw new IllegalArgumentException("cannot provide a routing partition size value when shrinking an index");
    }
    targetIndex.cause("shrink_index");
    Settings.Builder settingsBuilder = Settings.builder().put(targetIndexSettings);
    settingsBuilder.put("index.number_of_shards", numShards);
    targetIndex.settings(settingsBuilder);
    return new CreateIndexClusterStateUpdateRequest(targetIndex, "shrink_index", targetIndex.index(), targetIndexName, true).ackTimeout(targetIndex.timeout()).masterNodeTimeout(targetIndex.masterNodeTimeout()).settings(targetIndex.settings()).aliases(targetIndex.aliases()).customs(targetIndex.customs()).waitForActiveShards(targetIndex.waitForActiveShards()).shrinkFrom(metaData.getIndex());
}
Also used : IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) ShardId(org.elasticsearch.index.shard.ShardId) CreateIndexClusterStateUpdateRequest(org.elasticsearch.action.admin.indices.create.CreateIndexClusterStateUpdateRequest) DocsStats(org.elasticsearch.index.shard.DocsStats) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) Settings(org.elasticsearch.common.settings.Settings)

Example 63 with ShardId

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

the class TransportShardBulkAction method executeIndexRequestOnReplica.

/**
     * Execute the given {@link IndexRequest} on a replica shard, throwing a
     * {@link RetryOnReplicaException} if the operation needs to be re-tried.
     */
public static Engine.IndexResult executeIndexRequestOnReplica(DocWriteResponse primaryResponse, IndexRequest request, IndexShard replica) throws IOException {
    final ShardId shardId = replica.shardId();
    SourceToParse sourceToParse = SourceToParse.source(SourceToParse.Origin.REPLICA, shardId.getIndexName(), request.type(), request.id(), request.source(), request.getContentType()).routing(request.routing()).parent(request.parent());
    final Engine.Index operation;
    final long version = primaryResponse.getVersion();
    final VersionType versionType = request.versionType().versionTypeForReplicationAndRecovery();
    assert versionType.validateVersionForWrites(version);
    final long seqNo = primaryResponse.getSeqNo();
    try {
        operation = replica.prepareIndexOnReplica(sourceToParse, seqNo, version, versionType, request.getAutoGeneratedTimestamp(), request.isRetry());
    } catch (MapperParsingException e) {
        return new Engine.IndexResult(e, version, seqNo);
    }
    Mapping update = operation.parsedDoc().dynamicMappingsUpdate();
    if (update != null) {
        throw new RetryOnReplicaException(shardId, "Mappings are not available on the replica yet, triggered update: " + update);
    }
    return replica.index(operation);
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) SourceToParse(org.elasticsearch.index.mapper.SourceToParse) Mapping(org.elasticsearch.index.mapper.Mapping) Engine(org.elasticsearch.index.engine.Engine) VersionType(org.elasticsearch.index.VersionType)

Example 64 with ShardId

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

the class TransportFieldStatsAction method shardOperation.

@Override
protected FieldStatsShardResponse shardOperation(FieldStatsShardRequest request) {
    ShardId shardId = request.shardId();
    Map<String, FieldStats<?>> fieldStats = new HashMap<>();
    IndexService indexServices = indicesService.indexServiceSafe(shardId.getIndex());
    IndexShard shard = indexServices.getShard(shardId.id());
    try (Engine.Searcher searcher = shard.acquireSearcher("fieldstats")) {
        // Resolve patterns and deduplicate
        Set<String> fieldNames = new HashSet<>();
        for (String field : request.getFields()) {
            fieldNames.addAll(shard.mapperService().simpleMatchToIndexNames(field));
        }
        for (String field : fieldNames) {
            FieldStats<?> stats = indicesService.getFieldStats(shard, searcher, field, request.shouldUseCache());
            if (stats != null) {
                fieldStats.put(field, stats);
            }
        }
    } catch (Exception e) {
        throw ExceptionsHelper.convertToElastic(e);
    }
    return new FieldStatsShardResponse(shardId, fieldStats);
}
Also used : HashMap(java.util.HashMap) IndexService(org.elasticsearch.index.IndexService) IndexShard(org.elasticsearch.index.shard.IndexShard) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) BroadcastShardOperationFailedException(org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException) ShardOperationFailedException(org.elasticsearch.action.ShardOperationFailedException) DefaultShardOperationFailedException(org.elasticsearch.action.support.DefaultShardOperationFailedException) ShardId(org.elasticsearch.index.shard.ShardId) Engine(org.elasticsearch.index.engine.Engine) HashSet(java.util.HashSet)

Example 65 with ShardId

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

the class IndexMetaData method selectShrinkShards.

/**
     * Returns the source shard ids to shrink into the given shard id.
     * @param shardId the id of the target shard to shrink to
     * @param sourceIndexMetadata the source index metadata
     * @param numTargetShards the total number of shards in the target index
     * @return a set of shard IDs to shrink into the given shard ID.
     */
public static Set<ShardId> selectShrinkShards(int shardId, IndexMetaData sourceIndexMetadata, int numTargetShards) {
    if (shardId >= numTargetShards) {
        throw new IllegalArgumentException("the number of target shards (" + numTargetShards + ") must be greater than the shard id: " + shardId);
    }
    int routingFactor = getRoutingFactor(sourceIndexMetadata, numTargetShards);
    Set<ShardId> shards = new HashSet<>(routingFactor);
    for (int i = shardId * routingFactor; i < routingFactor * shardId + routingFactor; i++) {
        shards.add(new ShardId(sourceIndexMetadata.getIndex(), i));
    }
    return shards;
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) HashSet(java.util.HashSet)

Aggregations

ShardId (org.elasticsearch.index.shard.ShardId)478 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)106 Index (org.elasticsearch.index.Index)101 ClusterState (org.elasticsearch.cluster.ClusterState)98 Test (org.junit.Test)83 ArrayList (java.util.ArrayList)78 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)69 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)67 HashMap (java.util.HashMap)66 IOException (java.io.IOException)61 IndexShard (org.elasticsearch.index.shard.IndexShard)61 Map (java.util.Map)55 List (java.util.List)51 Settings (org.elasticsearch.common.settings.Settings)49 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)45 UnassignedInfo (org.elasticsearch.cluster.routing.UnassignedInfo)44 HashSet (java.util.HashSet)43 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)43 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)42 Matchers.containsString (org.hamcrest.Matchers.containsString)42