command.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. // 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. // check if the join command is from a previous machine, who lost all its previous log.
  118. response, _ := etcdStore.RawGet(path.Join("_etcd/machines", c.Name))
  119. b := make([]byte, 8)
  120. binary.PutUvarint(b, raftServer.CommitIndex())
  121. if response != nil {
  122. return b, nil
  123. }
  124. // check machine number in the cluster
  125. num := machineNum()
  126. if num == maxClusterSize {
  127. debug("Reject join request from ", c.Name)
  128. return []byte{0}, etcdErr.NewError(103, "")
  129. }
  130. addNameToURL(c.Name, c.RaftVersion, c.RaftURL, c.EtcdURL)
  131. // add peer in raft
  132. err := raftServer.AddPeer(c.Name, "")
  133. // add machine in etcd storage
  134. key := path.Join("_etcd/machines", c.Name)
  135. value := fmt.Sprintf("raft=%s&etcd=%s&raftVersion=%s", c.RaftURL, c.EtcdURL, c.RaftVersion)
  136. etcdStore.Set(key, value, time.Unix(0, 0), raftServer.CommitIndex())
  137. if c.Name != r.Name() {
  138. r.peersStats[c.Name] = &raftPeerStats{MinLatency: 1 << 63}
  139. }
  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 commandName("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. delete(r.peersStats, c.Name)
  159. if err != nil {
  160. return []byte{0}, err
  161. }
  162. // remove peer in raft
  163. err = raftServer.RemovePeer(c.Name)
  164. if err != nil {
  165. return []byte{0}, err
  166. }
  167. if c.Name == raftServer.Name() {
  168. // the removed node is this node
  169. // if the node is not replaying the previous logs
  170. // and the node has sent out a join request in this
  171. // start. It is sure that this node received a new remove
  172. // command and need to be removed
  173. if raftServer.CommitIndex() > r.joinIndex && r.joinIndex != 0 {
  174. debugf("server [%s] is removed", raftServer.Name())
  175. os.Exit(0)
  176. } else {
  177. // else ignore remove
  178. debugf("ignore previous remove command.")
  179. }
  180. }
  181. b := make([]byte, 8)
  182. binary.PutUvarint(b, raftServer.CommitIndex())
  183. return b, err
  184. }