join_command.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package server
  2. import (
  3. "encoding/binary"
  4. etcdErr "github.com/coreos/etcd/error"
  5. "github.com/coreos/etcd/log"
  6. "github.com/coreos/etcd/third_party/github.com/goraft/raft"
  7. )
  8. func init() {
  9. raft.RegisterCommand(&JoinCommand{})
  10. }
  11. // JoinCommand represents a request to join the cluster.
  12. // The command returns the join_index (Uvarint).
  13. type JoinCommand struct {
  14. MinVersion int `json:"minVersion"`
  15. MaxVersion int `json:"maxVersion"`
  16. Name string `json:"name"`
  17. RaftURL string `json:"raftURL"`
  18. EtcdURL string `json:"etcdURL"`
  19. }
  20. // The name of the join command in the log
  21. func (c *JoinCommand) CommandName() string {
  22. return "etcd:join"
  23. }
  24. // Apply attempts to join a machine to the cluster.
  25. func (c *JoinCommand) Apply(context raft.Context) (interface{}, error) {
  26. index, err := applyJoin(c, context)
  27. if err != nil {
  28. return nil, err
  29. }
  30. b := make([]byte, 8)
  31. binary.PutUvarint(b, index)
  32. return b, nil
  33. }
  34. func (c *JoinCommand) NodeName() string {
  35. return c.Name
  36. }
  37. // applyJoin attempts to join a machine to the cluster.
  38. func applyJoin(c *JoinCommand, context raft.Context) (uint64, error) {
  39. ps, _ := context.Server().Context().(*PeerServer)
  40. ps.raftServer.FlushCommitIndex()
  41. commitIndex := context.CommitIndex()
  42. // Make sure we're not getting a cached value from the registry.
  43. ps.registry.Invalidate(c.Name)
  44. // Check if the join command is from a previous peer, who lost all its previous log.
  45. if peerURL, ok := ps.registry.PeerURL(c.Name); ok {
  46. // If previous node restarts with different peer URL,
  47. // update its information.
  48. if peerURL != c.RaftURL {
  49. log.Infof("Rejoin with %v instead of %v from %v", c.RaftURL, peerURL, c.Name)
  50. if err := updatePeerURL(c, ps); err != nil {
  51. return 0, err
  52. }
  53. }
  54. if c.Name == context.Server().Name() {
  55. ps.removedInLog = false
  56. }
  57. return commitIndex, nil
  58. }
  59. // Check if the join command adds an instance that collides with existing one on peer URL.
  60. peerURLs := ps.registry.PeerURLs(ps.raftServer.Leader(), c.Name)
  61. for _, peerURL := range peerURLs {
  62. if peerURL == c.RaftURL {
  63. log.Warnf("%v tries to join the cluster with existing URL %v", c.Name, c.EtcdURL)
  64. return 0, etcdErr.NewError(etcdErr.EcodeExistingPeerAddr, c.EtcdURL, context.CommitIndex())
  65. }
  66. }
  67. // Check peer number in the cluster
  68. count := ps.registry.Count()
  69. // ClusterConfig doesn't init until first machine is added
  70. if count > 0 && count >= ps.ClusterConfig().ActiveSize {
  71. log.Debug("Reject join request from ", c.Name)
  72. return 0, etcdErr.NewError(etcdErr.EcodeNoMorePeer, "", context.CommitIndex())
  73. }
  74. // Add to shared peer registry.
  75. ps.registry.Register(c.Name, c.RaftURL, c.EtcdURL)
  76. // Add peer in raft
  77. if err := context.Server().AddPeer(c.Name, ""); err != nil {
  78. return 0, err
  79. }
  80. // Add peer stats
  81. if c.Name != ps.RaftServer().Name() {
  82. ps.followersStats.Followers[c.Name] = &raftFollowerStats{}
  83. ps.followersStats.Followers[c.Name].Latency.Minimum = 1 << 63
  84. }
  85. if c.Name == context.Server().Name() {
  86. ps.removedInLog = false
  87. }
  88. return commitIndex, nil
  89. }
  90. func updatePeerURL(c *JoinCommand, ps *PeerServer) error {
  91. log.Debugf("Update peer URL of %v to %v", c.Name, c.RaftURL)
  92. if err := ps.registry.UpdatePeerURL(c.Name, c.RaftURL); err != nil {
  93. log.Debugf("Error while updating in registry: %s (%v)", c.Name, err)
  94. return err
  95. }
  96. // Flush commit index, so raft will replay to here when restart
  97. ps.raftServer.FlushCommitIndex()
  98. return nil
  99. }