1 /**
2 * Copyright (C) 2008 Happy Fish / YuQing
3 *
4 * FastDFS may be copied only under the terms of the GNU General
5 * Public License V3, which may be found in the FastDFS source kit.
6 * Please visit the FastDFS Home Page http://www.fastken.com/ for more detail.
7 **/
8 
9 //tracker_nio.h
10 
11 #ifndef _TRACKER_NIO_H
12 #define _TRACKER_NIO_H
13 
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/time.h>
18 #include "tracker_types.h"
19 #include "storage_func.h"
20 #include "fastcommon/fast_task_queue.h"
21 #include "storage_global.h"
22 #include "fdht_types.h"
23 #include "trunk_mem.h"
24 #include "fastcommon/md5.h"
25 
26 #define FDFS_STORAGE_STAGE_NIO_INIT   0
27 #define FDFS_STORAGE_STAGE_NIO_RECV   1
28 #define FDFS_STORAGE_STAGE_NIO_SEND   2
29 #define FDFS_STORAGE_STAGE_NIO_CLOSE  4  //close socket
30 #define FDFS_STORAGE_STAGE_DIO_THREAD 8
31 
32 #define FDFS_STORAGE_FILE_OP_READ     'R'
33 #define FDFS_STORAGE_FILE_OP_WRITE    'W'
34 #define FDFS_STORAGE_FILE_OP_APPEND   'A'
35 #define FDFS_STORAGE_FILE_OP_DELETE   'D'
36 #define FDFS_STORAGE_FILE_OP_DISCARD  'd'
37 
38 typedef int (*TaskDealFunc)(struct fast_task_info *pTask);
39 
40 /* this clean func will be called when connection disconnected */
41 typedef void (*DisconnectCleanFunc)(struct fast_task_info *pTask);
42 
43 typedef void (*DeleteFileLogCallback)(struct fast_task_info *pTask, \
44 		const int err_no);
45 
46 typedef void (*FileDealDoneCallback)(struct fast_task_info *pTask, \
47 		const int err_no);
48 
49 typedef int (*FileDealContinueCallback)(struct fast_task_info *pTask);
50 
51 typedef int (*FileBeforeOpenCallback)(struct fast_task_info *pTask);
52 typedef int (*FileBeforeCloseCallback)(struct fast_task_info *pTask);
53 
54 #define _FILE_TYPE_APPENDER  1
55 #define _FILE_TYPE_TRUNK     2   //if trunk file, since V3.0
56 #define _FILE_TYPE_SLAVE     4
57 #define _FILE_TYPE_REGULAR   8
58 #define _FILE_TYPE_LINK     16
59 
60 typedef struct
61 {
62 	bool if_gen_filename;	  //if upload generate filename
63 	char file_type;           //regular or link file
64 	bool if_sub_path_alloced; //if sub path alloced since V3.0
65 	char master_filename[128];
66 	char file_ext_name[FDFS_FILE_EXT_NAME_MAX_LEN + 1];
67 	char formatted_ext_name[FDFS_FILE_EXT_NAME_MAX_LEN + 2];
68 	char prefix_name[FDFS_FILE_PREFIX_MAX_LEN + 1];
69 	char group_name[FDFS_GROUP_NAME_MAX_LEN + 1];  	//the upload group name
70 	int start_time;		//upload start timestamp
71 	FDFSTrunkFullInfo trunk_info;
72 	FileBeforeOpenCallback before_open_callback;
73 	FileBeforeCloseCallback before_close_callback;
74 } StorageUploadInfo;
75 
76 typedef struct
77 {
78 	char op_flag;
79 	char *meta_buff;
80 	int meta_bytes;
81 } StorageSetMetaInfo;
82 
83 typedef struct
84 {
85 	char filename[MAX_PATH_SIZE + 128];  	//full filename
86 
87 	/* FDFS logic filename to log not including group name */
88 	char fname2log[128+sizeof(FDFS_STORAGE_META_FILE_EXT)];
89 
90 	char op;            //w for writing, r for reading, d for deleting etc.
91 	char sync_flag;     //sync flag log to binlog
92 	bool calc_crc32;    //if calculate file content hash code
93 	bool calc_file_hash;      //if calculate file content hash code
94 	int open_flags;           //open file flags
95 	int file_hash_codes[4];   //file hash code
96 	int64_t crc32;            //file content crc32 signature
97 	MD5_CTX md5_context;
98 
99 	union
100 	{
101 		StorageUploadInfo upload;
102 		StorageSetMetaInfo setmeta;
103 	} extra_info;
104 
105 	int dio_thread_index;		//dio thread index
106 	int timestamp2log;		//timestamp to log
107 	int delete_flag;     //delete file flag
108 	int create_flag;    //create file flag
109 	int buff_offset;    //buffer offset after recv to write to file
110 	int fd;         //file description no
111 	int64_t start;  //the start offset of file
112 	int64_t end;    //the end offset of file
113 	int64_t offset; //the current offset of file
114     FileDealContinueCallback continue_callback;
115 	FileDealDoneCallback done_callback;
116 	DeleteFileLogCallback log_callback;
117 
118 	struct timeval tv_deal_start; //task deal start tv for access log
119 } StorageFileContext;
120 
121 typedef struct
122 {
123 	int nio_thread_index;  //nio thread index
124 	bool canceled;
125 	char stage;  //nio stage, send or recv
126 	char storage_server_id[FDFS_STORAGE_ID_MAX_SIZE];
127 
128 	StorageFileContext file_context;
129 
130 	int64_t total_length;   //pkg total length for req and request
131 	int64_t total_offset;   //pkg current offset for req and request
132 
133 	int64_t request_length;   //request pkg length for access log
134 
135 	FDFSStorageServer *pSrcStorage;
136 	TaskDealFunc deal_func;  //function pointer to deal this task
137 	void *extra_arg;   //store extra arg, such as (BinLogReader *)
138 	DisconnectCleanFunc clean_func;  //clean function pointer when finished
139 } StorageClientInfo;
140 
141 struct storage_nio_thread_data
142 {
143 	struct nio_thread_data thread_data;
144 	GroupArray group_array;  //FastDHT group array
145 };
146 
147 #ifdef __cplusplus
148 extern "C" {
149 #endif
150 
151 void storage_recv_notify_read(int sock, short event, void *arg);
152 int storage_send_add_event(struct fast_task_info *pTask);
153 
154 void task_finish_clean_up(struct fast_task_info *pTask);
155 void add_to_deleted_list(struct fast_task_info *pTask);
156 
157 #ifdef __cplusplus
158 }
159 #endif
160 
161 #endif
162 
163