forked from confluentinc/librdkafka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transactions-older-broker.c
668 lines (560 loc) · 26.2 KB
/
transactions-older-broker.c
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
/*
* librdkafka - Apache Kafka C library
*
* Copyright (c) 2020-2022, 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.
*/
/**
* @name Transactions example for Apache Kafka <= 2.4.0 (no KIP-447 support).
*
* This example show-cases a simple transactional consume-process-produce
* application that reads messages from an input topic, extracts all
* numbers from the message's value string, adds them up, and sends
* the sum to the output topic as part of a transaction.
* The transaction is committed every 5 seconds or 100 messages, whichever
* comes first. As the transaction is committed a new transaction is started.
*
* @remark This example does not yet support incremental rebalancing and thus
* not the cooperative-sticky partition.assignment.strategy.
*/
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
/* Typical include path would be <librdkafka/rdkafka.h>, but this program
* is builtin from within the librdkafka source tree and thus differs. */
#include "rdkafka.h"
static volatile sig_atomic_t run = 1;
static rd_kafka_t *consumer;
/* From command-line arguments */
static const char *brokers, *input_topic, *output_topic;
/**
* @struct This is the per input partition state, constisting of
* a transactional producer and the in-memory state for the current transaction.
* This demo simply finds all numbers (ascii string numbers) in the message
* payload and adds them.
*/
struct state {
rd_kafka_t *producer; /**< Per-input partition output producer */
rd_kafka_topic_partition_t *rktpar; /**< Back-pointer to the
* input partition. */
time_t last_commit; /**< Last transaction commit */
int msgcnt; /**< Number of messages processed in current txn */
};
/* Current assignment for the input consumer.
* The .opaque field of each partition points to an allocated 'struct state'.
*/
static rd_kafka_topic_partition_list_t *assigned_partitions;
/**
* @brief A fatal error has occurred, immediately exit the application.
*/
#define fatal(...) \
do { \
fprintf(stderr, "FATAL ERROR: "); \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, "\n"); \
exit(1); \
} while (0)
/**
* @brief Same as fatal() but takes an rd_kafka_error_t object, prints its
* error message, destroys the object and then exits fatally.
*/
#define fatal_error(what, error) \
do { \
fprintf(stderr, "FATAL ERROR: %s: %s: %s\n", what, \
rd_kafka_error_name(error), \
rd_kafka_error_string(error)); \
rd_kafka_error_destroy(error); \
exit(1); \
} while (0)
/**
* @brief Signal termination of program
*/
static void stop(int sig) {
run = 0;
}
/**
* @brief Message delivery report callback.
*
* This callback is called exactly once per message, indicating if
* the message was succesfully delivered
* (rkmessage->err == RD_KAFKA_RESP_ERR_NO_ERROR) or permanently
* failed delivery (rkmessage->err != RD_KAFKA_RESP_ERR_NO_ERROR).
*
* The callback is triggered from rd_kafka_poll(), rd_kafka_flush(),
* rd_kafka_abort_transaction() and rd_kafka_commit_transaction() and
* executes on the application's thread.
*
* The current transactional will enter the abortable state if any
* message permanently fails delivery and the application must then
* call rd_kafka_abort_transaction(). But it does not need to be done from
* here, this state is checked by all the transactional APIs and it is better
* to perform this error checking when calling
* rd_kafka_send_offsets_to_transaction() and rd_kafka_commit_transaction().
* In the case of transactional producing the delivery report callback is
* mostly useful for logging the produce failures.
*/
static void
dr_msg_cb(rd_kafka_t *rk, const rd_kafka_message_t *rkmessage, void *opaque) {
if (rkmessage->err)
fprintf(stderr, "%% Message delivery failed: %s\n",
rd_kafka_err2str(rkmessage->err));
/* The rkmessage is destroyed automatically by librdkafka */
}
/**
* @brief Create a transactional producer for the given input pratition
* and begin a new transaction.
*/
static rd_kafka_t *
create_transactional_producer(const rd_kafka_topic_partition_t *rktpar) {
rd_kafka_conf_t *conf = rd_kafka_conf_new();
rd_kafka_t *rk;
char errstr[256];
rd_kafka_error_t *error;
char transactional_id[256];
snprintf(transactional_id, sizeof(transactional_id),
"librdkafka_transactions_older_example_%s-%d", rktpar->topic,
rktpar->partition);
if (rd_kafka_conf_set(conf, "bootstrap.servers", brokers, errstr,
sizeof(errstr)) != RD_KAFKA_CONF_OK ||
rd_kafka_conf_set(conf, "transactional.id", transactional_id,
errstr, sizeof(errstr)) != RD_KAFKA_CONF_OK ||
rd_kafka_conf_set(conf, "transaction.timeout.ms", "60000", errstr,
sizeof(errstr)) != RD_KAFKA_CONF_OK)
fatal("Failed to configure producer: %s", errstr);
/* This callback will be called once per message to indicate
* final delivery status. */
rd_kafka_conf_set_dr_msg_cb(conf, dr_msg_cb);
/* Create producer */
rk = rd_kafka_new(RD_KAFKA_PRODUCER, conf, errstr, sizeof(errstr));
if (!rk) {
rd_kafka_conf_destroy(conf);
fatal("Failed to create producer: %s", errstr);
}
/* Initialize transactions, this is only performed once
* per transactional producer to acquire its producer id, et.al. */
error = rd_kafka_init_transactions(rk, -1);
if (error)
fatal_error("init_transactions()", error);
/* Begin a new transaction */
error = rd_kafka_begin_transaction(rk);
if (error)
fatal_error("begin_transaction()", error);
return rk;
}
/**
* @brief Abort the current transaction and destroy the producer.
*/
static void destroy_transactional_producer(rd_kafka_t *rk) {
rd_kafka_error_t *error;
fprintf(stdout, "%s: aborting transaction and terminating producer\n",
rd_kafka_name(rk));
/* Abort the current transaction, ignore any errors
* since we're terminating the producer anyway. */
error = rd_kafka_abort_transaction(rk, -1);
if (error) {
fprintf(stderr,
"WARNING: Ignoring abort_transaction() error since "
"producer is being destroyed: %s\n",
rd_kafka_error_string(error));
rd_kafka_error_destroy(error);
}
rd_kafka_destroy(rk);
}
/**
* @brief Abort the current transaction and rewind consumer offsets to
* position where the transaction last started, i.e., the committed
* consumer offset.
*/
static void abort_transaction_and_rewind(struct state *state) {
rd_kafka_topic_t *rkt =
rd_kafka_topic_new(consumer, state->rktpar->topic, NULL);
rd_kafka_topic_partition_list_t *offset;
rd_kafka_resp_err_t err;
rd_kafka_error_t *error;
fprintf(stdout,
"Aborting transaction and rewinding offset for %s [%d]\n",
state->rktpar->topic, state->rktpar->partition);
/* Abort the current transaction */
error = rd_kafka_abort_transaction(state->producer, -1);
if (error)
fatal_error("Failed to abort transaction", error);
/* Begin a new transaction */
error = rd_kafka_begin_transaction(state->producer);
if (error)
fatal_error("Failed to begin transaction", error);
/* Get committed offset for this partition */
offset = rd_kafka_topic_partition_list_new(1);
rd_kafka_topic_partition_list_add(offset, state->rktpar->topic,
state->rktpar->partition);
/* Note: Timeout must be lower than max.poll.interval.ms */
err = rd_kafka_committed(consumer, offset, 10 * 1000);
if (err)
fatal("Failed to acquire committed offset for %s [%d]: %s",
state->rktpar->topic, (int)state->rktpar->partition,
rd_kafka_err2str(err));
/* Seek to committed offset, or start of partition if no
* no committed offset is available. */
err = rd_kafka_seek(rkt, state->rktpar->partition,
offset->elems[0].offset < 0
?
/* No committed offset, start from beginning */
RD_KAFKA_OFFSET_BEGINNING
:
/* Use committed offset */
offset->elems[0].offset,
0);
if (err)
fatal("Failed to seek %s [%d]: %s", state->rktpar->topic,
(int)state->rktpar->partition, rd_kafka_err2str(err));
rd_kafka_topic_destroy(rkt);
}
/**
* @brief Commit the current transaction and start a new transaction.
*/
static void commit_transaction_and_start_new(struct state *state) {
rd_kafka_error_t *error;
rd_kafka_resp_err_t err;
rd_kafka_consumer_group_metadata_t *cgmd;
rd_kafka_topic_partition_list_t *offset;
fprintf(stdout, "Committing transaction for %s [%d]\n",
state->rktpar->topic, state->rktpar->partition);
/* Send the input consumer's offset to transaction
* to commit those offsets along with the transaction itself,
* this is what guarantees exactly-once-semantics (EOS), that
* input (offsets) and output (messages) are committed atomically. */
/* Get the consumer's current group state */
cgmd = rd_kafka_consumer_group_metadata(consumer);
/* Get consumer's current position for this partition */
offset = rd_kafka_topic_partition_list_new(1);
rd_kafka_topic_partition_list_add(offset, state->rktpar->topic,
state->rktpar->partition);
err = rd_kafka_position(consumer, offset);
if (err)
fatal("Failed to get consumer position for %s [%d]: %s",
state->rktpar->topic, state->rktpar->partition,
rd_kafka_err2str(err));
/* Send offsets to transaction coordinator */
error = rd_kafka_send_offsets_to_transaction(state->producer, offset,
cgmd, -1);
rd_kafka_consumer_group_metadata_destroy(cgmd);
rd_kafka_topic_partition_list_destroy(offset);
if (error) {
if (rd_kafka_error_txn_requires_abort(error)) {
fprintf(stderr,
"WARNING: Failed to send offsets to "
"transaction: %s: %s: aborting transaction\n",
rd_kafka_error_name(error),
rd_kafka_error_string(error));
rd_kafka_error_destroy(error);
abort_transaction_and_rewind(state);
return;
} else {
fatal_error("Failed to send offsets to transaction",
error);
}
}
/* Commit the transaction */
error = rd_kafka_commit_transaction(state->producer, -1);
if (error) {
if (rd_kafka_error_txn_requires_abort(error)) {
fprintf(stderr,
"WARNING: Failed to commit transaction: "
"%s: %s: aborting transaction\n",
rd_kafka_error_name(error),
rd_kafka_error_string(error));
abort_transaction_and_rewind(state);
rd_kafka_error_destroy(error);
return;
} else {
fatal_error("Failed to commit transaction", error);
}
}
/* Begin new transaction */
error = rd_kafka_begin_transaction(state->producer);
if (error)
fatal_error("Failed to begin new transaction", error);
}
/**
* @brief The rebalance will be triggered (from rd_kafka_consumer_poll())
* when the consumer's partition assignment is assigned or revoked.
*
* Prior to KIP-447 being supported there must be one transactional output
* producer for each consumed input partition, so we create and destroy
* these producer's from this callback.
*/
static void
consumer_group_rebalance_cb(rd_kafka_t *rk,
rd_kafka_resp_err_t err,
rd_kafka_topic_partition_list_t *partitions,
void *opaque) {
int i;
if (!strcmp(rd_kafka_rebalance_protocol(rk), "COOPERATIVE"))
fatal(
"This example has not yet been modified to work with "
"cooperative incremental rebalancing "
"(partition.assignment.strategy=cooperative-sticky)");
switch (err) {
case RD_KAFKA_RESP_ERR__ASSIGN_PARTITIONS:
assigned_partitions =
rd_kafka_topic_partition_list_copy(partitions);
fprintf(stdout, "Consumer group rebalanced, new assignment:\n");
/* Create a transactional producer for each input partition */
for (i = 0; i < assigned_partitions->cnt; i++) {
/* Store the partition-to-producer mapping
* in the partition's opaque field. */
rd_kafka_topic_partition_t *rktpar =
&assigned_partitions->elems[i];
struct state *state = calloc(1, sizeof(*state));
state->producer = create_transactional_producer(rktpar);
state->rktpar = rktpar;
rktpar->opaque = state;
state->last_commit = time(NULL);
fprintf(stdout,
" %s [%d] with transactional producer %s\n",
rktpar->topic, rktpar->partition,
rd_kafka_name(state->producer));
}
/* Let the consumer know the rebalance has been handled
* by calling assign.
* This will also tell the consumer to start fetching messages
* for the assigned partitions. */
rd_kafka_assign(rk, partitions);
break;
case RD_KAFKA_RESP_ERR__REVOKE_PARTITIONS:
fprintf(stdout,
"Consumer group rebalanced, assignment revoked\n");
/* Abort the current transactions and destroy all producers */
for (i = 0; i < assigned_partitions->cnt; i++) {
/* Store the partition-to-producer mapping
* in the partition's opaque field. */
struct state *state =
(struct state *)assigned_partitions->elems[i]
.opaque;
destroy_transactional_producer(state->producer);
free(state);
}
rd_kafka_topic_partition_list_destroy(assigned_partitions);
assigned_partitions = NULL;
/* Let the consumer know the rebalance has been handled
* and revoke the current assignment. */
rd_kafka_assign(rk, NULL);
break;
default:
/* NOTREACHED */
fatal("Unexpected rebalance event: %s", rd_kafka_err2name(err));
}
}
/**
* @brief Create the input consumer.
*/
static rd_kafka_t *create_input_consumer(const char *brokers,
const char *input_topic) {
rd_kafka_conf_t *conf = rd_kafka_conf_new();
rd_kafka_t *rk;
char errstr[256];
rd_kafka_resp_err_t err;
rd_kafka_topic_partition_list_t *topics;
if (rd_kafka_conf_set(conf, "bootstrap.servers", brokers, errstr,
sizeof(errstr)) != RD_KAFKA_CONF_OK ||
rd_kafka_conf_set(conf, "group.id",
"librdkafka_transactions_older_example_group",
errstr, sizeof(errstr)) != RD_KAFKA_CONF_OK ||
/* The input consumer's offsets are explicitly committed with the
* output producer's transaction using
* rd_kafka_send_offsets_to_transaction(), so auto commits
* must be disabled. */
rd_kafka_conf_set(conf, "enable.auto.commit", "false", errstr,
sizeof(errstr)) != RD_KAFKA_CONF_OK) {
fatal("Failed to configure consumer: %s", errstr);
}
/* This callback will be called when the consumer group is rebalanced
* and the consumer's partition assignment is assigned or revoked. */
rd_kafka_conf_set_rebalance_cb(conf, consumer_group_rebalance_cb);
/* Create consumer */
rk = rd_kafka_new(RD_KAFKA_CONSUMER, conf, errstr, sizeof(errstr));
if (!rk) {
rd_kafka_conf_destroy(conf);
fatal("Failed to create consumer: %s", errstr);
}
/* Forward all partition messages to the main queue and
* rd_kafka_consumer_poll(). */
rd_kafka_poll_set_consumer(rk);
/* Subscribe to the input topic */
topics = rd_kafka_topic_partition_list_new(1);
rd_kafka_topic_partition_list_add(topics, input_topic,
/* The partition is ignored in
* rd_kafka_subscribe() */
RD_KAFKA_PARTITION_UA);
err = rd_kafka_subscribe(rk, topics);
rd_kafka_topic_partition_list_destroy(topics);
if (err) {
rd_kafka_destroy(rk);
fatal("Failed to subscribe to %s: %s\n", input_topic,
rd_kafka_err2str(err));
}
return rk;
}
/**
* @brief Find and parse next integer string in \p start.
* @returns Pointer after found integer string, or NULL if not found.
*/
static const void *
find_next_int(const void *start, const void *end, int *intp) {
const char *p;
int collecting = 0;
int num = 0;
for (p = (const char *)start; p < (const char *)end; p++) {
if (isdigit((int)(*p))) {
collecting = 1;
num = (num * 10) + ((int)*p - ((int)'0'));
} else if (collecting)
break;
}
if (!collecting)
return NULL; /* No integer string found */
*intp = num;
return p;
}
/**
* @brief Process a message from the input consumer by parsing all
* integer strings, adding them, and then producing the sum
* the output topic using the transactional producer for the given
* inut partition.
*/
static void process_message(struct state *state,
const rd_kafka_message_t *rkmessage) {
int num;
long unsigned sum = 0;
const void *p, *end;
rd_kafka_resp_err_t err;
char value[64];
if (rkmessage->len == 0)
return; /* Ignore empty messages */
p = rkmessage->payload;
end = ((const char *)rkmessage->payload) + rkmessage->len;
/* Find and sum all numbers in the message */
while ((p = find_next_int(p, end, &num)))
sum += num;
if (sum == 0)
return; /* No integers in message, ignore it. */
snprintf(value, sizeof(value), "%lu", sum);
/* Emit output message on transactional producer */
while (1) {
err = rd_kafka_producev(
state->producer, RD_KAFKA_V_TOPIC(output_topic),
/* Use same key as input message */
RD_KAFKA_V_KEY(rkmessage->key, rkmessage->key_len),
/* Value is the current sum of this
* transaction. */
RD_KAFKA_V_VALUE(value, strlen(value)),
/* Copy value since it is allocated on the stack */
RD_KAFKA_V_MSGFLAGS(RD_KAFKA_MSG_F_COPY), RD_KAFKA_V_END);
if (!err)
break;
else if (err == RD_KAFKA_RESP_ERR__QUEUE_FULL) {
/* If output queue fills up we need to wait for
* some delivery reports and then retry. */
rd_kafka_poll(state->producer, 100);
continue;
} else {
fprintf(stderr,
"WARNING: Failed to produce message to %s: "
"%s: aborting transaction\n",
output_topic, rd_kafka_err2str(err));
abort_transaction_and_rewind(state);
return;
}
}
}
int main(int argc, char **argv) {
/*
* Argument validation
*/
if (argc != 4) {
fprintf(stderr,
"%% Usage: %s <broker> <input-topic> <output-topic>\n",
argv[0]);
return 1;
}
brokers = argv[1];
input_topic = argv[2];
output_topic = argv[3];
/* Signal handler for clean shutdown */
signal(SIGINT, stop);
consumer = create_input_consumer(brokers, input_topic);
fprintf(stdout,
"Expecting integers to sum on input topic %s ...\n"
"To generate input messages you can use:\n"
" $ seq 1 100 | examples/producer %s %s\n",
input_topic, brokers, input_topic);
while (run) {
rd_kafka_message_t *msg;
struct state *state;
rd_kafka_topic_partition_t *rktpar;
/* Wait for new mesages or error events */
msg = rd_kafka_consumer_poll(consumer, 1000 /*1 second*/);
if (!msg)
continue;
if (msg->err) {
/* Client errors are typically just informational
* since the client will automatically try to recover
* from all types of errors.
* It is thus sufficient for the application to log and
* continue operating when an error is received. */
fprintf(stderr, "WARNING: Consumer error: %s\n",
rd_kafka_message_errstr(msg));
rd_kafka_message_destroy(msg);
continue;
}
/* Find output producer for this input partition */
rktpar = rd_kafka_topic_partition_list_find(
assigned_partitions, rd_kafka_topic_name(msg->rkt),
msg->partition);
if (!rktpar)
fatal(
"BUG: No output producer for assigned "
"partition %s [%d]",
rd_kafka_topic_name(msg->rkt), (int)msg->partition);
/* Get state struct for this partition */
state = (struct state *)rktpar->opaque;
/* Process message */
process_message(state, msg);
rd_kafka_message_destroy(msg);
/* Commit transaction every 100 messages or 5 seconds */
if (++state->msgcnt > 100 ||
state->last_commit + 5 <= time(NULL)) {
commit_transaction_and_start_new(state);
state->msgcnt = 0;
state->last_commit = time(NULL);
}
}
fprintf(stdout, "Closing consumer\n");
rd_kafka_consumer_close(consumer);
rd_kafka_destroy(consumer);
return 0;
}