join_command.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // +build ignore
  2. package server
  3. import (
  4. "encoding/binary"
  5. etcdErr "github.com/coreos/etcd/error"
  6. "github.com/coreos/etcd/log"
  7. "github.com/coreos/etcd/third_party/github.com/goraft/raft"
  8. )
  9. func init() {
  10. raft.RegisterCommand(&JoinCommand{})
  11. }
  12. // JoinCommand represents a request to join the cluster.
  13. // The command returns the join_index (Uvarint).
  14. type JoinCommand struct {
  15. MinVersion int `json:"minVersion"`
  16. MaxVersion int `json:"maxVersion"`
  17. Name string `json:"name"`
  18. RaftURL string `json:"raftURL"`
  19. EtcdURL string `json:"etcdURL"`
  20. }
  21. // The name of the join command in the log
  22. func (c *JoinCommand) CommandName() string {
  23. return "etcd:join"
  24. }
  25. // Apply attempts to join a machine to the cluster.
  26. func (c *JoinCommand) Apply(context raft.Context) (interface{}, error) {
  27. index, err := applyJoin(c, context)
  28. if err != nil {
  29. return nil, err
  30. }
  31. b := make([]byte, 8)
  32. binary.PutUvarint(b, index)
  33. return b, nil
  34. }
  35. func (c *JoinCommand) NodeName() string {
  36. return c.Name
  37. }
  38. // applyJoin attempts to join a machine to the cluster.
  39. func applyJoin(c *JoinCommand, context raft.Context) (uint64, error) {
  40. ps, _ := context.Server().Context().(*PeerServer)
  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. }