Skip to content

Commit 6111d5b

Browse files
authored
Fold PlainShardIterator into ShardIterator (#121893) (#122379)
ShardIterator is an interface with a single implementation called PlainShardIterator. This commit makes it a concrete final class and folds its only implementation into it.
1 parent 7bf974c commit 6111d5b

File tree

14 files changed

+117
-173
lines changed

14 files changed

+117
-173
lines changed

server/src/main/java/org/elasticsearch/action/get/TransportGetAction.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.elasticsearch.cluster.ClusterStateObserver;
2828
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
2929
import org.elasticsearch.cluster.node.DiscoveryNode;
30-
import org.elasticsearch.cluster.routing.PlainShardIterator;
3130
import org.elasticsearch.cluster.routing.ShardIterator;
3231
import org.elasticsearch.cluster.service.ClusterService;
3332
import org.elasticsearch.common.io.stream.Writeable;
@@ -107,7 +106,7 @@ protected ShardIterator shards(ClusterState state, InternalRequest request) {
107106
if (iterator == null) {
108107
return null;
109108
}
110-
return PlainShardIterator.allSearchableShards(iterator);
109+
return ShardIterator.allSearchableShards(iterator);
111110
}
112111

113112
@Override

server/src/main/java/org/elasticsearch/action/get/TransportShardMultiGetAction.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.elasticsearch.cluster.ClusterStateObserver;
2828
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
2929
import org.elasticsearch.cluster.node.DiscoveryNode;
30-
import org.elasticsearch.cluster.routing.PlainShardIterator;
3130
import org.elasticsearch.cluster.routing.ShardIterator;
3231
import org.elasticsearch.cluster.service.ClusterService;
3332
import org.elasticsearch.common.io.stream.Writeable;
@@ -111,7 +110,7 @@ protected ShardIterator shards(ClusterState state, InternalRequest request) {
111110
if (iterator == null) {
112111
return null;
113112
}
114-
return PlainShardIterator.allSearchableShards(iterator);
113+
return ShardIterator.allSearchableShards(iterator);
115114
}
116115

117116
@Override

server/src/main/java/org/elasticsearch/action/search/SearchShardIterator.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
package org.elasticsearch.action.search;
1111

1212
import org.elasticsearch.action.OriginalIndices;
13-
import org.elasticsearch.cluster.routing.PlainShardIterator;
1413
import org.elasticsearch.cluster.routing.ShardRouting;
14+
import org.elasticsearch.cluster.routing.ShardsIterator;
1515
import org.elasticsearch.common.util.Countable;
1616
import org.elasticsearch.common.util.PlainIterator;
1717
import org.elasticsearch.core.Nullable;
@@ -24,7 +24,7 @@
2424
import java.util.Objects;
2525

2626
/**
27-
* Extension of {@link PlainShardIterator} used in the search api, which also holds the {@link OriginalIndices}
27+
* Iterator for shards used in the search api, which also holds the {@link OriginalIndices}
2828
* of the search request (useful especially with cross-cluster search, as each cluster has its own set of original indices) as well as
2929
* the cluster alias.
3030
* @see OriginalIndices
@@ -42,7 +42,7 @@ public final class SearchShardIterator implements Comparable<SearchShardIterator
4242
private final PlainIterator<String> targetNodesIterator;
4343

4444
/**
45-
* Creates a {@link PlainShardIterator} instance that iterates over a subset of the given shards
45+
* Creates a {@link SearchShardIterator} instance that iterates over a subset of the given shards
4646
* this the a given <code>shardId</code>.
4747
*
4848
* @param clusterAlias the alias of the cluster where the shard is located
@@ -55,7 +55,7 @@ public SearchShardIterator(@Nullable String clusterAlias, ShardId shardId, List<
5555
}
5656

5757
/**
58-
* Creates a {@link PlainShardIterator} instance that iterates over a subset of the given shards
58+
* Creates a {@link SearchShardIterator} instance that iterates over a subset of the given shards
5959
*
6060
* @param clusterAlias the alias of the cluster where the shard is located
6161
* @param shardId shard id of the group
@@ -103,6 +103,9 @@ public String getClusterAlias() {
103103
return clusterAlias;
104104
}
105105

106+
/**
107+
* Returns the next shard, or {@code null} if none available.
108+
*/
106109
SearchShardTarget nextOrNull() {
107110
final String nodeId = targetNodesIterator.nextOrNull();
108111
if (nodeId != null) {
@@ -111,6 +114,11 @@ SearchShardTarget nextOrNull() {
111114
return null;
112115
}
113116

117+
/**
118+
* Return the number of shards remaining in this {@link ShardsIterator}
119+
*
120+
* @return number of shard remaining
121+
*/
114122
int remaining() {
115123
return targetNodesIterator.remaining();
116124
}
@@ -130,6 +138,9 @@ List<String> getTargetNodeIds() {
130138
return targetNodesIterator.asList();
131139
}
132140

141+
/**
142+
* Resets the iterator to its initial state.
143+
*/
133144
void reset() {
134145
targetNodesIterator.reset();
135146
}
@@ -155,11 +166,19 @@ boolean prefiltered() {
155166
return prefiltered;
156167
}
157168

169+
/**
170+
* The number of shard routing instances.
171+
*
172+
* @return number of shard routing instances in this iterator
173+
*/
158174
@Override
159175
public int size() {
160176
return targetNodesIterator.size();
161177
}
162178

179+
/**
180+
* The shard id this group relates to.
181+
*/
163182
ShardId shardId() {
164183
return shardId;
165184
}

server/src/main/java/org/elasticsearch/cluster/routing/IndexShardRoutingTable.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,11 @@ public List<ShardRouting> unpromotableShards() {
189189
}
190190

191191
public ShardIterator shardsRandomIt() {
192-
return new PlainShardIterator(shardId, shuffler.shuffle(Arrays.asList(shards)));
192+
return new ShardIterator(shardId, shuffler.shuffle(Arrays.asList(shards)));
193193
}
194194

195195
public ShardIterator shardsIt(int seed) {
196-
return new PlainShardIterator(shardId, shuffler.shuffle(Arrays.asList(shards), seed));
196+
return new ShardIterator(shardId, shuffler.shuffle(Arrays.asList(shards), seed));
197197
}
198198

199199
/**
@@ -210,12 +210,12 @@ public ShardIterator activeInitializingShardsRandomIt() {
210210
*/
211211
public ShardIterator activeInitializingShardsIt(int seed) {
212212
if (allInitializingShards.isEmpty()) {
213-
return new PlainShardIterator(shardId, shuffler.shuffle(activeShards, seed));
213+
return new ShardIterator(shardId, shuffler.shuffle(activeShards, seed));
214214
}
215215
ArrayList<ShardRouting> ordered = new ArrayList<>(activeShards.size() + allInitializingShards.size());
216216
ordered.addAll(shuffler.shuffle(activeShards, seed));
217217
ordered.addAll(allInitializingShards);
218-
return new PlainShardIterator(shardId, ordered);
218+
return new ShardIterator(shardId, ordered);
219219
}
220220

221221
/**
@@ -229,18 +229,15 @@ public ShardIterator activeInitializingShardsRankedIt(
229229
) {
230230
final int seed = shuffler.nextSeed();
231231
if (allInitializingShards.isEmpty()) {
232-
return new PlainShardIterator(
233-
shardId,
234-
rankShardsAndUpdateStats(shuffler.shuffle(activeShards, seed), collector, nodeSearchCounts)
235-
);
232+
return new ShardIterator(shardId, rankShardsAndUpdateStats(shuffler.shuffle(activeShards, seed), collector, nodeSearchCounts));
236233
}
237234

238235
ArrayList<ShardRouting> ordered = new ArrayList<>(activeShards.size() + allInitializingShards.size());
239236
List<ShardRouting> rankedActiveShards = rankShardsAndUpdateStats(shuffler.shuffle(activeShards, seed), collector, nodeSearchCounts);
240237
ordered.addAll(rankedActiveShards);
241238
List<ShardRouting> rankedInitializingShards = rankShardsAndUpdateStats(allInitializingShards, collector, nodeSearchCounts);
242239
ordered.addAll(rankedInitializingShards);
243-
return new PlainShardIterator(shardId, ordered);
240+
return new ShardIterator(shardId, ordered);
244241
}
245242

246243
private static Set<String> getAllNodeIds(final List<ShardRouting> shards) {
@@ -398,9 +395,9 @@ public int compare(ShardRouting s1, ShardRouting s2) {
398395
*/
399396
public ShardIterator primaryShardIt() {
400397
if (primary != null) {
401-
return new PlainShardIterator(shardId, Collections.singletonList(primary));
398+
return new ShardIterator(shardId, Collections.singletonList(primary));
402399
}
403-
return new PlainShardIterator(shardId, Collections.emptyList());
400+
return new ShardIterator(shardId, Collections.emptyList());
404401
}
405402

406403
public ShardIterator onlyNodeActiveInitializingShardsIt(String nodeId) {
@@ -416,7 +413,7 @@ public ShardIterator onlyNodeActiveInitializingShardsIt(String nodeId) {
416413
ordered.add(shardRouting);
417414
}
418415
}
419-
return new PlainShardIterator(shardId, ordered);
416+
return new ShardIterator(shardId, ordered);
420417
}
421418

422419
public ShardIterator onlyNodeSelectorActiveInitializingShardsIt(String nodeAttributes, DiscoveryNodes discoveryNodes) {
@@ -451,7 +448,7 @@ public ShardIterator onlyNodeSelectorActiveInitializingShardsIt(String[] nodeAtt
451448
);
452449
throw new IllegalArgumentException(message);
453450
}
454-
return new PlainShardIterator(shardId, ordered);
451+
return new ShardIterator(shardId, ordered);
455452
}
456453

457454
public ShardIterator preferNodeActiveInitializingShardsIt(Set<String> nodeIds) {
@@ -469,7 +466,7 @@ public ShardIterator preferNodeActiveInitializingShardsIt(Set<String> nodeIds) {
469466
if (allInitializingShards.isEmpty() == false) {
470467
preferred.addAll(allInitializingShards);
471468
}
472-
return new PlainShardIterator(shardId, preferred);
469+
return new ShardIterator(shardId, preferred);
473470
}
474471

475472
@Override

server/src/main/java/org/elasticsearch/cluster/routing/OperationRouting.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public ShardIterator useOnlyPromotableShardsForStateless(ShardIterator shards) {
8888
// If it is stateless, only route promotable shards. This is a temporary workaround until a more cohesive solution can be
8989
// implemented for search shards.
9090
if (isStateless && shards != null) {
91-
return new PlainShardIterator(
91+
return new ShardIterator(
9292
shards.shardId(),
9393
shards.getShardRoutings().stream().filter(ShardRouting::isPromotableToPrimary).collect(Collectors.toList())
9494
);
@@ -126,10 +126,10 @@ public List<ShardIterator> searchShards(
126126
nodeCounts
127127
);
128128
if (iterator != null) {
129-
set.add(PlainShardIterator.allSearchableShards(iterator));
129+
set.add(ShardIterator.allSearchableShards(iterator));
130130
}
131131
}
132-
var res = new ArrayList<>(set);
132+
List<ShardIterator> res = new ArrayList<>(set);
133133
CollectionUtil.timSort(res);
134134
return res;
135135
}

server/src/main/java/org/elasticsearch/cluster/routing/PlainShardIterator.java

Lines changed: 0 additions & 73 deletions
This file was deleted.

server/src/main/java/org/elasticsearch/cluster/routing/RoutingTable.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ private List<ShardIterator> allSatisfyingPredicateShardsGrouped(
228228
if (predicate.test(shardRouting)) {
229229
set.add(shardRouting.shardsIt());
230230
} else if (includeEmpty) { // we need this for counting properly, just make it an empty one
231-
set.add(new PlainShardIterator(shardRouting.shardId(), Collections.emptyList()));
231+
set.add(new ShardIterator(shardRouting.shardId(), Collections.emptyList()));
232232
}
233233
}
234234
}
@@ -301,7 +301,7 @@ public List<ShardIterator> activePrimaryShardsGrouped(String[] indices, boolean
301301
if (primary.active()) {
302302
set.add(primary.shardsIt());
303303
} else if (includeEmpty) { // we need this for counting properly, just make it an empty one
304-
set.add(new PlainShardIterator(primary.shardId(), Collections.emptyList()));
304+
set.add(new ShardIterator(primary.shardId(), Collections.emptyList()));
305305
}
306306
}
307307
}

server/src/main/java/org/elasticsearch/cluster/routing/ShardIterator.java

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,65 @@
1111

1212
import org.elasticsearch.index.shard.ShardId;
1313

14+
import java.util.ArrayList;
15+
import java.util.List;
16+
1417
/**
15-
* Allows to iterate over a set of shard instances (routing) within a shard id group.
18+
* The {@link ShardIterator} is a {@link ShardsIterator} which iterates all
19+
* shards of a given {@link ShardId shard id}
1620
*/
17-
public interface ShardIterator extends ShardsIterator, Comparable<ShardIterator> {
21+
public final class ShardIterator extends PlainShardsIterator implements Comparable<ShardIterator> {
22+
23+
private final ShardId shardId;
24+
25+
public static ShardIterator allSearchableShards(ShardIterator shardIterator) {
26+
return new ShardIterator(shardIterator.shardId(), shardsThatCanHandleSearches(shardIterator));
27+
}
28+
29+
private static List<ShardRouting> shardsThatCanHandleSearches(ShardIterator iterator) {
30+
final List<ShardRouting> shardsThatCanHandleSearches = new ArrayList<>(iterator.size());
31+
for (ShardRouting shardRouting : iterator) {
32+
if (shardRouting.isSearchable()) {
33+
shardsThatCanHandleSearches.add(shardRouting);
34+
}
35+
}
36+
return shardsThatCanHandleSearches;
37+
}
1838

1939
/**
20-
* The shard id this group relates to.
40+
* Creates a {@link ShardIterator} instance that iterates all shards
41+
* of a given <code>shardId</code>.
42+
*
43+
* @param shardId shard id of the group
44+
* @param shards shards to iterate
2145
*/
22-
ShardId shardId();
46+
public ShardIterator(ShardId shardId, List<ShardRouting> shards) {
47+
super(shards);
48+
this.shardId = shardId;
49+
}
2350

2451
/**
25-
* Resets the iterator.
52+
* The shard id this group relates to.
2653
*/
54+
public ShardId shardId() {
55+
return this.shardId;
56+
}
57+
58+
@Override
59+
public boolean equals(Object o) {
60+
if (this == o) return true;
61+
if (o == null || getClass() != o.getClass()) return false;
62+
ShardIterator that = (ShardIterator) o;
63+
return shardId.equals(that.shardId());
64+
}
65+
66+
@Override
67+
public int hashCode() {
68+
return shardId.hashCode();
69+
}
70+
2771
@Override
28-
void reset();
72+
public int compareTo(ShardIterator o) {
73+
return shardId.compareTo(o.shardId());
74+
}
2975
}

0 commit comments

Comments
 (0)