Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.elasticsearch.cluster.ClusterStateObserver;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.routing.PlainShardIterator;
import org.elasticsearch.cluster.routing.ShardIterator;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.io.stream.Writeable;
Expand Down Expand Up @@ -108,7 +107,7 @@ protected ShardIterator shards(ClusterState state, InternalRequest request) {
if (iterator == null) {
return null;
}
return PlainShardIterator.allSearchableShards(iterator);
return ShardIterator.allSearchableShards(iterator);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.elasticsearch.cluster.ClusterStateObserver;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.routing.PlainShardIterator;
import org.elasticsearch.cluster.routing.ShardIterator;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.io.stream.Writeable;
Expand Down Expand Up @@ -112,7 +111,7 @@ protected ShardIterator shards(ClusterState state, InternalRequest request) {
if (iterator == null) {
return null;
}
return PlainShardIterator.allSearchableShards(iterator);
return ShardIterator.allSearchableShards(iterator);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
package org.elasticsearch.action.search;

import org.elasticsearch.action.OriginalIndices;
import org.elasticsearch.cluster.routing.PlainShardIterator;
import org.elasticsearch.cluster.routing.ShardIterator;
import org.elasticsearch.cluster.routing.ShardRouting;
import org.elasticsearch.common.util.Countable;
import org.elasticsearch.common.util.PlainIterator;
Expand All @@ -24,7 +24,7 @@
import java.util.Objects;

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

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

/**
* Creates a {@link PlainShardIterator} instance that iterates over a subset of the given shards
* Creates a {@link ShardIterator} instance that iterates over a subset of the given shards
*
* @param clusterAlias the alias of the cluster where the shard is located
* @param shardId shard id of the group
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,11 @@ protected boolean resolveIndex(MultiTermVectorsShardRequest request) {

@Override
protected ShardIterator shards(ClusterState state, InternalRequest request) {
ShardIterator shards = clusterService.operationRouting()
.getShards(state, request.concreteIndex(), request.request().shardId(), request.request().preference());
return clusterService.operationRouting().useOnlyPromotableShardsForStateless(shards);
return clusterService.operationRouting()
.useOnlyPromotableShardsForStateless(
clusterService.operationRouting()
.getShards(state, request.concreteIndex(), request.request().shardId(), request.request().preference())
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public TransportTermVectorsAction(

}

@Override
protected ShardIterator shards(ClusterState state, InternalRequest request) {
final var operationRouting = clusterService.operationRouting();
if (request.request().doc() != null && request.request().routing() == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
import java.util.List;

/**
* This class implements a compilation of {@link ShardIterator}s. Each {@link ShardIterator}
* iterated by this {@link Iterable} represents a group of shards.
* ShardsIterators are always returned in ascending order independently of their order at construction
* This class implements a compilation of shard iterators. Each shard iterator iterated by this {@link Iterable} represents a group of
* shards. ShardsIterators are always returned in ascending order independently of their order at construction
* time. The incoming iterators are sorted to ensure consistent iteration behavior across Nodes / JVMs.
*/
public final class GroupShardsIterator<ShardIt extends Comparable<ShardIt> & Countable> implements Iterable<ShardIt> {
Expand All @@ -27,7 +26,7 @@ public final class GroupShardsIterator<ShardIt extends Comparable<ShardIt> & Cou

/**
* Constructs a new sorted GroupShardsIterator from the given list. Items are sorted based on their natural ordering.
* @see PlainShardIterator#compareTo(ShardIterator)
* @see ShardIterator#compareTo(ShardIterator)
*/
public static <ShardIt extends Comparable<ShardIt> & Countable> GroupShardsIterator<ShardIt> sortAndCreate(List<ShardIt> iterators) {
CollectionUtil.timSort(iterators);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,11 @@ public List<ShardRouting> unpromotableShards() {
}

public ShardIterator shardsRandomIt() {
return new PlainShardIterator(shardId, shuffler.shuffle(Arrays.asList(shards)));
return new ShardIterator(shardId, shuffler.shuffle(Arrays.asList(shards)));
}

public ShardIterator shardsIt(int seed) {
return new PlainShardIterator(shardId, shuffler.shuffle(Arrays.asList(shards), seed));
return new ShardIterator(shardId, shuffler.shuffle(Arrays.asList(shards), seed));
}

/**
Expand All @@ -210,12 +210,12 @@ public ShardIterator activeInitializingShardsRandomIt() {
*/
public ShardIterator activeInitializingShardsIt(int seed) {
if (allInitializingShards.isEmpty()) {
return new PlainShardIterator(shardId, shuffler.shuffle(activeShards, seed));
return new ShardIterator(shardId, shuffler.shuffle(activeShards, seed));
}
ArrayList<ShardRouting> ordered = new ArrayList<>(activeShards.size() + allInitializingShards.size());
ordered.addAll(shuffler.shuffle(activeShards, seed));
ordered.addAll(allInitializingShards);
return new PlainShardIterator(shardId, ordered);
return new ShardIterator(shardId, ordered);
}

/**
Expand All @@ -229,18 +229,15 @@ public ShardIterator activeInitializingShardsRankedIt(
) {
final int seed = shuffler.nextSeed();
if (allInitializingShards.isEmpty()) {
return new PlainShardIterator(
shardId,
rankShardsAndUpdateStats(shuffler.shuffle(activeShards, seed), collector, nodeSearchCounts)
);
return new ShardIterator(shardId, rankShardsAndUpdateStats(shuffler.shuffle(activeShards, seed), collector, nodeSearchCounts));
}

ArrayList<ShardRouting> ordered = new ArrayList<>(activeShards.size() + allInitializingShards.size());
List<ShardRouting> rankedActiveShards = rankShardsAndUpdateStats(shuffler.shuffle(activeShards, seed), collector, nodeSearchCounts);
ordered.addAll(rankedActiveShards);
List<ShardRouting> rankedInitializingShards = rankShardsAndUpdateStats(allInitializingShards, collector, nodeSearchCounts);
ordered.addAll(rankedInitializingShards);
return new PlainShardIterator(shardId, ordered);
return new ShardIterator(shardId, ordered);
}

private static Set<String> getAllNodeIds(final List<ShardRouting> shards) {
Expand Down Expand Up @@ -398,9 +395,9 @@ public int compare(ShardRouting s1, ShardRouting s2) {
*/
public ShardIterator primaryShardIt() {
if (primary != null) {
return new PlainShardIterator(shardId, Collections.singletonList(primary));
return new ShardIterator(shardId, Collections.singletonList(primary));
}
return new PlainShardIterator(shardId, Collections.emptyList());
return new ShardIterator(shardId, Collections.emptyList());
}

public ShardIterator onlyNodeActiveInitializingShardsIt(String nodeId) {
Expand All @@ -416,7 +413,7 @@ public ShardIterator onlyNodeActiveInitializingShardsIt(String nodeId) {
ordered.add(shardRouting);
}
}
return new PlainShardIterator(shardId, ordered);
return new ShardIterator(shardId, ordered);
}

public ShardIterator onlyNodeSelectorActiveInitializingShardsIt(String nodeAttributes, DiscoveryNodes discoveryNodes) {
Expand Down Expand Up @@ -451,7 +448,7 @@ public ShardIterator onlyNodeSelectorActiveInitializingShardsIt(String[] nodeAtt
);
throw new IllegalArgumentException(message);
}
return new PlainShardIterator(shardId, ordered);
return new ShardIterator(shardId, ordered);
}

public ShardIterator preferNodeActiveInitializingShardsIt(Set<String> nodeIds) {
Expand All @@ -469,7 +466,7 @@ public ShardIterator preferNodeActiveInitializingShardsIt(Set<String> nodeIds) {
if (allInitializingShards.isEmpty() == false) {
preferred.addAll(allInitializingShards);
}
return new PlainShardIterator(shardId, preferred);
return new ShardIterator(shardId, preferred);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public ShardIterator useOnlyPromotableShardsForStateless(ShardIterator shards) {
// If it is stateless, only route promotable shards. This is a temporary workaround until a more cohesive solution can be
// implemented for search shards.
if (isStateless && shards != null) {
return new PlainShardIterator(
return new ShardIterator(
shards.shardId(),
shards.getShardRoutings().stream().filter(ShardRouting::isPromotableToPrimary).collect(Collectors.toList())
);
Expand Down Expand Up @@ -124,7 +124,7 @@ public GroupShardsIterator<ShardIterator> searchShards(
nodeCounts
);
if (iterator != null) {
set.add(PlainShardIterator.allSearchableShards(iterator));
set.add(ShardIterator.allSearchableShards(iterator));
}
}
return GroupShardsIterator.sortAndCreate(new ArrayList<>(set));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ private GroupShardsIterator<ShardIterator> allSatisfyingPredicateShardsGrouped(
if (predicate.test(shardRouting)) {
set.add(shardRouting.shardsIt());
} else if (includeEmpty) { // we need this for counting properly, just make it an empty one
set.add(new PlainShardIterator(shardRouting.shardId(), Collections.emptyList()));
set.add(new ShardIterator(shardRouting.shardId(), Collections.emptyList()));
}
}
}
Expand Down Expand Up @@ -299,7 +299,7 @@ public GroupShardsIterator<ShardIterator> activePrimaryShardsGrouped(String[] in
if (primary.active()) {
set.add(primary.shardsIt());
} else if (includeEmpty) { // we need this for counting properly, just make it an empty one
set.add(new PlainShardIterator(primary.shardId(), Collections.emptyList()));
set.add(new ShardIterator(primary.shardId(), Collections.emptyList()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,61 @@

import org.elasticsearch.index.shard.ShardId;

import java.util.ArrayList;
import java.util.List;

/**
* Allows to iterate over a set of shard instances (routing) within a shard id group.
* The {@link ShardIterator} iterates shard copies for a given {@link ShardId shard id}
*/
public interface ShardIterator extends ShardsIterator, Comparable<ShardIterator> {
public class ShardIterator extends PlainShardsIterator implements Comparable<ShardIterator> {

/**
* The shard id this group relates to.
*/
ShardId shardId();
private final ShardId shardId;

public static ShardIterator allSearchableShards(ShardIterator shardIterator) {
return new ShardIterator(shardIterator.shardId(), shardsThatCanHandleSearches(shardIterator));
}

private static List<ShardRouting> shardsThatCanHandleSearches(ShardIterator iterator) {
final List<ShardRouting> shardsThatCanHandleSearches = new ArrayList<>(iterator.size());
for (ShardRouting shardRouting : iterator) {
if (shardRouting.isSearchable()) {
shardsThatCanHandleSearches.add(shardRouting);
}
}
return shardsThatCanHandleSearches;
}

/**
* Resets the iterator.
* Creates a {@link ShardIterator} instance that iterates over a subset of the given shards
* this the a given <code>shardId</code>.
*
* @param shardId shard id of the group
* @param shards shards to iterate
*/
public ShardIterator(ShardId shardId, List<ShardRouting> shards) {
super(shards);
this.shardId = shardId;
}

public ShardId shardId() {
return this.shardId;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ShardIterator that = (ShardIterator) o;
return shardId.equals(that.shardId());
}

@Override
public int hashCode() {
return shardId.hashCode();
}

@Override
void reset();
public int compareTo(ShardIterator o) {
return shardId.compareTo(o.shardId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ public ShardId shardId() {
* A shard iterator with just this shard in it.
*/
public ShardIterator shardsIt() {
return new PlainShardIterator(shardId, List.of(this));
return new ShardIterator(shardId, List.of(this));
}

public ShardRouting(ShardId shardId, StreamInput in) throws IOException {
Expand Down
Loading