Skip to content
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

Remove LinkedDeque and replace with LinkedHashMap #6968

Merged
merged 2 commits into from
Apr 5, 2023

Conversation

andrross
Copy link
Member

@andrross andrross commented Apr 4, 2023

After the recent changes the usage of the LinkedDeque fits quite well with the insertion order semantics of LinkedHashMap, which also allows for constant time additions and removals.

Microbenchmark Results

The benchmark results are quite similar. The LinkedHashMap version seems to be slightly faster, but they are close enough there could be other variables in the test runs causing the difference. The benchmarks show there isn't any significant regression.

OS: Ubuntu 20.04.5 LTS
Host: AWS EC2 c6i.8xlarge
Benchmark command: ./gradlew -p benchmarks run --args 'FileCacheBenchmark'

This PR:

Benchmark                   (concurrencyLevel)  (maximumNumberOfEntries)   Mode  Cnt      Score   Error   Units
FileCacheBenchmark.get                       1                     65536  thrpt        3395.869          ops/ms
FileCacheBenchmark.get                       1                   1048576  thrpt        1542.995          ops/ms
FileCacheBenchmark.get                       8                     65536  thrpt        6674.145          ops/ms
FileCacheBenchmark.get                       8                   1048576  thrpt        3810.060          ops/ms
FileCacheBenchmark.put                       1                     65536  thrpt        3608.218          ops/ms
FileCacheBenchmark.put                       1                   1048576  thrpt        1667.715          ops/ms
FileCacheBenchmark.put                       8                     65536  thrpt        7229.409          ops/ms
FileCacheBenchmark.put                       8                   1048576  thrpt        4456.812          ops/ms
FileCacheBenchmark.remove                    1                     65536  thrpt       12417.484          ops/ms
FileCacheBenchmark.remove                    1                   1048576  thrpt        8151.180          ops/ms
FileCacheBenchmark.remove                    8                     65536  thrpt       18792.732          ops/ms
FileCacheBenchmark.remove                    8                   1048576  thrpt       15913.771          ops/ms
FileCacheBenchmark.replace                   1                     65536  thrpt        3326.174          ops/ms
FileCacheBenchmark.replace                   1                   1048576  thrpt        1485.508          ops/ms
FileCacheBenchmark.replace                   8                     65536  thrpt        6783.510          ops/ms
FileCacheBenchmark.replace                   8                   1048576  thrpt        3947.464          ops/ms

Previous implementation:

Benchmark                   (concurrencyLevel)  (maximumNumberOfEntries)   Mode  Cnt      Score   Error   Units
FileCacheBenchmark.get                       1                     65536  thrpt        3344.929          ops/ms
FileCacheBenchmark.get                       1                   1048576  thrpt        1513.615          ops/ms
FileCacheBenchmark.get                       8                     65536  thrpt        6536.668          ops/ms
FileCacheBenchmark.get                       8                   1048576  thrpt        3869.711          ops/ms
FileCacheBenchmark.put                       1                     65536  thrpt        3704.204          ops/ms
FileCacheBenchmark.put                       1                   1048576  thrpt        1684.997          ops/ms
FileCacheBenchmark.put                       8                     65536  thrpt        7023.869          ops/ms
FileCacheBenchmark.put                       8                   1048576  thrpt        4276.159          ops/ms
FileCacheBenchmark.remove                    1                     65536  thrpt       12547.969          ops/ms
FileCacheBenchmark.remove                    1                   1048576  thrpt        8131.708          ops/ms
FileCacheBenchmark.remove                    8                     65536  thrpt       16763.030          ops/ms
FileCacheBenchmark.remove                    8                   1048576  thrpt       15659.768          ops/ms
FileCacheBenchmark.replace                   1                     65536  thrpt        3394.281          ops/ms
FileCacheBenchmark.replace                   1                   1048576  thrpt        1495.073          ops/ms
FileCacheBenchmark.replace                   8                     65536  thrpt        7018.876          ops/ms
FileCacheBenchmark.replace                   8                   1048576  thrpt        3796.394          ops/ms

Check List

  • New functionality includes testing.
    • All tests pass
  • New functionality has been documented.
    • New functionality has javadoc added
  • Commits are signed per the DCO using --signoff
  • Commit changes are listed out in CHANGELOG.md file (See: Changelog)

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.

After the recent changes the usage of the LinkedDeque fits quite well
with the insertion order semantics of LinkedHashMap, which also allows
for constant time additions and removals.

Signed-off-by: Andrew Ross <[email protected]>
@github-actions
Copy link
Contributor

github-actions bot commented Apr 4, 2023

Gradle Check (Jenkins) Run Completed with:

@codecov-commenter
Copy link

codecov-commenter commented Apr 4, 2023

Codecov Report

Merging #6968 (1786649) into main (c85d33e) will decrease coverage by 0.10%.
The diff coverage is 84.28%.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

@@             Coverage Diff              @@
##               main    #6968      +/-   ##
============================================
- Coverage     70.66%   70.57%   -0.10%     
+ Complexity    59231    59071     -160     
============================================
  Files          4812     4812              
  Lines        283761   283617     -144     
  Branches      40917    40909       -8     
============================================
- Hits         200519   200153     -366     
- Misses        66784    67033     +249     
+ Partials      16458    16431      -27     
Impacted Files Coverage Δ
...a/org/opensearch/gradle/test/DistroTestPlugin.java 0.00% <0.00%> (ø)
...a/org/opensearch/plugins/InstallPluginCommand.java 85.14% <ø> (ø)
...rc/main/java/org/opensearch/plugins/PluginCli.java 0.00% <ø> (ø)
...va/org/opensearch/plugins/RemovePluginCommand.java 85.48% <ø> (ø)
...src/main/java/org/opensearch/cli/MultiCommand.java 93.54% <ø> (ø)
...java/org/opensearch/common/collect/MapBuilder.java 66.66% <ø> (ø)
...in/java/org/opensearch/common/util/io/IOUtils.java 78.88% <ø> (ø)
...in/java/org/opensearch/common/util/io/Streams.java 94.11% <ø> (ø)
.../java/org/opensearch/common/util/net/NetUtils.java 68.75% <ø> (ø)
.../src/main/java/org/opensearch/core/Assertions.java 75.00% <ø> (ø)
... and 133 more

... and 472 files with indirect coverage changes

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@reta
Copy link
Collaborator

reta commented Apr 4, 2023

@andrross I like it, the implementation is much simpler now, interestingly enough, the LinkedHashMap supports eviction with removeEldestEntry method (that could be implemented) but since we use 2 maps (data and lru that should be in sync), we may not benefit from it.


private final RemovalListener<K, V> listener;

private final Weigher<V> weigher;

private final StatsCounter<K> statsCounter;

private volatile ReentrantLock lock;
private final ReentrantLock lock;
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍 we could replace

        final ReentrantLock lock = this.lock;
        lock.lock();

with just

        lock.lock();

now

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh good catch! I'm sure there was some previous incarnation of this code where a volatile lock was needed, but it definitely was never necessary here.

@github-actions
Copy link
Contributor

github-actions bot commented Apr 4, 2023

Gradle Check (Jenkins) Run Completed with:

@andrross
Copy link
Member Author

andrross commented Apr 4, 2023

@reta regarding removeEldestEntry/cache improvements in general, I added some more details here with where the cache has evolved since that issue was opened, with my opinions on what should be improved: #6225 (comment)

@github-actions
Copy link
Contributor

github-actions bot commented Apr 4, 2023

Gradle Check (Jenkins) Run Completed with:

@github-actions
Copy link
Contributor

github-actions bot commented Apr 4, 2023

Gradle Check (Jenkins) Run Completed with:

  • RESULT: UNSTABLE ❕
  • TEST FAILURES:
      2 org.opensearch.cluster.service.MasterServiceTests.classMethod
      1 org.opensearch.cluster.service.MasterServiceTests.testThrottlingForMultipleTaskTypes

@andrross andrross merged commit 65443ad into opensearch-project:main Apr 5, 2023
@andrross andrross deleted the remove-linked-deque branch April 5, 2023 00:03
mitrofmep pushed a commit to mitrofmep/OpenSearch that referenced this pull request Apr 5, 2023
…#6968)

* Remove LinkedDeque and replace with LinkedHashMap

After the recent changes the usage of the LinkedDeque fits quite well
with the insertion order semantics of LinkedHashMap, which also allows
for constant time additions and removals.

Signed-off-by: Andrew Ross <[email protected]>

* Use class member reference now that lock is final

Signed-off-by: Andrew Ross <[email protected]>

---------

Signed-off-by: Andrew Ross <[email protected]>
Signed-off-by: Valentin Mitrofanov <[email protected]>
@kotwanikunal
Copy link
Member

@andrross Do we need to backport this to 2.x? Backport for #7498 got me here since there was a conflict

kotwanikunal pushed a commit to kotwanikunal/OpenSearch that referenced this pull request May 17, 2023
…#6968)

* Remove LinkedDeque and replace with LinkedHashMap

After the recent changes the usage of the LinkedDeque fits quite well
with the insertion order semantics of LinkedHashMap, which also allows
for constant time additions and removals.

Signed-off-by: Andrew Ross <[email protected]>

* Use class member reference now that lock is final

Signed-off-by: Andrew Ross <[email protected]>

---------

Signed-off-by: Andrew Ross <[email protected]>
(cherry picked from commit 65443ad)
reta pushed a commit that referenced this pull request May 17, 2023
… filecache support in clear indices cache API (#7595)

* Remove LinkedDeque and replace with LinkedHashMap (#6968)

* Remove LinkedDeque and replace with LinkedHashMap

After the recent changes the usage of the LinkedDeque fits quite well
with the insertion order semantics of LinkedHashMap, which also allows
for constant time additions and removals.

Signed-off-by: Andrew Ross <[email protected]>

* Use class member reference now that lock is final

Signed-off-by: Andrew Ross <[email protected]>

---------

Signed-off-by: Andrew Ross <[email protected]>
(cherry picked from commit 65443ad)

* Add filecache support in clear indices cache API (#7498)

Signed-off-by: Kunal Kotwani <[email protected]>
(cherry picked from commit a1e42b1)
Signed-off-by: Kunal Kotwani <[email protected]>

---------

Signed-off-by: Kunal Kotwani <[email protected]>
Co-authored-by: Andrew Ross <[email protected]>
@dblock
Copy link
Member

dblock commented May 22, 2023

Looks like #7595 includes this? @kotwanikunal

@kotwanikunal
Copy link
Member

Looks like #7595 includes this? @kotwanikunal

Yes @dblock. Backport was done with #7595

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants