Zoo keeper
Upcoming SlideShare
Loading in...5
×
 

Zoo keeper

on

  • 1,042 views

 

Statistics

Views

Total Views
1,042
Views on SlideShare
1,042
Embed Views
0

Actions

Likes
0
Downloads
10
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Zoo keeper Zoo keeper Presentation Transcript

  • ZooKeeper 高可用分布式协调系统high available and reliable coordination system
  • zk的角色 人体 分布式系统 神经 网络 器官 服务器 大脑 zk
  • zk的角色
  • Standalone模式$ cd zookeeper-3.4.2/$ cat conf/zoo.cfgtickTime=2000dataDir=/var/lib/zookeeperclientPort=2181
  • Standalone模式$ bin/zkServer.sh startJMX enabled by defaultUsing config: /home/jxq/code/zookeeper-3.4.2/bin/../conf/zoo.cfgStarting zookeeper ... STARTED$ bin/zkCli.sh -server 127.0.0.1:2181Connecting to localhost:2181Welcome to ZooKeeper!JLine support is enabled[zk: localhost:2181(CONNECTED) 0] help
  • Standalone模式[zk: localhost:2181(CONNECTED) 0] helpZooKeeper -server host:port cmd args connect host:port get path [watch] ls path [watch] set path data [version] rmr path delquota [-n|-b] path quit printwatches on|off create [-s] [-e] path data acl stat path [watch] close ls2 path [watch] listquota path setAcl path acl getAcl path sync path delete path [version]
  • Standalone模式[zk: localhost:2181(CONNECTED) 1] ls /[zookeeper][zk: localhost:2181(CONNECTED) 2] create /zk_test test_dataCreated /zk_test[zk: localhost:2181(CONNECTED) 3] ls /[zookeeper, zk_test][zk: localhost:2181(CONNECTED) 4] get /zk_testtest_datacZxid = 0x11ctime = Thu Mar 08 15:33:55 CST 2012mZxid = 0x11mtime = Thu Mar 08 15:33:55 CST 2012pZxid = 0x11cversion = 0dataVersion = 0aclVersion = 0ephemeralOwner = 0x0dataLength = 9numChildren = 0
  • Standalone模式[zk: localhost:2181(CONNECTED) 5] set /zk_test test_2cZxid = 0x11ctime = Thu Mar 08 15:33:55 CST 2012mZxid = 0x13mtime = Thu Mar 08 15:46:27 CST 2012pZxid = 0x11cversion = 0dataVersion = 1aclVersion = 0ephemeralOwner = 0x0dataLength = 6numChildren = 0
  • Standalone模式[zk: localhost:2181(CONNECTED) 6] get /zk_test watchtest_2cZxid = 0x11ctime = Thu Mar 08 15:33:55 CST 2012mZxid = 0x13mtime = Thu Mar 08 15:46:27 CST 2012pZxid = 0x11cversion = 0dataVersion = 1aclVersion = 0ephemeralOwner = 0x0dataLength = 6numChildren = 0
  • Standalone模式[zk: 127.0.0.1:2181(CONNECTED) 6] set /zk_test test_3WATCHER::cZxid = 0x11WatchedEvent state:SyncConnected type:NodeDataChangedpath:/zk_testctime = Thu Mar 08 15:33:55 CST 2012mZxid = 0x51mtime = Mon Mar 19 10:43:59 CST 2012pZxid = 0x11cversion = 0dataVersion = 11aclVersion = 0ephemeralOwner = 0x0dataLength = 6numChildren = 0
  • zk的一致性保证• 顺序性:客户端请求顺序生效• 原子性• 单一系统映像• 可靠性:一旦更新请求生效,会持续到下 一次请求
  • znode • 3种znode • persistent znode: 永久 有效地节点 • ephemeral znode: 临 时节点 • sequential znode: 顺序 节点 • 数据少于1MB
  • watches• getData(), getChildren(), exists()• One-time trigger• data watches and child watches• 有序的: • 客户端收到watch事件的顺序跟节点发生改变的顺序一 致 • 客户端收到watch事件后才会看到新数据• 注意延迟:收到watch事件和获取新数据之间数据 可能改变多次 void watcher(zhandle_t *zzh, int type, int state, const char *path, void* context)
  • ACL• 类似unix文件权限• 只对某一节点有效(非递归的)• 权限: • CREATE, READ,WRITE,DELETE,ADMIN
  • API 异步 同步 zoo_acreate() zoo_create() zoo_aexists() zoo_exists() zoo_aget() zoo_get()zoo_aget_children() zoo_get_children() zoo_aset() zoo_set() zoo_adelete() zoo_delete()
  • 典型应用• Naming service• 配置管理• 集群监控• Barriers• 分布式队列• 分布式锁• leader election
  • 配置管理• 配置文件、机器列表等等• 集中管理• 服务自动更新配置 • 客户端建立watch • zk节点内容(配置)更改时推送到客户端
  • 集群监控• 每个服务创建“/clusterServers/{hostname}” 节点, ephemeral• 监控服务watch“/clusterServers” 子节点数 量• 被监控服务停止时节点消失,监控服务收 到watch事件
  • Barriers
  • Barriers• “/barrier/[n]” 1. create(“/barrier/n”, EPHEMERAL_SEQUENTIAL) 2. getChildren(“/barrier/”, true),设定watch, 节点数量变化时通知 3. if 节点数量小于x,等待watch通知 4. else return 5. goto 2 watcher函数:pthread_cond_signal() 等待:pthread_cond_wait()
  • 分布式队列• “/q/element_[n]”• 生产者: create(“/q” + “/element_”, message, ZOO_SEQUENCE);• 消费者: get_children(); delete();• zk集群数据一致性
  • 分布式锁• “_locknode_/lock-[n]”• 获得锁: 1. create(“_locknode_/lock-[n]”) 2. getChildren() 3. 判断(1)中创建节点序号是否是最小的,是则取得锁 4. exists()判断第二小的节点是否存在,并加watch 5. 如果exists()返回false, goto 2。否则等待通知 后再跳到2• 释放锁:删除节点
  • leader election
  • ZK内部设计
  • • ZooKeeper Atomic Broadcast• 保证: • 消息的可靠传递 • 全局顺序 • 因果顺序• 消息传递的两个流程 • 选举 • 同步
  • zk节点的角色角色 描述leader(领导者) 进行投票的发起和决议 follower(跟随者) 接收客户端请求并返回结果,选举过程 中参与投票 接收客户端请求,将写操作转发给learner(学习 observer(观察 leader。但是不参与投票过程,只同步者) 者) leader状态。提高读性能。client(客户端) 发起请求
  • zk server工作状态• LOOKING:当前server不知道leader是谁• LEADING:当前server是leader• FOLLOWING:leader已经选举出来
  • 选举流程• basic paxos • 每个Server启动以后都询问其它的Server它要 投票给谁,收到所有Server回复以后,就计算 出zxid最大的哪个Server,并将这个Server相 关信息设置成下一次要投票的Server。如果此 时获胜的Server获得n/2 + 1的Server票数, 设 置当前推荐的leader为获胜的Server,并修改 自己状态。• election.jpg
  • 同步流程• leader等待server连接• follower连接leader,将最大的zxid发送给 leader• leader根据follower的zxid确定同步点• 完成同步后通知follower 已经成为uptodate 状态• follower收到uptodate消息后,又可以重新 接受client的请求进行服务