Browse Source

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

Ben Johnson 12 years ago
parent
commit
51406a3582

+ 11 - 28
third_party/github.com/coreos/raft/command.go

@@ -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 {

+ 32 - 0
third_party/github.com/coreos/raft/context.go

@@ -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
+}

+ 10 - 4
third_party/github.com/coreos/raft/server.go

@@ -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