现在我们来考察一下zookeeper的一致性模型。

  常见误区

  一开始看到网上有人说zookeeper满足了CAP的CP特性,我一直以为zookeeper至少也是Sequential Consistent。那zookeeper自己怎么说的呢?在它文档中,首先它宣称自己是“Sequential Consistency”,不过它的“Sequential Consistency”相比Leslie Lamport老哥的,似乎缩水了,怎么缩的呢?后面它羞羞答答的解释“Updates from a client will be applied in the order that they were sent”,你看,updates是in the order的,read呢,这可没说。

  紧接着它来了个免责声明

  Sometimes developers mistakenly assume one other guarantee that ZooKeeper does not in fact make. This is: Simultaneously Consistent Cross-Client Views : ZooKeeper does not guarantee that at every instance in time, two different clients will have identical views of ZooKeeper data. Due to factors like network delays, one client may perform an update before another client gets notified of the change. Consider the scenario of two clients, A and B. If client A sets the value of a znode /a from 0 to 1, then tells client B to read /a, client B may read the old value of 0, depending on which server it is connected to. If it is important that Client A and Client B read the same value, Client B should should call the sync() method from the ZooKeeper API method before it performs its read. So, ZooKeeper by itself doesn’t guarantee that changes occur synchronously across all servers, but ZooKeeper primitives can be used to construct higher level functions that provide useful client synchronization.

  这段话意思就是说,大家有时候对zookeeper期望过高了,zookeeper不能保证同一时刻所有client看到一致的数据。如果有A和B两个客户端,A把某个值更新了,然后它告诉B:“哈,我更新了,你去读吧”,这时B不一定能读到更新后的值。

  要知道出现这种现象的原因,要看看zookeeper的实现原理。

  在《ZooKeeper: Wait-free coordination for Internet-scale systems》一文中可以看出,zookeeper的所有node都是可以服务client的,我猜测是因为处理session过期、watch这些东西,一个leader真是独木难支。当client连接了一个follower以后,所有的读写请求都发给follower。

  对于写请求,follower抱着“事不关己高高挂起”的心态forward给leader。

  

  对于读请求,follower则从本地将数据返回给client。

  

  所以A更新了以后,B客户端如果时乖命蹇,连接的是一个落后的follower,那么就无法读到最新的数据了。

  如果client想确保每次到最新的数据,应该调用一个sync之后,再进行读操作,这一点请务必注意。

  顺便说一句,zookeeper client为了感知其他client的修改,应该通过watch的形式。

  Read-your-own-writes consistency

  那么如果A更新了一个数据,它自己能不能读到最新的呢?如果不能,那真是尴尬啊,幸好是可以的。

  文章里说

  All requests that update ZooKeeper state are forwarded to the leader……The server that

  receives the client request responds to the client when it delivers the corresponding state change.

  所以A的写请求结束以后,它连接的node已经deliver了state change,以后的读操作顺理成章得到更新后的数据。

  真正的Sequential Consistency

  在官网中,它只说“Updates from a client will be applied in the order that they were sent”,有没有可能read出现乱序呢?有没有可能后来的read读到更旧的数据?

  假设一个client连接到一个最新的follower上,它的一次read读取了最新的数据,然后client由于某种原因,需要重新连接一个zookeeper节点,恰好这次连接到一个stale状态的follower,那么这个client再一次read岂不是读取了旧的数据,造成了历史的倒退?

  还好zookeeper还是处理了这种情况,文中指出,首先client记录了自己已经读取的最大的zxid,当client重新连接server的时候,server发现client的zxid比自己大,是不会和client重新建立session的。

  If the client connects to a new server, that new server ensures that its view of the ZooKeeper data is at least as recent as the view of the client by checking the last zxid of the client against its last zxid. If the client has a more recent view than the server, the server does not reestablish the session with the client until the server has caught up.

  如果client能正确处理zxid的话,我感觉zookeeper也具有Lamport老哥的Sequential Consistency,但是zxid的存储应该是一个比较难以解决的问题,所以zookeeper谨慎一点,把自己的Sequential Consistency缩了一下水。

  如何理解Single System Image

  zookeeper官网还说它保证了“Single System Image”,其解释为“A client will see the same view of the service regardless of the server that it connects to.”。实际上看来这个解释还是有一点误导性的。其实由上面zxid的原理可以看出,它表达的意思是“client只要连接过一次zookeeper,就不会有历史的倒退”。

转载于:https://juejin.im/post/5cda70546fb9a0320d610c89

深入浅析zookeeper的一致性模型及其实现相关推荐

  1. 理解zookeeper的一致性及缺点

    理解zookeeper的一致性及缺点 zookeeper保证 理解zookeeper的顺序一致性 zookeeper的缺点 参考链接 zookeeper使用ZAB协议达到了极高的一致性.所以在互联网业 ...

  2. 分布式系统 一致性模型的介绍 以及 zookeeper的 “线性一致性“ 讨论

    文章目录 1. 一致性 概览 1.1 分布式系统的 "正确性" 1.2 线性一致性(Linearizability) 1.3 顺序一致性(Sequential consistenc ...

  3. Zookeeper的一致性是什么情况?

    Zookeeper的一致性,体现的是什么一致呢? 根据前面讲的zab协议的同步流程,在zookeeper集群内部的数据副本同步,是基于过半提交的策略,意味着它是最终一致性.并不满足强一致的要求. 其实 ...

  4. 分布式系统概念:一致性协议、一致性模型、拜占庭问题、租约、副本协议

    1,一致性协议 两阶段提交协议与Raft协议.Paxos协议 ①两阶段提交协议 在分布式系统中,每个节点虽然可以知晓自己的操作时成功或者失败,却无法知道其他节点的操作的成功或失败.当一个事务跨越多个节 ...

  5. 《从Paxos到zookeeper分布式一致性原理与实践》笔记

    <从Paxos到zookeeper分布式一致性原理与实践>笔记 文章目录 <从Paxos到zookeeper分布式一致性原理与实践>笔记 一.概念 二.一致性协调 2.1 2P ...

  6. 《从Paxos到zookeeper分布式一致性原理与实践》

    <从Paxos到zookeeper分布式一致性原理与实践> 一.概念 ACID: Automaticy.consistency.isolation. Durability CAP: con ...

  7. [201502][从 Paxos 到 ZooKeeper][分布式一致性原理与实践][倪超][著]

    [201502][从 Paxos 到 ZooKeeper][分布式一致性原理与实践][倪超][著] http://zookeeper.apache.org 第 1 章 分布式架构 1.1 从集中式到分 ...

  8. Zookeeper分布式一致性原理(四):Zookeeper简介

    zookeeper是一个典型的分布式数据一致性的解决方案,分布式应用程序可以基于它实现数据发布/订阅.负载均衡.命名服务.分布式协调/通知.集群管理.master选举.分布式锁和分布式队列等.Zook ...

  9. 分布式系统:一致性模型

    分布式系统中一个重要的问题就是数据复制,数据复制一般是为了增强系统的可用性或提高性能.而实现数据复制的一个主要难题就是保持各个副本的一致性.本文首先讨论数据复制的场景中一致性模型如此重要的原因,然后讨 ...

最新文章

  1. Myeclipse使用过程配置汇总
  2. 高二下学期计算机考试试题及答案,福建省泉州市第五高级中学校2020-2021学年高二下学期期中考试数学试题含答案.docx...
  3. Django1.6踩到的坑
  4. [BUUCTF-pwn]——铁人三项(第五赛区)_2018_rop
  5. DNN:LSTM的前向计算和参数训练
  6. 【渝粤教育】国家开放大学2018年春季 0014-22T秘书学(一) 参考试题
  7. 笔记--《谷歌和亚马逊是怎么做产品的》第一至三章
  8. UNIX 环境高级编程
  9. Virtualbox安装Debian 安装VBoxGuestAdditions增强工具
  10. Win10下 Java环境变量配置
  11. 三个精致的钢琴音源 Native Instruments Definitive Piano Collection
  12. vue集成wangeditor3.0版本 解决光标乱跳等问题
  13. “远程网络教学系统”UML用例图(练习题)
  14. 最新机器视觉研究团队汇总
  15. IDEA 设置前进,后退快捷键
  16. VS Code实现python代码语法检查、格式规范化、自动换行字数限制
  17. Intel 82576网卡
  18. 二级菜单(隐藏显示)
  19. Linux和windows下minio+springboot实现文件服务器上传下载和nginx反向代理
  20. signature=06503812625e47e9ee33634e47a778e8,Title page for ETD etd-07242013-155857

热门文章

  1. OpenGL中的混合(Blending)
  2. mysql.server 文件是什么_mysql的启动脚本mysql.server及示例配置文件
  3. html响应式布局media,JS中使用media实现响应式布局
  4. 浅析Java中的final关键字
  5. html div比例,我的前端组件 ---- 16:9固定宽高比例的div
  6. linux 5.8 yum源,Centos5.8 |linux yum源不能用报404错误
  7. com口驱动_手机都没耳机口了,平衡口现在才来是作死还是?
  8. 计算机软件3十2二5,计算机软件基础3.2 操作系统.pdf
  9. 不歧视双非的计算机院校,公平!考研西工大,双非院校倒数第一名考生上岸,本校考生被刷...
  10. 路由器与计算机IP配置,路由器设置之前 如何给电脑设置IP地址