Debian Codegraph @

tests/integration/consumer/test_cooperative_rebalance_1.py

raw symbols SCIP
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright 2020 Confluent Inc.
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 import pytest
19 import time
20 from uuid import uuid1
21
22 from confluent_kafka import KafkaException, KafkaError
23
24
25 def test_cooperative_rebalance_1(kafka_cluster):
26 """
27 Test cooperative-sticky assignor and various aspects
28 of the incremental rebalancing API.
29 """
30
31 consumer_conf = {'group.id': str(uuid1()),
32 'partition.assignment.strategy': 'cooperative-sticky',
33 'enable.auto.commit': 'false',
34 'auto.offset.reset': 'earliest',
35 'heartbeat.interval.ms': '2000',
36 'session.timeout.ms': '6000', # minimum allowed by broker
37 'max.poll.interval.ms': '6500'}
38
39 class RebalanceState:
40 def __init__(self):
41 self.assign_count = 0
42 self.revoke_count = 0
43 self.lost_count = 0
44
45 def on_assign(self, consumer, partitions):
46 self.assign_count += 1
47 if self.assign_count == 3:
48 # Assigning both partitions again after assignment lost
49 # due to max poll interval timeout exceeded
50 assert 2 == len(partitions)
51 else:
52 # Incremental assign cases
53 assert 1 == len(partitions)
54
55 def on_revoke(self, consumer, partitions):
56 self.revoke_count += 1
57
58 def on_lost(self, consumer, partitions):
59 self.lost_count += 1
60
61 reb = RebalanceState()
62
63 topic1 = kafka_cluster.create_topic_and_wait_propogation("topic1")
64 topic2 = kafka_cluster.create_topic_and_wait_propogation("topic2")
65
66 consumer = kafka_cluster.consumer(consumer_conf)
67
68 kafka_cluster.seed_topic(topic1, value_source=[b'a'])
69
70 consumer.subscribe([topic1],
71 on_assign=reb.on_assign,
72 on_revoke=reb.on_revoke,
73 on_lost=reb.on_lost)
74 msg = consumer.poll(10)
75 assert msg is not None
76 assert msg.value() == b'a'
77
78 # Subscribe to a second one partition topic, the second assign
79 # call should be incremental (checked in the handler).
80 consumer.subscribe([topic1, topic2],
81 on_assign=reb.on_assign,
82 on_revoke=reb.on_revoke,
83 on_lost=reb.on_lost)
84
85 kafka_cluster.seed_topic(topic2, value_source=[b'b'])
86 msg2 = consumer.poll(10)
87 assert msg2 is not None
88 assert msg2.value() == b'b'
89
90 assert 2 == reb.assign_count
91 assert 0 == reb.lost_count
92 assert 0 == reb.revoke_count
93
94 msg3 = consumer.poll(1)
95 assert msg3 is None
96
97 # Exceed MaxPollIntervalMs => lost partitions.
98 time.sleep(8)
99
100 # Poll again to raise the max.poll error.
101 with pytest.raises(KafkaException) as e:
102 consumer.poll(1)
103 assert e.value.args[0].code() == KafkaError._MAX_POLL_EXCEEDED
104
105 # And poll again to trigger rebalance callbacks
106 # It will trigger on_lost and then on_assign during rejoin
107 msg4 = consumer.poll(10)
108 assert msg4 is not None # Reading messages again after rejoin
109
110 assert 3 == reb.assign_count
111 assert 1 == reb.lost_count
112 assert 0 == reb.revoke_count
113
114 consumer.close()