Interface clean up.

pull/820/head
Ben Johnson 2013-05-26 20:17:20 -06:00
parent 34008d93eb
commit 97fbdde521
6 changed files with 12 additions and 23 deletions

View File

@ -31,12 +31,6 @@ type Command interface {
Apply(server *Server) Apply(server *Server)
} }
// This is a marker interface to filter out commands that are processed
// internally by the protocol such as the "Join" command.
type InternalCommand interface {
InternalCommand() bool
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// //
// Functions // Functions
@ -48,7 +42,7 @@ type InternalCommand interface {
//-------------------------------------- //--------------------------------------
// Creates a new instance of a command by name. // Creates a new instance of a command by name.
func NewCommand(name string) (Command, error) { func newCommand(name string) (Command, error) {
// Find the registered command. // Find the registered command.
command := commandTypes[name] command := commandTypes[name]
if command == nil { if command == nil {

View File

@ -22,11 +22,6 @@ type JoinCommand struct {
// //
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// This function marks the command as internal.
func (c *JoinCommand) InternalCommand() bool {
return true
}
// The name of the command in the log. // The name of the command in the log.
func (c *JoinCommand) CommandName() string { func (c *JoinCommand) CommandName() string {
return "raft:join" return "raft:join"

View File

@ -134,7 +134,7 @@ func (e *LogEntry) Decode(r io.Reader) (pos int, err error) {
} }
// Instantiate command by name. // Instantiate command by name.
command, err := NewCommand(commandName) command, err := newCommand(commandName)
if err != nil { if err != nil {
err = fmt.Errorf("raft.LogEntry: Unable to instantiate command (%s): %v", commandName, err) err = fmt.Errorf("raft.LogEntry: Unable to instantiate command (%s): %v", commandName, err)
return return
@ -184,7 +184,7 @@ func (e *LogEntry) UnmarshalJSON(data []byte) error {
// Create a command based on the name. // Create a command based on the name.
var err error var err error
if e.Command, err = NewCommand(obj.Name); err != nil { if e.Command, err = newCommand(obj.Name); err != nil {
return err return err
} }
json.Unmarshal(obj.Command, e.Command) json.Unmarshal(obj.Command, e.Command)

View File

@ -36,7 +36,7 @@ var NotLeaderError = errors.New("Not current leader")
// A server is involved in the consensus protocol and can act as a follower, // A server is involved in the consensus protocol and can act as a follower,
// candidate or a leader. // candidate or a leader.
type Server struct { type Server struct {
JoinHandler func(*Server, *Peer, Command) error JoinHandler func(*Server, *Peer, *JoinCommand) error
RequestVoteHandler func(*Server, *Peer, *RequestVoteRequest) (*RequestVoteResponse, error) RequestVoteHandler func(*Server, *Peer, *RequestVoteRequest) (*RequestVoteResponse, error)
AppendEntriesHandler func(*Server, *Peer, *AppendEntriesRequest) (*AppendEntriesResponse, error) AppendEntriesHandler func(*Server, *Peer, *AppendEntriesRequest) (*AppendEntriesResponse, error)
name string name string
@ -353,7 +353,7 @@ loop:
} }
// Executes the handler for doing a command on a particular peer. // Executes the handler for doing a command on a particular peer.
func (s *Server) executeJoinHandler(peer *Peer, command Command) error { func (s *Server) executeJoinHandler(peer *Peer, command *JoinCommand) error {
if s.JoinHandler == nil { if s.JoinHandler == nil {
panic("raft.Server: JoinHandler not registered") panic("raft.Server: JoinHandler not registered")
} }

View File

@ -324,9 +324,9 @@ func TestServerMultiNode(t *testing.T) {
} }
for _, name := range names { for _, name := range names {
server := newTestServer(name) server := newTestServer(name)
server.SetElectionTimeout(TestElectionTimeout) server.SetElectionTimeout(testElectionTimeout)
server.SetHeartbeatTimeout(TestHeartbeatTimeout) server.SetHeartbeatTimeout(testHeartbeatTimeout)
server.JoinHandler = func(server *Server, peer *Peer, command Command) error { server.JoinHandler = func(server *Server, peer *Peer, command *JoinCommand) error {
mutex.Lock() mutex.Lock()
s := servers[peer.name] s := servers[peer.name]
mutex.Unlock() mutex.Unlock()

View File

@ -8,8 +8,8 @@ import (
) )
const ( const (
TestHeartbeatTimeout = 20 * time.Millisecond testHeartbeatTimeout = 20 * time.Millisecond
TestElectionTimeout = 60 * time.Millisecond testElectionTimeout = 60 * time.Millisecond
) )
func init() { func init() {
@ -75,14 +75,14 @@ func newTestCluster(names []string) (Servers, map[string]*Server) {
panic(fmt.Sprintf("Duplicate server in test cluster! %v", name)) panic(fmt.Sprintf("Duplicate server in test cluster! %v", name))
} }
server := newTestServer(name) server := newTestServer(name)
server.SetElectionTimeout(TestElectionTimeout) server.SetElectionTimeout(testElectionTimeout)
servers = append(servers, server) servers = append(servers, server)
lookup[name] = server lookup[name] = server
} }
for _, server := range servers { for _, server := range servers {
for _, peer := range servers { for _, peer := range servers {
if server != peer { if server != peer {
server.peers[peer.Name()] = NewPeer(server, peer.Name(), TestHeartbeatTimeout) server.peers[peer.Name()] = NewPeer(server, peer.Name(), testHeartbeatTimeout)
} }
} }
server.Start() server.Start()