command.go 3.5 KB

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