TASK
Implementation
3PC adds PRE-COMMIT phase. If coordinator fails after PRE-COMMIT, participants can commit safely.
Sample Test Cases
Phase 1: PrepareTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1","n2","n3","n4"]}}
{"src":"c1","dest":"n1","body":{"type":"3pc_prepare","msg_id":2,"tx_id":"tx1","participants":["n2","n3","n4"],"operations":[{"key":"x","value":10}]}}
Expected Output
{"src":"n1","dest":"c0","body":{"type":"init_ok","in_reply_to":1,"msg_id":0}}
{"src":"n1","dest":"c1","body":{"type":"3pc_prepare_ok","in_reply_to":2,"msg_id":1,"tx_id":"tx1","votes":{"n2":"yes","n3":"yes","n4":"yes"}}}
Hints
Hint 1▾
Add PRE-COMMIT phase
Hint 2▾
PRE-COMMIT indicates intent to commit
Hint 3▾
Participants can proceed without coordinator
OVERVIEW
Theoretical Hub
Three-Phase Commit
3PC reduces blocking by adding PRE-COMMIT. Participants in PRE-COMMIT know decision was commit if coordinator fails.
Key Concepts
3PCnon-blockingpre-commit
main.py
python
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/env python3
class ThreePhaseCoordinator:
def __init__(self, participants):
self.participants = participants
def run_transaction(self, tx_id, ops):
# Phase 1: Prepare
votes = [p.prepare(tx_id, ops.get(p.id, [])) for p in self.
participants]
# Phase 2: Pre-Commit (if all YES)
# Phase 3: Commit
# TODO: Implement 3PC
pass