command.go 3.7 KB

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