Debian Codegraph @

storage/innobase/dict/dict0sdi.cc

raw
1 /*****************************************************************************
2
3 Copyright (c) 2017, 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 #include <algorithm>
29
30 #include <current_thd.h>
31 #include <sql_class.h>
32 #include <sql_show.h>
33 #include <sql_table.h>
34 #include <sql_tablespace.h>
35 #include "api0api.h"
36 #include "api0misc.h"
37 #include "dict0boot.h"
38 #include "dict0crea.h"
39 #include "dict0dd.h"
40 #include "dict0dict.h"
41 #include "dict0sdi-decompress.h"
42 #include "dict0sdi.h"
43 #include "fsp0fsp.h"
44 #include "ha_innodb.h"
45 #include "trx0trx.h"
46
47 /** Check if SDI Index exists in a tablespace
48 @param[in] dd_space DD tablespace object
49 @param[in,out] space_id space_id from tablespace object
50 @return DB_SUCCESS if SDI exists, else return DB_ERROR,
51 DB_TABLESPACE_NOT_FOUND */
52 static dberr_t dict_sdi_exists(const dd::Tablespace &dd_space,
53 uint32_t *space_id) {
54 if (dd_space.se_private_data().get(dd_space_key_strings[DD_SPACE_ID],
55 space_id)) {
56 /* error, attribute not found */
57 ut_d(ut_error);
58 ut_o(return (DB_ERROR));
59 }
60
61 ut_ad(check_trx_exists(current_thd) != nullptr);
62
63 if (fsp_is_undo_tablespace(*space_id) || fsp_is_system_temporary(*space_id)) {
64 /* Claim Success */
65 return (DB_SUCCESS);
66 }
67
68 return (fsp_has_sdi(*space_id));
69 }
70
71 /** Report error on failure
72 @param[in] errornum MySQL error number (for my_error()) (Must take
73 4 string arguments, in the same way as
74 ER_SDI_OPERATION_FAILED)
75 @param[in] operation SDI set or delete
76 @param[in] table table object for which SDI is serialized
77 @param[in] tablespace tablespace where SDI is stored */
78 static void dict_sdi_report_error(int errornum, const char *operation,
79 const dd::Table *table,
80 const dd::Tablespace &tablespace) {
81 THD *thd = current_thd;
82 const char *schema_name = nullptr;
83 const dd::Schema *schema = nullptr;
84 const char *table_name = nullptr;
85
86 if (thd != nullptr && table != nullptr) {
87 table_name = table->name().c_str();
88 if (thd->dd_client()->acquire(table->schema_id(), &schema)) {
89 schema_name = nullptr;
90 } else {
91 schema_name = schema->name().c_str();
92 }
93 }
94
95 if (schema_name == nullptr) {
96 schema_name = "<no schema>";
97 }
98
99 if (table_name == nullptr) {
100 table_name = "<no table>";
101 }
102
103 my_error(errornum, MYF(0), operation, schema_name, table_name,
104 tablespace.name().c_str());
105 }
106
107 /** Report error on failure. Calls dict_sdi_report_error(int errornum,
108 const char *operation, const dd::Table *table, const
109 dd::Tablespace &tablespace) with errornum=SDI_OPERATION_FAILED
110 (for compatibility with existing code).
111
112 @param[in] operation SDI set or delete
113 @param[in] table table object for which SDI is serialized
114 @param[in] tablespace tablespace where SDI is stored */
115 static void dict_sdi_report_error(const char *operation, const dd::Table *table,
116 const dd::Tablespace &tablespace) {
117 dict_sdi_report_error(ER_SDI_OPERATION_FAILED, operation, table, tablespace);
118 }
119
120 /** Create SDI in a tablespace. This API should be used when
121 upgrading a tablespace with no SDI.
122 @param[in,out] tablespace tablespace object
123 @retval false success
124 @retval true failure */
125 bool dict_sdi_create(dd::Tablespace *tablespace) {
126 DBUG_EXECUTE_IF("ib_sdi", ib::info(ER_IB_MSG_213)
127 << "SDI_CREATE: dict_sdi_create("
128 << tablespace->name() << "," << tablespace->id()
129 << ")";);
130
131 uint32_t space_id = 0;
132 if (tablespace->se_private_data().get("id", &space_id)) {
133 /* error, attribute not found */
134 ut_d(ut_error);
135 ut_o(return (true));
136 }
137
138 if (fsp_is_undo_tablespace(space_id) || fsp_is_system_temporary(space_id)) {
139 /* Upgrade calls sdi_create on dd::Tablespaces
140 registered. We shouldn't create SDI for
141 undo and temporary tablespace. */
142 return (false);
143 }
144
145 dberr_t err = ib_sdi_create(space_id);
146
147 /* If SDI index is created, update the tablespace flags in
148 dictionary */
149 if (err == DB_SUCCESS) {
150 fil_space_t *space = fil_space_acquire(space_id);
151 ut_ad(space != nullptr);
152
153 dd::Properties &p = tablespace->se_private_data();
154 p.set(dd_space_key_strings[DD_SPACE_FLAGS],
155 static_cast<uint32>(space->flags));
156
157 fil_space_release(space);
158 }
159
160 return (err != DB_SUCCESS);
161 }
162
163 /** Drop SDI in a tablespace. This API should be used only
164 when SDI is corrupted.
165 @param[in,out] tablespace tablespace object
166 @retval false success
167 @retval true failure */
168 bool dict_sdi_drop(dd::Tablespace *) {
169 #if 0 /* TODO: Enable in WL#9761 */
170 uint32 space_id;
171 if (dict_sdi_exists(tablespace, &space_id)
172 != DB_SUCCESS) {
173 return(true);
174 }
175
176 dberr_t err = ib_sdi_drop(space_id);
177 return(err != DB_SUCCESS);
178 #endif /* TODO: Enable in WL#9761 */
179 ut_d(ut_error);
180 ut_o(return (false));
181 }
182
183 /** Get the SDI keys in a tablespace into the vector provided.
184 @param[in] tablespace tablespace object
185 @param[in,out] vector vector to hold SDI keys
186 @retval false success
187 @retval true failure */
188 bool dict_sdi_get_keys(const dd::Tablespace &tablespace, sdi_vector_t &vector) {
189 uint32_t space_id;
190
191 if (dd_tablespace_is_discarded(&tablespace)) {
192 /* sdi_get_keys shouldn't be called on discarded tablespaces.*/
193 my_error(ER_SDI_GET_KEYS_INVALID_TABLESPACE, MYF(0),
194 tablespace.name().c_str());
195 ut_d(ut_error);
196 ut_o(return true);
197 }
198
199 if (dict_sdi_exists(tablespace, &space_id) != DB_SUCCESS) {
200 my_error(ER_SDI_GET_KEYS_INVALID_TABLESPACE, MYF(0),
201 tablespace.name().c_str());
202 return true;
203 }
204
205 if (fsp_is_undo_tablespace(space_id) || fsp_is_system_temporary(space_id)) {
206 /* There shouldn't be access for SDI on these tablespaces as
207 SDI doesn't exist. */
208
209 my_error(ER_SDI_GET_KEYS_INVALID_TABLESPACE, MYF(0),
210 tablespace.name().c_str());
211 ut_d(ut_error);
212 ut_o(return true);
213 }
214
215 ib_sdi_vector ib_vector;
216 ib_vector.sdi_vector = &vector;
217
218 trx_t *trx = check_trx_exists(current_thd);
219 trx_start_if_not_started(trx, true, UT_LOCATION_HERE);
220
221 dberr_t err = ib_sdi_get_keys(space_id, &ib_vector, trx);
222
223 return (err != DB_SUCCESS);
224 }
225
226 bool dict_sdi_get(const dd::Tablespace &, const sdi_key_t *, void *,
227 uint64_t *) {
228 #if 0 /* TODO: Enable in WL#9761 */
229 DBUG_EXECUTE_IF("ib_sdi",
230 ib::info(ER_IB_MSG_214) << "dict_sdi_get(" << tablespace.name()
231 << "," << tablespace.id()
232 << " sdi_key: type: " << sdi_key->type
233 << " id: " << sdi_key->id
234 << ")";
235 );
236
237 if (dd_tablespace_is_discarded(&tablespace)) {
238 /* sdi_get shouldn't be called on discarded tablespaces.*/
239 ut_d(ut_error);
240 }
241
242 uint32 space_id;
243 if (dict_sdi_exists(tablespace, &space_id)
244 != DB_SUCCESS) {
245 return(true);
246 }
247
248 #ifdef UNIV_DEBUG
249 if (fsp_is_undo_tablespace(space_id)
250 || fsp_is_system_temporary(space_id)) {
251 /* There shouldn't be access for SDI on these tablespaces as
252 SDI doesn't exist. */
253 ut_d(ut_error);
254 }
255 #endif /* UNIV_DEBUG */
256
257 trx_t* trx = check_trx_exists(current_thd);
258 trx_start_if_not_started(trx, true, UT_LOCATION_HERE);
259
260 ib_sdi_key_t ib_sdi_key;
261 ib_sdi_key.sdi_key = sdi_key;
262
263 ut_ad(*sdi_len < UINT32_MAX);
264 uint32_t uncompressed_sdi_len;
265 uint32_t compressed_sdi_len = static_cast<uint32_t>(*sdi_len);
266 byte* compressed_sdi = static_cast<byte*>(
267 ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, compressed_sdi_len));
268
269 dberr_t err = ib_sdi_get(
270 space_id, &ib_sdi_key,
271 compressed_sdi, &compressed_sdi_len,
272 &uncompressed_sdi_len,
273 trx);
274
275 if (err == DB_OUT_OF_MEMORY) {
276 *sdi_len = uncompressed_sdi_len;
277 } else if (err != DB_SUCCESS) {
278 *sdi_len = UINT64_MAX;
279 } else {
280 *sdi_len = uncompressed_sdi_len;
281 /* Decompress the data */
282 Sdi_Decompressor decompressor(static_cast<byte*>(sdi),
283 uncompressed_sdi_len,
284 compressed_sdi,
285 compressed_sdi_len);
286 decompressor.decompress();
287 }
288
289 ut::free(compressed_sdi);
290
291 return(err != DB_SUCCESS);
292 #endif /* TODO: Enable in WL#9761 */
293 ut_d(ut_error);
294 ut_o(return (false));
295 }
296
297 /** Insert/Update SDI in tablespace
298 @param[in] hton handlerton object
299 @param[in] tablespace tablespace object
300 @param[in] table table object
301 @param[in] sdi_key SDI key to uniquely identify the tablespace
302 object
303 @param[in] sdi SDI to be stored in tablespace
304 @param[in] sdi_len SDI length
305 @retval false success
306 @retval true failure */
307 bool dict_sdi_set(handlerton *hton, const dd::Tablespace &tablespace,
308 const dd::Table *table, const sdi_key_t *sdi_key,
309 const void *sdi, uint64_t sdi_len) {
310 const char *operation = "set";
311
312 DBUG_EXECUTE_IF("ib_sdi", ib::info(ER_IB_MSG_215)
313 << "dict_sdi_set(" << tablespace.name() << ","
314 << tablespace.id()
315 << " sdi_key: type: " << sdi_key->type
316 << " id: " << sdi_key->id << ")";);
317
318 /* Used for testing purpose for DDLs from Memcached */
319 DBUG_EXECUTE_IF("skip_sdi", return (false););
320
321 if (dd_tablespace_is_discarded(&tablespace)) {
322 /* Claim success. */
323 return (false);
324 }
325
326 /* Check if dd:Table has valid se_private_id. In case of partitions,
327 all partitions should have valid se_private_id. If not, we cannot
328 proceed with storing SDI as the tablespace is not created yet. */
329 if (table && (table->se_private_id() == dd::INVALID_OBJECT_ID) &&
330 std::all_of(table->leaf_partitions().begin(),
331 table->leaf_partitions().end(), [](const dd::Partition *lp) {
332 return (lp->se_private_id() == dd::INVALID_OBJECT_ID);
333 })) {
334 /* This is a preliminary store of the object - before SE has
335 added SE-specific data. Cannot, and should not, store sdi at
336 this point. We should not throw error here. There will be SDI
337 store again with valid se_private_id/data */
338 DBUG_EXECUTE_IF(
339 "ib_sdi", ib::info(ER_IB_MSG_216)
340 << "dict_sdi_set(" << tablespace.name() << ","
341 << tablespace.id() << " sdi_key: type: " << sdi_key->type
342 << " id: " << sdi_key->id << "): invalid se_private_id";);
343
344 return (false);
345 }
346
347 if (!tablespace.se_private_data().exists(dd_space_key_strings[DD_SPACE_ID])) {
348 /* Claim success, there will be one more sdi_set()
349 after the tablespace is created. */
350 return (false);
351 }
352
353 uint32_t space_id;
354 dberr_t err = dict_sdi_exists(tablespace, &space_id);
355 if (err != DB_SUCCESS) {
356 if (err == DB_TABLESPACE_NOT_FOUND) {
357 /* Claim Success */
358 return (false);
359 } else {
360 dict_sdi_report_error(operation, table, tablespace);
361 ut_d(ut_error);
362 ut_o(return (true));
363 }
364 }
365
366 if (fsp_is_undo_tablespace(space_id) || fsp_is_system_temporary(space_id)) {
367 /* Claim Success */
368 return (false);
369 }
370
371 trx_t *trx = check_trx_exists(current_thd);
372 trx_start_if_not_started(trx, true, UT_LOCATION_HERE);
373
374 innobase_register_trx(hton, current_thd, trx);
375
376 ib_sdi_key_t ib_sdi_key;
377 ib_sdi_key.sdi_key = sdi_key;
378
379 Sdi_Compressor compressor(static_cast<uint32_t>(sdi_len), sdi);
380 compressor.compress();
381
382 err = ib_sdi_set(space_id, &ib_sdi_key, static_cast<uint32_t>(sdi_len),
383 compressor.get_comp_len(), compressor.get_data(), trx);
384
385 DBUG_EXECUTE_IF("sdi_set_failure",
386 dict_sdi_report_error(operation, table, tablespace);
387 return (true););
388
389 if (err == DB_INTERRUPTED) {
390 my_error(ER_QUERY_INTERRUPTED, MYF(0));
391 DBUG_EXECUTE_IF("ib_sdi",
392 ib::info(ER_IB_MSG_217)
393 << "dict_sdi_set: " << tablespace.name() << ","
394 << tablespace.id() << " InnoDB space_id: " << space_id
395 << " sdi_key: type: " << sdi_key->type
396 << " id: " << sdi_key->id << " trx_id: " << trx->id
397 << " is interrupted";);
398 return (true);
399 } else if (err != DB_SUCCESS) {
400 dict_sdi_report_error(operation, table, tablespace);
401 ut_d(ut_error);
402 ut_o(return (true));
403 } else {
404 return (false);
405 }
406 }
407
408 /** Delete SDI from tablespace
409 @param[in] tablespace tablespace object
410 @param[in] table table object
411 @param[in] sdi_key SDI key to uniquely identify the tablespace
412 object
413 @retval false success
414 @retval true failure */
415 bool dict_sdi_delete(const dd::Tablespace &tablespace, const dd::Table *table,
416 const sdi_key_t *sdi_key) {
417 const char *operation = "delete";
418
419 DBUG_EXECUTE_IF("ib_sdi", ib::info(ER_IB_MSG_218)
420 << "dict_sdi_delete(" << tablespace.name()
421 << "," << tablespace.id()
422 << " sdi_key: type: " << sdi_key->type
423 << " id: " << sdi_key->id << ")";);
424
425 /* Used for testing purpose for DDLs from Memcached */
426 DBUG_EXECUTE_IF("skip_sdi", return (false););
427
428 if (dd_tablespace_is_discarded(&tablespace)) {
429 /* Claim success. */
430 return (false);
431 }
432
433 /* Check if dd:Table has valid se_private_id. In case of partitions,
434 all partitions should have valid se_private_id. If not, we cannot
435 proceed with storing SDI as the tablespace is not created yet. */
436 if (table && (table->se_private_id() == dd::INVALID_OBJECT_ID) &&
437 std::all_of(table->leaf_partitions().begin(),
438 table->leaf_partitions().end(), [](const dd::Partition *p) {
439 return (p->se_private_id() == dd::INVALID_OBJECT_ID);
440 })) {
441 /* This is a preliminary store of the object - before SE has
442 added SE-specific data. Cannot, and should not, store sdi at
443 this point. We should not throw error here. There will be SDI
444 store again with valid se_private_id/data */
445
446 DBUG_EXECUTE_IF(
447 "ib_sdi", ib::info(ER_IB_MSG_219)
448 << "dict_sdi_delete(" << tablespace.name() << ","
449 << tablespace.id() << " sdi_key: type: " << sdi_key->type
450 << " id: " << sdi_key->id << "): invalid se_private_id";);
451 return (false);
452 }
453
454 uint32_t space_id;
455 dberr_t err = dict_sdi_exists(tablespace, &space_id);
456 if (err != DB_SUCCESS) {
457 if (err == DB_TABLESPACE_NOT_FOUND) {
458 /* Claim Success */
459 return (false);
460 } else {
461 dict_sdi_report_error(operation, table, tablespace);
462 ut_d(ut_error);
463 ut_o(return (true));
464 }
465 }
466
467 if (fsp_is_undo_tablespace(space_id) || fsp_is_system_temporary(space_id)) {
468 /* Claim Success */
469 return (false);
470 }
471
472 trx_t *trx = check_trx_exists(current_thd);
473 trx_start_if_not_started(trx, true, UT_LOCATION_HERE);
474
475 ib_sdi_key_t ib_sdi_key;
476 ib_sdi_key.sdi_key = sdi_key;
477
478 err = ib_sdi_delete(space_id, &ib_sdi_key, trx);
479
480 DBUG_EXECUTE_IF("sdi_delete_failure",
481 dict_sdi_report_error(operation, table, tablespace);
482 return (true););
483
484 if (err == DB_INTERRUPTED) {
485 my_error(ER_QUERY_INTERRUPTED, MYF(0));
486
487 DBUG_EXECUTE_IF("ib_sdi",
488 ib::info(ER_IB_MSG_220)
489 << "dict_sdi_delete(" << tablespace.name() << ","
490 << tablespace.id() << " InnoDB space_id: " << space_id
491 << " sdi_key: type: " << sdi_key->type
492 << " id: " << sdi_key->id << " trx_id: " << trx->id
493 << " is interrupted";);
494 return (true);
495 } else if (err != DB_SUCCESS) {
496 if (err == DB_RECORD_NOT_FOUND)
497 dict_sdi_report_error(ER_SDI_OPERATION_FAILED_MISSING_RECORD, operation,
498 table, tablespace);
499 else
500 dict_sdi_report_error(operation, table, tablespace);
501 return (true);
502 } else {
503 return (false);
504 }
505 }