-
Notifications
You must be signed in to change notification settings - Fork 0
caching: export vlog mmap telemetry via expvar #830
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
Open
snissn
wants to merge
6
commits into
pr/vlog-mmap-sealed-byte-budget
Choose a base branch
from
pr/vlog-mmap-expvar-telemetry
base: pr/vlog-mmap-sealed-byte-budget
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6339068
caching: publish vlog mmap telemetry on expvar
codex 09592f7
Merge branch 'pr/vlog-mmap-sealed-byte-budget' into pr/vlog-mmap-expv…
codex d90a280
Merge branch 'pr/vlog-mmap-sealed-byte-budget' into pr/vlog-mmap-expv…
codex bee1cea
Merge branch 'pr/vlog-mmap-sealed-byte-budget' into pr/vlog-mmap-expv…
codex 720b926
Merge branch 'pr/vlog-mmap-sealed-byte-budget' into pr/vlog-mmap-expv…
codex 6474be1
Merge remote-tracking branch 'origin/pr/vlog-mmap-sealed-byte-budget'…
codex 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
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,94 @@ | ||
| package caching | ||
|
|
||
| import ( | ||
| "expvar" | ||
| "os" | ||
| "strconv" | ||
| "strings" | ||
| "sync" | ||
| "sync/atomic" | ||
| ) | ||
|
|
||
| const treedbExpvarEnabledEnvKey = "TREEDB_ENABLE_EXPVAR_STATS" | ||
|
|
||
| var ( | ||
| treedbExpvarEnabled = parseBoolEnvDefault(treedbExpvarEnabledEnvKey, true) | ||
snissn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| treedbExpvarPublishOnce sync.Once | ||
| treedbExpvarCurrentDB atomic.Pointer[DB] | ||
| ) | ||
|
|
||
| func parseBoolEnvDefault(key string, def bool) bool { | ||
| raw := strings.TrimSpace(os.Getenv(key)) | ||
| if raw == "" { | ||
| return def | ||
| } | ||
| switch strings.ToLower(raw) { | ||
| case "1", "true", "on", "yes": | ||
| return true | ||
| case "0", "false", "off", "no": | ||
| return false | ||
| default: | ||
| return def | ||
| } | ||
snissn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
snissn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| func publishTreeDBExpvarStats() { | ||
| treedbExpvarPublishOnce.Do(func() { | ||
snissn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| expvar.Publish("treedb", expvar.Func(func() any { | ||
| return currentTreeDBExpvarStats() | ||
| })) | ||
| }) | ||
| } | ||
snissn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| func registerTreeDBExpvarStatsDB(db *DB) { | ||
| if db == nil || !treedbExpvarEnabled { | ||
| return | ||
| } | ||
| publishTreeDBExpvarStats() | ||
| treedbExpvarCurrentDB.Store(db) | ||
| } | ||
snissn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| func unregisterTreeDBExpvarStatsDB(db *DB) { | ||
| if db == nil || !treedbExpvarEnabled { | ||
| return | ||
| } | ||
| treedbExpvarCurrentDB.CompareAndSwap(db, nil) | ||
| } | ||
|
|
||
| func currentTreeDBExpvarStats() map[string]any { | ||
| db := treedbExpvarCurrentDB.Load() | ||
| if db == nil { | ||
| return map[string]any{} | ||
| } | ||
| return selectTreeDBExpvarStats(db.Stats()) | ||
snissn marked this conversation as resolved.
Show resolved
Hide resolved
snissn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
snissn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| func selectTreeDBExpvarStats(stats map[string]string) map[string]any { | ||
| if len(stats) == 0 { | ||
| return map[string]any{} | ||
| } | ||
| out := make(map[string]any) | ||
| for k, v := range stats { | ||
| if strings.HasPrefix(k, "treedb.cache.vlog_mmap.") || | ||
| strings.HasPrefix(k, "treedb.process.memory.") { | ||
| out[k] = coerceStatsValue(v) | ||
| } | ||
| } | ||
| return out | ||
| } | ||
snissn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| func coerceStatsValue(v string) any { | ||
| if i, err := strconv.ParseInt(v, 10, 64); err == nil { | ||
| return i | ||
| } | ||
| if u, err := strconv.ParseUint(v, 10, 64); err == nil { | ||
| return u | ||
| } | ||
| if f, err := strconv.ParseFloat(v, 64); err == nil { | ||
| return f | ||
| } | ||
| if b, err := strconv.ParseBool(v); err == nil { | ||
| return b | ||
| } | ||
| return v | ||
| } | ||
snissn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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,47 @@ | ||
| package caching | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestSelectTreeDBExpvarStatsFiltersAndCoerces(t *testing.T) { | ||
| stats := map[string]string{ | ||
| "treedb.cache.vlog_mmap.active_bytes": "12345", | ||
| "treedb.cache.vlog_mmap.read.hit_ratio": "0.625000", | ||
| "treedb.cache.vlog_mmap.enabled": "true", | ||
| "treedb.process.memory.heap_inuse_bytes": "4096", | ||
| "treedb.process.memory.pool_pressure_level": "critical", | ||
| "treedb.cache.backpressure_mode": "adaptive", | ||
| "treedb.cache.entry_slice.trim_runs_total": "77", | ||
| "treedb.process.memory.pool_pressure_high_pct": "85.5", | ||
| } | ||
|
|
||
| got := selectTreeDBExpvarStats(stats) | ||
| if len(got) != 6 { | ||
| t.Fatalf("selectTreeDBExpvarStats len=%d want 6", len(got)) | ||
| } | ||
|
|
||
| if v, ok := got["treedb.cache.vlog_mmap.active_bytes"].(int64); !ok || v != 12345 { | ||
| t.Fatalf("active_bytes=%T(%v) want int64(12345)", got["treedb.cache.vlog_mmap.active_bytes"], got["treedb.cache.vlog_mmap.active_bytes"]) | ||
| } | ||
| if v, ok := got["treedb.cache.vlog_mmap.read.hit_ratio"].(float64); !ok || v != 0.625 { | ||
| t.Fatalf("hit_ratio=%T(%v) want float64(0.625)", got["treedb.cache.vlog_mmap.read.hit_ratio"], got["treedb.cache.vlog_mmap.read.hit_ratio"]) | ||
| } | ||
| if v, ok := got["treedb.cache.vlog_mmap.enabled"].(bool); !ok || !v { | ||
| t.Fatalf("enabled=%T(%v) want bool(true)", got["treedb.cache.vlog_mmap.enabled"], got["treedb.cache.vlog_mmap.enabled"]) | ||
| } | ||
| if v, ok := got["treedb.process.memory.heap_inuse_bytes"].(int64); !ok || v != 4096 { | ||
| t.Fatalf("heap_inuse_bytes=%T(%v) want int64(4096)", got["treedb.process.memory.heap_inuse_bytes"], got["treedb.process.memory.heap_inuse_bytes"]) | ||
| } | ||
| if v, ok := got["treedb.process.memory.pool_pressure_level"].(string); !ok || v != "critical" { | ||
| t.Fatalf("pool_pressure_level=%T(%v) want string(critical)", got["treedb.process.memory.pool_pressure_level"], got["treedb.process.memory.pool_pressure_level"]) | ||
| } | ||
| if _, ok := got["treedb.cache.backpressure_mode"]; ok { | ||
| t.Fatalf("unexpected backpressure_mode key in expvar selection") | ||
| } | ||
snissn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| func TestSelectTreeDBExpvarStatsEmpty(t *testing.T) { | ||
| got := selectTreeDBExpvarStats(nil) | ||
| if len(got) != 0 { | ||
| t.Fatalf("selectTreeDBExpvarStats(nil) len=%d want 0", len(got)) | ||
| } | ||
| } | ||
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.