TASK
Implementation
When a node leaves the ring (graceful shutdown or crash), its key range must be taken over by its successor. The two scenarios require different handling.
Graceful shutdown:
- Node announces it is leaving
- Node transfers all its keys to its clockwise successor(s)
- Ring topology is updated
- No data loss, minimal disruption
Crash recovery:
- Other nodes detect the failure (missed heartbeats)
- The successor takes over the key range
- Data is recovered from replica copies
- New replicas are created to restore the replication factor
Request: {"type": "ring_remove_node", "msg_id": 1, "node": "n2", "mode": "graceful"}
Response: {"type": "ring_remove_node_ok", "in_reply_to": 1, "keys_migrated": 333, "target_nodes": ["n1", "n3"], "mode": "graceful"}Sample Test Cases
Graceful removal migrates keysTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1","n2","n3"]}}
{"src":"c1","dest":"n1","body":{"type":"ring_remove_node","msg_id":2,"node":"n2","mode":"graceful"}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
Crash recovery takes over key rangeTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1","n2","n3"]}}
{"src":"c1","dest":"n1","body":{"type":"ring_remove_node","msg_id":2,"node":"n3","mode":"crash"}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
Hints
Hint 1▾
On graceful shutdown: node transfers its keys to its clockwise successor before leaving
Hint 2▾
On crash: the successor detects the failure and takes over the key range
Hint 3▾
Graceful is faster (pre-transfer), crash requires recovery from replicas
Hint 4▾
With virtual nodes, keys from the removed vnodes distribute to multiple successors
Hint 5▾
Replica copies ensure no data loss even on crash
OVERVIEW
Theoretical Hub
Concept overview coming soon
Key Concepts
node removalgraceful shutdowncrash recoverykey takeoversuccessor promotion
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()