从开发角度了解Zookeeper的工作原理及内部工作机制
来源:互联网 发布:php遍历对象数组 编辑:程序博客网 时间:2024/05/15 07:23
客户端ClientWatchManager,管理由ClientXncn产生的watchers和handle events在zookeeper的exists、getChildren、getData等这些API中可以注册watcher对象到ClientWatchManager中,create、setData、delete等这些引起zookeeper节点变化的API会触发watcher process的执行。
服务端WatchManager,服务端的watcher对象管理器;
注册watcher时候,会在服务端调用FinalRequestProcessor.processRequest,注册client对应的服务端连接对象ServerCnxn(实现了watcher接口)到DataTree中
这样在触发server端的watcher时,其实就是触发ServerCnxn的process方法,在ServerCnxn的process这个实现里会向对应的注册watcher对象的client发送notify消息,
而客户端会调用对应path注册的watcher对象的process方法
服务端注册watcher对象
FinalRequestProcessor.processRequest
case OpCode.getData: {
...............................
Stat stat = new Stat();
byte b[] = zks.getZKDatabase().getData(getDataRequest.getPath(), stat,
getDataRequest.getWatch() ? cnxn : null);//cnxn为ServerCnxn
rsp = new GetDataResponse(b, stat);
watcher的触发,WatchManager中的trigerWatch(String path,EvenType type),当server接受到例如createNode/deleteNode/setData等操作时,
将会操作ZKDatabase来操作DataTree中的数据,当然dataTree的数据改动,将会触发相应patch(节点)上的watch(有可能一个操作会导致多种watch被触发),
trigerWatch就是在这些时机下被调用。此操作中就是从watchManager中将相应path下注册的watch移除,并依次调用watch.process()。
此process()做了一件事情,就是向client发送一个nofication消息,此消息中包含一个WatchEvent对象,此对象封装了事件的类型/path等,
发送到EventThread中的waitingEvents队列中,EventThread后台线程从队列中拉取消息执行watcher中的process逻辑。
在ServerCnxn处理请求时出现异常或者client关闭,将会导致ServerCnxn调用close()方法,此方法中有个分支操作就是从DataTree中的两种watches列表中删除其关联的watch。
1.Zookeeper客户端有几部分组成?
- while (zooKeeper.state.isAlive()) {
- try {
- if (sockKey == null) {
- // don’t re-establish connection if we are closing
- if (closing) {
- break;
- }
- startConnect();
- lastSend = now;
- lastHeard = now;
- }
- … ….
- selector.select(to);
- Set<SelectionKey> selected;
- synchronized (this) {
- selected = selector.selectedKeys();
- }
- // Everything below and until we get back to the select is
- // non blocking, so time is effectively a constant. That is
- // Why we just have to do this once, here
- now = System.currentTimeMillis();
- for (SelectionKey k : selected) {
- … …
- if (doIO()) {
- lastHeard = now;
- }
- … …
- }
- }
- catch() {
- … …
- }
- }
- boolean doIO() throws InterruptedException, IOException {
- boolean packetReceived = false;
- SocketChannel sock = (SocketChannel) sockKey.channel();
- if (sock == null) {
- throw new IOException(“Socket is null!”);
- }
- if (sockKey.isReadable()) {
- … …
- }
-
- if (sockKey.isWritable()) {
- … …
- }
- if (outgoingQueue.isEmpty()) {
- disableWrite();
- } else {
- enableWrite();
- }
- return packetReceived;
- }
- 从开发角度了解Zookeeper的工作原理及内部工作机制
- 了解内部工作原理
- zookeeper的工作原理与选举机制
- Zookeeper 的工作原理
- ZooKeeper的工作原理
- ZooKeeper的工作原理
- zookeeper的工作原理
- zookeeper的工作原理
- zookeeper的工作原理
- 6. Zookeeper精要-内部工作原理
- ZooKeeper详解及工作原理
- Zookeeper基本概念及工作原理
- Zookeeper功能及工作原理
- ROWNUM的工作机制及原理
- 从System.out.println()了解Java程序的工作原理
- SEO从业者必须了解的搜索引擎工作原理
- 从三张表中了解交换机和路由器的工作原理
- spring工作机制及原理
- Poj 3691 & Hdu 2457 DNA repair
- Android Fragment完全解析,关于碎片你所需知道的一切
- Oracle ERP 11i 应用技巧与维护经验
- puppet与nagios的结合
- 开始
- 从开发角度了解Zookeeper的工作原理及内部工作机制
- EM12C 安装及卸载 注意点整理
- Android手机平板两不误,使用Fragment实现兼容手机和平板的程序
- LeetCode | Permutations(全排列)
- [资料推荐] 实时Linux
- For anyone who is in reactions to receive some innovative models then you are up to get a heal now
- hdu 1506 Largest Rectangle in a Histogram(单调栈)
- It is actually really and properly asserted that if you wish to provide a great and pleasant present
- When you are in emotions to get some ground breaking designs you will be up for your heal now