-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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 all 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,80 @@ | ||
| /* | ||
| 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 | ||
|
|
||
| // 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 waits for all pending operations on the cache to settle. Since cache writes | ||
| // are asynchronous, a write may not be immediately accessible unless the user | ||
| // manually calls Wait. | ||
| Wait() | ||
|
|
||
| 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. The options in the | ||
| // Config struct control the memory and entry limits for the cache, and the underlying cache | ||
| // implementation. | ||
| func NewDefaultCacheImpl(cfg *Config) Cache { | ||
| switch { | ||
| case cfg == nil || (cfg.MaxEntries == 0 && cfg.MaxMemoryUsage == 0): | ||
| return &nullCache{} | ||
|
|
||
| case cfg.LFU: | ||
| return NewRistrettoCache(cfg.MaxEntries, cfg.MaxMemoryUsage, func(val interface{}) int64 { | ||
| return val.(cachedObject).CachedSize(true) | ||
| }) | ||
|
|
||
| default: | ||
| return NewLRUCache(cfg.MaxEntries, func(_ interface{}) int64 { | ||
| return 1 | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // Config is the configuration options for a cache instance | ||
| type Config struct { | ||
| // MaxEntries is the estimated amount of entries that the cache will hold at capacity | ||
| MaxEntries int64 | ||
| // MaxMemoryUsage is the maximum amount of memory the cache can handle | ||
| MaxMemoryUsage int64 | ||
| // LFU toggles whether to use a new cache implementation with a TinyLFU admission policy | ||
| LFU bool | ||
| } | ||
|
|
||
| // DefaultConfig is the default configuration for a cache instance in Vitess | ||
| var DefaultConfig = &Config{ | ||
| MaxEntries: 5000, | ||
| MaxMemoryUsage: 32 * 1024 * 1024, | ||
| LFU: false, | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.