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

Conversation

arvindbr8
Copy link
Member

@arvindbr8 arvindbr8 commented Sep 27, 2023

Add() is always called as part of constructing the WRR object and never concurrently. Also turns out Next() is never called concurrently either. I started the PR (#5970) started out to avoid the race when accessing WRR.items in String(), but the mutex can be avoided all together for random WRR.

Also updating docstring to say the same thing.

RELEASE NOTES: N/A

@arvindbr8 arvindbr8 requested a review from dfawley October 2, 2023 16:54
@arvindbr8 arvindbr8 added this to the 1.59 Release milestone Oct 2, 2023
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

Copy link
Member

Choose a reason for hiding this comment

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

+1

Comment on lines 90 to 91
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.

@dfawley dfawley assigned arvindbr8 and unassigned dfawley Oct 5, 2023
@codecov
Copy link

codecov bot commented Oct 5, 2023

Codecov Report

Merging #6666 (84f59a7) into master (afaf31a) will increase coverage by 0.06%.
The diff coverage is n/a.

Additional details and impacted files

@ginayeh ginayeh modified the milestones: 1.59 Release, 1.60 Release Oct 5, 2023
@arvindbr8
Copy link
Member Author

Life happens, and pull request intents change.

Discussed with Doug offline. Add() is always called as part of constructing the WRR object and never concurrently. Also turns out Next() is never called concurrently either. I started the PR started out to avoid the race when accessing WRR.items in String() to fix #5970, the mutex can be avoided all together for random WRR.

Updating the PR description

@arvindbr8 arvindbr8 changed the title randomWRR: attain lock before read in String() randomWRR: remove lock for accessing WRR.items Oct 6, 2023
@arvindbr8 arvindbr8 requested a review from dfawley October 6, 2023 23:15
@arvindbr8 arvindbr8 removed their assignment Oct 6, 2023
Comment on lines 24 to 25
// Add adds an item with weight to the WRR set. All Add must be only called
// before any calls to Next().
Copy link
Member

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/

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

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.
Copy link
Member

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."

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

@dfawley dfawley added Type: Internal Cleanup Refactors, etc and removed Type: Bug labels Oct 6, 2023
@dfawley dfawley assigned arvindbr8 and unassigned dfawley Oct 6, 2023
@arvindbr8 arvindbr8 merged commit 59f57b1 into grpc:master Oct 7, 2023
13 checks passed
@arvindbr8 arvindbr8 deleted the i_5970 branch October 11, 2023 20:08
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants