-
Notifications
You must be signed in to change notification settings - Fork 64
/
ngx_http_kafka_module.c
547 lines (426 loc) · 14.9 KB
/
ngx_http_kafka_module.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
/*
* nginx kafka module
*
* using librdkafka: https://github.com/edenhill/librdkafka
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <librdkafka/rdkafka.h>
#define KAFKA_TOPIC_MAXLEN 256
#define KAFKA_BROKER_MAXLEN 512
#define KAFKA_ERR_NO_DATA "no_message\n"
#define KAFKA_ERR_BODY_TO_LARGE "body_too_large\n"
#define KAFKA_ERR_PRODUCER "kafka_producer_error\n"
#define KAFKA_PARTITION_UNSET 0xFFFFFFFF
static ngx_int_t ngx_http_kafka_init_worker(ngx_cycle_t *cycle);
static void ngx_http_kafka_exit_worker(ngx_cycle_t *cycle);
static void *ngx_http_kafka_create_main_conf(ngx_conf_t *cf);
static void *ngx_http_kafka_create_loc_conf(ngx_conf_t *cf);
static char *ngx_http_kafka_merge_loc_conf(ngx_conf_t *cf,
void *parent, void *child);
static char *ngx_http_set_kafka(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char *ngx_http_set_kafka_broker_list(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
static char *ngx_http_set_kafka_topic(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
static char *ngx_http_set_kafka_partition(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
static char *ngx_http_set_kafka_broker(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
static ngx_int_t ngx_http_kafka_handler(ngx_http_request_t *r);
static void ngx_http_kafka_post_callback_handler(ngx_http_request_t *r);
typedef enum {
ngx_str_push = 0,
ngx_str_pop = 1
} ngx_str_op;
static void ngx_str_helper(ngx_str_t *str, ngx_str_op op);
typedef struct {
rd_kafka_t *rk;
rd_kafka_conf_t *rkc;
ngx_array_t *broker_list;
} ngx_http_kafka_main_conf_t;
static char *ngx_http_kafka_main_conf_broker_add(ngx_http_kafka_main_conf_t *cf,
ngx_str_t *broker);
typedef struct {
ngx_str_t topic; /* kafka topic */
ngx_str_t broker; /* broker addr (eg: localhost:9092) */
/* kafka partition(0...N), default value: RD_KAFKA_PARTITION_UA */
ngx_int_t partition;
rd_kafka_topic_t *rkt;
rd_kafka_topic_conf_t *rktc;
} ngx_http_kafka_loc_conf_t;
static ngx_command_t ngx_http_kafka_commands[] = {
{
ngx_string("kafka"),
NGX_HTTP_MAIN_CONF|NGX_CONF_NOARGS,
ngx_http_set_kafka,
NGX_HTTP_MAIN_CONF_OFFSET,
0,
NULL },
{
ngx_string("kafka_broker_list"),
NGX_HTTP_MAIN_CONF|NGX_CONF_1MORE,
ngx_http_set_kafka_broker_list,
NGX_HTTP_MAIN_CONF_OFFSET,
0,
NULL },
{
ngx_string("kafka_topic"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_http_set_kafka_topic,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_kafka_loc_conf_t, topic),
NULL },
{
ngx_string("kafka_partition"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_http_set_kafka_partition,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_kafka_loc_conf_t, partition),
NULL },
{
ngx_string("kafka_broker"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_http_set_kafka_broker,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_kafka_loc_conf_t, broker),
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_http_kafka_module_ctx = {
NULL, /* pre conf */
NULL, /* post conf */
ngx_http_kafka_create_main_conf, /* create main conf */
NULL, /* init main conf */
NULL, /* create server conf */
NULL, /* merge server conf */
ngx_http_kafka_create_loc_conf, /* create local conf */
ngx_http_kafka_merge_loc_conf, /* merge location conf */
};
ngx_module_t ngx_http_kafka_module = {
NGX_MODULE_V1,
&ngx_http_kafka_module_ctx, /* module context */
ngx_http_kafka_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
ngx_http_kafka_init_worker, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
ngx_http_kafka_exit_worker, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
char *ngx_http_kafka_main_conf_broker_add(ngx_http_kafka_main_conf_t *cf,
ngx_str_t *broker)
{
ngx_str_t *new_broker;
new_broker = ngx_array_push(cf->broker_list);
if (new_broker == NULL) {
return NGX_CONF_ERROR;
}
*new_broker = *broker;
return NGX_OK;
}
void *ngx_http_kafka_create_main_conf(ngx_conf_t *cf)
{
ngx_http_kafka_main_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_kafka_main_conf_t));
if (conf == NULL) {
return NGX_CONF_ERROR;
}
conf->rk = NULL;
conf->rkc = NULL;
conf->broker_list = ngx_array_create(cf->pool, 4, sizeof(ngx_str_t));
if (conf->broker_list == NULL) {
return NULL;
}
return conf;
}
void *ngx_http_kafka_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_kafka_loc_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_kafka_loc_conf_t));
if (conf == NULL) {
return NGX_CONF_ERROR;
}
ngx_str_null(&conf->topic);
ngx_str_null(&conf->broker);
/*
* Could not set conf->partition RD_KAFKA_PARTITION_UA,
* because both values of RD_KAFKA_PARTITION_UA and NGX_CONF_UNSET is -1
*/
conf->partition = KAFKA_PARTITION_UNSET;
return conf;
}
char *ngx_http_kafka_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_http_kafka_loc_conf_t *prev = parent;
ngx_http_kafka_loc_conf_t *conf = child;
#define ngx_conf_merge_kafka_partition_conf(conf, prev, def) \
if (conf == KAFKA_PARTITION_UNSET) { \
conf = (prev == KAFKA_PARTITION_UNSET) ? def : prev; \
}
ngx_conf_merge_kafka_partition_conf(conf->partition, prev->partition,
RD_KAFKA_PARTITION_UA);
#undef ngx_conf_merge_kafka_partition_conf
return NGX_CONF_OK;
}
void kafka_callback_handler(rd_kafka_t *rk,
void *msg, size_t len, int err, void *opaque, void *msg_opaque)
{
if (err != 0) {
ngx_log_error(NGX_LOG_ERR,
(ngx_log_t *)msg_opaque, 0, rd_kafka_err2str(err));
}
}
char *ngx_http_set_kafka(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
/* we can add more code here to config ngx_http_kafka_main_conf_t */
return NGX_CONF_OK;
}
char *ngx_http_set_kafka_broker_list(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf)
{
char *cf_result;
ngx_uint_t i;
ngx_str_t *value;
ngx_http_kafka_main_conf_t *main_conf;
main_conf = conf;
value = cf->args->elts;
for (i = 1; i < cf->args->nelts; ++i) {
cf_result = ngx_http_kafka_main_conf_broker_add(main_conf, &value[i]);
if (cf_result != NGX_OK) {
return cf_result;
}
}
return NGX_OK;
}
char *ngx_http_set_kafka_topic(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
char *cf_result;
ngx_http_core_loc_conf_t *clcf;
ngx_http_kafka_loc_conf_t *local_conf;
/* install ngx_http_kafka_handler */
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
if (clcf == NULL) {
return NGX_CONF_ERROR;
}
clcf->handler = ngx_http_kafka_handler;
/* ngx_http_kafka_loc_conf_t::topic assignment */
cf_result = ngx_conf_set_str_slot(cf, cmd, conf);
if (cf_result != NGX_CONF_OK) {
return cf_result;
}
local_conf = conf;
local_conf->rktc = rd_kafka_topic_conf_new();
return NGX_CONF_OK;
}
char *ngx_http_set_kafka_partition(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
char *p = conf;
ngx_int_t *np;
ngx_str_t *value;
np = (ngx_int_t *)(p + cmd->offset);
if (*np != KAFKA_PARTITION_UNSET) {
return "is duplicate";
}
value = cf->args->elts;
if (ngx_strncmp("auto", (const char *)value[1].data, value[1].len) == 0) {
*np = RD_KAFKA_PARTITION_UA;
} else {
*np = ngx_atoi(value[1].data, value[1].len);
if (*np == NGX_ERROR) {
return "invalid number";
}
}
return NGX_CONF_OK;
}
char *ngx_http_set_kafka_broker(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
char *cf_result;
ngx_http_kafka_loc_conf_t *local_conf;
ngx_http_kafka_main_conf_t *main_conf;
/* ngx_http_kafka_loc_conf_t::broker assignment */
cf_result = ngx_conf_set_str_slot(cf, cmd, conf);
if (cf_result != NGX_CONF_OK) {
return cf_result;
}
local_conf = conf;
main_conf = ngx_http_conf_get_module_main_conf(cf, ngx_http_kafka_module);
if (main_conf == NULL) {
return NGX_CONF_ERROR;
}
return ngx_http_kafka_main_conf_broker_add(main_conf, &local_conf->broker);
}
static ngx_int_t ngx_http_kafka_handler(ngx_http_request_t *r)
{
ngx_int_t rv;
if (!(r->method & NGX_HTTP_POST)) {
return NGX_HTTP_NOT_ALLOWED;
}
rv = ngx_http_read_client_request_body(r, ngx_http_kafka_post_callback_handler);
if (rv >= NGX_HTTP_SPECIAL_RESPONSE) {
return rv;
}
return NGX_DONE;
}
static void ngx_http_kafka_post_callback_handler(ngx_http_request_t *r)
{
int rc, nbufs;
u_char *msg, *err_msg;
size_t len, err_msg_size;
ngx_log_t *conn_log;
ngx_buf_t *buf;
ngx_chain_t out;
ngx_chain_t *cl, *in;
ngx_http_request_body_t *body;
ngx_http_kafka_main_conf_t *main_conf;
ngx_http_kafka_loc_conf_t *local_conf;
err_msg = NULL;
err_msg_size = 0;
main_conf = NULL;
/* get body */
body = r->request_body;
if (body == NULL || body->bufs == NULL) {
err_msg = (u_char *)KAFKA_ERR_NO_DATA;
err_msg_size = sizeof(KAFKA_ERR_NO_DATA);
r->headers_out.status = NGX_HTTP_OK;
goto end;
}
/* calc len and bufs */
len = 0;
nbufs = 0;
in = body->bufs;
for (cl = in; cl != NULL; cl = cl->next) {
nbufs++;
len += (size_t)(cl->buf->last - cl->buf->pos);
}
/* get msg */
if (nbufs == 0) {
err_msg = (u_char *)KAFKA_ERR_NO_DATA;
err_msg_size = sizeof(KAFKA_ERR_NO_DATA);
r->headers_out.status = NGX_HTTP_OK;
goto end;
}
if (nbufs == 1 && ngx_buf_in_memory(in->buf)) {
msg = in->buf->pos;
} else {
if ((msg = ngx_pnalloc(r->pool, len)) == NULL) {
ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
return;
}
for (cl = in; cl != NULL; cl = cl->next) {
if (ngx_buf_in_memory(cl->buf)) {
msg = ngx_copy(msg, cl->buf->pos, cl->buf->last - cl->buf->pos);
} else {
/* TODO: handle buf in file */
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_kafka_handler cannot handle in-file-post-buf");
err_msg = (u_char *)KAFKA_ERR_BODY_TO_LARGE;
err_msg_size = sizeof(KAFKA_ERR_BODY_TO_LARGE);
r->headers_out.status = NGX_HTTP_INTERNAL_SERVER_ERROR;
goto end;
}
}
msg -= len;
}
/* send to kafka */
main_conf = ngx_http_get_module_main_conf(r, ngx_http_kafka_module);
local_conf = ngx_http_get_module_loc_conf(r, ngx_http_kafka_module);
if (local_conf->rkt == NULL) {
ngx_str_helper(&local_conf->topic, ngx_str_push);
local_conf->rkt = rd_kafka_topic_new(main_conf->rk,
(const char *)local_conf->topic.data, local_conf->rktc);
ngx_str_helper(&local_conf->topic, ngx_str_pop);
}
/*
* the last param should NOT be r->connection->log, for reason that
* the callback handler (func: kafka_callback_handler) would be called
* asynchronously when some errors being happened.
*
* At this time, ngx_http_finalize_request may have been invoked.
* In this case, the object r had been destroyed
* but kafka_callback_handler use the pointer
* r->connection->log! Worker processes CRASH!
*
* Thanks for engineers of www.360buy.com report me this bug.
*
* */
conn_log = r->connection->log;
rc = rd_kafka_produce(local_conf->rkt, (int32_t)local_conf->partition,
RD_KAFKA_MSG_F_COPY, (void *)msg, len, NULL, 0, conn_log);
if (rc != 0) {
ngx_log_error(NGX_LOG_ERR, conn_log, 0,
rd_kafka_err2str(rd_kafka_last_error()));
err_msg = (u_char *)KAFKA_ERR_PRODUCER;
err_msg_size = sizeof(KAFKA_ERR_PRODUCER);
r->headers_out.status = NGX_HTTP_INTERNAL_SERVER_ERROR;
}
end:
if (err_msg != NULL) {
buf = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
out.buf = buf;
out.next = NULL;
buf->pos = err_msg;
buf->last = err_msg + err_msg_size - 1;
buf->memory = 1;
buf->last_buf = 1;
ngx_str_set(&(r->headers_out.content_type), "text/html");
ngx_http_send_header(r);
ngx_http_output_filter(r, &out);
} else {
r->headers_out.status = NGX_HTTP_NO_CONTENT;
ngx_http_send_header(r);
}
ngx_http_finalize_request(r, NGX_OK);
if (main_conf != NULL) {
rd_kafka_poll(main_conf->rk, 0);
}
}
ngx_int_t ngx_http_kafka_init_worker(ngx_cycle_t *cycle)
{
ngx_uint_t n;
ngx_str_t *broker_list;
ngx_http_kafka_main_conf_t *main_conf;
main_conf = ngx_http_cycle_get_module_main_conf(cycle,
ngx_http_kafka_module);
main_conf->rkc = rd_kafka_conf_new();
rd_kafka_conf_set_dr_cb(main_conf->rkc, kafka_callback_handler);
main_conf->rk = rd_kafka_new(RD_KAFKA_PRODUCER, main_conf->rkc, NULL, 0);
broker_list = main_conf->broker_list->elts;
for (n = 0; n < main_conf->broker_list->nelts; ++n) {
ngx_str_helper(&broker_list[n], ngx_str_push);
rd_kafka_brokers_add(main_conf->rk, (const char *)broker_list[n].data);
ngx_str_helper(&broker_list[n], ngx_str_pop);
}
return 0;
}
void ngx_http_kafka_exit_worker(ngx_cycle_t *cycle)
{
ngx_http_kafka_main_conf_t *main_conf;
main_conf = ngx_http_cycle_get_module_main_conf(cycle,
ngx_http_kafka_module);
rd_kafka_poll(main_conf->rk, 0);
while (rd_kafka_outq_len(main_conf->rk) > 0) {
rd_kafka_poll(main_conf->rk, 100);
}
// TODO: rd_kafka_topic_destroy(each loc conf rkt);
rd_kafka_destroy(main_conf->rk);
}
void ngx_str_helper(ngx_str_t *str, ngx_str_op op)
{
static char backup;
switch (op) {
case ngx_str_push:
backup = str->data[str->len];
str->data[str->len] = 0;
break;
case ngx_str_pop:
str->data[str->len] = backup;
break;
default:
ngx_abort();
}
}