Zookeeper事务日志预分配空间解析
admin
2024-03-14 23:57:10
0次

前言:

Zookeeper的通过快照日志和事务日志将内存信息保存下来,记录下来每次请求的具体信息。

尤其是其事务日志,每次处理事务请求时都需要将其记录下来。

Zookeeper事务日志的默认存储方式是磁盘文件,那么Zookeeper的总体性能就受限与磁盘文件的写入速度。

针对这个瓶颈,Zookeeper做了什么优化操作呢,本文我们就一起来了解下。

1.事务日志的预分配

事务日志的添加,我们需要从FileTxnLog.append()方法看起

public class FileTxnLog implements TxnLog {volatile BufferedOutputStream logStream = null;volatile OutputArchive oa;volatile FileOutputStream fos = null;// 追加事务日志public synchronized boolean append(TxnHeader hdr, Record txn)throws IOException{if (hdr == null) {return false;}if (hdr.getZxid() <= lastZxidSeen) {LOG.warn("Current zxid " + hdr.getZxid()+ " is <= " + lastZxidSeen + " for "+ hdr.getType());} else {lastZxidSeen = hdr.getZxid();}// 默认logStream为空if (logStream==null) {if(LOG.isInfoEnabled()){LOG.info("Creating new log file: " + Util.makeLogName(hdr.getZxid()));}// 以下代码为创建事务日志文件// 根据当前事务ID来创建具体文件名,并写入文件头信息logFileWrite = new File(logDir, Util.makeLogName(hdr.getZxid()));fos = new FileOutputStream(logFileWrite);logStream=new BufferedOutputStream(fos);oa = BinaryOutputArchive.getArchive(logStream);FileHeader fhdr = new FileHeader(TXNLOG_MAGIC,VERSION, dbId);fhdr.serialize(oa, "fileheader");// Make sure that the magic number is written before padding.logStream.flush();filePadding.setCurrentSize(fos.getChannel().position());streamsToFlush.add(fos);}// 预分配代码在这里filePadding.padFile(fos.getChannel());byte[] buf = Util.marshallTxnEntry(hdr, txn);if (buf == null || buf.length == 0) {throw new IOException("Faulty serialization for header " +"and txn");}Checksum crc = makeChecksumAlgorithm();crc.update(buf, 0, buf.length);oa.writeLong(crc.getValue(), "txnEntryCRC");Util.writeTxnBytes(oa, buf);return true;}
}

创建FileTxnLog对象时,其logStream属性为null,所以当第一次处理事务请求时,会先根据当前事务ID来创建一个文件。

1.1 事务日志预分配

public class FilePadding {long padFile(FileChannel fileChannel) throws IOException {// 针对新文件而言,newFileSize=64Mlong newFileSize = calculateFileSizeWithPadding(fileChannel.position(), currentSize, preAllocSize);if (currentSize != newFileSize) {// 将文件扩充到64M,全部用0来填充fileChannel.write((ByteBuffer) fill.position(0), newFileSize - fill.remaining());currentSize = newFileSize;}return currentSize;}// size计算public static long calculateFileSizeWithPadding(long position, long fileSize, long preAllocSize) {// If preAllocSize is positive and we are within 4KB of the known end of the file calculate a new file size// 初始时候position=0,fileSize为0,preAllocSize为系统参数执行,默认为64Mif (preAllocSize > 0 && position + 4096 >= fileSize) {// If we have written more than we have previously preallocated we need to make sure the new// file size is larger than what we already have// Q:这里确实没看懂...if (position > fileSize) {fileSize = position + preAllocSize;fileSize -= fileSize % preAllocSize;} else {fileSize += preAllocSize;}}return fileSize;}
}

预分配的过程比较简单,就是看下当前文件的剩余空间是否<4096,如果是,则扩容。

Q:

这里有一个不太明白的问题,position > fileSize的场景是怎样的呢?

2.创建新的事务日志文件时机

通过上述代码分析我们知道,当logStream=null时,就会创建一个新的事务日志文件,那么logStream对象什么时候为空呢?

搜索代码,只看到FileTxnLog.rollLog()方法会主动将logStream设置为null

public class FileTxnLog implements TxnLog {public synchronized void rollLog() throws IOException {if (logStream != null) {this.logStream.flush();this.logStream = null;oa = null;}}
}

那么根据这个线索,我们来搜索下rollLog的调用链

SyncRequestProcessor.run() -> ZKDatabase.rollLog() -> FileTxnSnapLog.rollLog() -> FileTxnLog.rollLog()

最终看到是在SyncRequestProcessor.run()方法中发起调用的,而且只有这一条调用链,我们来分析下

2.1 SyncRequestProcessor.run()

public class SyncRequestProcessor extends ZooKeeperCriticalThread implements RequestProcessor {public void run() {try {int logCount = 0;setRandRoll(r.nextInt(snapCount/2));while (true) {...if (si != null) {// 追加事务日志if (zks.getZKDatabase().append(si)) {logCount++;if (logCount > (snapCount / 2 + randRoll)) {setRandRoll(r.nextInt(snapCount/2));// 注意:在这里发起了rollLogzks.getZKDatabase().rollLog();...}} else if (toFlush.isEmpty()) {...}toFlush.add(si);if (toFlush.size() > 1000) {flush(toFlush);}}}} catch (Throwable t) {handleException(this.getName(), t);running = false;}LOG.info("SyncRequestProcessor exited!");}
}

需要注意下rollLog()方法执行的条件,就是logCount > (snapCount / 2 + randRoll)

snapCount是一个系统参数,System.getProperty("zookeeper.snapCount"),默认值为100000

randRoll是一个随机值

那么该条件触发的时机为:处理的事务请求数至少要大于50000。

这时就出现了一个笔者无法理解的情况:

通过对事务日志的观察可以看到其都是64M,而至少处理50000次事务请求后,Zookeeper才会分配一个新的事务日志文件,那么这个snapCount是一个经验值嘛?

如果事务请求的value信息都很大,那么可能到不了50000次,就会超过64M,理论上应该要创建一个新的文件了,但是貌似并没有,这个该怎么处理呢?

如果事务请求value信息都很小,那么即使到了50000次,也不会超过64M,那么之前预分配的文件大小就浪费了一部分。

总结:

希望有比较懂的小伙伴给点意见,感谢!

相关内容

热门资讯

AI做攻略、“双向奔赴”……国... 本文转自:人民网人民网记者 许维娜今年国庆假期,AI(人工智能)成了不少年轻人的“旅行搭子”。行前做...
合肥的这个“梗”, 翻篇了 社交平台的评论区,曾长期流传一句专属于合肥的黑色幽默。有人问合肥哪里好玩,本地网友半开玩笑地回复:合...
7万+旅游中高层都在的群,今天... 做旅游这行,资源卡位、渠道互通、项目落地——往往就差一个“对的机会”。我们的行业社群已聚集70,00...
一场帐篷音乐节,如何让井研从“... 帐篷音乐节现场封面新闻记者 周洪攀10月的研溪湿地,晚风里裹着柑橘的清甜与民谣的回响。夜幕降临,10...
白鹤翩翩落星愿,上海迪士尼度假... 10月9日,世界候鸟日前夕,上海迪士尼度假区星愿公园的湖风里多了一分白鹤的灵动。三座白鹤雕塑正式揭幕...
成都锦江边赏初秋枫叶飘飘 目前,成都锦江边国华街枫叶树红了。近年来,国华街附近的几条街都栽种了红枫树,是城区较集中看枫叶的点位...
GBTA:商务旅行正在复苏,但... 亚太正在成为全球商务旅行增长的重要引擎。在经历疫情后的快速反弹之后,全球商务旅行正在进入一个新的增长...
从景区焕新到节赛引流 邹城双节... 齐鲁网·闪电新闻10月9日讯 中秋、国庆“双节”假期,邹城市文化和旅游局提前谋划、周密部署,立足自身...
江山如此多娇 且看石楼今朝 金秋十月,黄河岸畔秋意盎然,丰收盛会喜迎国庆。本次石楼国庆文旅活动以“江山如此多娇,且看石楼今朝”为...
海南国庆假期接待游客475.4... 2026年国庆假期,海南接待游客475.40万人次,较2025年增长13.1%,较2024年增长15...