2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
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"
|
2020-11-14 00:26:08 +00:00
|
|
|
"github.com/armon/go-metrics/prometheus"
|
2023-04-04 16:30:06 +00:00
|
|
|
"github.com/hashicorp/go-raftchunking"
|
|
|
|
"github.com/hashicorp/raft"
|
|
|
|
|
2022-02-14 17:45:45 +00:00
|
|
|
"github.com/hashicorp/consul-net-rpc/go-msgpack/codec"
|
2017-11-30 00:07:18 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/state"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2023-04-04 16:30:06 +00:00
|
|
|
raftstorage "github.com/hashicorp/consul/internal/storage/raft"
|
2017-11-30 00:07:18 +00:00
|
|
|
)
|
|
|
|
|
2023-09-13 22:12:59 +00:00
|
|
|
var cePersister, entPersister persister
|
|
|
|
|
2020-11-14 00:26:08 +00:00
|
|
|
var SnapshotSummaries = []prometheus.SummaryDefinition{
|
|
|
|
{
|
|
|
|
Name: []string{"fsm", "persist"},
|
2020-11-16 19:02:11 +00:00
|
|
|
Help: "Measures the time it takes to persist the FSM to a raft snapshot.",
|
2020-11-14 00:26:08 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-11-30 00:07:18 +00:00
|
|
|
// 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 {
|
2023-04-04 16:30:06 +00:00
|
|
|
state *state.Snapshot
|
|
|
|
chunkState *raftchunking.State
|
|
|
|
storageSnapshot *raftstorage.Snapshot
|
2017-11-30 00:07:18 +00:00
|
|
|
}
|
|
|
|
|
2020-10-09 19:57:29 +00:00
|
|
|
// SnapshotHeader is the first entry in our snapshot
|
|
|
|
type SnapshotHeader struct {
|
2017-11-30 00:07:18 +00:00
|
|
|
// 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
|
|
|
// restorer is a function used to load back a snapshot of the FSM state.
|
2020-10-09 19:57:29 +00:00
|
|
|
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{"fsm", "persist"}, time.Now())
|
2017-11-30 00:07:18 +00:00
|
|
|
|
2017-11-30 01:33:57 +00:00
|
|
|
// Write the header
|
2020-10-09 19:57:29 +00:00
|
|
|
header := SnapshotHeader{
|
2017-11-30 01:33:57 +00:00
|
|
|
LastIndex: s.state.LastIndex(),
|
2017-11-30 00:07:18 +00:00
|
|
|
}
|
2020-02-07 21:50:24 +00:00
|
|
|
encoder := codec.NewEncoder(sink, structs.MsgpackHandle)
|
2017-11-30 01:33:57 +00:00
|
|
|
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.
|
2023-09-13 22:12:59 +00:00
|
|
|
for _, fn := range []persister{
|
|
|
|
// The enterprise version MUST be executed first, otherwise the snapshot will
|
|
|
|
// not properly function during restore due to missing tenancy objects.
|
|
|
|
entPersister,
|
|
|
|
cePersister,
|
|
|
|
} {
|
|
|
|
// Check for nil, since the enterprise version may not exist in CE.
|
|
|
|
if fn == nil {
|
|
|
|
continue
|
|
|
|
}
|
2017-11-30 01:33:57 +00:00
|
|
|
if err := fn(s, sink, encoder); err != nil {
|
|
|
|
sink.Cancel()
|
2017-11-30 00:07:18 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-07-24 21:06:39 +00:00
|
|
|
|
|
|
|
// Persist chunking state
|
|
|
|
if s.chunkState == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if _, err := sink.Write([]byte{byte(structs.ChunkingStateType)}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := encoder.Encode(s.chunkState); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-30 00:07:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *snapshot) Release() {
|
|
|
|
s.state.Close()
|
|
|
|
}
|