-
Notifications
You must be signed in to change notification settings - Fork 5.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
bindinfo: add warning message when the memory usage of the cache exceeds its capacity #32866
Merged
Merged
Changes from 2 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
3f152ae
bindinfo: add warning message when the memory usage of the binding_ca…
Reminiscent 0062a52
Merge branch 'master' of github.com:pingcap/tidb into addWarning4bind…
Reminiscent 7b37080
add more test cases
Reminiscent a40f644
remove useless line
Reminiscent 10bf341
fix ut
Reminiscent 03f6b3a
Merge branch 'master' of github.com:pingcap/tidb into addWarning4bind…
Reminiscent 538182c
Merge branch 'master' of github.com:pingcap/tidb into addWarning4bind…
Reminiscent fa9ef08
rename TiDBMemQuotaBindCache to TiDBMemQuotaBindingCache
Reminiscent 0dfbe1a
fix ut
Reminiscent 3a1ea99
fix ut
Reminiscent 8a09095
Merge branch 'master' of github.com:pingcap/tidb into addWarning4bind…
Reminiscent 1da4ed2
address comments
Reminiscent ae595fb
Merge branch 'master' of github.com:pingcap/tidb into addWarning4bind…
Reminiscent 7b1a3f7
fix ut
Reminiscent 7fd22f7
fix ut
Reminiscent 1094fd3
Merge branch 'master' of github.com:pingcap/tidb into addWarning4bind…
Reminiscent 985f3af
address comments
Reminiscent afe84a7
address comments
Reminiscent 77598b4
Merge branch 'master' of github.com:pingcap/tidb into addWarning4bind…
Reminiscent 3691299
address comments
Reminiscent 5525271
Merge branch 'master' of github.com:pingcap/tidb into addWarning4bind…
Reminiscent 6ddf3b2
Merge branch 'master' into addWarning4bindCache
ti-chi-bot c08a031
fix ut
Reminiscent 3ecd695
Merge branch 'master' of github.com:pingcap/tidb into addWarning4bind…
Reminiscent 9cdbb67
Merge remote-tracking branch 'origin/addWarning4bindCache' into addWa…
Reminiscent 43db335
Merge branch 'master' into addWarning4bindCache
ti-chi-bot ee3f7ef
Merge branch 'master' into addWarning4bindCache
ti-chi-bot 8ef10da
Merge branch 'master' of github.com:pingcap/tidb into addWarning4bind…
Reminiscent 8e209b5
fix ut
Reminiscent 4425015
Merge remote-tracking branch 'origin/addWarning4bindCache' into addWa…
Reminiscent 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,13 +15,16 @@ | |
package bindinfo | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
|
||
"github.com/cznic/mathutil" | ||
"github.com/pingcap/tidb/sessionctx/variable" | ||
"github.com/pingcap/tidb/util/hack" | ||
"github.com/pingcap/tidb/util/kvcache" | ||
"github.com/pingcap/tidb/util/logutil" | ||
"github.com/pingcap/tidb/util/memory" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// bindCache uses the LRU cache to store the bindRecord. | ||
|
@@ -75,26 +78,29 @@ func (c *bindCache) get(key bindCacheKey) []*BindRecord { | |
|
||
// set inserts an item to the cache. It's not thread-safe. | ||
// Only other functions of the bindCache can use this function. | ||
func (c *bindCache) set(key bindCacheKey, value []*BindRecord) bool { | ||
// The set operation is failed when the memory usage of binding_cache exceeds its capacity. | ||
func (c *bindCache) set(key bindCacheKey, value []*BindRecord) (err error) { | ||
mem := calcBindCacheKVMem(key, value) | ||
if mem > c.memCapacity { // ignore this kv pair if its size is too large | ||
return false | ||
err = errors.New("The memory usage of the binding_cache exceeds its capacity") | ||
return | ||
} | ||
bindRecords := c.get(key) | ||
if bindRecords != nil { | ||
// Remove the origin key-value pair. | ||
mem -= calcBindCacheKVMem(key, bindRecords) | ||
} | ||
for mem+c.memTracker.BytesConsumed() > c.memCapacity { | ||
err = errors.New("The memory usage of the binding_cache exceeds its capacity") | ||
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. ditto. If we delete those old cache entries, we won't exceed the capacity. |
||
evictedKey, evictedValue, evicted := c.cache.RemoveOldest() | ||
if !evicted { | ||
return false | ||
return | ||
} | ||
c.memTracker.Consume(-calcBindCacheKVMem(evictedKey.(bindCacheKey), evictedValue.([]*BindRecord))) | ||
} | ||
c.memTracker.Consume(mem) | ||
c.cache.Put(key, value) | ||
return true | ||
return | ||
} | ||
|
||
// delete remove an item from the cache. It's not thread-safe. | ||
|
@@ -105,8 +111,9 @@ func (c *bindCache) delete(key bindCacheKey) bool { | |
mem := calcBindCacheKVMem(key, bindRecords) | ||
c.cache.Delete(key) | ||
c.memTracker.Consume(-mem) | ||
return true | ||
} | ||
return true | ||
return false | ||
Comment on lines
+129
to
+131
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. It's a bug here, But for now, the return value has not been used yet. |
||
} | ||
|
||
// GetBindRecord gets the BindRecord from the cache. | ||
|
@@ -150,7 +157,10 @@ func (c *bindCache) SetBindRecord(hash string, meta *BindRecord) { | |
metas[i] = meta | ||
} | ||
} | ||
c.set(cacheKey, []*BindRecord{meta}) | ||
err := c.set(cacheKey, []*BindRecord{meta}) | ||
if err != nil { | ||
logutil.BgLogger().Warn("[sql-bind] ", zap.Error(err)) | ||
} | ||
} | ||
|
||
// RemoveBindRecord removes the BindRecord which has same originSQL with specified BindRecord. | ||
|
@@ -175,7 +185,9 @@ func (c *bindCache) RemoveBindRecord(hash string, meta *BindRecord) { | |
} | ||
} | ||
} | ||
c.set(bindCacheKey(hash), metas) | ||
// This function can guarantee the memory usage for the cache will never grow up. | ||
// So we don't need to handle the return value here. | ||
_ = c.set(bindCacheKey(hash), metas) | ||
} | ||
|
||
// SetMemCapacity sets the memory capacity for the cache. | ||
|
@@ -195,19 +207,34 @@ func (c *bindCache) GetMemCapacity() int64 { | |
return c.memCapacity | ||
} | ||
|
||
// GetMemUsage get the memory Usage for the cache. | ||
// The function is thread-safe. | ||
func (c *bindCache) GetMemUsage() int64 { | ||
c.lock.Lock() | ||
defer c.lock.Unlock() | ||
return c.memTracker.BytesConsumed() | ||
} | ||
|
||
// Copy copies a new bindCache from the origin cache. | ||
// The function is thread-safe. | ||
func (c *bindCache) Copy() *bindCache { | ||
c.lock.Lock() | ||
defer c.lock.Unlock() | ||
newCache := newBindCache() | ||
if c.GetMemUsage() > newCache.GetMemCapacity() { | ||
logutil.BgLogger().Warn("[sql-bind] During the cache copy operation, " + | ||
"the memory capacity of the new cache is less than the memory consumption of the old cache, " + | ||
"so there are bindings that are not loaded into the cache") | ||
} | ||
keys := c.cache.Keys() | ||
for _, key := range keys { | ||
cacheKey := key.(bindCacheKey) | ||
v := c.get(cacheKey) | ||
bindRecords := make([]*BindRecord, len(v)) | ||
copy(bindRecords, v) | ||
newCache.set(cacheKey, bindRecords) | ||
// The memory usage of cache has been handled at the beginning of this function. | ||
// So we don't need to handle the return value here. | ||
_ = newCache.set(cacheKey, bindRecords) | ||
} | ||
return newCache | ||
} |
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
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 it's a bit strange here. Actually we haven't exceeded the capacity yet here, because we didn't put this binding into the cache