You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1307 lines
33 KiB

10 years ago
10 years ago
12 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
  1. /*****************************************************************************
  2. Copyright (c) 1996, 2015, Oracle and/or its affiliates. All Rights Reserved.
  3. This program is free software; you can redistribute it and/or modify it under
  4. the terms of the GNU General Public License as published by the Free Software
  5. Foundation; version 2 of the License.
  6. This program is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along with
  10. this program; if not, write to the Free Software Foundation, Inc.,
  11. 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
  12. *****************************************************************************/
  13. /**************************************************//**
  14. @file trx/trx0sys.cc
  15. Transaction system
  16. Created 3/26/1996 Heikki Tuuri
  17. *******************************************************/
  18. #include "trx0sys.h"
  19. #ifdef UNIV_NONINL
  20. #include "trx0sys.ic"
  21. #endif
  22. #ifdef UNIV_HOTBACKUP
  23. #include "fsp0types.h"
  24. #else /* !UNIV_HOTBACKUP */
  25. #include "fsp0fsp.h"
  26. #include "mtr0log.h"
  27. #include "mtr0log.h"
  28. #include "trx0trx.h"
  29. #include "trx0rseg.h"
  30. #include "trx0undo.h"
  31. #include "srv0srv.h"
  32. #include "srv0start.h"
  33. #include "trx0purge.h"
  34. #include "log0log.h"
  35. #include "log0recv.h"
  36. #include "os0file.h"
  37. #include "read0read.h"
  38. /** The file format tag structure with id and name. */
  39. struct file_format_t {
  40. ulint id; /*!< id of the file format */
  41. const char* name; /*!< text representation of the
  42. file format */
  43. ib_mutex_t mutex; /*!< covers changes to the above
  44. fields */
  45. };
  46. /** The transaction system */
  47. UNIV_INTERN trx_sys_t* trx_sys = NULL;
  48. /** In a MySQL replication slave, in crash recovery we store the master log
  49. file name and position here. */
  50. /* @{ */
  51. /** Master binlog file name */
  52. UNIV_INTERN char trx_sys_mysql_master_log_name[TRX_SYS_MYSQL_LOG_NAME_LEN];
  53. /** Master binlog file position. We have successfully got the updates
  54. up to this position. -1 means that no crash recovery was needed, or
  55. there was no master log position info inside InnoDB.*/
  56. UNIV_INTERN ib_int64_t trx_sys_mysql_master_log_pos = -1;
  57. /* @} */
  58. /** If this MySQL server uses binary logging, after InnoDB has been inited
  59. and if it has done a crash recovery, we store the binlog file name and position
  60. here. */
  61. /* @{ */
  62. /** Binlog file name */
  63. UNIV_INTERN char trx_sys_mysql_bin_log_name[TRX_SYS_MYSQL_LOG_NAME_LEN];
  64. /** Binlog file position, or -1 if unknown */
  65. UNIV_INTERN ib_int64_t trx_sys_mysql_bin_log_pos = -1;
  66. /* @} */
  67. #endif /* !UNIV_HOTBACKUP */
  68. /** List of animal names representing file format. */
  69. static const char* file_format_name_map[] = {
  70. "Antelope",
  71. "Barracuda",
  72. "Cheetah",
  73. "Dragon",
  74. "Elk",
  75. "Fox",
  76. "Gazelle",
  77. "Hornet",
  78. "Impala",
  79. "Jaguar",
  80. "Kangaroo",
  81. "Leopard",
  82. "Moose",
  83. "Nautilus",
  84. "Ocelot",
  85. "Porpoise",
  86. "Quail",
  87. "Rabbit",
  88. "Shark",
  89. "Tiger",
  90. "Urchin",
  91. "Viper",
  92. "Whale",
  93. "Xenops",
  94. "Yak",
  95. "Zebra"
  96. };
  97. /** The number of elements in the file format name array. */
  98. static const ulint FILE_FORMAT_NAME_N
  99. = sizeof(file_format_name_map) / sizeof(file_format_name_map[0]);
  100. #ifdef UNIV_PFS_MUTEX
  101. /* Key to register the mutex with performance schema */
  102. UNIV_INTERN mysql_pfs_key_t file_format_max_mutex_key;
  103. UNIV_INTERN mysql_pfs_key_t trx_sys_mutex_key;
  104. #endif /* UNIV_PFS_RWLOCK */
  105. #ifndef UNIV_HOTBACKUP
  106. #ifdef UNIV_DEBUG
  107. /* Flag to control TRX_RSEG_N_SLOTS behavior debugging. */
  108. UNIV_INTERN uint trx_rseg_n_slots_debug = 0;
  109. #endif
  110. /** This is used to track the maximum file format id known to InnoDB. It's
  111. updated via SET GLOBAL innodb_file_format_max = 'x' or when we open
  112. or create a table. */
  113. static file_format_t file_format_max;
  114. #ifdef UNIV_DEBUG
  115. /****************************************************************//**
  116. Checks whether a trx is in one of rw_trx_list or ro_trx_list.
  117. @return TRUE if is in */
  118. UNIV_INTERN
  119. ibool
  120. trx_in_trx_list(
  121. /*============*/
  122. const trx_t* in_trx) /*!< in: transaction */
  123. {
  124. const trx_t* trx;
  125. trx_list_t* trx_list;
  126. /* Non-locking autocommits should not hold any locks. */
  127. assert_trx_in_list(in_trx);
  128. trx_list = in_trx->read_only
  129. ? &trx_sys->ro_trx_list : &trx_sys->rw_trx_list;
  130. ut_ad(mutex_own(&trx_sys->mutex));
  131. ut_ad(trx_assert_started(in_trx));
  132. for (trx = UT_LIST_GET_FIRST(*trx_list);
  133. trx != NULL && trx != in_trx;
  134. trx = UT_LIST_GET_NEXT(trx_list, trx)) {
  135. assert_trx_in_list(trx);
  136. ut_ad(trx->read_only == (trx_list == &trx_sys->ro_trx_list));
  137. }
  138. return(trx != NULL);
  139. }
  140. #endif /* UNIV_DEBUG */
  141. /*****************************************************************//**
  142. Writes the value of max_trx_id to the file based trx system header. */
  143. UNIV_INTERN
  144. void
  145. trx_sys_flush_max_trx_id(void)
  146. /*==========================*/
  147. {
  148. mtr_t mtr;
  149. trx_sysf_t* sys_header;
  150. ut_ad(mutex_own(&trx_sys->mutex));
  151. if (!srv_read_only_mode) {
  152. mtr_start(&mtr);
  153. sys_header = trx_sysf_get(&mtr);
  154. mlog_write_ull(
  155. sys_header + TRX_SYS_TRX_ID_STORE,
  156. trx_sys->max_trx_id, &mtr);
  157. mtr_commit(&mtr);
  158. }
  159. }
  160. /*****************************************************************//**
  161. Updates the offset information about the end of the MySQL binlog entry
  162. which corresponds to the transaction just being committed. In a MySQL
  163. replication slave updates the latest master binlog position up to which
  164. replication has proceeded. */
  165. UNIV_INTERN
  166. void
  167. trx_sys_update_mysql_binlog_offset(
  168. /*===============================*/
  169. const char* file_name,/*!< in: MySQL log file name */
  170. ib_int64_t offset, /*!< in: position in that log file */
  171. ulint field, /*!< in: offset of the MySQL log info field in
  172. the trx sys header */
  173. mtr_t* mtr) /*!< in: mtr */
  174. {
  175. trx_sysf_t* sys_header;
  176. if (ut_strlen(file_name) >= TRX_SYS_MYSQL_LOG_NAME_LEN) {
  177. /* We cannot fit the name to the 512 bytes we have reserved */
  178. return;
  179. }
  180. sys_header = trx_sysf_get(mtr);
  181. if (mach_read_from_4(sys_header + field
  182. + TRX_SYS_MYSQL_LOG_MAGIC_N_FLD)
  183. != TRX_SYS_MYSQL_LOG_MAGIC_N) {
  184. mlog_write_ulint(sys_header + field
  185. + TRX_SYS_MYSQL_LOG_MAGIC_N_FLD,
  186. TRX_SYS_MYSQL_LOG_MAGIC_N,
  187. MLOG_4BYTES, mtr);
  188. }
  189. if (0 != strcmp((char*) (sys_header + field + TRX_SYS_MYSQL_LOG_NAME),
  190. file_name)) {
  191. mlog_write_string(sys_header + field
  192. + TRX_SYS_MYSQL_LOG_NAME,
  193. (byte*) file_name, 1 + ut_strlen(file_name),
  194. mtr);
  195. }
  196. if (mach_read_from_4(sys_header + field
  197. + TRX_SYS_MYSQL_LOG_OFFSET_HIGH) > 0
  198. || (offset >> 32) > 0) {
  199. mlog_write_ulint(sys_header + field
  200. + TRX_SYS_MYSQL_LOG_OFFSET_HIGH,
  201. (ulint)(offset >> 32),
  202. MLOG_4BYTES, mtr);
  203. }
  204. mlog_write_ulint(sys_header + field
  205. + TRX_SYS_MYSQL_LOG_OFFSET_LOW,
  206. (ulint)(offset & 0xFFFFFFFFUL),
  207. MLOG_4BYTES, mtr);
  208. }
  209. /*****************************************************************//**
  210. Stores the MySQL binlog offset info in the trx system header if
  211. the magic number shows it valid, and print the info to stderr */
  212. UNIV_INTERN
  213. void
  214. trx_sys_print_mysql_binlog_offset(void)
  215. /*===================================*/
  216. {
  217. trx_sysf_t* sys_header;
  218. mtr_t mtr;
  219. ulint trx_sys_mysql_bin_log_pos_high;
  220. ulint trx_sys_mysql_bin_log_pos_low;
  221. mtr_start(&mtr);
  222. sys_header = trx_sysf_get(&mtr);
  223. if (mach_read_from_4(sys_header + TRX_SYS_MYSQL_LOG_INFO
  224. + TRX_SYS_MYSQL_LOG_MAGIC_N_FLD)
  225. != TRX_SYS_MYSQL_LOG_MAGIC_N) {
  226. mtr_commit(&mtr);
  227. return;
  228. }
  229. trx_sys_mysql_bin_log_pos_high = mach_read_from_4(
  230. sys_header + TRX_SYS_MYSQL_LOG_INFO
  231. + TRX_SYS_MYSQL_LOG_OFFSET_HIGH);
  232. trx_sys_mysql_bin_log_pos_low = mach_read_from_4(
  233. sys_header + TRX_SYS_MYSQL_LOG_INFO
  234. + TRX_SYS_MYSQL_LOG_OFFSET_LOW);
  235. trx_sys_mysql_bin_log_pos
  236. = (((ib_int64_t) trx_sys_mysql_bin_log_pos_high) << 32)
  237. + (ib_int64_t) trx_sys_mysql_bin_log_pos_low;
  238. ut_memcpy(trx_sys_mysql_bin_log_name,
  239. sys_header + TRX_SYS_MYSQL_LOG_INFO
  240. + TRX_SYS_MYSQL_LOG_NAME, TRX_SYS_MYSQL_LOG_NAME_LEN);
  241. fprintf(stderr,
  242. "InnoDB: Last MySQL binlog file position %lu %lu,"
  243. " file name %s\n",
  244. trx_sys_mysql_bin_log_pos_high, trx_sys_mysql_bin_log_pos_low,
  245. trx_sys_mysql_bin_log_name);
  246. mtr_commit(&mtr);
  247. }
  248. /*****************************************************************//**
  249. Prints to stderr the MySQL master log offset info in the trx system header if
  250. the magic number shows it valid. */
  251. UNIV_INTERN
  252. void
  253. trx_sys_print_mysql_master_log_pos(void)
  254. /*====================================*/
  255. {
  256. trx_sysf_t* sys_header;
  257. mtr_t mtr;
  258. mtr_start(&mtr);
  259. sys_header = trx_sysf_get(&mtr);
  260. if (mach_read_from_4(sys_header + TRX_SYS_MYSQL_MASTER_LOG_INFO
  261. + TRX_SYS_MYSQL_LOG_MAGIC_N_FLD)
  262. != TRX_SYS_MYSQL_LOG_MAGIC_N) {
  263. mtr_commit(&mtr);
  264. return;
  265. }
  266. fprintf(stderr,
  267. "InnoDB: In a MySQL replication slave the last"
  268. " master binlog file\n"
  269. "InnoDB: position %lu %lu, file name %s\n",
  270. (ulong) mach_read_from_4(sys_header
  271. + TRX_SYS_MYSQL_MASTER_LOG_INFO
  272. + TRX_SYS_MYSQL_LOG_OFFSET_HIGH),
  273. (ulong) mach_read_from_4(sys_header
  274. + TRX_SYS_MYSQL_MASTER_LOG_INFO
  275. + TRX_SYS_MYSQL_LOG_OFFSET_LOW),
  276. sys_header + TRX_SYS_MYSQL_MASTER_LOG_INFO
  277. + TRX_SYS_MYSQL_LOG_NAME);
  278. /* Copy the master log position info to global variables we can
  279. use in ha_innobase.cc to initialize glob_mi to right values */
  280. ut_memcpy(trx_sys_mysql_master_log_name,
  281. sys_header + TRX_SYS_MYSQL_MASTER_LOG_INFO
  282. + TRX_SYS_MYSQL_LOG_NAME,
  283. TRX_SYS_MYSQL_LOG_NAME_LEN);
  284. trx_sys_mysql_master_log_pos
  285. = (((ib_int64_t) mach_read_from_4(
  286. sys_header + TRX_SYS_MYSQL_MASTER_LOG_INFO
  287. + TRX_SYS_MYSQL_LOG_OFFSET_HIGH)) << 32)
  288. + ((ib_int64_t) mach_read_from_4(
  289. sys_header + TRX_SYS_MYSQL_MASTER_LOG_INFO
  290. + TRX_SYS_MYSQL_LOG_OFFSET_LOW));
  291. mtr_commit(&mtr);
  292. }
  293. /****************************************************************//**
  294. Looks for a free slot for a rollback segment in the trx system file copy.
  295. @return slot index or ULINT_UNDEFINED if not found */
  296. UNIV_INTERN
  297. ulint
  298. trx_sysf_rseg_find_free(
  299. /*====================*/
  300. mtr_t* mtr) /*!< in: mtr */
  301. {
  302. ulint i;
  303. trx_sysf_t* sys_header;
  304. sys_header = trx_sysf_get(mtr);
  305. for (i = 0; i < TRX_SYS_N_RSEGS; i++) {
  306. ulint page_no;
  307. page_no = trx_sysf_rseg_get_page_no(sys_header, i, mtr);
  308. if (page_no == FIL_NULL) {
  309. return(i);
  310. }
  311. }
  312. return(ULINT_UNDEFINED);
  313. }
  314. /*****************************************************************//**
  315. Creates the file page for the transaction system. This function is called only
  316. at the database creation, before trx_sys_init. */
  317. static
  318. void
  319. trx_sysf_create(
  320. /*============*/
  321. mtr_t* mtr) /*!< in: mtr */
  322. {
  323. trx_sysf_t* sys_header;
  324. ulint slot_no;
  325. buf_block_t* block;
  326. page_t* page;
  327. ulint page_no;
  328. byte* ptr;
  329. ulint len;
  330. ut_ad(mtr);
  331. /* Note that below we first reserve the file space x-latch, and
  332. then enter the kernel: we must do it in this order to conform
  333. to the latching order rules. */
  334. mtr_x_lock(fil_space_get_latch(TRX_SYS_SPACE, NULL), mtr);
  335. /* Create the trx sys file block in a new allocated file segment */
  336. block = fseg_create(TRX_SYS_SPACE, 0, TRX_SYS + TRX_SYS_FSEG_HEADER,
  337. mtr);
  338. buf_block_dbg_add_level(block, SYNC_TRX_SYS_HEADER);
  339. ut_a(buf_block_get_page_no(block) == TRX_SYS_PAGE_NO);
  340. page = buf_block_get_frame(block);
  341. mlog_write_ulint(page + FIL_PAGE_TYPE, FIL_PAGE_TYPE_TRX_SYS,
  342. MLOG_2BYTES, mtr);
  343. /* Reset the doublewrite buffer magic number to zero so that we
  344. know that the doublewrite buffer has not yet been created (this
  345. suppresses a Valgrind warning) */
  346. mlog_write_ulint(page + TRX_SYS_DOUBLEWRITE
  347. + TRX_SYS_DOUBLEWRITE_MAGIC, 0, MLOG_4BYTES, mtr);
  348. sys_header = trx_sysf_get(mtr);
  349. /* Start counting transaction ids from number 1 up */
  350. mach_write_to_8(sys_header + TRX_SYS_TRX_ID_STORE, 1);
  351. /* Reset the rollback segment slots. Old versions of InnoDB
  352. define TRX_SYS_N_RSEGS as 256 (TRX_SYS_OLD_N_RSEGS) and expect
  353. that the whole array is initialized. */
  354. ptr = TRX_SYS_RSEGS + sys_header;
  355. len = ut_max(TRX_SYS_OLD_N_RSEGS, TRX_SYS_N_RSEGS)
  356. * TRX_SYS_RSEG_SLOT_SIZE;
  357. memset(ptr, 0xff, len);
  358. ptr += len;
  359. ut_a(ptr <= page + (UNIV_PAGE_SIZE - FIL_PAGE_DATA_END));
  360. /* Initialize all of the page. This part used to be uninitialized. */
  361. memset(ptr, 0, UNIV_PAGE_SIZE - FIL_PAGE_DATA_END + page - ptr);
  362. mlog_log_string(sys_header, UNIV_PAGE_SIZE - FIL_PAGE_DATA_END
  363. + page - sys_header, mtr);
  364. /* Create the first rollback segment in the SYSTEM tablespace */
  365. slot_no = trx_sysf_rseg_find_free(mtr);
  366. page_no = trx_rseg_header_create(TRX_SYS_SPACE, 0, ULINT_MAX, slot_no,
  367. mtr);
  368. ut_a(slot_no == TRX_SYS_SYSTEM_RSEG_ID);
  369. ut_a(page_no == FSP_FIRST_RSEG_PAGE_NO);
  370. }
  371. /*****************************************************************//**
  372. Compare two trx_rseg_t instances on last_trx_no. */
  373. static
  374. int
  375. trx_rseg_compare_last_trx_no(
  376. /*=========================*/
  377. const void* p1, /*!< in: elem to compare */
  378. const void* p2) /*!< in: elem to compare */
  379. {
  380. ib_int64_t cmp;
  381. const rseg_queue_t* rseg_q1 = (const rseg_queue_t*) p1;
  382. const rseg_queue_t* rseg_q2 = (const rseg_queue_t*) p2;
  383. cmp = rseg_q1->trx_no - rseg_q2->trx_no;
  384. if (cmp < 0) {
  385. return(-1);
  386. } else if (cmp > 0) {
  387. return(1);
  388. }
  389. return(0);
  390. }
  391. /*****************************************************************//**
  392. Creates and initializes the central memory structures for the transaction
  393. system. This is called when the database is started.
  394. @return min binary heap of rsegs to purge */
  395. UNIV_INTERN
  396. ib_bh_t*
  397. trx_sys_init_at_db_start(void)
  398. /*==========================*/
  399. {
  400. mtr_t mtr;
  401. ib_bh_t* ib_bh;
  402. trx_sysf_t* sys_header;
  403. ib_uint64_t rows_to_undo = 0;
  404. const char* unit = "";
  405. /* We create the min binary heap here and pass ownership to
  406. purge when we init the purge sub-system. Purge is responsible
  407. for freeing the binary heap. */
  408. ib_bh = ib_bh_create(
  409. trx_rseg_compare_last_trx_no,
  410. sizeof(rseg_queue_t), TRX_SYS_N_RSEGS);
  411. mtr_start(&mtr);
  412. sys_header = trx_sysf_get(&mtr);
  413. if (srv_force_recovery < SRV_FORCE_NO_UNDO_LOG_SCAN) {
  414. trx_rseg_array_init(sys_header, ib_bh, &mtr);
  415. }
  416. /* VERY important: after the database is started, max_trx_id value is
  417. divisible by TRX_SYS_TRX_ID_WRITE_MARGIN, and the 'if' in
  418. trx_sys_get_new_trx_id will evaluate to TRUE when the function
  419. is first time called, and the value for trx id will be written
  420. to the disk-based header! Thus trx id values will not overlap when
  421. the database is repeatedly started! */
  422. trx_sys->max_trx_id = 2 * TRX_SYS_TRX_ID_WRITE_MARGIN
  423. + ut_uint64_align_up(mach_read_from_8(sys_header
  424. + TRX_SYS_TRX_ID_STORE),
  425. TRX_SYS_TRX_ID_WRITE_MARGIN);
  426. ut_d(trx_sys->rw_max_trx_id = trx_sys->max_trx_id);
  427. UT_LIST_INIT(trx_sys->mysql_trx_list);
  428. trx_dummy_sess = sess_open();
  429. trx_lists_init_at_db_start();
  430. /* This S lock is not strictly required, it is here only to satisfy
  431. the debug code (assertions). We are still running in single threaded
  432. bootstrap mode. */
  433. mutex_enter(&trx_sys->mutex);
  434. ut_a(UT_LIST_GET_LEN(trx_sys->ro_trx_list) == 0);
  435. if (UT_LIST_GET_LEN(trx_sys->rw_trx_list) > 0) {
  436. const trx_t* trx;
  437. for (trx = UT_LIST_GET_FIRST(trx_sys->rw_trx_list);
  438. trx != NULL;
  439. trx = UT_LIST_GET_NEXT(trx_list, trx)) {
  440. ut_ad(trx->is_recovered);
  441. assert_trx_in_rw_list(trx);
  442. if (trx_state_eq(trx, TRX_STATE_ACTIVE)) {
  443. rows_to_undo += trx->undo_no;
  444. }
  445. }
  446. if (rows_to_undo > 1000000000) {
  447. unit = "M";
  448. rows_to_undo = rows_to_undo / 1000000;
  449. }
  450. fprintf(stderr,
  451. "InnoDB: %lu transaction(s) which must be"
  452. " rolled back or cleaned up\n"
  453. "InnoDB: in total %lu%s row operations to undo\n",
  454. (ulong) UT_LIST_GET_LEN(trx_sys->rw_trx_list),
  455. (ulong) rows_to_undo, unit);
  456. fprintf(stderr, "InnoDB: Trx id counter is " TRX_ID_FMT "\n",
  457. trx_sys->max_trx_id);
  458. }
  459. mutex_exit(&trx_sys->mutex);
  460. UT_LIST_INIT(trx_sys->view_list);
  461. mtr_commit(&mtr);
  462. return(ib_bh);
  463. }
  464. /*****************************************************************//**
  465. Creates the trx_sys instance and initializes ib_bh and mutex. */
  466. UNIV_INTERN
  467. void
  468. trx_sys_create(void)
  469. /*================*/
  470. {
  471. ut_ad(trx_sys == NULL);
  472. trx_sys = static_cast<trx_sys_t*>(mem_zalloc(sizeof(*trx_sys)));
  473. mutex_create(trx_sys_mutex_key, &trx_sys->mutex, SYNC_TRX_SYS);
  474. }
  475. /*****************************************************************//**
  476. Creates and initializes the transaction system at the database creation. */
  477. UNIV_INTERN
  478. void
  479. trx_sys_create_sys_pages(void)
  480. /*==========================*/
  481. {
  482. mtr_t mtr;
  483. mtr_start(&mtr);
  484. trx_sysf_create(&mtr);
  485. mtr_commit(&mtr);
  486. }
  487. /*****************************************************************//**
  488. Update the file format tag.
  489. @return always TRUE */
  490. static
  491. ibool
  492. trx_sys_file_format_max_write(
  493. /*==========================*/
  494. ulint format_id, /*!< in: file format id */
  495. const char** name) /*!< out: max file format name, can
  496. be NULL */
  497. {
  498. mtr_t mtr;
  499. byte* ptr;
  500. buf_block_t* block;
  501. ib_uint64_t tag_value;
  502. mtr_start(&mtr);
  503. block = buf_page_get(
  504. TRX_SYS_SPACE, 0, TRX_SYS_PAGE_NO, RW_X_LATCH, &mtr);
  505. file_format_max.id = format_id;
  506. file_format_max.name = trx_sys_file_format_id_to_name(format_id);
  507. ptr = buf_block_get_frame(block) + TRX_SYS_FILE_FORMAT_TAG;
  508. tag_value = format_id + TRX_SYS_FILE_FORMAT_TAG_MAGIC_N;
  509. if (name) {
  510. *name = file_format_max.name;
  511. }
  512. mlog_write_ull(ptr, tag_value, &mtr);
  513. mtr_commit(&mtr);
  514. return(TRUE);
  515. }
  516. /*****************************************************************//**
  517. Read the file format tag.
  518. @return the file format or ULINT_UNDEFINED if not set. */
  519. static
  520. ulint
  521. trx_sys_file_format_max_read(void)
  522. /*==============================*/
  523. {
  524. mtr_t mtr;
  525. const byte* ptr;
  526. const buf_block_t* block;
  527. ib_id_t file_format_id;
  528. /* Since this is called during the startup phase it's safe to
  529. read the value without a covering mutex. */
  530. mtr_start(&mtr);
  531. block = buf_page_get(
  532. TRX_SYS_SPACE, 0, TRX_SYS_PAGE_NO, RW_X_LATCH, &mtr);
  533. ptr = buf_block_get_frame(block) + TRX_SYS_FILE_FORMAT_TAG;
  534. file_format_id = mach_read_from_8(ptr);
  535. mtr_commit(&mtr);
  536. file_format_id -= TRX_SYS_FILE_FORMAT_TAG_MAGIC_N;
  537. if (file_format_id >= FILE_FORMAT_NAME_N) {
  538. /* Either it has never been tagged, or garbage in it. */
  539. return(ULINT_UNDEFINED);
  540. }
  541. return((ulint) file_format_id);
  542. }
  543. /*****************************************************************//**
  544. Get the name representation of the file format from its id.
  545. @return pointer to the name */
  546. UNIV_INTERN
  547. const char*
  548. trx_sys_file_format_id_to_name(
  549. /*===========================*/
  550. const ulint id) /*!< in: id of the file format */
  551. {
  552. ut_a(id < FILE_FORMAT_NAME_N);
  553. return(file_format_name_map[id]);
  554. }
  555. /*****************************************************************//**
  556. Check for the max file format tag stored on disk. Note: If max_format_id
  557. is == UNIV_FORMAT_MAX + 1 then we only print a warning.
  558. @return DB_SUCCESS or error code */
  559. UNIV_INTERN
  560. dberr_t
  561. trx_sys_file_format_max_check(
  562. /*==========================*/
  563. ulint max_format_id) /*!< in: max format id to check */
  564. {
  565. ulint format_id;
  566. /* Check the file format in the tablespace. Do not try to
  567. recover if the file format is not supported by the engine
  568. unless forced by the user. */
  569. format_id = trx_sys_file_format_max_read();
  570. if (format_id == ULINT_UNDEFINED) {
  571. /* Format ID was not set. Set it to minimum possible
  572. value. */
  573. format_id = UNIV_FORMAT_MIN;
  574. }
  575. ib_logf(IB_LOG_LEVEL_INFO,
  576. "Highest supported file format is %s.",
  577. trx_sys_file_format_id_to_name(UNIV_FORMAT_MAX));
  578. if (format_id > UNIV_FORMAT_MAX) {
  579. ut_a(format_id < FILE_FORMAT_NAME_N);
  580. ib_logf(max_format_id <= UNIV_FORMAT_MAX
  581. ? IB_LOG_LEVEL_ERROR : IB_LOG_LEVEL_WARN,
  582. "The system tablespace is in a file "
  583. "format that this version doesn't support - %s.",
  584. trx_sys_file_format_id_to_name(format_id));
  585. if (max_format_id <= UNIV_FORMAT_MAX) {
  586. return(DB_ERROR);
  587. }
  588. }
  589. format_id = (format_id > max_format_id) ? format_id : max_format_id;
  590. /* We don't need a mutex here, as this function should only
  591. be called once at start up. */
  592. file_format_max.id = format_id;
  593. file_format_max.name = trx_sys_file_format_id_to_name(format_id);
  594. return(DB_SUCCESS);
  595. }
  596. /*****************************************************************//**
  597. Set the file format id unconditionally except if it's already the
  598. same value.
  599. @return TRUE if value updated */
  600. UNIV_INTERN
  601. ibool
  602. trx_sys_file_format_max_set(
  603. /*========================*/
  604. ulint format_id, /*!< in: file format id */
  605. const char** name) /*!< out: max file format name or
  606. NULL if not needed. */
  607. {
  608. ibool ret = FALSE;
  609. ut_a(format_id <= UNIV_FORMAT_MAX);
  610. mutex_enter(&file_format_max.mutex);
  611. /* Only update if not already same value. */
  612. if (format_id != file_format_max.id) {
  613. ret = trx_sys_file_format_max_write(format_id, name);
  614. }
  615. mutex_exit(&file_format_max.mutex);
  616. return(ret);
  617. }
  618. /********************************************************************//**
  619. Tags the system table space with minimum format id if it has not been
  620. tagged yet.
  621. WARNING: This function is only called during the startup and AFTER the
  622. redo log application during recovery has finished. */
  623. UNIV_INTERN
  624. void
  625. trx_sys_file_format_tag_init(void)
  626. /*==============================*/
  627. {
  628. ulint format_id;
  629. format_id = trx_sys_file_format_max_read();
  630. /* If format_id is not set then set it to the minimum. */
  631. if (format_id == ULINT_UNDEFINED) {
  632. trx_sys_file_format_max_set(UNIV_FORMAT_MIN, NULL);
  633. }
  634. }
  635. /********************************************************************//**
  636. Update the file format tag in the system tablespace only if the given
  637. format id is greater than the known max id.
  638. @return TRUE if format_id was bigger than the known max id */
  639. UNIV_INTERN
  640. ibool
  641. trx_sys_file_format_max_upgrade(
  642. /*============================*/
  643. const char** name, /*!< out: max file format name */
  644. ulint format_id) /*!< in: file format identifier */
  645. {
  646. ibool ret = FALSE;
  647. ut_a(name);
  648. ut_a(file_format_max.name != NULL);
  649. ut_a(format_id <= UNIV_FORMAT_MAX);
  650. mutex_enter(&file_format_max.mutex);
  651. if (format_id > file_format_max.id) {
  652. ret = trx_sys_file_format_max_write(format_id, name);
  653. }
  654. mutex_exit(&file_format_max.mutex);
  655. return(ret);
  656. }
  657. /*****************************************************************//**
  658. Get the name representation of the file format from its id.
  659. @return pointer to the max format name */
  660. UNIV_INTERN
  661. const char*
  662. trx_sys_file_format_max_get(void)
  663. /*=============================*/
  664. {
  665. return(file_format_max.name);
  666. }
  667. /*****************************************************************//**
  668. Initializes the tablespace tag system. */
  669. UNIV_INTERN
  670. void
  671. trx_sys_file_format_init(void)
  672. /*==========================*/
  673. {
  674. mutex_create(file_format_max_mutex_key,
  675. &file_format_max.mutex, SYNC_FILE_FORMAT_TAG);
  676. /* We don't need a mutex here, as this function should only
  677. be called once at start up. */
  678. file_format_max.id = UNIV_FORMAT_MIN;
  679. file_format_max.name = trx_sys_file_format_id_to_name(
  680. file_format_max.id);
  681. }
  682. /*****************************************************************//**
  683. Closes the tablespace tag system. */
  684. UNIV_INTERN
  685. void
  686. trx_sys_file_format_close(void)
  687. /*===========================*/
  688. {
  689. /* Does nothing at the moment */
  690. }
  691. /*********************************************************************
  692. Creates the rollback segments.
  693. @return number of rollback segments that are active. */
  694. UNIV_INTERN
  695. ulint
  696. trx_sys_create_rsegs(
  697. /*=================*/
  698. ulint n_spaces, /*!< number of tablespaces for UNDO logs */
  699. ulint n_rsegs) /*!< number of rollback segments to create */
  700. {
  701. mtr_t mtr;
  702. ulint n_used;
  703. ut_a(n_spaces < TRX_SYS_N_RSEGS);
  704. ut_a(n_rsegs <= TRX_SYS_N_RSEGS);
  705. if (srv_read_only_mode) {
  706. return(ULINT_UNDEFINED);
  707. }
  708. /* This is executed in single-threaded mode therefore it is not
  709. necessary to use the same mtr in trx_rseg_create(). n_used cannot
  710. change while the function is executing. */
  711. mtr_start(&mtr);
  712. n_used = trx_sysf_rseg_find_free(&mtr);
  713. mtr_commit(&mtr);
  714. if (n_used == ULINT_UNDEFINED) {
  715. n_used = TRX_SYS_N_RSEGS;
  716. }
  717. /* Do not create additional rollback segments if innodb_force_recovery
  718. has been set and the database was not shutdown cleanly. */
  719. if (!srv_force_recovery && !recv_needed_recovery && n_used < n_rsegs) {
  720. ulint i;
  721. ulint new_rsegs = n_rsegs - n_used;
  722. for (i = 0; i < new_rsegs; ++i) {
  723. ulint space;
  724. /* Tablespace 0 is the system tablespace. All UNDO
  725. log tablespaces start from 1. */
  726. if (n_spaces > 0) {
  727. space = (i % n_spaces) + 1;
  728. } else {
  729. space = 0; /* System tablespace */
  730. }
  731. if (trx_rseg_create(space) != NULL) {
  732. ++n_used;
  733. } else {
  734. break;
  735. }
  736. }
  737. }
  738. ib_logf(IB_LOG_LEVEL_INFO,
  739. "%lu rollback segment(s) are active.", n_used);
  740. return(n_used);
  741. }
  742. #else /* !UNIV_HOTBACKUP */
  743. /*****************************************************************//**
  744. Prints to stderr the MySQL binlog info in the system header if the
  745. magic number shows it valid. */
  746. UNIV_INTERN
  747. void
  748. trx_sys_print_mysql_binlog_offset_from_page(
  749. /*========================================*/
  750. const byte* page) /*!< in: buffer containing the trx
  751. system header page, i.e., page number
  752. TRX_SYS_PAGE_NO in the tablespace */
  753. {
  754. const trx_sysf_t* sys_header;
  755. sys_header = page + TRX_SYS;
  756. if (mach_read_from_4(sys_header + TRX_SYS_MYSQL_LOG_INFO
  757. + TRX_SYS_MYSQL_LOG_MAGIC_N_FLD)
  758. == TRX_SYS_MYSQL_LOG_MAGIC_N) {
  759. fprintf(stderr,
  760. "mysqlbackup: Last MySQL binlog file position %lu %lu,"
  761. " file name %s\n",
  762. (ulong) mach_read_from_4(
  763. sys_header + TRX_SYS_MYSQL_LOG_INFO
  764. + TRX_SYS_MYSQL_LOG_OFFSET_HIGH),
  765. (ulong) mach_read_from_4(
  766. sys_header + TRX_SYS_MYSQL_LOG_INFO
  767. + TRX_SYS_MYSQL_LOG_OFFSET_LOW),
  768. sys_header + TRX_SYS_MYSQL_LOG_INFO
  769. + TRX_SYS_MYSQL_LOG_NAME);
  770. }
  771. }
  772. /*****************************************************************//**
  773. Reads the file format id from the first system table space file.
  774. Even if the call succeeds and returns TRUE, the returned format id
  775. may be ULINT_UNDEFINED signalling that the format id was not present
  776. in the data file.
  777. @return TRUE if call succeeds */
  778. UNIV_INTERN
  779. ibool
  780. trx_sys_read_file_format_id(
  781. /*========================*/
  782. const char *pathname, /*!< in: pathname of the first system
  783. table space file */
  784. ulint *format_id) /*!< out: file format of the system table
  785. space */
  786. {
  787. os_file_t file;
  788. ibool success;
  789. byte buf[UNIV_PAGE_SIZE * 2];
  790. page_t* page = ut_align(buf, UNIV_PAGE_SIZE);
  791. const byte* ptr;
  792. ib_id_t file_format_id;
  793. *format_id = ULINT_UNDEFINED;
  794. file = os_file_create_simple_no_error_handling(
  795. innodb_file_data_key,
  796. pathname,
  797. OS_FILE_OPEN,
  798. OS_FILE_READ_ONLY,
  799. &success
  800. );
  801. if (!success) {
  802. /* The following call prints an error message */
  803. os_file_get_last_error(true);
  804. ut_print_timestamp(stderr);
  805. fprintf(stderr,
  806. " mysqlbackup: Error: trying to read system "
  807. "tablespace file format,\n"
  808. " mysqlbackup: but could not open the tablespace "
  809. "file %s!\n", pathname);
  810. return(FALSE);
  811. }
  812. /* Read the page on which file format is stored */
  813. success = os_file_read_no_error_handling(
  814. file, page, TRX_SYS_PAGE_NO * UNIV_PAGE_SIZE, UNIV_PAGE_SIZE);
  815. if (!success) {
  816. /* The following call prints an error message */
  817. os_file_get_last_error(true);
  818. ut_print_timestamp(stderr);
  819. fprintf(stderr,
  820. " mysqlbackup: Error: trying to read system "
  821. "tablespace file format,\n"
  822. " mysqlbackup: but failed to read the tablespace "
  823. "file %s!\n", pathname);
  824. os_file_close(file);
  825. return(FALSE);
  826. }
  827. os_file_close(file);
  828. /* get the file format from the page */
  829. ptr = page + TRX_SYS_FILE_FORMAT_TAG;
  830. file_format_id = mach_read_from_8(ptr);
  831. file_format_id -= TRX_SYS_FILE_FORMAT_TAG_MAGIC_N;
  832. if (file_format_id >= FILE_FORMAT_NAME_N) {
  833. /* Either it has never been tagged, or garbage in it. */
  834. return(TRUE);
  835. }
  836. *format_id = (ulint) file_format_id;
  837. return(TRUE);
  838. }
  839. /*****************************************************************//**
  840. Reads the file format id from the given per-table data file.
  841. @return TRUE if call succeeds */
  842. UNIV_INTERN
  843. ibool
  844. trx_sys_read_pertable_file_format_id(
  845. /*=================================*/
  846. const char *pathname, /*!< in: pathname of a per-table
  847. datafile */
  848. ulint *format_id) /*!< out: file format of the per-table
  849. data file */
  850. {
  851. os_file_t file;
  852. ibool success;
  853. byte buf[UNIV_PAGE_SIZE * 2];
  854. page_t* page = ut_align(buf, UNIV_PAGE_SIZE);
  855. const byte* ptr;
  856. ib_uint32_t flags;
  857. *format_id = ULINT_UNDEFINED;
  858. file = os_file_create_simple_no_error_handling(
  859. innodb_file_data_key,
  860. pathname,
  861. OS_FILE_OPEN,
  862. OS_FILE_READ_ONLY,
  863. &success
  864. );
  865. if (!success) {
  866. /* The following call prints an error message */
  867. os_file_get_last_error(true);
  868. ut_print_timestamp(stderr);
  869. fprintf(stderr,
  870. " mysqlbackup: Error: trying to read per-table "
  871. "tablespace format,\n"
  872. " mysqlbackup: but could not open the tablespace "
  873. "file %s!\n", pathname);
  874. return(FALSE);
  875. }
  876. /* Read the first page of the per-table datafile */
  877. success = os_file_read_no_error_handling(file, page, 0, UNIV_PAGE_SIZE);
  878. if (!success) {
  879. /* The following call prints an error message */
  880. os_file_get_last_error(true);
  881. ut_print_timestamp(stderr);
  882. fprintf(stderr,
  883. " mysqlbackup: Error: trying to per-table data file "
  884. "format,\n"
  885. " mysqlbackup: but failed to read the tablespace "
  886. "file %s!\n", pathname);
  887. os_file_close(file);
  888. return(FALSE);
  889. }
  890. os_file_close(file);
  891. /* get the file format from the page */
  892. ptr = page + 54;
  893. flags = mach_read_from_4(ptr);
  894. if (!fsp_flags_is_valid(flags) {
  895. /* bad tablespace flags */
  896. return(FALSE);
  897. }
  898. *format_id = FSP_FLAGS_GET_POST_ANTELOPE(flags);
  899. return(TRUE);
  900. }
  901. /*****************************************************************//**
  902. Get the name representation of the file format from its id.
  903. @return pointer to the name */
  904. UNIV_INTERN
  905. const char*
  906. trx_sys_file_format_id_to_name(
  907. /*===========================*/
  908. const ulint id) /*!< in: id of the file format */
  909. {
  910. if (!(id < FILE_FORMAT_NAME_N)) {
  911. /* unknown id */
  912. return("Unknown");
  913. }
  914. return(file_format_name_map[id]);
  915. }
  916. #endif /* !UNIV_HOTBACKUP */
  917. #ifndef UNIV_HOTBACKUP
  918. /*********************************************************************
  919. Shutdown/Close the transaction system. */
  920. UNIV_INTERN
  921. void
  922. trx_sys_close(void)
  923. /*===============*/
  924. {
  925. ulint i;
  926. trx_t* trx;
  927. read_view_t* view;
  928. ut_ad(trx_sys != NULL);
  929. ut_ad(srv_shutdown_state == SRV_SHUTDOWN_EXIT_THREADS);
  930. /* Check that all read views are closed except read view owned
  931. by a purge. */
  932. mutex_enter(&trx_sys->mutex);
  933. if (UT_LIST_GET_LEN(trx_sys->view_list) > 1) {
  934. fprintf(stderr,
  935. "InnoDB: Error: all read views were not closed"
  936. " before shutdown:\n"
  937. "InnoDB: %lu read views open \n",
  938. UT_LIST_GET_LEN(trx_sys->view_list) - 1);
  939. }
  940. mutex_exit(&trx_sys->mutex);
  941. sess_close(trx_dummy_sess);
  942. trx_dummy_sess = NULL;
  943. trx_purge_sys_close();
  944. /* Free the double write data structures. */
  945. buf_dblwr_free();
  946. ut_a(UT_LIST_GET_LEN(trx_sys->ro_trx_list) == 0);
  947. /* Only prepared transactions may be left in the system. Free them. */
  948. ut_a(UT_LIST_GET_LEN(trx_sys->rw_trx_list) == trx_sys->n_prepared_trx);
  949. while ((trx = UT_LIST_GET_FIRST(trx_sys->rw_trx_list)) != NULL) {
  950. trx_free_prepared(trx);
  951. }
  952. /* There can't be any active transactions. */
  953. for (i = 0; i < TRX_SYS_N_RSEGS; ++i) {
  954. trx_rseg_t* rseg;
  955. rseg = trx_sys->rseg_array[i];
  956. if (rseg != NULL) {
  957. trx_rseg_mem_free(rseg);
  958. } else {
  959. break;
  960. }
  961. }
  962. view = UT_LIST_GET_FIRST(trx_sys->view_list);
  963. while (view != NULL) {
  964. read_view_t* prev_view = view;
  965. view = UT_LIST_GET_NEXT(view_list, prev_view);
  966. /* Views are allocated from the trx_sys->global_read_view_heap.
  967. So, we simply remove the element here. */
  968. UT_LIST_REMOVE(view_list, trx_sys->view_list, prev_view);
  969. }
  970. ut_a(UT_LIST_GET_LEN(trx_sys->view_list) == 0);
  971. ut_a(UT_LIST_GET_LEN(trx_sys->ro_trx_list) == 0);
  972. ut_a(UT_LIST_GET_LEN(trx_sys->rw_trx_list) == 0);
  973. ut_a(UT_LIST_GET_LEN(trx_sys->mysql_trx_list) == 0);
  974. mutex_free(&trx_sys->mutex);
  975. mem_free(trx_sys);
  976. trx_sys = NULL;
  977. }
  978. /*********************************************************************
  979. Check if there are any active (non-prepared) transactions.
  980. @return total number of active transactions or 0 if none */
  981. UNIV_INTERN
  982. ulint
  983. trx_sys_any_active_transactions(void)
  984. /*=================================*/
  985. {
  986. ulint total_trx = 0;
  987. mutex_enter(&trx_sys->mutex);
  988. total_trx = UT_LIST_GET_LEN(trx_sys->rw_trx_list)
  989. + UT_LIST_GET_LEN(trx_sys->mysql_trx_list);
  990. ut_a(total_trx >= trx_sys->n_prepared_trx);
  991. total_trx -= trx_sys->n_prepared_trx;
  992. mutex_exit(&trx_sys->mutex);
  993. return(total_trx);
  994. }
  995. #ifdef UNIV_DEBUG
  996. /*************************************************************//**
  997. Validate the trx_list_t.
  998. @return TRUE if valid. */
  999. static
  1000. ibool
  1001. trx_sys_validate_trx_list_low(
  1002. /*===========================*/
  1003. trx_list_t* trx_list) /*!< in: &trx_sys->ro_trx_list
  1004. or &trx_sys->rw_trx_list */
  1005. {
  1006. const trx_t* trx;
  1007. const trx_t* prev_trx = NULL;
  1008. ut_ad(mutex_own(&trx_sys->mutex));
  1009. ut_ad(trx_list == &trx_sys->ro_trx_list
  1010. || trx_list == &trx_sys->rw_trx_list);
  1011. for (trx = UT_LIST_GET_FIRST(*trx_list);
  1012. trx != NULL;
  1013. prev_trx = trx, trx = UT_LIST_GET_NEXT(trx_list, prev_trx)) {
  1014. assert_trx_in_list(trx);
  1015. ut_ad(trx->read_only == (trx_list == &trx_sys->ro_trx_list));
  1016. ut_a(prev_trx == NULL || prev_trx->id > trx->id);
  1017. }
  1018. return(TRUE);
  1019. }
  1020. /*************************************************************//**
  1021. Validate the trx_sys_t::ro_trx_list and trx_sys_t::rw_trx_list.
  1022. @return TRUE if lists are valid. */
  1023. UNIV_INTERN
  1024. ibool
  1025. trx_sys_validate_trx_list(void)
  1026. /*===========================*/
  1027. {
  1028. ut_ad(mutex_own(&trx_sys->mutex));
  1029. ut_a(trx_sys_validate_trx_list_low(&trx_sys->ro_trx_list));
  1030. ut_a(trx_sys_validate_trx_list_low(&trx_sys->rw_trx_list));
  1031. return(TRUE);
  1032. }
  1033. #endif /* UNIV_DEBUG */
  1034. #endif /* !UNIV_HOTBACKUP */