-
Notifications
You must be signed in to change notification settings - Fork 2.4k
LFU Cache Implementation #7439
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
Merged
Merged
LFU Cache Implementation #7439
Changes from 17 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
187f808
executor: do not lookup in cache twice
vmg 4641065
plan: benchmark plan building for DML vs SELECT statements
vmg f34f884
cache: reduce API surface
vmg cbbba9e
cache: abstract into a Cache interface
vmg 76bd931
tools: do not cache E2E tests between runs
vmg 652a94c
cache: configure using total memory usage
vmg 1093c69
cache: do not return `nil` stats
vmg 2e3e0c0
cache: fix flaky memory usage test
vmg d6a13f1
cache: switch to a new implementation based on Ristretto
vmg 1cade52
endtoend: fix test values
vmg 5f2a612
plan builder: use standard V3 planner
vmg 4c35cfa
cache: make unit test more reliable
vmg 70c8038
cache: speed up clearing large caches
vmg f681e00
cache: make the cache implementation swappable
vmg 7c0c56f
cache: fix DropUpdates test
vmg f7ad521
cache: use the legacy LRU cache by default
vmg 9fe7cc0
cache: remove more unused features
vmg a3feeb8
cache: handle default arguments for both cache types
vmg d9cd613
Merge branch 'master' into lfu-cache
vmg 15dae44
hack: document empty Goassembly file
vmg a8b242d
cache: allow configuring both entries & size
vmg 37db725
hack: update license header
vmg 915e019
query_engine: add test for cache pollution
vmg 431da53
cache: review feedback
vmg 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 hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| /* | ||
| Copyright 2021 The Vitess Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package cache | ||
|
|
||
| // DefaultCacheSize is the default size for a Vitess cache instance. | ||
| // If this value is specified in BYTES, Vitess will use a LFU-based cache that keeps track of the total | ||
| // memory usage of all cached entries accurately. If this value is specified in ENTRIES, Vitess will | ||
| // use the legacy LRU cache implementation which only tracks the amount of entries being stored. | ||
| // Changing this value affects: | ||
| // - the default values for CLI arguments in VTGate | ||
| // - the default values for the config files in VTTablet | ||
| // - the default values for the test caches used in integration and end-to-end tests | ||
| // Regardless of the default value used here, the user can always override Vitess' configuration to | ||
| // force a specific cache type (e.g. when passing a value in ENTRIES to vtgate, the service will use | ||
| // a LRU cache). | ||
| const DefaultCacheSize = SizeInEntries(5000) | ||
|
|
||
| // const DefaultCacheSize = SizeInBytes(64 * 1024 * 1024) | ||
|
vmg marked this conversation as resolved.
Outdated
|
||
|
|
||
| // Cache is a generic interface type for a data structure that keeps recently used | ||
| // objects in memory and evicts them when it becomes full. | ||
| type Cache interface { | ||
| Get(key string) (interface{}, bool) | ||
| Set(key string, val interface{}) bool | ||
| ForEach(callback func(interface{}) bool) | ||
|
|
||
| Delete(key string) | ||
| Clear() | ||
| Wait() | ||
|
vmg marked this conversation as resolved.
|
||
|
|
||
| Len() int | ||
| Evictions() int64 | ||
| UsedCapacity() int64 | ||
| MaxCapacity() int64 | ||
| SetCapacity(int64) | ||
| } | ||
|
|
||
| type cachedObject interface { | ||
| CachedSize(alloc bool) int64 | ||
| } | ||
|
|
||
| // NewDefaultCacheImpl returns the default cache implementation for Vitess. If the given capacity | ||
| // is given in bytes, the implementation will be LFU-based and keep track of the total memory usage | ||
| // for the cache. If the implementation is given in entries, the legacy LRU implementation will be used, | ||
| // keeping track | ||
|
vmg marked this conversation as resolved.
Outdated
|
||
| func NewDefaultCacheImpl(capacity Capacity, averageItemSize int64) Cache { | ||
| switch { | ||
| case capacity == nil || (capacity.Entries() == 0 && capacity.Bytes() == 0): | ||
| return &nullCache{} | ||
|
|
||
| case capacity.Bytes() != 0: | ||
| return NewRistrettoCache(capacity.Bytes(), averageItemSize, func(val interface{}) int64 { | ||
| return val.(cachedObject).CachedSize(true) | ||
| }) | ||
|
|
||
| default: | ||
| return NewLRUCache(capacity.Entries(), func(_ interface{}) int64 { | ||
| return 1 | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // Capacity is the interface implemented by numeric types that define a cache's capacity | ||
| type Capacity interface { | ||
| Bytes() int64 | ||
| Entries() int64 | ||
| } | ||
|
|
||
| // SizeInBytes is a Capacity that measures the total size of the cache in Bytes | ||
| type SizeInBytes int64 | ||
|
|
||
| // Bytes returns the size of the cache in Bytes | ||
| func (s SizeInBytes) Bytes() int64 { | ||
| return int64(s) | ||
| } | ||
|
|
||
| // Entries returns 0 because this Capacity measures the cache size in Bytes | ||
| func (s SizeInBytes) Entries() int64 { | ||
| return 0 | ||
| } | ||
|
|
||
| // SizeInEntries is a Capacity that measures the total size of the cache in Entries | ||
| type SizeInEntries int64 | ||
|
|
||
| // Bytes returns 0 because this Capacity measures the cache size in Entries | ||
| func (s SizeInEntries) Bytes() int64 { | ||
| return 0 | ||
| } | ||
|
|
||
| // Entries returns the size of the cache in Entries | ||
| func (s SizeInEntries) Entries() int64 { | ||
| return int64(s) | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
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.
This is the global toggle that will allow us to switch the default cache implementation.
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.
This is clever! Correct me if I'm wrong, but it seems to me that if we toggle this, then the code in
vtgate.gowould have to change to allow use of theEntriesoption.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.
Correct! I should probably flag that. The code in
vtgate.gois written so that when the LRU cache is the default, setting a LFU cache flag overrides it. The opposite needs to be true when the LFU cache is the default.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.
@deepthi Fixed in a3feeb8 so we don't have to change this in the future 👌