summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/librdkafka-2.1.0/tests/0106-cgrp_sess_timeout.c
blob: 0451e4a00c62b794635ad5cdb3ca82799f14bf80 (plain)
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
/*
 * librdkafka - Apache Kafka C library
 *
 * Copyright (c) 2020, Magnus Edenhill
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "test.h"

#include "../src/rdkafka_proto.h"


/**
 * @name Verify that the high-level consumer times out itself if
 *       heartbeats are not successful (issue #2631).
 */

static const char *commit_type;
static int rebalance_cnt;
static rd_kafka_resp_err_t rebalance_exp_event;
static rd_kafka_resp_err_t commit_exp_err;

static void rebalance_cb(rd_kafka_t *rk,
                         rd_kafka_resp_err_t err,
                         rd_kafka_topic_partition_list_t *parts,
                         void *opaque) {

        rebalance_cnt++;
        TEST_SAY("Rebalance #%d: %s: %d partition(s)\n", rebalance_cnt,
                 rd_kafka_err2name(err), parts->cnt);

        TEST_ASSERT(
            err == rebalance_exp_event, "Expected rebalance event %s, not %s",
            rd_kafka_err2name(rebalance_exp_event), rd_kafka_err2name(err));

        if (err == RD_KAFKA_RESP_ERR__ASSIGN_PARTITIONS) {
                test_consumer_assign("assign", rk, parts);
        } else {
                rd_kafka_resp_err_t commit_err;

                if (strcmp(commit_type, "auto")) {
                        rd_kafka_resp_err_t perr;

                        TEST_SAY("Performing %s commit\n", commit_type);

                        perr = rd_kafka_position(rk, parts);
                        TEST_ASSERT(!perr, "Failed to acquire position: %s",
                                    rd_kafka_err2str(perr));

                        /* Sleep a short while so the broker times out the
                         * member too. */
                        rd_sleep(1);

                        commit_err = rd_kafka_commit(
                            rk, parts, !strcmp(commit_type, "async"));

                        if (!strcmp(commit_type, "async"))
                                TEST_ASSERT(!commit_err,
                                            "Async commit should not fail, "
                                            "but it returned %s",
                                            rd_kafka_err2name(commit_err));
                        else
                                TEST_ASSERT(
                                    commit_err == commit_exp_err ||
                                        (!commit_exp_err &&
                                         commit_err ==
                                             RD_KAFKA_RESP_ERR__NO_OFFSET),
                                    "Expected %s commit to return %s, "
                                    "not %s",
                                    commit_type,
                                    rd_kafka_err2name(commit_exp_err),
                                    rd_kafka_err2name(commit_err));
                }

                test_consumer_unassign("unassign", rk);
        }

        /* Make sure only one rebalance callback is served per poll()
         * so that expect_rebalance() returns to the test logic on each
         * rebalance. */
        rd_kafka_yield(rk);
}


/**
 * @brief Wait for an expected rebalance event, or fail.
 */
static void expect_rebalance(const char *what,
                             rd_kafka_t *c,
                             rd_kafka_resp_err_t exp_event,
                             int timeout_s) {
        int64_t tmout = test_clock() + (timeout_s * 1000000);
        int start_cnt = rebalance_cnt;

        TEST_SAY("Waiting for %s (%s) for %ds\n", what,
                 rd_kafka_err2name(exp_event), timeout_s);

        rebalance_exp_event = exp_event;

        while (tmout > test_clock() && rebalance_cnt == start_cnt) {
                if (test_consumer_poll_once(c, NULL, 1000))
                        rd_sleep(1);
        }

        if (rebalance_cnt == start_cnt + 1) {
                rebalance_exp_event = RD_KAFKA_RESP_ERR_NO_ERROR;
                return;
        }

        TEST_FAIL("Timed out waiting for %s (%s)\n", what,
                  rd_kafka_err2name(exp_event));
}


/**
 * @brief Verify that session timeouts are handled by the consumer itself.
 *
 * @param use_commit_type "auto", "sync" (manual), "async" (manual)
 */
static void do_test_session_timeout(const char *use_commit_type) {
        const char *bootstraps;
        rd_kafka_mock_cluster_t *mcluster;
        rd_kafka_conf_t *conf;
        rd_kafka_t *c;
        const char *groupid = "mygroup";
        const char *topic   = "test";

        rebalance_cnt = 0;
        commit_type   = use_commit_type;

        SUB_TEST0(!strcmp(use_commit_type, "sync") /*quick*/,
                  "Test session timeout with %s commit", use_commit_type);

        mcluster = test_mock_cluster_new(3, &bootstraps);

        rd_kafka_mock_coordinator_set(mcluster, "group", groupid, 1);

        /* Seed the topic with messages */
        test_produce_msgs_easy_v(topic, 0, 0, 0, 100, 10, "bootstrap.servers",
                                 bootstraps, "batch.num.messages", "10", NULL);

        test_conf_init(&conf, NULL, 30);
        test_conf_set(conf, "bootstrap.servers", bootstraps);
        test_conf_set(conf, "security.protocol", "PLAINTEXT");
        test_conf_set(conf, "group.id", groupid);
        test_conf_set(conf, "session.timeout.ms", "5000");
        test_conf_set(conf, "heartbeat.interval.ms", "1000");
        test_conf_set(conf, "auto.offset.reset", "earliest");
        test_conf_set(conf, "enable.auto.commit",
                      !strcmp(commit_type, "auto") ? "true" : "false");

        c = test_create_consumer(groupid, rebalance_cb, conf, NULL);

        test_consumer_subscribe(c, topic);

        /* Let Heartbeats fail after a couple of successful ones */
        rd_kafka_mock_push_request_errors(
            mcluster, RD_KAFKAP_Heartbeat, 9, RD_KAFKA_RESP_ERR_NO_ERROR,
            RD_KAFKA_RESP_ERR_NO_ERROR, RD_KAFKA_RESP_ERR_NOT_COORDINATOR,
            RD_KAFKA_RESP_ERR_NOT_COORDINATOR,
            RD_KAFKA_RESP_ERR_NOT_COORDINATOR,
            RD_KAFKA_RESP_ERR_NOT_COORDINATOR,
            RD_KAFKA_RESP_ERR_NOT_COORDINATOR,
            RD_KAFKA_RESP_ERR_NOT_COORDINATOR,
            RD_KAFKA_RESP_ERR_NOT_COORDINATOR);

        expect_rebalance("initial assignment", c,
                         RD_KAFKA_RESP_ERR__ASSIGN_PARTITIONS, 5 + 2);

        /* Consume a couple of messages so that we have something to commit */
        test_consumer_poll("consume", c, 0, -1, 0, 10, NULL);

        /* The commit in the rebalance callback should fail when the
         * member has timed out from the group. */
        commit_exp_err = RD_KAFKA_RESP_ERR_UNKNOWN_MEMBER_ID;

        expect_rebalance("session timeout revoke", c,
                         RD_KAFKA_RESP_ERR__REVOKE_PARTITIONS, 2 + 5 + 2);

        expect_rebalance("second assignment", c,
                         RD_KAFKA_RESP_ERR__ASSIGN_PARTITIONS, 5 + 2);

        /* Final rebalance in close().
         * Its commit will work. */
        rebalance_exp_event = RD_KAFKA_RESP_ERR__REVOKE_PARTITIONS;
        commit_exp_err      = RD_KAFKA_RESP_ERR_NO_ERROR;

        test_consumer_close(c);

        rd_kafka_destroy(c);

        test_mock_cluster_destroy(mcluster);

        SUB_TEST_PASS();
}


/**
 * @brief Attempt manual commit when assignment has been lost (#3217)
 */
static void do_test_commit_on_lost(void) {
        const char *bootstraps;
        rd_kafka_mock_cluster_t *mcluster;
        rd_kafka_conf_t *conf;
        rd_kafka_t *c;
        const char *groupid = "mygroup";
        const char *topic   = "test";
        rd_kafka_resp_err_t err;

        SUB_TEST();

        test_curr->is_fatal_cb = test_error_is_not_fatal_cb;

        mcluster = test_mock_cluster_new(3, &bootstraps);

        rd_kafka_mock_coordinator_set(mcluster, "group", groupid, 1);

        /* Seed the topic with messages */
        test_produce_msgs_easy_v(topic, 0, 0, 0, 100, 10, "bootstrap.servers",
                                 bootstraps, "batch.num.messages", "10", NULL);

        test_conf_init(&conf, NULL, 30);
        test_conf_set(conf, "bootstrap.servers", bootstraps);
        test_conf_set(conf, "security.protocol", "PLAINTEXT");
        test_conf_set(conf, "group.id", groupid);
        test_conf_set(conf, "session.timeout.ms", "5000");
        test_conf_set(conf, "heartbeat.interval.ms", "1000");
        test_conf_set(conf, "auto.offset.reset", "earliest");
        test_conf_set(conf, "enable.auto.commit", "false");

        c = test_create_consumer(groupid, test_rebalance_cb, conf, NULL);

        test_consumer_subscribe(c, topic);

        /* Consume a couple of messages so that we have something to commit */
        test_consumer_poll("consume", c, 0, -1, 0, 10, NULL);

        /* Make the coordinator unreachable, this will cause a local session
         * timeout followed by a revoke and assignment lost. */
        rd_kafka_mock_broker_set_down(mcluster, 1);

        /* Wait until the assignment is lost */
        TEST_SAY("Waiting for assignment to be lost...\n");
        while (!rd_kafka_assignment_lost(c))
                rd_sleep(1);

        TEST_SAY("Assignment is lost, committing\n");
        /* Perform manual commit */
        err = rd_kafka_commit(c, NULL, 0 /*sync*/);
        TEST_SAY("commit() returned: %s\n", rd_kafka_err2name(err));
        TEST_ASSERT(err, "expected commit to fail");

        test_consumer_close(c);

        rd_kafka_destroy(c);

        test_mock_cluster_destroy(mcluster);

        test_curr->is_fatal_cb = NULL;

        SUB_TEST_PASS();
}


int main_0106_cgrp_sess_timeout(int argc, char **argv) {

        if (test_needs_auth()) {
                TEST_SKIP("Mock cluster does not support SSL/SASL\n");
                return 0;
        }

        do_test_session_timeout("sync");
        do_test_session_timeout("async");
        do_test_session_timeout("auto");

        do_test_commit_on_lost();

        return 0;
}