Skip to content

Commit 0b62737

Browse files
MDEV-33661 MENT-1591 Documenting spider_mon_table_cache and friends.
Partial documentation due to time constraints. Will improve over time. Also removed a redundant parameter link_idx from spider_get_sys_tables_connect_info(). And deleted some commented out code.
1 parent fa1ae36 commit 0b62737

File tree

6 files changed

+150
-103
lines changed

6 files changed

+150
-103
lines changed

storage/spider/spd_copy_tables.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ int spider_udf_get_copy_tgt_tables(
307307

308308
if (
309309
(error_num = spider_get_sys_tables_connect_info(
310-
table_tables, tmp_share, 0, mem_root)) ||
310+
table_tables, tmp_share, mem_root)) ||
311311
(error_num = spider_get_sys_tables_link_status(
312312
table_tables, tmp_share, 0, mem_root)) ||
313313
(error_num = spider_get_sys_tables_link_idx(

storage/spider/spd_include.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,6 +1606,7 @@ typedef struct st_spider_table_mon
16061606
st_spider_table_mon *next;
16071607
} SPIDER_TABLE_MON;
16081608

1609+
/* List of `SPIDER_TABLE_MON's */
16091610
typedef struct st_spider_table_mon_list
16101611
{
16111612
char *key;

storage/spider/spd_ping_table.cc

Lines changed: 87 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ extern PSI_mutex_key spd_key_mutex_mon_list_update_status;
5454
extern PSI_mutex_key spd_key_mutex_mon_table_cache;
5555
#endif
5656

57+
/* Array (of size `spider_udf_table_mon_mutex_count') of hashes of
58+
`SPIDER_TABLE_MON_LIST'. */
5759
HASH *spider_udf_table_mon_list_hash;
5860
uint spider_udf_table_mon_list_hash_id;
5961
const char *spider_udf_table_mon_list_hash_func_name;
@@ -63,23 +65,43 @@ pthread_mutex_t *spider_udf_table_mon_mutexes;
6365
pthread_cond_t *spider_udf_table_mon_conds;
6466

6567
pthread_mutex_t spider_mon_table_cache_mutex;
68+
/* A cache to store distinct SPIDER_MON_KEYs with db name, table name
69+
and link id read from mysql.spider_link_mon_servers table. Initialised
70+
and populated in spider_init_ping_table_mon_cache(), and used in
71+
spider_ping_table_cache_compare(). The udf
72+
spider_flush_table_mon_cache is used to flag a initialisation. */
6673
DYNAMIC_ARRAY spider_mon_table_cache;
6774
uint spider_mon_table_cache_id;
6875
const char *spider_mon_table_cache_func_name;
6976
const char *spider_mon_table_cache_file_name;
7077
ulong spider_mon_table_cache_line_no;
78+
/* The mon table cache version, initialised at 0, and always no
79+
greater than spider_mon_table_cache_version_req. When the inequality
80+
is strict, an initialisation of spider_mon_table_cache will be
81+
triggered. */
7182
volatile ulonglong spider_mon_table_cache_version = 0;
83+
/* The required mon table cache version, incremented by one by the
84+
udf spider_flush_table_mon_cache */
7285
volatile ulonglong spider_mon_table_cache_version_req = 1;
7386

87+
/* Get or create a `SPIDER_TABLE_MON_LIST' for a key `str' */
7488
SPIDER_TABLE_MON_LIST *spider_get_ping_table_mon_list(
7589
SPIDER_TRX *trx,
7690
THD *thd,
77-
spider_string *str,
91+
spider_string *str, /* The key to search in
92+
`spider_udf_table_mon_list_hash',
93+
usually in the format of
94+
"./$db_name/$table_name000000000$link_idx" */
7895
uint conv_name_length,
7996
int link_idx,
8097
char *static_link_id,
8198
uint static_link_id_length,
82-
uint32 server_id,
99+
uint32 server_id, /* The server id of the monitor
100+
server, used for creating a new
101+
table mon list having a
102+
`SPIDER_TABLE_MON' corresponding to
103+
the server id as the `current'
104+
field */
83105
bool need_lock,
84106
int *error_num
85107
) {
@@ -91,6 +113,7 @@ SPIDER_TABLE_MON_LIST *spider_get_ping_table_mon_list(
91113
my_hash_value_type hash_value;
92114
#endif
93115
DBUG_ENTER("spider_get_ping_table_mon_list");
116+
/* Reset the cache if the version does not match the requirement */
94117
if (spider_mon_table_cache_version != spider_mon_table_cache_version_req)
95118
{
96119
SPD_INIT_ALLOC_ROOT(&mem_root, 4096, 0, MYF(MY_WME));
@@ -103,6 +126,9 @@ SPIDER_TABLE_MON_LIST *spider_get_ping_table_mon_list(
103126
free_root(&mem_root, MYF(0));
104127
}
105128

129+
/* Search for the table mon list in the hash, if one is not found or
130+
if it is found but has the wrong cache version, create and
131+
initialise a new one. */
106132
mutex_hash = spider_udf_calc_hash(str->c_ptr(),
107133
spider_param_udf_table_mon_mutex_count());
108134
DBUG_PRINT("info",("spider hash key=%s", str->c_ptr()));
@@ -129,12 +155,15 @@ SPIDER_TABLE_MON_LIST *spider_get_ping_table_mon_list(
129155
)
130156
#endif
131157
{
158+
/* If table_mon_list is found but the cache version does not
159+
match, remove it from the hash and free it. */
132160
if (
133161
table_mon_list &&
134162
table_mon_list->mon_table_cache_version != mon_table_cache_version
135163
)
136164
spider_release_ping_table_mon_list_loop(mutex_hash, table_mon_list);
137-
165+
/* create and initialise `table_mon_list' and insert it into the
166+
hash */
138167
if (!(table_mon_list = spider_get_ping_table_tgt(thd, str->c_ptr(),
139168
conv_name_length, link_idx, static_link_id, static_link_id_length,
140169
server_id, str, need_lock, error_num)))
@@ -277,6 +306,14 @@ int spider_release_ping_table_mon_list(
277306
DBUG_RETURN(0);
278307
}
279308

309+
/*
310+
Look for a `SPIDER_MON_KEY` in `spider_mon_table_cache' whose db and
311+
table name and link_idx matching `name' and `link_idx' with wild
312+
card matching. If a match is found, create `SPIDER_TABLE_MON's from
313+
all rows in mysql.spider_link_mon_servers that match the info in the
314+
`SPIDER_MON_KEY' and populate the `table_mon_list' with these
315+
`SPIDER_TABLE_MON's.
316+
*/
280317
int spider_get_ping_table_mon(
281318
THD *thd,
282319
SPIDER_TABLE_MON_LIST *table_mon_list,
@@ -357,13 +394,18 @@ int spider_get_ping_table_mon(
357394
goto error;
358395

359396
create_table_mon:
397+
/* Find the first row in mysql.spider_link_mon_servers matching the
398+
db name, table name and link_idx */
360399
if ((error_num = spider_get_sys_table_by_idx(table_link_mon, table_key,
361400
table_link_mon->s->primary_key, 3)))
362401
{
363402
table_link_mon->file->print_error(error_num, MYF(0));
364403
goto error;
365404
}
366405

406+
/* create one `SPIDER_TABLE_MON' per row in
407+
mysql.spider_link_mon_servers with matching db name, table name and
408+
link_idx, and add it to `table_mon_list'. */
367409
do {
368410
if (!(table_mon = (SPIDER_TABLE_MON *)
369411
spider_bulk_malloc(spider_current_trx, SPD_MID_GET_PING_TABLE_MON_1, MYF(MY_WME | MY_ZEROFILL),
@@ -394,7 +436,7 @@ int spider_get_ping_table_mon(
394436
(error_num = spider_get_sys_link_mon_server_id(
395437
table_link_mon, &table_mon->server_id, mem_root)) ||
396438
(error_num = spider_get_sys_link_mon_connect_info(
397-
table_link_mon, tmp_share, 0, mem_root))
439+
table_link_mon, tmp_share, mem_root))
398440
) {
399441
table_link_mon->file->print_error(error_num, MYF(0));
400442
spider_sys_index_end(table_link_mon);
@@ -458,15 +500,21 @@ int spider_get_ping_table_mon(
458500
DBUG_RETURN(error_num);
459501
}
460502

503+
/*
504+
creates and return table_mon_list associated with table with `name'
505+
and `link_idx'th link.
506+
*/
461507
SPIDER_TABLE_MON_LIST *spider_get_ping_table_tgt(
462508
THD *thd,
463-
char *name,
509+
char *name, /* The table name, usually fully qualified */
464510
uint name_length,
465511
int link_idx,
466512
char *static_link_id,
467513
uint static_link_id_length,
468-
uint32 server_id,
469-
spider_string *str,
514+
uint32 server_id, /* The server_id will determine the
515+
`current' field of the returned
516+
`SPIDER_TABLE_MON_LIST'. */
517+
spider_string *str, /* str->c_ptr() == name */
470518
bool need_lock,
471519
int *error_num
472520
) {
@@ -511,6 +559,7 @@ SPIDER_TABLE_MON_LIST *spider_get_ping_table_tgt(
511559
memcpy(key_str, str->ptr(), table_mon_list->key_length);
512560
tmp_share->access_charset = thd->variables.character_set_client;
513561

562+
/* Open mysql.spider_tables */
514563
if (
515564
!(table_tables = spider_open_sys_table(
516565
thd, SPIDER_SYS_TABLES_TABLE_NAME_STR,
@@ -520,6 +569,8 @@ SPIDER_TABLE_MON_LIST *spider_get_ping_table_tgt(
520569
my_error(*error_num, MYF(0));
521570
goto error;
522571
}
572+
/* store db and table names and link idx in mysql.spider_tables for
573+
reading */
523574
spider_store_tables_name(table_tables, name, name_length);
524575
if (static_link_id)
525576
{
@@ -543,9 +594,10 @@ SPIDER_TABLE_MON_LIST *spider_get_ping_table_tgt(
543594
goto error;
544595
}
545596
}
597+
/* Populate tmp_share with info read from mysql.spider_tables */
546598
if (
547599
(*error_num = spider_get_sys_tables_connect_info(
548-
table_tables, tmp_share, 0, &mem_root)) ||
600+
table_tables, tmp_share, &mem_root)) ||
549601
(*error_num = spider_get_sys_tables_link_status(
550602
table_tables, tmp_share, 0, &mem_root))
551603
) {
@@ -569,9 +621,8 @@ SPIDER_TABLE_MON_LIST *spider_get_ping_table_tgt(
569621
tmp_share, name, name_length
570622
)) ||
571623
(*error_num = spider_create_conn_keys(tmp_share)) ||
572-
/*
573-
(*error_num = spider_db_create_table_names_str(tmp_share)) ||
574-
*/
624+
/* Pinally, populate `table_mon_list' with newly created
625+
`SPIDER_TABLE_MON's */
575626
(*error_num = spider_get_ping_table_mon(
576627
thd, table_mon_list, name, name_length, link_idx, server_id, &mem_root,
577628
need_lock))
@@ -836,6 +887,11 @@ int spider_get_ping_table_gtid_pos(
836887
DBUG_RETURN(error_num);
837888
}
838889

890+
/*
891+
Initialise `spider_mon_table_cache' by scanning the
892+
mysql.spider_link_mon_servers table, creating distinct
893+
`SPIDER_MON_KEY's with the info and inserting them into the cache.
894+
*/
839895
int spider_init_ping_table_mon_cache(
840896
THD *thd,
841897
MEM_ROOT *mem_root,
@@ -867,6 +923,7 @@ int spider_init_ping_table_mon_cache(
867923
/* reset */
868924
spider_mon_table_cache.elements = 0;
869925

926+
/* start at the first row */
870927
if ((error_num = spider_sys_index_first(table_link_mon,
871928
table_link_mon->s->primary_key)))
872929
{
@@ -883,10 +940,16 @@ int spider_init_ping_table_mon_cache(
883940
mon_key.table_name_length = SPIDER_SYS_LINK_MON_TABLE_TABLE_NAME_SIZE + 1;
884941
mon_key.link_id_length = SPIDER_SYS_LINK_MON_TABLE_LINK_ID_SIZE + 1;
885942
do {
943+
/* update content of `mon_key' */
886944
if ((error_num = spider_get_sys_link_mon_key(table_link_mon, &mon_key,
887945
mem_root, &same)))
888946
goto error_get_sys_link_mon_key;
889947

948+
/* `mon_key' has changed content. since
949+
mysql.spider_link_mon_servers is indexed by db_name,
950+
table_name, link_idx, and server_id, it is possible that
951+
different server_ids share the same mon_key which only has
952+
db_name, table_name, link_idx */
890953
if (!same)
891954
{
892955
mon_key.sort = spider_calc_for_sort(3, mon_key.db_name,
@@ -941,6 +1004,13 @@ int spider_init_ping_table_mon_cache(
9411004
DBUG_RETURN(error_num);
9421005
}
9431006

1007+
/*
1008+
Read from msyql.spider_link_mon_servers table fields the db name,
1009+
table name and link_id and search for them with wild card matching
1010+
in `spider_mon_table_cache'. store the db name, table name, and
1011+
link_id of the matching `SPIDER_MON_KEY' back to the table field on
1012+
success.
1013+
*/
9441014
int spider_ping_table_cache_compare(
9451015
TABLE *table,
9461016
MEM_ROOT *mem_root
@@ -1239,9 +1309,6 @@ long long spider_ping_table_body(
12391309
DBUG_PRINT("info",("spider mon_table_result->result_status=SPIDER_LINK_MON_NG 2"));
12401310
if (table_mon_list->mon_status != SPIDER_LINK_MON_NG)
12411311
{
1242-
/*
1243-
pthread_mutex_lock(&table_mon_list->update_status_mutex);
1244-
*/
12451312
pthread_mutex_lock(&spider_udf_table_mon_mutexes[table_mon_list->mutex_hash]);
12461313
if (table_mon_list->mon_status != SPIDER_LINK_MON_NG)
12471314
{
@@ -1256,9 +1323,6 @@ long long spider_ping_table_body(
12561323
conv_name.c_ptr(), conv_name_length, link_idx, TRUE);
12571324
status_changed_to_ng = TRUE;
12581325
}
1259-
/*
1260-
pthread_mutex_unlock(&table_mon_list->update_status_mutex);
1261-
*/
12621326
pthread_mutex_unlock(&spider_udf_table_mon_mutexes[table_mon_list->mutex_hash]);
12631327
if (status_changed_to_ng)
12641328
{
@@ -1312,9 +1376,6 @@ long long spider_ping_table_body(
13121376
DBUG_PRINT("info",("spider mon_table_result->result_status=SPIDER_LINK_MON_NG 3"));
13131377
if (table_mon_list->mon_status != SPIDER_LINK_MON_NG)
13141378
{
1315-
/*
1316-
pthread_mutex_lock(&table_mon_list->update_status_mutex);
1317-
*/
13181379
pthread_mutex_lock(&spider_udf_table_mon_mutexes[table_mon_list->mutex_hash]);
13191380
if (table_mon_list->mon_status != SPIDER_LINK_MON_NG)
13201381
{
@@ -1329,9 +1390,6 @@ long long spider_ping_table_body(
13291390
conv_name.c_ptr(), conv_name_length, link_idx, TRUE);
13301391
status_changed_to_ng = TRUE;
13311392
}
1332-
/*
1333-
pthread_mutex_unlock(&table_mon_list->update_status_mutex);
1334-
*/
13351393
pthread_mutex_unlock(&spider_udf_table_mon_mutexes[table_mon_list->mutex_hash]);
13361394
if (status_changed_to_ng)
13371395
{
@@ -1375,9 +1433,6 @@ long long spider_ping_table_body(
13751433
mon_table_result->result_status == SPIDER_LINK_MON_NG &&
13761434
table_mon_list->mon_status != SPIDER_LINK_MON_NG
13771435
) {
1378-
/*
1379-
pthread_mutex_lock(&table_mon_list->update_status_mutex);
1380-
*/
13811436
pthread_mutex_lock(&spider_udf_table_mon_mutexes[table_mon_list->mutex_hash]);
13821437
if (table_mon_list->mon_status != SPIDER_LINK_MON_NG)
13831438
{
@@ -1392,9 +1447,6 @@ long long spider_ping_table_body(
13921447
conv_name.c_ptr(), conv_name_length, link_idx, TRUE);
13931448
status_changed_to_ng = TRUE;
13941449
}
1395-
/*
1396-
pthread_mutex_unlock(&table_mon_list->update_status_mutex);
1397-
*/
13981450
pthread_mutex_unlock(&spider_udf_table_mon_mutexes[table_mon_list->mutex_hash]);
13991451
if (status_changed_to_ng)
14001452
{
@@ -1565,9 +1617,9 @@ int spider_ping_table_mon_from_table(
15651617
SPIDER_SHARE *share,
15661618
int base_link_idx,
15671619
uint32 server_id,
1568-
char *conv_name,
1620+
char *conv_name, /* Usually fully qualified table name */
15691621
uint conv_name_length,
1570-
int link_idx,
1622+
int link_idx, /* The link id to ping */
15711623
char *where_clause,
15721624
uint where_clause_length,
15731625
long monitoring_kind,
@@ -1577,9 +1629,6 @@ int spider_ping_table_mon_from_table(
15771629
) {
15781630
int error_num = 0, current_mon_count, flags;
15791631
uint32 first_sid;
1580-
/*
1581-
THD *thd = trx->thd;
1582-
*/
15831632
SPIDER_TABLE_MON_LIST *table_mon_list;
15841633
SPIDER_TABLE_MON *table_mon;
15851634
SPIDER_MON_TABLE_RESULT mon_table_result;
@@ -1648,6 +1697,7 @@ int spider_ping_table_mon_from_table(
16481697
if (monitoring_flag & 1)
16491698
flags |= SPIDER_UDF_PING_TABLE_USE_ALL_MONITORING_NODES;
16501699

1700+
/* Get or create `table_mon_list' for `conv_name_str'. */
16511701
if (!(table_mon_list = spider_get_ping_table_mon_list(trx, thd,
16521702
&conv_name_str, conv_name_length, link_idx,
16531703
share->static_link_ids[link_idx],
@@ -1679,6 +1729,8 @@ int spider_ping_table_mon_from_table(
16791729
table_mon = table_mon_list->current;
16801730
first_sid = table_mon->server_id;
16811731
current_mon_count = 1;
1732+
/* Call spider_ping_table on each table_mon of `table_mon_list',
1733+
until one succeeds */
16821734
while (TRUE)
16831735
{
16841736
DBUG_PRINT("info",("spider thd->killed=%s",
@@ -1723,16 +1775,13 @@ int spider_ping_table_mon_from_table(
17231775
if (!spider_db_udf_ping_table_mon_next(
17241776
thd, table_mon, mon_conn, &mon_table_result, conv_name,
17251777
conv_name_length, link_idx,
1726-
where_clause, where_clause_length, -1, table_mon_list->list_size,
1778+
where_clause, where_clause_length, /*first_sid=*/-1, table_mon_list->list_size,
17271779
0, 0, 0, flags, monitoring_limit))
17281780
{
17291781
if (
17301782
mon_table_result.result_status == SPIDER_LINK_MON_NG &&
17311783
table_mon_list->mon_status != SPIDER_LINK_MON_NG
17321784
) {
1733-
/*
1734-
pthread_mutex_lock(&table_mon_list->update_status_mutex);
1735-
*/
17361785
pthread_mutex_lock(&spider_udf_table_mon_mutexes[table_mon_list->mutex_hash]);
17371786
if (table_mon_list->mon_status != SPIDER_LINK_MON_NG)
17381787
{
@@ -1747,9 +1796,6 @@ int spider_ping_table_mon_from_table(
17471796
spider_sys_log_tables_link_failed(thd, conv_name,
17481797
conv_name_length, link_idx, need_lock);
17491798
}
1750-
/*
1751-
pthread_mutex_unlock(&table_mon_list->update_status_mutex);
1752-
*/
17531799
pthread_mutex_unlock(&spider_udf_table_mon_mutexes[table_mon_list->mutex_hash]);
17541800
}
17551801
table_mon_list->last_caller_result = mon_table_result.result_status;

0 commit comments

Comments
 (0)