demote_command.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package server
  2. import (
  3. "fmt"
  4. "github.com/coreos/etcd/log"
  5. "github.com/coreos/etcd/third_party/github.com/coreos/raft"
  6. )
  7. func init() {
  8. raft.RegisterCommand(&DemoteCommand{})
  9. }
  10. // DemoteCommand represents a command to change a peer to a proxy.
  11. type DemoteCommand struct {
  12. Name string `json:"name"`
  13. }
  14. // CommandName returns the name of the command.
  15. func (c *DemoteCommand) CommandName() string {
  16. return "etcd:demote"
  17. }
  18. // Apply executes the command.
  19. func (c *DemoteCommand) Apply(context raft.Context) (interface{}, error) {
  20. ps, _ := context.Server().Context().(*PeerServer)
  21. // Ignore this command if there is no peer.
  22. if !ps.registry.PeerExists(c.Name) {
  23. return nil, fmt.Errorf("peer does not exist: %s", c.Name)
  24. }
  25. // Save URLs.
  26. clientURL, _ := ps.registry.ClientURL(c.Name)
  27. peerURL, _ := ps.registry.PeerURL(c.Name)
  28. // Perform a removal.
  29. (&RemoveCommand{Name: c.Name}).Apply(context)
  30. // Register node as a proxy.
  31. ps.registry.RegisterProxy(c.Name, peerURL, clientURL)
  32. // Update mode if this change applies to this server.
  33. if c.Name == ps.Config.Name {
  34. log.Infof("Set mode after demotion: %s", c.Name)
  35. ps.setMode(ProxyMode)
  36. }
  37. return nil, nil
  38. }
  39. // NodeName returns the name of the affected node.
  40. func (c *DemoteCommand) NodeName() string {
  41. return c.Name
  42. }