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.

3735 lines
92 KiB

10 years ago
11 years ago
12 years ago
12 years ago
12 years ago
12 years ago
11 years ago
11 years ago
11 years ago
10 years ago
  1. /*****************************************************************************
  2. Copyright (c) 1995, 2015, Oracle and/or its affiliates. All Rights Reserved.
  3. Copyright (c) 2009, Google Inc.
  4. Portions of this file contain modifications contributed and copyrighted by
  5. Google, Inc. Those modifications are gratefully acknowledged and are described
  6. briefly in the InnoDB documentation. The contributions by Google are
  7. incorporated with their permission, and subject to the conditions contained in
  8. the file COPYING.Google.
  9. This program is free software; you can redistribute it and/or modify it under
  10. the terms of the GNU General Public License as published by the Free Software
  11. Foundation; version 2 of the License.
  12. This program is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  14. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License along with
  16. this program; if not, write to the Free Software Foundation, Inc.,
  17. 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
  18. *****************************************************************************/
  19. /**************************************************//**
  20. @file log/log0log.cc
  21. Database log
  22. Created 12/9/1995 Heikki Tuuri
  23. *******************************************************/
  24. #include "log0log.h"
  25. #ifdef UNIV_NONINL
  26. #include "log0log.ic"
  27. #endif
  28. #ifndef UNIV_HOTBACKUP
  29. #include "mem0mem.h"
  30. #include "buf0buf.h"
  31. #include "buf0flu.h"
  32. #include "srv0srv.h"
  33. #include "log0recv.h"
  34. #include "fil0fil.h"
  35. #include "dict0boot.h"
  36. #include "srv0srv.h"
  37. #include "srv0start.h"
  38. #include "trx0sys.h"
  39. #include "trx0trx.h"
  40. #include "trx0roll.h"
  41. #include "srv0mon.h"
  42. /*
  43. General philosophy of InnoDB redo-logs:
  44. 1) Every change to a contents of a data page must be done
  45. through mtr, which in mtr_commit() writes log records
  46. to the InnoDB redo log.
  47. 2) Normally these changes are performed using a mlog_write_ulint()
  48. or similar function.
  49. 3) In some page level operations only a code number of a
  50. c-function and its parameters are written to the log to
  51. reduce the size of the log.
  52. 3a) You should not add parameters to these kind of functions
  53. (e.g. trx_undo_header_create(), trx_undo_insert_header_reuse())
  54. 3b) You should not add such functionality which either change
  55. working when compared with the old or are dependent on data
  56. outside of the page. These kind of functions should implement
  57. self-contained page transformation and it should be unchanged
  58. if you don't have very essential reasons to change log
  59. semantics or format.
  60. */
  61. /* Global log system variable */
  62. UNIV_INTERN log_t* log_sys = NULL;
  63. #ifdef UNIV_PFS_RWLOCK
  64. UNIV_INTERN mysql_pfs_key_t checkpoint_lock_key;
  65. # ifdef UNIV_LOG_ARCHIVE
  66. UNIV_INTERN mysql_pfs_key_t archive_lock_key;
  67. # endif
  68. #endif /* UNIV_PFS_RWLOCK */
  69. #ifdef UNIV_PFS_MUTEX
  70. UNIV_INTERN mysql_pfs_key_t log_sys_mutex_key;
  71. UNIV_INTERN mysql_pfs_key_t log_flush_order_mutex_key;
  72. #endif /* UNIV_PFS_MUTEX */
  73. #ifdef UNIV_DEBUG
  74. UNIV_INTERN ibool log_do_write = TRUE;
  75. #endif /* UNIV_DEBUG */
  76. /* These control how often we print warnings if the last checkpoint is too
  77. old */
  78. UNIV_INTERN ibool log_has_printed_chkp_warning = FALSE;
  79. UNIV_INTERN time_t log_last_warning_time;
  80. #ifdef UNIV_LOG_ARCHIVE
  81. /* Pointer to this variable is used as the i/o-message when we do i/o to an
  82. archive */
  83. UNIV_INTERN byte log_archive_io;
  84. #endif /* UNIV_LOG_ARCHIVE */
  85. /* A margin for free space in the log buffer before a log entry is catenated */
  86. #define LOG_BUF_WRITE_MARGIN (4 * OS_FILE_LOG_BLOCK_SIZE)
  87. /* Margins for free space in the log buffer after a log entry is catenated */
  88. #define LOG_BUF_FLUSH_RATIO 2
  89. #define LOG_BUF_FLUSH_MARGIN (LOG_BUF_WRITE_MARGIN + 4 * UNIV_PAGE_SIZE)
  90. /* Margin for the free space in the smallest log group, before a new query
  91. step which modifies the database, is started */
  92. #define LOG_CHECKPOINT_FREE_PER_THREAD (4 * UNIV_PAGE_SIZE)
  93. #define LOG_CHECKPOINT_EXTRA_FREE (8 * UNIV_PAGE_SIZE)
  94. /* This parameter controls asynchronous making of a new checkpoint; the value
  95. should be bigger than LOG_POOL_PREFLUSH_RATIO_SYNC */
  96. #define LOG_POOL_CHECKPOINT_RATIO_ASYNC 32
  97. /* This parameter controls synchronous preflushing of modified buffer pages */
  98. #define LOG_POOL_PREFLUSH_RATIO_SYNC 16
  99. /* The same ratio for asynchronous preflushing; this value should be less than
  100. the previous */
  101. #define LOG_POOL_PREFLUSH_RATIO_ASYNC 8
  102. /* Extra margin, in addition to one log file, used in archiving */
  103. #define LOG_ARCHIVE_EXTRA_MARGIN (4 * UNIV_PAGE_SIZE)
  104. /* This parameter controls asynchronous writing to the archive */
  105. #define LOG_ARCHIVE_RATIO_ASYNC 16
  106. /* Codes used in unlocking flush latches */
  107. #define LOG_UNLOCK_NONE_FLUSHED_LOCK 1
  108. #define LOG_UNLOCK_FLUSH_LOCK 2
  109. /* States of an archiving operation */
  110. #define LOG_ARCHIVE_READ 1
  111. #define LOG_ARCHIVE_WRITE 2
  112. /******************************************************//**
  113. Completes a checkpoint write i/o to a log file. */
  114. static
  115. void
  116. log_io_complete_checkpoint(void);
  117. /*============================*/
  118. #ifdef UNIV_LOG_ARCHIVE
  119. /******************************************************//**
  120. Completes an archiving i/o. */
  121. static
  122. void
  123. log_io_complete_archive(void);
  124. /*=========================*/
  125. #endif /* UNIV_LOG_ARCHIVE */
  126. /****************************************************************//**
  127. Returns the oldest modified block lsn in the pool, or log_sys->lsn if none
  128. exists.
  129. @return LSN of oldest modification */
  130. static
  131. lsn_t
  132. log_buf_pool_get_oldest_modification(void)
  133. /*======================================*/
  134. {
  135. lsn_t lsn;
  136. ut_ad(mutex_own(&(log_sys->mutex)));
  137. lsn = buf_pool_get_oldest_modification();
  138. if (!lsn) {
  139. lsn = log_sys->lsn;
  140. }
  141. return(lsn);
  142. }
  143. /** Extends the log buffer.
  144. @param[in] len requested minimum size in bytes */
  145. static
  146. void
  147. log_buffer_extend(
  148. ulint len)
  149. {
  150. ulint move_start;
  151. ulint move_end;
  152. byte tmp_buf[OS_FILE_LOG_BLOCK_SIZE];
  153. mutex_enter(&(log_sys->mutex));
  154. while (log_sys->is_extending) {
  155. /* Another thread is trying to extend already.
  156. Needs to wait for. */
  157. mutex_exit(&(log_sys->mutex));
  158. log_buffer_flush_to_disk();
  159. mutex_enter(&(log_sys->mutex));
  160. if (srv_log_buffer_size > len / UNIV_PAGE_SIZE) {
  161. /* Already extended enough by the others */
  162. mutex_exit(&(log_sys->mutex));
  163. return;
  164. }
  165. }
  166. log_sys->is_extending = true;
  167. while (log_sys->n_pending_writes != 0
  168. || ut_calc_align_down(log_sys->buf_free,
  169. OS_FILE_LOG_BLOCK_SIZE)
  170. != ut_calc_align_down(log_sys->buf_next_to_write,
  171. OS_FILE_LOG_BLOCK_SIZE)) {
  172. /* Buffer might have >1 blocks to write still. */
  173. mutex_exit(&(log_sys->mutex));
  174. log_buffer_flush_to_disk();
  175. mutex_enter(&(log_sys->mutex));
  176. }
  177. move_start = ut_calc_align_down(
  178. log_sys->buf_free,
  179. OS_FILE_LOG_BLOCK_SIZE);
  180. move_end = log_sys->buf_free;
  181. /* store the last log block in buffer */
  182. ut_memcpy(tmp_buf, log_sys->buf + move_start,
  183. move_end - move_start);
  184. log_sys->buf_free -= move_start;
  185. log_sys->buf_next_to_write -= move_start;
  186. /* reallocate log buffer */
  187. srv_log_buffer_size = len / UNIV_PAGE_SIZE + 1;
  188. mem_free(log_sys->buf_ptr);
  189. log_sys->buf_ptr = static_cast<byte*>(
  190. mem_zalloc(LOG_BUFFER_SIZE + OS_FILE_LOG_BLOCK_SIZE));
  191. log_sys->buf = static_cast<byte*>(
  192. ut_align(log_sys->buf_ptr, OS_FILE_LOG_BLOCK_SIZE));
  193. log_sys->buf_size = LOG_BUFFER_SIZE;
  194. log_sys->max_buf_free = log_sys->buf_size / LOG_BUF_FLUSH_RATIO
  195. - LOG_BUF_FLUSH_MARGIN;
  196. /* restore the last log block */
  197. ut_memcpy(log_sys->buf, tmp_buf, move_end - move_start);
  198. ut_ad(log_sys->is_extending);
  199. log_sys->is_extending = false;
  200. mutex_exit(&(log_sys->mutex));
  201. ib_logf(IB_LOG_LEVEL_INFO,
  202. "innodb_log_buffer_size was extended to %lu.",
  203. LOG_BUFFER_SIZE);
  204. }
  205. /************************************************************//**
  206. Opens the log for log_write_low. The log must be closed with log_close and
  207. released with log_release.
  208. @return start lsn of the log record */
  209. UNIV_INTERN
  210. lsn_t
  211. log_reserve_and_open(
  212. /*=================*/
  213. ulint len) /*!< in: length of data to be catenated */
  214. {
  215. log_t* log = log_sys;
  216. ulint len_upper_limit;
  217. #ifdef UNIV_LOG_ARCHIVE
  218. ulint archived_lsn_age;
  219. ulint dummy;
  220. #endif /* UNIV_LOG_ARCHIVE */
  221. #ifdef UNIV_DEBUG
  222. ulint count = 0;
  223. #endif /* UNIV_DEBUG */
  224. if (len >= log->buf_size / 2) {
  225. DBUG_EXECUTE_IF("ib_log_buffer_is_short_crash",
  226. DBUG_SUICIDE(););
  227. /* log_buffer is too small. try to extend instead of crash. */
  228. ib_logf(IB_LOG_LEVEL_WARN,
  229. "The transaction log size is too large"
  230. " for innodb_log_buffer_size (%lu >= %lu / 2). "
  231. "Trying to extend it.",
  232. len, LOG_BUFFER_SIZE);
  233. log_buffer_extend((len + 1) * 2);
  234. }
  235. loop:
  236. mutex_enter(&(log->mutex));
  237. ut_ad(!recv_no_log_write);
  238. if (log->is_extending) {
  239. mutex_exit(&(log->mutex));
  240. /* Log buffer size is extending. Writing up to the next block
  241. should wait for the extending finished. */
  242. os_thread_sleep(100000);
  243. ut_ad(++count < 50);
  244. goto loop;
  245. }
  246. /* Calculate an upper limit for the space the string may take in the
  247. log buffer */
  248. len_upper_limit = LOG_BUF_WRITE_MARGIN + (5 * len) / 4;
  249. if (log->buf_free + len_upper_limit > log->buf_size) {
  250. mutex_exit(&(log->mutex));
  251. /* Not enough free space, do a syncronous flush of the log
  252. buffer */
  253. log_buffer_flush_to_disk();
  254. srv_stats.log_waits.inc();
  255. ut_ad(++count < 50);
  256. goto loop;
  257. }
  258. #ifdef UNIV_LOG_ARCHIVE
  259. if (log->archiving_state != LOG_ARCH_OFF) {
  260. archived_lsn_age = log->lsn - log->archived_lsn;
  261. if (archived_lsn_age + len_upper_limit
  262. > log->max_archived_lsn_age) {
  263. /* Not enough free archived space in log groups: do a
  264. synchronous archive write batch: */
  265. mutex_exit(&(log->mutex));
  266. ut_ad(len_upper_limit <= log->max_archived_lsn_age);
  267. log_archive_do(TRUE, &dummy);
  268. ut_ad(++count < 50);
  269. goto loop;
  270. }
  271. }
  272. #endif /* UNIV_LOG_ARCHIVE */
  273. #ifdef UNIV_LOG_DEBUG
  274. log->old_buf_free = log->buf_free;
  275. log->old_lsn = log->lsn;
  276. #endif
  277. return(log->lsn);
  278. }
  279. /************************************************************//**
  280. Writes to the log the string given. It is assumed that the caller holds the
  281. log mutex. */
  282. UNIV_INTERN
  283. void
  284. log_write_low(
  285. /*==========*/
  286. byte* str, /*!< in: string */
  287. ulint str_len) /*!< in: string length */
  288. {
  289. log_t* log = log_sys;
  290. ulint len;
  291. ulint data_len;
  292. byte* log_block;
  293. ut_ad(mutex_own(&(log->mutex)));
  294. part_loop:
  295. ut_ad(!recv_no_log_write);
  296. /* Calculate a part length */
  297. data_len = (log->buf_free % OS_FILE_LOG_BLOCK_SIZE) + str_len;
  298. if (data_len <= OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_TRL_SIZE) {
  299. /* The string fits within the current log block */
  300. len = str_len;
  301. } else {
  302. data_len = OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_TRL_SIZE;
  303. len = OS_FILE_LOG_BLOCK_SIZE
  304. - (log->buf_free % OS_FILE_LOG_BLOCK_SIZE)
  305. - LOG_BLOCK_TRL_SIZE;
  306. }
  307. ut_memcpy(log->buf + log->buf_free, str, len);
  308. str_len -= len;
  309. str = str + len;
  310. log_block = static_cast<byte*>(
  311. ut_align_down(
  312. log->buf + log->buf_free, OS_FILE_LOG_BLOCK_SIZE));
  313. log_block_set_data_len(log_block, data_len);
  314. if (data_len == OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_TRL_SIZE) {
  315. /* This block became full */
  316. log_block_set_data_len(log_block, OS_FILE_LOG_BLOCK_SIZE);
  317. log_block_set_checkpoint_no(log_block,
  318. log_sys->next_checkpoint_no);
  319. len += LOG_BLOCK_HDR_SIZE + LOG_BLOCK_TRL_SIZE;
  320. log->lsn += len;
  321. /* Initialize the next block header */
  322. log_block_init(log_block + OS_FILE_LOG_BLOCK_SIZE, log->lsn);
  323. } else {
  324. log->lsn += len;
  325. }
  326. log->buf_free += len;
  327. ut_ad(log->buf_free <= log->buf_size);
  328. if (str_len > 0) {
  329. goto part_loop;
  330. }
  331. srv_stats.log_write_requests.inc();
  332. }
  333. /************************************************************//**
  334. Closes the log.
  335. @return lsn */
  336. UNIV_INTERN
  337. lsn_t
  338. log_close(void)
  339. /*===========*/
  340. {
  341. byte* log_block;
  342. ulint first_rec_group;
  343. lsn_t oldest_lsn;
  344. lsn_t lsn;
  345. log_t* log = log_sys;
  346. lsn_t checkpoint_age;
  347. ut_ad(mutex_own(&(log->mutex)));
  348. ut_ad(!recv_no_log_write);
  349. lsn = log->lsn;
  350. log_block = static_cast<byte*>(
  351. ut_align_down(
  352. log->buf + log->buf_free, OS_FILE_LOG_BLOCK_SIZE));
  353. first_rec_group = log_block_get_first_rec_group(log_block);
  354. if (first_rec_group == 0) {
  355. /* We initialized a new log block which was not written
  356. full by the current mtr: the next mtr log record group
  357. will start within this block at the offset data_len */
  358. log_block_set_first_rec_group(
  359. log_block, log_block_get_data_len(log_block));
  360. }
  361. if (log->buf_free > log->max_buf_free) {
  362. log->check_flush_or_checkpoint = TRUE;
  363. }
  364. checkpoint_age = lsn - log->last_checkpoint_lsn;
  365. if (checkpoint_age >= log->log_group_capacity) {
  366. /* TODO: split btr_store_big_rec_extern_fields() into small
  367. steps so that we can release all latches in the middle, and
  368. call log_free_check() to ensure we never write over log written
  369. after the latest checkpoint. In principle, we should split all
  370. big_rec operations, but other operations are smaller. */
  371. if (!log_has_printed_chkp_warning
  372. || difftime(time(NULL), log_last_warning_time) > 15) {
  373. log_has_printed_chkp_warning = TRUE;
  374. log_last_warning_time = time(NULL);
  375. ut_print_timestamp(stderr);
  376. fprintf(stderr,
  377. " InnoDB: ERROR: the age of the last"
  378. " checkpoint is " LSN_PF ",\n"
  379. "InnoDB: which exceeds the log group"
  380. " capacity " LSN_PF ".\n"
  381. "InnoDB: If you are using big"
  382. " BLOB or TEXT rows, you must set the\n"
  383. "InnoDB: combined size of log files"
  384. " at least 10 times bigger than the\n"
  385. "InnoDB: largest such row.\n",
  386. checkpoint_age,
  387. log->log_group_capacity);
  388. }
  389. }
  390. if (checkpoint_age <= log->max_modified_age_sync) {
  391. goto function_exit;
  392. }
  393. oldest_lsn = buf_pool_get_oldest_modification();
  394. if (!oldest_lsn
  395. || lsn - oldest_lsn > log->max_modified_age_sync
  396. || checkpoint_age > log->max_checkpoint_age_async) {
  397. log->check_flush_or_checkpoint = TRUE;
  398. }
  399. function_exit:
  400. #ifdef UNIV_LOG_DEBUG
  401. log_check_log_recs(log->buf + log->old_buf_free,
  402. log->buf_free - log->old_buf_free, log->old_lsn);
  403. #endif
  404. return(lsn);
  405. }
  406. #ifdef UNIV_LOG_ARCHIVE
  407. /******************************************************//**
  408. Pads the current log block full with dummy log records. Used in producing
  409. consistent archived log files. */
  410. static
  411. void
  412. log_pad_current_log_block(void)
  413. /*===========================*/
  414. {
  415. byte b = MLOG_DUMMY_RECORD;
  416. ulint pad_length;
  417. ulint i;
  418. ib_uint64_t lsn;
  419. /* We retrieve lsn only because otherwise gcc crashed on HP-UX */
  420. lsn = log_reserve_and_open(OS_FILE_LOG_BLOCK_SIZE);
  421. pad_length = OS_FILE_LOG_BLOCK_SIZE
  422. - (log_sys->buf_free % OS_FILE_LOG_BLOCK_SIZE)
  423. - LOG_BLOCK_TRL_SIZE;
  424. for (i = 0; i < pad_length; i++) {
  425. log_write_low(&b, 1);
  426. }
  427. lsn = log_sys->lsn;
  428. log_close();
  429. log_release();
  430. ut_a(lsn % OS_FILE_LOG_BLOCK_SIZE == LOG_BLOCK_HDR_SIZE);
  431. }
  432. #endif /* UNIV_LOG_ARCHIVE */
  433. /******************************************************//**
  434. Calculates the data capacity of a log group, when the log file headers are not
  435. included.
  436. @return capacity in bytes */
  437. UNIV_INTERN
  438. lsn_t
  439. log_group_get_capacity(
  440. /*===================*/
  441. const log_group_t* group) /*!< in: log group */
  442. {
  443. ut_ad(mutex_own(&(log_sys->mutex)));
  444. return((group->file_size - LOG_FILE_HDR_SIZE) * group->n_files);
  445. }
  446. /******************************************************//**
  447. Calculates the offset within a log group, when the log file headers are not
  448. included.
  449. @return size offset (<= offset) */
  450. UNIV_INLINE
  451. lsn_t
  452. log_group_calc_size_offset(
  453. /*=======================*/
  454. lsn_t offset, /*!< in: real offset within the
  455. log group */
  456. const log_group_t* group) /*!< in: log group */
  457. {
  458. ut_ad(mutex_own(&(log_sys->mutex)));
  459. return(offset - LOG_FILE_HDR_SIZE * (1 + offset / group->file_size));
  460. }
  461. /******************************************************//**
  462. Calculates the offset within a log group, when the log file headers are
  463. included.
  464. @return real offset (>= offset) */
  465. UNIV_INLINE
  466. lsn_t
  467. log_group_calc_real_offset(
  468. /*=======================*/
  469. lsn_t offset, /*!< in: size offset within the
  470. log group */
  471. const log_group_t* group) /*!< in: log group */
  472. {
  473. ut_ad(mutex_own(&(log_sys->mutex)));
  474. return(offset + LOG_FILE_HDR_SIZE
  475. * (1 + offset / (group->file_size - LOG_FILE_HDR_SIZE)));
  476. }
  477. /******************************************************//**
  478. Calculates the offset of an lsn within a log group.
  479. @return offset within the log group */
  480. static
  481. lsn_t
  482. log_group_calc_lsn_offset(
  483. /*======================*/
  484. lsn_t lsn, /*!< in: lsn */
  485. const log_group_t* group) /*!< in: log group */
  486. {
  487. lsn_t gr_lsn;
  488. lsn_t gr_lsn_size_offset;
  489. lsn_t difference;
  490. lsn_t group_size;
  491. lsn_t offset;
  492. ut_ad(mutex_own(&(log_sys->mutex)));
  493. gr_lsn = group->lsn;
  494. gr_lsn_size_offset = log_group_calc_size_offset(group->lsn_offset, group);
  495. group_size = log_group_get_capacity(group);
  496. if (lsn >= gr_lsn) {
  497. difference = lsn - gr_lsn;
  498. } else {
  499. difference = gr_lsn - lsn;
  500. difference = difference % group_size;
  501. difference = group_size - difference;
  502. }
  503. offset = (gr_lsn_size_offset + difference) % group_size;
  504. /* fprintf(stderr,
  505. "Offset is " LSN_PF " gr_lsn_offset is " LSN_PF
  506. " difference is " LSN_PF "\n",
  507. offset, gr_lsn_size_offset, difference);
  508. */
  509. return(log_group_calc_real_offset(offset, group));
  510. }
  511. #endif /* !UNIV_HOTBACKUP */
  512. #ifdef UNIV_DEBUG
  513. UNIV_INTERN ibool log_debug_writes = FALSE;
  514. #endif /* UNIV_DEBUG */
  515. /*******************************************************************//**
  516. Calculates where in log files we find a specified lsn.
  517. @return log file number */
  518. UNIV_INTERN
  519. ulint
  520. log_calc_where_lsn_is(
  521. /*==================*/
  522. ib_int64_t* log_file_offset, /*!< out: offset in that file
  523. (including the header) */
  524. ib_uint64_t first_header_lsn, /*!< in: first log file start
  525. lsn */
  526. ib_uint64_t lsn, /*!< in: lsn whose position to
  527. determine */
  528. ulint n_log_files, /*!< in: total number of log
  529. files */
  530. ib_int64_t log_file_size) /*!< in: log file size
  531. (including the header) */
  532. {
  533. ib_int64_t capacity = log_file_size - LOG_FILE_HDR_SIZE;
  534. ulint file_no;
  535. ib_int64_t add_this_many;
  536. if (lsn < first_header_lsn) {
  537. add_this_many = 1 + (first_header_lsn - lsn)
  538. / (capacity * (ib_int64_t) n_log_files);
  539. lsn += add_this_many
  540. * capacity * (ib_int64_t) n_log_files;
  541. }
  542. ut_a(lsn >= first_header_lsn);
  543. file_no = ((ulint)((lsn - first_header_lsn) / capacity))
  544. % n_log_files;
  545. *log_file_offset = (lsn - first_header_lsn) % capacity;
  546. *log_file_offset = *log_file_offset + LOG_FILE_HDR_SIZE;
  547. return(file_no);
  548. }
  549. #ifndef UNIV_HOTBACKUP
  550. /********************************************************//**
  551. Sets the field values in group to correspond to a given lsn. For this function
  552. to work, the values must already be correctly initialized to correspond to
  553. some lsn, for instance, a checkpoint lsn. */
  554. UNIV_INTERN
  555. void
  556. log_group_set_fields(
  557. /*=================*/
  558. log_group_t* group, /*!< in/out: group */
  559. lsn_t lsn) /*!< in: lsn for which the values should be
  560. set */
  561. {
  562. group->lsn_offset = log_group_calc_lsn_offset(lsn, group);
  563. group->lsn = lsn;
  564. }
  565. /*****************************************************************//**
  566. Calculates the recommended highest values for lsn - last_checkpoint_lsn,
  567. lsn - buf_get_oldest_modification(), and lsn - max_archive_lsn_age.
  568. @return error value FALSE if the smallest log group is too small to
  569. accommodate the number of OS threads in the database server */
  570. static
  571. ibool
  572. log_calc_max_ages(void)
  573. /*===================*/
  574. {
  575. log_group_t* group;
  576. lsn_t margin;
  577. ulint free;
  578. ibool success = TRUE;
  579. lsn_t smallest_capacity;
  580. lsn_t archive_margin;
  581. lsn_t smallest_archive_margin;
  582. mutex_enter(&(log_sys->mutex));
  583. group = UT_LIST_GET_FIRST(log_sys->log_groups);
  584. ut_ad(group);
  585. smallest_capacity = LSN_MAX;
  586. smallest_archive_margin = LSN_MAX;
  587. while (group) {
  588. if (log_group_get_capacity(group) < smallest_capacity) {
  589. smallest_capacity = log_group_get_capacity(group);
  590. }
  591. archive_margin = log_group_get_capacity(group)
  592. - (group->file_size - LOG_FILE_HDR_SIZE)
  593. - LOG_ARCHIVE_EXTRA_MARGIN;
  594. if (archive_margin < smallest_archive_margin) {
  595. smallest_archive_margin = archive_margin;
  596. }
  597. group = UT_LIST_GET_NEXT(log_groups, group);
  598. }
  599. /* Add extra safety */
  600. smallest_capacity = smallest_capacity - smallest_capacity / 10;
  601. /* For each OS thread we must reserve so much free space in the
  602. smallest log group that it can accommodate the log entries produced
  603. by single query steps: running out of free log space is a serious
  604. system error which requires rebooting the database. */
  605. free = LOG_CHECKPOINT_FREE_PER_THREAD * (10 + srv_thread_concurrency)
  606. + LOG_CHECKPOINT_EXTRA_FREE;
  607. if (free >= smallest_capacity / 2) {
  608. success = FALSE;
  609. goto failure;
  610. } else {
  611. margin = smallest_capacity - free;
  612. }
  613. margin = margin - margin / 10; /* Add still some extra safety */
  614. log_sys->log_group_capacity = smallest_capacity;
  615. log_sys->max_modified_age_async = margin
  616. - margin / LOG_POOL_PREFLUSH_RATIO_ASYNC;
  617. log_sys->max_modified_age_sync = margin
  618. - margin / LOG_POOL_PREFLUSH_RATIO_SYNC;
  619. log_sys->max_checkpoint_age_async = margin - margin
  620. / LOG_POOL_CHECKPOINT_RATIO_ASYNC;
  621. log_sys->max_checkpoint_age = margin;
  622. #ifdef UNIV_LOG_ARCHIVE
  623. log_sys->max_archived_lsn_age = smallest_archive_margin;
  624. log_sys->max_archived_lsn_age_async = smallest_archive_margin
  625. - smallest_archive_margin / LOG_ARCHIVE_RATIO_ASYNC;
  626. #endif /* UNIV_LOG_ARCHIVE */
  627. failure:
  628. mutex_exit(&(log_sys->mutex));
  629. if (!success) {
  630. fprintf(stderr,
  631. "InnoDB: Error: ib_logfiles are too small"
  632. " for innodb_thread_concurrency %lu.\n"
  633. "InnoDB: The combined size of ib_logfiles"
  634. " should be bigger than\n"
  635. "InnoDB: 200 kB * innodb_thread_concurrency.\n"
  636. "InnoDB: To get mysqld to start up, set"
  637. " innodb_thread_concurrency in my.cnf\n"
  638. "InnoDB: to a lower value, for example, to 8."
  639. " After an ERROR-FREE shutdown\n"
  640. "InnoDB: of mysqld you can adjust the size of"
  641. " ib_logfiles, as explained in\n"
  642. "InnoDB: " REFMAN "adding-and-removing.html\n"
  643. "InnoDB: Cannot continue operation."
  644. " Calling exit(1).\n",
  645. (ulong) srv_thread_concurrency);
  646. exit(1);
  647. }
  648. return(success);
  649. }
  650. /******************************************************//**
  651. Initializes the log. */
  652. UNIV_INTERN
  653. void
  654. log_init(void)
  655. /*==========*/
  656. {
  657. log_sys = static_cast<log_t*>(mem_alloc(sizeof(log_t)));
  658. mutex_create(log_sys_mutex_key, &log_sys->mutex, SYNC_LOG);
  659. mutex_create(log_flush_order_mutex_key,
  660. &log_sys->log_flush_order_mutex,
  661. SYNC_LOG_FLUSH_ORDER);
  662. mutex_enter(&(log_sys->mutex));
  663. /* Start the lsn from one log block from zero: this way every
  664. log record has a start lsn != zero, a fact which we will use */
  665. log_sys->lsn = LOG_START_LSN;
  666. ut_a(LOG_BUFFER_SIZE >= 16 * OS_FILE_LOG_BLOCK_SIZE);
  667. ut_a(LOG_BUFFER_SIZE >= 4 * UNIV_PAGE_SIZE);
  668. log_sys->buf_ptr = static_cast<byte*>(
  669. mem_zalloc(LOG_BUFFER_SIZE + OS_FILE_LOG_BLOCK_SIZE));
  670. log_sys->buf = static_cast<byte*>(
  671. ut_align(log_sys->buf_ptr, OS_FILE_LOG_BLOCK_SIZE));
  672. log_sys->buf_size = LOG_BUFFER_SIZE;
  673. log_sys->is_extending = false;
  674. log_sys->max_buf_free = log_sys->buf_size / LOG_BUF_FLUSH_RATIO
  675. - LOG_BUF_FLUSH_MARGIN;
  676. log_sys->check_flush_or_checkpoint = TRUE;
  677. UT_LIST_INIT(log_sys->log_groups);
  678. log_sys->n_log_ios = 0;
  679. log_sys->n_log_ios_old = log_sys->n_log_ios;
  680. log_sys->last_printout_time = time(NULL);
  681. /*----------------------------*/
  682. log_sys->buf_next_to_write = 0;
  683. log_sys->write_lsn = 0;
  684. log_sys->current_flush_lsn = 0;
  685. log_sys->flushed_to_disk_lsn = 0;
  686. log_sys->written_to_some_lsn = log_sys->lsn;
  687. log_sys->written_to_all_lsn = log_sys->lsn;
  688. log_sys->n_pending_writes = 0;
  689. log_sys->no_flush_event = os_event_create();
  690. os_event_set(log_sys->no_flush_event);
  691. log_sys->one_flushed_event = os_event_create();
  692. os_event_set(log_sys->one_flushed_event);
  693. /*----------------------------*/
  694. log_sys->next_checkpoint_no = 0;
  695. log_sys->last_checkpoint_lsn = log_sys->lsn;
  696. log_sys->n_pending_checkpoint_writes = 0;
  697. rw_lock_create(checkpoint_lock_key, &log_sys->checkpoint_lock,
  698. SYNC_NO_ORDER_CHECK);
  699. log_sys->checkpoint_buf_ptr = static_cast<byte*>(
  700. mem_zalloc(2 * OS_FILE_LOG_BLOCK_SIZE));
  701. log_sys->checkpoint_buf = static_cast<byte*>(
  702. ut_align(log_sys->checkpoint_buf_ptr, OS_FILE_LOG_BLOCK_SIZE));
  703. /*----------------------------*/
  704. #ifdef UNIV_LOG_ARCHIVE
  705. /* Under MySQL, log archiving is always off */
  706. log_sys->archiving_state = LOG_ARCH_OFF;
  707. log_sys->archived_lsn = log_sys->lsn;
  708. log_sys->next_archived_lsn = 0;
  709. log_sys->n_pending_archive_ios = 0;
  710. rw_lock_create(archive_lock_key, &log_sys->archive_lock,
  711. SYNC_NO_ORDER_CHECK);
  712. log_sys->archive_buf = NULL;
  713. /* ut_align(
  714. ut_malloc(LOG_ARCHIVE_BUF_SIZE
  715. + OS_FILE_LOG_BLOCK_SIZE),
  716. OS_FILE_LOG_BLOCK_SIZE); */
  717. log_sys->archive_buf_size = 0;
  718. /* memset(log_sys->archive_buf, '\0', LOG_ARCHIVE_BUF_SIZE); */
  719. log_sys->archiving_on = os_event_create();
  720. #endif /* UNIV_LOG_ARCHIVE */
  721. /*----------------------------*/
  722. log_block_init(log_sys->buf, log_sys->lsn);
  723. log_block_set_first_rec_group(log_sys->buf, LOG_BLOCK_HDR_SIZE);
  724. log_sys->buf_free = LOG_BLOCK_HDR_SIZE;
  725. log_sys->lsn = LOG_START_LSN + LOG_BLOCK_HDR_SIZE;
  726. MONITOR_SET(MONITOR_LSN_CHECKPOINT_AGE,
  727. log_sys->lsn - log_sys->last_checkpoint_lsn);
  728. mutex_exit(&(log_sys->mutex));
  729. #ifdef UNIV_LOG_DEBUG
  730. recv_sys_create();
  731. recv_sys_init(buf_pool_get_curr_size());
  732. recv_sys->parse_start_lsn = log_sys->lsn;
  733. recv_sys->scanned_lsn = log_sys->lsn;
  734. recv_sys->scanned_checkpoint_no = 0;
  735. recv_sys->recovered_lsn = log_sys->lsn;
  736. recv_sys->limit_lsn = LSN_MAX;
  737. #endif
  738. }
  739. /******************************************************************//**
  740. Inits a log group to the log system. */
  741. UNIV_INTERN
  742. void
  743. log_group_init(
  744. /*===========*/
  745. ulint id, /*!< in: group id */
  746. ulint n_files, /*!< in: number of log files */
  747. lsn_t file_size, /*!< in: log file size in bytes */
  748. ulint space_id, /*!< in: space id of the file space
  749. which contains the log files of this
  750. group */
  751. ulint archive_space_id __attribute__((unused)))
  752. /*!< in: space id of the file space
  753. which contains some archived log
  754. files for this group; currently, only
  755. for the first log group this is
  756. used */
  757. {
  758. ulint i;
  759. log_group_t* group;
  760. group = static_cast<log_group_t*>(mem_alloc(sizeof(log_group_t)));
  761. group->id = id;
  762. group->n_files = n_files;
  763. group->file_size = file_size;
  764. group->space_id = space_id;
  765. group->state = LOG_GROUP_OK;
  766. group->lsn = LOG_START_LSN;
  767. group->lsn_offset = LOG_FILE_HDR_SIZE;
  768. group->n_pending_writes = 0;
  769. group->file_header_bufs_ptr = static_cast<byte**>(
  770. mem_zalloc(sizeof(byte*) * n_files));
  771. group->file_header_bufs = static_cast<byte**>(
  772. mem_zalloc(sizeof(byte**) * n_files));
  773. #ifdef UNIV_LOG_ARCHIVE
  774. group->archive_file_header_bufs_ptr = static_cast<byte*>(
  775. mem_zalloc( sizeof(byte*) * n_files));
  776. group->archive_file_header_bufs = static_cast<byte*>(
  777. mem_zalloc(sizeof(byte*) * n_files));
  778. #endif /* UNIV_LOG_ARCHIVE */
  779. for (i = 0; i < n_files; i++) {
  780. group->file_header_bufs_ptr[i] = static_cast<byte*>(
  781. mem_zalloc(LOG_FILE_HDR_SIZE + OS_FILE_LOG_BLOCK_SIZE));
  782. group->file_header_bufs[i] = static_cast<byte*>(
  783. ut_align(group->file_header_bufs_ptr[i],
  784. OS_FILE_LOG_BLOCK_SIZE));
  785. #ifdef UNIV_LOG_ARCHIVE
  786. group->archive_file_header_bufs_ptr[i] = static_cast<byte*>(
  787. mem_zalloc(LOG_FILE_HDR_SIZE + OS_FILE_LOG_BLOCK_SIZE));
  788. group->archive_file_header_bufs[i] = static_cast<byte*>(
  789. ut_align(group->archive_file_header_bufs_ptr[i],
  790. OS_FILE_LOG_BLOCK_SIZE));
  791. #endif /* UNIV_LOG_ARCHIVE */
  792. }
  793. #ifdef UNIV_LOG_ARCHIVE
  794. group->archive_space_id = archive_space_id;
  795. group->archived_file_no = 0;
  796. group->archived_offset = 0;
  797. #endif /* UNIV_LOG_ARCHIVE */
  798. group->checkpoint_buf_ptr = static_cast<byte*>(
  799. mem_zalloc(2 * OS_FILE_LOG_BLOCK_SIZE));
  800. group->checkpoint_buf = static_cast<byte*>(
  801. ut_align(group->checkpoint_buf_ptr,OS_FILE_LOG_BLOCK_SIZE));
  802. UT_LIST_ADD_LAST(log_groups, log_sys->log_groups, group);
  803. ut_a(log_calc_max_ages());
  804. }
  805. /******************************************************************//**
  806. Does the unlockings needed in flush i/o completion. */
  807. UNIV_INLINE
  808. void
  809. log_flush_do_unlocks(
  810. /*=================*/
  811. ulint code) /*!< in: any ORed combination of LOG_UNLOCK_FLUSH_LOCK
  812. and LOG_UNLOCK_NONE_FLUSHED_LOCK */
  813. {
  814. ut_ad(mutex_own(&(log_sys->mutex)));
  815. /* NOTE that we must own the log mutex when doing the setting of the
  816. events: this is because transactions will wait for these events to
  817. be set, and at that moment the log flush they were waiting for must
  818. have ended. If the log mutex were not reserved here, the i/o-thread
  819. calling this function might be preempted for a while, and when it
  820. resumed execution, it might be that a new flush had been started, and
  821. this function would erroneously signal the NEW flush as completed.
  822. Thus, the changes in the state of these events are performed
  823. atomically in conjunction with the changes in the state of
  824. log_sys->n_pending_writes etc. */
  825. if (code & LOG_UNLOCK_NONE_FLUSHED_LOCK) {
  826. os_event_set(log_sys->one_flushed_event);
  827. }
  828. if (code & LOG_UNLOCK_FLUSH_LOCK) {
  829. os_event_set(log_sys->no_flush_event);
  830. }
  831. }
  832. /******************************************************************//**
  833. Checks if a flush is completed for a log group and does the completion
  834. routine if yes.
  835. @return LOG_UNLOCK_NONE_FLUSHED_LOCK or 0 */
  836. UNIV_INLINE
  837. ulint
  838. log_group_check_flush_completion(
  839. /*=============================*/
  840. log_group_t* group) /*!< in: log group */
  841. {
  842. ut_ad(mutex_own(&(log_sys->mutex)));
  843. if (!log_sys->one_flushed && group->n_pending_writes == 0) {
  844. #ifdef UNIV_DEBUG
  845. if (log_debug_writes) {
  846. fprintf(stderr,
  847. "Log flushed first to group %lu\n",
  848. (ulong) group->id);
  849. }
  850. #endif /* UNIV_DEBUG */
  851. log_sys->written_to_some_lsn = log_sys->write_lsn;
  852. log_sys->one_flushed = TRUE;
  853. return(LOG_UNLOCK_NONE_FLUSHED_LOCK);
  854. }
  855. #ifdef UNIV_DEBUG
  856. if (log_debug_writes && (group->n_pending_writes == 0)) {
  857. fprintf(stderr, "Log flushed to group %lu\n",
  858. (ulong) group->id);
  859. }
  860. #endif /* UNIV_DEBUG */
  861. return(0);
  862. }
  863. /******************************************************//**
  864. Checks if a flush is completed and does the completion routine if yes.
  865. @return LOG_UNLOCK_FLUSH_LOCK or 0 */
  866. static
  867. ulint
  868. log_sys_check_flush_completion(void)
  869. /*================================*/
  870. {
  871. ulint move_start;
  872. ulint move_end;
  873. ut_ad(mutex_own(&(log_sys->mutex)));
  874. if (log_sys->n_pending_writes == 0) {
  875. log_sys->written_to_all_lsn = log_sys->write_lsn;
  876. log_sys->buf_next_to_write = log_sys->write_end_offset;
  877. if (log_sys->write_end_offset > log_sys->max_buf_free / 2) {
  878. /* Move the log buffer content to the start of the
  879. buffer */
  880. move_start = ut_calc_align_down(
  881. log_sys->write_end_offset,
  882. OS_FILE_LOG_BLOCK_SIZE);
  883. move_end = ut_calc_align(log_sys->buf_free,
  884. OS_FILE_LOG_BLOCK_SIZE);
  885. ut_memmove(log_sys->buf, log_sys->buf + move_start,
  886. move_end - move_start);
  887. log_sys->buf_free -= move_start;
  888. log_sys->buf_next_to_write -= move_start;
  889. }
  890. return(LOG_UNLOCK_FLUSH_LOCK);
  891. }
  892. return(0);
  893. }
  894. /******************************************************//**
  895. Completes an i/o to a log file. */
  896. UNIV_INTERN
  897. void
  898. log_io_complete(
  899. /*============*/
  900. log_group_t* group) /*!< in: log group or a dummy pointer */
  901. {
  902. ulint unlock;
  903. #ifdef UNIV_LOG_ARCHIVE
  904. if ((byte*) group == &log_archive_io) {
  905. /* It was an archive write */
  906. log_io_complete_archive();
  907. return;
  908. }
  909. #endif /* UNIV_LOG_ARCHIVE */
  910. if ((ulint) group & 0x1UL) {
  911. /* It was a checkpoint write */
  912. group = (log_group_t*)((ulint) group - 1);
  913. if (srv_unix_file_flush_method != SRV_UNIX_O_DSYNC
  914. && srv_unix_file_flush_method != SRV_UNIX_NOSYNC) {
  915. fil_flush(group->space_id);
  916. }
  917. #ifdef UNIV_DEBUG
  918. if (log_debug_writes) {
  919. fprintf(stderr,
  920. "Checkpoint info written to group %lu\n",
  921. group->id);
  922. }
  923. #endif /* UNIV_DEBUG */
  924. log_io_complete_checkpoint();
  925. return;
  926. }
  927. ut_error; /*!< We currently use synchronous writing of the
  928. logs and cannot end up here! */
  929. if (srv_unix_file_flush_method != SRV_UNIX_O_DSYNC
  930. && srv_unix_file_flush_method != SRV_UNIX_NOSYNC
  931. && srv_flush_log_at_trx_commit != 2) {
  932. fil_flush(group->space_id);
  933. }
  934. mutex_enter(&(log_sys->mutex));
  935. ut_ad(!recv_no_log_write);
  936. ut_a(group->n_pending_writes > 0);
  937. ut_a(log_sys->n_pending_writes > 0);
  938. group->n_pending_writes--;
  939. log_sys->n_pending_writes--;
  940. MONITOR_DEC(MONITOR_PENDING_LOG_WRITE);
  941. unlock = log_group_check_flush_completion(group);
  942. unlock = unlock | log_sys_check_flush_completion();
  943. log_flush_do_unlocks(unlock);
  944. mutex_exit(&(log_sys->mutex));
  945. }
  946. /******************************************************//**
  947. Writes a log file header to a log file space. */
  948. static
  949. void
  950. log_group_file_header_flush(
  951. /*========================*/
  952. log_group_t* group, /*!< in: log group */
  953. ulint nth_file, /*!< in: header to the nth file in the
  954. log file space */
  955. lsn_t start_lsn) /*!< in: log file data starts at this
  956. lsn */
  957. {
  958. byte* buf;
  959. lsn_t dest_offset;
  960. ut_ad(mutex_own(&(log_sys->mutex)));
  961. ut_ad(!recv_no_log_write);
  962. ut_a(nth_file < group->n_files);
  963. buf = *(group->file_header_bufs + nth_file);
  964. mach_write_to_4(buf + LOG_GROUP_ID, group->id);
  965. mach_write_to_8(buf + LOG_FILE_START_LSN, start_lsn);
  966. /* Wipe over possible label of mysqlbackup --restore */
  967. memcpy(buf + LOG_FILE_WAS_CREATED_BY_HOT_BACKUP, " ", 4);
  968. dest_offset = nth_file * group->file_size;
  969. #ifdef UNIV_DEBUG
  970. if (log_debug_writes) {
  971. fprintf(stderr,
  972. "Writing log file header to group %lu file %lu\n",
  973. (ulong) group->id, (ulong) nth_file);
  974. }
  975. #endif /* UNIV_DEBUG */
  976. if (log_do_write) {
  977. log_sys->n_log_ios++;
  978. MONITOR_INC(MONITOR_LOG_IO);
  979. srv_stats.os_log_pending_writes.inc();
  980. fil_io(OS_FILE_WRITE | OS_FILE_LOG, true, group->space_id, 0,
  981. (ulint) (dest_offset / UNIV_PAGE_SIZE),
  982. (ulint) (dest_offset % UNIV_PAGE_SIZE),
  983. OS_FILE_LOG_BLOCK_SIZE,
  984. buf, group);
  985. srv_stats.os_log_pending_writes.dec();
  986. }
  987. }
  988. /******************************************************//**
  989. Stores a 4-byte checksum to the trailer checksum field of a log block
  990. before writing it to a log file. This checksum is used in recovery to
  991. check the consistency of a log block. */
  992. static
  993. void
  994. log_block_store_checksum(
  995. /*=====================*/
  996. byte* block) /*!< in/out: pointer to a log block */
  997. {
  998. log_block_set_checksum(block, log_block_calc_checksum(block));
  999. }
  1000. /******************************************************//**
  1001. Writes a buffer to a log file group. */
  1002. UNIV_INTERN
  1003. void
  1004. log_group_write_buf(
  1005. /*================*/
  1006. log_group_t* group, /*!< in: log group */
  1007. byte* buf, /*!< in: buffer */
  1008. ulint len, /*!< in: buffer len; must be divisible
  1009. by OS_FILE_LOG_BLOCK_SIZE */
  1010. lsn_t start_lsn, /*!< in: start lsn of the buffer; must
  1011. be divisible by
  1012. OS_FILE_LOG_BLOCK_SIZE */
  1013. ulint new_data_offset)/*!< in: start offset of new data in
  1014. buf: this parameter is used to decide
  1015. if we have to write a new log file
  1016. header */
  1017. {
  1018. ulint write_len;
  1019. ibool write_header;
  1020. lsn_t next_offset;
  1021. ulint i;
  1022. ut_ad(mutex_own(&(log_sys->mutex)));
  1023. ut_ad(!recv_no_log_write);
  1024. ut_a(len % OS_FILE_LOG_BLOCK_SIZE == 0);
  1025. ut_a(start_lsn % OS_FILE_LOG_BLOCK_SIZE == 0);
  1026. if (new_data_offset == 0) {
  1027. write_header = TRUE;
  1028. } else {
  1029. write_header = FALSE;
  1030. }
  1031. loop:
  1032. if (len == 0) {
  1033. return;
  1034. }
  1035. next_offset = log_group_calc_lsn_offset(start_lsn, group);
  1036. if ((next_offset % group->file_size == LOG_FILE_HDR_SIZE)
  1037. && write_header) {
  1038. /* We start to write a new log file instance in the group */
  1039. ut_a(next_offset / group->file_size <= ULINT_MAX);
  1040. log_group_file_header_flush(group, (ulint)
  1041. (next_offset / group->file_size),
  1042. start_lsn);
  1043. srv_stats.os_log_written.add(OS_FILE_LOG_BLOCK_SIZE);
  1044. srv_stats.log_writes.inc();
  1045. }
  1046. if ((next_offset % group->file_size) + len > group->file_size) {
  1047. /* if the above condition holds, then the below expression
  1048. is < len which is ulint, so the typecast is ok */
  1049. write_len = (ulint)
  1050. (group->file_size - (next_offset % group->file_size));
  1051. } else {
  1052. write_len = len;
  1053. }
  1054. #ifdef UNIV_DEBUG
  1055. if (log_debug_writes) {
  1056. fprintf(stderr,
  1057. "Writing log file segment to group %lu"
  1058. " offset " LSN_PF " len %lu\n"
  1059. "start lsn " LSN_PF "\n"
  1060. "First block n:o %lu last block n:o %lu\n",
  1061. (ulong) group->id, next_offset,
  1062. write_len,
  1063. start_lsn,
  1064. (ulong) log_block_get_hdr_no(buf),
  1065. (ulong) log_block_get_hdr_no(
  1066. buf + write_len - OS_FILE_LOG_BLOCK_SIZE));
  1067. ut_a(log_block_get_hdr_no(buf)
  1068. == log_block_convert_lsn_to_no(start_lsn));
  1069. for (i = 0; i < write_len / OS_FILE_LOG_BLOCK_SIZE; i++) {
  1070. ut_a(log_block_get_hdr_no(buf) + i
  1071. == log_block_get_hdr_no(
  1072. buf + i * OS_FILE_LOG_BLOCK_SIZE));
  1073. }
  1074. }
  1075. #endif /* UNIV_DEBUG */
  1076. /* Calculate the checksums for each log block and write them to
  1077. the trailer fields of the log blocks */
  1078. for (i = 0; i < write_len / OS_FILE_LOG_BLOCK_SIZE; i++) {
  1079. log_block_store_checksum(buf + i * OS_FILE_LOG_BLOCK_SIZE);
  1080. }
  1081. if (log_do_write) {
  1082. log_sys->n_log_ios++;
  1083. MONITOR_INC(MONITOR_LOG_IO);
  1084. srv_stats.os_log_pending_writes.inc();
  1085. ut_a(next_offset / UNIV_PAGE_SIZE <= ULINT_MAX);
  1086. fil_io(OS_FILE_WRITE | OS_FILE_LOG, true, group->space_id, 0,
  1087. (ulint) (next_offset / UNIV_PAGE_SIZE),
  1088. (ulint) (next_offset % UNIV_PAGE_SIZE), write_len, buf,
  1089. group);
  1090. srv_stats.os_log_pending_writes.dec();
  1091. srv_stats.os_log_written.add(write_len);
  1092. srv_stats.log_writes.inc();
  1093. }
  1094. if (write_len < len) {
  1095. start_lsn += write_len;
  1096. len -= write_len;
  1097. buf += write_len;
  1098. write_header = TRUE;
  1099. goto loop;
  1100. }
  1101. }
  1102. /******************************************************//**
  1103. This function is called, e.g., when a transaction wants to commit. It checks
  1104. that the log has been written to the log file up to the last log entry written
  1105. by the transaction. If there is a flush running, it waits and checks if the
  1106. flush flushed enough. If not, starts a new flush. */
  1107. UNIV_INTERN
  1108. void
  1109. log_write_up_to(
  1110. /*============*/
  1111. lsn_t lsn, /*!< in: log sequence number up to which
  1112. the log should be written,
  1113. LSN_MAX if not specified */
  1114. ulint wait, /*!< in: LOG_NO_WAIT, LOG_WAIT_ONE_GROUP,
  1115. or LOG_WAIT_ALL_GROUPS */
  1116. ibool flush_to_disk)
  1117. /*!< in: TRUE if we want the written log
  1118. also to be flushed to disk */
  1119. {
  1120. log_group_t* group;
  1121. ulint start_offset;
  1122. ulint end_offset;
  1123. ulint area_start;
  1124. ulint area_end;
  1125. #ifdef UNIV_DEBUG
  1126. ulint loop_count = 0;
  1127. #endif /* UNIV_DEBUG */
  1128. ulint unlock;
  1129. ut_ad(!srv_read_only_mode);
  1130. if (recv_no_ibuf_operations) {
  1131. /* Recovery is running and no operations on the log files are
  1132. allowed yet (the variable name .._no_ibuf_.. is misleading) */
  1133. return;
  1134. }
  1135. loop:
  1136. #ifdef UNIV_DEBUG
  1137. loop_count++;
  1138. ut_ad(loop_count < 5);
  1139. # if 0
  1140. if (loop_count > 2) {
  1141. fprintf(stderr, "Log loop count %lu\n", loop_count);
  1142. }
  1143. # endif
  1144. #endif
  1145. mutex_enter(&(log_sys->mutex));
  1146. ut_ad(!recv_no_log_write);
  1147. if (flush_to_disk
  1148. && log_sys->flushed_to_disk_lsn >= lsn) {
  1149. mutex_exit(&(log_sys->mutex));
  1150. return;
  1151. }
  1152. if (!flush_to_disk
  1153. && (log_sys->written_to_all_lsn >= lsn
  1154. || (log_sys->written_to_some_lsn >= lsn
  1155. && wait != LOG_WAIT_ALL_GROUPS))) {
  1156. mutex_exit(&(log_sys->mutex));
  1157. return;
  1158. }
  1159. if (log_sys->n_pending_writes > 0) {
  1160. /* A write (+ possibly flush to disk) is running */
  1161. if (flush_to_disk
  1162. && log_sys->current_flush_lsn >= lsn) {
  1163. /* The write + flush will write enough: wait for it to
  1164. complete */
  1165. goto do_waits;
  1166. }
  1167. if (!flush_to_disk
  1168. && log_sys->write_lsn >= lsn) {
  1169. /* The write will write enough: wait for it to
  1170. complete */
  1171. goto do_waits;
  1172. }
  1173. mutex_exit(&(log_sys->mutex));
  1174. /* Wait for the write to complete and try to start a new
  1175. write */
  1176. os_event_wait(log_sys->no_flush_event);
  1177. goto loop;
  1178. }
  1179. if (!flush_to_disk
  1180. && log_sys->buf_free == log_sys->buf_next_to_write) {
  1181. /* Nothing to write and no flush to disk requested */
  1182. mutex_exit(&(log_sys->mutex));
  1183. return;
  1184. }
  1185. #ifdef UNIV_DEBUG
  1186. if (log_debug_writes) {
  1187. fprintf(stderr,
  1188. "Writing log from " LSN_PF " up to lsn " LSN_PF "\n",
  1189. log_sys->written_to_all_lsn,
  1190. log_sys->lsn);
  1191. }
  1192. #endif /* UNIV_DEBUG */
  1193. log_sys->n_pending_writes++;
  1194. MONITOR_INC(MONITOR_PENDING_LOG_WRITE);
  1195. group = UT_LIST_GET_FIRST(log_sys->log_groups);
  1196. group->n_pending_writes++; /*!< We assume here that we have only
  1197. one log group! */
  1198. os_event_reset(log_sys->no_flush_event);
  1199. os_event_reset(log_sys->one_flushed_event);
  1200. start_offset = log_sys->buf_next_to_write;
  1201. end_offset = log_sys->buf_free;
  1202. area_start = ut_calc_align_down(start_offset, OS_FILE_LOG_BLOCK_SIZE);
  1203. area_end = ut_calc_align(end_offset, OS_FILE_LOG_BLOCK_SIZE);
  1204. ut_ad(area_end - area_start > 0);
  1205. log_sys->write_lsn = log_sys->lsn;
  1206. if (flush_to_disk) {
  1207. log_sys->current_flush_lsn = log_sys->lsn;
  1208. }
  1209. log_sys->one_flushed = FALSE;
  1210. log_block_set_flush_bit(log_sys->buf + area_start, TRUE);
  1211. log_block_set_checkpoint_no(
  1212. log_sys->buf + area_end - OS_FILE_LOG_BLOCK_SIZE,
  1213. log_sys->next_checkpoint_no);
  1214. /* Copy the last, incompletely written, log block a log block length
  1215. up, so that when the flush operation writes from the log buffer, the
  1216. segment to write will not be changed by writers to the log */
  1217. ut_memcpy(log_sys->buf + area_end,
  1218. log_sys->buf + area_end - OS_FILE_LOG_BLOCK_SIZE,
  1219. OS_FILE_LOG_BLOCK_SIZE);
  1220. log_sys->buf_free += OS_FILE_LOG_BLOCK_SIZE;
  1221. log_sys->write_end_offset = log_sys->buf_free;
  1222. group = UT_LIST_GET_FIRST(log_sys->log_groups);
  1223. /* Do the write to the log files */
  1224. while (group) {
  1225. log_group_write_buf(
  1226. group, log_sys->buf + area_start,
  1227. area_end - area_start,
  1228. ut_uint64_align_down(log_sys->written_to_all_lsn,
  1229. OS_FILE_LOG_BLOCK_SIZE),
  1230. start_offset - area_start);
  1231. log_group_set_fields(group, log_sys->write_lsn);
  1232. group = UT_LIST_GET_NEXT(log_groups, group);
  1233. }
  1234. mutex_exit(&(log_sys->mutex));
  1235. if (srv_unix_file_flush_method == SRV_UNIX_O_DSYNC) {
  1236. /* O_DSYNC means the OS did not buffer the log file at all:
  1237. so we have also flushed to disk what we have written */
  1238. log_sys->flushed_to_disk_lsn = log_sys->write_lsn;
  1239. } else if (flush_to_disk) {
  1240. group = UT_LIST_GET_FIRST(log_sys->log_groups);
  1241. fil_flush(group->space_id);
  1242. log_sys->flushed_to_disk_lsn = log_sys->write_lsn;
  1243. }
  1244. mutex_enter(&(log_sys->mutex));
  1245. group = UT_LIST_GET_FIRST(log_sys->log_groups);
  1246. ut_a(group->n_pending_writes == 1);
  1247. ut_a(log_sys->n_pending_writes == 1);
  1248. group->n_pending_writes--;
  1249. log_sys->n_pending_writes--;
  1250. MONITOR_DEC(MONITOR_PENDING_LOG_WRITE);
  1251. unlock = log_group_check_flush_completion(group);
  1252. unlock = unlock | log_sys_check_flush_completion();
  1253. log_flush_do_unlocks(unlock);
  1254. mutex_exit(&(log_sys->mutex));
  1255. return;
  1256. do_waits:
  1257. mutex_exit(&(log_sys->mutex));
  1258. switch (wait) {
  1259. case LOG_WAIT_ONE_GROUP:
  1260. os_event_wait(log_sys->one_flushed_event);
  1261. break;
  1262. case LOG_WAIT_ALL_GROUPS:
  1263. os_event_wait(log_sys->no_flush_event);
  1264. break;
  1265. #ifdef UNIV_DEBUG
  1266. case LOG_NO_WAIT:
  1267. break;
  1268. default:
  1269. ut_error;
  1270. #endif /* UNIV_DEBUG */
  1271. }
  1272. }
  1273. /****************************************************************//**
  1274. Does a syncronous flush of the log buffer to disk. */
  1275. UNIV_INTERN
  1276. void
  1277. log_buffer_flush_to_disk(void)
  1278. /*==========================*/
  1279. {
  1280. lsn_t lsn;
  1281. ut_ad(!srv_read_only_mode);
  1282. mutex_enter(&(log_sys->mutex));
  1283. lsn = log_sys->lsn;
  1284. mutex_exit(&(log_sys->mutex));
  1285. log_write_up_to(lsn, LOG_WAIT_ALL_GROUPS, TRUE);
  1286. }
  1287. /****************************************************************//**
  1288. This functions writes the log buffer to the log file and if 'flush'
  1289. is set it forces a flush of the log file as well. This is meant to be
  1290. called from background master thread only as it does not wait for
  1291. the write (+ possible flush) to finish. */
  1292. UNIV_INTERN
  1293. void
  1294. log_buffer_sync_in_background(
  1295. /*==========================*/
  1296. ibool flush) /*!< in: flush the logs to disk */
  1297. {
  1298. lsn_t lsn;
  1299. mutex_enter(&(log_sys->mutex));
  1300. lsn = log_sys->lsn;
  1301. mutex_exit(&(log_sys->mutex));
  1302. log_write_up_to(lsn, LOG_NO_WAIT, flush);
  1303. }
  1304. /********************************************************************
  1305. Tries to establish a big enough margin of free space in the log buffer, such
  1306. that a new log entry can be catenated without an immediate need for a flush. */
  1307. static
  1308. void
  1309. log_flush_margin(void)
  1310. /*==================*/
  1311. {
  1312. log_t* log = log_sys;
  1313. lsn_t lsn = 0;
  1314. mutex_enter(&(log->mutex));
  1315. if (log->buf_free > log->max_buf_free) {
  1316. if (log->n_pending_writes > 0) {
  1317. /* A flush is running: hope that it will provide enough
  1318. free space */
  1319. } else {
  1320. lsn = log->lsn;
  1321. }
  1322. }
  1323. mutex_exit(&(log->mutex));
  1324. if (lsn) {
  1325. log_write_up_to(lsn, LOG_NO_WAIT, FALSE);
  1326. }
  1327. }
  1328. /****************************************************************//**
  1329. Advances the smallest lsn for which there are unflushed dirty blocks in the
  1330. buffer pool. NOTE: this function may only be called if the calling thread owns
  1331. no synchronization objects!
  1332. @return false if there was a flush batch of the same type running,
  1333. which means that we could not start this flush batch */
  1334. static
  1335. bool
  1336. log_preflush_pool_modified_pages(
  1337. /*=============================*/
  1338. lsn_t new_oldest) /*!< in: try to advance oldest_modified_lsn
  1339. at least to this lsn */
  1340. {
  1341. bool success;
  1342. ulint n_pages;
  1343. if (recv_recovery_on) {
  1344. /* If the recovery is running, we must first apply all
  1345. log records to their respective file pages to get the
  1346. right modify lsn values to these pages: otherwise, there
  1347. might be pages on disk which are not yet recovered to the
  1348. current lsn, and even after calling this function, we could
  1349. not know how up-to-date the disk version of the database is,
  1350. and we could not make a new checkpoint on the basis of the
  1351. info on the buffer pool only. */
  1352. recv_apply_hashed_log_recs(TRUE);
  1353. }
  1354. success = buf_flush_list(ULINT_MAX, new_oldest, &n_pages);
  1355. buf_flush_wait_batch_end(NULL, BUF_FLUSH_LIST);
  1356. if (!success) {
  1357. MONITOR_INC(MONITOR_FLUSH_SYNC_WAITS);
  1358. }
  1359. MONITOR_INC_VALUE_CUMULATIVE(
  1360. MONITOR_FLUSH_SYNC_TOTAL_PAGE,
  1361. MONITOR_FLUSH_SYNC_COUNT,
  1362. MONITOR_FLUSH_SYNC_PAGES,
  1363. n_pages);
  1364. return(success);
  1365. }
  1366. /******************************************************//**
  1367. Completes a checkpoint. */
  1368. static
  1369. void
  1370. log_complete_checkpoint(void)
  1371. /*=========================*/
  1372. {
  1373. ut_ad(mutex_own(&(log_sys->mutex)));
  1374. ut_ad(log_sys->n_pending_checkpoint_writes == 0);
  1375. log_sys->next_checkpoint_no++;
  1376. log_sys->last_checkpoint_lsn = log_sys->next_checkpoint_lsn;
  1377. MONITOR_SET(MONITOR_LSN_CHECKPOINT_AGE,
  1378. log_sys->lsn - log_sys->last_checkpoint_lsn);
  1379. rw_lock_x_unlock_gen(&(log_sys->checkpoint_lock), LOG_CHECKPOINT);
  1380. }
  1381. /******************************************************//**
  1382. Completes an asynchronous checkpoint info write i/o to a log file. */
  1383. static
  1384. void
  1385. log_io_complete_checkpoint(void)
  1386. /*============================*/
  1387. {
  1388. mutex_enter(&(log_sys->mutex));
  1389. ut_ad(log_sys->n_pending_checkpoint_writes > 0);
  1390. log_sys->n_pending_checkpoint_writes--;
  1391. MONITOR_DEC(MONITOR_PENDING_CHECKPOINT_WRITE);
  1392. if (log_sys->n_pending_checkpoint_writes == 0) {
  1393. log_complete_checkpoint();
  1394. }
  1395. mutex_exit(&(log_sys->mutex));
  1396. }
  1397. /*******************************************************************//**
  1398. Writes info to a checkpoint about a log group. */
  1399. static
  1400. void
  1401. log_checkpoint_set_nth_group_info(
  1402. /*==============================*/
  1403. byte* buf, /*!< in: buffer for checkpoint info */
  1404. ulint n, /*!< in: nth slot */
  1405. ulint file_no,/*!< in: archived file number */
  1406. ulint offset) /*!< in: archived file offset */
  1407. {
  1408. ut_ad(n < LOG_MAX_N_GROUPS);
  1409. mach_write_to_4(buf + LOG_CHECKPOINT_GROUP_ARRAY
  1410. + 8 * n + LOG_CHECKPOINT_ARCHIVED_FILE_NO, file_no);
  1411. mach_write_to_4(buf + LOG_CHECKPOINT_GROUP_ARRAY
  1412. + 8 * n + LOG_CHECKPOINT_ARCHIVED_OFFSET, offset);
  1413. }
  1414. /*******************************************************************//**
  1415. Gets info from a checkpoint about a log group. */
  1416. UNIV_INTERN
  1417. void
  1418. log_checkpoint_get_nth_group_info(
  1419. /*==============================*/
  1420. const byte* buf, /*!< in: buffer containing checkpoint info */
  1421. ulint n, /*!< in: nth slot */
  1422. ulint* file_no,/*!< out: archived file number */
  1423. ulint* offset) /*!< out: archived file offset */
  1424. {
  1425. ut_ad(n < LOG_MAX_N_GROUPS);
  1426. *file_no = mach_read_from_4(buf + LOG_CHECKPOINT_GROUP_ARRAY
  1427. + 8 * n + LOG_CHECKPOINT_ARCHIVED_FILE_NO);
  1428. *offset = mach_read_from_4(buf + LOG_CHECKPOINT_GROUP_ARRAY
  1429. + 8 * n + LOG_CHECKPOINT_ARCHIVED_OFFSET);
  1430. }
  1431. /******************************************************//**
  1432. Writes the checkpoint info to a log group header. */
  1433. static
  1434. void
  1435. log_group_checkpoint(
  1436. /*=================*/
  1437. log_group_t* group) /*!< in: log group */
  1438. {
  1439. log_group_t* group2;
  1440. #ifdef UNIV_LOG_ARCHIVE
  1441. ib_uint64_t archived_lsn;
  1442. ib_uint64_t next_archived_lsn;
  1443. #endif /* UNIV_LOG_ARCHIVE */
  1444. lsn_t lsn_offset;
  1445. ulint write_offset;
  1446. ulint fold;
  1447. byte* buf;
  1448. ulint i;
  1449. ut_ad(!srv_read_only_mode);
  1450. ut_ad(mutex_own(&(log_sys->mutex)));
  1451. #if LOG_CHECKPOINT_SIZE > OS_FILE_LOG_BLOCK_SIZE
  1452. # error "LOG_CHECKPOINT_SIZE > OS_FILE_LOG_BLOCK_SIZE"
  1453. #endif
  1454. buf = group->checkpoint_buf;
  1455. mach_write_to_8(buf + LOG_CHECKPOINT_NO, log_sys->next_checkpoint_no);
  1456. mach_write_to_8(buf + LOG_CHECKPOINT_LSN, log_sys->next_checkpoint_lsn);
  1457. lsn_offset = log_group_calc_lsn_offset(log_sys->next_checkpoint_lsn,
  1458. group);
  1459. mach_write_to_4(buf + LOG_CHECKPOINT_OFFSET_LOW32,
  1460. lsn_offset & 0xFFFFFFFFUL);
  1461. mach_write_to_4(buf + LOG_CHECKPOINT_OFFSET_HIGH32,
  1462. lsn_offset >> 32);
  1463. mach_write_to_4(buf + LOG_CHECKPOINT_LOG_BUF_SIZE, log_sys->buf_size);
  1464. #ifdef UNIV_LOG_ARCHIVE
  1465. if (log_sys->archiving_state == LOG_ARCH_OFF) {
  1466. archived_lsn = LSN_MAX;
  1467. } else {
  1468. archived_lsn = log_sys->archived_lsn;
  1469. if (archived_lsn != log_sys->next_archived_lsn) {
  1470. next_archived_lsn = log_sys->next_archived_lsn;
  1471. /* For debugging only */
  1472. }
  1473. }
  1474. mach_write_to_8(buf + LOG_CHECKPOINT_ARCHIVED_LSN, archived_lsn);
  1475. #else /* UNIV_LOG_ARCHIVE */
  1476. mach_write_to_8(buf + LOG_CHECKPOINT_ARCHIVED_LSN, LSN_MAX);
  1477. #endif /* UNIV_LOG_ARCHIVE */
  1478. for (i = 0; i < LOG_MAX_N_GROUPS; i++) {
  1479. log_checkpoint_set_nth_group_info(buf, i, 0, 0);
  1480. }
  1481. group2 = UT_LIST_GET_FIRST(log_sys->log_groups);
  1482. while (group2) {
  1483. log_checkpoint_set_nth_group_info(buf, group2->id,
  1484. #ifdef UNIV_LOG_ARCHIVE
  1485. group2->archived_file_no,
  1486. group2->archived_offset
  1487. #else /* UNIV_LOG_ARCHIVE */
  1488. 0, 0
  1489. #endif /* UNIV_LOG_ARCHIVE */
  1490. );
  1491. group2 = UT_LIST_GET_NEXT(log_groups, group2);
  1492. }
  1493. fold = ut_fold_binary(buf, LOG_CHECKPOINT_CHECKSUM_1);
  1494. mach_write_to_4(buf + LOG_CHECKPOINT_CHECKSUM_1, fold);
  1495. fold = ut_fold_binary(buf + LOG_CHECKPOINT_LSN,
  1496. LOG_CHECKPOINT_CHECKSUM_2 - LOG_CHECKPOINT_LSN);
  1497. mach_write_to_4(buf + LOG_CHECKPOINT_CHECKSUM_2, fold);
  1498. /* We alternate the physical place of the checkpoint info in the first
  1499. log file */
  1500. if ((log_sys->next_checkpoint_no & 1) == 0) {
  1501. write_offset = LOG_CHECKPOINT_1;
  1502. } else {
  1503. write_offset = LOG_CHECKPOINT_2;
  1504. }
  1505. if (log_do_write) {
  1506. if (log_sys->n_pending_checkpoint_writes == 0) {
  1507. rw_lock_x_lock_gen(&(log_sys->checkpoint_lock),
  1508. LOG_CHECKPOINT);
  1509. }
  1510. log_sys->n_pending_checkpoint_writes++;
  1511. MONITOR_INC(MONITOR_PENDING_CHECKPOINT_WRITE);
  1512. log_sys->n_log_ios++;
  1513. MONITOR_INC(MONITOR_LOG_IO);
  1514. /* We send as the last parameter the group machine address
  1515. added with 1, as we want to distinguish between a normal log
  1516. file write and a checkpoint field write */
  1517. fil_io(OS_FILE_WRITE | OS_FILE_LOG, false, group->space_id, 0,
  1518. write_offset / UNIV_PAGE_SIZE,
  1519. write_offset % UNIV_PAGE_SIZE,
  1520. OS_FILE_LOG_BLOCK_SIZE,
  1521. buf, ((byte*) group + 1));
  1522. ut_ad(((ulint) group & 0x1UL) == 0);
  1523. }
  1524. }
  1525. #endif /* !UNIV_HOTBACKUP */
  1526. #ifdef UNIV_HOTBACKUP
  1527. /******************************************************//**
  1528. Writes info to a buffer of a log group when log files are created in
  1529. backup restoration. */
  1530. UNIV_INTERN
  1531. void
  1532. log_reset_first_header_and_checkpoint(
  1533. /*==================================*/
  1534. byte* hdr_buf,/*!< in: buffer which will be written to the
  1535. start of the first log file */
  1536. ib_uint64_t start) /*!< in: lsn of the start of the first log file;
  1537. we pretend that there is a checkpoint at
  1538. start + LOG_BLOCK_HDR_SIZE */
  1539. {
  1540. ulint fold;
  1541. byte* buf;
  1542. ib_uint64_t lsn;
  1543. mach_write_to_4(hdr_buf + LOG_GROUP_ID, 0);
  1544. mach_write_to_8(hdr_buf + LOG_FILE_START_LSN, start);
  1545. lsn = start + LOG_BLOCK_HDR_SIZE;
  1546. /* Write the label of mysqlbackup --restore */
  1547. strcpy((char*) hdr_buf + LOG_FILE_WAS_CREATED_BY_HOT_BACKUP,
  1548. "ibbackup ");
  1549. ut_sprintf_timestamp((char*) hdr_buf
  1550. + (LOG_FILE_WAS_CREATED_BY_HOT_BACKUP
  1551. + (sizeof "ibbackup ") - 1));
  1552. buf = hdr_buf + LOG_CHECKPOINT_1;
  1553. mach_write_to_8(buf + LOG_CHECKPOINT_NO, 0);
  1554. mach_write_to_8(buf + LOG_CHECKPOINT_LSN, lsn);
  1555. mach_write_to_4(buf + LOG_CHECKPOINT_OFFSET_LOW32,
  1556. LOG_FILE_HDR_SIZE + LOG_BLOCK_HDR_SIZE);
  1557. mach_write_to_4(buf + LOG_CHECKPOINT_OFFSET_HIGH32, 0);
  1558. mach_write_to_4(buf + LOG_CHECKPOINT_LOG_BUF_SIZE, 2 * 1024 * 1024);
  1559. mach_write_to_8(buf + LOG_CHECKPOINT_ARCHIVED_LSN, LSN_MAX);
  1560. fold = ut_fold_binary(buf, LOG_CHECKPOINT_CHECKSUM_1);
  1561. mach_write_to_4(buf + LOG_CHECKPOINT_CHECKSUM_1, fold);
  1562. fold = ut_fold_binary(buf + LOG_CHECKPOINT_LSN,
  1563. LOG_CHECKPOINT_CHECKSUM_2 - LOG_CHECKPOINT_LSN);
  1564. mach_write_to_4(buf + LOG_CHECKPOINT_CHECKSUM_2, fold);
  1565. /* Starting from InnoDB-3.23.50, we should also write info on
  1566. allocated size in the tablespace, but unfortunately we do not
  1567. know it here */
  1568. }
  1569. #endif /* UNIV_HOTBACKUP */
  1570. #ifndef UNIV_HOTBACKUP
  1571. /******************************************************//**
  1572. Reads a checkpoint info from a log group header to log_sys->checkpoint_buf. */
  1573. UNIV_INTERN
  1574. void
  1575. log_group_read_checkpoint_info(
  1576. /*===========================*/
  1577. log_group_t* group, /*!< in: log group */
  1578. ulint field) /*!< in: LOG_CHECKPOINT_1 or LOG_CHECKPOINT_2 */
  1579. {
  1580. ut_ad(mutex_own(&(log_sys->mutex)));
  1581. log_sys->n_log_ios++;
  1582. MONITOR_INC(MONITOR_LOG_IO);
  1583. fil_io(OS_FILE_READ | OS_FILE_LOG, true, group->space_id, 0,
  1584. field / UNIV_PAGE_SIZE, field % UNIV_PAGE_SIZE,
  1585. OS_FILE_LOG_BLOCK_SIZE, log_sys->checkpoint_buf, NULL);
  1586. }
  1587. /******************************************************//**
  1588. Writes checkpoint info to groups. */
  1589. UNIV_INTERN
  1590. void
  1591. log_groups_write_checkpoint_info(void)
  1592. /*==================================*/
  1593. {
  1594. log_group_t* group;
  1595. ut_ad(mutex_own(&(log_sys->mutex)));
  1596. if (!srv_read_only_mode) {
  1597. for (group = UT_LIST_GET_FIRST(log_sys->log_groups);
  1598. group;
  1599. group = UT_LIST_GET_NEXT(log_groups, group)) {
  1600. log_group_checkpoint(group);
  1601. }
  1602. }
  1603. }
  1604. /******************************************************//**
  1605. Makes a checkpoint. Note that this function does not flush dirty
  1606. blocks from the buffer pool: it only checks what is lsn of the oldest
  1607. modification in the pool, and writes information about the lsn in
  1608. log files. Use log_make_checkpoint_at to flush also the pool.
  1609. @return TRUE if success, FALSE if a checkpoint write was already running */
  1610. UNIV_INTERN
  1611. ibool
  1612. log_checkpoint(
  1613. /*===========*/
  1614. ibool sync, /*!< in: TRUE if synchronous operation is
  1615. desired */
  1616. ibool write_always) /*!< in: the function normally checks if the
  1617. the new checkpoint would have a greater
  1618. lsn than the previous one: if not, then no
  1619. physical write is done; by setting this
  1620. parameter TRUE, a physical write will always be
  1621. made to log files */
  1622. {
  1623. lsn_t oldest_lsn;
  1624. ut_ad(!srv_read_only_mode);
  1625. if (recv_recovery_is_on()) {
  1626. recv_apply_hashed_log_recs(TRUE);
  1627. }
  1628. if (srv_unix_file_flush_method != SRV_UNIX_NOSYNC) {
  1629. fil_flush_file_spaces(FIL_TABLESPACE);
  1630. }
  1631. mutex_enter(&(log_sys->mutex));
  1632. ut_ad(!recv_no_log_write);
  1633. oldest_lsn = log_buf_pool_get_oldest_modification();
  1634. mutex_exit(&(log_sys->mutex));
  1635. /* Because log also contains headers and dummy log records,
  1636. if the buffer pool contains no dirty buffers, oldest_lsn
  1637. gets the value log_sys->lsn from the previous function,
  1638. and we must make sure that the log is flushed up to that
  1639. lsn. If there are dirty buffers in the buffer pool, then our
  1640. write-ahead-logging algorithm ensures that the log has been flushed
  1641. up to oldest_lsn. */
  1642. log_write_up_to(oldest_lsn, LOG_WAIT_ALL_GROUPS, TRUE);
  1643. mutex_enter(&(log_sys->mutex));
  1644. if (!write_always
  1645. && log_sys->last_checkpoint_lsn >= oldest_lsn) {
  1646. mutex_exit(&(log_sys->mutex));
  1647. return(TRUE);
  1648. }
  1649. ut_ad(log_sys->flushed_to_disk_lsn >= oldest_lsn);
  1650. if (log_sys->n_pending_checkpoint_writes > 0) {
  1651. /* A checkpoint write is running */
  1652. mutex_exit(&(log_sys->mutex));
  1653. if (sync) {
  1654. /* Wait for the checkpoint write to complete */
  1655. rw_lock_s_lock(&(log_sys->checkpoint_lock));
  1656. rw_lock_s_unlock(&(log_sys->checkpoint_lock));
  1657. }
  1658. return(FALSE);
  1659. }
  1660. log_sys->next_checkpoint_lsn = oldest_lsn;
  1661. #ifdef UNIV_DEBUG
  1662. if (log_debug_writes) {
  1663. fprintf(stderr, "Making checkpoint no "
  1664. LSN_PF " at lsn " LSN_PF "\n",
  1665. log_sys->next_checkpoint_no,
  1666. oldest_lsn);
  1667. }
  1668. #endif /* UNIV_DEBUG */
  1669. log_groups_write_checkpoint_info();
  1670. MONITOR_INC(MONITOR_NUM_CHECKPOINT);
  1671. mutex_exit(&(log_sys->mutex));
  1672. if (sync) {
  1673. /* Wait for the checkpoint write to complete */
  1674. rw_lock_s_lock(&(log_sys->checkpoint_lock));
  1675. rw_lock_s_unlock(&(log_sys->checkpoint_lock));
  1676. }
  1677. return(TRUE);
  1678. }
  1679. /****************************************************************//**
  1680. Makes a checkpoint at a given lsn or later. */
  1681. UNIV_INTERN
  1682. void
  1683. log_make_checkpoint_at(
  1684. /*===================*/
  1685. lsn_t lsn, /*!< in: make a checkpoint at this or a
  1686. later lsn, if LSN_MAX, makes
  1687. a checkpoint at the latest lsn */
  1688. ibool write_always) /*!< in: the function normally checks if
  1689. the new checkpoint would have a
  1690. greater lsn than the previous one: if
  1691. not, then no physical write is done;
  1692. by setting this parameter TRUE, a
  1693. physical write will always be made to
  1694. log files */
  1695. {
  1696. /* Preflush pages synchronously */
  1697. while (!log_preflush_pool_modified_pages(lsn)) {
  1698. /* Flush as much as we can */
  1699. }
  1700. while (!log_checkpoint(TRUE, write_always)) {
  1701. /* Force a checkpoint */
  1702. }
  1703. }
  1704. /****************************************************************//**
  1705. Tries to establish a big enough margin of free space in the log groups, such
  1706. that a new log entry can be catenated without an immediate need for a
  1707. checkpoint. NOTE: this function may only be called if the calling thread
  1708. owns no synchronization objects! */
  1709. static
  1710. void
  1711. log_checkpoint_margin(void)
  1712. /*=======================*/
  1713. {
  1714. log_t* log = log_sys;
  1715. lsn_t age;
  1716. lsn_t checkpoint_age;
  1717. ib_uint64_t advance;
  1718. lsn_t oldest_lsn;
  1719. ibool checkpoint_sync;
  1720. ibool do_checkpoint;
  1721. bool success;
  1722. loop:
  1723. checkpoint_sync = FALSE;
  1724. do_checkpoint = FALSE;
  1725. advance = 0;
  1726. mutex_enter(&(log->mutex));
  1727. ut_ad(!recv_no_log_write);
  1728. if (log->check_flush_or_checkpoint == FALSE) {
  1729. mutex_exit(&(log->mutex));
  1730. return;
  1731. }
  1732. oldest_lsn = log_buf_pool_get_oldest_modification();
  1733. age = log->lsn - oldest_lsn;
  1734. if (age > log->max_modified_age_sync) {
  1735. /* A flush is urgent: we have to do a synchronous preflush */
  1736. advance = 2 * (age - log->max_modified_age_sync);
  1737. }
  1738. checkpoint_age = log->lsn - log->last_checkpoint_lsn;
  1739. if (checkpoint_age > log->max_checkpoint_age) {
  1740. /* A checkpoint is urgent: we do it synchronously */
  1741. checkpoint_sync = TRUE;
  1742. do_checkpoint = TRUE;
  1743. } else if (checkpoint_age > log->max_checkpoint_age_async) {
  1744. /* A checkpoint is not urgent: do it asynchronously */
  1745. do_checkpoint = TRUE;
  1746. log->check_flush_or_checkpoint = FALSE;
  1747. } else {
  1748. log->check_flush_or_checkpoint = FALSE;
  1749. }
  1750. mutex_exit(&(log->mutex));
  1751. if (advance) {
  1752. lsn_t new_oldest = oldest_lsn + advance;
  1753. success = log_preflush_pool_modified_pages(new_oldest);
  1754. /* If the flush succeeded, this thread has done its part
  1755. and can proceed. If it did not succeed, there was another
  1756. thread doing a flush at the same time. */
  1757. if (!success) {
  1758. mutex_enter(&(log->mutex));
  1759. log->check_flush_or_checkpoint = TRUE;
  1760. mutex_exit(&(log->mutex));
  1761. goto loop;
  1762. }
  1763. }
  1764. if (do_checkpoint) {
  1765. log_checkpoint(checkpoint_sync, FALSE);
  1766. if (checkpoint_sync) {
  1767. goto loop;
  1768. }
  1769. }
  1770. }
  1771. /******************************************************//**
  1772. Reads a specified log segment to a buffer. */
  1773. UNIV_INTERN
  1774. void
  1775. log_group_read_log_seg(
  1776. /*===================*/
  1777. ulint type, /*!< in: LOG_ARCHIVE or LOG_RECOVER */
  1778. byte* buf, /*!< in: buffer where to read */
  1779. log_group_t* group, /*!< in: log group */
  1780. lsn_t start_lsn, /*!< in: read area start */
  1781. lsn_t end_lsn) /*!< in: read area end */
  1782. {
  1783. ulint len;
  1784. lsn_t source_offset;
  1785. bool sync;
  1786. ut_ad(mutex_own(&(log_sys->mutex)));
  1787. sync = (type == LOG_RECOVER);
  1788. loop:
  1789. source_offset = log_group_calc_lsn_offset(start_lsn, group);
  1790. ut_a(end_lsn - start_lsn <= ULINT_MAX);
  1791. len = (ulint) (end_lsn - start_lsn);
  1792. ut_ad(len != 0);
  1793. if ((source_offset % group->file_size) + len > group->file_size) {
  1794. /* If the above condition is true then len (which is ulint)
  1795. is > the expression below, so the typecast is ok */
  1796. len = (ulint) (group->file_size -
  1797. (source_offset % group->file_size));
  1798. }
  1799. #ifdef UNIV_LOG_ARCHIVE
  1800. if (type == LOG_ARCHIVE) {
  1801. log_sys->n_pending_archive_ios++;
  1802. }
  1803. #endif /* UNIV_LOG_ARCHIVE */
  1804. log_sys->n_log_ios++;
  1805. MONITOR_INC(MONITOR_LOG_IO);
  1806. ut_a(source_offset / UNIV_PAGE_SIZE <= ULINT_MAX);
  1807. fil_io(OS_FILE_READ | OS_FILE_LOG, sync, group->space_id, 0,
  1808. (ulint) (source_offset / UNIV_PAGE_SIZE),
  1809. (ulint) (source_offset % UNIV_PAGE_SIZE),
  1810. len, buf, NULL);
  1811. start_lsn += len;
  1812. buf += len;
  1813. if (start_lsn != end_lsn) {
  1814. goto loop;
  1815. }
  1816. }
  1817. #ifdef UNIV_LOG_ARCHIVE
  1818. /******************************************************//**
  1819. Generates an archived log file name. */
  1820. UNIV_INTERN
  1821. void
  1822. log_archived_file_name_gen(
  1823. /*=======================*/
  1824. char* buf, /*!< in: buffer where to write */
  1825. ulint id __attribute__((unused)),
  1826. /*!< in: group id;
  1827. currently we only archive the first group */
  1828. ulint file_no)/*!< in: file number */
  1829. {
  1830. sprintf(buf, "%sib_arch_log_%010lu", srv_arch_dir, (ulong) file_no);
  1831. }
  1832. /******************************************************//**
  1833. Writes a log file header to a log file space. */
  1834. static
  1835. void
  1836. log_group_archive_file_header_write(
  1837. /*================================*/
  1838. log_group_t* group, /*!< in: log group */
  1839. ulint nth_file, /*!< in: header to the nth file in the
  1840. archive log file space */
  1841. ulint file_no, /*!< in: archived file number */
  1842. ib_uint64_t start_lsn) /*!< in: log file data starts at this
  1843. lsn */
  1844. {
  1845. byte* buf;
  1846. ulint dest_offset;
  1847. ut_ad(mutex_own(&(log_sys->mutex)));
  1848. ut_a(nth_file < group->n_files);
  1849. buf = *(group->archive_file_header_bufs + nth_file);
  1850. mach_write_to_4(buf + LOG_GROUP_ID, group->id);
  1851. mach_write_to_8(buf + LOG_FILE_START_LSN, start_lsn);
  1852. mach_write_to_4(buf + LOG_FILE_NO, file_no);
  1853. mach_write_to_4(buf + LOG_FILE_ARCH_COMPLETED, FALSE);
  1854. dest_offset = nth_file * group->file_size;
  1855. log_sys->n_log_ios++;
  1856. MONITOR_INC(MONITOR_LOG_IO);
  1857. fil_io(OS_FILE_WRITE | OS_FILE_LOG, true, group->archive_space_id,
  1858. dest_offset / UNIV_PAGE_SIZE,
  1859. dest_offset % UNIV_PAGE_SIZE,
  1860. 2 * OS_FILE_LOG_BLOCK_SIZE,
  1861. buf, &log_archive_io);
  1862. }
  1863. /******************************************************//**
  1864. Writes a log file header to a completed archived log file. */
  1865. static
  1866. void
  1867. log_group_archive_completed_header_write(
  1868. /*=====================================*/
  1869. log_group_t* group, /*!< in: log group */
  1870. ulint nth_file, /*!< in: header to the nth file in the
  1871. archive log file space */
  1872. ib_uint64_t end_lsn) /*!< in: end lsn of the file */
  1873. {
  1874. byte* buf;
  1875. ulint dest_offset;
  1876. ut_ad(mutex_own(&(log_sys->mutex)));
  1877. ut_a(nth_file < group->n_files);
  1878. buf = *(group->archive_file_header_bufs + nth_file);
  1879. mach_write_to_4(buf + LOG_FILE_ARCH_COMPLETED, TRUE);
  1880. mach_write_to_8(buf + LOG_FILE_END_LSN, end_lsn);
  1881. dest_offset = nth_file * group->file_size + LOG_FILE_ARCH_COMPLETED;
  1882. log_sys->n_log_ios++;
  1883. MONITOR_INC(MONITOR_LOG_IO);
  1884. fil_io(OS_FILE_WRITE | OS_FILE_LOG, true, group->archive_space_id,
  1885. dest_offset / UNIV_PAGE_SIZE,
  1886. dest_offset % UNIV_PAGE_SIZE,
  1887. OS_FILE_LOG_BLOCK_SIZE,
  1888. buf + LOG_FILE_ARCH_COMPLETED,
  1889. &log_archive_io);
  1890. }
  1891. /******************************************************//**
  1892. Does the archive writes for a single log group. */
  1893. static
  1894. void
  1895. log_group_archive(
  1896. /*==============*/
  1897. log_group_t* group) /*!< in: log group */
  1898. {
  1899. os_file_t file_handle;
  1900. lsn_t start_lsn;
  1901. lsn_t end_lsn;
  1902. char name[1024];
  1903. byte* buf;
  1904. ulint len;
  1905. ibool ret;
  1906. lsn_t next_offset;
  1907. ulint n_files;
  1908. ulint open_mode;
  1909. ut_ad(mutex_own(&(log_sys->mutex)));
  1910. start_lsn = log_sys->archived_lsn;
  1911. ut_a(start_lsn % OS_FILE_LOG_BLOCK_SIZE == 0);
  1912. end_lsn = log_sys->next_archived_lsn;
  1913. ut_a(end_lsn % OS_FILE_LOG_BLOCK_SIZE == 0);
  1914. buf = log_sys->archive_buf;
  1915. n_files = 0;
  1916. next_offset = group->archived_offset;
  1917. loop:
  1918. if ((next_offset % group->file_size == 0)
  1919. || (fil_space_get_size(group->archive_space_id) == 0)) {
  1920. /* Add the file to the archive file space; create or open the
  1921. file */
  1922. if (next_offset % group->file_size == 0) {
  1923. open_mode = OS_FILE_CREATE;
  1924. } else {
  1925. open_mode = OS_FILE_OPEN;
  1926. }
  1927. log_archived_file_name_gen(name, group->id,
  1928. group->archived_file_no + n_files);
  1929. file_handle = os_file_create(innodb_file_log_key,
  1930. name, open_mode,
  1931. OS_FILE_AIO,
  1932. OS_DATA_FILE, &ret);
  1933. if (!ret && (open_mode == OS_FILE_CREATE)) {
  1934. file_handle = os_file_create(
  1935. innodb_file_log_key, name, OS_FILE_OPEN,
  1936. OS_FILE_AIO, OS_DATA_FILE, &ret);
  1937. }
  1938. if (!ret) {
  1939. fprintf(stderr,
  1940. "InnoDB: Cannot create or open"
  1941. " archive log file %s.\n"
  1942. "InnoDB: Cannot continue operation.\n"
  1943. "InnoDB: Check that the log archive"
  1944. " directory exists,\n"
  1945. "InnoDB: you have access rights to it, and\n"
  1946. "InnoDB: there is space available.\n", name);
  1947. exit(1);
  1948. }
  1949. #ifdef UNIV_DEBUG
  1950. if (log_debug_writes) {
  1951. fprintf(stderr, "Created archive file %s\n", name);
  1952. }
  1953. #endif /* UNIV_DEBUG */
  1954. ret = os_file_close(file_handle);
  1955. ut_a(ret);
  1956. /* Add the archive file as a node to the space */
  1957. fil_node_create(name, group->file_size / UNIV_PAGE_SIZE,
  1958. group->archive_space_id, FALSE);
  1959. if (next_offset % group->file_size == 0) {
  1960. log_group_archive_file_header_write(
  1961. group, n_files,
  1962. group->archived_file_no + n_files,
  1963. start_lsn);
  1964. next_offset += LOG_FILE_HDR_SIZE;
  1965. }
  1966. }
  1967. len = end_lsn - start_lsn;
  1968. if (group->file_size < (next_offset % group->file_size) + len) {
  1969. len = group->file_size - (next_offset % group->file_size);
  1970. }
  1971. #ifdef UNIV_DEBUG
  1972. if (log_debug_writes) {
  1973. fprintf(stderr,
  1974. "Archiving starting at lsn " LSN_PF ", len %lu"
  1975. " to group %lu\n",
  1976. start_lsn,
  1977. (ulong) len, (ulong) group->id);
  1978. }
  1979. #endif /* UNIV_DEBUG */
  1980. log_sys->n_pending_archive_ios++;
  1981. log_sys->n_log_ios++;
  1982. MONITOR_INC(MONITOR_LOG_IO);
  1983. fil_io(OS_FILE_WRITE | OS_FILE_LOG, false, group->archive_space_id,
  1984. (ulint) (next_offset / UNIV_PAGE_SIZE),
  1985. (ulint) (next_offset % UNIV_PAGE_SIZE),
  1986. ut_calc_align(len, OS_FILE_LOG_BLOCK_SIZE), buf,
  1987. &log_archive_io);
  1988. start_lsn += len;
  1989. next_offset += len;
  1990. buf += len;
  1991. if (next_offset % group->file_size == 0) {
  1992. n_files++;
  1993. }
  1994. if (end_lsn != start_lsn) {
  1995. goto loop;
  1996. }
  1997. group->next_archived_file_no = group->archived_file_no + n_files;
  1998. group->next_archived_offset = next_offset % group->file_size;
  1999. ut_a(group->next_archived_offset % OS_FILE_LOG_BLOCK_SIZE == 0);
  2000. }
  2001. /*****************************************************//**
  2002. (Writes to the archive of each log group.) Currently, only the first
  2003. group is archived. */
  2004. static
  2005. void
  2006. log_archive_groups(void)
  2007. /*====================*/
  2008. {
  2009. log_group_t* group;
  2010. ut_ad(mutex_own(&(log_sys->mutex)));
  2011. group = UT_LIST_GET_FIRST(log_sys->log_groups);
  2012. log_group_archive(group);
  2013. }
  2014. /*****************************************************//**
  2015. Completes the archiving write phase for (each log group), currently,
  2016. the first log group. */
  2017. static
  2018. void
  2019. log_archive_write_complete_groups(void)
  2020. /*===================================*/
  2021. {
  2022. log_group_t* group;
  2023. ulint end_offset;
  2024. ulint trunc_files;
  2025. ulint n_files;
  2026. ib_uint64_t start_lsn;
  2027. ib_uint64_t end_lsn;
  2028. ulint i;
  2029. ut_ad(mutex_own(&(log_sys->mutex)));
  2030. group = UT_LIST_GET_FIRST(log_sys->log_groups);
  2031. group->archived_file_no = group->next_archived_file_no;
  2032. group->archived_offset = group->next_archived_offset;
  2033. /* Truncate from the archive file space all but the last
  2034. file, or if it has been written full, all files */
  2035. n_files = (UNIV_PAGE_SIZE
  2036. * fil_space_get_size(group->archive_space_id))
  2037. / group->file_size;
  2038. ut_ad(n_files > 0);
  2039. end_offset = group->archived_offset;
  2040. if (end_offset % group->file_size == 0) {
  2041. trunc_files = n_files;
  2042. } else {
  2043. trunc_files = n_files - 1;
  2044. }
  2045. #ifdef UNIV_DEBUG
  2046. if (log_debug_writes && trunc_files) {
  2047. fprintf(stderr,
  2048. "Complete file(s) archived to group %lu\n",
  2049. (ulong) group->id);
  2050. }
  2051. #endif /* UNIV_DEBUG */
  2052. /* Calculate the archive file space start lsn */
  2053. start_lsn = log_sys->next_archived_lsn
  2054. - (end_offset - LOG_FILE_HDR_SIZE + trunc_files
  2055. * (group->file_size - LOG_FILE_HDR_SIZE));
  2056. end_lsn = start_lsn;
  2057. for (i = 0; i < trunc_files; i++) {
  2058. end_lsn += group->file_size - LOG_FILE_HDR_SIZE;
  2059. /* Write a notice to the headers of archived log
  2060. files that the file write has been completed */
  2061. log_group_archive_completed_header_write(group, i, end_lsn);
  2062. }
  2063. fil_space_truncate_start(group->archive_space_id,
  2064. trunc_files * group->file_size);
  2065. #ifdef UNIV_DEBUG
  2066. if (log_debug_writes) {
  2067. fputs("Archiving writes completed\n", stderr);
  2068. }
  2069. #endif /* UNIV_DEBUG */
  2070. }
  2071. /******************************************************//**
  2072. Completes an archiving i/o. */
  2073. static
  2074. void
  2075. log_archive_check_completion_low(void)
  2076. /*==================================*/
  2077. {
  2078. ut_ad(mutex_own(&(log_sys->mutex)));
  2079. if (log_sys->n_pending_archive_ios == 0
  2080. && log_sys->archiving_phase == LOG_ARCHIVE_READ) {
  2081. #ifdef UNIV_DEBUG
  2082. if (log_debug_writes) {
  2083. fputs("Archiving read completed\n", stderr);
  2084. }
  2085. #endif /* UNIV_DEBUG */
  2086. /* Archive buffer has now been read in: start archive writes */
  2087. log_sys->archiving_phase = LOG_ARCHIVE_WRITE;
  2088. log_archive_groups();
  2089. }
  2090. if (log_sys->n_pending_archive_ios == 0
  2091. && log_sys->archiving_phase == LOG_ARCHIVE_WRITE) {
  2092. log_archive_write_complete_groups();
  2093. log_sys->archived_lsn = log_sys->next_archived_lsn;
  2094. rw_lock_x_unlock_gen(&(log_sys->archive_lock), LOG_ARCHIVE);
  2095. }
  2096. }
  2097. /******************************************************//**
  2098. Completes an archiving i/o. */
  2099. static
  2100. void
  2101. log_io_complete_archive(void)
  2102. /*=========================*/
  2103. {
  2104. log_group_t* group;
  2105. mutex_enter(&(log_sys->mutex));
  2106. group = UT_LIST_GET_FIRST(log_sys->log_groups);
  2107. mutex_exit(&(log_sys->mutex));
  2108. fil_flush(group->archive_space_id);
  2109. mutex_enter(&(log_sys->mutex));
  2110. ut_ad(log_sys->n_pending_archive_ios > 0);
  2111. log_sys->n_pending_archive_ios--;
  2112. log_archive_check_completion_low();
  2113. mutex_exit(&(log_sys->mutex));
  2114. }
  2115. /********************************************************************//**
  2116. Starts an archiving operation.
  2117. @return TRUE if succeed, FALSE if an archiving operation was already running */
  2118. UNIV_INTERN
  2119. ibool
  2120. log_archive_do(
  2121. /*===========*/
  2122. ibool sync, /*!< in: TRUE if synchronous operation is desired */
  2123. ulint* n_bytes)/*!< out: archive log buffer size, 0 if nothing to
  2124. archive */
  2125. {
  2126. ibool calc_new_limit;
  2127. ib_uint64_t start_lsn;
  2128. ib_uint64_t limit_lsn;
  2129. calc_new_limit = TRUE;
  2130. loop:
  2131. mutex_enter(&(log_sys->mutex));
  2132. switch (log_sys->archiving_state) {
  2133. case LOG_ARCH_OFF:
  2134. arch_none:
  2135. mutex_exit(&(log_sys->mutex));
  2136. *n_bytes = 0;
  2137. return(TRUE);
  2138. case LOG_ARCH_STOPPED:
  2139. case LOG_ARCH_STOPPING2:
  2140. mutex_exit(&(log_sys->mutex));
  2141. os_event_wait(log_sys->archiving_on);
  2142. goto loop;
  2143. }
  2144. start_lsn = log_sys->archived_lsn;
  2145. if (calc_new_limit) {
  2146. ut_a(log_sys->archive_buf_size % OS_FILE_LOG_BLOCK_SIZE == 0);
  2147. limit_lsn = start_lsn + log_sys->archive_buf_size;
  2148. *n_bytes = log_sys->archive_buf_size;
  2149. if (limit_lsn >= log_sys->lsn) {
  2150. limit_lsn = ut_uint64_align_down(
  2151. log_sys->lsn, OS_FILE_LOG_BLOCK_SIZE);
  2152. }
  2153. }
  2154. if (log_sys->archived_lsn >= limit_lsn) {
  2155. goto arch_none;
  2156. }
  2157. if (log_sys->written_to_all_lsn < limit_lsn) {
  2158. mutex_exit(&(log_sys->mutex));
  2159. log_write_up_to(limit_lsn, LOG_WAIT_ALL_GROUPS, TRUE);
  2160. calc_new_limit = FALSE;
  2161. goto loop;
  2162. }
  2163. if (log_sys->n_pending_archive_ios > 0) {
  2164. /* An archiving operation is running */
  2165. mutex_exit(&(log_sys->mutex));
  2166. if (sync) {
  2167. rw_lock_s_lock(&(log_sys->archive_lock));
  2168. rw_lock_s_unlock(&(log_sys->archive_lock));
  2169. }
  2170. *n_bytes = log_sys->archive_buf_size;
  2171. return(FALSE);
  2172. }
  2173. rw_lock_x_lock_gen(&(log_sys->archive_lock), LOG_ARCHIVE);
  2174. log_sys->archiving_phase = LOG_ARCHIVE_READ;
  2175. log_sys->next_archived_lsn = limit_lsn;
  2176. #ifdef UNIV_DEBUG
  2177. if (log_debug_writes) {
  2178. fprintf(stderr,
  2179. "Archiving from lsn " LSN_PF " to lsn " LSN_PF "\n",
  2180. log_sys->archived_lsn, limit_lsn);
  2181. }
  2182. #endif /* UNIV_DEBUG */
  2183. /* Read the log segment to the archive buffer */
  2184. log_group_read_log_seg(LOG_ARCHIVE, log_sys->archive_buf,
  2185. UT_LIST_GET_FIRST(log_sys->log_groups),
  2186. start_lsn, limit_lsn);
  2187. mutex_exit(&(log_sys->mutex));
  2188. if (sync) {
  2189. rw_lock_s_lock(&(log_sys->archive_lock));
  2190. rw_lock_s_unlock(&(log_sys->archive_lock));
  2191. }
  2192. *n_bytes = log_sys->archive_buf_size;
  2193. return(TRUE);
  2194. }
  2195. /****************************************************************//**
  2196. Writes the log contents to the archive at least up to the lsn when this
  2197. function was called. */
  2198. static
  2199. void
  2200. log_archive_all(void)
  2201. /*=================*/
  2202. {
  2203. ib_uint64_t present_lsn;
  2204. ulint dummy;
  2205. mutex_enter(&(log_sys->mutex));
  2206. if (log_sys->archiving_state == LOG_ARCH_OFF) {
  2207. mutex_exit(&(log_sys->mutex));
  2208. return;
  2209. }
  2210. present_lsn = log_sys->lsn;
  2211. mutex_exit(&(log_sys->mutex));
  2212. log_pad_current_log_block();
  2213. for (;;) {
  2214. mutex_enter(&(log_sys->mutex));
  2215. if (present_lsn <= log_sys->archived_lsn) {
  2216. mutex_exit(&(log_sys->mutex));
  2217. return;
  2218. }
  2219. mutex_exit(&(log_sys->mutex));
  2220. log_archive_do(TRUE, &dummy);
  2221. }
  2222. }
  2223. /*****************************************************//**
  2224. Closes the possible open archive log file (for each group) the first group,
  2225. and if it was open, increments the group file count by 2, if desired. */
  2226. static
  2227. void
  2228. log_archive_close_groups(
  2229. /*=====================*/
  2230. ibool increment_file_count) /*!< in: TRUE if we want to increment
  2231. the file count */
  2232. {
  2233. log_group_t* group;
  2234. ulint trunc_len;
  2235. ut_ad(mutex_own(&(log_sys->mutex)));
  2236. if (log_sys->archiving_state == LOG_ARCH_OFF) {
  2237. return;
  2238. }
  2239. group = UT_LIST_GET_FIRST(log_sys->log_groups);
  2240. trunc_len = UNIV_PAGE_SIZE
  2241. * fil_space_get_size(group->archive_space_id);
  2242. if (trunc_len > 0) {
  2243. ut_a(trunc_len == group->file_size);
  2244. /* Write a notice to the headers of archived log
  2245. files that the file write has been completed */
  2246. log_group_archive_completed_header_write(
  2247. group, 0, log_sys->archived_lsn);
  2248. fil_space_truncate_start(group->archive_space_id,
  2249. trunc_len);
  2250. if (increment_file_count) {
  2251. group->archived_offset = 0;
  2252. group->archived_file_no += 2;
  2253. }
  2254. #ifdef UNIV_DEBUG
  2255. if (log_debug_writes) {
  2256. fprintf(stderr,
  2257. "Incrementing arch file no to %lu"
  2258. " in log group %lu\n",
  2259. (ulong) group->archived_file_no + 2,
  2260. (ulong) group->id);
  2261. }
  2262. #endif /* UNIV_DEBUG */
  2263. }
  2264. }
  2265. /****************************************************************//**
  2266. Writes the log contents to the archive up to the lsn when this function was
  2267. called, and stops the archiving. When archiving is started again, the archived
  2268. log file numbers start from 2 higher, so that the archiving will not write
  2269. again to the archived log files which exist when this function returns.
  2270. @return DB_SUCCESS or DB_ERROR */
  2271. UNIV_INTERN
  2272. ulint
  2273. log_archive_stop(void)
  2274. /*==================*/
  2275. {
  2276. ibool success;
  2277. mutex_enter(&(log_sys->mutex));
  2278. if (log_sys->archiving_state != LOG_ARCH_ON) {
  2279. mutex_exit(&(log_sys->mutex));
  2280. return(DB_ERROR);
  2281. }
  2282. log_sys->archiving_state = LOG_ARCH_STOPPING;
  2283. mutex_exit(&(log_sys->mutex));
  2284. log_archive_all();
  2285. mutex_enter(&(log_sys->mutex));
  2286. log_sys->archiving_state = LOG_ARCH_STOPPING2;
  2287. os_event_reset(log_sys->archiving_on);
  2288. mutex_exit(&(log_sys->mutex));
  2289. /* Wait for a possible archiving operation to end */
  2290. rw_lock_s_lock(&(log_sys->archive_lock));
  2291. rw_lock_s_unlock(&(log_sys->archive_lock));
  2292. mutex_enter(&(log_sys->mutex));
  2293. /* Close all archived log files, incrementing the file count by 2,
  2294. if appropriate */
  2295. log_archive_close_groups(TRUE);
  2296. mutex_exit(&(log_sys->mutex));
  2297. /* Make a checkpoint, so that if recovery is needed, the file numbers
  2298. of new archived log files will start from the right value */
  2299. success = FALSE;
  2300. while (!success) {
  2301. success = log_checkpoint(TRUE, TRUE);
  2302. }
  2303. mutex_enter(&(log_sys->mutex));
  2304. log_sys->archiving_state = LOG_ARCH_STOPPED;
  2305. mutex_exit(&(log_sys->mutex));
  2306. return(DB_SUCCESS);
  2307. }
  2308. /****************************************************************//**
  2309. Starts again archiving which has been stopped.
  2310. @return DB_SUCCESS or DB_ERROR */
  2311. UNIV_INTERN
  2312. ulint
  2313. log_archive_start(void)
  2314. /*===================*/
  2315. {
  2316. mutex_enter(&(log_sys->mutex));
  2317. if (log_sys->archiving_state != LOG_ARCH_STOPPED) {
  2318. mutex_exit(&(log_sys->mutex));
  2319. return(DB_ERROR);
  2320. }
  2321. log_sys->archiving_state = LOG_ARCH_ON;
  2322. os_event_set(log_sys->archiving_on);
  2323. mutex_exit(&(log_sys->mutex));
  2324. return(DB_SUCCESS);
  2325. }
  2326. /****************************************************************//**
  2327. Stop archiving the log so that a gap may occur in the archived log files.
  2328. @return DB_SUCCESS or DB_ERROR */
  2329. UNIV_INTERN
  2330. ulint
  2331. log_archive_noarchivelog(void)
  2332. /*==========================*/
  2333. {
  2334. loop:
  2335. mutex_enter(&(log_sys->mutex));
  2336. if (log_sys->archiving_state == LOG_ARCH_STOPPED
  2337. || log_sys->archiving_state == LOG_ARCH_OFF) {
  2338. log_sys->archiving_state = LOG_ARCH_OFF;
  2339. os_event_set(log_sys->archiving_on);
  2340. mutex_exit(&(log_sys->mutex));
  2341. return(DB_SUCCESS);
  2342. }
  2343. mutex_exit(&(log_sys->mutex));
  2344. log_archive_stop();
  2345. os_thread_sleep(500000);
  2346. goto loop;
  2347. }
  2348. /****************************************************************//**
  2349. Start archiving the log so that a gap may occur in the archived log files.
  2350. @return DB_SUCCESS or DB_ERROR */
  2351. UNIV_INTERN
  2352. ulint
  2353. log_archive_archivelog(void)
  2354. /*========================*/
  2355. {
  2356. mutex_enter(&(log_sys->mutex));
  2357. if (log_sys->archiving_state == LOG_ARCH_OFF) {
  2358. log_sys->archiving_state = LOG_ARCH_ON;
  2359. log_sys->archived_lsn
  2360. = ut_uint64_align_down(log_sys->lsn,
  2361. OS_FILE_LOG_BLOCK_SIZE);
  2362. mutex_exit(&(log_sys->mutex));
  2363. return(DB_SUCCESS);
  2364. }
  2365. mutex_exit(&(log_sys->mutex));
  2366. return(DB_ERROR);
  2367. }
  2368. /****************************************************************//**
  2369. Tries to establish a big enough margin of free space in the log groups, such
  2370. that a new log entry can be catenated without an immediate need for
  2371. archiving. */
  2372. static
  2373. void
  2374. log_archive_margin(void)
  2375. /*====================*/
  2376. {
  2377. log_t* log = log_sys;
  2378. ulint age;
  2379. ibool sync;
  2380. ulint dummy;
  2381. loop:
  2382. mutex_enter(&(log->mutex));
  2383. if (log->archiving_state == LOG_ARCH_OFF) {
  2384. mutex_exit(&(log->mutex));
  2385. return;
  2386. }
  2387. age = log->lsn - log->archived_lsn;
  2388. if (age > log->max_archived_lsn_age) {
  2389. /* An archiving is urgent: we have to do synchronous i/o */
  2390. sync = TRUE;
  2391. } else if (age > log->max_archived_lsn_age_async) {
  2392. /* An archiving is not urgent: we do asynchronous i/o */
  2393. sync = FALSE;
  2394. } else {
  2395. /* No archiving required yet */
  2396. mutex_exit(&(log->mutex));
  2397. return;
  2398. }
  2399. mutex_exit(&(log->mutex));
  2400. log_archive_do(sync, &dummy);
  2401. if (sync == TRUE) {
  2402. /* Check again that enough was written to the archive */
  2403. goto loop;
  2404. }
  2405. }
  2406. #endif /* UNIV_LOG_ARCHIVE */
  2407. /********************************************************************//**
  2408. Checks that there is enough free space in the log to start a new query step.
  2409. Flushes the log buffer or makes a new checkpoint if necessary. NOTE: this
  2410. function may only be called if the calling thread owns no synchronization
  2411. objects! */
  2412. UNIV_INTERN
  2413. void
  2414. log_check_margins(void)
  2415. /*===================*/
  2416. {
  2417. loop:
  2418. log_flush_margin();
  2419. log_checkpoint_margin();
  2420. #ifdef UNIV_LOG_ARCHIVE
  2421. log_archive_margin();
  2422. #endif /* UNIV_LOG_ARCHIVE */
  2423. mutex_enter(&(log_sys->mutex));
  2424. ut_ad(!recv_no_log_write);
  2425. if (log_sys->check_flush_or_checkpoint) {
  2426. mutex_exit(&(log_sys->mutex));
  2427. goto loop;
  2428. }
  2429. mutex_exit(&(log_sys->mutex));
  2430. }
  2431. /****************************************************************//**
  2432. Makes a checkpoint at the latest lsn and writes it to first page of each
  2433. data file in the database, so that we know that the file spaces contain
  2434. all modifications up to that lsn. This can only be called at database
  2435. shutdown. This function also writes all log in log files to the log archive. */
  2436. UNIV_INTERN
  2437. void
  2438. logs_empty_and_mark_files_at_shutdown(void)
  2439. /*=======================================*/
  2440. {
  2441. lsn_t lsn;
  2442. ulint arch_log_no;
  2443. ulint count = 0;
  2444. ulint total_trx;
  2445. ulint pending_io;
  2446. enum srv_thread_type active_thd;
  2447. const char* thread_name;
  2448. ibool server_busy;
  2449. ib_logf(IB_LOG_LEVEL_INFO, "Starting shutdown...");
  2450. while (srv_fast_shutdown == 0 && trx_rollback_or_clean_is_active) {
  2451. /* we should wait until rollback after recovery end
  2452. for slow shutdown */
  2453. os_thread_sleep(100000);
  2454. }
  2455. /* Wait until the master thread and all other operations are idle: our
  2456. algorithm only works if the server is idle at shutdown */
  2457. srv_shutdown_state = SRV_SHUTDOWN_CLEANUP;
  2458. loop:
  2459. os_thread_sleep(100000);
  2460. count++;
  2461. /* We need the monitor threads to stop before we proceed with
  2462. a shutdown. */
  2463. thread_name = srv_any_background_threads_are_active();
  2464. if (thread_name != NULL) {
  2465. /* Print a message every 60 seconds if we are waiting
  2466. for the monitor thread to exit. Master and worker
  2467. threads check will be done later. */
  2468. if (srv_print_verbose_log && count > 600) {
  2469. ib_logf(IB_LOG_LEVEL_INFO,
  2470. "Waiting for %s to exit", thread_name);
  2471. count = 0;
  2472. }
  2473. goto loop;
  2474. }
  2475. /* Check that there are no longer transactions, except for
  2476. PREPARED ones. We need this wait even for the 'very fast'
  2477. shutdown, because the InnoDB layer may have committed or
  2478. prepared transactions and we don't want to lose them. */
  2479. total_trx = trx_sys_any_active_transactions();
  2480. if (total_trx > 0) {
  2481. if (srv_print_verbose_log && count > 600) {
  2482. ib_logf(IB_LOG_LEVEL_INFO,
  2483. "Waiting for %lu active transactions to finish",
  2484. (ulong) total_trx);
  2485. count = 0;
  2486. }
  2487. goto loop;
  2488. }
  2489. /* Check that the background threads are suspended */
  2490. active_thd = srv_get_active_thread_type();
  2491. if (active_thd != SRV_NONE) {
  2492. if (active_thd == SRV_PURGE) {
  2493. srv_purge_wakeup();
  2494. }
  2495. /* The srv_lock_timeout_thread, srv_error_monitor_thread
  2496. and srv_monitor_thread should already exit by now. The
  2497. only threads to be suspended are the master threads
  2498. and worker threads (purge threads). Print the thread
  2499. type if any of such threads not in suspended mode */
  2500. if (srv_print_verbose_log && count > 600) {
  2501. const char* thread_type = "<null>";
  2502. switch (active_thd) {
  2503. case SRV_NONE:
  2504. /* This shouldn't happen because we've
  2505. already checked for this case before
  2506. entering the if(). We handle it here
  2507. to avoid a compiler warning. */
  2508. ut_error;
  2509. case SRV_WORKER:
  2510. thread_type = "worker threads";
  2511. break;
  2512. case SRV_MASTER:
  2513. thread_type = "master thread";
  2514. break;
  2515. case SRV_PURGE:
  2516. thread_type = "purge thread";
  2517. break;
  2518. }
  2519. ib_logf(IB_LOG_LEVEL_INFO,
  2520. "Waiting for %s to be suspended",
  2521. thread_type);
  2522. count = 0;
  2523. }
  2524. goto loop;
  2525. }
  2526. /* At this point only page_cleaner should be active. We wait
  2527. here to let it complete the flushing of the buffer pools
  2528. before proceeding further. */
  2529. srv_shutdown_state = SRV_SHUTDOWN_FLUSH_PHASE;
  2530. count = 0;
  2531. while (buf_page_cleaner_is_active) {
  2532. ++count;
  2533. os_thread_sleep(100000);
  2534. if (srv_print_verbose_log && count > 600) {
  2535. ib_logf(IB_LOG_LEVEL_INFO,
  2536. "Waiting for page_cleaner to "
  2537. "finish flushing of buffer pool");
  2538. count = 0;
  2539. }
  2540. }
  2541. mutex_enter(&log_sys->mutex);
  2542. server_busy = log_sys->n_pending_checkpoint_writes
  2543. #ifdef UNIV_LOG_ARCHIVE
  2544. || log_sys->n_pending_archive_ios
  2545. #endif /* UNIV_LOG_ARCHIVE */
  2546. || log_sys->n_pending_writes;
  2547. mutex_exit(&log_sys->mutex);
  2548. if (server_busy) {
  2549. if (srv_print_verbose_log && count > 600) {
  2550. ib_logf(IB_LOG_LEVEL_INFO,
  2551. "Pending checkpoint_writes: %lu. "
  2552. "Pending log flush writes: %lu",
  2553. (ulong) log_sys->n_pending_checkpoint_writes,
  2554. (ulong) log_sys->n_pending_writes);
  2555. count = 0;
  2556. }
  2557. goto loop;
  2558. }
  2559. pending_io = buf_pool_check_no_pending_io();
  2560. if (pending_io) {
  2561. if (srv_print_verbose_log && count > 600) {
  2562. ib_logf(IB_LOG_LEVEL_INFO,
  2563. "Waiting for %lu buffer page I/Os to complete",
  2564. (ulong) pending_io);
  2565. count = 0;
  2566. }
  2567. goto loop;
  2568. }
  2569. #ifdef UNIV_LOG_ARCHIVE
  2570. log_archive_all();
  2571. #endif /* UNIV_LOG_ARCHIVE */
  2572. if (srv_fast_shutdown == 2) {
  2573. if (!srv_read_only_mode) {
  2574. ib_logf(IB_LOG_LEVEL_INFO,
  2575. "MySQL has requested a very fast shutdown "
  2576. "without flushing the InnoDB buffer pool to "
  2577. "data files. At the next mysqld startup "
  2578. "InnoDB will do a crash recovery!");
  2579. /* In this fastest shutdown we do not flush the
  2580. buffer pool:
  2581. it is essentially a 'crash' of the InnoDB server.
  2582. Make sure that the log is all flushed to disk, so
  2583. that we can recover all committed transactions in
  2584. a crash recovery. We must not write the lsn stamps
  2585. to the data files, since at a startup InnoDB deduces
  2586. from the stamps if the previous shutdown was clean. */
  2587. log_buffer_flush_to_disk();
  2588. /* Check that the background threads stay suspended */
  2589. thread_name = srv_any_background_threads_are_active();
  2590. if (thread_name != NULL) {
  2591. ib_logf(IB_LOG_LEVEL_WARN,
  2592. "Background thread %s woke up "
  2593. "during shutdown", thread_name);
  2594. goto loop;
  2595. }
  2596. }
  2597. srv_shutdown_state = SRV_SHUTDOWN_LAST_PHASE;
  2598. fil_close_all_files();
  2599. thread_name = srv_any_background_threads_are_active();
  2600. ut_a(!thread_name);
  2601. return;
  2602. }
  2603. if (!srv_read_only_mode) {
  2604. log_make_checkpoint_at(LSN_MAX, TRUE);
  2605. }
  2606. mutex_enter(&log_sys->mutex);
  2607. lsn = log_sys->lsn;
  2608. if (lsn != log_sys->last_checkpoint_lsn
  2609. #ifdef UNIV_LOG_ARCHIVE
  2610. || (srv_log_archive_on
  2611. && lsn != log_sys->archived_lsn + LOG_BLOCK_HDR_SIZE)
  2612. #endif /* UNIV_LOG_ARCHIVE */
  2613. ) {
  2614. mutex_exit(&log_sys->mutex);
  2615. goto loop;
  2616. }
  2617. arch_log_no = 0;
  2618. #ifdef UNIV_LOG_ARCHIVE
  2619. UT_LIST_GET_FIRST(log_sys->log_groups)->archived_file_no;
  2620. if (0 == UT_LIST_GET_FIRST(log_sys->log_groups)->archived_offset) {
  2621. arch_log_no--;
  2622. }
  2623. log_archive_close_groups(TRUE);
  2624. #endif /* UNIV_LOG_ARCHIVE */
  2625. mutex_exit(&log_sys->mutex);
  2626. /* Check that the background threads stay suspended */
  2627. thread_name = srv_any_background_threads_are_active();
  2628. if (thread_name != NULL) {
  2629. ib_logf(IB_LOG_LEVEL_WARN,
  2630. "Background thread %s woke up during shutdown",
  2631. thread_name);
  2632. goto loop;
  2633. }
  2634. if (!srv_read_only_mode) {
  2635. fil_flush_file_spaces(FIL_TABLESPACE);
  2636. fil_flush_file_spaces(FIL_LOG);
  2637. }
  2638. /* The call fil_write_flushed_lsn_to_data_files() will pass the buffer
  2639. pool: therefore it is essential that the buffer pool has been
  2640. completely flushed to disk! (We do not call fil_write... if the
  2641. 'very fast' shutdown is enabled.) */
  2642. if (!buf_all_freed()) {
  2643. if (srv_print_verbose_log && count > 600) {
  2644. ib_logf(IB_LOG_LEVEL_INFO,
  2645. "Waiting for dirty buffer pages to be flushed");
  2646. count = 0;
  2647. }
  2648. goto loop;
  2649. }
  2650. srv_shutdown_state = SRV_SHUTDOWN_LAST_PHASE;
  2651. /* Make some checks that the server really is quiet */
  2652. srv_thread_type type = srv_get_active_thread_type();
  2653. ut_a(type == SRV_NONE);
  2654. bool freed = buf_all_freed();
  2655. ut_a(freed);
  2656. ut_a(lsn == log_sys->lsn);
  2657. if (lsn < srv_start_lsn) {
  2658. ib_logf(IB_LOG_LEVEL_ERROR,
  2659. "Log sequence number at shutdown " LSN_PF " "
  2660. "is lower than at startup " LSN_PF "!",
  2661. lsn, srv_start_lsn);
  2662. }
  2663. srv_shutdown_lsn = lsn;
  2664. if (!srv_read_only_mode) {
  2665. fil_write_flushed_lsn_to_data_files(lsn, arch_log_no);
  2666. fil_flush_file_spaces(FIL_TABLESPACE);
  2667. }
  2668. fil_close_all_files();
  2669. /* Make some checks that the server really is quiet */
  2670. type = srv_get_active_thread_type();
  2671. ut_a(type == SRV_NONE);
  2672. freed = buf_all_freed();
  2673. ut_a(freed);
  2674. ut_a(lsn == log_sys->lsn);
  2675. }
  2676. #ifdef UNIV_LOG_DEBUG
  2677. /******************************************************//**
  2678. Checks by parsing that the catenated log segment for a single mtr is
  2679. consistent. */
  2680. UNIV_INTERN
  2681. ibool
  2682. log_check_log_recs(
  2683. /*===============*/
  2684. const byte* buf, /*!< in: pointer to the start of
  2685. the log segment in the
  2686. log_sys->buf log buffer */
  2687. ulint len, /*!< in: segment length in bytes */
  2688. ib_uint64_t buf_start_lsn) /*!< in: buffer start lsn */
  2689. {
  2690. ib_uint64_t contiguous_lsn;
  2691. ib_uint64_t scanned_lsn;
  2692. const byte* start;
  2693. const byte* end;
  2694. byte* buf1;
  2695. byte* scan_buf;
  2696. ut_ad(mutex_own(&(log_sys->mutex)));
  2697. if (len == 0) {
  2698. return(TRUE);
  2699. }
  2700. start = ut_align_down(buf, OS_FILE_LOG_BLOCK_SIZE);
  2701. end = ut_align(buf + len, OS_FILE_LOG_BLOCK_SIZE);
  2702. buf1 = mem_alloc((end - start) + OS_FILE_LOG_BLOCK_SIZE);
  2703. scan_buf = ut_align(buf1, OS_FILE_LOG_BLOCK_SIZE);
  2704. ut_memcpy(scan_buf, start, end - start);
  2705. recv_scan_log_recs((buf_pool_get_n_pages()
  2706. - (recv_n_pool_free_frames * srv_buf_pool_instances))
  2707. * UNIV_PAGE_SIZE, FALSE, scan_buf, end - start,
  2708. ut_uint64_align_down(buf_start_lsn,
  2709. OS_FILE_LOG_BLOCK_SIZE),
  2710. &contiguous_lsn, &scanned_lsn);
  2711. ut_a(scanned_lsn == buf_start_lsn + len);
  2712. ut_a(recv_sys->recovered_lsn == scanned_lsn);
  2713. mem_free(buf1);
  2714. return(TRUE);
  2715. }
  2716. #endif /* UNIV_LOG_DEBUG */
  2717. /******************************************************//**
  2718. Peeks the current lsn.
  2719. @return TRUE if success, FALSE if could not get the log system mutex */
  2720. UNIV_INTERN
  2721. ibool
  2722. log_peek_lsn(
  2723. /*=========*/
  2724. lsn_t* lsn) /*!< out: if returns TRUE, current lsn is here */
  2725. {
  2726. if (0 == mutex_enter_nowait(&(log_sys->mutex))) {
  2727. *lsn = log_sys->lsn;
  2728. mutex_exit(&(log_sys->mutex));
  2729. return(TRUE);
  2730. }
  2731. return(FALSE);
  2732. }
  2733. /******************************************************//**
  2734. Prints info of the log. */
  2735. UNIV_INTERN
  2736. void
  2737. log_print(
  2738. /*======*/
  2739. FILE* file) /*!< in: file where to print */
  2740. {
  2741. double time_elapsed;
  2742. time_t current_time;
  2743. mutex_enter(&(log_sys->mutex));
  2744. fprintf(file,
  2745. "Log sequence number " LSN_PF "\n"
  2746. "Log flushed up to " LSN_PF "\n"
  2747. "Pages flushed up to " LSN_PF "\n"
  2748. "Last checkpoint at " LSN_PF "\n",
  2749. log_sys->lsn,
  2750. log_sys->flushed_to_disk_lsn,
  2751. log_buf_pool_get_oldest_modification(),
  2752. log_sys->last_checkpoint_lsn);
  2753. current_time = time(NULL);
  2754. time_elapsed = difftime(current_time,
  2755. log_sys->last_printout_time);
  2756. if (time_elapsed <= 0) {
  2757. time_elapsed = 1;
  2758. }
  2759. fprintf(file,
  2760. "%lu pending log writes, %lu pending chkp writes\n"
  2761. "%lu log i/o's done, %.2f log i/o's/second\n",
  2762. (ulong) log_sys->n_pending_writes,
  2763. (ulong) log_sys->n_pending_checkpoint_writes,
  2764. (ulong) log_sys->n_log_ios,
  2765. ((double)(log_sys->n_log_ios - log_sys->n_log_ios_old)
  2766. / time_elapsed));
  2767. log_sys->n_log_ios_old = log_sys->n_log_ios;
  2768. log_sys->last_printout_time = current_time;
  2769. mutex_exit(&(log_sys->mutex));
  2770. }
  2771. /**********************************************************************//**
  2772. Refreshes the statistics used to print per-second averages. */
  2773. UNIV_INTERN
  2774. void
  2775. log_refresh_stats(void)
  2776. /*===================*/
  2777. {
  2778. log_sys->n_log_ios_old = log_sys->n_log_ios;
  2779. log_sys->last_printout_time = time(NULL);
  2780. }
  2781. /********************************************************//**
  2782. Closes a log group. */
  2783. static
  2784. void
  2785. log_group_close(
  2786. /*===========*/
  2787. log_group_t* group) /* in,own: log group to close */
  2788. {
  2789. ulint i;
  2790. for (i = 0; i < group->n_files; i++) {
  2791. mem_free(group->file_header_bufs_ptr[i]);
  2792. #ifdef UNIV_LOG_ARCHIVE
  2793. mem_free(group->archive_file_header_bufs_ptr[i]);
  2794. #endif /* UNIV_LOG_ARCHIVE */
  2795. }
  2796. mem_free(group->file_header_bufs_ptr);
  2797. mem_free(group->file_header_bufs);
  2798. #ifdef UNIV_LOG_ARCHIVE
  2799. mem_free(group->archive_file_header_bufs_ptr);
  2800. mem_free(group->archive_file_header_bufs);
  2801. #endif /* UNIV_LOG_ARCHIVE */
  2802. mem_free(group->checkpoint_buf_ptr);
  2803. mem_free(group);
  2804. }
  2805. /********************************************************//**
  2806. Closes all log groups. */
  2807. UNIV_INTERN
  2808. void
  2809. log_group_close_all(void)
  2810. /*=====================*/
  2811. {
  2812. log_group_t* group;
  2813. group = UT_LIST_GET_FIRST(log_sys->log_groups);
  2814. while (UT_LIST_GET_LEN(log_sys->log_groups) > 0) {
  2815. log_group_t* prev_group = group;
  2816. group = UT_LIST_GET_NEXT(log_groups, group);
  2817. UT_LIST_REMOVE(log_groups, log_sys->log_groups, prev_group);
  2818. log_group_close(prev_group);
  2819. }
  2820. }
  2821. /********************************************************//**
  2822. Shutdown the log system but do not release all the memory. */
  2823. UNIV_INTERN
  2824. void
  2825. log_shutdown(void)
  2826. /*==============*/
  2827. {
  2828. log_group_close_all();
  2829. mem_free(log_sys->buf_ptr);
  2830. log_sys->buf_ptr = NULL;
  2831. log_sys->buf = NULL;
  2832. mem_free(log_sys->checkpoint_buf_ptr);
  2833. log_sys->checkpoint_buf_ptr = NULL;
  2834. log_sys->checkpoint_buf = NULL;
  2835. os_event_free(log_sys->no_flush_event);
  2836. os_event_free(log_sys->one_flushed_event);
  2837. rw_lock_free(&log_sys->checkpoint_lock);
  2838. mutex_free(&log_sys->mutex);
  2839. #ifdef UNIV_LOG_ARCHIVE
  2840. rw_lock_free(&log_sys->archive_lock);
  2841. os_event_create();
  2842. #endif /* UNIV_LOG_ARCHIVE */
  2843. #ifdef UNIV_LOG_DEBUG
  2844. recv_sys_debug_free();
  2845. #endif
  2846. recv_sys_close();
  2847. }
  2848. /********************************************************//**
  2849. Free the log system data structures. */
  2850. UNIV_INTERN
  2851. void
  2852. log_mem_free(void)
  2853. /*==============*/
  2854. {
  2855. if (log_sys != NULL) {
  2856. recv_sys_mem_free();
  2857. mem_free(log_sys);
  2858. log_sys = NULL;
  2859. }
  2860. }
  2861. #endif /* !UNIV_HOTBACKUP */