Skip to content

Commit 521e267

Browse files
authored
Remove reset usages and remaining method from SearchShardIterator (#121934) (#122760)
remaining is used in tests only since the recent change to counting in AbstractSearchAsyncAction. reset was used in a couple of places where it does not seem like it's needed anymore.
1 parent a32611c commit 521e267

File tree

4 files changed

+9
-34
lines changed

4 files changed

+9
-34
lines changed

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
import org.elasticsearch.action.OriginalIndices;
1313
import org.elasticsearch.cluster.routing.ShardRouting;
14-
import org.elasticsearch.cluster.routing.ShardsIterator;
1514
import org.elasticsearch.common.util.PlainIterator;
1615
import org.elasticsearch.core.Nullable;
1716
import org.elasticsearch.core.TimeValue;
@@ -113,15 +112,6 @@ SearchShardTarget nextOrNull() {
113112
return null;
114113
}
115114

116-
/**
117-
* Return the number of shards remaining in this {@link ShardsIterator}
118-
*
119-
* @return number of shard remaining
120-
*/
121-
int remaining() {
122-
return targetNodesIterator.remaining();
123-
}
124-
125115
/**
126116
* Returns a non-null value if this request should use a specific search context instead of the latest one.
127117
*/

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@
2222
import org.elasticsearch.cluster.service.ClusterService;
2323
import org.elasticsearch.index.Index;
2424
import org.elasticsearch.index.query.Rewriteable;
25-
import org.elasticsearch.index.shard.ShardId;
2625
import org.elasticsearch.injection.guice.Inject;
2726
import org.elasticsearch.search.SearchService;
28-
import org.elasticsearch.search.SearchShardTarget;
2927
import org.elasticsearch.search.builder.SearchSourceBuilder;
3028
import org.elasticsearch.search.internal.AliasFilter;
3129
import org.elasticsearch.tasks.Task;
@@ -173,15 +171,7 @@ public void searchShards(Task task, SearchShardsRequest searchShardsRequest, Act
173171
private static List<SearchShardsGroup> toGroups(List<SearchShardIterator> shardIts) {
174172
List<SearchShardsGroup> groups = new ArrayList<>(shardIts.size());
175173
for (SearchShardIterator shardIt : shardIts) {
176-
boolean skip = shardIt.skip();
177-
shardIt.reset();
178-
List<String> targetNodes = new ArrayList<>();
179-
SearchShardTarget target;
180-
while ((target = shardIt.nextOrNull()) != null) {
181-
targetNodes.add(target.getNodeId());
182-
}
183-
ShardId shardId = shardIt.shardId();
184-
groups.add(new SearchShardsGroup(shardId, targetNodes, skip));
174+
groups.add(new SearchShardsGroup(shardIt.shardId(), shardIt.getTargetNodeIds(), shardIt.skip()));
185175
}
186176
return groups;
187177
}

server/src/test/java/org/elasticsearch/action/search/AbstractSearchAsyncActionTests.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,6 @@ public void testShardNotAvailableWithDisallowPartialFailures() {
230230
// skip one to avoid the "all shards failed" failure.
231231
SearchShardIterator skipIterator = new SearchShardIterator(null, null, Collections.emptyList(), null);
232232
skipIterator.skip(true);
233-
skipIterator.reset();
234233
action.skipShard(skipIterator);
235234
assertThat(exception.get(), instanceOf(SearchPhaseExecutionException.class));
236235
SearchPhaseExecutionException searchPhaseExecutionException = (SearchPhaseExecutionException) exception.get();

server/src/test/java/org/elasticsearch/action/search/SearchAsyncActionTests.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ public void testAllowPartialResults() throws InterruptedException {
545545

546546
SearchTransportService transportService = new SearchTransportService(null, null, null);
547547
Map<String, Transport.Connection> lookup = new HashMap<>();
548-
Map<ShardId, Boolean> seenShard = new ConcurrentHashMap<>();
548+
Map<ShardId, AtomicInteger> seenShard = new ConcurrentHashMap<>();
549549
lookup.put(primaryNode.getId(), new MockConnection(primaryNode));
550550
lookup.put(replicaNode.getId(), new MockConnection(replicaNode));
551551
Map<String, AliasFilter> aliasFilters = Collections.singletonMap("_na_", AliasFilter.EMPTY);
@@ -581,17 +581,18 @@ protected void executePhaseOnShard(
581581
Transport.Connection connection,
582582
SearchActionListener<TestSearchPhaseResult> listener
583583
) {
584-
seenShard.computeIfAbsent(shardIt.shardId(), (i) -> {
584+
AtomicInteger retries = seenShard.computeIfAbsent(shardIt.shardId(), (i) -> {
585585
numRequests.incrementAndGet(); // only count this once per shard copy
586-
return Boolean.TRUE;
586+
return new AtomicInteger(0);
587587
});
588+
int numRetries = retries.incrementAndGet();
588589
new Thread(() -> {
589590
TestSearchPhaseResult testSearchPhaseResult = new TestSearchPhaseResult(
590591
new ShardSearchContextId(UUIDs.randomBase64UUID(), contextIdGenerator.incrementAndGet()),
591592
connection.getNode()
592593
);
593594
try {
594-
if (shardIt.remaining() > 0) {
595+
if (numRetries < shardIt.size()) {
595596
numFailReplicas.incrementAndGet();
596597
listener.onFailure(new RuntimeException());
597598
} else {
@@ -643,10 +644,8 @@ public void testSkipUnavailableSearchShards() throws InterruptedException {
643644
);
644645
// Skip all the shards
645646
searchShardIterator.skip(true);
646-
searchShardIterator.reset();
647647
searchShardIterators.add(searchShardIterator);
648648
}
649-
List<SearchShardIterator> shardsIter = searchShardIterators;
650649
Map<String, Transport.Connection> lookup = Map.of(primaryNode.getId(), new MockConnection(primaryNode));
651650

652651
CountDownLatch latch = new CountDownLatch(1);
@@ -665,11 +664,11 @@ public void testSkipUnavailableSearchShards() throws InterruptedException {
665664
null,
666665
request,
667666
responseListener,
668-
shardsIter,
667+
searchShardIterators,
669668
new TransportSearchAction.SearchTimeProvider(0, 0, () -> 0),
670669
ClusterState.EMPTY_STATE,
671670
null,
672-
new ArraySearchPhaseResults<>(shardsIter.size()),
671+
new ArraySearchPhaseResults<>(searchShardIterators.size()),
673672
request.getMaxConcurrentShardRequests(),
674673
SearchResponse.Clusters.EMPTY
675674
) {
@@ -702,7 +701,7 @@ public void run() {
702701
assertNotNull(searchResponse.get());
703702
assertThat(searchResponse.get().getSkippedShards(), equalTo(numUnavailableSkippedShards));
704703
assertThat(searchResponse.get().getFailedShards(), equalTo(0));
705-
assertThat(searchResponse.get().getSuccessfulShards(), equalTo(shardsIter.size()));
704+
assertThat(searchResponse.get().getSuccessfulShards(), equalTo(searchShardIterators.size()));
706705
}
707706

708707
static List<SearchShardIterator> getShardsIter(
@@ -728,7 +727,6 @@ static List<SearchShardIterator> getShardsIter(
728727
for (int i = 0; i < numShards; i++) {
729728
ArrayList<ShardRouting> started = new ArrayList<>();
730729
ArrayList<ShardRouting> initializing = new ArrayList<>();
731-
ArrayList<ShardRouting> unassigned = new ArrayList<>();
732730

733731
ShardRouting routing = ShardRouting.newUnassigned(
734732
new ShardId(index, i),
@@ -758,8 +756,6 @@ static List<SearchShardIterator> getShardsIter(
758756
} else {
759757
initializing.add(routing);
760758
}
761-
} else {
762-
unassigned.add(routing); // unused yet
763759
}
764760
}
765761
Collections.shuffle(started, random());

0 commit comments

Comments
 (0)