Browse Source

raft: return offset for unstableEnts

Yicheng Qin 11 years ago
parent
commit
e850c644da
3 changed files with 16 additions and 7 deletions
  1. 3 2
      raft/log.go
  2. 11 4
      raft/log_test.go
  3. 2 1
      raft/node.go

+ 3 - 2
raft/log.go

@@ -84,10 +84,11 @@ func (l *raftLog) findConflict(from int64, ents []Entry) int64 {
 	return -1
 }
 
-func (l *raftLog) unstableEnts() []Entry {
+func (l *raftLog) unstableEnts() (int64, []Entry) {
+	offset := l.unstable
 	ents := l.entries(l.unstable)
 	l.unstable = l.lastIndex() + 1
-	return ents
+	return offset, ents
 }
 
 func (l *raftLog) lastIndex() int64 {

+ 11 - 4
raft/log_test.go

@@ -98,7 +98,10 @@ func TestCompactionSideEffects(t *testing.T) {
 		}
 	}
 
-	unstableEnts := raftLog.unstableEnts()
+	offset, unstableEnts := raftLog.unstableEnts()
+	if offset != 501 {
+		t.Errorf("offset(unstableEntries) = %d, want = %d", offset, 500)
+	}
 	if g := len(unstableEnts); g != 500 {
 		t.Errorf("len(unstableEntries) = %d, want = %d", g, 500)
 	}
@@ -119,18 +122,22 @@ func TestUnstableEnts(t *testing.T) {
 	previousEnts := []Entry{{Term: 1}, {Term: 2}}
 	tests := []struct {
 		unstable  int64
+		woffset   int64
 		wents     []Entry
 		wunstable int64
 	}{
-		{3, nil, 3},
-		{1, []Entry{{Term: 1}, {Term: 2}}, 3},
+		{3, 3, nil, 3},
+		{1, 1, []Entry{{Term: 1}, {Term: 2}}, 3},
 	}
 
 	for i, tt := range tests {
 		raftLog := newLog()
 		raftLog.ents = append(raftLog.ents, previousEnts...)
 		raftLog.unstable = tt.unstable
-		ents := raftLog.unstableEnts()
+		offset, ents := raftLog.unstableEnts()
+		if offset != tt.woffset {
+			t.Errorf("#%d: offset = %d, want = %d", i, offset, tt.woffset)
+		}
 		if !reflect.DeepEqual(ents, tt.wents) {
 			t.Errorf("#%d: unstableEnts = %+v, want %+v", i, ents, tt.wents)
 		}

+ 2 - 1
raft/node.go

@@ -206,7 +206,8 @@ func (n *Node) UpdateConf(t int64, c *Config) {
 }
 
 // UnstableEnts retuens all the entries that need to be persistent.
-func (n *Node) UnstableEnts() []Entry {
+// The first return value is offset, and the second one is unstable entries.
+func (n *Node) UnstableEnts() (int64, []Entry) {
 	return n.sm.raftLog.unstableEnts()
 }