2017-11-30 00:07:18 +00:00
|
|
|
package fsm
|
|
|
|
|
|
|
|
import (
|
2017-11-30 01:33:57 +00:00
|
|
|
"fmt"
|
2017-11-30 00:07:18 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/armon/go-metrics"
|
|
|
|
"github.com/hashicorp/consul/agent/consul/state"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
"github.com/hashicorp/go-msgpack/codec"
|
|
|
|
"github.com/hashicorp/raft"
|
|
|
|
)
|
|
|
|
|
|
|
|
// snapshot is used to provide a snapshot of the current
|
|
|
|
// state in a way that can be accessed concurrently with operations
|
|
|
|
// that may modify the live state.
|
|
|
|
type snapshot struct {
|
|
|
|
state *state.Snapshot
|
|
|
|
}
|
|
|
|
|
|
|
|
// snapshotHeader is the first entry in our snapshot
|
|
|
|
type snapshotHeader struct {
|
|
|
|
// LastIndex is the last index that affects the data.
|
|
|
|
// This is used when we do the restore for watchers.
|
|
|
|
LastIndex uint64
|
|
|
|
}
|
|
|
|
|
2017-11-30 01:33:57 +00:00
|
|
|
// persister is a function used to help snapshot the FSM state.
|
|
|
|
type persister func(s *snapshot, sink raft.SnapshotSink, encoder *codec.Encoder) error
|
2017-11-30 00:07:18 +00:00
|
|
|
|
2017-11-30 01:33:57 +00:00
|
|
|
// persisters is a list of snapshot functions.
|
|
|
|
var persisters []persister
|
2017-11-30 00:07:18 +00:00
|
|
|
|
2017-11-30 01:33:57 +00:00
|
|
|
// registerPersister adds a new helper. This should be called at package
|
|
|
|
// init() time.
|
|
|
|
func registerPersister(fn persister) {
|
|
|
|
persisters = append(persisters, fn)
|
2017-11-30 00:07:18 +00:00
|
|
|
}
|
|
|
|
|
2017-11-30 01:33:57 +00:00
|
|
|
// restorer is a function used to load back a snapshot of the FSM state.
|
|
|
|
type restorer func(header *snapshotHeader, restore *state.Restore, decoder *codec.Decoder) error
|
2017-11-30 00:07:18 +00:00
|
|
|
|
2017-11-30 01:33:57 +00:00
|
|
|
// restorers is a map of restore functions by message type.
|
|
|
|
var restorers map[structs.MessageType]restorer
|
2017-11-30 00:07:18 +00:00
|
|
|
|
2017-11-30 01:33:57 +00:00
|
|
|
// registerRestorer adds a new helper. This should be called at package
|
|
|
|
// init() time.
|
|
|
|
func registerRestorer(msg structs.MessageType, fn restorer) {
|
|
|
|
if restorers == nil {
|
|
|
|
restorers = make(map[structs.MessageType]restorer)
|
2017-11-30 00:07:18 +00:00
|
|
|
}
|
2017-11-30 01:33:57 +00:00
|
|
|
if restorers[msg] != nil {
|
|
|
|
panic(fmt.Errorf("Message %d is already registered", msg))
|
2017-11-30 00:07:18 +00:00
|
|
|
}
|
2017-11-30 01:33:57 +00:00
|
|
|
restorers[msg] = fn
|
2017-11-30 00:07:18 +00:00
|
|
|
}
|
|
|
|
|
2017-11-30 01:33:57 +00:00
|
|
|
// Persist saves the FSM snapshot out to the given sink.
|
|
|
|
func (s *snapshot) Persist(sink raft.SnapshotSink) error {
|
|
|
|
defer metrics.MeasureSince([]string{"consul", "fsm", "persist"}, time.Now())
|
|
|
|
defer metrics.MeasureSince([]string{"fsm", "persist"}, time.Now())
|
2017-11-30 00:07:18 +00:00
|
|
|
|
2017-11-30 01:33:57 +00:00
|
|
|
// Write the header
|
|
|
|
header := snapshotHeader{
|
|
|
|
LastIndex: s.state.LastIndex(),
|
2017-11-30 00:07:18 +00:00
|
|
|
}
|
2017-11-30 01:33:57 +00:00
|
|
|
encoder := codec.NewEncoder(sink, msgpackHandle)
|
|
|
|
if err := encoder.Encode(&header); err != nil {
|
|
|
|
sink.Cancel()
|
2017-11-30 00:07:18 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-30 01:33:57 +00:00
|
|
|
// Run all the persisters to write the FSM state.
|
|
|
|
for _, fn := range persisters {
|
|
|
|
if err := fn(s, sink, encoder); err != nil {
|
|
|
|
sink.Cancel()
|
2017-11-30 00:07:18 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *snapshot) Release() {
|
|
|
|
s.state.Close()
|
|
|
|
}
|