设为首页 收藏本站
查看: 1080|回复: 0

[经验分享] Zookeeper学习笔记 1

[复制链接]

尚未签到

发表于 2015-12-24 11:33:48 | 显示全部楼层 |阅读模式
先说结论,之后会通过编程以及新的学习来验证:
zookeeper是通过实现一个分布式的具有数据一致性的文件系统来实现分布式的事物控制。官方文档上关于数据访问有这么一句话。


The data stored at each znode in a namespace is read and written atomically. Reads get all the data bytes associated with a znode and a write replaces all the data. Each node has an Access Control List (ACL) that restricts who can do what.


ZooKeeper was not designed to be a general database or large object store. Instead, it manages coordination data. This data can come in the form of configuration, status information, rendezvous, etc. A common property of the various forms of coordination data is that they are relatively small: measured in kilobytes. The ZooKeeper client and the server implementations have sanity checks to ensure that znodes have less than 1M of data, but the data should be much less than that on average. Operating on relatively large data sizes will cause some operations to take much more time than others and will affect the latencies of some operations because of the extra time needed to move more data over the network and onto storage media. If large data storage is needed, the usually pattern of dealing with such data is to store it on a bulk storage system, such as NFS or HDFS, and store pointers to the storage locations in ZooKeeper.


这段话有这么几个概念,znode、ACL。znode就相当于文件系统的目录,ACL则相当于访问权限,但是这些在Zookeeper里面都是原子性的。这些实现,一定很复杂,但是官方文档中有这样的描述







The replicated database is an in-memory database containing the entire data tree. Updates are logged to disk for recoverability, and writes are serialized to disk before they are applied to the in-memory database. Every ZooKeeper server services clients. Clients connect to exactly one server to submit irequests. Read requests are serviced from the local replica of each server database. Requests that change the state of the service, write requests, are processed by an agreement protocol.
As part of the agreement protocol all write requests from clients are forwarded to a single server, called the leader. The rest of the ZooKeeper servers, called followers, receive message proposals from the leader and agree upon message delivery. The messaging layer takes care of replacing leaders on failures and syncing followers with leaders.
ZooKeeper uses a custom atomic messaging protocol. Since the messaging layer is atomic, ZooKeeper can guarantee that the local replicas never diverge. When the leader receives a write request, it calculates what the state of the system is when the write is to be applied and transforms this into a transaction that captures this new state.



实现这些原子行读写的操作,在这段文档里,可以大致猜测出来,他是通过在一个集群中选出一个Leader,然后,通过其他server与leader之间的原子的通信(这个描述很诡异,后续了解如何实现的)然后实现的数据的一致性。搞清楚了Zookeeper的大体架构,他得一些设计思路,可以通过官方文档中给的几个例子中进一步体会。如何应用Zookeeper编写我们自己的分布式应用呢?


官方文档中有这么一个图。


DSC0000.jpg

1)分布式BarrierBarrier是一种控制和协调多个任务触发次序的机制,简单说来就是搞个闸门把欲执行的任务给拦住,等所有任务都处于可以执行的状态时,才放开闸门。它的机理可以见下图所示:
DSC0001.jpg
在单机上JDK提供了CyclicBarrier这个类来实现这个机制,但在分布式环境中JDK就无能为力了。在分布式里实现Barrer需要高一致性做保障,因此 ZooKeeper可以派上用场,所采取的方案就是用一个Node作为Barrer的实体,需要被Barrer的任务通过调用exists()检测这个Node的存在,当需要打开Barrier的时候,删掉这个Node,ZooKeeper的watch机制会通知到各个任务可以开始执行。
2) 分布式 Queue
与 Barrier类似 分布式环境中 实现Queue也需要高一致性做保障, ZooKeeper提供了一个种简单的方式,ZooKeeper通过一个Node来维护Queue的实体,用其children来存储Queue的内容,并且 ZooKeeper的create方法中提供了顺序递增的模式,会自动地在name后面加上一个递增的数字来插入新元素。可以用其 children来构建一个queue的数据结构,offer的时候使用create,take的时候按照children的顺序删除第一个即可。 ZooKeeper保障了各个server上数据是一致的,因此也就实现了一个 分布式 Queue。take和offer的实例代码如下所示:




  • /**

  • * Removes the head of the queue and returns it, blocks until it succeeds.
  • * @return The former head of the queue
  • * @throws NoSuchElementException
  • * @throws KeeperException
  • * @throws InterruptedException
  • */
  • public byte[] take() throws KeeperException, InterruptedException {
  •     TreeMap orderedChildren;
  •     // Same as for element.  Should refactor this.
  •     while(true){
  •         LatchChildWatcher childWatcher = new LatchChildWatcher();
  •         try{
  •             orderedChildren = orderedChildren(childWatcher);
  •         }catch(KeeperException.NoNodeException e){
  •             zookeeper.create(dir, new byte[0], acl, CreateMode.PERSISTENT);
  •             continue;
  •         }
  •         if(orderedChildren.size() == 0){
  •             childWatcher.await();
  •             continue;
  •         }
  •         for(String headNode : orderedChildren.values()){
  •             String path = dir +"/"+headNode;
  •             try{
  •                 byte[] data = zookeeper.getData(path, false, null);
  •                 zookeeper.delete(path, -1);
  •                 return data;
  •             }catch(KeeperException.NoNodeException e){
  •                 // Another client deleted the node first.
  •             }
  •         }
  •     }
  • }
  • /**

  • * Inserts data into queue.
  • * @param data
  • * @return true if data was successfully added
  • */
  • public boolean offer(byte[] data) throws KeeperException, InterruptedException{
  •     for(;;){
  •         try{
  •             zookeeper.create(dir+"/"+prefix, data, acl, CreateMode.PERSISTENT_SEQUENTIAL);
  •             return true;
  •         }catch(KeeperException.NoNodeException e){
  •             zookeeper.create(dir, new byte[0], acl, CreateMode.PERSISTENT);
  •         }
  •     }
  • }


  

3)分布式lock
利用 ZooKeeper实现 分布式lock,主要是通过一个Node来代表一个Lock,当一个client去拿锁的时候,会在这个Node下创建一个自增序列的child,然后通过getChildren()方式来check创建的child是不是最靠前的,如果是则拿到锁,否则就调用exist()来check第二靠前的child,并加上watch来监视。当拿到锁的child执行完后归还锁,归还锁仅仅需要删除自己创建的child,这时watch机制会通知到所有没有拿到锁的client,这些child就会根据前面所讲的拿锁规则来竞争锁。



以上应用场景,可以看出,Zookeeper只是提供了很基本的东西,如何去应用,需要我们自己开脑洞。接下来的学习会重点分析Zookeeper的API,然后结合具体的业务场景,具体分析。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-155661-1-1.html 上篇帖子: [java, zookeeper] ZooKeeper 的安装与编程 下篇帖子: Zookeeper3.4.6的安装
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表