zookeeper源码之服务端数据库管理中心

  负责管理ZooKeeper整个数据。主要管理树结构数据、session数据、持久化管理。

类图

  

ZKDatabase

  ZooKeeper数据管理门户类,底层通过DataTree来管理树结构,通过FileTxnSnapLog来管理数据持久化。

初始化

  初始化过程主要是从持久化文件中恢复数据,通过FileTxnSnapLog实现。

    public long loadDataBase() throws IOException {
        ...long zxid = snapLog.restore(dataTree,sessionsWithTimeouts,listener);
        initialized = true;
        return zxid;
    }

树结构管理

  树结构管理主要通过DataTree来实现。

...
public byte[] getData(String path, Stat stat, Watcher watcher) 
    throws KeeperException.NoNodeException {
        return dataTree.getData(path, stat, watcher);
    }
    public void setWatches(long relativeZxid, List<String> dataWatches,
            List<String> existWatches, List<String> childWatches, Watcher watcher) {
        dataTree.setWatches(relativeZxid, dataWatches, existWatches, childWatches, watcher);
    }
    public List<ACL> getACL(String path, Stat stat) throws NoNodeException {
        return dataTree.getACL(path, stat);
    }
...

持久化管理

  持久化管理主要通过FileTxnSnapLog和SerializeUtils来实现。

    public boolean append(Request si) throws IOException {
        return this.snapLog.append(si);
    }
    public void rollLog() throws IOException {
        this.snapLog.rollLog();
    }
    public void commit() throws IOException {
        this.snapLog.commit();
    }
    public void close() throws IOException {
        this.snapLog.close();
    } 
    public void deserializeSnapshot(InputArchive ia) throws IOException {
        clear();
        SerializeUtils.deserializeSnapshot(getDataTree(),ia,getSessionWithTimeOuts());
        initialized = true;
    }   
    
    public void serializeSnapshot(OutputArchive oa) throws IOException,
    InterruptedException {
        SerializeUtils.serializeSnapshot(getDataTree(), oa, getSessionWithTimeOuts());
    }

DataTree

  管理树结构数据,详见:zookeeper源码之服务端数据库树结构管理

FileTxnSnapLog

  管理持久化数据,详见: ZooKeeper源码之服务端持久化管理

原文地址:https://www.cnblogs.com/zhangwanhua/p/8472303.html