summaryrefslogtreecommitdiff
path: root/src/include/tango_cache_client.h
blob: 1a3493afceae8fbcc05e56e1dcd713b511ee32e9 (plain)
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
#ifndef __TANGO_CACHE_CLIENT_H__
#define __TANGO_CACHE_CLIENT_H__

#include <event2/event.h>  
#include <event.h>

#include <tfe_future.h>
#include "tango_cache_pending.h"

#define USER_TAG_MAX_LEN 1518

enum CACHE_ERR_CODE
{
	CACHE_OK=0,
	CACHE_CACHE_MISS  = -101, //�������
	CACHE_TIMEOUT     = -102,	//���泬ʱ
	CACHE_OUTOF_MEMORY= -103,//��ǰ�ڴ�ռ�ó������ƣ��鿴MAX_USED_MEMORY_SIZE_MB�Ƿ��С���ߵ�ǰ�ϴ����ʸ����ϵ����ߵ�����
	CACHE_ERR_CURL    = -104,
	CACHE_ERR_WIREDLB = -105,
	CACHE_ERR_SOCKPAIR= -106,
	CACHE_ERR_INTERNAL= -107,
	CACHE_ERR_REDIS_JSON   = -108,
	CACHE_ERR_REDIS_CONNECT= -109,
	CACHE_ERR_REDIS_EXEC   = -110,
	CACHE_OUTOF_SESSION    = -111,
	CACHE_ERR_EVBUFFER     = -112,
	CACHE_UPDATE_CANCELED  = -113,
	CACHE_ERR_INSTANCE_ID  = -114,
	CACHE_ERR_BAD_REQUEST  = -115,
};

struct cache_statistics
{
	long long get_recv_num;  //����GET�Ĵ���
	long long get_succ_http; //GET minio�ɹ��Ĵ���
	long long get_succ_redis;//GET redis�ɹ��Ĵ���
	long long get_miss_num;	 //GETδ���еĴ���
	long long get_err_http;  //GET minioʧ�ܵĴ���
	long long get_err_redis; //GET redisʧ�ܵĴ���
	long long put_recv_num;	 //����UPLOAD�Ĵ���
	long long put_succ_http; //UPLOAD minio�ɹ��Ĵ���
	long long put_succ_redis;//UPLOAD redis�ɹ��Ĵ���
	long long put_err_http;  //UPLOAD minioʧ�ܵĴ���
	long long put_err_redis; //UPLOAD redisʧ�ܵĴ���
	long long put_cancel;    //UPLOAD Cancel����
	long long put_losf_num;  //LOSF�ϲ���ĸ���
	long long put_multipart; //�����˷ֶ��ϴ��Ķ������
	long long put_duplicate; //LOSF�ظ���
	long long del_recv_num;	 //����DELETE�Ĵ���
	long long del_succ_num;	 //DELETE�ɹ��Ĵ���
	long long del_error_num; //DELETEʧ�ܵĴ���
	long long totaldrop_num; //�ڴ����Լ�WiredLB����ʱDROP�Ĵ���
	long long memory_used;	 //��ǰUPLOAD BODY��ռ�ڴ��С
	long long session_http;	 //��ǰ���ڽ���GET/PUT��HTTP�Ự��
	long long session_redis; //��ǰ���ڽ���GET/PUT��redis�Ự��
};


struct tango_cache_parameter;
struct tango_cache_instance;
struct tango_cache_ctx;

enum CACHE_ERR_CODE tango_cache_get_last_error(const struct tango_cache_ctx *ctx);
enum CACHE_ERR_CODE tango_cache_ctx_error(const struct tango_cache_instance *instance);
long long tango_cache_maximum_session(const struct tango_cache_instance *instance);
void tango_cache_get_statistics(const struct tango_cache_instance *instance, struct cache_statistics *out);

/*ÿ������ִ��һ�γ�ʼ��*/
void tango_cache_global_init(void);

//ÿ��minio��Ⱥ��bucket����һ��parameter�����instance�ɹ���һ��parameter
struct tango_cache_parameter *tango_cache_parameter_new(const char* profile_path, const char* section, void *runtimelog);
/*��������API�̲߳���ȫ*/
//ÿ�������̴߳���һ��instance
struct tango_cache_instance *tango_cache_instance_new(struct tango_cache_parameter *param, struct event_base* evbase, void *runtimelog);


/****************************************** GET�ӿڵ�API ******************************************/
enum CACHE_RESULT_TYPE
{
	RESULT_TYPE_HEADER=0, //���ֻ����һ��
	RESULT_TYPE_USERTAG,  //���ֻ����һ��
	RESULT_TYPE_BODY,     //���ܵ��ö��
	RESULT_TYPE_END,	  //ȫ��������ֻ����һ�Σ�����������
	RESULT_TYPE_MISS,	  //����δ���У����������ͻ��⣬ֻ����һ��(��END֮��)������������
};

enum OBJECT_LOCATION
{
	OBJECT_IN_UNKNOWN=0,
	OBJECT_IN_MINIO,
	OBJECT_IN_REDIS
};

//Range֧��3��: bytes=x-y; bytes=x-; bytes=-y
struct tango_cache_get_range
{
	int64_t start_offset; // -1��ʾ������
	int64_t end_offset;   // -1��ʾ������
};

struct tango_cache_meta_get
{
	const char* url; //����:URL���ǽṹ����־:�ļ�·������CACHE_OBJECT_KEY_HASH_SWITCH=0ʱ��󳤶�256�ֽڣ�=1ʱ������
	struct request_freshness get;
	struct tango_cache_get_range *range; //����ͷRange
};

//promise_success�Ľ��result
struct tango_cache_result
{
	const char *data_frag; //���typeΪRESULT_TYPE_HEADER��ÿ��ͷ��������һ������(HTTP1.1��ʽ)
	size_t size;   //����Ƭ�εij���
	size_t tlength;//��������response body���Ƶ��ܳ���(��Range����ͷʱ������Ĵ�С)���ص�ʱ����Ч
	enum CACHE_RESULT_TYPE type;
	enum OBJECT_LOCATION location;
};

//�ɹ�ʱ�ص�promise_success
//ʧ��ʱ�ص�promise_failed(��һ��)��ʹ��get_last_error��ȡ�����룻
//future������ΪNULL
int tango_cache_fetch_object(struct tango_cache_instance *instance, struct future* f, struct tango_cache_meta_get *meta, enum OBJECT_LOCATION where_to_get);
int tango_cache_head_object(struct tango_cache_instance *instance, struct future* f, struct tango_cache_meta_get *meta);
//��promise_success��result������ȡ���
struct tango_cache_result *tango_cache_read_result(future_result_t *promise_result);


/****************************************** DELETE�ӿڵ�API ******************************************/
int tango_cache_delete_object(struct tango_cache_instance *instance, struct future* f, const char *objkey, const char *minio_addr=NULL, const char *bucket=NULL);


/****************************************** UPLOAD�ӿڵ�API ******************************************/
/* ע��: ��future��ΪNULL�������ϴ�����ʱ�����֪ͨ�ص����������򲻵��ã�*/

enum PUT_MEMORY_COPY_WAY
{
	PUT_MEM_COPY=0, //��������ڴ�
	PUT_MEM_FREE,   //�������ڴ棬��������ɱ�����ģ���ͷŸ��ڴ�
};
enum EVBUFFER_COPY_WAY
{
	EVBUFFER_MOVE=0,//evbuffer_add_buffer
	EVBUFFER_COPY,  //evbuffer_add_buffer_reference�����Ƽ�
};

enum CACHE_HTTP_HDR_TYPE
{
	HDR_CONTENT_TYPE=0,
	HDR_CONTENT_ENCODING,
	HDR_CONTENT_DISPOSITION,
	HDR_CONTENT_MD5,
	
	HDR_CONTENT_NUM,
};

struct tango_cache_meta_put
{
	const char* url;
	const char* std_hdr[HDR_CONTENT_NUM]; //����ͷ������"Content-Type: text/html"����Ҫ�������У�NULL��ʾû�и�ͷ����
	const char* usertag; //�������������ݣ�GETʱ��ԭ������
	size_t usertag_len;  //��󳤶�USER_TAG_MAX_LEN��0��ʾû�и�ͷ��
	struct response_freshness put;
};

/****************************************** ����һ��UPLOAD�ӿڵ�API ******************************************/
//��path��Ϊ�գ����������Ĵ洢·��
//����ֵ: 0-�ɹ���<0ʧ�ܣ���ͬ
int tango_cache_upload_once_data(struct tango_cache_instance *instance, struct future* f, 
                                            enum PUT_MEMORY_COPY_WAY way, const char *data, size_t size, 
                                            struct tango_cache_meta_put *meta, 
                                            char *path/*OUT*/, size_t pathsize);
int tango_cache_upload_once_evbuf(struct tango_cache_instance *instance, struct future* f, 
                                            enum EVBUFFER_COPY_WAY way, struct evbuffer *evbuf, 
                                            struct tango_cache_meta_put *meta, 
                                            char *path/*OUT*/, size_t pathsize);

/****************************************** ��ʽUPLOAD�ӿڵ�API ******************************************/
//����ֵ: ��ΪNULL���ʾ����ʧ�ܣ�����tango_cache_ctx_error�鿴�������Ƿ���CACHE_OUTOF_MEMORY(�����������)��
struct tango_cache_ctx *tango_cache_update_start(struct tango_cache_instance *instance, struct future* f, struct tango_cache_meta_put *meta);
//����ֵ: 0-�ɹ���<0ʧ�ܣ�����tango_cache_get_last_error�鿴�����룻
int tango_cache_update_frag_data(struct tango_cache_ctx *ctx, const char *data, size_t size);
int tango_cache_update_frag_evbuf(struct tango_cache_ctx *ctx, enum EVBUFFER_COPY_WAY way, struct evbuffer *evbuf);
//ע��: ����ʧ��ʱ���ٵ���promise�ص�������path��ΪNULLʱ���ش洢·��
int tango_cache_update_end(struct tango_cache_ctx *ctx, char *path/*OUT*/, size_t pathsize);
//����cancel�󲻻��������ص�����������ʧ��
void tango_cache_update_cancel(struct tango_cache_ctx *ctx);

#endif