Browse Source

raft: replace Node.id

Blake Mizerany 11 years ago
parent
commit
96059a496a
2 changed files with 6 additions and 7 deletions
  1. 2 2
      raft/cluster_test.go
  2. 4 5
      raft/node.go

+ 2 - 2
raft/cluster_test.go

@@ -60,8 +60,8 @@ func TestBasicCluster(t *testing.T) {
 
 		for j := 0; j < tt.round; j++ {
 			for _, n := range nodes {
-				data := []byte{byte(n.id)}
-				nt.send(Message{Type: msgProp, To: n.id, Entries: []Entry{{Data: data}}})
+				data := []byte{byte(n.Id())}
+				nt.send(Message{Type: msgProp, To: n.Id(), Entries: []Entry{{Data: data}}})
 
 				base := nodes[0].Next()
 				if len(base) != 1 {

+ 4 - 5
raft/node.go

@@ -26,8 +26,6 @@ type Node struct {
 	// elapsed ticks after the last reset
 	elapsed tick
 	sm      *stateMachine
-
-	id int
 }
 
 func New(id int, heartbeat, election tick) *Node {
@@ -38,7 +36,6 @@ func New(id int, heartbeat, election tick) *Node {
 	n := &Node{
 		heartbeat: heartbeat,
 		election:  election,
-		id:        id,
 		sm:        newStateMachine(id, []int{id}),
 	}
 
@@ -47,10 +44,12 @@ func New(id int, heartbeat, election tick) *Node {
 
 func Dictate(n *Node) *Node {
 	n.Step(Message{Type: msgHup})
-	n.Step(n.newConfMessage(configAdd, &Config{NodeId: n.id}))
+	n.Step(n.newConfMessage(configAdd, &Config{NodeId: n.Id()}))
 	return n
 }
 
+func (n *Node) Id() int { return n.sm.id }
+
 // Propose asynchronously proposes data be applied to the underlying state machine.
 func (n *Node) Propose(data []byte) {
 	m := Message{Type: msgProp, Entries: []Entry{{Data: data}}}
@@ -138,5 +137,5 @@ func (n *Node) newConfMessage(t int, c *Config) Message {
 	if err != nil {
 		panic(err)
 	}
-	return Message{Type: msgProp, To: n.id, Entries: []Entry{Entry{Type: t, Data: data}}}
+	return Message{Type: msgProp, Entries: []Entry{Entry{Type: t, Data: data}}}
 }