Skip to content
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

Merged
merged 5 commits into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal/wrr/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,7 @@ func (rw *randomWRR) Add(item any, weight int64) {
}

func (rw *randomWRR) String() string {
rw.mu.Lock()
defer rw.mu.Unlock()
Copy link
Member

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.

Copy link
Member Author

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

Copy link
Member Author

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.

return fmt.Sprint(rw.items)
}
18 changes: 18 additions & 0 deletions internal/wrr/random_test.go
Copy link
Contributor

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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Copy link
Member Author

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
*
* Copyright 2023 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package wrr