command.go 6.8 KB

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