Search in sources :

Example 1 with RoutingChangesObserver

use of org.elasticsearch.cluster.routing.RoutingChangesObserver 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)

Aggregations

Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 ClusterState (org.elasticsearch.cluster.ClusterState)1 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)1 MetaData (org.elasticsearch.cluster.metadata.MetaData)1 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)1 RecoverySource (org.elasticsearch.cluster.routing.RecoverySource)1 RoutingChangesObserver (org.elasticsearch.cluster.routing.RoutingChangesObserver)1 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)1 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)1 UnassignedInfo (org.elasticsearch.cluster.routing.UnassignedInfo)1 Sets (org.elasticsearch.common.util.set.Sets)1 Index (org.elasticsearch.index.Index)1 ShardId (org.elasticsearch.index.shard.ShardId)1