Search in sources :

Example 36 with RoutingTable

use of org.elasticsearch.cluster.routing.RoutingTable in project elasticsearch by elastic.

the class IndexMetaDataUpdater method applyChanges.

/**
     * Updates the current {@link MetaData} based on the changes of this RoutingChangesObserver. Specifically
     * we update {@link IndexMetaData#getInSyncAllocationIds()} and {@link IndexMetaData#primaryTerm(int)} based on
     * the changes made during this allocation.
     *
     * @param oldMetaData {@link MetaData} object from before the routing nodes was changed.
     * @param newRoutingTable {@link RoutingTable} object after routing changes were applied.
     * @return adapted {@link MetaData}, potentially the original one if no change was needed.
     */
public MetaData applyChanges(MetaData oldMetaData, RoutingTable newRoutingTable) {
    Map<Index, List<Map.Entry<ShardId, Updates>>> changesGroupedByIndex = shardChanges.entrySet().stream().collect(Collectors.groupingBy(e -> e.getKey().getIndex()));
    MetaData.Builder metaDataBuilder = null;
    for (Map.Entry<Index, List<Map.Entry<ShardId, Updates>>> indexChanges : changesGroupedByIndex.entrySet()) {
        Index index = indexChanges.getKey();
        final IndexMetaData oldIndexMetaData = oldMetaData.getIndexSafe(index);
        IndexMetaData.Builder indexMetaDataBuilder = null;
        for (Map.Entry<ShardId, Updates> shardEntry : indexChanges.getValue()) {
            ShardId shardId = shardEntry.getKey();
            Updates updates = shardEntry.getValue();
            indexMetaDataBuilder = updateInSyncAllocations(newRoutingTable, oldIndexMetaData, indexMetaDataBuilder, shardId, updates);
            indexMetaDataBuilder = updatePrimaryTerm(oldIndexMetaData, indexMetaDataBuilder, shardId, updates);
        }
        if (indexMetaDataBuilder != null) {
            if (metaDataBuilder == null) {
                metaDataBuilder = MetaData.builder(oldMetaData);
            }
            metaDataBuilder.put(indexMetaDataBuilder);
        }
    }
    if (metaDataBuilder != null) {
        return metaDataBuilder.build();
    } else {
        return oldMetaData;
    }
}
Also used : MetaData(org.elasticsearch.cluster.metadata.MetaData) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) ShardId(org.elasticsearch.index.shard.ShardId) Set(java.util.Set) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) HashMap(java.util.HashMap) RoutingChangesObserver(org.elasticsearch.cluster.routing.RoutingChangesObserver) UnassignedInfo(org.elasticsearch.cluster.routing.UnassignedInfo) Index(org.elasticsearch.index.Index) Collectors(java.util.stream.Collectors) Sets(org.elasticsearch.common.util.set.Sets) HashSet(java.util.HashSet) RecoverySource(org.elasticsearch.cluster.routing.RecoverySource) ClusterState(org.elasticsearch.cluster.ClusterState) List(java.util.List) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) Map(java.util.Map) Comparator(java.util.Comparator) Collections(java.util.Collections) Index(org.elasticsearch.index.Index) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) ShardId(org.elasticsearch.index.shard.ShardId) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 37 with RoutingTable

use of org.elasticsearch.cluster.routing.RoutingTable in project elasticsearch by elastic.

the class AllocationService method buildResultAndLogHealthChange.

protected ClusterState buildResultAndLogHealthChange(ClusterState oldState, RoutingAllocation allocation, String reason) {
    RoutingTable oldRoutingTable = oldState.routingTable();
    RoutingNodes newRoutingNodes = allocation.routingNodes();
    final RoutingTable newRoutingTable = new RoutingTable.Builder().updateNodes(oldRoutingTable.version(), newRoutingNodes).build();
    MetaData newMetaData = allocation.updateMetaDataWithRoutingChanges(newRoutingTable);
    // validates the routing table is coherent with the cluster state metadata
    assert newRoutingTable.validate(newMetaData);
    final ClusterState.Builder newStateBuilder = ClusterState.builder(oldState).routingTable(newRoutingTable).metaData(newMetaData);
    final RestoreInProgress restoreInProgress = allocation.custom(RestoreInProgress.TYPE);
    if (restoreInProgress != null) {
        RestoreInProgress updatedRestoreInProgress = allocation.updateRestoreInfoWithRoutingChanges(restoreInProgress);
        if (updatedRestoreInProgress != restoreInProgress) {
            ImmutableOpenMap.Builder<String, ClusterState.Custom> customsBuilder = ImmutableOpenMap.builder(allocation.getCustoms());
            customsBuilder.put(RestoreInProgress.TYPE, updatedRestoreInProgress);
            newStateBuilder.customs(customsBuilder.build());
        }
    }
    final ClusterState newState = newStateBuilder.build();
    logClusterHealthStateChange(new ClusterStateHealth(oldState), new ClusterStateHealth(newState), reason);
    return newState;
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterStateHealth(org.elasticsearch.cluster.health.ClusterStateHealth) RestoreInProgress(org.elasticsearch.cluster.RestoreInProgress) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) RoutingNodes(org.elasticsearch.cluster.routing.RoutingNodes) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap)

Example 38 with RoutingTable

use of org.elasticsearch.cluster.routing.RoutingTable in project elasticsearch by elastic.

the class IndicesClusterStateService method removeShards.

/**
     * Removes shards that are currently loaded by indicesService but have disappeared from the routing table of the current node.
     * Also removes shards where the recovery source node has changed.
     * This method does not delete the shard data.
     *
     * @param state new cluster state
     */
private void removeShards(final ClusterState state) {
    final RoutingTable routingTable = state.routingTable();
    final DiscoveryNodes nodes = state.nodes();
    final String localNodeId = state.nodes().getLocalNodeId();
    assert localNodeId != null;
    // remove shards based on routing nodes (no deletion of data)
    RoutingNode localRoutingNode = state.getRoutingNodes().node(localNodeId);
    for (AllocatedIndex<? extends Shard> indexService : indicesService) {
        for (Shard shard : indexService) {
            ShardRouting currentRoutingEntry = shard.routingEntry();
            ShardId shardId = currentRoutingEntry.shardId();
            ShardRouting newShardRouting = localRoutingNode == null ? null : localRoutingNode.getByShardId(shardId);
            if (newShardRouting == null) {
                // we can just remove the shard without cleaning it locally, since we will clean it in IndicesStore
                // once all shards are allocated
                logger.debug("{} removing shard (not allocated)", shardId);
                indexService.removeShard(shardId.id(), "removing shard (not allocated)");
            } else if (newShardRouting.isSameAllocation(currentRoutingEntry) == false) {
                logger.debug("{} removing shard (stale allocation id, stale {}, new {})", shardId, currentRoutingEntry, newShardRouting);
                indexService.removeShard(shardId.id(), "removing shard (stale copy)");
            } else if (newShardRouting.initializing() && currentRoutingEntry.active()) {
                // this can happen if the node was isolated/gc-ed, rejoins the cluster and a new shard with the same allocation id
                // is assigned to it. Batch cluster state processing or if shard fetching completes before the node gets a new cluster
                // state may result in a new shard being initialized while having the same allocation id as the currently started shard.
                logger.debug("{} removing shard (not active, current {}, new {})", shardId, currentRoutingEntry, newShardRouting);
                indexService.removeShard(shardId.id(), "removing shard (stale copy)");
            } else {
                // remove shards where recovery source has changed. This re-initializes shards later in createOrUpdateShards
                if (newShardRouting.recoverySource() != null && newShardRouting.recoverySource().getType() == Type.PEER) {
                    RecoveryState recoveryState = shard.recoveryState();
                    final DiscoveryNode sourceNode = findSourceNodeForPeerRecovery(logger, routingTable, nodes, newShardRouting);
                    if (recoveryState.getSourceNode().equals(sourceNode) == false) {
                        if (recoveryTargetService.cancelRecoveriesForShard(shardId, "recovery source node changed")) {
                            // getting here means that the shard was still recovering
                            logger.debug("{} removing shard (recovery source changed), current [{}], global [{}], shard [{}])", shardId, recoveryState.getSourceNode(), sourceNode, newShardRouting);
                            indexService.removeShard(shardId.id(), "removing shard (recovery source node changed)");
                        }
                    }
                }
            }
        }
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) IndexShard(org.elasticsearch.index.shard.IndexShard) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) RecoveryState(org.elasticsearch.indices.recovery.RecoveryState) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes)

Example 39 with RoutingTable

use of org.elasticsearch.cluster.routing.RoutingTable in project elasticsearch by elastic.

the class IndicesClusterStateService method createOrUpdateShards.

private void createOrUpdateShards(final ClusterState state) {
    RoutingNode localRoutingNode = state.getRoutingNodes().node(state.nodes().getLocalNodeId());
    if (localRoutingNode == null) {
        return;
    }
    DiscoveryNodes nodes = state.nodes();
    RoutingTable routingTable = state.routingTable();
    for (final ShardRouting shardRouting : localRoutingNode) {
        ShardId shardId = shardRouting.shardId();
        if (failedShardsCache.containsKey(shardId) == false) {
            AllocatedIndex<? extends Shard> indexService = indicesService.indexService(shardId.getIndex());
            assert indexService != null : "index " + shardId.getIndex() + " should have been created by createIndices";
            Shard shard = indexService.getShardOrNull(shardId.id());
            if (shard == null) {
                assert shardRouting.initializing() : shardRouting + " should have been removed by failMissingShards";
                createShard(nodes, routingTable, shardRouting, state);
            } else {
                updateShard(nodes, shardRouting, shard, routingTable, state);
            }
        }
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) IndexShard(org.elasticsearch.index.shard.IndexShard) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes)

Example 40 with RoutingTable

use of org.elasticsearch.cluster.routing.RoutingTable in project elasticsearch by elastic.

the class TransportShrinkActionTests method createClusterState.

private ClusterState createClusterState(String name, int numShards, int numReplicas, Settings settings) {
    MetaData.Builder metaBuilder = MetaData.builder();
    IndexMetaData indexMetaData = IndexMetaData.builder(name).settings(settings(Version.CURRENT).put(settings)).numberOfShards(numShards).numberOfReplicas(numReplicas).build();
    metaBuilder.put(indexMetaData, false);
    MetaData metaData = metaBuilder.build();
    RoutingTable.Builder routingTableBuilder = RoutingTable.builder();
    routingTableBuilder.addAsNew(metaData.index(name));
    RoutingTable routingTable = routingTableBuilder.build();
    ClusterState clusterState = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).blocks(ClusterBlocks.builder().addBlocks(indexMetaData)).build();
    return clusterState;
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Aggregations

RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)228 ClusterState (org.elasticsearch.cluster.ClusterState)201 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)140 MetaData (org.elasticsearch.cluster.metadata.MetaData)135 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)74 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)66 RoutingNodes (org.elasticsearch.cluster.routing.RoutingNodes)64 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)59 Metadata (org.elasticsearch.cluster.metadata.Metadata)57 IndexRoutingTable (org.elasticsearch.cluster.routing.IndexRoutingTable)51 AllocationService (org.elasticsearch.cluster.routing.allocation.AllocationService)48 ShardId (org.elasticsearch.index.shard.ShardId)37 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)33 Settings (org.elasticsearch.common.settings.Settings)33 Index (org.elasticsearch.index.Index)31 Matchers.containsString (org.hamcrest.Matchers.containsString)30 HashSet (java.util.HashSet)29 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)29 ImmutableOpenMap (org.elasticsearch.common.collect.ImmutableOpenMap)29 ClusterSettings (org.elasticsearch.common.settings.ClusterSettings)28