Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Stats & Observability Pipeline

NoKV exposes internal health via the Go expvar package and the nokv stats CLI. The statistics subsystem is implemented in stats.go and runs continuously once the DB is open.


1. Architecture

flowchart TD
    subgraph Collectors
        Flush[lsm.FlushMetrics]
        Levels[lsm.CompactionStats]
        VLog[valueLog.metrics]
        WAL[wal.Manager.Metrics]
        Txn[oracle.txnMetricsSnapshot]
        Cache[lsm.CacheMetrics]
        Hot[hotring.TopN]
    end
    Collectors --> Stats
    Stats -->|expvar publish| Runtime
    Stats -->|Snapshot| CLI
  • newStats wires together reusable expvar.Int/Float gauges (avoiding duplicates if the process restarts an embedded DB).
  • Stats.StartStats launches a goroutine that ticks every 5s (configurable via Stats.interval) to refresh values.
  • Stats.Snapshot can be called on-demand (e.g. CLI) without mutating expvar state.

2. Snapshot Fields

FieldSourceDescription
Entrieslsm.EntryCount()Total MVCC entries (L0-Ln + memtables). Mirrors Stats.EntryNum for backwards compat.
FlushPending/Queue/Activelsm.FlushMetrics()Pending immutables, queue length, workers currently building SSTs.
FlushWait/Build/ReleaseMsDerived from WaitNs/BuildNs/ReleaseNs averagesEnd-to-end latency of flush pipeline stages.
CompactionBacklog/MaxScorelsm.CompactionStats()How many level files await compaction and the hottest score.
ValueLogSegments/PendingDel/DiscardQueue/HeadvalueLog.metrics()Tracks vlog utilisation and GC backlog.
WALActiveSegment/SegmentCount/Removed/ActiveSizewal.Manager.Metrics()Observes WAL rotation cadence and current segment byte usage (pairs with raft lag metrics).
WALTypedRecordRatio/Warning/ReasonWAL backlog watchdog (Stats.Snapshot)Tracks ratio of raft typed records in the WAL and surfaces warnings with reasons when exceeding thresholds.
WALAutoGCRuns/Removed/LastUnixWAL backlog watchdogAutomated WAL GC passes, total segments removed, and the Unix timestamp of the last run.
WriteQueueDepth/Entries/ByteswriteMetrics.snapshot()Size of the asynchronous write queue.
WriteAvg*writeMetrics averagesRequest wait times, vlog latency, apply latency.
WriteBatchesTotalwriteMetricsLifetime batches processed.
HotWriteLimiteddb.hotWriteLimitedNumber of write attempts rejected by Options.WriteHotKeyLimit (HotRing write throttling).
WriteThrottleActivedb.blockWritesIndicates when writes are being throttled.
TxnsActive/Started/Committed/Conflictsoracle.txnMetricsSnapshot()MVCC activity counters.
HotKeyshotring.TopN()Top-K hot key counts.
BlockL0/L1/BloomHitRatelsm.CacheMetrics()Block and bloom cache hit ratios.
IndexHitRatelsm.CacheMetrics()SST 索引块缓存命中率。
IteratorReusediteratorPool.reused()Frequency of iterator pooling hits.
RaftGroupCount/LaggingGroups/MaxLagSegments/LagWarnThreshold/RaftLagWarningmanifest.RaftPointerSnapshot()Tracks follower backlogs; LagWarnThreshold comes from Options.RaftLagWarnSegments, and RaftLagWarning toggles when any group exceeds it.
RegionTotal/New/Running/Removing/Tombstone/Otherstore.RegionMetricsMulti-Raft region state distribution. CLI attaches the first available RegionMetrics by default; pass --no-region-metrics to disable.

All values are exported under the NoKV.* namespace via expvar (see newStats).


3. CLI & JSON Output

  • nokv stats --workdir <dir> prints a human-readable table (queue lengths, throughput, hot keys, region totals). It automatically attaches RegionMetrics when available; add --no-region-metrics to produce a manifest-only snapshot.
  • When RaftLagWarning=true the CLI emits an extra Raft.Warning line; it also surfaces Regions.Total (...) so operators can quickly gauge Region lifecycle health.
  • nokv stats --json emits the raw snapshot for automation. Example snippet:
{
  "entries": 1048576,
  "flush_queue_length": 2,
  "vlog_head": {"fid": 5, "offset": 184320},
  "hot_keys": [{"key": "user:123", "count": 42}]
}

The CLI internally instantiates a read-only DB handle, calls Stats.Snapshot, and formats the response—no background goroutine is needed.


4. Integration with Other Modules

ModuleContribution
WALwal.Manager.Metrics() counts active/removable segments, aiding post-recovery validation.
Value LogvalueLog.metrics() exposes GC backlog, enabling alerting when discard queues stall.
HotRingPublishes hot key JSON via expvar so dashboards can visualise top offenders.
TransactionsOracle counters help gauge contention (high conflicts → tune workload).
CacheHit rates clarify whether cache sizing (hot/cold tier) needs adjustment.

5. Comparisons

EngineObservability
RocksDBiostats, perf_context, ldb commands. Requires manual parsing.
BadgerPrometheus metrics (optional).
NoKVBuilt-in expvar gauges + CLI + recovery trace toggles.

NoKV emphasises zero-dependency observability. Everything is consumable via HTTP /debug/vars or the CLI, making it easy to integrate with Go services.


6. Operational Guidance

  • Watch FlushQueueLength and CompactionBacklog together—if both grow, increase flush workers or adjust level sizes.
  • ValueLogDiscardQueue > 0 for extended periods indicates GC is blocked; inspect NoKV.ValueLog.GcRuns and consider tuning thresholds.
  • WriteThrottleActive toggling frequently suggests L0 is overwhelmed; cross-check BlockL0HitRate and compaction metrics.
  • HotWriteLimited climbing steadily means HotRing write throttling is firing—surface utils.ErrHotKeyWriteThrottle to clients and investigate abusive keys via the HotKeys list.
  • RaftLagWarning toggling to true means at least one follower lags the leader by more than Options.RaftLagWarnSegments; inspect Raft.Warning from the CLI and consider snapshot resend or throttling the offending node.
  • Regions.Total should match the expected cluster topology; sustained Removing/Tombstone counts indicate stalled cleanup—investigate split/merge logic or stuck replicas.

Refer to docs/testing.md for scripted checks that validate stats during CI runs.