-
Notifications
You must be signed in to change notification settings - Fork 84
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
Fix panic during bind throttle #404
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. The change looks good. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,9 +54,9 @@ func GetBindEvict() *BindEvict { | |
} | ||
return cfg.(*BindEvict) | ||
} | ||
func (this *BindEvict) Copy() *BindEvict { | ||
out := BindEvict{BindThrottle: make(map[uint32]map[string]*BindThrottle)} | ||
for k, v := range this.BindThrottle { | ||
func (bindEvict *BindEvict) Copy() *BindEvict { | ||
out := BindEvict{BindThrottle:make(map[uint32]map[string]*BindThrottle)} | ||
for k,v := range bindEvict.BindThrottle { | ||
out.BindThrottle[k] = v | ||
} | ||
return &out | ||
|
@@ -85,41 +85,44 @@ func (entry *BindThrottle) decrAllowEveryX(y int) { | |
return | ||
} | ||
entry.AllowEveryX = 0 | ||
GetBindEvict().lock.Lock() | ||
defer GetBindEvict().lock.Unlock() | ||
} | ||
func (entry *BindThrottle) incrAllowEveryX() { | ||
if logger.GetLogger().V(logger.Warning) { | ||
info := fmt.Sprintf("hash:%d bindName:%s val:%s prev:%d", entry.Sqlhash, entry.Name, entry.Value, entry.AllowEveryX) | ||
logger.GetLogger().Log(logger.Warning, "bind throttle incr", info) | ||
} | ||
entry.AllowEveryX = 3*entry.AllowEveryX + 1 | ||
if entry.AllowEveryX > 10000 { | ||
entry.AllowEveryX = 10000 | ||
} | ||
} | ||
|
||
func (bindEvict *BindEvict) updateThrottle(entry *BindThrottle) { | ||
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. This function needs to be used with caution (hold the lock when updating the copy), the new struct has a different lock. 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. Can we change decrease logic also to avoid slow recovery? |
||
// delete entry | ||
if len(GetBindEvict().BindThrottle[entry.Sqlhash]) == 1 { | ||
updateCopy := GetBindEvict().Copy() | ||
if len(bindEvict.BindThrottle[entry.Sqlhash]) == 1 { | ||
updateCopy := bindEvict.Copy() | ||
delete(updateCopy.BindThrottle, entry.Sqlhash) | ||
gBindEvict.Store(updateCopy) | ||
} else { | ||
// copy everything except bindKV (skipping it is deleting it) | ||
bindKV := fmt.Sprintf("%s|%s", entry.Name, entry.Value) | ||
updateCopy := make(map[string]*BindThrottle) | ||
for k, v := range GetBindEvict().BindThrottle[entry.Sqlhash] { | ||
updateCopy := bindEvict.Copy() | ||
updateBindThrottleCopy := make(map[string]*BindThrottle) | ||
for k,v := range bindEvict.BindThrottle[entry.Sqlhash] { | ||
if k == bindKV { | ||
continue | ||
} | ||
updateCopy[k] = v | ||
updateBindThrottleCopy[k] = v | ||
} | ||
GetBindEvict().BindThrottle[entry.Sqlhash] = updateCopy | ||
} | ||
} | ||
func (entry *BindThrottle) incrAllowEveryX() { | ||
if logger.GetLogger().V(logger.Warning) { | ||
info := fmt.Sprintf("hash:%d bindName:%s val:%s prev:%d", entry.Sqlhash, entry.Name, entry.Value, entry.AllowEveryX) | ||
logger.GetLogger().Log(logger.Warning, "bind throttle incr", info) | ||
} | ||
entry.AllowEveryX = 3*entry.AllowEveryX + 1 | ||
if entry.AllowEveryX > 10000 { | ||
entry.AllowEveryX = 10000 | ||
updateCopy.BindThrottle[entry.Sqlhash] = updateBindThrottleCopy | ||
gBindEvict.Store(updateCopy) | ||
} | ||
} | ||
|
||
func (be *BindEvict) ShouldBlock(sqlhash uint32, bindKV map[string]string, heavyUsage bool) (bool, *BindThrottle) { | ||
GetBindEvict().lock.Lock() | ||
sqlBinds := GetBindEvict().BindThrottle[sqlhash] | ||
GetBindEvict().lock.Unlock() | ||
func (bindEvict *BindEvict) ShouldBlock(sqlhash uint32, bindKV map[string]string, heavyUsage bool) (bool, *BindThrottle) { | ||
bindEvict.lock.Lock() | ||
defer bindEvict.lock.Unlock() | ||
sqlBinds := bindEvict.BindThrottle[sqlhash] | ||
for k0, v := range bindKV /*parseBinds(request)*/ { | ||
k := NormalizeBindName(k0) | ||
concatKey := fmt.Sprintf("%s|%s", k, v) | ||
|
@@ -143,6 +146,7 @@ func (be *BindEvict) ShouldBlock(sqlhash uint32, bindKV map[string]string, heavy | |
gap := now.Sub(*recent).Seconds() * GetConfig().BindEvictionDecrPerSec | ||
entry.decrAllowEveryX(int(gap)) | ||
if entry.AllowEveryX == 0 { | ||
bindEvict.updateThrottle(entry) | ||
return false, 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.
The outer lock on bindevict is being held for too long. It gets unlocked only after inner lock on workerpool is done processing dispatched workers.
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.
Though it is on copy. It is safe reduce scope of the lock