Paxos 的多决提案场景下的 Prepare、Accept、学习阶段失败补偿要幂等,重放不能二次伤害。本文给出 Paxos 的多决提案在分布式场景下的并发建议。

一、问题定位

容量评估用均值而不是分位数,是 Paxos 的多决提案最常见的误判:Prepare 的均值好看,Accept 场景下的 P99 早已越过红线。

1.1 从最简单的解释试起

Paxos 的多决提案场景下的边界在分布式里要能让维护者知道改动的代价:

  • 需求按粒度拆分(Paxos 的多决提案·Prepare):需求按粒度拆分,验收标准才能写清楚
  • 需求有优先级(Paxos 的多决提案·Prepare):需求有明确优先级,资源紧张时才知先做什么
  • 选型看生态成熟度(Paxos 的多决提案·Prepare):选型看生态成熟度,社区活跃度影响长期维护

二、按要点拆解:Paxos 的多决提案

2.1 Prepare

Prepare 的成本要有预算上限,分布式场景下无上限的支出会在月末集中暴露。

  • 准出清单:Prepare 要有准出清单,逐项确认后才能交付给下游。
  • 交付标准:Prepare 的交付标准要书面化,验收时照单核对不靠印象。
  • 测试证据:Prepare 的测试要留证据,跑了哪些用例、结果如何要可查。

2.2 Accept

Accept 的性能要能回归对比,分布式里无基线的改动会让退化难以发现。

  • 告警分层:Accept 场景下的告警要分级,全部同级别会让人全部忽略
  • 文档时效:Accept 的文档要标注时效,过期的文档比没有更糟
  • 文档评审:Accept 的文档要随变更一起评审,两张皮会让实现走样

2.3 学习阶段

学习阶段的资源要能按需申请,分布式场景下固定配额会在峰谷都浪费。

  • 成本核算:学习阶段要核算单位成本,避免性能提升靠堆资源
  • 效果验证:学习阶段上线后要验证效果,而不是发布即结束
  • 读己之写:学习阶段的读己之写一致性要靠会话粘滞或版本校验

把 Prepare、Accept、学习阶段的每一段都加上健康检查,链路状态才可见:

Prepare → Accept → 学习阶段

链路里 Prepare 的异常要能区分偶发与持续,处置优先级不同。

三、最小可运行实现

某分布式服务的 Prepare 重启后要预热,Accept 不等预热就放流量,启动瞬间出现大量超时。

解法是把 Prepare 的失败重试改成指数退避加抖动,Accept 侧同时加限流。两边同时改,Paxos 的多决提案场景下的重试风暴才避免。

分布式场景下,Prepare、Accept、学习阶段的主干逻辑可以抽成下面这段骨架:

// Paxos 的多决提案:Prepare、Accept 与学习阶段
package paxos

type Accept struct {
 N int         // 提案号
 V interface{} // 已接受的值
}

// Acceptor 按槽位记住承诺与接受状态
type Acceptor struct {
 promised map[int]int
 accepted map[int]Accept
}

func newAcceptor() *Acceptor {
 return &Acceptor{promised: map[int]int{}, accepted: map[int]Accept{}}
}

// Prepare 承诺不再接受编号更小的提案,并回传本槽位已接受的值
func (a *Acceptor) Prepare(slot, n int) (int, Accept, bool) {
 if n > a.promised[slot] {
  a.promised[slot] = n
 }
 acc, ok := a.accepted[slot]
 return a.promised[slot], acc, ok
}

// Accept 仅在编号不低于承诺时落盘
func (a *Acceptor) Accept(slot, n int, v interface{}) bool {
 if a.promised[slot] > n {
  return false
 }
 acc, ok := a.accepted[slot]
 if !ok || n >= acc.N {
  a.accepted[slot] = Accept{N: n, V: v}
 }
 return true
}

// Proposer 为单个槽位跑 Prepare/Accept 两阶段
type Proposer struct {
 ID        int
 Acceptors []*Acceptor
 Round     int
}

// Propose 先取多数派承诺并沿用编号最大的已接受值,再发起 Accept
func (p *Proposer) Propose(slot int, value interface{}) (interface{}, bool) {
 p.Round++
 n := p.Round*256 + p.ID // 提案号:轮次占高位,节点 ID 占低位
 quorum := len(p.Acceptors)/2 + 1

 maxN, chosen, promised := -1, value, 0
 for _, a := range p.Acceptors {
  _, acc, ok := a.Prepare(slot, n)
  promised++
  if ok && acc.N > maxN {
   maxN, chosen = acc.N, acc.V
  }
 }
 if quorum > promised {
  return nil, false // 未达多数派:可能活锁,退避后重试
 }
 ack := 0
 for _, a := range p.Acceptors {
  if a.Accept(slot, n, chosen) {
   ack++
  }
 }
 if quorum > ack {
  return nil, false
 }
 return chosen, true
}

// Learner 按槽位统计「同一值被多少 Acceptor 接受」,达多数派即视为已学习
type Learner struct {
 counts  map[int]map[interface{}]int
 learned map[int]bool
}

func (l *Learner) Learn(slot int, v interface{}, quorum int) bool {
 if l.counts[slot] == nil {
  l.counts[slot] = map[interface{}]int{}
 }
 l.counts[slot][v]++
 if l.counts[slot][v] >= quorum {
  l.learned[slot] = true
 }
 return l.learned[slot]
}

四、验证与调优

同一批流量模型下 Paxos 的多决提案场景下的对照(关闭其他变更,确保归因单一):

环节单次耗时关键路径占比优化思路
Prepare1.4ms38%把性能回归做成持续监控
Accept2.2ms27%用日志关联追踪一次请求的全链路
学习阶段3.1ms20%把错误按类型聚合,避免被单点刷屏

综合读数:40 万 QPS 起步、226ms 起步、86% 起步,终点 236 万、86ms、66%。

4.1 灰度策略

  • 阶段一(Paxos 的多决提案·Prepare):先在低并发下验证功能正确性
  • 阶段二(Paxos 的多决提案·Prepare):抬到目标并发,观察资源与错误率
  • 阶段三(Paxos 的多决提案·Prepare):叠加突发并发,重点看排队与背压
  • 阶段四(Paxos 的多决提案·Prepare):全量后把并发水位接入自动限流

4.2 观测面

Paxos 的多决提案场景下的观测要能发现「超时但没失败」的请求,它们同样消耗资源。

  • 链路(Paxos 的多决提案·Prepare):跨系统数据同步的链路要可观测,同步延迟与丢失都要能看到
  • 指标(Paxos 的多决提案·Prepare):同步延迟要分位数统计,平均值掩盖突发延迟
  • 日志(Paxos 的多决提案·Prepare):同步失败要带上下游标识,定位时才能区分责任

五、结论

Paxos 的多决提案场景下的恢复目标要写清楚,Prepare、Accept、学习阶段的优先级排序才有依据。

  • 代码要按路由分割(Paxos 的多决提案·Prepare):一次只加载当前页面需要的代码
  • 公共依赖要提取(Paxos 的多决提案·Prepare):多个页面共用的部分单独打包,缓存才能复用
  • 重复打包要消除(Paxos 的多决提案·Prepare):同一依赖被打进多个包,体积会成倍上涨

延伸阅读

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部