Interface clean up.
parent
34008d93eb
commit
97fbdde521
|
@ -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 {
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
|
8
test.go
8
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()
|
||||
|
|
Loading…
Reference in New Issue