Distributed Systems-白话Paxos

本文主要提炼了《Paxos Make Simple》中的一些主要观点,然后加上自己的理解,使用通俗的语言尝试做一些解释。

  • 关于Paxos算法背景和一致性相关问题可以参见原论文
  • 算法涉及的主要对象
    • action 对一条记录(某个变量)的一次操作(这一点只是本人便于后面理解加上的)
      • 这里选用操作这个词,而不是值,因为一个在对某个变量达成某个值的共识前可能已经经过多个更新操作,所以为了区别,使用操作作为每次proposal的对象,而操作的值代表具体的修改动作,而且这也算是状态机复制(SMR)的一个基本组成单元,个人感觉更易于理解。比如action(log_id, log_content),log_id全局标识了此action的唯一性,log_content通常是针对某条记录的修改,可看做action的值。
    • proposer
      • 发起proposal的机器,注意在Paxos算法中允许多台机器同时发起proposal,并且有可能由于并发获取”需要达成一致的下一操作(action)”,从而使得不同的proposal针对同一个”需要达成一致的下一操作”达成共识,但是算法保证了其达成共识的action的值相同。
    • acceptor
      • 接受来自proposer的proposal,并根据对于proposer的prepare和accept消息做出响应。
    • learner
      • 从错误中恢复的机器,需要重新学习出错之前最后一次accpet的proposal id之后的所有proposal
  • Paxos instance
    • 针对某个”需要达成一致的操作(action)”运行一次Paxos算法的完整过程。
  • 算法推导逻辑
    • P0. To ensure data integrity under fault tolerence, a proposal is succeeded only when more than half machines accepted the proposal.
    • notice: P1[a] stands for requirement and algorithm for acceptors; P2[abc] stands for requirement and algorithm for proposer.
    • P1. An acceptor must accept the first proposal that it receives
      => problem : maybe two proposal are proposed at the same time and two less-than-half machine quorums receive separately these two different proposals, then these two proposals can not be succeeded.
      => so we must allow each acceptor to receive multiple proposals for the same value
      => so we must give each proposal a global unique and increasing id
    • P1a. An acceptor can accept a proposal numbered n iff it has not responed to a prepare request having a number greater than n
      => P1a -> P1 because this can ensure acceptor do not receive the before proposals which arrive later
      => so we ignore these proposal whose id is <= accepted and prepared id of acceptors
    • P2. If a proposal with value v is chosen, then every higher-numbered proposal that is chosen has value v
      => this is because we must ensure there is only a specific value chosen for a specific paxos instance which may contains multiple proposals
    • P2a. if a proposal with value v is chosen, then every higher-numbered proposal accepted by any acceptor has value v
      => notice: P2a -> P2
    • P2b. if a proposal with value v is chosen, then every higher-numbered proposal issued by any proposer has value v
      => notice: P2b -> P2a -> P2
    • P2c. for any v and n, if a proposal with value v and number n is issued, then there is a set S consisting of a majority of acceptors such that either
      • (a) no acceptor in S has accepted any proposal numbered less than n
      • (b) v is set to the value of the highest-numbered proposal among all proposals numbered less than n and accepted by the acceptors in S
        => notice: P2c -> P2b -> P2a -> P2
        => this is the specific algorithm for proposer in prepare phase
    • 总结
      • 根据上面的推导,核心的就两点,P1a和P2c,P1a规定了acceptor的行为,P2c规定了proposer的行为,由于P2c的需求,决定了需要有prepare阶段,这阶段主要是为了accept阶段为当前proposal设置正确的值。
  • 算法基本流程
    论文上主要有prepare和accept两个阶段,省略了选action(值)和选proposal id的阶段

    • 0.数据结构
      • 每台机器需要记录最大accpeted的proposal id(latest_accepted_id)和对应的accepted的操作(latest_accepted_action)以及最大promised的proposal id(latest_promised_id),这些数据需要刷盘。
    • 1.选择需要达成一致的操作
      • 来自客户端的请求,比如 Action{write A 12} => 通常这作为需要达成的某个操作的值,还需要一个全局唯一的id标识这个操作,比如对于某个log记录达成一致,需要寻找下一次需要记录的log id,这就需要向其他节点询问其记录的最近log id,并取最大值+1作为下一次需要达成一致的”记录日志这个操作(action)”的action(log) id。而这个过程可能会产生并发问题,即不同的机器可能针对同一个log id发起proposal,这一点后面阶段保证了一旦达成了proposal,则后续所有proposal都以相同的操作(值)达成。
    • 2.选择proposal id
      • proposal id需要保证全局唯一递增(这个后面补充)。
    • 3.prepare
      • 假设2中选择的proposal id为n,proposer发送prepare(n)给大多数机器
        • 对于acceptor,如果(n > latest_promised_id) /\ (n > latest_promised_id)
          • 如果acceptor已经有latest_accepted_id(说明之前对于同一个操作已经达到proposal了),则返回对应于latest_accepted_id的操作的值,为了accept阶段保证当前的proposal和以前已经达成的proposal最终操作值一样。
          • 如果acceptor没有latest_accepted_id(说明之前还未达成proposal),则不用返回值(accept阶段可以使用任意proposed的值) 
          • 令latest_accepted_id = latest_promised_id = n,并保证不再接受proposal id小于latest_promised_id的proposal
        • 否则acceptor返回拒绝,重新开始算法。
    • 4.accept
      • proposer收到大多数的机器对prepare的回复
        • 如果返回消息中latest_accepted_action集合不为空,则将当前proposal的action设置为对应于最大latest_accepted_id的latest_accepted_action,发送accept(n, action)消息
        • 如果返回消息中latest_accepted_action集合为空,则直接使用当前proposal的action(也就是论文中所说的any value),发送accept(n, action)消息 
      • 如果acceptor收到accept(n, action)消息时
        • latest_promised_id > n(说明有更新的),则放弃当前proposal,重新进入算法。
        • 否则接受proposal,完成此次proposal
      • 如果proposer收到大多数acceptor的成功消息,则成功返回给客户端,否则重新进入算法,由于liveness requirement,一个proposed的value必须eventually chosen,所以要么客户端返回成功,要么客户端请求超时,对于超时,客户端需要重新发起读的请求,此时可能已经成功了,否则继续重新发超时请求。
  • 几点辅助理解的说明
    • 多数是为了保证至少会有一台机器记录了上次达成的proposal的值,这样保证在不多于n/2台机器挂掉的条件下,在每次proposal的过程中,至少有一台机器有前面所有的proposal值的记录,从而保证所有的数据的完整。
    • 一轮paxos instance 是针对某个变量的一次操作的,而不是同一个变量。比如针对同一变量的一次操作打一次log,而这个log id应当是唯一的,而且针对这条log可能会有多次proposal,但是只要有一次proposal已经达成,那么针对这条log的proposal只能使用相同的log值更新(这也是为什么在prepare返回阶段,如果有一个acceptor已经达成过proposal,则返回其值替换当前proposal值)。
    • 对某个唯一的记录比如log或者变量的某次操作达成一致,那么proposer在发起proposal之前必定要到某个地方取下次需要达成一致的值,比如下一条日志记录的id,某个变量的下一个版本(某个变量的下一次操作)。而由于proposer可能有多个,那么在并发发起proposal时,不同的proposal可能会针对相同的某次操作,这时对于后达成的proposal来说,只能将其propose的值换为已经达成的proposal的值,而这个过程是通过prepare阶段accptor返回的结果集是否空来判断的。如果结果集不为空,说明针对此次操作,之前已经达成了一致,则后续proposal只能使用相同值;如果为空,那么可以使用此次proposal的值(也就是论文中所说的any value)。另外,在accept阶段,如果有accptor的最小promise id大于当前proposal id,那么说明已经有更更大proposal id的proposal先到达了(此时不管之前是否已经达成一致),此时需要放弃当前次的proposal
  • ref:
时间: 2024-08-22 14:49:20

Distributed Systems-白话Paxos的相关文章

Chubby - lock service for loosely-coupled distributed systems

1 Introduction a lock service called Chubby, It is intended for use within a loosely-coupled distributed system consisting of moderately large numbers of small machines connected by a high-speed network. We expected Chubby to help developers deal wit

分布式系统(Distributed System)资料

原文地址:https://github.com/ty4z2008/Qix/blob/master/ds.md 希望转载的朋友,你可以不用联系我.但是**一定要保留原文链接**,因为这个项目还在继续也在不定期更新.希望看到文章的朋友能够学到更多. <Reconfigurable Distributed Storage for Dynamic Networks> 介绍:这是一篇介绍在动态网络里面实现分布式系统重构的paper.论文的作者(导师)是MIT读博的时候是做分布式系统的研究的,现在在NUS

Distributed Systems-Basics

This post is a simple outline about some basic(really basic) ideas behind distributed systems and I will add more (detail) stuff sometimes according to my narrow understanding. (update 2015.12.2) I maintains a reading list for distributed systems at

bigtable: A Distributed Storage System for Structured Data

bigtable: A Distributed Storage System for Structured Data http://static.googleusercontent.com/external_content/untrusted_dlcp/research.google.com/en//archive/bigtable-osdi06.pdf http://www.dbthink.com/?p=493, 中文翻译   总结 A Bigtable is a sparse, distri

Paxos Made Simple

The Part-Time Parliament,Lamport,1998,ACM Transactions on Computer Systems. 晦涩的原文 http://research.microsoft.com/en-us/um/people/lamport/pubs/lamport-paxos.pdf Paxos Made Simple http://www.cs.utexas.edu/users/lorenzo/corsi/cs380d/past/03F/notes/paxos-

一篇文章带你了解Paxos算法

本文讲的是一篇文章带你了解Paxos算法,[编者的话]本文是Quora上关于Paxos算法的回答,两位答者分别从不同的角度描述Paxos算法.Vineet Gupta的回答细致入微,更偏向理论.Russell Cohen用具体的例子讲解Paxos算法,相辅相成. Vineet Gupta的回答 有很多关于一致性(consensus)问题的解决方案,而这些解决方案中,我认为Paxos相对来说很好理解. 『达成一致性』最简单的例子就是结婚誓词: "你愿意......."(男:)"

Distributed Systems-Materials

最近整理了下自己接触过的一些分布式系统与并行并发相关的一些不错资料,主要包括论文.书籍.MOOC.博客.社交问答等,由于太多,没有全部看完,所以质量可能因人而异,但是还是希望对感兴趣的同学有点帮助吧.主要涉及的内容包括(后面会继续更新): 资料地址 a reading list for distributed systems 分布式系统相关理论 time, order, logic clock, vector clock- fault-tolerence replication and cons

(转)分布式深度学习系统构建 简介 Distributed Deep Learning

HOME ABOUT CONTACT SUBSCRIBE VIA RSS   DEEP LEARNING FOR ENTERPRISE Distributed Deep Learning, Part 1: An Introduction to Distributed Training of Neural Networks  Oct 3, 2016 3:00:00 AM / by Alex Black and Vyacheslav Kokorin   Tweet inShare27   This

大数据的那些事儿

资源列表:   关系数据库管理系统(RDBMS)   框架   分布式编程   分布式文件系统   文件数据模型   Key -Map 数据模型   键-值数据模型   图形数据模型   NewSQL数据库   列式数据库   时间序列数据库   类SQL处理   数据摄取   服务编程   调度   机器学习   基准测试   安全性   系统部署   应用程序   搜索引擎与框架   MySQL的分支和演化   PostgreSQL的分支和演化   Memcached的分支和演化   嵌入式