Add proxy mode.

This commit is contained in:
Ben Johnson
2014-02-18 13:29:18 -07:00
parent 8485987b74
commit 1d961b8e56
13 changed files with 522 additions and 69 deletions

View File

@ -1,9 +1,9 @@
package server
import (
"bytes"
"encoding/binary"
etcdErr "github.com/coreos/etcd/error"
"github.com/coreos/etcd/log"
"github.com/coreos/etcd/third_party/github.com/coreos/raft"
)
@ -40,25 +40,30 @@ func (c *JoinCommand) CommandName() string {
func (c *JoinCommand) Apply(context raft.Context) (interface{}, error) {
ps, _ := context.Server().Context().(*PeerServer)
var buf bytes.Buffer
b := make([]byte, 8)
binary.PutUvarint(b, context.CommitIndex())
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 {
return b, nil
binary.Write(&buf, binary.BigEndian, uint8(0)) // Mark as peer.
return buf.Bytes(), nil
}
// Check peer number in the cluster
if ps.registry.Count() == ps.Config.MaxClusterSize {
log.Debug("Reject join request from ", c.Name)
return []byte{0}, etcdErr.NewError(etcdErr.EcodeNoMorePeer, "", context.CommitIndex())
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
}
// Add to shared peer registry.
ps.registry.Register(c.Name, c.RaftURL, c.EtcdURL)
ps.registry.RegisterPeer(c.Name, c.RaftURL, c.EtcdURL)
// Add peer in raft
err := context.Server().AddPeer(c.Name, "")
@ -69,7 +74,8 @@ func (c *JoinCommand) Apply(context raft.Context) (interface{}, error) {
ps.followersStats.Followers[c.Name].Latency.Minimum = 1 << 63
}
return b, err
binary.Write(&buf, binary.BigEndian, uint8(0)) // Mark as peer.
return buf.Bytes(), err
}
func (c *JoinCommand) NodeName() string {