Files
streamline/control-server/test/node_utilization.test.ts
T
2026-11-16 17:26:44 -08:00

208 lines
8.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// MUST be first: points DATABASE_URL at a closed local port before db_client is
// evaluated, so importing the module under test can't reach production.
import "./setup_env";
import { test, expect, describe } from "bun:test";
import { rollupUtilization, MAX_WINDOW_DAYS, type RollupInput } from "../src/node_utilization";
/**
* 只测折算口径这一段纯函数。它值得测,是因为每一条取舍都不是「自然而然」的,
* 而且错了以后面板照样画得出一张漂亮的图 —— 空置率从 69% 变成 83% 不会有任何
* 报错,只会让人得出「机器买多了」的相反结论。这里把每条口径钉成一个用例:
* 多轮全算、卡死单列、分母并上无心跳的执行分钟、同节点重叠去重、等待归属给
* 认领者、在队与未派发分开报。
*
* 取数那半截(只认 forward、往前多捞 90 天)连库,留给线上探针。
*/
const MIN = 60_000;
const NOW = Date.UTC(2026, 8, 17, 12, 0, 0);
const DAY = 86_400_000;
const SINCE = NOW - DAY;
/** 从窗口起点往后 n 分钟的时刻。用相对量写用例,读的人不必换算 epoch。 */
const at = (min: number) => SINCE + min * MIN;
function input(over: Partial<RollupInput> = {}): RollupInput {
return {
txRows: [], heartbeats: [], clusters: [], registered: [],
sinceMs: SINCE, now: NOW, days: 1, ...over,
};
}
/** 整个窗口都在心跳的一台机器 —— 大多数用例要的是干净的分母。 */
const fullHeartbeat = (nodeId: string) =>
Array.from({ length: DAY / MIN }, (_, i) => ({ nodeId, at: at(i) }));
const tx = (
targetId: string, toStatus: string, min: number,
nodeId: string | null = "n1", round: number | null = 0,
) => ({ targetId, kind: "bug", toStatus, nodeId, round, ts: at(min) });
const only = (r: ReturnType<typeof rollupUtilization>) => {
expect(r.nodes.length).toBe(1);
return r.nodes[0];
};
describe("占用:多轮必须全算", () => {
test("同一任务的三轮执行分别计入,不是只算第一轮", () => {
const r = rollupUtilization(input({
heartbeats: fullHeartbeat("n1"),
txRows: [
tx("t1", "fixing", 0, "n1", 0), tx("t1", "manual_review", 60, "n1", 0),
tx("t1", "fixing", 120, "n1", 1), tx("t1", "manual_review", 180, "n1", 1),
tx("t1", "fixing", 240, "n1", 2), tx("t1", "fix_succeeded", 300, "n1", 2),
],
}));
const n = only(r);
expect(n.busyMin).toBe(180); // 三段各 60 分钟
expect(n.firstRunMin).toBe(60); // round 0
expect(n.reworkMin).toBe(120); // round 1 + 2
});
test("只算首轮会把空置率抬高 —— 这个用例就是防这条回归", () => {
const r = rollupUtilization(input({
heartbeats: fullHeartbeat("n1"),
txRows: [
tx("t1", "fixing", 0, "n1", 0), tx("t1", "manual_review", 360, "n1", 0),
tx("t1", "fixing", 400, "n1", 1), tx("t1", "fix_succeeded", 1000, "n1", 1),
],
}));
const n = only(r);
// 只认首轮的话占用会是 360,空置率 75%;全算是 960,空置率 33%。
expect(n.busyMin).toBe(960);
expect(Math.round(100 - (n.busyMin / n.onlineMin) * 100)).toBe(33);
});
});
describe("卡死占用单列", () => {
test("单段超过 8 小时的执行既计入占用,也计入 stuckMin", () => {
const r = rollupUtilization(input({
heartbeats: fullHeartbeat("n1"),
txRows: [tx("t1", "fixing", 0, "n1", 0), tx("t1", "fix_failed", 600, "n1", 0)],
}));
const n = only(r);
expect(n.busyMin).toBe(600);
expect(n.stuckMin).toBe(600); // 10 小时 > 8 小时阈值
});
test("刚好没超过阈值的长任务不算卡死", () => {
const r = rollupUtilization(input({
heartbeats: fullHeartbeat("n1"),
txRows: [tx("t1", "fixing", 0, "n1", 0), tx("t1", "fix_succeeded", 479, "n1", 0)],
}));
expect(only(r).stuckMin).toBe(0); // 7h59m
});
});
describe("分母 = 心跳 ∪ 在执行", () => {
test("在跑任务但那几分钟没有心跳行时,这些分钟仍进分母", () => {
// 只有前 60 分钟有心跳,但任务从第 0 分钟跑到第 120 分钟。
const r = rollupUtilization(input({
heartbeats: Array.from({ length: 60 }, (_, i) => ({ nodeId: "n1", at: at(i) })),
txRows: [tx("t1", "fixing", 0, "n1", 0), tx("t1", "fix_succeeded", 120, "n1", 0)],
}));
const n = only(r);
expect(n.onlineMin).toBe(120); // 60 心跳 ∪ 120 执行
expect(n.unheartbeatedMin).toBe(60); // 后 60 分钟在跑但没心跳
expect(n.busyMin).toBe(120);
// 光用心跳做分母的话,这里会算出 60/60 = 0% 空置,把缺口藏起来。
expect(100 - (n.busyMin / n.onlineMin) * 100).toBe(0);
});
});
describe("同节点重叠去重", () => {
test("同一台机器上时间重叠的两个任务只算一次,并记进 overlapMin", () => {
const r = rollupUtilization(input({
heartbeats: fullHeartbeat("n1"),
txRows: [
tx("t1", "fixing", 0, "n1", 0), tx("t1", "fix_succeeded", 60, "n1", 0),
tx("t2", "fixing", 30, "n1", 0), tx("t2", "fix_succeeded", 90, "n1", 0),
],
}));
const n = only(r);
expect(n.busyMin).toBe(90); // 0–90,不是 60+60
expect(n.overlapMin).toBe(30); // 30–60 这段重叠
});
});
describe("等待归属给认领它的机器", () => {
test("绑在 A 上、被 B 抢走的任务,等待记在 B 头上", () => {
const r = rollupUtilization(input({
heartbeats: [...fullHeartbeat("A"), ...fullHeartbeat("B")],
registered: [{ id: "A", platform: "ios" }, { id: "B", platform: "ios" }],
txRows: [
// 排队时还绑在 A 上
tx("t1", "awaiting_fix", 0, "A", 0),
// 被 B 认领 —— 转入执行那条流转上的 node 才是认领者
tx("t1", "fixing", 45, "B", 0),
tx("t1", "fix_succeeded", 100, "B", 0),
],
}));
const a = r.nodes.find((n) => n.nodeId === "A")!;
const b = r.nodes.find((n) => n.nodeId === "B")!;
expect(a.waitSamplesMs).toEqual([]);
expect(b.waitSamplesMs).toEqual([45 * MIN]);
});
test("排到一半被归档的不算一次等待(没有认领者,归给谁都是错的)", () => {
const r = rollupUtilization(input({
heartbeats: fullHeartbeat("n1"),
txRows: [tx("t1", "awaiting_fix", 0, "n1", 0), tx("t1", "archived", 30, "n1", 0)],
}));
expect(only(r).waitSamplesMs).toEqual([]);
});
});
describe("在队与未派发分开报", () => {
test("仍在排队的任务不进等待样本,只进 queuedNow", () => {
const r = rollupUtilization(input({
heartbeats: fullHeartbeat("n1"),
txRows: [tx("t1", "awaiting_fix", 30, "n1", 0)],
}));
const n = only(r);
expect(n.waitSamplesMs).toEqual([]); // 没有终点,进了分布就是编造
expect(n.queuedNow).toBe(1);
expect(n.queuedOldestMs).toBe((DAY / MIN - 30) * MIN);
});
test("卡在评估、没有归属节点的任务归到 unbound,不挂在任何机器上", () => {
const r = rollupUtilization(input({
heartbeats: fullHeartbeat("n1"),
txRows: [
tx("t1", "initializing", 10, null, 0),
tx("t2", "awaiting_fix", 20, null, 0),
],
}));
expect(r.unbound.count).toBe(2);
expect(r.nodes.every((n) => n.queuedNow === 0)).toBe(true);
});
});
describe("窗口边界", () => {
test("跨越窗口左边界的执行段只计入窗口内的那部分", () => {
const r = rollupUtilization(input({
heartbeats: fullHeartbeat("n1"),
txRows: [
{ targetId: "t1", kind: "bug", toStatus: "fixing", nodeId: "n1", round: 0, ts: SINCE - 120 * MIN },
tx("t1", "fix_succeeded", 60, "n1", 0),
],
}));
expect(only(r).busyMin).toBe(60); // 窗口外那 120 分钟不算
});
test("最后仍在执行的任务用 now 收口", () => {
const r = rollupUtilization(input({
heartbeats: fullHeartbeat("n1"),
txRows: [tx("t1", "fixing", DAY / MIN - 30, "n1", 0)],
}));
expect(only(r).busyMin).toBe(30);
});
});
test("保留期常量与 node_metrics 的清理窗口一致", () => {
// 窗口超过保留期,分母就会缺一块而没有任何报错。改 pruneNodeMetrics 的
// RETENTION_MS 时必须同步改这里。
expect(MAX_WINDOW_DAYS).toBe(30);
});