Browse Source

raft: index -> State.LastIndex

Blake Mizerany 11 years ago
parent
commit
40ed560e16
4 changed files with 25 additions and 3 deletions
  1. 1 0
      raft2/genproto.sh
  2. 3 3
      raft2/raft.go
  3. 20 0
      raft2/state.pb.go
  4. 1 0
      raft2/state.proto

+ 1 - 0
raft2/genproto.sh

@@ -0,0 +1 @@
+exec protoc --gogo_out=. -I=.:$GOPATH/src/code.google.com/p/gogoprotobuf/protobuf:$GOPATH/src *.proto

+ 3 - 3
raft2/raft.go

@@ -311,7 +311,7 @@ func (sm *raft) q() int {
 func (sm *raft) appendEntry(e Entry) {
 	e.Term = sm.Term
 	e.Index = sm.raftLog.lastIndex() + 1
-	sm.index.Set(sm.raftLog.append(sm.raftLog.lastIndex(), e))
+	sm.LastIndex = sm.raftLog.append(sm.raftLog.lastIndex(), e)
 	sm.ins[sm.id].update(sm.raftLog.lastIndex())
 	sm.maybeCommit()
 }
@@ -392,7 +392,7 @@ func (sm *raft) Step(m Message) error {
 
 func (sm *raft) handleAppendEntries(m Message) {
 	if sm.raftLog.maybeAppend(m.Index, m.LogTerm, m.Commit, m.Entries...) {
-		sm.index.Set(sm.raftLog.lastIndex())
+		sm.LastIndex = sm.raftLog.lastIndex()
 		sm.send(Message{To: m.From, Type: msgAppResp, Index: sm.raftLog.lastIndex()})
 	} else {
 		sm.send(Message{To: m.From, Type: msgAppResp, Index: -1})
@@ -517,7 +517,7 @@ func (sm *raft) restore(s Snapshot) bool {
 	}
 
 	sm.raftLog.restore(s)
-	sm.index.Set(sm.raftLog.lastIndex())
+	sm.LastIndex = sm.raftLog.lastIndex()
 	sm.ins = make(map[int64]*index)
 	for _, n := range s.Nodes {
 		if n == sm.id {

+ 20 - 0
raft2/state.pb.go

@@ -31,6 +31,7 @@ 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:"-"`
 }
 
@@ -104,6 +105,21 @@ 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 {
@@ -133,6 +149,7 @@ func (m *State) Size() (n int) {
 	n += 1 + sovState(uint64(m.Term))
 	n += 1 + sovState(uint64(m.Vote))
 	n += 1 + sovState(uint64(m.Commit))
+	n += 1 + sovState(uint64(m.LastIndex))
 	if m.XXX_unrecognized != nil {
 		n += len(m.XXX_unrecognized)
 	}
@@ -176,6 +193,9 @@ func (m *State) MarshalTo(data []byte) (n int, err error) {
 	data[i] = 0x18
 	i++
 	i = encodeVarintState(data, i, uint64(m.Commit))
+	data[i] = 0x20
+	i++
+	i = encodeVarintState(data, i, uint64(m.LastIndex))
 	if m.XXX_unrecognized != nil {
 		i += copy(data[i:], m.XXX_unrecognized)
 	}

+ 1 - 0
raft2/state.proto

@@ -11,4 +11,5 @@ 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];
 }