| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
import struct |
| 17 |
import time |
| 18 |
|
| 19 |
from confluent_kafka import ConsumerGroupTopicPartitions, TopicPartition, ConsumerGroupState, KafkaError |
| 20 |
from confluent_kafka.admin import (NewPartitions, ConfigResource, |
| 21 |
AclBinding, AclBindingFilter, ResourceType, |
| 22 |
ResourcePatternType, AclOperation, AclPermissionType) |
| 23 |
from confluent_kafka.error import ConsumeError |
| 24 |
|
| 25 |
topic_prefix = "test-topic" |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
produce_headers = [('foo1', 'bar'), |
| 31 |
('foo1', 'bar2'), |
| 32 |
('foo2', b'1'), |
| 33 |
(u'Jämtland', u'Härjedalen'),ed |
| 34 |
('nullheader', None), |
| 35 |
('empty', ''), |
| 36 |
('foobin', struct.pack('hhl', 10, 20, 30))] |
| 37 |
|
| 38 |
|
| 39 |
def verify_commit_result(err, partitions): |
| 40 |
assert err is not None |
| 41 |
|
| 42 |
|
| 43 |
def verify_admin_acls(admin_client, |
| 44 |
topic, |
| 45 |
group): |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
acl_binding_1 = AclBinding(ResourceType.TOPIC, topic, ResourcePatternType.LITERAL, |
| 51 |
"User:test-user-1", "*", AclOperation.READ, AclPermissionType.ALLOW) |
| 52 |
acl_binding_2 = AclBinding(ResourceType.TOPIC, topic, ResourcePatternType.PREFIXED, |
| 53 |
"User:test-user-2", "*", AclOperation.WRITE, AclPermissionType.DENY) |
| 54 |
acl_binding_3 = AclBinding(ResourceType.GROUP, group, ResourcePatternType.PREFIXED, |
| 55 |
"User:test-user-2", "*", AclOperation.ALL, AclPermissionType.ALLOW) |
| 56 |
|
| 57 |
fs = admin_client.create_acls([acl_binding_1, acl_binding_2, acl_binding_3]) |
| 58 |
for acl_binding, f in fs.items(): |
| 59 |
f.result() |
| 60 |
|
| 61 |
time.sleep(1) |
| 62 |
|
| 63 |
acl_binding_filter1 = AclBindingFilter(ResourceType.ANY, None, ResourcePatternType.ANY, |
| 64 |
None, None, AclOperation.ANY, AclPermissionType.ANY) |
| 65 |
acl_binding_filter2 = AclBindingFilter(ResourceType.ANY, None, ResourcePatternType.PREFIXED, |
| 66 |
None, None, AclOperation.ANY, AclPermissionType.ANY) |
| 67 |
acl_binding_filter3 = AclBindingFilter(ResourceType.TOPIC, None, ResourcePatternType.ANY, |
| 68 |
None, None, AclOperation.ANY, AclPermissionType.ANY) |
| 69 |
acl_binding_filter4 = AclBindingFilter(ResourceType.GROUP, None, ResourcePatternType.ANY, |
| 70 |
None, None, AclOperation.ANY, AclPermissionType.ANY) |
| 71 |
|
| 72 |
expected_acl_bindings = [acl_binding_1, acl_binding_2, acl_binding_3] |
| 73 |
acl_bindings = admin_client.describe_acls(acl_binding_filter1).result() |
| 74 |
assert sorted(acl_bindings) == sorted(expected_acl_bindings), \ |
| 75 |
"ACL bindings don't match, actual: {} expected: {}".format(acl_bindings, |
| 76 |
expected_acl_bindings) |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
expected_acl_bindings = [acl_binding_2, acl_binding_3] |
| 82 |
fs = admin_client.delete_acls([acl_binding_filter2]) |
| 83 |
deleted_acl_bindings = sorted(fs[acl_binding_filter2].result()) |
| 84 |
assert deleted_acl_bindings == expected_acl_bindings, \ |
| 85 |
"Deleted ACL bindings don't match, actual {} expected {}".format(deleted_acl_bindings, |
| 86 |
expected_acl_bindings) |
| 87 |
|
| 88 |
time.sleep(1) |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
expected_acl_bindings = [[acl_binding_1], []] |
| 94 |
delete_acl_binding_filters = [acl_binding_filter3, acl_binding_filter4] |
| 95 |
fs = admin_client.delete_acls(delete_acl_binding_filters) |
| 96 |
for acl_binding, expected in zip(delete_acl_binding_filters, expected_acl_bindings): |
| 97 |
deleted_acl_bindings = sorted(fs[acl_binding].result()) |
| 98 |
assert deleted_acl_bindings == expected, \ |
| 99 |
"Deleted ACL bindings don't match, actual {} expected {}".format(deleted_acl_bindings, |
| 100 |
expected) |
| 101 |
|
| 102 |
time.sleep(1) |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
expected_acl_bindings = [] |
| 108 |
acl_bindings = admin_client.describe_acls(acl_binding_filter1).result() |
| 109 |
assert acl_bindings == expected_acl_bindings, \ |
| 110 |
"ACL bindings don't match, actual: {} expected: {}".format(acl_bindings, |
| 111 |
expected_acl_bindings) |
| 112 |
|
| 113 |
|
| 114 |
def verify_topic_metadata(client, exp_topics, *args, **kwargs): |
| 115 |
""" |
| 116 |
Verify that exp_topics (dict<topicname,partcnt>) is reported in metadata. |
| 117 |
Will retry and wait for some time to let changes propagate. |
| 118 |
|
| 119 |
Non-controller brokers may return the previous partition count for some |
| 120 |
time before being updated, in this case simply retry. |
| 121 |
""" |
| 122 |
|
| 123 |
for retry in range(0, 3): |
| 124 |
do_retry = 0 |
| 125 |
|
| 126 |
md = client.list_topics(*args, **kwargs) |
| 127 |
|
| 128 |
for exptopic, exppartcnt in exp_topics.items(): |
| 129 |
if exptopic not in md.topics: |
| 130 |
print("Topic {} not yet reported in metadata: retrying".format(exptopic)) |
| 131 |
do_retry += 1 |
| 132 |
continue |
| 133 |
|
| 134 |
if len(md.topics[exptopic].partitions) < exppartcnt: |
| 135 |
print("Topic {} partition count not yet updated ({} != expected {}): retrying".format( |
| 136 |
exptopic, len(md.topics[exptopic].partitions), exppartcnt)) |
| 137 |
do_retry += 1 |
| 138 |
continue |
| 139 |
|
| 140 |
assert len(md.topics[exptopic].partitions) == exppartcnt, \ |
| 141 |
"Expected {} partitions for topic {}, not {}".format( |
| 142 |
exppartcnt, exptopic, md.topics[exptopic].partitions) |
| 143 |
|
| 144 |
if do_retry == 0: |
| 145 |
return |
| 146 |
|
| 147 |
time.sleep(1) |
| 148 |
|
| 149 |
|
| 150 |
def verify_consumer_group_offsets_operations(client, our_topic, group_id): |
| 151 |
|
| 152 |
|
| 153 |
request = ConsumerGroupTopicPartitions(group_id) |
| 154 |
fs = client.list_consumer_group_offsets([request]) |
| 155 |
f = fs[group_id] |
| 156 |
res = f.result() |
| 157 |
assert isinstance(res, ConsumerGroupTopicPartitions) |
| 158 |
assert res.group_id == group_id |
| 159 |
assert len(res.topic_partitions) == 2 |
| 160 |
is_any_message_consumed = False |
| 161 |
for topic_partition in res.topic_partitions: |
| 162 |
assert topic_partition.topic == our_topic |
| 163 |
if topic_partition.offset > 0: |
| 164 |
is_any_message_consumed = True |
| 165 |
assert is_any_message_consumed |
| 166 |
|
| 167 |
|
| 168 |
alter_group_topic_partitions = list(map(lambda topic_partition: TopicPartition(topic_partition.topic, |
| 169 |
topic_partition.partition, |
| 170 |
0), |
| 171 |
res.topic_partitions)) |
| 172 |
alter_group_topic_partition_request = ConsumerGroupTopicPartitions(group_id, |
| 173 |
alter_group_topic_partitions) |
| 174 |
afs = client.alter_consumer_group_offsets([alter_group_topic_partition_request]) |
| 175 |
af = afs[group_id] |
| 176 |
ares = af.result() |
| 177 |
assert isinstance(ares, ConsumerGroupTopicPartitions) |
| 178 |
assert ares.group_id == group_id |
| 179 |
assert len(ares.topic_partitions) == 2 |
| 180 |
for topic_partition in ares.topic_partitions: |
| 181 |
assert topic_partition.topic == our_topic |
| 182 |
assert topic_partition.offset == 0 |
| 183 |
|
| 184 |
|
| 185 |
list_group_topic_partitions = list(map(lambda topic_partition: TopicPartition(topic_partition.topic, |
| 186 |
topic_partition.partition), |
| 187 |
ares.topic_partitions)) |
| 188 |
list_group_topic_partition_request = ConsumerGroupTopicPartitions(group_id, |
| 189 |
list_group_topic_partitions) |
| 190 |
lfs = client.list_consumer_group_offsets([list_group_topic_partition_request]) |
| 191 |
lf = lfs[group_id] |
| 192 |
lres = lf.result() |
| 193 |
|
| 194 |
assert isinstance(lres, ConsumerGroupTopicPartitions) |
| 195 |
assert lres.group_id == group_id |
| 196 |
assert len(lres.topic_partitions) == 2 |
| 197 |
for topic_partition in lres.topic_partitions: |
| 198 |
assert topic_partition.topic == our_topic |
| 199 |
assert topic_partition.offset == 0 |
| 200 |
|
| 201 |
|
| 202 |
def test_basic_operations(kafka_cluster): |
| 203 |
num_partitions = 2 |
| 204 |
topic_config = {"compression.type": "gzip"} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
for validate in (True, False): |
| 211 |
our_topic = kafka_cluster.create_topic_and_wait_propogation(topic_prefix, |
| 212 |
{ |
| 213 |
"num_partitions": num_partitions, |
| 214 |
"config": topic_config, |
| 215 |
"replication_factor": 1, |
| 216 |
}, |
| 217 |
validate_only=validate |
| 218 |
) |
| 219 |
|
| 220 |
admin_client = kafka_cluster.admin() |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
verify_topic_metadata(admin_client, {our_topic: num_partitions}) |
| 226 |
verify_topic_metadata(admin_client, {our_topic: num_partitions}, topic=our_topic) |
| 227 |
verify_topic_metadata(admin_client, {our_topic: num_partitions}, our_topic) |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
num_partitions += 3 |
| 233 |
fs = admin_client.create_partitions([NewPartitions(our_topic, |
| 234 |
new_total_count=num_partitions)], |
| 235 |
operation_timeout=10.0) |
| 236 |
|
| 237 |
for topic2, f in fs.items(): |
| 238 |
f.result() |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
verify_topic_metadata(admin_client, {our_topic: num_partitions}) |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
p = kafka_cluster.producer() |
| 251 |
p.produce(our_topic, 'Hello Python!', headers=produce_headers) |
| 252 |
p.produce(our_topic, key='Just a key and headers', headers=produce_headers) |
| 253 |
p.flush() |
| 254 |
|
| 255 |
def consume_messages(group_id, num_messages=None): |
| 256 |
|
| 257 |
conf = {'group.id': group_id, |
| 258 |
'session.timeout.ms': 6000, |
| 259 |
'enable.auto.commit': False, |
| 260 |
'on_commit': verify_commit_result, |
| 261 |
'auto.offset.reset': 'earliest', |
| 262 |
'enable.partition.eof': True} |
| 263 |
c = kafka_cluster.consumer(conf) |
| 264 |
c.subscribe([our_topic]) |
| 265 |
eof_reached = dict() |
| 266 |
read_messages = 0 |
| 267 |
msg = None |
| 268 |
while True: |
| 269 |
try: |
| 270 |
msg = c.poll() |
| 271 |
if msg is None: |
| 272 |
raise Exception('Got timeout from poll() without a timeout set: %s' % msg) |
| 273 |
|
| 274 |
c.commit(msg, asynchronous=False) |
| 275 |
read_messages += 1 |
| 276 |
if num_messages is not None and read_messages == num_messages: |
| 277 |
print('Read all the required messages: exiting') |
| 278 |
break |
| 279 |
except ConsumeError as e: |
| 280 |
if e.code == KafkaError._PARTITION_EOF: |
| 281 |
msg = e.kafka_message |
| 282 |
print('Reached end of %s [%d] at offset %d' % ( |
| 283 |
msg.topic(), msg.partition(), msg.offset())) |
| 284 |
eof_reached[(msg.topic(), msg.partition())] = True |
| 285 |
if len(eof_reached) == len(c.assignment()): |
| 286 |
print('EOF reached for all assigned partitions: exiting') |
| 287 |
break |
| 288 |
else: |
| 289 |
print('Consumer error: %s: ignoring' % str(e)) |
| 290 |
break |
| 291 |
c.close() |
| 292 |
|
| 293 |
group1 = 'test-group-1' |
| 294 |
group2 = 'test-group-2' |
| 295 |
acls_topic = our_topic + "-acls" |
| 296 |
acls_group = "test-group-acls" |
| 297 |
consume_messages(group1, 2) |
| 298 |
consume_messages(group2, 2) |
| 299 |
|
| 300 |
|
| 301 |
groups = set(group.id for group in admin_client.list_groups(timeout=10)) |
| 302 |
assert group1 in groups, "Consumer group {} not found".format(group1) |
| 303 |
assert group2 in groups, "Consumer group {} not found".format(group2) |
| 304 |
|
| 305 |
groups = set(group.id for group in admin_client.list_groups(group1)) |
| 306 |
assert group1 in groups, "Consumer group {} not found".format(group1) |
| 307 |
groups = set(group.id for group in admin_client.list_groups(group2)) |
| 308 |
assert group2 in groups, "Consumer group {} not found".format(group2) |
| 309 |
|
| 310 |
|
| 311 |
future = admin_client.list_consumer_groups(request_timeout=10) |
| 312 |
result = future.result() |
| 313 |
group_ids = [group.group_id for group in result.valid] |
| 314 |
assert group1 in group_ids, "Consumer group {} not found".format(group1) |
| 315 |
assert group2 in group_ids, "Consumer group {} not found".format(group2) |
| 316 |
|
| 317 |
future = admin_client.list_consumer_groups(request_timeout=10, states={ConsumerGroupState.STABLE}) |
| 318 |
result = future.result() |
| 319 |
assert isinstance(result.valid, list) |
| 320 |
assert not result.valid |
| 321 |
|
| 322 |
def verify_config(expconfig, configs): |
| 323 |
""" |
| 324 |
Verify that the config key,values in expconfig are found |
| 325 |
and matches the ConfigEntry in configs. |
| 326 |
""" |
| 327 |
for key, expvalue in expconfig.items(): |
| 328 |
entry = configs.get(key, None) |
| 329 |
assert entry is not None, "Config {} not found in returned configs".format(key) |
| 330 |
|
| 331 |
assert entry.value == str(expvalue), \ |
| 332 |
"Config {} with value {} does not match expected value {}".format(key, entry, expvalue) |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
resource = ConfigResource(ResourceType.TOPIC, our_topic) |
| 338 |
fs = admin_client.describe_configs([resource]) |
| 339 |
configs = fs[resource].result() |
| 340 |
|
| 341 |
|
| 342 |
verify_config(topic_config, configs) |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
topic_config["file.delete.delay.ms"] = 12345 |
| 348 |
topic_config["compression.type"] = "snappy" |
| 349 |
|
| 350 |
for key, value in topic_config.items(): |
| 351 |
resource.set_config(key, value) |
| 352 |
|
| 353 |
fs = admin_client.alter_configs([resource]) |
| 354 |
fs[resource].result() |
| 355 |
|
| 356 |
time.sleep(1) |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
fs = admin_client.describe_configs([resource]) |
| 362 |
configs = fs[resource].result() |
| 363 |
|
| 364 |
|
| 365 |
verify_config(topic_config, configs) |
| 366 |
|
| 367 |
|
| 368 |
verify_consumer_group_offsets_operations(admin_client, our_topic, group1) |
| 369 |
|
| 370 |
|
| 371 |
fs = admin_client.delete_consumer_groups([group1, group2], request_timeout=10) |
| 372 |
fs[group1].result() |
| 373 |
fs[group2].result() |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
fs = admin_client.delete_topics([our_topic]) |
| 379 |
fs[our_topic].result() |
| 380 |
print("Topic {} marked for deletion".format(our_topic)) |
| 381 |
|
| 382 |
|
| 383 |
verify_admin_acls(admin_client, acls_topic, acls_group) |