Debian Codegraph @

storage/innobase/ddl/ddl0loader.cc

raw
1 /*****************************************************************************
2
3 Copyright (c) 2020, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27
28 /** @file ddl/ddl0loader.cc
29 DDL index loader implementation.
30 Created 2020-11-01 by Sunny Bains. */
31
32 #include "btr0load.h"
33 #include "ddl0impl-builder.h"
34 #include "ddl0impl-cursor.h"
35 #include "ddl0impl-loader.h"
36 #include "handler0alter.h"
37 #include "os0thread-create.h"
38 #include "ut0stage.h"
39
40 #include "sql/table.h"
41 #include "sql_class.h"
42
43 namespace ddl {
44
45 /** Unbounded task queue. */
46 class Loader::Task_queue {
47 public:
48 /** Constructor.
49 @param[in] ctx DDL context.
50 @param[in] sync True for synchronous execution. */
51 explicit Task_queue(const Context &ctx, bool sync) noexcept;
52
53 /** Destructor. */
54 ~Task_queue() noexcept;
55
56 /** Enqueue a task to execute.
57 @param[in] task Task to queue. */
58 void enqueue(const Task &task) noexcept {
59 if (!m_sync) {
60 mutex_enter(&m_mutex);
61 }
62
63 m_tasks.push_back(task);
64 IF_DEBUG(++m_n_tasks_submitted;)
65
66 if (!m_sync) {
67 mutex_exit(&m_mutex);
68 os_event_set(m_consumer_event);
69 }
70 }
71
72 /** Dequeue and execute a task.
73 @return DB_SUCCESS or error code. */
74 dberr_t execute() {
75 if (!m_sync) {
76 ut_a(m_n_threads >= 1);
77 return mt_execute();
78 } else {
79 m_n_threads = 0;
80 return st_execute();
81 }
82 }
83
84 /** Note that we failed to create the configured number of threads. */
85 void thread_create_failed() noexcept {
86 ut_a(!m_sync);
87 mutex_enter(&m_mutex);
88 ut_a(m_n_threads > 0);
89 --m_n_threads;
90 mutex_exit(&m_mutex);
91 }
92
93 #ifdef UNIV_DEBUG
94 /** @return true if number of tasks submitted == number of tasks executed. */
95 bool validate() const noexcept {
96 return m_n_tasks_executed == m_n_tasks_submitted && m_n_threads == 0 &&
97 m_n_idle == 0;
98 }
99 #endif /* UNIV_DEBUG */
100
101 /** Wakeup the other threads e.g., on error. */
102 void signal() noexcept { os_event_set(m_consumer_event); }
103
104 private:
105 /** Execute function when there is more than one thread. The general idea
106 is as follows:
107 1. Some initial tasks are added before threads come here to execute tasks.
108 2. While executing, a task can generate more tasks. That is the only way
109 a task can be added.
110
111 [1] & [2] implies that when all threads are idle, all tasks are completed
112 and no more tasks can be added.
113
114 We exit here when all running threads are idle. */
115 dberr_t mt_execute() {
116 ut_a(!m_sync);
117
118 dberr_t err{DB_SUCCESS};
119
120 do {
121 mutex_enter(&m_mutex);
122
123 while (m_tasks.empty()) {
124 const auto sig_count = os_event_reset(m_consumer_event);
125
126 ++m_n_idle;
127
128 if (m_n_idle >= m_n_threads) {
129 ut_a(m_n_threads > 0);
130 --m_n_threads;
131
132 ut_a(m_n_idle > 0);
133 --m_n_idle;
134
135 mutex_exit(&m_mutex);
136
137 os_event_set(m_consumer_event);
138
139 return DB_SUCCESS;
140 }
141
142 mutex_exit(&m_mutex);
143
144 os_event_wait_low(m_consumer_event, sig_count);
145
146 err = m_ctx.get_error();
147
148 if (err != DB_SUCCESS) {
149 mutex_enter(&m_mutex);
150
151 ut_a(m_n_threads > 0);
152 --m_n_threads;
153
154 ut_a(m_n_idle > 0);
155 --m_n_idle;
156
157 mutex_exit(&m_mutex);
158
159 os_event_set(m_consumer_event);
160
161 return err;
162 }
163
164 os_event_reset(m_consumer_event);
165
166 mutex_enter(&m_mutex);
167
168 --m_n_idle;
169 }
170
171 auto task = m_tasks.front();
172 m_tasks.pop_front();
173
174 IF_DEBUG(++m_n_tasks_executed;)
175
176 mutex_exit(&m_mutex);
177
178 err = task();
179
180 } while (err == DB_SUCCESS);
181
182 mutex_enter(&m_mutex);
183
184 ut_a(m_n_threads > 0);
185 --m_n_threads;
186
187 mutex_exit(&m_mutex);
188
189 os_event_set(m_consumer_event);
190
191 return err;
192 }
193
194 /** Execute function when there is a single thread. */
195 dberr_t st_execute() {
196 ut_a(m_sync);
197
198 dberr_t err{DB_SUCCESS};
199
200 while (!m_tasks.empty()) {
201 auto task = m_tasks.front();
202 m_tasks.pop_front();
203
204 IF_DEBUG(++m_n_tasks_executed;)
205
206 err = task();
207
208 if (err != DB_SUCCESS) {
209 break;
210 }
211 }
212
213 return err;
214 }
215
216 private:
217 using Tasks = std::deque<Task, ut::allocator<Task>>;
218
219 /** DDL context. */
220 const Context &m_ctx;
221
222 /** true if synchronous execution model. */
223 bool m_sync{};
224
225 /** The task queue. */
226 Tasks m_tasks{};
227
228 /** Mutex protecting m_tasks access. */
229 ib_mutex_t m_mutex;
230
231 /** Task queue consumer event. */
232 os_event_t m_consumer_event{};
233
234 /** Number of threads (including foreground thread). */
235 size_t m_n_threads{};
236
237 /** Number of threads idle. */
238 size_t m_n_idle{};
239
240 /** Number of tasks executed. */
241 IF_DEBUG(size_t m_n_tasks_executed{};)
242
243 /** Number of tasks submitted. */
244 IF_DEBUG(size_t m_n_tasks_submitted{};)
245 };
246
247 Loader::Task_queue::Task_queue(const Context &ctx, bool sync) noexcept
248 : m_ctx(ctx), m_sync(sync), m_n_threads(ctx.m_max_threads) {
249 if (!m_sync) {
250 m_consumer_event = os_event_create();
251 mutex_create(LATCH_ID_WORK_QUEUE, &m_mutex);
252 }
253 }
254
255 Loader::Task_queue::~Task_queue() noexcept {
256 if (!m_sync) {
257 mutex_destroy(&m_mutex);
258
259 if (m_consumer_event != nullptr) {
260 os_event_destroy(m_consumer_event);
261 }
262 }
263 }
264
265 Loader::Loader(Context &ctx) noexcept : m_ctx(ctx) {}
266
267 Loader::~Loader() noexcept {
268 if (m_ctx.m_stage != nullptr) {
269 Alter_stages alter_stages{};
270
271 for (auto &builder : m_builders) {
272 alter_stages.push_back(builder->stage());
273 }
274
275 m_ctx.m_stage->aggregate(alter_stages);
276 }
277
278 for (auto builder : m_builders) {
279 ut::delete_(builder);
280 }
281
282 if (m_taskq != nullptr) {
283 ut::delete_(m_taskq);
284 }
285 }
286
287 void Loader::add_task(Task task) noexcept { m_taskq->enqueue(task); }
288
289 dberr_t Loader::load() noexcept {
290 ut_a(m_taskq == nullptr);
291
292 const bool sync = m_ctx.m_max_threads <= 1;
293
294 m_taskq = ut::new_withkey<Task_queue>(ut::make_psi_memory_key(mem_key_ddl),
295 m_ctx, sync);
296
297 if (m_taskq == nullptr) {
298 return DB_OUT_OF_MEMORY;
299 }
300
301 for (auto builder : m_builders) {
302 ut_a(builder->get_state() == Builder::State::ADD);
303 /* RTrees are built during the scan phase, using row by row insert. */
304 if (!builder->is_spatial_index()) {
305 builder->set_next_state();
306 add_task(Task{builder});
307 }
308 }
309
310 std::vector<std::thread> threads{};
311
312 if (!sync) {
313 auto fn = [=](PSI_thread_seqnum seqnum) -> dberr_t {
314 #ifdef UNIV_PFS_THREAD
315 Runnable runnable{ddl_thread_key, seqnum};
316 #else
317 Runnable runnable{PSI_NOT_INSTRUMENTED, seqnum};
318 #endif /* UNIV_PFS_THREAD */
319
320 current_thd = nullptr;
321
322 const auto err = runnable(&Task_queue::execute, m_taskq);
323
324 if (err != DB_SUCCESS) {
325 m_taskq->signal();
326 }
327
328 return err;
329 };
330
331 for (size_t i = 1; i < m_ctx.m_max_threads; ++i) {
332 try {
333 threads.push_back(std::thread{fn, i});
334 } catch (...) {
335 ib::warn(ER_DDL_MSG_1);
336 m_taskq->thread_create_failed();
337 break;
338 }
339 }
340 }
341
342 auto err = m_taskq->execute();
343
344 if (!sync) {
345 if (err != DB_SUCCESS) {
346 m_taskq->signal();
347 }
348
349 for (auto &thread : threads) {
350 thread.join();
351 }
352 }
353
354 if (err == DB_SUCCESS) {
355 err = m_ctx.get_error();
356 }
357
358 ut_ad(m_taskq->validate() || err != DB_SUCCESS);
359
360 ut::delete_(m_taskq);
361 m_taskq = nullptr;
362
363 return err;
364 }
365
366 dberr_t Loader::prepare() noexcept {
367 ut_a(m_builders.empty());
368 ut_a(!srv_read_only_mode);
369 ut_a(!m_ctx.m_add_cols || m_ctx.m_col_map != nullptr);
370 ut_a((m_ctx.m_old_table == m_ctx.m_new_table) == !m_ctx.m_col_map);
371
372 /* Allocate memory for merge file data structure and initialize fields */
373
374 auto err = m_ctx.setup_fts_build();
375
376 if (err != DB_SUCCESS) {
377 return err;
378 }
379
380 for (size_t i = 0; i < m_ctx.m_indexes.size(); ++i) {
381 auto builder = ut::new_withkey<Builder>(
382 ut::make_psi_memory_key(mem_key_ddl), m_ctx, *this, i);
383
384 if (builder == nullptr) {
385 return DB_OUT_OF_MEMORY;
386 }
387
388 m_builders.push_back(builder);
389 }
390
391 return DB_SUCCESS;
392 }
393
394 #define SYNC_POINT_ADD(t, n) \
395 DBUG_EXECUTE_IF((n), Sync_point::add((t), (n)); \
396 sync_points.push_back({(t), (n)});)
397
398 dberr_t Loader::scan_and_build_indexes() noexcept {
399 #ifdef UNIV_DEBUG
400 using Sync_points = std::vector<std::pair<THD *, const std::string>>;
401
402 auto thd = m_ctx.thd();
403 Sync_points sync_points{};
404
405 SYNC_POINT_ADD(thd, "ddl_tmpfile_fail");
406 SYNC_POINT_ADD(thd, "ddl_read_failure");
407 SYNC_POINT_ADD(thd, "ddl_write_failure");
408 SYNC_POINT_ADD(thd, "ddl_ins_spatial_fail");
409 SYNC_POINT_ADD(thd, "ddl_fts_write_failure");
410 SYNC_POINT_ADD(thd, "ddl_merge_sort_interrupt");
411 SYNC_POINT_ADD(thd, "ddl_instrument_log_check_flush");
412 SYNC_POINT_ADD(thd, "fts_instrument_sync_interrupted");
413 SYNC_POINT_ADD(thd, "ddl_btree_build_too_big_record");
414 SYNC_POINT_ADD(thd, "ddl_btree_build_oom");
415 SYNC_POINT_ADD(thd, "ddl_btree_build_interrupt");
416 SYNC_POINT_ADD(thd, "ddl_btree_build_sleep");
417 SYNC_POINT_ADD(thd, "ddl_btree_build_insert_return_interrupt");
418
419 auto cleanup = [&]() {
420 for (auto &s : sync_points) {
421 Sync_point::erase(s.first, s.second);
422 }
423 };
424
425 #endif /* UNIV_DEBUG */
426
427 auto cursor = Cursor::create_cursor(m_ctx);
428
429 if (cursor == nullptr) {
430 ut_d(cleanup());
431 return DB_OUT_OF_MEMORY;
432 }
433
434 auto err = m_ctx.read_init(cursor);
435
436 if (err == DB_SUCCESS) {
437 cursor->open();
438
439 /* Reset the MySQL row buffer that is used when reporting duplicate keys.
440 Return needs to be checked since innobase_rec_reset tries to evaluate
441 set_default() which can also be a function and might return errors */
442 innobase_rec_reset(m_ctx.m_table);
443
444 if (m_ctx.m_table->in_use->is_error()) {
445 err = DB_COMPUTE_VALUE_FAILED;
446 } else {
447 /* Read clustered index of the table and create files for secondary
448 index entries for merge sort and bulk build of the indexes. */
449 err = cursor->scan(m_builders);
450 }
451
452 /* Close the mtr and release any locks, wait for FTS etc. */
453 err = cursor->finish(err);
454
455 DBUG_EXECUTE_IF("force_virtual_col_build_fail",
456 err = DB_COMPUTE_VALUE_FAILED;);
457
458 DEBUG_SYNC_C("ddl_after_scan");
459
460 if (err == DB_SUCCESS) {
461 err = load();
462 }
463
464 DBUG_EXECUTE_IF("ddl_insert_big_row", err = DB_TOO_BIG_RECORD;);
465 }
466
467 if (cursor != nullptr) {
468 ut::delete_(cursor);
469 cursor = nullptr;
470 }
471
472 ut_d(cleanup());
473
474 return err;
475 }
476
477 dberr_t Loader::build_all() noexcept {
478 auto err = prepare();
479
480 if (err == DB_SUCCESS) {
481 err = scan_and_build_indexes();
482 }
483
484 DBUG_EXECUTE_IF("ib_build_indexes_too_many_concurrent_trxs",
485 err = DB_TOO_MANY_CONCURRENT_TRXS;
486 m_ctx.m_trx->error_state = err;);
487
488 if (m_ctx.m_fts.m_ptr != nullptr) {
489 /* Clean up FTS psort related resource */
490 ut::delete_(m_ctx.m_fts.m_ptr);
491 m_ctx.m_fts.m_ptr = nullptr;
492 }
493
494 DICT_TF2_FLAG_UNSET(m_ctx.m_new_table, DICT_TF2_FTS_ADD_DOC_ID);
495
496 if (err == DB_AUTOINC_READ_ERROR) {
497 auto trx = m_ctx.m_trx;
498 ib_senderrf(trx->mysql_thd, IB_LOG_LEVEL_ERROR, ER_AUTOINC_READ_FAILED);
499 }
500
501 if (err != DB_SUCCESS) {
502 m_ctx.set_error(err);
503 }
504
505 return err;
506 }
507
508 #ifdef UNIV_DEBUG
509 bool Loader::validate_indexes() const noexcept {
510 for (auto &builder : m_builders) {
511 if (!builder->is_fts_index() &&
512 !btr_validate_index(builder->index(), nullptr, false)) {
513 return false;
514 }
515 }
516
517 return true;
518 }
519 #endif /* UNIV_DEBUG */
520
521 } // namespace ddl