TASK
Implementation
Real networks drop messages. Netflix pioneered chaos engineering — deliberately injecting failures to test resilience. Your task is to add a "chaos mode" to your node.
When chaos mode is enabled, the node randomly drops a configurable percentage of outgoing messages (does not send them to stdout). This simulates network packet loss.
Implement these message types:
chaos_on— Enable chaos mode with a given drop rate:
Request: {"type": "chaos_on", "msg_id": 1, "drop_rate": 0.1}
Response: {"type": "chaos_on_ok", "in_reply_to": 1, "drop_rate": 0.1}chaos_off— Disable chaos mode:
Request: {"type": "chaos_off", "msg_id": 2}
Response: {"type": "chaos_off_ok", "in_reply_to": 2}chaos_stats— Report chaos statistics:
Request: {"type": "chaos_stats", "msg_id": 3}
Response: {"type": "chaos_stats_ok", "in_reply_to": 3, "enabled": true, "drop_rate": 0.1, "total_sent": 50, "total_dropped": 5}Use a fixed random seed (42) for reproducibility in tests. The drop decision uses random.random() < drop_rate.
Chaos mode should NOT drop control messages (init_ok, chaos_on_ok, chaos_off_ok, chaos_stats_ok) — only application messages like echo_ok.
Sample Test Cases
Init and echo work without chaosTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"echo","msg_id":2,"echo":"safe"}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
{"src": "n1", "dest": "c1", "body": {"type": "echo_ok", "echo": "safe", "in_reply_to": 2, "msg_id": 1}}
Chaos on responds with drop_rateTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"chaos_on","msg_id":2,"drop_rate":0.5}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
{"src": "n1", "dest": "c1", "body": {"type": "chaos_on_ok", "drop_rate": 0.5, "in_reply_to": 2, "msg_id": 1}}
Hints
Hint 1▾
Use a random number generator to decide whether to drop each outgoing message
Hint 2▾
The drop rate should be configurable (default 10%)
Hint 3▾
Log dropped messages to stderr so you can observe chaos effects
Hint 4▾
Track how many messages were dropped vs sent
Hint 5▾
Chaos mode should be toggleable via a message
OVERVIEW
Theoretical Hub
Concept overview coming soon
Key Concepts
chaos engineeringfault injectionresilience testingnetwork partitions
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python3
import sys
import json
import random
class Node:
def __init__(self):
self.node_id = None
self.node_ids = []
self.next_msg_id = 0
self.chaos_enabled = False
self.drop_rate = 0.0
self.total_sent = 0
self.total_dropped = 0
random.seed(42)
def should_drop(self, body_type):
# TODO: Decide whether to drop this message
# Never drop control messages (init_ok, chaos_*_ok)
# Return True if the message should be dropped
pass
def send(self, dest, body):
body["msg_id"] = self.next_msg_id
self.next_msg_id += 1
message = {"src": self.node_id, "dest": dest, "body": body}
# TODO: Check chaos mode and maybe drop the message
print(json.dumps(message), flush=True)
def reply(self, request, body):
body["in_reply_to"] = request["body"]["msg_id"]
self.send(request["src"], body)
def main():
node = Node()
for line in sys.stdin:
line = line.strip()
if not line: