目录 / 文档-技术白皮书 / 07-EFT.WP.Core.Threads v1.0
I. 范围与约定
- 适用域
并发控制、调度、背压、限流、超时与重试、幂等与去重、资源配额与隔离、到达时校准、可观测性与 SLO。 - 统一语法
- 配置以键值为主,支持层级合并;时间以秒表示,字段名若以 _ms 结尾则为毫秒。
- 策略对象以 policy 命名并可组合:policy = rate_limit ⊕ backpressure ⊕ retry ⊕ timeout ⊕ idempotency ⊕ quota。
- 关键符号
- 速率与利用率:lambda、mu、rho = lambda / mu。
- 队列与时延:L、L_q、W、W_q,且 L = lambda * W。
- 到达时两口径:T_arr = ( 1 / c_ref ) * ( ∫ n_eff d ell );T_arr = ( ∫ ( n_eff / c_ref ) d ell )。
- 口径差:delta_form = | ( 1 / c_ref ) * ( ∫ n_eff d ell ) - ( ∫ ( n_eff / c_ref ) d ell ) |。
II. 调度策略模板(DAG 与线程池)
- 目标
最小化 T_make(G),保障关键路径 crit(G);在 K_thr 与配额内最大化吞吐。 - 模板字段
- scheduler.type ∈ {"work_steal","fair","priority"}。
- scheduler.max_parallel = K_thr;scheduler.affinity = [cpu_id...];scheduler.prio_bias ∈ [-1,1]。
- scheduler.preemption.quantum_ms ∈ [1,50];scheduler.queue = "mpmc"。
- 约束
- T_make(G) approx sum(w on crit(G)) + sum(c on crit(G))。
- 若 rho >= 1,降低就绪度或限流,避免漂移至不稳定区。
III. 通道与背压策略库
- 信号定义
- bp = f(q_len, cap, W_q),取 bp ∈ [0,1]。
- 参考函数:bp = clamp( alpha*(q_len/cap) + beta*(W_q/W_q_target) + gamma*max(0, rho-1), 0, 1 )。
- 策略族
- type="block":生产者在 bp >= th_block 阈值阻塞。
- type="drop":在 bp >= th_drop 丢弃最新或最旧项,drop_policy ∈ {"newest","oldest"}。
- type="shed":向上游回送 E_BACKPRESSURE,触发限流协商。
- type="resize":cap' = clamp( cap * (1 + k*(bp - bp_target)), cap_min, cap_max )。
- 建议阈值
- th_block = 0.6,th_drop = 0.8,bp_target = 0.5,alpha=0.7,beta=0.2,gamma=0.1。
- 保持 rho < 1 以近似稳定;给出 lambda、mu 的估计来源。
IV. 限流策略模板(令牌桶/漏桶)
- 令牌桶
- rate_limit.mode = "token_bucket";rate_limit.rps;rate_limit.burst;rate_limit.warmup_s。
- 动态耦合:rps' = rps * ( 1 - bp ),当 bp 升高自动降速。
- 漏桶
rate_limit.mode = "leaky_bucket";rate_limit.drain_rps;rate_limit.queue_cap。 - 失败语义
超过限额返回 E_RATE_LIMIT 或等待至 timeout;与 retry 联动时需预算控制(见第 V 节)。
V. 超时与重试策略模板(含抖动)
- 下界
timeout_floor = T_arr + J + P99(service),强制 timeout >= timeout_floor。 - 重试参数
- retry.max;retry.backoff ∈ {"const","lin","exp"};retry.base_s;retry.jitter ∈ {"none","full"}。
- 抖动实现建议:delay' = U(0, delay)(jitter="full")。
- 上界
W_retry <= timeout * ( retries + 1 ) + J_total。 - 错误映射
E_TIMEOUT|E_RATE_LIMIT|E_BACKPRESSURE 可重试;E_CONTRACT|E_DEDUP 不重试。 - 预算型重试
若存在 deadline,保证 sum(planned_delays) + E[service_left] <= deadline - now()。
VI. 幂等与去重策略模板
- 键与窗口
- idempotency.key = "idemp_key";idempotency.window_s = Delta_t_dedup。
- 保证:f(x; idemp_key) = f(x; idemp_key)。
- 存储
idempotency.store ∈ {"inmem","redis","db"};idempotency.ttl_s >= window_s。 - 合并策略
merge ∈ {"first_wins","last_wins","combine"};combine_fn 需显式注册。
VII. 资源配额与隔离策略模板
- 作用域
scope ∈ {"batch","online","stream","control"}。 - 配额字段
- quota.cpu(核数或配额)、quota.mem、quota.io、quota.net、quota.gpu。
- isolation.cgroup=true;affinity=[cpu_ids];numa.policy ∈ {"local","interleave"}。
- 建议基线
- online: quota.cpu=1-2、quota.mem="512Mi-1Gi"、burst=10%。
- stream: quota.cpu=2-4、quota.mem="2Gi"、cap >= 10 * rps_target * W_q_target。
- batch: quota.cpu>4,优先级降低 prio=-1。
VIII. 可观测性、告警与预算模板
- SLI 集
延迟:svc.latency_ms{quantile};可用性:1 - ErrRate;队列:chan.q_len;背压:bp。 - SLO
SLO.latency.P99 <= target_ms;SLO.err_budget = 1 - SLO.availability。 - 预算燃尽告警
窗口 SLA_window;双门限:burn_rate ∈ {2h,1h} 两档;超阈触发降级或限流。 - 追踪
对关键节点创建 trace_span,将 eid、pid_thr 以 trace_link 关联,维护 hb。
IX. 到达时绑定与校准策略模板
- 数据字段
gamma(ell)、d ell、L_gamma = ( ∫ 1 d ell )、n_eff(x,t)、c_ref。 - 校准流程
计算两口径 T_arr,得 delta_form;满足 delta_form <= tol_form_ms 才通过。 - 建议
tol_form_ms = 1(或按场景设定);对外报告以 ts 记录,内部评估以 tau_mono。
X. 策略组合与优先级
- 决策顺序
backpressure → rate_limit → timeout → retry → idempotency → quota。 - 冲突处理
- 当 rate_limit 与 backpressure 同时触发,优先降低 rps',若 bp 仍高则切换 type="shed"。
- 当 deadline 与 retry 冲突,优先满足 deadline,提前停止重试。
XI. 策略片段示例(可直接落地)
- 执行图与调度
scheduler: { type: "work_steal", max_parallel: 8, prio_bias: 0.2, preemption: { quantum_ms: 5 } } - 通道与背压
chan: { name: "ingress", cap: 10000, bp: { type: "resize", alpha: 0.7, beta: 0.2, gamma: 0.1, bp_target: 0.5, cap_min: 2000, cap_max: 20000 } } - 限流
rate_limit: { mode: "token_bucket", rps: 1500, burst: 300, warmup_s: 10, dynamic: "rps' = rps * (1 - bp)" } - 超时与重试
- timeout: { seconds: 0.8, floor_expr: "T_arr + J + P99(service)" }
- retry: { max: 3, backoff: "exp", base_s: 0.05, jitter: "full" }
- 幂等与去重
idempotency: { key: "idemp_key", window_s: 120, store: "redis", merge: "first_wins" } - 资源与隔离
- quota: { cpu: 2, mem: "1Gi", io: "100MBps" }
- isolation: { cgroup: true, affinity: [0,1], numa: { policy: "local" } }
- 可观测性与 SLO
- sli: { latency_ms: [50,90,99], error_rate: true, q_len: true, bp: true }
- slo: { p99_ms: 200, availability: 0.999, window: "28d", err_budget_burn: { fast: "1h", slow: "24h" } }
- 到达时校准
arrival: { tol_form_ms: 1.0, equations: ["T_arr"], enforce: true }
XII. 契约化校验清单(断言模板)
- {"type":"rho_lt_1","chan":"ingress","lambda":"obs","mu":"obs"}
- {"type":"deadline","expr":"T_make <= 180000 ms"}
- {"type":"arrival_form_consistency","tol_form_ms":1.0}
- {"type":"timeout_floor","expr":"timeout >= T_arr + J + P99_service"}
- {"type":"ell_monotonic","field":"ell_seq","strict":true}
XIII. 场景化预设(批/在线/流)
- 批处理
scheduler.type="work_steal";retry.max=0-1;rate_limit 关闭;quota.cpu>4;bp.type="block"。 - 在线服务
rate_limit.rps = QPS_target * 1.1;burst=QPS_target * 0.2;retry.backoff="exp";timeout 取 P99(service)+margin;bp.type="shed"。 - 事件流
chan.cap >= 10 * rps_target * W_q_target;bp.type="resize";retry.max<=2;idempotency.window_s >= watermark_s。
XIV. 版本与变更策略
- schema_version 采用语义化;破坏性更改 major+1,同步提供兼容层。
- 任一策略变更需输出 diff、Trace=[source -> method -> artifact] 与 signature。
- 变更发布需在 SLA_window 内完成灰度与回滚预案。
XV. 速查表(关键公式与阈值)
- 稳定性:rho = lambda / mu < 1。
- 背压:bp = clamp( alpha*(q_len/cap) + beta*(W_q/W_q_target) + gamma*max(0, rho-1), 0, 1 )。
- 降速:rps' = rps * ( 1 - bp )。
- 超时下界:timeout >= T_arr + J + P99(service)。
- 重试上界:W_retry <= timeout * ( retries + 1 ) + J_total。
- 到达时一致:delta_form <= tol_form_ms。
版权与许可(CC BY 4.0)
版权声明:除另有说明外,《能量丝理论》(含文本、图表、插图、符号与公式)的著作权由作者(“屠广林”先生)享有。
许可方式:本作品采用 Creative Commons 署名 4.0 国际许可协议(CC BY 4.0)进行许可;在注明作者与来源的前提下,允许为商业或非商业目的进行复制、转载、节选、改编与再分发。
署名格式(建议):作者:“屠广林”;作品:《能量丝理论》;来源:energyfilament.org;许可证:CC BY 4.0。
首次发布: 2025-11-11|当前版本:v5.1
协议链接:https://creativecommons.org/licenses/by/4.0/