Debian Codegraph @

tests/0151-purge-brokers.c

raw symbols SCIP
1 /*
2 * librdkafka - Apache Kafka C library
3 *
4 * Copyright (c) 2023, Confluent Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "test.h"
30
31 static rd_atomic32_t do_test_remove_then_add_received_terminate;
32 static rd_atomic32_t verification_complete;
33
34 /** @brief Verify that \p expected_broker_ids
35 * and \p actual_broker_ids correspond in
36 * count and value.
37 */
38 static rd_bool_t fetch_metadata_verify_brokers(int32_t *expected_broker_ids,
39 size_t expected_broker_id_cnt,
40 int32_t *actual_broker_ids,
41 size_t actual_broker_id_cnt) {
42 size_t i;
43 if (actual_broker_id_cnt != expected_broker_id_cnt)
44 return rd_false;
45
46 for (i = 0; i < actual_broker_id_cnt; i++) {
47 if (actual_broker_ids[i] != expected_broker_ids[i])
48 return rd_false;
49 }
50 return rd_true;
51 }
52
53 static void fetch_metadata(rd_kafka_t *rk,
54 int32_t *expected_broker_ids,
55 size_t expected_broker_id_cnt,
56 rd_bool_t (*request_metadata_cb)(int action),
57 rd_bool_t (*await_after_action_cb)(int action),
58 int action) {
59 const rd_kafka_metadata_t *md = NULL;
60 rd_kafka_resp_err_t err;
61 size_t actual_broker_id_cnt = 0;
62 int32_t *actual_broker_ids = NULL;
63 size_t i;
64 int timeout_usecs = 10000000;
65 int64_t abs_timeout_us = test_clock() + timeout_usecs;
66 rd_bool_t continue_requesting_metadata = rd_true;
67
68 TEST_SAY("Waiting for up to 10s for metadata update\n");
69
70 /* Trigger Metadata request which will update learned brokers. */
71 do {
72 if (!request_metadata_cb || request_metadata_cb(action)) {
73 err = rd_kafka_metadata(rk, 0, NULL, &md,
74 tmout_multip(5000));
75 if (md) {
76 rd_kafka_metadata_destroy(md);
77 md = NULL;
78 } else if (err != RD_KAFKA_RESP_ERR__TRANSPORT)
79 TEST_ASSERT(!err, "%s", rd_kafka_err2str(err));
80 }
81
82 rd_usleep(100 * 1000, 0);
83
84 RD_IF_FREE(actual_broker_ids, rd_free);
85 actual_broker_ids =
86 rd_kafka_brokers_learned_ids(rk, &actual_broker_id_cnt);
87 continue_requesting_metadata = test_clock() <= abs_timeout_us;
88
89 continue_requesting_metadata =
90 continue_requesting_metadata &&
91 !fetch_metadata_verify_brokers(
92 expected_broker_ids, expected_broker_id_cnt,
93 actual_broker_ids, actual_broker_id_cnt);
94
95 if (await_after_action_cb)
96 continue_requesting_metadata =
97 continue_requesting_metadata ||
98 await_after_action_cb(action);
99
100 } while (continue_requesting_metadata);
101
102 TEST_ASSERT(actual_broker_id_cnt == expected_broker_id_cnt,
103 "expected %" PRIusz " brokers in cache, got %" PRIusz,
104 expected_broker_id_cnt, actual_broker_id_cnt);
105
106 for (i = 0; i < actual_broker_id_cnt; i++) {
107 TEST_ASSERT(actual_broker_ids[i] == expected_broker_ids[i],
108 "expected broker id[%" PRIusz
109 "] to be "
110 "%" PRId32 ", got %" PRId32,
111 i, expected_broker_ids[i], actual_broker_ids[i]);
112 }
113 RD_IF_FREE(actual_broker_ids, rd_free);
114 }
115
116 #define do_test_add_remove_brokers(initial_cluster_size, actions, action_cnt, \
117 expected_broker_ids, expected_brokers_cnt) \
118 do_test_add_remove_brokers0(initial_cluster_size, actions, action_cnt, \
119 expected_broker_ids, expected_brokers_cnt, \
120 NULL, NULL, NULL);
121 /**
122 * @brief Test adding and removing brokers from the mock cluster.
123 * Verify that the client is updated with the new broker list.
124 *
125 * All \p actions are executed in sequence. \p expected_brokers_cnt
126 *
127 * After each action, the client is expected to have the broker
128 * ids in \p expected_broker_ids and the count to be
129 * \p expected_brokers_cnt .
130 *
131 * @param initial_cluster_size Initial number of brokers in the cluster.
132 * @param actions Array of actions to perform. Each action is a pair
133 * (action,broker id). 0 to remove, 1 to add,
134 * 2 to set down, 3 to set up.
135 * @param expected_broker_ids Array of broker ids expected after each
136 * action.
137 * @param expected_broker_ids_cnt Number of elements in
138 * \p expected_broker_ids .
139 * @param expected_brokers_cnt Array of expected broker count after each
140 * action.
141 * @param await_verification If `rd_false`, the verification is
142 * done only after last action.
143 */
144 #define TEST_ACTION_REMOVE_BROKER 0
145 #define TEST_ACTION_ADD_BROKER 1
146 #define TEST_ACTION_SET_DOWN_BROKER 2
147 #define TEST_ACTION_SET_UP_BROKER 3
148 #define TEST_ACTION_SET_GROUP_COORDINATOR 4
149 #define TEST_GROUP "topic1"
150 static void do_test_add_remove_brokers0(
151 int32_t initial_cluster_size,
152 int32_t actions[][2],
153 size_t action_cnt,
154 int32_t expected_broker_ids[][5],
155 int32_t expected_brokers_cnt[],
156 rd_kafka_type_t (*edit_configuration_cb)(rd_kafka_conf_t *conf),
157 rd_bool_t (*request_metadata_cb)(int action),
158 rd_bool_t (*await_after_action_cb)(int action)) {
159 rd_kafka_mock_cluster_t *cluster;
160 const char *bootstraps;
161 rd_kafka_conf_t *conf;
162 rd_kafka_t *rk;
163 size_t action = 0;
164
165 cluster = test_mock_cluster_new(initial_cluster_size, &bootstraps);
166
167 test_conf_init(&conf, NULL, 100);
168
169 test_conf_set(conf, "bootstrap.servers", bootstraps);
170 test_conf_set(conf, "topic.metadata.refresh.interval.ms", "1000");
171 rd_kafka_type_t type = RD_KAFKA_CONSUMER;
172 if (edit_configuration_cb)
173 type = edit_configuration_cb(conf);
174
175 if (type == RD_KAFKA_CONSUMER)
176 test_conf_set(conf, "group.id", TEST_GROUP);
177
178 rk = test_create_handle(type, conf);
179
180 if (type == RD_KAFKA_CONSUMER)
181 test_consumer_subscribe(rk, TEST_GROUP);
182
183 /* Create a new topic to trigger partition reassignment */
184 rd_kafka_mock_topic_create(cluster, TEST_GROUP, 3,
185 initial_cluster_size);
186
187 /* Verify state zero is reached */
188 fetch_metadata(rk, expected_broker_ids[0], expected_brokers_cnt[0],
189 request_metadata_cb, await_after_action_cb, 0);
190
191 for (action = 0; action < action_cnt; action++) {
192 /* action: N, state: N+1 */
193 int next_state = action + 1;
194 int32_t action_type = actions[action][0];
195 int32_t broker_id = actions[action][1];
196 TEST_SAY("Executing action %zu\n", action + 1);
197 switch (action_type) {
198 case TEST_ACTION_REMOVE_BROKER:
199 TEST_SAY("Removing broker %" PRId32 "\n", broker_id);
200 TEST_ASSERT(rd_kafka_mock_broker_decommission(
201 cluster, broker_id) == 0,
202 "Failed to remove broker from cluster");
203 break;
204
205 case TEST_ACTION_ADD_BROKER:
206 TEST_SAY("Adding broker %" PRId32 "\n", broker_id);
207 TEST_ASSERT(
208 rd_kafka_mock_broker_add(cluster, broker_id) == 0,
209 "Failed to add broker to cluster");
210 break;
211
212 case TEST_ACTION_SET_DOWN_BROKER:
213 TEST_SAY("Setting down broker %" PRId32 "\n",
214 broker_id);
215 TEST_ASSERT(rd_kafka_mock_broker_set_down(
216 cluster, broker_id) == 0,
217 "Failed to set broker %" PRId32 " down",
218 broker_id);
219 break;
220
221 case TEST_ACTION_SET_UP_BROKER:
222 TEST_SAY("Setting up broker %" PRId32 "\n", broker_id);
223 TEST_ASSERT(rd_kafka_mock_broker_set_up(cluster,
224 broker_id) == 0,
225 "Failed to set broker %" PRId32 " up",
226 broker_id);
227 break;
228 case TEST_ACTION_SET_GROUP_COORDINATOR:
229 TEST_ASSERT(
230 rd_kafka_mock_coordinator_set(
231 cluster, "group", TEST_GROUP, broker_id) == 0,
232 "Failed to set group coordinator "
233 "to %" PRId32,
234 broker_id);
235 break;
236 default:
237 break;
238 }
239
240 fetch_metadata(rk, expected_broker_ids[next_state],
241 expected_brokers_cnt[next_state],
242 request_metadata_cb, await_after_action_cb,
243 action);
244 }
245 TEST_SAY("Test verification complete\n");
246 rd_atomic32_set(&verification_complete, 1);
247
248 rd_kafka_destroy(rk);
249 test_mock_cluster_destroy(cluster);
250 }
251
252 /**
253 * @brief Test replacing the brokers in the mock cluster with new ones.
254 * At each step a majority of brokers are returned by the Metadata call.
255 * At the end all brokers from the old cluster are removed.
256 */
257 static void do_test_replace_with_new_cluster(void) {
258 SUB_TEST_QUICK();
259
260 int32_t expected_brokers_cnt[] = {3, 3, 2, 3, 2, 3, 3, 2, 3};
261
262 int32_t expected_broker_ids[][5] = {{1, 2, 3}, {1, 2, 3}, {2, 3},
263 {2, 3, 4}, {3, 4}, {3, 4, 5},
264 {3, 4, 5}, {4, 5}, {4, 5, 6}};
265
266 int32_t actions[][2] = {
267 {TEST_ACTION_SET_GROUP_COORDINATOR, 3},
268 {TEST_ACTION_REMOVE_BROKER, 1},
269 {TEST_ACTION_ADD_BROKER, 4},
270 {TEST_ACTION_REMOVE_BROKER, 2},
271 {TEST_ACTION_ADD_BROKER, 5},
272 {TEST_ACTION_SET_GROUP_COORDINATOR, 5},
273 {TEST_ACTION_REMOVE_BROKER, 3},
274 {TEST_ACTION_ADD_BROKER, 6},
275 };
276
277 do_test_add_remove_brokers(3, actions, RD_ARRAY_SIZE(actions),
278 expected_broker_ids, expected_brokers_cnt);
279
280 SUB_TEST_PASS();
281 }
282
283 /**
284 * @brief Test setting down all brokers from the mock cluster,
285 * simulating a correct cluster roll that never sets down the majority
286 * of brokers.
287 *
288 * The effect is similar to decommissioning the brokers. Partition
289 * reassignment is not triggered in this case but they are not announced
290 * anymore by the Metadata response.
291 */
292 static void do_test_cluster_roll(void) {
293 SUB_TEST_QUICK();
294
295 int32_t expected_brokers_cnt[] = {5, 5, 4, 3, 4, 3, 4,
296 3, 4, 4, 3, 4, 5};
297
298 int32_t expected_broker_ids[][5] = {
299 {1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}, {2, 3, 4, 5}, {3, 4, 5},
300 {1, 3, 4, 5}, {1, 4, 5}, {1, 2, 4, 5}, {1, 2, 5},
301 {1, 2, 3, 5}, {1, 2, 3, 5}, {1, 2, 3}, {1, 2, 3, 4},
302 {1, 2, 3, 4, 5}};
303
304 int32_t actions[][2] = {
305 {TEST_ACTION_SET_GROUP_COORDINATOR, 5},
306 {TEST_ACTION_SET_DOWN_BROKER, 1},
307 {TEST_ACTION_SET_DOWN_BROKER, 2},
308 {TEST_ACTION_SET_UP_BROKER, 1},
309 {TEST_ACTION_SET_DOWN_BROKER, 3},
310 {TEST_ACTION_SET_UP_BROKER, 2},
311 {TEST_ACTION_SET_DOWN_BROKER, 4},
312 {TEST_ACTION_SET_UP_BROKER, 3},
313 {TEST_ACTION_SET_GROUP_COORDINATOR, 1},
314 {TEST_ACTION_SET_DOWN_BROKER, 5},
315 {TEST_ACTION_SET_UP_BROKER, 4},
316 {TEST_ACTION_SET_UP_BROKER, 5},
317 };
318
319 do_test_add_remove_brokers(5, actions, RD_ARRAY_SIZE(actions),
320 expected_broker_ids, expected_brokers_cnt);
321
322 SUB_TEST_PASS();
323 }
324
325 /**
326 * @brief Log callback that waits for the TERMINATE op to be received
327 */
328 static void do_test_remove_then_add_log_cb(const rd_kafka_t *rk,
329 int level,
330 const char *fac,
331 const char *buf) {
332 if (!rd_atomic32_get(&do_test_remove_then_add_received_terminate) &&
333 strstr(buf, "/1: Handle terminates in state")) {
334 rd_atomic32_set(&do_test_remove_then_add_received_terminate, 1);
335 while (!rd_atomic32_get(&verification_complete))
336 rd_usleep(100 * 1000, 0);
337 }
338 }
339
340 /**
341 * @brief Await for the TERMINATE op to be received after the action
342 * that removes the broker then proceed to
343 * add the broker again.
344 */
345 static rd_bool_t do_test_remove_then_add_await_after_action_cb(int action) {
346 /* Second action */
347 if (action == 1) {
348 /* Wait until TERMINATE is received */
349 return !rd_atomic32_get(
350 &do_test_remove_then_add_received_terminate);
351 }
352 return rd_false;
353 }
354
355 /**
356 * @brief Disable sparse connections to increase likely of problems
357 * when the decommisioned broker is re-connecting.
358 * Add a pause after receiving the TERMINATE op to allow to
359 * proceed with adding it again before it's decommissioned.
360 */
361 static test_conf_log_interceptor_t *log_interceptor;
362 static rd_kafka_type_t
363 do_test_remove_then_add_edit_configuration_cb(rd_kafka_conf_t *conf) {
364 const char *debug_contexts[2] = {"broker", NULL};
365
366 /* This timeout verifies that the correct brokers are returned
367 * without duplicates as soon as possible. */
368 test_timeout_set(6);
369 /* Hidden property that forces connections to all brokers,
370 * increasing likelyhood of wrong behaviour if the decommissioned broker
371 * starts re-connecting. */
372 test_conf_set(conf, "enable.sparse.connections", "false");
373 log_interceptor = test_conf_set_log_interceptor(
374 conf, do_test_remove_then_add_log_cb, debug_contexts);
375
376 return RD_KAFKA_CONSUMER;
377 }
378
379 /**
380 * @brief Test setting down one broker and then adding it again
381 * while it's still being decommissioned.
382 *
383 * This should not leave dangling references that prevent broker
384 * destruction.
385 */
386 static void do_test_remove_then_add(void) {
387 SUB_TEST_QUICK();
388 rd_atomic32_init(&do_test_remove_then_add_received_terminate, 0);
389 rd_atomic32_init(&verification_complete, 0);
390
391 int32_t expected_brokers_cnt[] = {3, 3, 2, 3};
392
393 int32_t expected_broker_ids[][5] = {
394 {1, 2, 3}, {1, 2, 3}, {2, 3}, {1, 2, 3}};
395
396 int32_t actions[][2] = {
397 {TEST_ACTION_SET_GROUP_COORDINATOR, 3},
398 {TEST_ACTION_REMOVE_BROKER, 1},
399 {TEST_ACTION_ADD_BROKER, 1},
400 };
401
402 do_test_add_remove_brokers0(
403 3, actions, RD_ARRAY_SIZE(actions), expected_broker_ids,
404 expected_brokers_cnt, do_test_remove_then_add_edit_configuration_cb,
405 NULL, do_test_remove_then_add_await_after_action_cb);
406
407 rd_free(log_interceptor);
408 log_interceptor = NULL;
409 SUB_TEST_PASS();
410 }
411
412 static rd_atomic32_t
413 do_test_down_then_up_no_rebootstrap_loop_rebootstrap_sequence_cnt;
414
415 /**
416 * @brief Log callback that counts numer of rebootstrap sequences received.
417 */
418 static void
419 do_test_down_then_up_no_rebootstrap_loop_log_cb(const rd_kafka_t *rk,
420 int level,
421 const char *fac,
422 const char *buf) {
423 if (strstr(buf, "Starting re-bootstrap sequence")) {
424 rd_atomic32_add(
425 &do_test_down_then_up_no_rebootstrap_loop_rebootstrap_sequence_cnt,
426 1);
427 }
428 }
429
430 /**
431 * @brief Sets the logs callback to the log interceptor.
432 */
433 static rd_kafka_type_t
434 do_test_down_then_up_no_rebootstrap_loop_edit_configuration_cb(
435 rd_kafka_conf_t *conf) {
436 const char *debug_contexts[2] = {"generic", NULL};
437
438 log_interceptor = test_conf_set_log_interceptor(
439 conf, do_test_down_then_up_no_rebootstrap_loop_log_cb,
440 debug_contexts);
441 return RD_KAFKA_PRODUCER;
442 }
443
444 /**
445 * @brief After action 1 the broker is set down.
446 * Don't await for metadata update.
447 */
448 static rd_bool_t
449 do_test_down_then_up_no_rebootstrap_loop_request_metadata_cb(int action) {
450 return action != 1;
451 }
452
453 /**
454 * @brief Await 5s after setting up the broker down
455 * to check for re-bootstrap sequences.
456 */
457 static rd_bool_t
458 do_test_down_then_up_no_rebootstrap_loop_await_after_action_cb(int action) {
459 if (action == 1) {
460 rd_sleep(6);
461 }
462 return rd_false;
463 }
464
465 /**
466 * @brief Test setting down a broker and then setting it up again.
467 * It shouldn't cause a loop of re-bootstrap sequences.
468 */
469 static void do_test_down_then_up_no_rebootstrap_loop(void) {
470 SUB_TEST_QUICK();
471 rd_atomic32_init(
472 &do_test_down_then_up_no_rebootstrap_loop_rebootstrap_sequence_cnt,
473 0);
474
475 int32_t expected_brokers_cnt[] = {1, 1, 1, 1};
476
477 int32_t expected_broker_ids[][5] = {{1}, {1}, {1}, {1}};
478
479 int32_t actions[][2] = {
480 {TEST_ACTION_SET_UP_BROKER, 1},
481 {TEST_ACTION_SET_DOWN_BROKER, 1},
482 {TEST_ACTION_SET_UP_BROKER, 1},
483 };
484
485 do_test_add_remove_brokers0(
486 1, actions, RD_ARRAY_SIZE(actions), expected_broker_ids,
487 expected_brokers_cnt,
488 do_test_down_then_up_no_rebootstrap_loop_edit_configuration_cb,
489 do_test_down_then_up_no_rebootstrap_loop_request_metadata_cb,
490 do_test_down_then_up_no_rebootstrap_loop_await_after_action_cb);
491
492 /* With a rebootstrap every time the bootstrap brokers are removed
493 * we get 6 re-bootstrap sequences.
494 * With the fix we require connection to all learned brokers before
495 * reaching all brokers down again.
496 * In this case we have to connect to the bootstrap broker
497 * and the learned broker, 2s in the slowest case as it depends
498 * on periodic 10s brokers refresh timer too.
499 * We expect 5 or less re-bootstrap sequences. */
500 TEST_ASSERT(
501 rd_atomic32_get(
502 &do_test_down_then_up_no_rebootstrap_loop_rebootstrap_sequence_cnt) <=
503 5,
504 "Expected <= 5 re-bootstrap sequences, got %d",
505 rd_atomic32_get(
506 &do_test_down_then_up_no_rebootstrap_loop_rebootstrap_sequence_cnt));
507
508 rd_free(log_interceptor);
509 log_interceptor = NULL;
510 SUB_TEST_PASS();
511 }
512
513 /**
514 * @brief Test for the mock cluster to ensure there are no problems with
515 * other tests in case they're adding a broker with the same id
516 * as an existing one.
517 */
518 static void do_test_add_same_broker_id(void) {
519 rd_kafka_mock_cluster_t *cluster;
520 const char *bootstraps;
521 rd_kafka_resp_err_t err;
522
523 SUB_TEST_QUICK();
524
525 cluster = test_mock_cluster_new(1, &bootstraps);
526 TEST_SAY("Broker 1 was present from the start, should fail\n");
527 err = rd_kafka_mock_broker_add(cluster, 1);
528 TEST_ASSERT(err == RD_KAFKA_RESP_ERR__INVALID_ARG,
529 "Expected error %s, got %s",
530 rd_kafka_err2str(RD_KAFKA_RESP_ERR__INVALID_ARG),
531 rd_kafka_err2str(err));
532
533 TEST_SAY("Broker 2 should be added\n");
534 TEST_CALL_ERR__(rd_kafka_mock_broker_add(cluster, 2));
535
536 TEST_SAY("Broker 2 cannot be added two times\n");
537 err = rd_kafka_mock_broker_add(cluster, 2);
538 TEST_ASSERT(err == RD_KAFKA_RESP_ERR__INVALID_ARG,
539 "Expected error %s, got %s",
540 rd_kafka_err2str(RD_KAFKA_RESP_ERR__INVALID_ARG),
541 rd_kafka_err2str(err));
542
543 test_mock_cluster_destroy(cluster);
544
545 SUB_TEST_PASS();
546 }
547
548 int main_0151_purge_brokers_mock(int argc, char **argv) {
549
550 if (test_needs_auth()) {
551 TEST_SKIP("Mock cluster does not support SSL/SASL\n");
552 return 0;
553 }
554
555 do_test_add_same_broker_id();
556
557 do_test_replace_with_new_cluster();
558
559 do_test_cluster_roll();
560
561 do_test_remove_then_add();
562
563 do_test_down_then_up_no_rebootstrap_loop();
564
565 return 0;
566 }