From 40ed560e164860b4023017d350ae5b3cdf7beb2b Mon Sep 17 00:00:00 2001 From: Blake Mizerany Date: Fri, 22 Aug 2014 14:29:37 -0700 Subject: [PATCH] raft: index -> State.LastIndex --- raft2/genproto.sh | 1 + raft2/raft.go | 6 +++--- raft2/state.pb.go | 20 ++++++++++++++++++++ raft2/state.proto | 1 + 4 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 raft2/genproto.sh diff --git a/raft2/genproto.sh b/raft2/genproto.sh new file mode 100644 index 000000000..39dc53a6c --- /dev/null +++ b/raft2/genproto.sh @@ -0,0 +1 @@ +exec protoc --gogo_out=. -I=.:$GOPATH/src/code.google.com/p/gogoprotobuf/protobuf:$GOPATH/src *.proto diff --git a/raft2/raft.go b/raft2/raft.go index ac1f9c12b..6c5197526 100644 --- a/raft2/raft.go +++ b/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 { diff --git a/raft2/state.pb.go b/raft2/state.pb.go index c7ecb13ab..4152a7a84 100644 --- a/raft2/state.pb.go +++ b/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) } diff --git a/raft2/state.proto b/raft2/state.proto index d923e23de..bfdfcab1d 100644 --- a/raft2/state.proto +++ b/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]; }