bump(github.com/coreos/raft): 0409b22a50cb2576318f294eba5c1092316dbea1
This commit is contained in:
39
third_party/github.com/coreos/raft/command.go
vendored
39
third_party/github.com/coreos/raft/command.go
vendored
@ -8,28 +8,25 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
//
|
|
||||||
// Globals
|
|
||||||
//
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
var commandTypes map[string]Command
|
var commandTypes map[string]Command
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
commandTypes = map[string]Command{}
|
commandTypes = map[string]Command{}
|
||||||
}
|
}
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
// Command represents an action to be taken on the replicated state machine.
|
||||||
//
|
|
||||||
// Typedefs
|
|
||||||
//
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
// A command represents an action to be taken on the replicated state machine.
|
|
||||||
type Command interface {
|
type Command interface {
|
||||||
CommandName() string
|
CommandName() string
|
||||||
Apply(server Server) (interface{}, error)
|
}
|
||||||
|
|
||||||
|
// CommandApply represents the interface to apply a command to the server.
|
||||||
|
type CommandApply interface {
|
||||||
|
Apply(Context) (interface{}, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// deprecatedCommandApply represents the old interface to apply a command to the server.
|
||||||
|
type deprecatedCommandApply interface {
|
||||||
|
Apply(Server) (interface{}, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type CommandEncoder interface {
|
type CommandEncoder interface {
|
||||||
@ -37,16 +34,6 @@ type CommandEncoder interface {
|
|||||||
Decode(r io.Reader) error
|
Decode(r io.Reader) error
|
||||||
}
|
}
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
//
|
|
||||||
// Functions
|
|
||||||
//
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
//--------------------------------------
|
|
||||||
// Instantiation
|
|
||||||
//--------------------------------------
|
|
||||||
|
|
||||||
// Creates a new instance of a command by name.
|
// Creates a new instance of a command by name.
|
||||||
func newCommand(name string, data []byte) (Command, error) {
|
func newCommand(name string, data []byte) (Command, error) {
|
||||||
// Find the registered command.
|
// Find the registered command.
|
||||||
@ -76,10 +63,6 @@ func newCommand(name string, data []byte) (Command, error) {
|
|||||||
return copy, nil
|
return copy, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------
|
|
||||||
// Registration
|
|
||||||
//--------------------------------------
|
|
||||||
|
|
||||||
// Registers a command by storing a reference to an instance of it.
|
// Registers a command by storing a reference to an instance of it.
|
||||||
func RegisterCommand(command Command) {
|
func RegisterCommand(command Command) {
|
||||||
if command == nil {
|
if command == nil {
|
||||||
|
32
third_party/github.com/coreos/raft/context.go
vendored
Normal file
32
third_party/github.com/coreos/raft/context.go
vendored
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package raft
|
||||||
|
|
||||||
|
// Context represents the current state of the server. It is passed into
|
||||||
|
// a command when the command is being applied since the server methods
|
||||||
|
// are locked.
|
||||||
|
type Context interface {
|
||||||
|
Server() Server
|
||||||
|
CurrentTerm() uint64
|
||||||
|
CurrentIndex() uint64
|
||||||
|
}
|
||||||
|
|
||||||
|
// context is the concrete implementation of Context.
|
||||||
|
type context struct {
|
||||||
|
server Server
|
||||||
|
currentIndex uint64
|
||||||
|
currentTerm uint64
|
||||||
|
}
|
||||||
|
|
||||||
|
// Server returns a reference to the server.
|
||||||
|
func (c *context) Server() Server {
|
||||||
|
return c.server
|
||||||
|
}
|
||||||
|
|
||||||
|
// CurrentTerm returns current term the server is in.
|
||||||
|
func (c *context) CurrentTerm() uint64 {
|
||||||
|
return c.currentTerm
|
||||||
|
}
|
||||||
|
|
||||||
|
// CurrentIndex returns current index the server is at.
|
||||||
|
func (c *context) CurrentIndex() uint64 {
|
||||||
|
return c.currentIndex
|
||||||
|
}
|
14
third_party/github.com/coreos/raft/server.go
vendored
14
third_party/github.com/coreos/raft/server.go
vendored
@ -144,7 +144,7 @@ type ev struct {
|
|||||||
// compaction is to be disabled. context can be anything (including nil)
|
// compaction is to be disabled. context can be anything (including nil)
|
||||||
// and is not used by the raft package except returned by
|
// and is not used by the raft package except returned by
|
||||||
// Server.Context(). connectionString can be anything.
|
// Server.Context(). connectionString can be anything.
|
||||||
func NewServer(name string, path string, transporter Transporter, stateMachine StateMachine, context interface{}, connectionString string) (Server, error) {
|
func NewServer(name string, path string, transporter Transporter, stateMachine StateMachine, ctx interface{}, connectionString string) (Server, error) {
|
||||||
if name == "" {
|
if name == "" {
|
||||||
return nil, errors.New("raft.Server: Name cannot be blank")
|
return nil, errors.New("raft.Server: Name cannot be blank")
|
||||||
}
|
}
|
||||||
@ -157,7 +157,7 @@ func NewServer(name string, path string, transporter Transporter, stateMachine S
|
|||||||
path: path,
|
path: path,
|
||||||
transporter: transporter,
|
transporter: transporter,
|
||||||
stateMachine: stateMachine,
|
stateMachine: stateMachine,
|
||||||
context: context,
|
context: ctx,
|
||||||
state: Stopped,
|
state: Stopped,
|
||||||
peers: make(map[string]*Peer),
|
peers: make(map[string]*Peer),
|
||||||
log: newLog(),
|
log: newLog(),
|
||||||
@ -171,8 +171,14 @@ func NewServer(name string, path string, transporter Transporter, stateMachine S
|
|||||||
|
|
||||||
// Setup apply function.
|
// Setup apply function.
|
||||||
s.log.ApplyFunc = func(c Command) (interface{}, error) {
|
s.log.ApplyFunc = func(c Command) (interface{}, error) {
|
||||||
result, err := c.Apply(s)
|
switch c := c.(type) {
|
||||||
return result, err
|
case CommandApply:
|
||||||
|
return c.Apply(&context{server: s, currentTerm: s.currentTerm, currentIndex: s.log.currentIndex()})
|
||||||
|
case deprecatedCommandApply:
|
||||||
|
return c.Apply(s)
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("Command does not implement Apply()")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return s, nil
|
return s, nil
|
||||||
|
Reference in New Issue
Block a user