@@ -2992,12 +2992,6 @@ void *spider_bg_sts_action(
2992
2992
if (spider.search_link_idx < 0 )
2993
2993
{
2994
2994
spider_trx_set_link_idx_for_all (&spider);
2995
- /*
2996
- spider.search_link_idx = spider_conn_next_link_idx(
2997
- thd, share->link_statuses, share->access_balances,
2998
- spider.conn_link_idx, spider.search_link_idx, share->link_count,
2999
- SPIDER_LINK_STATUS_OK);
3000
- */
3001
2995
spider.search_link_idx = spider_conn_first_link_idx (thd,
3002
2996
share->link_statuses , share->access_balances , spider.conn_link_idx ,
3003
2997
share->link_count , SPIDER_LINK_STATUS_OK);
@@ -3013,32 +3007,6 @@ void *spider_bg_sts_action(
3013
3007
share->conn_keys [spider.search_link_idx ], trx,
3014
3008
&spider, FALSE , FALSE , &error_num);
3015
3009
conns[spider.search_link_idx ]->error_mode = 0 ;
3016
- /*
3017
- if (
3018
- error_num &&
3019
- share->monitoring_kind[spider.search_link_idx] &&
3020
- need_mons[spider.search_link_idx]
3021
- ) {
3022
- lex_start(thd);
3023
- error_num = spider_ping_table_mon_from_table(
3024
- trx,
3025
- thd,
3026
- share,
3027
- spider.search_link_idx,
3028
- (uint32) share->monitoring_sid[spider.search_link_idx],
3029
- share->table_name,
3030
- share->table_name_length,
3031
- spider.conn_link_idx[spider.search_link_idx],
3032
- NULL,
3033
- 0,
3034
- share->monitoring_kind[spider.search_link_idx],
3035
- share->monitoring_limit[spider.search_link_idx],
3036
- share->monitoring_flag[spider.search_link_idx],
3037
- TRUE
3038
- );
3039
- lex_end(thd->lex);
3040
- }
3041
- */
3042
3010
spider.search_link_idx = -1 ;
3043
3011
}
3044
3012
if (spider.search_link_idx != -1 && conns[spider.search_link_idx ])
@@ -3049,31 +3017,6 @@ void *spider_bg_sts_action(
3049
3017
share->bg_sts_sync ,
3050
3018
2 , HA_STATUS_CONST | HA_STATUS_VARIABLE))
3051
3019
{
3052
- /*
3053
- if (
3054
- share->monitoring_kind[spider.search_link_idx] &&
3055
- need_mons[spider.search_link_idx]
3056
- ) {
3057
- lex_start(thd);
3058
- error_num = spider_ping_table_mon_from_table(
3059
- trx,
3060
- thd,
3061
- share,
3062
- spider.search_link_idx,
3063
- (uint32) share->monitoring_sid[spider.search_link_idx],
3064
- share->table_name,
3065
- share->table_name_length,
3066
- spider.conn_link_idx[spider.search_link_idx],
3067
- NULL,
3068
- 0,
3069
- share->monitoring_kind[spider.search_link_idx],
3070
- share->monitoring_limit[spider.search_link_idx],
3071
- share->monitoring_flag[spider.search_link_idx],
3072
- TRUE
3073
- );
3074
- lex_end(thd->lex);
3075
- }
3076
- */
3077
3020
spider.search_link_idx = -1 ;
3078
3021
}
3079
3022
}
@@ -3316,12 +3259,6 @@ void *spider_bg_crd_action(
3316
3259
if (spider.search_link_idx < 0 )
3317
3260
{
3318
3261
spider_trx_set_link_idx_for_all (&spider);
3319
- /*
3320
- spider.search_link_idx = spider_conn_next_link_idx(
3321
- thd, share->link_statuses, share->access_balances,
3322
- spider.conn_link_idx, spider.search_link_idx, share->link_count,
3323
- SPIDER_LINK_STATUS_OK);
3324
- */
3325
3262
spider.search_link_idx = spider_conn_first_link_idx (thd,
3326
3263
share->link_statuses , share->access_balances , spider.conn_link_idx ,
3327
3264
share->link_count , SPIDER_LINK_STATUS_OK);
@@ -3747,6 +3684,24 @@ void *spider_bg_mon_action(
3747
3684
}
3748
3685
}
3749
3686
3687
+ /* *
3688
+ Returns a random (active) server with a maximum required link status
3689
+
3690
+ Calculate the sum of balances of all servers whose link status is at
3691
+ most the specified status ("eligible"), generate a random number
3692
+ less than this balance, then find the first server cumulatively
3693
+ exceeding this balance
3694
+
3695
+ @param thd Connection used for generating a random number
3696
+ @param link_statuses The link statuses of servers
3697
+ @param access_balances The access balances of servers
3698
+ @param conn_link_idx Array of indexes to servers
3699
+ @param link_count Number of servers
3700
+ @param link_status The maximum required link status
3701
+ @retval Index to the found server
3702
+ @retval -1 if no eligible servers
3703
+ @retval -2 if out of memory
3704
+ */
3750
3705
int spider_conn_first_link_idx (
3751
3706
THD *thd,
3752
3707
long *link_statuses,
@@ -3755,35 +3710,35 @@ int spider_conn_first_link_idx(
3755
3710
int link_count,
3756
3711
int link_status
3757
3712
) {
3758
- int roop_count, active_links = 0 ;
3759
- longlong balance_total = 0 , balance_val ;
3713
+ int eligible_link_idx, eligible_links = 0 ;
3714
+ longlong balance_total = 0 , balance_threshold ;
3760
3715
double rand_val;
3761
- int *link_idxs, link_idx;
3762
- long *balances;
3716
+ int *link_idxs, result;
3763
3717
DBUG_ENTER (" spider_conn_first_link_idx" );
3764
3718
char *ptr;
3765
- ptr = (char *) my_alloca ((sizeof (int ) * link_count) + (sizeof (long ) * link_count));
3719
+ /* Allocate memory for link_idxs */
3720
+ ptr = (char *) my_alloca ((sizeof (int ) * link_count));
3766
3721
if (!ptr)
3767
3722
{
3768
3723
DBUG_PRINT (" info" ,(" spider out of memory" ));
3769
3724
DBUG_RETURN (-2 );
3770
3725
}
3771
3726
link_idxs = (int *) ptr;
3772
- ptr += sizeof (int ) * link_count;
3773
- balances = (long *) ptr;
3774
- for (roop_count = 0 ; roop_count < link_count; roop_count++)
3727
+
3728
+ /* Filter for eligible servers, store their indexes and calculate
3729
+ the total balances */
3730
+ for (int link_idx = 0 ; link_idx < link_count; link_idx++)
3775
3731
{
3776
- DBUG_ASSERT ((conn_link_idx[roop_count ] - roop_count ) % link_count == 0 );
3777
- if (link_statuses[conn_link_idx[roop_count ]] <= link_status)
3732
+ DBUG_ASSERT ((conn_link_idx[link_idx ] - link_idx ) % link_count == 0 );
3733
+ if (link_statuses[conn_link_idx[link_idx ]] <= link_status)
3778
3734
{
3779
- link_idxs[active_links] = roop_count;
3780
- balances[active_links] = access_balances[roop_count];
3781
- balance_total += access_balances[roop_count];
3782
- active_links++;
3735
+ link_idxs[eligible_links] = link_idx;
3736
+ balance_total += access_balances[link_idx];
3737
+ eligible_links++;
3783
3738
}
3784
3739
}
3785
3740
3786
- if (active_links == 0 )
3741
+ if (eligible_links == 0 )
3787
3742
{
3788
3743
DBUG_PRINT (" info" ,(" spider all links are failed" ));
3789
3744
my_afree (link_idxs);
@@ -3793,21 +3748,25 @@ int spider_conn_first_link_idx(
3793
3748
DBUG_PRINT (" info" ,(" spider thread_id=%lu" , thd_get_thread_id (thd)));
3794
3749
rand_val = spider_rand (thd->variables .server_id + thd_get_thread_id (thd));
3795
3750
DBUG_PRINT (" info" ,(" spider rand_val=%f" , rand_val));
3796
- balance_val = (longlong) (rand_val * balance_total);
3797
- DBUG_PRINT (" info" ,(" spider balance_val=%lld" , balance_val));
3798
- for (roop_count = 0 ; roop_count < active_links - 1 ; roop_count++)
3799
- {
3751
+ balance_threshold = (longlong) (rand_val * balance_total);
3752
+ DBUG_PRINT (" info" ,(" spider balance_threshold=%lld" , balance_threshold));
3753
+ /* Since balance_threshold < total balance, this loop WILL break */
3754
+ for (eligible_link_idx = 0 ;
3755
+ eligible_link_idx < eligible_links;
3756
+ eligible_link_idx++)
3757
+ {
3758
+ result = link_idxs[eligible_link_idx];
3759
+ const long balance = access_balances[result];
3800
3760
DBUG_PRINT (" info" ,(" spider balances[%d]=%ld" ,
3801
- roop_count, balances[roop_count] ));
3802
- if (balance_val < balances[roop_count] )
3761
+ link_idxs[eligible_link_idx], balance ));
3762
+ if (balance_threshold < balance )
3803
3763
break ;
3804
- balance_val -= balances[roop_count] ;
3764
+ balance_threshold -= balance ;
3805
3765
}
3806
3766
3807
- DBUG_PRINT (" info" ,(" spider first link_idx=%d" , link_idxs[roop_count]));
3808
- link_idx = link_idxs[roop_count];
3767
+ DBUG_PRINT (" info" ,(" spider first link_idx=%d" , result));
3809
3768
my_afree (link_idxs);
3810
- DBUG_RETURN (link_idx );
3769
+ DBUG_RETURN (result );
3811
3770
}
3812
3771
3813
3772
int spider_conn_next_link_idx (
@@ -3842,6 +3801,17 @@ int spider_conn_next_link_idx(
3842
3801
DBUG_RETURN (tmp_link_idx);
3843
3802
}
3844
3803
3804
+ /* *
3805
+ Finds the next active server with a maximum required link status
3806
+
3807
+ @param link_statuses The statuses of servers
3808
+ @param conn_link_idx The array of active servers
3809
+ @param link_idx The index of the current active server
3810
+ @param link_count The number of active servers
3811
+ @param link_status The required maximum link status
3812
+ @return The next active server whose link status is
3813
+ at most the required one.
3814
+ */
3845
3815
int spider_conn_link_idx_next (
3846
3816
long *link_statuses,
3847
3817
uint *conn_link_idx,
@@ -3854,6 +3824,8 @@ int spider_conn_link_idx_next(
3854
3824
link_idx++;
3855
3825
if (link_idx >= link_count)
3856
3826
break ;
3827
+ /* Asserts that the `link_idx`th active server is in the correct
3828
+ "group" */
3857
3829
DBUG_ASSERT ((conn_link_idx[link_idx] - link_idx) % link_count == 0 );
3858
3830
} while (link_statuses[conn_link_idx[link_idx]] > link_status);
3859
3831
DBUG_PRINT (" info" ,(" spider link_idx=%d" , link_idx));
0 commit comments