command.go 4.1 KB

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