-
Notifications
You must be signed in to change notification settings - Fork 3
feat: cold / warm / hot layer levels cache storage #40
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3ed2749
feat: introduce storage migration functionality with configurable pro…
sendya 959323e
feat: promote support
sendya 9be7f17
feat: Add `Touch` method to storage buckets for updating object acces…
sendya 7323e1a
fix: test case config apply `migration`
sendya f304555
feat: implement object migration for memory and disk buckets and corr…
sendya 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package storage | ||
|
|
||
| import "time" | ||
|
|
||
| type ( | ||
| PromoteConfig struct { | ||
| MinHits int `json:"min_hits" yaml:"min_hits"` // 时间窗口内命中 >= N | ||
| Window time.Duration `json:"window" yaml:"window"` // 时间窗口 1m | ||
| } | ||
| DemoteConfig struct { | ||
| MinHits int `json:"min_hits" yaml:"min_hits"` // 时间窗口内命中 <= N | ||
| Window time.Duration `json:"window" yaml:"window"` // 时间窗口 1m | ||
| Occupancy float64 `json:"occupancy" yaml:"occupancy"` // 热盘存储占用率 >= N% | ||
| } | ||
| MigrationConfig struct { | ||
| Enabled bool `json:"enabled" yaml:"enabled"` | ||
| Promote PromoteConfig `json:"promote" yaml:"promote"` // 升温 | ||
| Demote DemoteConfig `json:"demote" yaml:"demote"` // 降温 | ||
| } | ||
|
|
||
| BucketConfig struct { | ||
| Path string `json:"path" yaml:"path"` // local path or ? | ||
| Driver string `json:"driver" yaml:"driver"` // native, custom-driver | ||
| Type string `json:"type" yaml:"type"` // normal, cold, hot, fastmemory | ||
| DBType string `json:"db_type" yaml:"db_type"` // boltdb, badgerdb, pebble | ||
| DBPath string `json:"db_path" yaml:"db_path"` // db path, defult: <bucket_path>/.indexdb | ||
| AsyncLoad bool `json:"async_load" yaml:"async_load"` // load metadata async | ||
| SliceSize uint64 `json:"slice_size" yaml:"slice_size"` // slice size for each part | ||
| MaxObjectLimit int `json:"max_object_limit" yaml:"max_object_limit"` // max object limit, upper Bound discard | ||
| Migration *MigrationConfig `json:"migration" yaml:"migration"` // migration config | ||
| DBConfig map[string]any `json:"db_config" yaml:"db_config"` // custom db config | ||
| } | ||
| ) |
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
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
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
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -93,6 +93,15 @@ storage: | |||||
| eviction_policy: fifo # fifo, lru, lfu | ||||||
| selection_policy: hashring # hashring, roundrobin | ||||||
| slice_size: 1048576 # 1MB | ||||||
| migration: | ||||||
| enabled: false # enable tiering bucket | ||||||
| promote: | ||||||
| min_hits: 10 # window hits to promote | ||||||
| window: 1m # 1 minute window | ||||||
| demote: | ||||||
| min_hits: 2 # window hits to demote | ||||||
| window: 5m # 5 minutes window | ||||||
| occupancy: 75 # percent useage | ||||||
|
||||||
| occupancy: 75 # percent useage | |
| occupancy: 75 # percent usage |
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
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
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,139 @@ | ||
| package heavykeeper | ||
|
|
||
| import ( | ||
| "hash/fnv" | ||
| "math" | ||
| "math/rand" | ||
| "sync" | ||
| "time" | ||
| ) | ||
|
|
||
| // HeavyKeeper is a probabilistic data structure for top-k items. | ||
| type HeavyKeeper struct { | ||
| buckets [][]bucket | ||
| depth int | ||
| width int | ||
| decay float64 | ||
| r *rand.Rand | ||
| mu sync.RWMutex | ||
| } | ||
|
|
||
| type bucket struct { | ||
| fingerprint uint64 | ||
| count uint32 | ||
| } | ||
|
|
||
| // New creates a new HeavyKeeper. | ||
| // depth: number of arrays (hash functions) | ||
| // width: number of buckets per array | ||
| // decay: probability of decay (0.9 means 90% chance to decay) | ||
| func New(depth, width int, decay float64) *HeavyKeeper { | ||
| hk := &HeavyKeeper{ | ||
| buckets: make([][]bucket, depth), | ||
| depth: depth, | ||
| width: width, | ||
| decay: decay, | ||
| r: rand.New(rand.NewSource(time.Now().UnixNano())), | ||
| } | ||
|
|
||
| for i := range hk.buckets { | ||
| hk.buckets[i] = make([]bucket, width) | ||
| } | ||
|
|
||
| return hk | ||
| } | ||
|
|
||
| // Add adds a key to the HeavyKeeper. | ||
| func (hk *HeavyKeeper) Add(key []byte) { | ||
| hk.mu.Lock() | ||
| defer hk.mu.Unlock() | ||
|
|
||
| fingerprint := hk.hash(key) | ||
|
|
||
| // Use double hashing for multiple hash functions | ||
| // h1 = fingerprint | ||
| // h2 = fnv(key) | ||
| // idx = (h1 + i*h2) % width | ||
| h2 := hk.hash2(key) | ||
|
|
||
| for i := 0; i < hk.depth; i++ { | ||
| idx := (fingerprint + uint64(i)*h2) % uint64(hk.width) | ||
| b := &hk.buckets[i][idx] | ||
|
|
||
| if b.count == 0 { | ||
| b.fingerprint = fingerprint | ||
| b.count = 1 | ||
| continue | ||
| } | ||
|
|
||
| if b.fingerprint == fingerprint { | ||
| b.count++ | ||
| continue | ||
| } | ||
|
|
||
| // Decay | ||
| if hk.r.Float64() < math.Pow(hk.decay, float64(b.count)) { | ||
| b.count-- | ||
| if b.count == 0 { | ||
| b.fingerprint = fingerprint | ||
| b.count = 1 | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Query returns the estimated count for the key. | ||
| func (hk *HeavyKeeper) Query(key []byte) uint32 { | ||
| hk.mu.RLock() | ||
| defer hk.mu.RUnlock() | ||
|
|
||
| fingerprint := hk.hash(key) | ||
| h2 := hk.hash2(key) | ||
| var maxCount uint32 | ||
|
|
||
| for i := 0; i < hk.depth; i++ { | ||
| idx := (fingerprint + uint64(i)*h2) % uint64(hk.width) | ||
| b := &hk.buckets[i][idx] | ||
|
|
||
| if b.fingerprint == fingerprint { | ||
| if b.count > maxCount { | ||
| maxCount = b.count | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return maxCount | ||
| } | ||
|
|
||
| // Clear resets the HeavyKeeper. | ||
| func (hk *HeavyKeeper) Clear() { | ||
| hk.mu.Lock() | ||
| defer hk.mu.Unlock() | ||
|
|
||
| for i := range hk.buckets { | ||
| for j := range hk.buckets[i] { | ||
| hk.buckets[i][j].count = 0 | ||
| hk.buckets[i][j].fingerprint = 0 | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func (hk *HeavyKeeper) hash(key []byte) uint64 { | ||
| h := fnv.New64a() | ||
| h.Write(key) | ||
| return h.Sum64() | ||
| } | ||
|
|
||
| func (hk *HeavyKeeper) hash2(key []byte) uint64 { | ||
| // Simple secondary hash: fnv with salt or just different algo | ||
| // Using FNV-1 (not 1a) or just rotate? | ||
| // Let's use a simple mix. | ||
| h := uint64(2166136261) | ||
| for _, c := range key { | ||
| h *= 16777619 | ||
| h ^= uint64(c) | ||
| } | ||
| // salt | ||
| h ^= 0x9e3779b97f4a7c15 | ||
| return h | ||
| } |
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.
NewMarkpacksclockandrefsinto a bitfield, but it currently ORs the rawrefsvalue without masking/clamping toRefsMask. Ifrefsexceeds 16 bits it will corrupt the clock bits. Sincerefsis nowint64, negative values would also corrupt the mark. Clamp/maskrefs(and enforce non-negative) insideNewMarkto keep the bit layout valid.