WAL files are generated with increasing sequence number in the WAL directory. In order to reconstruct the state of the database, these files are read in the order of sequence number. WAL manager provides the abstraction for reading the WAL files as a single unit. Internally, it opens and reads the files using Reader or Writer abstraction.

Writer provides an abstraction for appending log records to a log file. The medium specific internal details are handled by WriteableFile interface. Similarly, Reader provides an abstraction for sequentially reading log records from the log file. The internal medium specific details are handled by SequentialFile interface.

Log file consists of a sequence of variable length records. Records are grouped by (32k). If a certain record cannot fit into the leftover space, then the leftover space is padded with empty (null) data. The writer writes and the reader reads in chunks of kBlockSize.

The record layout format is as shown below. There are two kinds of record format, Legacy and Recyclable:

The Legacy Record Format

  1. +---------+-----------+-----------+--- ... ---+
  2. +---------+-----------+-----------+--- ... ---+
  3. CRC = 32bit hash computed over the payload using CRC
  4. Size = Length of the payload data
  5. Type = Type of record
  6. (kZeroType, kFullType, kFirstType, kLastType, kMiddleType )
  7. The type is used to group a bunch of records together to represent
  8. blocks that are larger than kBlockSize
  9. Payload = Byte stream as long as specified by the payload size

The Recyclable Record Format

Record Format Details For Legacy Format

The log file contents are a sequence of 32KB blocks. The only exception is that the tail of the file may contain a partial block.

  1. checksum: uint32 // crc32c of type and data[]
  2. length: uint16
  3. type: uint8 // One of FULL, FIRST, MIDDLE, LAST
  4. data: uint8[length]

A record never starts within the last six bytes of a block (since it won’t fit). Any leftover bytes here form the trailer, which must consist entirely of zero bytes and must be skipped by readers.

More types may be added in the future. Some Readers may skip record types they do not understand, others may report that some data was skipped.

The FULL record contains the contents of an entire user record.

FIRST, MIDDLE, LAST are types used for user records that have been split into multiple fragments (typically because of block boundaries). FIRST is the type of the first fragment of a user record, LAST is the type of the last fragment of a user record, and MID is the type of all interior fragments of a user record.

  1. B: length 97270
  2. C: length 8000

A will be stored as a FULL record in the first block.

B will be split into three fragments: first fragment occupies the rest of the first block, second fragment occupies the entirety of the second block, and the third fragment occupies a prefix of the third block. This will leave six bytes free in the third block, which will be left empty as the trailer.

C will be stored as a FULL record in the fourth block.

Some benefits over the recordio format:

  1. We do not need any heuristics for resyncing - just go to next block boundary and scan. If there is a corruption, skip to the next block. As a side-benefit, we do not get confused when part of the contents of one log file are embedded as a record inside another log file.
  2. Splitting at approximate boundaries (e.g., for mapreduce) is simple: find the next block boundary and skip records until we hit a FULL or FIRST record.
  3. We do not need extra buffering for large records.

Some downsides compared to recordio format:

  1. No compression. Again, this could be fixed by adding new record types.