TASK
Implementation
Implement multi-key transactions. A transaction is a batch of operations committed as a single log entry for atomicity.
Request: {"type": "txn_execute", "msg_id": 1, "operations": [
{"op": "put", "key": "balance_a", "value": "900"},
{"op": "put", "key": "balance_b", "value": "1100"}
]}
Response: {"type": "txn_execute_ok", "in_reply_to": 1, "committed": true, "log_index": 5, "ops_applied": 2}
Request: {"type": "txn_execute", "msg_id": 2, "operations": [
{"op": "get", "key": "balance_a"},
{"op": "get", "key": "balance_b"}
]}
Response: {"type": "txn_execute_ok", "in_reply_to": 2, "committed": true, "results": [
{"op": "get", "key": "balance_a", "value": "900"},
{"op": "get", "key": "balance_b", "value": "1100"}
]}Sample Test Cases
Atomic multi-key writeTimeout: 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_execute","msg_id":2,"operations":[{"op":"put","key":"a","value":"1"},{"op":"put","key":"b","value":"2"}]}}
{"src":"c1","dest":"n1","body":{"type":"txn_execute","msg_id":3,"operations":[{"op":"get","key":"a"},{"op":"get","key":"b"}]}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
Hints
Hint 1▾
A transaction is a batch of Get/Put/Delete operations committed as a single log entry
Hint 2▾
All operations in the batch succeed or fail together (atomicity)
Hint 3▾
The batch is serialized as a single command in the Raft log
Hint 4▾
The state machine applies all operations in the batch atomically
Hint 5▾
If any operation fails validation, the entire batch is rejected
OVERVIEW
Theoretical Hub
Concept overview coming soon
Key Concepts
multi-key transactionatomic batchlog entryall-or-nothing
main.py
python
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/env python3
import sys
import json
def main():
# Your implementation here
for line in sys.stdin:
msg = json.loads(line)
print(json.dumps(msg), flush=True)
if __name__ == "__main__":
main()