一、前言
前面已经分析了请求处理链中的多数类,接着继续分析Zookeeper中的网络通信模块。
二、总体框图
对于网络通信模块,其总体框图如下所示
说明:
Stats,表示ServerCnxn上的统计数据。
Watcher,表示时间处理器。
ServerCnxn,表示服务器连接,表示一个从客户端到服务器的连接。
NettyServerCnxn,基于Netty的连接的具体实现。
NIOServerCnxn,基于NIO的连接的具体实现。
三、ServerCnxn源码分析
3.1 类的继承关系
public abstract class ServerCnxn implements Stats, Watcher {}
说明:ServerCnxn为抽象类,其继承Stats和Watcher两个接口,表示客户端到服务端的连接。
3.2 类的内部类
// 请求关闭异常类 protected static class CloseRequestException extends IOException { private static final long serialVersionUID = -7854505709816442681L; public CloseRequestException(String msg) { super(msg); } } // 流结束异常类 protected static class EndOfStreamException extends IOException { private static final long serialVersionUID = -8255690282104294178L; public EndOfStreamException(String msg) { super(msg); } public String toString() { return "EndOfStreamException: " + getMessage(); } }
说明:ServerCnxn包含了两个异常类,用于表示在连接中发生的异常情况。
3.3 类的属性
public abstract class ServerCnxn implements Stats, Watcher { // This is just an arbitrary object to represent requests issued by // (aka owned by) this class // 代表由本类提出的请求 final public static Object me = new Object(); // 认证信息 protected ArrayList<Id> authInfo = new ArrayList<Id>(); /** * If the client is of old version, we don't send r-o mode info to it. * The reason is that if we would, old C client doesn't read it, which * results in TCP RST packet, i.e. "connection reset by peer". */ // 是否为旧的C客户端 boolean isOldClient = true; // Zookeeper的Sasl服务器 protected ZooKeeperSaslServer zooKeeperSaslServer = null; /** * CMD命令 **/ /* * See <a href="{@docRoot}/../../../docs/zookeeperAdmin.html#sc_zkCommands"> * Zk Admin</a>. this link is for all the commands. */ // CMD命令 protected final static int confCmd = ByteBuffer.wrap("conf".getBytes()).getInt(); /* * See <a href="{@docRoot}/../../../docs/zookeeperAdmin.html#sc_zkCommands"> * Zk Admin</a>. this link is for all the commands. */ protected final static int consCmd = ByteBuffer.wrap("cons".getBytes()).getInt(); /* * See <a href="{@docRoot}/../../../docs/zookeeperAdmin.html#sc_zkCommands"> * Zk Admin</a>. this link is for all the commands. */ protected final static int crstCmd = ByteBuffer.wrap("crst".getBytes()).getInt(); /* * See <a href="{@docRoot}/../../../docs/zookeeperAdmin.html#sc_zkCommands"> * Zk Admin</a>. this link is for all the commands. */ protected final static int dumpCmd = ByteBuffer.wrap("dump".getBytes()).getInt(); /* * See <a href="{@docRoot}/../../../docs/zookeeperAdmin.html#sc_zkCommands"> * Zk Admin</a>. this link is for all the commands. */ protected final static int enviCmd = ByteBuffer.wrap("envi".getBytes()).getInt(); /* * See <a href="{@docRoot}/../../../docs/zookeeperAdmin.html#sc_zkCommands"> * Zk Admin</a>. this link is for all the commands. */ protected final static int getTraceMaskCmd = ByteBuffer.wrap("gtmk".getBytes()).getInt(); /* * See <a href="{@docRoot}/../../../docs/zookeeperAdmin.html#sc_zkCommands"> * Zk Admin</a>. this link is for all the commands. */ protected final static int ruokCmd = ByteBuffer.wrap("ruok".getBytes()).getInt(); /* * See <a href="{@docRoot}/../../../docs/zookeeperAdmin.html#sc_zkCommands"> * Zk Admin</a>. this link is for all the commands. */ protected final static int setTraceMaskCmd = ByteBuffer.wrap("stmk".getBytes()).getInt(); /* * See <a href="{@docRoot}/../../../docs/zookeeperAdmin.html#sc_zkCommands"> * Zk Admin</a>. this link is for all the commands. */ protected final static int srvrCmd = ByteBuffer.wrap("srvr".getBytes()).getInt(); /* * See <a href="{@docRoot}/../../../docs/zookeeperAdmin.html#sc_zkCommands"> * Zk Admin</a>. this link is for all the commands. */ protected final static int srstCmd = ByteBuffer.wrap("srst".getBytes()).getInt(); /* * See <a href="{@docRoot}/../../../docs/zookeeperAdmin.html#sc_zkCommands"> * Zk Admin</a>. this link is for all the commands. */ protected final static int statCmd = ByteBuffer.wrap("stat".getBytes()).getInt(); /* * See <a href="{@docRoot}/../../../docs/zookeeperAdmin.html#sc_zkCommands"> * Zk Admin</a>. this link is for all the commands. */ protected final static int wchcCmd = ByteBuffer.wrap("wchc".getBytes()).getInt(); /* * See <a href="{@docRoot}/../../../docs/zookeeperAdmin.html#sc_zkCommands"> * Zk Admin</a>. this link is for all the commands. */ protected final static int wchpCmd = ByteBuffer.wrap("wchp".getBytes()).getInt(); /* * See <a href="{@docRoot}/../../../docs/zookeeperAdmin.html#sc_zkCommands"> * Zk Admin</a>. this link is for all the commands. */ protected final static int wchsCmd = ByteBuffer.wrap("wchs".getBytes()).getInt(); /* * See <a href="{@docRoot}/../../../docs/zookeeperAdmin.html#sc_zkCommands"> * Zk Admin</a>. this link is for all the commands. */ protected final static int mntrCmd = ByteBuffer.wrap("mntr".getBytes()) .getInt(); /* * See <a href="{@docRoot}/../../../docs/zookeeperAdmin.html#sc_zkCommands"> * Zk Admin</a>. this link is for all the commands. */ protected final static int isroCmd = ByteBuffer.wrap("isro".getBytes()) .getInt(); // 存储CMD的整形值与String的键值对 protected final static HashMap<Integer, String> cmd2String = new HashMap<Integer, String>(); // specify all of the commands that are available static { cmd2String.put(confCmd, "conf"); cmd2String.put(consCmd, "cons"); cmd2String.put(crstCmd, "crst"); cmd2String.put(dumpCmd, "dump"); cmd2String.put(enviCmd, "envi"); cmd2String.put(getTraceMaskCmd, "gtmk"); cmd2String.put(ruokCmd, "ruok"); cmd2String.put(setTraceMaskCmd, "stmk"); cmd2String.put(srstCmd, "srst"); cmd2String.put(srvrCmd, "srvr"); cmd2String.put(statCmd, "stat"); cmd2String.put(wchcCmd, "wchc"); cmd2String.put(wchpCmd, "wchp"); cmd2String.put(wchsCmd, "wchs"); cmd2String.put(mntrCmd, "mntr"); cmd2String.put(isroCmd, "isro"); } /** * 服务器的统计数据 **/ // 创建连接的时间 protected final Date established = new Date(); // 接受的packet数量 protected final AtomicLong packetsReceived = new AtomicLong(); // 发送的packet数量 protected final AtomicLong packetsSent = new AtomicLong(); // 最小延迟 protected long minLatency; // 最大延迟 protected long maxLatency; // 最后操作类型 protected String lastOp; // 最后的cxid protected long lastCxid; // 最后的zxid protected long lastZxid; // 最后的响应时间 protected long lastResponseTime; // 最后的延迟 protected long lastLatency; // 数量 protected long count; // 总的延迟 protected long totalLatency; }