github.com/clly/consul@v1.4.5/agent/consul/fsm/fsm.go (about)

     1  package fsm
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"log"
     7  	"sync"
     8  	"time"
     9  
    10  	"github.com/hashicorp/consul/agent/consul/state"
    11  	"github.com/hashicorp/consul/agent/structs"
    12  	"github.com/hashicorp/go-msgpack/codec"
    13  	"github.com/hashicorp/raft"
    14  )
    15  
    16  // msgpackHandle is a shared handle for encoding/decoding msgpack payloads
    17  var msgpackHandle = &codec.MsgpackHandle{
    18  	RawToString: true,
    19  }
    20  
    21  // command is a command method on the FSM.
    22  type command func(buf []byte, index uint64) interface{}
    23  
    24  // unboundCommand is a command method on the FSM, not yet bound to an FSM
    25  // instance.
    26  type unboundCommand func(c *FSM, buf []byte, index uint64) interface{}
    27  
    28  // commands is a map from message type to unbound command.
    29  var commands map[structs.MessageType]unboundCommand
    30  
    31  // registerCommand registers a new command with the FSM, which should be done
    32  // at package init() time.
    33  func registerCommand(msg structs.MessageType, fn unboundCommand) {
    34  	if commands == nil {
    35  		commands = make(map[structs.MessageType]unboundCommand)
    36  	}
    37  	if commands[msg] != nil {
    38  		panic(fmt.Errorf("Message %d is already registered", msg))
    39  	}
    40  	commands[msg] = fn
    41  }
    42  
    43  // FSM implements a finite state machine that is used
    44  // along with Raft to provide strong consistency. We implement
    45  // this outside the Server to avoid exposing this outside the package.
    46  type FSM struct {
    47  	logOutput io.Writer
    48  	logger    *log.Logger
    49  	path      string
    50  
    51  	// apply is built off the commands global and is used to route apply
    52  	// operations to their appropriate handlers.
    53  	apply map[structs.MessageType]command
    54  
    55  	// stateLock is only used to protect outside callers to State() from
    56  	// racing with Restore(), which is called by Raft (it puts in a totally
    57  	// new state store). Everything internal here is synchronized by the
    58  	// Raft side, so doesn't need to lock this.
    59  	stateLock sync.RWMutex
    60  	state     *state.Store
    61  
    62  	gc *state.TombstoneGC
    63  }
    64  
    65  // New is used to construct a new FSM with a blank state.
    66  func New(gc *state.TombstoneGC, logOutput io.Writer) (*FSM, error) {
    67  	stateNew, err := state.NewStateStore(gc)
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  
    72  	fsm := &FSM{
    73  		logOutput: logOutput,
    74  		logger:    log.New(logOutput, "", log.LstdFlags),
    75  		apply:     make(map[structs.MessageType]command),
    76  		state:     stateNew,
    77  		gc:        gc,
    78  	}
    79  
    80  	// Build out the apply dispatch table based on the registered commands.
    81  	for msg, fn := range commands {
    82  		thisFn := fn
    83  		fsm.apply[msg] = func(buf []byte, index uint64) interface{} {
    84  			return thisFn(fsm, buf, index)
    85  		}
    86  	}
    87  
    88  	return fsm, nil
    89  }
    90  
    91  // State is used to return a handle to the current state
    92  func (c *FSM) State() *state.Store {
    93  	c.stateLock.RLock()
    94  	defer c.stateLock.RUnlock()
    95  	return c.state
    96  }
    97  
    98  func (c *FSM) Apply(log *raft.Log) interface{} {
    99  	buf := log.Data
   100  	msgType := structs.MessageType(buf[0])
   101  
   102  	// Check if this message type should be ignored when unknown. This is
   103  	// used so that new commands can be added with developer control if older
   104  	// versions can safely ignore the command, or if they should crash.
   105  	ignoreUnknown := false
   106  	if msgType&structs.IgnoreUnknownTypeFlag == structs.IgnoreUnknownTypeFlag {
   107  		msgType &= ^structs.IgnoreUnknownTypeFlag
   108  		ignoreUnknown = true
   109  	}
   110  
   111  	// Apply based on the dispatch table, if possible.
   112  	if fn := c.apply[msgType]; fn != nil {
   113  		return fn(buf[1:], log.Index)
   114  	}
   115  
   116  	// Otherwise, see if it's safe to ignore. If not, we have to panic so
   117  	// that we crash and our state doesn't diverge.
   118  	if ignoreUnknown {
   119  		c.logger.Printf("[WARN] consul.fsm: ignoring unknown message type (%d), upgrade to newer version", msgType)
   120  		return nil
   121  	}
   122  	panic(fmt.Errorf("failed to apply request: %#v", buf))
   123  }
   124  
   125  func (c *FSM) Snapshot() (raft.FSMSnapshot, error) {
   126  	defer func(start time.Time) {
   127  		c.logger.Printf("[INFO] consul.fsm: snapshot created in %v", time.Since(start))
   128  	}(time.Now())
   129  
   130  	return &snapshot{c.state.Snapshot()}, nil
   131  }
   132  
   133  // Restore streams in the snapshot and replaces the current state store with a
   134  // new one based on the snapshot if all goes OK during the restore.
   135  func (c *FSM) Restore(old io.ReadCloser) error {
   136  	defer old.Close()
   137  
   138  	// Create a new state store.
   139  	stateNew, err := state.NewStateStore(c.gc)
   140  	if err != nil {
   141  		return err
   142  	}
   143  
   144  	// Set up a new restore transaction
   145  	restore := stateNew.Restore()
   146  	defer restore.Abort()
   147  
   148  	// Create a decoder
   149  	dec := codec.NewDecoder(old, msgpackHandle)
   150  
   151  	// Read in the header
   152  	var header snapshotHeader
   153  	if err := dec.Decode(&header); err != nil {
   154  		return err
   155  	}
   156  
   157  	// Populate the new state
   158  	msgType := make([]byte, 1)
   159  	for {
   160  		// Read the message type
   161  		_, err := old.Read(msgType)
   162  		if err == io.EOF {
   163  			break
   164  		} else if err != nil {
   165  			return err
   166  		}
   167  
   168  		// Decode
   169  		msg := structs.MessageType(msgType[0])
   170  		if fn := restorers[msg]; fn != nil {
   171  			if err := fn(&header, restore, dec); err != nil {
   172  				return err
   173  			}
   174  		} else {
   175  			return fmt.Errorf("Unrecognized msg type %d", msg)
   176  		}
   177  	}
   178  	restore.Commit()
   179  
   180  	// External code might be calling State(), so we need to synchronize
   181  	// here to make sure we swap in the new state store atomically.
   182  	c.stateLock.Lock()
   183  	stateOld := c.state
   184  	c.state = stateNew
   185  	c.stateLock.Unlock()
   186  
   187  	// Signal that the old state store has been abandoned. This is required
   188  	// because we don't operate on it any more, we just throw it away, so
   189  	// blocking queries won't see any changes and need to be woken up.
   190  	stateOld.Abandon()
   191  	return nil
   192  }