2017-11-29 02:01:17 +00:00
|
|
|
package fsm
|
2013-12-06 23:43:07 +00:00
|
|
|
|
|
|
|
import (
|
2013-12-11 22:04:44 +00:00
|
|
|
"fmt"
|
2013-12-06 23:43:07 +00:00
|
|
|
"io"
|
2017-01-20 14:12:10 +00:00
|
|
|
"sync"
|
2013-12-17 19:13:19 +00:00
|
|
|
"time"
|
2014-10-09 19:23:32 +00:00
|
|
|
|
pkg refactor
command/agent/* -> agent/*
command/consul/* -> agent/consul/*
command/agent/command{,_test}.go -> command/agent{,_test}.go
command/base/command.go -> command/base.go
command/base/* -> command/*
commands.go -> command/commands.go
The script which did the refactor is:
(
cd $GOPATH/src/github.com/hashicorp/consul
git mv command/agent/command.go command/agent.go
git mv command/agent/command_test.go command/agent_test.go
git mv command/agent/flag_slice_value{,_test}.go command/
git mv command/agent .
git mv command/base/command.go command/base.go
git mv command/base/config_util{,_test}.go command/
git mv commands.go command/
git mv consul agent
rmdir command/base/
gsed -i -e 's|package agent|package command|' command/agent{,_test}.go
gsed -i -e 's|package agent|package command|' command/flag_slice_value{,_test}.go
gsed -i -e 's|package base|package command|' command/base.go command/config_util{,_test}.go
gsed -i -e 's|package main|package command|' command/commands.go
gsed -i -e 's|base.Command|BaseCommand|' command/commands.go
gsed -i -e 's|agent.Command|AgentCommand|' command/commands.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/commands.go
gsed -i -e 's|base\.||' command/commands.go
gsed -i -e 's|command\.||' command/commands.go
gsed -i -e 's|command|c|' main.go
gsed -i -e 's|range Commands|range command.Commands|' main.go
gsed -i -e 's|Commands: Commands|Commands: command.Commands|' main.go
gsed -i -e 's|base\.BoolValue|BoolValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.DurationValue|DurationValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.StringValue|StringValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.UintValue|UintValue|' command/operator_autopilot_set.go
gsed -i -e 's|\bCommand\b|BaseCommand|' command/base.go
gsed -i -e 's|BaseCommand Options|Command Options|' command/base.go
gsed -i -e 's|base.Command|BaseCommand|' command/*.go
gsed -i -e 's|c\.Command|c.BaseCommand|g' command/*.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/*_test.go
gsed -i -e 's|base\.||' command/*_test.go
gsed -i -e 's|\bCommand\b|AgentCommand|' command/agent{,_test}.go
gsed -i -e 's|cmd.AgentCommand|cmd.BaseCommand|' command/agent.go
gsed -i -e 's|cli.AgentCommand = new(Command)|cli.Command = new(AgentCommand)|' command/agent_test.go
gsed -i -e 's|exec.AgentCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|exec.BaseCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|NewTestAgent|agent.NewTestAgent|' command/agent_test.go
gsed -i -e 's|= TestConfig|= agent.TestConfig|' command/agent_test.go
gsed -i -e 's|: RetryJoin|: agent.RetryJoin|' command/agent_test.go
gsed -i -e 's|\.\./\.\./|../|' command/config_util_test.go
gsed -i -e 's|\bverifyUniqueListeners|VerifyUniqueListeners|' agent/config{,_test}.go command/agent.go
gsed -i -e 's|\bserfLANKeyring\b|SerfLANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bserfWANKeyring\b|SerfWANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bNewAgent\b|agent.New|g' command/agent{,_test}.go
gsed -i -e 's|\bNewAgent|New|' agent/{acl_test,agent,testagent}.go
gsed -i -e 's|\bAgent\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bBool\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDefaultConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDevConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bMergeConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bReadConfigPaths\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bParseMetaPair\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfLANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfWANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|circonus\.agent|circonus|g' command/agent{,_test}.go
gsed -i -e 's|logger\.agent|logger|g' command/agent{,_test}.go
gsed -i -e 's|metrics\.agent|metrics|g' command/agent{,_test}.go
gsed -i -e 's|// agent.Agent|// agent|' command/agent{,_test}.go
gsed -i -e 's|a\.agent\.Config|a.Config|' command/agent{,_test}.go
gsed -i -e 's|agent\.AppendSliceValue|AppendSliceValue|' command/{configtest,validate}.go
gsed -i -e 's|consul/consul|agent/consul|' GNUmakefile
gsed -i -e 's|\.\./test|../../test|' agent/consul/server_test.go
# fix imports
f=$(grep -rl 'github.com/hashicorp/consul/command/agent' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/command/agent|github.com/hashicorp/consul/agent|' $f
goimports -w $f
f=$(grep -rl 'github.com/hashicorp/consul/consul' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/consul|github.com/hashicorp/consul/agent/consul|' $f
goimports -w $f
goimports -w command/*.go main.go
)
2017-06-09 22:28:28 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/state"
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2020-01-28 23:50:41 +00:00
|
|
|
"github.com/hashicorp/consul/logging"
|
|
|
|
"github.com/hashicorp/go-hclog"
|
2014-10-18 01:26:19 +00:00
|
|
|
"github.com/hashicorp/go-msgpack/codec"
|
2019-07-24 21:06:39 +00:00
|
|
|
"github.com/hashicorp/go-raftchunking"
|
2014-10-20 17:21:31 +00:00
|
|
|
"github.com/hashicorp/raft"
|
2013-12-06 23:43:07 +00:00
|
|
|
)
|
|
|
|
|
2017-11-29 20:43:27 +00:00
|
|
|
// command is a command method on the FSM.
|
|
|
|
type command func(buf []byte, index uint64) interface{}
|
|
|
|
|
|
|
|
// unboundCommand is a command method on the FSM, not yet bound to an FSM
|
|
|
|
// instance.
|
|
|
|
type unboundCommand func(c *FSM, buf []byte, index uint64) interface{}
|
|
|
|
|
|
|
|
// commands is a map from message type to unbound command.
|
|
|
|
var commands map[structs.MessageType]unboundCommand
|
|
|
|
|
|
|
|
// registerCommand registers a new command with the FSM, which should be done
|
|
|
|
// at package init() time.
|
|
|
|
func registerCommand(msg structs.MessageType, fn unboundCommand) {
|
|
|
|
if commands == nil {
|
|
|
|
commands = make(map[structs.MessageType]unboundCommand)
|
|
|
|
}
|
|
|
|
if commands[msg] != nil {
|
|
|
|
panic(fmt.Errorf("Message %d is already registered", msg))
|
|
|
|
}
|
|
|
|
commands[msg] = fn
|
|
|
|
}
|
|
|
|
|
2017-11-29 02:01:17 +00:00
|
|
|
// FSM implements a finite state machine that is used
|
2013-12-11 01:00:48 +00:00
|
|
|
// along with Raft to provide strong consistency. We implement
|
|
|
|
// this outside the Server to avoid exposing this outside the package.
|
2017-11-29 02:01:17 +00:00
|
|
|
type FSM struct {
|
2020-01-28 23:50:41 +00:00
|
|
|
logger hclog.Logger
|
|
|
|
path string
|
2017-01-20 14:12:10 +00:00
|
|
|
|
2017-11-29 20:43:27 +00:00
|
|
|
// apply is built off the commands global and is used to route apply
|
|
|
|
// operations to their appropriate handlers.
|
|
|
|
apply map[structs.MessageType]command
|
|
|
|
|
2017-01-20 14:12:10 +00:00
|
|
|
// stateLock is only used to protect outside callers to State() from
|
|
|
|
// racing with Restore(), which is called by Raft (it puts in a totally
|
2017-01-25 17:45:25 +00:00
|
|
|
// new state store). Everything internal here is synchronized by the
|
|
|
|
// Raft side, so doesn't need to lock this.
|
2017-01-20 14:12:10 +00:00
|
|
|
stateLock sync.RWMutex
|
2017-04-21 00:46:29 +00:00
|
|
|
state *state.Store
|
2017-01-20 14:12:10 +00:00
|
|
|
|
|
|
|
gc *state.TombstoneGC
|
2019-07-24 21:06:39 +00:00
|
|
|
|
|
|
|
chunker *raftchunking.ChunkingFSM
|
2013-12-06 23:43:07 +00:00
|
|
|
}
|
|
|
|
|
2017-11-29 02:01:17 +00:00
|
|
|
// New is used to construct a new FSM with a blank state.
|
2020-01-28 23:50:41 +00:00
|
|
|
func New(gc *state.TombstoneGC, logger hclog.Logger) (*FSM, error) {
|
|
|
|
if logger == nil {
|
|
|
|
logger = hclog.New(&hclog.LoggerOptions{})
|
|
|
|
}
|
|
|
|
|
2015-10-16 21:07:48 +00:00
|
|
|
stateNew, err := state.NewStateStore(gc)
|
2015-09-20 08:36:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-11-29 02:01:17 +00:00
|
|
|
fsm := &FSM{
|
2020-01-28 23:50:41 +00:00
|
|
|
logger: logger.Named(logging.FSM),
|
|
|
|
apply: make(map[structs.MessageType]command),
|
|
|
|
state: stateNew,
|
|
|
|
gc: gc,
|
2013-12-11 01:00:48 +00:00
|
|
|
}
|
2017-11-29 20:43:27 +00:00
|
|
|
|
|
|
|
// Build out the apply dispatch table based on the registered commands.
|
|
|
|
for msg, fn := range commands {
|
|
|
|
thisFn := fn
|
|
|
|
fsm.apply[msg] = func(buf []byte, index uint64) interface{} {
|
|
|
|
return thisFn(fsm, buf, index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-24 21:06:39 +00:00
|
|
|
fsm.chunker = raftchunking.NewChunkingFSM(fsm, nil)
|
|
|
|
|
2013-12-11 01:00:48 +00:00
|
|
|
return fsm, nil
|
|
|
|
}
|
|
|
|
|
2019-07-24 21:06:39 +00:00
|
|
|
func (c *FSM) ChunkingFSM() *raftchunking.ChunkingFSM {
|
|
|
|
return c.chunker
|
|
|
|
}
|
|
|
|
|
2015-10-13 05:21:39 +00:00
|
|
|
// State is used to return a handle to the current state
|
2017-11-29 02:01:17 +00:00
|
|
|
func (c *FSM) State() *state.Store {
|
2017-01-20 14:12:10 +00:00
|
|
|
c.stateLock.RLock()
|
|
|
|
defer c.stateLock.RUnlock()
|
2015-10-13 05:21:39 +00:00
|
|
|
return c.state
|
2015-09-20 08:36:39 +00:00
|
|
|
}
|
|
|
|
|
2017-11-29 02:01:17 +00:00
|
|
|
func (c *FSM) Apply(log *raft.Log) interface{} {
|
2014-01-04 01:15:09 +00:00
|
|
|
buf := log.Data
|
2015-05-06 02:44:21 +00:00
|
|
|
msgType := structs.MessageType(buf[0])
|
|
|
|
|
|
|
|
// Check if this message type should be ignored when unknown. This is
|
|
|
|
// used so that new commands can be added with developer control if older
|
|
|
|
// versions can safely ignore the command, or if they should crash.
|
|
|
|
ignoreUnknown := false
|
|
|
|
if msgType&structs.IgnoreUnknownTypeFlag == structs.IgnoreUnknownTypeFlag {
|
|
|
|
msgType &= ^structs.IgnoreUnknownTypeFlag
|
|
|
|
ignoreUnknown = true
|
|
|
|
}
|
|
|
|
|
2017-11-29 20:43:27 +00:00
|
|
|
// Apply based on the dispatch table, if possible.
|
2017-11-30 01:33:57 +00:00
|
|
|
if fn := c.apply[msgType]; fn != nil {
|
2017-11-29 20:43:27 +00:00
|
|
|
return fn(buf[1:], log.Index)
|
2014-12-15 23:04:21 +00:00
|
|
|
}
|
|
|
|
|
2017-11-29 20:43:27 +00:00
|
|
|
// Otherwise, see if it's safe to ignore. If not, we have to panic so
|
|
|
|
// that we crash and our state doesn't diverge.
|
|
|
|
if ignoreUnknown {
|
2020-01-28 23:50:41 +00:00
|
|
|
c.logger.Warn("ignoring unknown message type, upgrade to newer version", "type", msgType)
|
2017-11-29 20:43:27 +00:00
|
|
|
return nil
|
2017-02-24 21:08:49 +00:00
|
|
|
}
|
2017-11-29 20:43:27 +00:00
|
|
|
panic(fmt.Errorf("failed to apply request: %#v", buf))
|
2017-02-24 21:08:49 +00:00
|
|
|
}
|
|
|
|
|
2017-11-29 02:01:17 +00:00
|
|
|
func (c *FSM) Snapshot() (raft.FSMSnapshot, error) {
|
2013-12-17 19:13:19 +00:00
|
|
|
defer func(start time.Time) {
|
2020-01-28 23:50:41 +00:00
|
|
|
c.logger.Info("snapshot created", "duration", time.Since(start).String())
|
2013-12-17 19:13:19 +00:00
|
|
|
}(time.Now())
|
|
|
|
|
2019-07-24 21:06:39 +00:00
|
|
|
chunkState, err := c.chunker.CurrentState()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &snapshot{
|
|
|
|
state: c.state.Snapshot(),
|
|
|
|
chunkState: chunkState,
|
|
|
|
}, nil
|
2013-12-06 23:43:07 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 03:42:34 +00:00
|
|
|
// Restore streams in the snapshot and replaces the current state store with a
|
|
|
|
// new one based on the snapshot if all goes OK during the restore.
|
2017-11-29 02:01:17 +00:00
|
|
|
func (c *FSM) Restore(old io.ReadCloser) error {
|
2013-12-11 02:19:15 +00:00
|
|
|
defer old.Close()
|
|
|
|
|
2017-01-26 03:42:34 +00:00
|
|
|
// Create a new state store.
|
2015-10-16 21:07:48 +00:00
|
|
|
stateNew, err := state.NewStateStore(c.gc)
|
2013-12-11 02:19:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-20 14:12:10 +00:00
|
|
|
|
2015-10-20 06:06:59 +00:00
|
|
|
// Set up a new restore transaction
|
2017-01-26 03:42:34 +00:00
|
|
|
restore := stateNew.Restore()
|
2015-10-20 06:06:59 +00:00
|
|
|
defer restore.Abort()
|
|
|
|
|
2013-12-16 18:47:14 +00:00
|
|
|
// Create a decoder
|
2020-02-07 21:50:24 +00:00
|
|
|
dec := codec.NewDecoder(old, structs.MsgpackHandle)
|
2013-12-16 18:47:14 +00:00
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
// Read in the header
|
|
|
|
var header snapshotHeader
|
|
|
|
if err := dec.Decode(&header); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-12-16 18:47:14 +00:00
|
|
|
// Populate the new state
|
|
|
|
msgType := make([]byte, 1)
|
|
|
|
for {
|
|
|
|
// Read the message type
|
|
|
|
_, err := old.Read(msgType)
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode
|
2017-11-30 01:33:57 +00:00
|
|
|
msg := structs.MessageType(msgType[0])
|
2019-07-24 21:06:39 +00:00
|
|
|
switch {
|
|
|
|
case msg == structs.ChunkingStateType:
|
|
|
|
chunkState := &raftchunking.State{
|
|
|
|
ChunkMap: make(raftchunking.ChunkMap),
|
|
|
|
}
|
|
|
|
if err := dec.Decode(chunkState); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := c.chunker.RestoreState(chunkState); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case restorers[msg] != nil:
|
|
|
|
fn := restorers[msg]
|
2017-11-30 01:33:57 +00:00
|
|
|
if err := fn(&header, restore, dec); err != nil {
|
2013-12-16 18:47:14 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-07-24 21:06:39 +00:00
|
|
|
default:
|
2017-11-30 01:33:57 +00:00
|
|
|
return fmt.Errorf("Unrecognized msg type %d", msg)
|
2013-12-16 18:47:14 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-20 06:06:59 +00:00
|
|
|
restore.Commit()
|
2017-01-26 03:42:34 +00:00
|
|
|
|
|
|
|
// External code might be calling State(), so we need to synchronize
|
|
|
|
// here to make sure we swap in the new state store atomically.
|
|
|
|
c.stateLock.Lock()
|
|
|
|
stateOld := c.state
|
|
|
|
c.state = stateNew
|
|
|
|
c.stateLock.Unlock()
|
|
|
|
|
|
|
|
// Signal that the old state store has been abandoned. This is required
|
|
|
|
// because we don't operate on it any more, we just throw it away, so
|
|
|
|
// blocking queries won't see any changes and need to be woken up.
|
|
|
|
stateOld.Abandon()
|
2013-12-06 23:43:07 +00:00
|
|
|
return nil
|
|
|
|
}
|