forked from alibaba/nginx-tfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngx_http_tfs_duplicate.c
504 lines (411 loc) · 17 KB
/
ngx_http_tfs_duplicate.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
/*
* Copyright (C) 2010-2012 Alibaba Group Holding Limited
*/
#include <ngx_md5.h>
#include <ngx_http_tfs_duplicate.h>
#include <ngx_http_tfs_data_server_message.h>
#include <ngx_http_tfs_remote_block_cache.h>
static void ngx_http_tfs_duplicate_get_handler(ngx_http_tair_key_value_t *kv, ngx_int_t rc, void *data);
static void ngx_http_tfs_duplicate_put_handler(ngx_int_t rc, void *data);
static void ngx_http_tfs_duplicate_delete_handler(ngx_int_t rc, void *data);
static void ngx_http_tfs_duplicate_callback(ngx_http_tfs_dedup_ctx_t *ctx, ngx_int_t rc);
ngx_int_t ngx_http_tfs_duplicate_check_suffix(ngx_str_t *tfs_name, ngx_str_t *suffix);
ngx_int_t ngx_http_tfs_duplicate_check_filename(ngx_str_t *dup_name, ngx_http_tfs_raw_fsname_t* fsname);
ngx_int_t
ngx_http_tfs_get_duplicate_info(ngx_http_tfs_dedup_ctx_t *ctx,
ngx_pool_t *pool, ngx_log_t * log, ngx_chain_t *data)
{
u_char *p;
ssize_t data_len;
ngx_int_t rc;
ngx_http_tair_data_t tair_key;
data_len = 0;
rc = ngx_http_tfs_sum_md5(data, ctx->tair_key, &data_len, log);
if (rc == NGX_ERROR) {
return NGX_ERROR;
}
p = ctx->tair_key;
p += NGX_HTTP_TFS_MD5_RESULT_LEN;
*(uint32_t *) p = htonl(data_len);
tair_key.type = NGX_HTTP_TAIR_BYTEARRAY;
tair_key.data = ctx->tair_key;
tair_key.len = NGX_HTTP_TFS_DUPLICATE_KEY_SIZE;
ctx->md5_sumed = 1;
rc = ngx_http_tfs_tair_get_helper(ctx->tair_instance, pool, log,
&tair_key,
ngx_http_tfs_duplicate_get_handler, ctx);
return rc;
}
static void
ngx_http_tfs_duplicate_get_handler(ngx_http_tair_key_value_t *kv, ngx_int_t rc, void *data)
{
u_char *p;
ngx_http_tfs_t *t;
ngx_http_tfs_dedup_ctx_t *ctx;
ctx = data;
t = ctx->data;
if (rc == NGX_HTTP_ETAIR_SUCCESS) {
p = kv->value->data;
if (p != NULL && kv->value->len > NGX_HTTP_TFS_DUPLICATE_VALUE_BASE_SIZE) {
ctx->file_ref_count = *(int32_t *)p;
p += sizeof(int32_t);
ctx->dup_file_name.len = kv->value->len - sizeof(int32_t);
ctx->dup_file_name.data = ngx_pcalloc(t->pool, ctx->dup_file_name.len);
if (ctx->dup_file_name.data == NULL) {
rc = NGX_ERROR;
} else {
ngx_memcpy(ctx->dup_file_name.data, p, ctx->dup_file_name.len);
rc = NGX_OK;
}
ctx->dup_version = kv->version;
} else {
rc = NGX_ERROR;
}
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, t->log, 0,
"get duplicate info: file name: %V, file ref count: %d, dup_version: %d",
&ctx->dup_file_name, ctx->file_ref_count, ctx->dup_version);
} else {
rc = NGX_ERROR;
ctx->dup_version = NGX_HTTP_TFS_DUPLICATE_INITIAL_MAGIC_VERSION;
}
ngx_http_tfs_duplicate_callback(ctx, rc);
}
ngx_int_t
ngx_http_tfs_set_duplicate_info(ngx_http_tfs_dedup_ctx_t *ctx,
ngx_pool_t *pool, ngx_log_t * log, ngx_chain_t *data)
{
u_char *p;
ssize_t data_len;
ngx_int_t rc;
ngx_http_tair_data_t tair_key;
ngx_http_tair_data_t tair_value;
data_len = 0;
if (!ctx->md5_sumed) {
rc = ngx_http_tfs_sum_md5(data, ctx->tair_key, &data_len, log);
if (rc == NGX_ERROR) {
return NGX_ERROR;
}
p = ctx->tair_key;
p += NGX_HTTP_TFS_MD5_RESULT_LEN;
*(uint32_t *) p = htonl(data_len);
ctx->md5_sumed = 1;
}
tair_key.len = NGX_HTTP_TFS_DUPLICATE_KEY_SIZE;
tair_key.data = ctx->tair_key;
tair_key.type = NGX_HTTP_TAIR_BYTEARRAY;
tair_value.len = NGX_HTTP_TFS_DUPLICATE_VALUE_BASE_SIZE + ctx->dup_file_name.len;
tair_value.data = ngx_pcalloc(pool, tair_value.len);
if (tair_value.data == NULL) {
return NGX_ERROR;
}
ngx_memcpy(tair_value.data, &ctx->file_ref_count, NGX_HTTP_TFS_DUPLICATE_VALUE_BASE_SIZE);
ngx_memcpy(tair_value.data + NGX_HTTP_TFS_DUPLICATE_VALUE_BASE_SIZE,
ctx->dup_file_name.data, ctx->dup_file_name.len);
tair_value.type = NGX_HTTP_TAIR_BYTEARRAY;
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, log, 0,
"set duplicate info: file name: %V, file ref count: %d, dup_version: %d",
&ctx->dup_file_name, ctx->file_ref_count, ctx->dup_version);
rc = ngx_http_tfs_tair_put_helper(ctx->tair_instance, pool, log,
&tair_key, &tair_value, 0/*expire*/, ctx->dup_version,
ngx_http_tfs_duplicate_put_handler, ctx);
return rc;
}
static void
ngx_http_tfs_duplicate_put_handler(ngx_int_t rc, void *data)
{
ngx_http_tfs_dedup_ctx_t *ctx;
ctx = data;
if (rc == NGX_HTTP_ETAIR_SUCCESS) {
rc = NGX_OK;
} else {
rc = NGX_ERROR;
}
ngx_http_tfs_duplicate_callback(ctx, rc);
}
ngx_int_t
ngx_http_tfs_delete_duplicate_info(ngx_http_tfs_dedup_ctx_t *ctx,
ngx_pool_t *pool, ngx_log_t * log, ngx_chain_t *data)
{
u_char *p;
ssize_t data_len;
ngx_int_t rc;
ngx_array_t tair_keys;
ngx_http_tair_data_t *tair_key;
data_len = 0;
if (!ctx->md5_sumed) {
rc = ngx_http_tfs_sum_md5(data, ctx->tair_key, &data_len, log);
if (rc == NGX_ERROR) {
return NGX_ERROR;
}
p = ctx->tair_key;
p += NGX_HTTP_TFS_MD5_RESULT_LEN;
*(uint32_t *) p = htonl(data_len);
ctx->md5_sumed = 1;
}
rc = ngx_array_init(&tair_keys, pool, 1, sizeof(ngx_http_tair_data_t));
if (rc == NGX_ERROR) {
return rc;
}
tair_key = (ngx_http_tair_data_t*) ngx_array_push(&tair_keys);
tair_key->type = NGX_HTTP_TAIR_BYTEARRAY;
tair_key->data = ctx->tair_key;
tair_key->len = NGX_HTTP_TFS_DUPLICATE_KEY_SIZE;
rc = ngx_http_tfs_tair_delete_helper(ctx->tair_instance, pool, log,
&tair_keys,
ngx_http_tfs_duplicate_delete_handler, ctx);
return rc;
}
static void
ngx_http_tfs_duplicate_delete_handler(ngx_int_t rc, void *data)
{
ngx_http_tfs_dedup_ctx_t *ctx;
ctx = data;
if (rc == NGX_HTTP_ETAIR_SUCCESS) {
rc = NGX_OK;
} else {
rc = NGX_ERROR;
}
ngx_http_tfs_duplicate_callback(ctx, rc);
}
ngx_int_t
ngx_http_tfs_duplicate_check_suffix(ngx_str_t *tfs_name, ngx_str_t *suffix)
{
ngx_int_t rc;
rc = NGX_ERROR;
if ((tfs_name->len == NGX_HTTP_TFS_FILE_NAME_LEN && suffix->len == 0)
|| (tfs_name->len > NGX_HTTP_TFS_FILE_NAME_LEN && suffix->len > 0
&& ((tfs_name->len - NGX_HTTP_TFS_FILE_NAME_LEN) == suffix->len)
&& (ngx_strncmp(suffix->data, tfs_name->data + NGX_HTTP_TFS_FILE_NAME_LEN, suffix->len)
== 0)))
{
rc = NGX_OK;
}
return rc;
}
ngx_int_t
ngx_http_tfs_duplicate_check_filename(ngx_str_t *dup_file_name, ngx_http_tfs_raw_fsname_t* fsname)
{
ngx_int_t rc;
ngx_str_t dup_file_suffix = ngx_null_string;
ngx_http_tfs_raw_fsname_t dup_fsname;
rc = ngx_http_tfs_raw_fsname_parse(dup_file_name, &dup_file_suffix, &dup_fsname);
if (rc == NGX_OK) {
if (fsname->cluster_id == dup_fsname.cluster_id
&& fsname->file.block_id == dup_fsname.file.block_id
&& fsname->file.seq_id == dup_fsname.file.seq_id
&& fsname->file.suffix == dup_fsname.file.suffix)
{
return NGX_OK;
}
}
return NGX_ERROR;
}
static void
ngx_http_tfs_duplicate_callback(ngx_http_tfs_dedup_ctx_t *ctx, ngx_int_t rc)
{
ngx_http_tfs_t *t;
ngx_http_tfs_rcs_info_t *rc_info;
t = ctx->data;
rc_info = t->rc_info_node;
switch (t->r_ctx.action.code) {
case NGX_HTTP_TFS_ACTION_REMOVE_FILE:
switch(t->state) {
case NGX_HTTP_TFS_STATE_REMOVE_READ_META_SEGMENT:
/* exist in tair */
if (rc == NGX_OK) {
/* check file name */
rc = ngx_http_tfs_duplicate_check_filename(&ctx->dup_file_name, &t->r_ctx.fsname);
if (rc == NGX_OK) {
/* file name match, modify ref count and save tair */
if (t->r_ctx.unlink_type == NGX_HTTP_TFS_UNLINK_DELETE) {
if (--ctx->file_ref_count <= 0) {
/* if ref count is 0, remove key from tair then unlink file */
t->state = NGX_HTTP_TFS_STATE_REMOVE_GET_BLK_INFO;
t->is_stat_dup_file = NGX_HTTP_TFS_NO;
t->tfs_peer->body_buffer = ctx->save_body_buffer;
rc = ngx_http_tfs_delete_duplicate_info(ctx, t->pool, t->log,
t->meta_segment_data);
/* do not care delete tair fail, go on unlinking file */
if (rc == NGX_ERROR) {
ngx_http_tfs_finalize_state(t, NGX_OK);
}
return;
}
/* file_ref_count > 0, just save tair */
t->state = NGX_HTTP_TFS_STATE_REMOVE_DONE;
rc = ngx_http_tfs_set_duplicate_info(ctx, t->pool, t->log,
t->meta_segment_data);
/* do not care save tair fail, return success */
if (rc == NGX_ERROR) {
ngx_http_tfs_finalize_state(t, NGX_DONE);
}
return;
}
}
/* file name not match, unlink file */
t->tfs_peer->body_buffer = ctx->save_body_buffer;
t->state = NGX_HTTP_TFS_STATE_REMOVE_GET_BLK_INFO;
t->is_stat_dup_file = NGX_HTTP_TFS_NO;
ngx_http_tfs_finalize_state(t, NGX_OK);
return;
}
/* not exist in tair, unlink file */
t->tfs_peer->body_buffer = ctx->save_body_buffer;
t->state = NGX_HTTP_TFS_STATE_REMOVE_GET_BLK_INFO;
t->is_stat_dup_file = NGX_HTTP_TFS_NO;
ngx_http_tfs_finalize_state(t, NGX_OK);
return;
case NGX_HTTP_TFS_STATE_REMOVE_GET_BLK_INFO:
case NGX_HTTP_TFS_STATE_REMOVE_DELETE_DATA:
ngx_http_tfs_finalize_state(t, NGX_OK);
return;
case NGX_HTTP_TFS_STATE_REMOVE_DONE:
ngx_http_tfs_finalize_state(t, NGX_DONE);
return;
}
break;
case NGX_HTTP_TFS_ACTION_WRITE_FILE:
switch(t->state) {
case NGX_HTTP_TFS_STATE_WRITE_CLUSTER_ID_NS:
case NGX_HTTP_TFS_STATE_WRITE_GET_BLK_INFO:
/* exist in tair */
if (rc == NGX_OK) {
/* check suffix */
rc = ngx_http_tfs_duplicate_check_suffix(&ctx->dup_file_name, &t->r_ctx.file_suffix);
if (rc == NGX_OK) {
/* suffix match, need to stat file */
rc = ngx_http_tfs_raw_fsname_parse(&ctx->dup_file_name, &ctx->dup_file_suffix, &t->r_ctx.fsname);
if (rc == NGX_OK) {
t->file.cluster_id = t->r_ctx.fsname.cluster_id;
t->is_stat_dup_file = NGX_HTTP_TFS_YES;
t->state = NGX_HTTP_TFS_STATE_WRITE_GET_BLK_INFO;
}
} else {
/* suffix not match, need save new tfs file, do not save tair */
t->use_dedup = NGX_HTTP_TFS_NO;
}
} /* not exist in tair need save new tfs file and tair */
/* need reset meta segment */
rc = ngx_http_tfs_get_meta_segment(t);
if (rc != NGX_OK) {
ngx_log_error(NGX_LOG_ERR, t->log, 0,
"tfs get meta segment failed");
ngx_http_tfs_finalize_request(t->data, t, NGX_HTTP_INTERNAL_SERVER_ERROR);
return;
}
/* lookup block cache */
if (t->is_stat_dup_file) {
/* dedup write may need to stat file */
if (rc_info->use_remote_block_cache) {
rc = ngx_http_tfs_get_remote_block_cache_instance(&t->block_cache_ctx.remote_ctx,
&rc_info->remote_block_cache_info);
if (rc == NGX_ERROR) {
ngx_log_error(NGX_LOG_ERR, t->log, 0,
"get remote block cache instance failed.");
} else {
t->block_cache_ctx.use_cache |= NGX_HTTP_TFS_REMOTE_BLOCK_CACHE;
}
}
rc = ngx_http_tfs_lookup_block_cache(t, &t->file.segment_data[t->file.segment_index]);
if (rc == NGX_ERROR) {
ngx_log_error(NGX_LOG_ERR, t->log, 0,
"lookup block cache failed");
}
if (rc == NGX_DECLINED
&& (t->block_cache_ctx.use_cache & NGX_HTTP_TFS_REMOTE_BLOCK_CACHE))
{
return;
}
}
ngx_http_tfs_finalize_state(t, NGX_OK);
break;
case NGX_HTTP_TFS_STATE_WRITE_STAT_DUP_FILE:
if (rc == NGX_OK) {
t->state = NGX_HTTP_TFS_STATE_WRITE_DONE;
ngx_http_tfs_finalize_state(t, NGX_DONE);
} else {
/* save tair(add ref count) failed, need save new tfs file, do not save tair */
t->state = NGX_HTTP_TFS_STATE_WRITE_CLUSTER_ID_NS;
t->is_stat_dup_file = NGX_HTTP_TFS_NO;
t->use_dedup = NGX_HTTP_TFS_NO;
/* need reset output buf */
t->out_bufs = NULL;
/* need reset block id and file id */
t->file.segment_data[0].segment_info.block_id = 0;
t->file.segment_data[0].segment_info.file_id = 0;
ngx_http_tfs_finalize_state(t, NGX_OK);
}
break;
case NGX_HTTP_TFS_STATE_WRITE_DONE:
ngx_http_tfs_finalize_state(t, NGX_DONE);
break;
}
}
return;
}
#ifdef NGX_HTTP_TFS_USE_TAIR
ngx_int_t
ngx_http_tfs_get_dedup_instance(ngx_http_tfs_dedup_ctx_t *ctx,
ngx_http_tfs_tair_server_addr_info_t *server_addr_info,
uint32_t server_addr_hash)
{
ngx_str_t *st;
ngx_int_t rc, i;
ngx_array_t config_server;
ngx_http_tfs_t *t;
ngx_http_etair_server_conf_t *server;
ngx_http_tfs_tair_instance_t *instance;
t = ctx->data;
for (i = 0; i < NGX_HTTP_TFS_MAX_CLUSTER_COUNT; i++) {
instance = &t->main_conf->dup_instances[i];
if (instance->server == NULL) {
break;
}
if (instance->server_addr_hash == server_addr_hash) {
ctx->tair_instance = instance;
return NGX_OK;
}
}
/* not found && full, clear */
if (i > NGX_HTTP_TFS_MAX_CLUSTER_COUNT) {
for (i = 0; i < NGX_HTTP_TFS_MAX_CLUSTER_COUNT; i++) {
instance = &t->main_conf->dup_instances[i];
if (instance->server != NULL) {
ngx_http_etair_destory_server(instance->server, (ngx_cycle_t *) ngx_cycle);
instance->server = NULL;
}
}
instance = &t->main_conf->dup_instances[0];
}
rc = ngx_array_init(&config_server, t->pool, NGX_HTTP_TFS_TAIR_CONFIG_SERVER_COUNT, sizeof(ngx_str_t));
if (rc == NGX_ERROR) {
return NGX_ERROR;
}
for (i = 0; i < NGX_HTTP_TFS_TAIR_CONFIG_SERVER_COUNT; i++) {
if (server_addr_info->server[i].len > 0 ) {
st = (ngx_str_t *) ngx_array_push(&config_server);
*st = server_addr_info->server[i];
}
}
server = ngx_http_etair_create_server(&server_addr_info->server[NGX_HTTP_TFS_TAIR_CONFIG_SERVER_COUNT],
&config_server,
t->main_conf->tair_timeout,
(ngx_cycle_t *) ngx_cycle);
if (server == NULL) {
return NGX_ERROR;
}
instance->server = server;
instance->server_addr_hash = server_addr_hash;
instance->area = server_addr_info->area;
ctx->tair_instance = instance;
return NGX_OK;
}
#else
ngx_int_t
ngx_http_tfs_get_dedup_instance(ngx_http_tfs_dedup_ctx_t *ctx,
ngx_http_tfs_tair_server_addr_info_t *server_addr_info,
uint32_t server_addr_hash)
{
return NGX_ERROR;
}
#endif