在ZK里面有两个数据文件目录可以配置,
- dataDir, The location where ZooKeeper will store the in-memory database snapshots.
- dataLogDir, This option will direct the machine to write the transaction log to the dataLogDir rather than the dataDir.
dataDir用来存储ZKDatabase的快照文件(Snapshot File);而dataLogDir用来存储对ZKDatabase所做操作的操作日志文件(Transaction Log File),如果不配置dataLogDir,则dataLogDir=dataDir。
当我们对ZKDatabase进行操作,例如修改znode信息,操作日志便会被写入到操作日志文件。操作日志文件会一直增大,当达到snapCount
个操作时,ZK便会保存一份ZKDatabase当前的数据到快照文件,并丢弃之前的操作日志文件(已经没有必要保存了),生成新的操作日志文件。snapCount配置说明如下,
- snapCount,ZooKeeper logs transactions to a transaction log. After snapCount transactions are written to a log file a snapshot is started and a new transaction log file is created. The default snapCount is 100,000.
那么这两个文件到底有啥用?
By replaying the transaction log against fuzzy snapshots ZooKeeper gets the state of the system at the end of the log.
也就是说,这俩货是用来做快速恢复(fast recovery)用的。当我们的ZK机器挂掉了,只要数据文件还在,重启机器,将快照文件load到ZKDatabase,然后对ZKDatabase重放操作日志文件这样就能恢复到机器挂掉前的状态了。
另外快照文件需要有清除操作,当然你也可以保留所有快照,但没那个必要。下面是两个跟自动清除相关的配置,
- autopurge.purgeInterval, The time interval in hours for which the purge task has to be triggered. Set to a positive integer (1 and above) to enable the auto purging. Defaults to 0.
- autopurge.snapRetainCount, When enabled, ZooKeeper auto purge feature retains the autopurge.snapRetainCount most recent snapshots and the corresponding transaction logs in the dataDir and dataLogDir respectively and deletes the rest. Defaults to 3. Minimum value is 3.
参考资料
时间: 2024-10-12 19:09:35