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.

3949 lines
98 KiB

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