join_command.go 2.4 KB

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