日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Apache ZooKeeper - ZK的内存数据 + 持久化事务日志 + 数据快照 初探

發(fā)布時(shí)間:2025/3/21 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Apache ZooKeeper - ZK的内存数据 + 持久化事务日志 + 数据快照 初探 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

  • 內(nèi)存數(shù)據(jù)
    • 源碼實(shí)現(xiàn)
  • 事務(wù)日志
    • 配置項(xiàng)
    • 查看事務(wù)日志數(shù)據(jù) LogFormatter
    • 寫入日志的優(yōu)化 (預(yù)分配)
  • 數(shù)據(jù)快照
    • 查看數(shù)據(jù)快照數(shù)據(jù) SnapshotFormatter
  • 事務(wù)日志 VS 快照數(shù)據(jù)

內(nèi)存數(shù)據(jù)

Zookeeper數(shù)據(jù)的組織形式為一個(gè)類似文件系統(tǒng)的數(shù)據(jù)結(jié)構(gòu),而這些數(shù)據(jù)都是存儲在內(nèi)存中的, 可以認(rèn)為 Zookeeper是一個(gè)基于內(nèi)存的小型數(shù)據(jù)庫 .

源碼實(shí)現(xiàn)

看看源碼是如何存儲的

public class DataTree {private final ConcurrentHashMap<String, DataNode> nodes =new ConcurrentHashMap<String, DataNode>();private final WatchManager dataWatches = new WatchManager();private final WatchManager childWatches = new WatchManager();

DataNode 是Zookeeper存儲節(jié)點(diǎn)數(shù)據(jù)的最小單位

public class DataNode implements Record {byte data[];Long acl;public StatPersisted stat;private Set<String> children = null;.....}

事務(wù)日志

針對每一次客戶端的事務(wù)操作,Zookeeper都會將他們記錄到事務(wù)日志中,當(dāng)然,Zookeeper也會將數(shù)據(jù)變更應(yīng)用到內(nèi)存數(shù)據(jù)庫中。


配置項(xiàng)

在zookeeper的主配置文件zoo.cfg 中配置內(nèi)存中的數(shù)據(jù)持久化目錄 dataLogDir 用于存儲事務(wù)日志.

如果沒有配置dataLogDir(非必填), 事務(wù)日志將存儲到dataDir (必填項(xiàng))目錄.

當(dāng)然了,不推薦放到/tmp目錄下,我這里暫時(shí)未調(diào)整。


查看事務(wù)日志數(shù)據(jù) LogFormatter

zookeeper提供了格式化工具可以進(jìn)行數(shù)據(jù)查看事務(wù)日志數(shù)據(jù) : org.apache.zookeeper.server.LogFormatter

進(jìn)入到zk的安裝目錄/lib 目錄下

執(zhí)行java -cp

[root@localhost lib]# java -cp slf4j-api-1.7.25.jar:zookeeper-3.5.8.jar:zookeeper-jute-3.5.8.jar org.apache.zookeeper.server.LogFormatter /tmp/zookeeper/version-2/log.1

會看到操作日志

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. ZooKeeper Transactional Log File with dbid 0 txnlog format version 2 11/15/20 11:43:05 PM CST session 0x100018c4d000000 cxid 0x0 zxid 0x1 createSession 3000011/16/20 12:01:48 AM CST session 0x100018c4d000000 cxid 0x7 zxid 0x2 closeSession null 11/16/20 12:13:32 AM CST session 0x100018c4d000001 cxid 0x0 zxid 0x3 createSession 30000................... 11/16/20 11:08:55 PM CST session 0x100018c4d000003 cxid 0x9 zxid 0xb setData '/artisan-persist,#646174615f7878785f6e6577,111/16/20 11:20:56 PM CST session 0x100018c4d000003 cxid 0x14 zxid 0xc create '/artisan_seq0000000002,,v{s{31,s{'world,'anyone}}},F,311/16/20 11:21:00 PM CST session 0x100018c4d000003 cxid 0x15 zxid 0xd create '/artisan_seq0000000003,,v{s{31,s{'world,'anyone}}},F,4.... ............... 11/18/20 7:23:56 PM CST session 0x100018c4d000013 cxid 0x47 zxid 0x66 create '/artisan_node,#6172746973616e5f76616c7565,v{s{31,s{'digest,'artisan:Xe7+HMYId2eNV48821ZrcFwIqIE=}}},F,2611/18/20 7:42:40 PM CST session 0x100018c4d000013 cxid 0x4a zxid 0x67 create '/artisanNNN,#6e6f646556616c7565,v{s{31,s{'digest,'artisan:Xe7+HMYId2eNV48821ZrcFwIqIE=}},s{31,s{'digest,'aaa:10XRRJA48HZz23x1IeCQfJcCEbE=}}},F,2711/18/20 9:07:56 PM CST session 0x100018c4d000013 cxid 0x0 zxid 0x68 closeSession null EOF reached after 104 txns.

從左到右分別記錄了操作時(shí)間,客戶端會話ID,CXID,ZXID,操作類型,節(jié)點(diǎn)路徑,節(jié)點(diǎn)數(shù)據(jù)(用#+ascii 碼表示),節(jié)點(diǎn)版本。


寫入日志的優(yōu)化 (預(yù)分配)

Zookeeper進(jìn)行事務(wù)日志文件操作的時(shí)候會頻繁進(jìn)行磁盤IO操作,事務(wù)日志的不斷追加寫操作會觸發(fā)底層磁盤IO為文件開辟新的磁盤塊,即磁盤Seek

因此,為了提升磁盤IO的效率,Zookeeper在創(chuàng)建事務(wù)日志文件的時(shí)候就進(jìn)行文件空間的預(yù)分配- 即在創(chuàng)建文件的時(shí)候,就向操作系統(tǒng)申請一塊大一點(diǎn)的磁盤塊。這個(gè)預(yù)分配的磁盤大小可以通過系統(tǒng)參數(shù) zookeeper.preAllocSize 進(jìn)行配置。

事務(wù)日志文件名為: log.<當(dāng)時(shí)最大事務(wù)ID>,應(yīng)為日志文件時(shí)順序?qū)懭氲?#xff0c;所以這個(gè)最大事務(wù)ID也將是整個(gè)事務(wù)日志文件中,最小的事務(wù)ID,日志滿了即進(jìn)行下一次事務(wù)日志文件的創(chuàng)建


數(shù)據(jù)快照

數(shù)據(jù)快照用于記錄Zookeeper服務(wù)器上某一時(shí)刻的全量數(shù)據(jù),并將其寫入到指定的磁盤文件中。

可以通過配置snapCount配置每間隔事務(wù)請求個(gè)數(shù),生成快照,數(shù)據(jù)存儲在dataDir 指定的目錄中,

可以通過如下方式進(jìn)行查看快照數(shù)據(jù)( 為了避免集群中所有機(jī)器在同一時(shí)間進(jìn)行快照,實(shí)際的快照生成時(shí)機(jī)為事務(wù)數(shù)達(dá)到 [snapCount/2 + 隨機(jī)數(shù)(隨機(jī)數(shù)范圍為1 ~ snapCount/2 )] 個(gè)數(shù)時(shí)開始快照)

查看數(shù)據(jù)快照數(shù)據(jù) SnapshotFormatter

進(jìn)入到 lib目錄下

執(zhí)行 java -cp slf4j-api-1.7.25.jar:zookeeper-3.5.8.jar:zookeeper-jute-3.5.8.jar org.apache.zookeeper.server.SnapshotFormatter /tmp/zookeeper/version-2/snapshot.0

[root@localhost lib]# java -cp slf4j-api-1.7.25.jar:zookeeper-3.5.8.jar:zookeeper-jute-3.5.8.jar org.apache.zookeeper.server.SnapshotFormatter /tmp/zookeeper/version-2/snapshot.0 SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. ZNode Details (count=5): ---- /cZxid = 0x00000000000000ctime = Thu Jan 01 08:00:00 CST 1970mZxid = 0x00000000000000mtime = Thu Jan 01 08:00:00 CST 1970pZxid = 0x00000000000000cversion = 0dataVersion = 0aclVersion = 0ephemeralOwner = 0x00000000000000dataLength = 0 ---- /zookeepercZxid = 0x00000000000000ctime = Thu Jan 01 08:00:00 CST 1970mZxid = 0x00000000000000mtime = Thu Jan 01 08:00:00 CST 1970pZxid = 0x00000000000000cversion = 0dataVersion = 0aclVersion = 0ephemeralOwner = 0x00000000000000dataLength = 0 ---- /zookeeper/configcZxid = 0x00000000000000ctime = Thu Jan 01 08:00:00 CST 1970mZxid = 0x00000000000000mtime = Thu Jan 01 08:00:00 CST 1970pZxid = 0x00000000000000cversion = 0dataVersion = 0aclVersion = -1ephemeralOwner = 0x00000000000000dataLength = 0 ---- /zookeeper/quotacZxid = 0x00000000000000ctime = Thu Jan 01 08:00:00 CST 1970mZxid = 0x00000000000000mtime = Thu Jan 01 08:00:00 CST 1970pZxid = 0x00000000000000cversion = 0dataVersion = 0aclVersion = 0ephemeralOwner = 0x00000000000000dataLength = 0 ---- Session Details (sid, timeout, ephemeralCount): [root@localhost lib]#

快照事務(wù)日志文件名為: snapshot.<當(dāng)時(shí)最大事務(wù)ID>,日志滿了即進(jìn)行下一次事務(wù)日志文件的創(chuàng)建


事務(wù)日志 VS 快照數(shù)據(jù)

快照數(shù)據(jù)主要時(shí)為了快速恢復(fù), 事務(wù)日志文件是每次事務(wù)請求都會進(jìn)行追加的操作,而快照是達(dá)到某種設(shè)定條件下的內(nèi)存全量數(shù)據(jù)。

所以通常快照數(shù)據(jù)是反應(yīng)當(dāng)時(shí)內(nèi)存數(shù)據(jù)的狀態(tài)。事務(wù)日志是更全面的數(shù)據(jù),所以恢復(fù)數(shù)據(jù)的時(shí)候,可以先恢復(fù)快照數(shù)據(jù),再通過增量恢復(fù)事務(wù)日志中的數(shù)據(jù)即可。

總結(jié)

以上是生活随笔為你收集整理的Apache ZooKeeper - ZK的内存数据 + 持久化事务日志 + 数据快照 初探的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。