command.go 7.0 KB

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