dsh-lan-peers 0.7.2 → 0.7.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/HANDOVER.md +5 -1
- package/host/server.mjs +320 -33
- package/host/test/group-cluster.mjs +60 -1
- package/host/test/group-commit.test.mjs +48 -1
- package/host/test/group-election.test.mjs +64 -1
- package/host/test/group-key-rotation.test.mjs +219 -0
- package/host/test/group-takeover.test.mjs +122 -0
- package/host/test/group.test.mjs +2 -1
- package/lib/admin-group.test.mjs +89 -0
- package/lib/admin.mjs +28 -4
- package/package.json +1 -1
package/lib/admin-group.test.mjs
CHANGED
|
@@ -863,6 +863,95 @@ test('S3 · leader.epoch 以角色门面为准:角色 3 / 群元数据 1 → 3
|
|
|
863
863
|
assert.equal(fallback.json.leader.epoch, 1, '没有角色门面时回退到群元数据的 epoch');
|
|
864
864
|
});
|
|
865
865
|
|
|
866
|
+
test('S3 · 成员侧 leader.peerId 同样以角色门面为准(配置的 group.hub 只是引导入口)', async () => {
|
|
867
|
+
// 动态 hub 的语义:leader 可能是任何成员,group.hub 只是引导入口,不代表当前 leader。
|
|
868
|
+
const members = [
|
|
869
|
+
{ peerId: 'peer-hub', name: 'hub-box', online: true, lastSeen: null, joinedAt: 1, baseUrl: 'http://192.168.5.51:7788', lastSeq: 8 },
|
|
870
|
+
{ peerId: 'peer-other', name: 'dsh-desktop', online: true, lastSeen: null, joinedAt: 2, baseUrl: 'http://192.168.5.20:7788', lastSeq: 8 },
|
|
871
|
+
{ peerId: 'member-inst', name: 'me', online: true, lastSeen: null, joinedAt: 3, baseUrl: '', lastSeq: null },
|
|
872
|
+
];
|
|
873
|
+
const leadsAs = (leaderPeerId) => stubAdmin({
|
|
874
|
+
group: { enabled: true, hub: 'hub-box' },
|
|
875
|
+
api: apiWith({ isHub: false, lastSeq: 8, members }),
|
|
876
|
+
peers: HUB_PEERS,
|
|
877
|
+
facade: { enabled: false, isHub: false, role: () => ({ role: 'follower', epoch: 3, leaderPeerId, leaseUntil: Date.now() + 9000 }) },
|
|
878
|
+
});
|
|
879
|
+
|
|
880
|
+
const out = await hit(leadsAs('peer-other').admin, 'GET', '/api/lan-peers/group');
|
|
881
|
+
assert.equal(out.json.selfRole, 'member');
|
|
882
|
+
assert.equal(out.json.role, 'follower');
|
|
883
|
+
assert.equal(out.json.leader.peerId, 'peer-other', '门面的 leaderPeerId 压过配置的 group.hub');
|
|
884
|
+
assert.notEqual(out.json.leader.peerId, 'peer-hub');
|
|
885
|
+
assert.equal(out.json.leader.name, 'dsh-desktop', '名字从群成员表查');
|
|
886
|
+
assert.equal(out.json.leader.epoch, 3);
|
|
887
|
+
|
|
888
|
+
// 本机自己当 leader:成员表里有名字就用表里的,没有就用本机名(不是 UUID)
|
|
889
|
+
const named = await hit(leadsAs('member-inst').admin, 'GET', '/api/lan-peers/group');
|
|
890
|
+
assert.equal(named.json.leader.peerId, 'member-inst');
|
|
891
|
+
assert.equal(named.json.leader.name, 'me');
|
|
892
|
+
// 成员表里没有本机那条(还没进群状态):本机自己当 leader 时用自己的名字,不是 UUID
|
|
893
|
+
const unnamed = await hit(stubAdmin({
|
|
894
|
+
group: { enabled: true, hub: 'hub-box' },
|
|
895
|
+
api: apiWith({ isHub: false, lastSeq: 8 }),
|
|
896
|
+
peers: HUB_PEERS,
|
|
897
|
+
facade: { enabled: false, isHub: false, role: () => ({ role: 'leader', epoch: 3, leaderPeerId: 'inst-self' }) },
|
|
898
|
+
}).admin, 'GET', '/api/lan-peers/group');
|
|
899
|
+
assert.equal(unnamed.json.leader.peerId, 'inst-self', '本机(store.instanceId)当选');
|
|
900
|
+
assert.equal(unnamed.json.leader.name, 'self', '查不到时本机用自己的名字');
|
|
901
|
+
|
|
902
|
+
// 门面缺席 / leaderPeerId 为空:回退到配置的 group.hub(旧行为不破)
|
|
903
|
+
const legacy = await hit(stubAdmin({
|
|
904
|
+
group: { enabled: true, hub: 'hub-box' },
|
|
905
|
+
api: apiWith({ isHub: false, lastSeq: 8, members }),
|
|
906
|
+
peers: HUB_PEERS,
|
|
907
|
+
}).admin, 'GET', '/api/lan-peers/group');
|
|
908
|
+
assert.equal(legacy.json.leader.peerId, 'peer-hub', '没有角色门面时仍认配置的 hub');
|
|
909
|
+
assert.equal(legacy.json.leader.name, 'hub-box');
|
|
910
|
+
|
|
911
|
+
const emptyLeader = await hit(leadsAs('').admin, 'GET', '/api/lan-peers/group');
|
|
912
|
+
assert.equal(emptyLeader.json.leader.peerId, 'peer-hub', 'leaderPeerId 为空字符串等于没接线');
|
|
913
|
+
});
|
|
914
|
+
|
|
915
|
+
test('S3 · hub 且选举为 follower:leader.peerId 是真 leader(不是本机),selfRole 仍是 hub', async () => {
|
|
916
|
+
// 真机(0.7.2 NAS):配置上是 hub,但选举输给了 dsh-desktop。
|
|
917
|
+
const members = [
|
|
918
|
+
{ peerId: 'inst-self', name: 'fn-302', online: true, lastSeen: null, joinedAt: 1, baseUrl: 'http://192.168.5.51:7788', lastSeq: 8 },
|
|
919
|
+
{ peerId: 'peer-desktop', name: 'dsh-desktop', online: true, lastSeen: null, joinedAt: 2, baseUrl: 'http://192.168.5.20:7788', lastSeq: 8 },
|
|
920
|
+
];
|
|
921
|
+
const state = () => ({
|
|
922
|
+
groupId: 'g-1', name: 'default', epoch: 4, members, messages: MESSAGES, lastSeq: 8,
|
|
923
|
+
});
|
|
924
|
+
const followerHub = await hit(stubAdmin({
|
|
925
|
+
group: { enabled: true, hub: '' },
|
|
926
|
+
api: { ...makeApi(), isHub: true, state },
|
|
927
|
+
facade: { enabled: true, isHub: true, role: () => ({ role: 'follower', epoch: 4, leaderPeerId: 'peer-desktop', leaseUntil: Date.now() + 9000 }) },
|
|
928
|
+
}).admin, 'GET', '/api/lan-peers/group');
|
|
929
|
+
|
|
930
|
+
assert.equal(followerHub.json.selfRole, 'hub', 'selfRole 是「配置上的 hub」,与选举角色不是一个概念');
|
|
931
|
+
assert.equal(followerHub.json.role, 'follower', '选举角色如实透出');
|
|
932
|
+
assert.equal(followerHub.json.leader.peerId, 'peer-desktop', 'leader 必须是选举赢家,不能写死本机');
|
|
933
|
+
assert.notEqual(followerHub.json.leader.peerId, 'inst-self');
|
|
934
|
+
assert.equal(followerHub.json.leader.name, 'dsh-desktop', '名字从成员表里查');
|
|
935
|
+
assert.equal(followerHub.json.leader.epoch, 4);
|
|
936
|
+
|
|
937
|
+
// 成员表里查不到那个 peerId:名字退回 peerId(不编名字,也不回退本机)
|
|
938
|
+
const ghost = await hit(stubAdmin({
|
|
939
|
+
group: { enabled: true, hub: '' },
|
|
940
|
+
api: { ...makeApi(), isHub: true, state },
|
|
941
|
+
facade: { enabled: true, isHub: true, role: () => ({ role: 'follower', epoch: 4, leaderPeerId: 'peer-ghost' }) },
|
|
942
|
+
}).admin, 'GET', '/api/lan-peers/group');
|
|
943
|
+
assert.equal(ghost.json.leader.peerId, 'peer-ghost');
|
|
944
|
+
assert.equal(ghost.json.leader.name, 'peer-ghost', '查不到名字就用 peerId');
|
|
945
|
+
|
|
946
|
+
// 角色门面缺席(老 host / 未接线):回退到「hub 就是本机」的旧行为
|
|
947
|
+
const legacy = await hit(stubAdmin({
|
|
948
|
+
group: { enabled: true, hub: '' },
|
|
949
|
+
api: { ...makeApi(), isHub: true, state },
|
|
950
|
+
}).admin, 'GET', '/api/lan-peers/group');
|
|
951
|
+
assert.equal(legacy.json.leader.peerId, 'inst-self', '没有角色门面时回退本机');
|
|
952
|
+
assert.equal(legacy.json.leader.name, 'self');
|
|
953
|
+
});
|
|
954
|
+
|
|
866
955
|
test('S3 · 群状态快照里的角色字段是第二来源(门面缺席时也能显示)', async () => {
|
|
867
956
|
const leaseUntil = Date.now() + 9000;
|
|
868
957
|
const api = apiWith({ isHub: true, lastSeq: 8 });
|
package/lib/admin.mjs
CHANGED
|
@@ -339,15 +339,39 @@ export function createAdmin({
|
|
|
339
339
|
return typeof fromConfig === 'string' ? fromConfig : '';
|
|
340
340
|
});
|
|
341
341
|
|
|
342
|
+
/** 群状态成员表里的名字(找不到就是空串):给「真 leader 不是本机」时用。 */
|
|
343
|
+
const memberNameOf = (snapshot, peerId) => {
|
|
344
|
+
if (peerId === '') return '';
|
|
345
|
+
const list = Array.isArray(snapshot?.members) ? snapshot.members : [];
|
|
346
|
+
const found = list.find((member) => String(member?.peerId ?? '') === peerId);
|
|
347
|
+
return String(found?.name ?? '');
|
|
348
|
+
};
|
|
349
|
+
|
|
342
350
|
/**
|
|
343
|
-
* S1:leader —— hub 侧就是本机,成员侧就是 group.hub 指向的那个 peer。
|
|
351
|
+
* S1:leader —— 没有选举信息时,hub 侧就是本机,成员侧就是 group.hub 指向的那个 peer。
|
|
344
352
|
*
|
|
345
|
-
* S3
|
|
346
|
-
*
|
|
347
|
-
*
|
|
353
|
+
* S3:角色门面说了算的两个字段:
|
|
354
|
+
* - `epoch` 取选举世代(群元数据那份是 group.json 的 keyVersion 世代,真机上出现
|
|
355
|
+
* 过「leader.epoch=1 而状态行 epoch=3」的打架);
|
|
356
|
+
* - `leaderPeerId` 取真 leader(配置上是 hub 的机器可能输掉选举,那时 selfRole 仍是
|
|
357
|
+
* hub、role 是 follower,leader 必须是赢家,而不是本机)。
|
|
358
|
+
* 门面缺席(老 host / 未接线)时两者都回退到 S1 的旧口径,行为不变。
|
|
348
359
|
*/
|
|
349
360
|
const leaderOf = (snapshot, resolved, face = null) => {
|
|
350
361
|
const epoch = asSeqOrNull(face?.epoch) ?? asSeqOrNull(snapshot?.epoch);
|
|
362
|
+
const declared = face && typeof face === 'object' ? face.leaderPeerId : undefined;
|
|
363
|
+
const declaredId = declared == null ? '' : String(declared);
|
|
364
|
+
if (declaredId !== '') {
|
|
365
|
+
const fromMembers = memberNameOf(snapshot, declaredId);
|
|
366
|
+
// 成员表里查不到时:若声明的 leader 就是本机,仍用本机名称(别把 peerId 当名字显示);
|
|
367
|
+
// 是别的机器就退到 peerId(面板会原样显示,不会编名字)。
|
|
368
|
+
const name = fromMembers !== ''
|
|
369
|
+
? fromMembers
|
|
370
|
+
: (declaredId === String(store?.instanceId ?? '')
|
|
371
|
+
? String(typeof nameOf === 'function' ? nameOf() : '')
|
|
372
|
+
: declaredId);
|
|
373
|
+
return { peerId: declaredId, name, epoch };
|
|
374
|
+
}
|
|
351
375
|
if (resolved.isHub === true) {
|
|
352
376
|
return {
|
|
353
377
|
peerId: String(store?.instanceId ?? ''),
|