summaryrefslogtreecommitdiff
path: root/src/frag_redis.c
blob: b129673406456d336b105d04db9931980e51076f (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
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <math.h>
#include <net/if.h>
#include <unistd.h> 
#include <stdio.h> 
#include <stdlib.h>

#include "hiredis.h"
#include "hircluster.h"

#include "MESA_handle_logger.h"

#include "frag_redis.h"
#include "frag_reassembly_in.h"

extern frag_reassembly_t 			frag_rssb;     //use media hash

/*
���ܣ�	Ϊ��Ⱥ��������ṩ��ʵ����Ϣ,����redis��Ⱥ�������Ӻ����
������  cc:			��Ⱥ�������
		flag��		�Ӽ�Ⱥ��ΨһIP�α�ʶ�������涨����ĸ���ѡһ
		uni_ipfrag:	ΨһIP�εľ���ֵ
����ֵ��0 �ɹ�
		-1 ʧ��
*/
extern "C" int redisClusterEnableSalve(redisClusterContext *cc, int flag, const char *uni_ipfrag);

/**
*
*return : 
*		-1:connect error; 
*		-2:reply error(need to freeReplyObject) ; 
*		0:succ(need to freeReplyObject) 
*/
int redis_nocluster_excute_command(int thread_seq, void* logger, redisReply** reply, char* cmmd, int argc, const char **argv, const size_t *argvlen)
{
	int rec = 0;
	redisReply* cmmd_reply = *reply;	

	if(NULL==frag_rssb.redis_ctx[thread_seq] || frag_rssb.redis_ctx[thread_seq]->err)
	{		
		if(NULL!=frag_rssb.redis_ctx[thread_seq])
		{
			redisFree(frag_rssb.redis_ctx[thread_seq]);
			frag_rssb.redis_ctx[thread_seq] = NULL;
		}
		frag_rssb.redis_ctx[thread_seq] = redisConnectWithTimeout(frag_rssb.redis_ip, frag_rssb.redis_port, frag_rssb.redis_tv);
	}
	if(NULL==frag_rssb.redis_ctx[thread_seq]) return -1;

	if(frag_rssb.redis_ctx[thread_seq]->err)
	{
		MESA_handle_runtime_log(logger, RLOG_LV_FATAL, FRAG_REASSEMBLY_MODULE_NAME, 
								"{%s:%d} [redis exec command '%s' fail, connect error:%s].",
								__FILE__,__LINE__, cmmd, frag_rssb.redis_ctx[thread_seq]->errstr);
		redisFree(frag_rssb.redis_ctx[thread_seq]);		
		frag_rssb.redis_ctx[thread_seq] = NULL;
		rec = -1;		
	}
	else
	{
		//cmmd_reply = (redisReply *)redisCommand(frag_rssb.redis_ctx[thread_seq], cmmd);
		cmmd_reply = (redisReply *)redisCommandArgv(frag_rssb.redis_ctx[thread_seq], argc, argv, argvlen);
		/*replyΪNULL����ʾ�ͻ��˺ͷ������˳��������ش��󣬱�����������*/
		if(NULL==cmmd_reply)
		{			
			MESA_handle_runtime_log(logger, RLOG_LV_FATAL, FRAG_REASSEMBLY_MODULE_NAME, 
								"{%s:%d} [redis exec command '%s' fail, connect error:%s].",
								__FILE__,__LINE__, cmmd, frag_rssb.redis_ctx[thread_seq]->errstr);
			redisFree(frag_rssb.redis_ctx[thread_seq]);				
			frag_rssb.redis_ctx[thread_seq] = NULL;
			freeReplyObject(cmmd_reply);
			rec = -2;
		}
		else if(cmmd_reply->type==REDIS_REPLY_ERROR)
		{
			MESA_handle_runtime_log(logger, RLOG_LV_FATAL, FRAG_REASSEMBLY_MODULE_NAME, 
									"{%s:%d} [redis exec command '%s' fail, reply error:%s].",
									__FILE__,__LINE__, cmmd, cmmd_reply->str);
			freeReplyObject(cmmd_reply);
			rec = -2;			
		}		
	}	

	if(0==rec)
	{	
		MESA_handle_runtime_log(logger, RLOG_LV_INFO, FRAG_REASSEMBLY_MODULE_NAME, 
								"{%s:%d} [redis exec command '%s' succ].",
								__FILE__,__LINE__, cmmd);
	}
	*reply = cmmd_reply;
	return rec;
}

/**
*
*return : 
*		-1:connect error; 
*		-2:reply error(need to freeReplyObject) ; 
*		0:succ(need to freeReplyObject) 
*/
int redis_cluster_excute_command(int thread_seq, void* logger, redisReply** reply, char* cmmd, int argc, const char **argv, const size_t *argvlen)
{
	int rec = 0;
	redisReply* cmmd_reply = *reply;	

	if(NULL==frag_rssb.redis_cluster_ctx[thread_seq] || frag_rssb.redis_cluster_ctx[thread_seq]->err)
	{		
		if(NULL!=frag_rssb.redis_cluster_ctx[thread_seq])
		{
			redisClusterFree(frag_rssb.redis_cluster_ctx[thread_seq]);
			frag_rssb.redis_cluster_ctx[thread_seq] = NULL;
		}
		frag_rssb.redis_cluster_ctx[thread_seq] = redisClusterConnectWithTimeout(frag_rssb.redis_addr, frag_rssb.redis_tv, HIRCLUSTER_FLAG_NULL);
		//redisClusterEnableSalve(frag_rssb.redis_cluster_ctx[thread_seq], frag_rssb.redis_cluster_netflag, frag_rssb.redis_cluster_net);
	}
	if(NULL==frag_rssb.redis_cluster_ctx[thread_seq]) return -1;

	if(frag_rssb.redis_cluster_ctx[thread_seq]->err)
	{
		MESA_handle_runtime_log(logger, RLOG_LV_FATAL, FRAG_REASSEMBLY_MODULE_NAME, 
								"{%s:%d} [redis cluster exec command '%s' fail, connect error:%s].",
								__FILE__,__LINE__, cmmd, frag_rssb.redis_cluster_ctx[thread_seq]->errstr);
		redisClusterFree(frag_rssb.redis_cluster_ctx[thread_seq]);		
		frag_rssb.redis_cluster_ctx[thread_seq] = NULL;
		rec = -1;		
	}
	else
	{
		//cmmd_reply = (redisReply *)redisClusterCommand(frag_rssb.redis_cluster_ctx[thread_seq], cmmd);
		cmmd_reply = (redisReply *)redisClusterCommandArgv(frag_rssb.redis_cluster_ctx[thread_seq], argc, argv, argvlen);
		/*replyΪNULL����ʾ�ͻ��˺ͷ������˳��������ش��󣬱�����������*/
		if(NULL==cmmd_reply)
		{			
			MESA_handle_runtime_log(logger, RLOG_LV_FATAL, FRAG_REASSEMBLY_MODULE_NAME, 
								"{%s:%d} [redis cluster exec command '%s' fail, connect error:%s].",
								__FILE__,__LINE__, cmmd, frag_rssb.redis_cluster_ctx[thread_seq]->errstr);
			redisClusterFree(frag_rssb.redis_cluster_ctx[thread_seq]);				
			frag_rssb.redis_cluster_ctx[thread_seq] = NULL;
			freeReplyObject(cmmd_reply);
			rec = -2;
		}
		else if(cmmd_reply->type==REDIS_REPLY_ERROR)
		{
			MESA_handle_runtime_log(logger, RLOG_LV_FATAL, FRAG_REASSEMBLY_MODULE_NAME, 
									"{%s:%d} [redis cluster exec command '%s' fail, reply error:%s].",
									__FILE__,__LINE__, cmmd, cmmd_reply->str);
			freeReplyObject(cmmd_reply);
			rec = -2;			
		}		
	}	

	if(0==rec)
	{	
		MESA_handle_runtime_log(logger, RLOG_LV_INFO, FRAG_REASSEMBLY_MODULE_NAME, 
								"{%s:%d} [redis cluster exec command '%s' succ].",
								__FILE__,__LINE__, cmmd);
	}
	*reply = cmmd_reply;
	return rec;
}

int redis_excute_command(int thread_seq, void* logger, redisReply** reply, char* cmmd, int argc, const char **argv, const size_t *argvlen)
{
	if(frag_rssb.redis_cluster_switch==1)
	{
	 	return redis_cluster_excute_command(thread_seq, logger, reply, cmmd, argc, argv, argvlen);
	}
	else if(frag_rssb.redis_cluster_switch==2)
	{
		return redis_nocluster_excute_command(thread_seq, logger, reply, cmmd, argc, argv, argvlen);
	}
	else
	{
		return -2;
	}
}