forked from kulv2012/myrelay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_pool.c
1128 lines (926 loc) · 25.7 KB
/
my_pool.c
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
/*
* Copyright 2011-2013 Alibaba Group Holding Limited. All rights reserved.
* Use and distribution licensed under the GPL license.
*
* Authors: XiaoJinliang <xiaoshi.xjl@taobao.com>
*
*/
#include <stdio.h>
#include <list.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include <errno.h>
#include <genpool.h>
#include <sock.h>
#include <log.h>
#include <timer.h>
#include <handler.h>
#include "my_pool.h"
#include "my_buf.h"
#include "my_ops.h"
#include "conn_pool.h"
#include "my_conf.h"
#include "def.h"
extern log_t *g_log;
extern struct conf_t g_conf;
static my_pool_t *mypool;
static genpool_handler_t *handler;//mysql 的连接池,在my_pool_init分配
static my_info_t myinfo;
static int my_conn_init(my_conn_t *my, my_node_t *n);
static int my_node_init(my_node_t *n);
static my_conn_t *my_conn_alloc(my_node_t *n);
static int make_my_conn(my_conn_t *my);
static int _my_reg(my_node_t *node, char *host, char *srv, char *user, char *pass, int mincount, int maxcount);
static int my_conn_set_used(my_conn_t *my, void *ptr);
static int my_conn_set_dead(my_conn_t *my);
static int my_conn_set_raw(my_conn_t *my);
static int my_conn_set_fail(my_conn_t *my);
static int my_conn_set_ping(my_conn_t *my);
static int my_conn_dead_reconnect_timer(unsigned long arg);
static int my_conn_fail_reconnect_timer(unsigned long arg);
static int my_conn_pool_status_timer(unsigned long arg);
static int my_conn_pool_ping_timer(unsigned long arg);
static int my_conn_pool_ping_timeout_timer(unsigned long arg);
static int my_node_set_closing(my_node_t *node);
static int my_node_is_closing(my_node_t *node);
static int my_node_closing_cleanup_timer(unsigned long arg);
/*
* fun: init mysql context
* arg: mysql context
* ret: always return 0
*
*/
static int my_ctx_init(my_ctx_t *ctx)
{
ctx->dirty = 0;
bzero(ctx->curdb, sizeof(ctx->curdb));
return 0;
}
/*
* fun: set mysql info
* arg: mysql protocol, language, status, cap, version, verlen
* ret: success 0, error -1
*
*/
int my_info_set(uint8_t prot, uint8_t lang, uint16_t status, \
uint32_t cap, char *ver, int ver_len)
{
int len;
time_t now = time(NULL);
if(now - myinfo.update_time < 10){
return 0;
}
myinfo.avail = 1;
myinfo.protocol = prot;
myinfo.lang = lang;
myinfo.status = status;
myinfo.cap = cap;
if(ver_len > (sizeof(myinfo.ver) - 1)){
len = sizeof(myinfo.ver) - 1;
} else {
len = ver_len;
}
memcpy(myinfo.ver, ver, len);
myinfo.ver[len] = '\0';
myinfo.update_time = now;
return 0;
}
/*
* fun: init mysql connection
* arg: mysql connection, mysql node
* ret: success 0, error -1
*
*/
static int my_conn_init(my_conn_t *my, my_node_t *n)
{//初始化mysql连接的字段
my->fd = -1;
my->node = (void *)n;//我所属的节点
INIT_LIST_HEAD(&(my->link));
my->conn = NULL;
buf_init(&(my->buf));
my_ctx_init(&(my->ctx));
my->state_time = 0;
my->lastused_time = 0;
my->setnamesql[0] = '\0' ;
return 0;
}
/*
* fun: init mysql node
* arg: mysql node
* ret: success 0, error -1
*
*/
static int my_node_init(my_node_t *n)
{
bzero(n->host, sizeof(n->host));
bzero(n->srv, sizeof(n->srv));
bzero(n->user, sizeof(n->user));
bzero(n->pass, sizeof(n->pass));
INIT_LIST_HEAD(&(n->used_head));
INIT_LIST_HEAD(&(n->avail_head));
INIT_LIST_HEAD(&(n->dead_head));
INIT_LIST_HEAD(&(n->raw_head));
INIT_LIST_HEAD(&(n->fail_head));
INIT_LIST_HEAD(&(n->ping_head));
n->info = &myinfo;
n->avail_count = 0;
n->role = 0;
n->closing = 0;
n->closing_time = 0;
n->curall_connection = 0 ;
n->min_connection = 0 ;
n->max_connection = 0 ;
return 0;
}
/*
* fun: alloc mysql connection from mysql node
* arg: mysql node
* ret: mysql connection
*
*/
static my_conn_t *my_conn_alloc(my_node_t *n)
{//申请一个mysql 连接结构,初始化
int res = 0;
my_conn_t *my;
if( (my = genpool_alloc_page(handler)) == NULL ){//申请一个mysql连接
log(g_log, "genpool alloc page error\n");
return NULL;
}
if( (res = my_conn_init(my, n)) < 0 ){
genpool_release_page(handler, my);
log(g_log, "my_conn_init error\n");
return NULL;
}
++ n->curall_connection ;
return my;
}
/*
* fun: release mysql connection
* arg: mysql connection
* ret: success 0, error -1
*
*/
static int my_conn_release(my_conn_t *my)
{
if( my->node != NULL){
my_node_t *node = (my_node_t*)my->node ;
-- node->curall_connection ;
}
return genpool_release_page(handler, my);
}
/*
* fun: init mysql connection pool
* arg: max mysql connection number
* ret: success 0, error -1
*
*/
int my_pool_init(int count)
{//设置mysql连接的初始化结构,各种定时器等
int i, res = 0;
if( (mypool = malloc(sizeof(my_pool_t))) == NULL ){
log_err(g_log, "malloc error\n");
return -1;
}
for( i = 0 ; i < MAX_SLAVE_NODE; ++i ){ // 初始化数据结构
my_node_init( &( mypool->slave[i]) ) ;
}
if( (handler = genpool_init(sizeof(my_conn_t), count)) == NULL ){
log(g_log, "genpool_init error\n");
free(mypool);
return -1;
}
mypool->slave_num = 0;
res = timer_register(my_conn_dead_reconnect_timer, 9, "my_conn_dead_reconnect_timer", 1);
if(res < 0){
log(g_log, "my_conn_dead_reconnect_timer register error\n");
return -1;
}
res = timer_register(my_conn_fail_reconnect_timer, 1, "my_conn_fail_reconnect_timer", 1);
if(res < 0){
log(g_log, "my_conn_fail_reconnect_timer register error\n");
return -1;
}
res = timer_register(my_conn_pool_status_timer, 30, "my_conn_pool_status_timer", 10);
if(res < 0){
log(g_log, "my_conn_pool_status_timer register error\n");
return -1;
}
res = timer_register(my_conn_pool_ping_timer, 10, "my_conn_pool_ping_timer", 10);
if(res < 0){
log(g_log, "my_conn_pool_ping_timer register error\n");
return -1;
}
res = timer_register(my_conn_pool_ping_timeout_timer, 3, "my_conn_pool_ping_timeout_timer", 1);
if(res < 0){
log(g_log, "my_conn_pool_ping_timeout_timer register error\n");
return -1;
}
res = timer_register(my_node_closing_cleanup_timer, 3, "my_node_closing_cleanup_timer", 60);
if(res < 0){
log(g_log, "my_node_closing_cleanup_timer register error\n");
return -1;
}
return 0;
}
int my_pool_destroy( )
{
if( handler != NULL){
genpool_destroy( handler ) ;
handler = NULL ;
}
return 0;
}
/*
* fun: make mysql connection
* arg: mysql connection
* ret: success 0, error -1
*
*/
static int make_my_conn(my_conn_t *my)
{
int fd, done, res = 0;
my_node_t *node;
info( g_log, "make_my_conn new a connection.\n");
node = my->node;
if( (res = my_conn_init(my, node)) < 0 ){
my_conn_release(my) ;//这里必须释放结构,因为没法重连
log_err(g_log, "my_conn_init failed , my_conn_release release\n") ;
return res;
}
++ node->cur_connecting_cnt ;
fd = connect_nonblock(node->host, node->srv, &done);
if(fd >= 0){
my->fd = fd;
res = add_handler(fd, EPOLLIN, my_hs_stage1_cb, my);//my为这个mysql的连接。暂时只记录了fd 和所属node
if(res < 0){
log(g_log, "add_handler error\n");
-- node->cur_connecting_cnt ;
return my_conn_close_on_fail(my);
}
} else {
-- node->cur_connecting_cnt ;
log_err(g_log, "connect_nonblock %s:%s error\n", node->host, node->srv);
return my_conn_close_on_fail(my);
}
return 0;
}
/*
* fun: register mysql
* arg: mysql node, host, srv, user, pass, connection number
* ret: success 0, error -1
*
*/
static int _my_reg(my_node_t *node, char *host, char *srv, char *user, char *pass, int mincount, int maxcount )
{//初始化连接数据,并建立真实mysql连接
int i, fd, res = 0;
my_conn_t *my;
if( (res = my_node_init(node)) < 0 ){//初始化节点的字段
log(g_log, "my_node_init error\n");
return res;
}
node->min_connection = mincount ;
node->max_connection = maxcount ;
strncpy(node->host, host, MAX_HOST_LEN - 1);
strncpy(node->srv, srv, MAX_SRV_LEN - 1);
strncpy(node->user, user, MAX_USER_LEN - 1);
strncpy(node->pass, pass, MAX_PASS_LEN - 1);
for(i = 0; i < mincount; i++){//一个个建立那么多连接
if( (my = my_conn_alloc(node)) == NULL ){//申请一个mysql 连接结构,初始化
log(g_log, "my_conn_alloc error\n");
return -1;
}
if( (res = make_my_conn(my)) < 0 ){
log(g_log, "make my conn error, ignore, continue next.\n");
}
}
return 0;
}
/*
* fun: register slave mysql
* arg: host, srv, user, pass, connection number
* ret: success 0, error -1
*
*/
int my_slave_reg(char *host, char *srv, char *user, char *pass, int mincount, int maxcount)
{
int i, res = 0;
my_node_t *node;
for(i = 0; i < MAX_SLAVE_NODE; i++){
node = &(mypool->slave[i]);//找一个空位置,用来存储IP等信息
if(node->role == 0){
break;
}
}
if(i == MAX_SLAVE_NODE){
log(g_log, "slave number exceed limit[%d]\n", MAX_SLAVE_NODE);
return -1;
}
if(i == mypool->slave_num){
mypool->slave_num++;
}
res = _my_reg(node, host, srv, user, pass, mincount, maxcount);
if(res < 0){
log(g_log, "_my_reg error\n");
return res;
}
node->role = 1;
log(g_log, "host: %s, srv: %s, user: %s, cnum: %d\n", host, srv, user, mincount);
return res;
}
/*
* fun: set mysql node closing
* arg: mysql node
* ret: always return 0
*
*/
static int my_node_set_closing(my_node_t *node)
{
node->closing = 1;
node->closing_time = time(NULL);
return 0;
}
/*
* fun: check mysql node closing
* arg: mysql node
* ret: yes 1, no 0
*
*/
static int my_node_is_closing(my_node_t *node)
{
return node->closing;
}
/*
* fun: unregister mysql
* arg: host, srv
* ret: success 0, error -1
*
*/
int my_unreg(char *host, char *srv)
{
int i;
my_node_t *node;
log(g_log, "%s called\n", __func__);
for(i = 0; i < mypool->slave_num; i++){
node = &(mypool->slave[i]);
if((!strcmp(node->host, host)) && (!strcmp(node->srv, srv))){
my_node_set_closing(node);
log(g_log, "slave %s:%s unregister\n", host, srv);
}
}
return 0;
}
/*
* fun: get a slave connection
* arg: connection, client ip, client port
* ret: success return mysql connection, error return NULL
*
*/
my_conn_t *my_slave_conn_get(void *c, uint32_t ip, uint16_t port)
{//ip:port 为客户端连接IP,端口
int i, index;
my_node_t *node;
my_conn_t *my;
struct list_head *head;
time_t now = time(NULL);
if(mypool->slave_num == 0){
log(g_log, "no slave register\n");
return NULL;
}
for(i = 0; i < mypool->slave_num; i++){
index = ((ip + port) + i) % (mypool->slave_num);//用ip和Port做哈希, 从第i个开始找,其实这样就分散了的,
//一个个slave找,注意这里是先找第一个mysql,再找第二个
node = &(mypool->slave[index]);
head = &(node->avail_head);//从avail_head上面找有没有可用连接
if( (!my_node_is_closing(node)) && (!list_empty(head)) ){
break;
}
}
if(i == mypool->slave_num){//没找到`````
log(g_log, "no slave available, slave_num:%d\n", mypool->slave_num );
return NULL;
}
my = list_first_entry(head, my_conn_t, link);
my_conn_set_used(my, c);//将一个mysql连接标记为被使用了。也就是my->conn指向中间结构conn_t
return my;
}
/*
* fun: close mysql connection
* arg: mysql connection
* ret: success 0, error -1
*
*/
int my_conn_close(my_conn_t *my)
{
int res;
if( (res = del_handler(my->fd)) < 0 ){
log(g_log, "del_handler error, ignore it\n");
} else {
;//debug(g_log, "del_handler success\n");
}
if(my->fd >= 0){
close(my->fd);
my->fd = -1;
}
my->conn = NULL;
buf_reset(&(my->buf));
my_conn_set_dead(my);
return 0;
}
/*
* fun: close mysql connection and set fail
* arg: mysql connection
* ret: success 0, error -1
*
*/
int my_conn_close_on_fail(my_conn_t *my)
{
my_conn_close(my);
my_conn_set_fail(my);
return 0;
}
/*
* fun: close mysql connection and release it
* arg: mysql connection
* ret: success 0, error -1
*
*/
static int my_conn_close_and_release(my_conn_t *my)
{
my_conn_close(my);
list_del_init(&(my->link));//需要release,所以移除
my_conn_release(my);
return 0;
}
/*
* fun: put mysql connection
* arg: mysql connection
* ret: success 0, error -1
*
*/
int my_conn_put(my_conn_t *my, int isupdatestatustime )
{
int res = 0;
my_node_t *node = my->node;
if( (res = del_handler(my->fd)) < 0 ){
log(g_log, "del_handler error, ignore it\n");
} else {
;//debug(g_log, "del_handler success\n");
}
if(my_conn_ctx_is_dirty(my)){
my_conn_close(my);
return 0;
}
my_conn_set_avail(my, isupdatestatustime);
return 0;
}
/*
* fun: set mysql connection used
* arg: mysql connection, connection pointer
* ret: success 0, error -1
*
*/
static int my_conn_set_used(my_conn_t *my, void *ptr)
{//将一个mysql连接标记为被使用了。也就是my->conn指向中间结构conn_t,
int res = 0;
my_node_t *node = my->node;
my->conn = ptr;//指向中间结构conn_t
buf_reset(&(my->buf));
list_move_tail(&(my->link), &(node->used_head));//将我这个连接从之前的地方移除,然后挂到used_head上面
my->state_time = g_cursecond;
if( (res = del_handler(my->fd)) < 0 ){//清楚,重新再来
log(g_log, "del_handler error, ignore it\n");
} else {
;//debug(g_log, "del_handler success\n");
}
node->avail_count--;//减少可用连接数
return 0;
}
/*
* fun: set mysql connection avail
* arg: mysql connection
* ret: success 0, error -1
*
*/
int my_conn_set_avail(my_conn_t *my, int isupdatestatustime )
{//跟mysql直接的验证成功了,下面标记这个连接为可用的,放入node的avail_head上面
int res = 0;
my_node_t *node = my->node;
my->conn = NULL;
buf_reset(&(my->buf));
//下面不放到尾部了,否则一个个轮的话,没法删除过期连接了就
//list_move_tail(&(my->link), &(node->avail_head));
list_move(&(my->link), &(node->avail_head));
my->state_time = time(NULL);
if( 1 == isupdatestatustime){
my->lastused_time = g_cursecond ;//更新一下这个值,用来标记这个连接空等了多久
}
node->avail_count++;
return 0;
}
/*
* fun: set mysql connection dead
* arg: mysql connection
* ret: success 0, error -1
*
*/
static int my_conn_set_dead(my_conn_t *my)
{
int res = 0;
my_node_t *node = my->node;
my->conn = NULL;
buf_reset(&(my->buf));
list_move_tail(&(my->link), &(node->dead_head));
my->state_time = time(NULL);
node->avail_count--;//减少可用连接数
return 0;
}
/*
* fun: set mysql connection raw
* arg: mysql connection
* ret: success 0, error -1
*
*/
static int my_conn_set_raw(my_conn_t *my)
{
int res = 0;
my_node_t *node = my->node;
my->conn = NULL;
buf_reset(&(my->buf));
list_move_tail(&(my->link), &(node->raw_head));
my->state_time = time(NULL);
node->avail_count--;//减少可用连接数
return 0;
}
/*
* fun: set mysql connection fail
* arg: mysql connection
* ret: success 0, error -1
*
*/
static int my_conn_set_fail(my_conn_t *my)
{
int res = 0;
my_node_t *node = my->node;
list_move_tail(&(my->link), &(node->fail_head));
my->state_time = time(NULL);
node->avail_count--;//减少可用连接数
return 0;
}
/*
* fun: set mysql connection ping
* arg: mysql connection
* ret: success 0, error -1
*
*/
static int my_conn_set_ping(my_conn_t *my)
{
int res = 0;
my_node_t *node = my->node;
my->conn = NULL;
buf_reset(&(my->buf));
list_move_tail(&(my->link), &(node->ping_head));
my->state_time = time(NULL);
node->avail_count--;//减少可用连接数
return 0;
}
/*
* fun: dead reconnect timer
* arg: max connection to be processed
* ret: success 0, error -1
*
*/
static int my_conn_dead_reconnect_timer(unsigned long arg)
{//扫描每一个机器的mysql连接dead_head链表,将里面死掉的连接恢复
int i, res = 0;
my_node_t *node;
my_conn_t *my;
struct list_head *head, *pos, *n;
for(i = 0; i < mypool->slave_num; i++){
node = &(mypool->slave[i]);
if(my_node_is_closing(node)){
continue;
}
head = &(node->dead_head);
list_for_each_safe(pos, n, head){
my = list_entry(pos, my_conn_t, link);
list_del_init(pos);
if( (res = make_my_conn(my)) < 0 ){
log(g_log, "make_my_conn error\n");
}
}
}
return 0;
}
/*
* fun: fail reconnect timer
* arg: max connection to be processed
* ret: success 0, error -1
*
*/
static int my_conn_fail_reconnect_timer(unsigned long arg)
{
int i, res = 0, count;
my_node_t *node;
my_conn_t *my;
struct list_head *head, *pos, *n;
for(i = 0; i < mypool->slave_num; i++){
count = 0;
node = &(mypool->slave[i]);
if(my_node_is_closing(node)){
continue;
}
head = &(node->fail_head);
list_for_each_safe(pos, n, head){
if(count++ >= arg){
break;
}
my = list_entry(pos, my_conn_t, link);
list_del_init(pos);
if( (res = make_my_conn(my)) < 0 ){
log(g_log, "make_my_conn error\n");
}
}
}
return 0;
}
/*
* fun: mysql connection pool status timer
* arg: void
* ret: success 0, error -1
*
*/
static int my_conn_pool_status_timer(unsigned long arg)
{
int i, count1, count2, count3, count4, count5, count6;
my_node_t *node;
my_conn_t *my;
struct list_head *head, *pos, *n;
conn_t *c;
for(i = 0; i < mypool->slave_num; i++){
count1 = count2 = count3 = count4 = count5 = count6 = 0;
node = &(mypool->slave[i]);
if(my_node_is_closing(node)){
continue;
}
head = &(node->used_head);
list_for_each_safe(pos, n, head){
my = list_entry(pos, my_conn_t, link);
c = my->conn;
if(c != NULL){
debug(g_log, "connid: %d\n", c->connid);
} else {
debug(g_log, "no connect attach\n");
}
count1++;
}
head = &(node->avail_head);
list_for_each_safe(pos, n, head){
count2++;
}
head = &(node->dead_head);
list_for_each_safe(pos, n, head){
count3++;
}
head = &(node->raw_head);
list_for_each_safe(pos, n, head){
count4++;
}
head = &(node->fail_head);
list_for_each_safe(pos, n, head){
count5++;
}
head = &(node->ping_head);
list_for_each_safe(pos, n, head){
count6++;
}
log(g_log, \
"slave %s:%s used:%d free:%d dead:%d raw:%d fail:%d ping:%d\n", \
node->host, node->srv, count1, count2, count3, count4, count5, count6);
}
return 0;
}
/*
* fun: ping timer
* arg: max connection to be processed
* ret: success 0, error -1
*
*/
static int my_conn_pool_ping_timer(unsigned long arg)
{
int i, res = 0, count;
my_node_t *node;
my_conn_t *my;
struct list_head *head, *pos, *n;
size_t now = time(NULL);
for(i = 0; i < mypool->slave_num; i++){
count = 0;
node = &(mypool->slave[i]);
if(my_node_is_closing(node)){
continue;
}
head = &(node->avail_head);
list_for_each_safe(pos, n, head){
my = list_entry(pos, my_conn_t, link);
if( now - my->lastused_time < 30 || node->curall_connection <= node->min_connection ){
if(count++ >= arg){//一次最多允许ping这么多个连接
break;
}
if( (res = my_conn_set_ping(my)) < 0 ){
log(g_log, "my_conn_set_ping error\n");
}
my_ping_prepare(my);
}else {
//需要释放这个连接,因为他太久没有活动过了
my_conn_close_and_release( my) ;
info( g_log, "my_conn_pool_ping_timer connection idle for a long time, called my_conn_close_and_release\n") ;
}
}
}
return 0;
}
/*
* fun: ping timeout timer
* arg: max connection to be processed
* ret: success 0, error -1
*
*/
static int my_conn_pool_ping_timeout_timer(unsigned long arg)
{
int i, res = 0, count;
my_node_t *node;
my_conn_t *my;
struct list_head *head, *pos, *n;
time_t now = time(NULL);
for(i = 0; i < mypool->slave_num; i++){
count = 0;
node = &(mypool->slave[i]);
if(my_node_is_closing(node)){
continue;
}
head = &(node->ping_head);
list_for_each_safe(pos, n, head){
if(count++ >= arg){
break;
}
my = list_entry(pos, my_conn_t, link);
if(now - my->state_time > g_conf.mysql_ping_timeout){
my_conn_close_on_fail(my);
} else {
break;
}
}
}
return 0;
}
/*
* fun: cleanup closing node
* arg: mysql node
* ret: success 0, error -1
*
*/
static int my_node_closing_cleanup(my_node_t *node)
{
struct list_head *head, *pos, *n;
my_conn_t *my;
conn_t *c;
head = &(node->used_head);
list_for_each_safe(pos, n, head){
my = list_entry(pos, my_conn_t, link);
c = my->conn;
log(g_log, "conn:%d cleanup\n", c->connid);
conn_close(c);
my_conn_close_and_release(my);
}
head = &(node->avail_head);
list_for_each_safe(pos, n, head){
my = list_entry(pos, my_conn_t, link);
my_conn_close_and_release(my);
}
head = &(node->dead_head);
list_for_each_safe(pos, n, head){
my = list_entry(pos, my_conn_t, link);
my_conn_close_and_release(my);
}
head = &(node->raw_head);
list_for_each_safe(pos, n, head){
my = list_entry(pos, my_conn_t, link);
my_conn_close_and_release(my);
}
head = &(node->fail_head);
list_for_each_safe(pos, n, head){
my = list_entry(pos, my_conn_t, link);
my_conn_close_and_release(my);
}
head = &(node->ping_head);
list_for_each_safe(pos, n, head){
my = list_entry(pos, my_conn_t, link);
my_conn_close_and_release(my);
}
node->role = 0;