2013-12-06 23:43:07 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2014-12-06 12:08:35 +00:00
|
|
|
"errors"
|
2013-12-11 22:04:44 +00:00
|
|
|
"fmt"
|
2013-12-06 23:43:07 +00:00
|
|
|
"io"
|
2014-10-15 21:56:12 +00:00
|
|
|
"io/ioutil"
|
2013-12-17 19:13:19 +00:00
|
|
|
"log"
|
|
|
|
"time"
|
2014-10-09 19:23:32 +00:00
|
|
|
|
2015-01-05 23:13:39 +00:00
|
|
|
"github.com/armon/go-metrics"
|
2014-10-09 19:23:32 +00:00
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
// consulFSM 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.
|
2013-12-06 23:43:07 +00:00
|
|
|
type consulFSM 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
|
2014-03-05 22:34:29 +00:00
|
|
|
state *StateStore
|
2014-12-11 07:49:44 +00:00
|
|
|
gc *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 {
|
2013-12-18 23:09:38 +00:00
|
|
|
state *StateSnapshot
|
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
|
|
|
|
}
|
|
|
|
|
2014-10-15 21:56:12 +00:00
|
|
|
// NewFSMPath is used to construct a new FSM with a blank state
|
2014-12-11 07:49:44 +00:00
|
|
|
func NewFSM(gc *TombstoneGC, path string, logOutput io.Writer) (*consulFSM, error) {
|
2014-10-15 21:56:12 +00:00
|
|
|
// Create a temporary path for the state store
|
|
|
|
tmpPath, err := ioutil.TempDir(path, "state")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a state store
|
2014-12-11 07:49:44 +00:00
|
|
|
state, err := NewStateStorePath(gc, tmpPath, logOutput)
|
2013-12-11 01:00:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fsm := &consulFSM{
|
2014-03-05 22:34:29 +00:00
|
|
|
logOutput: logOutput,
|
|
|
|
logger: log.New(logOutput, "", log.LstdFlags),
|
2014-10-15 21:56:12 +00:00
|
|
|
path: path,
|
2014-03-05 22:34:29 +00:00
|
|
|
state: state,
|
2014-12-11 07:49:44 +00:00
|
|
|
gc: gc,
|
2013-12-11 01:00:48 +00:00
|
|
|
}
|
|
|
|
return fsm, nil
|
|
|
|
}
|
|
|
|
|
2014-02-05 19:00:10 +00:00
|
|
|
// Close is used to cleanup resources associated with the FSM
|
|
|
|
func (c *consulFSM) Close() error {
|
|
|
|
return c.state.Close()
|
|
|
|
}
|
|
|
|
|
2013-12-12 18:48:36 +00:00
|
|
|
// State is used to return a handle to the current state
|
|
|
|
func (c *consulFSM) State() *StateStore {
|
|
|
|
return c.state
|
|
|
|
}
|
|
|
|
|
2014-01-04 01:15:09 +00:00
|
|
|
func (c *consulFSM) Apply(log *raft.Log) interface{} {
|
|
|
|
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:
|
2014-02-05 02:33:15 +00:00
|
|
|
return c.decodeRegister(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)
|
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
|
|
|
|
} else {
|
|
|
|
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
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
func (c *consulFSM) decodeRegister(buf []byte, index uint64) interface{} {
|
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-02-05 02:33:15 +00:00
|
|
|
return c.applyRegister(&req, index)
|
2014-01-08 21:39:40 +00:00
|
|
|
}
|
2013-12-11 22:04:44 +00:00
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
func (c *consulFSM) applyRegister(req *structs.RegisterRequest, index uint64) interface{} {
|
2015-01-05 23:13:39 +00:00
|
|
|
defer metrics.MeasureSince([]string{"consul", "fsm", "register"}, time.Now())
|
2014-08-22 19:38:33 +00:00
|
|
|
// Apply all updates in a single transaction
|
|
|
|
if err := c.state.EnsureRegistration(index, req); err != nil {
|
|
|
|
c.logger.Printf("[INFO] 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
|
|
|
}
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
func (c *consulFSM) applyDeregister(buf []byte, index uint64) interface{} {
|
2015-01-05 23:13:39 +00:00
|
|
|
defer metrics.MeasureSince([]string{"consul", "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))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Either remove the service entry or the whole node
|
2014-01-06 22:18:38 +00:00
|
|
|
if req.ServiceID != "" {
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := c.state.DeleteNodeService(index, req.Node, req.ServiceID); err != nil {
|
2014-02-03 23:21:56 +00:00
|
|
|
c.logger.Printf("[INFO] consul.fsm: DeleteNodeService failed: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2014-01-08 21:39:40 +00:00
|
|
|
} else if req.CheckID != "" {
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := c.state.DeleteNodeCheck(index, req.Node, req.CheckID); err != nil {
|
2014-02-03 23:21:56 +00:00
|
|
|
c.logger.Printf("[INFO] consul.fsm: DeleteNodeCheck failed: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2013-12-11 23:34:10 +00:00
|
|
|
} else {
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := c.state.DeleteNode(index, req.Node); err != nil {
|
2014-02-03 23:21:56 +00:00
|
|
|
c.logger.Printf("[INFO] consul.fsm: DeleteNode failed: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2013-12-11 23:34:10 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-03-31 20:41:43 +00:00
|
|
|
func (c *consulFSM) applyKVSOperation(buf []byte, index uint64) interface{} {
|
|
|
|
var req structs.KVSRequest
|
|
|
|
if err := structs.Decode(buf, &req); err != nil {
|
|
|
|
panic(fmt.Errorf("failed to decode request: %v", err))
|
|
|
|
}
|
2015-01-05 23:13:39 +00:00
|
|
|
defer metrics.MeasureSince([]string{"consul", "fsm", "kvs", string(req.Op)}, time.Now())
|
2014-03-31 20:41:43 +00:00
|
|
|
switch req.Op {
|
|
|
|
case structs.KVSSet:
|
|
|
|
return c.state.KVSSet(index, &req.DirEnt)
|
|
|
|
case structs.KVSDelete:
|
|
|
|
return c.state.KVSDelete(index, req.DirEnt.Key)
|
2015-01-09 01:08:44 +00:00
|
|
|
case structs.KVSDeleteCAS:
|
|
|
|
act, err := c.state.KVSDeleteCheckAndSet(index, req.DirEnt.Key, req.DirEnt.ModifyIndex)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
return act
|
|
|
|
}
|
2014-03-31 20:41:43 +00:00
|
|
|
case structs.KVSDeleteTree:
|
|
|
|
return c.state.KVSDeleteTree(index, req.DirEnt.Key)
|
|
|
|
case structs.KVSCAS:
|
|
|
|
act, err := c.state.KVSCheckAndSet(index, &req.DirEnt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
return act
|
|
|
|
}
|
2014-05-16 02:22:31 +00:00
|
|
|
case structs.KVSLock:
|
|
|
|
act, err := c.state.KVSLock(index, &req.DirEnt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
return act
|
|
|
|
}
|
|
|
|
case structs.KVSUnlock:
|
|
|
|
act, err := c.state.KVSUnlock(index, &req.DirEnt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
return act
|
|
|
|
}
|
2014-03-31 20:41:43 +00:00
|
|
|
default:
|
2014-12-06 12:08:35 +00:00
|
|
|
err := errors.New(fmt.Sprintf("Invalid KVS operation '%s'", req.Op))
|
|
|
|
c.logger.Printf("[WARN] consul.fsm: %v", err)
|
|
|
|
return err
|
2014-03-31 20:41:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-16 02:22:31 +00:00
|
|
|
func (c *consulFSM) applySessionOperation(buf []byte, index uint64) interface{} {
|
|
|
|
var req structs.SessionRequest
|
|
|
|
if err := structs.Decode(buf, &req); err != nil {
|
|
|
|
panic(fmt.Errorf("failed to decode request: %v", err))
|
|
|
|
}
|
2015-01-05 23:13:39 +00:00
|
|
|
defer metrics.MeasureSince([]string{"consul", "fsm", "session", string(req.Op)}, time.Now())
|
2014-05-16 02:22:31 +00:00
|
|
|
switch req.Op {
|
|
|
|
case structs.SessionCreate:
|
2014-05-16 02:37:58 +00:00
|
|
|
if err := c.state.SessionCreate(index, &req.Session); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
return req.Session.ID
|
|
|
|
}
|
2014-05-16 02:22:31 +00:00
|
|
|
case structs.SessionDestroy:
|
|
|
|
return c.state.SessionDestroy(index, req.Session.ID)
|
|
|
|
default:
|
|
|
|
c.logger.Printf("[WARN] consul.fsm: Invalid Session operation '%s'", req.Op)
|
|
|
|
return fmt.Errorf("Invalid Session operation '%s'", req.Op)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-05 23:55:58 +00:00
|
|
|
func (c *consulFSM) applyACLOperation(buf []byte, index uint64) interface{} {
|
|
|
|
var req structs.ACLRequest
|
|
|
|
if err := structs.Decode(buf, &req); err != nil {
|
|
|
|
panic(fmt.Errorf("failed to decode request: %v", err))
|
|
|
|
}
|
2015-01-05 23:13:39 +00:00
|
|
|
defer metrics.MeasureSince([]string{"consul", "fsm", "acl", string(req.Op)}, time.Now())
|
2014-08-05 23:55:58 +00:00
|
|
|
switch req.Op {
|
2015-05-27 01:30:14 +00:00
|
|
|
case structs.ACLForceSet, structs.ACLSet:
|
2014-10-09 19:23:32 +00:00
|
|
|
if err := c.state.ACLSet(index, &req.ACL); err != nil {
|
2014-08-05 23:55:58 +00:00
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
return req.ACL.ID
|
|
|
|
}
|
|
|
|
case structs.ACLDelete:
|
|
|
|
return c.state.ACLDelete(index, req.ACL.ID)
|
|
|
|
default:
|
|
|
|
c.logger.Printf("[WARN] consul.fsm: Invalid ACL operation '%s'", req.Op)
|
|
|
|
return fmt.Errorf("Invalid ACL operation '%s'", req.Op)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-15 23:28:56 +00:00
|
|
|
func (c *consulFSM) applyTombstoneOperation(buf []byte, index uint64) interface{} {
|
|
|
|
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))
|
|
|
|
}
|
2015-01-05 23:13:39 +00:00
|
|
|
defer metrics.MeasureSince([]string{"consul", "fsm", "tombstone", string(req.Op)}, time.Now())
|
2014-12-15 23:28:56 +00:00
|
|
|
switch req.Op {
|
|
|
|
case structs.TombstoneReap:
|
|
|
|
return c.state.ReapTombstones(req.ReapIndex)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-12-06 23:43:07 +00:00
|
|
|
func (c *consulFSM) Snapshot() (raft.FSMSnapshot, error) {
|
2013-12-17 19:13:19 +00:00
|
|
|
defer func(start time.Time) {
|
2014-02-03 23:21:56 +00:00
|
|
|
c.logger.Printf("[INFO] consul.fsm: snapshot created in %v", time.Now().Sub(start))
|
2013-12-17 19:13:19 +00:00
|
|
|
}(time.Now())
|
|
|
|
|
2013-12-16 18:47:14 +00:00
|
|
|
// Create a new snapshot
|
|
|
|
snap, err := c.state.Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &consulSnapshot{snap}, nil
|
2013-12-06 23:43:07 +00:00
|
|
|
}
|
|
|
|
|
2013-12-11 02:19:15 +00:00
|
|
|
func (c *consulFSM) Restore(old io.ReadCloser) error {
|
|
|
|
defer old.Close()
|
|
|
|
|
2014-10-15 21:56:12 +00:00
|
|
|
// Create a temporary path for the state store
|
|
|
|
tmpPath, err := ioutil.TempDir(c.path, "state")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-12-11 02:19:15 +00:00
|
|
|
// Create a new state store
|
2014-12-11 07:49:44 +00:00
|
|
|
state, err := NewStateStorePath(c.gc, tmpPath, c.logOutput)
|
2013-12-11 02:19:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-03-31 20:41:43 +00:00
|
|
|
c.state.Close()
|
2014-01-08 21:39:40 +00:00
|
|
|
c.state = state
|
2013-12-11 02:19:15 +00:00
|
|
|
|
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
|
|
|
|
}
|
2014-02-05 02:33:15 +00:00
|
|
|
c.applyRegister(&req, header.LastIndex)
|
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
|
|
|
|
}
|
2014-04-01 18:29:55 +00:00
|
|
|
if err := c.state.KVSRestore(&req); err != nil {
|
2014-03-31 20:56:16 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-05-16 02:22:31 +00:00
|
|
|
case structs.SessionRequestType:
|
|
|
|
var req structs.Session
|
|
|
|
if err := dec.Decode(&req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := c.state.SessionRestore(&req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-05 23:55:58 +00:00
|
|
|
case structs.ACLRequestType:
|
|
|
|
var req structs.ACL
|
|
|
|
if err := dec.Decode(&req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := c.state.ACLRestore(&req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-12-15 23:31:13 +00:00
|
|
|
case structs.TombstoneRequestType:
|
|
|
|
var req structs.DirEntry
|
|
|
|
if err := dec.Decode(&req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := c.state.TombstoneRestore(&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
|
|
|
|
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())
|
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{
|
|
|
|
LastIndex: s.state.LastIndex(),
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-05-16 02:22:31 +00:00
|
|
|
if err := s.persistKV(sink, encoder); err != nil {
|
|
|
|
sink.Cancel()
|
|
|
|
return err
|
|
|
|
}
|
2014-12-15 23:31:13 +00:00
|
|
|
|
|
|
|
if err := s.persistTombstones(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 {
|
2014-02-05 02:33:15 +00:00
|
|
|
// Get all the nodes
|
|
|
|
nodes := s.state.Nodes()
|
|
|
|
|
2013-12-16 18:47:14 +00:00
|
|
|
// Register each node
|
2013-12-19 20:03:57 +00:00
|
|
|
var req structs.RegisterRequest
|
2014-01-08 18:31:42 +00:00
|
|
|
for i := 0; i < len(nodes); i++ {
|
2013-12-19 20:03:57 +00:00
|
|
|
req = structs.RegisterRequest{
|
2014-01-08 18:31:42 +00:00
|
|
|
Node: nodes[i].Node,
|
|
|
|
Address: nodes[i].Address,
|
2013-12-16 18:47:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Register the node itself
|
2013-12-19 20:03:57 +00:00
|
|
|
sink.Write([]byte{byte(structs.RegisterRequestType)})
|
2013-12-16 18:47:14 +00:00
|
|
|
if err := encoder.Encode(&req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register each service this node has
|
2014-01-08 18:31:42 +00:00
|
|
|
services := s.state.NodeServices(nodes[i].Node)
|
2014-01-08 21:39:40 +00:00
|
|
|
for _, srv := range services.Services {
|
|
|
|
req.Service = srv
|
|
|
|
sink.Write([]byte{byte(structs.RegisterRequestType)})
|
|
|
|
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
|
|
|
|
checks := s.state.NodeChecks(nodes[i].Node)
|
|
|
|
for _, check := range checks {
|
|
|
|
req.Check = check
|
2013-12-19 20:03:57 +00:00
|
|
|
sink.Write([]byte{byte(structs.RegisterRequestType)})
|
2013-12-16 18:47:14 +00:00
|
|
|
if err := encoder.Encode(&req); 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 {
|
|
|
|
sessions, err := s.state.SessionList()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-03-31 20:56:16 +00:00
|
|
|
|
2014-05-16 02:22:31 +00:00
|
|
|
for _, s := range sessions {
|
|
|
|
sink.Write([]byte{byte(structs.SessionRequestType)})
|
|
|
|
if err := encoder.Encode(s); err != nil {
|
|
|
|
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 {
|
|
|
|
acls, err := s.state.ACLList()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range acls {
|
|
|
|
sink.Write([]byte{byte(structs.ACLRequestType)})
|
|
|
|
if err := encoder.Encode(s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-16 02:22:31 +00:00
|
|
|
func (s *consulSnapshot) persistKV(sink raft.SnapshotSink,
|
|
|
|
encoder *codec.Encoder) error {
|
2014-04-01 18:55:25 +00:00
|
|
|
streamCh := make(chan interface{}, 256)
|
|
|
|
errorCh := make(chan error)
|
|
|
|
go func() {
|
|
|
|
if err := s.state.KVSDump(streamCh); err != nil {
|
|
|
|
errorCh <- err
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case raw := <-streamCh:
|
|
|
|
if raw == nil {
|
2014-05-16 02:22:31 +00:00
|
|
|
return nil
|
2014-04-01 18:55:25 +00:00
|
|
|
}
|
|
|
|
sink.Write([]byte{byte(structs.KVSRequestType)})
|
2014-04-01 19:10:58 +00:00
|
|
|
if err := encoder.Encode(raw); err != nil {
|
2014-04-01 18:55:25 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case err := <-errorCh:
|
2014-03-31 20:56:16 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
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 {
|
|
|
|
streamCh := make(chan interface{}, 256)
|
|
|
|
errorCh := make(chan error)
|
|
|
|
go func() {
|
|
|
|
if err := s.state.TombstoneDump(streamCh); err != nil {
|
|
|
|
errorCh <- err
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case raw := <-streamCh:
|
|
|
|
if raw == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
sink.Write([]byte{byte(structs.TombstoneRequestType)})
|
|
|
|
if err := encoder.Encode(raw); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case err := <-errorCh:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-06 23:43:07 +00:00
|
|
|
func (s *consulSnapshot) Release() {
|
2013-12-16 18:47:14 +00:00
|
|
|
s.state.Close()
|
2013-12-06 23:43:07 +00:00
|
|
|
}
|