-
Notifications
You must be signed in to change notification settings - Fork 24.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simplify shardsWithState #91991
Simplify shardsWithState #91991
Changes from 1 commit
e2e96a8
a49170c
76b5269
a4017d2
f0ac7c7
e2742c5
159aa53
4a316c2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,9 @@ | |
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Stream; | ||
|
||
import static java.util.stream.Collectors.toCollection; | ||
|
||
/** | ||
* A {@link RoutingNode} represents a cluster node associated with a single {@link DiscoveryNode} including all shards | ||
|
@@ -215,25 +218,11 @@ void remove(ShardRouting shard) { | |
* @return number of shards | ||
*/ | ||
public int numberOfShardsWithState(ShardRoutingState... states) { | ||
if (states.length == 1) { | ||
if (states[0] == ShardRoutingState.INITIALIZING) { | ||
return initializingShards.size(); | ||
} else if (states[0] == ShardRoutingState.RELOCATING) { | ||
return relocatingShards.size(); | ||
} else if (states[0] == ShardRoutingState.STARTED) { | ||
return startedShards.size(); | ||
} | ||
} | ||
return Stream.of(states).mapToInt(this::numberOfShardsWithState).sum(); | ||
} | ||
|
||
int count = 0; | ||
for (ShardRouting shardEntry : this) { | ||
for (ShardRoutingState state : states) { | ||
if (shardEntry.state() == state) { | ||
count++; | ||
} | ||
} | ||
} | ||
return count; | ||
public int numberOfShardsWithState(ShardRoutingState state) { | ||
return getShardsWithState(state).size(); | ||
} | ||
|
||
/** | ||
|
@@ -242,20 +231,7 @@ public int numberOfShardsWithState(ShardRoutingState... states) { | |
* @return List of shards | ||
*/ | ||
public List<ShardRouting> shardsWithState(ShardRoutingState state) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAICT this method is only used in two places in prod code, both of which could reasonably accept a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This has plenty of unit test usages that needs to be updated as well. Do you mind if I do it in a followup pr (possibly with moving some of this methods to a test code)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sounds good |
||
if (state == ShardRoutingState.INITIALIZING) { | ||
return new ArrayList<>(initializingShards); | ||
} else if (state == ShardRoutingState.RELOCATING) { | ||
return new ArrayList<>(relocatingShards); | ||
} else if (state == ShardRoutingState.STARTED) { | ||
return new ArrayList<>(startedShards); | ||
} | ||
List<ShardRouting> shards = new ArrayList<>(); | ||
for (ShardRouting shardEntry : this) { | ||
if (shardEntry.state() == state) { | ||
shards.add(shardEntry); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This branch was effectively unreachable. |
||
return shards; | ||
return new ArrayList<>(getShardsWithState(state)); | ||
} | ||
|
||
private static final ShardRouting[] EMPTY_SHARD_ROUTING_ARRAY = new ShardRouting[0]; | ||
|
@@ -279,49 +255,28 @@ public ShardRouting[] started() { | |
* @return a list of shards | ||
*/ | ||
public List<ShardRouting> shardsWithState(String index, ShardRoutingState... states) { | ||
List<ShardRouting> shards = new ArrayList<>(); | ||
|
||
if (states.length == 1) { | ||
if (states[0] == ShardRoutingState.INITIALIZING) { | ||
for (ShardRouting shardEntry : initializingShards) { | ||
if (shardEntry.getIndexName().equals(index) == false) { | ||
continue; | ||
} | ||
shards.add(shardEntry); | ||
} | ||
return shards; | ||
} else if (states[0] == ShardRoutingState.RELOCATING) { | ||
for (ShardRouting shardEntry : relocatingShards) { | ||
if (shardEntry.getIndexName().equals(index) == false) { | ||
continue; | ||
} | ||
shards.add(shardEntry); | ||
} | ||
return shards; | ||
} else if (states[0] == ShardRoutingState.STARTED) { | ||
for (ShardRouting shardEntry : startedShards) { | ||
if (shardEntry.getIndexName().equals(index) == false) { | ||
continue; | ||
} | ||
shards.add(shardEntry); | ||
} | ||
return shards; | ||
} | ||
} | ||
return Stream.of(states).flatMap(state -> shardsWithState(index, state).stream()).collect(toCollection(ArrayList::new)); | ||
} | ||
|
||
for (ShardRouting shardEntry : this) { | ||
if (shardEntry.getIndexName().equals(index) == false) { | ||
continue; | ||
} | ||
for (ShardRoutingState state : states) { | ||
if (shardEntry.state() == state) { | ||
shards.add(shardEntry); | ||
} | ||
public List<ShardRouting> shardsWithState(String index, ShardRoutingState state) { | ||
var shards = new ArrayList<ShardRouting>(); | ||
for (ShardRouting shardEntry : getShardsWithState(state)) { | ||
if (shardEntry.getIndexName().equals(index)) { | ||
shards.add(shardEntry); | ||
} | ||
} | ||
return shards; | ||
} | ||
|
||
private LinkedHashSet<ShardRouting> getShardsWithState(ShardRoutingState state) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO it's confusing to have |
||
return switch (state) { | ||
case UNASSIGNED -> throw new IllegalArgumentException("Unassigned shards are not linked to a routing node"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was previously returning empty list or 0 but I guess it is better to be explicit that routing node can not be used to obtain unassigned shards. |
||
case INITIALIZING -> initializingShards; | ||
case STARTED -> startedShards; | ||
case RELOCATING -> relocatingShards; | ||
}; | ||
} | ||
|
||
/** | ||
* The number of shards on this node that will not be eventually relocated. | ||
*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we inline this? It's only used in a handful of tests.