From 86f3fda86e400dd948e8d9cf17590e100ffcd745 Mon Sep 17 00:00:00 2001 From: Philip O'Toole Date: Sat, 9 Sep 2023 13:57:01 -0400 Subject: [PATCH] Record size of snapshot persist --- snapshot/snapshot.go | 4 +++- snapshot/store.go | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/snapshot/snapshot.go b/snapshot/snapshot.go index 0282117c..e6102d2e 100644 --- a/snapshot/snapshot.go +++ b/snapshot/snapshot.go @@ -1,6 +1,7 @@ package snapshot import ( + "expvar" "fmt" "io" "os" @@ -38,7 +39,8 @@ func (s *Snapshot) Persist(sink raft.SnapshotSink) error { } defer stream.Close() - _, err = io.Copy(sink, stream) + n, err := io.Copy(sink, stream) + stats.Get(persistSize).(*expvar.Int).Set(n) return err } diff --git a/snapshot/store.go b/snapshot/store.go index 2bc0fda1..f806430e 100644 --- a/snapshot/store.go +++ b/snapshot/store.go @@ -3,6 +3,7 @@ package snapshot import ( "encoding/json" "errors" + "expvar" "fmt" "io" "log" @@ -19,6 +20,11 @@ import ( "github.com/rqlite/rqlite/db" ) +func init() { + stats = expvar.NewMap("snapshot") + ResetStats() +} + const ( minSnapshotRetain = 2 @@ -32,6 +38,10 @@ const ( tmpSuffix = ".tmp" ) +const ( + persistSize = "persist_size" +) + var ( // ErrRetainCountTooLow is returned when the retain count is too low. ErrRetainCountTooLow = errors.New("retain count must be >= 2") @@ -43,6 +53,15 @@ var ( ErrSnapshotBaseMissing = errors.New("snapshot base SQLite file missing") ) +// stats captures stats for the Store. +var stats *expvar.Map + +// ResetStats resets the expvar stats for this module. Mostly for test purposes. +func ResetStats() { + stats.Init() + stats.Add(persistSize, 0) +} + // Meta represents the metadata for a snapshot. type Meta struct { raft.SnapshotMeta