optimize getIndices in IndicesSegmentResponse#80064
optimize getIndices in IndicesSegmentResponse#80064DaveCTurner merged 4 commits intoelastic:masterfrom
Conversation
|
Pinging @elastic/es-data-management (Team:Data Management) |
|
@elasticmachine ok to test |
DaveCTurner
left a comment
There was a problem hiding this comment.
Seems like a nice improvement. I left a few suggestions.
There was a problem hiding this comment.
Suggested rename:
| Map<String, List<ShardSegments>> tmpIndicesSegment = new HashMap<>(); | |
| final Map<String, List<ShardSegments>> segmentsByIndex = new HashMap<>(); |
There was a problem hiding this comment.
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
Outdated
Show resolved
Hide resolved
DaveCTurner
left a comment
There was a problem hiding this comment.
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. |
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.