-
Notifications
You must be signed in to change notification settings - Fork 727
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
Improve lock mechanism in tso.KeyspaceGroupManager #6305
Conversation
…the state of the keyspace group manager Signed-off-by: Bin Shi <[email protected]>
[REVIEW NOTIFICATION] This pull request has been approved by:
To complete the pull request process, please ask the reviewers in the list to review by filling The full list of commands accepted by this bot can be found here. Reviewer can indicate their review by submitting an approval review. |
Signed-off-by: Bin Shi <[email protected]>
return | ||
} | ||
|
||
assignedToMe := kgm.isAssignedToMe(group) | ||
if assignedToMe { | ||
if kgm.ams[group.ID].Load() != nil { | ||
if kgm.ams[group.ID] != nil { |
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.
Do we need a lock here?
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.
No need.
There are three writers -- initialization (Load the initial keyspace group assignment from storage), KeyspaceGroupsMetaWatchLoop (the code shown above is in this single goroutine), and shutdown. They are serialized, i.e., there are no concurrent writers. Specifically, we start KeyspaceGroupsMetaWatchLoop after static initialization, and the system exits from KeyspaceGroupsMetaWatchLoop before write access in shutdown.
In summary, KeyspaceGroupsMetaWatchLoop can read without lock, but it must require lock for write so that keyspace group manager can handle concurrent reads when processing requests.
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.
But I suggest adding one to prevent the potential problem, it doesn't cost too much I think.
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.
It has cost. If I add a lock it needs to be a write lock and adding here will increase the length of the critical section.
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.
The lock isn't re-entrant. If I'm right, with sync.RWMutex, I even can't require a read lock then upgrade it the write 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.
good suggestion, actually.
changed to (kgm.getAllocatorManager requires the read lock)
if oldAM, oldGroup := kgm.getAllocatorManager(group.ID); oldAM != nil {
log.Info("keyspace group already initialized, so update meta only",
zap.Uint32("keyspace-group-id", group.ID))
kgm.updateKeyspaceGroupMembership(oldGroup, group)
return
}
} | ||
|
||
// deleteKeyspaceGroup deletes the given keyspace group. | ||
func (kgm *KeyspaceGroupManager) deleteKeyspaceGroup(groupID uint32) { | ||
kg := kgm.kgs[groupID].Swap(nil) | ||
kg := kgm.kgs[groupID] |
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.
ditto
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.
good catch. You saved me. I remember I added the lock here, but somehow it's gone.
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.
kgm.Lock()
defer kgm.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.
Confirm that no other places need 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.
The rest LGTM.
Signed-off-by: Bin Shi <[email protected]>
97c21e3
to
5bc37f9
Compare
Signed-off-by: Bin Shi <[email protected]>
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## master #6305 +/- ##
==========================================
- Coverage 75.13% 75.11% -0.03%
==========================================
Files 404 404
Lines 39686 39697 +11
==========================================
- Hits 29819 29818 -1
- Misses 7267 7270 +3
- Partials 2600 2609 +9
Flags with carried forward coverage won't be shown. Click here to find out more.
... and 32 files with indirect coverage changes Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report in Codecov by Sentry. |
pkg/tso/keyspace_group_manager.go
Outdated
@@ -415,6 +462,8 @@ func (kgm *KeyspaceGroupManager) watchKeyspaceGroupsMetaChange(revision int64) ( | |||
case clientv3.EventTypeDelete: | |||
kgm.deleteKeyspaceGroup(id) | |||
} | |||
|
|||
kgm.storageChangeEventsApplied.Add(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.
Could the counter overflow? Do you think we can only add it during the test? Maybe a failpoint could help.
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 already changed before saw your comment. Basically, I required read lock in the test. Check the commit 5.
6ddd516
to
37ec9f9
Compare
Signed-off-by: Bin Shi <[email protected]> Fix go fmt error Signed-off-by: Bin Shi <[email protected]>
37ec9f9
to
24f3faa
Compare
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.
The rest LGTM
/merge |
@JmPotato: It seems you want to merge this PR, I will help you trigger all the tests: /run-all-tests Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository. |
This pull request has been accepted and is ready to merge. Commit hash: 24f3faa
|
ref #6232, ref #6305 follow-up #6305 Add read lock at one place for protection and better structure Signed-off-by: Bin Shi <[email protected]> Co-authored-by: Ti Chi Robot <[email protected]>
What problem does this PR solve?
Issue Number: Ref #6232
What is changed and how does it work?
Check List
Tests
Release note