command.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. e, err := etcdStore.Create(c.Key, c.Value, c.IncrementalSuffix, c.Force, c.ExpireTime, server.CommitIndex(), server.Term())
  36. if err != nil {
  37. debug(err)
  38. return nil, err
  39. }
  40. return e, nil
  41. }
  42. // Update command
  43. type UpdateCommand struct {
  44. Key string `json:"key"`
  45. Value string `json:"value"`
  46. ExpireTime time.Time `json:"expireTime"`
  47. }
  48. // The name of the update command in the log
  49. func (c *UpdateCommand) CommandName() string {
  50. return commandName("update")
  51. }
  52. // Update node
  53. func (c *UpdateCommand) Apply(server *raft.Server) (interface{}, error) {
  54. e, err := etcdStore.Update(c.Key, c.Value, c.ExpireTime, server.CommitIndex(), server.Term())
  55. if err != nil {
  56. debug(err)
  57. return nil, err
  58. }
  59. return e, nil
  60. }
  61. // TestAndSet command
  62. type TestAndSetCommand struct {
  63. Key string `json:"key"`
  64. Value string `json:"value"`
  65. ExpireTime time.Time `json:"expireTime"`
  66. PrevValue string `json: prevValue`
  67. PrevIndex uint64 `json: prevValue`
  68. }
  69. // The name of the testAndSet command in the log
  70. func (c *TestAndSetCommand) CommandName() string {
  71. return commandName("testAndSet")
  72. }
  73. // Set the key-value pair if the current value of the key equals to the given prevValue
  74. func (c *TestAndSetCommand) Apply(server *raft.Server) (interface{}, error) {
  75. e, err := etcdStore.TestAndSet(c.Key, c.PrevValue, c.PrevIndex,
  76. c.Value, c.ExpireTime, server.CommitIndex(), server.Term())
  77. if err != nil {
  78. debug(err)
  79. return nil, err
  80. }
  81. return e, nil
  82. }
  83. // Get command
  84. type GetCommand struct {
  85. Key string `json:"key"`
  86. Recursive bool `json:"recursive"`
  87. Sorted bool `json:"sorted"`
  88. }
  89. // The name of the get command in the log
  90. func (c *GetCommand) CommandName() string {
  91. return commandName("get")
  92. }
  93. // Get the value of key
  94. func (c *GetCommand) Apply(server *raft.Server) (interface{}, error) {
  95. e, err := etcdStore.Get(c.Key, c.Recursive, c.Sorted, server.CommitIndex(), server.Term())
  96. if err != nil {
  97. debug(err)
  98. return nil, err
  99. }
  100. return e, nil
  101. }
  102. // Delete command
  103. type DeleteCommand struct {
  104. Key string `json:"key"`
  105. Recursive bool `json:"recursive"`
  106. }
  107. // The name of the delete command in the log
  108. func (c *DeleteCommand) CommandName() string {
  109. return commandName("delete")
  110. }
  111. // Delete the key
  112. func (c *DeleteCommand) Apply(server *raft.Server) (interface{}, error) {
  113. e, err := etcdStore.Delete(c.Key, c.Recursive, server.CommitIndex(), server.Term())
  114. if err != nil {
  115. debug(err)
  116. return nil, err
  117. }
  118. return e, nil
  119. }
  120. // Watch command
  121. type WatchCommand struct {
  122. Key string `json:"key"`
  123. SinceIndex uint64 `json:"sinceIndex"`
  124. Recursive bool `json:"recursive"`
  125. }
  126. // The name of the watch command in the log
  127. func (c *WatchCommand) CommandName() string {
  128. return commandName("watch")
  129. }
  130. func (c *WatchCommand) Apply(server *raft.Server) (interface{}, error) {
  131. eventChan, err := etcdStore.Watch(c.Key, c.Recursive, c.SinceIndex, server.CommitIndex(), server.Term())
  132. if err != nil {
  133. return nil, err
  134. }
  135. e := <-eventChan
  136. return e, nil
  137. }
  138. // JoinCommand
  139. type JoinCommand struct {
  140. RaftVersion string `json:"raftVersion"`
  141. Name string `json:"name"`
  142. RaftURL string `json:"raftURL"`
  143. EtcdURL string `json:"etcdURL"`
  144. }
  145. func newJoinCommand() *JoinCommand {
  146. return &JoinCommand{
  147. RaftVersion: r.version,
  148. Name: r.name,
  149. RaftURL: r.url,
  150. EtcdURL: e.url,
  151. }
  152. }
  153. // The name of the join command in the log
  154. func (c *JoinCommand) CommandName() string {
  155. return commandName("join")
  156. }
  157. // Join a server to the cluster
  158. func (c *JoinCommand) Apply(raftServer *raft.Server) (interface{}, error) {
  159. // check if the join command is from a previous machine, who lost all its previous log.
  160. e, _ := etcdStore.Get(path.Join("/_etcd/machines", c.Name), false, false, raftServer.CommitIndex(), raftServer.Term())
  161. b := make([]byte, 8)
  162. binary.PutUvarint(b, raftServer.CommitIndex())
  163. if e != nil {
  164. return b, nil
  165. }
  166. // check machine number in the cluster
  167. num := machineNum()
  168. if num == maxClusterSize {
  169. debug("Reject join request from ", c.Name)
  170. return []byte{0}, etcdErr.NewError(etcdErr.EcodeNoMoreMachine, "", raftServer.CommitIndex(), raftServer.Term())
  171. }
  172. addNameToURL(c.Name, c.RaftVersion, c.RaftURL, c.EtcdURL)
  173. // add peer in raft
  174. err := raftServer.AddPeer(c.Name, "")
  175. // add machine in etcd storage
  176. key := path.Join("_etcd/machines", c.Name)
  177. value := fmt.Sprintf("raft=%s&etcd=%s&raftVersion=%s", c.RaftURL, c.EtcdURL, c.RaftVersion)
  178. etcdStore.Create(key, value, false, false, store.Permanent, raftServer.CommitIndex(), raftServer.Term())
  179. // add peer stats
  180. if c.Name != r.Name() {
  181. r.followersStats.Followers[c.Name] = &raftFollowerStats{}
  182. r.followersStats.Followers[c.Name].Latency.Minimum = 1 << 63
  183. }
  184. return b, err
  185. }
  186. func (c *JoinCommand) NodeName() string {
  187. return c.Name
  188. }
  189. // RemoveCommand
  190. type RemoveCommand struct {
  191. Name string `json:"name"`
  192. }
  193. // The name of the remove command in the log
  194. func (c *RemoveCommand) CommandName() string {
  195. return commandName("remove")
  196. }
  197. // Remove a server from the cluster
  198. func (c *RemoveCommand) Apply(raftServer *raft.Server) (interface{}, error) {
  199. // remove machine in etcd storage
  200. key := path.Join("_etcd/machines", c.Name)
  201. _, err := etcdStore.Delete(key, false, raftServer.CommitIndex(), raftServer.Term())
  202. // delete from stats
  203. delete(r.followersStats.Followers, c.Name)
  204. if err != nil {
  205. return []byte{0}, err
  206. }
  207. // remove peer in raft
  208. err = raftServer.RemovePeer(c.Name)
  209. if err != nil {
  210. return []byte{0}, err
  211. }
  212. if c.Name == raftServer.Name() {
  213. // the removed node is this node
  214. // if the node is not replaying the previous logs
  215. // and the node has sent out a join request in this
  216. // start. It is sure that this node received a new remove
  217. // command and need to be removed
  218. if raftServer.CommitIndex() > r.joinIndex && r.joinIndex != 0 {
  219. debugf("server [%s] is removed", raftServer.Name())
  220. os.Exit(0)
  221. } else {
  222. // else ignore remove
  223. debugf("ignore previous remove command.")
  224. }
  225. }
  226. b := make([]byte, 8)
  227. binary.PutUvarint(b, raftServer.CommitIndex())
  228. return b, err
  229. }