promote_command.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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(&PromoteCommand{})
  8. }
  9. // PromoteCommand represents a Raft command for converting a proxy to a peer.
  10. type PromoteCommand struct {
  11. Name string `json:"name"`
  12. }
  13. // CommandName returns the name of the command.
  14. func (c *PromoteCommand) CommandName() string {
  15. return "etcd:promote"
  16. }
  17. // Apply promotes a named proxy to a peer.
  18. func (c *PromoteCommand) Apply(context raft.Context) (interface{}, error) {
  19. ps, _ := context.Server().Context().(*PeerServer)
  20. config := ps.ClusterConfig()
  21. // If cluster size is larger than max cluster size then return an error.
  22. if ps.registry.PeerCount() >= config.ActiveSize {
  23. return etcdErr.NewError(etcdErr.EcodePromoteError, "", 0)
  24. }
  25. // If proxy doesn't exist then return an error.
  26. if !ps.registry.ProxyExists(c.Name) {
  27. return etcdErr.NewError(etcdErr.EcodePromoteError, "", 0)
  28. }
  29. // Retrieve proxy settings.
  30. proxyClientURL := ps.registry.ProxyClientURL()
  31. proxyPeerURL := ps.registry.ProxyPeerURL()
  32. // Remove from registry as a proxy.
  33. if err := ps.registry.UnregisterProxy(c.Name); err != nil {
  34. log.Info("Cannot remove proxy: ", c.Name)
  35. return nil, err
  36. }
  37. // Add to shared peer registry.
  38. ps.registry.RegisterPeer(c.Name, c.RaftURL, c.EtcdURL)
  39. // Add peer in raft
  40. err := context.Server().AddPeer(c.Name, "")
  41. // Add peer stats
  42. if c.Name != ps.RaftServer().Name() {
  43. ps.followersStats.Followers[c.Name] = &raftFollowerStats{}
  44. ps.followersStats.Followers[c.Name].Latency.Minimum = 1 << 63
  45. }
  46. return nil, err
  47. }
  48. func (c *JoinCommand) NodeName() string {
  49. return c.Name
  50. }