目录文档-技术白皮书07-EFT.WP.Core.Threads v1.0

附录B 策略模板与配置


I. 范围与约定

  1. 适用域
    并发控制、调度、背压、限流、超时与重试、幂等与去重、资源配额与隔离、到达时校准、可观测性与 SLO。
  2. 统一语法
    • 配置以键值为主,支持层级合并;时间以秒表示,字段名若以 _ms 结尾则为毫秒。
    • 策略对象以 policy 命名并可组合:policy = rate_limit ⊕ backpressure ⊕ retry ⊕ timeout ⊕ idempotency ⊕ quota。
  3. 关键符号
    • 速率与利用率: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 与线程池)

  1. 目标
    最小化 T_make(G),保障关键路径 crit(G);在 K_thr 与配额内最大化吞吐。
  2. 模板字段
    • 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"。
  3. 约束
    • T_make(G) approx sum(w on crit(G)) + sum(c on crit(G))。
    • 若 rho >= 1,降低就绪度或限流,避免漂移至不稳定区。

III. 通道与背压策略库

  1. 信号定义
    • 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 )。
  2. 策略族
    • 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 )。
  3. 建议阈值
    • 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. 限流策略模板(令牌桶/漏桶)

  1. 令牌桶
    • rate_limit.mode = "token_bucket";rate_limit.rps;rate_limit.burst;rate_limit.warmup_s。
    • 动态耦合:rps' = rps * ( 1 - bp ),当 bp 升高自动降速。
  2. 漏桶
    rate_limit.mode = "leaky_bucket";rate_limit.drain_rps;rate_limit.queue_cap。
  3. 失败语义
    超过限额返回 E_RATE_LIMIT 或等待至 timeout;与 retry 联动时需预算控制(见第 V 节)。

V. 超时与重试策略模板(含抖动)

  1. 下界
    timeout_floor = T_arr + J + P99(service),强制 timeout >= timeout_floor。
  2. 重试参数
    • retry.max;retry.backoff ∈ {"const","lin","exp"};retry.base_s;retry.jitter ∈ {"none","full"}。
    • 抖动实现建议:delay' = U(0, delay)(jitter="full")。
  3. 上界
    W_retry <= timeout * ( retries + 1 ) + J_total。
  4. 错误映射
    E_TIMEOUT|E_RATE_LIMIT|E_BACKPRESSURE 可重试;E_CONTRACT|E_DEDUP 不重试。
  5. 预算型重试
    若存在 deadline,保证 sum(planned_delays) + E[service_left] <= deadline - now()。

VI. 幂等与去重策略模板

  1. 键与窗口
    • idempotency.key = "idemp_key";idempotency.window_s = Delta_t_dedup。
    • 保证:f(x; idemp_key) = f(x; idemp_key)。
  2. 存储
    idempotency.store ∈ {"inmem","redis","db"};idempotency.ttl_s >= window_s。
  3. 合并策略
    merge ∈ {"first_wins","last_wins","combine"};combine_fn 需显式注册。

VII. 资源配额与隔离策略模板

  1. 作用域
    scope ∈ {"batch","online","stream","control"}。
  2. 配额字段
    • quota.cpu(核数或配额)、quota.mem、quota.io、quota.net、quota.gpu。
    • isolation.cgroup=true;affinity=[cpu_ids];numa.policy ∈ {"local","interleave"}。
  3. 建议基线
    • 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. 可观测性、告警与预算模板


IX. 到达时绑定与校准策略模板


X. 策略组合与优先级

  1. 决策顺序
    backpressure → rate_limit → timeout → retry → idempotency → quota。
  2. 冲突处理
    • 当 rate_limit 与 backpressure 同时触发,优先降低 rps',若 bp 仍高则切换 type="shed"。
    • 当 deadline 与 retry 冲突,优先满足 deadline,提前停止重试。

XI. 策略片段示例(可直接落地)

  1. 执行图与调度
    scheduler: { type: "work_steal", max_parallel: 8, prio_bias: 0.2, preemption: { quantum_ms: 5 } }
  2. 通道与背压
    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 } }
  3. 限流
    rate_limit: { mode: "token_bucket", rps: 1500, burst: 300, warmup_s: 10, dynamic: "rps' = rps * (1 - bp)" }
  4. 超时与重试
    • timeout: { seconds: 0.8, floor_expr: "T_arr + J + P99(service)" }
    • retry: { max: 3, backoff: "exp", base_s: 0.05, jitter: "full" }
  5. 幂等与去重
    idempotency: { key: "idemp_key", window_s: 120, store: "redis", merge: "first_wins" }
  6. 资源与隔离
    • quota: { cpu: 2, mem: "1Gi", io: "100MBps" }
    • isolation: { cgroup: true, affinity: [0,1], numa: { policy: "local" } }
  7. 可观测性与 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" } }
  8. 到达时校准
    arrival: { tol_form_ms: 1.0, equations: ["T_arr"], enforce: true }

XII. 契约化校验清单(断言模板)


XIII. 场景化预设(批/在线/流)


XIV. 版本与变更策略


XV. 速查表(关键公式与阈值)


版权与许可(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/