-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmysql_proxy_server.cpp
1194 lines (1070 loc) · 29.1 KB
/
mysql_proxy_server.cpp
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdlib.h>
#include <mysql.h>
#include <mysql/errmsg.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <rpc/des_crypt.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
#include <sys/time.h>
#include <sstream>
#include <iostream>
#include <fstream>
#include <queue>
#include "oi_log.h"
#include "oi_cfg.h"
#include "Attr_API.h"
#include "atomic_counter_array.h"
#include "storage.h"
#include "fork_children.h"
#define MYSQL_PROXY_DEFAULT_PORT 34765
#define MYSQL_PROXY_DEFAULT_SPORT "34765"
//error
#define LOG_ERROR(fmt, args...) do { if (stConfig.iLogLevel >= _LOG_ERROR_) Log(&(stConfig.stLog), 2, "[ERR][PID_%d]%s:%d(%s):" fmt, stConfig.pid, __FILE__, __LINE__, __FUNCTION__ , ## args);} while (0)
//warning
#define LOG_WARNING(fmt, args...) do { if (stConfig.iLogLevel >= _LOG_WARNING_) Log(&(stConfig.stLog), 2, "[WARNING][PID_%d]%s:%d(%s):" fmt, stConfig.pid, __FILE__, __LINE__, __FUNCTION__ , ## args);} while (0)
//info
#define LOG_INFO(fmt, args...) do { if (stConfig.iLogLevel >= _LOG_INFO_) Log(&(stConfig.stLog), 2, "[INFO][PID_%d]%s:%d(%s):" fmt, stConfig.pid, __FILE__, __LINE__, __FUNCTION__ , ## args);} while (0)
//debug
#define LOG_DEBUG(fmt, args...) do { if (stConfig.iLogLevel >= _LOG_DEBUG_) Log(&(stConfig.stLog), 2, "[DEBUG][PID_%d]%s:%d(%s):" fmt, stConfig.pid, __FILE__, __LINE__, __FUNCTION__ , ## args);} while (0)
static struct CONFIG
{
char sDBIP[30] ;
char sDBUser[30];
char sDbPwd[20];
char sDBName[20];
int iLogLevel;
LogFile stLog;
char sLogFilePath[256];
char sServerIP[128];
char sServerPort[32];
char sCharset[32];
time_t lCurrentTime;
time_t lLastGetTime;
time_t lForkTime;
atomic_counter stSendBytesCounter;
int iSendBytesLimit;
int iLoopWaitTime;
int iMinInterval;
int iEnaTraficCtrl;
int iShmFd;
int iShmKey;
int iShmMaxKeyLen;
int iCacheValidSeconds;
pid_t pid;
map<string, string> mapAppKeys; // app name to key mappings
} stConfig;
template<typename T> class ConcurrentQueue : public std::queue<T>
{
public:
ConcurrentQueue<T>():queue<T>()
{
pthread_mutex_init(&mutex, NULL);
}
~ConcurrentQueue<T>()
{
pthread_mutex_destroy(&mutex);
}
void Put(T &t)
{
pthread_mutex_lock(&mutex);
queue<T>::push(t);
pthread_mutex_unlock(&mutex);
}
bool Get(T &t)
{
bool result = false;
pthread_mutex_lock(&mutex);
if(queue<T>::size())
{
t = queue<T>::front();
queue<T>::pop();
result = true;
}
pthread_mutex_unlock(&mutex);
return result;
}
private:
pthread_mutex_t mutex;
};
ConcurrentQueue<MYSQL *> vstMySQLPool;
string sMySQLError;
#define PACKET_START_TOKEN "@MyPROXY"
#define PACKET_HEADER_LENGTH 16
#define MAX_REQUEST_PACKET_LENGTH (4096+PACKET_HEADER_LENGTH)
/*
* struct ProxyPacket
* {
* uint64_t start_token; // '@MyPROXY'
* uint64_t length; // including start_token
* pb_message Request
* {
* string appname = 1;
* uint64 sequence = 2;
* uint32 errorno = 3;
* string content = 11;
* }
* Request request;
* };
*/
struct ProxyPacket
{
string appname;
string appkey;
uint64_t sequence;
uint32_t errnum;
string content;
};
/*
* Turn password into DES key
*/
static void passwd2des(char *pw, char *key)
{
int i;
memset (key, 0, 8);
for (i = 0; *pw; ++i)
key[i%8] ^= *pw++ << 1;
des_setparity (key);
}
static string StrToHex(const string &content)
{
char buf[content.length()*2+2];
for(size_t i=0; i<content.length(); i++)
sprintf(buf+i*2, "%02X", content[i]);
return string(buf, content.length()*2);
}
static string HexToStr(const string &hex)
{
size_t newlen = hex.length()/2;
char buf[newlen];
for(size_t i=0; i<newlen; i++)
buf[i] = strtoul(hex.substr(i*2, 2).c_str(), NULL, 16);
return string(buf, newlen);
}
#define max_encrypt_length 512
static string Encrypt(const string &key, const string &content)
{
uint32_t length = content.length();
string source = string((char*)&length, sizeof(uint32_t)) + content;
if(source.length()&0x7)
{
static char spaces[] = " ";
source.append(spaces, 8-source.length()%8);
}
char data[max_encrypt_length];
length = source.length();
string result;
while(length > 0)
{
int left = 0;
if(length > max_encrypt_length)
{
left = length - max_encrypt_length;
length = max_encrypt_length;
}
memcpy(data, source.data()+result.length(), length);
int iRet = ecb_crypt((char*)key.c_str(), data, length, DES_ENCRYPT);
if(DES_FAILED(iRet))
{
Attr_API(2977140, 1);
return string();
}
result.append(data, length);
length = left;
}
return result;
}
static string Decrypt(const string &key, const string &content)
{
char data[max_encrypt_length];
int length = content.length();
string result;
if(length < sizeof(uint32_t))
{
Attr_API(2977142, 1);
return string();
}
while(length > 0)
{
if(length > max_encrypt_length)
{
length = max_encrypt_length;
}
memcpy(data, content.data()+result.length(), length);
int iRet = ecb_crypt((char*)key.c_str(), data, length, DES_DECRYPT);
if(DES_FAILED(iRet))
{
Attr_API(2977141, 1);
return string();
}
result.append(data, length);
length = content.length() - result.length();
}
if(result.length()>sizeof(uint32_t))
{
length = *(uint32_t*)result.data();
if(length+sizeof(uint32_t)<=result.length())
{
return result.substr(sizeof(uint32_t), length);
}
}
Attr_API(2977143, 1);
LOG_ERROR("Decrypt successful, but length (%d) is incorrect.", result.length());
return string();
}
static void SendResponse(int sockfd, ProxyPacket &stRequest, int errornum, const string &content)
{
ProxyPacket stResponse = stRequest;
stResponse.errnum = errornum;
if(errornum==0)
{
stResponse.content = Encrypt(stResponse.appkey, content);
if(stResponse.content.length()==0 && content.length())
{
stResponse.errnum = 2;
stResponse.content = "Encryption failed";
}
}
else
{
stResponse.content = content;
}
KnvNode *tree = KnvNode::NewTree(1);
if(tree==NULL)
{
Attr_API(2973314, 1);
LOG_ERROR("KnvNode::NewTree() failed: %s", KnvNode::GetGlobalErrorMsg());
return;
}
tree->SetFieldStr(1, stResponse.appname.length(), stResponse.appname.data());
tree->SetFieldInt(2, stResponse.sequence);
tree->SetFieldInt(3, stResponse.errnum);
tree->SetFieldStr(11, stResponse.content.length(), stResponse.content.data());
int len = tree->EvaluateSize();
char *rsp = new char[PACKET_HEADER_LENGTH + len];
memcpy(rsp, PACKET_START_TOKEN, 8);
int ret = tree->Serialize(rsp+PACKET_HEADER_LENGTH, len, false);
if(ret<0)
{
Attr_API(2973316, 1);
LOG_ERROR("KnvNode::Serialize() failed: %s", tree->GetErrorMsg());
KnvNode::Delete(tree);
delete []rsp;
return;
}
KnvNode::Delete(tree);
len += PACKET_HEADER_LENGTH;
((uint64_t*)rsp)[1] = len;
atomic_counter_array_add_current_slot(&(stConfig.stSendBytesCounter), (uint32_t)len);
LOG_DEBUG("Sending response, packet length %d, original content length %d, encrypted content length %d.", len, content.length(), stResponse.content.length());
if(send(sockfd, rsp, len, 0)<0)
{
Attr_API(2973317, 1);
LOG_ERROR("Send response failed: errno=%d:%s", errno, strerror(errno));
}
delete []rsp;
}
static MYSQL *InitMySQL()
{
//connect to db
MYSQL *mysql = mysql_init(NULL);
if(mysql==NULL)
{
sMySQLError = "mysql_init failed: ";
sMySQLError += mysql_error(NULL);
printf("%s\n", sMySQLError.c_str());
return NULL;
}
if (mysql_real_connect(mysql, stConfig.sDBIP, stConfig.sDBUser, stConfig.sDbPwd, stConfig.sDBName, 0, NULL, 0) == NULL)
{
sMySQLError = "mysql_real_connect failed: ";
sMySQLError += mysql_error(mysql);
printf("%s\n", sMySQLError.c_str());
mysql_close(mysql);
return NULL;
}
if(stConfig.sCharset[0])
mysql_set_character_set(mysql, stConfig.sCharset);
return mysql;
}
static MYSQL *AllocMySQL()
{
Attr_API(2974016, 1);
MYSQL *mysql = NULL;
if(vstMySQLPool.Get(mysql)) // use recycled fd
{
// report fd_list size
int iNum = 0;
Attr_API_Get(2973318, &iNum);
if(vstMySQLPool.size()>iNum)
Attr_API_Set(2973318, vstMySQLPool.size());
if(mysql)
return mysql;
}
return InitMySQL();
}
static int FreeMySQL(MYSQL *mysql)
{
Attr_API(2974017, 1);
if(mysql)
{
vstMySQLPool.Put(mysql);
}
}
static int RealFreeMySQL(MYSQL *mysql)
{
Attr_API(2974018, 1);
mysql_close(mysql);
}
static int CreateSockAddr(int socket_type, char* sIP, char* sPort, int* iListener)
{
struct sockaddr_in stAddr;
int iListenSocket;
int iReuseAddr = 1;
int iPort = atoi(sPort);
if(iPort==0)
iPort = MYSQL_PROXY_DEFAULT_PORT;
/* Setup internet address information.
This is used with the bind() call */
memset((char *) &stAddr, 0, sizeof(stAddr));
stAddr.sin_family = AF_INET;
stAddr.sin_port = htons(atoi(sPort));
stAddr.sin_addr.s_addr = inet_addr(sIP);
iListenSocket = socket(AF_INET, socket_type, 0);
if (iListenSocket < 0) {
perror("socket");
return -1;
}
if (iListener != NULL)
*iListener = iListenSocket;
setsockopt(iListenSocket, SOL_SOCKET, SO_REUSEADDR, &iReuseAddr,
sizeof(iReuseAddr));
if (bind(iListenSocket, (struct sockaddr *) &stAddr,
sizeof(stAddr)) < 0) {
perror("bind");
close(iListenSocket);
return -1;
}
if (socket_type == SOCK_STREAM) {
listen(iListenSocket, 5); /* Queue up to five connections before
having them automatically rejected. */
}
return iListenSocket;
}
int SetNBlock(int iSock)
{
int iFlags;
iFlags = fcntl(iSock, F_GETFL, 0);
iFlags |= O_NONBLOCK;
iFlags |= O_NDELAY;
fcntl(iSock, F_SETFL, iFlags);
return 0;
}
static int CreateListenSock()
{
int iSock = CreateSockAddr(SOCK_STREAM, stConfig.sServerIP, stConfig.sServerPort, NULL);
if(iSock < 0)
return -1;
int iOn = 1;
int iRet = setsockopt(iSock, SOL_SOCKET, SO_KEEPALIVE, (char *)&iOn, sizeof(iOn));
if(iRet == -1)
{
perror("setsockopt");
close(iSock);
return -1;
}
SetNBlock(iSock);
return iSock;
}
int LoadKeyFile(const char *sKeyFile)
{
try {
printf("Loading key mapping from file %s.\n", sKeyFile);
ifstream ifs(sKeyFile, std::ifstream::in);
if(ifs.is_open())
{
char line[10240];
while(1)
{
line[0] = 0;
ifs.getline(line, sizeof(line));
if(line[0])
{
char *pstr = line;
while(isspace(*pstr))
pstr ++;
if(*pstr=='#')
{
continue;
}
string appname, appkey;
while(*pstr && !isspace(*pstr) && *pstr!='#')
{
appname.append(string(pstr, 1));
pstr ++;
}
if(*pstr==0 || *pstr=='#')
{
printf("Warning: app name %s has no key!\n", appname.c_str());
}
else
{
while(isspace(*pstr))
pstr ++;
while(*pstr && !isspace(*pstr) && *pstr!='#')
{
appkey.append(string(pstr, 1));
pstr ++;
}
}
printf("Got one app: %s - %s.\n", appname.c_str(), appkey.c_str());
char key[8];
passwd2des((char*)appkey.c_str(), key);
stConfig.mapAppKeys[appname].assign(key, 8);
}
else
{
if(!ifs.good())
break;
}
}
return stConfig.mapAppKeys.size();
}
else
{
printf("Failed to open faile %s\n", sKeyFile);
return -1;
}
}
catch (ios_base::failure &f)
{
printf("Failed to open faile %s: %s\n", sKeyFile, f.what());
return -1;
}
}
int ProxyInit(int argc, char *argv[])
{
char sConfFilePath[255]={0};
if(argc > 2)
{
printf("Usage: %s Config_file\n", argv[0]);
return -1;
}
else if(argc == 2)
{
strncpy(sConfFilePath, argv[1], sizeof(sConfFilePath)-1);
}
else
{
strncpy(sConfFilePath, "/home/oicq/mysql_proxy/conf/mysql_proxy_server.conf", sizeof(sConfFilePath)-1);
}
stConfig.iShmKey = 0x447755;
int iShmPercentage = 20;
int iShmBlockSize = 4096;
stConfig.iShmMaxKeyLen = 128;
int iChildNum = 1;
stConfig.iCacheValidSeconds = 60;
char sKeyFile[256];
TLib_Cfg_GetConfig(sConfFilePath,
"LOGFILE", CFG_STRING, stConfig.sLogFilePath, "/home/oicq/mysql_proxy/log/mysql_proxy_server", sizeof(stConfig.sLogFilePath),
"KEYFILE", CFG_STRING, sKeyFile, "/home/oicq/mysql_proxy/conf/key_file.txt", sizeof(sKeyFile),
"LOG_LEVEL", CFG_INT, &(stConfig.iLogLevel),5,
"SERVER_IP", CFG_STRING, stConfig.sServerIP, "0.0.0.0", sizeof(stConfig.sServerIP),
"SERVER_PORT", CFG_STRING, stConfig.sServerPort, MYSQL_PROXY_DEFAULT_SPORT, sizeof(stConfig.sServerPort),
"MYSQL_HOST", CFG_STRING, stConfig.sDBIP, "localhost", sizeof(stConfig.sDBIP),
"MYSQL_USER", CFG_STRING, stConfig.sDBUser, "root", sizeof(stConfig.sDBUser),
"MYSQL_PASSWD", CFG_STRING, stConfig.sDbPwd, "", sizeof(stConfig.sDbPwd),
"MYSQL_DB", CFG_STRING, stConfig.sDBName, "mysql_test", sizeof(stConfig.sDBName),
"MYSQL_CHARSET", CFG_STRING, stConfig.sCharset, "utf8", sizeof(stConfig.sCharset),
"SEND_BYTES_LIMIT", CFG_INT, &(stConfig.iSendBytesLimit), 300000000, //默认一秒可以发送300MByte配置
"MIN_INTERVAL",CFG_INT,&(stConfig.iMinInterval), 10, //流量计算的最小时间间隔,1/10秒为单位
"ENA_TRAFIC_CTRL",CFG_INT,&(stConfig.iEnaTraficCtrl), 1, //是否开启流量控制,默认开启
"SHM_KEY", CFG_INT, &stConfig.iShmKey, stConfig.iShmKey, //Shm缓存的key
"SHM_PERCENT", CFG_INT, &iShmPercentage, iShmPercentage, //Shm缓存占用系统内存的百分比
"SHM_BLOCKSIZE", CFG_INT, &iShmBlockSize, iShmBlockSize, //Shm缓存分配的块大小
"SHM_MAX_KEYLEN", CFG_INT, &stConfig.iShmMaxKeyLen, stConfig.iShmMaxKeyLen, //Shm哈希表存储的最大key长度
"CHILD_NUM", CFG_INT, &iChildNum, iChildNum, //工作进程个数
"CACHE_VALID_SECONDS", CFG_INT, &stConfig.iCacheValidSeconds, stConfig.iCacheValidSeconds, //Cache在shm中的有效时间
(void*)NULL
);
if(LoadKeyFile(sKeyFile)<0)
return -1;
if(stConfig.mapAppKeys.size()==0)
{
printf("Key file is empty: %s, the server can not continue without app/key configs.\n", sKeyFile);
return -1;
}
if(stConfig.iShmMaxKeyLen<128)
{
printf("SHM_MAX_KEYLEN of %d is too small, forced to %d.\n", stConfig.iShmMaxKeyLen, 128);
stConfig.iShmMaxKeyLen = 128;
}
//SEND_BYTES_LIMIT做一下限制,免得配错了影响服务
if(stConfig.iSendBytesLimit < 10000000)
stConfig.iSendBytesLimit = 10000000;
if(stConfig.iSendBytesLimit > 500000000)
stConfig.iSendBytesLimit = 500000000;
if(stConfig.iMinInterval > 50) //计算流量的最小时间间隔是10ms,最大时间间隔是1分钟
stConfig.iMinInterval = 50;
printf("SHM_KEY:0x%X\n", (uint32_t)stConfig.iShmKey);
printf("LOG_LEVEL:%d\n", stConfig.iLogLevel);
printf("SEND_BYTES_LIMIT:%d\n", stConfig.iSendBytesLimit);
printf("MIN_INTERVAL:%d\n", stConfig.iMinInterval);
printf("SHM_PERCENT:%d\n", iShmPercentage);
InitLogFile(&stConfig.stLog, stConfig.sLogFilePath, 0, 5, 5000000);
stConfig.iShmFd = UcStorage::InitWrite(stConfig.iShmKey, iShmPercentage, stConfig.iShmMaxKeyLen, 20, 1, iShmBlockSize);
if(stConfig.iShmFd<0)
{
printf("Shm init failed: %s\n", UcStorage::GetInitErrorMsg().c_str());
return -1;
}
if (atomic_counter_array_init(&(stConfig.stSendBytesCounter), stConfig.iShmKey+1, 0) != 0)
{
printf("atomic_counter_init() failed!\n");
return -1;
}
stConfig.stSendBytesCounter.ptr->cIdx = 0;
int iListenFd = CreateListenSock();
if (iListenFd < 0)
{
printf("CreateListenSock Fail: %d:%s\n", errno, strerror(errno));
return -1;
}
//connect to db
MYSQL *pstMySQL = InitMySQL();
if(pstMySQL==NULL)
return -1;
if(iChildNum>1)
{
RealFreeMySQL(pstMySQL);
}
else // single child-mode, can be reuse
{
FreeMySQL(pstMySQL);
}
int iRet = ForkChildren(iChildNum);
if(iRet<0)
{
printf("Fork children failed.\n");
return -1;
}
stConfig.pid = getpid();
return iListenFd;
}
static uint64_t TimeToUsec(const struct timeval *pval)
{
uint64_t ret;
if (pval == NULL) return 0;
ret = pval->tv_sec;
ret = ret * 1000000;
ret += pval->tv_usec;
return ret;
}
static uint64_t SubTime(struct timeval tv1, struct timeval tv2)
{
return TimeToUsec(&tv1) - TimeToUsec(&tv2);
}
static int TrafficCtl(int iUTimePice)
{
int iRet = 0, i = 0;
static struct timeval tvTime[6] = {{0,0},{0,0},{0,0},{0,0},{0,0}};
struct timeval tvNow;
gettimeofday(&tvNow, NULL);
uint32_t dwSendBytes = 0;
uint64_t ullUsec = SubTime(tvNow, tvTime[stConfig.stSendBytesCounter.ptr->cIdx]);
if(ullUsec >= 5000 * (uint64_t)1000000 * iUTimePice)
{
Attr_API(253031,1);
memset(stConfig.stSendBytesCounter.ptr, 0, sizeof(atomic_counter_t)); //发生时间跳变时可能出现再次初始化,上报
for(i = 0; i < 6; ++i)
{
tvTime[i] = tvNow;
}
return 0;
}
if(ullUsec > 1 * (uint64_t)100000 * iUTimePice) //与本时间片计数器进入时间超过一个时间片
{
//更新索引,更新时间戳
atomic_counter_array_set_lastest_slot(&(stConfig.stSendBytesCounter), 0);
stConfig.stSendBytesCounter.ptr->cIdx = (stConfig.stSendBytesCounter.ptr->cIdx + 5) % 6;
tvTime[stConfig.stSendBytesCounter.ptr->cIdx] = tvNow;
}
ullUsec = SubTime(tvNow,tvTime[(stConfig.stSendBytesCounter.ptr->cIdx + 4)%6]); //开始计算流量,此处计算与4个时间片前的计数器时间戳的时间差
if(ullUsec >= 1 * (uint64_t)100000 * iUTimePice)
{
iRet = atomic_counter_array_get_five_slots(&(stConfig.stSendBytesCounter), &dwSendBytes);
// LOG_INFO("PId %d Send Speed:%llu Bytes/s [%u]:[%llu]\n", getpid(), dwSendBytes * (uint64_t)1000000 / ullUsec, dwSendBytes, ullUsec);
// int iSpeed = (int) dwSendBytes * (uint64_t)1000000 / ullUsec;
if(dwSendBytes * (uint64_t)1000000 / ullUsec > (uint64_t)stConfig.iSendBytesLimit)
return 1;
}
return 0;
}
void DoLoop()
{
}
/*
* message ReqResult
* {
* string key = 1; // query sql statement
* uint32 time = 2; // query time
* uint32 num_rows = 3; // mysql_num_rows()
* uint32 num_fields = 4; // mysql_num_fields()
*
* message Fields
* {
* string key = 1; // "fields"
* message Field
* {
* string name = 1;
*
* // future fied info can be added below
* };
* repeated Field field = 11;
* };
* Fields fields = 11;
*
* message Row
* {
* string key = 1; // "row <i>" i = {0,1,2...}
* optional string field_0 = 11; // field_0
* optional string field_1 = 12; // field_1
* // ... ...
* };
*
* // for KNV to work more efficently, we split data rows into sets
* // each set contains up to 10000 rows of data
* message Sets
* {
* string key = 1; // "set_<i>" i= {0,1,2...}
* repeated Row row = 11; // set_0, row 0-9999, set_1, row 10000-19999, ...
* };
*
* repeated Sets sets = 12;
* };
*/
#define max_row_num_for_each_set 10000
static KnvNode *PackResult(MYSQL_RES *result, knv_key_t &reqkey)
{
KnvNode *tree = KnvNode::NewTree(1, &reqkey);
if(tree==NULL)
{
Attr_API(2973314, 1);
sMySQLError = "Create KNV tree failed: ";
sMySQLError += KnvNode::GetGlobalErrorMsg();
return NULL;
}
unsigned int num_rows = mysql_num_rows(result);
unsigned int num_fields = mysql_num_fields(result);
tree->SetFieldInt(2, time(NULL));
tree->SetFieldInt(3, num_rows);
tree->SetFieldInt(4, num_fields);
MYSQL_FIELD *field;
vector<string> fields;
knv_key_t kfields((char*)"fields", 6);
KnvNode *fieldstree = tree->InsertSubNode(11, &kfields);
if(fieldstree==NULL)
{
Attr_API(2973315, 1);
sMySQLError = "Add KNV node failed: ";
sMySQLError += tree->GetErrorMsg();
KnvNode::Delete(tree);
return NULL;
}
while((field = mysql_fetch_field(result)))
{
knv_key_t kfield(field->name, field->name_length);
KnvNode *fieldnode = fieldstree->InsertSubNode(11, &kfield);
if(fieldnode==NULL)
{
Attr_API(2973315, 1);
sMySQLError = "Add KNV node failed: ";
sMySQLError += tree->GetErrorMsg();
KnvNode::Delete(tree);
return NULL;
}
fields.push_back(field->name);
}
MYSQL_ROW row;
KnvNode *settree = NULL;
int r = 0;
while ((row = mysql_fetch_row(result)))
{
char rowname[64];
snprintf(rowname, sizeof(rowname), "row %d", r);
if(settree==NULL)
{
char setname[64];
snprintf(setname, sizeof(setname), "set %d", r/max_row_num_for_each_set);
knv_key_t kset(setname, strlen(setname));
settree = tree->InsertSubNode(12, &kset);
if(settree==NULL)
{
Attr_API(2973315, 1);
sMySQLError = "Add KNV node failed: ";
sMySQLError += tree->GetErrorMsg();
KnvNode::Delete(tree);
return NULL;
}
}
knv_key_t k(rowname, strlen(rowname));
KnvNode *subtree = settree->InsertSubNode(11, &k);
if(subtree==NULL)
{
Attr_API(2973315, 1);
sMySQLError = "Add KNV node failed: ";
sMySQLError += settree->GetErrorMsg();
KnvNode::Delete(tree);
return NULL;
}
unsigned long *lengths = mysql_fetch_lengths(result);
for(int i = 0; i < num_fields; i++)
{
if(row[i])
{
if(lengths)
subtree->AddFieldStr(11+i, lengths[i], row[i]);
else // mysql_fetch_lengths() failed
subtree->AddFieldStr(11+i, strlen(row[i]), row[i]);
}
}
if(r && (r%max_row_num_for_each_set==0))
{
settree->GetValue(); // Fold and delete children objects
settree = NULL; // start a new set
}
r++;
}
//printf("done.\n");
LOG_DEBUG("Rsp data: fields=%d/%d, rows=%d/%d, data_size=%d", num_fields, fields.size(), num_rows, r, tree->EvaluateSize());
return tree;
}
static inline string GetReqHashKey(const char *reqstr, int length)
{
if(length<stConfig.iShmMaxKeyLen-32)
return string(reqstr, length);
else
return string(reqstr, stConfig.iShmMaxKeyLen-32);
}
void ProcessRequest(int sockfd, ProxyPacket &stRequest)
{
string reqstr = Decrypt(stRequest.appkey, stRequest.content);
if(reqstr.length()==0)
{
LOG_INFO("Recv bad request, length=%d.", stRequest.content.length());
sMySQLError = "Unauthorized request";
SendResponse(sockfd, stRequest, 1, sMySQLError);
return;
}
LOG_DEBUG("Recv request: %s.", reqstr.c_str());
// skip spaces
const char *sqlstr = reqstr.c_str();
const char *sql = sqlstr;
while(*sql=='\n'||*sql=='\r'||isspace(*sql))
sql ++;
if(strncasecmp(sql, "select", 6) && strncasecmp(sql, "show", 4) && strncasecmp(sql, "desc", 4))
{
sMySQLError = "statement not starting with SELECT/SHOW/DESC is not supperted.";
SendResponse(sockfd, stRequest, 1, sMySQLError);
LOG_DEBUG("%s", sMySQLError.c_str());
return;
}
int len = reqstr.length() - (sql-sqlstr);
string keystr = GetReqHashKey(sql, len);
knv_key_t key((char*)keystr.data(), keystr.length());
UcStorage st(stConfig.iShmFd, key);
KnvNode *tree = NULL;
if(st.Read()>=0)
{
Attr_API(2974111, 1);
LOG_DEBUG("Got data in cache.");
tree = st.GetDataTree();
time_t t = tree? tree->GetFieldInt(2) : 0;
if(t==0 || t+stConfig.iCacheValidSeconds<time(NULL))
{
LOG_DEBUG("Data in cache is outdated.");
Attr_API(2974110, 1);
tree = NULL;
}
}
else
{
LOG_DEBUG("Get data from cache failed: %s", st.GetErrorMsg().c_str());
}
int try_num = 0, max_try_num = 10;
while(tree==NULL)
{
Attr_API(2974112, 1);
LOG_DEBUG("Getting data from DB.\n");
MYSQL *mysql = AllocMySQL();
if(mysql==NULL)
{
Attr_API(2973330, 1);
SendResponse(sockfd, stRequest, 1, sMySQLError);
LOG_ERROR("AllocMySQL failed: %s", sMySQLError.c_str());
return;
}
int iRet = mysql_query(mysql, sql);
if(iRet<0)
{
Attr_API(2973331, 1);
sMySQLError = "mysql_query failed: ";
sMySQLError += mysql_error(mysql);
int err = mysql_errno(mysql);
LOG_DEBUG("%s, mysql_errno=%d", sMySQLError.c_str(), err);
if(err>=ER_ERROR_FIRST && err<=ER_ERROR_LAST) // server side error, not relavant to socket
{
FreeMySQL(mysql);
}
else
{
RealFreeMySQL(mysql);
}
// if connection is closed by server, try again
if((err==CR_SERVER_GONE_ERROR || err==CR_SERVER_LOST ||
err==CR_CONNECTION_ERROR || err==CR_CONN_HOST_ERROR ||
err==CR_LOCALHOST_CONNECTION || err==CR_TCP_CONNECTION) &&
try_num < max_try_num)
{
try_num ++;
continue;
}
SendResponse(sockfd, stRequest, err, sMySQLError);
return;
}
MYSQL_RES *result = mysql_store_result(mysql);
if (result == NULL)
{
Attr_API(2973335, 1);
sMySQLError = "mysql_store_result failed: ";
sMySQLError += mysql_error(mysql);
int err = mysql_errno(mysql);
LOG_DEBUG("%s, mysql_errno=%d", sMySQLError.c_str(), err);
if(err>=ER_ERROR_FIRST && err<=ER_ERROR_LAST) // client side error, not relavant to socket
{
FreeMySQL(mysql);
}
else
{
RealFreeMySQL(mysql);
}
// if connection is closed by server, try again
if((err==CR_SERVER_GONE_ERROR || err==CR_SERVER_LOST ||
err==CR_CONNECTION_ERROR || err==CR_CONN_HOST_ERROR ||
err==CR_LOCALHOST_CONNECTION || err==CR_TCP_CONNECTION) &&
try_num < max_try_num)
{
try_num ++;
continue;
}
SendResponse(sockfd, stRequest, err, sMySQLError);
return;
}
tree = PackResult(result, key);
mysql_free_result(result);
FreeMySQL(mysql);
if (tree == NULL)
{
Attr_API(2973336, 1);
SendResponse(sockfd, stRequest, CR_OUT_OF_MEMORY, sMySQLError);
LOG_DEBUG("PackResult failed: %s", sMySQLError.c_str());
return;
}
st.Attach(*tree);
if(st.TryLockShm()==0)
{
Attr_API(2973339, 1);
LOG_DEBUG("Wrote data to cache.");
if(st.Write(true)) // enable auto_remove
{
Attr_API(2973344, 1);
LOG_ERROR("Write to cache failed: %s", st.GetErrorMsg().c_str());
}
st.UnlockShm();
}
else
{
LOG_WARNING("TrylockShm failed.");
Attr_API(2973338, 1);
}
}
string rsp = tree->GetStrVal();
if(rsp.length()==0)
{
LOG_ERROR("KNV GetStrVal() failed: %s", tree->GetErrorMsg()? tree->GetErrorMsg() : "NULL");
}
LOG_DEBUG("Sending rsp of %lu bytes.", rsp.length());
SendResponse(sockfd, stRequest, 0, rsp);
}