commands.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package raft
  2. import (
  3. "io"
  4. )
  5. // Join command interface
  6. type JoinCommand interface {
  7. Command
  8. NodeName() string
  9. }
  10. // Join command
  11. type DefaultJoinCommand struct {
  12. Name string `json:"name"`
  13. ConnectionString string `json:"connectionString"`
  14. }
  15. // Leave command interface
  16. type LeaveCommand interface {
  17. Command
  18. NodeName() string
  19. }
  20. // Leave command
  21. type DefaultLeaveCommand struct {
  22. Name string `json:"name"`
  23. }
  24. // NOP command
  25. type NOPCommand struct {
  26. }
  27. // The name of the Join command in the log
  28. func (c *DefaultJoinCommand) CommandName() string {
  29. return "raft:join"
  30. }
  31. func (c *DefaultJoinCommand) Apply(server Server) (interface{}, error) {
  32. err := server.AddPeer(c.Name, c.ConnectionString)
  33. return []byte("join"), err
  34. }
  35. func (c *DefaultJoinCommand) NodeName() string {
  36. return c.Name
  37. }
  38. // The name of the Leave command in the log
  39. func (c *DefaultLeaveCommand) CommandName() string {
  40. return "raft:leave"
  41. }
  42. func (c *DefaultLeaveCommand) Apply(server Server) (interface{}, error) {
  43. err := server.RemovePeer(c.Name)
  44. return []byte("leave"), err
  45. }
  46. func (c *DefaultLeaveCommand) NodeName() string {
  47. return c.Name
  48. }
  49. // The name of the NOP command in the log
  50. func (c NOPCommand) CommandName() string {
  51. return "raft:nop"
  52. }
  53. func (c NOPCommand) Apply(server Server) (interface{}, error) {
  54. return nil, nil
  55. }
  56. func (c NOPCommand) Encode(w io.Writer) error {
  57. return nil
  58. }
  59. func (c NOPCommand) Decode(r io.Reader) error {
  60. return nil
  61. }