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
cacher avoid double locking #117410
cacher avoid double locking #117410
Conversation
2da8995
to
e4ee8fd
Compare
/retest |
/assign @wojtek-t |
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.
Couple minor comments - overall LGTM.
@@ -205,7 +204,7 @@ func newTimeBucketWatchers(clock clock.Clock, bookmarkFrequency time.Duration) * | |||
|
|||
// adds a watcher to the bucket, if the deadline is before the start, it will be | |||
// added to the first one. | |||
func (t *watcherBookmarkTimeBuckets) addWatcher(w *cacheWatcher) bool { | |||
func (t *watcherBookmarkTimeBuckets) addWatcherThreadUnsafe(w *cacheWatcher) bool { |
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.
Can you also in this line:
https://github.com/kubernetes/kubernetes/blob/e4ee8fded488ee400f03862f83b3d46a3ec8a4c5/staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher.go#L326
(where bookmarkWatchers
field is defined)
add an additional comment, that this field when accessed should be protected by the Cacher.lock itself.
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.
done
defer cacher.bookmarkWatchers.lock.Unlock() | ||
cacher.Lock() | ||
cacher.bookmarkWatchers.popExpiredWatchersThreadUnsafe() | ||
cacher.Unlock() |
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.
Why did you change this line?
Below we're also accessing bookmarkWatchers, so we should (as previously) do that under lock.
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.
If we don't acquire a lock here then we race with https://github.com/kubernetes/kubernetes/pull/117410/files#diff-14c4fb9f290753f50e0af4856d871ff41e2b520d747158cbff7bafc577fbb29eR929 which tics every second.
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.
You didn't understand my comment - we need to aquire a lock.
I'm just saying you shouldn't unlock here, just revert back to "defer unlock"
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.
ah, now I see it, popExpiredWatchersThreadUnsafe
also accesses the bookmarkWatchers
, good catch, thx!
it turns out that the watcherBookmarkTimeBuckets is called from only three places/methods: startDispatching, finishDispatching and Watch. All these methods acquire c.Lock() before touching watcherBookmarkTimeBuckets. Thus we could remove explicit locking in watcherBookmarkTimeBuckets since the access is already synced.
e4ee8fd
to
bb89d17
Compare
…oper synchronisation must be used
bb89d17
to
3a54489
Compare
/label tide/merge-method-squash /lgtm Thanks! |
LGTM label has been added. Git tree hash: b4d90edec930edc7cbf5b83c665086c1d10b5591
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: p0lyn0mial, wojtek-t The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
/kind cleanup |
* cacher: remove locking from watcherBookmarkTimeBuckets it turns out that the watcherBookmarkTimeBuckets is called from only three places/methods: startDispatching, finishDispatching and Watch. All these methods acquire c.Lock() before touching watcherBookmarkTimeBuckets. Thus we could remove explicit locking in watcherBookmarkTimeBuckets since the access is already synced. * cacher: rename watcherBookmarkTimeBuckets methods to indicate that proper synchronisation must be used
What type of PR is this?
/kind cleanup
What this PR does / why we need it:
it turns out that the watcherBookmarkTimeBuckets
is called from only three places/methods: startDispatching, finishDispatching and Watch.
All these methods acquire c.Lock() before touching watcherBookmarkTimeBuckets.
Thus we could remove explicit locking in
watcherBookmarkTimeBuckets since the access is already synced.
xref: kubernetes/enhancements#3157
Which issue(s) this PR fixes:
Fixes #
Special notes for your reviewer:
Does this PR introduce a user-facing change?
Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.: