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.

1373 lines
35 KiB

17 years ago
10 years ago
17 years ago
17 years ago
16 years ago
10 years ago
16 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
15 years ago
16 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
15 years ago
16 years ago
15 years ago
16 years ago
15 years ago
16 years ago
15 years ago
16 years ago
15 years ago
16 years ago
16 years ago
16 years ago
15 years ago
16 years ago
15 years ago
16 years ago
16 years ago
15 years ago
16 years ago
15 years ago
16 years ago
15 years ago
16 years ago
15 years ago
16 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
10 years ago
16 years ago
10 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
9 years ago
9 years ago
9 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. /* Allocate the trx descriptors array */
  413. trx_sys->descriptors = static_cast<trx_id_t*>(
  414. ut_malloc(sizeof(trx_id_t) *
  415. TRX_DESCR_ARRAY_INITIAL_SIZE));
  416. trx_sys->descr_n_max = TRX_DESCR_ARRAY_INITIAL_SIZE;
  417. trx_sys->descr_n_used = 0;
  418. srv_descriptors_memory = TRX_DESCR_ARRAY_INITIAL_SIZE *
  419. sizeof(trx_id_t);
  420. sys_header = trx_sysf_get(&mtr);
  421. if (srv_force_recovery < SRV_FORCE_NO_UNDO_LOG_SCAN) {
  422. trx_rseg_array_init(sys_header, ib_bh, &mtr);
  423. }
  424. /* VERY important: after the database is started, max_trx_id value is
  425. divisible by TRX_SYS_TRX_ID_WRITE_MARGIN, and the 'if' in
  426. trx_sys_get_new_trx_id will evaluate to TRUE when the function
  427. is first time called, and the value for trx id will be written
  428. to the disk-based header! Thus trx id values will not overlap when
  429. the database is repeatedly started! */
  430. trx_sys->max_trx_id = 2 * TRX_SYS_TRX_ID_WRITE_MARGIN
  431. + ut_uint64_align_up(mach_read_from_8(sys_header
  432. + TRX_SYS_TRX_ID_STORE),
  433. TRX_SYS_TRX_ID_WRITE_MARGIN);
  434. ut_d(trx_sys->rw_max_trx_id = trx_sys->max_trx_id);
  435. UT_LIST_INIT(trx_sys->mysql_trx_list);
  436. trx_dummy_sess = sess_open();
  437. trx_lists_init_at_db_start();
  438. /* This S lock is not strictly required, it is here only to satisfy
  439. the debug code (assertions). We are still running in single threaded
  440. bootstrap mode. */
  441. mutex_enter(&trx_sys->mutex);
  442. ut_a(UT_LIST_GET_LEN(trx_sys->ro_trx_list) == 0);
  443. if (UT_LIST_GET_LEN(trx_sys->rw_trx_list) > 0) {
  444. const trx_t* trx;
  445. for (trx = UT_LIST_GET_FIRST(trx_sys->rw_trx_list);
  446. trx != NULL;
  447. trx = UT_LIST_GET_NEXT(trx_list, trx)) {
  448. ut_ad(trx->is_recovered);
  449. assert_trx_in_rw_list(trx);
  450. if (trx_state_eq(trx, TRX_STATE_ACTIVE)) {
  451. rows_to_undo += trx->undo_no;
  452. }
  453. }
  454. if (rows_to_undo > 1000000000) {
  455. unit = "M";
  456. rows_to_undo = rows_to_undo / 1000000;
  457. }
  458. fprintf(stderr,
  459. "InnoDB: %lu transaction(s) which must be"
  460. " rolled back or cleaned up\n"
  461. "InnoDB: in total %lu%s row operations to undo\n",
  462. (ulong) UT_LIST_GET_LEN(trx_sys->rw_trx_list),
  463. (ulong) rows_to_undo, unit);
  464. fprintf(stderr, "InnoDB: Trx id counter is " TRX_ID_FMT "\n",
  465. trx_sys->max_trx_id);
  466. }
  467. mutex_exit(&trx_sys->mutex);
  468. UT_LIST_INIT(trx_sys->view_list);
  469. mtr_commit(&mtr);
  470. return(ib_bh);
  471. }
  472. /*****************************************************************//**
  473. Creates the trx_sys instance and initializes ib_bh and mutex. */
  474. UNIV_INTERN
  475. void
  476. trx_sys_create(void)
  477. /*================*/
  478. {
  479. ut_ad(trx_sys == NULL);
  480. trx_sys = static_cast<trx_sys_t*>(mem_zalloc(sizeof(*trx_sys)));
  481. mutex_create(trx_sys_mutex_key, &trx_sys->mutex, SYNC_TRX_SYS);
  482. }
  483. /*****************************************************************//**
  484. Creates and initializes the transaction system at the database creation. */
  485. UNIV_INTERN
  486. void
  487. trx_sys_create_sys_pages(void)
  488. /*==========================*/
  489. {
  490. mtr_t mtr;
  491. mtr_start(&mtr);
  492. trx_sysf_create(&mtr);
  493. mtr_commit(&mtr);
  494. }
  495. /*****************************************************************//**
  496. Update the file format tag.
  497. @return always TRUE */
  498. static
  499. ibool
  500. trx_sys_file_format_max_write(
  501. /*==========================*/
  502. ulint format_id, /*!< in: file format id */
  503. const char** name) /*!< out: max file format name, can
  504. be NULL */
  505. {
  506. mtr_t mtr;
  507. byte* ptr;
  508. buf_block_t* block;
  509. ib_uint64_t tag_value;
  510. mtr_start(&mtr);
  511. block = buf_page_get(
  512. TRX_SYS_SPACE, 0, TRX_SYS_PAGE_NO, RW_X_LATCH, &mtr);
  513. file_format_max.id = format_id;
  514. file_format_max.name = trx_sys_file_format_id_to_name(format_id);
  515. ptr = buf_block_get_frame(block) + TRX_SYS_FILE_FORMAT_TAG;
  516. tag_value = format_id + TRX_SYS_FILE_FORMAT_TAG_MAGIC_N;
  517. if (name) {
  518. *name = file_format_max.name;
  519. }
  520. mlog_write_ull(ptr, tag_value, &mtr);
  521. mtr_commit(&mtr);
  522. return(TRUE);
  523. }
  524. /*****************************************************************//**
  525. Read the file format tag.
  526. @return the file format or ULINT_UNDEFINED if not set. */
  527. static
  528. ulint
  529. trx_sys_file_format_max_read(void)
  530. /*==============================*/
  531. {
  532. mtr_t mtr;
  533. const byte* ptr;
  534. const buf_block_t* block;
  535. ib_id_t file_format_id;
  536. /* Since this is called during the startup phase it's safe to
  537. read the value without a covering mutex. */
  538. mtr_start(&mtr);
  539. block = buf_page_get(
  540. TRX_SYS_SPACE, 0, TRX_SYS_PAGE_NO, RW_X_LATCH, &mtr);
  541. ptr = buf_block_get_frame(block) + TRX_SYS_FILE_FORMAT_TAG;
  542. file_format_id = mach_read_from_8(ptr);
  543. mtr_commit(&mtr);
  544. file_format_id -= TRX_SYS_FILE_FORMAT_TAG_MAGIC_N;
  545. if (file_format_id >= FILE_FORMAT_NAME_N) {
  546. /* Either it has never been tagged, or garbage in it. */
  547. return(ULINT_UNDEFINED);
  548. }
  549. return((ulint) file_format_id);
  550. }
  551. /*****************************************************************//**
  552. Get the name representation of the file format from its id.
  553. @return pointer to the name */
  554. UNIV_INTERN
  555. const char*
  556. trx_sys_file_format_id_to_name(
  557. /*===========================*/
  558. const ulint id) /*!< in: id of the file format */
  559. {
  560. ut_a(id < FILE_FORMAT_NAME_N);
  561. return(file_format_name_map[id]);
  562. }
  563. /*****************************************************************//**
  564. Check for the max file format tag stored on disk. Note: If max_format_id
  565. is == UNIV_FORMAT_MAX + 1 then we only print a warning.
  566. @return DB_SUCCESS or error code */
  567. UNIV_INTERN
  568. dberr_t
  569. trx_sys_file_format_max_check(
  570. /*==========================*/
  571. ulint max_format_id) /*!< in: max format id to check */
  572. {
  573. ulint format_id;
  574. /* Check the file format in the tablespace. Do not try to
  575. recover if the file format is not supported by the engine
  576. unless forced by the user. */
  577. format_id = trx_sys_file_format_max_read();
  578. if (format_id == ULINT_UNDEFINED) {
  579. /* Format ID was not set. Set it to minimum possible
  580. value. */
  581. format_id = UNIV_FORMAT_MIN;
  582. }
  583. ib_logf(IB_LOG_LEVEL_INFO,
  584. "Highest supported file format is %s.",
  585. trx_sys_file_format_id_to_name(UNIV_FORMAT_MAX));
  586. if (format_id > UNIV_FORMAT_MAX) {
  587. ut_a(format_id < FILE_FORMAT_NAME_N);
  588. ib_logf(max_format_id <= UNIV_FORMAT_MAX
  589. ? IB_LOG_LEVEL_ERROR : IB_LOG_LEVEL_WARN,
  590. "The system tablespace is in a file "
  591. "format that this version doesn't support - %s.",
  592. trx_sys_file_format_id_to_name(format_id));
  593. if (max_format_id <= UNIV_FORMAT_MAX) {
  594. return(DB_ERROR);
  595. }
  596. }
  597. format_id = (format_id > max_format_id) ? format_id : max_format_id;
  598. /* We don't need a mutex here, as this function should only
  599. be called once at start up. */
  600. file_format_max.id = format_id;
  601. file_format_max.name = trx_sys_file_format_id_to_name(format_id);
  602. return(DB_SUCCESS);
  603. }
  604. /*****************************************************************//**
  605. Set the file format id unconditionally except if it's already the
  606. same value.
  607. @return TRUE if value updated */
  608. UNIV_INTERN
  609. ibool
  610. trx_sys_file_format_max_set(
  611. /*========================*/
  612. ulint format_id, /*!< in: file format id */
  613. const char** name) /*!< out: max file format name or
  614. NULL if not needed. */
  615. {
  616. ibool ret = FALSE;
  617. ut_a(format_id <= UNIV_FORMAT_MAX);
  618. mutex_enter(&file_format_max.mutex);
  619. /* Only update if not already same value. */
  620. if (format_id != file_format_max.id) {
  621. ret = trx_sys_file_format_max_write(format_id, name);
  622. }
  623. mutex_exit(&file_format_max.mutex);
  624. return(ret);
  625. }
  626. /********************************************************************//**
  627. Tags the system table space with minimum format id if it has not been
  628. tagged yet.
  629. WARNING: This function is only called during the startup and AFTER the
  630. redo log application during recovery has finished. */
  631. UNIV_INTERN
  632. void
  633. trx_sys_file_format_tag_init(void)
  634. /*==============================*/
  635. {
  636. ulint format_id;
  637. format_id = trx_sys_file_format_max_read();
  638. /* If format_id is not set then set it to the minimum. */
  639. if (format_id == ULINT_UNDEFINED) {
  640. trx_sys_file_format_max_set(UNIV_FORMAT_MIN, NULL);
  641. }
  642. }
  643. /********************************************************************//**
  644. Update the file format tag in the system tablespace only if the given
  645. format id is greater than the known max id.
  646. @return TRUE if format_id was bigger than the known max id */
  647. UNIV_INTERN
  648. ibool
  649. trx_sys_file_format_max_upgrade(
  650. /*============================*/
  651. const char** name, /*!< out: max file format name */
  652. ulint format_id) /*!< in: file format identifier */
  653. {
  654. ibool ret = FALSE;
  655. ut_a(name);
  656. ut_a(file_format_max.name != NULL);
  657. ut_a(format_id <= UNIV_FORMAT_MAX);
  658. mutex_enter(&file_format_max.mutex);
  659. if (format_id > file_format_max.id) {
  660. ret = trx_sys_file_format_max_write(format_id, name);
  661. }
  662. mutex_exit(&file_format_max.mutex);
  663. return(ret);
  664. }
  665. /*****************************************************************//**
  666. Get the name representation of the file format from its id.
  667. @return pointer to the max format name */
  668. UNIV_INTERN
  669. const char*
  670. trx_sys_file_format_max_get(void)
  671. /*=============================*/
  672. {
  673. return(file_format_max.name);
  674. }
  675. /*****************************************************************//**
  676. Initializes the tablespace tag system. */
  677. UNIV_INTERN
  678. void
  679. trx_sys_file_format_init(void)
  680. /*==========================*/
  681. {
  682. mutex_create(file_format_max_mutex_key,
  683. &file_format_max.mutex, SYNC_FILE_FORMAT_TAG);
  684. /* We don't need a mutex here, as this function should only
  685. be called once at start up. */
  686. file_format_max.id = UNIV_FORMAT_MIN;
  687. file_format_max.name = trx_sys_file_format_id_to_name(
  688. file_format_max.id);
  689. }
  690. /*****************************************************************//**
  691. Closes the tablespace tag system. */
  692. UNIV_INTERN
  693. void
  694. trx_sys_file_format_close(void)
  695. /*===========================*/
  696. {
  697. /* Does nothing at the moment */
  698. }
  699. /*********************************************************************
  700. Creates the rollback segments.
  701. @return number of rollback segments that are active. */
  702. UNIV_INTERN
  703. ulint
  704. trx_sys_create_rsegs(
  705. /*=================*/
  706. ulint n_spaces, /*!< number of tablespaces for UNDO logs */
  707. ulint n_rsegs) /*!< number of rollback segments to create */
  708. {
  709. mtr_t mtr;
  710. ulint n_used;
  711. ut_a(n_spaces < TRX_SYS_N_RSEGS);
  712. ut_a(n_rsegs <= TRX_SYS_N_RSEGS);
  713. if (srv_read_only_mode) {
  714. return(ULINT_UNDEFINED);
  715. }
  716. /* This is executed in single-threaded mode therefore it is not
  717. necessary to use the same mtr in trx_rseg_create(). n_used cannot
  718. change while the function is executing. */
  719. mtr_start(&mtr);
  720. n_used = trx_sysf_rseg_find_free(&mtr);
  721. mtr_commit(&mtr);
  722. if (n_used == ULINT_UNDEFINED) {
  723. n_used = TRX_SYS_N_RSEGS;
  724. }
  725. /* Do not create additional rollback segments if innodb_force_recovery
  726. has been set and the database was not shutdown cleanly. */
  727. if (!srv_force_recovery && !recv_needed_recovery && n_used < n_rsegs) {
  728. ulint i;
  729. ulint new_rsegs = n_rsegs - n_used;
  730. for (i = 0; i < new_rsegs; ++i) {
  731. ulint space;
  732. /* Tablespace 0 is the system tablespace. All UNDO
  733. log tablespaces start from 1. */
  734. if (n_spaces > 0) {
  735. space = (i % n_spaces) + 1;
  736. } else {
  737. space = 0; /* System tablespace */
  738. }
  739. if (trx_rseg_create(space) != NULL) {
  740. ++n_used;
  741. } else {
  742. break;
  743. }
  744. }
  745. }
  746. ib_logf(IB_LOG_LEVEL_INFO,
  747. "%lu rollback segment(s) are active.", n_used);
  748. return(n_used);
  749. }
  750. #else /* !UNIV_HOTBACKUP */
  751. /*****************************************************************//**
  752. Prints to stderr the MySQL binlog info in the system header if the
  753. magic number shows it valid. */
  754. UNIV_INTERN
  755. void
  756. trx_sys_print_mysql_binlog_offset_from_page(
  757. /*========================================*/
  758. const byte* page) /*!< in: buffer containing the trx
  759. system header page, i.e., page number
  760. TRX_SYS_PAGE_NO in the tablespace */
  761. {
  762. const trx_sysf_t* sys_header;
  763. sys_header = page + TRX_SYS;
  764. if (mach_read_from_4(sys_header + TRX_SYS_MYSQL_LOG_INFO
  765. + TRX_SYS_MYSQL_LOG_MAGIC_N_FLD)
  766. == TRX_SYS_MYSQL_LOG_MAGIC_N) {
  767. fprintf(stderr,
  768. "mysqlbackup: Last MySQL binlog file position %lu %lu,"
  769. " file name %s\n",
  770. (ulong) mach_read_from_4(
  771. sys_header + TRX_SYS_MYSQL_LOG_INFO
  772. + TRX_SYS_MYSQL_LOG_OFFSET_HIGH),
  773. (ulong) mach_read_from_4(
  774. sys_header + TRX_SYS_MYSQL_LOG_INFO
  775. + TRX_SYS_MYSQL_LOG_OFFSET_LOW),
  776. sys_header + TRX_SYS_MYSQL_LOG_INFO
  777. + TRX_SYS_MYSQL_LOG_NAME);
  778. }
  779. }
  780. /*****************************************************************//**
  781. Reads the file format id from the first system table space file.
  782. Even if the call succeeds and returns TRUE, the returned format id
  783. may be ULINT_UNDEFINED signalling that the format id was not present
  784. in the data file.
  785. @return TRUE if call succeeds */
  786. UNIV_INTERN
  787. ibool
  788. trx_sys_read_file_format_id(
  789. /*========================*/
  790. const char *pathname, /*!< in: pathname of the first system
  791. table space file */
  792. ulint *format_id) /*!< out: file format of the system table
  793. space */
  794. {
  795. os_file_t file;
  796. ibool success;
  797. byte buf[UNIV_PAGE_SIZE * 2];
  798. page_t* page = ut_align(buf, UNIV_PAGE_SIZE);
  799. const byte* ptr;
  800. ib_id_t file_format_id;
  801. *format_id = ULINT_UNDEFINED;
  802. file = os_file_create_simple_no_error_handling(
  803. innodb_file_data_key,
  804. pathname,
  805. OS_FILE_OPEN,
  806. OS_FILE_READ_ONLY,
  807. &success
  808. );
  809. if (!success) {
  810. /* The following call prints an error message */
  811. os_file_get_last_error(true);
  812. ut_print_timestamp(stderr);
  813. fprintf(stderr,
  814. " mysqlbackup: Error: trying to read system "
  815. "tablespace file format,\n"
  816. " mysqlbackup: but could not open the tablespace "
  817. "file %s!\n", pathname);
  818. return(FALSE);
  819. }
  820. /* Read the page on which file format is stored */
  821. success = os_file_read_no_error_handling(
  822. file, page, TRX_SYS_PAGE_NO * UNIV_PAGE_SIZE, UNIV_PAGE_SIZE);
  823. if (!success) {
  824. /* The following call prints an error message */
  825. os_file_get_last_error(true);
  826. ut_print_timestamp(stderr);
  827. fprintf(stderr,
  828. " mysqlbackup: Error: trying to read system "
  829. "tablespace file format,\n"
  830. " mysqlbackup: but failed to read the tablespace "
  831. "file %s!\n", pathname);
  832. os_file_close(file);
  833. return(FALSE);
  834. }
  835. os_file_close(file);
  836. /* get the file format from the page */
  837. ptr = page + TRX_SYS_FILE_FORMAT_TAG;
  838. file_format_id = mach_read_from_8(ptr);
  839. file_format_id -= TRX_SYS_FILE_FORMAT_TAG_MAGIC_N;
  840. if (file_format_id >= FILE_FORMAT_NAME_N) {
  841. /* Either it has never been tagged, or garbage in it. */
  842. return(TRUE);
  843. }
  844. *format_id = (ulint) file_format_id;
  845. return(TRUE);
  846. }
  847. /*****************************************************************//**
  848. Reads the file format id from the given per-table data file.
  849. @return TRUE if call succeeds */
  850. UNIV_INTERN
  851. ibool
  852. trx_sys_read_pertable_file_format_id(
  853. /*=================================*/
  854. const char *pathname, /*!< in: pathname of a per-table
  855. datafile */
  856. ulint *format_id) /*!< out: file format of the per-table
  857. data file */
  858. {
  859. os_file_t file;
  860. ibool success;
  861. byte buf[UNIV_PAGE_SIZE * 2];
  862. page_t* page = ut_align(buf, UNIV_PAGE_SIZE);
  863. const byte* ptr;
  864. ib_uint32_t flags;
  865. *format_id = ULINT_UNDEFINED;
  866. file = os_file_create_simple_no_error_handling(
  867. innodb_file_data_key,
  868. pathname,
  869. OS_FILE_OPEN,
  870. OS_FILE_READ_ONLY,
  871. &success
  872. );
  873. if (!success) {
  874. /* The following call prints an error message */
  875. os_file_get_last_error(true);
  876. ut_print_timestamp(stderr);
  877. fprintf(stderr,
  878. " mysqlbackup: Error: trying to read per-table "
  879. "tablespace format,\n"
  880. " mysqlbackup: but could not open the tablespace "
  881. "file %s!\n", pathname);
  882. return(FALSE);
  883. }
  884. /* Read the first page of the per-table datafile */
  885. success = os_file_read_no_error_handling(file, page, 0, UNIV_PAGE_SIZE);
  886. if (!success) {
  887. /* The following call prints an error message */
  888. os_file_get_last_error(true);
  889. ut_print_timestamp(stderr);
  890. fprintf(stderr,
  891. " mysqlbackup: Error: trying to per-table data file "
  892. "format,\n"
  893. " mysqlbackup: but failed to read the tablespace "
  894. "file %s!\n", pathname);
  895. os_file_close(file);
  896. return(FALSE);
  897. }
  898. os_file_close(file);
  899. /* get the file format from the page */
  900. ptr = page + 54;
  901. flags = mach_read_from_4(ptr);
  902. if (!fsp_flags_is_valid(flags) {
  903. /* bad tablespace flags */
  904. return(FALSE);
  905. }
  906. *format_id = FSP_FLAGS_GET_POST_ANTELOPE(flags);
  907. return(TRUE);
  908. }
  909. /*****************************************************************//**
  910. Get the name representation of the file format from its id.
  911. @return pointer to the name */
  912. UNIV_INTERN
  913. const char*
  914. trx_sys_file_format_id_to_name(
  915. /*===========================*/
  916. const ulint id) /*!< in: id of the file format */
  917. {
  918. if (!(id < FILE_FORMAT_NAME_N)) {
  919. /* unknown id */
  920. return("Unknown");
  921. }
  922. return(file_format_name_map[id]);
  923. }
  924. #endif /* !UNIV_HOTBACKUP */
  925. #ifndef UNIV_HOTBACKUP
  926. /*********************************************************************
  927. Shutdown/Close the transaction system. */
  928. UNIV_INTERN
  929. void
  930. trx_sys_close(void)
  931. /*===============*/
  932. {
  933. ulint i;
  934. trx_t* trx;
  935. read_view_t* view;
  936. ut_ad(trx_sys != NULL);
  937. ut_ad(srv_shutdown_state == SRV_SHUTDOWN_EXIT_THREADS);
  938. /* Check that all read views are closed except read view owned
  939. by a purge. */
  940. mutex_enter(&trx_sys->mutex);
  941. if (UT_LIST_GET_LEN(trx_sys->view_list) > 1) {
  942. fprintf(stderr,
  943. "InnoDB: Error: all read views were not closed"
  944. " before shutdown:\n"
  945. "InnoDB: %lu read views open \n",
  946. UT_LIST_GET_LEN(trx_sys->view_list) - 1);
  947. }
  948. mutex_exit(&trx_sys->mutex);
  949. sess_close(trx_dummy_sess);
  950. trx_dummy_sess = NULL;
  951. trx_purge_sys_close();
  952. /* Free the double write data structures. */
  953. buf_dblwr_free();
  954. ut_a(UT_LIST_GET_LEN(trx_sys->ro_trx_list) == 0);
  955. /* Only prepared transactions may be left in the system. Free them. */
  956. ut_a(UT_LIST_GET_LEN(trx_sys->rw_trx_list) == trx_sys->n_prepared_trx);
  957. while ((trx = UT_LIST_GET_FIRST(trx_sys->rw_trx_list)) != NULL) {
  958. trx_free_prepared(trx);
  959. }
  960. /* There can't be any active transactions. */
  961. for (i = 0; i < TRX_SYS_N_RSEGS; ++i) {
  962. trx_rseg_t* rseg;
  963. rseg = trx_sys->rseg_array[i];
  964. if (rseg != NULL) {
  965. trx_rseg_mem_free(rseg);
  966. } else {
  967. break;
  968. }
  969. }
  970. view = UT_LIST_GET_FIRST(trx_sys->view_list);
  971. while (view != NULL) {
  972. read_view_t* prev_view = view;
  973. view = UT_LIST_GET_NEXT(view_list, prev_view);
  974. /* Views are allocated from the trx_sys->global_read_view_heap.
  975. So, we simply remove the element here. */
  976. UT_LIST_REMOVE(view_list, trx_sys->view_list, prev_view);
  977. }
  978. ut_a(UT_LIST_GET_LEN(trx_sys->view_list) == 0);
  979. ut_a(UT_LIST_GET_LEN(trx_sys->ro_trx_list) == 0);
  980. ut_a(UT_LIST_GET_LEN(trx_sys->rw_trx_list) == 0);
  981. ut_a(UT_LIST_GET_LEN(trx_sys->mysql_trx_list) == 0);
  982. mutex_free(&trx_sys->mutex);
  983. ut_ad(trx_sys->descr_n_used == 0);
  984. ut_free(trx_sys->descriptors);
  985. mem_free(trx_sys);
  986. trx_sys = NULL;
  987. }
  988. /** @brief Convert an undo log to TRX_UNDO_PREPARED state on shutdown.
  989. If any prepared ACTIVE transactions exist, and their rollback was
  990. prevented by innodb_force_recovery, we convert these transactions to
  991. XA PREPARE state in the main-memory data structures, so that shutdown
  992. will proceed normally. These transactions will again recover as ACTIVE
  993. on the next restart, and they will be rolled back unless
  994. innodb_force_recovery prevents it again.
  995. @param[in] trx transaction
  996. @param[in,out] undo undo log to convert to TRX_UNDO_PREPARED */
  997. static
  998. void
  999. trx_undo_fake_prepared(
  1000. const trx_t* trx,
  1001. trx_undo_t* undo)
  1002. {
  1003. ut_ad(srv_force_recovery >= SRV_FORCE_NO_TRX_UNDO);
  1004. ut_ad(trx_state_eq(trx, TRX_STATE_ACTIVE));
  1005. ut_ad(trx->is_recovered);
  1006. if (undo != NULL) {
  1007. ut_ad(undo->state == TRX_UNDO_ACTIVE);
  1008. undo->state = TRX_UNDO_PREPARED;
  1009. }
  1010. }
  1011. /*********************************************************************
  1012. Check if there are any active (non-prepared) transactions.
  1013. @return total number of active transactions or 0 if none */
  1014. UNIV_INTERN
  1015. ulint
  1016. trx_sys_any_active_transactions(void)
  1017. /*=================================*/
  1018. {
  1019. mutex_enter(&trx_sys->mutex);
  1020. ulint total_trx = UT_LIST_GET_LEN(trx_sys->mysql_trx_list);
  1021. if (total_trx == 0) {
  1022. total_trx = UT_LIST_GET_LEN(trx_sys->rw_trx_list);
  1023. ut_a(total_trx >= trx_sys->n_prepared_trx);
  1024. if (total_trx > trx_sys->n_prepared_trx
  1025. && srv_force_recovery >= SRV_FORCE_NO_TRX_UNDO) {
  1026. for (trx_t* trx = UT_LIST_GET_FIRST(
  1027. trx_sys->rw_trx_list);
  1028. trx != NULL;
  1029. trx = UT_LIST_GET_NEXT(trx_list, trx)) {
  1030. if (!trx_state_eq(trx, TRX_STATE_ACTIVE)
  1031. || !trx->is_recovered) {
  1032. continue;
  1033. }
  1034. /* This was a recovered transaction
  1035. whose rollback was disabled by
  1036. the innodb_force_recovery setting.
  1037. Pretend that it is in XA PREPARE
  1038. state so that shutdown will work. */
  1039. trx_undo_fake_prepared(
  1040. trx, trx->insert_undo);
  1041. trx_undo_fake_prepared(
  1042. trx, trx->update_undo);
  1043. trx->state = TRX_STATE_PREPARED;
  1044. trx_sys->n_prepared_trx++;
  1045. trx_sys->n_prepared_recovered_trx++;
  1046. }
  1047. }
  1048. ut_a(total_trx >= trx_sys->n_prepared_trx);
  1049. total_trx -= trx_sys->n_prepared_trx;
  1050. }
  1051. mutex_exit(&trx_sys->mutex);
  1052. return(total_trx);
  1053. }
  1054. #ifdef UNIV_DEBUG
  1055. /*************************************************************//**
  1056. Validate the trx_list_t.
  1057. @return TRUE if valid. */
  1058. static
  1059. ibool
  1060. trx_sys_validate_trx_list_low(
  1061. /*===========================*/
  1062. trx_list_t* trx_list) /*!< in: &trx_sys->ro_trx_list
  1063. or &trx_sys->rw_trx_list */
  1064. {
  1065. const trx_t* trx;
  1066. const trx_t* prev_trx = NULL;
  1067. ut_ad(mutex_own(&trx_sys->mutex));
  1068. ut_ad(trx_list == &trx_sys->ro_trx_list
  1069. || trx_list == &trx_sys->rw_trx_list);
  1070. for (trx = UT_LIST_GET_FIRST(*trx_list);
  1071. trx != NULL;
  1072. prev_trx = trx, trx = UT_LIST_GET_NEXT(trx_list, prev_trx)) {
  1073. assert_trx_in_list(trx);
  1074. ut_ad(trx->read_only == (trx_list == &trx_sys->ro_trx_list));
  1075. ut_a(prev_trx == NULL || prev_trx->id > trx->id);
  1076. }
  1077. return(TRUE);
  1078. }
  1079. /*************************************************************//**
  1080. Validate the trx_sys_t::ro_trx_list and trx_sys_t::rw_trx_list.
  1081. @return TRUE if lists are valid. */
  1082. UNIV_INTERN
  1083. ibool
  1084. trx_sys_validate_trx_list(void)
  1085. /*===========================*/
  1086. {
  1087. ut_ad(mutex_own(&trx_sys->mutex));
  1088. ut_a(trx_sys_validate_trx_list_low(&trx_sys->ro_trx_list));
  1089. ut_a(trx_sys_validate_trx_list_low(&trx_sys->rw_trx_list));
  1090. return(TRUE);
  1091. }
  1092. #endif /* UNIV_DEBUG */
  1093. #endif /* !UNIV_HOTBACKUP */