-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathclient_brokers_util_materialize.cpp
294 lines (231 loc) · 11.6 KB
/
client_brokers_util_materialize.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// XXX: request_partition_ctx::as_op.response_valid
#include "client_common.h"
bool TankClient::materialize_consume_api_request(api_request *api_req) {
enum {
trace = false,
};
const auto req_id = api_req->request_id;
if (trace) {
SLog("Materializing consume request, ", api_req->ready_partitions_list.size(), " partitions\n");
}
for (auto it : api_req->ready_partitions_list) {
auto req_part = containerof(request_partition_ctx, partitions_list_ll, it);
const auto &resp_ctx = req_part->as_op.consume.response;
const range_base<consumed_msg *, uint32_t> msgs(
const_cast<consumed_msg *>(resp_ctx.msgs.cnt <= sizeof_array(resp_ctx.msgs.list.small)
? resp_ctx.msgs.list.small
: resp_ctx.msgs.list.large),
resp_ctx.msgs.cnt);
if (trace) {
SLog("Got ", resp_ctx.msgs.cnt, " for ", req_part->topic, "/", req_part->partition, "\n");
}
consumed_content.emplace_back(partition_content{
.clientReqId = req_id,
.topic = req_part->topic,
.partition = req_part->partition,
.msgs = msgs,
.respComplete = true,
.drained = resp_ctx.drained,
.next.seqNum = resp_ctx.next.seq_num,
.next.minFetchSize = static_cast<uint32_t>(resp_ctx.next.min_size),
});
// we will not reset req_part->as_op.response_valid to false
// because we not migrate anything to the api_request
}
return true;
}
bool TankClient::materialize_reload_config_request(api_request *api_req) {
if (not api_req->ready_partitions_list.empty()) {
const auto req_part = containerof(request_partition_ctx, partitions_list_ll, api_req->ready_partitions_list.next);
reload_conf_results_v.emplace_back(reload_conf_result{
.clientReqId = api_req->request_id,
.topic = req_part->topic,
.partition = req_part->partition,
});
}
return false;
}
bool TankClient::materialize_create_topic_request(api_request *api_req) {
if (not api_req->ready_partitions_list.empty()) {
const auto req_part = containerof(request_partition_ctx, partitions_list_ll, api_req->ready_partitions_list.next);
created_topics_v.emplace_back(created_topic{
.clientReqId = api_req->request_id,
.topic = req_part->topic,
});
}
return false;
}
bool TankClient::materialize_discover_topology_request(api_request *api_req) {
assert(api_req);
assert(api_req->type == api_request::Type::DiscoverTopology);
if (api_req->ready_partitions_list.empty()) {
return false;
}
const auto req_part = containerof(request_partition_ctx,
partitions_list_ll,
api_req->ready_partitions_list.next);
if (req_part->as_op.response_valid) {
auto all = req_part->as_op.discover_topology.response.all;
_discovered_topologies.emplace_back(discovered_topology{
.client_req_id = api_req->request_id,
.cluster_name = req_part->as_op.discover_topology.response.cluster_name,
.nodes.size = static_cast<uint32_t>(all->size()),
.nodes.data = all->data(),
});
api_req->materialized_resp.discover_topology.v = all;
api_req->materialized_resp.discover_topology._cluster_name_ptr = const_cast<char *>(req_part->as_op.discover_topology.response.cluster_name.p);
req_part->as_op.response_valid = false;
return true; // retain
} else {
return false;
}
}
bool TankClient::materialize_discover_topics_request(api_request *api_req) {
assert(api_req);
if (api_req->ready_partitions_list.empty()) {
return false;
}
// just one, dummy, part context
const auto req_part = containerof(request_partition_ctx,
partitions_list_ll,
api_req->ready_partitions_list.next);
if (req_part->as_op.response_valid) {
auto all = req_part->as_op.discover_topics.response.all;
all_discovered_topics.emplace_back(discovered_srv_topics{
.client_req_id = api_req->request_id,
.topics.size = static_cast<uint32_t>(all->size()),
.topics.data = all->data(),
});
api_req->materialized_resp.discover_topics.v = all;
req_part->as_op.response_valid = false;
return true; // retain
} else {
return false;
}
}
bool TankClient::materialize_srv_request(api_request *api_req) {
if (api_req->ready_partitions_list.empty()) {
return false;
}
const auto req_part = containerof(request_partition_ctx,
partitions_list_ll,
api_req->ready_partitions_list.next);
srv_status s;
s.counts = req_part->as_op.srv_status.counts;
s.metrics = req_part->as_op.srv_status.metrics;
s.startup_ts = req_part->as_op.srv_status.startup_ts;
s.version = req_part->as_op.srv_status.version;
TANK_EXPECT(req_part->as_op.srv_status.cluster_name.len <= sizeof(s.cluster_name.data));
s.cluster_name.len = req_part->as_op.srv_status.cluster_name.len;
memcpy(s.cluster_name.data,
req_part->as_op.srv_status.cluster_name.data,
req_part->as_op.srv_status.cluster_name.len);
collected_cluster_status_v.emplace_back(s);
return false;
}
bool TankClient::materialize_produce_request(api_request *api_req) {
enum {
trace = false,
};
if (trace) {
SLog("To materialize:", api_req->ready_partitions_list.size(), "\n");
}
for (const auto it : api_req->ready_partitions_list) {
const auto req_part = containerof(request_partition_ctx, partitions_list_ll, it);
produce_acks_v.emplace_back(produce_ack{
.clientReqId = api_req->request_id,
.topic = req_part->topic,
.partition = req_part->partition});
}
return false;
}
bool TankClient::materialize_discover_partitions_request(api_request *api_req) {
enum {
trace = false,
};
const auto req_id = api_req->request_id;
if (trace) {
SLog("Materializing discover_partitions request, ", api_req->ready_partitions_list.size(), " partitions\n");
}
// we can have multiple because multiple brokers may have been involved
std::unique_ptr<std::vector<std::pair<uint16_t, std::pair<uint64_t, uint64_t>>>> all;
str_view8 topic;
while (not api_req->ready_partitions_list.empty()) {
auto it = api_req->ready_partitions_list.next;
auto req_part = containerof(request_partition_ctx, partitions_list_ll, it);
if (req_part->as_op.response_valid) {
if (auto a = req_part->as_op.discover_partitions.response.all) {
topic = req_part->topic;
if (not all) {
all.reset(a);
} else {
all->insert(all->end(), a->begin(), a->end());
// no need to keep this around
delete a;
}
}
req_part->as_op.response_valid = false;
}
discard_request_partition_ctx(api_req, req_part);
it->detach_and_reset();
}
if (const size_t n = all ? all->size() : 0) {
// we need to sort here because they may have arrived out of order
std::sort(all->begin(), all->end(), [](const auto &a, const auto &b) noexcept {
return a.first < b.first;
});
if (trace) {
for (const auto &[partition, ctx] : *all) {
SLog(partition, " ", ctx, "\n");
}
}
const auto max = all->back().first;
auto v = static_cast<std::pair<uint64_t, uint64_t> *>(malloc(sizeof(std::pair<uint64_t, uint64_t>) * (max + 1)));
uint16_t partition{0}, i{0};
while (partition <= max) {
if (all->at(i).first == partition) {
v[partition] = all->at(i++).second;
} else {
v[partition] = {0, 0};
}
++partition;
}
all_discovered_partitions.emplace_back(discovered_topic_partitions{
.clientReqId = req_id,
.topic = topic,
.watermarks = {v, static_cast<uint16_t>(max + 1)},
});
api_req->materialized_resp.discover_partitions.v = v;
return true;
}
return false;
}
// Whenever an api_request becomes ready, it is processed to generate
// a response or whatever is appropriate based on the type of request
//
// returns true if the the api_req will be retained to be destroyed by gc_api_request() in the next
// iteration (in begin_reactor_loop_iteration() impl.) as opposed to immediately
bool TankClient::materialize_api_response(api_request *const api_req) {
assert(api_req);
switch (api_req->type) {
case api_request::Type::Consume:
return materialize_consume_api_request(api_req);
case api_request::Type::DiscoverPartitions:
return materialize_discover_partitions_request(api_req);
case api_request::Type::Produce:
case api_request::Type::ProduceWithSeqnum:
return materialize_produce_request(api_req);
case api_request::Type::CreateTopic:
return materialize_create_topic_request(api_req);
case api_request::Type::ReloadConfig:
return materialize_reload_config_request(api_req);
case api_request::Type::SrvStatus:
return materialize_srv_request(api_req);
case api_request::Type::DiscoverTopics:
return materialize_discover_topics_request(api_req);
case api_request::Type::DiscoverTopology:
return materialize_discover_topology_request(api_req);
default:
IMPLEMENT_ME();
}
}