-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
nsqlookupd: fix write lock starvation #1208
Open
andyxning
wants to merge
1
commit into
nsqio:master
Choose a base branch
from
andyxning:nsqlookupd_fix_write_lock_starvation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ import ( | |
|
||
type RegistrationDB struct { | ||
sync.RWMutex | ||
registrationMap map[Registration]ProducerMap | ||
registrationMap *sync.Map | ||
} | ||
|
||
type Registration struct { | ||
|
@@ -54,119 +54,124 @@ func (p *Producer) IsTombstoned(lifetime time.Duration) bool { | |
|
||
func NewRegistrationDB() *RegistrationDB { | ||
return &RegistrationDB{ | ||
registrationMap: make(map[Registration]ProducerMap), | ||
registrationMap: &sync.Map{}, | ||
} | ||
} | ||
|
||
// add a registration key | ||
func (r *RegistrationDB) AddRegistration(k Registration) { | ||
r.Lock() | ||
defer r.Unlock() | ||
_, ok := r.registrationMap[k] | ||
if !ok { | ||
r.registrationMap[k] = make(map[string]*Producer) | ||
} | ||
r.registrationMap.LoadOrStore(k, make(ProducerMap)) | ||
} | ||
|
||
// add a producer to a registration | ||
func (r *RegistrationDB) AddProducer(k Registration, p *Producer) bool { | ||
r.Lock() | ||
defer r.Unlock() | ||
_, ok := r.registrationMap[k] | ||
if !ok { | ||
r.registrationMap[k] = make(map[string]*Producer) | ||
} | ||
producers := r.registrationMap[k] | ||
val, _ := r.registrationMap.LoadOrStore(k, make(ProducerMap)) | ||
producers := val.(ProducerMap) | ||
_, found := producers[p.peerInfo.id] | ||
if found == false { | ||
producers[p.peerInfo.id] = p | ||
} | ||
|
||
return !found | ||
} | ||
|
||
// remove a producer from a registration | ||
func (r *RegistrationDB) RemoveProducer(k Registration, id string) (bool, int) { | ||
r.Lock() | ||
defer r.Unlock() | ||
producers, ok := r.registrationMap[k] | ||
value, ok := r.registrationMap.Load(k) | ||
if !ok { | ||
return false, 0 | ||
} | ||
producers := value.(ProducerMap) | ||
removed := false | ||
if _, exists := producers[id]; exists { | ||
removed = true | ||
} | ||
|
||
// Note: this leaves keys in the DB even if they have empty lists | ||
delete(producers, id) | ||
|
||
return removed, len(producers) | ||
} | ||
|
||
// remove a Registration and all it's producers | ||
func (r *RegistrationDB) RemoveRegistration(k Registration) { | ||
r.Lock() | ||
defer r.Unlock() | ||
delete(r.registrationMap, k) | ||
r.registrationMap.Delete(k) | ||
} | ||
|
||
func (r *RegistrationDB) needFilter(key string, subkey string) bool { | ||
return key == "*" || subkey == "*" | ||
} | ||
|
||
func (r *RegistrationDB) FindRegistrations(category string, key string, subkey string) Registrations { | ||
r.RLock() | ||
defer r.RUnlock() | ||
if !r.needFilter(key, subkey) { | ||
k := Registration{category, key, subkey} | ||
if _, ok := r.registrationMap[k]; ok { | ||
if _, ok := r.registrationMap.Load(k); ok { | ||
return Registrations{k} | ||
} | ||
return Registrations{} | ||
} | ||
results := Registrations{} | ||
for k := range r.registrationMap { | ||
if !k.IsMatch(category, key, subkey) { | ||
continue | ||
r.registrationMap.Range(func(k, _ interface{}) bool { | ||
if k.(Registration).IsMatch(category, key, subkey) { | ||
results = append(results, k.(Registration)) | ||
} | ||
results = append(results, k) | ||
} | ||
return true | ||
}) | ||
|
||
return results | ||
} | ||
|
||
func (r *RegistrationDB) FindProducers(category string, key string, subkey string) Producers { | ||
r.RLock() | ||
defer r.RUnlock() | ||
if !r.needFilter(key, subkey) { | ||
k := Registration{category, key, subkey} | ||
return ProducerMap2Slice(r.registrationMap[k]) | ||
val, _ := r.registrationMap.Load(k) | ||
|
||
r.RLock() | ||
defer r.RUnlock() | ||
return ProducerMap2Slice(val.(ProducerMap)) | ||
} | ||
|
||
r.RLock() | ||
results := make(map[string]struct{}) | ||
var retProducers Producers | ||
for k, producers := range r.registrationMap { | ||
if !k.IsMatch(category, key, subkey) { | ||
continue | ||
} | ||
for _, producer := range producers { | ||
_, found := results[producer.peerInfo.id] | ||
if found == false { | ||
results[producer.peerInfo.id] = struct{}{} | ||
retProducers = append(retProducers, producer) | ||
r.registrationMap.Range(func(k, v interface{}) bool { | ||
if k.(Registration).IsMatch(category, key, subkey) { | ||
producers := v.(ProducerMap) | ||
for _, producer := range producers { | ||
_, found := results[producer.peerInfo.id] | ||
if found == false { | ||
results[producer.peerInfo.id] = struct{}{} | ||
retProducers = append(retProducers, producer) | ||
} | ||
} | ||
} | ||
} | ||
return true | ||
}) | ||
|
||
r.RUnlock() | ||
|
||
return retProducers | ||
} | ||
|
||
func (r *RegistrationDB) LookupRegistrations(id string) Registrations { | ||
r.RLock() | ||
defer r.RUnlock() | ||
|
||
results := Registrations{} | ||
for k, producers := range r.registrationMap { | ||
r.registrationMap.Range(func(k, v interface{}) bool { | ||
producers := v.(ProducerMap) | ||
if _, exists := producers[id]; exists { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you also need the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
results = append(results, k) | ||
results = append(results, k.(Registration)) | ||
} | ||
} | ||
|
||
return true | ||
}) | ||
|
||
r.RUnlock() | ||
|
||
return results | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 think you need to
r.RLock()
around thisThere 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.
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 you take the
r.RLock()
around the whole loop, that puts this back close to where it started. If you can take theRLock()
only after matching a Registration and before iterating over theProducerMap
, and then releasing it after finishing with thatProducerMap
, it could avoid starving 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.
Yes. Actually after finally finished this PR, i have thought about this. The PR now seems similar to the original one. But, some write lock has been erased.
Not quite sure about this. Since read lock can be acquired simultaneously and acquiring and releasing read lock frequently may utilize more CPU.
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.
yeah ... that's why I said this strategy is "sounding more iffy"
as I see it, it's either go back to the original, or take the read lock just around accessing each ProducerMap ("thrashing" the lock I guess), or turn all these ProducerMap into sync.Map themselves