command.go 6.9 KB

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