command.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package main
  2. import (
  3. "encoding/binary"
  4. "encoding/json"
  5. "fmt"
  6. etcdErr "github.com/coreos/etcd/error"
  7. "github.com/coreos/etcd/store"
  8. "github.com/coreos/go-raft"
  9. "os"
  10. "path"
  11. "time"
  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. // Set command
  23. type SetCommand struct {
  24. Key string `json:"key"`
  25. Value string `json:"value"`
  26. ExpireTime time.Time `json:"expireTime"`
  27. }
  28. // The name of the set command in the log
  29. func (c *SetCommand) CommandName() string {
  30. return commandName("set")
  31. }
  32. // Set the key-value pair
  33. func (c *SetCommand) Apply(server *raft.Server) (interface{}, error) {
  34. return etcdStore.Set(c.Key, c.Value, c.ExpireTime, server.CommitIndex())
  35. }
  36. // TestAndSet command
  37. type TestAndSetCommand struct {
  38. Key string `json:"key"`
  39. Value string `json:"value"`
  40. PrevValue string `json: prevValue`
  41. ExpireTime time.Time `json:"expireTime"`
  42. }
  43. // The name of the testAndSet command in the log
  44. func (c *TestAndSetCommand) CommandName() string {
  45. return commandName("testAndSet")
  46. }
  47. // Set the key-value pair if the current value of the key equals to the given prevValue
  48. func (c *TestAndSetCommand) Apply(server *raft.Server) (interface{}, error) {
  49. return etcdStore.TestAndSet(c.Key, c.PrevValue, c.Value, c.ExpireTime, server.CommitIndex())
  50. }
  51. // Get command
  52. type GetCommand struct {
  53. Key string `json:"key"`
  54. }
  55. // The name of the get command in the log
  56. func (c *GetCommand) CommandName() string {
  57. return commandName("get")
  58. }
  59. // Get the value of key
  60. func (c *GetCommand) Apply(server *raft.Server) (interface{}, error) {
  61. return etcdStore.Get(c.Key)
  62. }
  63. // Delete command
  64. type DeleteCommand struct {
  65. Key string `json:"key"`
  66. }
  67. // The name of the delete command in the log
  68. func (c *DeleteCommand) CommandName() string {
  69. return commandName("delete")
  70. }
  71. // Delete the key
  72. func (c *DeleteCommand) Apply(server *raft.Server) (interface{}, error) {
  73. return etcdStore.Delete(c.Key, server.CommitIndex())
  74. }
  75. // Watch command
  76. type WatchCommand struct {
  77. Key string `json:"key"`
  78. SinceIndex uint64 `json:"sinceIndex"`
  79. }
  80. // The name of the watch command in the log
  81. func (c *WatchCommand) CommandName() string {
  82. return commandName("watch")
  83. }
  84. func (c *WatchCommand) Apply(server *raft.Server) (interface{}, error) {
  85. // create a new watcher
  86. watcher := store.NewWatcher()
  87. // add to the watchers list
  88. etcdStore.AddWatcher(c.Key, watcher, c.SinceIndex)
  89. // wait for the notification for any changing
  90. res := <-watcher.C
  91. if res == nil {
  92. return nil, fmt.Errorf("Clearing watch")
  93. }
  94. return json.Marshal(res)
  95. }
  96. // JoinCommand
  97. type JoinCommand struct {
  98. RaftVersion string `json:"raftVersion"`
  99. Name string `json:"name"`
  100. RaftURL string `json:"raftURL"`
  101. EtcdURL string `json:"etcdURL"`
  102. }
  103. func newJoinCommand() *JoinCommand {
  104. return &JoinCommand{
  105. RaftVersion: r.version,
  106. Name: r.name,
  107. RaftURL: r.url,
  108. EtcdURL: e.url,
  109. }
  110. }
  111. // The name of the join command in the log
  112. func (c *JoinCommand) CommandName() string {
  113. return commandName("join")
  114. }
  115. // Join a server to the cluster
  116. func (c *JoinCommand) Apply(raftServer *raft.Server) (interface{}, error) {
  117. if c.Name == r.Name() {
  118. r.pendingJoin = false
  119. }
  120. // check if the join command is from a previous machine, who lost all its previous log.
  121. response, _ := etcdStore.RawGet(path.Join("_etcd/machines", c.Name))
  122. b := make([]byte, 8)
  123. binary.PutUvarint(b, raftServer.CommitIndex())
  124. if response != nil {
  125. return b, nil
  126. }
  127. // check machine number in the cluster
  128. num := machineNum()
  129. if num == maxClusterSize {
  130. debug("Reject join request from ", c.Name)
  131. return []byte{0}, etcdErr.NewError(103, "")
  132. }
  133. addNameToURL(c.Name, c.RaftVersion, c.RaftURL, c.EtcdURL)
  134. // add peer in raft
  135. err := raftServer.AddPeer(c.Name, "")
  136. // add machine in etcd storage
  137. key := path.Join("_etcd/machines", c.Name)
  138. value := fmt.Sprintf("raft=%s&etcd=%s&raftVersion=%s", c.RaftURL, c.EtcdURL, c.RaftVersion)
  139. etcdStore.Set(key, value, time.Unix(0, 0), raftServer.CommitIndex())
  140. return b, err
  141. }
  142. func (c *JoinCommand) NodeName() string {
  143. return c.Name
  144. }
  145. // RemoveCommand
  146. type RemoveCommand struct {
  147. Name string `json:"name"`
  148. }
  149. // The name of the remove command in the log
  150. func (c *RemoveCommand) CommandName() string {
  151. return "etcd:remove"
  152. }
  153. // Remove a server from the cluster
  154. func (c *RemoveCommand) Apply(raftServer *raft.Server) (interface{}, error) {
  155. // remove machine in etcd storage
  156. key := path.Join("_etcd/machines", c.Name)
  157. _, err := etcdStore.Delete(key, raftServer.CommitIndex())
  158. if err != nil {
  159. return []byte{0}, err
  160. }
  161. // remove peer in raft
  162. err = raftServer.RemovePeer(c.Name)
  163. if err != nil {
  164. return []byte{0}, err
  165. }
  166. if c.Name == raftServer.Name() {
  167. // the removed node is this node
  168. // if the node is not replaying the previous logs
  169. // and the node has sent out a join request in this
  170. // start. It is sure that this node received a new remove
  171. // command and need to be removed
  172. if raftServer.CommitIndex() > r.joinIndex && r.joinIndex != 0 {
  173. debugf("server [%s] is removed", raftServer.Name())
  174. os.Exit(0)
  175. } else {
  176. // the node is replaying previous logs and there is a join command
  177. // afterwards, we should not exit
  178. if r.joinIndex == 0 {
  179. // if the node has not sent a join command in this start
  180. // it will need to send a join command after reply the logs
  181. r.pendingJoin = true
  182. } else {
  183. // else ignore remove
  184. debugf("ignore previous remove command.")
  185. }
  186. }
  187. }
  188. b := make([]byte, 8)
  189. binary.PutUvarint(b, raftServer.CommitIndex())
  190. return b, err
  191. }