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 //trunk_mem.h
10 
11 #ifndef _TRUNK_MEM_H_
12 #define _TRUNK_MEM_H_
13 
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <time.h>
17 #include <unistd.h>
18 #include <pthread.h>
19 #include "fastcommon/common_define.h"
20 #include "fdfs_global.h"
21 #include "fastcommon/fast_mblock.h"
22 #include "trunk_shared.h"
23 #include "fdfs_shared_func.h"
24 
25 #define STORAGE_TRUNK_COMPRESS_STAGE_NONE                0
26 #define STORAGE_TRUNK_COMPRESS_STAGE_COMPRESS_BEGIN      1
27 #define STORAGE_TRUNK_COMPRESS_STAGE_APPLY_DONE          2
28 #define STORAGE_TRUNK_COMPRESS_STAGE_SAVE_DONE           3
29 #define STORAGE_TRUNK_COMPRESS_STAGE_COMMIT_MERGING      4
30 #define STORAGE_TRUNK_COMPRESS_STAGE_COMMIT_MERGE_DONE   5
31 #define STORAGE_TRUNK_COMPRESS_STAGE_COMPRESS_SUCCESS    6
32 #define STORAGE_TRUNK_COMPRESS_STAGE_ROLLBACK_MERGING    7
33 #define STORAGE_TRUNK_COMPRESS_STAGE_ROLLBACK_MERGE_DONE 8
34 #define STORAGE_TRUNK_COMPRESS_STAGE_FINISHED            9
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 extern int g_slot_min_size;    //slot min size, such as 256 bytes
41 extern int g_slot_max_size;    //slot max size
42 extern int g_trunk_alloc_alignment_size;  //the alignment size for trunk alloc
43 extern int g_trunk_file_size;  //the trunk file size, such as 64MB
44 extern int g_store_path_mode;  //store which path mode, fetch from tracker
45 extern FDFSStorageReservedSpace g_storage_reserved_space;  //fetch from tracker
46 extern int g_avg_storage_reserved_mb;  //calc by above var: g_storage_reserved_mb
47 extern int g_store_path_index;  //store to which path
48 extern volatile int g_current_trunk_file_id;  //current trunk file id
49 extern TimeInfo g_trunk_create_file_time_base;
50 extern TimeInfo g_trunk_compress_binlog_time_base;
51 extern int g_trunk_create_file_interval;
52 extern int g_trunk_compress_binlog_min_interval;
53 extern int g_trunk_compress_binlog_interval;
54 extern int g_trunk_binlog_max_backups;
55 extern TrackerServerInfo g_trunk_server;  //the trunk server
56 extern bool g_if_use_trunk_file;   //if use trunk file
57 extern bool g_trunk_create_file_advance;
58 extern bool g_trunk_init_check_occupying;
59 extern bool g_trunk_init_reload_from_binlog;
60 extern bool g_trunk_free_space_merge;
61 extern bool g_delete_unused_trunk_files;
62 extern int g_trunk_binlog_compress_stage;
63 extern bool g_if_trunker_self;   //if am i trunk server
64 extern int64_t g_trunk_create_file_space_threshold;
65 extern volatile int64_t g_trunk_total_free_space;  //trunk total free space in bytes
66 extern time_t g_trunk_last_compress_time;
67 
68 typedef struct tagFDFSTrunkNode {
69 	FDFSTrunkFullInfo trunk;    //trunk info
70 	struct fast_mblock_node *pMblockNode;   //for free
71 	struct tagFDFSTrunkNode *next;
72 } FDFSTrunkNode;
73 
74 typedef struct {
75 	int size;
76 	FDFSTrunkNode *head;
77 	struct fast_mblock_node *pMblockNode;   //for free
78 } FDFSTrunkSlot;
79 
80 int storage_trunk_init();
81 int storage_trunk_destroy_ex(const bool bNeedSleep,
82         const bool bSaveData);
83 
84 #define storage_trunk_destroy() storage_trunk_destroy_ex(false, true)
85 
86 int trunk_alloc_space(const int size, FDFSTrunkFullInfo *pResult);
87 int trunk_alloc_confirm(const FDFSTrunkFullInfo *pTrunkInfo, const int status);
88 
89 int trunk_free_space(const FDFSTrunkFullInfo *pTrunkInfo, \
90 		const bool bWriteBinLog);
91 
92 bool trunk_check_size(const int64_t file_size);
93 
94 #define trunk_init_file(filename) \
95 	trunk_init_file_ex(filename, g_trunk_file_size)
96 
97 #define trunk_check_and_init_file(filename) \
98 	trunk_check_and_init_file_ex(filename, g_trunk_file_size)
99 
100 int trunk_init_file_ex(const char *filename, const int64_t file_size);
101 
102 int trunk_check_and_init_file_ex(const char *filename, const int64_t file_size);
103 
104 int trunk_file_delete(const char *trunk_filename, \
105 		const FDFSTrunkFullInfo *pTrunkInfo);
106 
107 int trunk_create_trunk_file_advance(void *args);
108 
109 int trunk_binlog_compress_func(void *args);
110 
111 int storage_trunk_binlog_compress_check_recovery();
112 
113 char *storage_trunk_get_data_filename(char *full_filename);
114 
115 #define storage_check_reserved_space(pGroup) \
116         fdfs_check_reserved_space(pGroup, &g_storage_reserved_space)
117 
118 #define storage_check_reserved_space_trunk(pGroup) \
119         fdfs_check_reserved_space_trunk(pGroup, &g_storage_reserved_space)
120 
121 #define storage_check_reserved_space_path(total_mb, free_mb, avg_mb) \
122         fdfs_check_reserved_space_path(total_mb, free_mb, avg_mb, \
123                                 &g_storage_reserved_space)
124 
125 #define storage_get_storage_reserved_space_mb(total_mb) \
126 	fdfs_get_storage_reserved_space_mb(total_mb, &g_storage_reserved_space)
127 
128 #ifdef __cplusplus
129 }
130 #endif
131 
132 #endif
133 
134