TASK
Implementation
Handle coordinator failures: log PREPARE before sending, log COMMIT/ABORT decision, recover from log.
Sample Test Cases
Log transaction state before prepareTimeout: 5000ms
Input
{
"src": "c0",
"dest": "n1",
"body": {
"type": "init",
"msg_id": 1,
"node_id": "n1",
"node_ids": [
"n1"
]
}
}Expected Output
{"src":"n1","dest":"c0","body":{"type":"init_ok","in_reply_to":1,"msg_id":0}}Hints
Hint 1▾
Log before sending messages
Hint 2▾
Recovery reads log state
Hint 3▾
Participants query coordinator for decision
OVERVIEW
Theoretical Hub
Write-Ahead Logging
Log decision before sending. On recovery, read log to resume. Participants in PREPARED are blocked until decision known.
Key Concepts
failure recoveryblockingwrite-ahead log
main.py
python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env python3
import json, os
class DurableLog:
def __init__(self, path):
self.path = path
self.log = {}
def write(self, tx_id, state):
self.log[tx_id] = state
with open(self.path, 'w') as f:
json.dump(self.log, f)
class RecoverableCoordinator:
def __init__(self, participants, log_path):
self.participants = participants
self.log = DurableLog(log_path)
def recover(self):
# TODO: Resume in-progress transactions
pass