command.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. Name string `json:"name"`
  97. RaftURL string `json:"raftURL"`
  98. EtcdURL string `json:"etcdURL"`
  99. }
  100. func newJoinCommand() *JoinCommand {
  101. return &JoinCommand{
  102. Name: r.name,
  103. RaftURL: r.url,
  104. EtcdURL: e.url,
  105. }
  106. }
  107. // The name of the join command in the log
  108. func (c *JoinCommand) CommandName() string {
  109. return commandName("join")
  110. }
  111. // Join a server to the cluster
  112. func (c *JoinCommand) Apply(raftServer *raft.Server) (interface{}, error) {
  113. // check if the join command is from a previous machine, who lost all its previous log.
  114. response, _ := etcdStore.RawGet(path.Join("_etcd/machines", c.Name))
  115. if response != nil {
  116. return []byte("join success"), nil
  117. }
  118. // check machine number in the cluster
  119. num := machineNum()
  120. if num == maxClusterSize {
  121. debug("Reject join request from ", c.Name)
  122. return []byte("join fail"), etcdErr.NewError(103, "")
  123. }
  124. addNameToURL(c.Name, c.RaftURL, c.EtcdURL)
  125. // add peer in raft
  126. err := raftServer.AddPeer(c.Name, "")
  127. // add machine in etcd storage
  128. key := path.Join("_etcd/machines", c.Name)
  129. value := fmt.Sprintf("raft=%s&etcd=%s", c.RaftURL, c.EtcdURL)
  130. etcdStore.Set(key, value, time.Unix(0, 0), raftServer.CommitIndex())
  131. return []byte("join success"), err
  132. }
  133. func (c *JoinCommand) NodeName() string {
  134. return c.Name
  135. }