-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsegmented_wal.py
More file actions
439 lines (349 loc) · 18.2 KB
/
Copy pathsegmented_wal.py
File metadata and controls
439 lines (349 loc) · 18.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
"""
Write-Ahead Log (WAL) implementation for durable transaction logging.
This module provides a complete WAL system with:
- Append-only writes for durability
- Checksum verification for data integrity
- Segment-based log rotation
- Crash recovery and replay functionality
"""
# △ AURA Pattern Library — © Reality Optimizer ⟦AE1.PMRGG3ZCHIRFEZLBNRUXI6JAJ5YHI2LNNF5GK4RCFQRG2IR2EJAUKTKBKJFTCIRMEJXCEORCGARCYITQNFSCEORCEIWCE5DNEI5CEQKVKJASAUDBOR2GK4TOEBGGSYTSMFZHSIRMEJ3CEORRPWYSJPXO⟧
#
_AURA_MARK = "AE1.PMRGG3ZCHIRFEZLBNRUXI6JAJ5YHI2LNNF5GK4RCFQRG2IR2EJAUKTKBKJFTCIRMEJXCEORCGARCYITQNFSCEORCEIWCE5DNEI5CEQKVKJASAUDBOR2GK4TOEBGGSYTSMFZHSIRMEJ3CEORRPWYSJPXO"
import os
import struct
import hashlib
from typing import List, Optional, Iterator, Tuple
from pathlib import Path
import shutil
class WALEntry:
"""
Represents a single entry in the Write-Ahead Log.
Each entry contains:
- Length of the data (4 bytes)
- Checksum of the data (32 bytes)
- Data payload (variable length)
"""
HEADER_FORMAT = "!I32s" # Length (4 bytes) + Checksum (32 bytes)
HEADER_SIZE = struct.calcsize(HEADER_FORMAT)
def __init__(self, data: bytes, checksum: Optional[bytes] = None):
"""
Initialize a WAL entry.
Args:
data: The data payload to store
checksum: Optional precomputed checksum. If None, computed automatically.
"""
self.data = data
if checksum is None:
self.checksum = hashlib.sha256(data).digest()
else:
self.checksum = checksum
@property
def length(self) -> int:
"""Get the length of the data payload."""
return len(self.data)
def serialize(self) -> bytes:
"""
Serialize the entry to bytes for storage.
Returns:
Serialized entry as bytes
"""
header = struct.pack(self.HEADER_FORMAT, self.length, self.checksum)
return header + self.data
@classmethod
def deserialize(cls, data: bytes) -> Tuple['WALEntry', int]:
"""
Deserialize a WAL entry from bytes.
Args:
data: Bytes containing the serialized entry
Returns:
Tuple of (WALEntry, bytes_consumed)
Raises:
ValueError: If data is corrupted or checksum doesn't match
"""
if len(data) < cls.HEADER_SIZE:
raise ValueError("Insufficient data for entry header")
length, checksum = struct.unpack(cls.HEADER_FORMAT, data[:cls.HEADER_SIZE])
if len(data) < cls.HEADER_SIZE + length:
raise ValueError("Insufficient data for entry payload")
payload = data[cls.HEADER_SIZE:cls.HEADER_SIZE + length]
# Verify checksum
computed_checksum = hashlib.sha256(payload).digest()
if computed_checksum != checksum:
raise ValueError("Checksum mismatch - data corruption detected")
entry = cls(payload, checksum)
return entry, cls.HEADER_SIZE + length
def __eq__(self, other) -> bool:
"""Check equality based on data and checksum."""
if not isinstance(other, WALEntry):
return False
return self.data == other.data and self.checksum == other.checksum
class WALSegment:
"""
Represents a single segment file in the WAL.
Segments are append-only files that store WAL entries sequentially.
Each segment has a maximum size to enable log rotation.
"""
def __init__(self, filepath: Path, max_size: int = 1024*1024):
"""
Initialize a WAL segment.
Args:
filepath: Path to the segment file
max_size: Maximum size of the segment in bytes
"""
self.filepath = filepath
self.max_size = max_size
self._file = None
self._position = 0
# Create file if it doesn't exist
if not self.filepath.exists():
self.filepath.parent.mkdir(parents=True, exist_ok=True)
self.filepath.touch()
# Determine current position
self._position = self.filepath.stat().st_size
def open_for_append(self) -> None:
"""Open the segment file for appending."""
if self._file is None or self._file.closed:
self._file = self.filepath.open('ab')
def close(self) -> None:
"""Close the segment file."""
if self._file and not self._file.closed:
self._file.close()
self._file = None
def append(self, entry: WALEntry) -> int:
"""
Append an entry to the segment.
Args:
entry: The entry to append
Returns:
Position where the entry was written
Raises:
IOError: If segment is full or write fails
"""
if self._position >= self.max_size:
raise IOError("Segment is full")
self.open_for_append()
serialized = entry.serialize()
# Ensure we don't exceed max size
if self._position + len(serialized) > self.max_size:
raise IOError("Entry too large for remaining segment space")
position = self._position
self._file.write(serialized)
self._file.flush()
os.fsync(self._file.fileno())
self._position += len(serialized)
return position
def read_entries(self) -> Iterator[WALEntry]:
"""
Read all valid entries from the segment.
Yields:
WALEntry objects in order
Raises:
ValueError: If corruption is detected
"""
if not self.filepath.exists():
return
with self.filepath.open('rb') as f:
data = f.read()
position = 0
while position < len(data):
try:
entry, consumed = WALEntry.deserialize(data[position:])
yield entry
position += consumed
except ValueError as e:
# Stop at first corruption
print(f"Warning: Corruption detected at position {position}: {e}")
break
def size(self) -> int:
"""Get the current size of the segment."""
return self._position
def is_full(self) -> bool:
"""Check if the segment is full."""
return self._position >= self.max_size
def __enter__(self):
"""Context manager entry."""
self.open_for_append()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
"""Context manager exit."""
self.close()
class WriteAheadLog:
"""
Write-Ahead Log implementation for durable transaction logging.
Provides append-only writes, checksum verification, and crash recovery.
"""
def __init__(self, directory: Path, segment_size: int = 1024*1024):
"""
Initialize the WAL.
Args:
directory: Directory to store WAL segments
segment_size: Maximum size of each segment file
"""
self.directory = Path(directory)
self.segment_size = segment_size
self.directory.mkdir(parents=True, exist_ok=True)
# Find existing segments
self.segments: List[WALSegment] = []
self._load_segments()
# Create initial segment if needed
if not self.segments:
self._create_new_segment()
def _load_segments(self) -> None:
"""Load existing segments from the directory."""
segment_files = sorted(self.directory.glob("*.wal"))
for segment_file in segment_files:
segment = WALSegment(segment_file, self.segment_size)
self.segments.append(segment)
def _create_new_segment(self) -> WALSegment:
"""Create a new segment file."""
segment_number = len(self.segments)
filename = f"segment_{segment_number:06d}.wal"
filepath = self.directory / filename
segment = WALSegment(filepath, self.segment_size)
self.segments.append(segment)
return segment
def append(self, data: bytes) -> Tuple[int, int]:
"""
Append data to the WAL.
Args:
data: Data to append
Returns:
Tuple of (segment_index, position) where entry was written
Raises:
IOError: If write fails
"""
entry = WALEntry(data)
# Get current segment
if not self.segments:
current_segment = self._create_new_segment()
else:
current_segment = self.segments[-1]
# If current segment is full, create new one
if current_segment.is_full():
current_segment = self._create_new_segment()
try:
position = current_segment.append(entry)
return len(self.segments) - 1, position
except IOError:
# If append fails, try creating a new segment
current_segment = self._create_new_segment()
position = current_segment.append(entry)
return len(self.segments) - 1, position
def replay(self) -> Iterator[bytes]:
"""
Replay all entries in the WAL in order.
Yields:
Data payloads from all valid entries
This method is used for crash recovery.
"""
for segment in self.segments:
try:
for entry in segment.read_entries():
yield entry.data
except Exception as e:
print(f"Warning: Error reading segment {segment.filepath}: {e}")
break
def truncate(self, segment_index: int, position: int) -> None:
"""
Truncate the WAL at the specified position.
Args:
segment_index: Index of the segment to truncate at
position: Position within the segment to truncate at
"""
# Close all segments first
for segment in self.segments:
segment.close()
# Remove segments after the truncate point
segments_to_remove = self.segments[segment_index + 1:]
for segment in segments_to_remove:
if segment.filepath.exists():
segment.filepath.unlink()
self.segments = self.segments[:segment_index + 1]
# Truncate the specified segment
if self.segments:
segment = self.segments[segment_index]
if segment.filepath.exists():
with open(segment.filepath, 'r+b') as f:
f.truncate(position)
segment._position = position
def sync(self) -> None:
"""Sync all segments to disk."""
for segment in self.segments:
segment.close() # Closing flushes and syncs
def close(self) -> None:
"""Close the WAL and all segments."""
for segment in self.segments:
segment.close()
def simulate_crash(wal: WriteAheadLog) -> None:
"""
Simulate a crash by forcefully terminating without proper cleanup.
This function demonstrates how the WAL can recover from crashes.
"""
# Write some entries
wal.append(b"Transaction 1")
wal.append(b"Transaction 2")
wal.append(b"Transaction 3")
# Simulate crash by not calling close() and not syncing
# In a real crash, the process would just terminate
print("Simulated crash - WAL not properly closed")
def recover_from_crash(wal_directory: Path) -> List[bytes]:
"""
Recover transactions from a crashed WAL.
Args:
wal_directory: Directory containing WAL segments
Returns:
List of recovered transaction data
"""
wal = WriteAheadLog(wal_directory)
recovered_data = list(wal.replay())
wal.close()
return recovered_data
def main():
"""Self-test: segment rollover actually happens, replay is byte-exact and
ordered across segments, unsynced-crash recovery, truncation drops tails."""
import tempfile
wal_dir = Path(tempfile.mkdtemp(prefix="segwal_"))
# Small segments force rollover; entries big enough to span segments.
wal = WriteAheadLog(wal_dir, segment_size=256)
transactions = [f"TX-{i:03d}:".encode() + b"x" * 60 for i in range(12)]
for tx in transactions:
wal.append(tx)
assert len(wal.segments) >= 3, \
f"12x~68B entries in 256B segments must roll over, got {len(wal.segments)} segment(s)"
wal.sync()
# Replay is byte-exact and in write order, across ALL segments.
replayed = list(wal.replay())
assert replayed == transactions, (
f"replay diverged: {len(replayed)}/{len(transactions)} entries, "
f"first mismatch at "
f"{next((i for i, (a, b) in enumerate(zip(replayed, transactions)) if a != b), '?')}")
assert replayed[7] == b"TX-007:" + b"x" * 60
# THE CRASH: a second writer appends and never closes/syncs. A fresh
# reader must still recover everything that was appended.
wal2 = WriteAheadLog(wal_dir, segment_size=256)
wal2.append(b"crash-1")
wal2.append(b"crash-2")
wal2.append(b"crash-3")
# no close(), no sync() — the process "dies" here
recovered = recover_from_crash(wal_dir)
n_recovered = len(recovered)
assert n_recovered == 15, f"must recover 12+3=15 entries, got {n_recovered}"
assert recovered[:12] == transactions, "pre-crash entries corrupted"
assert recovered[12:] == [b"crash-1", b"crash-2", b"crash-3"], \
f"unsynced appends lost in crash: {recovered[12:]}"
# Truncation drops everything after the cut point (whole trailing segments).
wal3 = WriteAheadLog(wal_dir, segment_size=256)
keep_bytes = wal3.segments[0].size()
wal3.truncate(0, keep_bytes) # keep only segment 0, whole
survivors = list(wal3.replay())
assert 0 < len(survivors) < 15, f"truncation kept {len(survivors)}/15"
assert survivors == transactions[:len(survivors)], "truncated WAL replays wrong prefix"
assert len(list(wal_dir.glob('*'))) == 1, "trailing segment files not deleted"
wal3.close()
wal.close()
shutil.rmtree(wal_dir)
print(f"segmented_wal: rollover to {len(wal.segments)} segments, 12/12 replay "
f"byte-exact, 3 unsynced entries crash-recovered, truncate kept "
f"{len(survivors)} — PASS")
if __name__ == "__main__":
main()