forked from alibaba/nginx-tfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngx_http_tfs.h
347 lines (256 loc) · 12.1 KB
/
ngx_http_tfs.h
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
/*
* Copyright (C) 2010-2012 Alibaba Group Holding Limited
*/
#ifndef _NGX_HTTP_TFS_H_INCLUDED_
#define _NGX_HTTP_TFS_H_INCLUDED_
#include <ngx_core.h>
#include <ngx_http.h>
#include <ngx_tfs_common.h>
#include <ngx_http_tfs_json.h>
#include <ngx_http_tfs_protocol.h>
#include <ngx_http_tfs_restful.h>
#include <ngx_http_connection_pool.h>
#include <ngx_http_tfs_rc_server_info.h>
#include <ngx_http_tfs_block_cache.h>
#include <ngx_http_tfs_duplicate.h>
#include <ngx_http_tfs_raw_fsname.h>
#include <ngx_http_tfs_peer_connection.h>
#include <ngx_http_tfs_tair_helper.h>
typedef ngx_table_elt_t *(*ngx_http_tfs_create_header_pt)(ngx_http_request_t *r);
typedef struct {
ngx_str_t state_msg;
ngx_int_t state;
} ngx_http_tfs_state_t;
typedef struct {
uint32_t ds_count;
ngx_http_tfs_inet_t *ds_addrs;
int32_t version;
int32_t lease_id;
} ngx_http_tfs_block_info_t;
typedef enum {
NGX_HTTP_TFS_FROM_NONE = 0,
NGX_HTTP_TFS_FROM_CACHE,
NGX_HTTP_TFS_FROM_NS,
} ngx_http_tfs_block_info_src_e;
typedef struct {
uint32_t block_id;
uint64_t file_id;
int64_t offset; /* offset in the file */
uint32_t size;
uint32_t crc;
} NGX_PACKED ngx_http_tfs_segment_info_t;
typedef struct {
u_char file_name[NGX_HTTP_TFS_FILE_NAME_LEN];
int64_t offset;
uint32_t size;
uint32_t crc;
} NGX_PACKED ngx_http_tfs_tmp_segment_info_t;
struct ngx_http_tfs_segment_data_s {
uint8_t cache_hit;
ngx_http_tfs_block_info_src_e block_info_src;
ngx_http_tfs_segment_info_t segment_info;
uint32_t oper_offset; /* read/write offset inside this segment */
uint32_t oper_size; /* read/write size inside this segment */
union {
uint64_t write_file_number;
};
ngx_http_tfs_block_info_t block_info;
ngx_uint_t ds_retry;
ngx_uint_t ds_index;
ngx_chain_t *data;
} NGX_PACKED;
typedef struct {
uint8_t still_have; // for custom file
uint32_t cluster_id;
uint32_t open_mode;
int64_t file_offset; // not for large_file's data
uint64_t left_length;
uint64_t file_hole_size;
uint32_t last_write_segment_index;
uint32_t segment_index;
uint32_t segment_count;
ngx_http_tfs_segment_data_t *segment_data;
} NGX_PACKED ngx_http_tfs_file_t;
typedef struct {
ngx_msec_t timeout;
size_t max_temp_file_size;
size_t temp_file_write_size;
size_t busy_buffers_size_conf;
size_t rcs_zone_size;
uint64_t meta_root_server;
ngx_http_tfs_meta_table_t meta_server_table;
} ngx_http_tfs_loc_conf_t;
typedef struct {
struct sockaddr_in local_addr;
u_char local_addr_text[NGX_INET_ADDRSTRLEN];
ngx_log_t *log;
} ngx_http_tfs_srv_conf_t;
typedef struct {
ngx_msec_t tfs_connect_timeout;
ngx_msec_t tfs_send_timeout;
ngx_msec_t tfs_read_timeout;
ngx_msec_t tair_timeout;
ngx_http_tfs_tair_instance_t dup_instances[NGX_HTTP_TFS_MAX_CLUSTER_COUNT];
size_t send_lowat;
size_t buffer_size;
size_t body_buffer_size;
size_t busy_buffers_size;
ngx_str_t lock_file;
ngx_msec_t rcs_interval;
ngx_shm_zone_t *rcs_shm_zone;
ngx_shm_zone_t *block_cache_shm_zone;
ngx_flag_t enable_remote_block_cache;
ngx_http_tfs_rc_ctx_t *rc_ctx;
ngx_http_tfs_tair_instance_t remote_block_cache_instance;
ngx_http_tfs_local_block_cache_ctx_t *local_block_cache_ctx;
ngx_flag_t rcs_kp_enable;
ngx_http_connection_pool_t *conn_pool;
ngx_addr_t *ups_addr;
ngx_flag_t enable_rcs;
uint32_t cluster_id;
} ngx_http_tfs_main_conf_t;
typedef ngx_int_t (*tfs_peer_handler_pt)(ngx_http_tfs_t *t);
typedef void (*ngx_http_tfs_handler_pt)(ngx_http_request_t *r, ngx_http_tfs_t *t);
typedef ngx_int_t (*ngx_http_tfs_sub_process_pt)(ngx_http_tfs_t *t);
typedef struct {
ngx_list_t headers;
ngx_uint_t status_n;
ngx_str_t status_line;
ngx_table_elt_t *status;
ngx_table_elt_t *date;
ngx_table_elt_t *server;
ngx_table_elt_t *connection;
ngx_table_elt_t *expires;
ngx_table_elt_t *etag;
ngx_table_elt_t *x_accel_expires;
ngx_table_elt_t *x_accel_redirect;
ngx_table_elt_t *x_accel_limit_rate;
ngx_table_elt_t *content_type;
ngx_table_elt_t *content_length;
ngx_table_elt_t *last_modified;
ngx_table_elt_t *location;
ngx_table_elt_t *accept_ranges;
ngx_table_elt_t *www_authenticate;
#if (NGX_HTTP_GZIP)
ngx_table_elt_t *content_encoding;
#endif
off_t content_length_n;
ngx_array_t cache_control;
} ngx_http_tfs_headers_in_t;
struct ngx_http_tfs_s {
ngx_http_tfs_handler_pt read_event_handler;
ngx_http_tfs_handler_pt write_event_handler;
ngx_http_tfs_peer_connection_t *tfs_peer;
ngx_http_tfs_peer_connection_t *tfs_peer_servers;
uint8_t tfs_peer_count;
ngx_http_tfs_loc_conf_t *loc_conf;
ngx_http_tfs_srv_conf_t *srv_conf;
ngx_http_tfs_main_conf_t *main_conf;
ngx_http_tfs_restful_ctx_t r_ctx;
ngx_output_chain_ctx_t output;
ngx_chain_writer_ctx_t writer;
ngx_chain_t *request_bufs;
ngx_chain_t *send_body;
ngx_pool_t *pool;
ngx_buf_t header_buffer;
ngx_chain_t *recv_chain;
ngx_chain_t *out_bufs;
ngx_chain_t *busy_bufs;
ngx_chain_t *free_bufs;
ngx_http_tfs_rcs_info_t *rc_info_node;
ngx_rbtree_node_t *node;
ngx_uint_t logical_cluster_index;
ngx_uint_t rw_cluster_index;
/* keep alive */
ngx_queue_t *curr_ka_queue;
/* header pointer */
void *header;
ngx_int_t header_size;
tfs_peer_handler_pt create_request;
tfs_peer_handler_pt input_filter;
tfs_peer_handler_pt retry_handler;
tfs_peer_handler_pt process_request_body;
tfs_peer_handler_pt finalize_request;
void *finalize_data;
void *data;
ngx_int_t request_sent;
ngx_uint_t sent_size;
off_t length;
ngx_log_t *log;
ngx_int_t parse_state;
/* final file name */
ngx_str_t file_name;
ngx_int_t state;
/* custom file */
ngx_http_tfs_custom_meta_info_t meta_info;
ngx_str_t last_file_path;
int64_t last_file_pid;
uint8_t last_file_type;
ngx_int_t *dir_lens;
ngx_int_t last_dir_level;
uint16_t orig_action;
ngx_array_t file_holes;
ngx_http_tfs_headers_in_t headers_in;
/* delete */
ngx_int_t group_count;
ngx_int_t group_seq;
/* name ip */
ngx_http_tfs_inet_t name_server_addr;
ngx_str_t name_server_addr_text;
ngx_http_tfs_json_gen_t *json_output;
ngx_uint_t status;
ngx_str_t status_line;
ngx_int_t tfs_status;
uint64_t output_size;
/* de-duplicate info */
ngx_http_tfs_dedup_ctx_t dedup_ctx;
/* stat info */
ngx_http_tfs_stat_info_t stat_info;
/* file info */
ngx_chain_t *meta_segment_data;
ngx_http_tfs_file_t file;
ngx_http_tfs_segment_head_t *seg_head;
ngx_http_tfs_raw_file_info_t file_info;
ngx_buf_t *readv2_rsp_tail_buf;
uint8_t read_ver;
/* block cache */
ngx_http_tfs_block_cache_ctx_t block_cache_ctx;
/* for parallel write segments */
ngx_http_tfs_t *parent;
ngx_http_tfs_t *next;
ngx_http_tfs_t *free_sts;
ngx_http_tfs_sub_process_pt sp_callback;
uint32_t sp_count;
uint32_t sp_done_count;
uint32_t sp_fail_count;
uint32_t sp_succ_count;
uint32_t sp_curr;
unsigned sp_ready:1;
unsigned header_only:1;
unsigned header_sent:1;
unsigned has_split_frag:1;
unsigned is_first_segment:1; /* for custrom file read */
unsigned use_dedup:1;
unsigned is_stat_dup_file:1;
unsigned is_large_file:1;
unsigned is_process_meta_seg:1;
unsigned retry_curr_ns:1;
};
ngx_int_t ngx_http_tfs_init(ngx_http_tfs_t *t);
void ngx_http_tfs_finalize_request(ngx_http_request_t *r,
ngx_http_tfs_t *t, ngx_int_t rc);
void ngx_http_tfs_finalize_state(ngx_http_tfs_t *t, ngx_int_t rc);
ngx_int_t ngx_http_tfs_reinit(ngx_http_request_t *r, ngx_http_tfs_t *t);
ngx_int_t ngx_http_tfs_connect(ngx_http_tfs_t *t);
ngx_int_t ngx_http_tfs_lookup_block_cache(ngx_http_tfs_t *t,
ngx_http_tfs_segment_data_t *segment_data);
ngx_int_t ngx_http_tfs_batch_lookup_block_cache(ngx_http_tfs_t *t);
void ngx_http_tfs_remove_block_cache(ngx_http_tfs_t *t,
ngx_http_tfs_segment_data_t *segment_data);
ngx_int_t ngx_http_tfs_set_output_appid(ngx_http_tfs_t *t, uint64_t app_id);
void ngx_http_tfs_set_custom_initial_parameters(ngx_http_tfs_t *t);
ngx_int_t ngx_http_tfs_misc_ctx_init(ngx_http_tfs_t *t, ngx_http_tfs_rcs_info_t *rc_node);
ngx_int_t ngx_http_tfs_batch_process_start(ngx_http_tfs_t *t);
ngx_int_t ngx_http_tfs_batch_process_end(ngx_http_tfs_t *t);
ngx_int_t ngx_http_tfs_batch_process_next(ngx_http_tfs_t *t);
#endif /* _NGX_TFS_H_INCLUDED_ */