command.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/coreos/etcd/store"
  6. "github.com/coreos/go-raft"
  7. "path"
  8. "time"
  9. )
  10. const commandPrefix = "etcd:"
  11. func commandName(name string) string {
  12. return commandPrefix + name
  13. }
  14. // A command represents an action to be taken on the replicated state machine.
  15. type Command interface {
  16. CommandName() string
  17. Apply(server *raft.Server) (interface{}, error)
  18. }
  19. // Set command
  20. type SetCommand struct {
  21. Key string `json:"key"`
  22. Value string `json:"value"`
  23. ExpireTime time.Time `json:"expireTime"`
  24. }
  25. // The name of the set command in the log
  26. func (c *SetCommand) CommandName() string {
  27. return commandName("set")
  28. }
  29. // Set the key-value pair
  30. func (c *SetCommand) Apply(server *raft.Server) (interface{}, error) {
  31. return etcdStore.Set(c.Key, c.Value, c.ExpireTime, server.CommitIndex())
  32. }
  33. // TestAndSet command
  34. type TestAndSetCommand struct {
  35. Key string `json:"key"`
  36. Value string `json:"value"`
  37. PrevValue string `json: prevValue`
  38. ExpireTime time.Time `json:"expireTime"`
  39. }
  40. // The name of the testAndSet command in the log
  41. func (c *TestAndSetCommand) CommandName() string {
  42. return commandName("testAndSet")
  43. }
  44. // Set the key-value pair if the current value of the key equals to the given prevValue
  45. func (c *TestAndSetCommand) Apply(server *raft.Server) (interface{}, error) {
  46. return etcdStore.TestAndSet(c.Key, c.PrevValue, c.Value, c.ExpireTime, server.CommitIndex())
  47. }
  48. // Get command
  49. type GetCommand struct {
  50. Key string `json:"key"`
  51. }
  52. // The name of the get command in the log
  53. func (c *GetCommand) CommandName() string {
  54. return commandName("get")
  55. }
  56. // Get the value of key
  57. func (c *GetCommand) Apply(server *raft.Server) (interface{}, error) {
  58. return etcdStore.Get(c.Key)
  59. }
  60. // Delete command
  61. type DeleteCommand struct {
  62. Key string `json:"key"`
  63. }
  64. // The name of the delete command in the log
  65. func (c *DeleteCommand) CommandName() string {
  66. return commandName("delete")
  67. }
  68. // Delete the key
  69. func (c *DeleteCommand) Apply(server *raft.Server) (interface{}, error) {
  70. return etcdStore.Delete(c.Key, server.CommitIndex())
  71. }
  72. // Watch command
  73. type WatchCommand struct {
  74. Key string `json:"key"`
  75. SinceIndex uint64 `json:"sinceIndex"`
  76. }
  77. // The name of the watch command in the log
  78. func (c *WatchCommand) CommandName() string {
  79. return commandName("watch")
  80. }
  81. func (c *WatchCommand) Apply(server *raft.Server) (interface{}, error) {
  82. // create a new watcher
  83. watcher := store.NewWatcher()
  84. // add to the watchers list
  85. etcdStore.AddWatcher(c.Key, watcher, c.SinceIndex)
  86. // wait for the notification for any changing
  87. res := <-watcher.C
  88. if res == nil {
  89. return nil, fmt.Errorf("Clearing watch")
  90. }
  91. return json.Marshal(res)
  92. }
  93. // JoinCommand
  94. type JoinCommand struct {
  95. Name string `json:"name"`
  96. RaftURL string `json:"raftURL"`
  97. EtcdURL string `json:"etcdURL"`
  98. }
  99. // The name of the join command in the log
  100. func (c *JoinCommand) CommandName() string {
  101. return commandName("join")
  102. }
  103. // Join a server to the cluster
  104. func (c *JoinCommand) Apply(raftServer *raft.Server) (interface{}, error) {
  105. // check if the join command is from a previous machine, who lost all its previous log.
  106. response, _ := etcdStore.RawGet(path.Join("_etcd/machines", c.Name))
  107. if response != nil {
  108. return []byte("join success"), nil
  109. }
  110. // check machine number in the cluster
  111. num := machineNum()
  112. if num == maxClusterSize {
  113. return []byte("join fail"), fmt.Errorf(errors[103])
  114. }
  115. addNameToURL(c.Name, c.RaftURL, c.EtcdURL)
  116. // add peer in raft
  117. err := raftServer.AddPeer(c.Name)
  118. // add machine in etcd storage
  119. key := path.Join("_etcd/machines", c.Name)
  120. value := fmt.Sprintf("raft=%s&etcd=%s", c.RaftURL, c.EtcdURL)
  121. etcdStore.Set(key, value, time.Unix(0, 0), raftServer.CommitIndex())
  122. return []byte("join success"), err
  123. }
  124. func (c *JoinCommand) NodeName() string {
  125. return c.Name
  126. }