| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2012, 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 row/row0import.cc |
| 29 |
Import a tablespace to a running instance. |
| 30 |
|
| 31 |
Created 2012-02-08 by Sunny Bains. |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#include <errno.h> |
| 35 |
#include <my_aes.h> |
| 36 |
#include <sys/types.h> |
| 37 |
#include <memory> |
| 38 |
#include <vector> |
| 39 |
|
| 40 |
#include "sql/dd/types/column_type_element.h" |
| 41 |
|
| 42 |
#include "btr0pcur.h" |
| 43 |
#include "dict0boot.h" |
| 44 |
#include "dict0crea.h" |
| 45 |
#include "dict0dd.h" |
| 46 |
#include "dict0upgrade.h" |
| 47 |
#include "ha_prototypes.h" |
| 48 |
#include "ibuf0ibuf.h" |
| 49 |
#include "lob0first.h" |
| 50 |
#include "lob0impl.h" |
| 51 |
#include "lob0lob.h" |
| 52 |
#include "lob0pages.h" |
| 53 |
#include "log0chkp.h" |
| 54 |
#include "pars0pars.h" |
| 55 |
#include "que0que.h" |
| 56 |
#include "row0import.h" |
| 57 |
#include "row0mysql.h" |
| 58 |
#include "row0quiesce.h" |
| 59 |
#include "row0sel.h" |
| 60 |
#include "row0upd.h" |
| 61 |
#include "sql/mysqld.h" |
| 62 |
#include "srv0start.h" |
| 63 |
#include "ut0new.h" |
| 64 |
#include "zlob0first.h" |
| 65 |
|
| 66 |
#include <vector> |
| 67 |
|
| 68 |
#include "my_aes.h" |
| 69 |
#include "my_dbug.h" |
| 70 |
|
| 71 |
/** The size of the buffer to use for IO. Note: os_file_read() doesn't expect |
| 72 |
reads to fail. If you set the buffer size to be greater than a multiple of the |
| 73 |
file size then it will assert. TODO: Fix this limitation of the IO functions. |
| 74 |
@param m page size of the tablespace. |
| 75 |
@param n page size of the tablespace. |
| 76 |
@retval number of pages */ |
| 77 |
inline size_t IO_BUFFER_SIZE(size_t m, size_t n) { return m / n; } |
| 78 |
|
| 79 |
/** For gathering stats on records during phase I */ |
| 80 |
struct row_stats_t { |
| 81 |
ulint m_n_deleted; /*!< Number of deleted records |
| 82 |
found in the index */ |
| 83 |
|
| 84 |
ulint m_n_purged; /*!< Number of records purged |
| 85 |
optimistically */ |
| 86 |
|
| 87 |
ulint m_n_rows; /*!< Number of rows */ |
| 88 |
|
| 89 |
ulint m_n_purge_failed; /*!< Number of deleted rows |
| 90 |
that could not be purged */ |
| 91 |
}; |
| 92 |
|
| 93 |
/** Index information required by IMPORT. */ |
| 94 |
struct row_index_t { |
| 95 |
space_index_t m_id; /*!< Index id of the table |
| 96 |
in the exporting server */ |
| 97 |
byte *m_name; /*!< Index name */ |
| 98 |
|
| 99 |
space_id_t m_space; /*!< Space where it is placed */ |
| 100 |
|
| 101 |
page_no_t m_page_no; /*!< Root page number */ |
| 102 |
|
| 103 |
ulint m_type; /*!< Index type */ |
| 104 |
|
| 105 |
ulint m_trx_id_offset; /*!< Relevant only for clustered |
| 106 |
indexes, offset of transaction |
| 107 |
id system column */ |
| 108 |
|
| 109 |
ulint m_n_user_defined_cols; /*!< User defined columns */ |
| 110 |
|
| 111 |
ulint m_n_uniq; /*!< Number of columns that can |
| 112 |
uniquely identify the row */ |
| 113 |
|
| 114 |
ulint m_n_nullable; /*!< Number of nullable |
| 115 |
columns */ |
| 116 |
|
| 117 |
ulint m_n_fields; /*!< Total number of fields */ |
| 118 |
|
| 119 |
dict_field_t *m_fields; /*!< Index fields */ |
| 120 |
|
| 121 |
const dict_index_t *m_srv_index; /*!< Index instance in the |
| 122 |
importing server */ |
| 123 |
|
| 124 |
row_stats_t m_stats; /*!< Statistics gathered during |
| 125 |
the import phase */ |
| 126 |
}; |
| 127 |
|
| 128 |
/** Meta data required by IMPORT. */ |
| 129 |
struct row_import { |
| 130 |
public: |
| 131 |
row_import() UNIV_NOTHROW : m_table(), |
| 132 |
m_version(), |
| 133 |
m_hostname(), |
| 134 |
m_table_name(), |
| 135 |
m_heap(nullptr), |
| 136 |
m_autoinc(), |
| 137 |
m_page_size(0, 0, false), |
| 138 |
m_flags(), |
| 139 |
m_n_cols(), |
| 140 |
m_n_instant_cols(0), |
| 141 |
m_n_instant_nullable(0), |
| 142 |
m_cols(nullptr), |
| 143 |
m_col_names(nullptr), |
| 144 |
m_n_indexes(), |
| 145 |
m_indexes(nullptr), |
| 146 |
m_missing(true), |
| 147 |
m_has_sdi(false), |
| 148 |
m_cfp_missing(true) {} |
| 149 |
|
| 150 |
~row_import() UNIV_NOTHROW; |
| 151 |
|
| 152 |
/** Find the index entry in in the indexes array. |
| 153 |
@param name index name |
| 154 |
@return instance if found else 0. */ |
| 155 |
row_index_t *get_index(const char *name) const UNIV_NOTHROW; |
| 156 |
|
| 157 |
/** Get the number of rows in the index. |
| 158 |
@param name index name |
| 159 |
@return number of rows (doesn't include delete marked rows). */ |
| 160 |
ulint get_n_rows(const char *name) const UNIV_NOTHROW; |
| 161 |
|
| 162 |
/** Find the ordinal value of the column name in the cfg table columns. |
| 163 |
@param name of column to look for. |
| 164 |
@return ULINT_UNDEFINED if not found. */ |
| 165 |
ulint find_col(const char *name) const UNIV_NOTHROW; |
| 166 |
|
| 167 |
/** Get the number of rows for which purge failed during the |
| 168 |
convert phase. |
| 169 |
@param name index name |
| 170 |
@return number of rows for which purge failed. */ |
| 171 |
ulint get_n_purge_failed(const char *name) const UNIV_NOTHROW; |
| 172 |
|
| 173 |
/** Check if the index is clean. ie. no delete-marked records |
| 174 |
@param name index name |
| 175 |
@return true if index needs to be purged. */ |
| 176 |
bool requires_purge(const char *name) const UNIV_NOTHROW { |
| 177 |
return (get_n_purge_failed(name) > 0); |
| 178 |
} |
| 179 |
|
| 180 |
/** Set the index root <space, pageno> using the index name */ |
| 181 |
void set_root_by_name() UNIV_NOTHROW; |
| 182 |
|
| 183 |
/** Set the index root <space, pageno> using a heuristic |
| 184 |
@return DB_SUCCESS or error code */ |
| 185 |
dberr_t set_root_by_heuristic() UNIV_NOTHROW; |
| 186 |
|
| 187 |
/** Check if the index schema that was read from the .cfg file |
| 188 |
matches the in memory index definition. |
| 189 |
Note: It will update row_import_t::m_srv_index to map the meta-data |
| 190 |
read from the .cfg file to the server index instance. |
| 191 |
@return DB_SUCCESS or error code. */ |
| 192 |
dberr_t match_index_columns(THD *thd, const dict_index_t *index) UNIV_NOTHROW; |
| 193 |
|
| 194 |
/** Check if the column default values of table schema that was |
| 195 |
read from the .cfg file matches the in memory column definition. |
| 196 |
@param[in] thd MySQL session variable |
| 197 |
@param[in] dd_table dd::Table |
| 198 |
@return DB_SUCCESS or error code. */ |
| 199 |
dberr_t match_col_default_values(THD *thd, |
| 200 |
const dd::Table *dd_table) UNIV_NOTHROW; |
| 201 |
|
| 202 |
/** Check if the table schema that was read from the .cfg file matches the |
| 203 |
in memory table definition. |
| 204 |
@param[in] thd MySQL session variable |
| 205 |
@param[in] dd_table dd::Table |
| 206 |
@return DB_SUCCESS or error code. */ |
| 207 |
dberr_t match_compression_type_option(THD *thd, |
| 208 |
const dd::Table *dd_table) UNIV_NOTHROW; |
| 209 |
|
| 210 |
/** Check if the table schema that was read from the .cfg file |
| 211 |
matches the in memory table definition. |
| 212 |
@param thd MySQL session variable |
| 213 |
@return DB_SUCCESS or error code. */ |
| 214 |
dberr_t match_table_columns(THD *thd) UNIV_NOTHROW; |
| 215 |
|
| 216 |
/** Check if the table (and index) schema that was read from the |
| 217 |
.cfg file matches the in memory table definition. |
| 218 |
@param[in] thd MySQL session variable |
| 219 |
@param[in] dd_table dd::Table |
| 220 |
@return DB_SUCCESS or error code. */ |
| 221 |
dberr_t match_schema(THD *thd, const dd::Table *dd_table) UNIV_NOTHROW; |
| 222 |
|
| 223 |
/** Check if table being imported has INSTANT ADD/DROP columns |
| 224 |
@return true if table has INSTANT ADD/DROP columns */ |
| 225 |
bool has_row_versions() { |
| 226 |
return (m_total_column_count > m_initial_column_count || |
| 227 |
m_total_column_count > m_current_column_count); |
| 228 |
} |
| 229 |
|
| 230 |
private: |
| 231 |
/** Set the instant ADD COLUMN information to the table */ |
| 232 |
dberr_t set_instant_info(THD *thd, const dd::Table *dd_table) UNIV_NOTHROW; |
| 233 |
|
| 234 |
/** Set the instant ADD/DROP COLUMN information to the table |
| 235 |
@param[in] thd MySQL session |
| 236 |
@param[in,out] dd_table target table definition |
| 237 |
@return DB_SUCCESS or error code. */ |
| 238 |
dberr_t set_instant_info_v2(THD *thd, const dd::Table *dd_table) UNIV_NOTHROW; |
| 239 |
|
| 240 |
/** Match INSTANT metadata of CFG file and target table when both source and |
| 241 |
target table has INSTANT columns. |
| 242 |
@param[in] thd MySQL session |
| 243 |
@return DB_SUCCESS or error code. */ |
| 244 |
dberr_t match_instant_metadata_in_target_table(THD *thd); |
| 245 |
|
| 246 |
/** Update INSTANT metadata into target table when only source has INSTANT |
| 247 |
columns. |
| 248 |
@param[in] thd MySQL session |
| 249 |
@param[in,out] dd_table target table definition |
| 250 |
@return DB_SUCCESS or error code. */ |
| 251 |
dberr_t adjust_instant_metadata_in_taregt_table(THD *thd, |
| 252 |
const dd::Table *dd_table); |
| 253 |
|
| 254 |
/** Add INSTANT DROP columns to target table innodb cache. |
| 255 |
@param[in] target_table target table in InnoDB cache. |
| 256 |
@return DB_SUCCESS or error code. */ |
| 257 |
dberr_t add_instant_dropped_columns(dict_table_t *target_table); |
| 258 |
|
| 259 |
public: |
| 260 |
dict_table_t *m_table; /*!< Table instance */ |
| 261 |
|
| 262 |
uint32_t m_version; /*!< Version of config file */ |
| 263 |
|
| 264 |
byte *m_hostname; /*!< Hostname where the |
| 265 |
tablespace was exported */ |
| 266 |
byte *m_table_name; /*!< Exporting instance table |
| 267 |
name */ |
| 268 |
|
| 269 |
mem_heap_t *m_heap; /*!< Memory heap for default |
| 270 |
value of instant columns */ |
| 271 |
|
| 272 |
uint64_t m_autoinc; /*!< Next autoinc value */ |
| 273 |
|
| 274 |
page_size_t m_page_size; /*!< Tablespace page size */ |
| 275 |
|
| 276 |
uint32_t m_flags; /*!< Table flags */ |
| 277 |
|
| 278 |
ulint m_n_cols; /*!< Number of columns in the |
| 279 |
meta-data file */ |
| 280 |
|
| 281 |
/* Column counts for table */ |
| 282 |
uint32_t m_initial_column_count{0}; |
| 283 |
uint32_t m_current_column_count{0}; |
| 284 |
uint32_t m_total_column_count{0}; |
| 285 |
uint32_t m_n_instant_drop_cols{0}; |
| 286 |
uint32_t m_current_row_version{0}; |
| 287 |
|
| 288 |
uint16_t m_n_instant_cols; /*!< Number of columns before |
| 289 |
first instant ADD COLUMN in |
| 290 |
the meta-data file */ |
| 291 |
|
| 292 |
uint32_t m_n_instant_nullable; |
| 293 |
|
| 294 |
dict_col_t *m_cols; /*!< Column data */ |
| 295 |
|
| 296 |
byte **m_col_names; /*!< Column names, we store the |
| 297 |
column names separately because |
| 298 |
there is no field to store the |
| 299 |
value in dict_col_t */ |
| 300 |
|
| 301 |
ulint m_n_indexes; /*!< Number of indexes, |
| 302 |
including clustered index */ |
| 303 |
|
| 304 |
row_index_t *m_indexes; /*!< Index meta data */ |
| 305 |
|
| 306 |
bool m_missing; /*!< true if a .cfg file was |
| 307 |
found and was readable */ |
| 308 |
bool m_has_sdi; /*!< true if tablespace has |
| 309 |
SDI */ |
| 310 |
|
| 311 |
bool m_cfp_missing; /*!< true if a .cfp file was |
| 312 |
found and was readable */ |
| 313 |
|
| 314 |
/** Compression type in the meta-data file */ |
| 315 |
Compression::Type m_compression_type{}; |
| 316 |
|
| 317 |
/** Encryption settings */ |
| 318 |
Encryption_metadata m_encryption_metadata{}; |
| 319 |
}; |
| 320 |
|
| 321 |
/** Use the page cursor to iterate over records in a block. */ |
| 322 |
class RecIterator { |
| 323 |
public: |
| 324 |
/** Default constructor */ |
| 325 |
RecIterator() UNIV_NOTHROW = default; |
| 326 |
|
| 327 |
/** Position the cursor on the first user record. */ |
| 328 |
void open(buf_block_t *block) UNIV_NOTHROW { |
| 329 |
page_cur_set_before_first(block, &m_cur); |
| 330 |
|
| 331 |
if (!end()) { |
| 332 |
next(); |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
/** Move to the next record. */ |
| 337 |
void next() UNIV_NOTHROW { page_cur_move_to_next(&m_cur); } |
| 338 |
|
| 339 |
/** |
| 340 |
@return the current record */ |
| 341 |
rec_t *current() UNIV_NOTHROW { |
| 342 |
ut_ad(!end()); |
| 343 |
return (page_cur_get_rec(&m_cur)); |
| 344 |
} |
| 345 |
|
| 346 |
/** |
| 347 |
@return true if cursor is at the end */ |
| 348 |
bool end() UNIV_NOTHROW { return (page_cur_is_after_last(&m_cur) == true); } |
| 349 |
|
| 350 |
/** Remove the current record |
| 351 |
@return true on success */ |
| 352 |
bool remove(const dict_index_t *index, ulint *offsets) UNIV_NOTHROW { |
| 353 |
/* We can't end up with an empty page unless it is root. */ |
| 354 |
if (page_get_n_recs(m_cur.block->frame) <= 1) { |
| 355 |
return (false); |
| 356 |
} |
| 357 |
|
| 358 |
return (page_delete_rec(index, &m_cur, offsets)); |
| 359 |
} |
| 360 |
|
| 361 |
private: |
| 362 |
page_cur_t m_cur; |
| 363 |
}; |
| 364 |
|
| 365 |
/** Class that purges delete marked records from indexes, both secondary |
| 366 |
and cluster. It does a pessimistic delete. This should only be done if we |
| 367 |
couldn't purge the delete marked records during Phase I. */ |
| 368 |
class IndexPurge { |
| 369 |
public: |
| 370 |
/** Constructor |
| 371 |
@param trx the user transaction covering the import tablespace |
| 372 |
@param index to be imported. */ |
| 373 |
IndexPurge(trx_t *trx, dict_index_t *index) UNIV_NOTHROW : m_trx(trx), |
| 374 |
m_index(index), |
| 375 |
m_n_rows(0) { |
| 376 |
ib::info(ER_IB_MSG_934) |
| 377 |
<< "Phase II - Purge records from index " << index->name; |
| 378 |
} |
| 379 |
|
| 380 |
/** Destructor */ |
| 381 |
~IndexPurge() UNIV_NOTHROW = default; |
| 382 |
|
| 383 |
/** Purge delete marked records. |
| 384 |
@return DB_SUCCESS or error code. */ |
| 385 |
dberr_t garbage_collect() UNIV_NOTHROW; |
| 386 |
|
| 387 |
/** The number of records that are not delete marked. |
| 388 |
@return total records in the index after purge */ |
| 389 |
ulint get_n_rows() const UNIV_NOTHROW { return (m_n_rows); } |
| 390 |
|
| 391 |
private: |
| 392 |
/** Begin import, position the cursor on the first record. */ |
| 393 |
void open() UNIV_NOTHROW; |
| 394 |
|
| 395 |
/** Close the persistent curosr and commit the mini-transaction. */ |
| 396 |
void close() UNIV_NOTHROW; |
| 397 |
|
| 398 |
/** Position the cursor on the next record. |
| 399 |
@return DB_SUCCESS or error code */ |
| 400 |
dberr_t next() UNIV_NOTHROW; |
| 401 |
|
| 402 |
/** Store the persistent cursor position and reopen the |
| 403 |
B-tree cursor in BTR_MODIFY_TREE mode, because the |
| 404 |
tree structure may be changed during a pessimistic delete. */ |
| 405 |
void purge_pessimistic_delete() UNIV_NOTHROW; |
| 406 |
|
| 407 |
/** Purge delete-marked records. */ |
| 408 |
void purge() UNIV_NOTHROW; |
| 409 |
|
| 410 |
protected: |
| 411 |
// Disable copying |
| 412 |
IndexPurge(); |
| 413 |
IndexPurge(const IndexPurge &); |
| 414 |
IndexPurge &operator=(const IndexPurge &); |
| 415 |
|
| 416 |
private: |
| 417 |
trx_t *m_trx; /*!< User transaction */ |
| 418 |
mtr_t m_mtr; /*!< Mini-transaction */ |
| 419 |
btr_pcur_t m_pcur; /*!< Persistent cursor */ |
| 420 |
dict_index_t *m_index; /*!< Index to be processed */ |
| 421 |
ulint m_n_rows; /*!< Records in index */ |
| 422 |
}; |
| 423 |
|
| 424 |
/** Functor that is called for each physical page that is read from the |
| 425 |
tablespace file. */ |
| 426 |
class AbstractCallback : public PageCallback { |
| 427 |
public: |
| 428 |
/** Constructor |
| 429 |
@param trx covering transaction */ |
| 430 |
AbstractCallback(trx_t *trx) |
| 431 |
: m_trx(trx), |
| 432 |
m_space(SPACE_UNKNOWN), |
| 433 |
m_xdes(), |
| 434 |
m_xdes_page_no(FIL_NULL), |
| 435 |
m_space_flags(UINT32_UNDEFINED), |
| 436 |
m_table_flags(UINT32_UNDEFINED) UNIV_NOTHROW {} |
| 437 |
|
| 438 |
/** Free any extent descriptor instance */ |
| 439 |
~AbstractCallback() override { ut::delete_arr(m_xdes); } |
| 440 |
|
| 441 |
/** Determine the page size to use for traversing the tablespace |
| 442 |
@param file_size size of the tablespace file in bytes |
| 443 |
@param block contents of the first page in the tablespace file. |
| 444 |
@retval DB_SUCCESS or error code. */ |
| 445 |
dberr_t init(os_offset_t file_size, |
| 446 |
const buf_block_t *block) override UNIV_NOTHROW; |
| 447 |
|
| 448 |
/** @return true if compressed table. */ |
| 449 |
bool is_compressed_table() const UNIV_NOTHROW { |
| 450 |
return (get_page_size().is_compressed()); |
| 451 |
} |
| 452 |
|
| 453 |
protected: |
| 454 |
/** Get the data page depending on the table type, compressed or not. |
| 455 |
@param block block read from disk |
| 456 |
@retval the buffer frame */ |
| 457 |
buf_frame_t *get_frame(buf_block_t *block) const UNIV_NOTHROW { |
| 458 |
if (is_compressed_table()) { |
| 459 |
return (block->page.zip.data); |
| 460 |
} |
| 461 |
|
| 462 |
return (buf_block_get_frame(block)); |
| 463 |
} |
| 464 |
|
| 465 |
/** Check for session interrupt. If required we could |
| 466 |
even flush to disk here every N pages. |
| 467 |
@retval DB_SUCCESS or error code */ |
| 468 |
dberr_t periodic_check() UNIV_NOTHROW { |
| 469 |
if (trx_is_interrupted(m_trx)) { |
| 470 |
return (DB_INTERRUPTED); |
| 471 |
} |
| 472 |
|
| 473 |
return (DB_SUCCESS); |
| 474 |
} |
| 475 |
|
| 476 |
/** Get the physical offset of the extent descriptor within the page. |
| 477 |
@param page_no page number of the extent descriptor |
| 478 |
@param page contents of the page containing the extent descriptor. |
| 479 |
@return the start of the xdes array in a page */ |
| 480 |
const xdes_t *xdes(ulint page_no, const page_t *page) const UNIV_NOTHROW { |
| 481 |
ulint offset; |
| 482 |
|
| 483 |
offset = xdes_calc_descriptor_index(get_page_size(), page_no); |
| 484 |
|
| 485 |
return (page + XDES_ARR_OFFSET + XDES_SIZE * offset); |
| 486 |
} |
| 487 |
|
| 488 |
/** Set the current page directory (xdes). If the extent descriptor is |
| 489 |
marked as free then free the current extent descriptor and set it to |
| 490 |
0. This implies that all pages that are covered by this extent |
| 491 |
descriptor are also freed. |
| 492 |
|
| 493 |
@param page_no offset of page within the file |
| 494 |
@param page page contents |
| 495 |
@return DB_SUCCESS or error code. */ |
| 496 |
dberr_t set_current_xdes(page_no_t page_no, const page_t *page) UNIV_NOTHROW { |
| 497 |
m_xdes_page_no = page_no; |
| 498 |
|
| 499 |
ut::delete_arr(m_xdes); |
| 500 |
m_xdes = nullptr; |
| 501 |
|
| 502 |
ulint state; |
| 503 |
const xdes_t *xdesc = page + XDES_ARR_OFFSET; |
| 504 |
|
| 505 |
state = mach_read_ulint(xdesc + XDES_STATE, MLOG_4BYTES); |
| 506 |
|
| 507 |
if (state != XDES_FREE) { |
| 508 |
m_xdes = ut::new_arr_withkey<xdes_t>(UT_NEW_THIS_FILE_PSI_KEY, |
| 509 |
ut::Count{m_page_size.physical()}); |
| 510 |
|
| 511 |
/* Trigger OOM */ |
| 512 |
DBUG_EXECUTE_IF("ib_import_OOM_13", ut::delete_arr(m_xdes); |
| 513 |
m_xdes = nullptr;); |
| 514 |
|
| 515 |
if (m_xdes == nullptr) { |
| 516 |
return (DB_OUT_OF_MEMORY); |
| 517 |
} |
| 518 |
|
| 519 |
memcpy(m_xdes, page, m_page_size.physical()); |
| 520 |
} |
| 521 |
|
| 522 |
return (DB_SUCCESS); |
| 523 |
} |
| 524 |
|
| 525 |
/** |
| 526 |
@return true if it is a root page */ |
| 527 |
bool is_root_page(const page_t *page) const UNIV_NOTHROW { |
| 528 |
ut_ad(fil_page_index_page_check(page)); |
| 529 |
|
| 530 |
return (mach_read_from_4(page + FIL_PAGE_NEXT) == FIL_NULL && |
| 531 |
mach_read_from_4(page + FIL_PAGE_PREV) == FIL_NULL); |
| 532 |
} |
| 533 |
|
| 534 |
/** Check if the page is marked as free in the extent descriptor. |
| 535 |
@param page_no page number to check in the extent descriptor. |
| 536 |
@return true if the page is marked as free */ |
| 537 |
bool is_free(page_no_t page_no) const UNIV_NOTHROW { |
| 538 |
ut_a(xdes_calc_descriptor_page(get_page_size(), page_no) == m_xdes_page_no); |
| 539 |
|
| 540 |
if (m_xdes != nullptr) { |
| 541 |
const xdes_t *xdesc = xdes(page_no, m_xdes); |
| 542 |
page_no_t pos = page_no % FSP_EXTENT_SIZE; |
| 543 |
|
| 544 |
return (xdes_get_bit(xdesc, XDES_FREE_BIT, pos)); |
| 545 |
} |
| 546 |
|
| 547 |
/* If the current xdes was free, the page must be free. */ |
| 548 |
return (true); |
| 549 |
} |
| 550 |
|
| 551 |
protected: |
| 552 |
/** Covering transaction. */ |
| 553 |
trx_t *m_trx; |
| 554 |
|
| 555 |
/** Space id of the file being iterated over. */ |
| 556 |
space_id_t m_space; |
| 557 |
|
| 558 |
/** Minimum page number for which the free list has not been |
| 559 |
initialized: the pages >= this limit are, by definition, free; |
| 560 |
note that in a single-table tablespace where size < 64 pages, |
| 561 |
this number is 64, i.e., we have initialized the space about |
| 562 |
the first extent, but have not physically allocated those pages |
| 563 |
to the file. @see FSP_LIMIT. */ |
| 564 |
page_no_t m_free_limit; |
| 565 |
|
| 566 |
/** Current size of the space in pages */ |
| 567 |
page_no_t m_size; |
| 568 |
|
| 569 |
/** Current extent descriptor page */ |
| 570 |
xdes_t *m_xdes; |
| 571 |
|
| 572 |
/** Physical page offset in the file of the extent descriptor */ |
| 573 |
page_no_t m_xdes_page_no; |
| 574 |
|
| 575 |
/** Flags value read from the header page */ |
| 576 |
uint32_t m_space_flags; |
| 577 |
|
| 578 |
/** Derived from m_space_flags and row format type, the row format |
| 579 |
type is determined from the page header. */ |
| 580 |
uint32_t m_table_flags; |
| 581 |
}; |
| 582 |
|
| 583 |
/** Determine the page size to use for traversing the tablespace |
| 584 |
@param file_size size of the tablespace file in bytes |
| 585 |
@param block contents of the first page in the tablespace file. |
| 586 |
@retval DB_SUCCESS or error code. */ |
| 587 |
dberr_t AbstractCallback::init(os_offset_t file_size, |
| 588 |
const buf_block_t *block) UNIV_NOTHROW { |
| 589 |
const page_t *page = block->frame; |
| 590 |
|
| 591 |
m_space_flags = fsp_header_get_flags(page); |
| 592 |
|
| 593 |
/* Since we don't know whether it is a compressed table |
| 594 |
or not, the data is always read into the block->frame. */ |
| 595 |
|
| 596 |
set_page_size(block->frame); |
| 597 |
|
| 598 |
/* Set the page size used to traverse the tablespace. */ |
| 599 |
|
| 600 |
if (!is_compressed_table() && !m_page_size.equals_to(univ_page_size)) { |
| 601 |
ib::error(ER_IB_MSG_935) |
| 602 |
<< "Page size " << m_page_size.physical() |
| 603 |
<< " of ibd file is not the same as the server page" |
| 604 |
" size " |
| 605 |
<< univ_page_size.physical(); |
| 606 |
|
| 607 |
return (DB_CORRUPTION); |
| 608 |
|
| 609 |
} else if (file_size % m_page_size.physical() != 0) { |
| 610 |
ib::error(ER_IB_MSG_936) << "File size " << file_size |
| 611 |
<< " is not a" |
| 612 |
" multiple of the page size " |
| 613 |
<< m_page_size.physical(); |
| 614 |
|
| 615 |
return (DB_CORRUPTION); |
| 616 |
} |
| 617 |
|
| 618 |
ut_a(m_space == SPACE_UNKNOWN); |
| 619 |
|
| 620 |
m_size = mach_read_from_4(page + FSP_SIZE); |
| 621 |
m_free_limit = mach_read_from_4(page + FSP_FREE_LIMIT); |
| 622 |
m_space = fsp_header_get_field(page, FSP_SPACE_ID); |
| 623 |
dberr_t err = set_current_xdes(0, page); |
| 624 |
|
| 625 |
return (err); |
| 626 |
} |
| 627 |
|
| 628 |
/** |
| 629 |
Try and determine the index root pages by checking if the next/prev |
| 630 |
pointers are both FIL_NULL. We need to ensure that skip deleted pages. */ |
| 631 |
struct FetchIndexRootPages : public AbstractCallback { |
| 632 |
/** Index information gathered from the .ibd file. */ |
| 633 |
struct Index { |
| 634 |
Index(space_index_t id, page_no_t page_no) : m_id(id), m_page_no(page_no) {} |
| 635 |
|
| 636 |
space_index_t m_id; /*!< Index id */ |
| 637 |
page_no_t m_page_no; /*!< Root page number */ |
| 638 |
}; |
| 639 |
|
| 640 |
typedef std::vector<Index, ut::allocator<Index>> Indexes; |
| 641 |
|
| 642 |
/** Constructor |
| 643 |
@param trx covering (user) transaction |
| 644 |
@param table table definition in server .*/ |
| 645 |
FetchIndexRootPages(const dict_table_t *table, trx_t *trx) |
| 646 |
: AbstractCallback(trx), m_table(table) UNIV_NOTHROW {} |
| 647 |
|
| 648 |
/** Destructor */ |
| 649 |
~FetchIndexRootPages() UNIV_NOTHROW override = default; |
| 650 |
|
| 651 |
/** |
| 652 |
@retval the space id of the tablespace being iterated over */ |
| 653 |
space_id_t get_space_id() const UNIV_NOTHROW override { return (m_space); } |
| 654 |
|
| 655 |
/** |
| 656 |
@retval the space flags of the tablespace being iterated over */ |
| 657 |
ulint get_space_flags() const UNIV_NOTHROW override { |
| 658 |
return (m_space_flags); |
| 659 |
} |
| 660 |
|
| 661 |
/** Check if the .ibd file row format is the same as the table's. |
| 662 |
@param ibd_table_flags determined from space and page. |
| 663 |
@return DB_SUCCESS or error code. */ |
| 664 |
dberr_t check_row_format(uint32_t ibd_table_flags) UNIV_NOTHROW { |
| 665 |
dberr_t err; |
| 666 |
rec_format_t ibd_rec_format; |
| 667 |
rec_format_t table_rec_format; |
| 668 |
|
| 669 |
if (!dict_tf_is_valid(ibd_table_flags)) { |
| 670 |
ib_errf(m_trx->mysql_thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 671 |
".ibd file has invalid table flags: %x", ibd_table_flags); |
| 672 |
|
| 673 |
return (DB_CORRUPTION); |
| 674 |
} |
| 675 |
|
| 676 |
ibd_rec_format = dict_tf_get_rec_format(ibd_table_flags); |
| 677 |
table_rec_format = dict_tf_get_rec_format(m_table->flags); |
| 678 |
|
| 679 |
if (table_rec_format != ibd_rec_format) { |
| 680 |
ib_errf(m_trx->mysql_thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 681 |
"Table has %s row format, .ibd" |
| 682 |
" file has %s row format.", |
| 683 |
dict_tf_to_row_format_string(m_table->flags), |
| 684 |
dict_tf_to_row_format_string(ibd_table_flags)); |
| 685 |
|
| 686 |
err = DB_CORRUPTION; |
| 687 |
} else { |
| 688 |
err = DB_SUCCESS; |
| 689 |
} |
| 690 |
|
| 691 |
return (err); |
| 692 |
} |
| 693 |
|
| 694 |
/** Called for each block as it is read from the file. Check index pages to |
| 695 |
determine the exact row format. We can't get that from the tablespace |
| 696 |
header flags alone. |
| 697 |
|
| 698 |
@param offset physical offset in the file |
| 699 |
@param block block to convert, it is not from the buffer pool. |
| 700 |
@retval DB_SUCCESS or error code. */ |
| 701 |
dberr_t operator()(os_offset_t offset, |
| 702 |
buf_block_t *block) override UNIV_NOTHROW; |
| 703 |
|
| 704 |
/** Update the import configuration that will be used to import |
| 705 |
the tablespace. */ |
| 706 |
dberr_t build_row_import(row_import *cfg) const UNIV_NOTHROW; |
| 707 |
|
| 708 |
/** Table definition in server. */ |
| 709 |
const dict_table_t *m_table; |
| 710 |
|
| 711 |
/** Index information */ |
| 712 |
Indexes m_indexes; |
| 713 |
}; |
| 714 |
|
| 715 |
/** Called for each block as it is read from the file. Check index pages to |
| 716 |
determine the exact row format. We can't get that from the tablespace |
| 717 |
header flags alone. |
| 718 |
|
| 719 |
@param offset physical offset in the file |
| 720 |
@param block block to convert, it is not from the buffer pool. |
| 721 |
@retval DB_SUCCESS or error code. */ |
| 722 |
dberr_t FetchIndexRootPages::operator()(os_offset_t offset, |
| 723 |
buf_block_t *block) UNIV_NOTHROW { |
| 724 |
dberr_t err; |
| 725 |
|
| 726 |
if ((err = periodic_check()) != DB_SUCCESS) { |
| 727 |
return (err); |
| 728 |
} |
| 729 |
|
| 730 |
const page_t *page = get_frame(block); |
| 731 |
|
| 732 |
ulint page_type = fil_page_get_type(page); |
| 733 |
|
| 734 |
if (block->page.id.page_no() * m_page_size.physical() != offset) { |
| 735 |
ib::error(ER_IB_MSG_937) |
| 736 |
<< "Page offset doesn't match file offset:" |
| 737 |
" page offset: " |
| 738 |
<< block->page.id.page_no() |
| 739 |
<< ", file offset: " << (offset / m_page_size.physical()); |
| 740 |
|
| 741 |
err = DB_CORRUPTION; |
| 742 |
} else if (page_type == FIL_PAGE_TYPE_XDES) { |
| 743 |
err = set_current_xdes(block->page.id.page_no(), page); |
| 744 |
} else if (fil_page_index_page_check(page) && |
| 745 |
!is_free(block->page.id.page_no()) && is_root_page(page)) { |
| 746 |
space_index_t id = btr_page_get_index_id(page); |
| 747 |
|
| 748 |
m_indexes.push_back(Index(id, block->page.id.page_no())); |
| 749 |
|
| 750 |
/* Since there are SDI Indexes before normal indexes, we |
| 751 |
check for FIL_PAGE_INDEX type. */ |
| 752 |
if (page_type == FIL_PAGE_INDEX) { |
| 753 |
m_table_flags = fsp_flags_to_dict_tf(m_space_flags, page_is_comp(page)); |
| 754 |
|
| 755 |
err = check_row_format(m_table_flags); |
| 756 |
} |
| 757 |
} |
| 758 |
|
| 759 |
return (err); |
| 760 |
} |
| 761 |
|
| 762 |
/** |
| 763 |
Update the import configuration that will be used to import the tablespace. |
| 764 |
@return error code or DB_SUCCESS */ |
| 765 |
dberr_t FetchIndexRootPages::build_row_import(row_import *cfg) const |
| 766 |
UNIV_NOTHROW { |
| 767 |
Indexes::const_iterator end = m_indexes.end(); |
| 768 |
|
| 769 |
ut_a(cfg->m_table == m_table); |
| 770 |
cfg->m_page_size.copy_from(m_page_size); |
| 771 |
cfg->m_n_indexes = m_indexes.size(); |
| 772 |
cfg->m_has_sdi = FSP_FLAGS_HAS_SDI(m_space_flags); |
| 773 |
cfg->m_flags = m_table->flags; |
| 774 |
|
| 775 |
if (cfg->m_n_indexes == 0) { |
| 776 |
ib::error(ER_IB_MSG_938) << "No B+Tree found in tablespace"; |
| 777 |
|
| 778 |
return (DB_CORRUPTION); |
| 779 |
} |
| 780 |
|
| 781 |
cfg->m_indexes = ut::new_arr_withkey<row_index_t>( |
| 782 |
UT_NEW_THIS_FILE_PSI_KEY, ut::Count{cfg->m_n_indexes}); |
| 783 |
|
| 784 |
/* Trigger OOM */ |
| 785 |
DBUG_EXECUTE_IF("ib_import_OOM_11", ut::delete_arr(cfg->m_indexes); |
| 786 |
cfg->m_indexes = nullptr;); |
| 787 |
|
| 788 |
if (cfg->m_indexes == nullptr) { |
| 789 |
return (DB_OUT_OF_MEMORY); |
| 790 |
} |
| 791 |
|
| 792 |
memset(cfg->m_indexes, 0x0, sizeof(*cfg->m_indexes) * cfg->m_n_indexes); |
| 793 |
|
| 794 |
row_index_t *cfg_index = cfg->m_indexes; |
| 795 |
|
| 796 |
for (Indexes::const_iterator it = m_indexes.begin(); it != end; |
| 797 |
++it, ++cfg_index) { |
| 798 |
char name[BUFSIZ]; |
| 799 |
|
| 800 |
snprintf(name, sizeof(name), "index" IB_ID_FMT, it->m_id); |
| 801 |
|
| 802 |
ulint len = strlen(name) + 1; |
| 803 |
|
| 804 |
cfg_index->m_name = |
| 805 |
ut::new_arr_withkey<byte>(UT_NEW_THIS_FILE_PSI_KEY, ut::Count{len}); |
| 806 |
|
| 807 |
/* Trigger OOM */ |
| 808 |
DBUG_EXECUTE_IF("ib_import_OOM_12", ut::delete_arr(cfg_index->m_name); |
| 809 |
cfg_index->m_name = nullptr;); |
| 810 |
|
| 811 |
if (cfg_index->m_name == nullptr) { |
| 812 |
return (DB_OUT_OF_MEMORY); |
| 813 |
} |
| 814 |
|
| 815 |
memcpy(cfg_index->m_name, name, len); |
| 816 |
|
| 817 |
cfg_index->m_id = it->m_id; |
| 818 |
|
| 819 |
cfg_index->m_space = m_space; |
| 820 |
|
| 821 |
cfg_index->m_page_no = it->m_page_no; |
| 822 |
} |
| 823 |
|
| 824 |
return (DB_SUCCESS); |
| 825 |
} |
| 826 |
|
| 827 |
/* Functor that is called for each physical page that is read from the |
| 828 |
tablespace file. |
| 829 |
|
| 830 |
1. Check each page for corruption. |
| 831 |
|
| 832 |
2. Update the space id and LSN on every page |
| 833 |
- For the header page |
| 834 |
- Validate the flags |
| 835 |
- Update the LSN |
| 836 |
|
| 837 |
3. On Btree pages |
| 838 |
- Set the index id |
| 839 |
- Update the max trx id |
| 840 |
- In a cluster index, update the system columns |
| 841 |
- In a cluster index, update the BLOB ptr, set the space id |
| 842 |
- Purge delete marked records, but only if they can be easily |
| 843 |
removed from the page |
| 844 |
- Keep a counter of number of rows, ie. non-delete-marked rows |
| 845 |
- Keep a counter of number of delete marked rows |
| 846 |
- Keep a counter of number of purge failure |
| 847 |
- If a page is stamped with an index id that isn't in the .cfg file |
| 848 |
we assume it is deleted and the page can be ignored. |
| 849 |
|
| 850 |
4. Set the page state to dirty so that it will be written to disk. |
| 851 |
*/ |
| 852 |
class PageConverter : public AbstractCallback { |
| 853 |
public: |
| 854 |
/** Constructor |
| 855 |
@param cfg config of table being imported. |
| 856 |
@param trx transaction covering the import */ |
| 857 |
PageConverter(row_import *cfg, trx_t *trx) UNIV_NOTHROW; |
| 858 |
|
| 859 |
~PageConverter() UNIV_NOTHROW override { |
| 860 |
if (m_heap != nullptr) { |
| 861 |
mem_heap_free(m_heap); |
| 862 |
} |
| 863 |
} |
| 864 |
|
| 865 |
/** |
| 866 |
@retval the server space id of the tablespace being iterated over */ |
| 867 |
space_id_t get_space_id() const UNIV_NOTHROW override { |
| 868 |
return (m_cfg->m_table->space); |
| 869 |
} |
| 870 |
|
| 871 |
/** |
| 872 |
@retval the space flags of the tablespace being iterated over */ |
| 873 |
ulint get_space_flags() const UNIV_NOTHROW override { |
| 874 |
return (m_space_flags); |
| 875 |
} |
| 876 |
|
| 877 |
/** Called for every page in the tablespace. If the page was not |
| 878 |
updated then its state must be set to BUF_PAGE_NOT_USED. |
| 879 |
@param offset physical offset within the file |
| 880 |
@param block block read from file, note it is not from the buffer pool |
| 881 |
@retval DB_SUCCESS or error code. */ |
| 882 |
dberr_t operator()(os_offset_t offset, |
| 883 |
buf_block_t *block) override UNIV_NOTHROW; |
| 884 |
|
| 885 |
private: |
| 886 |
/** Status returned by PageConverter::validate() */ |
| 887 |
enum import_page_status_t { |
| 888 |
IMPORT_PAGE_STATUS_OK, /*!< Page is OK */ |
| 889 |
IMPORT_PAGE_STATUS_ALL_ZERO, /*!< Page is all zeros */ |
| 890 |
IMPORT_PAGE_STATUS_CORRUPTED /*!< Page is corrupted */ |
| 891 |
}; |
| 892 |
|
| 893 |
/** Update the page, set the space id, max trx id and index id. |
| 894 |
@param block block read from file |
| 895 |
@param page_type type of the page |
| 896 |
@retval DB_SUCCESS or error code */ |
| 897 |
dberr_t update_page(buf_block_t *block, ulint &page_type) UNIV_NOTHROW; |
| 898 |
|
| 899 |
#ifdef UNIV_DEBUG |
| 900 |
/** |
| 901 |
@return true error condition is enabled. */ |
| 902 |
bool trigger_corruption() UNIV_NOTHROW { return (false); } |
| 903 |
#else |
| 904 |
#define trigger_corruption() (false) |
| 905 |
#endif /* UNIV_DEBUG */ |
| 906 |
|
| 907 |
/** Update the space, index id, trx id. |
| 908 |
@param block block to convert |
| 909 |
@return DB_SUCCESS or error code */ |
| 910 |
dberr_t update_index_page(buf_block_t *block) UNIV_NOTHROW; |
| 911 |
|
| 912 |
/** Update the BLOB references and write UNDO log entries for |
| 913 |
rows that can't be purged optimistically. |
| 914 |
@param block block to update |
| 915 |
@retval DB_SUCCESS or error code */ |
| 916 |
dberr_t update_records(buf_block_t *block) UNIV_NOTHROW; |
| 917 |
|
| 918 |
/** Validate the page, check for corruption. |
| 919 |
@param offset physical offset within file. |
| 920 |
@param block page read from file. |
| 921 |
@return 0 on success, 1 if all zero, 2 if corrupted */ |
| 922 |
import_page_status_t validate(os_offset_t offset, |
| 923 |
buf_block_t *block) UNIV_NOTHROW; |
| 924 |
|
| 925 |
/** Validate the space flags and update tablespace header page. |
| 926 |
@param block block read from file, not from the buffer pool. |
| 927 |
@retval DB_SUCCESS or error code */ |
| 928 |
dberr_t update_header(buf_block_t *block) UNIV_NOTHROW; |
| 929 |
|
| 930 |
/** Adjust the BLOB reference for a single column that is externally stored |
| 931 |
@param rec record to update |
| 932 |
@param offsets column offsets for the record |
| 933 |
@param i column ordinal value |
| 934 |
@return DB_SUCCESS or error code */ |
| 935 |
dberr_t adjust_cluster_index_blob_column(rec_t *rec, const ulint *offsets, |
| 936 |
ulint i) UNIV_NOTHROW; |
| 937 |
|
| 938 |
/** Adjusts the BLOB reference in the clustered index row for all |
| 939 |
externally stored columns. |
| 940 |
@param rec record to update |
| 941 |
@param offsets column offsets for the record |
| 942 |
@return DB_SUCCESS or error code */ |
| 943 |
dberr_t adjust_cluster_index_blob_columns(rec_t *rec, |
| 944 |
const ulint *offsets) UNIV_NOTHROW; |
| 945 |
|
| 946 |
/** In the clustered index, adjist the BLOB pointers as needed. |
| 947 |
Also update the BLOB reference, write the new space id. |
| 948 |
@param rec record to update |
| 949 |
@param offsets column offsets for the record |
| 950 |
@return DB_SUCCESS or error code */ |
| 951 |
dberr_t adjust_cluster_index_blob_ref(rec_t *rec, |
| 952 |
const ulint *offsets) UNIV_NOTHROW; |
| 953 |
|
| 954 |
/** Purge delete-marked records, only if it is possible to do so without |
| 955 |
re-organising the B+tree. |
| 956 |
@return true if purge succeeded */ |
| 957 |
bool purge() UNIV_NOTHROW; |
| 958 |
|
| 959 |
/** Adjust the BLOB references and sys fields for the current record. |
| 960 |
@param index the index being converted |
| 961 |
@param rec record to update |
| 962 |
@param offsets column offsets for the record |
| 963 |
@return DB_SUCCESS or error code. */ |
| 964 |
dberr_t adjust_cluster_record(const dict_index_t *index, rec_t *rec, |
| 965 |
const ulint *offsets) UNIV_NOTHROW; |
| 966 |
|
| 967 |
/** Find an index with the matching id. |
| 968 |
@return row_index_t* instance or 0 */ |
| 969 |
row_index_t *find_index(space_index_t id) UNIV_NOTHROW { |
| 970 |
row_index_t *index = &m_cfg->m_indexes[0]; |
| 971 |
|
| 972 |
for (ulint i = 0; i < m_cfg->m_n_indexes; ++i, ++index) { |
| 973 |
if (id == index->m_id) { |
| 974 |
return (index); |
| 975 |
} |
| 976 |
} |
| 977 |
|
| 978 |
return (nullptr); |
| 979 |
} |
| 980 |
|
| 981 |
private: |
| 982 |
/** Config for table that is being imported. */ |
| 983 |
row_import *m_cfg; |
| 984 |
|
| 985 |
/** Current index whose pages are being imported */ |
| 986 |
row_index_t *m_index; |
| 987 |
|
| 988 |
/** Current system LSN */ |
| 989 |
lsn_t m_current_lsn; |
| 990 |
|
| 991 |
/** Alias for m_page_zip, only set for compressed pages. */ |
| 992 |
page_zip_des_t *m_page_zip_ptr; |
| 993 |
|
| 994 |
/** Iterator over records in a block */ |
| 995 |
RecIterator m_rec_iter; |
| 996 |
|
| 997 |
/** Record offset */ |
| 998 |
ulint m_offsets_[REC_OFFS_NORMAL_SIZE]; |
| 999 |
|
| 1000 |
/** Pointer to m_offsets_ */ |
| 1001 |
ulint *m_offsets; |
| 1002 |
|
| 1003 |
/** Memory heap for the record offsets */ |
| 1004 |
mem_heap_t *m_heap; |
| 1005 |
|
| 1006 |
/** Cluster index instance */ |
| 1007 |
dict_index_t *m_cluster_index; |
| 1008 |
}; |
| 1009 |
|
| 1010 |
/** |
| 1011 |
row_import destructor. */ |
| 1012 |
row_import::~row_import() UNIV_NOTHROW { |
| 1013 |
for (ulint i = 0; m_indexes != nullptr && i < m_n_indexes; ++i) { |
| 1014 |
ut::delete_arr(m_indexes[i].m_name); |
| 1015 |
|
| 1016 |
if (m_indexes[i].m_fields == nullptr) { |
| 1017 |
continue; |
| 1018 |
} |
| 1019 |
|
| 1020 |
dict_field_t *fields = m_indexes[i].m_fields; |
| 1021 |
ulint n_fields = m_indexes[i].m_n_fields; |
| 1022 |
|
| 1023 |
for (ulint j = 0; j < n_fields; ++j) { |
| 1024 |
ut::delete_arr(const_cast<char *>(fields[j].name())); |
| 1025 |
} |
| 1026 |
|
| 1027 |
ut::delete_arr(fields); |
| 1028 |
} |
| 1029 |
|
| 1030 |
for (ulint i = 0; m_col_names != nullptr && i < m_n_cols; ++i) { |
| 1031 |
ut::delete_arr(m_col_names[i]); |
| 1032 |
} |
| 1033 |
|
| 1034 |
ut::delete_arr(m_cols); |
| 1035 |
ut::delete_arr(m_indexes); |
| 1036 |
ut::delete_arr(m_col_names); |
| 1037 |
ut::delete_arr(m_table_name); |
| 1038 |
ut::delete_arr(m_hostname); |
| 1039 |
|
| 1040 |
if (m_heap != nullptr) { |
| 1041 |
mem_heap_free(m_heap); |
| 1042 |
} |
| 1043 |
} |
| 1044 |
|
| 1045 |
/** Find the index entry in in the indexes array. |
| 1046 |
@param name index name |
| 1047 |
@return instance if found else 0. */ |
| 1048 |
row_index_t *row_import::get_index(const char *name) const UNIV_NOTHROW { |
| 1049 |
for (ulint i = 0; i < m_n_indexes; ++i) { |
| 1050 |
const char *index_name; |
| 1051 |
row_index_t *index = &m_indexes[i]; |
| 1052 |
|
| 1053 |
index_name = reinterpret_cast<const char *>(index->m_name); |
| 1054 |
|
| 1055 |
if (strcmp(index_name, name) == 0) { |
| 1056 |
return (index); |
| 1057 |
} |
| 1058 |
} |
| 1059 |
|
| 1060 |
return (nullptr); |
| 1061 |
} |
| 1062 |
|
| 1063 |
/** Get the number of rows in the index. |
| 1064 |
@param name index name |
| 1065 |
@return number of rows (doesn't include delete marked rows). */ |
| 1066 |
ulint row_import::get_n_rows(const char *name) const UNIV_NOTHROW { |
| 1067 |
const row_index_t *index = get_index(name); |
| 1068 |
|
| 1069 |
ut_a(name != nullptr); |
| 1070 |
|
| 1071 |
return (index->m_stats.m_n_rows); |
| 1072 |
} |
| 1073 |
|
| 1074 |
/** Get the number of rows for which purge failed uding the convert phase. |
| 1075 |
@param name index name |
| 1076 |
@return number of rows for which purge failed. */ |
| 1077 |
ulint row_import::get_n_purge_failed(const char *name) const UNIV_NOTHROW { |
| 1078 |
const row_index_t *index = get_index(name); |
| 1079 |
|
| 1080 |
ut_a(name != nullptr); |
| 1081 |
|
| 1082 |
return (index->m_stats.m_n_purge_failed); |
| 1083 |
} |
| 1084 |
|
| 1085 |
/** Find the ordinal value of the column name in the cfg table columns. |
| 1086 |
@param name of column to look for. |
| 1087 |
@return ULINT_UNDEFINED if not found. */ |
| 1088 |
ulint row_import::find_col(const char *name) const UNIV_NOTHROW { |
| 1089 |
for (ulint i = 0; i < m_n_cols; ++i) { |
| 1090 |
const char *col_name; |
| 1091 |
|
| 1092 |
col_name = reinterpret_cast<const char *>(m_col_names[i]); |
| 1093 |
|
| 1094 |
if (strcmp(col_name, name) == 0) { |
| 1095 |
return (i); |
| 1096 |
} |
| 1097 |
} |
| 1098 |
|
| 1099 |
return (ULINT_UNDEFINED); |
| 1100 |
} |
| 1101 |
|
| 1102 |
/** |
| 1103 |
Check if the index schema that was read from the .cfg file matches the |
| 1104 |
in memory index definition. |
| 1105 |
@return DB_SUCCESS or error code. */ |
| 1106 |
dberr_t row_import::match_index_columns(THD *thd, const dict_index_t *index) |
| 1107 |
UNIV_NOTHROW { |
| 1108 |
row_index_t *cfg_index; |
| 1109 |
dberr_t err = DB_SUCCESS; |
| 1110 |
|
| 1111 |
cfg_index = get_index(index->name); |
| 1112 |
|
| 1113 |
if (cfg_index == nullptr) { |
| 1114 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 1115 |
"Index %s not found in tablespace meta-data file.", index->name()); |
| 1116 |
|
| 1117 |
return DB_ERROR; |
| 1118 |
} |
| 1119 |
|
| 1120 |
if (cfg_index->m_n_fields != index->n_fields) { |
| 1121 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 1122 |
"Index field count %lu doesn't match" |
| 1123 |
" tablespace metadata file value %lu", |
| 1124 |
(ulong)index->n_fields, (ulong)cfg_index->m_n_fields); |
| 1125 |
|
| 1126 |
return DB_ERROR; |
| 1127 |
} |
| 1128 |
|
| 1129 |
cfg_index->m_srv_index = index; |
| 1130 |
|
| 1131 |
if (cfg_index->m_n_nullable != index->n_nullable) { |
| 1132 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 1133 |
"Index %s's nullability of %u doesn't match tablespace metadata " |
| 1134 |
"file value of " ULINTPF, |
| 1135 |
index->name(), index->n_nullable, cfg_index->m_n_nullable); |
| 1136 |
|
| 1137 |
return DB_ERROR; |
| 1138 |
} |
| 1139 |
|
| 1140 |
const dict_field_t *field = index->fields; |
| 1141 |
const dict_field_t *cfg_field = cfg_index->m_fields; |
| 1142 |
|
| 1143 |
for (ulint i = 0; i < index->n_fields; ++i, ++field, ++cfg_field) { |
| 1144 |
if (strcmp(field->name(), cfg_field->name()) != 0) { |
| 1145 |
bool allow = false; |
| 1146 |
if (dict_index_is_sdi(index)) { |
| 1147 |
/* SDI-only compatibility: accept legacy swap between |
| 1148 |
compressed_len and uncompressed_len, regardless of position. */ |
| 1149 |
const bool swap_mismatch = |
| 1150 |
(strcmp(field->name(), "uncompressed_len") == 0 && |
| 1151 |
strcmp(cfg_field->name(), "compressed_len") == 0) || |
| 1152 |
(strcmp(field->name(), "compressed_len") == 0 && |
| 1153 |
strcmp(cfg_field->name(), "uncompressed_len") == 0); |
| 1154 |
|
| 1155 |
allow = swap_mismatch; |
| 1156 |
} |
| 1157 |
|
| 1158 |
if (!allow) { |
| 1159 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 1160 |
"Index field name %s doesn't match tablespace metadata" |
| 1161 |
" field name %s for field position %lu", |
| 1162 |
field->name(), cfg_field->name(), (ulong)i); |
| 1163 |
|
| 1164 |
err = DB_ERROR; |
| 1165 |
} |
| 1166 |
} |
| 1167 |
|
| 1168 |
if (cfg_field->prefix_len != field->prefix_len) { |
| 1169 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 1170 |
"Index %s field %s prefix len %lu doesn't match metadata" |
| 1171 |
" file value %lu", |
| 1172 |
index->name(), field->name(), (ulong)field->prefix_len, |
| 1173 |
(ulong)cfg_field->prefix_len); |
| 1174 |
|
| 1175 |
err = DB_ERROR; |
| 1176 |
} |
| 1177 |
|
| 1178 |
if (cfg_field->fixed_len != field->fixed_len) { |
| 1179 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 1180 |
"Index %s field %s fixed len %lu doesn't match metadata" |
| 1181 |
" file value %lu", |
| 1182 |
index->name(), field->name(), (ulong)field->fixed_len, |
| 1183 |
(ulong)cfg_field->fixed_len); |
| 1184 |
|
| 1185 |
err = DB_ERROR; |
| 1186 |
} |
| 1187 |
|
| 1188 |
constexpr char asc[] = "ascending"; |
| 1189 |
constexpr char desc[] = "descending"; |
| 1190 |
|
| 1191 |
if (cfg_field->is_ascending != field->is_ascending) { |
| 1192 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 1193 |
"Index %s field %s is %s which does not match metadata" |
| 1194 |
" file which is %s", |
| 1195 |
index->name(), field->name(), (field->is_ascending ? asc : desc), |
| 1196 |
(cfg_field->is_ascending ? asc : desc)); |
| 1197 |
|
| 1198 |
err = DB_SCHEMA_MISMATCH; |
| 1199 |
} |
| 1200 |
} |
| 1201 |
|
| 1202 |
return err; |
| 1203 |
} |
| 1204 |
|
| 1205 |
/** Check if the column default values of table schema that was |
| 1206 |
read from the .cfg file matches the in memory column definition. |
| 1207 |
@param[in] thd MySQL session variable |
| 1208 |
@param[in] dd_table dd::Table |
| 1209 |
@return DB_SUCCESS or error code. */ |
| 1210 |
dberr_t row_import::match_col_default_values( |
| 1211 |
THD *thd, const dd::Table *dd_table) UNIV_NOTHROW { |
| 1212 |
dberr_t err = DB_SUCCESS; |
| 1213 |
|
| 1214 |
ut_ad(dd_table_is_partitioned(*dd_table) == dict_table_is_partition(m_table)); |
| 1215 |
|
| 1216 |
err = set_instant_info(thd, dd_table); |
| 1217 |
|
| 1218 |
if (err != DB_SUCCESS) { |
| 1219 |
return (err); |
| 1220 |
} |
| 1221 |
|
| 1222 |
/* Only check instant partitioned table. Because different partitions |
| 1223 |
may have different number of default values, make sure the default |
| 1224 |
values of this imported table match the default values which are |
| 1225 |
already remembered in server. |
| 1226 |
Also if the table in server is not instant, then all fine, just |
| 1227 |
store the new default values */ |
| 1228 |
if (!m_table->has_instant_cols() || !dict_table_is_partition(m_table) || |
| 1229 |
!dd_table_has_instant_cols(*dd_table)) { |
| 1230 |
return (err); |
| 1231 |
} |
| 1232 |
|
| 1233 |
for (uint16_t i = 0; i < m_table->get_n_user_cols(); ++i) { |
| 1234 |
dict_col_t *col = m_table->get_col(i); |
| 1235 |
if (col->instant_default == nullptr) { |
| 1236 |
continue; |
| 1237 |
} |
| 1238 |
|
| 1239 |
const dd::Column *dd_col = |
| 1240 |
dd_find_column(dd_table, m_table->get_col_name(i)); |
| 1241 |
|
| 1242 |
if (!dd_match_default_value(dd_col, col)) { |
| 1243 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 1244 |
"Default values of instant column %s mismatch", |
| 1245 |
dd_col->name().c_str()); |
| 1246 |
|
| 1247 |
err = DB_ERROR; |
| 1248 |
break; |
| 1249 |
} |
| 1250 |
} |
| 1251 |
|
| 1252 |
return (err); |
| 1253 |
} |
| 1254 |
|
| 1255 |
/** Check if the table schema that was read from the .cfg file matches the |
| 1256 |
in memory table definition. |
| 1257 |
@param[in] thd MySQL session variable |
| 1258 |
@param[in] dd_table dd::Table |
| 1259 |
@return DB_SUCCESS or error code. */ |
| 1260 |
dberr_t row_import::match_compression_type_option( |
| 1261 |
THD *thd, const dd::Table *dd_table) UNIV_NOTHROW { |
| 1262 |
dd::String_type compress_option; |
| 1263 |
auto &options = dd_table->options(); |
| 1264 |
|
| 1265 |
if (options.exists("compress")) { |
| 1266 |
options.get("compress", &compress_option); |
| 1267 |
} else { |
| 1268 |
compress_option = "none"; |
| 1269 |
} |
| 1270 |
|
| 1271 |
if (innobase_strcasecmp(Compression::to_string(m_compression_type), |
| 1272 |
compress_option.c_str()) != 0) { |
| 1273 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 1274 |
"Compression option does not match"); |
| 1275 |
return DB_ERROR; |
| 1276 |
} |
| 1277 |
|
| 1278 |
return DB_SUCCESS; |
| 1279 |
} |
| 1280 |
|
| 1281 |
/** Check if the table schema that was read from the .cfg file matches the |
| 1282 |
in memory table definition. |
| 1283 |
@param thd MySQL session variable |
| 1284 |
@return DB_SUCCESS or error code. */ |
| 1285 |
dberr_t row_import::match_table_columns(THD *thd) UNIV_NOTHROW { |
| 1286 |
dberr_t err = DB_SUCCESS; |
| 1287 |
const dict_col_t *col = m_table->cols; |
| 1288 |
uint32_t n_sys_cols = 0; |
| 1289 |
|
| 1290 |
if (m_version >= IB_EXPORT_CFG_VERSION_V7 && m_table->has_row_versions()) { |
| 1291 |
/* Only target table has row versions, ERROR */ |
| 1292 |
if (!has_row_versions()) { |
| 1293 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 1294 |
"The .cfg file indicates no INSTANT column in the source table" |
| 1295 |
" whereas the metadata in data dictionary says there are instant" |
| 1296 |
" columns in the target table"); |
| 1297 |
|
| 1298 |
return (DB_ERROR); |
| 1299 |
} |
| 1300 |
|
| 1301 |
if (m_table->current_row_version != m_current_row_version) { |
| 1302 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 1303 |
"Table has instant column but current row version didn't match."); |
| 1304 |
return (DB_ERROR); |
| 1305 |
} |
| 1306 |
|
| 1307 |
if ((m_table->initial_col_count != m_initial_column_count) || |
| 1308 |
(m_table->current_col_count != m_current_column_count) || |
| 1309 |
(m_table->total_col_count != m_total_column_count)) { |
| 1310 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 1311 |
"Table has instant column but column counts didn't match."); |
| 1312 |
return (DB_ERROR); |
| 1313 |
} |
| 1314 |
} |
| 1315 |
|
| 1316 |
/* Following loop makes sure all the columns present in target table are |
| 1317 |
accounted for */ |
| 1318 |
for (ulint i = 0; i < m_table->n_cols; ++i, ++col) { |
| 1319 |
const char *col_name; |
| 1320 |
ulint cfg_col_index; |
| 1321 |
|
| 1322 |
col_name = m_table->get_col_name(dict_col_get_no(col)); |
| 1323 |
|
| 1324 |
if ((strcmp(col_name, "DB_ROW_ID") == 0) || |
| 1325 |
(strcmp(col_name, "DB_TRX_ID") == 0) || |
| 1326 |
(strcmp(col_name, "DB_ROLL_PTR") == 0)) { |
| 1327 |
n_sys_cols += 1; |
| 1328 |
} |
| 1329 |
|
| 1330 |
cfg_col_index = find_col(col_name); |
| 1331 |
|
| 1332 |
if (cfg_col_index == ULINT_UNDEFINED) { |
| 1333 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 1334 |
"Column %s not found in tablespace.", col_name); |
| 1335 |
|
| 1336 |
err = DB_ERROR; |
| 1337 |
} else if (cfg_col_index != col->ind) { |
| 1338 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 1339 |
"Column %s ordinal value mismatch, it's at" |
| 1340 |
" %lu in the table and %lu in the tablespace" |
| 1341 |
" meta-data file", |
| 1342 |
col_name, (ulong)col->ind, (ulong)cfg_col_index); |
| 1343 |
|
| 1344 |
err = DB_ERROR; |
| 1345 |
} else { |
| 1346 |
const dict_col_t *cfg_col; |
| 1347 |
|
| 1348 |
cfg_col = &m_cols[cfg_col_index]; |
| 1349 |
ut_a(cfg_col->ind == cfg_col_index); |
| 1350 |
|
| 1351 |
if (cfg_col->prtype != col->prtype) { |
| 1352 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 1353 |
"Column %s precise type mismatch.", col_name); |
| 1354 |
err = DB_ERROR; |
| 1355 |
} |
| 1356 |
|
| 1357 |
if (cfg_col->mtype != col->mtype) { |
| 1358 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 1359 |
"Column %s main type mismatch.", col_name); |
| 1360 |
err = DB_ERROR; |
| 1361 |
} |
| 1362 |
|
| 1363 |
if (cfg_col->len != col->len) { |
| 1364 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 1365 |
"Column %s length mismatch.", col_name); |
| 1366 |
err = DB_ERROR; |
| 1367 |
} |
| 1368 |
|
| 1369 |
if (cfg_col->mbminmaxlen != col->mbminmaxlen) { |
| 1370 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 1371 |
"Column %s multi-byte len mismatch.", col_name); |
| 1372 |
err = DB_ERROR; |
| 1373 |
} |
| 1374 |
|
| 1375 |
if (cfg_col->ind != col->ind) { |
| 1376 |
err = DB_ERROR; |
| 1377 |
} |
| 1378 |
|
| 1379 |
if (cfg_col->ord_part != col->ord_part) { |
| 1380 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 1381 |
"Column %s ordering mismatch.", col_name); |
| 1382 |
err = DB_ERROR; |
| 1383 |
} |
| 1384 |
|
| 1385 |
if (cfg_col->max_prefix != col->max_prefix) { |
| 1386 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 1387 |
"Column %s max prefix mismatch.", col_name); |
| 1388 |
err = DB_ERROR; |
| 1389 |
} |
| 1390 |
} |
| 1391 |
} |
| 1392 |
|
| 1393 |
/* Following check makes sure all the columns present in config file are |
| 1394 |
accounted for */ |
| 1395 |
if (m_version >= IB_EXPORT_CFG_VERSION_V7 && has_row_versions()) { |
| 1396 |
if (!(m_table->n_cols - n_sys_cols == m_current_column_count)) { |
| 1397 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 1398 |
"Found %u columns in destination table whereas cfg file has %u" |
| 1399 |
" columns.", |
| 1400 |
(m_table->n_cols - n_sys_cols), m_current_column_count); |
| 1401 |
err = DB_ERROR; |
| 1402 |
} |
| 1403 |
} else { |
| 1404 |
if (!(m_table->n_cols == m_n_cols)) { |
| 1405 |
ib_errf( |
| 1406 |
thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 1407 |
"Found %u columns in destination table whereas cfg file has " ULINTPF |
| 1408 |
" columns.", |
| 1409 |
(m_table->n_cols - n_sys_cols), (m_n_cols - n_sys_cols)); |
| 1410 |
err = DB_ERROR; |
| 1411 |
} |
| 1412 |
} |
| 1413 |
|
| 1414 |
return (err); |
| 1415 |
} |
| 1416 |
|
| 1417 |
/** Check if the table (and index) schema that was read from the .cfg file |
| 1418 |
matches the in memory table definition. |
| 1419 |
@param[in] thd MySQL session variable |
| 1420 |
@param[in] dd_table dd::Table |
| 1421 |
@return DB_SUCCESS or error code. */ |
| 1422 |
dberr_t row_import::match_schema(THD *thd, |
| 1423 |
const dd::Table *dd_table) UNIV_NOTHROW { |
| 1424 |
/* Do some simple checks. */ |
| 1425 |
|
| 1426 |
if (m_flags != m_table->flags) { |
| 1427 |
if (dict_tf_to_row_format_string(m_flags) != |
| 1428 |
dict_tf_to_row_format_string(m_table->flags)) { |
| 1429 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 1430 |
"Table flags don't match, server table has %s" |
| 1431 |
" and the meta-data file has %s", |
| 1432 |
(const char *)dict_tf_to_row_format_string(m_table->flags), |
| 1433 |
(const char *)dict_tf_to_row_format_string(m_flags)); |
| 1434 |
} else if (DICT_TF_HAS_DATA_DIR(m_flags) != |
| 1435 |
DICT_TF_HAS_DATA_DIR(m_table->flags)) { |
| 1436 |
/* If the meta-data flag is set for data_dir, but table flag is not set |
| 1437 |
for data_dir or vice versa then return error. */ |
| 1438 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 1439 |
"Table location flags do not match. The source table %s a " |
| 1440 |
"DATA DIRECTORY but the destination table %s.", |
| 1441 |
(DICT_TF_HAS_DATA_DIR(m_flags) ? "uses" : "does not use"), |
| 1442 |
(DICT_TF_HAS_DATA_DIR(m_table->flags) ? "does" : "does not")); |
| 1443 |
} else { |
| 1444 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 1445 |
"Table flags don't match"); |
| 1446 |
} |
| 1447 |
return (DB_ERROR); |
| 1448 |
} else if (m_table->n_cols != m_n_cols - m_n_instant_drop_cols) { |
| 1449 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 1450 |
"Number of columns don't match, table has %lu" |
| 1451 |
" columns but the tablespace meta-data file has" |
| 1452 |
" %lu columns", |
| 1453 |
(ulong)m_table->n_cols, (ulong)(m_n_cols - m_n_instant_drop_cols)); |
| 1454 |
|
| 1455 |
return (DB_ERROR); |
| 1456 |
} else if (UT_LIST_GET_LEN(m_table->indexes) + (m_has_sdi ? 1 : 0) != |
| 1457 |
m_n_indexes) { |
| 1458 |
/* If the number of indexes don't match then it is better |
| 1459 |
to abort the IMPORT. It is easy for the user to create a |
| 1460 |
table matching the IMPORT definition. */ |
| 1461 |
|
| 1462 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 1463 |
"Number of indexes don't match, table has %lu" |
| 1464 |
" indexes but the tablespace meta-data file has" |
| 1465 |
" %lu indexes", |
| 1466 |
(ulong)UT_LIST_GET_LEN(m_table->indexes), (ulong)m_n_indexes); |
| 1467 |
|
| 1468 |
return (DB_ERROR); |
| 1469 |
} |
| 1470 |
|
| 1471 |
dberr_t err; |
| 1472 |
|
| 1473 |
if (m_version >= IB_EXPORT_CFG_VERSION_V6) { |
| 1474 |
err = match_compression_type_option(thd, dd_table); |
| 1475 |
|
| 1476 |
if (err != DB_SUCCESS) { |
| 1477 |
return err; |
| 1478 |
} |
| 1479 |
} |
| 1480 |
|
| 1481 |
err = match_table_columns(thd); |
| 1482 |
|
| 1483 |
if (err != DB_SUCCESS) { |
| 1484 |
return (err); |
| 1485 |
} |
| 1486 |
|
| 1487 |
err = match_col_default_values(thd, dd_table); |
| 1488 |
|
| 1489 |
if (err != DB_SUCCESS) { |
| 1490 |
return (err); |
| 1491 |
} |
| 1492 |
|
| 1493 |
/* Check if the SDI index definitions match */ |
| 1494 |
|
| 1495 |
if (m_has_sdi) { |
| 1496 |
const dict_index_t *index; |
| 1497 |
dict_mutex_enter_for_mysql(); |
| 1498 |
|
| 1499 |
index = dict_sdi_get_index(m_table->space); |
| 1500 |
|
| 1501 |
if (index == nullptr) { |
| 1502 |
dict_sdi_create_idx_in_mem(m_table->space, true, |
| 1503 |
dict_tf_to_fsp_flags(m_flags), false); |
| 1504 |
|
| 1505 |
index = dict_sdi_get_index(m_table->space); |
| 1506 |
} |
| 1507 |
|
| 1508 |
dict_mutex_exit_for_mysql(); |
| 1509 |
|
| 1510 |
ut_ad(index != nullptr); |
| 1511 |
|
| 1512 |
dberr_t index_err = match_index_columns(thd, index); |
| 1513 |
|
| 1514 |
if (index_err != DB_SUCCESS) { |
| 1515 |
err = index_err; |
| 1516 |
} |
| 1517 |
} |
| 1518 |
|
| 1519 |
if (err != DB_SUCCESS) { |
| 1520 |
return (err); |
| 1521 |
} |
| 1522 |
|
| 1523 |
/* Check if the index definitions match. */ |
| 1524 |
for (auto index : m_table->indexes) { |
| 1525 |
dberr_t index_err; |
| 1526 |
|
| 1527 |
index_err = match_index_columns(thd, index); |
| 1528 |
|
| 1529 |
if (index_err != DB_SUCCESS) { |
| 1530 |
err = index_err; |
| 1531 |
} |
| 1532 |
} |
| 1533 |
|
| 1534 |
return (err); |
| 1535 |
} |
| 1536 |
|
| 1537 |
/** |
| 1538 |
Set the index root <space, pageno>, using index name. */ |
| 1539 |
void row_import::set_root_by_name() UNIV_NOTHROW { |
| 1540 |
row_index_t *cfg_index = m_indexes; |
| 1541 |
dict_index_t *index; |
| 1542 |
ulint normal_indexes_count = m_has_sdi ? (m_n_indexes - 1) : m_n_indexes; |
| 1543 |
|
| 1544 |
if (m_has_sdi) { |
| 1545 |
dict_mutex_enter_for_mysql(); |
| 1546 |
index = dict_sdi_get_index(m_table->space); |
| 1547 |
dict_mutex_exit_for_mysql(); |
| 1548 |
|
| 1549 |
ut_ad(index != nullptr); |
| 1550 |
index->space = m_table->space; |
| 1551 |
index->page = cfg_index->m_page_no; |
| 1552 |
++cfg_index; |
| 1553 |
} |
| 1554 |
|
| 1555 |
for (uint32_t i = 0; i < normal_indexes_count; ++i, ++cfg_index) { |
| 1556 |
const char *index_name; |
| 1557 |
|
| 1558 |
index_name = reinterpret_cast<const char *>(cfg_index->m_name); |
| 1559 |
|
| 1560 |
index = dict_table_get_index_on_name(m_table, index_name); |
| 1561 |
|
| 1562 |
/* We've already checked that it exists. */ |
| 1563 |
ut_a(index != nullptr); |
| 1564 |
|
| 1565 |
/* Set the root page number and space id. */ |
| 1566 |
index->space = m_table->space; |
| 1567 |
index->page = cfg_index->m_page_no; |
| 1568 |
} |
| 1569 |
} |
| 1570 |
|
| 1571 |
/** |
| 1572 |
Set the index root <space, pageno>, using a heuristic. |
| 1573 |
@return DB_SUCCESS or error code */ |
| 1574 |
dberr_t row_import::set_root_by_heuristic() UNIV_NOTHROW { |
| 1575 |
row_index_t *cfg_index = m_indexes; |
| 1576 |
|
| 1577 |
ut_a(m_n_indexes > 0); |
| 1578 |
|
| 1579 |
// TODO: For now use brute force, based on ordinality |
| 1580 |
|
| 1581 |
ulint num_indexes = UT_LIST_GET_LEN(m_table->indexes) + (m_has_sdi ? 1 : 0); |
| 1582 |
if (num_indexes != m_n_indexes) { |
| 1583 |
ib::warn(ER_IB_MSG_939) |
| 1584 |
<< "Table " << m_table->name << " should have " << num_indexes |
| 1585 |
<< " indexes but" |
| 1586 |
" the tablespace has " |
| 1587 |
<< m_n_indexes << " indexes"; |
| 1588 |
} |
| 1589 |
|
| 1590 |
dict_mutex_enter_for_mysql(); |
| 1591 |
|
| 1592 |
ulint i = 0; |
| 1593 |
dberr_t err = DB_SUCCESS; |
| 1594 |
|
| 1595 |
if (m_has_sdi) { |
| 1596 |
dict_index_t *index = dict_sdi_get_index(m_table->space); |
| 1597 |
if (index == nullptr) { |
| 1598 |
dict_sdi_create_idx_in_mem(m_table->space, true, |
| 1599 |
dict_tf_to_fsp_flags(m_flags), false); |
| 1600 |
|
| 1601 |
index = dict_sdi_get_index(m_table->space); |
| 1602 |
} |
| 1603 |
|
| 1604 |
ut_ad(index != nullptr); |
| 1605 |
ut::delete_arr(cfg_index[i].m_name); |
| 1606 |
|
| 1607 |
ulint len = strlen(index->name) + 1; |
| 1608 |
|
| 1609 |
cfg_index[i].m_name = |
| 1610 |
ut::new_arr_withkey<byte>(UT_NEW_THIS_FILE_PSI_KEY, ut::Count{len}); |
| 1611 |
|
| 1612 |
if (cfg_index[i].m_name == nullptr) { |
| 1613 |
err = DB_OUT_OF_MEMORY; |
| 1614 |
dict_mutex_exit_for_mysql(); |
| 1615 |
return (err); |
| 1616 |
} |
| 1617 |
|
| 1618 |
memcpy(cfg_index[i].m_name, index->name, len); |
| 1619 |
|
| 1620 |
cfg_index[i].m_srv_index = index; |
| 1621 |
|
| 1622 |
index->space = m_table->space; |
| 1623 |
index->page = cfg_index[i].m_page_no; |
| 1624 |
++i; |
| 1625 |
} |
| 1626 |
|
| 1627 |
for (auto index : m_table->indexes) { |
| 1628 |
if (index->type & DICT_FTS) { |
| 1629 |
dict_set_corrupted(index); |
| 1630 |
ib::warn(ER_IB_MSG_940) << "Skipping FTS index: " << index->name; |
| 1631 |
} else if (i < m_n_indexes) { |
| 1632 |
ut::delete_arr(cfg_index[i].m_name); |
| 1633 |
|
| 1634 |
ulint len = strlen(index->name) + 1; |
| 1635 |
|
| 1636 |
cfg_index[i].m_name = |
| 1637 |
ut::new_arr_withkey<byte>(UT_NEW_THIS_FILE_PSI_KEY, ut::Count{len}); |
| 1638 |
|
| 1639 |
/* Trigger OOM */ |
| 1640 |
DBUG_EXECUTE_IF("ib_import_OOM_14", ut::delete_arr(cfg_index[i].m_name); |
| 1641 |
cfg_index[i].m_name = nullptr;); |
| 1642 |
|
| 1643 |
if (cfg_index[i].m_name == nullptr) { |
| 1644 |
err = DB_OUT_OF_MEMORY; |
| 1645 |
break; |
| 1646 |
} |
| 1647 |
|
| 1648 |
memcpy(cfg_index[i].m_name, index->name, len); |
| 1649 |
|
| 1650 |
cfg_index[i].m_srv_index = index; |
| 1651 |
|
| 1652 |
index->space = m_table->space; |
| 1653 |
index->page = cfg_index[i].m_page_no; |
| 1654 |
++i; |
| 1655 |
} |
| 1656 |
} |
| 1657 |
|
| 1658 |
dict_mutex_exit_for_mysql(); |
| 1659 |
|
| 1660 |
return (err); |
| 1661 |
} |
| 1662 |
|
| 1663 |
dberr_t row_import::match_instant_metadata_in_target_table(THD *thd) { |
| 1664 |
if (m_table->current_row_version != m_current_row_version) { |
| 1665 |
/* It must have already been checked in match_table_columns */ |
| 1666 |
ut_ad(false); |
| 1667 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 1668 |
"Target table also has instant column but current row version" |
| 1669 |
" didn't match with the configuration file."); |
| 1670 |
return (DB_ERROR); |
| 1671 |
} |
| 1672 |
|
| 1673 |
if ((m_table->initial_col_count != m_initial_column_count) || |
| 1674 |
(m_table->current_col_count != m_current_column_count) || |
| 1675 |
(m_table->total_col_count != m_total_column_count)) { |
| 1676 |
/* It must have already been checked in match_table_columns */ |
| 1677 |
ut_ad(false); |
| 1678 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 1679 |
"Target table also has instant columns but column counts didn't" |
| 1680 |
" match with the configuration file."); |
| 1681 |
return (DB_ERROR); |
| 1682 |
} |
| 1683 |
|
| 1684 |
for (uint32_t i = 0; i < m_n_cols; i++) { |
| 1685 |
dict_col_t *cfg_col = &m_cols[i]; |
| 1686 |
ut_ad(cfg_col != nullptr); |
| 1687 |
|
| 1688 |
const char *col_name = (char *)m_col_names[i]; |
| 1689 |
|
| 1690 |
/* Search for this column in target table */ |
| 1691 |
dict_col_t *target_col = m_table->get_col_by_name(col_name); |
| 1692 |
|
| 1693 |
if (target_col == nullptr) { |
| 1694 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 1695 |
"Instant metadata didn't match for dropped column"); |
| 1696 |
return DB_ERROR; |
| 1697 |
} |
| 1698 |
|
| 1699 |
ut_a(target_col != nullptr); |
| 1700 |
|
| 1701 |
if (!cfg_col->is_version_added_match(target_col) || |
| 1702 |
!cfg_col->is_version_dropped_match(target_col) || |
| 1703 |
(cfg_col->ind != target_col->ind) || |
| 1704 |
(cfg_col->get_phy_pos() != target_col->get_phy_pos())) { |
| 1705 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 1706 |
"Instant metadata didn't match for column %s", col_name); |
| 1707 |
return DB_ERROR; |
| 1708 |
} |
| 1709 |
|
| 1710 |
if (cfg_col->instant_default == nullptr && |
| 1711 |
target_col->instant_default == nullptr) { |
| 1712 |
/* This isn't an INSTANT ADD columns or this column has been dropped. */ |
| 1713 |
ut_ad(!cfg_col->is_instant_added() || cfg_col->is_instant_dropped()); |
| 1714 |
continue; |
| 1715 |
} |
| 1716 |
|
| 1717 |
if (cfg_col->instant_default == nullptr && |
| 1718 |
target_col->instant_default != nullptr) { |
| 1719 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 1720 |
"The metadata in the data dictionary and the .cfg file contain" |
| 1721 |
" different default values for column %s!", |
| 1722 |
col_name); |
| 1723 |
return DB_ERROR; |
| 1724 |
} |
| 1725 |
|
| 1726 |
if (cfg_col->instant_default != nullptr && |
| 1727 |
target_col->instant_default == nullptr) { |
| 1728 |
/* set the value from .cfg file. */ |
| 1729 |
target_col->set_default(cfg_col->instant_default->value, |
| 1730 |
cfg_col->instant_default->len, m_table->heap); |
| 1731 |
} |
| 1732 |
|
| 1733 |
/* If instant_default values are different, error */ |
| 1734 |
if (*target_col->instant_default != *cfg_col->instant_default) { |
| 1735 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 1736 |
"The metadata in the data dictionary and the .cfg file contain" |
| 1737 |
" different default values for column %s!", |
| 1738 |
col_name); |
| 1739 |
return DB_ERROR; |
| 1740 |
} |
| 1741 |
} |
| 1742 |
|
| 1743 |
return DB_SUCCESS; |
| 1744 |
} |
| 1745 |
|
| 1746 |
dberr_t row_import::add_instant_dropped_columns(dict_table_t *target_table) { |
| 1747 |
dict_index_t *index = m_table->first_index(); |
| 1748 |
ut_ad(index->is_clustered()); |
| 1749 |
|
| 1750 |
/* NOTE : Generated columns can't be part of clustered index so all the |
| 1751 |
fields have to be pointing to cols in table->cols. */ |
| 1752 |
uint16_t *mapping = ut::new_arr_withkey<uint16_t>(UT_NEW_THIS_FILE_PSI_KEY, |
| 1753 |
ut::Count{index->n_fields}); |
| 1754 |
for (size_t i = 0; i < index->n_fields; i++) { |
| 1755 |
mapping[i] = index->get_field(i)->col->ind; |
| 1756 |
} |
| 1757 |
|
| 1758 |
/* Get the table->heap size in saved_heap_size */ |
| 1759 |
size_t old_heap_size = |
| 1760 |
mem_heap_get_size(target_table->heap) + mem_heap_get_size(index->heap); |
| 1761 |
|
| 1762 |
uint32_t n_dropped_cols = m_total_column_count - m_current_column_count; |
| 1763 |
|
| 1764 |
/* Allocate memory for n_cols (table->n_cols + n_dropped_cols) */ |
| 1765 |
{ |
| 1766 |
dict_col_t *cols = target_table->cols; |
| 1767 |
uint32_t total_cols = target_table->n_cols + n_dropped_cols; |
| 1768 |
target_table->cols = (dict_col_t *)mem_heap_alloc( |
| 1769 |
target_table->heap, total_cols * sizeof(dict_col_t)); |
| 1770 |
memcpy(target_table->cols, cols, target_table->n_cols * sizeof(dict_col_t)); |
| 1771 |
} |
| 1772 |
|
| 1773 |
/* Allocate memory for n_fields (index->n_fields + n_dropped_cols) */ |
| 1774 |
{ |
| 1775 |
dict_field_t *fields = index->fields; |
| 1776 |
uint32_t total_fields = index->n_fields + n_dropped_cols; |
| 1777 |
index->fields = (dict_field_t *)mem_heap_alloc( |
| 1778 |
index->heap, 1 + (total_fields) * sizeof(dict_field_t)); |
| 1779 |
memcpy(index->fields, fields, 1 + index->n_fields * sizeof(dict_field_t)); |
| 1780 |
|
| 1781 |
/* Fix field->col pointers with the mapping created. */ |
| 1782 |
for (size_t i = 0; i < index->n_fields; i++) { |
| 1783 |
index->get_field(i)->col = target_table->get_col(mapping[i]); |
| 1784 |
} |
| 1785 |
} |
| 1786 |
|
| 1787 |
ut::delete_arr(mapping); |
| 1788 |
|
| 1789 |
/* Correct field mapping for secondary indexes too. */ |
| 1790 |
for (auto *secondary_index : m_table->indexes) { |
| 1791 |
if (secondary_index == index) continue; |
| 1792 |
|
| 1793 |
auto secondary_mapping = |
| 1794 |
std::make_unique<uint16_t[]>(secondary_index->n_fields); |
| 1795 |
|
| 1796 |
for (size_t i = 0; i < secondary_index->n_fields; i++) { |
| 1797 |
secondary_mapping[i] = secondary_index->get_field(i)->col->ind; |
| 1798 |
} |
| 1799 |
|
| 1800 |
/* Allocate memory for n_fields (secondary_index->n_fields). No need to |
| 1801 |
allocate for n_dropped_cols, because instant column drop is not supported |
| 1802 |
for columns that are part of secondary index. */ |
| 1803 |
{ |
| 1804 |
dict_field_t *fields = secondary_index->fields; |
| 1805 |
secondary_index->fields = (dict_field_t *)mem_heap_alloc( |
| 1806 |
secondary_index->heap, |
| 1807 |
1 + secondary_index->n_fields * sizeof(dict_field_t)); |
| 1808 |
memcpy(secondary_index->fields, fields, |
| 1809 |
1 + secondary_index->n_fields * sizeof(dict_field_t)); |
| 1810 |
|
| 1811 |
/* Fix field->col pointers with the mapping created. */ |
| 1812 |
for (size_t i = 0; i < secondary_index->n_fields; i++) { |
| 1813 |
secondary_index->get_field(i)->col = |
| 1814 |
target_table->get_col(secondary_mapping[i]); |
| 1815 |
} |
| 1816 |
} |
| 1817 |
} |
| 1818 |
|
| 1819 |
/* Set initial/current/total_col_count for table */ |
| 1820 |
target_table->initial_col_count = m_initial_column_count; |
| 1821 |
target_table->current_col_count = m_current_column_count; |
| 1822 |
target_table->total_col_count = m_total_column_count; |
| 1823 |
|
| 1824 |
/* Take a temp heap and add columns */ |
| 1825 |
mem_heap_t *heap = mem_heap_create(1000, UT_LOCATION_HERE); |
| 1826 |
for (size_t i = 0; i < m_n_cols; i++) { |
| 1827 |
dict_col_t *cfg_col = &m_cols[i]; |
| 1828 |
ut_ad(cfg_col != nullptr); |
| 1829 |
|
| 1830 |
if (cfg_col->is_instant_dropped()) { |
| 1831 |
uint8_t v_added = cfg_col->is_instant_added() |
| 1832 |
? cfg_col->get_version_added() |
| 1833 |
: UINT8_UNDEFINED; |
| 1834 |
uint8_t v_dropped = cfg_col->get_version_dropped(); |
| 1835 |
uint32_t phy_pos = cfg_col->get_phy_pos(); |
| 1836 |
std::string col_name = (char *)m_col_names[i]; |
| 1837 |
|
| 1838 |
dict_mem_table_add_col(m_table, heap, col_name.c_str(), cfg_col->mtype, |
| 1839 |
cfg_col->prtype, cfg_col->len, false, phy_pos, |
| 1840 |
v_added, v_dropped); |
| 1841 |
} |
| 1842 |
} |
| 1843 |
mem_heap_free(heap); |
| 1844 |
|
| 1845 |
for (size_t i = 0; i < m_n_cols; i++) { |
| 1846 |
dict_col_t *cfg_col = &m_cols[i]; |
| 1847 |
ut_ad(cfg_col != nullptr); |
| 1848 |
|
| 1849 |
if (cfg_col->is_instant_dropped()) { |
| 1850 |
std::string col_name = (char *)m_col_names[i]; |
| 1851 |
/* Add this field into clustered index fields */ |
| 1852 |
dict_col_t *col = m_table->get_col_by_name(col_name.c_str()); |
| 1853 |
ut_ad(col->mtype != DATA_SYS); |
| 1854 |
/* Physical position must have already been set */ |
| 1855 |
ut_ad(col->get_phy_pos() != UINT32_UNDEFINED); |
| 1856 |
|
| 1857 |
dict_index_add_col(index, m_table, col, 0, true); |
| 1858 |
|
| 1859 |
index->n_total_fields++; |
| 1860 |
} |
| 1861 |
} |
| 1862 |
|
| 1863 |
/* index->fields_array doesn't take space in index->heap. It will be updated |
| 1864 |
in the caller. */ |
| 1865 |
|
| 1866 |
/* Update the size change in dict_sys */ |
| 1867 |
size_t new_heap_size = |
| 1868 |
mem_heap_get_size(target_table->heap) + mem_heap_get_size(index->heap); |
| 1869 |
if (new_heap_size > old_heap_size) { |
| 1870 |
mutex_enter(&dict_sys->mutex); |
| 1871 |
dict_sys->size += new_heap_size - old_heap_size; |
| 1872 |
mutex_exit(&dict_sys->mutex); |
| 1873 |
} |
| 1874 |
|
| 1875 |
return DB_SUCCESS; |
| 1876 |
} |
| 1877 |
|
| 1878 |
/** Set the instant ADD/DROP COLUMN information to the table. |
| 1879 |
@return DB_SUCCESS if successful, or error code */ |
| 1880 |
dberr_t row_import::set_instant_info_v2(THD *thd, const dd::Table *dd_table) |
| 1881 |
UNIV_NOTHROW { |
| 1882 |
dberr_t err = DB_SUCCESS; |
| 1883 |
|
| 1884 |
bool src_has_row_versions = has_row_versions(); |
| 1885 |
bool dst_has_row_versions = m_table->has_row_versions(); |
| 1886 |
|
| 1887 |
/* None of the table has INSTANT columns. Return success. */ |
| 1888 |
if (!src_has_row_versions && !dst_has_row_versions) { |
| 1889 |
return (DB_SUCCESS); |
| 1890 |
} |
| 1891 |
|
| 1892 |
/* Only target table has INSTANT columns, ERROR */ |
| 1893 |
/* It must have already been checked in match_table_columns */ |
| 1894 |
ut_ad(!(!src_has_row_versions && dst_has_row_versions)); |
| 1895 |
if (!src_has_row_versions && dst_has_row_versions) { |
| 1896 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 1897 |
"The .cfg file indicates no INSTANT column in the source table" |
| 1898 |
" whereas the metadata in data dictionary says there are instant" |
| 1899 |
" columns in the target table"); |
| 1900 |
|
| 1901 |
return (DB_ERROR); |
| 1902 |
} |
| 1903 |
|
| 1904 |
/* Only source table has INSTANT columns. */ |
| 1905 |
if (src_has_row_versions && !dst_has_row_versions) { |
| 1906 |
/* Update INSTANT metadata in target table. */ |
| 1907 |
return (adjust_instant_metadata_in_taregt_table(thd, dd_table)); |
| 1908 |
} |
| 1909 |
|
| 1910 |
/* Both the tables have INSTANT columns. */ |
| 1911 |
if (src_has_row_versions && dst_has_row_versions) { |
| 1912 |
/* INSTANT metadata must match. */ |
| 1913 |
return (match_instant_metadata_in_target_table(thd)); |
| 1914 |
} |
| 1915 |
|
| 1916 |
return (err); |
| 1917 |
} |
| 1918 |
|
| 1919 |
dberr_t row_import::adjust_instant_metadata_in_taregt_table( |
| 1920 |
THD *thd, const dd::Table *dd_table) { |
| 1921 |
dberr_t err = DB_SUCCESS; |
| 1922 |
|
| 1923 |
/* It must have already been checked in match_table_columns */ |
| 1924 |
ut_ad(m_table->get_n_user_cols() == m_current_column_count); |
| 1925 |
if (m_table->get_n_user_cols() != m_current_column_count) { |
| 1926 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 1927 |
"Source table has INSTANT columns. Target table column count" |
| 1928 |
" didn't match with the configuration file."); |
| 1929 |
return (DB_ERROR); |
| 1930 |
} |
| 1931 |
|
| 1932 |
bool has_instant_drop_cols = m_total_column_count > m_current_column_count; |
| 1933 |
if (has_instant_drop_cols) { |
| 1934 |
/* Add dropped columns to target table definition. */ |
| 1935 |
add_instant_dropped_columns(m_table); |
| 1936 |
} |
| 1937 |
|
| 1938 |
size_t old_size = mem_heap_get_size(m_table->heap); |
| 1939 |
|
| 1940 |
for (uint32_t i = 0; i < m_n_cols; i++) { |
| 1941 |
dict_col_t *cfg_col = &m_cols[i]; |
| 1942 |
ut_ad(cfg_col != nullptr); |
| 1943 |
|
| 1944 |
std::string col_name = (char *)m_col_names[i]; |
| 1945 |
|
| 1946 |
/* Search for this column in target table */ |
| 1947 |
dict_col_t *target_col = m_table->get_col_by_name(col_name.c_str()); |
| 1948 |
|
| 1949 |
/* Normal column */ |
| 1950 |
if (!cfg_col->is_instant_added() && !cfg_col->is_instant_dropped()) { |
| 1951 |
ut_ad(target_col != nullptr); |
| 1952 |
ut_ad(!target_col->is_instant_added() && |
| 1953 |
!target_col->is_instant_dropped()); |
| 1954 |
ut_ad(cfg_col->instant_default == nullptr); |
| 1955 |
|
| 1956 |
/* We need to adjust phy_pos for column here */ |
| 1957 |
target_col->set_phy_pos(cfg_col->get_phy_pos()); |
| 1958 |
continue; |
| 1959 |
} |
| 1960 |
|
| 1961 |
/* INSTANT DROP column */ |
| 1962 |
if (cfg_col->is_instant_dropped()) { |
| 1963 |
ut_ad(dict_col_t::is_instant_dropped_name(col_name)); |
| 1964 |
|
| 1965 |
/* This columns must have already been added to table cache in |
| 1966 |
add_instant_dropped_columns() */ |
| 1967 |
ut_ad(target_col != nullptr); |
| 1968 |
ut_ad(target_col->get_phy_pos() == cfg_col->get_phy_pos()); |
| 1969 |
ut_ad(target_col->is_version_added_match(cfg_col)); |
| 1970 |
ut_ad(target_col->is_instant_dropped()); |
| 1971 |
ut_ad(target_col->is_version_dropped_match(cfg_col)); |
| 1972 |
|
| 1973 |
/* This column must have already been added to DD::Columns while |
| 1974 |
reading columns data from CFG file in row_import_read_columns(). */ |
| 1975 |
ut_ad(nullptr != dd_find_column(dd_table, col_name.c_str())); |
| 1976 |
|
| 1977 |
continue; |
| 1978 |
} |
| 1979 |
|
| 1980 |
/* INSTANT ADD column */ |
| 1981 |
if (cfg_col->is_instant_added()) { |
| 1982 |
ut_ad(!cfg_col->is_instant_dropped()); |
| 1983 |
/* This must be present in target. */ |
| 1984 |
ut_ad(target_col != nullptr); |
| 1985 |
if (target_col == nullptr) { |
| 1986 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 1987 |
"The column %s isn't found in target table.", col_name.c_str()); |
| 1988 |
err = DB_ERROR; |
| 1989 |
break; |
| 1990 |
} |
| 1991 |
|
| 1992 |
/* Update version_added/phy_pos for column. */ |
| 1993 |
target_col->set_version_added(cfg_col->get_version_added()); |
| 1994 |
target_col->set_phy_pos(cfg_col->get_phy_pos()); |
| 1995 |
|
| 1996 |
/* Set default value from .cfg file. */ |
| 1997 |
ut_ad(cfg_col->instant_default != nullptr); |
| 1998 |
target_col->set_default(cfg_col->instant_default->value, |
| 1999 |
cfg_col->instant_default->len, m_table->heap); |
| 2000 |
} |
| 2001 |
|
| 2002 |
/* Note: these info has to be updated in DD as well in |
| 2003 |
dd_import_instant_add_columns(). */ |
| 2004 |
} |
| 2005 |
|
| 2006 |
size_t new_size = mem_heap_get_size(m_table->heap); |
| 2007 |
if (new_size > old_size) { |
| 2008 |
mutex_enter(&dict_sys->mutex); |
| 2009 |
dict_sys->size += new_size - old_size; |
| 2010 |
mutex_exit(&dict_sys->mutex); |
| 2011 |
} |
| 2012 |
|
| 2013 |
if (err != DB_SUCCESS) { |
| 2014 |
return (err); |
| 2015 |
} |
| 2016 |
|
| 2017 |
m_table->initial_col_count = m_initial_column_count; |
| 2018 |
m_table->current_col_count = m_current_column_count; |
| 2019 |
m_table->total_col_count = m_total_column_count; |
| 2020 |
m_table->current_row_version = m_current_row_version; |
| 2021 |
|
| 2022 |
ut_ad(m_table->has_row_versions()); |
| 2023 |
|
| 2024 |
/* Reset field cache for every index, because it becomes invalid in case |
| 2025 |
of instant column add or drop. */ |
| 2026 |
for (auto *index : m_table->indexes) { |
| 2027 |
if (index == m_table->first_index()) { |
| 2028 |
ut_ad(index->is_clustered()); |
| 2029 |
|
| 2030 |
index->row_versions = true; |
| 2031 |
|
| 2032 |
/* Recreate fields array for clustered index */ |
| 2033 |
index->create_fields_array(); |
| 2034 |
index->create_nullables(m_table->current_row_version); |
| 2035 |
} |
| 2036 |
index->rec_cache.offsets = nullptr; |
| 2037 |
index->rec_cache.nullable_cols = 0; |
| 2038 |
} |
| 2039 |
|
| 2040 |
/* FIXME: Force to discard the table, in case of any rollback later. */ |
| 2041 |
// m_table->discard_after_ddl = true; |
| 2042 |
|
| 2043 |
return (err); |
| 2044 |
} |
| 2045 |
|
| 2046 |
/** Set the instant ADD COLUMN information to the table. |
| 2047 |
@return DB_SUCCESS if all instant columns are trailing columns, or error code */ |
| 2048 |
dberr_t row_import::set_instant_info(THD *thd, |
| 2049 |
const dd::Table *dd_table) UNIV_NOTHROW { |
| 2050 |
if (m_version >= IB_EXPORT_CFG_VERSION_V7) { |
| 2051 |
return set_instant_info_v2(thd, dd_table); |
| 2052 |
} |
| 2053 |
|
| 2054 |
dberr_t error = DB_SUCCESS; |
| 2055 |
dict_col_t *col = m_table->cols; |
| 2056 |
uint16_t instants = 0; |
| 2057 |
uint64_t old_size; |
| 2058 |
uint64_t new_size; |
| 2059 |
|
| 2060 |
/* If .cfg file indicates no INSTANT column in source table. */ |
| 2061 |
if (m_n_instant_cols == 0) { |
| 2062 |
/* But if target table has INSTANT columns, report error. */ |
| 2063 |
if (m_table->has_instant_cols()) { |
| 2064 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 2065 |
"The .cfg file indicates no INSTANT column in the source table" |
| 2066 |
" whereas the metadata in data dictionary says there are instant" |
| 2067 |
" columns in the target table"); |
| 2068 |
|
| 2069 |
return (DB_ERROR); |
| 2070 |
} |
| 2071 |
|
| 2072 |
/* All good. Return success. */ |
| 2073 |
m_table->set_instant_cols(m_table->get_n_user_cols()); |
| 2074 |
ut_ad(!m_table->has_instant_cols()); |
| 2075 |
return (DB_SUCCESS); |
| 2076 |
} |
| 2077 |
|
| 2078 |
/* Do not allow IMPORT if target table also have INSTANT columns. As after |
| 2079 |
the implementation of row versions in table |
| 2080 |
- IMPORT allowed with INSTANT columns in target table iff metadata matches |
| 2081 |
exactly with source table. |
| 2082 |
- The source table is from earlier release so metadata can't match. */ |
| 2083 |
if (m_table->has_row_versions()) { |
| 2084 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 2085 |
"Target table has INSTANT columns but the .cfg file is from earlier" |
| 2086 |
" release with INSTANT column in the source table. Instant metadata" |
| 2087 |
" can't match. Please create target table with no INSTANT column" |
| 2088 |
" and try IMPORT."); |
| 2089 |
return (DB_ERROR); |
| 2090 |
} |
| 2091 |
|
| 2092 |
old_size = mem_heap_get_size(m_table->heap); |
| 2093 |
|
| 2094 |
for (ulint i = 0; i < m_table->get_n_user_cols(); ++i, ++col) { |
| 2095 |
const char *col_name; |
| 2096 |
ulint cfg_col_index; |
| 2097 |
|
| 2098 |
col_name = m_table->get_col_name(dict_col_get_no(col)); |
| 2099 |
|
| 2100 |
cfg_col_index = find_col(col_name); |
| 2101 |
ut_ad(cfg_col_index != ULINT_UNDEFINED); |
| 2102 |
|
| 2103 |
const dict_col_t *cfg_col = &m_cols[cfg_col_index]; |
| 2104 |
|
| 2105 |
if (cfg_col->instant_default == nullptr) { |
| 2106 |
if (instants > 0) { |
| 2107 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 2108 |
"Instant columns read from meta-data" |
| 2109 |
" file mismatch, because there are" |
| 2110 |
" some columns which were not instantly" |
| 2111 |
" added after columns which were" |
| 2112 |
" instantly added"); |
| 2113 |
|
| 2114 |
error = DB_ERROR; |
| 2115 |
break; |
| 2116 |
} |
| 2117 |
|
| 2118 |
continue; |
| 2119 |
} |
| 2120 |
|
| 2121 |
++instants; |
| 2122 |
|
| 2123 |
/* If the data dictionary does not contain a default for this column set |
| 2124 |
the value from .cfg file. */ |
| 2125 |
if (col->instant_default == nullptr) { |
| 2126 |
col->set_default(cfg_col->instant_default->value, |
| 2127 |
cfg_col->instant_default->len, m_table->heap); |
| 2128 |
} |
| 2129 |
/* If the instant_default field is equal in the .cfg and DD just continue, |
| 2130 |
through the loop. Otherwise there's a collision, return an error here. */ |
| 2131 |
else if (*col->instant_default != *cfg_col->instant_default) { |
| 2132 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 2133 |
"The metadata in the data dictionary and the .cfg file contain" |
| 2134 |
" different default values for column %s!", |
| 2135 |
col_name); |
| 2136 |
|
| 2137 |
error = DB_ERROR; |
| 2138 |
break; |
| 2139 |
} |
| 2140 |
} |
| 2141 |
|
| 2142 |
new_size = mem_heap_get_size(m_table->heap); |
| 2143 |
if (new_size > old_size) { |
| 2144 |
dict_sys_mutex_enter(); |
| 2145 |
dict_sys->size += new_size - old_size; |
| 2146 |
dict_sys_mutex_exit(); |
| 2147 |
} |
| 2148 |
|
| 2149 |
if (error == DB_SUCCESS && instants != m_n_instant_cols) { |
| 2150 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 2151 |
"Number of instant columns don't match, table has" |
| 2152 |
" %lu instant columns record in meta-data file but" |
| 2153 |
" there are %lu columns with default value", |
| 2154 |
static_cast<ulong>(m_n_instant_cols), static_cast<ulong>(instants)); |
| 2155 |
|
| 2156 |
error = DB_ERROR; |
| 2157 |
} |
| 2158 |
|
| 2159 |
if (error != DB_SUCCESS) { |
| 2160 |
return (error); |
| 2161 |
} |
| 2162 |
|
| 2163 |
m_table->set_instant_cols(m_table->get_n_user_cols() - m_n_instant_cols); |
| 2164 |
ut_ad(m_table->has_instant_cols()); |
| 2165 |
m_table->set_upgraded_instant(); |
| 2166 |
|
| 2167 |
/* Reset field cache for every index, because it becomes invalid in case |
| 2168 |
of instant columns. */ |
| 2169 |
for (auto *index : m_table->indexes) { |
| 2170 |
if (index == m_table->first_index()) { |
| 2171 |
ut_ad(index->is_clustered()); |
| 2172 |
index->instant_cols = true; |
| 2173 |
index->set_instant_nullable(m_n_instant_nullable); |
| 2174 |
} |
| 2175 |
index->rec_cache.offsets = nullptr; |
| 2176 |
index->rec_cache.nullable_cols = 0; |
| 2177 |
} |
| 2178 |
|
| 2179 |
/* FIXME: Force to discard the table, in case of any rollback later. */ |
| 2180 |
// m_table->discard_after_ddl = true; |
| 2181 |
|
| 2182 |
return (DB_SUCCESS); |
| 2183 |
} |
| 2184 |
|
| 2185 |
/** |
| 2186 |
Purge delete marked records. |
| 2187 |
@return DB_SUCCESS or error code. */ |
| 2188 |
dberr_t IndexPurge::garbage_collect() UNIV_NOTHROW { |
| 2189 |
dberr_t err; |
| 2190 |
auto comp = dict_table_is_comp(m_index->table); |
| 2191 |
|
| 2192 |
/* Open the persistent cursor and start the mini-transaction. */ |
| 2193 |
|
| 2194 |
open(); |
| 2195 |
import_ctx_t import_ctx = {false}; |
| 2196 |
m_pcur.import_ctx = &import_ctx; |
| 2197 |
|
| 2198 |
while ((err = next()) == DB_SUCCESS) { |
| 2199 |
rec_t *rec = m_pcur.get_rec(); |
| 2200 |
auto deleted = rec_get_deleted_flag(rec, comp); |
| 2201 |
|
| 2202 |
if (!deleted) { |
| 2203 |
++m_n_rows; |
| 2204 |
} else { |
| 2205 |
purge(); |
| 2206 |
} |
| 2207 |
} |
| 2208 |
|
| 2209 |
/* Close the persistent cursor and commit the mini-transaction. */ |
| 2210 |
|
| 2211 |
close(); |
| 2212 |
if (m_pcur.import_ctx->is_error == true) { |
| 2213 |
m_pcur.import_ctx = nullptr; |
| 2214 |
return DB_TABLE_CORRUPT; |
| 2215 |
} |
| 2216 |
|
| 2217 |
m_pcur.import_ctx = nullptr; |
| 2218 |
|
| 2219 |
return (err == DB_END_OF_INDEX ? DB_SUCCESS : err); |
| 2220 |
} |
| 2221 |
|
| 2222 |
/** |
| 2223 |
Begin import, position the cursor on the first record. */ |
| 2224 |
void IndexPurge::open() UNIV_NOTHROW { |
| 2225 |
mtr_start(&m_mtr); |
| 2226 |
mtr_set_log_mode(&m_mtr, MTR_LOG_NO_REDO); |
| 2227 |
|
| 2228 |
m_pcur.open_at_side(true, m_index, BTR_MODIFY_LEAF, true, 0, &m_mtr); |
| 2229 |
} |
| 2230 |
|
| 2231 |
/** |
| 2232 |
Close the persistent curosr and commit the mini-transaction. */ |
| 2233 |
void IndexPurge::close() UNIV_NOTHROW { |
| 2234 |
m_pcur.close(); |
| 2235 |
mtr_commit(&m_mtr); |
| 2236 |
} |
| 2237 |
|
| 2238 |
/** |
| 2239 |
Position the cursor on the next record. |
| 2240 |
@return DB_SUCCESS or error code */ |
| 2241 |
dberr_t IndexPurge::next() UNIV_NOTHROW { |
| 2242 |
m_pcur.move_to_next_on_page(); |
| 2243 |
|
| 2244 |
/* When switching pages, commit the mini-transaction |
| 2245 |
in order to release the latch on the old page. */ |
| 2246 |
|
| 2247 |
if (!m_pcur.is_after_last_on_page()) { |
| 2248 |
return (DB_SUCCESS); |
| 2249 |
} else if (trx_is_interrupted(m_trx)) { |
| 2250 |
/* Check after every page because the check |
| 2251 |
is expensive. */ |
| 2252 |
return (DB_INTERRUPTED); |
| 2253 |
} |
| 2254 |
|
| 2255 |
m_pcur.store_position(&m_mtr); |
| 2256 |
|
| 2257 |
mtr_commit(&m_mtr); |
| 2258 |
|
| 2259 |
mtr_start(&m_mtr); |
| 2260 |
mtr_set_log_mode(&m_mtr, MTR_LOG_NO_REDO); |
| 2261 |
|
| 2262 |
m_pcur.restore_position(BTR_MODIFY_LEAF, &m_mtr, UT_LOCATION_HERE); |
| 2263 |
|
| 2264 |
if (m_pcur.move_to_next_user_rec(&m_mtr) != DB_SUCCESS) { |
| 2265 |
return (DB_END_OF_INDEX); |
| 2266 |
} |
| 2267 |
|
| 2268 |
return (DB_SUCCESS); |
| 2269 |
} |
| 2270 |
|
| 2271 |
/** |
| 2272 |
Store the persistent cursor position and reopen the |
| 2273 |
B-tree cursor in BTR_MODIFY_TREE mode, because the |
| 2274 |
tree structure may be changed during a pessimistic delete. */ |
| 2275 |
void IndexPurge::purge_pessimistic_delete() UNIV_NOTHROW { |
| 2276 |
dberr_t err; |
| 2277 |
|
| 2278 |
m_pcur.restore_position(BTR_MODIFY_TREE | BTR_LATCH_FOR_DELETE, &m_mtr, |
| 2279 |
UT_LOCATION_HERE); |
| 2280 |
|
| 2281 |
ut_ad(rec_get_deleted_flag(m_pcur.get_rec(), |
| 2282 |
dict_table_is_comp(m_index->table))); |
| 2283 |
|
| 2284 |
btr_cur_pessimistic_delete(&err, false, m_pcur.get_btr_cur(), 0, false, 0, 0, |
| 2285 |
0, &m_mtr, &m_pcur, nullptr); |
| 2286 |
|
| 2287 |
ut_a(err == DB_SUCCESS); |
| 2288 |
|
| 2289 |
/* Reopen the B-tree cursor in BTR_MODIFY_LEAF mode */ |
| 2290 |
mtr_commit(&m_mtr); |
| 2291 |
} |
| 2292 |
|
| 2293 |
/** |
| 2294 |
Purge delete-marked records. */ |
| 2295 |
void IndexPurge::purge() UNIV_NOTHROW { |
| 2296 |
m_pcur.store_position(&m_mtr); |
| 2297 |
|
| 2298 |
purge_pessimistic_delete(); |
| 2299 |
|
| 2300 |
mtr_start(&m_mtr); |
| 2301 |
mtr_set_log_mode(&m_mtr, MTR_LOG_NO_REDO); |
| 2302 |
|
| 2303 |
m_pcur.restore_position(BTR_MODIFY_LEAF, &m_mtr, UT_LOCATION_HERE); |
| 2304 |
} |
| 2305 |
|
| 2306 |
/** Constructor |
| 2307 |
@param cfg config of table being imported. |
| 2308 |
@param trx transaction covering the import */ |
| 2309 |
PageConverter::PageConverter(row_import *cfg, trx_t *trx) |
| 2310 |
: AbstractCallback(trx), |
| 2311 |
m_cfg(cfg), |
| 2312 |
m_page_zip_ptr(nullptr), |
| 2313 |
m_heap(nullptr) UNIV_NOTHROW { |
| 2314 |
m_index = m_cfg->m_indexes; |
| 2315 |
|
| 2316 |
m_current_lsn = log_sys->flushed_to_disk_lsn; |
| 2317 |
ut_a(m_current_lsn > 0); |
| 2318 |
|
| 2319 |
m_offsets = m_offsets_; |
| 2320 |
rec_offs_init(m_offsets_); |
| 2321 |
|
| 2322 |
m_cluster_index = m_cfg->m_table->first_index(); |
| 2323 |
} |
| 2324 |
|
| 2325 |
/** Adjust the BLOB reference for a single column that is externally stored |
| 2326 |
@param rec record to update |
| 2327 |
@param offsets column offsets for the record |
| 2328 |
@param i column ordinal value |
| 2329 |
@return DB_SUCCESS or error code */ |
| 2330 |
dberr_t PageConverter::adjust_cluster_index_blob_column(rec_t *rec, |
| 2331 |
const ulint *offsets, |
| 2332 |
ulint i) UNIV_NOTHROW { |
| 2333 |
ulint len; |
| 2334 |
byte *field; |
| 2335 |
|
| 2336 |
field = rec_get_nth_field(m_cluster_index, rec, offsets, i, &len); |
| 2337 |
|
| 2338 |
DBUG_EXECUTE_IF("ib_import_trigger_corruption_2", |
| 2339 |
len = BTR_EXTERN_FIELD_REF_SIZE - 1;); |
| 2340 |
|
| 2341 |
if (len < BTR_EXTERN_FIELD_REF_SIZE) { |
| 2342 |
ib_errf(m_trx->mysql_thd, IB_LOG_LEVEL_ERROR, ER_INNODB_INDEX_CORRUPT, |
| 2343 |
"Externally stored column(%lu) has a reference" |
| 2344 |
" length of %lu in the cluster index %s", |
| 2345 |
(ulong)i, (ulong)len, m_cluster_index->name()); |
| 2346 |
|
| 2347 |
return (DB_CORRUPTION); |
| 2348 |
} |
| 2349 |
|
| 2350 |
field += lob::BTR_EXTERN_SPACE_ID - BTR_EXTERN_FIELD_REF_SIZE + len; |
| 2351 |
|
| 2352 |
if (is_compressed_table()) { |
| 2353 |
mach_write_to_4(field, get_space_id()); |
| 2354 |
|
| 2355 |
ut_ad(m_index->m_srv_index != nullptr); |
| 2356 |
ut_ad(m_index->m_srv_index->is_clustered()); |
| 2357 |
|
| 2358 |
page_zip_write_blob_ptr(m_page_zip_ptr, rec, m_index->m_srv_index, offsets, |
| 2359 |
i, nullptr); |
| 2360 |
|
| 2361 |
} else { |
| 2362 |
mlog_write_ulint(field, get_space_id(), MLOG_4BYTES, nullptr); |
| 2363 |
} |
| 2364 |
|
| 2365 |
return (DB_SUCCESS); |
| 2366 |
} |
| 2367 |
|
| 2368 |
/** Adjusts the BLOB reference in the clustered index row for all externally |
| 2369 |
stored columns. |
| 2370 |
@param rec record to update |
| 2371 |
@param offsets column offsets for the record |
| 2372 |
@return DB_SUCCESS or error code */ |
| 2373 |
dberr_t PageConverter::adjust_cluster_index_blob_columns( |
| 2374 |
rec_t *rec, const ulint *offsets) UNIV_NOTHROW { |
| 2375 |
ut_ad(rec_offs_any_extern(offsets)); |
| 2376 |
|
| 2377 |
/* Adjust the space_id in the BLOB pointers. */ |
| 2378 |
|
| 2379 |
for (ulint i = 0; i < rec_offs_n_fields(offsets); ++i) { |
| 2380 |
/* Only if the column is stored "externally". */ |
| 2381 |
|
| 2382 |
if (rec_offs_nth_extern(m_cluster_index, offsets, i)) { |
| 2383 |
dberr_t err; |
| 2384 |
|
| 2385 |
err = adjust_cluster_index_blob_column(rec, offsets, i); |
| 2386 |
|
| 2387 |
if (err != DB_SUCCESS) { |
| 2388 |
return (err); |
| 2389 |
} |
| 2390 |
} |
| 2391 |
} |
| 2392 |
|
| 2393 |
return (DB_SUCCESS); |
| 2394 |
} |
| 2395 |
|
| 2396 |
/** In the clustered index, adjust BLOB pointers as needed. Also update the |
| 2397 |
BLOB reference, write the new space id. |
| 2398 |
@param rec record to update |
| 2399 |
@param offsets column offsets for the record |
| 2400 |
@return DB_SUCCESS or error code */ |
| 2401 |
dberr_t PageConverter::adjust_cluster_index_blob_ref( |
| 2402 |
rec_t *rec, const ulint *offsets) UNIV_NOTHROW { |
| 2403 |
if (rec_offs_any_extern(offsets)) { |
| 2404 |
dberr_t err; |
| 2405 |
|
| 2406 |
err = adjust_cluster_index_blob_columns(rec, offsets); |
| 2407 |
|
| 2408 |
if (err != DB_SUCCESS) { |
| 2409 |
return (err); |
| 2410 |
} |
| 2411 |
} |
| 2412 |
|
| 2413 |
return (DB_SUCCESS); |
| 2414 |
} |
| 2415 |
|
| 2416 |
bool PageConverter::purge() UNIV_NOTHROW { |
| 2417 |
const dict_index_t *index = m_index->m_srv_index; |
| 2418 |
|
| 2419 |
/* We can't have a page that is empty and not root. */ |
| 2420 |
if (m_rec_iter.remove(index, m_offsets)) { |
| 2421 |
++m_index->m_stats.m_n_purged; |
| 2422 |
|
| 2423 |
return (true); |
| 2424 |
} else { |
| 2425 |
++m_index->m_stats.m_n_purge_failed; |
| 2426 |
} |
| 2427 |
|
| 2428 |
return (false); |
| 2429 |
} |
| 2430 |
|
| 2431 |
dberr_t PageConverter::adjust_cluster_record( |
| 2432 |
const dict_index_t *index, rec_t *rec, const ulint *offsets) UNIV_NOTHROW { |
| 2433 |
dberr_t err; |
| 2434 |
|
| 2435 |
ut_ad(index->is_clustered()); |
| 2436 |
|
| 2437 |
if ((err = adjust_cluster_index_blob_ref(rec, offsets)) == DB_SUCCESS) { |
| 2438 |
/* Reset DB_TRX_ID and DB_ROLL_PTR. Normally, these fields |
| 2439 |
are only written in conjunction with other changes to the |
| 2440 |
record. */ |
| 2441 |
|
| 2442 |
row_upd_rec_sys_fields(rec, m_page_zip_ptr, index, m_offsets, m_trx, 0); |
| 2443 |
} |
| 2444 |
|
| 2445 |
return (err); |
| 2446 |
} |
| 2447 |
|
| 2448 |
/** Update the BLOB references and write UNDO log entries for |
| 2449 |
rows that can't be purged optimistically. |
| 2450 |
@param block block to update |
| 2451 |
@retval DB_SUCCESS or error code */ |
| 2452 |
dberr_t PageConverter::update_records(buf_block_t *block) UNIV_NOTHROW { |
| 2453 |
auto comp = dict_table_is_comp(m_cfg->m_table); |
| 2454 |
bool clust_index = (m_index->m_srv_index == m_cluster_index) || |
| 2455 |
dict_index_is_sdi(m_index->m_srv_index); |
| 2456 |
|
| 2457 |
/* This will also position the cursor on the first user record. */ |
| 2458 |
|
| 2459 |
m_rec_iter.open(block); |
| 2460 |
|
| 2461 |
while (!m_rec_iter.end()) { |
| 2462 |
rec_t *rec = m_rec_iter.current(); |
| 2463 |
|
| 2464 |
auto has_version = |
| 2465 |
(comp ? rec_new_is_versioned(rec) : rec_old_is_versioned(rec)); |
| 2466 |
|
| 2467 |
/* CFG file is required to process records having version */ |
| 2468 |
|
| 2469 |
if (m_cfg->m_missing && has_version) { |
| 2470 |
return (DB_SCHEMA_MISMATCH); |
| 2471 |
} |
| 2472 |
|
| 2473 |
auto deleted = rec_get_deleted_flag(rec, comp); |
| 2474 |
/* For the clustered index we have to adjust the BLOB |
| 2475 |
reference and the system fields irrespective of the |
| 2476 |
delete marked flag. The adjustment of delete marked |
| 2477 |
cluster records is required for purge to work later. */ |
| 2478 |
|
| 2479 |
if (deleted || clust_index) { |
| 2480 |
m_offsets = rec_get_offsets(rec, m_index->m_srv_index, m_offsets, |
| 2481 |
ULINT_UNDEFINED, UT_LOCATION_HERE, &m_heap); |
| 2482 |
} |
| 2483 |
|
| 2484 |
if (clust_index) { |
| 2485 |
dberr_t err = adjust_cluster_record(m_index->m_srv_index, rec, m_offsets); |
| 2486 |
|
| 2487 |
if (err != DB_SUCCESS) { |
| 2488 |
return (err); |
| 2489 |
} |
| 2490 |
} |
| 2491 |
|
| 2492 |
/* If it is a delete marked record then try an |
| 2493 |
optimistic delete. */ |
| 2494 |
|
| 2495 |
if (deleted) { |
| 2496 |
/* A successful purge will move the cursor to the |
| 2497 |
next record. */ |
| 2498 |
|
| 2499 |
if (!purge()) { |
| 2500 |
m_rec_iter.next(); |
| 2501 |
} |
| 2502 |
|
| 2503 |
++m_index->m_stats.m_n_deleted; |
| 2504 |
} else { |
| 2505 |
++m_index->m_stats.m_n_rows; |
| 2506 |
m_rec_iter.next(); |
| 2507 |
} |
| 2508 |
} |
| 2509 |
|
| 2510 |
return (DB_SUCCESS); |
| 2511 |
} |
| 2512 |
|
| 2513 |
/** Update the space, index id, trx id. |
| 2514 |
@return DB_SUCCESS or error code */ |
| 2515 |
dberr_t PageConverter::update_index_page(buf_block_t *block) UNIV_NOTHROW { |
| 2516 |
space_index_t id; |
| 2517 |
buf_frame_t *page = block->frame; |
| 2518 |
|
| 2519 |
if (is_free(block->page.id.page_no())) { |
| 2520 |
return (DB_SUCCESS); |
| 2521 |
} else if ((id = btr_page_get_index_id(page)) != m_index->m_id) { |
| 2522 |
row_index_t *index = find_index(id); |
| 2523 |
|
| 2524 |
if (index == nullptr) { |
| 2525 |
m_index = nullptr; |
| 2526 |
return (DB_CORRUPTION); |
| 2527 |
} |
| 2528 |
|
| 2529 |
/* Update current index */ |
| 2530 |
m_index = index; |
| 2531 |
} |
| 2532 |
|
| 2533 |
/* If the .cfg file is missing and there is an index mismatch |
| 2534 |
then ignore the error. */ |
| 2535 |
if (m_cfg->m_missing && |
| 2536 |
(m_index == nullptr || m_index->m_srv_index == nullptr)) { |
| 2537 |
return (DB_SUCCESS); |
| 2538 |
} |
| 2539 |
|
| 2540 |
#ifdef UNIV_ZIP_DEBUG |
| 2541 |
ut_a(!is_compressed_table() || |
| 2542 |
page_zip_validate(m_page_zip_ptr, page, m_index->m_srv_index)); |
| 2543 |
#endif /* UNIV_ZIP_DEBUG */ |
| 2544 |
|
| 2545 |
/* This has to be written to uncompressed index header. Set it to |
| 2546 |
the current index id. */ |
| 2547 |
btr_page_set_index_id(page, m_page_zip_ptr, m_index->m_srv_index->id, |
| 2548 |
nullptr); |
| 2549 |
|
| 2550 |
page_set_max_trx_id(block, m_page_zip_ptr, m_trx->id, nullptr); |
| 2551 |
|
| 2552 |
if (page_is_empty(block->frame)) { |
| 2553 |
/* Only a root page can be empty. */ |
| 2554 |
if (!is_root_page(block->frame)) { |
| 2555 |
// TODO: We should relax this and skip secondary |
| 2556 |
// indexes. Mark them as corrupt because they can |
| 2557 |
// always be rebuilt. |
| 2558 |
return (DB_CORRUPTION); |
| 2559 |
} |
| 2560 |
|
| 2561 |
return (DB_SUCCESS); |
| 2562 |
} |
| 2563 |
|
| 2564 |
if (!page_is_leaf(block->frame)) { |
| 2565 |
return (DB_SUCCESS); |
| 2566 |
} |
| 2567 |
|
| 2568 |
return (update_records(block)); |
| 2569 |
} |
| 2570 |
|
| 2571 |
/** Validate the space flags and update tablespace header page. |
| 2572 |
@param block block read from file, not from the buffer pool. |
| 2573 |
@retval DB_SUCCESS or error code */ |
| 2574 |
dberr_t PageConverter::update_header(buf_block_t *block) UNIV_NOTHROW { |
| 2575 |
/* Check for valid header */ |
| 2576 |
switch (fsp_header_get_space_id(get_frame(block))) { |
| 2577 |
case 0: |
| 2578 |
return (DB_CORRUPTION); |
| 2579 |
case SPACE_UNKNOWN: |
| 2580 |
ib::warn(ER_IB_MSG_941) << "Space id check in the header failed: ignored"; |
| 2581 |
} |
| 2582 |
|
| 2583 |
uint32_t space_flags = fsp_header_get_flags(get_frame(block)); |
| 2584 |
|
| 2585 |
if (!fsp_flags_is_valid(space_flags)) { |
| 2586 |
ib::error(ER_IB_MSG_942) << "Unsupported tablespace format " << space_flags; |
| 2587 |
|
| 2588 |
return (DB_UNSUPPORTED); |
| 2589 |
} |
| 2590 |
|
| 2591 |
/* Write space_id to the tablespace header, page 0. */ |
| 2592 |
fsp_header_set_field(get_frame(block), FSP_SPACE_ID, get_space_id()); |
| 2593 |
|
| 2594 |
/* This is on every page in the tablespace. */ |
| 2595 |
mach_write_to_4(get_frame(block) + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID, |
| 2596 |
get_space_id()); |
| 2597 |
|
| 2598 |
return (DB_SUCCESS); |
| 2599 |
} |
| 2600 |
|
| 2601 |
/** Update the page, set the space id, max trx id and index id. |
| 2602 |
@param block block read from file |
| 2603 |
@param page_type type of the page |
| 2604 |
@retval DB_SUCCESS or error code */ |
| 2605 |
dberr_t PageConverter::update_page(buf_block_t *block, |
| 2606 |
ulint &page_type) UNIV_NOTHROW { |
| 2607 |
dberr_t err = DB_SUCCESS; |
| 2608 |
|
| 2609 |
switch (page_type = fil_page_get_type(get_frame(block))) { |
| 2610 |
case FIL_PAGE_TYPE_FSP_HDR: |
| 2611 |
/* Work directly on the uncompressed page headers. */ |
| 2612 |
ut_a(block->page.id.page_no() == 0); |
| 2613 |
return (update_header(block)); |
| 2614 |
|
| 2615 |
case FIL_PAGE_INDEX: |
| 2616 |
case FIL_PAGE_RTREE: |
| 2617 |
case FIL_PAGE_SDI: |
| 2618 |
/* We need to decompress the contents into block->frame |
| 2619 |
before we can do any thing with Btree pages. */ |
| 2620 |
|
| 2621 |
if (is_compressed_table() && !buf_zip_decompress(block, true)) { |
| 2622 |
return (DB_CORRUPTION); |
| 2623 |
} |
| 2624 |
|
| 2625 |
/* This is on every page in the tablespace. */ |
| 2626 |
mach_write_to_4(get_frame(block) + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID, |
| 2627 |
get_space_id()); |
| 2628 |
|
| 2629 |
/* Only update the Btree nodes. */ |
| 2630 |
return (update_index_page(block)); |
| 2631 |
|
| 2632 |
case FIL_PAGE_TYPE_SYS: |
| 2633 |
/* This is page 0 in the system tablespace. */ |
| 2634 |
return (DB_CORRUPTION); |
| 2635 |
|
| 2636 |
case FIL_PAGE_TYPE_LOB_FIRST: { |
| 2637 |
lob::first_page_t first_page(block); |
| 2638 |
first_page.import(m_trx->id); |
| 2639 |
first_page.set_space_id_no_redo(get_space_id()); |
| 2640 |
return (err); |
| 2641 |
} |
| 2642 |
|
| 2643 |
case FIL_PAGE_TYPE_LOB_INDEX: { |
| 2644 |
lob::node_page_t node_page(block); |
| 2645 |
node_page.import(m_trx->id); |
| 2646 |
node_page.set_space_id_no_redo(get_space_id()); |
| 2647 |
return (err); |
| 2648 |
} |
| 2649 |
|
| 2650 |
case FIL_PAGE_TYPE_LOB_DATA: { |
| 2651 |
lob::data_page_t data_page(block); |
| 2652 |
data_page.set_trx_id_no_redo(m_trx->id); |
| 2653 |
data_page.set_space_id_no_redo(get_space_id()); |
| 2654 |
return (err); |
| 2655 |
} |
| 2656 |
|
| 2657 |
case FIL_PAGE_TYPE_ZLOB_FIRST: { |
| 2658 |
dict_index_t *index = const_cast<dict_index_t *>(m_index->m_srv_index); |
| 2659 |
lob::z_first_page_t first_page(block, nullptr, index); |
| 2660 |
first_page.import(m_trx->id); |
| 2661 |
byte *ptr = get_frame(block) + FIL_PAGE_SPACE_ID; |
| 2662 |
mach_write_to_4(ptr, get_space_id()); |
| 2663 |
return (err); |
| 2664 |
} |
| 2665 |
|
| 2666 |
case FIL_PAGE_TYPE_ZLOB_DATA: { |
| 2667 |
lob::z_data_page_t dpage(block); |
| 2668 |
dpage.set_trx_id_no_redo(m_trx->id); |
| 2669 |
byte *ptr = get_frame(block) + FIL_PAGE_SPACE_ID; |
| 2670 |
mach_write_to_4(ptr, get_space_id()); |
| 2671 |
return (err); |
| 2672 |
} |
| 2673 |
|
| 2674 |
case FIL_PAGE_TYPE_ZLOB_INDEX: { |
| 2675 |
lob::z_index_page_t ipage( |
| 2676 |
block, const_cast<dict_index_t *>(m_index->m_srv_index)); |
| 2677 |
ipage.import(m_trx->id); |
| 2678 |
byte *ptr = get_frame(block) + FIL_PAGE_SPACE_ID; |
| 2679 |
mach_write_to_4(ptr, get_space_id()); |
| 2680 |
return (err); |
| 2681 |
} |
| 2682 |
|
| 2683 |
case FIL_PAGE_TYPE_ZLOB_FRAG: { |
| 2684 |
byte *ptr = get_frame(block) + FIL_PAGE_SPACE_ID; |
| 2685 |
mach_write_to_4(ptr, get_space_id()); |
| 2686 |
return (err); |
| 2687 |
} |
| 2688 |
|
| 2689 |
case FIL_PAGE_TYPE_ZLOB_FRAG_ENTRY: { |
| 2690 |
byte *ptr = get_frame(block) + FIL_PAGE_SPACE_ID; |
| 2691 |
mach_write_to_4(ptr, get_space_id()); |
| 2692 |
return (err); |
| 2693 |
} |
| 2694 |
|
| 2695 |
case FIL_PAGE_TYPE_XDES: |
| 2696 |
err = set_current_xdes(block->page.id.page_no(), get_frame(block)); |
| 2697 |
[[fallthrough]]; |
| 2698 |
case FIL_PAGE_INODE: |
| 2699 |
case FIL_PAGE_TYPE_TRX_SYS: |
| 2700 |
case FIL_PAGE_IBUF_FREE_LIST: |
| 2701 |
case FIL_PAGE_TYPE_ALLOCATED: |
| 2702 |
case FIL_PAGE_IBUF_BITMAP: |
| 2703 |
case FIL_PAGE_TYPE_BLOB: |
| 2704 |
case FIL_PAGE_TYPE_ZBLOB: |
| 2705 |
case FIL_PAGE_TYPE_ZBLOB2: |
| 2706 |
case FIL_PAGE_SDI_BLOB: |
| 2707 |
case FIL_PAGE_SDI_ZBLOB: |
| 2708 |
case FIL_PAGE_TYPE_RSEG_ARRAY: |
| 2709 |
|
| 2710 |
/* Work directly on the uncompressed page headers. */ |
| 2711 |
/* This is on every page in the tablespace. */ |
| 2712 |
mach_write_to_4(get_frame(block) + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID, |
| 2713 |
get_space_id()); |
| 2714 |
|
| 2715 |
return (err); |
| 2716 |
} |
| 2717 |
|
| 2718 |
ib::warn(ER_IB_MSG_943) << "Unknown page type (" << page_type << ")"; |
| 2719 |
|
| 2720 |
return (DB_CORRUPTION); |
| 2721 |
} |
| 2722 |
|
| 2723 |
/** Validate the page, check for corruption. |
| 2724 |
@param offset physical offset within file. |
| 2725 |
@param block page read from file. |
| 2726 |
@return 0 on success, 1 if all zero, 2 if corrupted */ |
| 2727 |
PageConverter::import_page_status_t PageConverter::validate( |
| 2728 |
os_offset_t offset, buf_block_t *block) UNIV_NOTHROW { |
| 2729 |
buf_frame_t *page = get_frame(block); |
| 2730 |
|
| 2731 |
/* Check that the page number corresponds to the offset in |
| 2732 |
the file. Flag as corrupt if it doesn't. Disable the check |
| 2733 |
for LSN in buf_page_is_corrupted() */ |
| 2734 |
|
| 2735 |
BlockReporter reporter(false, page, get_page_size(), |
| 2736 |
fsp_is_checksum_disabled(block->page.id.space())); |
| 2737 |
|
| 2738 |
if (reporter.is_corrupted() || |
| 2739 |
(page_get_page_no(page) != offset / m_page_size.physical() && |
| 2740 |
page_get_page_no(page) != 0)) { |
| 2741 |
return (IMPORT_PAGE_STATUS_CORRUPTED); |
| 2742 |
|
| 2743 |
} else if (offset > 0 && page_get_page_no(page) == 0) { |
| 2744 |
/* The page is all zero: do nothing. We already checked |
| 2745 |
for all NULs in buf_page_is_corrupted() */ |
| 2746 |
return (IMPORT_PAGE_STATUS_ALL_ZERO); |
| 2747 |
} |
| 2748 |
|
| 2749 |
return (IMPORT_PAGE_STATUS_OK); |
| 2750 |
} |
| 2751 |
|
| 2752 |
/** Called for every page in the tablespace. If the page was not |
| 2753 |
updated then its state must be set to BUF_PAGE_NOT_USED. |
| 2754 |
@param offset physical offset within the file |
| 2755 |
@param block block read from file, note it is not from the buffer pool |
| 2756 |
@retval DB_SUCCESS or error code. */ |
| 2757 |
dberr_t PageConverter::operator()(os_offset_t offset, |
| 2758 |
buf_block_t *block) UNIV_NOTHROW { |
| 2759 |
ulint page_type; |
| 2760 |
dberr_t err = DB_SUCCESS; |
| 2761 |
|
| 2762 |
if ((err = periodic_check()) != DB_SUCCESS) { |
| 2763 |
return (err); |
| 2764 |
} |
| 2765 |
|
| 2766 |
if (is_compressed_table()) { |
| 2767 |
m_page_zip_ptr = &block->page.zip; |
| 2768 |
} else { |
| 2769 |
ut_ad(m_page_zip_ptr == nullptr); |
| 2770 |
} |
| 2771 |
|
| 2772 |
switch (validate(offset, block)) { |
| 2773 |
case IMPORT_PAGE_STATUS_OK: |
| 2774 |
|
| 2775 |
/* We have to decompress the compressed pages before |
| 2776 |
we can work on them */ |
| 2777 |
|
| 2778 |
if ((err = update_page(block, page_type)) != DB_SUCCESS) { |
| 2779 |
return (err); |
| 2780 |
} |
| 2781 |
|
| 2782 |
/* Note: For compressed pages this function will write to the |
| 2783 |
zip descriptor and for uncompressed pages it will write to |
| 2784 |
page (ie. the block->frame). Therefore the caller should write |
| 2785 |
out the descriptor contents and not block->frame for compressed |
| 2786 |
pages. */ |
| 2787 |
|
| 2788 |
if (!is_compressed_table() || fil_page_type_is_index(page_type)) { |
| 2789 |
buf_flush_init_for_writing( |
| 2790 |
!is_compressed_table() ? block : nullptr, |
| 2791 |
!is_compressed_table() ? block->frame : block->page.zip.data, |
| 2792 |
!is_compressed_table() ? nullptr : m_page_zip_ptr, m_current_lsn, |
| 2793 |
fsp_is_checksum_disabled(block->page.id.space()), |
| 2794 |
true /* skip_lsn_check */); |
| 2795 |
} else { |
| 2796 |
/* Calculate and update the checksum of non-btree |
| 2797 |
pages for compressed tables explicitly here. */ |
| 2798 |
|
| 2799 |
buf_flush_update_zip_checksum(get_frame(block), |
| 2800 |
get_page_size().physical(), m_current_lsn, |
| 2801 |
true /* skip_lsn_check */); |
| 2802 |
} |
| 2803 |
|
| 2804 |
break; |
| 2805 |
|
| 2806 |
case IMPORT_PAGE_STATUS_ALL_ZERO: |
| 2807 |
/* The page is all zero: leave it as is. */ |
| 2808 |
break; |
| 2809 |
|
| 2810 |
case IMPORT_PAGE_STATUS_CORRUPTED: |
| 2811 |
|
| 2812 |
ib::warn(ER_IB_MSG_944) |
| 2813 |
<< "Page " << (offset / m_page_size.physical()) << " at offset " |
| 2814 |
<< offset << " looks corrupted in file " << m_filepath; |
| 2815 |
|
| 2816 |
return (DB_CORRUPTION); |
| 2817 |
} |
| 2818 |
|
| 2819 |
return (err); |
| 2820 |
} |
| 2821 |
|
| 2822 |
/** Clean up after import tablespace failure, this function will acquire |
| 2823 |
the dictionary latches on behalf of the transaction if the transaction |
| 2824 |
hasn't already acquired them. */ |
| 2825 |
static void row_import_discard_changes( |
| 2826 |
row_prebuilt_t *prebuilt, /*!< in/out: prebuilt from handler */ |
| 2827 |
trx_t *trx, /*!< in/out: transaction for import */ |
| 2828 |
dberr_t err) /*!< in: error code */ |
| 2829 |
{ |
| 2830 |
dict_table_t *table = prebuilt->table; |
| 2831 |
|
| 2832 |
ut_a(err != DB_SUCCESS); |
| 2833 |
|
| 2834 |
prebuilt->trx->error_index = nullptr; |
| 2835 |
|
| 2836 |
ib::info(ER_IB_MSG_945) << "Failed to import tablespace of table '" |
| 2837 |
<< prebuilt->table->name.m_name |
| 2838 |
<< (err == DB_UNSUPPORTED |
| 2839 |
? "': the CFG file version is " |
| 2840 |
: "': ") |
| 2841 |
<< ut_strerr(err); |
| 2842 |
|
| 2843 |
if (trx->dict_operation_lock_mode != RW_X_LATCH) { |
| 2844 |
ut_a(trx->dict_operation_lock_mode == 0); |
| 2845 |
row_mysql_lock_data_dictionary(trx, UT_LOCATION_HERE); |
| 2846 |
} |
| 2847 |
|
| 2848 |
ut_a(trx->dict_operation_lock_mode == RW_X_LATCH); |
| 2849 |
|
| 2850 |
/* Since we update the index root page numbers on disk after |
| 2851 |
we've done a successful import. The table will not be loadable. |
| 2852 |
However, we need to ensure that the in memory root page numbers |
| 2853 |
are reset to "NULL". We assume these indexes were not added to AHI, otherwise |
| 2854 |
the btr_search_drop_page_hash_index() will fail for these indexes. */ |
| 2855 |
|
| 2856 |
for (auto index : table->indexes) { |
| 2857 |
index->page = FIL_NULL; |
| 2858 |
index->space = FIL_NULL; |
| 2859 |
} |
| 2860 |
|
| 2861 |
table->ibd_file_missing = true; |
| 2862 |
|
| 2863 |
err = fil_close_tablespace(table->space); |
| 2864 |
ut_a(err == DB_SUCCESS || err == DB_TABLESPACE_NOT_FOUND); |
| 2865 |
} |
| 2866 |
|
| 2867 |
/** Clean up after import tablespace. */ |
| 2868 |
[[nodiscard]] static dberr_t row_import_cleanup( |
| 2869 |
row_prebuilt_t *prebuilt, /*!< in/out: prebuilt from handler */ |
| 2870 |
trx_t *trx, /*!< in/out: transaction for import */ |
| 2871 |
dberr_t err) /*!< in: error code */ |
| 2872 |
{ |
| 2873 |
ut_a(prebuilt->trx != trx); |
| 2874 |
|
| 2875 |
if (err != DB_SUCCESS) { |
| 2876 |
row_import_discard_changes(prebuilt, trx, err); |
| 2877 |
} |
| 2878 |
|
| 2879 |
ut_a(trx->dict_operation_lock_mode == RW_X_LATCH); |
| 2880 |
|
| 2881 |
DBUG_EXECUTE_IF("ib_import_before_commit_crash", DBUG_SUICIDE();); |
| 2882 |
|
| 2883 |
trx_commit_for_mysql(trx); |
| 2884 |
|
| 2885 |
row_mysql_unlock_data_dictionary(trx); |
| 2886 |
|
| 2887 |
trx_free_for_mysql(trx); |
| 2888 |
|
| 2889 |
prebuilt->trx->op_info = ""; |
| 2890 |
|
| 2891 |
DBUG_EXECUTE_IF("ib_import_before_checkpoint_crash", DBUG_SUICIDE();); |
| 2892 |
|
| 2893 |
log_make_latest_checkpoint(); |
| 2894 |
|
| 2895 |
return (err); |
| 2896 |
} |
| 2897 |
|
| 2898 |
/** Report error during tablespace import. */ |
| 2899 |
[[nodiscard]] static dberr_t row_import_error( |
| 2900 |
row_prebuilt_t *prebuilt, /*!< in/out: prebuilt from handler */ |
| 2901 |
trx_t *trx, /*!< in/out: transaction for import */ |
| 2902 |
dberr_t err) /*!< in: error code */ |
| 2903 |
{ |
| 2904 |
if (!trx_is_interrupted(trx)) { |
| 2905 |
char table_name[MAX_FULL_NAME_LEN + 1]; |
| 2906 |
|
| 2907 |
innobase_format_name(table_name, sizeof(table_name), |
| 2908 |
prebuilt->table->name.m_name); |
| 2909 |
|
| 2910 |
ib_senderrf(trx->mysql_thd, IB_LOG_LEVEL_WARN, ER_INNODB_IMPORT_ERROR, |
| 2911 |
table_name, (ulong)err, ut_strerr(err)); |
| 2912 |
} |
| 2913 |
|
| 2914 |
return (row_import_cleanup(prebuilt, trx, err)); |
| 2915 |
} |
| 2916 |
|
| 2917 |
/** Adjust the root page index node and leaf node segment headers, update |
| 2918 |
with the new space id. For all the table's secondary indexes. |
| 2919 |
@return error code */ |
| 2920 |
[[nodiscard]] static dberr_t row_import_adjust_root_pages_of_secondary_indexes( |
| 2921 |
trx_t *trx, /*!< in: transaction used for |
| 2922 |
the import */ |
| 2923 |
dict_table_t *table, /*!< in: table the indexes |
| 2924 |
belong to */ |
| 2925 |
const row_import &cfg) /*!< Import context */ |
| 2926 |
{ |
| 2927 |
dict_index_t *index; |
| 2928 |
ulint n_rows_in_table; |
| 2929 |
dberr_t err = DB_SUCCESS; |
| 2930 |
|
| 2931 |
/* Skip the clustered index. */ |
| 2932 |
index = table->first_index(); |
| 2933 |
|
| 2934 |
n_rows_in_table = cfg.get_n_rows(index->name); |
| 2935 |
|
| 2936 |
/* Adjust the root pages of the secondary indexes only. */ |
| 2937 |
while ((index = index->next()) != nullptr) { |
| 2938 |
ut_a(!index->is_clustered()); |
| 2939 |
|
| 2940 |
if (!index->is_corrupted() && index->space != FIL_NULL && |
| 2941 |
index->page != FIL_NULL) { |
| 2942 |
/* Update the Btree segment headers for index node and |
| 2943 |
leaf nodes in the root page. Set the new space id. */ |
| 2944 |
|
| 2945 |
err = btr_root_adjust_on_import(index); |
| 2946 |
} else { |
| 2947 |
ib::warn(ER_IB_MSG_946) << "Skip adjustment of root pages for" |
| 2948 |
" index " |
| 2949 |
<< index->name << "."; |
| 2950 |
|
| 2951 |
err = DB_CORRUPTION; |
| 2952 |
} |
| 2953 |
|
| 2954 |
if (err != DB_SUCCESS) { |
| 2955 |
if (index->type & DICT_CLUSTERED) { |
| 2956 |
break; |
| 2957 |
} |
| 2958 |
|
| 2959 |
ib_errf(trx->mysql_thd, IB_LOG_LEVEL_WARN, ER_INNODB_INDEX_CORRUPT, |
| 2960 |
"Index %s not found or corrupt," |
| 2961 |
" you should recreate this index.", |
| 2962 |
index->name()); |
| 2963 |
|
| 2964 |
/* Do not bail out, so that the data |
| 2965 |
can be recovered. */ |
| 2966 |
|
| 2967 |
err = DB_SUCCESS; |
| 2968 |
dict_set_corrupted(index); |
| 2969 |
continue; |
| 2970 |
} |
| 2971 |
|
| 2972 |
/* If we failed to purge any records in the index then |
| 2973 |
do it the hard way. |
| 2974 |
|
| 2975 |
TODO: We can do this in the first pass by generating UNDO log |
| 2976 |
records for the failed rows. */ |
| 2977 |
|
| 2978 |
if (!cfg.requires_purge(index->name)) { |
| 2979 |
continue; |
| 2980 |
} |
| 2981 |
|
| 2982 |
IndexPurge purge(trx, index); |
| 2983 |
|
| 2984 |
trx->op_info = "secondary: purge delete marked records"; |
| 2985 |
|
| 2986 |
err = purge.garbage_collect(); |
| 2987 |
|
| 2988 |
trx->op_info = ""; |
| 2989 |
|
| 2990 |
if (err != DB_SUCCESS) { |
| 2991 |
break; |
| 2992 |
} else if (purge.get_n_rows() != n_rows_in_table) { |
| 2993 |
ib_errf(trx->mysql_thd, IB_LOG_LEVEL_WARN, ER_INNODB_INDEX_CORRUPT, |
| 2994 |
"Index %s contains %lu entries," |
| 2995 |
" should be %lu, you should recreate" |
| 2996 |
" this index.", |
| 2997 |
index->name(), (ulong)purge.get_n_rows(), (ulong)n_rows_in_table); |
| 2998 |
|
| 2999 |
dict_set_corrupted(index); |
| 3000 |
|
| 3001 |
/* Do not bail out, so that the data |
| 3002 |
can be recovered. */ |
| 3003 |
|
| 3004 |
err = DB_SUCCESS; |
| 3005 |
} |
| 3006 |
} |
| 3007 |
|
| 3008 |
return (err); |
| 3009 |
} |
| 3010 |
|
| 3011 |
/** Ensure that dict_sys->row_id exceeds SELECT MAX(DB_ROW_ID). |
| 3012 |
@return error code */ |
| 3013 |
[[nodiscard]] static dberr_t row_import_set_sys_max_row_id( |
| 3014 |
row_prebuilt_t *prebuilt, /*!< in/out: prebuilt from |
| 3015 |
handler */ |
| 3016 |
dict_table_t *table) /*!< in: table to import */ |
| 3017 |
{ |
| 3018 |
dberr_t err; |
| 3019 |
const rec_t *rec; |
| 3020 |
mtr_t mtr; |
| 3021 |
btr_pcur_t pcur; |
| 3022 |
row_id_t row_id = 0; |
| 3023 |
dict_index_t *index; |
| 3024 |
|
| 3025 |
index = table->first_index(); |
| 3026 |
ut_a(index->is_clustered()); |
| 3027 |
|
| 3028 |
mtr_start(&mtr); |
| 3029 |
|
| 3030 |
mtr_set_log_mode(&mtr, MTR_LOG_NO_REDO); |
| 3031 |
|
| 3032 |
pcur.open_at_side(false, // High end |
| 3033 |
index, BTR_SEARCH_LEAF, |
| 3034 |
true, // Init cursor |
| 3035 |
0, // Leaf level |
| 3036 |
&mtr); |
| 3037 |
|
| 3038 |
pcur.move_to_prev_on_page(); |
| 3039 |
rec = pcur.get_rec(); |
| 3040 |
|
| 3041 |
/* Check for empty table. */ |
| 3042 |
if (!page_rec_is_infimum(rec)) { |
| 3043 |
ulint len; |
| 3044 |
const byte *field; |
| 3045 |
mem_heap_t *heap = nullptr; |
| 3046 |
ulint offsets_[1 + REC_OFFS_HEADER_SIZE]; |
| 3047 |
ulint *offsets; |
| 3048 |
|
| 3049 |
rec_offs_init(offsets_); |
| 3050 |
|
| 3051 |
offsets = rec_get_offsets(rec, index, offsets_, ULINT_UNDEFINED, |
| 3052 |
UT_LOCATION_HERE, &heap); |
| 3053 |
|
| 3054 |
field = rec_get_nth_field(index, rec, offsets, |
| 3055 |
index->get_sys_col_pos(DATA_ROW_ID), &len); |
| 3056 |
|
| 3057 |
if (len == DATA_ROW_ID_LEN) { |
| 3058 |
row_id = mach_read_from_6(field); |
| 3059 |
err = DB_SUCCESS; |
| 3060 |
} else { |
| 3061 |
err = DB_CORRUPTION; |
| 3062 |
} |
| 3063 |
|
| 3064 |
if (heap != nullptr) { |
| 3065 |
mem_heap_free(heap); |
| 3066 |
} |
| 3067 |
} else { |
| 3068 |
/* The table is empty. */ |
| 3069 |
err = DB_SUCCESS; |
| 3070 |
} |
| 3071 |
|
| 3072 |
pcur.close(); |
| 3073 |
mtr_commit(&mtr); |
| 3074 |
|
| 3075 |
DBUG_EXECUTE_IF("ib_import_set_max_rowid_failure", err = DB_CORRUPTION;); |
| 3076 |
|
| 3077 |
if (err != DB_SUCCESS) { |
| 3078 |
ib_errf(prebuilt->trx->mysql_thd, IB_LOG_LEVEL_WARN, |
| 3079 |
ER_INNODB_INDEX_CORRUPT, |
| 3080 |
"Index `%s` corruption detected, invalid DB_ROW_ID" |
| 3081 |
" in index.", |
| 3082 |
index->name()); |
| 3083 |
|
| 3084 |
return (err); |
| 3085 |
|
| 3086 |
} else if (row_id > 0) { |
| 3087 |
/* Update the system row id if the imported index row id is |
| 3088 |
greater than the max system row id. */ |
| 3089 |
|
| 3090 |
dict_sys_mutex_enter(); |
| 3091 |
|
| 3092 |
if (row_id >= dict_sys->row_id) { |
| 3093 |
dict_sys->row_id = row_id + 1; |
| 3094 |
dict_hdr_flush_row_id(); |
| 3095 |
} |
| 3096 |
|
| 3097 |
dict_sys_mutex_exit(); |
| 3098 |
} |
| 3099 |
|
| 3100 |
return (DB_SUCCESS); |
| 3101 |
} |
| 3102 |
|
| 3103 |
/** Read the a string from the meta data file. |
| 3104 |
@return DB_SUCCESS or error code. */ |
| 3105 |
static dberr_t row_import_cfg_read_string( |
| 3106 |
FILE *file, /*!< in/out: File to read from */ |
| 3107 |
byte *ptr, /*!< out: string to read */ |
| 3108 |
ulint max_len) /*!< in: maximum length of the output |
| 3109 |
buffer in bytes */ |
| 3110 |
{ |
| 3111 |
DBUG_EXECUTE_IF("ib_import_string_read_error", errno = EINVAL; |
| 3112 |
return (DB_IO_ERROR);); |
| 3113 |
|
| 3114 |
ulint len = 0; |
| 3115 |
|
| 3116 |
while (!feof(file)) { |
| 3117 |
int ch = fgetc(file); |
| 3118 |
|
| 3119 |
if (ch == EOF) { |
| 3120 |
break; |
| 3121 |
} else if (ch != 0) { |
| 3122 |
if (len < max_len) { |
| 3123 |
ptr[len++] = ch; |
| 3124 |
} else { |
| 3125 |
break; |
| 3126 |
} |
| 3127 |
/* max_len includes the NUL byte */ |
| 3128 |
} else if (len != max_len - 1) { |
| 3129 |
break; |
| 3130 |
} else { |
| 3131 |
ptr[len] = 0; |
| 3132 |
return (DB_SUCCESS); |
| 3133 |
} |
| 3134 |
} |
| 3135 |
|
| 3136 |
errno = EINVAL; |
| 3137 |
|
| 3138 |
return (DB_IO_ERROR); |
| 3139 |
} |
| 3140 |
|
| 3141 |
/** Write the meta data (index user fields) config file. |
| 3142 |
@return DB_SUCCESS or error code. */ |
| 3143 |
[[nodiscard]] static dberr_t row_import_cfg_read_index_fields( |
| 3144 |
FILE *file, /*!< in: file to write to */ |
| 3145 |
THD *thd, /*!< in/out: session */ |
| 3146 |
row_index_t *index, /*!< Index being read in */ |
| 3147 |
row_import *cfg) /*!< in/out: meta-data read */ |
| 3148 |
{ |
| 3149 |
/* v4 row will have prefix_len, fixed_len, is_ascending, name length */ |
| 3150 |
byte row[sizeof(uint32_t) * 4]; |
| 3151 |
size_t row_len = sizeof(row); |
| 3152 |
if (cfg->m_version < IB_EXPORT_CFG_VERSION_V4) { |
| 3153 |
/* v3 row will have prefix_len, fixed_len, name length */ |
| 3154 |
row_len = sizeof(uint32_t) * 3; |
| 3155 |
} |
| 3156 |
|
| 3157 |
ulint n_fields = index->m_n_fields; |
| 3158 |
|
| 3159 |
index->m_fields = ut::new_arr_withkey<dict_field_t>(UT_NEW_THIS_FILE_PSI_KEY, |
| 3160 |
ut::Count{n_fields}); |
| 3161 |
|
| 3162 |
/* Trigger OOM */ |
| 3163 |
DBUG_EXECUTE_IF("ib_import_OOM_4", ut::delete_arr(index->m_fields); |
| 3164 |
index->m_fields = nullptr;); |
| 3165 |
|
| 3166 |
if (index->m_fields == nullptr) { |
| 3167 |
return (DB_OUT_OF_MEMORY); |
| 3168 |
} |
| 3169 |
|
| 3170 |
dict_field_t *field = index->m_fields; |
| 3171 |
|
| 3172 |
std::uninitialized_fill_n(field, n_fields, dict_field_t()); |
| 3173 |
|
| 3174 |
for (ulint i = 0; i < n_fields; ++i, ++field) { |
| 3175 |
byte *ptr = row; |
| 3176 |
|
| 3177 |
/* Trigger EOF */ |
| 3178 |
DBUG_EXECUTE_IF("ib_import_io_read_error_1", |
| 3179 |
(void)fseek(file, 0L, SEEK_END);); |
| 3180 |
|
| 3181 |
if (fread(row, 1, row_len, file) != row_len) { |
| 3182 |
ib_senderrf(thd, IB_LOG_LEVEL_ERROR, ER_IO_READ_ERROR, errno, |
| 3183 |
strerror(errno), "while reading index fields."); |
| 3184 |
|
| 3185 |
return (DB_IO_ERROR); |
| 3186 |
} |
| 3187 |
|
| 3188 |
field->prefix_len = mach_read_from_4(ptr); |
| 3189 |
ptr += sizeof(uint32_t); |
| 3190 |
|
| 3191 |
field->fixed_len = mach_read_from_4(ptr); |
| 3192 |
ptr += sizeof(uint32_t); |
| 3193 |
|
| 3194 |
if (cfg->m_version >= IB_EXPORT_CFG_VERSION_V4) { |
| 3195 |
field->is_ascending = mach_read_from_4(ptr); |
| 3196 |
ptr += sizeof(uint32_t); |
| 3197 |
} else { |
| 3198 |
/* Previous to CFG version 4 the DESC key was not recorded. |
| 3199 |
Assume the index column is ascending. |
| 3200 |
This flag became available in v8.0. */ |
| 3201 |
field->is_ascending = true; |
| 3202 |
} |
| 3203 |
|
| 3204 |
/* Include the NUL byte in the length. */ |
| 3205 |
ulint len = mach_read_from_4(ptr); |
| 3206 |
|
| 3207 |
byte *name = |
| 3208 |
ut::new_arr_withkey<byte>(UT_NEW_THIS_FILE_PSI_KEY, ut::Count{len}); |
| 3209 |
|
| 3210 |
/* Trigger OOM */ |
| 3211 |
DBUG_EXECUTE_IF("ib_import_OOM_5", ut::delete_arr(name); name = nullptr;); |
| 3212 |
|
| 3213 |
if (name == nullptr) { |
| 3214 |
return (DB_OUT_OF_MEMORY); |
| 3215 |
} |
| 3216 |
|
| 3217 |
field->name = reinterpret_cast<const char *>(name); |
| 3218 |
|
| 3219 |
dberr_t err = row_import_cfg_read_string(file, name, len); |
| 3220 |
|
| 3221 |
if (err != DB_SUCCESS) { |
| 3222 |
ib_senderrf(thd, IB_LOG_LEVEL_ERROR, ER_IO_READ_ERROR, errno, |
| 3223 |
strerror(errno), "while parsing table name."); |
| 3224 |
|
| 3225 |
return (err); |
| 3226 |
} |
| 3227 |
} |
| 3228 |
|
| 3229 |
return (DB_SUCCESS); |
| 3230 |
} |
| 3231 |
|
| 3232 |
/** Read the index names and root page numbers of the indexes and set the |
| 3233 |
values. Row format [root_page_no, len of str, str ... ] |
| 3234 |
@return DB_SUCCESS or error code. */ |
| 3235 |
[[nodiscard]] static dberr_t row_import_read_index_data( |
| 3236 |
FILE *file, /*!< in: File to read from */ |
| 3237 |
THD *thd, /*!< in: session */ |
| 3238 |
row_import *cfg) /*!< in/out: meta-data read */ |
| 3239 |
{ |
| 3240 |
byte *ptr; |
| 3241 |
row_index_t *cfg_index; |
| 3242 |
byte row[sizeof(space_index_t) + sizeof(uint32_t) * 9]; |
| 3243 |
|
| 3244 |
/* FIXME: What is the max value? */ |
| 3245 |
ut_a(cfg->m_n_indexes > 0); |
| 3246 |
ut_a(cfg->m_n_indexes < 1024); |
| 3247 |
|
| 3248 |
cfg->m_indexes = ut::new_arr_withkey<row_index_t>( |
| 3249 |
UT_NEW_THIS_FILE_PSI_KEY, ut::Count{cfg->m_n_indexes}); |
| 3250 |
|
| 3251 |
/* Trigger OOM */ |
| 3252 |
DBUG_EXECUTE_IF("ib_import_OOM_6", ut::delete_arr(cfg->m_indexes); |
| 3253 |
cfg->m_indexes = nullptr;); |
| 3254 |
|
| 3255 |
if (cfg->m_indexes == nullptr) { |
| 3256 |
return (DB_OUT_OF_MEMORY); |
| 3257 |
} |
| 3258 |
|
| 3259 |
memset(cfg->m_indexes, 0x0, sizeof(*cfg->m_indexes) * cfg->m_n_indexes); |
| 3260 |
|
| 3261 |
cfg_index = cfg->m_indexes; |
| 3262 |
|
| 3263 |
for (ulint i = 0; i < cfg->m_n_indexes; ++i, ++cfg_index) { |
| 3264 |
/* Trigger EOF */ |
| 3265 |
DBUG_EXECUTE_IF("ib_import_io_read_error_2", |
| 3266 |
(void)fseek(file, 0L, SEEK_END);); |
| 3267 |
|
| 3268 |
/* Read the index data. */ |
| 3269 |
size_t n_bytes = fread(row, 1, sizeof(row), file); |
| 3270 |
|
| 3271 |
/* Trigger EOF */ |
| 3272 |
DBUG_EXECUTE_IF("ib_import_io_read_error", |
| 3273 |
(void)fseek(file, 0L, SEEK_END);); |
| 3274 |
|
| 3275 |
if (n_bytes != sizeof(row)) { |
| 3276 |
char msg[BUFSIZ]; |
| 3277 |
|
| 3278 |
snprintf(msg, sizeof(msg), |
| 3279 |
"while reading index meta-data, expected" |
| 3280 |
" to read %lu bytes but read only %lu" |
| 3281 |
" bytes", |
| 3282 |
(ulong)sizeof(row), (ulong)n_bytes); |
| 3283 |
|
| 3284 |
ib_senderrf(thd, IB_LOG_LEVEL_ERROR, ER_IO_READ_ERROR, errno, |
| 3285 |
strerror(errno), msg); |
| 3286 |
|
| 3287 |
ib::error(ER_IB_MSG_947) << "IO Error: " << msg; |
| 3288 |
|
| 3289 |
return (DB_IO_ERROR); |
| 3290 |
} |
| 3291 |
|
| 3292 |
ptr = row; |
| 3293 |
|
| 3294 |
cfg_index->m_id = mach_read_from_8(ptr); |
| 3295 |
ptr += sizeof(space_index_t); |
| 3296 |
|
| 3297 |
cfg_index->m_space = mach_read_from_4(ptr); |
| 3298 |
ptr += sizeof(uint32_t); |
| 3299 |
|
| 3300 |
cfg_index->m_page_no = mach_read_from_4(ptr); |
| 3301 |
ptr += sizeof(uint32_t); |
| 3302 |
|
| 3303 |
cfg_index->m_type = mach_read_from_4(ptr); |
| 3304 |
ptr += sizeof(uint32_t); |
| 3305 |
|
| 3306 |
cfg_index->m_trx_id_offset = mach_read_from_4(ptr); |
| 3307 |
if (cfg_index->m_trx_id_offset != mach_read_from_4(ptr)) { |
| 3308 |
ut_d(ut_error); |
| 3309 |
/* Overflow. Pretend that the clustered index |
| 3310 |
has a variable-length PRIMARY KEY. */ |
| 3311 |
ut_o(cfg_index->m_trx_id_offset = 0); |
| 3312 |
} |
| 3313 |
ptr += sizeof(uint32_t); |
| 3314 |
|
| 3315 |
cfg_index->m_n_user_defined_cols = mach_read_from_4(ptr); |
| 3316 |
ptr += sizeof(uint32_t); |
| 3317 |
|
| 3318 |
cfg_index->m_n_uniq = mach_read_from_4(ptr); |
| 3319 |
ptr += sizeof(uint32_t); |
| 3320 |
|
| 3321 |
cfg_index->m_n_nullable = mach_read_from_4(ptr); |
| 3322 |
ptr += sizeof(uint32_t); |
| 3323 |
|
| 3324 |
cfg_index->m_n_fields = mach_read_from_4(ptr); |
| 3325 |
ptr += sizeof(uint32_t); |
| 3326 |
|
| 3327 |
/* The NUL byte is included in the name length. */ |
| 3328 |
ulint len = mach_read_from_4(ptr); |
| 3329 |
|
| 3330 |
if (len > OS_FILE_MAX_PATH) { |
| 3331 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_INNODB_INDEX_CORRUPT, |
| 3332 |
"Index name length (" ULINTPF |
| 3333 |
") is too long," |
| 3334 |
" the meta-data is corrupt", |
| 3335 |
len); |
| 3336 |
|
| 3337 |
return (DB_CORRUPTION); |
| 3338 |
} |
| 3339 |
|
| 3340 |
cfg_index->m_name = |
| 3341 |
ut::new_arr_withkey<byte>(UT_NEW_THIS_FILE_PSI_KEY, ut::Count{len}); |
| 3342 |
|
| 3343 |
/* Trigger OOM */ |
| 3344 |
DBUG_EXECUTE_IF("ib_import_OOM_7", ut::delete_arr(cfg_index->m_name); |
| 3345 |
cfg_index->m_name = nullptr;); |
| 3346 |
|
| 3347 |
if (cfg_index->m_name == nullptr) { |
| 3348 |
return (DB_OUT_OF_MEMORY); |
| 3349 |
} |
| 3350 |
|
| 3351 |
dberr_t err; |
| 3352 |
|
| 3353 |
err = row_import_cfg_read_string(file, cfg_index->m_name, len); |
| 3354 |
|
| 3355 |
if (err != DB_SUCCESS) { |
| 3356 |
ib_senderrf(thd, IB_LOG_LEVEL_ERROR, ER_IO_READ_ERROR, errno, |
| 3357 |
strerror(errno), "while parsing index name."); |
| 3358 |
|
| 3359 |
return (err); |
| 3360 |
} |
| 3361 |
|
| 3362 |
err = row_import_cfg_read_index_fields(file, thd, cfg_index, cfg); |
| 3363 |
|
| 3364 |
if (err != DB_SUCCESS) { |
| 3365 |
return (err); |
| 3366 |
} |
| 3367 |
} |
| 3368 |
|
| 3369 |
return (DB_SUCCESS); |
| 3370 |
} |
| 3371 |
|
| 3372 |
/** Set the index root page number for v1 format. |
| 3373 |
@return DB_SUCCESS or error code. */ |
| 3374 |
static dberr_t row_import_read_indexes( |
| 3375 |
FILE *file, /*!< in: File to read from */ |
| 3376 |
THD *thd, /*!< in: session */ |
| 3377 |
row_import *cfg) /*!< in/out: meta-data read */ |
| 3378 |
{ |
| 3379 |
byte row[sizeof(uint32_t)]; |
| 3380 |
|
| 3381 |
/* Trigger EOF */ |
| 3382 |
DBUG_EXECUTE_IF("ib_import_io_read_error_3", |
| 3383 |
(void)fseek(file, 0L, SEEK_END);); |
| 3384 |
|
| 3385 |
/* Read the number of indexes. */ |
| 3386 |
if (fread(row, 1, sizeof(row), file) != sizeof(row)) { |
| 3387 |
ib_senderrf(thd, IB_LOG_LEVEL_ERROR, ER_IO_READ_ERROR, errno, |
| 3388 |
strerror(errno), "while reading number of indexes."); |
| 3389 |
|
| 3390 |
return (DB_IO_ERROR); |
| 3391 |
} |
| 3392 |
|
| 3393 |
cfg->m_n_indexes = mach_read_from_4(row); |
| 3394 |
|
| 3395 |
if (cfg->m_n_indexes == 0) { |
| 3396 |
ib_senderrf(thd, IB_LOG_LEVEL_ERROR, ER_IO_READ_ERROR, errno, |
| 3397 |
strerror(errno), "Number of indexes in meta-data file is 0"); |
| 3398 |
|
| 3399 |
return (DB_CORRUPTION); |
| 3400 |
|
| 3401 |
} else if (cfg->m_n_indexes > 1024) { |
| 3402 |
// FIXME: What is the upper limit? */ |
| 3403 |
ib_senderrf(thd, IB_LOG_LEVEL_ERROR, ER_IO_READ_ERROR, errno, |
| 3404 |
strerror(errno), |
| 3405 |
"Number of indexes in meta-data file is too high: %lu", |
| 3406 |
(ulong)cfg->m_n_indexes); |
| 3407 |
cfg->m_n_indexes = 0; |
| 3408 |
|
| 3409 |
return (DB_CORRUPTION); |
| 3410 |
} |
| 3411 |
|
| 3412 |
return (row_import_read_index_data(file, thd, cfg)); |
| 3413 |
} |
| 3414 |
|
| 3415 |
/** Read specified bytes from the meta data file. |
| 3416 |
@param[in] file file to read from |
| 3417 |
@param[in] length length of bytes to read |
| 3418 |
@return the bytes stream, caller has to free the memory if not nullptr */ |
| 3419 |
[[nodiscard]] static byte *row_import_read_bytes(FILE *file, size_t length) { |
| 3420 |
size_t read = 0; |
| 3421 |
byte *r = |
| 3422 |
ut::new_arr_withkey<byte>(UT_NEW_THIS_FILE_PSI_KEY, ut::Count{length}); |
| 3423 |
|
| 3424 |
if (length == 0) { |
| 3425 |
return (r); |
| 3426 |
} |
| 3427 |
|
| 3428 |
while (!feof(file)) { |
| 3429 |
int ch = fgetc(file); |
| 3430 |
|
| 3431 |
if (ch == EOF) { |
| 3432 |
break; |
| 3433 |
} |
| 3434 |
|
| 3435 |
r[read++] = ch; |
| 3436 |
if (read == length) { |
| 3437 |
return (r); |
| 3438 |
} |
| 3439 |
} |
| 3440 |
|
| 3441 |
errno = EINVAL; |
| 3442 |
|
| 3443 |
ut::delete_arr(r); |
| 3444 |
|
| 3445 |
return (nullptr); |
| 3446 |
} |
| 3447 |
|
| 3448 |
/** Read the metadata config file. Deserialise the contents of |
| 3449 |
dict_col_t::instant_default if exists. |
| 3450 |
Refer to row_quiesce_write_default_value() for the format details. |
| 3451 |
@param[in] file file to read from |
| 3452 |
@param[in,out] col column whose default value to read |
| 3453 |
@param[in,out] heap memory heap to store default value |
| 3454 |
@param[in,out] read true if default value read */ |
| 3455 |
[[nodiscard]] static dberr_t row_import_read_default_values(FILE *file, |
| 3456 |
dict_col_t *col, |
| 3457 |
mem_heap_t **heap, |
| 3458 |
bool *read) { |
| 3459 |
byte *str; |
| 3460 |
|
| 3461 |
/* Instant or not byte */ |
| 3462 |
if ((str = row_import_read_bytes(file, 1)) == nullptr) { |
| 3463 |
return (DB_IO_ERROR); |
| 3464 |
} |
| 3465 |
|
| 3466 |
if (str[0] == 0) { |
| 3467 |
ut::delete_arr(str); |
| 3468 |
*read = false; |
| 3469 |
return (DB_SUCCESS); |
| 3470 |
} |
| 3471 |
|
| 3472 |
*read = true; |
| 3473 |
|
| 3474 |
ut::delete_arr(str); |
| 3475 |
|
| 3476 |
/* Null byte */ |
| 3477 |
if ((str = row_import_read_bytes(file, 1)) == nullptr) { |
| 3478 |
return (DB_IO_ERROR); |
| 3479 |
} |
| 3480 |
|
| 3481 |
if (*heap == nullptr) { |
| 3482 |
*heap = mem_heap_create(100, UT_LOCATION_HERE); |
| 3483 |
} |
| 3484 |
|
| 3485 |
if (str[0] == 1) { |
| 3486 |
ut::delete_arr(str); |
| 3487 |
col->set_default(nullptr, UNIV_SQL_NULL, *heap); |
| 3488 |
return (DB_SUCCESS); |
| 3489 |
} else { |
| 3490 |
ut::delete_arr(str); |
| 3491 |
|
| 3492 |
/* Length bytes */ |
| 3493 |
if ((str = row_import_read_bytes(file, 4)) == nullptr) { |
| 3494 |
return (DB_IO_ERROR); |
| 3495 |
} |
| 3496 |
|
| 3497 |
size_t length = mach_read_from_4(str); |
| 3498 |
|
| 3499 |
ut::delete_arr(str); |
| 3500 |
|
| 3501 |
/* Value bytes */ |
| 3502 |
if (((str = row_import_read_bytes(file, length)) == nullptr) && |
| 3503 |
(length != 0)) { |
| 3504 |
return (DB_IO_ERROR); |
| 3505 |
} |
| 3506 |
|
| 3507 |
col->set_default(str, length, *heap); |
| 3508 |
|
| 3509 |
ut::delete_arr(str); |
| 3510 |
|
| 3511 |
return (DB_SUCCESS); |
| 3512 |
} |
| 3513 |
} |
| 3514 |
|
| 3515 |
/** Read dd::Column metadata for the dropped table. |
| 3516 |
@param[in,out] table_def Table definition |
| 3517 |
@param[in] file file to read from |
| 3518 |
@param[in] thd session |
| 3519 |
@param[in] col dict_col_t |
| 3520 |
@param[in] col_name name of the columns */ |
| 3521 |
static dberr_t row_import_read_dropped_col_metadata(dd::Table *table_def, |
| 3522 |
FILE *file, THD *thd, |
| 3523 |
dict_col_t *col, |
| 3524 |
const char *col_name) { |
| 3525 |
ut_ad(col->is_instant_dropped()); |
| 3526 |
|
| 3527 |
/* Total metadata to be written |
| 3528 |
1 byte for is NULLABLE |
| 3529 |
1 byte for is_unsigned |
| 3530 |
4 bytes for char_length |
| 3531 |
4 bytes for column type |
| 3532 |
4 bytes for numeric scale |
| 3533 |
8 bytes for collation id */ |
| 3534 |
constexpr size_t METADATA_SIZE = 22; |
| 3535 |
|
| 3536 |
byte row[METADATA_SIZE]; |
| 3537 |
|
| 3538 |
/* Read column's v_added, v_dropped, phy_pos */ |
| 3539 |
if (fread(row, 1, sizeof(row), file) != sizeof(row)) { |
| 3540 |
ib_senderrf(thd, IB_LOG_LEVEL_ERROR, ER_IO_READ_ERROR, errno, |
| 3541 |
strerror(errno), "while reading dropped column meta-data."); |
| 3542 |
return (DB_IO_ERROR); |
| 3543 |
} |
| 3544 |
|
| 3545 |
byte *ptr = row; |
| 3546 |
|
| 3547 |
/* 1 byte for is NULLABLE */ |
| 3548 |
bool is_nullable = mach_read_from_1(ptr); |
| 3549 |
ptr += 1; |
| 3550 |
|
| 3551 |
/* 1 byte for is_unsigned */ |
| 3552 |
bool is_unsigned = mach_read_from_1(ptr); |
| 3553 |
ptr += 1; |
| 3554 |
|
| 3555 |
/* 4 bytes for char_length */ |
| 3556 |
uint32_t char_length = mach_read_from_4(ptr); |
| 3557 |
ptr += sizeof(uint32_t); |
| 3558 |
|
| 3559 |
/* 4 bytes for column type */ |
| 3560 |
uint32_t col_type = mach_read_from_4(ptr); |
| 3561 |
ptr += sizeof(uint32_t); |
| 3562 |
|
| 3563 |
/* 4 bytes for numeric scale */ |
| 3564 |
uint32_t numeric_scale = mach_read_from_4(ptr); |
| 3565 |
ptr += sizeof(uint32_t); |
| 3566 |
|
| 3567 |
/* 8 bytes for collation id */ |
| 3568 |
uint64_t collation_id = mach_read_from_8(ptr); |
| 3569 |
ptr += sizeof(uint64_t); |
| 3570 |
|
| 3571 |
/* Read elements for enum column type. |
| 3572 |
[4] bytes : number of elements |
| 3573 |
For each element |
| 3574 |
[4] bytes : element name length (len+1) |
| 3575 |
[len+1] bytes : element name */ |
| 3576 |
std::vector<dd::String_type> enum_names; |
| 3577 |
if ((dd::enum_column_types)col_type == dd::enum_column_types::ENUM || |
| 3578 |
(dd::enum_column_types)col_type == dd::enum_column_types::SET) { |
| 3579 |
byte _row[4]; |
| 3580 |
|
| 3581 |
/* Read element count */ |
| 3582 |
if (fread(_row, 1, sizeof(_row), file) != sizeof(_row)) { |
| 3583 |
ib_senderrf(thd, IB_LOG_LEVEL_ERROR, ER_IO_READ_ERROR, errno, |
| 3584 |
strerror(errno), "while reading dropped column meta-data."); |
| 3585 |
return (DB_IO_ERROR); |
| 3586 |
} |
| 3587 |
size_t n_elem = mach_read_from_4(_row); |
| 3588 |
|
| 3589 |
for (size_t i = 0; i < n_elem; i++) { |
| 3590 |
/* Read element name length */ |
| 3591 |
if (fread(_row, 1, sizeof(_row), file) != sizeof(_row)) { |
| 3592 |
ib_senderrf(thd, IB_LOG_LEVEL_ERROR, ER_IO_READ_ERROR, errno, |
| 3593 |
strerror(errno), "while reading dropped column meta-data."); |
| 3594 |
return (DB_IO_ERROR); |
| 3595 |
} |
| 3596 |
uint32_t len = mach_read_from_4(_row); |
| 3597 |
|
| 3598 |
if (len == 0) { |
| 3599 |
ib_senderrf(thd, IB_LOG_LEVEL_ERROR, ER_IO_READ_ERROR, errno, |
| 3600 |
strerror(errno), |
| 3601 |
"Enum element name length %lu, is invalid for column %s", |
| 3602 |
(ulong)len, col_name); |
| 3603 |
|
| 3604 |
return (DB_CORRUPTION); |
| 3605 |
} |
| 3606 |
|
| 3607 |
/* Read element name */ |
| 3608 |
byte *elem_name = |
| 3609 |
ut::new_arr_withkey<byte>(UT_NEW_THIS_FILE_PSI_KEY, ut::Count{len}); |
| 3610 |
dberr_t err = row_import_cfg_read_string(file, elem_name, len); |
| 3611 |
if (err != DB_SUCCESS) { |
| 3612 |
ib_senderrf(thd, IB_LOG_LEVEL_ERROR, ER_IO_READ_ERROR, errno, |
| 3613 |
strerror(errno), |
| 3614 |
"Error reading Enum element name for column %s", col_name); |
| 3615 |
|
| 3616 |
return (DB_CORRUPTION); |
| 3617 |
} |
| 3618 |
enum_names.push_back((const char *)elem_name); |
| 3619 |
ut::delete_arr(elem_name); |
| 3620 |
} |
| 3621 |
} |
| 3622 |
|
| 3623 |
dd::Column *new_column = |
| 3624 |
const_cast<dd::Column *>(dd_find_column(table_def, col_name)); |
| 3625 |
/* If the INSTANT DROP column already exists in target table DD, confirm it's |
| 3626 |
metadata is matching which the CFG. */ |
| 3627 |
if (new_column != nullptr) { |
| 3628 |
ut_ad(new_column->is_se_hidden()); |
| 3629 |
|
| 3630 |
bool err = false; |
| 3631 |
|
| 3632 |
{ |
| 3633 |
/* Match version added */ |
| 3634 |
if (col->is_instant_added() && dd_column_is_added(new_column)) { |
| 3635 |
uint32_t v = dd_column_get_version_added(new_column); |
| 3636 |
err = (v != (uint32_t)col->get_version_added()); |
| 3637 |
} else if (col->is_instant_added() || dd_column_is_added(new_column)) { |
| 3638 |
err = true; |
| 3639 |
} |
| 3640 |
|
| 3641 |
/* Match version dropped */ |
| 3642 |
if (!err) { |
| 3643 |
if (dd_column_is_dropped(new_column)) { |
| 3644 |
uint32_t v = dd_column_get_version_dropped(new_column); |
| 3645 |
err = (v != (uint32_t)col->get_version_dropped()); |
| 3646 |
} else { |
| 3647 |
err = true; |
| 3648 |
} |
| 3649 |
} |
| 3650 |
|
| 3651 |
/* Match phy_pos */ |
| 3652 |
if (!err) { |
| 3653 |
const char *s = dd_column_key_strings[DD_INSTANT_PHYSICAL_POS]; |
| 3654 |
uint32_t v = 0; |
| 3655 |
new_column->se_private_data().get(s, &v); |
| 3656 |
err = (v != col->get_phy_pos()); |
| 3657 |
} |
| 3658 |
} |
| 3659 |
|
| 3660 |
auto match_enum_values = [&](dd::Column *col) { |
| 3661 |
if (enum_names.size() == 0) { |
| 3662 |
ut_ad(col->type() != dd::enum_column_types::ENUM && |
| 3663 |
col->type() != dd::enum_column_types::SET); |
| 3664 |
return false; |
| 3665 |
} |
| 3666 |
|
| 3667 |
size_t i = 0; |
| 3668 |
for (const auto &elem : col->elements()) { |
| 3669 |
if (enum_names[i++].compare(elem->name()) != 0) { |
| 3670 |
return true; |
| 3671 |
} |
| 3672 |
} |
| 3673 |
|
| 3674 |
return false; |
| 3675 |
}; |
| 3676 |
|
| 3677 |
if (err || new_column->is_nullable() != is_nullable || |
| 3678 |
new_column->is_unsigned() != is_unsigned || |
| 3679 |
new_column->char_length() != char_length || |
| 3680 |
new_column->numeric_scale() != numeric_scale || |
| 3681 |
new_column->collation_id() != collation_id || |
| 3682 |
match_enum_values(new_column)) { |
| 3683 |
ib_senderrf(thd, IB_LOG_LEVEL_ERROR, ER_IO_READ_ERROR, errno, |
| 3684 |
strerror(errno), |
| 3685 |
"DD metadata for INSTNAT DROP column %s in" |
| 3686 |
" target table doesn't match with CFG.", |
| 3687 |
col_name); |
| 3688 |
return DB_ERROR; |
| 3689 |
} |
| 3690 |
|
| 3691 |
return DB_SUCCESS; |
| 3692 |
} |
| 3693 |
|
| 3694 |
/* Add this column as a SE_HIDDEN column in dest table def */ |
| 3695 |
new_column = dd_add_hidden_column(table_def, col_name, char_length, |
| 3696 |
(dd::enum_column_types)col_type); |
| 3697 |
ut_ad(new_column != nullptr); |
| 3698 |
|
| 3699 |
/* Set SE Private data of newly added hidden column here */ |
| 3700 |
{ |
| 3701 |
auto set = [&](const char *s, uint32_t v) { |
| 3702 |
new_column->se_private_data().set(s, v); |
| 3703 |
}; |
| 3704 |
|
| 3705 |
new_column->se_private_data().clear(); |
| 3706 |
if (col->is_instant_added()) { |
| 3707 |
set(dd_column_key_strings[DD_INSTANT_VERSION_ADDED], |
| 3708 |
(uint32_t)col->get_version_added()); |
| 3709 |
} |
| 3710 |
|
| 3711 |
set(dd_column_key_strings[DD_INSTANT_VERSION_DROPPED], |
| 3712 |
(uint32_t)col->get_version_dropped()); |
| 3713 |
set(dd_column_key_strings[DD_INSTANT_PHYSICAL_POS], col->get_phy_pos()); |
| 3714 |
} |
| 3715 |
|
| 3716 |
new_column->set_nullable(is_nullable); |
| 3717 |
new_column->set_unsigned(is_unsigned); |
| 3718 |
new_column->set_char_length(char_length); |
| 3719 |
new_column->set_numeric_scale(numeric_scale); |
| 3720 |
new_column->set_collation_id(collation_id); |
| 3721 |
new_column->set_type((dd::enum_column_types)col_type); |
| 3722 |
/* Elements for enum columns */ |
| 3723 |
if ((dd::enum_column_types)col_type == dd::enum_column_types::ENUM || |
| 3724 |
(dd::enum_column_types)col_type == dd::enum_column_types::SET) { |
| 3725 |
for (auto &name : enum_names) { |
| 3726 |
auto *elem_obj = new_column->add_element(); |
| 3727 |
elem_obj->set_name(name.c_str()); |
| 3728 |
} |
| 3729 |
} |
| 3730 |
|
| 3731 |
return DB_SUCCESS; |
| 3732 |
} |
| 3733 |
|
| 3734 |
/** Read the meta data (table columns) config file. Deserialise the contents of |
| 3735 |
dict_col_t structure, along with the column name. |
| 3736 |
@param[in,out] table_def Table definition |
| 3737 |
@param[in] file file to read from |
| 3738 |
@param[in] thd session |
| 3739 |
@param[in] cfg meta-data read */ |
| 3740 |
[[nodiscard]] static dberr_t row_import_read_columns(dd::Table *table_def, |
| 3741 |
FILE *file, THD *thd, |
| 3742 |
row_import *cfg) { |
| 3743 |
/* FIXME: What should the upper limit be? */ |
| 3744 |
ut_a(cfg->m_n_cols > 0); |
| 3745 |
ut_a(cfg->m_n_cols < 1024); |
| 3746 |
|
| 3747 |
/* Allocate array of columns */ |
| 3748 |
cfg->m_cols = ut::new_arr_withkey<dict_col_t>(UT_NEW_THIS_FILE_PSI_KEY, |
| 3749 |
ut::Count{cfg->m_n_cols}); |
| 3750 |
|
| 3751 |
/* Trigger OOM */ |
| 3752 |
DBUG_EXECUTE_IF("ib_import_OOM_8", ut::delete_arr(cfg->m_cols); |
| 3753 |
cfg->m_cols = nullptr;); |
| 3754 |
|
| 3755 |
if (cfg->m_cols == nullptr) { |
| 3756 |
return (DB_OUT_OF_MEMORY); |
| 3757 |
} |
| 3758 |
|
| 3759 |
// memset(cfg->m_cols, 0x0, sizeof(*cfg->m_cols) * cfg->m_n_cols); |
| 3760 |
|
| 3761 |
/* Allocated array to store name of the columns */ |
| 3762 |
cfg->m_col_names = ut::new_arr_withkey<byte *>(UT_NEW_THIS_FILE_PSI_KEY, |
| 3763 |
ut::Count{cfg->m_n_cols}); |
| 3764 |
|
| 3765 |
/* Trigger OOM */ |
| 3766 |
DBUG_EXECUTE_IF("ib_import_OOM_9", ut::delete_arr(cfg->m_col_names); |
| 3767 |
cfg->m_col_names = nullptr;); |
| 3768 |
|
| 3769 |
if (cfg->m_col_names == nullptr) { |
| 3770 |
return (DB_OUT_OF_MEMORY); |
| 3771 |
} |
| 3772 |
|
| 3773 |
memset(cfg->m_col_names, 0x0, sizeof(cfg->m_col_names) * cfg->m_n_cols); |
| 3774 |
|
| 3775 |
dict_col_t *col = cfg->m_cols; |
| 3776 |
byte row[sizeof(uint32_t) * 8]; |
| 3777 |
|
| 3778 |
for (ulint i = 0; i < cfg->m_n_cols; ++i, ++col) { |
| 3779 |
byte *ptr = row; |
| 3780 |
|
| 3781 |
/* Trigger EOF */ |
| 3782 |
DBUG_EXECUTE_IF("ib_import_io_read_error_4", |
| 3783 |
(void)fseek(file, 0L, SEEK_END);); |
| 3784 |
|
| 3785 |
if (fread(row, 1, sizeof(row), file) != sizeof(row)) { |
| 3786 |
ib_senderrf(thd, IB_LOG_LEVEL_ERROR, ER_IO_READ_ERROR, errno, |
| 3787 |
strerror(errno), "while reading table column meta-data."); |
| 3788 |
|
| 3789 |
return (DB_IO_ERROR); |
| 3790 |
} |
| 3791 |
|
| 3792 |
col->prtype = mach_read_from_4(ptr); |
| 3793 |
ptr += sizeof(uint32_t); |
| 3794 |
|
| 3795 |
col->mtype = mach_read_from_4(ptr); |
| 3796 |
ptr += sizeof(uint32_t); |
| 3797 |
|
| 3798 |
col->len = mach_read_from_4(ptr); |
| 3799 |
ptr += sizeof(uint32_t); |
| 3800 |
|
| 3801 |
col->mbminmaxlen = mach_read_from_4(ptr); |
| 3802 |
ptr += sizeof(uint32_t); |
| 3803 |
|
| 3804 |
col->ind = mach_read_from_4(ptr); |
| 3805 |
ptr += sizeof(uint32_t); |
| 3806 |
|
| 3807 |
col->ord_part = mach_read_from_4(ptr); |
| 3808 |
ptr += sizeof(uint32_t); |
| 3809 |
|
| 3810 |
col->max_prefix = mach_read_from_4(ptr); |
| 3811 |
ptr += sizeof(uint32_t); |
| 3812 |
|
| 3813 |
/* Read in the column name as [len, byte array]. The len |
| 3814 |
includes the NUL byte. */ |
| 3815 |
|
| 3816 |
ulint len = mach_read_from_4(ptr); |
| 3817 |
|
| 3818 |
/* FIXME: What is the maximum column name length? */ |
| 3819 |
if (len == 0 || len > 128) { |
| 3820 |
ib_senderrf(thd, IB_LOG_LEVEL_ERROR, ER_IO_READ_ERROR, errno, |
| 3821 |
strerror(errno), "Column name length %lu, is invalid", |
| 3822 |
(ulong)len); |
| 3823 |
|
| 3824 |
return (DB_CORRUPTION); |
| 3825 |
} |
| 3826 |
|
| 3827 |
cfg->m_col_names[i] = |
| 3828 |
ut::new_arr_withkey<byte>(UT_NEW_THIS_FILE_PSI_KEY, ut::Count{len}); |
| 3829 |
|
| 3830 |
/* Trigger OOM */ |
| 3831 |
DBUG_EXECUTE_IF("ib_import_OOM_10", ut::delete_arr(cfg->m_col_names[i]); |
| 3832 |
cfg->m_col_names[i] = nullptr;); |
| 3833 |
|
| 3834 |
if (cfg->m_col_names[i] == nullptr) { |
| 3835 |
return (DB_OUT_OF_MEMORY); |
| 3836 |
} |
| 3837 |
|
| 3838 |
dberr_t err; |
| 3839 |
|
| 3840 |
err = row_import_cfg_read_string(file, cfg->m_col_names[i], len); |
| 3841 |
|
| 3842 |
if (err != DB_SUCCESS) { |
| 3843 |
ib_senderrf(thd, IB_LOG_LEVEL_ERROR, ER_IO_READ_ERROR, errno, |
| 3844 |
strerror(errno), "while parsing table column name."); |
| 3845 |
|
| 3846 |
return (err); |
| 3847 |
} |
| 3848 |
|
| 3849 |
/* Read INSTANT metadata of column */ |
| 3850 |
if (cfg->m_version >= IB_EXPORT_CFG_VERSION_V7) { |
| 3851 |
byte row[2 + sizeof(uint32_t)]; |
| 3852 |
|
| 3853 |
/* Read column's v_added, v_dropped, phy_pos */ |
| 3854 |
if (fread(row, 1, sizeof(row), file) != sizeof(row)) { |
| 3855 |
ib_senderrf(thd, IB_LOG_LEVEL_ERROR, ER_IO_READ_ERROR, errno, |
| 3856 |
strerror(errno), |
| 3857 |
"while reading table column INSTANT meta-data."); |
| 3858 |
return (DB_IO_ERROR); |
| 3859 |
} |
| 3860 |
|
| 3861 |
byte *ptr = row; |
| 3862 |
uint8_t v = mach_read_from_1(ptr); |
| 3863 |
col->set_version_added(v); |
| 3864 |
ptr++; |
| 3865 |
|
| 3866 |
v = mach_read_from_1(ptr); |
| 3867 |
col->set_version_dropped(v); |
| 3868 |
ptr++; |
| 3869 |
|
| 3870 |
col->set_phy_pos(mach_read_from_4(ptr)); |
| 3871 |
|
| 3872 |
if (col->is_instant_dropped()) { |
| 3873 |
const char *col_name = (const char *)cfg->m_col_names[i]; |
| 3874 |
ut_ad(dict_col_t::is_instant_dropped_name(std::string(col_name))); |
| 3875 |
|
| 3876 |
/* Read dropped col dd::Column metadata and add it to dd::Table */ |
| 3877 |
dberr_t err = row_import_read_dropped_col_metadata(table_def, file, thd, |
| 3878 |
col, col_name); |
| 3879 |
|
| 3880 |
if (err != DB_SUCCESS) { |
| 3881 |
return err; |
| 3882 |
} |
| 3883 |
} |
| 3884 |
} |
| 3885 |
|
| 3886 |
if (cfg->m_version >= IB_EXPORT_CFG_VERSION_V3) { |
| 3887 |
bool read = false; |
| 3888 |
dberr_t err; |
| 3889 |
|
| 3890 |
err = row_import_read_default_values(file, col, &cfg->m_heap, &read); |
| 3891 |
|
| 3892 |
if (err != DB_SUCCESS) { |
| 3893 |
ib_senderrf(thd, IB_LOG_LEVEL_ERROR, ER_IO_READ_ERROR, errno, |
| 3894 |
strerror(errno), |
| 3895 |
"while reading table column" |
| 3896 |
" default value."); |
| 3897 |
return (err); |
| 3898 |
} |
| 3899 |
|
| 3900 |
if (read) { |
| 3901 |
++cfg->m_n_instant_cols; |
| 3902 |
} |
| 3903 |
} |
| 3904 |
} |
| 3905 |
|
| 3906 |
return (DB_SUCCESS); |
| 3907 |
} |
| 3908 |
|
| 3909 |
/** Read the contents of the @<tablespace@>.cfg file. |
| 3910 |
@return DB_SUCCESS or error code. */ |
| 3911 |
[[nodiscard]] static dberr_t row_import_read_v1( |
| 3912 |
FILE *file, /*!< in: File to read from */ |
| 3913 |
THD *thd, /*!< in: session */ |
| 3914 |
row_import *cfg) /*!< out: meta data */ |
| 3915 |
{ |
| 3916 |
byte value[sizeof(uint32_t)]; |
| 3917 |
|
| 3918 |
/* Trigger EOF */ |
| 3919 |
DBUG_EXECUTE_IF("ib_import_io_read_error_5", |
| 3920 |
(void)fseek(file, 0L, SEEK_END);); |
| 3921 |
|
| 3922 |
/* Read the hostname where the tablespace was exported. */ |
| 3923 |
if (fread(value, 1, sizeof(value), file) != sizeof(value)) { |
| 3924 |
ib_senderrf(thd, IB_LOG_LEVEL_ERROR, ER_IO_READ_ERROR, errno, |
| 3925 |
strerror(errno), |
| 3926 |
"while reading meta-data export hostname length."); |
| 3927 |
|
| 3928 |
return (DB_IO_ERROR); |
| 3929 |
} |
| 3930 |
|
| 3931 |
ulint len = mach_read_from_4(value); |
| 3932 |
|
| 3933 |
/* NUL byte is part of name length. */ |
| 3934 |
cfg->m_hostname = |
| 3935 |
ut::new_arr_withkey<byte>(UT_NEW_THIS_FILE_PSI_KEY, ut::Count{len}); |
| 3936 |
|
| 3937 |
/* Trigger OOM */ |
| 3938 |
DBUG_EXECUTE_IF("ib_import_OOM_1", ut::delete_arr(cfg->m_hostname); |
| 3939 |
cfg->m_hostname = nullptr;); |
| 3940 |
|
| 3941 |
if (cfg->m_hostname == nullptr) { |
| 3942 |
return (DB_OUT_OF_MEMORY); |
| 3943 |
} |
| 3944 |
|
| 3945 |
dberr_t err = row_import_cfg_read_string(file, cfg->m_hostname, len); |
| 3946 |
|
| 3947 |
if (err != DB_SUCCESS) { |
| 3948 |
ib_senderrf(thd, IB_LOG_LEVEL_ERROR, ER_IO_READ_ERROR, errno, |
| 3949 |
strerror(errno), "while parsing export hostname."); |
| 3950 |
|
| 3951 |
return (err); |
| 3952 |
} |
| 3953 |
|
| 3954 |
/* Trigger EOF */ |
| 3955 |
DBUG_EXECUTE_IF("ib_import_io_read_error_6", |
| 3956 |
(void)fseek(file, 0L, SEEK_END);); |
| 3957 |
|
| 3958 |
/* Read the table name of tablespace that was exported. */ |
| 3959 |
if (fread(value, 1, sizeof(value), file) != sizeof(value)) { |
| 3960 |
ib_senderrf(thd, IB_LOG_LEVEL_ERROR, ER_IO_READ_ERROR, errno, |
| 3961 |
strerror(errno), "while reading meta-data table name length."); |
| 3962 |
|
| 3963 |
return (DB_IO_ERROR); |
| 3964 |
} |
| 3965 |
|
| 3966 |
len = mach_read_from_4(value); |
| 3967 |
|
| 3968 |
/* NUL byte is part of name length. */ |
| 3969 |
cfg->m_table_name = |
| 3970 |
ut::new_arr_withkey<byte>(UT_NEW_THIS_FILE_PSI_KEY, ut::Count{len}); |
| 3971 |
|
| 3972 |
/* Trigger OOM */ |
| 3973 |
DBUG_EXECUTE_IF("ib_import_OOM_2", ut::delete_arr(cfg->m_table_name); |
| 3974 |
cfg->m_table_name = nullptr;); |
| 3975 |
|
| 3976 |
if (cfg->m_table_name == nullptr) { |
| 3977 |
return (DB_OUT_OF_MEMORY); |
| 3978 |
} |
| 3979 |
|
| 3980 |
err = row_import_cfg_read_string(file, cfg->m_table_name, len); |
| 3981 |
|
| 3982 |
if (err != DB_SUCCESS) { |
| 3983 |
ib_senderrf(thd, IB_LOG_LEVEL_ERROR, ER_IO_READ_ERROR, errno, |
| 3984 |
strerror(errno), "while parsing table name."); |
| 3985 |
|
| 3986 |
return (err); |
| 3987 |
} |
| 3988 |
|
| 3989 |
ib::info(ER_IB_MSG_948) << "Importing tablespace for table '" |
| 3990 |
<< cfg->m_table_name |
| 3991 |
<< "' that was exported from host '" |
| 3992 |
<< cfg->m_hostname << "'"; |
| 3993 |
|
| 3994 |
byte row[sizeof(uint32_t) * 3]; |
| 3995 |
|
| 3996 |
/* Trigger EOF */ |
| 3997 |
DBUG_EXECUTE_IF("ib_import_io_read_error_7", |
| 3998 |
(void)fseek(file, 0L, SEEK_END);); |
| 3999 |
|
| 4000 |
/* Read the autoinc value. */ |
| 4001 |
if (fread(row, 1, sizeof(uint64_t), file) != sizeof(uint64_t)) { |
| 4002 |
ib_senderrf(thd, IB_LOG_LEVEL_ERROR, ER_IO_READ_ERROR, errno, |
| 4003 |
strerror(errno), "while reading autoinc value."); |
| 4004 |
|
| 4005 |
return (DB_IO_ERROR); |
| 4006 |
} |
| 4007 |
|
| 4008 |
cfg->m_autoinc = mach_read_from_8(row); |
| 4009 |
|
| 4010 |
/* Trigger EOF */ |
| 4011 |
DBUG_EXECUTE_IF("ib_import_io_read_error_8", |
| 4012 |
(void)fseek(file, 0L, SEEK_END);); |
| 4013 |
|
| 4014 |
/* Read the tablespace page size. */ |
| 4015 |
if (fread(row, 1, sizeof(row), file) != sizeof(row)) { |
| 4016 |
ib_senderrf(thd, IB_LOG_LEVEL_ERROR, ER_IO_READ_ERROR, errno, |
| 4017 |
strerror(errno), "while reading meta-data header."); |
| 4018 |
|
| 4019 |
return (DB_IO_ERROR); |
| 4020 |
} |
| 4021 |
|
| 4022 |
byte *ptr = row; |
| 4023 |
|
| 4024 |
const ulint logical_page_size = mach_read_from_4(ptr); |
| 4025 |
ptr += sizeof(uint32_t); |
| 4026 |
|
| 4027 |
if (logical_page_size != univ_page_size.logical()) { |
| 4028 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 4029 |
"Tablespace to be imported has a different" |
| 4030 |
" page size than this server. Server page size" |
| 4031 |
" is %zu, whereas tablespace page size" |
| 4032 |
" is " ULINTPF, |
| 4033 |
univ_page_size.logical(), logical_page_size); |
| 4034 |
|
| 4035 |
return (DB_ERROR); |
| 4036 |
} |
| 4037 |
|
| 4038 |
cfg->m_flags = mach_read_from_4(ptr); |
| 4039 |
ptr += sizeof(uint32_t); |
| 4040 |
|
| 4041 |
cfg->m_page_size.copy_from(dict_tf_get_page_size(cfg->m_flags)); |
| 4042 |
|
| 4043 |
ut_a(logical_page_size == cfg->m_page_size.logical()); |
| 4044 |
|
| 4045 |
/* Read Total number of columns in table */ |
| 4046 |
cfg->m_n_cols = mach_read_from_4(ptr); |
| 4047 |
|
| 4048 |
if (!dict_tf_is_valid(cfg->m_flags)) { |
| 4049 |
return (DB_CORRUPTION); |
| 4050 |
} |
| 4051 |
|
| 4052 |
if (cfg->m_version >= IB_EXPORT_CFG_VERSION_V5) { |
| 4053 |
/* Read the nullable field before first instant column */ |
| 4054 |
if (fread(value, 1, sizeof(value), file) != sizeof(value)) { |
| 4055 |
ib_senderrf(thd, IB_LOG_LEVEL_ERROR, ER_IO_READ_ERROR, errno, |
| 4056 |
strerror(errno), |
| 4057 |
"while reading meta-data nullable column" |
| 4058 |
" before first instant column."); |
| 4059 |
|
| 4060 |
return (DB_IO_ERROR); |
| 4061 |
} |
| 4062 |
|
| 4063 |
cfg->m_n_instant_nullable = mach_read_from_4(value); |
| 4064 |
} else { |
| 4065 |
cfg->m_n_instant_nullable = 0; |
| 4066 |
} |
| 4067 |
|
| 4068 |
if (cfg->m_version >= IB_EXPORT_CFG_VERSION_V7) { |
| 4069 |
byte row[sizeof(uint32_t) * 5]; |
| 4070 |
|
| 4071 |
/* Read column's count for the table */ |
| 4072 |
if (fread(row, 1, sizeof(row), file) != sizeof(row)) { |
| 4073 |
ib_senderrf(thd, IB_LOG_LEVEL_ERROR, ER_IO_READ_ERROR, errno, |
| 4074 |
strerror(errno), "while reading table column counts."); |
| 4075 |
return (DB_IO_ERROR); |
| 4076 |
} |
| 4077 |
|
| 4078 |
byte *ptr = row; |
| 4079 |
cfg->m_initial_column_count = mach_read_from_4(ptr); |
| 4080 |
ptr += sizeof(uint32_t); |
| 4081 |
|
| 4082 |
cfg->m_current_column_count = mach_read_from_4(ptr); |
| 4083 |
ptr += sizeof(uint32_t); |
| 4084 |
|
| 4085 |
cfg->m_total_column_count = mach_read_from_4(ptr); |
| 4086 |
ptr += sizeof(uint32_t); |
| 4087 |
|
| 4088 |
cfg->m_n_instant_drop_cols = mach_read_from_4(ptr); |
| 4089 |
ptr += sizeof(uint32_t); |
| 4090 |
|
| 4091 |
cfg->m_current_row_version = mach_read_from_4(ptr); |
| 4092 |
ptr += sizeof(uint32_t); |
| 4093 |
} |
| 4094 |
|
| 4095 |
return (err); |
| 4096 |
} |
| 4097 |
|
| 4098 |
/** Read tablespace flags and compression type info from @<tablespace@>.cfg |
| 4099 |
file. |
| 4100 |
@param[in] file File to read from |
| 4101 |
@param[in] thd session |
| 4102 |
@param[in,out] cfg meta data |
| 4103 |
@return DB_SUCCESS or error code. */ |
| 4104 |
[[nodiscard]] static MY_ATTRIBUTE((nonnull)) dberr_t |
| 4105 |
row_import_read_v2(FILE *file, THD *thd, row_import *cfg) { |
| 4106 |
byte value[sizeof(uint32_t)]; |
| 4107 |
|
| 4108 |
/* Read the tablespace flags */ |
| 4109 |
if (fread(value, 1, sizeof(value), file) != sizeof(value)) { |
| 4110 |
ib_senderrf(thd, IB_LOG_LEVEL_ERROR, ER_IO_READ_ERROR, errno, |
| 4111 |
strerror(errno), "while reading meta-data tablespace flags."); |
| 4112 |
|
| 4113 |
return DB_IO_ERROR; |
| 4114 |
} |
| 4115 |
|
| 4116 |
ulint space_flags = mach_read_from_4(value); |
| 4117 |
ut_ad(space_flags != UINT32_UNDEFINED); |
| 4118 |
cfg->m_has_sdi = FSP_FLAGS_HAS_SDI(space_flags); |
| 4119 |
|
| 4120 |
if (cfg->m_version >= IB_EXPORT_CFG_VERSION_V6) { |
| 4121 |
/* Read the compression type info. */ |
| 4122 |
if (fread(value, 1, sizeof(uint8_t), file) != sizeof(uint8_t)) { |
| 4123 |
ib_senderrf(thd, IB_LOG_LEVEL_ERROR, ER_IO_READ_ERROR, errno, |
| 4124 |
strerror(errno), "while reading compression type info."); |
| 4125 |
|
| 4126 |
return DB_IO_ERROR; |
| 4127 |
} |
| 4128 |
|
| 4129 |
auto compression_type = |
| 4130 |
static_cast<Compression::Type>(mach_read_from_1(value)); |
| 4131 |
|
| 4132 |
ut_ad(Compression::validate(compression_type)); |
| 4133 |
|
| 4134 |
cfg->m_compression_type = compression_type; |
| 4135 |
} |
| 4136 |
|
| 4137 |
return DB_SUCCESS; |
| 4138 |
} |
| 4139 |
|
| 4140 |
/** Read the contents of the @<tablespace@>.cfg file |
| 4141 |
@param[in] table_def Table definition |
| 4142 |
@param[in] file file to read from |
| 4143 |
@param[in] thd session |
| 4144 |
@param[in,out] cfg meta data |
| 4145 |
@return DB_SUCCESS or error code. */ |
| 4146 |
[[nodiscard]] static MY_ATTRIBUTE((nonnull)) dberr_t |
| 4147 |
row_import_read_common(dd::Table *table_def, FILE *file, THD *thd, |
| 4148 |
row_import *cfg) { |
| 4149 |
dberr_t err; |
| 4150 |
if ((err = row_import_read_columns(table_def, file, thd, cfg)) != |
| 4151 |
DB_SUCCESS) { |
| 4152 |
return (err); |
| 4153 |
|
| 4154 |
} else if ((err = row_import_read_indexes(file, thd, cfg)) != DB_SUCCESS) { |
| 4155 |
return (err); |
| 4156 |
} |
| 4157 |
|
| 4158 |
ut_a(err == DB_SUCCESS); |
| 4159 |
return (err); |
| 4160 |
} |
| 4161 |
|
| 4162 |
/** |
| 4163 |
Read the contents of the @<tablespace@>.cfg file. |
| 4164 |
@param[in] table dict table |
| 4165 |
@param[in] table_def Table definition |
| 4166 |
@param[in] file File to read from |
| 4167 |
@param[in] thd session |
| 4168 |
@param[out] cfg contents of the .cfg file |
| 4169 |
@return DB_SUCCESS or error code. */ |
| 4170 |
[[nodiscard]] static dberr_t row_import_read_meta_data(dict_table_t *table, |
| 4171 |
dd::Table *table_def, |
| 4172 |
FILE *file, THD *thd, |
| 4173 |
row_import &cfg) { |
| 4174 |
byte row[sizeof(uint32_t)]; |
| 4175 |
|
| 4176 |
/* Trigger EOF */ |
| 4177 |
DBUG_EXECUTE_IF("ib_import_io_read_error_9", |
| 4178 |
(void)fseek(file, 0L, SEEK_END);); |
| 4179 |
|
| 4180 |
if (fread(&row, 1, sizeof(row), file) != sizeof(row)) { |
| 4181 |
ib_senderrf(thd, IB_LOG_LEVEL_ERROR, ER_IO_READ_ERROR, errno, |
| 4182 |
strerror(errno), "while reading meta-data version."); |
| 4183 |
|
| 4184 |
return (DB_IO_ERROR); |
| 4185 |
} |
| 4186 |
|
| 4187 |
cfg.m_version = mach_read_from_4(row); |
| 4188 |
|
| 4189 |
/* Check the version number. */ |
| 4190 |
switch (cfg.m_version) { |
| 4191 |
dberr_t err; |
| 4192 |
case IB_EXPORT_CFG_VERSION_V1: |
| 4193 |
err = row_import_read_v1(file, thd, &cfg); |
| 4194 |
if (err == DB_SUCCESS) { |
| 4195 |
err = row_import_read_common(table_def, file, thd, &cfg); |
| 4196 |
} |
| 4197 |
return (err); |
| 4198 |
|
| 4199 |
case IB_EXPORT_CFG_VERSION_V2: |
| 4200 |
case IB_EXPORT_CFG_VERSION_V3: |
| 4201 |
case IB_EXPORT_CFG_VERSION_V4: |
| 4202 |
case IB_EXPORT_CFG_VERSION_V5: |
| 4203 |
case IB_EXPORT_CFG_VERSION_V6: |
| 4204 |
case IB_EXPORT_CFG_VERSION_V7: |
| 4205 |
err = row_import_read_v1(file, thd, &cfg); |
| 4206 |
|
| 4207 |
if (err == DB_SUCCESS) { |
| 4208 |
err = row_import_read_v2(file, thd, &cfg); |
| 4209 |
} |
| 4210 |
|
| 4211 |
if (err == DB_SUCCESS) { |
| 4212 |
err = row_import_read_common(table_def, file, thd, &cfg); |
| 4213 |
} |
| 4214 |
return (err); |
| 4215 |
default: |
| 4216 |
my_error(ER_IMP_INCOMPATIBLE_CFG_VERSION, MYF(0), table->name.m_name, |
| 4217 |
unsigned{cfg.m_version}, unsigned{IB_EXPORT_CFG_VERSION_V5}); |
| 4218 |
} |
| 4219 |
|
| 4220 |
return (DB_UNSUPPORTED); |
| 4221 |
} |
| 4222 |
|
| 4223 |
/** |
| 4224 |
Read the contents of the @<tablename@>.cfg file. |
| 4225 |
@param[in] table table |
| 4226 |
@param[in] table_def dd table |
| 4227 |
@param[in] thd session |
| 4228 |
@param[in,out] cfg contents of the .cfg file |
| 4229 |
@return DB_SUCCESS or error code. */ |
| 4230 |
[[nodiscard]] static dberr_t row_import_read_cfg(dict_table_t *table, |
| 4231 |
dd::Table *table_def, THD *thd, |
| 4232 |
row_import &cfg) { |
| 4233 |
dberr_t err; |
| 4234 |
char name[OS_FILE_MAX_PATH]; |
| 4235 |
|
| 4236 |
cfg.m_table = table; |
| 4237 |
|
| 4238 |
dd_get_meta_data_filename(table, table_def, name, sizeof(name)); |
| 4239 |
|
| 4240 |
fil_adjust_name_import(table, name, CFG); |
| 4241 |
|
| 4242 |
FILE *file = fopen(name, "rb"); |
| 4243 |
|
| 4244 |
if (file == nullptr) { |
| 4245 |
char msg[BUFSIZ]; |
| 4246 |
|
| 4247 |
snprintf(msg, sizeof(msg), |
| 4248 |
"Error opening '%s', will attempt to import" |
| 4249 |
" without schema verification", |
| 4250 |
name); |
| 4251 |
|
| 4252 |
ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_IO_READ_ERROR, errno, |
| 4253 |
strerror(errno), msg); |
| 4254 |
|
| 4255 |
cfg.m_missing = true; |
| 4256 |
|
| 4257 |
err = DB_FAIL; |
| 4258 |
} else { |
| 4259 |
cfg.m_missing = false; |
| 4260 |
|
| 4261 |
err = row_import_read_meta_data(table, table_def, file, thd, cfg); |
| 4262 |
fclose(file); |
| 4263 |
} |
| 4264 |
|
| 4265 |
return (err); |
| 4266 |
} |
| 4267 |
|
| 4268 |
/** Read the contents of the .cfp file. |
| 4269 |
@param[out] cfg the encryption key will be stored to it |
| 4270 |
@param[in] file file to read from |
| 4271 |
@param[in] thd session |
| 4272 |
@return DB_SUCCESS or error code. */ |
| 4273 |
static dberr_t row_import_read_encryption_data(row_import &cfg, FILE *file, |
| 4274 |
THD *thd) { |
| 4275 |
byte row[sizeof(uint32_t)]; |
| 4276 |
ulint key_size; |
| 4277 |
byte transfer_key[Encryption::KEY_LEN]; |
| 4278 |
byte encryption_key[Encryption::KEY_LEN]; |
| 4279 |
byte encryption_iv[Encryption::KEY_LEN]; |
| 4280 |
lint elen; |
| 4281 |
|
| 4282 |
if (fread(&row, 1, sizeof(row), file) != sizeof(row)) { |
| 4283 |
ib_senderrf(thd, IB_LOG_LEVEL_ERROR, ER_IO_READ_ERROR, errno, |
| 4284 |
strerror(errno), "while reading encrypton key size."); |
| 4285 |
|
| 4286 |
return (DB_IO_ERROR); |
| 4287 |
} |
| 4288 |
|
| 4289 |
key_size = mach_read_from_4(row); |
| 4290 |
if (key_size != Encryption::KEY_LEN) { |
| 4291 |
ib_senderrf(thd, IB_LOG_LEVEL_ERROR, ER_IO_READ_ERROR, errno, |
| 4292 |
strerror(errno), "while parsing encryption key size."); |
| 4293 |
|
| 4294 |
return (DB_IO_ERROR); |
| 4295 |
} |
| 4296 |
|
| 4297 |
/* Read the transfer key. */ |
| 4298 |
if (fread(transfer_key, 1, Encryption::KEY_LEN, file) != |
| 4299 |
Encryption::KEY_LEN) { |
| 4300 |
ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_IO_WRITE_ERROR, errno, |
| 4301 |
strerror(errno), "while reading tranfer key."); |
| 4302 |
|
| 4303 |
return (DB_IO_ERROR); |
| 4304 |
} |
| 4305 |
|
| 4306 |
/* Read the encrypted key. */ |
| 4307 |
if (fread(encryption_key, 1, Encryption::KEY_LEN, file) != |
| 4308 |
Encryption::KEY_LEN) { |
| 4309 |
ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_IO_WRITE_ERROR, errno, |
| 4310 |
strerror(errno), "while reading encryption key."); |
| 4311 |
|
| 4312 |
return (DB_IO_ERROR); |
| 4313 |
} |
| 4314 |
|
| 4315 |
/* Read the encrypted iv. */ |
| 4316 |
if (fread(encryption_iv, 1, Encryption::KEY_LEN, file) != |
| 4317 |
Encryption::KEY_LEN) { |
| 4318 |
ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_IO_WRITE_ERROR, errno, |
| 4319 |
strerror(errno), "while reading encryption iv."); |
| 4320 |
|
| 4321 |
return (DB_IO_ERROR); |
| 4322 |
} |
| 4323 |
/* Decrypt tablespace key and iv. */ |
| 4324 |
elen = my_aes_decrypt(encryption_key, Encryption::KEY_LEN, |
| 4325 |
cfg.m_encryption_metadata.m_key, transfer_key, |
| 4326 |
Encryption::KEY_LEN, my_aes_256_ecb, nullptr, false); |
| 4327 |
|
| 4328 |
if (elen == MY_AES_BAD_DATA) { |
| 4329 |
ib_senderrf(thd, IB_LOG_LEVEL_ERROR, ER_IO_READ_ERROR, errno, |
| 4330 |
strerror(errno), "while decrypt encryption key."); |
| 4331 |
|
| 4332 |
return (DB_IO_ERROR); |
| 4333 |
} |
| 4334 |
|
| 4335 |
elen = my_aes_decrypt(encryption_iv, Encryption::KEY_LEN, |
| 4336 |
cfg.m_encryption_metadata.m_iv, transfer_key, |
| 4337 |
Encryption::KEY_LEN, my_aes_256_ecb, nullptr, false); |
| 4338 |
|
| 4339 |
if (elen == MY_AES_BAD_DATA) { |
| 4340 |
ib_senderrf(thd, IB_LOG_LEVEL_ERROR, ER_IO_READ_ERROR, errno, |
| 4341 |
strerror(errno), "while decrypt encryption iv."); |
| 4342 |
|
| 4343 |
return (DB_IO_ERROR); |
| 4344 |
} |
| 4345 |
cfg.m_encryption_metadata.m_type = Encryption::Type::AES; |
| 4346 |
cfg.m_encryption_metadata.m_key_len = Encryption::KEY_LEN; |
| 4347 |
|
| 4348 |
return (DB_SUCCESS); |
| 4349 |
} |
| 4350 |
|
| 4351 |
/** Read the contents of the .cfp file. |
| 4352 |
@param[in] table table |
| 4353 |
@param[in] thd session |
| 4354 |
@param[in,out] import meta data |
| 4355 |
@return DB_SUCCESS or error code. */ |
| 4356 |
static dberr_t row_import_read_cfp(dict_table_t *table, THD *thd, |
| 4357 |
row_import &import) { |
| 4358 |
dberr_t err; |
| 4359 |
char name[OS_FILE_MAX_PATH]; |
| 4360 |
|
| 4361 |
/* Clear table encryption information. */ |
| 4362 |
import.m_encryption_metadata.m_type = Encryption::Type::NONE; |
| 4363 |
|
| 4364 |
srv_get_encryption_data_filename(table, name, sizeof(name)); |
| 4365 |
|
| 4366 |
fil_adjust_name_import(table, name, CFP); |
| 4367 |
|
| 4368 |
FILE *file = fopen(name, "rb"); |
| 4369 |
|
| 4370 |
if (file != nullptr) { |
| 4371 |
import.m_cfp_missing = false; |
| 4372 |
err = row_import_read_encryption_data(import, file, thd); |
| 4373 |
fclose(file); |
| 4374 |
} else { |
| 4375 |
/* If there's no cfp file, we assume it's not an |
| 4376 |
encrpyted table. return directly. */ |
| 4377 |
import.m_cfp_missing = true; |
| 4378 |
err = DB_SUCCESS; |
| 4379 |
} |
| 4380 |
return (err); |
| 4381 |
} |
| 4382 |
|
| 4383 |
/** Check the correctness of clustered index of imported table. |
| 4384 |
Once there is corruption found, the IMPORT would be refused. This can |
| 4385 |
help to detect the missing .cfg file for a table with instant added columns. |
| 4386 |
@param[in,out] table InnoDB table object |
| 4387 |
@param[in,out] thd MySQL session variable |
| 4388 |
@param[in] missing true if .cfg file is missing |
| 4389 |
@return DB_SUCCESS or error code. */ |
| 4390 |
dberr_t row_import_check_corruption(dict_table_t *table, THD *thd, |
| 4391 |
bool missing) { |
| 4392 |
dict_index_t *clust_index = table->first_index(); |
| 4393 |
if (!btr_validate_index(clust_index, nullptr, true)) { |
| 4394 |
if (missing) { |
| 4395 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 4396 |
"Clustered index validation failed. Because" |
| 4397 |
" the .cfg file is missing, table definition" |
| 4398 |
" of the IBD file could be different. Or" |
| 4399 |
" the data file itself is already corrupted."); |
| 4400 |
} else { |
| 4401 |
ib_errf(thd, IB_LOG_LEVEL_ERROR, ER_INNODB_INDEX_CORRUPT, |
| 4402 |
"Clustered index validation failed, due to" |
| 4403 |
" data file corruption."); |
| 4404 |
} |
| 4405 |
return DB_CORRUPTION; |
| 4406 |
} |
| 4407 |
|
| 4408 |
/* Check secondary indexes for corruption. If an index is found corrupt, mark |
| 4409 |
it but allow IMPORT to continue as the corrupt index can be recreated from the |
| 4410 |
clustered index. Any FTS index is already marked corrupt and skipped. */ |
| 4411 |
for (dict_index_t *index = clust_index->next(); index != nullptr; |
| 4412 |
index = index->next()) { |
| 4413 |
if (!index->is_corrupted() && !btr_validate_index(index, nullptr, true)) { |
| 4414 |
ib_errf(thd, IB_LOG_LEVEL_WARN, ER_INNODB_INDEX_CORRUPT, |
| 4415 |
"Index '%s' is found to be corrupt and should be recreated.", |
| 4416 |
index->name()); |
| 4417 |
dict_set_corrupted(index); |
| 4418 |
} |
| 4419 |
} |
| 4420 |
return DB_SUCCESS; |
| 4421 |
} |
| 4422 |
|
| 4423 |
/** Imports a tablespace. The space id in the .ibd file must match the space id |
| 4424 |
of the table in the data dictionary. |
| 4425 |
@param[in] table table |
| 4426 |
@param[in] table_def dd table |
| 4427 |
@param[in] prebuilt prebuilt struct in MySQL |
| 4428 |
@return error code or DB_SUCCESS */ |
| 4429 |
dberr_t row_import_for_mysql(dict_table_t *table, dd::Table *table_def, |
| 4430 |
row_prebuilt_t *prebuilt) { |
| 4431 |
dberr_t err; |
| 4432 |
trx_t *trx; |
| 4433 |
uint64_t autoinc = 0; |
| 4434 |
char *filepath = nullptr; |
| 4435 |
|
| 4436 |
/* The caller assured that this is not read_only_mode and that no |
| 4437 |
temporary tablespace is being imported. */ |
| 4438 |
ut_ad(!srv_read_only_mode); |
| 4439 |
ut_ad(!table->is_temporary()); |
| 4440 |
|
| 4441 |
ut_a(table->space); |
| 4442 |
ut_ad(prebuilt->trx); |
| 4443 |
ut_a(table->ibd_file_missing); |
| 4444 |
|
| 4445 |
ibuf_delete_for_discarded_space(table->space); |
| 4446 |
|
| 4447 |
trx_start_if_not_started(prebuilt->trx, true, UT_LOCATION_HERE); |
| 4448 |
|
| 4449 |
trx = trx_allocate_for_mysql(); |
| 4450 |
|
| 4451 |
/* So that the table is not DROPped during recovery. */ |
| 4452 |
trx_set_dict_operation(trx, TRX_DICT_OP_INDEX); |
| 4453 |
|
| 4454 |
trx_start_if_not_started(trx, true, UT_LOCATION_HERE); |
| 4455 |
|
| 4456 |
/* So that we can send error messages to the user. */ |
| 4457 |
trx->mysql_thd = prebuilt->trx->mysql_thd; |
| 4458 |
|
| 4459 |
/* Assign an undo segment for the transaction, so that the |
| 4460 |
transaction will be recovered after a crash. */ |
| 4461 |
|
| 4462 |
mutex_enter(&trx->undo_mutex); |
| 4463 |
|
| 4464 |
/* IMPORT tablespace is blocked for temp-tables and so we don't |
| 4465 |
need to assign temporary rollback segment for this trx. */ |
| 4466 |
err = trx_undo_assign_undo(trx, &trx->rsegs.m_redo, TRX_UNDO_UPDATE); |
| 4467 |
|
| 4468 |
mutex_exit(&trx->undo_mutex); |
| 4469 |
|
| 4470 |
if (err != DB_SUCCESS) { |
| 4471 |
return (row_import_cleanup(prebuilt, trx, err)); |
| 4472 |
|
| 4473 |
} else if (trx->rsegs.m_redo.update_undo == nullptr) { |
| 4474 |
err = DB_TOO_MANY_CONCURRENT_TRXS; |
| 4475 |
return (row_import_cleanup(prebuilt, trx, err)); |
| 4476 |
} |
| 4477 |
|
| 4478 |
ut_a(err == DB_SUCCESS); |
| 4479 |
|
| 4480 |
prebuilt->trx->op_info = "read meta-data file"; |
| 4481 |
|
| 4482 |
/* Prevent DDL operations while we are checking. */ |
| 4483 |
rw_lock_s_lock_func(dict_operation_lock, 0, UT_LOCATION_HERE); |
| 4484 |
|
| 4485 |
row_import cfg{}; |
| 4486 |
ulint space_flags = 0; |
| 4487 |
|
| 4488 |
/* Read CFP file */ |
| 4489 |
if (dd_is_table_in_encrypted_tablespace(table)) { |
| 4490 |
/* First try to read CFP file here. */ |
| 4491 |
err = row_import_read_cfp(table, trx->mysql_thd, cfg); |
| 4492 |
ut_ad(cfg.m_cfp_missing || err == DB_SUCCESS); |
| 4493 |
|
| 4494 |
if (err != DB_SUCCESS) { |
| 4495 |
rw_lock_s_unlock_gen(dict_operation_lock, 0); |
| 4496 |
return (row_import_error(prebuilt, trx, err)); |
| 4497 |
} |
| 4498 |
|
| 4499 |
/* If table is encrypted, but can't find cfp file, return error. */ |
| 4500 |
if (cfg.m_cfp_missing) { |
| 4501 |
ib_errf(trx->mysql_thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 4502 |
"Table is in an encrypted tablespace, but the encryption" |
| 4503 |
" meta-data file cannot be found while importing."); |
| 4504 |
err = DB_ERROR; |
| 4505 |
rw_lock_s_unlock_gen(dict_operation_lock, 0); |
| 4506 |
return (row_import_error(prebuilt, trx, err)); |
| 4507 |
} else { |
| 4508 |
/* If CFP file is read, encryption_key must have been populted. */ |
| 4509 |
ut_ad(cfg.m_encryption_metadata.can_encrypt()); |
| 4510 |
} |
| 4511 |
} |
| 4512 |
|
| 4513 |
/* Read CFG file */ |
| 4514 |
err = row_import_read_cfg(table, table_def, trx->mysql_thd, cfg); |
| 4515 |
|
| 4516 |
/* Check if the table column definitions match the contents |
| 4517 |
of the config file. */ |
| 4518 |
|
| 4519 |
if (err == DB_SUCCESS) { |
| 4520 |
/* We have a schema file, try and match it with our |
| 4521 |
data dictionary. */ |
| 4522 |
|
| 4523 |
if (err == DB_SUCCESS) { |
| 4524 |
err = cfg.match_schema(trx->mysql_thd, table_def); |
| 4525 |
} |
| 4526 |
|
| 4527 |
/* Update index->page and SYS_INDEXES.PAGE_NO to match the |
| 4528 |
B-tree root page numbers in the tablespace. Use the index |
| 4529 |
name from the .cfg file to find match. */ |
| 4530 |
|
| 4531 |
if (err == DB_SUCCESS) { |
| 4532 |
cfg.set_root_by_name(); |
| 4533 |
autoinc = cfg.m_autoinc; |
| 4534 |
} |
| 4535 |
|
| 4536 |
rw_lock_s_unlock_gen(dict_operation_lock, 0); |
| 4537 |
|
| 4538 |
} else if (cfg.m_missing) { |
| 4539 |
rw_lock_s_unlock_gen(dict_operation_lock, 0); |
| 4540 |
|
| 4541 |
/* We don't have a schema file, we will have to discover |
| 4542 |
the index root pages from the .ibd file and skip the schema |
| 4543 |
matching step. */ |
| 4544 |
|
| 4545 |
ut_a(err == DB_FAIL); |
| 4546 |
|
| 4547 |
cfg.m_page_size.copy_from(univ_page_size); |
| 4548 |
|
| 4549 |
/* Check and store compression type. */ |
| 4550 |
Compression compression; |
| 4551 |
|
| 4552 |
err = Compression::check(prebuilt->m_mysql_table->s->compress.str, |
| 4553 |
&compression); |
| 4554 |
|
| 4555 |
ut_a(err == DB_SUCCESS); |
| 4556 |
cfg.m_compression_type = compression.m_type; |
| 4557 |
|
| 4558 |
FetchIndexRootPages fetchIndexRootPages(table, trx); |
| 4559 |
|
| 4560 |
err = fil_tablespace_iterate( |
| 4561 |
cfg.m_encryption_metadata, table, |
| 4562 |
IO_BUFFER_SIZE(cfg.m_page_size.physical(), cfg.m_page_size.physical()), |
| 4563 |
cfg.m_compression_type, fetchIndexRootPages); |
| 4564 |
|
| 4565 |
if (err == DB_SCHEMA_MISMATCH) { |
| 4566 |
ib_errf(trx->mysql_thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 4567 |
"CFG file is missing and source table is found to have row " |
| 4568 |
"versions. CFG file is must to IMPORT tables with row versions."); |
| 4569 |
return (row_import_cleanup(prebuilt, trx, err)); |
| 4570 |
} |
| 4571 |
|
| 4572 |
if (err == DB_SUCCESS) { |
| 4573 |
err = fetchIndexRootPages.build_row_import(&cfg); |
| 4574 |
|
| 4575 |
/* Update index->page and SYS_INDEXES.PAGE_NO |
| 4576 |
to match the B-tree root page numbers in the |
| 4577 |
tablespace. */ |
| 4578 |
|
| 4579 |
if (err == DB_SUCCESS) { |
| 4580 |
err = cfg.set_root_by_heuristic(); |
| 4581 |
} |
| 4582 |
} |
| 4583 |
|
| 4584 |
space_flags = fetchIndexRootPages.get_space_flags(); |
| 4585 |
|
| 4586 |
/* If the fsp flag is set for data_dir, but table flag is not set |
| 4587 |
for data_dir or vice versa then return error. */ |
| 4588 |
if (err == DB_SUCCESS && FSP_FLAGS_HAS_DATA_DIR(space_flags) != |
| 4589 |
DICT_TF_HAS_DATA_DIR(table->flags)) { |
| 4590 |
ib_errf(trx->mysql_thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 4591 |
"Table location flags do not match. The source table %s a " |
| 4592 |
"DATA DIRECTORY but the destination table %s.", |
| 4593 |
(FSP_FLAGS_HAS_DATA_DIR(space_flags) ? "uses" : "does not use"), |
| 4594 |
(DICT_TF_HAS_DATA_DIR(table->flags) ? "does" : "does not")); |
| 4595 |
err = DB_ERROR; |
| 4596 |
return (row_import_error(prebuilt, trx, err)); |
| 4597 |
} |
| 4598 |
} else { |
| 4599 |
rw_lock_s_unlock_gen(dict_operation_lock, 0); |
| 4600 |
} |
| 4601 |
|
| 4602 |
if (err != DB_SUCCESS) { |
| 4603 |
if (err == DB_IO_NO_ENCRYPT_TABLESPACE) { |
| 4604 |
ib_errf( |
| 4605 |
trx->mysql_thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 4606 |
"Encryption attribute in the file does not match the dictionary."); |
| 4607 |
|
| 4608 |
return (row_import_cleanup(prebuilt, trx, err)); |
| 4609 |
} |
| 4610 |
return (row_import_error(prebuilt, trx, err)); |
| 4611 |
} |
| 4612 |
|
| 4613 |
/* At this point, all required information has been collected for IMPORT. */ |
| 4614 |
|
| 4615 |
prebuilt->trx->op_info = "importing tablespace"; |
| 4616 |
|
| 4617 |
ib::info(ER_IB_MSG_949) << "Phase I - Update all pages"; |
| 4618 |
|
| 4619 |
/* Iterate over all the pages and do the sanity checking and |
| 4620 |
the conversion required to import the tablespace. */ |
| 4621 |
|
| 4622 |
PageConverter converter(&cfg, trx); |
| 4623 |
|
| 4624 |
/* Set the IO buffer size in pages. */ |
| 4625 |
|
| 4626 |
err = fil_tablespace_iterate( |
| 4627 |
cfg.m_encryption_metadata, table, |
| 4628 |
IO_BUFFER_SIZE(cfg.m_page_size.physical(), cfg.m_page_size.physical()), |
| 4629 |
cfg.m_compression_type, converter); |
| 4630 |
|
| 4631 |
if (err == DB_SCHEMA_MISMATCH) { |
| 4632 |
ib_errf(trx->mysql_thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 4633 |
"CFG file is missing and source table is found to have row " |
| 4634 |
"versions. CFG file is must to IMPORT tables with row versions."); |
| 4635 |
return (row_import_cleanup(prebuilt, trx, err)); |
| 4636 |
} |
| 4637 |
|
| 4638 |
DBUG_EXECUTE_IF("ib_import_reset_space_and_lsn_failure", |
| 4639 |
err = DB_TOO_MANY_CONCURRENT_TRXS;); |
| 4640 |
|
| 4641 |
if (err == DB_IO_NO_ENCRYPT_TABLESPACE) { |
| 4642 |
ib_errf(trx->mysql_thd, IB_LOG_LEVEL_ERROR, ER_TABLE_SCHEMA_MISMATCH, |
| 4643 |
"Encryption attribute in the file does not match the dictionary."); |
| 4644 |
|
| 4645 |
return (row_import_cleanup(prebuilt, trx, err)); |
| 4646 |
} |
| 4647 |
|
| 4648 |
if (err != DB_SUCCESS) { |
| 4649 |
char table_name[MAX_FULL_NAME_LEN + 1]; |
| 4650 |
|
| 4651 |
innobase_format_name(table_name, sizeof(table_name), table->name.m_name); |
| 4652 |
|
| 4653 |
ib_errf(trx->mysql_thd, IB_LOG_LEVEL_ERROR, ER_INTERNAL_ERROR, |
| 4654 |
"Cannot reset LSNs in table %s : %s", table_name, ut_strerr(err)); |
| 4655 |
|
| 4656 |
return (row_import_cleanup(prebuilt, trx, err)); |
| 4657 |
} |
| 4658 |
|
| 4659 |
row_mysql_lock_data_dictionary(trx, UT_LOCATION_HERE); |
| 4660 |
|
| 4661 |
if (table->has_instant_cols() || table->has_row_versions()) { |
| 4662 |
dd_import_instant_add_columns(table, table_def); |
| 4663 |
} |
| 4664 |
|
| 4665 |
/* If the table is stored in a remote tablespace, we need to |
| 4666 |
determine that filepath from the link file and system tables. |
| 4667 |
Find the space ID in SYS_TABLES since this is an ALTER TABLE. */ |
| 4668 |
dd_get_and_save_data_dir_path(table, table_def, true); |
| 4669 |
|
| 4670 |
if (DICT_TF_HAS_DATA_DIR(table->flags)) { |
| 4671 |
ut_a(table->data_dir_path != nullptr); |
| 4672 |
|
| 4673 |
const auto dir = table->data_dir_path; |
| 4674 |
|
| 4675 |
filepath = Fil_path::make(dir, table->name.m_name, IBD, true); |
| 4676 |
} else { |
| 4677 |
filepath = Fil_path::make_ibd_from_table_name(table->name.m_name); |
| 4678 |
} |
| 4679 |
|
| 4680 |
DBUG_EXECUTE_IF("ib_import_OOM_15", ut::free(filepath); filepath = nullptr;); |
| 4681 |
|
| 4682 |
if (filepath == nullptr) { |
| 4683 |
row_mysql_unlock_data_dictionary(trx); |
| 4684 |
return (row_import_cleanup(prebuilt, trx, DB_OUT_OF_MEMORY)); |
| 4685 |
} |
| 4686 |
|
| 4687 |
/* Open the tablespace so that we can access via the buffer pool. |
| 4688 |
The tablespace is initially opened as a temporary one, because |
| 4689 |
we will not be writing any redo log for it before we have invoked |
| 4690 |
fil_space_set_imported() to declare it a persistent tablespace. */ |
| 4691 |
|
| 4692 |
uint32_t fsp_flags = dict_tf_to_fsp_flags(table->flags); |
| 4693 |
if (cfg.m_encryption_metadata.can_encrypt()) { |
| 4694 |
fsp_flags_set_encryption(fsp_flags); |
| 4695 |
} |
| 4696 |
|
| 4697 |
std::string tablespace_name(table->name.m_name); |
| 4698 |
dict_name::convert_to_space(tablespace_name); |
| 4699 |
|
| 4700 |
err = fil_ibd_open(true, FIL_TYPE_IMPORT, table->space, fsp_flags, |
| 4701 |
tablespace_name.c_str(), filepath, true, false); |
| 4702 |
|
| 4703 |
DBUG_EXECUTE_IF("ib_import_open_tablespace_failure", |
| 4704 |
err = DB_TABLESPACE_NOT_FOUND;); |
| 4705 |
|
| 4706 |
if (err != DB_SUCCESS) { |
| 4707 |
row_mysql_unlock_data_dictionary(trx); |
| 4708 |
|
| 4709 |
ib_senderrf(trx->mysql_thd, IB_LOG_LEVEL_ERROR, ER_FILE_NOT_FOUND, filepath, |
| 4710 |
err, ut_strerr(err)); |
| 4711 |
|
| 4712 |
ut::free(filepath); |
| 4713 |
|
| 4714 |
return row_import_cleanup(prebuilt, trx, err); |
| 4715 |
} |
| 4716 |
|
| 4717 |
/* For encrypted tablespace, set encryption information. */ |
| 4718 |
if (FSP_FLAGS_GET_ENCRYPTION(fsp_flags)) { |
| 4719 |
ut_ad(cfg.m_encryption_metadata.can_encrypt()); |
| 4720 |
err = fil_set_encryption(table->space, cfg.m_encryption_metadata.m_type, |
| 4721 |
cfg.m_encryption_metadata.m_key, |
| 4722 |
cfg.m_encryption_metadata.m_iv); |
| 4723 |
} |
| 4724 |
|
| 4725 |
const char *compression_algorithm = |
| 4726 |
Compression::to_string(cfg.m_compression_type); |
| 4727 |
|
| 4728 |
if (err == DB_SUCCESS && !Compression::is_none(compression_algorithm)) { |
| 4729 |
err = dict_set_compression(table, compression_algorithm, true); |
| 4730 |
} |
| 4731 |
|
| 4732 |
row_mysql_unlock_data_dictionary(trx); |
| 4733 |
|
| 4734 |
if (err != DB_SUCCESS) { |
| 4735 |
return row_import_cleanup(prebuilt, trx, err); |
| 4736 |
} |
| 4737 |
|
| 4738 |
/* Set the autoextend_size attribute. */ |
| 4739 |
{ |
| 4740 |
auto dc = dd::get_dd_client(trx->mysql_thd); |
| 4741 |
dd::cache::Dictionary_client::Auto_releaser releaser(dc); |
| 4742 |
uint64_t autoextend_size{}; |
| 4743 |
if (!dd_get_tablespace_size_option(dc, table->dd_space_id, |
| 4744 |
&autoextend_size)) { |
| 4745 |
ut_d(dberr_t ret =) |
| 4746 |
fil_set_autoextend_size(table->space, autoextend_size); |
| 4747 |
ut_ad(ret == DB_SUCCESS); |
| 4748 |
} |
| 4749 |
} |
| 4750 |
|
| 4751 |
ut::free(filepath); |
| 4752 |
|
| 4753 |
err = ibuf_check_bitmap_on_import(trx, table->space); |
| 4754 |
|
| 4755 |
DBUG_EXECUTE_IF("ib_import_check_bitmap_failure", err = DB_CORRUPTION;); |
| 4756 |
|
| 4757 |
if (err != DB_SUCCESS) { |
| 4758 |
return (row_import_cleanup(prebuilt, trx, err)); |
| 4759 |
} |
| 4760 |
|
| 4761 |
/* The first index must always be the clustered index. */ |
| 4762 |
|
| 4763 |
dict_index_t *index = table->first_index(); |
| 4764 |
|
| 4765 |
if (!index->is_clustered()) { |
| 4766 |
return (row_import_error(prebuilt, trx, DB_CORRUPTION)); |
| 4767 |
} |
| 4768 |
|
| 4769 |
/* Update the Btree segment headers for index node and |
| 4770 |
leaf nodes in the root page. Set the new space id. */ |
| 4771 |
|
| 4772 |
err = btr_root_adjust_on_import(index); |
| 4773 |
|
| 4774 |
DBUG_EXECUTE_IF("ib_import_cluster_root_adjust_failure", |
| 4775 |
err = DB_CORRUPTION;); |
| 4776 |
|
| 4777 |
if (err != DB_SUCCESS) { |
| 4778 |
return (row_import_error(prebuilt, trx, err)); |
| 4779 |
} |
| 4780 |
|
| 4781 |
DBUG_EXECUTE_IF("ib_import_page_corrupt", |
| 4782 |
row_index_t *i_index = cfg.get_index(index->name); |
| 4783 |
++i_index->m_stats.m_n_purge_failed;); |
| 4784 |
|
| 4785 |
if (err != DB_SUCCESS) { |
| 4786 |
return (row_import_error(prebuilt, trx, err)); |
| 4787 |
} else if (cfg.requires_purge(index->name)) { |
| 4788 |
/* Purge any delete-marked records that couldn't be |
| 4789 |
purged during the page conversion phase from the |
| 4790 |
cluster index. */ |
| 4791 |
|
| 4792 |
IndexPurge purge(trx, index); |
| 4793 |
|
| 4794 |
trx->op_info = "cluster: purging delete marked records"; |
| 4795 |
|
| 4796 |
err = purge.garbage_collect(); |
| 4797 |
|
| 4798 |
trx->op_info = ""; |
| 4799 |
} |
| 4800 |
|
| 4801 |
DBUG_EXECUTE_IF("ib_import_cluster_failure", err = DB_CORRUPTION;); |
| 4802 |
|
| 4803 |
if (err != DB_SUCCESS) { |
| 4804 |
return (row_import_error(prebuilt, trx, err)); |
| 4805 |
} |
| 4806 |
|
| 4807 |
/* For secondary indexes, purge any records that couldn't be purged |
| 4808 |
during the page conversion phase. */ |
| 4809 |
|
| 4810 |
err = row_import_adjust_root_pages_of_secondary_indexes(trx, table, cfg); |
| 4811 |
|
| 4812 |
DBUG_EXECUTE_IF("ib_import_sec_root_adjust_failure", err = DB_CORRUPTION;); |
| 4813 |
|
| 4814 |
if (err != DB_SUCCESS) { |
| 4815 |
return (row_import_error(prebuilt, trx, err)); |
| 4816 |
} |
| 4817 |
|
| 4818 |
/* Ensure that the next available DB_ROW_ID is not smaller than |
| 4819 |
any DB_ROW_ID stored in the table. */ |
| 4820 |
|
| 4821 |
if (prebuilt->clust_index_was_generated) { |
| 4822 |
err = row_import_set_sys_max_row_id(prebuilt, table); |
| 4823 |
|
| 4824 |
if (err != DB_SUCCESS) { |
| 4825 |
return (row_import_error(prebuilt, trx, err)); |
| 4826 |
} |
| 4827 |
} |
| 4828 |
|
| 4829 |
fil_space_t *space = fil_space_acquire(table->space); |
| 4830 |
|
| 4831 |
/* Update Btree segment headers for SDI Index */ |
| 4832 |
if (FSP_FLAGS_HAS_SDI(space->flags)) { |
| 4833 |
dict_mutex_enter_for_mysql(); |
| 4834 |
dict_index_t *sdi_index = dict_sdi_get_index(table->space); |
| 4835 |
dict_mutex_exit_for_mysql(); |
| 4836 |
|
| 4837 |
err = btr_root_adjust_on_import(sdi_index); |
| 4838 |
|
| 4839 |
if (err != DB_SUCCESS) { |
| 4840 |
fil_space_release(space); |
| 4841 |
return (row_import_error(prebuilt, trx, err)); |
| 4842 |
} |
| 4843 |
} |
| 4844 |
fil_space_release(space); |
| 4845 |
|
| 4846 |
ib::info(ER_IB_MSG_950) << "Phase III - Flush changes to disk"; |
| 4847 |
|
| 4848 |
/* Ensure that all pages dirtied during the IMPORT make it to disk. |
| 4849 |
The only dirty pages generated should be from the pessimistic purge |
| 4850 |
of delete marked records that couldn't be purged in Phase I. */ |
| 4851 |
|
| 4852 |
buf_LRU_flush_or_remove_pages(prebuilt->table->space, BUF_REMOVE_FLUSH_WRITE, |
| 4853 |
trx); |
| 4854 |
|
| 4855 |
if (trx_is_interrupted(trx)) { |
| 4856 |
ib::info(ER_IB_MSG_951) << "Phase III - Flush interrupted"; |
| 4857 |
return (row_import_error(prebuilt, trx, DB_INTERRUPTED)); |
| 4858 |
} |
| 4859 |
|
| 4860 |
ib::info(ER_IB_MSG_952) << "Phase IV - Flush complete"; |
| 4861 |
fil_space_set_imported(prebuilt->table->space); |
| 4862 |
|
| 4863 |
/* Check if the on-disk .ibd file doesn't have SDI index. |
| 4864 |
If it doesn't exist, create SDI Index page now. */ |
| 4865 |
mtr_t mtr; |
| 4866 |
mtr.start(); |
| 4867 |
buf_block_t *block = |
| 4868 |
buf_page_get(page_id_t(table->space, 0), dict_table_page_size(table), |
| 4869 |
RW_SX_LATCH, UT_LOCATION_HERE, &mtr); |
| 4870 |
|
| 4871 |
buf_block_dbg_add_level(block, SYNC_FSP_PAGE); |
| 4872 |
|
| 4873 |
page_t *page = buf_block_get_frame(block); |
| 4874 |
|
| 4875 |
ulint space_flags_from_disk = fsp_header_get_field(page, FSP_SPACE_FLAGS); |
| 4876 |
mtr.commit(); |
| 4877 |
|
| 4878 |
if (!FSP_FLAGS_HAS_SDI(space_flags_from_disk)) { |
| 4879 |
/* This is IMPORT from 5.7 .ibd file or pre 8.0.1 */ |
| 4880 |
dict_mutex_enter_for_mysql(); |
| 4881 |
dict_sdi_remove_from_cache(table->space, nullptr, true); |
| 4882 |
btr_sdi_create_index(table->space, true); |
| 4883 |
dict_mutex_exit_for_mysql(); |
| 4884 |
/* Update server and space version number in the page 0 of tablespace */ |
| 4885 |
if (upgrade_space_version(table->space, false)) { |
| 4886 |
return (row_import_error(prebuilt, trx, DB_TABLESPACE_NOT_FOUND)); |
| 4887 |
} |
| 4888 |
} else { |
| 4889 |
ut_ad(space->flags == space_flags_from_disk); |
| 4890 |
} |
| 4891 |
|
| 4892 |
if (dd_is_table_in_encrypted_tablespace(table)) { |
| 4893 |
mtr_t mtr; |
| 4894 |
byte encrypt_info[Encryption::INFO_SIZE]; |
| 4895 |
|
| 4896 |
fil_space_t *space = fil_space_get(table->space); |
| 4897 |
|
| 4898 |
mtr_start(&mtr); |
| 4899 |
|
| 4900 |
mtr_x_lock_space(space, &mtr); |
| 4901 |
|
| 4902 |
memset(encrypt_info, 0, Encryption::INFO_SIZE); |
| 4903 |
|
| 4904 |
if (!fsp_header_rotate_encryption(space, encrypt_info, &mtr)) { |
| 4905 |
mtr_commit(&mtr); |
| 4906 |
return (row_import_cleanup(prebuilt, trx, DB_ERROR)); |
| 4907 |
} |
| 4908 |
|
| 4909 |
mtr_commit(&mtr); |
| 4910 |
} |
| 4911 |
|
| 4912 |
/* The dictionary latches will be released in in row_import_cleanup() |
| 4913 |
after the transaction commit, for both success and error. */ |
| 4914 |
|
| 4915 |
row_mysql_lock_data_dictionary(trx, UT_LOCATION_HERE); |
| 4916 |
|
| 4917 |
DBUG_EXECUTE_IF("ib_import_internal_error", trx->error_state = DB_ERROR; |
| 4918 |
err = DB_ERROR; |
| 4919 |
ib_errf(trx->mysql_thd, IB_LOG_LEVEL_ERROR, ER_INTERNAL_ERROR, |
| 4920 |
"While importing table %s", table->name.m_name); |
| 4921 |
return (row_import_error(prebuilt, trx, err));); |
| 4922 |
|
| 4923 |
table->ibd_file_missing = false; |
| 4924 |
table->flags2 &= ~DICT_TF2_DISCARDED; |
| 4925 |
|
| 4926 |
/* Set autoinc value read from cfg file. The value is set to zero |
| 4927 |
if the cfg file is missing and is initialized later from table |
| 4928 |
column value. */ |
| 4929 |
ib::info(ER_IB_MSG_953) << table->name << " autoinc value set to " << autoinc; |
| 4930 |
|
| 4931 |
dict_table_autoinc_lock(table); |
| 4932 |
dict_table_autoinc_initialize(table, autoinc); |
| 4933 |
dict_table_autoinc_unlock(table); |
| 4934 |
/* This should be set later in handler level, where we know the |
| 4935 |
autoinc counter field index */ |
| 4936 |
table->autoinc_field_no = ULINT_UNDEFINED; |
| 4937 |
|
| 4938 |
ut_a(err == DB_SUCCESS); |
| 4939 |
|
| 4940 |
/* After discard, sdi_table->ibd_file_missing is set to true. |
| 4941 |
This is avoid to purge on SDI tables after discard. |
| 4942 |
At the end of successful import, set sdi_table->ibd_file_missing to |
| 4943 |
false, indicating that .ibd of SDI table is available */ |
| 4944 |
dict_table_t *sdi_table = dict_sdi_get_table(space->id, true, false); |
| 4945 |
sdi_table->ibd_file_missing = false; |
| 4946 |
dict_sdi_close_table(sdi_table); |
| 4947 |
|
| 4948 |
row_mysql_unlock_data_dictionary(trx); |
| 4949 |
|
| 4950 |
err = row_import_check_corruption(table, trx->mysql_thd, cfg.m_missing); |
| 4951 |
|
| 4952 |
row_mysql_lock_data_dictionary(trx, UT_LOCATION_HERE); |
| 4953 |
|
| 4954 |
return (row_import_cleanup(prebuilt, trx, err)); |
| 4955 |
} |