Debian Codegraph @

tests/integration/admin/test_delete_records.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # Copyright 2024 Confluent Inc.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
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 # Create a topic with a single partition
28 topic = kafka_cluster.create_topic_and_wait_propogation("test-del-records",
29 {
30 "num_partitions": 1,
31 "replication_factor": 1,
32 })
33
34 # Create Producer instance
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 # Check if the earliest avilable offset for this topic partition is 0
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 # Delete the records
52 fs1 = admin_client.delete_records([topic_partition_offset])
53
54 # Find the earliest available offset for that specific topic partition after deletion has been done
55 fs2 = admin_client.list_offsets(requests)
56
57 # Check if the earliest available offset is equal to the offset passed to the delete records function
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 # Delete created topic
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 # Create two topics with a single partition
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 # Create Producer instance
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 # Check if the earliest available offset for this topic partition is 0
105 fs = admin_client.list_offsets(requests)
106 assert all([p.result().offset == 0 for p in fs.values()])
107 delete_index = 0
108 # Delete the records
109 for delete_partitions in [
110 # Single partition no deletion
111 [TopicPartition(topic, 0, 0)],
112 # Single topic, two partitions, single record deleted
113 [TopicPartition(topic, 0, 1), TopicPartition(topic, 1, 1)],
114 # Two topics, four partitions, two records deleted
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 # Delete created topics
135 fs = admin_client.delete_topics(topics)
136 for topic, f in fs.items():
137 f.result()