如果开启journal,在dbpath选项指定的目录下会创建journal目录来存放journal文件,文件名形如j._<n>。journal文件用于数据库异常退出时恢复数据。这里是解析journal文件的示例代码。

journal文件的大小定义如下,smallfiles选项可以指定为128M。

// Rotate after reaching this data size in a journal (j._<n>) file
// We use a smaller size for 32 bit as the journal is mmapped during recovery (only)
// Note if you take a set of datafiles, including journal files, from 32->64 or vice-versa, it must
// work.  (and should as-is)
// --smallfiles makes the limit small.#if defined(_DEBUG)unsigned long long DataLimitPerJournalFile = 128 * 1024 * 1024;
#elif defined(__APPLE__)// assuming a developer box if OS Xunsigned long long DataLimitPerJournalFile = 256 * 1024 * 1024;
#elseunsigned long long DataLimitPerJournalFile= (sizeof(void*)==4) ? 256 * 1024 * 1024 : 1 * 1024 * 1024 * 1024;
#endif

DataLimitPerJournalFile

journal文件什么时候会被删除?

  • 数据库正常退出时,全部删除。
  • 对于老的journal文件,如果lastEventTimeMs < _lastFlushTime + ExtraKeepTimeMs,则删除。其中lastEventTimeMs为journal文件关闭的时间,_lastFlushTime为数据文件刷入到磁盘的时间,ExtraKeepTimeMs为额外的保留时间。
  • 执行fsync命令

journal文件存储的是对数据库文件(dbname.ns、dbname.<#>系列文件)的修改日志,包括写操作和创建文件操作。对数据库文件的写操作会记录一个WriteIntent,创建数据库文件会记录一个DurOp。WriteIntent记录了写操作的指针和长度,可以定位到修改的数据文件的位置和长度。DurOp由一个操作码来确定是什么操作,不同的操作,日志的格式不一样。每个WriteIntent或者DurOp都会形成一个JEntry。

journal文件由一个头部JHeader和很多Section组成,每次groupCommit都会产生一个Section。Section由一个头部JSectHeader、很多个JEntry和一个JSectFooter组成,每个JEntry代表一条修改日志。另外,JEntry前面可能会有一个JDbContext,表示修改的是哪个数据文件。如果多个JEntry都是同一个数据文件的操作,则只有一个JDbContext。除掉JSectHeader 和JSectFooter ,Section中间那部分会压缩。

JHeader

beginning header for a journal/j._<n> file
there is nothing important int this header at this time. except perhaps version #.

char magic[2] "j\n". j means journal, then a linefeed, fwiw if you were to run "less" on the file or something...
unsigned short _version 0x4149
char n1 '\n'
char ts[20] ascii timestamp of file generation.  for user reading, not used by code. for example: "Jan 29 14:14:23"
char n2 '\n'
char dbpath[128] path/filename of this file for human reading and diagnostics.  not used by code
char n3, n4 '\n', '\n'
unsigned long long fileId unique identifier that will be in each JSectHeader. important as we recycle prealloced files
char reserved3[8026] 8KB total for the file header
char txt2[2] "\n\n" at the end

头部的大部分字段都是可读的,很直观,用文本文件打开可以看到。

JSectHeader

"Section" header. A section corresponds to a group commit.len is length of the entire section including header and footer.header and footer are not compressed, just the stuff in between.

unsigned _sectionLen unpadded length in bytes of the whole section
unsigned long long seqNumber sequence number that can be used on recovery to not do too much work
unsigned long long fileId matches JHeader::fileId

_sectionLen是unpadded的长度,每个Section都会填充对齐。

每个Section都会有一个seqNumber,其值是上次同步数据库文件到磁盘的服务器时间,会不断增长。LSNFile(文件名是lsn)里面也存储了一个seqNumber,含义是一样的。恢复数据的时候,如果Section的seqNumber小于LSNFile的seqNumber,则不需要恢复。

JEntry

an individual write operation within a group commit section. Either the entire section should be applied, or nothing. (We check the md5 for the whole section before doing anything on recovery.)

unsigned len  length in bytes of the data of the JEntry. does not include the JEntry header. 实际就是数据data的长度
OpCodes opcode 此字段和上面len字段共用的。如果是对数据库文件的修改,则为长度len;否则为操作类型。
unsigned ofs offset in file. 指的是被修改的数据文件的偏移量
int _fileNo
high bit is set to indicate it should be the <dbpath>/local database. dbname.#文件中的数字,即#。
如果是dbname.ns文件,则值为0x7fffffff。高位bit为1表示是local库。
char data[] 更新的数据

OpCodes的取值

OpCode_Footer JSectFooter的sentinel字段的取值
OpCode_DbContext JDbContext的sentinel字段的取值
OpCode_FileCreated 表示创建pdfile的操作
OpCode_DropDb 表示dropDb的操作,目前没有使用

这几个取值都很大,因为跟JEntry的len是共用,必须比可能的长度值大得多
事实上JEntry、JDbContext和JSectFooter的前4字节是含义是相同的。这样比较容易解析。

当JEntry为Op时,格式根据不同的Op而变化,如OpCode_FileCreated的格式为

OpCodes opcode OpCode_FileCreated
unsigned long long reserved1  
unsigned long long reserved2  
unsigned long long _len size of file, not length of name
RelativePath _p 文件名的相对路径,以'\0'结尾的字符串

JDbContext

declares "the next entry(s) are for this database / file path prefix"

sentinel 哨兵值,为了解析方便,为OpCode_DbContext
file path 库的相对路径,相对于dbpath,以'\0'结尾的字符串。for a filename a/b/c.3, file path() is "a/b/c"

JSectFooter

group commit section footer. md5 is a key field.

unsigned sentinel 哨兵值,为了解析方便,为OpCode_Footer
unsigned char hash[16] 除掉JSectFooter,整个Section的checksum
unsigned long long reserved  
char magic[4] "\n\n\n\n"

LSNFile

last sequence number. lsn文件。

unsigned ver 为0,用以检查是否合法
unsigned reserved2  
unsigned long long lsn last sequence number。值为数据库文件上次同步到磁盘的时间。
unsigned long long checkbytes lsn的值取反,用以检查是否合法
unsigned long long reserved[8]  

转载于:https://www.cnblogs.com/tripleH/archive/2013/03/21/2972678.html

mongodb journal文件格式相关推荐

  1. mongodb journal文件格式(不错)

    如果开启journal,在dbpath选项指定的目录下会创建journal目录来存放journal文件,文件名形如j._<n>.journal文件用于数据库异常退出时恢复数据.这里是解析j ...

  2. mongodb journal占用磁盘处理

    1.停止mongodb服务 service mongodb stop 或 kill -2 进程号 2.修改mongodb配置信息 vim /etc/mongodb.conf nojournal = t ...

  3. mongodb数据文件格式

    mongodb的数据文件存在dbpath选项指定的目录里.每个库(database)都有一系列的文件:dbname.ns, dbname.0, dbname.1, ...数据文件也叫pdfile,意思 ...

  4. mongodb journal

    how mongodbs journaling works I was working on a section on the gooey innards of journaling for The ...

  5. mysql和mongodb配合_MongoDB和Mysql怎样结合

    二者结构有何不同? SQL中的许多概念都与MongoDB的文档结构相关.让我们来看一个简单的MongoDB环境结构,以更好地了解MongoDB的布局. 下面的图表涉及MySQL与MongoDB的不同点 ...

  6. Linux下的Mongodb部署应用梳理

    一.Mongodb简介 官网地址:http://www.mongodb.org/ MongoDB是一个高性能,开源,无模式的文档型数据库,是当前NoSql数据库中比较热门的一种.MongoDB 是一个 ...

  7. mongodb 初学 意外 连接服务器异常(Connection refused)

    啦啦啦 这种情况 root@localhost:/# mongo MongoDB shell version: 3.2.13 connecting to: test 2017-05-31T07:40: ...

  8. MongoDB 3.0 WiredTiger Compression and Performance

    MongoDB3.0中的压缩选项 在MongoDB 3.0中,WiredTiger为集合提供三个压缩选项: 无压缩 Snappy(默认启用) – 很不错的压缩,有效利用资源 zlib(类似gzip) ...

  9. mongodb 搜索速度_初识 MongoDB 数据库

    初识 MongoDB 数据库 前言 Flask 基础框架在之前的三篇文章中写完了.想要学习 web 相关的同学可以自己回顾翻一下,相信看完了,你也可以写出来一个简单的小案例来炫耀一波! 说到 web ...

最新文章

  1. 新手入门深度学习 | 2-2:结构化数据建模流程示例
  2. Gridview 手动排序实现
  3. 实现、设置-Android TabWidget-by小雨
  4. 上传一个 游戏server架构图
  5. Mybatis一级缓存、整合第三方缓存ehcache、Mybatis二级缓存
  6. Linux上简体繁体文件的相互转换
  7. 字符串指针与一维指针数组的区别
  8. zookeeper客户端下载与使用
  9. Oracle 取某100天的每一天的日期
  10. Linux: 介绍make menuconfig中的每个选项含义【转】
  11. 计算机组成原理中EMAR是什么,计算机组成原理(罗克露)第3章cpu.ppt
  12. [Android 4.4.3] 泛泰A860 Omni4.4.3 20140610 RC2.0 三版通刷 by syhost
  13. 社工必备查询网址汇总
  14. 计算机网络无法连接共享打印机驱动,Windows 10 安装网络共享打印机失败,提示0x00009c4a 无法连接到打印机解决办法...
  15. dword 占用多少信_【优质文档】土地占用投诉信-精选word文档 (5页)
  16. 中国不是不能开发出自己的浏览器,而是没必要
  17. MySQL 插入语句
  18. 怎样使用JS代码代码跳转的方法
  19. arm_neon.h引用
  20. 详述图片base64加密的原理,告诉你什么是“/9j/“

热门文章

  1. epson连接计算机后无法打印,epson打印机无法打印,教您epson打印机无法打印怎么解决...
  2. 引用拷贝、对象拷贝、浅拷贝、深拷贝 到底是什么【详细例子介绍】
  3. 闲人闲谈PS之一项目库存跨公司业务STO解决方案--SAP闲人的开篇
  4. Android-WebView加载网页
  5. 【光学设计基础】--02球差
  6. uplload 通关纪实 pass1
  7. 牛客小白月赛25 C-白魔法师 ( 图论 + 并查集 )
  8. 基于RT-Thread的两轮平衡小车设计
  9. 回顾维乐VELO创始人余彩云漫漫创新路
  10. ESP8266占空比测试