Reuse OperationRouting#searchShards(...) to select local enrich shard (#47359)

The currently logic shard selecting logic selects a random shard copy
instead of selecting the local shard copy and if local copy is not
available then selecting a random shard copy. The latter is desired
behaviour for enrich.

By reusing `OperationRouting#searchShards(...)` we get the desired
behaviour and reuse the same logic that the search api is using.
This commit is contained in:
Martijn van Groningen 2019-10-09 17:31:10 +02:00
parent 65f8294378
commit f8ebb75fcf
No known key found for this signature in database
GPG Key ID: AB236F4FCF2AF12A
1 changed files with 7 additions and 1 deletions

View File

@ -23,7 +23,10 @@ import org.elasticsearch.action.support.single.shard.SingleShardRequest;
import org.elasticsearch.action.support.single.shard.TransportSingleShardAction; import org.elasticsearch.action.support.single.shard.TransportSingleShardAction;
import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.routing.GroupShardsIterator;
import org.elasticsearch.cluster.routing.IndexRoutingTable; import org.elasticsearch.cluster.routing.IndexRoutingTable;
import org.elasticsearch.cluster.routing.Preference;
import org.elasticsearch.cluster.routing.ShardIterator;
import org.elasticsearch.cluster.routing.ShardsIterator; import org.elasticsearch.cluster.routing.ShardsIterator;
import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
@ -199,7 +202,10 @@ public class EnrichShardMultiSearchAction extends ActionType<MultiSearchResponse
if (numShards != 1) { if (numShards != 1) {
throw new IllegalStateException("index [" + index + "] should have 1 shard, but has " + numShards + " shards"); throw new IllegalStateException("index [" + index + "] should have 1 shard, but has " + numShards + " shards");
} }
return indexRouting.shard(0).shardsRandomIt();
GroupShardsIterator<ShardIterator> result =
clusterService.operationRouting().searchShards(state, new String[] {index}, null, Preference.LOCAL.type());
return result.get(0);
} }
@Override @Override