join_command.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package server
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "github.com/coreos/etcd/log"
  6. "github.com/coreos/etcd/third_party/github.com/coreos/raft"
  7. )
  8. func init() {
  9. raft.RegisterCommand(&JoinCommand{})
  10. }
  11. // The JoinCommand adds a node to the cluster.
  12. //
  13. // The command returns two values back to binary format.
  14. // The first value is a Uvarint representing the the join_index.
  15. // The second value is a single byte flag representing whether the joining
  16. // node is a peer (0) or a proxy (1).
  17. //
  18. // 8 bytes | 1 byte
  19. // join_index | join_mode
  20. //
  21. type JoinCommand struct {
  22. MinVersion int `json:"minVersion"`
  23. MaxVersion int `json:"maxVersion"`
  24. Name string `json:"name"`
  25. RaftURL string `json:"raftURL"`
  26. EtcdURL string `json:"etcdURL"`
  27. }
  28. func NewJoinCommand(minVersion int, maxVersion int, name, raftUrl, etcdUrl string) *JoinCommand {
  29. return &JoinCommand{
  30. MinVersion: minVersion,
  31. MaxVersion: maxVersion,
  32. Name: name,
  33. RaftURL: raftUrl,
  34. EtcdURL: etcdUrl,
  35. }
  36. }
  37. // The name of the join command in the log
  38. func (c *JoinCommand) CommandName() string {
  39. return "etcd:join"
  40. }
  41. // Join a server to the cluster
  42. func (c *JoinCommand) Apply(context raft.Context) (interface{}, error) {
  43. ps, _ := context.Server().Context().(*PeerServer)
  44. var buf bytes.Buffer
  45. b := make([]byte, 8)
  46. n := binary.PutUvarint(b, context.CommitIndex())
  47. buf.Write(b[:n])
  48. // Make sure we're not getting a cached value from the registry.
  49. ps.registry.Invalidate(c.Name)
  50. // Check if the join command is from a previous peer, who lost all its previous log.
  51. if _, ok := ps.registry.ClientURL(c.Name); ok {
  52. binary.Write(&buf, binary.BigEndian, uint8(0)) // Mark as peer.
  53. return buf.Bytes(), nil
  54. }
  55. // Check peer number in the cluster
  56. if ps.registry.PeerCount() >= ps.ClusterConfig().ActiveSize {
  57. log.Debug("Join as proxy ", c.Name)
  58. ps.registry.RegisterProxy(c.Name, c.RaftURL, c.EtcdURL)
  59. binary.Write(&buf, binary.BigEndian, uint8(1)) // Mark as proxy.
  60. return buf.Bytes(), nil
  61. }
  62. // Remove it as a proxy if it is one.
  63. if ps.registry.ProxyExists(c.Name) {
  64. ps.registry.UnregisterProxy(c.Name)
  65. }
  66. // Add to shared peer registry.
  67. ps.registry.RegisterPeer(c.Name, c.RaftURL, c.EtcdURL)
  68. // Add peer in raft
  69. err := context.Server().AddPeer(c.Name, "")
  70. // Add peer stats
  71. if c.Name != ps.RaftServer().Name() {
  72. ps.followersStats.Followers[c.Name] = &raftFollowerStats{}
  73. ps.followersStats.Followers[c.Name].Latency.Minimum = 1 << 63
  74. }
  75. binary.Write(&buf, binary.BigEndian, uint8(0)) // Mark as peer.
  76. return buf.Bytes(), err
  77. }
  78. func (c *JoinCommand) NodeName() string {
  79. return c.Name
  80. }