如何限制 zookeeper 的 transaction log 大小

語言: CN / TW / HK

Zookeeper 中限制 transaction log 總大小主要有兩種方法。

zookeeper
如果想及時瞭解Spark、Hadoop或者HBase相關的文章,歡迎關注微信公眾號: iteblog_hadoop

限制 Zookeeper Transaction Log 裡面的事務條數

預設情況下,在寫入 snapCount(100000) 事務後,Zookeeper 事務日誌將會切換。如果 Zookeeper 的資料目錄的空間不足與儲存三個版本的 Zookeeper Transaction Logs,那麼我們需要限制每個 Transaction Logs 日誌的大小。

比如,如果我們把 zoo.cfg 檔案裡面的 snapCount 引數設定為20,那麼在我的測試環境下,將產生以下的事務日誌:

[root@v7 version-2]# ls -altr log*
-rw-r--r-- 1 mapr mapr 67108880 Jan 11 18:40 log.40000000d
-rw-r--r-- 1 mapr mapr 67108880 Jan 11 18:40 log.40000001f
-rw-r--r-- 1 mapr mapr 67108880 Jan 11 18:40 log.400000030
 
[root@v7 version-2]#  java -cp /opt/mapr/lib/zookeeper-3.4.5-mapr-1503.jar:/opt/mapr/lib/log4j-1.2.17.jar:/opt/mapr/lib/slf4j-log4j12-1.7.12.jar:/opt/mapr/lib/slf4j-api-1.7.12.jar org.apache.zookeeper.server.LogFormatter log.40000000d|tail -1
EOF reached after 18 txns.
[root@v7 version-2]#  java -cp /opt/mapr/lib/zookeeper-3.4.5-mapr-1503.jar:/opt/mapr/lib/log4j-1.2.17.jar:/opt/mapr/lib/slf4j-log4j12-1.7.12.jar:/opt/mapr/lib/slf4j-api-1.7.12.jar org.apache.zookeeper.server.LogFormatter log.40000001f|tail -1
EOF reached after 17 txns.
[root@v7 version-2]#  java -cp /opt/mapr/lib/zookeeper-3.4.5-mapr-1503.jar:/opt/mapr/lib/log4j-1.2.17.jar:/opt/mapr/lib/slf4j-log4j12-1.7.12.jar:/opt/mapr/lib/slf4j-api-1.7.12.jar org.apache.zookeeper.server.LogFormatter log.400000030|tail -1
EOF reached after 8 txns.

限制 Zookeeper Transaction Log 的塊大小

儘管我們進行了上面的設定,從上面的輸出我們可以看到每個日誌仍然是64MB。很顯然,這些檔案裡面的很多空間是讓費的,因為最小塊大小被設定為 preAllocSize 引數配置的值。

比如,我們將 zoo.cfg 檔案裡面的 preAllocSize 引數設定為 1MB,然後重啟 Zookeeper:

[root@v5 conf]# cat zoo.cfg |grep pre
preAllocSize=1000

重啟之後,Zookeeper Transaction Log 最新的塊大小變成了 1MB:

[root@v7 version-2]# ls -altr log*
-rw-r--r-- 1 mapr mapr 67108880 Jan 11 18:38 log.400000001
-rw-r--r-- 1 mapr mapr 67108880 Jan 11 18:40 log.40000000d
-rw-r--r-- 1 mapr mapr 67108880 Jan 11 18:40 log.40000001f
-rw-r--r-- 1 mapr mapr 67108880 Jan 11 18:40 log.400000030
-rw-r--r-- 1 mapr mapr  1024016 Jan 11 19:43 log.500000001
-rw-r--r-- 1 mapr mapr  1024016 Jan 11 19:43 log.500000013
-rw-r--r-- 1 mapr mapr  1024016 Jan 11 19:43 log.500000022
-rw-r--r-- 1 mapr mapr  1024016 Jan 11 19:43 log.50000002e
-rw-r--r-- 1 mapr mapr  1024016 Jan 11 19:43 log.50000003c

關於 Zookeeper 的磁碟空間預分配策略可以參見過往記憶大資料這篇文章:Apache Zookeeper 磁碟空間預分配策略

preAllocSize 和 snapCount 兩個引數解釋

Zookeeper 官方文件裡面有對這兩個引數的詳細介紹,參見 https://zookeeper.apache.org/doc/current/zookeeperAdmin.html。

preAllocSize

(Java system property: zookeeper.preAllocSize) To avoid seeks ZooKeeper allocates space in the transaction log file in blocks of preAllocSize kilobytes. The default block size is 64M. One reason for changing the size of the blocks is to reduce the block size if snapshots are taken more often. (Also, see snapCount and snapSizeLimitInKb).

snapCount

(Java system property: zookeeper.snapCount) ZooKeeper records its transactions using snapshots and a transaction log (think write-ahead log).The number of transactions recorded in the transaction log before a snapshot can be taken (and the transaction log rolled) is determined by snapCount. In order to prevent all of the machines in the quorum from taking a snapshot at the same time, each ZooKeeper server will take a snapshot when the number of transactions in the transaction log reaches a runtime generated random value in the [snapCount/2+1, snapCount] range.The default snapCount is 100,000.

本部落格文章除特別宣告,全部都是原創!
轉載本文請加上:轉載自過往記憶(https://www.iteblog.com/)
本文連結: 【如何限制 zookeeper 的 transaction log 大小】(https://www.iteblog.com/archives/2046.html)

喜歡 (0) 分享 (0)