diff --git a/server/src/main/java/org/elasticsearch/action/get/TransportGetAction.java b/server/src/main/java/org/elasticsearch/action/get/TransportGetAction.java index c08eec6dd3cc1..b19dd9cd12437 100644 --- a/server/src/main/java/org/elasticsearch/action/get/TransportGetAction.java +++ b/server/src/main/java/org/elasticsearch/action/get/TransportGetAction.java @@ -13,10 +13,7 @@ import org.elasticsearch.action.support.single.shard.TransportSingleShardAction; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; -import org.elasticsearch.cluster.node.DiscoveryNode; -import org.elasticsearch.cluster.routing.PlainShardIterator; import org.elasticsearch.cluster.routing.ShardIterator; -import org.elasticsearch.cluster.routing.ShardRouting; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.io.stream.Writeable; @@ -30,7 +27,6 @@ import org.elasticsearch.transport.TransportService; import java.io.IOException; -import java.util.stream.Collectors; /** * Performs the get operation. @@ -79,19 +75,7 @@ protected ShardIterator shards(ClusterState state, InternalRequest request) { request.request().routing(), request.request().preference() ); - if (iterator == null) { - return null; - } - // If it is stateless, only route isPromotableToPrimary shards. This is a temporary workaround until a more cohesive solution can be - // implemented for search shards. - if (DiscoveryNode.isStateless(clusterService.getSettings())) { - return new PlainShardIterator( - iterator.shardId(), - iterator.getShardRoutings().stream().filter(ShardRouting::isPromotableToPrimary).collect(Collectors.toList()) - ); - } else { - return iterator; - } + return clusterService.operationRouting().useOnlyPromotableShardsForStateless(iterator); } @Override diff --git a/server/src/main/java/org/elasticsearch/action/get/TransportShardMultiGetAction.java b/server/src/main/java/org/elasticsearch/action/get/TransportShardMultiGetAction.java index a173ff4407968..b164e2e10ddde 100644 --- a/server/src/main/java/org/elasticsearch/action/get/TransportShardMultiGetAction.java +++ b/server/src/main/java/org/elasticsearch/action/get/TransportShardMultiGetAction.java @@ -15,10 +15,7 @@ import org.elasticsearch.action.support.single.shard.TransportSingleShardAction; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; -import org.elasticsearch.cluster.node.DiscoveryNode; -import org.elasticsearch.cluster.routing.PlainShardIterator; import org.elasticsearch.cluster.routing.ShardIterator; -import org.elasticsearch.cluster.routing.ShardRouting; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.io.stream.Writeable; @@ -32,7 +29,6 @@ import org.elasticsearch.transport.TransportService; import java.io.IOException; -import java.util.stream.Collectors; import static org.elasticsearch.core.Strings.format; @@ -87,19 +83,7 @@ protected boolean resolveIndex(MultiGetShardRequest request) { protected ShardIterator shards(ClusterState state, InternalRequest request) { ShardIterator iterator = clusterService.operationRouting() .getShards(state, request.request().index(), request.request().shardId(), request.request().preference()); - if (iterator == null) { - return null; - } - // If it is stateless, only route promotable shards. This is a temporary workaround until a more cohesive solution can be - // implemented for search shards. - if (DiscoveryNode.isStateless(clusterService.getSettings())) { - return new PlainShardIterator( - iterator.shardId(), - iterator.getShardRoutings().stream().filter(ShardRouting::isPromotableToPrimary).collect(Collectors.toList()) - ); - } else { - return iterator; - } + return clusterService.operationRouting().useOnlyPromotableShardsForStateless(iterator); } @Override diff --git a/server/src/main/java/org/elasticsearch/action/termvectors/TransportShardMultiTermsVectorAction.java b/server/src/main/java/org/elasticsearch/action/termvectors/TransportShardMultiTermsVectorAction.java index fbd0cb66a2506..3499496097580 100644 --- a/server/src/main/java/org/elasticsearch/action/termvectors/TransportShardMultiTermsVectorAction.java +++ b/server/src/main/java/org/elasticsearch/action/termvectors/TransportShardMultiTermsVectorAction.java @@ -76,8 +76,9 @@ protected boolean resolveIndex(MultiTermVectorsShardRequest request) { @Override protected ShardIterator shards(ClusterState state, InternalRequest request) { - return clusterService.operationRouting() + ShardIterator shards = clusterService.operationRouting() .getShards(state, request.concreteIndex(), request.request().shardId(), request.request().preference()); + return clusterService.operationRouting().useOnlyPromotableShardsForStateless(shards); } @Override diff --git a/server/src/main/java/org/elasticsearch/action/termvectors/TransportTermVectorsAction.java b/server/src/main/java/org/elasticsearch/action/termvectors/TransportTermVectorsAction.java index eb07ff914efe9..0877e789b67b9 100644 --- a/server/src/main/java/org/elasticsearch/action/termvectors/TransportTermVectorsAction.java +++ b/server/src/main/java/org/elasticsearch/action/termvectors/TransportTermVectorsAction.java @@ -67,8 +67,9 @@ protected ShardIterator shards(ClusterState state, InternalRequest request) { return groupShardsIter.iterator().next(); } - return clusterService.operationRouting() + ShardIterator shards = clusterService.operationRouting() .getShards(state, request.concreteIndex(), request.request().id(), request.request().routing(), request.request().preference()); + return clusterService.operationRouting().useOnlyPromotableShardsForStateless(shards); } @Override diff --git a/server/src/main/java/org/elasticsearch/cluster/routing/OperationRouting.java b/server/src/main/java/org/elasticsearch/cluster/routing/OperationRouting.java index 31817cb80b0c7..c4b14c8c16417 100644 --- a/server/src/main/java/org/elasticsearch/cluster/routing/OperationRouting.java +++ b/server/src/main/java/org/elasticsearch/cluster/routing/OperationRouting.java @@ -10,6 +10,7 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexMetadata; +import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.node.DiscoveryNodes; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.ClusterSettings; @@ -39,8 +40,10 @@ public class OperationRouting { ); private boolean useAdaptiveReplicaSelection; + private final boolean isStateless; public OperationRouting(Settings settings, ClusterSettings clusterSettings) { + this.isStateless = DiscoveryNode.isStateless(settings); this.useAdaptiveReplicaSelection = USE_ADAPTIVE_REPLICA_SELECTION_SETTING.get(settings); clusterSettings.addSettingsUpdateConsumer(USE_ADAPTIVE_REPLICA_SELECTION_SETTING, this::setUseAdaptiveReplicaSelection); } @@ -76,6 +79,19 @@ public ShardIterator getShards(ClusterState clusterState, String index, int shar ); } + public ShardIterator useOnlyPromotableShardsForStateless(ShardIterator shards) { + // If it is stateless, only route promotable shards. This is a temporary workaround until a more cohesive solution can be + // implemented for search shards. + if (isStateless && shards != null) { + return new PlainShardIterator( + shards.shardId(), + shards.getShardRoutings().stream().filter(ShardRouting::isPromotableToPrimary).collect(Collectors.toList()) + ); + } else { + return shards; + } + } + public GroupShardsIterator searchShards( ClusterState clusterState, String[] concreteIndices,