Skip to content

Commit c99d6bd

Browse files
authored
Add more async consumer unit & integration tests (#2052)
* basic rebalance test * rebalance tests * refactor and linter fix * feebdack * refactor and cleanup * update * remove jit imports
1 parent 4f0bc9d commit c99d6bd

File tree

3 files changed

+362
-96
lines changed

3 files changed

+362
-96
lines changed

tests/ducktape/services/kafka.py

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
import time
66
from ducktape.services.service import Service
77

8+
from confluent_kafka.admin import AdminClient, NewTopic, NewPartitions
9+
810

911
class KafkaClient(Service):
1012
"""Kafka client wrapper - assumes external Kafka is running"""
1113

14+
DEFAULT_TIMEOUT = 10
15+
1216
def __init__(self, context, bootstrap_servers="localhost:9092"):
1317
# Use num_nodes=0 since we're not managing any nodes
1418
super(KafkaClient, self).__init__(context, num_nodes=0)
@@ -33,23 +37,20 @@ def bootstrap_servers(self):
3337
def verify_connection(self):
3438
"""Verify that Kafka is accessible"""
3539
try:
36-
from confluent_kafka.admin import AdminClient
3740
admin_client = AdminClient({'bootstrap.servers': self.bootstrap_servers_str})
3841

3942
# Try to get cluster metadata to verify connection
40-
metadata = admin_client.list_topics(timeout=10)
43+
metadata = admin_client.list_topics(timeout=self.DEFAULT_TIMEOUT)
4144
self.logger.info("Successfully connected to Kafka. Available topics: %s",
4245
list(metadata.topics.keys()))
4346
return True
4447
except Exception as e:
45-
self.logger.error("Failed to connect to Kafka at %s: %s", self.bootstrap_servers_str, e)
48+
self.logger.error("Failed to connect to Kafka: %s", e)
4649
return False
4750

4851
def create_topic(self, topic, partitions=1, replication_factor=1):
4952
"""Create a topic using Kafka admin client"""
5053
try:
51-
from confluent_kafka.admin import AdminClient, NewTopic
52-
5354
admin_client = AdminClient({'bootstrap.servers': self.bootstrap_servers_str})
5455

5556
topic_config = NewTopic(
@@ -75,26 +76,19 @@ def create_topic(self, topic, partitions=1, replication_factor=1):
7576
else:
7677
self.logger.warning("Failed to create topic %s: %s", topic_name, e)
7778

78-
except ImportError:
79-
self.logger.error("confluent_kafka not available for topic creation")
8079
except Exception as e:
8180
self.logger.error("Failed to create topic %s: %s", topic, e)
8281

8382
def list_topics(self):
8483
"""List all topics using admin client"""
8584
try:
86-
from confluent_kafka.admin import AdminClient
87-
8885
admin_client = AdminClient({'bootstrap.servers': self.bootstrap_servers_str})
89-
metadata = admin_client.list_topics(timeout=10)
86+
metadata = admin_client.list_topics(timeout=self.DEFAULT_TIMEOUT)
9087

9188
topics = list(metadata.topics.keys())
9289
self.logger.debug("Available topics: %s", topics)
9390
return topics
9491

95-
except ImportError:
96-
self.logger.error("confluent_kafka not available for listing topics")
97-
return []
9892
except Exception as e:
9993
self.logger.error("Failed to list topics: %s", e)
10094
return []
@@ -124,3 +118,28 @@ def wait_for_topic(self, topic_name, max_wait_time=30, initial_wait=0.1):
124118

125119
self.logger.error("Timeout waiting for topic '%s' after %ds", topic_name, max_wait_time)
126120
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 # No change needed
134+
135+
# Add partitions
136+
new_partitions = NewPartitions(topic=topic_name, new_total_count=new_partition_count)
137+
fs = admin_client.create_partitions([new_partitions])
138+
139+
# Wait for completion
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

0 commit comments

Comments
 (0)