|
11 | 11 | import org.elasticsearch.cluster.ClusterState;
|
12 | 12 | import org.elasticsearch.cluster.metadata.IndexMetadata;
|
13 | 13 | import org.elasticsearch.cluster.node.DiscoveryNodes;
|
14 |
| -import org.elasticsearch.core.Nullable; |
15 | 14 | import org.elasticsearch.common.Strings;
|
16 | 15 | import org.elasticsearch.common.settings.ClusterSettings;
|
17 | 16 | import org.elasticsearch.common.settings.Setting;
|
18 | 17 | import org.elasticsearch.common.settings.Settings;
|
| 18 | +import org.elasticsearch.core.Nullable; |
19 | 19 | import org.elasticsearch.index.IndexNotFoundException;
|
20 | 20 | import org.elasticsearch.index.shard.ShardId;
|
21 | 21 | import org.elasticsearch.node.ResponseCollectorService;
|
@@ -45,14 +45,30 @@ void setUseAdaptiveReplicaSelection(boolean useAdaptiveReplicaSelection) {
|
45 | 45 | this.useAdaptiveReplicaSelection = useAdaptiveReplicaSelection;
|
46 | 46 | }
|
47 | 47 |
|
48 |
| - public ShardIterator indexShards(ClusterState clusterState, String index, String id, @Nullable String routing) { |
49 |
| - return shards(clusterState, index, id, routing).shardsIt(); |
| 48 | + public ShardIterator indexShards( |
| 49 | + ClusterState clusterState, |
| 50 | + String index, |
| 51 | + IndexRouting indexRouting, |
| 52 | + String id, |
| 53 | + @Nullable String routing |
| 54 | + ) { |
| 55 | + return shards(clusterState, index, indexRouting, id, routing).shardsIt(); |
50 | 56 | }
|
51 | 57 |
|
| 58 | + /** |
| 59 | + * Shards to use for a {@code GET} operation. |
| 60 | + */ |
52 | 61 | public ShardIterator getShards(ClusterState clusterState, String index, String id, @Nullable String routing,
|
53 | 62 | @Nullable String preference) {
|
54 |
| - return preferenceActiveShardIterator(shards(clusterState, index, id, routing), clusterState.nodes().getLocalNodeId(), |
55 |
| - clusterState.nodes(), preference, null, null); |
| 63 | + IndexRouting indexRouting = IndexRouting.fromIndexMetadata(indexMetadata(clusterState, index)); |
| 64 | + return preferenceActiveShardIterator( |
| 65 | + shards(clusterState, index, indexRouting, id, routing), |
| 66 | + clusterState.nodes().getLocalNodeId(), |
| 67 | + clusterState.nodes(), |
| 68 | + preference, |
| 69 | + null, |
| 70 | + null |
| 71 | + ); |
56 | 72 | }
|
57 | 73 |
|
58 | 74 | public ShardIterator getShards(ClusterState clusterState, String index, int shardId, @Nullable String preference) {
|
@@ -100,18 +116,16 @@ private Set<IndexShardRoutingTable> computeTargetedShards(ClusterState clusterSt
|
100 | 116 | final Set<IndexShardRoutingTable> set = new HashSet<>();
|
101 | 117 | // we use set here and not list since we might get duplicates
|
102 | 118 | for (String index : concreteIndices) {
|
103 |
| - final IndexRoutingTable indexRouting = indexRoutingTable(clusterState, index); |
| 119 | + final IndexRoutingTable indexRoutingTable = indexRoutingTable(clusterState, index); |
104 | 120 | final IndexMetadata indexMetadata = indexMetadata(clusterState, index);
|
105 |
| - final Set<String> effectiveRouting = routing.get(index); |
106 |
| - if (effectiveRouting != null) { |
107 |
| - for (String r : effectiveRouting) { |
108 |
| - final int routingPartitionSize = indexMetadata.getRoutingPartitionSize(); |
109 |
| - for (int partitionOffset = 0; partitionOffset < routingPartitionSize; partitionOffset++) { |
110 |
| - set.add(RoutingTable.shardRoutingTable(indexRouting, calculateScaledShardId(indexMetadata, r, partitionOffset))); |
111 |
| - } |
| 121 | + final Set<String> indexSearchRouting = routing.get(index); |
| 122 | + if (indexSearchRouting != null) { |
| 123 | + IndexRouting indexRouting = IndexRouting.fromIndexMetadata(indexMetadata); |
| 124 | + for (String r : indexSearchRouting) { |
| 125 | + indexRouting.collectSearchShards(r, s -> set.add(RoutingTable.shardRoutingTable(indexRoutingTable, s))); |
112 | 126 | }
|
113 | 127 | } else {
|
114 |
| - for (IndexShardRoutingTable indexShard : indexRouting) { |
| 128 | + for (IndexShardRoutingTable indexShard : indexRoutingTable) { |
115 | 129 | set.add(indexShard);
|
116 | 130 | }
|
117 | 131 | }
|
@@ -198,51 +212,20 @@ protected IndexRoutingTable indexRoutingTable(ClusterState clusterState, String
|
198 | 212 | return indexRouting;
|
199 | 213 | }
|
200 | 214 |
|
201 |
| - protected IndexMetadata indexMetadata(ClusterState clusterState, String index) { |
| 215 | + private IndexMetadata indexMetadata(ClusterState clusterState, String index) { |
202 | 216 | IndexMetadata indexMetadata = clusterState.metadata().index(index);
|
203 | 217 | if (indexMetadata == null) {
|
204 | 218 | throw new IndexNotFoundException(index);
|
205 | 219 | }
|
206 | 220 | return indexMetadata;
|
207 | 221 | }
|
208 | 222 |
|
209 |
| - protected IndexShardRoutingTable shards(ClusterState clusterState, String index, String id, String routing) { |
210 |
| - int shardId = generateShardId(indexMetadata(clusterState, index), id, routing); |
211 |
| - return clusterState.getRoutingTable().shardRoutingTable(index, shardId); |
| 223 | + private IndexShardRoutingTable shards(ClusterState clusterState, String index, IndexRouting indexRouting, String id, String routing) { |
| 224 | + return clusterState.getRoutingTable().shardRoutingTable(index, indexRouting.shardId(id, routing)); |
212 | 225 | }
|
213 | 226 |
|
214 | 227 | public ShardId shardId(ClusterState clusterState, String index, String id, @Nullable String routing) {
|
215 | 228 | IndexMetadata indexMetadata = indexMetadata(clusterState, index);
|
216 |
| - return new ShardId(indexMetadata.getIndex(), generateShardId(indexMetadata, id, routing)); |
217 |
| - } |
218 |
| - |
219 |
| - public static int generateShardId(IndexMetadata indexMetadata, @Nullable String id, @Nullable String routing) { |
220 |
| - final String effectiveRouting; |
221 |
| - final int partitionOffset; |
222 |
| - |
223 |
| - if (routing == null) { |
224 |
| - assert(indexMetadata.isRoutingPartitionedIndex() == false) : "A routing value is required for gets from a partitioned index"; |
225 |
| - effectiveRouting = id; |
226 |
| - } else { |
227 |
| - effectiveRouting = routing; |
228 |
| - } |
229 |
| - |
230 |
| - if (indexMetadata.isRoutingPartitionedIndex()) { |
231 |
| - partitionOffset = Math.floorMod(Murmur3HashFunction.hash(id), indexMetadata.getRoutingPartitionSize()); |
232 |
| - } else { |
233 |
| - // we would have still got 0 above but this check just saves us an unnecessary hash calculation |
234 |
| - partitionOffset = 0; |
235 |
| - } |
236 |
| - |
237 |
| - return calculateScaledShardId(indexMetadata, effectiveRouting, partitionOffset); |
| 229 | + return new ShardId(indexMetadata.getIndex(), IndexRouting.fromIndexMetadata(indexMetadata).shardId(id, routing)); |
238 | 230 | }
|
239 |
| - |
240 |
| - private static int calculateScaledShardId(IndexMetadata indexMetadata, String effectiveRouting, int partitionOffset) { |
241 |
| - final int hash = Murmur3HashFunction.hash(effectiveRouting) + partitionOffset; |
242 |
| - |
243 |
| - // we don't use IMD#getNumberOfShards since the index might have been shrunk such that we need to use the size |
244 |
| - // of original index to hash documents |
245 |
| - return Math.floorMod(hash, indexMetadata.getRoutingNumShards()) / indexMetadata.getRoutingFactor(); |
246 |
| - } |
247 |
| - |
248 | 231 | }
|
0 commit comments