二进制格式——网络线格式(Core.Net)
分册:总览与通用约定 | Log 族 | Ring 族 | 段元组/索引/快照 | TierVolume | 产品格式 | 网络线格式
4. 网络线格式(Core.Net)
4.1 Wire FrameHeader(16B,介质无关)
[0..4) PayloadLength 4B LE(≤ 16MB) [4] Kind 1B [5] ChannelId 1B [6] ProtocolId 1B [7] Version 1B(=1)
[8..12) HeaderCrc 4B(CRC32C 覆盖此前全部头字节——防半帧/错位)
[12..16) PayloadCrc 4B(CRC32C 覆盖载荷)
头版本 1 的字段永不再解释——演进走版本协商 + 新 FrameKind。[BinaryLayout] 声明即代码(零手写偏移)。
源码:Wire/FrameHeader.cs、FrameCodec.cs。
4.2 Raft RPC 消息族([WireMessage] 声明即线格式)
编码规则:[Tag 1B][根字段声明序][子类字段声明序],全 LE。根 RaftRpc 公共前缀 = [Term 8B]。bool=1B;定长原语小端原宽;NodeId=16B 不透明串;blob=[Len 4B][bytes];数组=[Count 4B][item×N]。消息不携带 CorrId(RpcId 取消由传输承载)。
| Tag | 消息 | 子类字段(根 Term 8B 在前) |
|---|---|---|
| 0x01 | PreVoteReq | CandidateId(16) + LastLogIndex(8) + LastLogTerm(8) |
| 0x02 | PreVoteResp | Granted(1) |
| 0x03 | RequestVoteReq | CandidateId(16) + LastLogIndex(8) + LastLogTerm(8) |
| 0x04 | RequestVoteResp | Granted(1) |
| 0x05 | AppendEntriesReq | LeaderId(16) + PrevLogIndex(8) + PrevLogTerm(8) + LeaderCommit(8) + EntriesRegion(blob) |
| 0x06 | AppendEntriesResp | Success(1) + MatchIndex(8) + ConflictTerm(8) + ConflictIndex(8) + SnapshotIndex(8) |
| 0x07 | InstallSnapshotReq | LeaderId(16) + SnapshotIndex(8) + Swarm(1) + ManifestId(16) + BlockSize(4) + TotalBytes(8) + Checksums(数组 u32) + Holders(数组 NodeId) |
| 0x08 | InstallSnapshotResp | Success(1) + SnapshotIndex(8) |
| 0x09 | JoinReq | CandidateId(16) + AutoPromote(1) + EndPointBytes(blob ≤64) + AsWitness(1) |
| 0x0A | JoinResp | Accepted(1) + LeaderId(16) |
| 0x0B | ReadIndexReq | (仅根 Term) |
| 0x0C | ReadIndexResp | ReadIndex(8) |
| 0x0D | TransferLeaderReq | (仅根 Term) |
| 0x0E | TransferLeaderResp | Accepted(1) |
线格式版本 v3(WireFormatVersion=3)。防御上限 MaxEntriesPerAppend=8192、MaxEntriesRegionBytes=16MiB。未知 tag/截断/超上限 = 畸形丢弃;已知 tag 尾部扩展字节容忍。
源码:Raft/RaftRpc.cs;线格式生成 = KernLab.Loom.Wire(组织包,仓内无 Tier 自有 WireMessageGenerator)。
4.3 RaftEntriesRegion(AppendEntries 条目区)
[Count 4B LE][×N: RaftAppendEntryHeader 13B + Content]
RaftAppendEntryHeader(13B):[0] Term 8B LE + [8] Kind 1B + [9] ContentLength 4B LE,Content 紧随。
源码:Raft/RaftEntriesRegion.cs:98-121。
4.4 Gossip 帧(37B)
[MsgId 16B][Origin 16B][Ttl 1B][PayloadLength 4B LE][data]
MsgId = 广播身份(去重键,同消息至多投递一次);Origin = 最初广播者(中继逐跳不变)。[BinaryLayout] 生成 codec。
源码:P2P/GossipFrameHeader.cs。
4.5 HyParView 消息族
六类消息(协议域 0x02):Join [Tag 1B][Origin 16B][Ttl 4B]、Neighbor [Tag][Priority 1B][Count 4B][NodeId×Count]、Disconnect [Tag]、Fail [Tag][Failed 16B]、Shuffle [Tag][Count 4B][NodeId×Count]、Heartbeat [Tag]。发送方身份不上线(分发即身份);解码防御(未知 tag/截断/Count 超上限 = 丢弃,尾部扩展容忍前向兼容)。
源码:P2P/HyParViewMessage.cs。