| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
import os |
| 20 |
from confluent_kafka import Consumer |
| 21 |
|
| 22 |
_GROUP_PROTOCOL_ENV = 'TEST_CONSUMER_GROUP_PROTOCOL' |
| 23 |
_TRIVUP_CLUSTER_TYPE_ENV = 'TEST_TRIVUP_CLUSTER_TYPE' |
| 24 |
|
| 25 |
|
| 26 |
def _trivup_cluster_type_kraft(): |
| 27 |
return _TRIVUP_CLUSTER_TYPE_ENV in os.environ and os.environ[_TRIVUP_CLUSTER_TYPE_ENV] == 'kraft' |
| 28 |
|
| 29 |
|
| 30 |
class TestUtils: |
| 31 |
@staticmethod |
| 32 |
def broker_version(): |
| 33 |
return '4.0.0' if TestUtils.use_group_protocol_consumer() else '3.9.0' |
| 34 |
|
| 35 |
@staticmethod |
| 36 |
def broker_conf(): |
| 37 |
broker_conf = ['transaction.state.log.replication.factor=1', |
| 38 |
'transaction.state.log.min.isr=1'] |
| 39 |
if TestUtils.use_group_protocol_consumer(): |
| 40 |
broker_conf.append('group.coordinator.rebalance.protocols=classic,consumer') |
| 41 |
return broker_conf |
| 42 |
|
| 43 |
@staticmethod |
| 44 |
def _broker_major_version(): |
| 45 |
return int(TestUtils.broker_version().split('.')[0]) |
| 46 |
|
| 47 |
@staticmethod |
| 48 |
def use_kraft(): |
| 49 |
return (TestUtils.use_group_protocol_consumer() or |
| 50 |
_trivup_cluster_type_kraft()) |
| 51 |
|
| 52 |
@staticmethod |
| 53 |
def use_group_protocol_consumer(): |
| 54 |
return _GROUP_PROTOCOL_ENV in os.environ and os.environ[_GROUP_PROTOCOL_ENV] == 'consumer' |
| 55 |
|
| 56 |
@staticmethod |
| 57 |
def update_conf_group_protocol(conf=None): |
| 58 |
if TestUtils.can_upgrade_group_protocol_to_consumer(conf): |
| 59 |
conf['group.protocol'] = 'consumer' |
| 60 |
|
| 61 |
@staticmethod |
| 62 |
def can_upgrade_group_protocol_to_consumer(conf): |
| 63 |
return (conf is not None and 'group.id' in conf and |
| 64 |
'group.protocol' not in conf and TestUtils.use_group_protocol_consumer()) |
| 65 |
|
| 66 |
@staticmethod |
| 67 |
def remove_forbidden_conf_group_protocol_consumer(conf): |
| 68 |
if (conf is None or |
| 69 |
not TestUtils.use_group_protocol_consumer() or |
| 70 |
conf.get('group.protocol', 'consumer') != 'consumer'): |
| 71 |
return |
| 72 |
forbidden_conf_properties = ["session.timeout.ms", |
| 73 |
"partition.assignment.strategy", |
| 74 |
"heartbeat.interval.ms", |
| 75 |
"group.protocol.type"] |
| 76 |
for prop in forbidden_conf_properties: |
| 77 |
if prop in conf: |
| 78 |
print(f"Skipping setting forbidden configuration {prop} for `CONSUMER` protocol") |
| 79 |
del conf[prop] |
| 80 |
|
| 81 |
|
| 82 |
class TestConsumer(Consumer): |
| 83 |
def __init__(self, conf=None, **kwargs): |
| 84 |
TestUtils.update_conf_group_protocol(conf) |
| 85 |
TestUtils.remove_forbidden_conf_group_protocol_consumer(conf) |
| 86 |
super(TestConsumer, self).__init__(conf, **kwargs) |
| 87 |
|
| 88 |
def assign(self, partitions): |
| 89 |
if TestUtils.use_group_protocol_consumer(): |
| 90 |
super(TestConsumer, self).incremental_assign(partitions) |
| 91 |
else: |
| 92 |
super(TestConsumer, self).assign(partitions) |
| 93 |
|
| 94 |
def unassign(self, partitions): |
| 95 |
if TestUtils.use_group_protocol_consumer(): |
| 96 |
super(TestConsumer, self).incremental_unassign(partitions) |
| 97 |
else: |
| 98 |
super(TestConsumer, self).unassign() |