command.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package main
  2. import (
  3. "encoding/binary"
  4. "fmt"
  5. "os"
  6. "path"
  7. "time"
  8. etcdErr "github.com/coreos/etcd/error"
  9. "github.com/coreos/etcd/store"
  10. "github.com/coreos/go-raft"
  11. )
  12. const commandPrefix = "etcd:"
  13. func commandName(name string) string {
  14. return commandPrefix + name
  15. }
  16. // A command represents an action to be taken on the replicated state machine.
  17. type Command interface {
  18. CommandName() string
  19. Apply(server *raft.Server) (interface{}, error)
  20. }
  21. // Create command
  22. type CreateCommand struct {
  23. Key string `json:"key"`
  24. Value string `json:"value"`
  25. ExpireTime time.Time `json:"expireTime"`
  26. IncrementalSuffix bool `json:"incrementalSuffix"`
  27. Force bool `json:"force"`
  28. }
  29. // The name of the create command in the log
  30. func (c *CreateCommand) CommandName() string {
  31. return commandName("create")
  32. }
  33. // Create node
  34. func (c *CreateCommand) Apply(server *raft.Server) (interface{}, error) {
  35. s, _ := server.StateMachine().(*store.Store)
  36. e, err := s.Create(c.Key, c.Value, c.IncrementalSuffix, c.Force, c.ExpireTime, server.CommitIndex(), server.Term())
  37. if err != nil {
  38. debug(err)
  39. return nil, err
  40. }
  41. return e, nil
  42. }
  43. // Update command
  44. type UpdateCommand struct {
  45. Key string `json:"key"`
  46. Value string `json:"value"`
  47. ExpireTime time.Time `json:"expireTime"`
  48. }
  49. // The name of the update command in the log
  50. func (c *UpdateCommand) CommandName() string {
  51. return commandName("update")
  52. }
  53. // Update node
  54. func (c *UpdateCommand) Apply(server *raft.Server) (interface{}, error) {
  55. s, _ := server.StateMachine().(*store.Store)
  56. e, err := s.Update(c.Key, c.Value, c.ExpireTime, server.CommitIndex(), server.Term())
  57. if err != nil {
  58. debug(err)
  59. return nil, err
  60. }
  61. return e, nil
  62. }
  63. // TestAndSet command
  64. type TestAndSetCommand struct {
  65. Key string `json:"key"`
  66. Value string `json:"value"`
  67. ExpireTime time.Time `json:"expireTime"`
  68. PrevValue string `json: prevValue`
  69. PrevIndex uint64 `json: prevValue`
  70. }
  71. // The name of the testAndSet command in the log
  72. func (c *TestAndSetCommand) CommandName() string {
  73. return commandName("testAndSet")
  74. }
  75. // Set the key-value pair if the current value of the key equals to the given prevValue
  76. func (c *TestAndSetCommand) Apply(server *raft.Server) (interface{}, error) {
  77. s, _ := server.StateMachine().(*store.Store)
  78. e, err := s.TestAndSet(c.Key, c.PrevValue, c.PrevIndex,
  79. c.Value, c.ExpireTime, server.CommitIndex(), server.Term())
  80. if err != nil {
  81. debug(err)
  82. return nil, err
  83. }
  84. return e, nil
  85. }
  86. // Delete command
  87. type DeleteCommand struct {
  88. Key string `json:"key"`
  89. Recursive bool `json:"recursive"`
  90. }
  91. // The name of the delete command in the log
  92. func (c *DeleteCommand) CommandName() string {
  93. return commandName("delete")
  94. }
  95. // Delete the key
  96. func (c *DeleteCommand) Apply(server *raft.Server) (interface{}, error) {
  97. s, _ := server.StateMachine().(*store.Store)
  98. e, err := s.Delete(c.Key, c.Recursive, server.CommitIndex(), server.Term())
  99. if err != nil {
  100. debug(err)
  101. return nil, err
  102. }
  103. return e, nil
  104. }
  105. // JoinCommand
  106. type JoinCommand struct {
  107. RaftVersion string `json:"raftVersion"`
  108. Name string `json:"name"`
  109. RaftURL string `json:"raftURL"`
  110. EtcdURL string `json:"etcdURL"`
  111. }
  112. func newJoinCommand(version, name, raftUrl, etcdUrl string) *JoinCommand {
  113. return &JoinCommand{
  114. RaftVersion: version,
  115. Name: name,
  116. RaftURL: raftUrl,
  117. EtcdURL: etcdUrl,
  118. }
  119. }
  120. // The name of the join command in the log
  121. func (c *JoinCommand) CommandName() string {
  122. return commandName("join")
  123. }
  124. // Join a server to the cluster
  125. func (c *JoinCommand) Apply(server *raft.Server) (interface{}, error) {
  126. s, _ := server.StateMachine().(*store.Store)
  127. r, _ := server.Context().(*raftServer)
  128. // check if the join command is from a previous machine, who lost all its previous log.
  129. e, _ := s.Get(path.Join("/_etcd/machines", c.Name), false, false, server.CommitIndex(), server.Term())
  130. b := make([]byte, 8)
  131. binary.PutUvarint(b, server.CommitIndex())
  132. if e != nil {
  133. return b, nil
  134. }
  135. // check machine number in the cluster
  136. num := machineNum()
  137. if num == maxClusterSize {
  138. debug("Reject join request from ", c.Name)
  139. return []byte{0}, etcdErr.NewError(etcdErr.EcodeNoMoreMachine, "", server.CommitIndex(), server.Term())
  140. }
  141. addNameToURL(c.Name, c.RaftVersion, c.RaftURL, c.EtcdURL)
  142. // add peer in raft
  143. err := server.AddPeer(c.Name, "")
  144. // add machine in etcd storage
  145. key := path.Join("_etcd/machines", c.Name)
  146. value := fmt.Sprintf("raft=%s&etcd=%s&raftVersion=%s", c.RaftURL, c.EtcdURL, c.RaftVersion)
  147. s.Create(key, value, false, false, store.Permanent, server.CommitIndex(), server.Term())
  148. // add peer stats
  149. if c.Name != r.Name() {
  150. r.followersStats.Followers[c.Name] = &raftFollowerStats{}
  151. r.followersStats.Followers[c.Name].Latency.Minimum = 1 << 63
  152. }
  153. return b, err
  154. }
  155. func (c *JoinCommand) NodeName() string {
  156. return c.Name
  157. }
  158. // RemoveCommand
  159. type RemoveCommand struct {
  160. Name string `json:"name"`
  161. }
  162. // The name of the remove command in the log
  163. func (c *RemoveCommand) CommandName() string {
  164. return commandName("remove")
  165. }
  166. // Remove a server from the cluster
  167. func (c *RemoveCommand) Apply(server *raft.Server) (interface{}, error) {
  168. s, _ := server.StateMachine().(*store.Store)
  169. r, _ := server.Context().(*raftServer)
  170. // remove machine in etcd storage
  171. key := path.Join("_etcd/machines", c.Name)
  172. _, err := s.Delete(key, false, server.CommitIndex(), server.Term())
  173. // delete from stats
  174. delete(r.followersStats.Followers, c.Name)
  175. if err != nil {
  176. return []byte{0}, err
  177. }
  178. // remove peer in raft
  179. err = server.RemovePeer(c.Name)
  180. if err != nil {
  181. return []byte{0}, err
  182. }
  183. if c.Name == server.Name() {
  184. // the removed node is this node
  185. // if the node is not replaying the previous logs
  186. // and the node has sent out a join request in this
  187. // start. It is sure that this node received a new remove
  188. // command and need to be removed
  189. if server.CommitIndex() > r.joinIndex && r.joinIndex != 0 {
  190. debugf("server [%s] is removed", server.Name())
  191. os.Exit(0)
  192. } else {
  193. // else ignore remove
  194. debugf("ignore previous remove command.")
  195. }
  196. }
  197. b := make([]byte, 8)
  198. binary.PutUvarint(b, server.CommitIndex())
  199. return b, err
  200. }