任务
逐样本求序列差,除以每秒写入量得到延迟秒数;与阈值比较,输出最大延迟与超阈值样本的下标。
契约
- 输入:JSON 对象:{ samples: [{ primarySeq, replicaSeq }], throughputPerSecond, thresholdSeconds }
- 输出:JSON 对象:{ maxLagSeconds, lagging: number[] }
样例
- 输入
{"samples":[{"primarySeq":1000,"replicaSeq":1000},{"primarySeq":1200,"replicaSeq":1100}],"throughputPerSecond":100,"thresholdSeconds":2}-> 期望{"maxLagSeconds":1,"lagging":[]}。每秒 100 次写入下,100 的序列差即 1 秒,低于阈值。 - 输入
{"samples":[{"primarySeq":1500,"replicaSeq":1000}],"throughputPerSecond":100,"thresholdSeconds":2}-> 期望{"maxLagSeconds":5,"lagging":[0]}。500 的序列差是 5 秒,超出阈值。
参考实现
function solve({ samples, throughputPerSecond, thresholdSeconds }) {
const lags = samples.map((sample) => +((sample.primarySeq - sample.replicaSeq) / throughputPerSecond).toFixed(2));
const lagging = lags.map((lag, index) => (lag > thresholdSeconds ? index : -1)).filter((index) => index !== -1);
return { maxLagSeconds: Math.max(0, ...lags), lagging };
}
为什么有效
副本延迟会造成“写后读”失败。把延迟换算成秒并对阈值判断,路由决策才有客观依据。
报告超阈值样本的下标,运维才能在源数据里定位到对应时间点。