zookeeper之持久化

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

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

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、处理持久化的核心类

4、序列化

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值