TASK
Implementation
Transactional KV: begin, read, write, commit/abort. Use 2PC for cross-shard transactions.
Sample Test Cases
Begin transactionTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"txn_begin","msg_id":2}}
Expected Output
{"src":"n1","dest":"c0","body":{"type":"init_ok","in_reply_to":1,"msg_id":0}}
{"src":"n1","dest":"c1","body":{"type":"txn_begin_ok","in_reply_to":2,"msg_id":1,"tx_id":"tx1"}}
Hints
Hint 1▾
Multi-key transactions
Hint 2▾
Use 2PC for cross-shard
Hint 3▾
Read-your-writes
OVERVIEW
Theoretical Hub
ACID
Atomic, Consistent, Isolated, Durable. Distributed transactions harder due to network partitions.
Key Concepts
transactionsACIDisolation
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
23
24
25
#!/usr/bin/env python3
import uuid
class TransactionalKV:
def __init__(self):
self.data = {}
self.transactions = {}
def begin(self):
tx_id = str(uuid.uuid4())
self.transactions[tx_id] = {'reads': {}, 'writes': {}}
return tx_id
def read(self, tx_id, key):
tx = self.transactions[tx_id]
if key in tx['writes']: return tx['writes'][key]
return self.data.get(key)
def write(self, tx_id, key, value):
self.transactions[tx_id]['writes'][key] = value
def commit(self, tx_id):
# TODO: Apply writes
pass