raft: node.Add needs the pubAddr field

release-2.0
Xiang Li 2014-06-19 14:54:02 -07:00 committed by Yicheng Qin
parent 5778c49689
commit e9cb510ef5
3 changed files with 16 additions and 16 deletions

View File

@ -92,10 +92,10 @@ func buildCluster(size int) (nt *network, nodes []*Node) {
}
nt = newNetwork(nis...)
lead := Dictate(nodes[0])
lead := dictate(nodes[0])
lead.Next()
for i := 1; i < size; i++ {
lead.Add(i)
lead.Add(i, "")
nt.send(lead.Msgs()...)
for j := 0; j < i; j++ {
nodes[j].Next()

View File

@ -13,8 +13,8 @@ type Interface interface {
type tick int
type config struct {
NodeId int
Address string
NodeId int
Addr string
}
type Node struct {
@ -39,12 +39,6 @@ func New(id int, heartbeat, election tick) *Node {
return n
}
func Dictate(n *Node) *Node {
n.Step(Message{Type: msgHup})
n.Add(n.Id())
return n
}
func (n *Node) Id() int { return n.sm.id }
func (n *Node) HasLeader() bool { return n.sm.lead != none }
@ -56,7 +50,7 @@ func (n *Node) propose(t int, data []byte) {
n.Step(Message{Type: msgProp, Entries: []Entry{{Type: t, Data: data}}})
}
func (n *Node) Add(id int) { n.updateConf(AddNode, &config{NodeId: id}) }
func (n *Node) Add(id int, addr string) { n.updateConf(AddNode, &config{NodeId: id, Addr: addr}) }
func (n *Node) Remove(id int) { n.updateConf(RemoveNode, &config{NodeId: id}) }

View File

@ -89,7 +89,7 @@ func TestResetElapse(t *testing.T) {
}
func TestStartCluster(t *testing.T) {
n := Dictate(New(0, defaultHeartbeat, defaultElection))
n := dictate(New(0, defaultHeartbeat, defaultElection))
n.Next()
if len(n.sm.ins) != 1 {
@ -104,9 +104,9 @@ func TestStartCluster(t *testing.T) {
}
func TestAdd(t *testing.T) {
n := Dictate(New(0, defaultHeartbeat, defaultElection))
n := dictate(New(0, defaultHeartbeat, defaultElection))
n.Next()
n.Add(1)
n.Add(1, "")
n.Next()
if len(n.sm.ins) != 2 {
@ -118,9 +118,9 @@ func TestAdd(t *testing.T) {
}
func TestRemove(t *testing.T) {
n := Dictate(New(0, defaultHeartbeat, defaultElection))
n := dictate(New(0, defaultHeartbeat, defaultElection))
n.Next()
n.Add(1)
n.Add(1, "")
n.Next()
n.Remove(0)
n.Step(Message{Type: msgAppResp, From: 1, Term: 1, Index: 3})
@ -133,3 +133,9 @@ func TestRemove(t *testing.T) {
t.Errorf("id = %d, want 0", n.sm.id)
}
}
func dictate(n *Node) *Node {
n.Step(Message{Type: msgHup})
n.Add(n.Id(), "")
return n
}