WormDb.Cluster
1.0.2
dotnet add package WormDb.Cluster --version 1.0.2
NuGet\Install-Package WormDb.Cluster -Version 1.0.2
<PackageReference Include="WormDb.Cluster" Version="1.0.2" />
<PackageVersion Include="WormDb.Cluster" Version="1.0.2" />
<PackageReference Include="WormDb.Cluster" />
paket add WormDb.Cluster --version 1.0.2
#r "nuget: WormDb.Cluster, 1.0.2"
#:package WormDb.Cluster@1.0.2
#addin nuget:?package=WormDb.Cluster&version=1.0.2
#tool nuget:?package=WormDb.Cluster&version=1.0.2
WormDb.Cluster
WormDb 的 gRPC 集群复制模块,实现 Follower → Leader 单向推流复制。
写节点(Follower)先将日志本地落盘,再异步推送到汇聚节点(Leader)归档。支持断线自动重连、补偿推送、背压保护。
功能
| 特性 | 说明 |
|---|---|
| Follower → Leader 推流 | gRPC 客户端流(Client Streaming),单向复制 |
| 本地先行落地 | Follower 先写本地 DB 再推流,保证数据不丢 |
| 去重(序列号水位) | Leader 按 per-follower 序列号去重,避免重复写入 |
| 断线自动重连 | 网络中断后自动重连,未确认条目自动补偿推送 |
| 背压保护 | Channel 满时返回 false,静默丢弃计数 |
| 优雅退出 | ShutdownAsync 等待 Channel 排空 + 超时保护 |
| 持久化水位 | Leader/Follower 水位状态持久化到 JSON 文件,重启可恢复 |
核心 API
Leader 端
// LeaderNode
int AggregateBatch(PushBatch push); // 汇聚批次(含去重),返回写入数
void OnFollowerConnected(uint id); // 跟踪连接
void OnFollowerDisconnected(uint id);
IReadOnlyList<LogEntry> QueryRange(long startNs, long endNs);
// 统计
long TotalReceived / TotalBatches / DuplicateSkipped
int ConnectedFollowers
uint NodeId
ulong GetFollowerWatermark(uint followerId)
Follower 端
// FollowerReplicationClient
bool Write(LogEntry entry); // 单条:本地写入 + 推流入队
bool WriteBatch(IReadOnlyList<LogEntry> entries); // 批量写入(false = 背压)
Task StartAsync(); // 启动后台推流循环
Task ShutdownAsync(TimeSpan timeout); // 优雅排空 + 关闭
Task RequestReplayAsync(ulong fromSeq); // 向 Leader 请求回放
// 统计
long SentCount / LastAckSeq / DroppedCount
int PushErrors
gRPC 服务定义
service WormReplication {
rpc PushEntries (stream PushBatch) returns (PushAck);
rpc ReplayFrom (ReplayRequest) returns (stream LogEntry);
}
核心组件
| 类 | 职责 |
|---|---|
LeaderNode |
Leader 汇聚节点:接收推流、去重、写入归档、连接管理 |
FollowerReplicationClient |
Follower 推流客户端:本地写入 + 异步推流、重连、补偿 |
LeaderReplicationService |
gRPC 服务端:实现 PushEntries 和 ReplayFrom |
LeaderReplicationState |
Leader 持久化水位:per-follower 去重序列号、统计 |
FollowerReplicationState |
Follower 持久化进度:lastAckedSeq、lastAckedTimeNs |
数据流
客户端 HTTP 写入
|
v
Follower: Write() -> 本地 LogDatabase.Append() -> Channel<T>.TryWrite()
|
| gRPC Client Streaming (PushEntries)
v
Leader: PushEntries() -> LeaderNode.AggregateBatch()
-> 去重检查(序列号水位) -> db.Append()
-> 更新持久化水位
依赖
<ProjectReference Include="..\WormDb.Core\WormDb.Core.csproj" />
<PackageReference Include="Google.Protobuf" Version="3.35.0" />
<PackageReference Include="Grpc.AspNetCore" Version="2.80.0" />
<PackageReference Include="Grpc.Net.Client" Version="2.80.0" />
<PackageReference Include="Grpc.Tools" Version="2.81.0" PrivateAssets="All" />
<FrameworkReference Include="Microsoft.AspNetCore.App" />
使用
Leader 模式
using WormDb.Cluster;
var leader = new LeaderNode(nodeId: 0, db, stateFile: "./leader-state.json");
var grpcService = new LeaderReplicationService(leader);
// 注册到 gRPC 服务...
Follower 模式
using WormDb.Cluster;
var follower = new FollowerReplicationClient(
nodeId: 1,
localDb,
leaderAddress: "http://localhost:5100",
stateFile: "./follower-state.json"
);
await follower.StartAsync();
// 写入(本地落盘 + 异步推流)
follower.Write(new LogEntry { ... });
// 优雅退出
await follower.ShutdownAsync(TimeSpan.FromSeconds(10));
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Google.Protobuf (>= 3.35.0)
- Grpc.AspNetCore (>= 2.80.0)
- Grpc.Net.Client (>= 2.80.0)
- WormDb.Core (>= 1.0.4)
-
net8.0
- Google.Protobuf (>= 3.35.0)
- Grpc.AspNetCore (>= 2.80.0)
- Grpc.Net.Client (>= 2.80.0)
- WormDb.Core (>= 1.0.4)
-
net9.0
- Google.Protobuf (>= 3.35.0)
- Grpc.AspNetCore (>= 2.80.0)
- Grpc.Net.Client (>= 2.80.0)
- WormDb.Core (>= 1.0.4)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on WormDb.Cluster:
| Package | Downloads |
|---|---|
|
WormDb
WORM (Write Once Read Many) append-only log database engine. Built-in TagIndex inverted index, LZ4 compression, mmap zero-copy reads, Protobuf serialization, gRPC cluster replication, and ASP.NET Core Minimal API. Zero external dependencies, single-binary deployment. |
|
|
WormDb.Extensions
WORM (Write Once Read Many) append-only log database engine. Built-in TagIndex inverted index, LZ4 compression, mmap zero-copy reads, Protobuf serialization, gRPC cluster replication, and ASP.NET Core Minimal API. Zero external dependencies, single-binary deployment. |
GitHub repositories
This package is not used by any popular GitHub repositories.