@@ -54,6 +54,8 @@ extern PSI_mutex_key spd_key_mutex_mon_list_update_status;
54
54
extern PSI_mutex_key spd_key_mutex_mon_table_cache;
55
55
#endif
56
56
57
+ /* Array (of size `spider_udf_table_mon_mutex_count') of hashes of
58
+ `SPIDER_TABLE_MON_LIST'. */
57
59
HASH *spider_udf_table_mon_list_hash;
58
60
uint spider_udf_table_mon_list_hash_id;
59
61
const char *spider_udf_table_mon_list_hash_func_name;
@@ -63,23 +65,43 @@ pthread_mutex_t *spider_udf_table_mon_mutexes;
63
65
pthread_cond_t *spider_udf_table_mon_conds;
64
66
65
67
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. */
66
73
DYNAMIC_ARRAY spider_mon_table_cache;
67
74
uint spider_mon_table_cache_id;
68
75
const char *spider_mon_table_cache_func_name;
69
76
const char *spider_mon_table_cache_file_name;
70
77
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. */
71
82
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 */
72
85
volatile ulonglong spider_mon_table_cache_version_req = 1 ;
73
86
87
+ /* Get or create a `SPIDER_TABLE_MON_LIST' for a key `str' */
74
88
SPIDER_TABLE_MON_LIST *spider_get_ping_table_mon_list (
75
89
SPIDER_TRX *trx,
76
90
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" */
78
95
uint conv_name_length,
79
96
int link_idx,
80
97
char *static_link_id,
81
98
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 */
83
105
bool need_lock,
84
106
int *error_num
85
107
) {
@@ -91,6 +113,7 @@ SPIDER_TABLE_MON_LIST *spider_get_ping_table_mon_list(
91
113
my_hash_value_type hash_value;
92
114
#endif
93
115
DBUG_ENTER (" spider_get_ping_table_mon_list" );
116
+ /* Reset the cache if the version does not match the requirement */
94
117
if (spider_mon_table_cache_version != spider_mon_table_cache_version_req)
95
118
{
96
119
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(
103
126
free_root (&mem_root, MYF (0 ));
104
127
}
105
128
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. */
106
132
mutex_hash = spider_udf_calc_hash (str->c_ptr (),
107
133
spider_param_udf_table_mon_mutex_count ());
108
134
DBUG_PRINT (" info" ,(" spider hash key=%s" , str->c_ptr ()));
@@ -129,12 +155,15 @@ SPIDER_TABLE_MON_LIST *spider_get_ping_table_mon_list(
129
155
)
130
156
#endif
131
157
{
158
+ /* If table_mon_list is found but the cache version does not
159
+ match, remove it from the hash and free it. */
132
160
if (
133
161
table_mon_list &&
134
162
table_mon_list->mon_table_cache_version != mon_table_cache_version
135
163
)
136
164
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 */
138
167
if (!(table_mon_list = spider_get_ping_table_tgt (thd, str->c_ptr (),
139
168
conv_name_length, link_idx, static_link_id, static_link_id_length,
140
169
server_id, str, need_lock, error_num)))
@@ -277,6 +306,14 @@ int spider_release_ping_table_mon_list(
277
306
DBUG_RETURN (0 );
278
307
}
279
308
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
+ */
280
317
int spider_get_ping_table_mon (
281
318
THD *thd,
282
319
SPIDER_TABLE_MON_LIST *table_mon_list,
@@ -357,13 +394,18 @@ int spider_get_ping_table_mon(
357
394
goto error;
358
395
359
396
create_table_mon:
397
+ /* Find the first row in mysql.spider_link_mon_servers matching the
398
+ db name, table name and link_idx */
360
399
if ((error_num = spider_get_sys_table_by_idx (table_link_mon, table_key,
361
400
table_link_mon->s ->primary_key , 3 )))
362
401
{
363
402
table_link_mon->file ->print_error (error_num, MYF (0 ));
364
403
goto error;
365
404
}
366
405
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'. */
367
409
do {
368
410
if (!(table_mon = (SPIDER_TABLE_MON *)
369
411
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(
394
436
(error_num = spider_get_sys_link_mon_server_id (
395
437
table_link_mon, &table_mon->server_id , mem_root)) ||
396
438
(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))
398
440
) {
399
441
table_link_mon->file ->print_error (error_num, MYF (0 ));
400
442
spider_sys_index_end (table_link_mon);
@@ -458,15 +500,21 @@ int spider_get_ping_table_mon(
458
500
DBUG_RETURN (error_num);
459
501
}
460
502
503
+ /*
504
+ creates and return table_mon_list associated with table with `name'
505
+ and `link_idx'th link.
506
+ */
461
507
SPIDER_TABLE_MON_LIST *spider_get_ping_table_tgt (
462
508
THD *thd,
463
- char *name,
509
+ char *name, /* The table name, usually fully qualified */
464
510
uint name_length,
465
511
int link_idx,
466
512
char *static_link_id,
467
513
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 */
470
518
bool need_lock,
471
519
int *error_num
472
520
) {
@@ -511,6 +559,7 @@ SPIDER_TABLE_MON_LIST *spider_get_ping_table_tgt(
511
559
memcpy (key_str, str->ptr (), table_mon_list->key_length );
512
560
tmp_share->access_charset = thd->variables .character_set_client ;
513
561
562
+ /* Open mysql.spider_tables */
514
563
if (
515
564
!(table_tables = spider_open_sys_table (
516
565
thd, SPIDER_SYS_TABLES_TABLE_NAME_STR,
@@ -520,6 +569,8 @@ SPIDER_TABLE_MON_LIST *spider_get_ping_table_tgt(
520
569
my_error (*error_num, MYF (0 ));
521
570
goto error;
522
571
}
572
+ /* store db and table names and link idx in mysql.spider_tables for
573
+ reading */
523
574
spider_store_tables_name (table_tables, name, name_length);
524
575
if (static_link_id)
525
576
{
@@ -543,9 +594,10 @@ SPIDER_TABLE_MON_LIST *spider_get_ping_table_tgt(
543
594
goto error;
544
595
}
545
596
}
597
+ /* Populate tmp_share with info read from mysql.spider_tables */
546
598
if (
547
599
(*error_num = spider_get_sys_tables_connect_info (
548
- table_tables, tmp_share, 0 , &mem_root)) ||
600
+ table_tables, tmp_share, &mem_root)) ||
549
601
(*error_num = spider_get_sys_tables_link_status (
550
602
table_tables, tmp_share, 0 , &mem_root))
551
603
) {
@@ -569,9 +621,8 @@ SPIDER_TABLE_MON_LIST *spider_get_ping_table_tgt(
569
621
tmp_share, name, name_length
570
622
)) ||
571
623
(*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 */
575
626
(*error_num = spider_get_ping_table_mon (
576
627
thd, table_mon_list, name, name_length, link_idx, server_id, &mem_root,
577
628
need_lock))
@@ -836,6 +887,11 @@ int spider_get_ping_table_gtid_pos(
836
887
DBUG_RETURN (error_num);
837
888
}
838
889
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
+ */
839
895
int spider_init_ping_table_mon_cache (
840
896
THD *thd,
841
897
MEM_ROOT *mem_root,
@@ -867,6 +923,7 @@ int spider_init_ping_table_mon_cache(
867
923
/* reset */
868
924
spider_mon_table_cache.elements = 0 ;
869
925
926
+ /* start at the first row */
870
927
if ((error_num = spider_sys_index_first (table_link_mon,
871
928
table_link_mon->s ->primary_key )))
872
929
{
@@ -883,10 +940,16 @@ int spider_init_ping_table_mon_cache(
883
940
mon_key.table_name_length = SPIDER_SYS_LINK_MON_TABLE_TABLE_NAME_SIZE + 1 ;
884
941
mon_key.link_id_length = SPIDER_SYS_LINK_MON_TABLE_LINK_ID_SIZE + 1 ;
885
942
do {
943
+ /* update content of `mon_key' */
886
944
if ((error_num = spider_get_sys_link_mon_key (table_link_mon, &mon_key,
887
945
mem_root, &same)))
888
946
goto error_get_sys_link_mon_key;
889
947
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 */
890
953
if (!same)
891
954
{
892
955
mon_key.sort = spider_calc_for_sort (3 , mon_key.db_name ,
@@ -941,6 +1004,13 @@ int spider_init_ping_table_mon_cache(
941
1004
DBUG_RETURN (error_num);
942
1005
}
943
1006
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
+ */
944
1014
int spider_ping_table_cache_compare (
945
1015
TABLE *table,
946
1016
MEM_ROOT *mem_root
@@ -1239,9 +1309,6 @@ long long spider_ping_table_body(
1239
1309
DBUG_PRINT (" info" ,(" spider mon_table_result->result_status=SPIDER_LINK_MON_NG 2" ));
1240
1310
if (table_mon_list->mon_status != SPIDER_LINK_MON_NG)
1241
1311
{
1242
- /*
1243
- pthread_mutex_lock(&table_mon_list->update_status_mutex);
1244
- */
1245
1312
pthread_mutex_lock (&spider_udf_table_mon_mutexes[table_mon_list->mutex_hash ]);
1246
1313
if (table_mon_list->mon_status != SPIDER_LINK_MON_NG)
1247
1314
{
@@ -1256,9 +1323,6 @@ long long spider_ping_table_body(
1256
1323
conv_name.c_ptr (), conv_name_length, link_idx, TRUE );
1257
1324
status_changed_to_ng = TRUE ;
1258
1325
}
1259
- /*
1260
- pthread_mutex_unlock(&table_mon_list->update_status_mutex);
1261
- */
1262
1326
pthread_mutex_unlock (&spider_udf_table_mon_mutexes[table_mon_list->mutex_hash ]);
1263
1327
if (status_changed_to_ng)
1264
1328
{
@@ -1312,9 +1376,6 @@ long long spider_ping_table_body(
1312
1376
DBUG_PRINT (" info" ,(" spider mon_table_result->result_status=SPIDER_LINK_MON_NG 3" ));
1313
1377
if (table_mon_list->mon_status != SPIDER_LINK_MON_NG)
1314
1378
{
1315
- /*
1316
- pthread_mutex_lock(&table_mon_list->update_status_mutex);
1317
- */
1318
1379
pthread_mutex_lock (&spider_udf_table_mon_mutexes[table_mon_list->mutex_hash ]);
1319
1380
if (table_mon_list->mon_status != SPIDER_LINK_MON_NG)
1320
1381
{
@@ -1329,9 +1390,6 @@ long long spider_ping_table_body(
1329
1390
conv_name.c_ptr (), conv_name_length, link_idx, TRUE );
1330
1391
status_changed_to_ng = TRUE ;
1331
1392
}
1332
- /*
1333
- pthread_mutex_unlock(&table_mon_list->update_status_mutex);
1334
- */
1335
1393
pthread_mutex_unlock (&spider_udf_table_mon_mutexes[table_mon_list->mutex_hash ]);
1336
1394
if (status_changed_to_ng)
1337
1395
{
@@ -1375,9 +1433,6 @@ long long spider_ping_table_body(
1375
1433
mon_table_result->result_status == SPIDER_LINK_MON_NG &&
1376
1434
table_mon_list->mon_status != SPIDER_LINK_MON_NG
1377
1435
) {
1378
- /*
1379
- pthread_mutex_lock(&table_mon_list->update_status_mutex);
1380
- */
1381
1436
pthread_mutex_lock (&spider_udf_table_mon_mutexes[table_mon_list->mutex_hash ]);
1382
1437
if (table_mon_list->mon_status != SPIDER_LINK_MON_NG)
1383
1438
{
@@ -1392,9 +1447,6 @@ long long spider_ping_table_body(
1392
1447
conv_name.c_ptr (), conv_name_length, link_idx, TRUE );
1393
1448
status_changed_to_ng = TRUE ;
1394
1449
}
1395
- /*
1396
- pthread_mutex_unlock(&table_mon_list->update_status_mutex);
1397
- */
1398
1450
pthread_mutex_unlock (&spider_udf_table_mon_mutexes[table_mon_list->mutex_hash ]);
1399
1451
if (status_changed_to_ng)
1400
1452
{
@@ -1565,9 +1617,9 @@ int spider_ping_table_mon_from_table(
1565
1617
SPIDER_SHARE *share,
1566
1618
int base_link_idx,
1567
1619
uint32 server_id,
1568
- char *conv_name,
1620
+ char *conv_name, /* Usually fully qualified table name */
1569
1621
uint conv_name_length,
1570
- int link_idx,
1622
+ int link_idx, /* The link id to ping */
1571
1623
char *where_clause,
1572
1624
uint where_clause_length,
1573
1625
long monitoring_kind,
@@ -1577,9 +1629,6 @@ int spider_ping_table_mon_from_table(
1577
1629
) {
1578
1630
int error_num = 0 , current_mon_count, flags;
1579
1631
uint32 first_sid;
1580
- /*
1581
- THD *thd = trx->thd;
1582
- */
1583
1632
SPIDER_TABLE_MON_LIST *table_mon_list;
1584
1633
SPIDER_TABLE_MON *table_mon;
1585
1634
SPIDER_MON_TABLE_RESULT mon_table_result;
@@ -1648,6 +1697,7 @@ int spider_ping_table_mon_from_table(
1648
1697
if (monitoring_flag & 1 )
1649
1698
flags |= SPIDER_UDF_PING_TABLE_USE_ALL_MONITORING_NODES;
1650
1699
1700
+ /* Get or create `table_mon_list' for `conv_name_str'. */
1651
1701
if (!(table_mon_list = spider_get_ping_table_mon_list (trx, thd,
1652
1702
&conv_name_str, conv_name_length, link_idx,
1653
1703
share->static_link_ids [link_idx],
@@ -1679,6 +1729,8 @@ int spider_ping_table_mon_from_table(
1679
1729
table_mon = table_mon_list->current ;
1680
1730
first_sid = table_mon->server_id ;
1681
1731
current_mon_count = 1 ;
1732
+ /* Call spider_ping_table on each table_mon of `table_mon_list',
1733
+ until one succeeds */
1682
1734
while (TRUE )
1683
1735
{
1684
1736
DBUG_PRINT (" info" ,(" spider thd->killed=%s" ,
@@ -1723,16 +1775,13 @@ int spider_ping_table_mon_from_table(
1723
1775
if (!spider_db_udf_ping_table_mon_next (
1724
1776
thd, table_mon, mon_conn, &mon_table_result, conv_name,
1725
1777
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 ,
1727
1779
0 , 0 , 0 , flags, monitoring_limit))
1728
1780
{
1729
1781
if (
1730
1782
mon_table_result.result_status == SPIDER_LINK_MON_NG &&
1731
1783
table_mon_list->mon_status != SPIDER_LINK_MON_NG
1732
1784
) {
1733
- /*
1734
- pthread_mutex_lock(&table_mon_list->update_status_mutex);
1735
- */
1736
1785
pthread_mutex_lock (&spider_udf_table_mon_mutexes[table_mon_list->mutex_hash ]);
1737
1786
if (table_mon_list->mon_status != SPIDER_LINK_MON_NG)
1738
1787
{
@@ -1747,9 +1796,6 @@ int spider_ping_table_mon_from_table(
1747
1796
spider_sys_log_tables_link_failed (thd, conv_name,
1748
1797
conv_name_length, link_idx, need_lock);
1749
1798
}
1750
- /*
1751
- pthread_mutex_unlock(&table_mon_list->update_status_mutex);
1752
- */
1753
1799
pthread_mutex_unlock (&spider_udf_table_mon_mutexes[table_mon_list->mutex_hash ]);
1754
1800
}
1755
1801
table_mon_list->last_caller_result = mon_table_result.result_status ;
0 commit comments