-
Notifications
You must be signed in to change notification settings - Fork 24.5k
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
optimize getIndices in IndicesSegmentResponse #80064
optimize getIndices in IndicesSegmentResponse #80064
Conversation
Pinging @elastic/es-data-management (Team:Data Management) |
@elasticmachine ok to test |
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.
Seems like a nice improvement. I left a few suggestions.
@@ -61,19 +59,16 @@ | |||
} | |||
Map<String, IndexSegments> indicesSegments = new HashMap<>(); | |||
|
|||
Set<String> indices = new HashSet<>(); | |||
Map<String, List<ShardSegments>> tmpIndicesSegment = new HashMap<>(); |
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.
Suggested rename:
Map<String, List<ShardSegments>> tmpIndicesSegment = new HashMap<>(); | |
final Map<String, List<ShardSegments>> segmentsByIndex = new HashMap<>(); |
List<ShardSegments> indexSegments = tmpIndicesSegment.computeIfAbsent( | ||
shard.getShardRouting().getIndexName(), | ||
k -> new ArrayList<>() | ||
); | ||
indexSegments.add(shard); |
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.
Suggest inlining the intermediate variable:
List<ShardSegments> indexSegments = tmpIndicesSegment.computeIfAbsent( | |
shard.getShardRouting().getIndexName(), | |
k -> new ArrayList<>() | |
); | |
indexSegments.add(shard); | |
segmentsByIndex.computeIfAbsent(shard.getShardRouting().getIndexName(), n -> new ArrayList<>()).add(shard); |
server/src/main/java/org/elasticsearch/action/admin/indices/segments/IndexSegments.java
Show resolved
Hide resolved
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.
Oh also please make IndicesSegmentResponse#indicesSegments
volatile. I don't see any places where we access it across threads today but it's possible we'd add one in future.
@DaveCTurner Thank you for your suggestion, I've updated the code. |
e390ac4
to
e652170
Compare
Thanks @mushao999, this looks good to me but CI is failing - you need to run |
@DaveCTurner Thank you , I found this problem in jenkins log and have reformat the code. I am sorry for force pushing the code, I did this just to let it keep up with the latest master. No force push later. |
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.
The getIndices method in IndicesSegmentResponse will traverse shards for n+1 times (n is indices count), which can be optimized to just traverse once by using Map and its computeIfAbsent method.
Same logic can be found in IndexSegments’ constructor, where we just traverse shard once.
This PR just want to make elasticsearch code better, thougth getIndices method may be called Infrequently and has no performance issue.