fix: 集群操作日志同步修复 — 全量回填 + 重启节点 seq 水位抬升

两个根因:
1. 离线错过条目永久缺失: token delta 被下游 trim (keep=seq>wm),
   离线节点错过的 seq 再也收不到 → 'log delta gap: want N got N+1' 死循环。
   修复: OnToken 改为附带自己的全量日志 (Snapshot),接收方 ApplyDelta 按 seq 幂等去重,
   缺口节点下一轮自动补齐。日志量小(几十条),开销可忽略。

2. 重启节点重新从 seq=1 编号: NewClusterLog 从 Seq=0 起,重启后产生的新事件
   与环上历史 seq 冲突 → ApplyDelta 视为 already-have 静默丢弃 + 全量附带出现歧义 id。
   修复: OnToken 收到日志时把本地 Seq 抬到环高水位之上,新编号接在历史之后。

测试: TestFullLogBackfillAfterGap / TestApplyDeltaIdempotentOnFullResend

实测: 三台集群撤销 minecraft 转发 → 三台均记录 seq=10 forward.remove;
恢复后三台均记录 seq=11 forward.add
This commit is contained in:
JianFeeeee
2026-08-25 11:55:34 +08:00
parent 94b6396738
commit 1abf1bb447
5 changed files with 350 additions and 218 deletions

View File

@ -1,28 +1,28 @@
// HTTP client and API functions for webui4frpc.
import type {
ApiKey,
ApiKeyCreated,
BinaryStatus,
CacheResp,
CanvasData,
CanvasExportEnvelope,
ClusterNodesResp,
InstallResult,
MeResp,
Remote,
RingSnapshot,
Settings,
StatusResp,
User,
WorkerLogBundle,
ApiKey,
ApiKeyCreated,
BinaryStatus,
CacheResp,
CanvasData,
CanvasExportEnvelope,
ClusterNodesResp,
InstallResult,
MeResp,
Remote,
RingSnapshot,
Settings,
StatusResp,
User,
WorkerLogBundle,
} from "./types";
class HTTPError extends Error {
status: number;
constructor(status: number, message: string) {
super(message);
this.status = status;
}
status: number;
constructor(status: number, message: string) {
super(message);
this.status = status;
}
}
// X-W4F-UI marks every SPA request. The server treats these as session-cookie
@ -31,219 +31,244 @@ class HTTPError extends Error {
const UI_HEADER = { "X-W4F-UI": "1" } as const;
async function request<T>(url: string, options: RequestInit = {}): Promise<T> {
const response = await fetch(url, {
credentials: "same-origin",
...options,
headers: { ...UI_HEADER, ...(options.headers as Record<string, string> | undefined) },
});
if (!response.ok) {
throw new HTTPError(response.status, `HTTP ${response.status}`);
}
const ct = response.headers.get("content-type") || "";
if (ct.includes("application/json")) {
return response.json() as Promise<T>;
}
return response.text() as unknown as Promise<T>;
const response = await fetch(url, {
credentials: "same-origin",
...options,
headers: {
...UI_HEADER,
...(options.headers as Record<string, string> | undefined),
},
});
if (!response.ok) {
throw new HTTPError(response.status, `HTTP ${response.status}`);
}
const ct = response.headers.get("content-type") || "";
if (ct.includes("application/json")) {
return response.json() as Promise<T>;
}
return response.text() as unknown as Promise<T>;
}
const json = (body: unknown): RequestInit => ({
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
export const api = {
status: () => request<StatusResp>("/api/manager/status"),
status: () => request<StatusResp>("/api/manager/status"),
canvas: () => request<CanvasData>("/api/manager/canvas"),
saveCanvas: (data: CanvasData) =>
request<CanvasData>("/api/manager/canvas", json(data)),
canvas: () => request<CanvasData>("/api/manager/canvas"),
saveCanvas: (data: CanvasData) =>
request<CanvasData>("/api/manager/canvas", json(data)),
settings: () => request<Settings>("/api/manager/settings"),
saveSettings: (s: Settings) =>
request<Settings>("/api/manager/settings", json(s)),
settings: () => request<Settings>("/api/manager/settings"),
saveSettings: (s: Settings) =>
request<Settings>("/api/manager/settings", json(s)),
binaryStatus: () => request<BinaryStatus>("/api/manager/binary/status"),
installBinary: (version?: string) =>
request<InstallResult>("/api/manager/binary/install", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: version ? JSON.stringify({ version }) : undefined,
}),
binaryStatus: () => request<BinaryStatus>("/api/manager/binary/status"),
installBinary: (version?: string) =>
request<InstallResult>("/api/manager/binary/install", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: version ? JSON.stringify({ version }) : undefined,
}),
saveRemote: (remote: Remote) =>
request<Remote>("/api/manager/remotes", json(remote)),
deleteRemote: (name: string) =>
request<void>(`/api/manager/remotes/${encodeURIComponent(name)}`, {
method: "DELETE",
}),
saveRemote: (remote: Remote) =>
request<Remote>("/api/manager/remotes", json(remote)),
deleteRemote: (name: string) =>
request<void>(`/api/manager/remotes/${encodeURIComponent(name)}`, {
method: "DELETE",
}),
// Per-forward start/stop (forwards page). local-only forwards toggle the
// local frpc worker; cluster forwards submit/revoke via the ring.
forwardStart: (local: string, remote: string, remotePort: number) =>
request<{ ok: boolean }>("/api/manager/forwards/start", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ local, remote, remotePort }),
}),
forwardStop: (local: string, remote: string, remotePort: number) =>
request<{ ok: boolean }>("/api/manager/forwards/stop", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ local, remote, remotePort }),
}),
groupStart: (group: string) =>
request<{ ok: boolean }>("/api/manager/forwards/group/start", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ group }),
}),
groupStop: (group: string) =>
request<{ ok: boolean }>("/api/manager/forwards/group/stop", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ group }),
}),
// assignGroup changes a single forward's group label (status page chip).
// Empty group clears the assignment (移出分组). Pure DB update, no worker.
assignGroup: (local: string, remote: string, remotePort: number, group: string) =>
request<{ ok: boolean }>("/api/manager/forwards/assign", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ local, remote, remotePort, group }),
}),
// deleteGroup dissolves a group: all members moved to 未分组. The group is
// just a label on links, so clearing all members is the complete delete.
deleteGroup: (group: string) =>
request<{ ok: boolean }>("/api/manager/forwards/group/delete", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ group }),
}),
// Per-forward start/stop (forwards page). local-only forwards toggle the
// local frpc worker; cluster forwards submit/revoke via the ring.
forwardStart: (local: string, remote: string, remotePort: number) =>
request<{ ok: boolean }>("/api/manager/forwards/start", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ local, remote, remotePort }),
}),
forwardStop: (local: string, remote: string, remotePort: number) =>
request<{ ok: boolean }>("/api/manager/forwards/stop", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ local, remote, remotePort }),
}),
groupStart: (group: string) =>
request<{ ok: boolean }>("/api/manager/forwards/group/start", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ group }),
}),
groupStop: (group: string) =>
request<{ ok: boolean }>("/api/manager/forwards/group/stop", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ group }),
}),
// assignGroup changes a single forward's group label (status page chip).
// Empty group clears the assignment (移出分组). Pure DB update, no worker.
assignGroup: (
local: string,
remote: string,
remotePort: number,
group: string,
) =>
request<{ ok: boolean }>("/api/manager/forwards/assign", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ local, remote, remotePort, group }),
}),
// deleteGroup dissolves a group: all members moved to 未分组. The group is
// just a label on links, so clearing all members is the complete delete.
deleteGroup: (group: string) =>
request<{ ok: boolean }>("/api/manager/forwards/group/delete", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ group }),
}),
profileStart: (name: string) =>
request<void>(`/api/manager/profiles/${encodeURIComponent(name)}/start`, {
method: "POST",
}),
profileStop: (name: string) =>
request<void>(`/api/manager/profiles/${encodeURIComponent(name)}/stop`, {
method: "POST",
}),
profileRestart: (name: string) =>
request<void>(`/api/manager/profiles/${encodeURIComponent(name)}/restart`, {
method: "POST",
}),
profileConfig: (name: string) =>
request<string>(`/api/manager/profiles/${encodeURIComponent(name)}/config`),
profileLogs: (name: string) =>
request<string>(`/api/manager/profiles/${encodeURIComponent(name)}/logs`),
profileStart: (name: string) =>
request<void>(`/api/manager/profiles/${encodeURIComponent(name)}/start`, {
method: "POST",
}),
profileStop: (name: string) =>
request<void>(`/api/manager/profiles/${encodeURIComponent(name)}/stop`, {
method: "POST",
}),
profileRestart: (name: string) =>
request<void>(`/api/manager/profiles/${encodeURIComponent(name)}/restart`, {
method: "POST",
}),
profileConfig: (name: string) =>
request<string>(`/api/manager/profiles/${encodeURIComponent(name)}/config`),
profileLogs: (name: string) =>
request<string>(`/api/manager/profiles/${encodeURIComponent(name)}/logs`),
// M6: cluster nodes + binary cache management.
clusterNodes: () => request<ClusterNodesResp>("/api/manager/cluster/nodes"),
clusterCache: () => request<CacheResp>("/api/manager/cluster/cache"),
pruneCache: (keep: number) =>
request<CacheResp>("/api/manager/cluster/cache", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ keep }),
}),
// M6: cluster nodes + binary cache management.
clusterNodes: () => request<ClusterNodesResp>("/api/manager/cluster/nodes"),
clusterCache: () => request<CacheResp>("/api/manager/cluster/cache"),
pruneCache: (keep: number) =>
request<CacheResp>("/api/manager/cluster/cache", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ keep }),
}),
// M6 token ring snapshot.
ring: () => request<RingSnapshot>("/api/manager/cluster/ring"),
// M6 token ring snapshot.
ring: () => request<RingSnapshot>("/api/manager/cluster/ring"),
// M6 cluster lifecycle (创建/加入/退出/移除).
// clusterCreate: reseed THIS node as a fresh standalone leader (创建集群).
clusterCreate: () =>
request<RingSnapshot>("/api/manager/cluster/create", { method: "POST" }),
// clusterJoinRing: THIS node joins the cluster at peer addr (加入集群).
// joinKey is the sponsor node's nodeKey — required for admission security.
clusterJoinRing: (addr: string, joinKey: string) =>
request<RingSnapshot>("/api/manager/cluster/join-ring", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ addr, joinKey }),
}),
// clusterRemoveNode: publish a node-removal command (移除节点 / 退出集群[self]).
clusterRemoveNode: (id: string) =>
request<{ task: unknown }>("/api/manager/cluster/node-remove", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ id }),
}),
// M6 cluster lifecycle (创建/加入/退出/移除).
// clusterCreate: reseed THIS node as a fresh standalone leader (创建集群).
clusterCreate: () =>
request<RingSnapshot>("/api/manager/cluster/create", { method: "POST" }),
// clusterJoinRing: THIS node joins the cluster at peer addr (加入集群).
// joinKey is the sponsor node's nodeKey — required for admission security.
clusterJoinRing: (addr: string, joinKey: string) =>
request<RingSnapshot>("/api/manager/cluster/join-ring", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ addr, joinKey }),
}),
// clusterRemoveNode: publish a node-removal command (移除节点 / 退出集群[self]).
clusterRemoveNode: (id: string) =>
request<{ task: unknown }>("/api/manager/cluster/node-remove", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ id }),
}),
// M7 auth/accounts/API keys/canvas export-import/worker logs.
me: () => request<MeResp>("/api/manager/me"),
// UI session login/logout. login() sets an HttpOnly session cookie (Set-Cookie
// on the 200 response) so the SPA stops using Basic Auth; logout clears it.
login: (username: string, password: string) =>
request<MeResp>("/api/manager/login", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ username, password }),
}),
logout: () =>
request<{ ok: boolean }>("/api/manager/logout", { method: "POST" }),
listUsers: () => request<{ users: User[] }>("/api/manager/users"),
createUser: (username: string, password: string, role: 'admin' | 'viewer' | 'superadmin') =>
request<User>("/api/manager/users", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ username, password, role }),
}),
updateUser: (name: string, patch: { password?: string; role?: 'admin' | 'viewer' | 'superadmin'; enabled?: boolean }) =>
request<User>(`/api/manager/users/${encodeURIComponent(name)}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(patch),
}),
deleteUser: (name: string) =>
request<void>(`/api/manager/users/${encodeURIComponent(name)}`, {
method: "DELETE",
}),
listApiKeys: () => request<{ apiKeys: ApiKey[] }>("/api/manager/apikeys"),
createApiKey: (userId: number, label: string, scope: 'read' | 'write' | 'admin') =>
request<ApiKeyCreated>("/api/manager/apikeys", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ userId, label, scope }),
}),
deleteApiKey: (id: number) =>
request<void>(`/api/manager/apikeys/${id}`, { method: "DELETE" }),
// M7 auth/accounts/API keys/canvas export-import/worker logs.
me: () => request<MeResp>("/api/manager/me"),
// UI session login/logout. login() sets an HttpOnly session cookie (Set-Cookie
// on the 200 response) so the SPA stops using Basic Auth; logout clears it.
login: (username: string, password: string) =>
request<MeResp>("/api/manager/login", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ username, password }),
}),
logout: () =>
request<{ ok: boolean }>("/api/manager/logout", { method: "POST" }),
listUsers: () => request<{ users: User[] }>("/api/manager/users"),
createUser: (
username: string,
password: string,
role: "admin" | "viewer" | "superadmin",
) =>
request<User>("/api/manager/users", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ username, password, role }),
}),
updateUser: (
name: string,
patch: {
password?: string;
role?: "admin" | "viewer" | "superadmin";
enabled?: boolean;
},
) =>
request<User>(`/api/manager/users/${encodeURIComponent(name)}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(patch),
}),
deleteUser: (name: string) =>
request<void>(`/api/manager/users/${encodeURIComponent(name)}`, {
method: "DELETE",
}),
listApiKeys: () => request<{ apiKeys: ApiKey[] }>("/api/manager/apikeys"),
createApiKey: (
userId: number,
label: string,
scope: "read" | "write" | "admin",
) =>
request<ApiKeyCreated>("/api/manager/apikeys", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ userId, label, scope }),
}),
deleteApiKey: (id: number) =>
request<void>(`/api/manager/apikeys/${id}`, { method: "DELETE" }),
// Canvas export/import (转发表 备份/还原).
exportCanvas: () => request<CanvasExportEnvelope>("/api/manager/canvas/export"),
importCanvas: (data: CanvasData | CanvasExportEnvelope) =>
request<CanvasData>("/api/manager/canvas/import", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
}),
// Canvas export/import (转发表 备份/还原).
exportCanvas: () =>
request<CanvasExportEnvelope>("/api/manager/canvas/export"),
importCanvas: (data: CanvasData | CanvasExportEnvelope) =>
request<CanvasData>("/api/manager/canvas/import", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
}),
// Worker-log bundle (HTTP fan-out across ring nodes; read-level/auditor).
exportWorkerLogs: () => request<WorkerLogBundle>("/api/manager/cluster/logs/export"),
// Worker-log bundle (HTTP fan-out across ring nodes; read-level/auditor).
exportWorkerLogs: () =>
request<WorkerLogBundle>("/api/manager/cluster/logs/export"),
};
// downloadAuditCsv streams one of the /audit/*.csv endpoints to a file.
export async function downloadAuditCsv(
kind: "users" | "apikeys" | "cluster-log" | "worker-logs",
kind: "users" | "apikeys" | "cluster-log" | "worker-logs",
): Promise<void> {
const resp = await fetch(`/api/manager/audit/${kind}.csv`, {
credentials: "same-origin",
headers: { ...UI_HEADER } as Record<string, string>,
});
if (!resp.ok) throw new Error(`HTTP ${resp.status}`);
const disposition = resp.headers.get("content-disposition") || "";
const m = disposition.match(/filename="?([^";]+)"?/);
const filename = m?.[1] ?? `audit-${kind}.csv`;
const blob = new Blob([await resp.text()], {
type: "text/csv;charset=utf-8",
});
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
const resp = await fetch(`/api/manager/audit/${kind}.csv`, {
credentials: "same-origin",
headers: { ...UI_HEADER } as Record<string, string>,
});
if (!resp.ok) throw new Error(`HTTP ${resp.status}`);
const disposition = resp.headers.get("content-disposition") || "";
const m = disposition.match(/filename="?([^";]+)"?/);
const filename = m?.[1] ?? `audit-${kind}.csv`;
const blob = new Blob([await resp.text()], {
type: "text/csv;charset=utf-8",
});
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}