command.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package main
  2. import (
  3. "encoding/json"
  4. //"errors"
  5. "github.com/coreos/etcd/store"
  6. "github.com/coreos/go-raft"
  7. "time"
  8. )
  9. // A command represents an action to be taken on the replicated state machine.
  10. type Command interface {
  11. CommandName() string
  12. Apply(server *raft.Server) (interface{}, error)
  13. }
  14. // Set command
  15. type SetCommand struct {
  16. Key string `json:"key"`
  17. Value string `json:"value"`
  18. ExpireTime time.Time `json:"expireTime"`
  19. }
  20. // The name of the set command in the log
  21. func (c *SetCommand) CommandName() string {
  22. return "set"
  23. }
  24. // Set the key-value pair
  25. func (c *SetCommand) Apply(server *raft.Server) (interface{}, error) {
  26. return etcdStore.Set(c.Key, c.Value, c.ExpireTime, server.CommitIndex())
  27. }
  28. // TestAndSet command
  29. type TestAndSetCommand struct {
  30. Key string `json:"key"`
  31. Value string `json:"value"`
  32. PrevValue string `json: prevValue`
  33. ExpireTime time.Time `json:"expireTime"`
  34. }
  35. // The name of the testAndSet command in the log
  36. func (c *TestAndSetCommand) CommandName() string {
  37. return "testAndSet"
  38. }
  39. // Set the key-value pair if the current value of the key equals to the given prevValue
  40. func (c *TestAndSetCommand) Apply(server *raft.Server) (interface{}, error) {
  41. return etcdStore.TestAndSet(c.Key, c.PrevValue, c.Value, c.ExpireTime, server.CommitIndex())
  42. }
  43. // Get command
  44. type GetCommand struct {
  45. Key string `json:"key"`
  46. }
  47. // The name of the get command in the log
  48. func (c *GetCommand) CommandName() string {
  49. return "get"
  50. }
  51. // Get the value of key
  52. func (c *GetCommand) Apply(server *raft.Server) (interface{}, error) {
  53. return etcdStore.Get(c.Key)
  54. }
  55. // Delete command
  56. type DeleteCommand struct {
  57. Key string `json:"key"`
  58. }
  59. // The name of the delete command in the log
  60. func (c *DeleteCommand) CommandName() string {
  61. return "delete"
  62. }
  63. // Delete the key
  64. func (c *DeleteCommand) Apply(server *raft.Server) (interface{}, error) {
  65. return etcdStore.Delete(c.Key, server.CommitIndex())
  66. }
  67. // Watch command
  68. type WatchCommand struct {
  69. Key string `json:"key"`
  70. SinceIndex uint64 `json:"sinceIndex"`
  71. }
  72. // The name of the watch command in the log
  73. func (c *WatchCommand) CommandName() string {
  74. return "watch"
  75. }
  76. func (c *WatchCommand) Apply(server *raft.Server) (interface{}, error) {
  77. // create a new watcher
  78. watcher := store.CreateWatcher()
  79. // add to the watchers list
  80. etcdStore.AddWatcher(c.Key, watcher, c.SinceIndex)
  81. // wait for the notification for any changing
  82. res := <-watcher.C
  83. return json.Marshal(res)
  84. }
  85. // JoinCommand
  86. type JoinCommand struct {
  87. Name string `json:"name"`
  88. Hostname string `json:"hostName"`
  89. RaftPort int `json:"raftPort"`
  90. ClientPort int `json:"clientPort"`
  91. }
  92. // The name of the join command in the log
  93. func (c *JoinCommand) CommandName() string {
  94. return "join"
  95. }
  96. // Join a server to the cluster
  97. func (c *JoinCommand) Apply(raftServer *raft.Server) (interface{}, error) {
  98. err := raftServer.AddPeer(c.Name)
  99. addMachine(c.Name, c.Hostname, c.RaftPort, c.ClientPort)
  100. return []byte("join success"), err
  101. }