You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.3 KiB
Go
61 lines
1.3 KiB
Go
1 year ago
|
package store
|
||
|
|
||
|
import (
|
||
|
"expvar"
|
||
9 months ago
|
"io"
|
||
1 year ago
|
"log"
|
||
|
"time"
|
||
|
|
||
|
"github.com/hashicorp/raft"
|
||
|
)
|
||
|
|
||
9 months ago
|
// FSM is a wrapper around the Store which implements raft.FSM.
|
||
|
type FSM struct {
|
||
|
s *Store
|
||
|
}
|
||
|
|
||
|
// NewFSM returns a new FSM.
|
||
|
func NewFSM(s *Store) *FSM {
|
||
|
return &FSM{s: s}
|
||
|
}
|
||
|
|
||
9 months ago
|
// Apply applies a Raft log entry to the Store.
|
||
9 months ago
|
func (f *FSM) Apply(l *raft.Log) interface{} {
|
||
|
return f.s.fsmApply(l)
|
||
|
}
|
||
|
|
||
|
// Snapshot returns a Snapshot of the Store
|
||
|
func (f *FSM) Snapshot() (raft.FSMSnapshot, error) {
|
||
|
return f.s.fsmSnapshot()
|
||
|
}
|
||
|
|
||
|
// Restore restores the Store from a snapshot.
|
||
|
func (f *FSM) Restore(rc io.ReadCloser) error {
|
||
|
return f.s.fsmRestore(rc)
|
||
|
}
|
||
|
|
||
1 year ago
|
// FSMSnapshot is a wrapper around raft.FSMSnapshot which adds instrumentation and
|
||
|
// logging.
|
||
|
type FSMSnapshot struct {
|
||
|
raft.FSMSnapshot
|
||
|
logger *log.Logger
|
||
|
}
|
||
|
|
||
|
// Persist writes the snapshot to the given sink.
|
||
|
func (f *FSMSnapshot) Persist(sink raft.SnapshotSink) (retError error) {
|
||
|
startT := time.Now()
|
||
|
defer func() {
|
||
|
if retError == nil {
|
||
|
dur := time.Since(startT)
|
||
|
stats.Get(snapshotPersistDuration).(*expvar.Int).Set(dur.Milliseconds())
|
||
|
f.logger.Printf("persisted snapshot %s in %s", sink.ID(), time.Since(startT))
|
||
|
}
|
||
|
}()
|
||
|
return f.FSMSnapshot.Persist(sink)
|
||
|
}
|
||
|
|
||
|
// Release is a no-op.
|
||
|
func (f *FSMSnapshot) Release() {
|
||
|
f.FSMSnapshot.Release()
|
||
|
}
|