
org.elasticsearch.cluster.routing.OperationRouting Maven / Gradle / Ivy
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
package org.elasticsearch.cluster.routing;
import org.elasticsearch.Version;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.node.DiscoveryNodes;
import org.elasticsearch.cluster.routing.allocation.decider.AwarenessAllocationDecider;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.logging.DeprecationCategory;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.index.IndexNotFoundException;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.node.ResponseCollectorService;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import static org.elasticsearch.core.Booleans.parseBoolean;
public class OperationRouting {
public static final Setting USE_ADAPTIVE_REPLICA_SELECTION_SETTING = Setting.boolSetting(
"cluster.routing.use_adaptive_replica_selection",
true,
Setting.Property.Dynamic,
Setting.Property.NodeScope
);
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(OperationRouting.class);
private static final String IGNORE_AWARENESS_ATTRIBUTES_PROPERTY = "es.search.ignore_awareness_attributes";
static final String IGNORE_AWARENESS_ATTRIBUTES_DEPRECATION_MESSAGE =
"searches will not be routed based on awareness attributes starting in version 8.0.0; "
+ "to opt into this behaviour now please set the system property ["
+ IGNORE_AWARENESS_ATTRIBUTES_PROPERTY
+ "] to [true]";
private List awarenessAttributes;
private boolean useAdaptiveReplicaSelection;
public OperationRouting(Settings settings, ClusterSettings clusterSettings) {
// whether to ignore awareness attributes when routing requests
boolean ignoreAwarenessAttr = parseBoolean(System.getProperty(IGNORE_AWARENESS_ATTRIBUTES_PROPERTY), false);
if (ignoreAwarenessAttr == false) {
awarenessAttributes = AwarenessAllocationDecider.CLUSTER_ROUTING_ALLOCATION_AWARENESS_ATTRIBUTE_SETTING.get(settings);
if (awarenessAttributes.isEmpty() == false) {
deprecationLogger.critical(
DeprecationCategory.SETTINGS,
"searches_not_routed_on_awareness_attributes",
IGNORE_AWARENESS_ATTRIBUTES_DEPRECATION_MESSAGE
);
}
clusterSettings.addSettingsUpdateConsumer(
AwarenessAllocationDecider.CLUSTER_ROUTING_ALLOCATION_AWARENESS_ATTRIBUTE_SETTING,
this::setAwarenessAttributes
);
} else {
awarenessAttributes = Collections.emptyList();
}
this.useAdaptiveReplicaSelection = USE_ADAPTIVE_REPLICA_SELECTION_SETTING.get(settings);
clusterSettings.addSettingsUpdateConsumer(USE_ADAPTIVE_REPLICA_SELECTION_SETTING, this::setUseAdaptiveReplicaSelection);
}
void setUseAdaptiveReplicaSelection(boolean useAdaptiveReplicaSelection) {
this.useAdaptiveReplicaSelection = useAdaptiveReplicaSelection;
}
List getAwarenessAttributes() {
return awarenessAttributes;
}
private void setAwarenessAttributes(List awarenessAttributes) {
boolean ignoreAwarenessAttr = parseBoolean(System.getProperty(IGNORE_AWARENESS_ATTRIBUTES_PROPERTY), false);
if (ignoreAwarenessAttr == false) {
if (this.awarenessAttributes.isEmpty() && awarenessAttributes.isEmpty() == false) {
deprecationLogger.critical(
DeprecationCategory.SETTINGS,
"searches_not_routed_on_awareness_attributes",
IGNORE_AWARENESS_ATTRIBUTES_DEPRECATION_MESSAGE
);
}
this.awarenessAttributes = awarenessAttributes;
}
}
public ShardIterator indexShards(
ClusterState clusterState,
String index,
IndexRouting indexRouting,
String id,
@Nullable String routing
) {
return shards(clusterState, index, indexRouting, id, routing).shardsIt();
}
/**
* Shards to use for a {@code GET} operation.
*/
public ShardIterator getShards(
ClusterState clusterState,
String index,
String id,
@Nullable String routing,
@Nullable String preference
) {
IndexRouting indexRouting = IndexRouting.fromIndexMetadata(indexMetadata(clusterState, index));
return preferenceActiveShardIterator(
shards(clusterState, index, indexRouting, id, routing),
clusterState.nodes().getLocalNodeId(),
clusterState.nodes(),
preference,
null,
null
);
}
public ShardIterator getShards(ClusterState clusterState, String index, int shardId, @Nullable String preference) {
final IndexShardRoutingTable indexShard = clusterState.getRoutingTable().shardRoutingTable(index, shardId);
return preferenceActiveShardIterator(
indexShard,
clusterState.nodes().getLocalNodeId(),
clusterState.nodes(),
preference,
null,
null
);
}
public GroupShardsIterator searchShards(
ClusterState clusterState,
String[] concreteIndices,
@Nullable Map> routing,
@Nullable String preference
) {
return searchShards(clusterState, concreteIndices, routing, preference, null, null);
}
public GroupShardsIterator searchShards(
ClusterState clusterState,
String[] concreteIndices,
@Nullable Map> routing,
@Nullable String preference,
@Nullable ResponseCollectorService collectorService,
@Nullable Map nodeCounts
) {
final Set shards = computeTargetedShards(clusterState, concreteIndices, routing);
final Set set = new HashSet<>(shards.size());
for (IndexShardRoutingTable shard : shards) {
ShardIterator iterator = preferenceActiveShardIterator(
shard,
clusterState.nodes().getLocalNodeId(),
clusterState.nodes(),
preference,
collectorService,
nodeCounts
);
if (iterator != null) {
set.add(iterator);
}
}
return GroupShardsIterator.sortAndCreate(new ArrayList<>(set));
}
public static ShardIterator getShards(ClusterState clusterState, ShardId shardId) {
final IndexShardRoutingTable shard = clusterState.routingTable().shardRoutingTable(shardId);
return shard.activeInitializingShardsRandomIt();
}
private static final Map> EMPTY_ROUTING = Collections.emptyMap();
private Set computeTargetedShards(
ClusterState clusterState,
String[] concreteIndices,
@Nullable Map> routing
) {
routing = routing == null ? EMPTY_ROUTING : routing; // just use an empty map
final Set set = new HashSet<>();
// we use set here and not list since we might get duplicates
for (String index : concreteIndices) {
final IndexRoutingTable indexRoutingTable = indexRoutingTable(clusterState, index);
final IndexMetadata indexMetadata = indexMetadata(clusterState, index);
final Set indexSearchRouting = routing.get(index);
if (indexSearchRouting != null) {
IndexRouting indexRouting = IndexRouting.fromIndexMetadata(indexMetadata);
for (String r : indexSearchRouting) {
indexRouting.collectSearchShards(r, s -> set.add(RoutingTable.shardRoutingTable(indexRoutingTable, s)));
}
} else {
for (IndexShardRoutingTable indexShard : indexRoutingTable) {
set.add(indexShard);
}
}
}
return set;
}
private ShardIterator preferenceActiveShardIterator(
IndexShardRoutingTable indexShard,
String localNodeId,
DiscoveryNodes nodes,
@Nullable String preference,
@Nullable ResponseCollectorService collectorService,
@Nullable Map nodeCounts
) {
if (preference == null || preference.isEmpty()) {
return shardRoutings(indexShard, nodes, collectorService, nodeCounts);
}
if (preference.charAt(0) == '_') {
Preference preferenceType = Preference.parse(preference);
if (preferenceType == Preference.SHARDS) {
// starts with _shards, so execute on specific ones
int index = preference.indexOf('|');
String shards;
if (index == -1) {
shards = preference.substring(Preference.SHARDS.type().length() + 1);
} else {
shards = preference.substring(Preference.SHARDS.type().length() + 1, index);
}
String[] ids = Strings.splitStringByCommaToArray(shards);
boolean found = false;
for (String id : ids) {
if (Integer.parseInt(id) == indexShard.shardId().id()) {
found = true;
break;
}
}
if (found == false) {
return null;
}
// no more preference
if (index == -1 || index == preference.length() - 1) {
return shardRoutings(indexShard, nodes, collectorService, nodeCounts);
} else {
// update the preference and continue
preference = preference.substring(index + 1);
}
}
preferenceType = Preference.parse(preference);
switch (preferenceType) {
case PREFER_NODES:
final Set nodesIds = Arrays.stream(preference.substring(Preference.PREFER_NODES.type().length() + 1).split(","))
.collect(Collectors.toSet());
return indexShard.preferNodeActiveInitializingShardsIt(nodesIds);
case LOCAL:
return indexShard.preferNodeActiveInitializingShardsIt(Collections.singleton(localNodeId));
case ONLY_LOCAL:
return indexShard.onlyNodeActiveInitializingShardsIt(localNodeId);
case ONLY_NODES:
String nodeAttributes = preference.substring(Preference.ONLY_NODES.type().length() + 1);
return indexShard.onlyNodeSelectorActiveInitializingShardsIt(nodeAttributes.split(","), nodes);
default:
throw new IllegalArgumentException("unknown preference [" + preferenceType + "]");
}
}
// if not, then use it as the index
int routingHash = Murmur3HashFunction.hash(preference);
if (nodes.getMinNodeVersion().onOrAfter(Version.V_6_0_0_alpha1)) {
// The AllocationService lists shards in a fixed order based on nodes
// so earlier versions of this class would have a tendency to
// select the same node across different shardIds.
// Better overall balancing can be achieved if each shardId opts
// for a different element in the list by also incorporating the
// shard ID into the hash of the user-supplied preference key.
routingHash = 31 * routingHash + indexShard.shardId.hashCode();
}
if (awarenessAttributes.isEmpty()) {
return indexShard.activeInitializingShardsIt(routingHash);
} else {
return indexShard.preferAttributesActiveInitializingShardsIt(awarenessAttributes, nodes, routingHash);
}
}
private ShardIterator shardRoutings(
IndexShardRoutingTable indexShard,
DiscoveryNodes nodes,
@Nullable ResponseCollectorService collectorService,
@Nullable Map nodeCounts
) {
if (awarenessAttributes.isEmpty()) {
if (useAdaptiveReplicaSelection) {
return indexShard.activeInitializingShardsRankedIt(collectorService, nodeCounts);
} else {
return indexShard.activeInitializingShardsRandomIt();
}
} else {
return indexShard.preferAttributesActiveInitializingShardsIt(awarenessAttributes, nodes);
}
}
protected IndexRoutingTable indexRoutingTable(ClusterState clusterState, String index) {
IndexRoutingTable indexRouting = clusterState.routingTable().index(index);
if (indexRouting == null) {
throw new IndexNotFoundException(index);
}
return indexRouting;
}
private IndexMetadata indexMetadata(ClusterState clusterState, String index) {
IndexMetadata indexMetadata = clusterState.metadata().index(index);
if (indexMetadata == null) {
throw new IndexNotFoundException(index);
}
return indexMetadata;
}
private IndexShardRoutingTable shards(ClusterState clusterState, String index, IndexRouting indexRouting, String id, String routing) {
return clusterState.getRoutingTable().shardRoutingTable(index, indexRouting.shardId(id, routing));
}
public ShardId shardId(ClusterState clusterState, String index, String id, @Nullable String routing) {
IndexMetadata indexMetadata = indexMetadata(clusterState, index);
return new ShardId(indexMetadata.getIndex(), IndexRouting.fromIndexMetadata(indexMetadata).shardId(id, routing));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy