LotusDB 设计与实现—2 WAL 日志

2022-04-16 17:47:00 +08:00
 roseduan

LotusDB 是一个全新的 KV 存储引擎,Github 地址:https://github.com/flower-corp/lotusdb,希望大家多多支持呀,点个 star 或者参与进来!

WAL 是 Write Ahead Log 的简称,通常叫做预写日志,是为了预防内存崩溃,保证数据不丢失的常用手段。WAL 是 LSM 存储模型中重要的组件,在 LotusDB 当中的重要性是一样的。 试想一下,如果没有 WAL ,写入的数据直接到内存的话,由于内存是易失性的,崩溃之后数据无法恢复,如果数据写到一半发生了这种情况,会造成数据不一致甚至丢失,在一个系统底层的存储引擎当中,这通常是不可接受的。

日志结构的数据文件一般是追加写的,WAL 也是一样。在 LotusDB 当中写入 k/v 时,会先将数据封装成一条日志项 LogEntry ,并将其追加到 WAL ,日志项 LogEntry 的结构体定义如下:

// LogEntry is the data will be appended in log file.
type LogEntry struct {
	Key       []byte
	Value     []byte
	ExpiredAt int64 // time.Unix
	Type      EntryType
}

写入前需要将 LogEntry 结构体进行编码,然后再追加到文件中,编码后主要包含 key 、value 信息,还有对应的长度 size 信息,以及对整条数据有效性做校验的 crc 值,格式如下:

// EncodeEntry will encode entry into a byte slice.
// The encoded Entry looks like:
// +-------+--------+----------+------------+-----------+-------+---------+
// |  crc  |  type  | key size | value size | expiresAt |  key  |  value  |
// +-------+--------+----------+------------+-----------+-------+---------+
// |------------------------HEADER----------------------|
//         |--------------------------crc check---------------------------|

编码后的数据写到 WAL 时,LotusDB 提供了两种 IO 模式:系统标准 IO 和 mmap ,可在打开数据库时通过配置项进行选择,LogFile 结构体定义的 IoSelector 负责实现:

// LogFile is an abstraction of a disk file, entry`s read and write will go through it.
type LogFile struct {
	sync.RWMutex
	Fid        uint32
	WriteAt    int64
	IoSelector ioselector.IOSelector
}

LogFile 是操作文件数据读写的结构体,其中最重要的是 IoSelector ,它是一个 interface ,负责具体的读写操作,具体的实现有 FileIO 和 MMap 。

// IOSelector io selector for fileio and mmap, used by wal and value log right now.
type IOSelector interface {
	// Write a slice to log file at offset.
	// It returns the number of bytes written and an error, if any.
	Write(b []byte, offset int64) (int, error)

	// Read a slice from offset.
	// It returns the number of bytes read and any error encountered.
	Read(b []byte, offset int64) (int, error)

	// Sync commits the current contents of the file to stable storage.
	// Typically, this means flushing the file system's in-memory copy
	// of recently written data to disk.
	Sync() error

	// Close closes the File, rendering it unusable for I/O.
	// It will return an error if it has already been closed.
	Close() error

	// Delete delete the file.
	// Must close it before delete, and will unmap if in MMapSelector.
	Delete() error
}

LotusDB 中的一个 WAL 和 一个 memtable 绑定,从 memtable 的结构体定义就能够体现出来:

type memtable struct {
    sync.RWMutex
    sklIter      *arenaskl.Iterator
    skl          *arenaskl.Skiplist
    wal          *logfile.LogFile
    bytesWritten uint32 // number of bytes written, used for flush wal file.
    opts         memOptions
}

WAL 文件的大小和跟之相绑定的 memtable 的容量相关,由于 memtable 通常会有一个阈值,写满之后就关闭了,WAL 此时也不会接受新的写入,因此 WAL 文件的容量通常不会无限膨胀。 memtable 中的数据被后台线程 flush 到磁盘之后,并且没有其他的错误发生,WAL 就可以被安全的删除了。

在 LotusDB 启动打开 memtable 的时候,会全量加载 WAL 中的数据,逻辑很简单,就是打开 WAL 文件,然后遍历其中的每条数据,将其应用到 memtable 的跳表数据结构当中,通过这样的方式,达到了 WAL 恢复数据的作用,参考代码如下:

	for {
		if entry, size, err := wal.ReadLogEntry(offset); err == nil {
			offset += size
			// No need to use atomic updates.
			// This function is only be executed in one goroutine at startup.
			wal.WriteAt += size

			// build memtable...
            // ...
		} else {
			if err == io.EOF || err == logfile.ErrEndOfEntry {
				break
			}
			return nil, err
		}
	}

WAL 刷盘策略

数据写到 WAL 文件中,实际上并没有完全落到磁盘,由于操作系统的实现,可能只是到了 page cache ,需要我们手动调用 Flush 才能够真正将数据持久化。

针对 Flush 的策略,LotusDB 提供了两个配置项: 一是在 WriteOptions 中的 Sync ,如果在数据写入时传递了这个 Options 并且将 Sync 设置为 true ,那么写 WAL 完成后会立即 Flush ,这种策略能够保证数据不丢,但性能是最差的。

二是 ColumnFamilyOptions 中的 WalBytesFlush ,表示写入累积到配置的字节后进行 Flush ,可以理解为每 WalBytesFlush 个字节 Flush 一次。这种情况下,如果系统发生异常,最多丢失 WalBytesFlush 个字节的数据。 如果都不设置,则完全交给操作系统,这是默认的策略,性能是最好的,但是如果系统崩溃,丢失的数据可能是最多的。

LotusDB 也提供了手动进行刷盘的方法,用户可以在适当的时候,调用 Sync 方法进行数据刷盘持久化。

其他和 WAL 相关的配置项: ColumnFamilyOptions: WalMMap 是否使用 mmap 进行写入,默认:false WriteOptions: DisableWal 是否禁用 WAL ,默认:false


LotusDB Github 地址:https://github.com/flower-corp/lotusdb

1337 次点击
所在节点    程序员
2 条回复
NeroKamin
2022-04-16 21:28:29 +08:00
正在看这方面的东西,正好看见这个项目了,start 一波
roseduan
2022-04-17 10:02:07 +08:00
@NeroKamin 感谢

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/847366

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX