Class WalReader
This utility reads WAL files created by WalWriter and performs recovery:
- Parses log entries with their term, index, explicit type when present, and payload bytes
- Validates CRC32 checksums to detect corruption
- Skips corrupted entries at the end of the file (from crash recovery)
- Returns recovery metadata including entries, statistics, and last valid index
Entry format in WAL: - 4 bytes: entry length (big-endian int) - 8 bytes: term (big-endian long) - 8 bytes: index (big-endian long) - 1 byte: entry type for new-format entries - N bytes: payload data - 4 bytes: CRC32 checksum (big-endian int)
Thread-safe: The readAll() method can be called concurrently, but the returned metadata should not be modified.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumstatic final recordRecovery metadata returned after reading a WAL file. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionRead all entries from a WAL file and validate checksums.readAll(Path walFile, @Nullable LoomMetrics metrics) Read all entries from a WAL file and validate checksums with optional metrics.readAll(Path walFile, @Nullable LoomMetrics metrics, WalReader.RecoveryMode recoveryMode) Read all entries with explicit recovery behavior for corrupt or torn WAL tails.
-
Constructor Details
-
WalReader
public WalReader()
-
-
Method Details
-
readAll
Read all entries from a WAL file and validate checksums.Performs crash recovery by: 1. Reading entries sequentially from the beginning of the WAL file 2. Validating CRC32 checksum for each entry 3. Stopping at the first corrupted/incomplete entry (crash boundary) 4. Truncating the WAL file at the corruption point to remove corrupt data 5. Returning successfully recovered entries plus recovery statistics
If the WAL file does not exist, returns an empty recovery metadata (no error). If the WAL file is empty or starts with corruption, returns empty entries with statistics.
Entry length validation prevents excessive memory allocation: - Minimum: 20 bytes (term + index + checksum) - Maximum: 100 MB (configurable defense against memory attacks)
- Parameters:
walFile- Path to the WAL file to read (must not be null)- Returns:
- WalRecoveryMetadata with recovered entries and diagnostic statistics
- Throws:
IOException- if file I/O operations fail (e.g., permission denied, disk errors). Note that corruption at the end of the file is NOT an error (expected in crash recovery).NullPointerException- if walFile is null
-
readAll
public static WalReader.WalRecoveryMetadata readAll(Path walFile, @Nullable LoomMetrics metrics) throws IOException Read all entries from a WAL file and validate checksums with optional metrics.Performs crash recovery by: 1. Reading entries sequentially from the beginning of the WAL file 2. Validating CRC32 checksum for each entry 3. Stopping at the first corrupted/incomplete entry (crash boundary) 4. Returning successfully recovered entries plus recovery statistics
If the WAL file does not exist, returns an empty recovery metadata (no error). If the WAL file is empty or starts with corruption, returns empty entries with statistics.
Entry length validation prevents excessive memory allocation: - Minimum: 20 bytes (term + index + checksum) - Maximum: 100 MB (configurable defense against memory attacks)
- Parameters:
walFile- Path to the WAL file to read (must not be null)metrics- Optional LoomMetrics instance to record CRC validation failures- Returns:
- WalRecoveryMetadata with recovered entries and diagnostic statistics
- Throws:
IOException- if file I/O operations fail (e.g., permission denied, disk errors). Note that corruption at the end of the file is NOT an error (expected in crash recovery).NullPointerException- if walFile or metrics is null
-
readAll
public static WalReader.WalRecoveryMetadata readAll(Path walFile, @Nullable LoomMetrics metrics, WalReader.RecoveryMode recoveryMode) throws IOException Read all entries with explicit recovery behavior for corrupt or torn WAL tails.- Parameters:
walFile- Path to the WAL file to readmetrics- Optional LoomMetrics instance to record CRC validation failuresrecoveryMode- Whether to truncate a torn tail or fail before partial recovery- Returns:
- WalRecoveryMetadata with recovered entries and diagnostic statistics
- Throws:
IOException- if file I/O fails, or corruption is rejected by recoveryMode
-