GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [fix][ml] Fix race conditions in RangeCache by lhotari · Pull Request #22789 · apache/pulsar

Conversation

@lhotari

@lhotari lhotari commented May 28, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Motivation

The RangeCache class contains several race conditions which cause instability.

When one thread removes the entry and another one uses it, that will become a problem.

The cacheEvictionIntervalMs setting is 10 ms by default. This results in the
RangeCache.evictLEntriesBeforeTimestamp method getting called about 100 times per second.
The default expiration is managedLedgerCacheEvictionTimeThresholdMillis which is 1000 ms by default.

It's also possible that 2 threads remove the entry at the same time. ManagedLedgerImpl.invalidateEntriesUpToSlowestReaderPosition will result in calls to RangeCache.removeRange method. These calls happen independently of the RangeCache.evictLEntriesBeforeTimestamp calls so there's a chance for race conditions.

Modifications

  • Add checks to validate that the value hasn't already been released before it is removed.
    • add extra .retain() and .release() calls
    • add a way to validate that the value matches the key
      • this is to prevent a possible race condition where the entry has already been recycled and contains another valid entry which is for a different key
  • Make all removals using the remove(key, value) method so that removals remove the correct value exactly once
    • Add a wrapper object for the value to ensure this so that exact identity match is used for the removal.
    • The wrapper instance is recycled to reduce the overhead.

Documentation

  • doc
  • doc-required
  • doc-not-needed
  • doc-complete

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@lhotari
[fix][ml] Fix race conditions in RangeCache
88f34bb
@lhotari
lhotari requested review from Technoboy-, hangc0276 and merlimat May 28, 2024 08:35
@github-actions github-actions Bot added the doc-not-needed Your PR changes do not impact docs label May 28, 2024
@lhotari
lhotari requested a review from codelipenghui May 28, 2024 08:35
@lhotari lhotari added the ready-to-test label May 28, 2024
@lhotari lhotari mentioned this pull request May 28, 2024
[Broker] Fix race condition in invalidating ledger cache entries #10480
Closed
@lhotari
lhotari requested a review from eolivelli May 28, 2024 08:47
@lhotari lhotari mentioned this pull request May 28, 2024
[Bug] parseMessageMetadata error when broker entry metadata enable with high loading #22601
Closed
2 of 3 tasks
@codecov-commenter

codecov-commenter commented May 28, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.45833% with 13 lines in your changes missing coverage. Please review.
✅ Project coverage is 73.27%. Comparing base (bbc6224) to head (2a6c2b1).
⚠️ Report is 1281 commits behind head on master.

Files with missing lines Patch % Lines
...org/apache/bookkeeper/mledger/util/RangeCache.java 87.36% 7 Missing and 5 partials ⚠️
.../org/apache/bookkeeper/mledger/impl/EntryImpl.java 0.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff              @@
##             master   #22789      +/-   ##
============================================
- Coverage     73.57%   73.27%   -0.31%     
- Complexity    32624    32648      +24     
============================================
  Files          1877     1889      +12     
  Lines        139502   141659    +2157     
  Branches      15299    15543     +244     
============================================
+ Hits         102638   103800    +1162     
- Misses        28908    29844     +936     
- Partials       7956     8015      +59     
Flag Coverage Δ
inttests 27.46% <76.04%> (+2.88%) ⬆️
systests 24.82% <72.91%> (+0.50%) ⬆️
unittests 72.27% <86.45%> (-0.58%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
.../org/apache/bookkeeper/mledger/impl/EntryImpl.java 78.31% <0.00%> (-0.17%) ⬇️
...org/apache/bookkeeper/mledger/util/RangeCache.java 89.31% <87.36%> (-6.09%) ⬇️

... and 367 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Technoboy-
Technoboy- reviewed May 30, 2024
View reviewed changes
Comment thread managed-ledger/src/main/java/org/apache/bookkeeper/mledger/util/RangeCache.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@lhotari lhotari self-assigned this May 30, 2024
@lhotari
lhotari marked this pull request as draft May 30, 2024 20:05
@lhotari
Refactor RangeCache and cover yet another race condition
172de16
@lhotari
lhotari marked this pull request as ready for review May 30, 2024 21:06
@lhotari

lhotari commented May 30, 2024

Copy link
Copy Markdown
Member Author

Previous work in RangeCache to prevent race conditions: #15707 and #21302.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

lhotari added 5 commits May 31, 2024 00:21
@lhotari
Add javadoc, add refCnt check
94101c7
@lhotari
Handle the race condition where the IdentityWrapper has already been …
d28db55
…recycled
@lhotari
Remove synchronization from the clear method since it's not needed an…
b0f3473
…ymore
@lhotari
Add javadoc
56c5823
@lhotari
Improve the current race condition test for put method
2a6c2b1
@lhotari
lhotari requested a review from Technoboy- May 30, 2024 22:02
merlimat
merlimat approved these changes May 30, 2024
View reviewed changes

@merlimat merlimat left a comment

Copy link
Copy Markdown
Contributor

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.

Very nice work

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@merlimat
merlimat merged commit c39f9f8 into apache:master May 31, 2024
This was referenced May 31, 2024
[improve][ml] RangeCache refactoring: test race conditions and prevent endless loops #22814
Merged
[improve][misc][WIP] Detect "double release" and "use after release" bugs with recycled objects #22110
Closed
@lhotari

lhotari commented May 31, 2024

Copy link
Copy Markdown
Member Author

This PR contained a few issues. I have a follow up PR #22814 to address the issues. Please review

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@lhotari lhotari added release/3.0.6 release/3.2.4 labels Jun 1, 2024
@lhotari lhotari added the release/3.3.1 label Jun 3, 2024
lhotari added a commit that referenced this pull request Jun 3, 2024
@lhotari
[fix][ml] Fix race conditions in RangeCache (#22789)
a8b8324
(cherry picked from commit c39f9f8)
@lhotari lhotari added the cherry-picked/branch-3.3 label Jun 3, 2024
lhotari added a commit that referenced this pull request Jun 3, 2024
@lhotari
[fix][ml] Fix race conditions in RangeCache (#22789)
9a99e45
(cherry picked from commit c39f9f8)
lhotari added a commit that referenced this pull request Jun 3, 2024
@lhotari
[fix][ml] Fix race conditions in RangeCache (#22789)
151ecb5
(cherry picked from commit c39f9f8)
@lhotari lhotari added cherry-picked/branch-3.0 cherry-picked/branch-3.2 labels Jun 3, 2024
nikhil-ctds pushed a commit to datastax/pulsar that referenced this pull request Jun 7, 2024
@lhotari @nikhil-ctds
[fix][ml] Fix race conditions in RangeCache (apache#22789)
4e2317a
(cherry picked from commit c39f9f8)
(cherry picked from commit 9a99e45)
srinath-ctds pushed a commit to datastax/pulsar that referenced this pull request Jun 7, 2024
@lhotari @srinath-ctds
[fix][ml] Fix race conditions in RangeCache (apache#22789)
6812af7
(cherry picked from commit c39f9f8)
(cherry picked from commit 9a99e45)
@lhotari lhotari mentioned this pull request Jun 14, 2024
[Bug] Ledger can not recover with Digest Mismatch Error #22906
Closed
2 of 3 tasks
@lhotari lhotari added this to the 4.0.0 milestone Oct 14, 2024
@lhotari lhotari mentioned this pull request Jan 28, 2025
[fix][broker Fix bug in RangeCache where different instance of the key wouldn't ever match #23903
Merged
1 of 4 tasks
hanmz pushed a commit to hanmz/pulsar that referenced this pull request Feb 12, 2025
@lhotari hanmz
[fix][ml] Fix race conditions in RangeCache (apache#22789)
6c2537d
@lhotari lhotari mentioned this pull request May 31, 2025
[refactor][ml] Replace cache eviction algorithm with centralized removal queue and job #24363
Merged
1 of 4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@merlimat merlimat merlimat approved these changes
@hangc0276 hangc0276 Awaiting requested review from hangc0276
@codelipenghui codelipenghui Awaiting requested review from codelipenghui
@eolivelli eolivelli Awaiting requested review from eolivelli
@Technoboy- Technoboy- Awaiting requested review from Technoboy-

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@lhotari lhotari

Labels

cherry-picked/branch-3.0 cherry-picked/branch-3.2 cherry-picked/branch-3.3 doc-not-needed Your PR changes do not impact docs ready-to-test release/3.0.6 release/3.2.4 release/3.3.1

Projects

None yet

Milestone

4.0.0

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@lhotari @codecov-commenter @merlimat @Technoboy-
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.