command.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. // Get command
  87. type GetCommand struct {
  88. Key string `json:"key"`
  89. Recursive bool `json:"recursive"`
  90. Sorted bool `json:"sorted"`
  91. }
  92. // The name of the get command in the log
  93. func (c *GetCommand) CommandName() string {
  94. return commandName("get")
  95. }
  96. // Get the value of key
  97. func (c *GetCommand) Apply(server *raft.Server) (interface{}, error) {
  98. s, _ := server.StateMachine().(*store.Store)
  99. e, err := s.Get(c.Key, c.Recursive, c.Sorted, server.CommitIndex(), server.Term())
  100. if err != nil {
  101. debug(err)
  102. return nil, err
  103. }
  104. return e, nil
  105. }
  106. // Delete command
  107. type DeleteCommand struct {
  108. Key string `json:"key"`
  109. Recursive bool `json:"recursive"`
  110. }
  111. // The name of the delete command in the log
  112. func (c *DeleteCommand) CommandName() string {
  113. return commandName("delete")
  114. }
  115. // Delete the key
  116. func (c *DeleteCommand) Apply(server *raft.Server) (interface{}, error) {
  117. s, _ := server.StateMachine().(*store.Store)
  118. e, err := s.Delete(c.Key, c.Recursive, server.CommitIndex(), server.Term())
  119. if err != nil {
  120. debug(err)
  121. return nil, err
  122. }
  123. return e, nil
  124. }
  125. // Watch command
  126. type WatchCommand struct {
  127. Key string `json:"key"`
  128. SinceIndex uint64 `json:"sinceIndex"`
  129. Recursive bool `json:"recursive"`
  130. }
  131. // The name of the watch command in the log
  132. func (c *WatchCommand) CommandName() string {
  133. return commandName("watch")
  134. }
  135. func (c *WatchCommand) Apply(server *raft.Server) (interface{}, error) {
  136. s, _ := server.StateMachine().(*store.Store)
  137. eventChan, err := s.Watch(c.Key, c.Recursive, c.SinceIndex, server.CommitIndex(), server.Term())
  138. if err != nil {
  139. return nil, err
  140. }
  141. e := <-eventChan
  142. return e, nil
  143. }
  144. // JoinCommand
  145. type JoinCommand struct {
  146. RaftVersion string `json:"raftVersion"`
  147. Name string `json:"name"`
  148. RaftURL string `json:"raftURL"`
  149. EtcdURL string `json:"etcdURL"`
  150. }
  151. func newJoinCommand(version, name, raftUrl, etcdUrl string) *JoinCommand {
  152. return &JoinCommand{
  153. RaftVersion: version,
  154. Name: name,
  155. RaftURL: raftUrl,
  156. EtcdURL: etcdUrl,
  157. }
  158. }
  159. // The name of the join command in the log
  160. func (c *JoinCommand) CommandName() string {
  161. return commandName("join")
  162. }
  163. // Join a server to the cluster
  164. func (c *JoinCommand) Apply(server *raft.Server) (interface{}, error) {
  165. s, _ := server.StateMachine().(*store.Store)
  166. r, _ := server.Context().(*raftServer)
  167. // check if the join command is from a previous machine, who lost all its previous log.
  168. e, _ := s.Get(path.Join("/_etcd/machines", c.Name), false, false, server.CommitIndex(), server.Term())
  169. b := make([]byte, 8)
  170. binary.PutUvarint(b, server.CommitIndex())
  171. if e != nil {
  172. return b, nil
  173. }
  174. // check machine number in the cluster
  175. num := machineNum()
  176. if num == maxClusterSize {
  177. debug("Reject join request from ", c.Name)
  178. return []byte{0}, etcdErr.NewError(etcdErr.EcodeNoMoreMachine, "", server.CommitIndex(), server.Term())
  179. }
  180. addNameToURL(c.Name, c.RaftVersion, c.RaftURL, c.EtcdURL)
  181. // add peer in raft
  182. err := server.AddPeer(c.Name, "")
  183. // add machine in etcd storage
  184. key := path.Join("_etcd/machines", c.Name)
  185. value := fmt.Sprintf("raft=%s&etcd=%s&raftVersion=%s", c.RaftURL, c.EtcdURL, c.RaftVersion)
  186. s.Create(key, value, false, false, store.Permanent, server.CommitIndex(), server.Term())
  187. // add peer stats
  188. if c.Name != r.Name() {
  189. r.followersStats.Followers[c.Name] = &raftFollowerStats{}
  190. r.followersStats.Followers[c.Name].Latency.Minimum = 1 << 63
  191. }
  192. return b, err
  193. }
  194. func (c *JoinCommand) NodeName() string {
  195. return c.Name
  196. }
  197. // RemoveCommand
  198. type RemoveCommand struct {
  199. Name string `json:"name"`
  200. }
  201. // The name of the remove command in the log
  202. func (c *RemoveCommand) CommandName() string {
  203. return commandName("remove")
  204. }
  205. // Remove a server from the cluster
  206. func (c *RemoveCommand) Apply(server *raft.Server) (interface{}, error) {
  207. s, _ := server.StateMachine().(*store.Store)
  208. r, _ := server.Context().(*raftServer)
  209. // remove machine in etcd storage
  210. key := path.Join("_etcd/machines", c.Name)
  211. _, err := s.Delete(key, false, server.CommitIndex(), server.Term())
  212. // delete from stats
  213. delete(r.followersStats.Followers, c.Name)
  214. if err != nil {
  215. return []byte{0}, err
  216. }
  217. // remove peer in raft
  218. err = server.RemovePeer(c.Name)
  219. if err != nil {
  220. return []byte{0}, err
  221. }
  222. if c.Name == server.Name() {
  223. // the removed node is this node
  224. // if the node is not replaying the previous logs
  225. // and the node has sent out a join request in this
  226. // start. It is sure that this node received a new remove
  227. // command and need to be removed
  228. if server.CommitIndex() > r.joinIndex && r.joinIndex != 0 {
  229. debugf("server [%s] is removed", server.Name())
  230. os.Exit(0)
  231. } else {
  232. // else ignore remove
  233. debugf("ignore previous remove command.")
  234. }
  235. }
  236. b := make([]byte, 8)
  237. binary.PutUvarint(b, server.CommitIndex())
  238. return b, err
  239. }