交流
商城
MCN
登入
注册
首页
提问
分享
讨论
建议
公告
动态
发表新帖
发表新帖
第6-2 章 JournalManager
分享
未结
0
775
李延
LV6
2022-03-08
悬赏:20积分
# 1、 作用 管理单个文件目录。通过他可以创建 新的EditLogOutputStream 或者,关闭已经完成的EditLogOutputStream 。 ## 2、 JournalManager 我们主要关注2个接口,其他接口在集群模式下,有需要再补充 ```java /** * Begin writing to a new segment of the log stream, which starts at * the given transaction ID. */ EditLogOutputStream startLogSegment(long txId, int layoutVersion) throws IOException; /** * Mark the log segment that spans from firstTxId to lastTxId * as finalized and complete. */ void finalizeLogSegment(long firstTxId, long lastTxId) throws IOException; ``` 这里我们看到一个startLogSegment ,根据txId, 创建一个新的日志文件 而另一个finalizeLogSegment,是将已经记录完成的日志关闭。 # 3、 FileJournalManager 同样看一下本地文件File的实现 ## 3.1 startLogSegment ```java @Override synchronized public EditLogOutputStream startLogSegment(long txid, int layoutVersion) throws IOException { try { //sd为目录的封装,根据目录与txid生成File对象 currentInProgress = NNStorage.getInProgressEditsFile(sd, txid); //创建输入流 EditLogOutputStream stm = new EditLogFileOutputStream(conf, currentInProgress, outputBufferCapacity); stm.create(layoutVersion); return stm; } catch (IOException e) { LOG.warn("Unable to start log segment " + txid + " at " + currentInProgress + ": " + e.getLocalizedMessage()); errorReporter.reportErrorOnFile(currentInProgress); throw e; } } ``` 我们看一下NNStorage.getInProgressEditsFile方法 ``` static File getInProgressEditsFile(StorageDirectory sd, long startTxId) { return new File(sd.getCurrentDir(), getInProgressEditsFileName(startTxId)); } ``` 就是简单的new了一个File对象 ## 3.2 finalizeLogSegment ``` @Override synchronized public void finalizeLogSegment(long firstTxId, long lastTxId) throws IOException { //获取到以firstTxId明名的文件 File inprogressFile = NNStorage.getInProgressEditsFile(sd, firstTxId); // 创建我们需要改名后的文件 File dstFile = NNStorage.getFinalizedEditsFile( sd, firstTxId, lastTxId); LOG.info("Finalizing edits file " + inprogressFile + " -> " + dstFile); //检查文件名是否存储 Preconditions.checkState(!dstFile.exists(), "Can't finalize edits file " + inprogressFile + " since finalized file " + "already exists"); try { //重命名 NativeIO.renameTo(inprogressFile, dstFile); } catch (IOException e) { errorReporter.reportErrorOnFile(dstFile); throw new IllegalStateException("Unable to finalize edits file " + inprogressFile, e); } if (inprogressFile.equals(currentInProgress)) { currentInProgress = null; } } ``` # 4 JournalSet JournalManager的集合,也继承自JournalManager,有JournalManager的所有方法。因为,日志目录可以存储在多个地方。所以我们只需要将其添加到这个集合中,当我们调用这个集合的方法时,就是在调用集合每个元素的方法。 具体逻辑较简单不在复述。
回帖
消灭零回复
提交回复
热议榜
java 相关知识分享
8
好的程序员与不好的程序员
6
写给工程师的十条精进原则
5
spring boot以jar包运行配置的logback日志文件没生成
5
一步一步分析SpringBoot启动源码(一)
5
MockMvc测试
5
【吐槽向】是不是有个吐槽的板块比较好玩
4
logstash jdbc同步mysql多表数据到elasticsearch
3
IntelliJ IDEA 优质License Server
3
.gitignore忽略规则
3
SpringBoot启动源码分析
3
一步一步分析SpringBoot启动源码(三)
3
2
一步一步分析SpringBoot启动源码(二)
2
积分不够将无法发表新帖
2
官方产品
Meta-Boot - 基于MCN
MCN - 快速构建SpringBoot应用
微信扫码关注公众号