Skip to content

Simplify usages of shardsWithState#92055

Merged
idegtiarenko merged 7 commits intoelastic:mainfrom
idegtiarenko:simplify_shardsWithState_2
Dec 2, 2022
Merged

Simplify usages of shardsWithState#92055
idegtiarenko merged 7 commits intoelastic:mainfrom
idegtiarenko:simplify_shardsWithState_2

Conversation

@idegtiarenko
Copy link
Copy Markdown
Contributor

This is a followup to #91991

@idegtiarenko idegtiarenko added >non-issue :Distributed/Allocation All issues relating to the decision making around placing a shard (both master logic & on the nodes) Team:Distributed Meta label for distributed team. v8.7.0 labels Dec 2, 2022
Copy link
Copy Markdown
Member

@DaveCTurner DaveCTurner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, assuming CI is happy. Since this is only test changes, can we backport to 8.6 too?

}
if (elements instanceof Collection) {
return new ArrayList<>((Collection) elements);
return new ArrayList<>((Collection<E>) elements);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prevent raw usage of parameterized class

@idegtiarenko idegtiarenko marked this pull request as ready for review December 2, 2022 09:32
@elasticsearchmachine
Copy link
Copy Markdown
Collaborator

Pinging @elastic/es-distributed (Team:Distributed)

? iterableAsArrayList(routingNodes.unassigned())
: routingNodes.stream()
.flatMap(routingNode -> routingNode.shardsWithState(state).stream())
.collect(toCollection(ArrayList::new));
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some tests modify the output of this method (eg shuffle)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd rather have those tests copy the result into a new list instead and make this immutable. It's only a couple of spots to change I think.

}
}
return shards;
public static List<ShardRouting> shardsWithState(RoutingNodes routingNodes, String index, ShardRoutingState states) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of the callers of this method need the mutability AFAICT?

? iterableAsArrayList(routingNodes.unassigned())
: routingNodes.stream()
.flatMap(routingNode -> routingNode.shardsWithState(state).stream())
.collect(toCollection(ArrayList::new));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd rather have those tests copy the result into a new list instead and make this immutable. It's only a couple of spots to change I think.

List<ShardRouting> startedShards = RoutingNodesHelper.shardsWithState(state.getRoutingNodes(), ShardRoutingState.STARTED);
Collections.shuffle(startedShards, random());
return state.nodes().get(startedShards.get(0).currentNodeId()).getName();
return state.nodes().get(randomFrom(startedShards).currentNodeId()).getName();
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to shuffle entire collection to get a single random element.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 even better

ShardRoutingState.INITIALIZING
);
initializingShards.sort(Comparator.comparing(ShardRouting::shardId).thenComparing(ShardRouting::primary, Boolean::compare));
List<ShardRouting> shards = randomSubsetOf(Math.min(randomIntBetween(1, 100), initializingShards.size()), initializingShards);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this sorting is unnecessary. randomSubsetOf shuffles the collection internally.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 even better

@idegtiarenko idegtiarenko merged commit 4ebb23d into elastic:main Dec 2, 2022
@idegtiarenko idegtiarenko deleted the simplify_shardsWithState_2 branch December 2, 2022 12:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:Distributed/Allocation All issues relating to the decision making around placing a shard (both master logic & on the nodes) >non-issue Team:Distributed Meta label for distributed team. v8.7.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants