Skip to content

Zookeeper源码之持久化和序列化

1. 下载源码

访问官网地址:https://archive.apache.org/dist/zookeeper/Alt text 解压后导入IDEA即可

2. 持久化源码

Leader和Follower中的数据会在内存和磁盘中各保存一份。所以需要将内存中的数据持久化到磁盘中。在org.apache.zookeeper.server.persistence包下的相关类都是序列化相关的代码。
Alt text
对应在zoo.cfg中配置的dataDir路径中存放者持久化的数据:

sh
[jack@hadoop106 version-2]$ cd /opt/module/zookeeper-3.8.4/data/version-2
[jack@hadoop106 version-2]$ ll
总用量 16240
-rw-r--r--. 1 jack wheel        1 5月  15 19:34 acceptedEpoch
-rw-r--r--. 1 jack wheel        1 5月  15 19:34 currentEpoch
-rw-r--r--. 1 jack wheel 67108880 5月   7 19:45 log.100000001
-rw-r--r--. 1 jack wheel 67108880 5月  18 18:54 log.500000001
-rw-r--r--. 1 jack wheel 67108880 5月  23 23:34 log.700014ff8
-rw-r--r--. 1 jack wheel      589 5月   4 10:23 snapshot.0
-rw-r--r--. 1 jack wheel      715 5月  14 07:28 snapshot.100000032
-rw-r--r--. 1 jack wheel      715 5月  15 16:43 snapshot.50000000a
-rw-r--r--. 1 jack wheel      715 5月  15 19:34 snapshot.600000000
-rw-r--r--. 1 jack wheel     2171 5月  18 18:54 snapshot.700014ff6
[jack@hadoop106 version-2]$

2.1 快照接口源码

java
public interface SnapShot {
    // 反序列化方法
    long deserialize(DataTree dt, Map<Long, Integer> sessions) 
    throws IOException;
    
    // 序列化方法
    void serialize(DataTree dt, Map<Long, Integer> sessions, 
    File name) 
    throws IOException;
    
    /**
     * find the most recent snapshot file
    * 查找最近的快照文件
    */
    File findMostRecentSnapshot() throws IOException;
    
    // 释放资源
    void close() throws IOException;
}

2.2 操作日志源码

java
public interface TxnLog {
    // 设置服务状态
    void setServerStats(ServerStats serverStats);
    
    // 滚动日志
    void rollLog() throws IOException;
    // 追加
    boolean append(TxnHeader hdr, Record r) throws IOException;
    // 读取数据
    TxnIterator read(long zxid) throws IOException;
    
    // 获取最后一个 zxid
    long getLastLoggedZxid() throws IOException;
    
    // 删除日志
    boolean truncate(long zxid) throws IOException;
    
    // 获取 DbId
    long getDbId() throws IOException;
    
    // 提交
    void commit() throws IOException;
    // 日志同步时间
    long getTxnLogSyncElapsedTime();
    
    // 关闭日志
    void close() throws IOException;
    // 读取日志的接口
    public interface TxnIterator {
        // 获取头信息
        TxnHeader getHeader();
        
        // 获取传输的内容
        Record getTxn();
        
        // 下一条记录
        boolean next() throws IOException;
        
        // 关闭资源
        void close() throws IOException;
        
        // 获取存储的大小
        long getStorageSize() throws IOException;
    }
}

2.3 处理持久化的核心类

Alt text

3. 序列化源码

zookeeper-jute模块是关于Zookeeper序列化相关源码 Alt text

3.1 序列化和反序列化方法

java
public interface Record {
    // 序列化方法
    public void serialize(OutputArchive archive, String tag)
    throws IOException;
    // 反序列化方法
    public void deserialize(InputArchive archive, String tag)
    throws IOException;
}

3.2 迭代

java
public interface Index {
    // 结束
    public boolean done();
    // 下一个
    public void incr();
}

3.3 序列化支持的数据类型

java
/**
* Interface that alll the serializers have to implement.
*/
public interface OutputArchive {
    public void writeByte(byte b, String tag) throws IOException;
    public void writeBool(boolean b, String tag) throws IOException;
    public void writeInt(int i, String tag) throws IOException;
    public void writeLong(long l, String tag) throws IOException;
    public void writeFloat(float f, String tag) throws IOException;
    public void writeDouble(double d, String tag) throws IOException;
    public void writeString(String s, String tag) throws IOException;
    public void writeBuffer(byte buf[], String tag)throws IOException;
    public void writeRecord(Record r, String tag) throws IOException;
    public void startRecord(Record r, String tag) throws IOException;
    public void endRecord(Record r, String tag) throws IOException;
    public void startVector(List<?> v, String tag) throws IOException;
    public void endVector(List<?> v, String tag) throws IOException;
    public void startMap(TreeMap<?,?> v, String tag) throws IOException;
    public void endMap(TreeMap<?,?> v, String tag) throws IOException;
}

3.4 反序列化支持的数据类型

java
/**
* Interface that all the Deserializers have to implement.
*/
public interface InputArchive {
    public byte readByte(String tag) throws IOException;
    public boolean readBool(String tag) throws IOException;
    public int readInt(String tag) throws IOException;
    public long readLong(String tag) throws IOException;
    public float readFloat(String tag) throws IOException;
    public double readDouble(String tag) throws IOException;
    public String readString(String tag) throws IOException;
    public byte[] readBuffer(String tag) throws IOException;
    public void readRecord(Record r, String tag) throws IOException;
    public void startRecord(String tag) throws IOException;
    public void endRecord(String tag) throws IOException;
    public Index startVector(String tag) throws IOException;
    public void endVector(String tag) throws IOException;
    public Index startMap(String tag) throws IOException;
    public void endMap(String tag) throws IOException;
}