Repository navigation
fix: wal replay recovery #314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
chrollo-lucifer-12
wants to merge
10
commits into
danieldeer:main
Choose a base branch
from
chrollo-lucifer-12:fix/wal-replay-recovery
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8abe56e
fix(wal): improve replay and recovery
chrollo-lucifer-12 61d4328
fix: manage WAL file handles during load, clear and unsuccessful writes
chrollo-lucifer-12 c9b704b
add: simple benchmark test for wal append
chrollo-lucifer-12 d4953c1
resolve merge conflicts
chrollo-lucifer-12 d60e09a
Merge branch 'main' into fix/wal-replay-recovery
chrollo-lucifer-12 8ea6fcd
resolve merge conflict, make close public method and idempotent and _…
chrollo-lucifer-12 99f9bae
fix Cache.load
chrollo-lucifer-12 fedd60d
fix: keep cache uninitialized when WAL replay fails
chrollo-lucifer-12 de18445
fix: add docstring for close and _ensure_open
chrollo-lucifer-12 e583d10
fix the format checks and benchmark test
chrollo-lucifer-12 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| """Benchmarks for write-ahead log persistence.""" | ||
|
|
||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| from seriousdb.wal import SetEntry, WriteAheadLog | ||
|
|
||
| from ._support import WARMUP_ROUNDS | ||
|
|
||
|
|
||
| @pytest.mark.benchmark(group="wal") | ||
| def test_wal_append( | ||
| benchmark, | ||
| database_file: Path, | ||
| measured_rounds: int, | ||
| ) -> None: | ||
| """Measure appending one entry to the WAL, including fsync.""" | ||
|
|
||
| entry = SetEntry(key="key", value="value") | ||
|
|
||
| def prepare_wal(): | ||
| database_file.unlink(missing_ok=True) | ||
| wal = WriteAheadLog(str(database_file)) | ||
| return (wal,), {} | ||
|
|
||
| def append_entry(wal: WriteAheadLog) -> None: | ||
| wal.append(entry) | ||
|
|
||
| def close_wal(wal: WriteAheadLog) -> None: | ||
| wal.close() | ||
|
mattsva marked this conversation as resolved.
|
||
|
|
||
| benchmark.extra_info.update( | ||
| operation="WAL append", | ||
| persistence="write + flush + fsync", | ||
| key_bytes=len(entry.key.encode()), | ||
| value_bytes=len(entry.value.encode()), | ||
| ) | ||
|
|
||
| benchmark.pedantic( | ||
| append_entry, | ||
| setup=prepare_wal, | ||
| teardown=close_wal, | ||
| rounds=50, | ||
| warmup_rounds=WARMUP_ROUNDS, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.benchmark(group="wal") | ||
| def test_wal_append_many( | ||
| benchmark, | ||
| database_file: Path, | ||
| ) -> None: | ||
| """Measure repeated appends using one open WAL.""" | ||
|
|
||
| entry = SetEntry(key="key", value="value") | ||
|
|
||
| def prepare_wal(): | ||
| database_file.unlink(missing_ok=True) | ||
| wal = WriteAheadLog(str(database_file)) | ||
| return (wal,), {} | ||
|
|
||
| def append_many(wal: WriteAheadLog) -> None: | ||
| for _ in range(1000): | ||
| wal.append(entry) | ||
|
|
||
| def close_wal(wal: WriteAheadLog) -> None: | ||
| wal.close() | ||
|
|
||
| benchmark.extra_info.update( | ||
| operation="1000 WAL appends", | ||
| persistence="write + flush + fsync", | ||
| entries=1000, | ||
| key_bytes=len(entry.key.encode()), | ||
| value_bytes=len(entry.value.encode()), | ||
| ) | ||
|
|
||
| benchmark.pedantic( | ||
| append_many, | ||
| setup=prepare_wal, | ||
| teardown=close_wal, | ||
| rounds=10, | ||
| warmup_rounds=WARMUP_ROUNDS, | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -320,15 +320,23 @@ def load(self, filename: str) -> None: | |
| db = _write_default(filename) | ||
|
|
||
| wal = WriteAheadLog(f"{filename}.wal") | ||
| replayed = wal.replay() | ||
|
|
||
| for entry in replayed: | ||
| entry.apply(db) | ||
|
|
||
| try: | ||
| replayed = wal.replay() | ||
| for entry in replayed: | ||
| entry.apply(db) | ||
| except Exception: | ||
| try: | ||
| wal.close() | ||
| except OSError: | ||
| pass | ||
| raise | ||
| old_wal = self.wal | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Closing the old WAL after the swap is fine, and safer than closing it first, since a failed load keeps the old state. Please add a test that the old WAL's handle is closed after a second load(). This is optinal, but I guess/hope it helps |
||
| self.db = db | ||
| self.filename = filename | ||
| self.wal = wal | ||
| self._writes_since_compact = len(replayed) | ||
| if old_wal is not None: | ||
| old_wal.close() | ||
|
|
||
| def flush(self) -> None: | ||
| """No-op, kept for backward compatibility. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This measures only the first append on a fresh file, so it mostly shows fsync cost. Since the change is about reusing the handle, please add a second benchmark that appends many entries to one open WAL, and post before/after numbers (command, OS, Python version, workload).
Make it more telling, please.