| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
from confluent_kafka.admin import OffsetSpec, DeletedRecords |
| 17 |
from confluent_kafka import TopicPartition |
| 18 |
|
| 19 |
|
| 20 |
def test_delete_records(kafka_cluster): |
| 21 |
""" |
| 22 |
Test delete_records, delete the records upto the specified offset |
| 23 |
in that particular partition of the specified topic. |
| 24 |
""" |
| 25 |
admin_client = kafka_cluster.admin() |
| 26 |
|
| 27 |
|
| 28 |
topic = kafka_cluster.create_topic_and_wait_propogation("test-del-records", |
| 29 |
{ |
| 30 |
"num_partitions": 1, |
| 31 |
"replication_factor": 1, |
| 32 |
}) |
| 33 |
|
| 34 |
|
| 35 |
p = kafka_cluster.producer() |
| 36 |
p.produce(topic, "Message-1") |
| 37 |
p.produce(topic, "Message-2") |
| 38 |
p.produce(topic, "Message-3") |
| 39 |
p.flush() |
| 40 |
|
| 41 |
topic_partition = TopicPartition(topic, 0) |
| 42 |
requests = {topic_partition: OffsetSpec.earliest()} |
| 43 |
|
| 44 |
|
| 45 |
fs = admin_client.list_offsets(requests) |
| 46 |
result = list(fs.values())[0].result() |
| 47 |
assert (result.offset == 0) |
| 48 |
|
| 49 |
topic_partition_offset = TopicPartition(topic, 0, 2) |
| 50 |
|
| 51 |
|
| 52 |
fs1 = admin_client.delete_records([topic_partition_offset]) |
| 53 |
|
| 54 |
|
| 55 |
fs2 = admin_client.list_offsets(requests) |
| 56 |
|
| 57 |
|
| 58 |
res = list(fs1.values())[0].result() |
| 59 |
assert isinstance(res, DeletedRecords) |
| 60 |
assert (res.low_watermark == list(fs2.values())[0].result().offset) |
| 61 |
|
| 62 |
|
| 63 |
fs = admin_client.delete_topics([topic]) |
| 64 |
for topic, f in fs.items(): |
| 65 |
f.result() |
| 66 |
|
| 67 |
|
| 68 |
def test_delete_records_multiple_topics_and_partitions(kafka_cluster): |
| 69 |
""" |
| 70 |
Test delete_records, delete the records upto the specified offset |
| 71 |
in that particular partition of the specified topic. |
| 72 |
""" |
| 73 |
admin_client = kafka_cluster.admin() |
| 74 |
num_partitions = 3 |
| 75 |
|
| 76 |
topic = kafka_cluster.create_topic_and_wait_propogation("test-del-records", |
| 77 |
{ |
| 78 |
"num_partitions": num_partitions, |
| 79 |
"replication_factor": 1, |
| 80 |
}) |
| 81 |
topic2 = kafka_cluster.create_topic_and_wait_propogation("test-del-records2", |
| 82 |
{ |
| 83 |
"num_partitions": num_partitions, |
| 84 |
"replication_factor": 1, |
| 85 |
}) |
| 86 |
|
| 87 |
topics = [topic, topic2] |
| 88 |
partitions = list(range(num_partitions)) |
| 89 |
|
| 90 |
p = kafka_cluster.producer() |
| 91 |
for t in topics: |
| 92 |
for partition in partitions: |
| 93 |
p.produce(t, "Message-1", partition=partition) |
| 94 |
p.produce(t, "Message-2", partition=partition) |
| 95 |
p.produce(t, "Message-3", partition=partition) |
| 96 |
p.flush() |
| 97 |
requests = dict( |
| 98 |
[ |
| 99 |
(TopicPartition(t, partition), OffsetSpec.earliest()) |
| 100 |
for t in topics |
| 101 |
for partition in partitions |
| 102 |
] |
| 103 |
) |
| 104 |
|
| 105 |
fs = admin_client.list_offsets(requests) |
| 106 |
assert all([p.result().offset == 0 for p in fs.values()]) |
| 107 |
delete_index = 0 |
| 108 |
|
| 109 |
for delete_partitions in [ |
| 110 |
|
| 111 |
[TopicPartition(topic, 0, 0)], |
| 112 |
|
| 113 |
[TopicPartition(topic, 0, 1), TopicPartition(topic, 1, 1)], |
| 114 |
|
| 115 |
[TopicPartition(topic, 2, 2), TopicPartition(topic2, 0, 2), |
| 116 |
TopicPartition(topic2, 1, 2), TopicPartition(topic2, 2, 2)], |
| 117 |
]: |
| 118 |
list_offsets_requests = dict([ |
| 119 |
(part, OffsetSpec.earliest()) for part in delete_partitions |
| 120 |
]) |
| 121 |
futmap_delete = admin_client.delete_records(delete_partitions) |
| 122 |
delete_results = [(part, fut.result()) |
| 123 |
for part, fut in futmap_delete.items()] |
| 124 |
futmap_list = admin_client.list_offsets(list_offsets_requests) |
| 125 |
list_results = dict([(part, fut.result()) |
| 126 |
for part, fut in futmap_list.items()]) |
| 127 |
for part, delete_result in delete_results: |
| 128 |
list_result = list_results[part] |
| 129 |
assert isinstance(delete_result, DeletedRecords) |
| 130 |
assert delete_result.low_watermark == list_result.offset |
| 131 |
assert delete_result.low_watermark == delete_index |
| 132 |
delete_index += 1 |
| 133 |
|
| 134 |
|
| 135 |
fs = admin_client.delete_topics(topics) |
| 136 |
for topic, f in fs.items(): |
| 137 |
f.result() |