-
Notifications
You must be signed in to change notification settings - Fork 691
/
snapshot.go
54 lines (46 loc) · 1.12 KB
/
snapshot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package snapshot
import (
"expvar"
"io"
"log"
"time"
"github.com/hashicorp/raft"
"github.com/rqlite/rqlite/v8/progress"
)
// Snapshot represents a snapshot of the database state.
type Snapshot struct {
rc io.ReadCloser
logger *log.Logger
}
// NewSnapshot creates a new snapshot.
func NewSnapshot(rc io.ReadCloser) *Snapshot {
return &Snapshot{
rc: rc,
logger: log.New(log.Writer(), "[snapshot] ", log.LstdFlags),
}
}
// Persist writes the snapshot to the given sink.
func (s *Snapshot) Persist(sink raft.SnapshotSink) error {
defer s.rc.Close()
startT := time.Now()
cw := progress.NewCountingWriter(sink)
cm := progress.StartCountingMonitor(func(n int64) {
s.logger.Printf("persisted %d bytes", n)
}, cw)
n, err := func() (int64, error) {
defer cm.StopAndWait()
return io.Copy(cw, s.rc)
}()
if err != nil {
return err
}
dur := time.Since(startT)
stats.Get(persistSize).(*expvar.Int).Set(n)
stats.Get(persistDuration).(*expvar.Int).Set(dur.Milliseconds())
return err
}
// Release releases the snapshot.
func (s *Snapshot) Release() {
// Necessary in case Persist() is never called.
s.rc.Close()
}