Skip to content

Commit b5d3171

Browse files
MDEV-29676 refactored and documented spider_get_share() and friends
Extracted out common subroutines, gave more meaningful names etc, added comments etc. Also: - Documented active servers load balancing reads, and other fields in SPIDER_SHARE etc. - Removed commented out code - Documented and refactored self-reference check - Removed some unnecessary functions - Renamed unhelpful roop_count - Refactored spider_get_{sts,crd}, where we turn get_type into an enum - Cleaned up spider_mbase_handler::show_table_status() and spider_mbase_handler::show_index()
1 parent a8dac17 commit b5d3171

15 files changed

+1305
-1961
lines changed

storage/spider/ha_spider.cc

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -309,10 +309,10 @@ int ha_spider::open(
309309
no_bytes_in_map(table->read_set));
310310
wide_handler_alloc = TRUE;
311311

312-
if (!share && !spider_get_share(name, table, thd, this, &error_num))
313-
goto error_get_share;
312+
if (!share && !spider_get_share(name, table, thd, this, &error_num))
313+
goto error_get_share;
314314

315-
wide_share = share->wide_share;
315+
wide_share = share->wide_share;
316316

317317
DBUG_PRINT("info",("spider create partition_handler"));
318318
DBUG_PRINT("info",("spider table=%p", table));
@@ -6559,13 +6559,6 @@ int ha_spider::info(
65596559
auto_inc_temporary = FALSE;
65606560
#endif
65616561
wide_handler->sql_command = thd_sql_command(thd);
6562-
/*
6563-
if (
6564-
sql_command == SQLCOM_DROP_TABLE ||
6565-
sql_command == SQLCOM_ALTER_TABLE ||
6566-
sql_command == SQLCOM_SHOW_CREATE
6567-
) {
6568-
*/
65696562
if (flag & HA_STATUS_AUTO)
65706563
{
65716564
if (share->lgtm_tblhnd_share->auto_increment_value)
@@ -6583,9 +6576,6 @@ int ha_spider::info(
65836576
wide_handler->sql_command == SQLCOM_ALTER_TABLE
65846577
)
65856578
DBUG_RETURN(0);
6586-
/*
6587-
}
6588-
*/
65896579

65906580
if (flag &
65916581
(HA_STATUS_TIME | HA_STATUS_CONST | HA_STATUS_VARIABLE | HA_STATUS_AUTO))

storage/spider/ha_spider.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ class ha_spider final : public handler
6363
char *conn_keys_first_ptr;
6464
char **conn_keys;
6565
SPIDER_CONN **conns;
66-
/* for active-standby mode */
66+
/* array of indexes of active servers */
6767
uint *conn_link_idx;
68+
/* A bitmap indicating whether each active server have some higher
69+
numbered server in the same "group" left to try (can fail over) */
6870
uchar *conn_can_fo;
6971
void **quick_targets;
7072
int *need_mons;

storage/spider/spd_conn.cc

Lines changed: 60 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -2992,12 +2992,6 @@ void *spider_bg_sts_action(
29922992
if (spider.search_link_idx < 0)
29932993
{
29942994
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-
*/
30012995
spider.search_link_idx = spider_conn_first_link_idx(thd,
30022996
share->link_statuses, share->access_balances, spider.conn_link_idx,
30032997
share->link_count, SPIDER_LINK_STATUS_OK);
@@ -3013,32 +3007,6 @@ void *spider_bg_sts_action(
30133007
share->conn_keys[spider.search_link_idx], trx,
30143008
&spider, FALSE, FALSE, &error_num);
30153009
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-
*/
30423010
spider.search_link_idx = -1;
30433011
}
30443012
if (spider.search_link_idx != -1 && conns[spider.search_link_idx])
@@ -3049,31 +3017,6 @@ void *spider_bg_sts_action(
30493017
share->bg_sts_sync,
30503018
2, HA_STATUS_CONST | HA_STATUS_VARIABLE))
30513019
{
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-
*/
30773020
spider.search_link_idx = -1;
30783021
}
30793022
}
@@ -3316,12 +3259,6 @@ void *spider_bg_crd_action(
33163259
if (spider.search_link_idx < 0)
33173260
{
33183261
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-
*/
33253262
spider.search_link_idx = spider_conn_first_link_idx(thd,
33263263
share->link_statuses, share->access_balances, spider.conn_link_idx,
33273264
share->link_count, SPIDER_LINK_STATUS_OK);
@@ -3747,6 +3684,24 @@ void *spider_bg_mon_action(
37473684
}
37483685
}
37493686

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+
*/
37503705
int spider_conn_first_link_idx(
37513706
THD *thd,
37523707
long *link_statuses,
@@ -3755,35 +3710,35 @@ int spider_conn_first_link_idx(
37553710
int link_count,
37563711
int link_status
37573712
) {
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;
37603715
double rand_val;
3761-
int *link_idxs, link_idx;
3762-
long *balances;
3716+
int *link_idxs, result;
37633717
DBUG_ENTER("spider_conn_first_link_idx");
37643718
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));
37663721
if (!ptr)
37673722
{
37683723
DBUG_PRINT("info",("spider out of memory"));
37693724
DBUG_RETURN(-2);
37703725
}
37713726
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++)
37753731
{
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)
37783734
{
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++;
37833738
}
37843739
}
37853740

3786-
if (active_links == 0)
3741+
if (eligible_links == 0)
37873742
{
37883743
DBUG_PRINT("info",("spider all links are failed"));
37893744
my_afree(link_idxs);
@@ -3793,21 +3748,25 @@ int spider_conn_first_link_idx(
37933748
DBUG_PRINT("info",("spider thread_id=%lu", thd_get_thread_id(thd)));
37943749
rand_val = spider_rand(thd->variables.server_id + thd_get_thread_id(thd));
37953750
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];
38003760
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)
38033763
break;
3804-
balance_val -= balances[roop_count];
3764+
balance_threshold -= balance;
38053765
}
38063766

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));
38093768
my_afree(link_idxs);
3810-
DBUG_RETURN(link_idx);
3769+
DBUG_RETURN(result);
38113770
}
38123771

38133772
int spider_conn_next_link_idx(
@@ -3842,6 +3801,17 @@ int spider_conn_next_link_idx(
38423801
DBUG_RETURN(tmp_link_idx);
38433802
}
38443803

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+
*/
38453815
int spider_conn_link_idx_next(
38463816
long *link_statuses,
38473817
uint *conn_link_idx,
@@ -3854,6 +3824,8 @@ int spider_conn_link_idx_next(
38543824
link_idx++;
38553825
if (link_idx >= link_count)
38563826
break;
3827+
/* Asserts that the `link_idx`th active server is in the correct
3828+
"group" */
38573829
DBUG_ASSERT((conn_link_idx[link_idx] - link_idx) % link_count == 0);
38583830
} while (link_statuses[conn_link_idx[link_idx]] > link_status);
38593831
DBUG_PRINT("info",("spider link_idx=%d", link_idx));

storage/spider/spd_copy_tables.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ int spider_udf_get_copy_tgt_tables(
398398
(error_num = spider_get_sys_tables_connect_info(
399399
table_tables, tmp_share, 0, mem_root)) ||
400400
(error_num = spider_get_sys_tables_link_status(
401-
table_tables, tmp_share, 0, mem_root)) ||
401+
table_tables, tmp_share->link_statuses, mem_root)) ||
402402
(error_num = spider_get_sys_tables_link_idx(
403403
table_tables, &table_conn->link_idx, mem_root))
404404
) {

storage/spider/spd_db_include.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,19 @@
1919
#define SPD_INIT_ALLOC_ROOT(A, B, C, D) \
2020
init_alloc_root(PSI_INSTRUMENT_ME, A, B, C, D)
2121

22+
/** Maximum possible number of `SPIDER_DBTON`s available to use */
2223
#define SPIDER_DBTON_SIZE 15
2324

2425
#ifndef SIZEOF_STORED_DOUBLE
2526
#define SIZEOF_STORED_DOUBLE 8
2627
#endif
2728

29+
/**
30+
Possible wrapper values, e.g. for `SPIDER_DBTON::wrapper` and
31+
`SPIDER_SHARE::tgt_wrappers`.
32+
33+
fixme: change this to enum
34+
*/
2835
#define SPIDER_DB_WRAPPER_MYSQL "mysql"
2936
#define SPIDER_DB_WRAPPER_MARIADB "mariadb"
3037

@@ -683,6 +690,8 @@ struct st_spider_db_request_key
683690
class spider_db_util
684691
{
685692
public:
693+
/** Same as the `SPIDER_DBTON::dbton_id` of the `SPIDER_DBTON`
694+
containing this `spider_db_util` */
686695
uint dbton_id;
687696
spider_db_util() = default;
688697
virtual ~spider_db_util() = default;
@@ -1683,7 +1692,10 @@ static const LEX_CSTRING maturity_name[] =
16831692

16841693
typedef struct st_spider_dbton
16851694
{
1695+
/** The index of this dbton in `spider_dbton` */
16861696
uint dbton_id;
1697+
/** The wrapper of this dbton, same possible values as each element
1698+
of `SPIDER_SHARE::tgt_wrappers` */
16871699
const char *wrapper;
16881700
enum spider_db_access_type db_access_type;
16891701
int (*init)();

0 commit comments

Comments
 (0)