etcd/server/join_command.go

89 lines
2.2 KiB
Go
Raw Normal View History

2013-10-11 11:02:38 +04:00
package server
import (
2014-02-19 00:29:18 +04:00
"bytes"
2013-10-11 11:02:38 +04:00
"encoding/binary"
"github.com/coreos/etcd/log"
"github.com/coreos/etcd/third_party/github.com/coreos/raft"
2013-10-11 11:02:38 +04:00
)
func init() {
raft.RegisterCommand(&JoinCommand{})
}
// The JoinCommand adds a node to the cluster.
type JoinCommand struct {
MinVersion int `json:"minVersion"`
MaxVersion int `json:"maxVersion"`
Name string `json:"name"`
RaftURL string `json:"raftURL"`
EtcdURL string `json:"etcdURL"`
2013-10-11 11:02:38 +04:00
}
2013-10-27 22:47:00 +04:00
func NewJoinCommand(minVersion int, maxVersion int, name, raftUrl, etcdUrl string) *JoinCommand {
2013-10-11 11:02:38 +04:00
return &JoinCommand{
MinVersion: minVersion,
MaxVersion: maxVersion,
Name: name,
RaftURL: raftUrl,
EtcdURL: etcdUrl,
2013-10-11 11:02:38 +04:00
}
}
// The name of the join command in the log
func (c *JoinCommand) CommandName() string {
return "etcd:join"
}
// Join a server to the cluster
2013-12-28 02:35:42 +04:00
func (c *JoinCommand) Apply(context raft.Context) (interface{}, error) {
ps, _ := context.Server().Context().(*PeerServer)
2013-10-11 11:02:38 +04:00
2014-02-19 00:29:18 +04:00
var buf bytes.Buffer
2013-10-11 11:02:38 +04:00
b := make([]byte, 8)
2014-02-19 00:29:18 +04:00
n := binary.PutUvarint(b, context.CommitIndex())
buf.Write(b[:n])
2013-10-11 11:02:38 +04:00
2013-10-14 07:09:56 +04:00
// Make sure we're not getting a cached value from the registry.
ps.registry.Invalidate(c.Name)
2013-11-15 07:58:47 +04:00
// Check if the join command is from a previous peer, who lost all its previous log.
2013-10-14 09:13:20 +04:00
if _, ok := ps.registry.ClientURL(c.Name); ok {
2014-02-19 00:29:18 +04:00
binary.Write(&buf, binary.BigEndian, uint8(0)) // Mark as peer.
return buf.Bytes(), nil
2013-10-11 11:02:38 +04:00
}
2013-11-15 07:58:47 +04:00
// Check peer number in the cluster
2014-02-19 00:29:18 +04:00
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
2013-10-11 11:02:38 +04:00
}
2014-02-25 04:01:04 +04:00
// Remove it as a proxy if it is one.
if ps.registry.ProxyExists(c.Name) {
ps.registry.UnregisterProxy(c.Name)
}
2013-11-15 07:58:47 +04:00
// Add to shared peer registry.
2014-02-19 00:29:18 +04:00
ps.registry.RegisterPeer(c.Name, c.RaftURL, c.EtcdURL)
2013-10-11 11:02:38 +04:00
2013-10-12 10:28:46 +04:00
// Add peer in raft
2013-12-28 02:35:42 +04:00
err := context.Server().AddPeer(c.Name, "")
2013-10-11 11:02:38 +04:00
2013-10-12 10:28:46 +04:00
// Add peer stats
2013-10-14 23:05:55 +04:00
if c.Name != ps.RaftServer().Name() {
2013-10-12 10:28:46 +04:00
ps.followersStats.Followers[c.Name] = &raftFollowerStats{}
ps.followersStats.Followers[c.Name].Latency.Minimum = 1 << 63
2013-10-11 11:02:38 +04:00
}
2014-02-19 00:29:18 +04:00
binary.Write(&buf, binary.BigEndian, uint8(0)) // Mark as peer.
return buf.Bytes(), err
2013-10-11 11:02:38 +04:00
}
func (c *JoinCommand) NodeName() string {
return c.Name
}