ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Zookeeper源码部分 第2章 辅助源码 持久化源码 序列化源码

2022-06-04 23:32:00  阅读:165  来源: 互联网

标签:String void Zookeeper tag public 源码 IOException 序列化 throws


2.1 辅助源码

2.1.1 持久化源码

Leader和Follower中的数据会在内存和磁盘中各保存一份。所以需要将内存中的数据持久化到磁盘中。

在org.apache.zookeeper.server.persistence包下的相关类都是序列化相关的代码。

image-20220604221548208

1)快照

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)操作日志

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;
    }
}

3)处理持久化的核心类

image-20220604221709153

2.1.2 序列化源码

zookeeper-jute代码是关于Zookeeper序列化相关源码

image-20220604222121412

1)序列化和反序列化方法

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

    // 反序列化方法
    public void deserialize(InputArchive archive, String tag)
        throws IOException;
}

2)迭代

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

3)序列化支持的数据类型

/
 * 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;
}

4)反序列化支持的数据类型

/
 * 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;
}

标签:String,void,Zookeeper,tag,public,源码,IOException,序列化,throws
来源: https://www.cnblogs.com/niuniu2022/p/16343101.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有