-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
randomWRR: remove lock for accessing WRR.items #6666
Conversation
internal/wrr/random_test.go
Outdated
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 is this file empty?
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.
+1
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.
oops. I had initially thought about adding a unit test for this package. But i realized this was basically covered by other tests indirectly. Removed
internal/wrr/random_test.go
Outdated
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.
+1
internal/wrr/random.go
Outdated
rw.mu.Lock() | ||
defer rw.mu.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.
This is risky as we'll deadlock if String
is ever called while the lock is held.
Note that String
is called indirectly, too, e.g. fmt.Sprint(rw)
. I don't think I like this. Maybe we should just exclude the items
from what is printed and print only static info instead, with a different method that can retrieve the detailed data as a string if needed.
Consider:
func (rw *randomWRR) Whatever() {
rw.mu.Lock()
defer rw.mu.Unlock()
...
if badThing {
rw.logger.Warningf("saw bad thing in %s", rw)
}
// or:
if rw.logger.V(2) {
rw.logger.Infof("saw weird thing in %s", rw)
}
}
We'd deadlock in rare cases that might not be covered in tests.
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.
I just noticed that rw.mu
is of type sync.RWMutex
. Which means i could prolly use mu.RLock()
in String(), which would avoid deadlocks on the mutex? wdyt? @dfawley
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.
Spoke with him offline.. my b, rLock still blocks on wLock.
Life happens, and pull request intents change. Discussed with Doug offline. Updating the PR description |
internal/wrr/wrr.go
Outdated
// Add adds an item with weight to the WRR set. All Add must be only called | ||
// before any calls to Next(). |
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.
Nits:
s/All//
s/Next()/Next/
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.
internal/wrr/wrr.go
Outdated
Add(item any, weight int64) | ||
// Next returns the next picked item. | ||
// | ||
// Add and Next need to be thread safe. | ||
// Next needs to be thread safe. |
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.
Possibly for clarity: "Add may not be called after any call to Next."
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.
Add()
is always called as part of constructing the WRR object and never concurrently. Also turns outNext()
is never called concurrently either. I started the PR (#5970) started out to avoid the race when accessing WRR.items inString()
, but the mutex can be avoided all together for random WRR.Also updating docstring to say the same thing.
RELEASE NOTES: N/A