Browse Source

raft: remove unused raftpb.LastIndex

Yicheng Qin 11 years ago
parent
commit
cc8d8f2102
5 changed files with 12 additions and 31 deletions
  1. 1 1
      raft/node.go
  2. 3 4
      raft/node_test.go
  3. 1 4
      raft/raft.go
  4. 7 21
      raft/raftpb/raft.pb.go
  5. 0 1
      raft/raftpb/raft.proto

+ 1 - 1
raft/node.go

@@ -34,7 +34,7 @@ type Ready struct {
 }
 
 func isStateEqual(a, b pb.State) bool {
-	return a.Term == b.Term && a.Vote == b.Vote && a.Commit == b.Commit && a.LastIndex == b.LastIndex
+	return a.Term == b.Term && a.Vote == b.Vote && a.Commit == b.Commit
 }
 
 func IsEmptyState(st pb.State) bool {

+ 3 - 4
raft/node_test.go

@@ -140,12 +140,12 @@ func TestNode(t *testing.T) {
 
 	wants := []Ready{
 		{
-			State:            raftpb.State{Term: 1, Commit: 1, LastIndex: 1},
+			State:            raftpb.State{Term: 1, Commit: 1},
 			Entries:          []raftpb.Entry{{}, {Term: 1, Index: 1}},
 			CommittedEntries: []raftpb.Entry{{Term: 1, Index: 1}},
 		},
 		{
-			State:            raftpb.State{Term: 1, Commit: 2, LastIndex: 2},
+			State:            raftpb.State{Term: 1, Commit: 2},
 			Entries:          []raftpb.Entry{{Term: 1, Index: 2, Data: []byte("foo")}},
 			CommittedEntries: []raftpb.Entry{{Term: 1, Index: 2, Data: []byte("foo")}},
 		},
@@ -175,7 +175,7 @@ func TestNodeRestart(t *testing.T) {
 		{Term: 1, Index: 1},
 		{Term: 1, Index: 2, Data: []byte("foo")},
 	}
-	st := raftpb.State{Term: 1, Commit: 1, LastIndex: 2}
+	st := raftpb.State{Term: 1, Commit: 1}
 
 	want := Ready{
 		State: emptyState,
@@ -204,7 +204,6 @@ func TestIsStateEqual(t *testing.T) {
 		{raftpb.State{Vote: 1}, false},
 		{raftpb.State{Commit: 1}, false},
 		{raftpb.State{Term: 1}, false},
-		{raftpb.State{LastIndex: 1}, false},
 	}
 
 	for i, tt := range tests {

+ 1 - 4
raft/raft.go

@@ -250,7 +250,7 @@ func (r *raft) q() int {
 func (r *raft) appendEntry(e pb.Entry) {
 	e.Term = r.Term
 	e.Index = r.raftLog.lastIndex() + 1
-	r.LastIndex = r.raftLog.append(r.raftLog.lastIndex(), e)
+	r.raftLog.append(r.raftLog.lastIndex(), e)
 	r.prs[r.id].update(r.raftLog.lastIndex())
 	r.maybeCommit()
 }
@@ -355,7 +355,6 @@ func (r *raft) Step(m pb.Message) error {
 
 func (r *raft) handleAppendEntries(m pb.Message) {
 	if r.raftLog.maybeAppend(m.Index, m.LogTerm, m.Commit, m.Entries...) {
-		r.LastIndex = r.raftLog.lastIndex()
 		r.send(pb.Message{To: m.From, Type: msgAppResp, Index: r.raftLog.lastIndex()})
 	} else {
 		r.send(pb.Message{To: m.From, Type: msgAppResp, Index: -1})
@@ -461,7 +460,6 @@ func (r *raft) restore(s pb.Snapshot) bool {
 	}
 
 	r.raftLog.restore(s)
-	r.LastIndex = r.raftLog.lastIndex()
 	r.prs = make(map[int64]*progress)
 	for _, n := range s.Nodes {
 		if n == r.id {
@@ -511,5 +509,4 @@ func (r *raft) loadState(state pb.State) {
 	r.Term = state.Term
 	r.Vote = state.Vote
 	r.Commit = state.Commit
-	r.LastIndex = state.LastIndex
 }

+ 7 - 21
raft/raftpb/raft.pb.go

@@ -84,7 +84,6 @@ type State struct {
 	Term             int64  `protobuf:"varint,1,req,name=term" json:"term"`
 	Vote             int64  `protobuf:"varint,2,req,name=vote" json:"vote"`
 	Commit           int64  `protobuf:"varint,3,req,name=commit" json:"commit"`
-	LastIndex        int64  `protobuf:"varint,4,req,name=lastIndex" json:"lastIndex"`
 	XXX_unrecognized []byte `json:"-"`
 }
 
@@ -614,21 +613,6 @@ func (m *State) Unmarshal(data []byte) error {
 					break
 				}
 			}
-		case 4:
-			if wireType != 0 {
-				return code_google_com_p_gogoprotobuf_proto.ErrWrongType
-			}
-			for shift := uint(0); ; shift += 7 {
-				if index >= l {
-					return io.ErrUnexpectedEOF
-				}
-				b := data[index]
-				index++
-				m.LastIndex |= (int64(b) & 0x7F) << shift
-				if b < 0x80 {
-					break
-				}
-			}
 		default:
 			var sizeOfWire int
 			for {
@@ -719,7 +703,6 @@ func (m *State) Size() (n int) {
 	n += 1 + sovRaft(uint64(m.Term))
 	n += 1 + sovRaft(uint64(m.Vote))
 	n += 1 + sovRaft(uint64(m.Commit))
-	n += 1 + sovRaft(uint64(m.LastIndex))
 	if m.XXX_unrecognized != nil {
 		n += len(m.XXX_unrecognized)
 	}
@@ -815,7 +798,13 @@ func (m *Snapshot) MarshalTo(data []byte) (n int, err error) {
 		for _, num := range m.Nodes {
 			data[i] = 0x10
 			i++
-			i = encodeVarintRaft(data, i, uint64(num))
+			for num >= 1<<7 {
+				data[i] = uint8(uint64(num)&0x7f | 0x80)
+				num >>= 7
+				i++
+			}
+			data[i] = uint8(num)
+			i++
 		}
 	}
 	data[i] = 0x18
@@ -914,9 +903,6 @@ func (m *State) MarshalTo(data []byte) (n int, err error) {
 	data[i] = 0x18
 	i++
 	i = encodeVarintRaft(data, i, uint64(m.Commit))
-	data[i] = 0x20
-	i++
-	i = encodeVarintRaft(data, i, uint64(m.LastIndex))
 	if m.XXX_unrecognized != nil {
 		i += copy(data[i:], m.XXX_unrecognized)
 	}

+ 0 - 1
raft/raftpb/raft.proto

@@ -40,5 +40,4 @@ message State {
 	required int64 term   = 1 [(gogoproto.nullable) = false];
 	required int64 vote   = 2 [(gogoproto.nullable) = false];
 	required int64 commit = 3 [(gogoproto.nullable) = false];
-	required int64 lastIndex = 4 [(gogoproto.nullable) = false];
 }