From 55e6a837076d30e50db813ee5cf6dd2b8b232145 Mon Sep 17 00:00:00 2001 From: Nathan VanBenschoten Date: Sun, 16 Oct 2022 04:28:00 +0000 Subject: [PATCH] raft: make Message.Snapshot nullable, halve struct size MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit makes the rarely used `raftpb.Message.Snapshot` field nullable. In doing so, it reduces the memory size of a `raftpb.Message` message from 264 bytes to 128 bytes — a 52% reduction in size. While this commit does not change the protobuf encoding, it does change how that encoding is used. `(gogoproto.nullable) = false` instruct the generated proto marshaling logic to always encode a value for the field, even if that value is empty. `(gogoproto.nullable) = true` instructs the generated proto marshaling logic to omit an encoded value for the field if the field is nil. This raises compatibility concerns in both directions. Messages encoded by new binary versions without a `Snapshot` field will be decoded as an empty field by old binary versions. In other words, old binary versions can't tell the difference. However, messages encoded by old binary versions with an empty Snapshot field will be decoded as a non-nil, empty field by new binary versions. As a result, new binary versions need to be prepared to handle such messages. While Message.Snapshot is not intentionally part of the external interface of this library, it was possible for users of the library to access it and manipulate it. As such, this change may be considered a breaking change. Signed-off-by: Nathan VanBenschoten --- raft.go | 12 ++- raft_test.go | 2 +- raftpb/raft.pb.go | 177 +++++++++++++++++++++++--------------------- raftpb/raft.proto | 6 +- raftpb/raft_test.go | 2 +- util.go | 4 +- 6 files changed, 112 insertions(+), 91 deletions(-) diff --git a/raft.go b/raft.go index d7603f10..5b313919 100644 --- a/raft.go +++ b/raft.go @@ -478,7 +478,7 @@ func (r *raft) maybeSendAppend(to uint64, sendIfEmpty bool) bool { pr.BecomeSnapshot(sindex) r.logger.Debugf("%x paused sending replication messages to %x [%s]", r.id, to, pr) - r.send(pb.Message{To: to, Type: pb.MsgSnap, Snapshot: snapshot}) + r.send(pb.Message{To: to, Type: pb.MsgSnap, Snapshot: &snapshot}) return true } @@ -1537,8 +1537,14 @@ func (r *raft) handleHeartbeat(m pb.Message) { } func (r *raft) handleSnapshot(m pb.Message) { - sindex, sterm := m.Snapshot.Metadata.Index, m.Snapshot.Metadata.Term - if r.restore(m.Snapshot) { + // MsgSnap messages should always carry a non-nil Snapshot, but err on the + // side of safety and treat a nil Snapshot as a zero-valued Snapshot. + var s pb.Snapshot + if m.Snapshot != nil { + s = *m.Snapshot + } + sindex, sterm := s.Metadata.Index, s.Metadata.Term + if r.restore(s) { r.logger.Infof("%x [commit: %d] restored snapshot [index: %d, term: %d]", r.id, r.raftLog.committed, sindex, sterm) r.send(pb.Message{To: m.From, Type: pb.MsgAppResp, Index: r.raftLog.lastIndex()}) diff --git a/raft_test.go b/raft_test.go index a04e60bf..95408976 100644 --- a/raft_test.go +++ b/raft_test.go @@ -3067,7 +3067,7 @@ func TestIgnoreProvidingSnap(t *testing.T) { } func TestRestoreFromSnapMsg(t *testing.T) { - s := pb.Snapshot{ + s := &pb.Snapshot{ Metadata: pb.SnapshotMetadata{ Index: 11, // magic number Term: 11, // magic number diff --git a/raftpb/raft.pb.go b/raftpb/raft.pb.go index 1ee77a9a..cdbfe3aa 100644 --- a/raftpb/raft.pb.go +++ b/raftpb/raft.pb.go @@ -390,14 +390,18 @@ type Message struct { // index=101, and the term of entry at index 100 is 5. // (type=MsgAppResp,reject=true,index=100,logTerm=5) means follower rejects some // entries from its leader as it already has an entry with term 5 at index 100. - LogTerm uint64 `protobuf:"varint,5,opt,name=logTerm" json:"logTerm"` - Index uint64 `protobuf:"varint,6,opt,name=index" json:"index"` - Entries []Entry `protobuf:"bytes,7,rep,name=entries" json:"entries"` - Commit uint64 `protobuf:"varint,8,opt,name=commit" json:"commit"` - Snapshot Snapshot `protobuf:"bytes,9,opt,name=snapshot" json:"snapshot"` - Reject bool `protobuf:"varint,10,opt,name=reject" json:"reject"` - RejectHint uint64 `protobuf:"varint,11,opt,name=rejectHint" json:"rejectHint"` - Context []byte `protobuf:"bytes,12,opt,name=context" json:"context,omitempty"` + LogTerm uint64 `protobuf:"varint,5,opt,name=logTerm" json:"logTerm"` + Index uint64 `protobuf:"varint,6,opt,name=index" json:"index"` + Entries []Entry `protobuf:"bytes,7,rep,name=entries" json:"entries"` + Commit uint64 `protobuf:"varint,8,opt,name=commit" json:"commit"` + // snapshot is non-nil and non-empty for MsgSnap messages and nil for all other + // message types. However, peer nodes running older binary versions may send a + // non-nil, empty value for the snapshot field of non-MsgSnap messages. Code + // should be prepared to handle such messages. + Snapshot *Snapshot `protobuf:"bytes,9,opt,name=snapshot" json:"snapshot,omitempty"` + Reject bool `protobuf:"varint,10,opt,name=reject" json:"reject"` + RejectHint uint64 `protobuf:"varint,11,opt,name=rejectHint" json:"rejectHint"` + Context []byte `protobuf:"bytes,12,opt,name=context" json:"context,omitempty"` } func (m *Message) Reset() { *m = Message{} } @@ -695,72 +699,72 @@ func init() { func init() { proto.RegisterFile("raft.proto", fileDescriptor_b042552c306ae59b) } var fileDescriptor_b042552c306ae59b = []byte{ - // 1026 bytes of a gzipped FileDescriptorProto + // 1028 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0x55, 0xcd, 0x6e, 0xdb, 0x46, - 0x17, 0x25, 0x29, 0x5a, 0x3f, 0x57, 0xb2, 0x3c, 0xbe, 0xf1, 0x17, 0x10, 0x86, 0xc1, 0xe8, 0x53, - 0x52, 0x44, 0x70, 0x11, 0xb7, 0xd0, 0xa2, 0x28, 0xba, 0xf3, 0x4f, 0x00, 0xab, 0xb0, 0xdc, 0x54, - 0x76, 0xbc, 0x28, 0x50, 0x08, 0x63, 0x71, 0x44, 0xb3, 0x15, 0x39, 0x04, 0x39, 0x72, 0xed, 0x4d, - 0x51, 0xf4, 0x09, 0xba, 0xec, 0x26, 0xdb, 0x3e, 0x40, 0x9f, 0xc2, 0x4b, 0x03, 0xdd, 0x74, 0x15, - 0x34, 0xf6, 0x8b, 0x14, 0x33, 0x1c, 0x4a, 0x94, 0x6c, 0x64, 0xd1, 0xdd, 0xcc, 0xb9, 0x67, 0xee, - 0x9c,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, 0x81, + 0xff, 0x3f, 0x16, 0xfd, 0x5a, 0x04, 0x91, 0x3c, 0x08, 0xe3, 0x71, 0x30, 0x0c, 0x54, 0x2b, 0x3e, + 0x46, 0x7b, 0x7d, 0x65, 0x68, 0xf6, 0xe6, 0x35, 0x34, 0xe7, 0x3f, 0x0a, 0x55, 0x8c, 0x19, 0xb2, + 0xed, 0x79, 0x6a, 0xfc, 0xa9, 0x85, 0x6e, 0x51, 0xec, 0x80, 0x87, 0xe2, 0x92, 0xeb, 0x08, 0x99, + 0x8f, 0xbc, 0x8d, 0x3d, 0x26, 0xb3, 0x88, 0x3d, 0x6f, 0x64, 0xdb, 0xf3, 0x0e, 0xb3, 0xb7, 0x47, + 0x47, 0x4b, 0x3b, 0x2f, 0x6e, 0x3e, 0xb4, 0xac, 0xdb, 0x0f, 0x2d, 0xeb, 0xe6, 0xae, 0x45, 0x6e, + 0xef, 0x5a, 0xe4, 0x9f, 0xbb, 0x16, 0xf9, 0xed, 0xbe, 0x65, 0xfd, 0x7e, 0xdf, 0xb2, 0x6e, 0xef, + 0x5b, 0xd6, 0xdf, 0xf7, 0x2d, 0xeb, 0xdf, 0x00, 0x00, 0x00, 0xff, 0xff, 0xc9, 0x35, 0x94, 0xd2, + 0xbb, 0x08, 0x00, 0x00, } func (m *Entry) Marshal() (dAtA []byte, err error) { @@ -919,16 +923,18 @@ func (m *Message) MarshalToSizedBuffer(dAtA []byte) (int, error) { } i-- dAtA[i] = 0x50 - { - size, err := m.Snapshot.MarshalToSizedBuffer(dAtA[:i]) - if err != nil { - return 0, err + if m.Snapshot != nil { + { + size, err := m.Snapshot.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintRaft(dAtA, i, uint64(size)) } - i -= size - i = encodeVarintRaft(dAtA, i, uint64(size)) + i-- + dAtA[i] = 0x4a } - i-- - dAtA[i] = 0x4a i = encodeVarintRaft(dAtA, i, uint64(m.Commit)) i-- dAtA[i] = 0x40 @@ -1247,8 +1253,10 @@ func (m *Message) Size() (n int) { } } n += 1 + sovRaft(uint64(m.Commit)) - l = m.Snapshot.Size() - n += 1 + l + sovRaft(uint64(l)) + if m.Snapshot != nil { + l = m.Snapshot.Size() + n += 1 + l + sovRaft(uint64(l)) + } n += 2 n += 1 + sovRaft(uint64(m.RejectHint)) if m.Context != nil { @@ -1957,6 +1965,9 @@ func (m *Message) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } + if m.Snapshot == nil { + m.Snapshot = &Snapshot{} + } if err := m.Snapshot.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } diff --git a/raftpb/raft.proto b/raftpb/raft.proto index 4a4eaf90..f21f3afe 100644 --- a/raftpb/raft.proto +++ b/raftpb/raft.proto @@ -76,7 +76,11 @@ message Message { optional uint64 index = 6 [(gogoproto.nullable) = false]; repeated Entry entries = 7 [(gogoproto.nullable) = false]; optional uint64 commit = 8 [(gogoproto.nullable) = false]; - optional Snapshot snapshot = 9 [(gogoproto.nullable) = false]; + // snapshot is non-nil and non-empty for MsgSnap messages and nil for all other + // message types. However, peer nodes running older binary versions may send a + // non-nil, empty value for the snapshot field of non-MsgSnap messages. Code + // should be prepared to handle such messages. + optional Snapshot snapshot = 9 [(gogoproto.nullable) = true]; optional bool reject = 10 [(gogoproto.nullable) = false]; optional uint64 rejectHint = 11 [(gogoproto.nullable) = false]; optional bytes context = 12; diff --git a/raftpb/raft_test.go b/raftpb/raft_test.go index ff6b6d86..7057446c 100644 --- a/raftpb/raft_test.go +++ b/raftpb/raft_test.go @@ -45,7 +45,7 @@ func TestProtoMemorySizes(t *testing.T) { assert(unsafe.Sizeof(s), if64Bit(144, 80), "Snapshot") var m Message - assert(unsafe.Sizeof(m), if64Bit(264, 168), "Message") + assert(unsafe.Sizeof(m), if64Bit(128, 92), "Message") var hs HardState assert(unsafe.Sizeof(hs), 24, "HardState") diff --git a/util.go b/util.go index 94ab368f..6e728fb0 100644 --- a/util.go +++ b/util.go @@ -149,8 +149,8 @@ func DescribeMessage(m pb.Message, f EntryFormatter) string { } fmt.Fprintf(&buf, "]") } - if !IsEmptySnap(m.Snapshot) { - fmt.Fprintf(&buf, " Snapshot: %s", DescribeSnapshot(m.Snapshot)) + if s := m.Snapshot; s != nil && !IsEmptySnap(*s) { + fmt.Fprintf(&buf, " Snapshot: %s", DescribeSnapshot(*s)) } return buf.String() }