| 1 |
""" |
| 2 |
Kafka client wrapper for Ducktape tests |
| 3 |
Assumes Kafka is already running on localhost:9092 |
| 4 |
""" |
| 5 |
import time |
| 6 |
from ducktape.services.service import Service |
| 7 |
|
| 8 |
from confluent_kafka.admin import AdminClient, NewTopic, NewPartitions |
| 9 |
|
| 10 |
|
| 11 |
class KafkaClient(Service): |
| 12 |
"""Kafka client wrapper - assumes external Kafka is running""" |
| 13 |
|
| 14 |
DEFAULT_TIMEOUT = 10 |
| 15 |
|
| 16 |
def __init__(self, context, bootstrap_servers="localhost:9092"): |
| 17 |
|
| 18 |
super(KafkaClient, self).__init__(context, num_nodes=0) |
| 19 |
self.bootstrap_servers_str = bootstrap_servers |
| 20 |
|
| 21 |
def start_node(self, node): |
| 22 |
"""No-op since we assume Kafka is already running""" |
| 23 |
self.logger.info("Assuming Kafka is already running on %s", self.bootstrap_servers_str) |
| 24 |
|
| 25 |
def stop_node(self, node): |
| 26 |
"""No-op since we don't manage the Kafka service""" |
| 27 |
self.logger.info("Not stopping external Kafka service") |
| 28 |
|
| 29 |
def clean_node(self, node): |
| 30 |
"""No-op since we don't manage any files""" |
| 31 |
self.logger.info("No cleanup needed for external Kafka service") |
| 32 |
|
| 33 |
def bootstrap_servers(self): |
| 34 |
"""Get bootstrap servers string for clients""" |
| 35 |
return self.bootstrap_servers_str |
| 36 |
|
| 37 |
def verify_connection(self): |
| 38 |
"""Verify that Kafka is accessible""" |
| 39 |
try: |
| 40 |
admin_client = AdminClient({'bootstrap.servers': self.bootstrap_servers_str}) |
| 41 |
|
| 42 |
|
| 43 |
metadata = admin_client.list_topics(timeout=self.DEFAULT_TIMEOUT) |
| 44 |
self.logger.info("Successfully connected to Kafka. Available topics: %s", |
| 45 |
list(metadata.topics.keys())) |
| 46 |
return True |
| 47 |
except Exception as e: |
| 48 |
self.logger.error("Failed to connect to Kafka: %s", e) |
| 49 |
return False |
| 50 |
|
| 51 |
def create_topic(self, topic, partitions=1, replication_factor=1): |
| 52 |
"""Create a topic using Kafka admin client""" |
| 53 |
try: |
| 54 |
admin_client = AdminClient({'bootstrap.servers': self.bootstrap_servers_str}) |
| 55 |
|
| 56 |
topic_config = NewTopic( |
| 57 |
topic=topic, |
| 58 |
num_partitions=partitions, |
| 59 |
replication_factor=replication_factor |
| 60 |
) |
| 61 |
|
| 62 |
self.logger.info("Creating topic: %s (partitions=%d, replication=%d)", |
| 63 |
topic, partitions, replication_factor) |
| 64 |
|
| 65 |
|
| 66 |
fs = admin_client.create_topics([topic_config]) |
| 67 |
|
| 68 |
|
| 69 |
for topic_name, f in fs.items(): |
| 70 |
try: |
| 71 |
f.result(timeout=30) |
| 72 |
self.logger.info("Topic %s created successfully", topic_name) |
| 73 |
except Exception as e: |
| 74 |
if "already exists" in str(e).lower(): |
| 75 |
self.logger.info("Topic %s already exists", topic_name) |
| 76 |
else: |
| 77 |
self.logger.warning("Failed to create topic %s: %s", topic_name, e) |
| 78 |
|
| 79 |
except Exception as e: |
| 80 |
self.logger.error("Failed to create topic %s: %s", topic, e) |
| 81 |
|
| 82 |
def list_topics(self): |
| 83 |
"""List all topics using admin client""" |
| 84 |
try: |
| 85 |
admin_client = AdminClient({'bootstrap.servers': self.bootstrap_servers_str}) |
| 86 |
metadata = admin_client.list_topics(timeout=self.DEFAULT_TIMEOUT) |
| 87 |
|
| 88 |
topics = list(metadata.topics.keys()) |
| 89 |
self.logger.debug("Available topics: %s", topics) |
| 90 |
return topics |
| 91 |
|
| 92 |
except Exception as e: |
| 93 |
self.logger.error("Failed to list topics: %s", e) |
| 94 |
return [] |
| 95 |
|
| 96 |
def wait_for_topic(self, topic_name, max_wait_time=30, initial_wait=0.1): |
| 97 |
""" |
| 98 |
Wait for topic to be created with exponential backoff retry logic. |
| 99 |
""" |
| 100 |
wait_time = initial_wait |
| 101 |
total_wait = 0 |
| 102 |
|
| 103 |
self.logger.info("Waiting for topic '%s' to be available...", topic_name) |
| 104 |
|
| 105 |
while total_wait < max_wait_time: |
| 106 |
topics = self.list_topics() |
| 107 |
if topic_name in topics: |
| 108 |
self.logger.info("Topic '%s' is ready after %.2fs", topic_name, total_wait) |
| 109 |
return True |
| 110 |
|
| 111 |
self.logger.debug("Topic '%s' not found, waiting %.2fs (total: %.2fs)", |
| 112 |
topic_name, wait_time, total_wait) |
| 113 |
time.sleep(wait_time) |
| 114 |
total_wait += wait_time |
| 115 |
|
| 116 |
|
| 117 |
wait_time = min(wait_time * 2, 2.0) |
| 118 |
|
| 119 |
self.logger.error("Timeout waiting for topic '%s' after %ds", topic_name, max_wait_time) |
| 120 |
return False |
| 121 |
|
| 122 |
def add_partitions(self, topic_name, new_partition_count): |
| 123 |
"""Add partitions to an existing topic""" |
| 124 |
try: |
| 125 |
admin_client = AdminClient({'bootstrap.servers': self.bootstrap_servers_str}) |
| 126 |
metadata = admin_client.list_topics(timeout=self.DEFAULT_TIMEOUT) |
| 127 |
|
| 128 |
if topic_name not in metadata.topics: |
| 129 |
raise ValueError(f"Topic {topic_name} does not exist") |
| 130 |
|
| 131 |
current_partitions = len(metadata.topics[topic_name].partitions) |
| 132 |
if new_partition_count <= current_partitions: |
| 133 |
return |
| 134 |
|
| 135 |
|
| 136 |
new_partitions = NewPartitions(topic=topic_name, new_total_count=new_partition_count) |
| 137 |
fs = admin_client.create_partitions([new_partitions]) |
| 138 |
|
| 139 |
|
| 140 |
for topic, f in fs.items(): |
| 141 |
f.result(timeout=30) |
| 142 |
|
| 143 |
except Exception as e: |
| 144 |
self.logger.error("Failed to add partitions to topic %s: %s", topic_name, e) |
| 145 |
raise |