Browse Source

raft: init stateMachine in New

Blake Mizerany 11 years ago
parent
commit
c24b6b4150
2 changed files with 5 additions and 13 deletions
  1. 0 1
      raft/cluster_test.go
  2. 5 12
      raft/node.go

+ 0 - 1
raft/cluster_test.go

@@ -95,7 +95,6 @@ func buildCluster(size int) (nt *network, nodes []*Node) {
 	Dictate(nodes[0]).Next()
 	Dictate(nodes[0]).Next()
 	for i := 1; i < size; i++ {
 	for i := 1; i < size; i++ {
 		nt.send(nodes[0].newConfMessage(configAdd, &Config{NodeId: i}))
 		nt.send(nodes[0].newConfMessage(configAdd, &Config{NodeId: i}))
-		nodes[i].Start()
 		for j := 0; j < i; j++ {
 		for j := 0; j < i; j++ {
 			nodes[j].Next()
 			nodes[j].Next()
 		}
 		}

+ 5 - 12
raft/node.go

@@ -39,29 +39,22 @@ func New(addr int, heartbeat, election tick) *Node {
 		heartbeat: heartbeat,
 		heartbeat: heartbeat,
 		election:  election,
 		election:  election,
 		addr:      addr,
 		addr:      addr,
+		sm:        newStateMachine(addr, []int{addr}),
 	}
 	}
 
 
 	return n
 	return n
 }
 }
 
 
-// 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}}}
-	n.Step(m)
-}
-
 func Dictate(n *Node) *Node {
 func Dictate(n *Node) *Node {
-	n.sm = newStateMachine(n.addr, []int{n.addr})
 	n.Step(Message{Type: msgHup})
 	n.Step(Message{Type: msgHup})
 	n.Step(n.newConfMessage(configAdd, &Config{NodeId: n.addr}))
 	n.Step(n.newConfMessage(configAdd, &Config{NodeId: n.addr}))
 	return n
 	return n
 }
 }
 
 
-func (n *Node) Start() {
-	if n.sm != nil {
-		panic("node is started")
-	}
-	n.sm = newStateMachine(n.addr, nil)
+// 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}}}
+	n.Step(m)
 }
 }
 
 
 func (n *Node) Add(addr int) {
 func (n *Node) Add(addr int) {