The following is a shell script that clears three zookeeper nodes (making it a scheduled task):
zookeeper home
Zkdir=/server/app/zookeeper-3.4.6
snapshot file dir
snapDir=/mnt/data/zookeeper/n_1
snapDir2=/mnt/data/zookeeper/n_2
snapDir3=/mnt/data/zookeeper/n_3
tran log dir
dataDir=/mnt/data/zookeeper/n_1
dataDir2=/mnt/data/zookeeper/n_2
dataDir3=/mnt/data/zookeeper/n_3
Leave 30 files
Count=15
Cd /server/app/zookeeper-3.4.6
Java -cp ${zkdir}/zookeeper-3.4.6.jar:${zkdir}/lib/slf4j-api-1.6.1.jar:${zkdir}/lib/slf4j-log4j12-1.6.1.jar: ${zkdir}/lib/log4j-1.2.16.jar:${zkdir}/conf org.apache.zookeeper.server.PurgeTxnLog ${dataDir} ${snapDir} -n ${count}
Java -cp ${zkdir}/zookeeper-3.4.6.jar:${zkdir}/lib/slf4j-api-1.6.1.jar:${zkdir}/lib/slf4j-log4j12-1.6.1.jar: ${zkdir}/lib/log4j-1.2.16.jar:${zkdir}/conf org.apache.zookeeper.server.PurgeTxnLog ${dataDir2} ${snapDir2} -n ${count}
Java -cp ${zkdir}/zookeeper-3.4.6.jar:${zkdir}/lib/slf4j-api-1.6.1.jar:${zkdir}/lib/slf4j-log4j12-1.6.1.jar: ${zkdir}/lib/log4j-1.2.16.jar:${zkdir}/conf org.apache.zookeeper.server.PurgeTxnLog ${dataDir3} ${snapDir3} -n ${count}
Since the zookeeper log file cannot be deleted at will, it is entirely possible that a node that has not been updated for a long time exists in a log file a few days ago. So how do you safely delete them? You can write your own program, but zookeeper also provides us with a handy gadget: PurgeTxnLog Usage:
Java -Djava.ext.dirs=lib org.apache.zookeeper.server.PurgeTxnLog log_path snap_path -n 10
Where -n indicates how many files to keep, not less than 3 log_path and snap_path must be the root path of the zookeeper’s log, which is the version-x path. The code will go to the input path to find the version-x directory, and then go to the following log file