@@ -13,6 +13,7 @@ using std::bind;
13
13
using std::runtime_error;
14
14
using std::make_tuple;
15
15
using std::tie;
16
+ using std::get;
16
17
using std::lock_guard;
17
18
using std::unique_lock;
18
19
using std::mutex;
@@ -47,11 +48,22 @@ void ConsumerMock::subscribe(const vector<string>& topics) {
47
48
consumer_id_,
48
49
topics,
49
50
bind (&ConsumerMock::on_assignment, this , _1),
50
- bind (&ConsumerMock::on_revocation, this , _1 ),
51
+ bind (&ConsumerMock::on_revocation, this ),
51
52
bind (&ConsumerMock::on_message, this , _1, _2, _3, _4)
52
53
);
53
54
}
54
55
56
+ void ConsumerMock::assign (const vector<TopicPartitionMock>& topic_partitions) {
57
+ for (const TopicPartitionMock& topic_partition : topic_partitions) {
58
+ handle_assign (topic_partition);
59
+ }
60
+ }
61
+
62
+ void ConsumerMock::unassign () {
63
+ lock_guard<mutex> _ (assigned_partitions_mutex_);
64
+ assigned_partitions_.clear ();
65
+ }
66
+
55
67
void ConsumerMock::set_opaque (void * opaque) {
56
68
opaque_ = opaque;
57
69
}
@@ -60,18 +72,23 @@ ConsumerMock::TopicPartitionId ConsumerMock::make_id(const TopicPartitionMock& t
60
72
return make_tuple (topic_partition.get_topic (), topic_partition.get_partition ());
61
73
}
62
74
63
- void ConsumerMock::on_assignment (vector<TopicPartitionMock>& topic_partitions) {
75
+ void ConsumerMock::on_assignment (const vector<TopicPartitionMock>& topic_partitions) {
64
76
handle_rebalance (RD_KAFKA_RESP_ERR__ASSIGN_PARTITIONS, topic_partitions);
65
- for (const TopicPartitionMock& topic_partition : topic_partitions) {
66
- handle_assign (topic_partition);
67
- }
68
77
}
69
78
70
- void ConsumerMock::on_revocation (const vector<TopicPartitionMock>& topic_partitions) {
79
+ void ConsumerMock::on_revocation () {
80
+ // Fetch and reset all assigned topic partitions
81
+ vector<TopicPartitionMock> topic_partitions = [&]() {
82
+ lock_guard<mutex> _ (assigned_partitions_mutex_);
83
+ vector<TopicPartitionMock> output;
84
+ for (const auto & topic_partition_pair : assigned_partitions_) {
85
+ const TopicPartitionId& id = topic_partition_pair.first ;
86
+ output.emplace_back (get<0 >(id), get<1 >(id));
87
+ }
88
+ assigned_partitions_.clear ();
89
+ return output;
90
+ }();
71
91
handle_rebalance (RD_KAFKA_RESP_ERR__REVOKE_PARTITIONS, topic_partitions);
72
- for (const TopicPartitionMock& topic_partition : topic_partitions) {
73
- handle_unassign (topic_partition);
74
- }
75
92
}
76
93
77
94
void ConsumerMock::on_message (const string& topic_name, unsigned partition, uint64_t offset,
@@ -91,8 +108,8 @@ void ConsumerMock::on_message(const string& topic_name, unsigned partition, uint
91
108
message_queue_condition_.notify_one ();
92
109
}
93
110
94
- template < typename List>
95
- void ConsumerMock::handle_rebalance ( rd_kafka_resp_err_t type, List & topic_partitions) {
111
+ void ConsumerMock::handle_rebalance ( rd_kafka_resp_err_t type,
112
+ const vector<TopicPartitionMock> & topic_partitions) {
96
113
auto rebalance_callback = config_.get_rebalance_callback ();
97
114
if (rebalance_callback) {
98
115
auto handle = to_rdkafka_handle (topic_partitions);
@@ -115,14 +132,8 @@ void ConsumerMock::handle_assign(const TopicPartitionMock& topic_partition) {
115
132
116
133
// Fetch any existing messages and push them to the available message queue
117
134
auto & cluster = get_cluster ();
118
- cluster.acquire_topic (topic_partition.get_topic (), [&](KafkaTopicMock& topic) {
119
- fetch_existing_messages (topic_partition.get_partition (), next_offset, topic);
120
- });
121
- }
122
-
123
- void ConsumerMock::handle_unassign (const TopicPartitionMock& topic_partition) {
124
- lock_guard<mutex> _ (assigned_partitions_mutex_);
125
- assigned_partitions_.erase (make_id (topic_partition));
135
+ KafkaTopicMock& topic = cluster.get_topic (topic_partition.get_topic ());
136
+ fetch_existing_messages (topic_partition.get_partition (), next_offset, topic);
126
137
}
127
138
128
139
void ConsumerMock::fetch_existing_messages (unsigned partition_index, uint64_t next_offset,
0 commit comments