askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
test_append_cache_torn_tail.py · 4.6 KB · 114 lines
| 1 | """A records-cache entry parsed while a torn tail was on disk must not |
| 2 | become the place the next read starts folding from. |
| 3 | |
| 4 | A full parse of an append-mode file drops a torn tail (a final row with no |
| 5 | newline: a writer died mid-append) from the records, but the cache entry it |
| 6 | stored said it covered the whole file, torn bytes included |
| 7 | (_cache_entry stored covered_bytes = signature[1]). The next writer -- in |
| 8 | another process -- first repairs the tail (_repair_torn_tail truncates the |
| 9 | fragment in place, same inode) and then appends its row. The file grew |
| 10 | past the stale covered_bytes on the same inode, so this process's next read |
| 11 | took the incremental branch (_try_incremental_cache_entry) and parsed from |
| 12 | there: partway into the new row, not at its start. The rest of that row, |
| 13 | read as a row, shifted every column left -- the new record was missing, |
| 14 | and a record whose id was one of its VALUES appeared instead. |
| 15 | |
| 16 | Every "other process" here is a real one (a subprocess, a fresh |
| 17 | interpreter), as in tests/test_append_cache_foreign_writer.py: the bug is |
| 18 | about what one process's memory believes after another process wrote. |
| 19 | """ |
| 20 | |
| 21 | import json |
| 22 | import subprocess |
| 23 | import sys |
| 24 | from pathlib import Path |
| 25 | |
| 26 | import pytest |
| 27 | |
| 28 | import object_records |
| 29 | |
| 30 | REPO_ROOT = str(Path(__file__).resolve().parents[1]) |
| 31 | |
| 32 | _OTHER = """\ |
| 33 | import sys |
| 34 | sys.path.insert(0, sys.argv[1]) |
| 35 | import object_records |
| 36 | data_dir, record_id, value, note = sys.argv[2:6] |
| 37 | object_records.create_collection_record( |
| 38 | "events", {"id": record_id, "value": value, "note": note}, base_dir=data_dir, roots=[]) |
| 39 | """ |
| 40 | |
| 41 | |
| 42 | @pytest.fixture |
| 43 | def data_dir(tmp_path): |
| 44 | data = tmp_path / "data" |
| 45 | (data / "schemas").mkdir(parents=True) |
| 46 | (data / "schemas" / "events.json").write_text(json.dumps({ |
| 47 | "name": "events", "storage": "append", |
| 48 | "fields": [{"name": "id"}, {"name": "value", "type": "text"}, |
| 49 | {"name": "note", "type": "text"}]})) |
| 50 | (tmp_path / "_other.py").write_text(_OTHER) |
| 51 | return data |
| 52 | |
| 53 | |
| 54 | def elsewhere(data_dir, record_id, value, note): |
| 55 | done = subprocess.run( |
| 56 | [sys.executable, str(data_dir.parent / "_other.py"), REPO_ROOT, str(data_dir), |
| 57 | record_id, value, note], |
| 58 | capture_output=True, text=True, timeout=60) |
| 59 | assert done.returncode == 0, done.stderr |
| 60 | |
| 61 | |
| 62 | def records_file(data_dir): |
| 63 | return object_records.collection_records_file("events", base_dir=data_dir) |
| 64 | |
| 65 | |
| 66 | def stored(data_dir): |
| 67 | return {row["id"]: (row["value"], row["note"]) |
| 68 | for row in object_records.read_collection_records("events", base_dir=data_dir, roots=[])} |
| 69 | |
| 70 | |
| 71 | def on_disk(data_dir): |
| 72 | """What a process with no cache at all reads: the file is the truth.""" |
| 73 | object_records._RECORDS_CACHE.clear() |
| 74 | object_records._OIDX_CACHE.clear() |
| 75 | return stored(data_dir) |
| 76 | |
| 77 | |
| 78 | @pytest.mark.parametrize("fragment", [ |
| 79 | b"x", # torn one byte in |
| 80 | b"\tdead\tpart", # torn inside the second field |
| 81 | b'\tq\t"an open', # torn inside an open quote |
| 82 | ], ids=["one-byte", "mid-value", "open-quote"]) |
| 83 | def test_a_row_another_process_appends_after_repairing_a_torn_tail_reads_whole( |
| 84 | data_dir, fragment): |
| 85 | object_records.create_collection_record( |
| 86 | "events", {"id": "e1", "value": "ours", "note": ""}, base_dir=data_dir, roots=[]) |
| 87 | with records_file(data_dir).open("ab") as handle: # a writer died mid-append |
| 88 | handle.write(fragment) |
| 89 | # This process reads the torn file -- e1 only -- and caches what it read. |
| 90 | assert stored(data_dir) == {"e1": ("ours", "")} |
| 91 | inode = records_file(data_dir).stat().st_ino |
| 92 | |
| 93 | elsewhere(data_dir, "e2", "theirs", "some note text") # repairs, then appends |
| 94 | |
| 95 | assert records_file(data_dir).stat().st_ino == inode # in place: the incremental case |
| 96 | expected = {"e1": ("ours", ""), "e2": ("theirs", "some note text")} |
| 97 | assert stored(data_dir) == expected # the reader holding the entry |
| 98 | assert object_records.get_collection_record( |
| 99 | "events", "e2", base_dir=data_dir, roots=[])["note"] == "some note text" |
| 100 | assert on_disk(data_dir) == expected |
| 101 | |
| 102 | |
| 103 | def test_an_untorn_file_still_folds_only_the_new_rows(data_dir): |
| 104 | """The fix must not cost the path it guards: with no torn tail, another |
| 105 | process's append is still folded incrementally, not by a full parse.""" |
| 106 | object_records.create_collection_record( |
| 107 | "events", {"id": "e1", "value": "ours", "note": ""}, base_dir=data_dir, roots=[]) |
| 108 | assert stored(data_dir) == {"e1": ("ours", "")} |
| 109 | folds = object_records._INCREMENTAL_CACHE_FOLDS |
| 110 | |
| 111 | elsewhere(data_dir, "e2", "theirs", "n") |
| 112 | |
| 113 | assert stored(data_dir) == {"e1": ("ours", ""), "e2": ("theirs", "n")} |
| 114 | assert object_records._INCREMENTAL_CACHE_FOLDS == folds + 1 |