zookeeper的client源码理解

##Watcher接口 实现Watcher的类是一个回调函数,process中实现的方法,会在znode变动的时候触发html

interface Watcher() {
// 不太懂为啥在Wacher中又定义了一个interface
    public interface Event(){        
        public enum EventType{}
    }
    abstract public void process(WatcherEvent event){}
}

调用zookeeper.create的时候要求传入权限控制参数(ACL),找了文章发如今UNIX中权限的管理仍是颇有意思的: http://ifeve.com/zookeeper-access-control-using-acls/ 有空能够继续研究一下java

##继续看zookeeper 发如今构造函数的实现中,有两个Thread。这个就有意思了,为啥在构造函数中有Thread?能够继续追踪下去node

SendThread sendThread = new SendThread(clientCnxnSocket); 
ReadThread eventThread = new EventThread();

查阅资料能够了解到zookeeper实现了一个事件驱动模型,先贴一张图或许有助于理解:函数

来源http://www.wangjingfeng.com/38.html

来源http://www.wangjingfeng.com/38.htmlcode

class sendThread {
    void run() {
        // 实现NIO
        // pendingQueue等待响应的packet,outgoingQueue等待发送的packet
        //int waitTimeOut, List<Packet> pendingQueue, LinkedList<Packet> outgoingQueue,ClientCnxn cnxn
        clientCnxnSocket.doTransport(waitTimeOut, pendingQueue, outgoingQueue,cnxn){};
    }   
}

###EventThread -> BlockedQueue -> SendThead 经过加锁固定长度的队列实现消费者模型,同时sendThread使用了java自带的NIO技术,每次轮询全部的selector,处理已经传输完成的selector。很好的实现了解耦。 ##一些其余classserver

class ClientCnxn {
    // ClientCnxn是一个管理I/O的类, 包含了一系列的可用的servers,而且控制这些servers的开关
    
}
class AuthData {
    String scheme;
    byte data[];
}
static class Packet {
    RequestHeader requestHeader;
    ReplyHeader replyHeader;
    Record request;
    Record response;
    ByteBuffer bb;
    AsyncCallBack cb;
}
class EventThread {
    void queueEvent(WatcherEvent watcherEvent){}
    void queuePacket(Packet packet){}
    void processEvent(Object event){
        WatcherSetEventPair pair = (WatcherSetEventPair) event;
        watcher = pair.watchers;
        watcher.process(pair.event);
    }
}
public class ClientCnxnSocketNio{
    
}
相关文章
相关标签/搜索