join_command.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. commitIndex := context.CommitIndex()
  41. // Make sure we're not getting a cached value from the registry.
  42. ps.registry.Invalidate(c.Name)
  43. // Check if the join command is from a previous peer, who lost all its previous log.
  44. if peerURL, ok := ps.registry.PeerURL(c.Name); ok {
  45. // If previous node restarts with different peer URL,
  46. // update its information.
  47. if peerURL != c.RaftURL {
  48. log.Infof("Rejoin with %v instead of %v from %v", c.RaftURL, peerURL, c.Name)
  49. if err := updatePeerURL(c, ps); err != nil {
  50. return 0, err
  51. }
  52. }
  53. if c.Name == context.Server().Name() {
  54. ps.removedInLog = false
  55. }
  56. return commitIndex, nil
  57. }
  58. // Check if the join command adds an instance that collides with existing one on peer URL.
  59. peerURLs := ps.registry.PeerURLs(ps.raftServer.Leader(), c.Name)
  60. for _, peerURL := range peerURLs {
  61. if peerURL == c.RaftURL {
  62. log.Warnf("%v tries to join the cluster with existing URL %v", c.Name, c.EtcdURL)
  63. return 0, etcdErr.NewError(etcdErr.EcodeExistingPeerAddr, c.EtcdURL, context.CommitIndex())
  64. }
  65. }
  66. // Check peer number in the cluster
  67. count := ps.registry.Count()
  68. // ClusterConfig doesn't init until first machine is added
  69. if count > 0 && count >= ps.ClusterConfig().ActiveSize {
  70. log.Debug("Reject join request from ", c.Name)
  71. return 0, etcdErr.NewError(etcdErr.EcodeNoMorePeer, "", context.CommitIndex())
  72. }
  73. // Add to shared peer registry.
  74. ps.registry.Register(c.Name, c.RaftURL, c.EtcdURL)
  75. // Add peer in raft
  76. if err := context.Server().AddPeer(c.Name, ""); err != nil {
  77. return 0, err
  78. }
  79. // Add peer stats
  80. if c.Name != ps.RaftServer().Name() {
  81. ps.followersStats.Followers[c.Name] = &raftFollowerStats{}
  82. ps.followersStats.Followers[c.Name].Latency.Minimum = 1 << 63
  83. }
  84. if c.Name == context.Server().Name() {
  85. ps.removedInLog = false
  86. }
  87. return commitIndex, nil
  88. }
  89. func updatePeerURL(c *JoinCommand, ps *PeerServer) error {
  90. log.Debugf("Update peer URL of %v to %v", c.Name, c.RaftURL)
  91. if err := ps.registry.UpdatePeerURL(c.Name, c.RaftURL); err != nil {
  92. log.Debugf("Error while updating in registry: %s (%v)", c.Name, err)
  93. return err
  94. }
  95. // Flush commit index, so raft will replay to here when restart
  96. ps.raftServer.FlushCommitIndex()
  97. return nil
  98. }