github.com/macb/etcd@v0.3.1-0.20140227003422-a60481c6b1a0/server/remove_command.go (about) 1 package server 2 3 import ( 4 "encoding/binary" 5 "os" 6 7 "github.com/coreos/etcd/log" 8 "github.com/coreos/etcd/third_party/github.com/coreos/raft" 9 ) 10 11 func init() { 12 raft.RegisterCommand(&RemoveCommand{}) 13 } 14 15 // The RemoveCommand removes a server from the cluster. 16 type RemoveCommand struct { 17 Name string `json:"name"` 18 } 19 20 // The name of the remove command in the log 21 func (c *RemoveCommand) CommandName() string { 22 return "etcd:remove" 23 } 24 25 // Remove a server from the cluster 26 func (c *RemoveCommand) Apply(context raft.Context) (interface{}, error) { 27 ps, _ := context.Server().Context().(*PeerServer) 28 29 // Remove node from the shared registry. 30 err := ps.registry.Unregister(c.Name) 31 32 // Delete from stats 33 delete(ps.followersStats.Followers, c.Name) 34 35 if err != nil { 36 log.Debugf("Error while unregistering: %s (%v)", c.Name, err) 37 return []byte{0}, err 38 } 39 40 // Remove peer in raft 41 err = context.Server().RemovePeer(c.Name) 42 if err != nil { 43 log.Debugf("Unable to remove peer: %s (%v)", c.Name, err) 44 return []byte{0}, err 45 } 46 47 if c.Name == context.Server().Name() { 48 // the removed node is this node 49 50 // if the node is not replaying the previous logs 51 // and the node has sent out a join request in this 52 // start. It is sure that this node received a new remove 53 // command and need to be removed 54 if context.CommitIndex() > ps.joinIndex && ps.joinIndex != 0 { 55 log.Debugf("server [%s] is removed", context.Server().Name()) 56 os.Exit(0) 57 } else { 58 // else ignore remove 59 log.Debugf("ignore previous remove command.") 60 } 61 } 62 63 b := make([]byte, 8) 64 binary.PutUvarint(b, context.CommitIndex()) 65 66 return b, err 67 }