-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
stats: fix race condition when refreshing cache #51805
Merged
Merged
Conversation
This file contains 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
First commit is #51780 |
RaduBerinde
approved these changes
Jul 22, 2020
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.
Reviewable status: complete! 1 of 0 LGTMs obtained (waiting on @RaduBerinde)
TFTR! bors r+ |
bors r+ |
Build failed: |
bors r+ |
Build failed: |
bors r+ |
Build failed: |
This commit fixes a race condition that was introduced in cockroachdb#51616. The race condition existed because it was possible for two refreshes to be triggered in close succession, and there was no mechanism to prevent the first refresh from overwriting the second if it was delayed. We fix the race condition by introducing two boolean flags into each cache entry: refreshing and mustRefreshAgain. If refreshing is true, that indicates that the current statistics for this table are stale, and we are in the process of fetching the updated stats from the database. In the mean time, other callers can use the stale stats and do not need to wait. If a goroutine tries to perform a refresh when a refresh is already in progress, it will see that refreshing=true and will set the mustRefreshAgain flag to true before returning. When the original goroutine that was performing the refresh returns from the database and sees that mustRefreshAgain=true, it will trigger another refresh. Informs cockroachdb#50863 Release note: None
bors r+ |
Build failed (retrying...): |
bors r+ |
bors r+ |
Build succeeded: |
This was referenced Jul 31, 2020
Merged
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This commit fixes a race condition that was introduced in #51616.
The race condition existed because it was possible for two refreshes
to be triggered in close succession, and there was no mechanism to
prevent the first refresh from overwriting the second if it was
delayed.
We fix the race condition by introducing two boolean flags into each
cache entry:
refreshing
andmustRefreshAgain
. Ifrefreshing
is true,that indicates that the current statistics for this table are stale,
and we are in the process of fetching the updated stats from the database.
In the mean time, other callers can use the stale stats and do not need to
wait.
If a goroutine tries to perform a refresh when a refresh is already
in progress, it will see that
refreshing=true
and will set themustRefreshAgain
flag totrue
before returning. When the originalgoroutine that was performing the refresh returns from the database and
sees that
mustRefreshAgain=true
, it will trigger another refresh.Informs #50863
Release note: None