etcd/server/join_command.go

76 lines
1.8 KiB
Go
Raw Normal View History

2013-10-11 11:02:38 +04:00
package server
import (
"encoding/binary"
etcdErr "github.com/coreos/etcd/error"
"github.com/coreos/etcd/log"
"github.com/coreos/go-raft"
)
func init() {
raft.RegisterCommand(&JoinCommand{})
}
// The JoinCommand adds a node to the cluster.
type JoinCommand struct {
RaftVersion string `json:"raftVersion"`
Name string `json:"name"`
RaftURL string `json:"raftURL"`
EtcdURL string `json:"etcdURL"`
2013-10-11 11:02:38 +04:00
}
2013-10-12 23:35:23 +04:00
func NewJoinCommand(version, name, raftUrl, etcdUrl string) *JoinCommand {
2013-10-11 11:02:38 +04:00
return &JoinCommand{
RaftVersion: version,
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
func (c *JoinCommand) Apply(server *raft.Server) (interface{}, error) {
2013-10-12 10:28:46 +04:00
ps, _ := server.Context().(*PeerServer)
2013-10-11 11:02:38 +04:00
b := make([]byte, 8)
binary.PutUvarint(b, server.CommitIndex())
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-10-12 10:28:46 +04:00
// Check if the join command is from a previous machine, who lost all its previous log.
if _, ok := ps.registry.URL(c.Name); ok {
2013-10-11 11:02:38 +04:00
return b, nil
}
2013-10-12 10:28:46 +04:00
// Check machine number in the cluster
2013-10-12 23:35:23 +04:00
if ps.registry.Count() == ps.MaxClusterSize {
2013-10-11 11:02:38 +04:00
log.Debug("Reject join request from ", c.Name)
return []byte{0}, etcdErr.NewError(etcdErr.EcodeNoMoreMachine, "", server.CommitIndex(), server.Term())
}
2013-10-12 10:28:46 +04:00
// Add to shared machine registry.
ps.registry.Register(c.Name, c.RaftVersion, c.RaftURL, c.EtcdURL, server.CommitIndex(), server.Term())
2013-10-11 11:02:38 +04:00
2013-10-12 10:28:46 +04:00
// Add peer in raft
2013-10-11 11:02:38 +04:00
err := server.AddPeer(c.Name, "")
2013-10-12 10:28:46 +04:00
// Add peer stats
if c.Name != ps.Name() {
ps.followersStats.Followers[c.Name] = &raftFollowerStats{}
ps.followersStats.Followers[c.Name].Latency.Minimum = 1 << 63
2013-10-11 11:02:38 +04:00
}
return b, err
}
func (c *JoinCommand) NodeName() string {
return c.Name
}