command.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package main
  2. //------------------------------------------------------------------------------
  3. //
  4. // Commands
  5. //
  6. //------------------------------------------------------------------------------
  7. import (
  8. "github.com/benbjohnson/go-raft"
  9. "encoding/json"
  10. "time"
  11. "github.com/xiangli-cmu/raft-etcd/store"
  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) ([]byte, error)
  17. GeneratePath() string // Gererate a path for http request
  18. Type() string // http request type
  19. GetValue() string
  20. GetKey() string
  21. Sensitive() bool // Sensitive to the stateMachine
  22. }
  23. // Set command
  24. type SetCommand struct {
  25. Key string `json:"key"`
  26. Value string `json:"value"`
  27. ExpireTime time.Time `json:"expireTime"`
  28. }
  29. // The name of the command in the log
  30. func (c *SetCommand) CommandName() string {
  31. return "set"
  32. }
  33. // Set the value of key to value
  34. func (c *SetCommand) Apply(server *raft.Server) ([]byte, error) {
  35. return store.Set(c.Key, c.Value, c.ExpireTime)
  36. }
  37. // Get the path for http request
  38. func (c *SetCommand) GeneratePath() string {
  39. return "set/" + c.Key
  40. }
  41. // Get the type for http request
  42. func (c *SetCommand) Type() string {
  43. return "POST"
  44. }
  45. func (c *SetCommand) GetValue() string {
  46. return c.Value
  47. }
  48. func (c *SetCommand) GetKey() string {
  49. return c.Key
  50. }
  51. func (c *SetCommand) Sensitive() bool {
  52. return true
  53. }
  54. // Get command
  55. type GetCommand struct {
  56. Key string `json:"key"`
  57. }
  58. // The name of the command in the log
  59. func (c *GetCommand) CommandName() string {
  60. return "get"
  61. }
  62. // Set the value of key to value
  63. func (c *GetCommand) Apply(server *raft.Server) ([]byte, error){
  64. res := store.Get(c.Key)
  65. return json.Marshal(res)
  66. }
  67. func (c *GetCommand) GeneratePath() string{
  68. return "get/" + c.Key
  69. }
  70. func (c *GetCommand) Type() string{
  71. return "GET"
  72. }
  73. func (c *GetCommand) GetValue() string{
  74. return ""
  75. }
  76. func (c *GetCommand) GetKey() string{
  77. return c.Key
  78. }
  79. func (c *GetCommand) Sensitive() bool {
  80. return false
  81. }
  82. // Delete command
  83. type DeleteCommand struct {
  84. Key string `json:"key"`
  85. }
  86. // The name of the command in the log
  87. func (c *DeleteCommand) CommandName() string {
  88. return "delete"
  89. }
  90. // Delete the key
  91. func (c *DeleteCommand) Apply(server *raft.Server) ([]byte, error){
  92. return store.Delete(c.Key)
  93. }
  94. func (c *DeleteCommand) GeneratePath() string{
  95. return "delete/" + c.Key
  96. }
  97. func (c *DeleteCommand) Type() string{
  98. return "GET"
  99. }
  100. func (c *DeleteCommand) GetValue() string{
  101. return ""
  102. }
  103. func (c *DeleteCommand) GetKey() string{
  104. return c.Key
  105. }
  106. func (c *DeleteCommand) Sensitive() bool {
  107. return true
  108. }
  109. // Watch command
  110. type WatchCommand struct {
  111. Key string `json:"key"`
  112. }
  113. //The name of the command in the log
  114. func (c *WatchCommand) CommandName() string {
  115. return "watch"
  116. }
  117. func (c *WatchCommand) Apply(server *raft.Server) ([]byte, error){
  118. ch := make(chan store.Response)
  119. // add to the watchers list
  120. store.AddWatcher(c.Key, ch)
  121. // wait for the notification for any changing
  122. res := <- ch
  123. return json.Marshal(res)
  124. }
  125. func (c *WatchCommand) GeneratePath() string{
  126. return "watch/" + c.Key
  127. }
  128. func (c *WatchCommand) Type() string{
  129. return "GET"
  130. }
  131. func (c *WatchCommand) GetValue() string{
  132. return ""
  133. }
  134. func (c *WatchCommand) GetKey() string{
  135. return c.Key
  136. }
  137. func (c *WatchCommand) Sensitive() bool {
  138. return false
  139. }
  140. // JoinCommand
  141. type JoinCommand struct {
  142. Name string `json:"name"`
  143. }
  144. func (c *JoinCommand) CommandName() string {
  145. return "join"
  146. }
  147. func (c *JoinCommand) Apply(server *raft.Server) ([]byte, error) {
  148. err := server.AddPeer(c.Name)
  149. // no result will be returned
  150. return nil, err
  151. }