forked from agile6v/ngx_http_conhash_test_module
/
ngx_http_conhash_test_module.c
437 lines (341 loc) · 12.9 KB
/
ngx_http_conhash_test_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
/*
* Copyright (C) agile6v
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <nginx.h>
#include "ngx_consistent_hash.h"
/*
* 1 = add curl -s "http://127.0.0.1/conhash?cmd=1&value=nodeA"
* 2 = del curl -s "http://127.0.0.1/conhash?cmd=2&value=nodeA"
* 3 = search curl -s "http://127.0.0.1/conhash?cmd=3&value=agile6v"
* 4 = traverse curl -s "http://127.0.0.1/conhash?cmd=4"
* 5 = clear curl -s "http://127.0.0.1/conhash?cmd=5"
*/
ngx_module_t ngx_http_conhash_test_module;
#define KEY_STR "cmd"
#define VALUE_STR "value"
#define BRACKET_L "("
#define BRACKET_R ")"
#define PROMPT_STR ") is in the node "
#define COMMA_STR ", "
static char *ngx_http_conhash_test(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static void *ngx_http_conhash_test_create_main_conf(ngx_conf_t *cf);
static char *ngx_http_conhash_test_init_main_conf(ngx_conf_t *cf, void *conf);
static ngx_int_t ngx_http_conhash_test_add(ngx_http_request_t *r, ngx_conhash_t *conhash, ngx_chain_t *out);
static ngx_int_t ngx_http_conhash_test_del(ngx_http_request_t *r, ngx_conhash_t *conhash, ngx_chain_t *out);
static ngx_int_t ngx_http_conhash_test_search(ngx_http_request_t *r, ngx_conhash_t *conhash, ngx_chain_t *out);
static ngx_int_t ngx_http_conhash_test_traverse(ngx_http_request_t *r, ngx_conhash_t *conhash, ngx_chain_t *out);
static ngx_int_t ngx_http_conhash_test_clear(ngx_http_request_t *r, ngx_conhash_t *conhash, ngx_chain_t *out);
static void ngx_http_conhash_test_make_len(ngx_conhash_vnode_t *vnode, void *data);
static void ngx_http_conhash_test_make_data(ngx_conhash_vnode_t *vnode, void *data);
static void ngx_http_conhash_test_get_data(ngx_conhash_vnode_t *vnode, void *data);
ngx_conhash_ctx_t ngx_http_conhash_test_ctx = {NULL, &ngx_http_conhash_test_module};
typedef struct {
ngx_rbtree_node_t key;
ngx_str_t name; // hnode name
ngx_str_t vname; // vnode name
ngx_pool_t *pool;
} ngx_http_conhash_test_node_name_t;
typedef struct {
ngx_conhash_t *conhash;
} ngx_http_conhash_test_main_conf_t;
static ngx_command_t ngx_http_conhash_test_module_commands[] = {
{ ngx_string("conhash_test"),
NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
ngx_http_conhash_test,
0,
0,
NULL },
{ ngx_string("conhash_test_zone"),
NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE12,
ngx_conhash_shm_set_slot,
NGX_HTTP_MAIN_CONF_OFFSET,
offsetof(ngx_http_conhash_test_main_conf_t, conhash),
&ngx_http_conhash_test_ctx},
ngx_null_command
};
static ngx_http_module_t ngx_http_conhash_test_module_ctx = {
NULL, /* preconfiguration */
NULL, /* postconfiguration */
ngx_http_conhash_test_create_main_conf, /* create main configuration */
ngx_http_conhash_test_init_main_conf, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
NULL, /* create location configuration */
NULL /* merge location configuration */
};
ngx_module_t ngx_http_conhash_test_module = {
NGX_MODULE_V1,
&ngx_http_conhash_test_module_ctx, /* module context */
ngx_http_conhash_test_module_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static ngx_int_t
ngx_http_conhash_test_handler(ngx_http_request_t *r)
{
ngx_int_t rc, cmd;
ngx_chain_t out;
ngx_str_t value;
ngx_conhash_t *conhash;
ngx_http_conhash_test_main_conf_t *ctmcf;
ctmcf = ngx_http_get_module_main_conf(r, ngx_http_conhash_test_module);
conhash = ctmcf->conhash;
out.buf = NULL;
out.next = NULL;
if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
return NGX_HTTP_NOT_ALLOWED;
}
if (conhash == NULL) {
return NGX_DECLINED;
}
rc = ngx_http_arg(r, (u_char *) KEY_STR, sizeof(KEY_STR) - 1, &value);
if (rc != NGX_OK) {
return rc;
}
cmd = ngx_atoi(value.data, value.len);
if (cmd < 1 && cmd > 4) {
return NGX_DECLINED;
}
switch (cmd) {
case 1: // add
rc = ngx_http_conhash_test_add(r, conhash, &out);
break;
case 2: // del
rc = ngx_http_conhash_test_del(r, conhash, &out);
break;
case 3: // search
rc = ngx_http_conhash_test_search(r, conhash, &out);
break;
case 4: // traverse
rc = ngx_http_conhash_test_traverse(r, conhash, &out);
break;
case 5: // clear
rc = ngx_http_conhash_test_clear(r, conhash, &out);
break;
}
if (rc != NGX_OK) {
return rc;
}
r->headers_out.status = NGX_HTTP_OK;
ngx_str_set(&r->headers_out.content_type, "text/plain");
if (out.buf != NULL) {
r->headers_out.content_length_n = ngx_buf_size(out.buf);
} else {
r->header_only = 1;
r->headers_out.content_length_n = 0;
}
rc = ngx_http_send_header(r);
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
return rc;
}
return ngx_http_output_filter(r, &out);
}
static char *
ngx_http_conhash_test(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_core_loc_conf_t *clcf;
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
clcf->handler = ngx_http_conhash_test_handler;
return NGX_CONF_OK;
}
static void *
ngx_http_conhash_test_create_main_conf(ngx_conf_t *cf)
{
ngx_http_conhash_test_main_conf_t *ctmcf;
ctmcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_conhash_test_main_conf_t));
if (ctmcf == NULL) {
return NULL;
}
ctmcf->conhash = NGX_CONF_UNSET_PTR;
return ctmcf;
}
static char *
ngx_http_conhash_test_init_main_conf(ngx_conf_t *cf, void *conf)
{
ngx_http_conhash_test_main_conf_t *ctmcf = conf;
if (ctmcf->conhash == NGX_CONF_UNSET_PTR) {
ctmcf->conhash = NULL;
}
return NGX_CONF_OK;
}
static ngx_int_t
ngx_http_conhash_test_add(ngx_http_request_t *r, ngx_conhash_t *conhash, ngx_chain_t *out)
{
ngx_str_t value;
ngx_int_t rc;
ngx_buf_t *b;
rc = ngx_http_arg(r, (u_char *) VALUE_STR, sizeof(VALUE_STR) - 1, &value);
if (rc != NGX_OK) {
return rc;
}
b = ngx_create_temp_buf(r->pool, 1024);
if (b == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
rc = ngx_conhash_add_node(conhash, value.data, value.len, NULL);
if (rc == NGX_OK) {
b->last = ngx_sprintf(b->last, "Add node successfully!" CRLF);
}
if (rc == NGX_DECLINED) {
b->last = ngx_sprintf(b->last, "The node already exists!" CRLF);
rc = NGX_OK;
}
if (rc == NGX_AGAIN) {
b->last = ngx_sprintf(b->last, "The conhash space is not enough!" CRLF);
rc = NGX_OK;
}
b->last_buf = 1;
out->buf = b;
return rc;
}
static ngx_int_t
ngx_http_conhash_test_del(ngx_http_request_t *r, ngx_conhash_t *conhash, ngx_chain_t *out)
{
ngx_str_t value;
ngx_int_t rc;
ngx_buf_t *b;
rc = ngx_http_arg(r, (u_char *) VALUE_STR, sizeof(VALUE_STR) - 1, &value);
if (rc != NGX_OK) {
return rc;
}
b = ngx_create_temp_buf(r->pool, 1024);
if (b == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
rc = ngx_conhash_del_node(conhash, value.data, value.len);
if (rc == NGX_OK) {
b->last = ngx_sprintf(b->last, "Delete node successfully!" CRLF);
}
if (rc == NGX_DECLINED) {
b->last = ngx_sprintf(b->last, "The node does not exists!" CRLF);
rc = NGX_OK;
}
b->last_buf = 1;
out->buf = b;
return rc;
}
static ngx_int_t
ngx_http_conhash_test_search(ngx_http_request_t *r, ngx_conhash_t *conhash, ngx_chain_t *out)
{
ngx_str_t value;
ngx_int_t rc;
size_t len;
ngx_buf_t *b;
ngx_rbtree_key_t key;
ngx_http_conhash_test_node_name_t node;
rc = ngx_http_arg(r, (u_char *) VALUE_STR, sizeof(VALUE_STR) - 1, &value);
if (rc != NGX_OK) {
return rc;
}
ngx_memzero(&node, sizeof(ngx_http_conhash_test_node_name_t));
node.pool = r->pool;
rc = ngx_conhash_lookup_node(conhash, value.data, value.len, ngx_http_conhash_test_get_data, &node);
if (rc != NGX_OK) {
b = ngx_create_temp_buf(r->pool, 1024);
if (b == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
b->last = ngx_sprintf(b->last, "The node tree is empty!" CRLF);
goto done;
}
if (node.vname.data == NULL || node.name.data == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
len = value.len + sizeof(BRACKET_L) - 1 + NGX_OFF_T_LEN + sizeof(PROMPT_STR) - 1 +
node.name.len + sizeof(BRACKET_L) - 1 + node.vname.len +
sizeof(COMMA_STR) - 1 + NGX_OFF_T_LEN + sizeof(BRACKET_R) - 1 + sizeof(CRLF) - 1;
b = ngx_create_temp_buf(r->pool, len);
if (b == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
key = conhash->hash_func(value.data, value.len);
b->last = ngx_sprintf(b->last, "%V" BRACKET_L "%ui" PROMPT_STR "%V" BRACKET_L "%V" COMMA_STR "%ui"\
BRACKET_R CRLF, &value, key, &node.name, &node.vname, node.key);
done:
b->last_buf = 1;
out->buf = b;
return NGX_OK;
}
static ngx_int_t
ngx_http_conhash_test_traverse(ngx_http_request_t *r, ngx_conhash_t *conhash, ngx_chain_t *out)
{
size_t len;
ngx_buf_t *b;
ngx_int_t rc;
len = 0;
rc = ngx_conhash_node_traverse(conhash, ngx_http_conhash_test_make_len, &len);
if (rc == NGX_DECLINED) {
b = ngx_create_temp_buf(r->pool, 1024);
if (b == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
b->last = ngx_sprintf(b->last, "The node tree is empty!" CRLF);
goto done;
}
b = ngx_create_temp_buf(r->pool, len);
if (b == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
// NOTE: Try to finish at a time.
rc = ngx_conhash_node_traverse(conhash, ngx_http_conhash_test_make_data, b);
if (rc == NGX_DECLINED) {
b = ngx_create_temp_buf(r->pool, 1024);
if (b == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
b->last = ngx_sprintf(b->last, "The node tree is empty!" CRLF);
}
done:
b->last_buf = 1;
out->buf = b;
return NGX_OK;
}
static ngx_int_t
ngx_http_conhash_test_clear(ngx_http_request_t *r, ngx_conhash_t *conhash, ngx_chain_t *out)
{
ngx_conhash_clear(conhash);
return NGX_OK;
}
static void
ngx_http_conhash_test_make_len(ngx_conhash_vnode_t *vnode, void *data)
{
size_t *len = data;
*len += vnode->name.len + sizeof(BRACKET_L) - 1 + NGX_OFF_T_LEN +
sizeof(BRACKET_R) - 1 + sizeof(CRLF) - 1;
}
static void
ngx_http_conhash_test_make_data(ngx_conhash_vnode_t *vnode, void *data)
{
ngx_buf_t *b = data;
b->last = ngx_sprintf(b->last, "%V" BRACKET_L "%ui" BRACKET_R CRLF, &vnode->name, vnode->node.key);
}
static void
ngx_http_conhash_test_get_data(ngx_conhash_vnode_t *vnode, void *data)
{
ngx_http_conhash_test_node_name_t *node = data;
u_char *p;
node->name.len = vnode->hnode->name.len;
node->name.data = ngx_pcalloc(node->pool, node->name.len + 1);
if (node->name.data == NULL) {
return;
}
p = ngx_cpymem(node->name.data, vnode->hnode->name.data, node->name.len);
*p = '\0';
node->vname.len = vnode->name.len;
node->vname.data = ngx_pcalloc(node->pool, node->vname.len + 1);
if (node->vname.data == NULL) {
return;
}
p = ngx_cpymem(node->vname.data, vnode->name.data, node->vname.len);
*p = '\0';
node->key = vnode->node;
}