Skip to content

Commit 8311eae

Browse files
committed
online alter: use thd->ha_data to store cache_list
1 parent cb52174 commit 8311eae

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

sql/online_alter.cc

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
static handlerton *online_alter_hton;
2626

27+
typedef ilist<online_alter_cache_data> Online_alter_cache_list;
2728

2829
class online_alter_cache_data: public Sql_alloc, public ilist_node<>,
2930
public binlog_cache_data
@@ -68,20 +69,39 @@ online_alter_cache_data *setup_cache_data(MEM_ROOT *root, TABLE_SHARE *share)
6869
}
6970

7071

71-
static online_alter_cache_data *get_cache_data(THD *thd, TABLE *table)
72+
static Online_alter_cache_list &get_cache_list(handlerton *ht, THD *thd)
7273
{
73-
ilist<online_alter_cache_data> &list= thd->online_alter_cache_list;
74+
void *data= thd_get_ha_data(thd, ht);
75+
DBUG_ASSERT(data);
76+
return *(Online_alter_cache_list*)data;
77+
}
78+
7479

80+
static Online_alter_cache_list &get_or_create_cache_list(THD *thd)
81+
{
82+
void *data= thd_get_ha_data(thd, online_alter_hton);
83+
if (!data)
84+
{
85+
data= new Online_alter_cache_list();
86+
thd_set_ha_data(thd, online_alter_hton, data);
87+
}
88+
return *(Online_alter_cache_list*)data;
89+
}
90+
91+
92+
static online_alter_cache_data* get_cache_data(THD *thd, TABLE *table)
93+
{
94+
auto &cache_list= get_or_create_cache_list(thd);
7595
/* we assume it's very rare to have more than one online ALTER running */
76-
for (auto &cache: list)
96+
for (auto &cache: cache_list)
7797
{
7898
if (cache.sink_log == table->s->online_alter_binlog)
7999
return &cache;
80100
}
81101

82102
MEM_ROOT *root= &thd->transaction->mem_root;
83103
auto *new_cache_data= setup_cache_data(root, table->s);
84-
list.push_back(*new_cache_data);
104+
cache_list.push_back(*new_cache_data);
85105

86106
return new_cache_data;
87107
}
@@ -148,12 +168,13 @@ int online_alter_end_trans(handlerton *hton, THD *thd, bool all, bool commit)
148168
{
149169
DBUG_ENTER("online_alter_end_trans");
150170
int error= 0;
151-
if (thd->online_alter_cache_list.empty())
171+
auto &cache_list= get_cache_list(hton, thd);
172+
if (cache_list.empty())
152173
DBUG_RETURN(0);
153174

154175
bool is_ending_transaction= ending_trans(thd, all);
155176

156-
for (auto &cache: thd->online_alter_cache_list)
177+
for (auto &cache: cache_list)
157178
{
158179
auto *binlog= cache.sink_log;
159180
DBUG_ASSERT(binlog);
@@ -197,14 +218,12 @@ int online_alter_end_trans(handlerton *hton, THD *thd, bool all, bool commit)
197218
{
198219
my_error(ER_ERROR_ON_WRITE, MYF(ME_ERROR_LOG),
199220
binlog->get_name(), errno);
200-
cleanup_cache_list(thd->online_alter_cache_list,
201-
is_ending_transaction);
221+
cleanup_cache_list(cache_list, is_ending_transaction);
202222
DBUG_RETURN(error);
203223
}
204224
}
205225

206-
cleanup_cache_list(thd->online_alter_cache_list,
207-
is_ending_transaction);
226+
cleanup_cache_list(cache_list, is_ending_transaction);
208227

209228
for (TABLE *table= thd->open_tables; table; table= table->next)
210229
table->online_alter_cache= NULL;
@@ -220,13 +239,14 @@ SAVEPOINT* savepoint_add(THD *thd, LEX_CSTRING name, SAVEPOINT **list,
220239
int online_alter_savepoint_set(THD *thd, LEX_CSTRING name)
221240
{
222241
DBUG_ENTER("binlog_online_alter_savepoint");
223-
if (thd->online_alter_cache_list.empty())
242+
auto &cache_list= get_cache_list(online_alter_hton, thd);
243+
if (cache_list.empty())
224244
DBUG_RETURN(0);
225245

226246
if (savepoint_alloc_size < sizeof (SAVEPOINT) + sizeof(my_off_t))
227247
savepoint_alloc_size= sizeof (SAVEPOINT) + sizeof(my_off_t);
228248

229-
for (auto &cache: thd->online_alter_cache_list)
249+
for (auto &cache: cache_list)
230250
{
231251
if (cache.hton->savepoint_set == NULL)
232252
continue;
@@ -246,7 +266,9 @@ int online_alter_savepoint_set(THD *thd, LEX_CSTRING name)
246266
int online_alter_savepoint_rollback(THD *thd, LEX_CSTRING name)
247267
{
248268
DBUG_ENTER("online_alter_savepoint_rollback");
249-
for (auto &cache: thd->online_alter_cache_list)
269+
270+
auto &cache_list= get_cache_list(online_alter_hton, thd);
271+
for (auto &cache: cache_list)
250272
{
251273
if (cache.hton->savepoint_set == NULL)
252274
continue;
@@ -264,7 +286,11 @@ int online_alter_savepoint_rollback(THD *thd, LEX_CSTRING name)
264286

265287
static int online_alter_close_connection(handlerton *hton, THD *thd)
266288
{
267-
DBUG_ASSERT(thd->online_alter_cache_list.empty());
289+
auto *cache_list= (Online_alter_cache_list*)thd_get_ha_data(thd, hton);
290+
291+
DBUG_ASSERT(!cache_list || cache_list->empty());
292+
delete cache_list;
293+
thd_set_ha_data(thd, hton, NULL);
268294
return 0;
269295
}
270296

sql/sql_class.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5634,8 +5634,6 @@ class THD: public THD_count, /* this must be first */
56345634
Item *sp_prepare_func_item(Item **it_addr, uint cols);
56355635
bool sp_eval_expr(Field *result_field, Item **expr_item_ptr);
56365636

5637-
ilist<online_alter_cache_data> online_alter_cache_list;
5638-
56395637
bool sql_parser(LEX *old_lex, LEX *lex,
56405638
char *str, uint str_len, bool stmt_prepare_mode);
56415639

0 commit comments

Comments
 (0)