Browse Source

raft: newStateMachine -> newRaft

Blake Mizerany 11 years ago
parent
commit
022a663e91
4 changed files with 17 additions and 17 deletions
  1. 1 1
      raft2/example_test.go
  2. 2 2
      raft2/node.go
  3. 1 1
      raft2/raft.go
  4. 13 13
      raft2/raft_test.go

+ 1 - 1
raft2/example_test.go

@@ -12,7 +12,7 @@ func saveStateToDisk(st State)    {}
 func saveToDisk(ents []Entry)     {}
 
 func Example_Node() {
-	n := Start(context.Background(), 0)
+	n := Start(context.Background(), 0, nil)
 
 	// stuff to n happens in other goroutines
 

+ 2 - 2
raft2/node.go

@@ -25,7 +25,7 @@ type Node struct {
 	tickc  chan struct{}
 }
 
-func Start(ctx context.Context, id int64) *Node {
+func Start(ctx context.Context, id int64, peers []int64) *Node {
 	n := &Node{
 		ctx:    ctx,
 		propc:  make(chan []byte),
@@ -33,7 +33,7 @@ func Start(ctx context.Context, id int64) *Node {
 		statec: make(chan stateResp),
 		tickc:  make(chan struct{}),
 	}
-	r := &raft{raftLog: newLog(), id: id}
+	r := newRaft(id, peers)
 	go n.run(r)
 	return n
 }

+ 1 - 1
raft2/raft.go

@@ -152,7 +152,7 @@ type raft struct {
 	promotable bool
 }
 
-func newStateMachine(id int64, peers []int64) *raft {
+func newRaft(id int64, peers []int64) *raft {
 	if id == none {
 		panic("cannot use none id")
 	}

+ 13 - 13
raft2/raft_test.go

@@ -201,9 +201,9 @@ func TestCommitWithoutNewTermEntry(t *testing.T) {
 }
 
 func TestDuelingCandidates(t *testing.T) {
-	a := newStateMachine(0, nil) // k, id are set later
-	b := newStateMachine(0, nil)
-	c := newStateMachine(0, nil)
+	a := newRaft(0, nil) // k, id are set later
+	b := newRaft(0, nil)
+	c := newRaft(0, nil)
 
 	nt := newNetwork(a, b, c)
 	nt.cut(0, 2)
@@ -590,7 +590,7 @@ func TestStateTransition(t *testing.T) {
 				}
 			}()
 
-			sm := newStateMachine(0, []int64{0})
+			sm := newRaft(0, []int64{0})
 			sm.state = tt.from
 
 			switch tt.to {
@@ -613,7 +613,7 @@ func TestStateTransition(t *testing.T) {
 }
 
 func TestConf(t *testing.T) {
-	sm := newStateMachine(0, []int64{0})
+	sm := newRaft(0, []int64{0})
 	sm.becomeCandidate()
 	sm.becomeLeader()
 
@@ -648,7 +648,7 @@ func TestConfChangeLeader(t *testing.T) {
 	}
 
 	for i, tt := range tests {
-		sm := newStateMachine(0, []int64{0})
+		sm := newRaft(0, []int64{0})
 		sm.raftLog = &raftLog{ents: []Entry{{}, {Type: tt.et}}}
 
 		sm.becomeCandidate()
@@ -677,7 +677,7 @@ func TestAllServerStepdown(t *testing.T) {
 	tterm := int64(3)
 
 	for i, tt := range tests {
-		sm := newStateMachine(0, []int64{0, 1, 2})
+		sm := newRaft(0, []int64{0, 1, 2})
 		switch tt.state {
 		case stateFollower:
 			sm.becomeFollower(1, 0)
@@ -725,7 +725,7 @@ func TestLeaderAppResp(t *testing.T) {
 	for i, tt := range tests {
 		// sm term is 1 after it becomes the leader.
 		// thus the last log term must be 1 to be committed.
-		sm := newStateMachine(0, []int64{0, 1, 2})
+		sm := newRaft(0, []int64{0, 1, 2})
 		sm.raftLog = &raftLog{ents: []Entry{{}, {Term: 0}, {Term: 1}}}
 		sm.becomeCandidate()
 		sm.becomeLeader()
@@ -760,7 +760,7 @@ func TestRecvMsgBeat(t *testing.T) {
 	}
 
 	for i, tt := range tests {
-		sm := newStateMachine(0, []int64{0, 1, 2})
+		sm := newRaft(0, []int64{0, 1, 2})
 		sm.raftLog = &raftLog{ents: []Entry{{}, {Term: 0}, {Term: 1}}}
 		sm.Term = 1
 		sm.state = tt.state
@@ -786,7 +786,7 @@ func TestRestore(t *testing.T) {
 		Nodes:     []int64{0, 1, 2},
 	}
 
-	sm := newStateMachine(0, []int64{0, 1})
+	sm := newRaft(0, []int64{0, 1})
 	if ok := sm.restore(s); !ok {
 		t.Fatal("restore fail, want succeed")
 	}
@@ -822,7 +822,7 @@ func TestProvideSnap(t *testing.T) {
 		Term:  defaultCompactThreshold + 1,
 		Nodes: []int64{0, 1},
 	}
-	sm := newStateMachine(0, []int64{0})
+	sm := newRaft(0, []int64{0})
 	// restore the statemachin from a snapshot
 	// so it has a compacted log and a snapshot
 	sm.restore(s)
@@ -863,7 +863,7 @@ func TestRestoreFromSnapMsg(t *testing.T) {
 	}
 	m := Message{Type: msgSnap, From: 0, Term: 1, Snapshot: s}
 
-	sm := newStateMachine(1, []int64{0, 1})
+	sm := newRaft(1, []int64{0, 1})
 	sm.Step(m)
 
 	if !reflect.DeepEqual(sm.raftLog.snapshot, s) {
@@ -932,7 +932,7 @@ func newNetwork(peers ...Interface) *network {
 		nid := int64(id)
 		switch v := p.(type) {
 		case nil:
-			sm := newStateMachine(nid, defaultPeerAddrs)
+			sm := newRaft(nid, defaultPeerAddrs)
 			npeers[nid] = sm
 		case *raft:
 			v.id = nid