Repository navigation
Expand file tree
/
Copy pathmini.py
More file actions
816 lines (680 loc) · 30.4 KB
/
Copy pathmini.py
File metadata and controls
816 lines (680 loc) · 30.4 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
69
8000
6
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
#!/usr/bin/env python3
"""
MiniKafka - A lightweight, production-ready Kafka-like message broker
Author: Your Name
License: MIT
Version: 1.0.0
A minimal but robust implementation of a Kafka-like message streaming platform
with persistence, clustering support, and high availability features.
Tests
echo '{"action":"create_topic","topic":"my_test_topic"}' | nc localhost 9092
echo '{"action":"produce","topic":"my_test_topic","value":"hello"}' | nc localhost 9092
echo '{"action":"consume","topic":"my_test_topic"}' | nc localhost 9092
Demo
demo.html
run it via
Example config
minikafka.yaml
broker_id: 1
host: 0.0.0.0
port: 9092
data_dir: ./minikafka_data
"""
import asyncio
import json
import os
import sys
import signal
import time
import uuid
import hashlib
import argparse
from collections import defaultdict, deque
from typing import Dict, List, Callable, Optional, Tuple, Set, Any
from dataclasses import dataclass, asdict
from pathlib import Path
import logging
from concurrent.futures import ThreadPoolExecutor
import threading
import weakref
from contextlib import asynccontextmanager
import aiofiles
import yaml
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler(),
logging.FileHandler('minikafka.log')
]
)
logger = logging.getLogger("MiniKafka")
@dataclass
class Message:
"""Represents a message in the broker"""
id: str
topic: str
partition: int
offset: int
timestamp: float
key: Optional[str]
value: bytes
headers: Dict[str, str]
def to_dict(self) -> Dict[str, Any]:
return {
**asdict(self),
'value': self.value.decode('utf-8', errors='replace'),
'timestamp': int(self.timestamp * 1000) # Convert to milliseconds
}
@dataclass
clas
8000
s TopicConfig:
"""Topic configuration"""
name: str
partitions: int = 1
replication_factor: int = 1
retention_ms: int = 604800000 # 7 days
max_message_bytes: int = 1000000 # 1MB
cleanup_policy: str = "delete" # delete or compact
@dataclass
class BrokerConfig:
"""Broker configuration"""
broker_id: int = 0
host: str = "127.0.0.1"
port: int = 9092
data_dir: str = "minikafka_data"
log_retention_hours: int = 168 # 7 days
log_segment_bytes: int = 1073741824 # 1GB
max_connections: int = 1000
socket_send_buffer_bytes: int = 102400
socket_receive_buffer_bytes: int = 102400
num_io_threads: int = 8
num_network_threads: int = 3
enable_metrics: bool = True
enable_auto_create_topics: bool = True
default_replication_factor: int = 1
class MetricsCollector:
"""Simple metrics collection"""
def __init__(self):
self._metrics: Dict[str, int] = defaultdict(int)
self._timings: Dict[str, List[float]] = defaultdict(list)
self._lock = threading.Lock()
def increment(self, metric: str, value: int = 1):
with self._lock:
self._metrics[metric] += value
def timing(self, metric: str, value: float):
with self._lock:
self._timings[metric].append(value)
# Keep only last 1000 measurements
if len(self._timings[metric]) > 1000:
self._timings[metric].pop(0)
def get_metrics(self) -> Dict[str, Any]:
with self._lock:
metrics = dict(self._metrics)
timings = {}
for metric, values in self._timings.items():
if values:
timings[f"{metric}_avg"] = sum(values) / len(values)
timings[f"{metric}_min"] = min(values)
timings[f"{metric}_max"] = max(values)
return {**metrics, **timings}
class Partition:
"""Represents a topic partition"""
def __init__(self, topic: str, partition_id: int, data_dir: str):
self.topic = topic
self.partition_id = partition_id
self.data_dir = Path(data_dir) / topic / str(partition_id)
self.data_dir.mkdir(parents=True, exist_ok=True)
self._messages: List[Message] = []
self._offset_counter = 0
self._lock = asyncio.Lock()
self._log_file = self.data_dir / "messages.log"
# Load existing messages
asyncio.create_task(self._load_messages())
async def _load_messages(self):
"""Load messages from disk"""
if not self._log_file.exists():
return
try:
async with aiofiles.open(self._log_file, 'r', encoding='utf-8') as f:
async for line in f:
if line.strip():
try:
data = json.loads(line.strip())
message = Message(
id=data['id'],
topic=data['topic'],
partition=data['partition'],
offset=data['offset'],
timestamp=data['timestamp'],
key=data.get('key'),
value=data['value'].encode('utf-8'),
headers=data.get('headers', {})
)
self._messages.append(message)
self._offset_counter = max(self._offset_counter, message.offset + 1)
except (json.JSONDecodeError, KeyError) as e:
logger.warning(f"Skipping corrupted message: {e}")
logger.info(f"Loaded {len(self._messages)} messages for {self.topic}:{self.partition_id}")
except Exception as e:
logger.error(f"Failed to load messages: {e}")
async def append_message(self, key: Optional[str], value: bytes, headers: Dict[str, str]) -> Message:
"""Append a message to this partition"""
async with self._lock:
message = Message(
id=str(uuid.uuid4()),
topic=self.topic,
partition=self.partition_id,
offset=self._offset_counter,
timestamp=time.time(),
key=key,
value=value,
headers=headers
)
self._messages.append(message)
self._offset_counter += 1
# Persist to disk
await self._persist_message(message)
return message
async def _persist_message(self, message: Message):
"""Persist message to disk"""
try:
async with aiofiles.open(self._log_file, 'a', encoding='utf-8') as f:
line = json.dumps(message.to_dict()) + '\n'
await f.write(line)
except Exception as e:
logger.error(f"Failed to persist message: {e}")
async def get_messages(self, start_offset: int = 0, max_messages: int = 100) -> List[Message]:
"""Get messages starting from offset"""
async with self._lock:
if start_offset < 0:
start_offset = max(0, len(self._messages) + start_offset)
end_offset = min(start_offset + max_messages, len(self._messages))
return self._messages[start_offset:end_offset]
async def get_latest_offset(self) -> int:
"""Get the latest offset"""
async with self._lock:
return self._offset_counter
async def cleanup_old_messages(self, retention_ms: int):
"""Remove old messages based on retention policy"""
current_time = time.time() * 1000
cutoff_time = current_time - retention_ms
async with self._lock:
# Find messages to keep
messages_to_keep = []
for msg in self._messages:
if msg.timestamp * 1000 > cutoff_time:
messages_to_keep.append(msg)
if len(messages_to_keep) < len(self._messages):
logger.info(f"Cleaned up {len(self._messages) - len(messages_to_keep)} old messages from {self.topic}:{self.partition_id}")
self._messages = messages_to_keep
# Rewrite log file
await self._rewrite_log_file()
async def _rewrite_log_file(self):
"""Rewrite the log file with current messages"""
try:
temp_file = self._log_file.with_suffix('.tmp')
async with aiofiles.open(temp_file, 'w', encoding='utf-8') as f:
for message in self._messages:
line = json.dumps(message.to_dict()) + '\n'
await f.write(line)
# Atomic replacement
temp_file.replace(self._log_file)
except Exception as e:
logger.error(f"Failed to rewrite log file: {e}")
class ConsumerGroup:
"""Manages consumer group state"""
def __init__(self, group_id: str):
self.group_id = group_id
self.consumers: Set[str] = set()
self.partition_assignments: Dict[str, Set[Tuple[str, int]]] = defaultdict(set)
self.offsets: Dict[Tuple[str, int], int] = {} # (topic, partition) -> offset
self._lock = asyncio.Lock()
async def add_consumer(self, consumer_id: str):
async with self._lock:
self.consumers.add(consumer_id)
async def remove_consumer(self, consumer_id: str):
async with self._lock:
self.consumers.discard(consumer_id)
if consumer_id in self.partition_assignments:
del self.partition_assignments[consumer_id]
async def commit_offset(self, topic: str, partition: int, offset: int):
async with self._lock:
self.offsets[(topic, partition)] = offset
async def get_offset(self, topic: str, partition: int) -> int:
async with self._lock:
return self.offsets.get((topic, partition), 0)
class MiniKafkaBroker:
"""Main broker class"""
def __init__(self, config: BrokerConfig):
self.config = config
self.topics: Dict[str, TopicConfig] = {}
self.partitions: Dict[Tuple[str, int], Partition] = {} # (topic, partition) -> Partition
self.consumer_groups: Dict[str, ConsumerGroup] = {}
self.subscribers: Dict[Tuple[str, int], List[Callable]] = defaultdict(list) # (topic, partition) -> callbacks
self.client_connections: Dict[str, asyncio.StreamWriter] = {}
self.metrics = MetricsCollector()
self._shutdown_event = asyncio.Event()
self._cleanup_task: Optional[asyncio.Task] = None
# Ensure data directory exists
Path(self.config.data_dir).mkdir(parents=True, exist_ok=True)
# Load topic configurations
asyncio.create_task(self._load_topics())
async def _load_topics(self):
"""Load topic configurations from disk"""
topics_file = Path(self.config.data_dir) / "topics.yaml"
if topics_file.exists():
try:
async with aiofiles.open(topics_file, 'r') as f:
content = await f.read()
topics_data = yaml.safe_load(content) or {}
for topic_name, topic_data in topics_data.items():
config = TopicConfig(name=topic_name, **topic_data)
await self._create_topic_partitions(config)
logger.info(f"Loaded {len(self.topics)} topics from disk")
except Exception as e:
logger.error(f"Failed to load topics: {e}")
async def _save_topics(self):
"""Save topic configurations to disk"""
topics_file = Path(self.config.data_dir) / "topics.yaml"
try:
topics_data = {}
for topic_name, config in self.topics.items():
topics_data[topic_name] = asdict(config)
del topics_data[topic_name]['name'] # Remove redundant name field
async with aiofiles.open(topics_file, 'w') as f:
await f.write(yaml.dump(topics_data, default_flow_style=False))
except Exception as e:
logger.error(f"Failed to save topics: {e}")
async def create_topic(self, name: str, partitions: int = 1, replication_factor: int = 1) -> bool:
"""Create a new topic"""
if name in self.topics:
return False
config = TopicConfig(
name=name,
partitions=partitions,
replication_factor=replication_factor
)
await self._create_topic_partitions(config)
await self._save_topics()
logger.info(f"Created topic '{name}' with {partitions} partitions")
self.metrics.increment("topics_created")
return True
async def _create_topic_partitions(self, config: TopicConfig):
"""Create partitions for a topic"""
self.topics[config.name] = config
for partition_id in range(config.partitions):
partition = Partition(config.name, partition_id, self.config.data_dir)
self.partitions[(config.name, partition_id)] = partition
def _get_partition_for_key(self, topic: str, key: Optional[str]) -> int:
"""Get partition ID for a message key"""
if topic not in self.topics:
return 0
num_partitions = self.topics[topic].partitions
if key is None:
# Round-robin for messages without keys
return hash(str(time.time())) % num_partitions
else:
# Consistent hashing for keyed messages
return hash(key) % num_partitions
async def produce(self, topic: str, value: bytes, key: Optional[str] = None,
headers: Optional[Dict[str, str]] = None) -> Optional[Message]:
"""Produce a message to a topic"""
start_time = time.time()
# Auto-create topic if enabled
if topic not in self.topics and self.config.enable_auto_create_topics:
await self.create_topic(topic)
if topic not in self.topics:
logger.warning(f"Topic '{topic}' does not exist")
return None
# Check message size
topic_config = self.topics[topic]
if len(value) > topic_config.max_message_bytes:
logger.warning(f"Message too large: {len(value)} > {topic_config.max_message_bytes}")
return None
partition_id = self._get_partition_for_key(topic, key)
partition = self.partitions[(topic, partition_id)]
message = await partition.append_message(key, value, headers or {})
# Notify subscribers
for callback in self.subscribers[(topic, partition_id)]:
try:
await callback(message)
except Exception as e:
logger.error(f"Subscriber callback failed: {e}")
self.metrics.increment("messages_produced")
self.metrics.timing("produce_time", time.time() - start_time)
logger.debug(f"Produced message to {topic}:{partition_id} at offset {message.offset}")
return message
async def consume(self, topic: str, partition: int = 0, offset: int = 0,
max_messages: int = 100) -> List[Message]:
"""Consume messages from a topic partition"""
start_time = time.time()
if topic not in self.topics:
return []
partition_key = (topic, partition)
if partition_key not in self.partitions:
return []
partition_obj = self.partitions[partition_key]
messages = await partition_obj.get_messages(offset, max_messages)
self.metrics.increment("messages_consumed", len(messages))
self.metrics.timing("consume_time", time.time() - start_time)
logger.debug(f"Consumed {len(messages)} messages from {topic}:{partition} at offset {offset}")
return messages
async def subscribe(self, topic: str, partition: int, callback: Callable,
consumer_id: Optional[str] = None):
"""Subscribe to a topic partition"""
# Auto-create topic if enabled
if topic not in self.topics and self.config.enable_auto_create_topics:
await self.create_topic(topic)
if topic not in self.topics:
logger.warning(f"Cannot subscribe to non-existent topic '{topic}'")
return False
self.subscribers[(topic, partition)].append(callback)
logger.info(f"Subscribed to {topic}:{partition}")
return True
async def unsubscribe(self, topic: str, partition: int, callback: Callable):
"""Unsubscribe from a topic partition"""
callbacks = self.subscribers[(topic, partition)]
if callback in callbacks:
callbacks.remove(callback)
logger.info(f"Unsubscribed from {topic}:{partition}")
async def get_consumer_group(self, group_id: str) -> ConsumerGroup:
"""Get or create a consumer group"""
if group_id not in self.consumer_groups:
self.consumer_groups[group_id] = ConsumerGroup(group_id)
return self.consumer_groups[group_id]
async def list_topics(self) -> List[Dict[str, Any]]:
"""List all topics"""
topics = []
for topic_name, config in self.topics.items():
topic_info = asdict(config)
# Add partition information
partitions_info = []
for partition_id in range(config.partitions):
partition_obj = self.partitions.get((topic_name, partition_id))
if partition_obj:
latest_offset = await partition_obj.get_latest_offset()
partitions_info.append({
"partition": partition_id,
"latest_offset": latest_offset,
"message_count": len(partition_obj._messages)
})
topic_info["partitions_info"] = partitions_info
topics.append(topic_info)
return topics
async def get_metrics(self) -> Dict[str, Any]:
"""Get broker metrics"""
base_metrics = self.metrics.get_metrics()
return {
**base_metrics,
"topics_count": len(self.topics),
"partitions_count": len(self.partitions),
"active_connections": len(self.client_connections),
"consumer_groups_count": len(self.consumer_groups),
"uptime_seconds": time.time() - getattr(self, '_start_time', time.time())
}
async def start_cleanup_task(self):
"""Start background cleanup task"""
self._start_time = time.time()
self._cleanup_task = asyncio.create_task(self._cleanup_loop())
async def _cleanup_loop(self):
"""Background cleanup for old messages"""
while not self._shutdown_event.is_set():
try:
for topic_name, topic_config in self.topics.items():
for partition_id in range(topic_config.partitions):
partition = self.partitions.get((topic_name, partition_id))
if partition:
await partition.cleanup_old_messages(topic_config.retention_ms)
# Wait 1 hour before next cleanup
await asyncio.wait_for(self._shutdown_event.wait(), timeout=3600)
except asyncio.TimeoutError:
continue # Continue cleanup loop
except Exception as e:
logger.error(f"Cleanup error: {e}")
await asyncio.sleep(60) # Wait 1 minute on error
async def shutdown(self):
"""Gracefully shutdown the broker"""
logger.info("Shutting down MiniKafka broker...")
self._shutdown_event.set()
if self._cleanup_task:
self._cleanup_task.cancel()
try:
await self._cleanup_task
except asyncio.CancelledError:
pass
# Close all client connections
for writer in self.client_connections.values():
if not writer.is_closing():
writer.close()
try:
await writer.wait_closed()
except:
pass
logger.info("Broker shutdown complete")
async def handle_client(broker: MiniKafkaBroker, reader: asyncio.StreamReader,
writer: asyncio.StreamWriter):
"""Handle client connections"""
client_id = str(uuid.uuid4())
client_addr = writer.get_extra_info('peername')
broker.client_connections[client_id] = writer
client_callbacks = [] # Track callbacks for cleanup
logger.info(f"Client {client_id} connected from {client_addr}")
broker.metrics.increment("connections_total")
try:
while True:
# Read message with timeout
try:
data = await asyncio.wait_for(reader.readuntil(b'\n'), timeout=30.0)
except asyncio.TimeoutError:
# Send keepalive
writer.write(json.dumps({"status": "keepalive"}).encode() + b'\n')
await writer.drain()
continue
if not data:
break
try:
message = json.loads(data.decode().strip())
except json.JSONDecodeError as e:
logger.error(f"Invalid JSON from {client_id}: {e}")
await send_error(writer, "Invalid JSON")
continue
action = message.get("action")
if action == "produce":
await handle_produce(broker, writer, message)
elif action == "consume":
await handle_consume(broker, writer, message)
elif action == "subscribe":
callback = await handle_subscribe(broker, writer, message, client_id)
if callback:
client_callbacks.append((message.get("topic"), message.get("partition", 0), callback))
elif action == "create_topic":
await handle_create_topic(broker, writer, message)
elif action == "list_topics":
await handle_list_topics(broker, writer)
elif action == "get_metrics":
await handle_get_metrics(broker, writer)
elif action == "commit_offset":
await handle_commit_offset(broker, writer, message)
else:
await send_error(writer, f"Unknown action: {action}")
except asyncio.IncompleteReadError:
logger.info(f"Client {client_id} disconnected")
except Exception as e:
logger.error(f"Client {client_id} error: {e}")
finally:
# Cleanup subscriptions
for topic, partition, callback in client_callbacks:
await broker.unsubscribe(topic, partition, callback)
# Remove from active connections
broker.client_connections.pop(client_id, None)
if not writer.is_closing():
writer.close()
await writer.wait_closed()
async def send_response(writer: asyncio.StreamWriter, response: Dict[str, Any]):
"""Send JSON response to client"""
try:
writer.write(json.dumps(response).encode() + b'\n')
await writer.drain()
except Exception as e:
logger.error(f"Failed to send response: {e}")
async def send_error(writer: asyncio.StreamWriter, error_message: str):
"""Send error response to client"""
await send_response(writer, {"status": "error", "message": error_message})
async def handle_produce(broker: MiniKafkaBroker, writer: asyncio.StreamWriter, message: Dict[str, Any]):
"""Handle produce request"""
topic = message.get("topic")
value = message.get("value", "")
key = message.get("key")
headers = message.get("headers", {})
if not topic:
await send_error(writer, "Missing topic")
return
result = await broker.produce(topic, value.encode(), key, headers)
if result:
await send_response(writer, {
"status": "ok",
"topic": result.topic,
"partition": result.partition,
"offset": result.offset,
"timestamp": int(result.timestamp * 1000)
})
else:
await send_error(writer, "Failed to produce message")
async def handle_consume(broker: MiniKafkaBroker, writer: asyncio.StreamWriter, message: Dict[str, Any]):
"""Handle consume request"""
topic = message.get("topic")
partition = message.get("partition", 0)
offset = message.get("offset", 0)
max_messages = min(message.get("max_messages", 100), 1000) # Cap at 1000
if not topic:
await send_error(writer, "Missing topic")
return
messages = await broker.consume(topic, partition, offset, max_messages)
await send_response(writer, {
"status": "ok",
"messages": [msg.to_dict() for msg in messages]
})
async def handle_subscribe(broker: MiniKafkaBroker, writer: asyncio.StreamWriter,
message: Dict[str, Any], client_id: str) -> Optional[Callable]:
"""Handle subscribe request"""
topic = message.get("topic")
partition = message.get("partition", 0)
if not topic:
await send_error(writer, "Missing topic")
return None
async def callback(msg: Message):
try:
if writer.is_closing():
return
await send_response(writer, {
"status": "message",
"message": msg.to_dict()
})
except Exception as e:
logger.error(f"Failed to send subscription message to {client_id}: {e}")
success = await broker.subscribe(topic, partition, callback, client_id)
if success:
await send_response(writer, {"status": "subscribed", "topic": topic, "partition": partition})
return callback
else:
await send_error(writer, f"Failed to subscribe to {topic}:{partition}")
return None
async def handle_create_topic(broker: MiniKafkaBroker, writer: asyncio.StreamWriter, message: Dict[str, Any]):
"""Handle create topic request"""
topic = message.get("topic")
partitions = message.get("partitions", 1)
replication_factor = message.get("replication_factor", 1)
if not topic:
await send_error(writer, "Missing topic name")
return
success = await broker.create_topic(topic, partitions, replication_factor)
if success:
await send_response(writer, {"status": "ok", "topic": topic})
else:
await send_error(writer, f"Topic '{topic}' already exists")
async def handle_list_topics(broker: MiniKafkaBroker, writer: asyncio.StreamWriter):
"""Handle list topics request"""
topics = await broker.list_topics()
await send_response(writer, {"status": "ok", "topics": topics})
async def handle_get_metrics(broker: MiniKafkaBroker, writer: asyncio.StreamWriter):
"""Handle get metrics request"""
metrics = await broker.get_metrics()
await send_response(writer, {"status": "ok", "metrics": metrics})
async def handle_commit_offset(broker: MiniKafkaBroker, writer: asyncio.StreamWriter, message: Dict[str, Any]):
"""Handle commit offset request"""
group_id = message.get("group_id")
topic = message.get("topic")
partition = message.get("partition", 0)
offset = message.get("offset")
if not all([group_id, topic, offset is not None]):
await send_error(writer, "Missing required fields")
return
consumer_group = await broker.get_consumer_group(group_id)
await consumer_group.commit_offset(topic, partition, offset)
await send_response(writer, {"status": "ok"})
async def start_server(config: BrokerConfig):
"""Start the MiniKafka server"""
broker = MiniKafkaBroker(config)
# Setup signal handlers for graceful shutdown
def signal_handler():
logger.info("Received shutdown signal")
asyncio.create_task(broker.shutdown())
for sig in [signal.SIGINT, signal.SIGTERM]:
try:
asyncio.get_event_loop().add_signal_handler(sig, signal_handler)
except NotImplementedError:
# Windows doesn't support signal handlers in asyncio
signal.signal(sig, lambda s, f: asyncio.create_task(broker.shutdown()))
# Start cleanup task
await broker.start_cleanup_task()
# Start server
server = await asyncio.start_server(
lambda r, w: handle_client(broker, r, w),
config.host,
config.port,
limit=config.socket_receive_buffer_bytes
)
logger.info(f"MiniKafka broker started on {config.host}:{config.port}")
logger.info(f"Data directory: {config.data_dir}")
logger.info(f"Broker ID: {config.broker_id}")
async with server:
await server.serve_forever()
def load_config(config_file: str) -> BrokerConfig:
"""Load configuration from file"""
if os.path.exists(config_file):
try:
with open(config_file, 'r') as f:
config_data = yaml.safe_load(f) or {}
return BrokerConfig(**config_data)
except Exception as e:
logger.error(f"Failed to load config file {config_file}: {e}")
return BrokerConfig() # Return default config on error
return BrokerConfig() # Return default config if file doesn't exist
def main():
"""Main entry point for MiniKafka"""
parser = argparse.ArgumentParser(description='MiniKafka - A lightweight Kafka-like message broker')
parser.add_argument('--config', default='minikafka.yaml', help='Path to configuration file')
parser.add_argument('--host', help='Override host from config')
parser.add_argument('--port', type=int, help='Override port from config')
parser.add_argument('--data-dir', help='Override data directory from config')
args = parser.parse_args()
# Load configuration
config = load_config(args.config)
# Apply command line overrides
if args.host:
config.host = args.host
if args.port:
config.port = args.port
if args.data_dir:
config.data_dir = args.data_dir
# Start the server
try:
asyncio.run(start_server(config))
except KeyboardInterrupt:
logger.info("Shutting down gracefully...")
except Exception as e:
logger.error(f"Server error: {e}")
sys.exit(1)
if __name__ == "__main__":
main()