demote_command.go 1.1 KB

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