Skip to content

Commit 628c778

Browse files
committed
Fold PlainShardIterator into ShardIterator
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 ed04ffa commit 628c778

File tree

12 files changed

+112
-124
lines changed

12 files changed

+112
-124
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
@@ -28,7 +28,6 @@
2828
import org.elasticsearch.cluster.ClusterStateObserver;
2929
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
3030
import org.elasticsearch.cluster.node.DiscoveryNode;
31-
import org.elasticsearch.cluster.routing.PlainShardIterator;
3231
import org.elasticsearch.cluster.routing.ShardIterator;
3332
import org.elasticsearch.cluster.service.ClusterService;
3433
import org.elasticsearch.common.io.stream.Writeable;
@@ -108,7 +107,7 @@ protected ShardIterator shards(ClusterState state, InternalRequest request) {
108107
if (iterator == null) {
109108
return null;
110109
}
111-
return PlainShardIterator.allSearchableShards(iterator);
110+
return ShardIterator.allSearchableShards(iterator);
112111
}
113112

114113
@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
@@ -28,7 +28,6 @@
2828
import org.elasticsearch.cluster.ClusterStateObserver;
2929
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
3030
import org.elasticsearch.cluster.node.DiscoveryNode;
31-
import org.elasticsearch.cluster.routing.PlainShardIterator;
3231
import org.elasticsearch.cluster.routing.ShardIterator;
3332
import org.elasticsearch.cluster.service.ClusterService;
3433
import org.elasticsearch.common.io.stream.Writeable;
@@ -112,7 +111,7 @@ protected ShardIterator shards(ClusterState state, InternalRequest request) {
112111
if (iterator == null) {
113112
return null;
114113
}
115-
return PlainShardIterator.allSearchableShards(iterator);
114+
return ShardIterator.allSearchableShards(iterator);
116115
}
117116

118117
@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 & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,11 @@ public List<ShardRouting> unpromotableShards() {
207207
}
208208

209209
public ShardIterator shardsRandomIt() {
210-
return new PlainShardIterator(shardId, shuffler.shuffle(Arrays.asList(shards)));
210+
return new ShardIterator(shardId, shuffler.shuffle(Arrays.asList(shards)));
211211
}
212212

213213
public ShardIterator shardsIt(int seed) {
214-
return new PlainShardIterator(shardId, shuffler.shuffle(Arrays.asList(shards), seed));
214+
return new ShardIterator(shardId, shuffler.shuffle(Arrays.asList(shards), seed));
215215
}
216216

217217
/**
@@ -228,12 +228,12 @@ public ShardIterator activeInitializingShardsRandomIt() {
228228
*/
229229
public ShardIterator activeInitializingShardsIt(int seed) {
230230
if (allInitializingShards.isEmpty()) {
231-
return new PlainShardIterator(shardId, shuffler.shuffle(activeShards, seed));
231+
return new ShardIterator(shardId, shuffler.shuffle(activeShards, seed));
232232
}
233233
ArrayList<ShardRouting> ordered = new ArrayList<>(activeShards.size() + allInitializingShards.size());
234234
ordered.addAll(shuffler.shuffle(activeShards, seed));
235235
ordered.addAll(allInitializingShards);
236-
return new PlainShardIterator(shardId, ordered);
236+
return new ShardIterator(shardId, ordered);
237237
}
238238

239239
/**
@@ -247,7 +247,7 @@ public ShardIterator activeInitializingShardsRankedIt(
247247
) {
248248
final int seed = shuffler.nextSeed();
249249
if (allInitializingShards.isEmpty()) {
250-
return new PlainShardIterator(
250+
return new ShardIterator(
251251
shardId,
252252
rankShardsAndUpdateStats(shuffler.shuffle(activeShards, seed), collector, nodeSearchCounts)
253253
);
@@ -258,7 +258,7 @@ public ShardIterator activeInitializingShardsRankedIt(
258258
ordered.addAll(rankedActiveShards);
259259
List<ShardRouting> rankedInitializingShards = rankShardsAndUpdateStats(allInitializingShards, collector, nodeSearchCounts);
260260
ordered.addAll(rankedInitializingShards);
261-
return new PlainShardIterator(shardId, ordered);
261+
return new ShardIterator(shardId, ordered);
262262
}
263263

264264
private static Set<String> getAllNodeIds(final List<ShardRouting> shards) {
@@ -416,9 +416,9 @@ public int compare(ShardRouting s1, ShardRouting s2) {
416416
*/
417417
public ShardIterator primaryShardIt() {
418418
if (primary != null) {
419-
return new PlainShardIterator(shardId, Collections.singletonList(primary));
419+
return new ShardIterator(shardId, Collections.singletonList(primary));
420420
}
421-
return new PlainShardIterator(shardId, Collections.emptyList());
421+
return new ShardIterator(shardId, Collections.emptyList());
422422
}
423423

424424
public ShardIterator onlyNodeActiveInitializingShardsIt(String nodeId) {
@@ -434,7 +434,7 @@ public ShardIterator onlyNodeActiveInitializingShardsIt(String nodeId) {
434434
ordered.add(shardRouting);
435435
}
436436
}
437-
return new PlainShardIterator(shardId, ordered);
437+
return new ShardIterator(shardId, ordered);
438438
}
439439

440440
public ShardIterator onlyNodeSelectorActiveInitializingShardsIt(String nodeAttributes, DiscoveryNodes discoveryNodes) {
@@ -469,7 +469,7 @@ public ShardIterator onlyNodeSelectorActiveInitializingShardsIt(String[] nodeAtt
469469
);
470470
throw new IllegalArgumentException(message);
471471
}
472-
return new PlainShardIterator(shardId, ordered);
472+
return new ShardIterator(shardId, ordered);
473473
}
474474

475475
public ShardIterator preferNodeActiveInitializingShardsIt(Set<String> nodeIds) {
@@ -487,7 +487,7 @@ public ShardIterator preferNodeActiveInitializingShardsIt(Set<String> nodeIds) {
487487
if (allInitializingShards.isEmpty() == false) {
488488
preferred.addAll(allInitializingShards);
489489
}
490-
return new PlainShardIterator(shardId, preferred);
490+
return new ShardIterator(shardId, preferred);
491491
}
492492

493493
@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 or 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 over a subset of the given shards
41+
* this the 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)