From 97fbdde52137680f41d689cf3052fdd681f6ddad Mon Sep 17 00:00:00 2001 From: Ben Johnson Date: Sun, 26 May 2013 20:17:20 -0600 Subject: [PATCH] Interface clean up. --- command.go | 8 +------- join_command.go | 5 ----- log_entry.go | 4 ++-- server.go | 4 ++-- server_test.go | 6 +++--- test.go | 8 ++++---- 6 files changed, 12 insertions(+), 23 deletions(-) diff --git a/command.go b/command.go index c0c75dcc37..ed21092d99 100644 --- a/command.go +++ b/command.go @@ -31,12 +31,6 @@ type Command interface { 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 @@ -48,7 +42,7 @@ type InternalCommand interface { //-------------------------------------- // 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. command := commandTypes[name] if command == nil { diff --git a/join_command.go b/join_command.go index 9e4d7dff00..bc99b9f373 100644 --- a/join_command.go +++ b/join_command.go @@ -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. func (c *JoinCommand) CommandName() string { return "raft:join" diff --git a/log_entry.go b/log_entry.go index 7f60c1957f..920b4eaf7d 100644 --- a/log_entry.go +++ b/log_entry.go @@ -134,7 +134,7 @@ func (e *LogEntry) Decode(r io.Reader) (pos int, err error) { } // Instantiate command by name. - command, err := NewCommand(commandName) + command, err := newCommand(commandName) if err != nil { err = fmt.Errorf("raft.LogEntry: Unable to instantiate command (%s): %v", commandName, err) return @@ -184,7 +184,7 @@ func (e *LogEntry) UnmarshalJSON(data []byte) error { // Create a command based on the name. var err error - if e.Command, err = NewCommand(obj.Name); err != nil { + if e.Command, err = newCommand(obj.Name); err != nil { return err } json.Unmarshal(obj.Command, e.Command) diff --git a/server.go b/server.go index 8fbe36614f..b05b50eebc 100644 --- a/server.go +++ b/server.go @@ -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, // candidate or a leader. type Server struct { - JoinHandler func(*Server, *Peer, Command) error + JoinHandler func(*Server, *Peer, *JoinCommand) error RequestVoteHandler func(*Server, *Peer, *RequestVoteRequest) (*RequestVoteResponse, error) AppendEntriesHandler func(*Server, *Peer, *AppendEntriesRequest) (*AppendEntriesResponse, error) name string @@ -353,7 +353,7 @@ loop: } // 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 { panic("raft.Server: JoinHandler not registered") } diff --git a/server_test.go b/server_test.go index 2d62e809d1..79b3cf0636 100644 --- a/server_test.go +++ b/server_test.go @@ -324,9 +324,9 @@ func TestServerMultiNode(t *testing.T) { } for _, name := range names { server := newTestServer(name) - server.SetElectionTimeout(TestElectionTimeout) - server.SetHeartbeatTimeout(TestHeartbeatTimeout) - server.JoinHandler = func(server *Server, peer *Peer, command Command) error { + server.SetElectionTimeout(testElectionTimeout) + server.SetHeartbeatTimeout(testHeartbeatTimeout) + server.JoinHandler = func(server *Server, peer *Peer, command *JoinCommand) error { mutex.Lock() s := servers[peer.name] mutex.Unlock() diff --git a/test.go b/test.go index 6a71f6b174..27da978406 100644 --- a/test.go +++ b/test.go @@ -8,8 +8,8 @@ import ( ) const ( - TestHeartbeatTimeout = 20 * time.Millisecond - TestElectionTimeout = 60 * time.Millisecond + testHeartbeatTimeout = 20 * time.Millisecond + testElectionTimeout = 60 * time.Millisecond ) func init() { @@ -75,14 +75,14 @@ func newTestCluster(names []string) (Servers, map[string]*Server) { panic(fmt.Sprintf("Duplicate server in test cluster! %v", name)) } server := newTestServer(name) - server.SetElectionTimeout(TestElectionTimeout) + server.SetElectionTimeout(testElectionTimeout) servers = append(servers, server) lookup[name] = server } for _, server := range servers { for _, peer := range servers { if server != peer { - server.peers[peer.Name()] = NewPeer(server, peer.Name(), TestHeartbeatTimeout) + server.peers[peer.Name()] = NewPeer(server, peer.Name(), testHeartbeatTimeout) } } server.Start()