command.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package main
  2. //------------------------------------------------------------------------------
  3. //
  4. // Commands
  5. //
  6. //------------------------------------------------------------------------------
  7. import (
  8. "github.com/benbjohnson/go-raft"
  9. "encoding/json"
  10. "time"
  11. )
  12. // A command represents an action to be taken on the replicated state machine.
  13. type Command interface {
  14. CommandName() string
  15. Apply(server *raft.Server) ([]byte, error)
  16. GeneratePath() string // Gererate a path for http request
  17. Type() string // http request type
  18. GetValue() string
  19. GetKey() string
  20. Sensitive() bool // Sensitive to the stateMachine
  21. }
  22. // Set command
  23. type SetCommand struct {
  24. Key string `json:"key"`
  25. Value string `json:"value"`
  26. ExpireTime time.Time `json:"expireTime"`
  27. }
  28. // The name of the command in the log
  29. func (c *SetCommand) CommandName() string {
  30. return "set"
  31. }
  32. // Set the value of key to value
  33. func (c *SetCommand) Apply(server *raft.Server) ([]byte, error) {
  34. res := s.Set(c.Key, c.Value, c.ExpireTime)
  35. return json.Marshal(res)
  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 := s.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. res := s.Delete(c.Key)
  93. return json.Marshal(res)
  94. }
  95. func (c *DeleteCommand) GeneratePath() string{
  96. return "delete/" + c.Key
  97. }
  98. func (c *DeleteCommand) Type() string{
  99. return "GET"
  100. }
  101. func (c *DeleteCommand) GetValue() string{
  102. return ""
  103. }
  104. func (c *DeleteCommand) GetKey() string{
  105. return c.Key
  106. }
  107. func (c *DeleteCommand) Sensitive() bool {
  108. return true
  109. }
  110. // Watch command
  111. type WatchCommand struct {
  112. Key string `json:"key"`
  113. }
  114. //The name of the command in the log
  115. func (c *WatchCommand) CommandName() string {
  116. return "watch"
  117. }
  118. func (c *WatchCommand) Apply(server *raft.Server) ([]byte, error){
  119. ch := make(chan Response)
  120. // add to the watchers list
  121. w.add(c.Key, ch)
  122. // wait for the notification for any changing
  123. res := <- ch
  124. return json.Marshal(res)
  125. }
  126. func (c *WatchCommand) GeneratePath() string{
  127. return "watch/" + c.Key
  128. }
  129. func (c *WatchCommand) Type() string{
  130. return "GET"
  131. }
  132. func (c *WatchCommand) GetValue() string{
  133. return ""
  134. }
  135. func (c *WatchCommand) GetKey() string{
  136. return c.Key
  137. }
  138. func (c *WatchCommand) Sensitive() bool {
  139. return false
  140. }
  141. // JoinCommand
  142. type JoinCommand struct {
  143. Name string `json:"name"`
  144. }
  145. func (c *JoinCommand) CommandName() string {
  146. return "join"
  147. }
  148. func (c *JoinCommand) Apply(server *raft.Server) ([]byte, error) {
  149. err := server.AddPeer(c.Name)
  150. // no result will be returned
  151. return nil, err
  152. }