command.go 3.1 KB

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