bump(github.com/coreos/raft): 0409b22a50cb2576318f294eba5c1092316dbea1

This commit is contained in:
Ben Johnson 2013-12-23 16:01:53 -07:00
parent 9ebac0b9fd
commit 51406a3582
3 changed files with 53 additions and 32 deletions

View File

@ -8,28 +8,25 @@ import (
"reflect"
)
//------------------------------------------------------------------------------
//
// Globals
//
//------------------------------------------------------------------------------
var commandTypes map[string]Command
func init() {
commandTypes = map[string]Command{}
}
//------------------------------------------------------------------------------
//
// Typedefs
//
//------------------------------------------------------------------------------
// A command represents an action to be taken on the replicated state machine.
// Command represents an action to be taken on the replicated state machine.
type Command interface {
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 {
@ -37,16 +34,6 @@ type CommandEncoder interface {
Decode(r io.Reader) error
}
//------------------------------------------------------------------------------
//
// Functions
//
//------------------------------------------------------------------------------
//--------------------------------------
// Instantiation
//--------------------------------------
// Creates a new instance of a command by name.
func newCommand(name string, data []byte) (Command, error) {
// Find the registered command.
@ -76,10 +63,6 @@ func newCommand(name string, data []byte) (Command, error) {
return copy, nil
}
//--------------------------------------
// Registration
//--------------------------------------
// Registers a command by storing a reference to an instance of it.
func RegisterCommand(command Command) {
if command == nil {

View 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
}

View File

@ -144,7 +144,7 @@ type ev struct {
// compaction is to be disabled. context can be anything (including nil)
// and is not used by the raft package except returned by
// 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 == "" {
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,
transporter: transporter,
stateMachine: stateMachine,
context: context,
context: ctx,
state: Stopped,
peers: make(map[string]*Peer),
log: newLog(),
@ -171,8 +171,14 @@ func NewServer(name string, path string, transporter Transporter, stateMachine S
// Setup apply function.
s.log.ApplyFunc = func(c Command) (interface{}, error) {
result, err := c.Apply(s)
return result, err
switch c := c.(type) {
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