Fix O(n^2) removeAll in remote translog metadata cleanup - #21350
Conversation
PR Reviewer Guide 🔍(Review updated until commit f395fcc)Here are some key observations to aid the review process:
|
ArrayList.removeAll(ArrayList) is O(n*m) due to linear contains() checks. Wrap the argument in HashSet for O(1) lookups, reducing the complexity to O(n). This was causing 500ms CPU spikes on the remote_purge thread when metadata file counts grew large. Signed-off-by: Gaurav Bafna <gbbafna@amazon.com>
|
Persistent review updated to latest commit b657f29 |
PR Code Suggestions ✨Latest suggestions up to f395fcc
Previous suggestionsSuggestions up to commit b657f29
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #21350 +/- ##
============================================
+ Coverage 73.34% 73.46% +0.11%
- Complexity 74223 74273 +50
============================================
Files 5958 5958
Lines 337309 337357 +48
Branches 48664 48687 +23
============================================
+ Hits 247408 247841 +433
+ Misses 70188 69744 -444
- Partials 19713 19772 +59 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Persistent review updated to latest commit f395fcc |
Bukhtawar
left a comment
There was a problem hiding this comment.
How do we ensure future consumers of metadataFiles handle this correctly. Can we open a TODO on refactoring the class structure broadly
|
❌ Gradle check result for f395fcc: FAILURE Please examine the workflow log, locate, and copy-paste the failure(s) below, then iterate to green. Is the failure a flaky test unrelated to your change? |
|
❕ Gradle check result for f395fcc: UNSTABLE Please review all flaky tests that succeeded after retry and create an issue if one does not already exist to track the flaky failure. |
…project#21350) ArrayList.removeAll(ArrayList) is O(n*m) due to linear contains() checks. Wrap the argument in HashSet for O(1) lookups, reducing the complexity to O(n). This was causing CPU spikes on the remote_purge thread when metadata file counts grew large. Signed-off-by: Gaurav Bafna <gbbafna@amazon.com>
…project#21350) ArrayList.removeAll(ArrayList) is O(n*m) due to linear contains() checks. Wrap the argument in HashSet for O(1) lookups, reducing the complexity to O(n). This was causing CPU spikes on the remote_purge thread when metadata file counts grew large. Signed-off-by: Gaurav Bafna <gbbafna@amazon.com>
Description
ArrayList.removeAll(ArrayList)inRemoteFsTimestampAwareTranslogis O(n*m) becauseArrayList.contains()is a linear scan. When the number of translog metadata files grows large (e.g., 500+ files for long-lived system indices like.opendistro-ism-config), this causes sustained CPU spikes (500ms at 99.9% CPU) on theremote_purgethread.Stack trace observed:
Hot threads showing 100% cpu
Solution
Wrap the
Listargument inHashSetbefore passing toremoveAll(), reducing lookup complexity from O(n) to O(1). This fixes 3 occurrences in the file. A 4thremoveAllcall already uses aSetargument and is unaffected.Check List
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.