Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit afc52cf

Browse files
committedSep 20, 2024·
storage/innobase/dict/dict0dict: add a RAII class to freeze a dict_sys_t
This replaces a lot of manual freeze() and unfreeze() call. Doing it with RAII is safer and easier. I did not replace all freeze()/unfreeze() pairs because some callers unfreeze and re-freeze in the middle of a scope. Sometimes, adding a new scope can be added just for such a RAII object. Refactoring that can be done later. Notes: - Instead of using the global variable `dict_sys`, I decided to pass a `dict_sys_t` reference parameter, because I believe it will be necessary to eliminate that global variable eventually (in order to have a per-catalog instance). Hard-coding this global variable here would generate identical (not better) machine code and would be a step in the wrong direction. - The new macros `SRW_LOCK_ARGS2` and `SRW_LOCK_CALL2` were necessary because this is the first time those debug-only parameters are forwarded from a function that has more parameters (i.e. the dict_sys_t reference).
1 parent 9802529 commit afc52cf

File tree

8 files changed

+48
-57
lines changed

8 files changed

+48
-57
lines changed
 

‎storage/innobase/dict/dict0dict.cc

+6-17
Original file line numberDiff line numberDiff line change
@@ -808,9 +808,8 @@ dict_acquire_mdl_shared(dict_table_t *table,
808808

809809
if (trylock)
810810
{
811-
dict_sys.freeze(SRW_LOCK_CALL);
811+
const Scope_freeze_dict_sys freeze_dict_sys{dict_sys SRW_LOCK_CALL2};
812812
db_len= dict_get_db_name_len(table->name.m_name);
813-
dict_sys.unfreeze();
814813
}
815814
else
816815
{
@@ -871,9 +870,8 @@ dict_table_t *dict_table_open_on_id(table_id_t table_id, bool dict_locked,
871870
dict_sys.unlock();
872871
if (table && thd)
873872
{
874-
dict_sys.freeze(SRW_LOCK_CALL);
873+
const Scope_freeze_dict_sys freeze_dict_sys{dict_sys SRW_LOCK_CALL2};
875874
table= dict_acquire_mdl_shared<false>(table, thd, mdl, table_op);
876-
dict_sys.unfreeze();
877875
}
878876
return table;
879877
}
@@ -3651,19 +3649,13 @@ dict_index_get_if_in_cache(
36513649
/*=======================*/
36523650
index_id_t index_id) /*!< in: index id */
36533651
{
3654-
dict_index_t* index;
3655-
36563652
if (!dict_sys.is_initialised()) {
36573653
return(NULL);
36583654
}
36593655

3660-
dict_sys.freeze(SRW_LOCK_CALL);
3661-
3662-
index = dict_index_get_if_in_cache_low(index_id);
3656+
const Scope_freeze_dict_sys freeze_dict_sys{dict_sys SRW_LOCK_CALL2};
36633657

3664-
dict_sys.unfreeze();
3665-
3666-
return(index);
3658+
return dict_index_get_if_in_cache_low(index_id);
36673659
}
36683660

36693661
/**********************************************************************//**
@@ -3930,7 +3922,7 @@ dict_print_info_on_foreign_keys(
39303922
dict_foreign_t* foreign;
39313923
std::string str;
39323924

3933-
dict_sys.freeze(SRW_LOCK_CALL);
3925+
const Scope_freeze_dict_sys freeze_dict_sys{dict_sys SRW_LOCK_CALL2};
39343926

39353927
for (dict_foreign_set::iterator it = table->foreign_set.begin();
39363928
it != table->foreign_set.end();
@@ -3997,7 +3989,6 @@ dict_print_info_on_foreign_keys(
39973989
}
39983990
}
39993991

4000-
dict_sys.unfreeze();
40013992
return str;
40023993
}
40033994

@@ -4191,14 +4182,12 @@ void
41914182
dict_set_merge_threshold_all_debug(
41924183
uint merge_threshold_all)
41934184
{
4194-
dict_sys.freeze(SRW_LOCK_CALL);
4185+
const Scope_freeze_dict_sys freeze_dict_sys{dict_sys SRW_LOCK_CALL2};
41954186

41964187
dict_set_merge_threshold_list_debug(
41974188
&dict_sys.table_LRU, merge_threshold_all);
41984189
dict_set_merge_threshold_list_debug(
41994190
&dict_sys.table_non_LRU, merge_threshold_all);
4200-
4201-
dict_sys.unfreeze();
42024191
}
42034192

42044193
#endif /* UNIV_DEBUG */

‎storage/innobase/dict/dict0stats.cc

+4-8
Original file line numberDiff line numberDiff line change
@@ -3209,10 +3209,9 @@ dict_stats_save(
32093209
dict_table_t* table_stats = dict_table_open_on_name(
32103210
TABLE_STATS_NAME, false, DICT_ERR_IGNORE_NONE);
32113211
if (table_stats) {
3212-
dict_sys.freeze(SRW_LOCK_CALL);
3212+
const Scope_freeze_dict_sys freeze_dict_sys{dict_sys SRW_LOCK_CALL2};
32133213
table_stats = dict_acquire_mdl_shared<false>(table_stats, thd,
32143214
&mdl_table);
3215-
dict_sys.unfreeze();
32163215
}
32173216
if (!table_stats
32183217
|| strcmp(table_stats->name.m_name, TABLE_STATS_NAME)) {
@@ -3226,10 +3225,9 @@ dict_stats_save(
32263225
dict_table_t* index_stats = dict_table_open_on_name(
32273226
INDEX_STATS_NAME, false, DICT_ERR_IGNORE_NONE);
32283227
if (index_stats) {
3229-
dict_sys.freeze(SRW_LOCK_CALL);
3228+
const Scope_freeze_dict_sys freeze_dict_sys{dict_sys SRW_LOCK_CALL2};
32303229
index_stats = dict_acquire_mdl_shared<false>(index_stats, thd,
32313230
&mdl_index);
3232-
dict_sys.unfreeze();
32333231
}
32343232
if (!index_stats) {
32353233
goto release_and_exit;
@@ -3788,10 +3786,9 @@ dict_stats_fetch_from_ps(
37883786
dict_table_t* table_stats = dict_table_open_on_name(
37893787
TABLE_STATS_NAME, false, DICT_ERR_IGNORE_NONE);
37903788
if (table_stats) {
3791-
dict_sys.freeze(SRW_LOCK_CALL);
3789+
const Scope_freeze_dict_sys freeze_dict_sys{dict_sys SRW_LOCK_CALL2};
37923790
table_stats = dict_acquire_mdl_shared<false>(table_stats, thd,
37933791
&mdl_table);
3794-
dict_sys.unfreeze();
37953792
}
37963793
if (!table_stats
37973794
|| strcmp(table_stats->name.m_name, TABLE_STATS_NAME)) {
@@ -3805,10 +3802,9 @@ dict_stats_fetch_from_ps(
38053802
dict_table_t* index_stats = dict_table_open_on_name(
38063803
INDEX_STATS_NAME, false, DICT_ERR_IGNORE_NONE);
38073804
if (index_stats) {
3808-
dict_sys.freeze(SRW_LOCK_CALL);
3805+
const Scope_freeze_dict_sys freeze_dict_sys{dict_sys SRW_LOCK_CALL2};
38093806
index_stats = dict_acquire_mdl_shared<false>(index_stats, thd,
38103807
&mdl_index);
3811-
dict_sys.unfreeze();
38123808
}
38133809
if (!index_stats) {
38143810
goto release_and_exit;

‎storage/innobase/handler/ha_innodb.cc

+11-22
Original file line numberDiff line numberDiff line change
@@ -1367,19 +1367,17 @@ static void innodb_drop_database(handlerton*, char *path)
13671367
DICT_ERR_IGNORE_NONE);
13681368
if (table_stats)
13691369
{
1370-
dict_sys.freeze(SRW_LOCK_CALL);
1370+
const Scope_freeze_dict_sys freeze_dict_sys{dict_sys SRW_LOCK_CALL2};
13711371
table_stats= dict_acquire_mdl_shared<false>(table_stats,
13721372
thd, &mdl_table);
1373-
dict_sys.unfreeze();
13741373
}
13751374
index_stats= dict_table_open_on_name(INDEX_STATS_NAME, false,
13761375
DICT_ERR_IGNORE_NONE);
13771376
if (index_stats)
13781377
{
1379-
dict_sys.freeze(SRW_LOCK_CALL);
1378+
const Scope_freeze_dict_sys freeze_dict_sys{dict_sys SRW_LOCK_CALL2};
13801379
index_stats= dict_acquire_mdl_shared<false>(index_stats,
13811380
thd, &mdl_index);
1382-
dict_sys.unfreeze();
13831381
}
13841382

13851383
trx_start_for_ddl(trx);
@@ -13553,20 +13551,18 @@ int ha_innobase::delete_table(const char *name)
1355313551
DICT_ERR_IGNORE_NONE);
1355413552
if (table_stats)
1355513553
{
13556-
dict_sys.freeze(SRW_LOCK_CALL);
13554+
const Scope_freeze_dict_sys freeze_dict_sys{dict_sys SRW_LOCK_CALL2};
1355713555
table_stats= dict_acquire_mdl_shared<false>(table_stats,
1355813556
thd, &mdl_table);
13559-
dict_sys.unfreeze();
1356013557
}
1356113558

1356213559
index_stats= dict_table_open_on_name(INDEX_STATS_NAME, false,
1356313560
DICT_ERR_IGNORE_NONE);
1356413561
if (index_stats)
1356513562
{
13566-
dict_sys.freeze(SRW_LOCK_CALL);
13563+
const Scope_freeze_dict_sys freeze_dict_sys{dict_sys SRW_LOCK_CALL2};
1356713564
index_stats= dict_acquire_mdl_shared<false>(index_stats,
1356813565
thd, &mdl_index);
13569-
dict_sys.unfreeze();
1357013566
}
1357113567

1357213568
const bool skip_wait{table->name.is_temporary()};
@@ -13805,10 +13801,9 @@ int ha_innobase::truncate()
1380513801
/* fk_truncate_illegal_if_parent() should have failed in
1380613802
Sql_cmd_truncate_table::handler_truncate() if foreign_key_checks=ON
1380713803
and child tables exist. */
13808-
dict_sys.freeze(SRW_LOCK_CALL);
13804+
const Scope_freeze_dict_sys freeze_dict_sys{dict_sys SRW_LOCK_CALL2};
1380913805
for (const auto foreign : m_prebuilt->table->referenced_set)
1381013806
ut_ad(foreign->foreign_table == m_prebuilt->table);
13811-
dict_sys.unfreeze();
1381213807
}
1381313808
#endif
1381413809

@@ -13927,19 +13922,17 @@ int ha_innobase::truncate()
1392713922
DICT_ERR_IGNORE_NONE);
1392813923
if (table_stats)
1392913924
{
13930-
dict_sys.freeze(SRW_LOCK_CALL);
13925+
const Scope_freeze_dict_sys freeze_dict_sys{dict_sys SRW_LOCK_CALL2};
1393113926
table_stats= dict_acquire_mdl_shared<false>(table_stats, m_user_thd,
1393213927
&mdl_table);
13933-
dict_sys.unfreeze();
1393413928
}
1393513929
index_stats= dict_table_open_on_name(INDEX_STATS_NAME, false,
1393613930
DICT_ERR_IGNORE_NONE);
1393713931
if (index_stats)
1393813932
{
13939-
dict_sys.freeze(SRW_LOCK_CALL);
13933+
const Scope_freeze_dict_sys freeze_dict_sys{dict_sys SRW_LOCK_CALL2};
1394013934
index_stats= dict_acquire_mdl_shared<false>(index_stats, m_user_thd,
1394113935
&mdl_index);
13942-
dict_sys.unfreeze();
1394313936
}
1394413937

1394513938
if (table_stats && index_stats &&
@@ -14099,18 +14092,16 @@ ha_innobase::rename_table(
1409914092
table_stats = dict_table_open_on_name(TABLE_STATS_NAME, false,
1410014093
DICT_ERR_IGNORE_NONE);
1410114094
if (table_stats) {
14102-
dict_sys.freeze(SRW_LOCK_CALL);
14095+
const Scope_freeze_dict_sys freeze_dict_sys{dict_sys SRW_LOCK_CALL2};
1410314096
table_stats = dict_acquire_mdl_shared<false>(
1410414097
table_stats, thd, &mdl_table);
14105-
dict_sys.unfreeze();
1410614098
}
1410714099
index_stats = dict_table_open_on_name(INDEX_STATS_NAME, false,
1410814100
DICT_ERR_IGNORE_NONE);
1410914101
if (index_stats) {
14110-
dict_sys.freeze(SRW_LOCK_CALL);
14102+
const Scope_freeze_dict_sys freeze_dict_sys{dict_sys SRW_LOCK_CALL2};
1411114103
index_stats = dict_acquire_mdl_shared<false>(
1411214104
index_stats, thd, &mdl_index);
14113-
dict_sys.unfreeze();
1411414105
}
1411514106

1411614107
if (error == DB_SUCCESS && table_stats && index_stats
@@ -15642,10 +15633,8 @@ REPLACE, not an update.
1564215633

1564315634
uint ha_innobase::referenced_by_foreign_key()
1564415635
{
15645-
dict_sys.freeze(SRW_LOCK_CALL);
15646-
const bool empty= m_prebuilt->table->referenced_set.empty();
15647-
dict_sys.unfreeze();
15648-
return !empty;
15636+
const Scope_freeze_dict_sys freeze_dict_sys{dict_sys SRW_LOCK_CALL2};
15637+
return !m_prebuilt->table->referenced_set.empty();
1564915638
}
1565015639

1565115640
/*******************************************************************//**

‎storage/innobase/handler/handler0alter.cc

+2-4
Original file line numberDiff line numberDiff line change
@@ -11404,18 +11404,16 @@ ha_innobase::commit_inplace_alter_table(
1140411404
table_stats = dict_table_open_on_name(
1140511405
TABLE_STATS_NAME, false, DICT_ERR_IGNORE_NONE);
1140611406
if (table_stats) {
11407-
dict_sys.freeze(SRW_LOCK_CALL);
11407+
const Scope_freeze_dict_sys freeze_dict_sys{dict_sys SRW_LOCK_CALL2};
1140811408
table_stats = dict_acquire_mdl_shared<false>(
1140911409
table_stats, m_user_thd, &mdl_table);
11410-
dict_sys.unfreeze();
1141111410
}
1141211411
index_stats = dict_table_open_on_name(
1141311412
INDEX_STATS_NAME, false, DICT_ERR_IGNORE_NONE);
1141411413
if (index_stats) {
11415-
dict_sys.freeze(SRW_LOCK_CALL);
11414+
const Scope_freeze_dict_sys freeze_dict_sys{dict_sys SRW_LOCK_CALL2};
1141611415
index_stats = dict_acquire_mdl_shared<false>(
1141711416
index_stats, m_user_thd, &mdl_index);
11418-
dict_sys.unfreeze();
1141911417
}
1142011418

1142111419
if (table_stats && index_stats

‎storage/innobase/include/dict0dict.h

+22
Original file line numberDiff line numberDiff line change
@@ -1570,6 +1570,28 @@ class dict_sys_t
15701570
dberr_t create_or_check_sys_tables();
15711571
};
15721572

1573+
/*********************************************************************//**
1574+
Freeze a dict_sys_t and automatically unfreeze it when the scope exits. */
1575+
class Scope_freeze_dict_sys {
1576+
dict_sys_t &the_dict_sys;
1577+
1578+
public:
1579+
explicit Scope_freeze_dict_sys(dict_sys_t &_the_dict_sys SRW_LOCK_ARGS2(const char *file, unsigned line)) noexcept
1580+
:the_dict_sys(_the_dict_sys)
1581+
{
1582+
the_dict_sys.freeze(SRW_LOCK_ARGS(file, line));
1583+
}
1584+
1585+
~Scope_freeze_dict_sys() noexcept {
1586+
the_dict_sys.unfreeze();
1587+
}
1588+
1589+
private:
1590+
// copying/moving not allowed
1591+
Scope_freeze_dict_sys(const Scope_freeze_dict_sys &) = delete;
1592+
Scope_freeze_dict_sys &operator=(const Scope_freeze_dict_sys &) = delete;
1593+
};
1594+
15731595
/** the data dictionary cache */
15741596
extern dict_sys_t dict_sys;
15751597

‎storage/innobase/lock/lock0lock.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -4121,7 +4121,7 @@ dberr_t lock_table_children(dict_table_t *table, trx_t *trx)
41214121
if ((err= lock_table_for_trx(child.table, trx, LOCK_X)) != DB_SUCCESS)
41224122
break;
41234123

4124-
dict_sys.freeze(SRW_LOCK_CALL);
4124+
const Scope_freeze_dict_sys freeze_dict_sys{dict_sys SRW_LOCK_CALL2};
41254125
for (table_mdl &child : children)
41264126
{
41274127
if (child.mdl)
@@ -4130,7 +4130,6 @@ dberr_t lock_table_children(dict_table_t *table, trx_t *trx)
41304130
mdl_context->release_lock(child.mdl);
41314131
}
41324132
}
4133-
dict_sys.unfreeze();
41344133

41354134
return err;
41364135
}

‎storage/innobase/row/row0uins.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -383,9 +383,8 @@ static bool row_undo_ins_parse_undo_rec(undo_node_t* node, bool dict_locked)
383383
node->table = dict_table_open_on_id(table_id, dict_locked,
384384
DICT_TABLE_OP_NORMAL);
385385
} else if (!dict_locked) {
386-
dict_sys.freeze(SRW_LOCK_CALL);
386+
const Scope_freeze_dict_sys freeze_dict_sys{dict_sys SRW_LOCK_CALL2};
387387
node->table = dict_sys.acquire_temporary_table(table_id);
388-
dict_sys.unfreeze();
389388
} else {
390389
node->table = dict_sys.acquire_temporary_table(table_id);
391390
}

‎storage/innobase/row/row0umod.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -1210,9 +1210,8 @@ static bool row_undo_mod_parse_undo_rec(undo_node_t* node, bool dict_locked)
12101210
node->table = dict_table_open_on_id(table_id, dict_locked,
12111211
DICT_TABLE_OP_NORMAL);
12121212
} else if (!dict_locked) {
1213-
dict_sys.freeze(SRW_LOCK_CALL);
1213+
const Scope_freeze_dict_sys freeze_dict_sys{dict_sys SRW_LOCK_CALL2};
12141214
node->table = dict_sys.acquire_temporary_table(table_id);
1215-
dict_sys.unfreeze();
12161215
} else {
12171216
node->table = dict_sys.acquire_temporary_table(table_id);
12181217
}

0 commit comments

Comments
 (0)
Please sign in to comment.