command.go 3.1 KB

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