command.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. // 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. }