summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/librdkafka-2.1.0/tests/0048-partitioner.c
blob: 84efee7dbd3b7018aff0e9d7604385e0271edac4 (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
/*
 * librdkafka - Apache Kafka C library
 *
 * Copyright (c) 2012-2015, 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 "rdkafka.h"

#include <stdarg.h>

/**
 * Various partitioner tests
 *
 * - Issue #797 - deadlock on failed partitioning
 * - Verify that partitioning works across partitioners.
 */

int32_t my_invalid_partitioner(const rd_kafka_topic_t *rkt,
                               const void *keydata,
                               size_t keylen,
                               int32_t partition_cnt,
                               void *rkt_opaque,
                               void *msg_opaque) {
        int32_t partition = partition_cnt + 10;
        TEST_SAYL(4, "partition \"%.*s\" to %" PRId32 "\n", (int)keylen,
                  (const char *)keydata, partition);
        return partition;
}


/* FIXME: This doesn't seem to trigger the bug in #797.
 *        Still a useful test though. */
static void do_test_failed_partitioning(void) {
        rd_kafka_t *rk;
        rd_kafka_conf_t *conf;
        rd_kafka_topic_t *rkt;
        rd_kafka_topic_conf_t *tconf;
        const char *topic = test_mk_topic_name(__FUNCTION__, 1);
        int i;
        int msgcnt = test_quick ? 100 : 10000;

        test_conf_init(&conf, &tconf, 0);
        rd_kafka_conf_set_dr_msg_cb(conf, test_dr_msg_cb);
        test_conf_set(conf, "sticky.partitioning.linger.ms", "0");
        rk = test_create_handle(RD_KAFKA_PRODUCER, conf);

        rd_kafka_topic_conf_set_partitioner_cb(tconf, my_invalid_partitioner);
        test_topic_conf_set(tconf, "message.timeout.ms",
                            tsprintf("%d", tmout_multip(10000)));
        rkt = rd_kafka_topic_new(rk, topic, tconf);
        TEST_ASSERT(rkt != NULL, "%s", rd_kafka_err2str(rd_kafka_last_error()));

        /* Produce some messages (to p 0) to create topic */
        test_produce_msgs(rk, rkt, 0, 0, 0, 2, NULL, 0);

        /* Now use partitioner */
        for (i = 0; i < msgcnt; i++) {
                rd_kafka_resp_err_t err = RD_KAFKA_RESP_ERR_NO_ERROR;
                if (rd_kafka_produce(rkt, RD_KAFKA_PARTITION_UA, 0, NULL, 0,
                                     NULL, 0, NULL) == -1)
                        err = rd_kafka_last_error();
                if (err != RD_KAFKA_RESP_ERR__UNKNOWN_PARTITION)
                        TEST_FAIL(
                            "produce(): "
                            "Expected UNKNOWN_PARTITION, got %s\n",
                            rd_kafka_err2str(err));
        }
        test_flush(rk, 5000);

        rd_kafka_topic_destroy(rkt);
        rd_kafka_destroy(rk);
}


static void part_dr_msg_cb(rd_kafka_t *rk,
                           const rd_kafka_message_t *rkmessage,
                           void *opaque) {
        int32_t *partp = rkmessage->_private;
        int *remainsp  = opaque;

        if (rkmessage->err) {
                /* Will fail later */
                TEST_WARN("Delivery failed: %s\n",
                          rd_kafka_err2str(rkmessage->err));
                *partp = -1;
        } else {
                *partp = rkmessage->partition;
        }

        (*remainsp)--;
}

/**
 * @brief Test single \p partitioner
 */
static void do_test_partitioner(const char *topic,
                                const char *partitioner,
                                int msgcnt,
                                const char **keys,
                                const int32_t *exp_part) {
        rd_kafka_t *rk;
        rd_kafka_conf_t *conf;
        int i;
        int32_t *parts;
        int remains = msgcnt;
        int randcnt = 0;
        int fails   = 0;

        TEST_SAY(_C_MAG "Test partitioner \"%s\"\n", partitioner);

        test_conf_init(&conf, NULL, 30);
        rd_kafka_conf_set_opaque(conf, &remains);
        rd_kafka_conf_set_dr_msg_cb(conf, part_dr_msg_cb);
        test_conf_set(conf, "partitioner", partitioner);
        test_conf_set(conf, "sticky.partitioning.linger.ms", "0");

        rk = test_create_handle(RD_KAFKA_PRODUCER, conf);

        parts = malloc(msgcnt * sizeof(*parts));
        for (i = 0; i < msgcnt; i++)
                parts[i] = -1;

        /*
         * Produce messages
         */
        for (i = 0; i < msgcnt; i++) {
                rd_kafka_resp_err_t err;

                err = rd_kafka_producev(
                    rk, RD_KAFKA_V_TOPIC(topic),
                    RD_KAFKA_V_KEY(keys[i], keys[i] ? strlen(keys[i]) : 0),
                    RD_KAFKA_V_OPAQUE(&parts[i]), RD_KAFKA_V_END);
                TEST_ASSERT(!err, "producev() failed: %s",
                            rd_kafka_err2str(err));

                randcnt += exp_part[i] == -1;
        }

        rd_kafka_flush(rk, tmout_multip(10000));

        TEST_ASSERT(remains == 0, "Expected remains=%d, not %d for %d messages",
                    0, remains, msgcnt);

        /*
         * Verify produced partitions to expected partitions.
         */

        /* First look for produce failures */
        for (i = 0; i < msgcnt; i++) {
                if (parts[i] == -1) {
                        TEST_WARN("Message #%d (exp part %" PRId32
                                  ") "
                                  "was not successfully produced\n",
                                  i, exp_part[i]);
                        fails++;
                }
        }

        TEST_ASSERT(!fails, "See %d previous failure(s)", fails);


        if (randcnt == msgcnt) {
                /* If all expected partitions are random make sure
                 * the produced partitions have some form of
                 * random distribution */
                int32_t last_part = parts[0];
                int samecnt       = 0;

                for (i = 0; i < msgcnt; i++) {
                        samecnt += parts[i] == last_part;
                        last_part = parts[i];
                }

                TEST_ASSERT(samecnt < msgcnt,
                            "No random distribution, all on partition %" PRId32,
                            last_part);
        } else {
                for (i = 0; i < msgcnt; i++) {
                        if (exp_part[i] != -1 && parts[i] != exp_part[i]) {
                                TEST_WARN(
                                    "Message #%d expected partition "
                                    "%" PRId32 " but got %" PRId32 ": %s\n",
                                    i, exp_part[i], parts[i], keys[i]);
                                fails++;
                        }
                }


                TEST_ASSERT(!fails, "See %d previous failure(s)", fails);
        }

        free(parts);

        rd_kafka_destroy(rk);

        TEST_SAY(_C_GRN "Test partitioner \"%s\": PASS\n", partitioner);
}

extern uint32_t rd_crc32(const char *, size_t);

/**
 * @brief Test all builtin partitioners
 */
static void do_test_partitioners(void) {
        int part_cnt = test_quick ? 7 : 17;
#define _MSG_CNT 5
        const char *unaligned = "123456";
        /* Message keys */
        const char *keys[_MSG_CNT] = {
            NULL,
            "",  // empty
            unaligned + 1,
            "this is another string with more length to it perhaps", "hejsan"};
        struct {
                const char *partitioner;
                /* Expected partition per message (see keys above) */
                int32_t exp_part[_MSG_CNT];
        } ptest[] = {{"random", {-1, -1, -1, -1, -1}},
                     {"consistent",
                      {/* These constants were acquired using
                        * the 'crc32' command on OSX */
                       0x0 % part_cnt, 0x0 % part_cnt, 0xb1b451d7 % part_cnt,
                       0xb0150df7 % part_cnt, 0xd077037e % part_cnt}},
                     {"consistent_random",
                      {-1, -1, 0xb1b451d7 % part_cnt, 0xb0150df7 % part_cnt,
                       0xd077037e % part_cnt}},
                     {"murmur2",
                      {/* .. using tests/java/Murmur2Cli */
                       0x106e08d9 % part_cnt, 0x106e08d9 % part_cnt,
                       0x058d780f % part_cnt, 0x4f7703da % part_cnt,
                       0x5ec19395 % part_cnt}},
                     {"murmur2_random",
                      {-1, 0x106e08d9 % part_cnt, 0x058d780f % part_cnt,
                       0x4f7703da % part_cnt, 0x5ec19395 % part_cnt}},
                     {"fnv1a",
                      {/* .. using https://play.golang.org/p/hRkA4xtYyJ6 */
                       0x7ee3623b % part_cnt, 0x7ee3623b % part_cnt,
                       0x27e6f469 % part_cnt, 0x155e3e5f % part_cnt,
                       0x17b1e27a % part_cnt}},
                     {"fnv1a_random",
                      {-1, 0x7ee3623b % part_cnt, 0x27e6f469 % part_cnt,
                       0x155e3e5f % part_cnt, 0x17b1e27a % part_cnt}},
                     {NULL}};
        int pi;
        const char *topic = test_mk_topic_name(__FUNCTION__, 1);

        test_create_topic(NULL, topic, part_cnt, 1);

        for (pi = 0; ptest[pi].partitioner; pi++) {
                do_test_partitioner(topic, ptest[pi].partitioner, _MSG_CNT,
                                    keys, ptest[pi].exp_part);
        }
}

int main_0048_partitioner(int argc, char **argv) {
        if (test_can_create_topics(0))
                do_test_partitioners();
        do_test_failed_partitioning();
        return 0;
}