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"
|
2013-12-17 19:13:19 +00:00
|
|
|
"log"
|
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
|
|
|
|
2015-01-05 23:13:39 +00:00
|
|
|
"github.com/armon/go-metrics"
|
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"
|
2017-04-19 23:00:11 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2014-10-18 01:26:19 +00:00
|
|
|
"github.com/hashicorp/go-msgpack/codec"
|
2014-10-20 17:21:31 +00:00
|
|
|
"github.com/hashicorp/raft"
|
2013-12-06 23:43:07 +00:00
|
|
|
)
|
|
|
|
|
2015-10-15 21:57:29 +00:00
|
|
|
// msgpackHandle is a shared handle for encoding/decoding msgpack payloads
|
2015-10-13 23:43:52 +00:00
|
|
|
var msgpackHandle = &codec.MsgpackHandle{}
|
|
|
|
|
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 {
|
2014-03-05 22:34:29 +00:00
|
|
|
logOutput io.Writer
|
|
|
|
logger *log.Logger
|
2014-10-15 21:56:12 +00:00
|
|
|
path string
|
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
|
2013-12-06 23:43:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// consulSnapshot 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 consulSnapshot struct {
|
2017-04-21 00:46:29 +00:00
|
|
|
state *state.Snapshot
|
2013-12-06 23:43:07 +00:00
|
|
|
}
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
// 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-29 02:01:17 +00:00
|
|
|
// New is used to construct a new FSM with a blank state.
|
|
|
|
func New(gc *state.TombstoneGC, logOutput io.Writer) (*FSM, error) {
|
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{
|
2014-03-05 22:34:29 +00:00
|
|
|
logOutput: logOutput,
|
|
|
|
logger: log.New(logOutput, "", log.LstdFlags),
|
2015-10-16 21:07:48 +00:00
|
|
|
state: stateNew,
|
2014-12-11 07:49:44 +00:00
|
|
|
gc: gc,
|
2013-12-11 01:00:48 +00:00
|
|
|
}
|
|
|
|
return fsm, nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
switch msgType {
|
2013-12-19 20:03:57 +00:00
|
|
|
case structs.RegisterRequestType:
|
2015-10-20 06:06:59 +00:00
|
|
|
return c.applyRegister(buf[1:], log.Index)
|
2013-12-19 20:03:57 +00:00
|
|
|
case structs.DeregisterRequestType:
|
2014-02-05 02:33:15 +00:00
|
|
|
return c.applyDeregister(buf[1:], log.Index)
|
2014-03-31 20:41:43 +00:00
|
|
|
case structs.KVSRequestType:
|
|
|
|
return c.applyKVSOperation(buf[1:], log.Index)
|
2014-05-16 02:22:31 +00:00
|
|
|
case structs.SessionRequestType:
|
|
|
|
return c.applySessionOperation(buf[1:], log.Index)
|
2014-08-05 23:55:58 +00:00
|
|
|
case structs.ACLRequestType:
|
|
|
|
return c.applyACLOperation(buf[1:], log.Index)
|
2014-12-15 23:28:56 +00:00
|
|
|
case structs.TombstoneRequestType:
|
|
|
|
return c.applyTombstoneOperation(buf[1:], log.Index)
|
2015-06-23 02:14:02 +00:00
|
|
|
case structs.CoordinateBatchUpdateType:
|
|
|
|
return c.applyCoordinateBatchUpdate(buf[1:], log.Index)
|
2015-11-10 04:37:41 +00:00
|
|
|
case structs.PreparedQueryRequestType:
|
|
|
|
return c.applyPreparedQueryOperation(buf[1:], log.Index)
|
2016-05-11 08:35:27 +00:00
|
|
|
case structs.TxnRequestType:
|
|
|
|
return c.applyTxn(buf[1:], log.Index)
|
2017-02-24 21:08:49 +00:00
|
|
|
case structs.AutopilotRequestType:
|
|
|
|
return c.applyAutopilotUpdate(buf[1:], log.Index)
|
2013-12-11 22:04:44 +00:00
|
|
|
default:
|
2015-05-06 02:44:21 +00:00
|
|
|
if ignoreUnknown {
|
|
|
|
c.logger.Printf("[WARN] consul.fsm: ignoring unknown message type (%d), upgrade to newer version", msgType)
|
|
|
|
return nil
|
|
|
|
}
|
2017-04-21 01:59:42 +00:00
|
|
|
panic(fmt.Errorf("failed to apply request: %#v", buf))
|
2013-12-11 22:04:44 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-11 02:19:15 +00:00
|
|
|
|
2017-11-29 02:01:17 +00:00
|
|
|
func (c *FSM) applyRegister(buf []byte, index uint64) interface{} {
|
2015-10-20 06:06:59 +00:00
|
|
|
defer metrics.MeasureSince([]string{"consul", "fsm", "register"}, time.Now())
|
2017-10-04 23:43:27 +00:00
|
|
|
defer metrics.MeasureSince([]string{"fsm", "register"}, time.Now())
|
2013-12-19 20:03:57 +00:00
|
|
|
var req structs.RegisterRequest
|
|
|
|
if err := structs.Decode(buf, &req); err != nil {
|
2013-12-11 22:04:44 +00:00
|
|
|
panic(fmt.Errorf("failed to decode request: %v", err))
|
|
|
|
}
|
|
|
|
|
2014-08-22 19:38:33 +00:00
|
|
|
// Apply all updates in a single transaction
|
2015-10-20 06:06:59 +00:00
|
|
|
if err := c.state.EnsureRegistration(index, &req); err != nil {
|
2017-06-08 03:01:36 +00:00
|
|
|
c.logger.Printf("[WARN] consul.fsm: EnsureRegistration failed: %v", err)
|
2014-02-03 23:21:56 +00:00
|
|
|
return err
|
|
|
|
}
|
2013-12-11 22:38:18 +00:00
|
|
|
return nil
|
2013-12-06 23:43:07 +00:00
|
|
|
}
|
|
|
|
|
2017-11-29 02:01:17 +00:00
|
|
|
func (c *FSM) applyDeregister(buf []byte, index uint64) interface{} {
|
2015-01-05 23:13:39 +00:00
|
|
|
defer metrics.MeasureSince([]string{"consul", "fsm", "deregister"}, time.Now())
|
2017-10-04 23:43:27 +00:00
|
|
|
defer metrics.MeasureSince([]string{"fsm", "deregister"}, time.Now())
|
2013-12-19 20:03:57 +00:00
|
|
|
var req structs.DeregisterRequest
|
|
|
|
if err := structs.Decode(buf, &req); err != nil {
|
2013-12-11 23:34:10 +00:00
|
|
|
panic(fmt.Errorf("failed to decode request: %v", err))
|
|
|
|
}
|
|
|
|
|
2016-12-10 03:15:44 +00:00
|
|
|
// Either remove the service entry or the whole node. The precedence
|
|
|
|
// here is also baked into vetDeregisterWithACL() in acl.go, so if you
|
|
|
|
// make changes here, be sure to also adjust the code over there.
|
2014-01-06 22:18:38 +00:00
|
|
|
if req.ServiceID != "" {
|
2015-10-13 05:21:39 +00:00
|
|
|
if err := c.state.DeleteService(index, req.Node, req.ServiceID); err != nil {
|
2017-06-08 03:01:36 +00:00
|
|
|
c.logger.Printf("[WARN] consul.fsm: DeleteNodeService failed: %v", err)
|
2014-02-03 23:21:56 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-01-08 21:39:40 +00:00
|
|
|
} else if req.CheckID != "" {
|
2015-10-13 05:21:39 +00:00
|
|
|
if err := c.state.DeleteCheck(index, req.Node, req.CheckID); err != nil {
|
2017-06-08 03:01:36 +00:00
|
|
|
c.logger.Printf("[WARN] consul.fsm: DeleteNodeCheck failed: %v", err)
|
2014-02-03 23:21:56 +00:00
|
|
|
return err
|
|
|
|
}
|
2013-12-11 23:34:10 +00:00
|
|
|
} else {
|
2015-10-13 05:21:39 +00:00
|
|
|
if err := c.state.DeleteNode(index, req.Node); err != nil {
|
2017-06-08 03:01:36 +00:00
|
|
|
c.logger.Printf("[WARN] consul.fsm: DeleteNode failed: %v", err)
|
2014-02-03 23:21:56 +00:00
|
|
|
return err
|
|
|
|
}
|
2013-12-11 23:34:10 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-29 02:01:17 +00:00
|
|
|
func (c *FSM) applyKVSOperation(buf []byte, index uint64) interface{} {
|
2014-03-31 20:41:43 +00:00
|
|
|
var req structs.KVSRequest
|
|
|
|
if err := structs.Decode(buf, &req); err != nil {
|
|
|
|
panic(fmt.Errorf("failed to decode request: %v", err))
|
|
|
|
}
|
2017-08-08 08:31:38 +00:00
|
|
|
defer metrics.MeasureSinceWithLabels([]string{"consul", "fsm", "kvs"}, time.Now(),
|
|
|
|
[]metrics.Label{{Name: "op", Value: string(req.Op)}})
|
2017-10-04 23:43:27 +00:00
|
|
|
defer metrics.MeasureSinceWithLabels([]string{"fsm", "kvs"}, time.Now(),
|
|
|
|
[]metrics.Label{{Name: "op", Value: string(req.Op)}})
|
2014-03-31 20:41:43 +00:00
|
|
|
switch req.Op {
|
2017-04-19 23:00:11 +00:00
|
|
|
case api.KVSet:
|
2015-10-13 05:21:39 +00:00
|
|
|
return c.state.KVSSet(index, &req.DirEnt)
|
2017-04-19 23:00:11 +00:00
|
|
|
case api.KVDelete:
|
2015-10-13 05:21:39 +00:00
|
|
|
return c.state.KVSDelete(index, req.DirEnt.Key)
|
2017-04-19 23:00:11 +00:00
|
|
|
case api.KVDeleteCAS:
|
2015-10-13 05:21:39 +00:00
|
|
|
act, err := c.state.KVSDeleteCAS(index, req.DirEnt.ModifyIndex, req.DirEnt.Key)
|
2015-01-09 01:08:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-21 01:59:42 +00:00
|
|
|
return act
|
2017-04-19 23:00:11 +00:00
|
|
|
case api.KVDeleteTree:
|
2015-10-13 05:21:39 +00:00
|
|
|
return c.state.KVSDeleteTree(index, req.DirEnt.Key)
|
2017-04-19 23:00:11 +00:00
|
|
|
case api.KVCAS:
|
2015-10-13 05:21:39 +00:00
|
|
|
act, err := c.state.KVSSetCAS(index, &req.DirEnt)
|
2014-03-31 20:41:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-21 01:59:42 +00:00
|
|
|
return act
|
2017-04-19 23:00:11 +00:00
|
|
|
case api.KVLock:
|
2015-10-13 05:21:39 +00:00
|
|
|
act, err := c.state.KVSLock(index, &req.DirEnt)
|
2014-05-16 02:22:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-21 01:59:42 +00:00
|
|
|
return act
|
2017-04-19 23:00:11 +00:00
|
|
|
case api.KVUnlock:
|
2015-10-13 05:21:39 +00:00
|
|
|
act, err := c.state.KVSUnlock(index, &req.DirEnt)
|
2014-05-16 02:22:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-21 01:59:42 +00:00
|
|
|
return act
|
2014-03-31 20:41:43 +00:00
|
|
|
default:
|
2017-04-20 18:49:34 +00:00
|
|
|
err := fmt.Errorf("Invalid KVS operation '%s'", req.Op)
|
2014-12-06 12:08:35 +00:00
|
|
|
c.logger.Printf("[WARN] consul.fsm: %v", err)
|
|
|
|
return err
|
2014-03-31 20:41:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-29 02:01:17 +00:00
|
|
|
func (c *FSM) applySessionOperation(buf []byte, index uint64) interface{} {
|
2014-05-16 02:22:31 +00:00
|
|
|
var req structs.SessionRequest
|
|
|
|
if err := structs.Decode(buf, &req); err != nil {
|
|
|
|
panic(fmt.Errorf("failed to decode request: %v", err))
|
|
|
|
}
|
2017-08-08 08:31:38 +00:00
|
|
|
defer metrics.MeasureSinceWithLabels([]string{"consul", "fsm", "session"}, time.Now(),
|
|
|
|
[]metrics.Label{{Name: "op", Value: string(req.Op)}})
|
2017-10-04 23:43:27 +00:00
|
|
|
defer metrics.MeasureSinceWithLabels([]string{"fsm", "session"}, time.Now(),
|
|
|
|
[]metrics.Label{{Name: "op", Value: string(req.Op)}})
|
2014-05-16 02:22:31 +00:00
|
|
|
switch req.Op {
|
|
|
|
case structs.SessionCreate:
|
2015-10-13 05:21:39 +00:00
|
|
|
if err := c.state.SessionCreate(index, &req.Session); err != nil {
|
2014-05-16 02:37:58 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-04-21 01:59:42 +00:00
|
|
|
return req.Session.ID
|
2014-05-16 02:22:31 +00:00
|
|
|
case structs.SessionDestroy:
|
2015-10-13 05:21:39 +00:00
|
|
|
return c.state.SessionDestroy(index, req.Session.ID)
|
2014-05-16 02:22:31 +00:00
|
|
|
default:
|
|
|
|
c.logger.Printf("[WARN] consul.fsm: Invalid Session operation '%s'", req.Op)
|
|
|
|
return fmt.Errorf("Invalid Session operation '%s'", req.Op)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-29 02:01:17 +00:00
|
|
|
func (c *FSM) applyACLOperation(buf []byte, index uint64) interface{} {
|
2014-08-05 23:55:58 +00:00
|
|
|
var req structs.ACLRequest
|
|
|
|
if err := structs.Decode(buf, &req); err != nil {
|
|
|
|
panic(fmt.Errorf("failed to decode request: %v", err))
|
|
|
|
}
|
2017-08-08 08:31:38 +00:00
|
|
|
defer metrics.MeasureSinceWithLabels([]string{"consul", "fsm", "acl"}, time.Now(),
|
|
|
|
[]metrics.Label{{Name: "op", Value: string(req.Op)}})
|
2017-10-04 23:43:27 +00:00
|
|
|
defer metrics.MeasureSinceWithLabels([]string{"fsm", "acl"}, time.Now(),
|
|
|
|
[]metrics.Label{{Name: "op", Value: string(req.Op)}})
|
2014-08-05 23:55:58 +00:00
|
|
|
switch req.Op {
|
2017-08-03 00:05:18 +00:00
|
|
|
case structs.ACLBootstrapInit:
|
|
|
|
enabled, err := c.state.ACLBootstrapInit(index)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return enabled
|
|
|
|
case structs.ACLBootstrapNow:
|
|
|
|
if err := c.state.ACLBootstrap(index, &req.ACL); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return &req.ACL
|
2015-05-27 01:30:14 +00:00
|
|
|
case structs.ACLForceSet, structs.ACLSet:
|
2015-10-13 05:21:39 +00:00
|
|
|
if err := c.state.ACLSet(index, &req.ACL); err != nil {
|
2014-08-05 23:55:58 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-04-21 01:59:42 +00:00
|
|
|
return req.ACL.ID
|
2014-08-05 23:55:58 +00:00
|
|
|
case structs.ACLDelete:
|
2015-10-13 05:21:39 +00:00
|
|
|
return c.state.ACLDelete(index, req.ACL.ID)
|
2014-08-05 23:55:58 +00:00
|
|
|
default:
|
|
|
|
c.logger.Printf("[WARN] consul.fsm: Invalid ACL operation '%s'", req.Op)
|
|
|
|
return fmt.Errorf("Invalid ACL operation '%s'", req.Op)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-29 02:01:17 +00:00
|
|
|
func (c *FSM) applyTombstoneOperation(buf []byte, index uint64) interface{} {
|
2014-12-15 23:28:56 +00:00
|
|
|
var req structs.TombstoneRequest
|
2014-12-15 23:04:21 +00:00
|
|
|
if err := structs.Decode(buf, &req); err != nil {
|
|
|
|
panic(fmt.Errorf("failed to decode request: %v", err))
|
|
|
|
}
|
2017-08-08 08:31:38 +00:00
|
|
|
defer metrics.MeasureSinceWithLabels([]string{"consul", "fsm", "tombstone"}, time.Now(),
|
|
|
|
[]metrics.Label{{Name: "op", Value: string(req.Op)}})
|
2017-10-04 23:43:27 +00:00
|
|
|
defer metrics.MeasureSinceWithLabels([]string{"fsm", "tombstone"}, time.Now(),
|
|
|
|
[]metrics.Label{{Name: "op", Value: string(req.Op)}})
|
2014-12-15 23:28:56 +00:00
|
|
|
switch req.Op {
|
|
|
|
case structs.TombstoneReap:
|
2015-10-13 05:21:39 +00:00
|
|
|
return c.state.ReapTombstones(req.ReapIndex)
|
2014-12-15 23:28:56 +00:00
|
|
|
default:
|
|
|
|
c.logger.Printf("[WARN] consul.fsm: Invalid Tombstone operation '%s'", req.Op)
|
|
|
|
return fmt.Errorf("Invalid Tombstone operation '%s'", req.Op)
|
|
|
|
}
|
2014-12-15 23:04:21 +00:00
|
|
|
}
|
|
|
|
|
2015-06-23 02:14:02 +00:00
|
|
|
// applyCoordinateBatchUpdate processes a batch of coordinate updates and applies
|
|
|
|
// them in a single underlying transaction. This interface isn't 1:1 with the outer
|
|
|
|
// update interface that the coordinate endpoint exposes, so we made it single
|
|
|
|
// purpose and avoided the opcode convention.
|
2017-11-29 02:01:17 +00:00
|
|
|
func (c *FSM) applyCoordinateBatchUpdate(buf []byte, index uint64) interface{} {
|
2015-10-23 22:19:14 +00:00
|
|
|
var updates structs.Coordinates
|
2015-06-23 02:14:02 +00:00
|
|
|
if err := structs.Decode(buf, &updates); err != nil {
|
|
|
|
panic(fmt.Errorf("failed to decode batch updates: %v", err))
|
2015-04-09 20:23:14 +00:00
|
|
|
}
|
2015-06-23 02:14:02 +00:00
|
|
|
defer metrics.MeasureSince([]string{"consul", "fsm", "coordinate", "batch-update"}, time.Now())
|
2017-10-04 23:43:27 +00:00
|
|
|
defer metrics.MeasureSince([]string{"fsm", "coordinate", "batch-update"}, time.Now())
|
2015-06-23 02:14:02 +00:00
|
|
|
if err := c.state.CoordinateBatchUpdate(index, updates); err != nil {
|
|
|
|
return err
|
2015-04-09 20:23:14 +00:00
|
|
|
}
|
2015-05-08 08:31:34 +00:00
|
|
|
return nil
|
2015-04-09 20:23:14 +00:00
|
|
|
}
|
|
|
|
|
2015-11-10 04:37:41 +00:00
|
|
|
// applyPreparedQueryOperation applies the given prepared query operation to the
|
|
|
|
// state store.
|
2017-11-29 02:01:17 +00:00
|
|
|
func (c *FSM) applyPreparedQueryOperation(buf []byte, index uint64) interface{} {
|
2015-11-10 04:37:41 +00:00
|
|
|
var req structs.PreparedQueryRequest
|
2015-11-07 06:18:11 +00:00
|
|
|
if err := structs.Decode(buf, &req); err != nil {
|
|
|
|
panic(fmt.Errorf("failed to decode request: %v", err))
|
|
|
|
}
|
2015-11-12 05:22:51 +00:00
|
|
|
|
2017-08-08 08:31:38 +00:00
|
|
|
defer metrics.MeasureSinceWithLabels([]string{"consul", "fsm", "prepared-query"}, time.Now(),
|
|
|
|
[]metrics.Label{{Name: "op", Value: string(req.Op)}})
|
2017-10-04 23:43:27 +00:00
|
|
|
defer metrics.MeasureSinceWithLabels([]string{"fsm", "prepared-query"}, time.Now(),
|
|
|
|
[]metrics.Label{{Name: "op", Value: string(req.Op)}})
|
2015-11-07 06:18:11 +00:00
|
|
|
switch req.Op {
|
2015-11-10 04:37:41 +00:00
|
|
|
case structs.PreparedQueryCreate, structs.PreparedQueryUpdate:
|
2015-11-10 18:29:55 +00:00
|
|
|
return c.state.PreparedQuerySet(index, req.Query)
|
2015-11-10 04:37:41 +00:00
|
|
|
case structs.PreparedQueryDelete:
|
|
|
|
return c.state.PreparedQueryDelete(index, req.Query.ID)
|
2015-11-07 06:18:11 +00:00
|
|
|
default:
|
2015-11-10 04:37:41 +00:00
|
|
|
c.logger.Printf("[WARN] consul.fsm: Invalid PreparedQuery operation '%s'", req.Op)
|
|
|
|
return fmt.Errorf("Invalid PreparedQuery operation '%s'", req.Op)
|
2015-11-07 06:18:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-29 02:01:17 +00:00
|
|
|
func (c *FSM) applyTxn(buf []byte, index uint64) interface{} {
|
2016-05-11 08:35:27 +00:00
|
|
|
var req structs.TxnRequest
|
|
|
|
if err := structs.Decode(buf, &req); err != nil {
|
|
|
|
panic(fmt.Errorf("failed to decode request: %v", err))
|
|
|
|
}
|
|
|
|
defer metrics.MeasureSince([]string{"consul", "fsm", "txn"}, time.Now())
|
2017-10-04 23:43:27 +00:00
|
|
|
defer metrics.MeasureSince([]string{"fsm", "txn"}, time.Now())
|
2016-05-13 00:38:25 +00:00
|
|
|
results, errors := c.state.TxnRW(index, req.Ops)
|
2017-03-23 23:05:35 +00:00
|
|
|
return structs.TxnResponse{
|
|
|
|
Results: results,
|
|
|
|
Errors: errors,
|
|
|
|
}
|
2016-05-11 08:35:27 +00:00
|
|
|
}
|
|
|
|
|
2017-11-29 02:01:17 +00:00
|
|
|
func (c *FSM) applyAutopilotUpdate(buf []byte, index uint64) interface{} {
|
2017-02-24 21:08:49 +00:00
|
|
|
var req structs.AutopilotSetConfigRequest
|
|
|
|
if err := structs.Decode(buf, &req); err != nil {
|
|
|
|
panic(fmt.Errorf("failed to decode request: %v", err))
|
|
|
|
}
|
|
|
|
defer metrics.MeasureSince([]string{"consul", "fsm", "autopilot"}, time.Now())
|
2017-10-04 23:43:27 +00:00
|
|
|
defer metrics.MeasureSince([]string{"fsm", "autopilot"}, time.Now())
|
2017-02-24 21:08:49 +00:00
|
|
|
|
|
|
|
if req.CAS {
|
|
|
|
act, err := c.state.AutopilotCASConfig(index, req.Config.ModifyIndex, &req.Config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-21 01:59:42 +00:00
|
|
|
return act
|
2017-02-24 21:08:49 +00:00
|
|
|
}
|
2017-04-21 01:59:42 +00:00
|
|
|
return c.state.AutopilotSetConfig(index, &req.Config)
|
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) {
|
2017-10-17 18:38:24 +00:00
|
|
|
c.logger.Printf("[INFO] consul.fsm: snapshot created in %v", time.Since(start))
|
2013-12-17 19:13:19 +00:00
|
|
|
}(time.Now())
|
|
|
|
|
2015-10-13 05:21:39 +00:00
|
|
|
return &consulSnapshot{c.state.Snapshot()}, 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
|
2014-06-08 21:02:42 +00:00
|
|
|
dec := codec.NewDecoder(old, 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
|
2013-12-19 20:03:57 +00:00
|
|
|
switch structs.MessageType(msgType[0]) {
|
|
|
|
case structs.RegisterRequestType:
|
|
|
|
var req structs.RegisterRequest
|
2013-12-16 18:47:14 +00:00
|
|
|
if err := dec.Decode(&req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-20 06:06:59 +00:00
|
|
|
if err := restore.Registration(header.LastIndex, &req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-12-16 18:47:14 +00:00
|
|
|
|
2014-03-31 20:56:16 +00:00
|
|
|
case structs.KVSRequestType:
|
|
|
|
var req structs.DirEntry
|
|
|
|
if err := dec.Decode(&req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-20 06:06:59 +00:00
|
|
|
if err := restore.KVS(&req); err != nil {
|
2014-03-31 20:56:16 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-20 06:06:59 +00:00
|
|
|
case structs.TombstoneRequestType:
|
|
|
|
var req structs.DirEntry
|
2014-05-16 02:22:31 +00:00
|
|
|
if err := dec.Decode(&req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-20 06:06:59 +00:00
|
|
|
|
|
|
|
// For historical reasons, these are serialized in the
|
|
|
|
// snapshots as KV entries. We want to keep the snapshot
|
|
|
|
// format compatible with pre-0.6 versions for now.
|
|
|
|
stone := &state.Tombstone{
|
|
|
|
Key: req.Key,
|
|
|
|
Index: req.ModifyIndex,
|
|
|
|
}
|
|
|
|
if err := restore.Tombstone(stone); err != nil {
|
2014-05-16 02:22:31 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-20 06:06:59 +00:00
|
|
|
case structs.SessionRequestType:
|
|
|
|
var req structs.Session
|
2014-08-05 23:55:58 +00:00
|
|
|
if err := dec.Decode(&req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-20 06:06:59 +00:00
|
|
|
if err := restore.Session(&req); err != nil {
|
2014-08-05 23:55:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-20 06:06:59 +00:00
|
|
|
case structs.ACLRequestType:
|
|
|
|
var req structs.ACL
|
2014-12-15 23:31:13 +00:00
|
|
|
if err := dec.Decode(&req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-20 06:06:59 +00:00
|
|
|
if err := restore.ACL(&req); err != nil {
|
2014-12-15 23:31:13 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-03 00:05:18 +00:00
|
|
|
case structs.ACLBootstrapRequestType:
|
|
|
|
var req structs.ACLBootstrap
|
|
|
|
if err := dec.Decode(&req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := restore.ACLBootstrap(&req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-30 00:04:47 +00:00
|
|
|
case structs.CoordinateBatchUpdateType:
|
2015-10-23 22:19:14 +00:00
|
|
|
var req structs.Coordinates
|
2015-06-30 00:04:47 +00:00
|
|
|
if err := dec.Decode(&req); err != nil {
|
|
|
|
return err
|
2015-10-23 22:19:14 +00:00
|
|
|
|
2015-06-30 00:04:47 +00:00
|
|
|
}
|
2015-10-23 22:19:14 +00:00
|
|
|
if err := restore.Coordinates(header.LastIndex, req); err != nil {
|
2015-06-30 00:04:47 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-12 05:22:51 +00:00
|
|
|
case structs.PreparedQueryRequestType:
|
|
|
|
var req structs.PreparedQuery
|
|
|
|
if err := dec.Decode(&req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := restore.PreparedQuery(&req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-21 23:36:44 +00:00
|
|
|
case structs.AutopilotRequestType:
|
|
|
|
var req structs.AutopilotConfig
|
|
|
|
if err := dec.Decode(&req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := restore.Autopilot(&req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-12-16 18:47:14 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("Unrecognized msg type: %v", msgType)
|
|
|
|
}
|
|
|
|
}
|
2013-12-11 02:19:15 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *consulSnapshot) Persist(sink raft.SnapshotSink) error {
|
2015-01-05 23:13:39 +00:00
|
|
|
defer metrics.MeasureSince([]string{"consul", "fsm", "persist"}, time.Now())
|
2017-10-04 23:43:27 +00:00
|
|
|
defer metrics.MeasureSince([]string{"fsm", "persist"}, time.Now())
|
2015-10-13 03:56:31 +00:00
|
|
|
|
2013-12-16 18:47:14 +00:00
|
|
|
// Register the nodes
|
2014-06-08 21:02:42 +00:00
|
|
|
encoder := codec.NewEncoder(sink, msgpackHandle)
|
2013-12-16 18:47:14 +00:00
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
// Write the header
|
|
|
|
header := snapshotHeader{
|
2015-10-13 05:21:39 +00:00
|
|
|
LastIndex: s.state.LastIndex(),
|
2014-02-05 02:33:15 +00:00
|
|
|
}
|
|
|
|
if err := encoder.Encode(&header); err != nil {
|
|
|
|
sink.Cancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-05-16 02:22:31 +00:00
|
|
|
if err := s.persistNodes(sink, encoder); err != nil {
|
|
|
|
sink.Cancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.persistSessions(sink, encoder); err != nil {
|
|
|
|
sink.Cancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-05 23:55:58 +00:00
|
|
|
if err := s.persistACLs(sink, encoder); err != nil {
|
|
|
|
sink.Cancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-19 21:07:57 +00:00
|
|
|
if err := s.persistKVs(sink, encoder); err != nil {
|
2014-05-16 02:22:31 +00:00
|
|
|
sink.Cancel()
|
|
|
|
return err
|
|
|
|
}
|
2014-12-15 23:31:13 +00:00
|
|
|
|
|
|
|
if err := s.persistTombstones(sink, encoder); err != nil {
|
|
|
|
sink.Cancel()
|
|
|
|
return err
|
|
|
|
}
|
2015-11-12 05:22:51 +00:00
|
|
|
|
|
|
|
if err := s.persistPreparedQueries(sink, encoder); err != nil {
|
|
|
|
sink.Cancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-21 23:36:44 +00:00
|
|
|
if err := s.persistAutopilot(sink, encoder); err != nil {
|
|
|
|
sink.Cancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-05-16 02:22:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *consulSnapshot) persistNodes(sink raft.SnapshotSink,
|
|
|
|
encoder *codec.Encoder) error {
|
2015-09-25 19:01:46 +00:00
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
// Get all the nodes
|
2015-10-19 20:55:35 +00:00
|
|
|
nodes, err := s.state.Nodes()
|
2015-09-25 19:01:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-02-05 02:33:15 +00:00
|
|
|
|
2013-12-16 18:47:14 +00:00
|
|
|
// Register each node
|
2015-10-19 22:51:11 +00:00
|
|
|
for node := nodes.Next(); node != nil; node = nodes.Next() {
|
|
|
|
n := node.(*structs.Node)
|
2015-10-19 20:55:35 +00:00
|
|
|
req := structs.RegisterRequest{
|
2016-02-07 18:37:34 +00:00
|
|
|
Node: n.Node,
|
|
|
|
Address: n.Address,
|
|
|
|
TaggedAddresses: n.TaggedAddresses,
|
2013-12-16 18:47:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Register the node itself
|
2017-11-29 01:26:16 +00:00
|
|
|
if _, err := sink.Write([]byte{byte(structs.RegisterRequestType)}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-12-16 18:47:14 +00:00
|
|
|
if err := encoder.Encode(&req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register each service this node has
|
2015-10-19 22:51:11 +00:00
|
|
|
services, err := s.state.Services(n.Node)
|
2015-09-25 19:01:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-19 22:51:11 +00:00
|
|
|
for service := services.Next(); service != nil; service = services.Next() {
|
2017-11-29 01:26:16 +00:00
|
|
|
if _, err := sink.Write([]byte{byte(structs.RegisterRequestType)}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-19 22:51:11 +00:00
|
|
|
req.Service = service.(*structs.ServiceNode).ToNodeService()
|
2014-01-08 21:39:40 +00:00
|
|
|
if err := encoder.Encode(&req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2013-12-16 18:47:14 +00:00
|
|
|
|
2014-01-08 21:39:40 +00:00
|
|
|
// Register each check this node has
|
|
|
|
req.Service = nil
|
2015-10-19 22:51:11 +00:00
|
|
|
checks, err := s.state.Checks(n.Node)
|
2015-09-25 19:01:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-19 22:51:11 +00:00
|
|
|
for check := checks.Next(); check != nil; check = checks.Next() {
|
2017-11-29 01:26:16 +00:00
|
|
|
if _, err := sink.Write([]byte{byte(structs.RegisterRequestType)}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-19 22:51:11 +00:00
|
|
|
req.Check = check.(*structs.HealthCheck)
|
2013-12-16 18:47:14 +00:00
|
|
|
if err := encoder.Encode(&req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-30 00:04:47 +00:00
|
|
|
|
2015-10-23 22:19:14 +00:00
|
|
|
// Save the coordinates separately since they are not part of the
|
|
|
|
// register request interface. To avoid copying them out, we turn
|
|
|
|
// them into batches with a single coordinate each.
|
|
|
|
coords, err := s.state.Coordinates()
|
|
|
|
if err != nil {
|
2015-06-30 00:04:47 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-10-23 22:19:14 +00:00
|
|
|
for coord := coords.Next(); coord != nil; coord = coords.Next() {
|
2017-11-29 01:26:16 +00:00
|
|
|
if _, err := sink.Write([]byte{byte(structs.CoordinateBatchUpdateType)}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-23 22:19:14 +00:00
|
|
|
updates := structs.Coordinates{coord.(*structs.Coordinate)}
|
|
|
|
if err := encoder.Encode(&updates); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2014-05-16 02:22:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *consulSnapshot) persistSessions(sink raft.SnapshotSink,
|
|
|
|
encoder *codec.Encoder) error {
|
2015-10-19 22:51:11 +00:00
|
|
|
sessions, err := s.state.Sessions()
|
2014-05-16 02:22:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-03-31 20:56:16 +00:00
|
|
|
|
2015-10-19 22:51:11 +00:00
|
|
|
for session := sessions.Next(); session != nil; session = sessions.Next() {
|
2017-11-29 01:26:16 +00:00
|
|
|
if _, err := sink.Write([]byte{byte(structs.SessionRequestType)}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-19 22:51:11 +00:00
|
|
|
if err := encoder.Encode(session.(*structs.Session)); err != nil {
|
2014-05-16 02:22:31 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2014-03-31 20:56:16 +00:00
|
|
|
|
2014-08-05 23:55:58 +00:00
|
|
|
func (s *consulSnapshot) persistACLs(sink raft.SnapshotSink,
|
|
|
|
encoder *codec.Encoder) error {
|
2015-10-19 22:51:11 +00:00
|
|
|
acls, err := s.state.ACLs()
|
2014-08-05 23:55:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-19 22:51:11 +00:00
|
|
|
for acl := acls.Next(); acl != nil; acl = acls.Next() {
|
2017-11-29 01:26:16 +00:00
|
|
|
if _, err := sink.Write([]byte{byte(structs.ACLRequestType)}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-19 22:51:11 +00:00
|
|
|
if err := encoder.Encode(acl.(*structs.ACL)); err != nil {
|
2014-08-05 23:55:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-08-03 00:05:18 +00:00
|
|
|
|
|
|
|
bs, err := s.state.ACLBootstrap()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if bs != nil {
|
2017-11-29 01:26:16 +00:00
|
|
|
if _, err := sink.Write([]byte{byte(structs.ACLBootstrapRequestType)}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-03 00:05:18 +00:00
|
|
|
if err := encoder.Encode(bs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-05 23:55:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-19 21:07:57 +00:00
|
|
|
func (s *consulSnapshot) persistKVs(sink raft.SnapshotSink,
|
2014-05-16 02:22:31 +00:00
|
|
|
encoder *codec.Encoder) error {
|
2015-10-19 22:51:11 +00:00
|
|
|
entries, err := s.state.KVs()
|
2015-09-25 19:01:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-04-01 18:55:25 +00:00
|
|
|
|
2015-10-19 22:51:11 +00:00
|
|
|
for entry := entries.Next(); entry != nil; entry = entries.Next() {
|
2017-11-29 01:26:16 +00:00
|
|
|
if _, err := sink.Write([]byte{byte(structs.KVSRequestType)}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-19 22:51:11 +00:00
|
|
|
if err := encoder.Encode(entry.(*structs.DirEntry)); err != nil {
|
2014-03-31 20:56:16 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-09-25 19:01:46 +00:00
|
|
|
return nil
|
2013-12-06 23:43:07 +00:00
|
|
|
}
|
|
|
|
|
2014-12-15 23:31:13 +00:00
|
|
|
func (s *consulSnapshot) persistTombstones(sink raft.SnapshotSink,
|
|
|
|
encoder *codec.Encoder) error {
|
2015-10-19 22:51:11 +00:00
|
|
|
stones, err := s.state.Tombstones()
|
2015-09-25 19:01:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-15 23:31:13 +00:00
|
|
|
|
2015-10-19 22:51:11 +00:00
|
|
|
for stone := stones.Next(); stone != nil; stone = stones.Next() {
|
2017-11-29 01:26:16 +00:00
|
|
|
if _, err := sink.Write([]byte{byte(structs.TombstoneRequestType)}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-15 23:31:13 +00:00
|
|
|
|
2015-09-25 19:01:46 +00:00
|
|
|
// For historical reasons, these are serialized in the snapshots
|
|
|
|
// as KV entries. We want to keep the snapshot format compatible
|
|
|
|
// with pre-0.6 versions for now.
|
2015-10-19 22:51:11 +00:00
|
|
|
s := stone.(*state.Tombstone)
|
2015-09-25 19:01:46 +00:00
|
|
|
fake := &structs.DirEntry{
|
2015-10-19 22:51:11 +00:00
|
|
|
Key: s.Key,
|
2015-09-25 19:01:46 +00:00
|
|
|
RaftIndex: structs.RaftIndex{
|
2015-10-19 22:51:11 +00:00
|
|
|
ModifyIndex: s.Index,
|
2015-09-25 19:01:46 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := encoder.Encode(fake); err != nil {
|
2014-12-15 23:31:13 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-09-25 19:01:46 +00:00
|
|
|
return nil
|
2014-12-15 23:31:13 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 05:22:51 +00:00
|
|
|
func (s *consulSnapshot) persistPreparedQueries(sink raft.SnapshotSink,
|
|
|
|
encoder *codec.Encoder) error {
|
|
|
|
queries, err := s.state.PreparedQueries()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-26 08:25:44 +00:00
|
|
|
for _, query := range queries {
|
2017-11-29 01:26:16 +00:00
|
|
|
if _, err := sink.Write([]byte{byte(structs.PreparedQueryRequestType)}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-26 08:25:44 +00:00
|
|
|
if err := encoder.Encode(query); err != nil {
|
2015-11-12 05:22:51 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-21 23:36:44 +00:00
|
|
|
func (s *consulSnapshot) persistAutopilot(sink raft.SnapshotSink,
|
|
|
|
encoder *codec.Encoder) error {
|
|
|
|
autopilot, err := s.state.Autopilot()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-28 22:48:42 +00:00
|
|
|
if autopilot == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2017-03-21 23:36:44 +00:00
|
|
|
|
2017-11-29 01:26:16 +00:00
|
|
|
if _, err := sink.Write([]byte{byte(structs.AutopilotRequestType)}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-21 23:36:44 +00:00
|
|
|
if err := encoder.Encode(autopilot); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-12-06 23:43:07 +00:00
|
|
|
func (s *consulSnapshot) Release() {
|
2015-10-13 05:21:39 +00:00
|
|
|
s.state.Close()
|
2013-12-06 23:43:07 +00:00
|
|
|
}
|