-
Notifications
You must be signed in to change notification settings - Fork 70
/
service_isr.cpp
360 lines (277 loc) · 12.8 KB
/
service_isr.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include "service_common.h"
#ifdef HWM_UPDATE_BASED_ON_ACKS
isr_entry *Service::isr_bind(topic_partition *p, cluster_node *peer, const uint8_t pinid, const uint32_t ref) {
static constexpr bool trace{false};
TANK_EXPECT(p);
TANK_EXPECT(peer);
#ifdef HWM_UPDATE_BASED_ON_ACKS
TANK_EXPECT(pinid < (sizeof(topic_partition::Cluster::PendingClientProduceAcks::pending_ack::isr_nodes_acknowledged_bm) * 8));
#endif
#ifdef TANK_RUNTIME_CHECKS
for (auto it : p->cluster.isr.list) {
auto node = switch_list_entry(isr_entry, partition_ll, it)->node();
TANK_EXPECT(node != peer);
}
#endif
auto isr_e = get_isr_entry();
isr_e->partition_ = p;
isr_e->node_ = peer;
#ifdef HWM_UPDATE_BASED_ON_ACKS
isr_e->partition_isr_node_id = pinid;
#endif
p->cluster.isr.list.push_back(&isr_e->partition_ll);
TANK_EXPECT(static_cast<size_t>(p->cluster.isr.cnt) + 1 < std::numeric_limits<decltype(p->cluster.isr.cnt)>::max());
p->cluster.isr.cnt++;
p->cluster.isr.dirty = true;
peer->isr.list.push_back(&isr_e->node_ll);
peer->isr.cnt++;
TANK_EXPECT(p->cluster.isr.list.size() == p->cluster.isr.cnt);
TANK_EXPECT(peer->isr.list.size() == peer->isr.cnt);
if (trace) {
SLog(ansifmt::color_blue, ansifmt::bgcolor_green, "ISR: adding ", peer->id, "@", peer->ep,
" to ISR of ", p->owner->name(), "/", p->idx, " at ", ref, ansifmt::reset, "\n");
}
#ifdef TANK_RUNTIME_CHECKS
{
std::unordered_set<nodeid_t> s1;
std::unordered_set<cluster_node *> s2;
for (auto it : p->cluster.isr.list) {
auto node = switch_list_entry(isr_entry, partition_ll, it)->node();
TANK_EXPECT(s1.insert(node->id).second);
TANK_EXPECT(s2.insert(node).second);
}
}
#endif
// you are supposed to persist_isr()
TANK_EXPECT(isr_e);
return isr_e;
}
#else
isr_entry *Service::isr_bind(topic_partition *p, cluster_node *peer, const uint32_t ref) {
static constexpr bool trace{false};
TANK_EXPECT(p);
TANK_EXPECT(peer);
#ifdef TANK_RUNTIME_CHECKS
for (auto it : p->cluster.isr.list) {
auto node = switch_list_entry(isr_entry, partition_ll, it)->node();
TANK_EXPECT(node != peer);
}
#endif
auto isr_e = get_isr_entry();
isr_e->partition_ = p;
isr_e->node_ = peer;
p->cluster.isr.list.push_back(&isr_e->partition_ll);
TANK_EXPECT(static_cast<size_t>(p->cluster.isr.cnt) + 1 < std::numeric_limits<decltype(p->cluster.isr.cnt)>::max());
p->cluster.isr.cnt++;
p->cluster.isr.dirty = true;
peer->isr.list.push_back(&isr_e->node_ll);
peer->isr.cnt++;
TANK_EXPECT(p->cluster.isr.list.size() == p->cluster.isr.cnt);
TANK_EXPECT(peer->isr.list.size() == peer->isr.cnt);
if (trace) {
SLog(ansifmt::color_blue, ansifmt::bgcolor_green, "ISR: adding ", peer->id, "@", peer->ep,
" to ISR of ", p->owner->name(), "/", p->idx, " at ", ref, ansifmt::reset, "\n");
}
#ifdef TANK_RUNTIME_CHECKS
{
std::unordered_set<nodeid_t> s1;
std::unordered_set<cluster_node *> s2;
for (auto it : p->cluster.isr.list) {
auto node = switch_list_entry(isr_entry, partition_ll, it)->node();
TANK_EXPECT(s1.insert(node->id).second);
TANK_EXPECT(s2.insert(node).second);
}
}
#endif
// you are supposed to persist_isr()
TANK_EXPECT(isr_e);
return isr_e;
}
#endif
void Service::isr_dispose(isr_entry *isr) {
static constexpr bool trace{false};
TANK_EXPECT(isr);
auto partition = isr->partition();
auto node = isr->node();
TANK_EXPECT(partition);
TANK_EXPECT(node);
TANK_EXPECT(node->isr.cnt);
TANK_EXPECT(!node->isr.list.empty());
TANK_EXPECT(node->isr.cnt);
TANK_EXPECT(!node->isr.list.empty());
TANK_EXPECT(node->isr.cnt);
// make sure we didn't forget this
TANK_EXPECT(isr->pending_next_ack_ll.empty());
isr->node_ll.detach();
node->isr.cnt--;
isr->partition_ll.detach();
partition->cluster.isr.cnt--;
partition->cluster.isr.dirty = true;
TANK_EXPECT(partition->cluster.isr.list.size() == partition->cluster.isr.cnt);
TANK_EXPECT(node->isr.list.size() == node->isr.cnt);
put_isr_entry(isr);
if (trace) {
SLog(ansifmt::color_blue, ansifmt::bgcolor_green, "ISR: removed ", node->id, "@", node->ep,
" from ISR of ", partition->owner->name(), "/", partition->idx, ", now partition ISR size = ", partition->cluster.isr.cnt, ansifmt::reset, "\n");
}
#ifdef TANK_RUNTIME_CHECKS
{
std::unordered_set<nodeid_t> s1;
std::unordered_set<cluster_node *> s2;
// make sure we haven't done that already
for (auto it : partition->cluster.isr.list) {
auto node = switch_list_entry(isr_entry, partition_ll, it)->node();
TANK_EXPECT(s1.insert(node->id).second);
TANK_EXPECT(s2.insert(node).second);
}
}
#endif
#ifndef HWM_UPDATE_BASED_ON_ACKS
rebuild_partition_tracked_isrs(partition);
#endif
// if we are removing a broker from the ISR of a partition, we need to consider if
// the invariants are satisfied for a pending client produce response
consider_pending_client_produce_responses(partition);
}
// called when failed to get a CONSUME request for the last partition message didn't happen in time
void Service::expire_isr_ack(isr_entry *isr) {
TANK_EXPECT(isr);
static constexpr bool trace{false};
auto partition = isr->partition();
auto node = isr->node();
if (trace) {
SLog(ansifmt::inverse, ansifmt::bold, ansifmt::color_green, "Expiring ", node->id, "@", node->ep,
" from ISR of ", partition->owner->name(), "/", partition->idx, ansifmt::reset, "\n");
}
// detach from pending ack.list if not detached already
isr->pending_next_ack_ll.try_detach_and_reset();
// and get rid of this ISR entry
isr_dispose(isr);
persist_isr(partition, __LINE__);
}
void Service::consider_isr_pending_ack() {
while (!isr_pending_ack_list.empty()) {
auto isr_e = switch_list_entry(isr_entry, pending_next_ack_ll, isr_pending_ack_list.prev);
if (isr_e->pending_next_ack_timeout > now_ms) {
break;
}
// took too long for node in an partition's ISR
// to consume past a sequence number
expire_isr_ack(isr_e);
}
isr_pending_ack_list_next = isr_pending_ack_list.empty()
? std::numeric_limits<uint64_t>::max()
: switch_list_entry(isr_entry, pending_next_ack_ll, isr_pending_ack_list.prev)->pending_next_ack_timeout;
}
void Service::persist_isr(topic_partition *p, const uint32_t ref) {
static constexpr bool trace{false};
TANK_EXPECT(p);
TANK_EXPECT(cluster_state.local_node.ref);
TANK_EXPECT(cluster_state.leader_self() || p->cluster.leader.node == cluster_state.local_node.ref);
#ifdef TANK_RUNTIME_CHECKS
{
std::unordered_set<nodeid_t> s1;
std::unordered_set<cluster_node *> s2;
// make sure we haven't done that already
for (auto it : p->cluster.isr.list) {
auto node = switch_list_entry(isr_entry, partition_ll, it)->node();
TANK_EXPECT(s1.insert(node->id).second);
TANK_EXPECT(s2.insert(node).second);
}
}
#endif
if (trace) {
puts("\n");
SLog(ansifmt::bold, ansifmt::color_brown, ansifmt::bgcolor_red, ansifmt::inverse, "Would have persisted ISR of ",
p->owner->name(), "/", p->idx,
" => ", p->cluster.isr.cnt, " ref=", ref, ansifmt::reset, "\n");
for (const auto it : p->cluster.isr.list) {
const auto isr_e = switch_list_entry(isr_entry, partition_ll, it);
SLog(">> ISR NODE ", isr_e->node()->id, "@", isr_e->node()->ep, "\n");
}
}
#ifdef TANK_RUNTIME_CHECKS
{
std::unordered_set<nodeid_t> s;
for (auto it : p->cluster.isr.list) {
auto node = switch_list_entry(isr_entry, partition_ll, it)->node();
TANK_EXPECT(s.insert(node->id).second);
}
}
#endif
// We are going to _defer_ this to the end of the loop
// so that we can collect multiple ISR updates and use a transaction to update them
// thereby reducing the number of requests to consul which shoul improve performance etc
// (if you disable a topic that has 100 partitions, we will need two transactions requests as opposed to 100)
//
// this makes sense because we will likely be updating ISRs very frequently
if (!p->cluster.isr_dirty()) {
p->cluster.flags |= unsigned(topic_partition::Cluster::Flags::ISRDirty);
TANK_EXPECT(p->cluster.isr_dirty());
isr_dirty_list.emplace_back(p);
}
p->cluster.isr.dirty = false;
}
#ifndef HWM_UPDATE_BASED_ON_ACKS
// seqnum would be the LSN of the last message persisted on peer's end, as advertised by the peer
void Service::isr_touch(isr_entry *const isr_e, const uint64_t advertised_lastmsgpersisted_id) {
static constexpr bool trace{false};
TANK_EXPECT(isr_e);
if (trace) {
SLog("touching ISR ent, advertised_lastmsgpersisted_id(", advertised_lastmsgpersisted_id, "), isr_e->last_msg_lsn(", isr_e->last_msg_lsn, ")\n");
}
if (advertised_lastmsgpersisted_id <= isr_e->last_msg_lsn) {
if (trace) {
SLog("No need to update\n");
}
return;
}
isr_e->last_msg_lsn = advertised_lastmsgpersisted_id;
auto p = isr_e->partition();
const auto node = isr_e->node();
const auto nid = node->id;
const auto size = p->cluster.isr.tracker.size;
auto & data = p->cluster.isr.tracker.data;
uint8_t insert_index = 0;
if (trace) {
SLog("Tracking ", size, " (node, lsn) for ", p->owner->name(), "/", p->idx, ", node ", node->id, "@", node->ep, "\n");
}
for (unsigned i = 0; i < size; ++i) {
auto &it = data[i];
if (it.nid == nid) {
if (it.lsn != advertised_lastmsgpersisted_id) {
//sort in place
it.lsn = advertised_lastmsgpersisted_id;
if (trace) {
SLog("Updated and sorting in place\n");
}
while (++i < size && data[i - 1].lsn > data[i].lsn) {
std::swap(data[i - 1], data[i]);
}
if (trace) {
for (unsigned i = 0; i < size; ++i) {
SLog(i, ": ", data[i].nid, " ", data[i].lsn, "\n");
}
}
update_hwmark(p, data[0].lsn);
}
return;
} else if (advertised_lastmsgpersisted_id > it.lsn) {
insert_index = i + 1;
}
}
if (trace) {
SLog("node not tracked, will track\n");
}
TANK_EXPECT(size != sizeof_array(p->cluster.isr.tracker.data));
memmove(data + insert_index + 1, data + insert_index, sizeof(data[0]) * (++(p->cluster.isr.tracker.size) - insert_index));
data[insert_index].nid = nid;
data[insert_index].lsn = advertised_lastmsgpersisted_id;
if (trace) {
for (unsigned i = 0; i < size; ++i) {
SLog(i, ": ", data[i].nid, " ", data[i].lsn, "\n");
}
}
update_hwmark(p, data[0].lsn);
}
#endif