24
24
25
25
static handlerton *online_alter_hton;
26
26
27
+ typedef ilist<online_alter_cache_data> Online_alter_cache_list;
27
28
28
29
class online_alter_cache_data : public Sql_alloc , public ilist_node <>,
29
30
public binlog_cache_data
@@ -68,20 +69,39 @@ online_alter_cache_data *setup_cache_data(MEM_ROOT *root, TABLE_SHARE *share)
68
69
}
69
70
70
71
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 )
72
73
{
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
+
74
79
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);
75
95
/* 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 )
77
97
{
78
98
if (cache.sink_log == table->s ->online_alter_binlog )
79
99
return &cache;
80
100
}
81
101
82
102
MEM_ROOT *root= &thd->transaction ->mem_root ;
83
103
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);
85
105
86
106
return new_cache_data;
87
107
}
@@ -148,12 +168,13 @@ int online_alter_end_trans(handlerton *hton, THD *thd, bool all, bool commit)
148
168
{
149
169
DBUG_ENTER (" online_alter_end_trans" );
150
170
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 ())
152
173
DBUG_RETURN (0 );
153
174
154
175
bool is_ending_transaction= ending_trans (thd, all);
155
176
156
- for (auto &cache: thd-> online_alter_cache_list )
177
+ for (auto &cache: cache_list )
157
178
{
158
179
auto *binlog= cache.sink_log ;
159
180
DBUG_ASSERT (binlog);
@@ -197,14 +218,12 @@ int online_alter_end_trans(handlerton *hton, THD *thd, bool all, bool commit)
197
218
{
198
219
my_error (ER_ERROR_ON_WRITE, MYF (ME_ERROR_LOG),
199
220
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);
202
222
DBUG_RETURN (error);
203
223
}
204
224
}
205
225
206
- cleanup_cache_list (thd->online_alter_cache_list ,
207
- is_ending_transaction);
226
+ cleanup_cache_list (cache_list, is_ending_transaction);
208
227
209
228
for (TABLE *table= thd->open_tables ; table; table= table->next )
210
229
table->online_alter_cache = NULL ;
@@ -220,13 +239,14 @@ SAVEPOINT* savepoint_add(THD *thd, LEX_CSTRING name, SAVEPOINT **list,
220
239
int online_alter_savepoint_set (THD *thd, LEX_CSTRING name)
221
240
{
222
241
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 ())
224
244
DBUG_RETURN (0 );
225
245
226
246
if (savepoint_alloc_size < sizeof (SAVEPOINT) + sizeof (my_off_t ))
227
247
savepoint_alloc_size= sizeof (SAVEPOINT) + sizeof (my_off_t );
228
248
229
- for (auto &cache: thd-> online_alter_cache_list )
249
+ for (auto &cache: cache_list )
230
250
{
231
251
if (cache.hton ->savepoint_set == NULL )
232
252
continue ;
@@ -246,7 +266,9 @@ int online_alter_savepoint_set(THD *thd, LEX_CSTRING name)
246
266
int online_alter_savepoint_rollback (THD *thd, LEX_CSTRING name)
247
267
{
248
268
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)
250
272
{
251
273
if (cache.hton ->savepoint_set == NULL )
252
274
continue ;
@@ -264,7 +286,11 @@ int online_alter_savepoint_rollback(THD *thd, LEX_CSTRING name)
264
286
265
287
static int online_alter_close_connection (handlerton *hton, THD *thd)
266
288
{
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 );
268
294
return 0 ;
269
295
}
270
296
0 commit comments