| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- package server
- import (
- "bytes"
- "encoding/binary"
- "github.com/coreos/etcd/log"
- "github.com/coreos/etcd/third_party/github.com/coreos/raft"
- )
- func init() {
- raft.RegisterCommand(&JoinCommand{})
- }
- // The JoinCommand adds a node to the cluster.
- //
- // The command returns two values back to binary format.
- // The first value is a Uvarint representing the the join_index.
- // The second value is a single byte flag representing whether the joining
- // node is a peer (0) or a proxy (1).
- //
- // 8 bytes | 1 byte
- // join_index | join_mode
- //
- type JoinCommand struct {
- MinVersion int `json:"minVersion"`
- MaxVersion int `json:"maxVersion"`
- Name string `json:"name"`
- RaftURL string `json:"raftURL"`
- EtcdURL string `json:"etcdURL"`
- }
- func NewJoinCommand(minVersion int, maxVersion int, name, raftUrl, etcdUrl string) *JoinCommand {
- return &JoinCommand{
- MinVersion: minVersion,
- MaxVersion: maxVersion,
- Name: name,
- RaftURL: raftUrl,
- EtcdURL: etcdUrl,
- }
- }
- // The name of the join command in the log
- func (c *JoinCommand) CommandName() string {
- return "etcd:join"
- }
- // Join a server to the cluster
- func (c *JoinCommand) Apply(context raft.Context) (interface{}, error) {
- ps, _ := context.Server().Context().(*PeerServer)
- var buf bytes.Buffer
- b := make([]byte, 8)
- n := binary.PutUvarint(b, context.CommitIndex())
- buf.Write(b[:n])
- // Make sure we're not getting a cached value from the registry.
- ps.registry.Invalidate(c.Name)
- // Check if the join command is from a previous peer, who lost all its previous log.
- if _, ok := ps.registry.ClientURL(c.Name); ok {
- binary.Write(&buf, binary.BigEndian, uint8(0)) // Mark as peer.
- return buf.Bytes(), nil
- }
- // Check peer number in the cluster
- if ps.registry.PeerCount() >= ps.ClusterConfig().ActiveSize {
- log.Debug("Join as proxy ", c.Name)
- ps.registry.RegisterProxy(c.Name, c.RaftURL, c.EtcdURL)
- binary.Write(&buf, binary.BigEndian, uint8(1)) // Mark as proxy.
- return buf.Bytes(), nil
- }
- // Remove it as a proxy if it is one.
- if ps.registry.ProxyExists(c.Name) {
- ps.registry.UnregisterProxy(c.Name)
- }
- // Add to shared peer registry.
- ps.registry.RegisterPeer(c.Name, c.RaftURL, c.EtcdURL)
- // Add peer in raft
- err := context.Server().AddPeer(c.Name, "")
- // Add peer stats
- if c.Name != ps.RaftServer().Name() {
- ps.followersStats.Followers[c.Name] = &raftFollowerStats{}
- ps.followersStats.Followers[c.Name].Latency.Minimum = 1 << 63
- }
- binary.Write(&buf, binary.BigEndian, uint8(0)) // Mark as peer.
- return buf.Bytes(), err
- }
- func (c *JoinCommand) NodeName() string {
- return c.Name
- }
|