-
Notifications
You must be signed in to change notification settings - Fork 460
Use double‑checked locking to eliminate redundant metadata lookups #5538
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
Open
DomGarguilo
wants to merge
2
commits into
apache:main
Choose a base branch
from
DomGarguilo:clientTabletCacheImplBug
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -690,25 +690,28 @@ private void lookupTablet(ClientContext context, Text row, LockCheckerSession lc | |
metadataRow.append(row.getBytes(), 0, row.getLength()); | ||
CachedTablet ptl = parent.findTablet(context, metadataRow, false, LocationNeed.REQUIRED); | ||
|
||
if (ptl != null) { | ||
// Only allow a single lookup at time per parent tablet. For example if a tables tablets are | ||
// all stored in three metadata tablets, then that table could have up to three concurrent | ||
// metadata lookups. | ||
Timer timer = Timer.startNew(); | ||
try (var unused = lookupLocks.lock(ptl.getExtent())) { | ||
// See if entry was added to cache by another thread while we were waiting on the lock | ||
var cached = findTabletInCache(row); | ||
if (cached != null && cached.getCreationTimer().startedAfter(timer)) { | ||
// This cache entry was added after we started waiting on the lock so lets use it and not | ||
// go to the metadata table. This means another thread was holding the lock and doing | ||
// metadata lookups when we requested the lock. | ||
return; | ||
} | ||
// Lookup tablets in metadata table and update cache. Also updating the cache while holding | ||
// the lock is important as it ensures other threads that are waiting on the lock will see | ||
// what this thread found and may be able to avoid metadata lookups. | ||
lookupTablet(context, lcSession, ptl, metadataRow); | ||
if (ptl == null) { | ||
return; | ||
} | ||
|
||
CachedTablet now = findTabletInCache(row); | ||
if (now != null) { | ||
if (now.getTserverLocation().isPresent() && lcSession.checkLock(now) != null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In support of ondemand tablets, tablets w/o a location can be cached in 4.0 (was not the case in 2.1). So these checks that look for the presence of a location do not seem correct. |
||
return; | ||
} | ||
} | ||
// Only allow a single lookup at time per parent tablet. For example if a tables tablets are | ||
// all stored in three metadata tablets, then that table could have up to three concurrent | ||
// metadata lookups. | ||
try (var unused = lookupLocks.lock(ptl.getExtent())) { | ||
now = findTabletInCache(row); | ||
if (now != null && now.getTserverLocation().isPresent() && lcSession.checkLock(now) != null) { | ||
return; | ||
} | ||
// Lookup tablets in metadata table and update cache. Also updating the cache while holding | ||
// the lock is important as it ensures other threads that are waiting on the lock will see | ||
// what this thread found and may be able to avoid metadata lookups. | ||
lookupTablet(context, lcSession, ptl, metadataRow); | ||
} | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
What made you suspect the timer here? I looked into the specifics of the timer and found how its implemented here https://github.com/openjdk/jdk/blob/b6b5ac1ef9042ed62a8358aa6943b8dc87dcf0ab/src/hotspot/os/posix/os_posix.cpp#L1474 . Looking at the docs for that system call it does mention that it will not go backwards but also there is no gaurantee it wil go forward between two calls. So its possible two threads got the exact same nanoTime causing the entry to look stale, that is probably ok. We could make the time comparison
>=
instead of>
if that is the case.Uh oh!
There was an error while loading. Please reload this page.
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.
Changing the
>
to a>=
is iffy and probably not good so please ignore that suggestion. One way we could avoid comparing the time or anything else is to just look for change in the object reference. So maybe could do the following?