2013-12-06 23:43:07 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2013-12-11 22:04:44 +00:00
|
|
|
"fmt"
|
2013-12-19 20:03:57 +00:00
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
2013-12-06 23:43:07 +00:00
|
|
|
"github.com/hashicorp/raft"
|
2013-12-16 18:47:14 +00:00
|
|
|
"github.com/ugorji/go/codec"
|
2013-12-06 23:43:07 +00:00
|
|
|
"io"
|
2013-12-17 19:13:19 +00:00
|
|
|
"log"
|
|
|
|
"time"
|
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 {
|
2013-12-11 01:00:48 +00:00
|
|
|
state *StateStore
|
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
|
|
|
}
|
|
|
|
|
2013-12-11 01:00:48 +00:00
|
|
|
// NewFSM is used to construct a new FSM with a blank state
|
|
|
|
func NewFSM() (*consulFSM, error) {
|
|
|
|
state, err := NewStateStore()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fsm := &consulFSM{
|
|
|
|
state: state,
|
|
|
|
}
|
|
|
|
return fsm, nil
|
|
|
|
}
|
|
|
|
|
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
|
2013-12-19 20:03:57 +00:00
|
|
|
switch structs.MessageType(buf[0]) {
|
|
|
|
case structs.RegisterRequestType:
|
2013-12-11 22:04:44 +00:00
|
|
|
return c.applyRegister(buf[1:])
|
2013-12-19 20:03:57 +00:00
|
|
|
case structs.DeregisterRequestType:
|
2013-12-11 23:34:10 +00:00
|
|
|
return c.applyDeregister(buf[1:])
|
2013-12-11 22:04:44 +00:00
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("failed to apply request: %#v", buf))
|
|
|
|
}
|
|
|
|
}
|
2013-12-11 02:19:15 +00:00
|
|
|
|
2013-12-11 22:04:44 +00:00
|
|
|
func (c *consulFSM) applyRegister(buf []byte) 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))
|
|
|
|
}
|
|
|
|
|
2013-12-11 22:38:18 +00:00
|
|
|
// Ensure the node
|
2014-01-08 18:31:42 +00:00
|
|
|
node := structs.Node{req.Node, req.Address}
|
|
|
|
c.state.EnsureNode(node)
|
2013-12-11 22:38:18 +00:00
|
|
|
|
|
|
|
// Ensure the service if provided
|
2014-01-06 22:18:38 +00:00
|
|
|
if req.ServiceID != "" && req.ServiceName != "" {
|
|
|
|
c.state.EnsureService(req.Node, req.ServiceID, req.ServiceName,
|
|
|
|
req.ServiceTag, req.ServicePort)
|
2013-12-11 22:38:18 +00:00
|
|
|
}
|
|
|
|
return nil
|
2013-12-06 23:43:07 +00:00
|
|
|
}
|
|
|
|
|
2013-12-11 23:34:10 +00:00
|
|
|
func (c *consulFSM) applyDeregister(buf []byte) interface{} {
|
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 != "" {
|
|
|
|
c.state.DeleteNodeService(req.Node, req.ServiceID)
|
2013-12-11 23:34:10 +00:00
|
|
|
} else {
|
|
|
|
c.state.DeleteNode(req.Node)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
log.Printf("[INFO] FSM Snapshot created in %v", time.Now().Sub(start))
|
|
|
|
}(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()
|
|
|
|
|
|
|
|
// Create a new state store
|
|
|
|
state, err := NewStateStore()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-12-16 18:47:14 +00:00
|
|
|
// Create a decoder
|
|
|
|
var handle codec.MsgpackHandle
|
|
|
|
dec := codec.NewDecoder(old, &handle)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register the service or the node
|
|
|
|
if req.ServiceName != "" {
|
2014-01-06 22:18:38 +00:00
|
|
|
state.EnsureService(req.Node, req.ServiceID, req.ServiceName,
|
2013-12-16 18:47:14 +00:00
|
|
|
req.ServiceTag, req.ServicePort)
|
|
|
|
} else {
|
2014-01-08 18:31:42 +00:00
|
|
|
node := structs.Node{req.Node, req.Address}
|
|
|
|
state.EnsureNode(node)
|
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
|
|
|
|
|
|
|
// Do an atomic flip, safe since Apply is not called concurrently
|
|
|
|
c.state = state
|
2013-12-06 23:43:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *consulSnapshot) Persist(sink raft.SnapshotSink) error {
|
2013-12-16 18:47:14 +00:00
|
|
|
// Get all the nodes
|
|
|
|
nodes := s.state.Nodes()
|
|
|
|
|
|
|
|
// Register the nodes
|
|
|
|
handle := codec.MsgpackHandle{}
|
|
|
|
encoder := codec.NewEncoder(sink, &handle)
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
sink.Cancel()
|
|
|
|
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-06 22:18:38 +00:00
|
|
|
for id, props := range services.Services {
|
|
|
|
req.ServiceID = id
|
|
|
|
req.ServiceName = props.Service
|
2013-12-16 18:47:14 +00:00
|
|
|
req.ServiceTag = props.Tag
|
|
|
|
req.ServicePort = props.Port
|
|
|
|
|
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 {
|
|
|
|
sink.Cancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-06 23:43:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *consulSnapshot) Release() {
|
2013-12-16 18:47:14 +00:00
|
|
|
s.state.Close()
|
2013-12-06 23:43:07 +00:00
|
|
|
}
|