log0log.c
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:66k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /******************************************************
  2. Database log
  3. (c) 1995-1997 Innobase Oy
  4. Created 12/9/1995 Heikki Tuuri
  5. *******************************************************/
  6. #include "log0log.h"
  7. #ifdef UNIV_NONINL
  8. #include "log0log.ic"
  9. #endif
  10. #include "mem0mem.h"
  11. #include "buf0buf.h"
  12. #include "buf0flu.h"
  13. #include "srv0srv.h"
  14. #include "log0recv.h"
  15. #include "fil0fil.h"
  16. #include "dict0boot.h"
  17. #include "srv0srv.h"
  18. #include "trx0sys.h"
  19. #include "trx0trx.h"
  20. /* Global log system variable */
  21. log_t* log_sys = NULL;
  22. ibool log_do_write = TRUE;
  23. ibool log_debug_writes = FALSE;
  24. /* Pointer to this variable is used as the i/o-message when we do i/o to an
  25. archive */
  26. byte log_archive_io;
  27. /* A margin for free space in the log buffer before a log entry is catenated */
  28. #define LOG_BUF_WRITE_MARGIN  (4 * OS_FILE_LOG_BLOCK_SIZE)
  29. /* Margins for free space in the log buffer after a log entry is catenated */
  30. #define LOG_BUF_FLUSH_RATIO 2
  31. #define LOG_BUF_FLUSH_MARGIN (LOG_BUF_WRITE_MARGIN + 4 * UNIV_PAGE_SIZE)
  32. /* Margin for the free space in the smallest log group, before a new query
  33. step which modifies the database, is started */
  34. #define LOG_CHECKPOINT_FREE_PER_THREAD (4 * UNIV_PAGE_SIZE)
  35. #define LOG_CHECKPOINT_EXTRA_FREE (8 * UNIV_PAGE_SIZE)
  36. /* This parameter controls asynchronous making of a new checkpoint; the value
  37. should be bigger than LOG_POOL_PREFLUSH_RATIO_SYNC */
  38. #define LOG_POOL_CHECKPOINT_RATIO_ASYNC 32
  39. /* This parameter controls synchronous preflushing of modified buffer pages */
  40. #define LOG_POOL_PREFLUSH_RATIO_SYNC 16
  41. /* The same ratio for asynchronous preflushing; this value should be less than
  42. the previous */
  43. #define LOG_POOL_PREFLUSH_RATIO_ASYNC 8
  44. /* Extra margin, in addition to one log file, used in archiving */
  45. #define LOG_ARCHIVE_EXTRA_MARGIN (4 * UNIV_PAGE_SIZE)
  46. /* This parameter controls asynchronous writing to the archive */
  47. #define LOG_ARCHIVE_RATIO_ASYNC 16
  48. /* Codes used in unlocking flush latches */
  49. #define LOG_UNLOCK_NONE_FLUSHED_LOCK 1
  50. #define LOG_UNLOCK_FLUSH_LOCK 2
  51. /* States of an archiving operation */
  52. #define LOG_ARCHIVE_READ 1
  53. #define LOG_ARCHIVE_WRITE 2
  54. /**********************************************************
  55. Completes a checkpoint write i/o to a log file. */
  56. static
  57. void
  58. log_io_complete_checkpoint(
  59. /*=======================*/
  60. log_group_t* group); /* in: log group */
  61. /**********************************************************
  62. Completes an archiving i/o. */
  63. static
  64. void
  65. log_io_complete_archive(void);
  66. /*=========================*/
  67. /********************************************************************
  68. Tries to establish a big enough margin of free space in the log groups, such
  69. that a new log entry can be catenated without an immediate need for a
  70. archiving. */
  71. static
  72. void
  73. log_archive_margin(void);
  74. /*====================*/
  75. /********************************************************************
  76. Returns the oldest modified block lsn in the pool, or log_sys->lsn if none
  77. exists. */
  78. static
  79. dulint
  80. log_buf_pool_get_oldest_modification(void)
  81. /*======================================*/
  82. {
  83. dulint lsn;
  84. ut_ad(mutex_own(&(log_sys->mutex)));
  85. lsn = buf_pool_get_oldest_modification();
  86. if (ut_dulint_is_zero(lsn)) {
  87. lsn = log_sys->lsn;
  88. }
  89. return(lsn);
  90. }
  91. /****************************************************************
  92. Opens the log for log_write_low. The log must be closed with log_close and
  93. released with log_release. */
  94. dulint
  95. log_reserve_and_open(
  96. /*=================*/
  97. /* out: start lsn of the log record */
  98. ulint len) /* in: length of data to be catenated */
  99. {
  100. log_t* log = log_sys;
  101. ulint len_upper_limit;
  102. ulint archived_lsn_age;
  103. ulint count = 0;
  104. ulint dummy;
  105. loop:
  106. mutex_enter(&(log->mutex));
  107. /* Calculate an upper limit for the space the string may take in the
  108. log buffer */
  109. len_upper_limit = LOG_BUF_WRITE_MARGIN + (5 * len) / 4;
  110. if (log->buf_free + len_upper_limit > log->buf_size) {
  111. mutex_exit(&(log->mutex));
  112. /* Not enough free space, do a syncronous flush of the log
  113. buffer */
  114. log_flush_up_to(ut_dulint_max, LOG_WAIT_ALL_GROUPS);
  115. count++;
  116. ut_ad(count < 50);
  117. goto loop;
  118. }
  119. if (log->archiving_state != LOG_ARCH_OFF) {
  120. archived_lsn_age = ut_dulint_minus(log->lsn, log->archived_lsn);
  121. if (archived_lsn_age + len_upper_limit
  122. > log->max_archived_lsn_age) {
  123. /* Not enough free archived space in log groups: do a
  124. synchronous archive write batch: */
  125. mutex_exit(&(log->mutex));
  126. ut_ad(len_upper_limit <= log->max_archived_lsn_age);
  127. log_archive_do(TRUE, &dummy);
  128. count++;
  129. ut_ad(count < 50);
  130. goto loop;
  131. }
  132. }
  133. #ifdef UNIV_LOG_DEBUG
  134. log->old_buf_free = log->buf_free;
  135. log->old_lsn = log->lsn;
  136. #endif
  137. return(log->lsn);
  138. }
  139. /****************************************************************
  140. Writes to the log the string given. It is assumed that the caller holds the
  141. log mutex. */
  142. void
  143. log_write_low(
  144. /*==========*/
  145. byte* str, /* in: string */
  146. ulint str_len) /* in: string length */
  147. {
  148. log_t* log = log_sys;
  149. ulint len;
  150. ulint data_len;
  151. byte* log_block;
  152. ut_ad(mutex_own(&(log->mutex)));
  153. part_loop:
  154. /* Calculate a part length */
  155. data_len = (log->buf_free % OS_FILE_LOG_BLOCK_SIZE) + str_len;
  156. if (data_len <= OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_TRL_SIZE) {
  157.      /* The string fits within the current log block */
  158.     
  159.      len = str_len;
  160. } else {
  161. data_len = OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_TRL_SIZE;
  162.      len = OS_FILE_LOG_BLOCK_SIZE
  163. - (log->buf_free % OS_FILE_LOG_BLOCK_SIZE)
  164.      - LOG_BLOCK_TRL_SIZE;
  165. }
  166. ut_memcpy(log->buf + log->buf_free, str, len);
  167. str_len -= len;
  168. str = str + len;
  169. log_block = ut_align_down(log->buf + log->buf_free,
  170. OS_FILE_LOG_BLOCK_SIZE);
  171. log_block_set_data_len(log_block, data_len);
  172. if (data_len == OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_TRL_SIZE) {
  173. /* This block became full */
  174. log_block_set_data_len(log_block, OS_FILE_LOG_BLOCK_SIZE);
  175. log_block_set_checkpoint_no(log_block,
  176. log_sys->next_checkpoint_no);
  177. len += LOG_BLOCK_HDR_SIZE + LOG_BLOCK_TRL_SIZE;
  178. log->lsn = ut_dulint_add(log->lsn, len);
  179. /* Initialize the next block header and trailer */
  180. log_block_init(log_block + OS_FILE_LOG_BLOCK_SIZE, log->lsn);
  181. } else {
  182. log->lsn = ut_dulint_add(log->lsn, len);
  183. }
  184. log->buf_free += len;
  185. ut_ad(log->buf_free <= log->buf_size);
  186. if (str_len > 0) {
  187. goto part_loop;
  188. }
  189. }
  190. /****************************************************************
  191. Closes the log. */
  192. dulint
  193. log_close(void)
  194. /*===========*/
  195. /* out: lsn */
  196. {
  197. byte* log_block;
  198. ulint first_rec_group;
  199. dulint oldest_lsn;
  200. dulint lsn;
  201. log_t* log = log_sys;
  202. ut_ad(mutex_own(&(log->mutex)));
  203. lsn = log->lsn;
  204. log_block = ut_align_down(log->buf + log->buf_free,
  205. OS_FILE_LOG_BLOCK_SIZE);
  206. first_rec_group = log_block_get_first_rec_group(log_block);
  207. if (first_rec_group == 0) {
  208. /* We initialized a new log block which was not written
  209. full by the current mtr: the next mtr log record group
  210. will start within this block at the offset data_len */
  211. log_block_set_first_rec_group(log_block,
  212.   log_block_get_data_len(log_block));
  213. }
  214. if (log->buf_free > log->max_buf_free) {
  215. log->check_flush_or_checkpoint = TRUE;
  216. }
  217. if (ut_dulint_minus(lsn, log->last_checkpoint_lsn)
  218. <= log->max_modified_age_async) {
  219. goto function_exit;
  220. }
  221. oldest_lsn = buf_pool_get_oldest_modification();
  222. if (ut_dulint_is_zero(oldest_lsn)
  223.     || (ut_dulint_minus(lsn, oldest_lsn)
  224. > log->max_modified_age_async)
  225.     || (ut_dulint_minus(lsn, log->last_checkpoint_lsn)
  226. > log->max_checkpoint_age_async)) {
  227. log->check_flush_or_checkpoint = TRUE;
  228. }
  229. function_exit:
  230. #ifdef UNIV_LOG_DEBUG
  231. log_check_log_recs(log->buf + log->old_buf_free,
  232. log->buf_free - log->old_buf_free, log->old_lsn);
  233. #endif
  234. return(lsn);
  235. }
  236. /**********************************************************
  237. Pads the current log block full with dummy log records. Used in producing
  238. consistent archived log files. */
  239. static
  240. void
  241. log_pad_current_log_block(void)
  242. /*===========================*/
  243. {
  244. byte b = MLOG_DUMMY_RECORD;
  245. ulint pad_length;
  246. ulint i;
  247. dulint lsn;
  248. log_reserve_and_open(OS_FILE_LOG_BLOCK_SIZE);
  249. pad_length = OS_FILE_LOG_BLOCK_SIZE
  250. - (log_sys->buf_free % OS_FILE_LOG_BLOCK_SIZE)
  251. - LOG_BLOCK_TRL_SIZE;
  252. for (i = 0; i < pad_length; i++) {
  253. log_write_low(&b, 1);
  254. }
  255. lsn = log_sys->lsn;
  256. log_close();
  257. log_release();
  258. ut_a((ut_dulint_get_low(lsn) % OS_FILE_LOG_BLOCK_SIZE)
  259. == LOG_BLOCK_HDR_SIZE);
  260. }
  261. /**********************************************************
  262. Calculates the data capacity of a log group, when the log file headers are not
  263. included. */
  264. ulint
  265. log_group_get_capacity(
  266. /*===================*/
  267. /* out: capacity in bytes */
  268. log_group_t* group) /* in: log group */
  269. {
  270. ut_ad(mutex_own(&(log_sys->mutex)));
  271. return((group->file_size - LOG_FILE_HDR_SIZE) * group->n_files); 
  272. }
  273. /**********************************************************
  274. Calculates the offset within a log group, when the log file headers are not
  275. included. */
  276. UNIV_INLINE
  277. ulint
  278. log_group_calc_size_offset(
  279. /*=======================*/
  280. /* out: size offset (<= offset) */
  281. ulint offset, /* in: real offset within the log group */
  282. log_group_t* group) /* in: log group */
  283. {
  284. ut_ad(mutex_own(&(log_sys->mutex)));
  285. return(offset - LOG_FILE_HDR_SIZE * (1 + offset / group->file_size));
  286. }
  287. /**********************************************************
  288. Calculates the offset within a log group, when the log file headers are
  289. included. */
  290. UNIV_INLINE
  291. ulint
  292. log_group_calc_real_offset(
  293. /*=======================*/
  294. /* out: real offset (>= offset) */
  295. ulint offset, /* in: size offset within the log group */
  296. log_group_t* group) /* in: log group */
  297. {
  298. ut_ad(mutex_own(&(log_sys->mutex)));
  299. return(offset + LOG_FILE_HDR_SIZE
  300. * (1 + offset / (group->file_size - LOG_FILE_HDR_SIZE)));
  301. }
  302. /**********************************************************
  303. Calculates the offset of an lsn within a log group. */
  304. static
  305. ulint
  306. log_group_calc_lsn_offset(
  307. /*======================*/
  308. /* out: offset within the log group */
  309. dulint lsn, /* in: lsn, must be within 4 GB of group->lsn */
  310. log_group_t* group) /* in: log group */
  311. {
  312. dulint gr_lsn;
  313. ulint gr_lsn_size_offset;
  314. ulint difference;
  315. ulint group_size;
  316. ulint offset;
  317. ut_ad(mutex_own(&(log_sys->mutex)));
  318. gr_lsn = group->lsn;
  319. gr_lsn_size_offset = log_group_calc_size_offset(group->lsn_offset,
  320. group);
  321. group_size = log_group_get_capacity(group);
  322. if (ut_dulint_cmp(lsn, gr_lsn) >= 0) {
  323. difference = ut_dulint_minus(lsn, gr_lsn);
  324. } else {
  325. difference = ut_dulint_minus(gr_lsn, lsn);
  326. difference = difference % group_size;
  327. difference = group_size - difference;
  328. }
  329. offset = (gr_lsn_size_offset + difference) % group_size;
  330. return(log_group_calc_real_offset(offset, group));
  331. }
  332. /************************************************************
  333. Sets the field values in group to correspond to a given lsn. For this function
  334. to work, the values must already be correctly initialized to correspond to
  335. some lsn, for instance, a checkpoint lsn. */
  336. void
  337. log_group_set_fields(
  338. /*=================*/
  339. log_group_t* group, /* in: group */
  340. dulint lsn) /* in: lsn for which the values should be
  341. set */
  342. {
  343. group->lsn_offset = log_group_calc_lsn_offset(lsn, group);
  344. group->lsn = lsn;
  345. }
  346. /*********************************************************************
  347. Calculates the recommended highest values for lsn - last_checkpoint_lsn,
  348. lsn - buf_get_oldest_modification(), and lsn - max_archive_lsn_age. */
  349. static
  350. ibool
  351. log_calc_max_ages(void)
  352. /*===================*/
  353. /* out: error value FALSE if the smallest log group is
  354. too small to accommodate the number of OS threads in
  355. the database server */
  356. {
  357. log_group_t* group;
  358. ulint n_threads;
  359. ulint margin;
  360. ulint free;
  361. ibool success = TRUE;
  362. ulint smallest_capacity;
  363. ulint archive_margin;
  364. ulint smallest_archive_margin;
  365. ut_ad(!mutex_own(&(log_sys->mutex)));
  366. n_threads = srv_get_n_threads();
  367. mutex_enter(&(log_sys->mutex));
  368. group = UT_LIST_GET_FIRST(log_sys->log_groups);
  369. ut_ad(group);
  370. smallest_capacity = ULINT_MAX;
  371. smallest_archive_margin = ULINT_MAX;
  372. while (group) {
  373. if (log_group_get_capacity(group) < smallest_capacity) {
  374. smallest_capacity = log_group_get_capacity(group);
  375. }
  376. archive_margin = log_group_get_capacity(group)
  377. - (group->file_size - LOG_FILE_HDR_SIZE)
  378. - LOG_ARCHIVE_EXTRA_MARGIN;
  379. if (archive_margin < smallest_archive_margin) {
  380. smallest_archive_margin = archive_margin;
  381. }
  382. group = UT_LIST_GET_NEXT(log_groups, group);
  383. }
  384. /* For each OS thread we must reserve so much free space in the
  385. smallest log group that it can accommodate the log entries produced
  386. by single query steps: running out of free log space is a serious
  387. system error which requires rebooting the database. */
  388. free = LOG_CHECKPOINT_FREE_PER_THREAD * n_threads
  389. + LOG_CHECKPOINT_EXTRA_FREE;
  390. if (free >= smallest_capacity / 2) {
  391. success = FALSE;
  392. goto failure;
  393. } else {
  394. margin = smallest_capacity - free;
  395. }
  396. margin = ut_min(margin, log_sys->adm_checkpoint_interval);
  397. log_sys->max_modified_age_async = margin
  398. - margin / LOG_POOL_PREFLUSH_RATIO_ASYNC;
  399. log_sys->max_modified_age_sync = margin
  400. - margin / LOG_POOL_PREFLUSH_RATIO_SYNC;
  401. log_sys->max_checkpoint_age_async = margin - margin
  402. / LOG_POOL_CHECKPOINT_RATIO_ASYNC;
  403. log_sys->max_checkpoint_age = margin;
  404. log_sys->max_archived_lsn_age = smallest_archive_margin;
  405. log_sys->max_archived_lsn_age_async = smallest_archive_margin
  406. - smallest_archive_margin /
  407.   LOG_ARCHIVE_RATIO_ASYNC;
  408. failure:
  409. mutex_exit(&(log_sys->mutex));
  410. if (!success) {
  411. fprintf(stderr,
  412.   "Error: log file group too small for the number of threadsn");
  413. }
  414. return(success);
  415. }
  416. /**********************************************************
  417. Initializes the log. */
  418. void
  419. log_init(void)
  420. /*==========*/
  421. {
  422. byte* buf;
  423. log_sys = mem_alloc(sizeof(log_t));
  424. mutex_create(&(log_sys->mutex));
  425. mutex_set_level(&(log_sys->mutex), SYNC_LOG);
  426. mutex_enter(&(log_sys->mutex));
  427. /* Start the lsn from one log block from zero: this way every
  428. log record has a start lsn != zero, a fact which we will use */
  429. log_sys->lsn = LOG_START_LSN;
  430. ut_a(LOG_BUFFER_SIZE >= 16 * OS_FILE_LOG_BLOCK_SIZE);
  431. ut_a(LOG_BUFFER_SIZE >= 4 * UNIV_PAGE_SIZE);
  432. buf = ut_malloc(LOG_BUFFER_SIZE + OS_FILE_LOG_BLOCK_SIZE);
  433. log_sys->buf = ut_align(buf, OS_FILE_LOG_BLOCK_SIZE);
  434. log_sys->buf_size = LOG_BUFFER_SIZE;
  435. log_sys->max_buf_free = log_sys->buf_size / LOG_BUF_FLUSH_RATIO
  436. - LOG_BUF_FLUSH_MARGIN;
  437. log_sys->check_flush_or_checkpoint = TRUE;
  438. UT_LIST_INIT(log_sys->log_groups);
  439. log_sys->n_log_ios = 0;
  440. /*----------------------------*/
  441. log_sys->buf_next_to_write = 0;
  442. log_sys->written_to_some_lsn = log_sys->lsn;
  443. log_sys->written_to_all_lsn = log_sys->lsn;
  444. log_sys->n_pending_writes = 0;
  445. log_sys->no_flush_event = os_event_create(NULL);
  446. os_event_set(log_sys->no_flush_event);
  447. log_sys->one_flushed_event = os_event_create(NULL);
  448. os_event_set(log_sys->one_flushed_event);
  449. /*----------------------------*/
  450. log_sys->adm_checkpoint_interval = ULINT_MAX;
  451. log_sys->next_checkpoint_no = ut_dulint_zero;
  452. log_sys->last_checkpoint_lsn = log_sys->lsn;
  453. log_sys->n_pending_checkpoint_writes = 0; 
  454. rw_lock_create(&(log_sys->checkpoint_lock));
  455. rw_lock_set_level(&(log_sys->checkpoint_lock), SYNC_NO_ORDER_CHECK);
  456. log_sys->checkpoint_buf = ut_align(
  457. mem_alloc(2 * OS_FILE_LOG_BLOCK_SIZE),
  458. OS_FILE_LOG_BLOCK_SIZE);
  459. /*----------------------------*/
  460. log_sys->archiving_state = LOG_ARCH_ON;
  461. log_sys->archived_lsn = log_sys->lsn;
  462. log_sys->n_pending_archive_ios = 0;
  463. rw_lock_create(&(log_sys->archive_lock));
  464. rw_lock_set_level(&(log_sys->archive_lock), SYNC_NO_ORDER_CHECK);
  465. log_sys->archive_buf = ut_align(
  466. ut_malloc(LOG_ARCHIVE_BUF_SIZE
  467.   + OS_FILE_LOG_BLOCK_SIZE),
  468. OS_FILE_LOG_BLOCK_SIZE);
  469. log_sys->archive_buf_size = LOG_ARCHIVE_BUF_SIZE;
  470. log_sys->archiving_on = os_event_create(NULL);
  471. /*----------------------------*/
  472. log_sys->online_backup_state = FALSE;
  473. /*----------------------------*/
  474. log_block_init(log_sys->buf, log_sys->lsn);
  475. log_block_set_first_rec_group(log_sys->buf, LOG_BLOCK_HDR_SIZE);
  476. log_sys->buf_free = LOG_BLOCK_HDR_SIZE;
  477. log_sys->lsn = ut_dulint_add(LOG_START_LSN, LOG_BLOCK_HDR_SIZE);
  478. mutex_exit(&(log_sys->mutex));
  479. #ifdef UNIV_LOG_DEBUG
  480. recv_sys_create();
  481. recv_sys_init();
  482. recv_sys->parse_start_lsn = log_sys->lsn;
  483. recv_sys->scanned_lsn = log_sys->lsn;
  484. recv_sys->scanned_checkpoint_no = 0;
  485. recv_sys->recovered_lsn = log_sys->lsn;
  486. recv_sys->limit_lsn = ut_dulint_max;
  487. #endif
  488. }
  489. /**********************************************************************
  490. Inits a log group to the log system. */
  491. void
  492. log_group_init(
  493. /*===========*/
  494. ulint id, /* in: group id */
  495. ulint n_files, /* in: number of log files */
  496. ulint file_size, /* in: log file size in bytes */
  497. ulint space_id, /* in: space id of the file space
  498. which contains the log files of this
  499. group */
  500. ulint archive_space_id) /* in: space id of the file space
  501. which contains some archived log
  502. files for this group; currently, only
  503. for the first log group this is
  504. used */
  505. {
  506. ulint i;
  507. log_group_t* group;
  508. group = mem_alloc(sizeof(log_group_t));
  509. group->id = id;
  510. group->n_files = n_files;
  511. group->file_size = file_size;
  512. group->space_id = space_id;
  513. group->state = LOG_GROUP_OK;
  514. group->lsn = LOG_START_LSN;
  515. group->lsn_offset = LOG_FILE_HDR_SIZE;
  516. group->n_pending_writes = 0;
  517. group->file_header_bufs = mem_alloc(sizeof(byte*) * n_files);
  518. group->archive_file_header_bufs = mem_alloc(sizeof(byte*) * n_files);
  519. for (i = 0; i < n_files; i++) {
  520. *(group->file_header_bufs + i) = ut_align(
  521. mem_alloc(LOG_FILE_HDR_SIZE + OS_FILE_LOG_BLOCK_SIZE),
  522. OS_FILE_LOG_BLOCK_SIZE);
  523. *(group->archive_file_header_bufs + i) = ut_align(
  524. mem_alloc(LOG_FILE_HDR_SIZE + OS_FILE_LOG_BLOCK_SIZE),
  525. OS_FILE_LOG_BLOCK_SIZE);
  526. }
  527. group->archive_space_id = archive_space_id;
  528. group->archived_file_no = 0;
  529. group->archived_offset = 0;
  530. group->checkpoint_buf = ut_align(
  531. mem_alloc(2 * OS_FILE_LOG_BLOCK_SIZE),
  532. OS_FILE_LOG_BLOCK_SIZE);
  533. UT_LIST_ADD_LAST(log_groups, log_sys->log_groups, group);
  534. ut_a(log_calc_max_ages());
  535. }
  536. /**********************************************************************
  537. Does the unlockings needed in flush i/o completion. */
  538. UNIV_INLINE
  539. void
  540. log_flush_do_unlocks(
  541. /*=================*/
  542. ulint code) /* in: any ORed combination of LOG_UNLOCK_FLUSH_LOCK
  543. and LOG_UNLOCK_NONE_FLUSHED_LOCK */
  544. {
  545. ut_ad(mutex_own(&(log_sys->mutex)));
  546. /* NOTE that we must own the log mutex when doing the setting of the
  547. events: this is because transactions will wait for these events to
  548. be set, and at that moment the log flush they were waiting for must
  549. have ended. If the log mutex were not reserved here, the i/o-thread
  550. calling this function might be preempted for a while, and when it
  551. resumed execution, it might be that a new flush had been started, and
  552. this function would erroneously signal the NEW flush as completed.
  553. Thus, the changes in the state of these events are performed
  554. atomically in conjunction with the changes in the state of
  555. log_sys->n_pending_writes etc. */ 
  556. if (code & LOG_UNLOCK_NONE_FLUSHED_LOCK) {
  557. os_event_set(log_sys->one_flushed_event);
  558. }
  559. if (code & LOG_UNLOCK_FLUSH_LOCK) {
  560. os_event_set(log_sys->no_flush_event);
  561. }
  562. }
  563. /**********************************************************************
  564. Checks if a flush is completed for a log group and does the completion
  565. routine if yes. */
  566. UNIV_INLINE
  567. ulint
  568. log_group_check_flush_completion(
  569. /*=============================*/
  570. /* out: LOG_UNLOCK_NONE_FLUSHED_LOCK or 0 */
  571. log_group_t* group) /* in: log group */
  572. {
  573. ut_ad(mutex_own(&(log_sys->mutex)));
  574. if (!log_sys->one_flushed && (group->n_pending_writes == 0)) {
  575. if (log_debug_writes) {
  576. printf("Log flushed first to group %lun", group->id);
  577. }
  578. log_sys->written_to_some_lsn = log_sys->flush_lsn;
  579. log_sys->one_flushed = TRUE;
  580. return(LOG_UNLOCK_NONE_FLUSHED_LOCK);
  581. }
  582. if (log_debug_writes && (group->n_pending_writes == 0)) {
  583. printf("Log flushed to group %lun", group->id);
  584. }
  585. return(0);
  586. }
  587. /**********************************************************
  588. Checks if a flush is completed and does the completion routine if yes. */
  589. static
  590. ulint
  591. log_sys_check_flush_completion(void)
  592. /*================================*/
  593. /* out: LOG_UNLOCK_FLUSH_LOCK or 0 */
  594. {
  595. ulint move_start;
  596. ulint move_end;
  597. ut_ad(mutex_own(&(log_sys->mutex)));
  598. if (log_sys->n_pending_writes == 0) {
  599. log_sys->written_to_all_lsn = log_sys->flush_lsn;
  600. log_sys->buf_next_to_write = log_sys->flush_end_offset;
  601. if (log_sys->flush_end_offset > log_sys->max_buf_free / 2) {
  602. /* Move the log buffer content to the start of the
  603. buffer */
  604. move_start = ut_calc_align_down(
  605. log_sys->flush_end_offset,
  606. OS_FILE_LOG_BLOCK_SIZE);
  607. move_end = ut_calc_align(log_sys->buf_free,
  608. OS_FILE_LOG_BLOCK_SIZE);
  609. ut_memmove(log_sys->buf, log_sys->buf + move_start,
  610. move_end - move_start);
  611. log_sys->buf_free -= move_start;
  612. log_sys->buf_next_to_write -= move_start;
  613. }
  614. return(LOG_UNLOCK_FLUSH_LOCK);
  615. }
  616. return(0);
  617. }
  618. /**********************************************************
  619. Completes an i/o to a log file. */
  620. void
  621. log_io_complete(
  622. /*============*/
  623. log_group_t* group) /* in: log group or a dummy pointer */
  624. {
  625. ulint unlock;
  626. if ((byte*)group == &log_archive_io) {
  627. /* It was an archive write */
  628. log_io_complete_archive();
  629. return;
  630. }
  631. if ((ulint)group & 0x1) {
  632. /* It was a checkpoint write */
  633. group = (log_group_t*)((ulint)group - 1);
  634. fil_flush(group->space_id);
  635. log_io_complete_checkpoint(group);
  636. return;
  637. }
  638. fil_flush(group->space_id);
  639. mutex_enter(&(log_sys->mutex));
  640. ut_ad(group->n_pending_writes > 0);
  641. ut_ad(log_sys->n_pending_writes > 0);
  642. group->n_pending_writes--;
  643. log_sys->n_pending_writes--;
  644. unlock = log_group_check_flush_completion(group);
  645. unlock = unlock | log_sys_check_flush_completion();
  646. log_flush_do_unlocks(unlock);
  647. mutex_exit(&(log_sys->mutex));
  648. }
  649. /**********************************************************
  650. Writes a log file header to a log file space. */
  651. static
  652. void
  653. log_group_file_header_flush(
  654. /*========================*/
  655. ulint type, /* in: LOG_FLUSH or LOG_RECOVER */
  656. log_group_t* group, /* in: log group */
  657. ulint nth_file, /* in: header to the nth file in the
  658. log file space */
  659. dulint start_lsn) /* in: log file data starts at this
  660. lsn */
  661. {
  662. byte* buf;
  663. ulint dest_offset;
  664. ibool sync;
  665. ut_ad(mutex_own(&(log_sys->mutex)));
  666. ut_a(nth_file < group->n_files);
  667. buf = *(group->file_header_bufs + nth_file);
  668. mach_write_to_4(buf + LOG_GROUP_ID, group->id);
  669. mach_write_to_8(buf + LOG_FILE_START_LSN, start_lsn);
  670. dest_offset = nth_file * group->file_size;
  671. sync = FALSE;
  672. if (type == LOG_RECOVER) {
  673. sync = TRUE;
  674. }
  675. if (log_debug_writes) {
  676. printf(
  677. "Writing log file header to group %lu file %lun", group->id,
  678. nth_file);
  679. }
  680. if (log_do_write) {
  681. if (type == LOG_FLUSH) {
  682. log_sys->n_pending_writes++;
  683. group->n_pending_writes++;
  684. }
  685. log_sys->n_log_ios++;
  686. fil_io(OS_FILE_WRITE | OS_FILE_LOG, sync, group->space_id,
  687. dest_offset / UNIV_PAGE_SIZE,
  688. dest_offset % UNIV_PAGE_SIZE,
  689. OS_FILE_LOG_BLOCK_SIZE,
  690. buf, group);
  691. }
  692. }
  693. /**********************************************************
  694. Writes a buffer to a log file group. */
  695. void
  696. log_group_write_buf(
  697. /*================*/
  698. ulint type, /* in: LOG_FLUSH or LOG_RECOVER */
  699. log_group_t* group, /* in: log group */
  700. byte* buf, /* in: buffer */
  701. ulint len, /* in: buffer len; must be divisible
  702. by OS_FILE_LOG_BLOCK_SIZE */
  703. dulint start_lsn, /* in: start lsn of the buffer; must
  704. be divisible by
  705. OS_FILE_LOG_BLOCK_SIZE */
  706. ulint new_data_offset)/* in: start offset of new data in
  707. buf: this parameter is used to decide
  708. if we have to write a new log file
  709. header */
  710. {
  711. ulint write_len;
  712. ibool sync;
  713. ibool write_header;
  714. ulint next_offset;
  715. ut_ad(mutex_own(&(log_sys->mutex)));
  716. ut_ad(len % OS_FILE_LOG_BLOCK_SIZE == 0);
  717. ut_ad(ut_dulint_get_low(start_lsn) % OS_FILE_LOG_BLOCK_SIZE == 0);
  718. sync = FALSE;
  719. if (type == LOG_RECOVER) {
  720. sync = TRUE;
  721. }
  722. if (new_data_offset == 0) {
  723. write_header = TRUE;
  724. } else {
  725. write_header = FALSE;
  726. }
  727. loop:
  728. if (len == 0) {
  729. return;
  730. }
  731. next_offset = log_group_calc_lsn_offset(start_lsn, group);
  732. if ((next_offset % group->file_size == LOG_FILE_HDR_SIZE)
  733.     && write_header) {
  734. /* We start to write a new log file instance in the group */
  735. log_group_file_header_flush(type, group,
  736. next_offset / group->file_size, start_lsn);
  737. }
  738. if ((next_offset % group->file_size) + len > group->file_size) {
  739. write_len = group->file_size - (next_offset % group->file_size);
  740. } else {
  741. write_len = len;
  742. }
  743. if (log_debug_writes) {
  744. printf(
  745. "Writing log file segment to group %lu offset %lu len %lun",
  746. group->id, next_offset, write_len);
  747. }
  748. if (log_do_write) {
  749. if (type == LOG_FLUSH) {
  750. log_sys->n_pending_writes++;
  751. group->n_pending_writes++;
  752. }
  753. log_sys->n_log_ios++;
  754. fil_io(OS_FILE_WRITE | OS_FILE_LOG, sync, group->space_id,
  755. next_offset / UNIV_PAGE_SIZE,
  756. next_offset % UNIV_PAGE_SIZE, write_len, buf, group);
  757. }
  758. if (write_len < len) {
  759. start_lsn = ut_dulint_add(start_lsn, write_len);
  760. len -= write_len;
  761. buf += write_len;
  762. write_header = TRUE;
  763. goto loop;
  764. }
  765. }
  766. /**********************************************************
  767. This function is called, e.g., when a transaction wants to commit. It checks
  768. that the log has been flushed to disk up to the last log entry written by the
  769. transaction. If there is a flush running, it waits and checks if the flush
  770. flushed enough. If not, starts a new flush. */
  771. void
  772. log_flush_up_to(
  773. /*============*/
  774. dulint lsn, /* in: log sequence number up to which the log should
  775. be flushed, ut_dulint_max if not specified */
  776. ulint wait) /* in: LOG_NO_WAIT, LOG_WAIT_ONE_GROUP,
  777. or LOG_WAIT_ALL_GROUPS */
  778. {
  779. log_group_t* group;
  780. ulint start_offset;
  781. ulint end_offset;
  782. ulint area_start;
  783. ulint area_end;
  784. ulint loop_count;
  785. if (recv_no_ibuf_operations) {
  786. /* Recovery is running and no operations on the log files are
  787. allowed yet (the variable name .._no_ibuf_.. is misleading) */
  788. return;
  789. }
  790. loop_count = 0;
  791. loop:
  792. loop_count++;
  793. ut_ad(loop_count < 5);
  794. if (loop_count > 2) {
  795. /* printf("Log loop count %lun", loop_count); */
  796. }
  797. mutex_enter(&(log_sys->mutex));
  798. if ((ut_dulint_cmp(log_sys->written_to_all_lsn, lsn) >= 0)
  799.      || ((ut_dulint_cmp(log_sys->written_to_some_lsn, lsn) >= 0)
  800.          && (wait != LOG_WAIT_ALL_GROUPS))) {
  801. mutex_exit(&(log_sys->mutex));
  802. return;
  803. }
  804. if (log_sys->n_pending_writes > 0) {
  805. /* A flush is running */
  806. if (ut_dulint_cmp(log_sys->flush_lsn, lsn) >= 0) {
  807. /* The flush will flush enough: wait for it to
  808. complete  */
  809. goto do_waits;
  810. }
  811. mutex_exit(&(log_sys->mutex));
  812. /* Wait for the flush to complete and try to start a new
  813. flush */
  814. os_event_wait(log_sys->no_flush_event);
  815. goto loop;
  816. }
  817. if (log_sys->buf_free == log_sys->buf_next_to_write) {
  818. /* Nothing to flush */
  819. mutex_exit(&(log_sys->mutex));
  820. return;
  821. }
  822. if (log_debug_writes) {
  823. printf("Flushing log from %lu %lu up to lsn %lu %lun",
  824. ut_dulint_get_high(log_sys->written_to_all_lsn),
  825. ut_dulint_get_low(log_sys->written_to_all_lsn),
  826. ut_dulint_get_high(log_sys->lsn),
  827. ut_dulint_get_low(log_sys->lsn));
  828. }
  829. os_event_reset(log_sys->no_flush_event);
  830. os_event_reset(log_sys->one_flushed_event);
  831. start_offset = log_sys->buf_next_to_write;
  832. end_offset = log_sys->buf_free;
  833. area_start = ut_calc_align_down(start_offset, OS_FILE_LOG_BLOCK_SIZE);
  834. area_end = ut_calc_align(end_offset, OS_FILE_LOG_BLOCK_SIZE);
  835. ut_ad(area_end - area_start > 0);
  836. log_sys->flush_lsn = log_sys->lsn;
  837. log_sys->one_flushed = FALSE;
  838. log_block_set_flush_bit(log_sys->buf + area_start, TRUE);
  839. log_block_set_checkpoint_no(
  840. log_sys->buf + area_end - OS_FILE_LOG_BLOCK_SIZE,
  841. log_sys->next_checkpoint_no);
  842. /* Copy the last, incompletely written, log block a log block length
  843. up, so that when the flush operation writes from the log buffer, the
  844. segment to write will not be changed by writers to the log */
  845. ut_memcpy(log_sys->buf + area_end,
  846. log_sys->buf + area_end - OS_FILE_LOG_BLOCK_SIZE,
  847. OS_FILE_LOG_BLOCK_SIZE);
  848. log_sys->buf_free += OS_FILE_LOG_BLOCK_SIZE;
  849. log_sys->flush_end_offset = log_sys->buf_free;
  850. group = UT_LIST_GET_FIRST(log_sys->log_groups);
  851. while (group) {
  852. log_group_write_buf(LOG_FLUSH, group,
  853. log_sys->buf + area_start,
  854. area_end - area_start,
  855. ut_dulint_align_down(log_sys->written_to_all_lsn,
  856. OS_FILE_LOG_BLOCK_SIZE),
  857. start_offset - area_start);
  858. log_group_set_fields(group, log_sys->flush_lsn);
  859. group = UT_LIST_GET_NEXT(log_groups, group);
  860. }
  861. do_waits:
  862. mutex_exit(&(log_sys->mutex));
  863. if (wait == LOG_WAIT_ONE_GROUP) {
  864. os_event_wait(log_sys->one_flushed_event);
  865. } else if (wait == LOG_WAIT_ALL_GROUPS) {
  866. os_event_wait(log_sys->no_flush_event);
  867. } else {
  868. ut_ad(wait == LOG_NO_WAIT);
  869. }
  870. }
  871. /********************************************************************
  872. Tries to establish a big enough margin of free space in the log buffer, such
  873. that a new log entry can be catenated without an immediate need for a flush. */
  874. static
  875. void
  876. log_flush_margin(void)
  877. /*==================*/
  878. {
  879. ibool do_flush = FALSE;
  880. log_t* log = log_sys;
  881. mutex_enter(&(log->mutex));
  882. if (log->buf_free > log->max_buf_free) {
  883. if (log->n_pending_writes > 0) {
  884. /* A flush is running: hope that it will provide enough
  885. free space */
  886. } else {
  887. do_flush = TRUE;
  888. }
  889. }
  890. mutex_exit(&(log->mutex));
  891. if (do_flush) {
  892. log_flush_up_to(ut_dulint_max, LOG_NO_WAIT);
  893. }
  894. }
  895. /********************************************************************
  896. Advances the smallest lsn for which there are unflushed dirty blocks in the
  897. buffer pool. NOTE: this function may only be called if the calling thread owns
  898. no synchronization objects! */
  899. ibool
  900. log_preflush_pool_modified_pages(
  901. /*=============================*/
  902. /* out: FALSE if there was a flush batch of
  903. the same type running, which means that we
  904. could not start this flush batch */
  905. dulint new_oldest, /* in: try to advance oldest_modified_lsn
  906. at least to this lsn */
  907. ibool sync) /* in: TRUE if synchronous operation is
  908. desired */
  909. {
  910. ulint n_pages;
  911. if (recv_recovery_on) {
  912. /* If the recovery is running, we must first apply all
  913. log records to their respective file pages to get the
  914. right modify lsn values to these pages: otherwise, there
  915. might be pages on disk which are not yet recovered to the
  916. current lsn, and even after calling this function, we could
  917. not know how up-to-date the disk version of the database is,
  918. and we could not make a new checkpoint on the basis of the
  919. info on the buffer pool only. */
  920. recv_apply_hashed_log_recs(TRUE);
  921. }
  922. n_pages = buf_flush_batch(BUF_FLUSH_LIST, ULINT_MAX, new_oldest);
  923. if (sync) {
  924. buf_flush_wait_batch_end(BUF_FLUSH_LIST);
  925. }
  926. if (n_pages == ULINT_UNDEFINED) {
  927. return(FALSE);
  928. }
  929. return(TRUE);
  930. }
  931. /**********************************************************
  932. Completes a checkpoint. */
  933. static
  934. void
  935. log_complete_checkpoint(void)
  936. /*=========================*/
  937. {
  938. ut_ad(mutex_own(&(log_sys->mutex)));
  939. ut_ad(log_sys->n_pending_checkpoint_writes == 0);
  940. log_sys->next_checkpoint_no
  941. = ut_dulint_add(log_sys->next_checkpoint_no, 1);
  942. log_sys->last_checkpoint_lsn = log_sys->next_checkpoint_lsn;
  943. rw_lock_x_unlock_gen(&(log_sys->checkpoint_lock), LOG_CHECKPOINT);
  944. }
  945. /**********************************************************
  946. Completes an asynchronous checkpoint info write i/o to a log file. */
  947. static
  948. void
  949. log_io_complete_checkpoint(
  950. /*=======================*/
  951. log_group_t* group) /* in: log group */
  952. {
  953. mutex_enter(&(log_sys->mutex));
  954. ut_ad(log_sys->n_pending_checkpoint_writes > 0);
  955. log_sys->n_pending_checkpoint_writes--;
  956. if (log_debug_writes) {
  957. printf("Checkpoint info written to group %lun", group->id);
  958. }
  959. if (log_sys->n_pending_checkpoint_writes == 0) {
  960. log_complete_checkpoint();
  961. }
  962. mutex_exit(&(log_sys->mutex));
  963. }
  964. /***********************************************************************
  965. Writes info to a checkpoint about a log group. */
  966. static
  967. void
  968. log_checkpoint_set_nth_group_info(
  969. /*==============================*/
  970. byte* buf, /* in: buffer for checkpoint info */
  971. ulint n, /* in: nth slot */
  972. ulint file_no,/* in: archived file number */
  973. ulint offset) /* in: archived file offset */
  974. {
  975. ut_ad(n < LOG_MAX_N_GROUPS);
  976. mach_write_to_4(buf + LOG_CHECKPOINT_GROUP_ARRAY
  977. + 8 * n + LOG_CHECKPOINT_ARCHIVED_FILE_NO, file_no);
  978. mach_write_to_4(buf + LOG_CHECKPOINT_GROUP_ARRAY
  979. + 8 * n + LOG_CHECKPOINT_ARCHIVED_OFFSET, offset);
  980. }
  981. /***********************************************************************
  982. Gets info from a checkpoint about a log group. */
  983. void
  984. log_checkpoint_get_nth_group_info(
  985. /*==============================*/
  986. byte* buf, /* in: buffer containing checkpoint info */
  987. ulint n, /* in: nth slot */
  988. ulint* file_no,/* out: archived file number */
  989. ulint* offset) /* out: archived file offset */
  990. {
  991. ut_ad(n < LOG_MAX_N_GROUPS);
  992. *file_no = mach_read_from_4(buf + LOG_CHECKPOINT_GROUP_ARRAY
  993. + 8 * n + LOG_CHECKPOINT_ARCHIVED_FILE_NO);
  994. *offset = mach_read_from_4(buf + LOG_CHECKPOINT_GROUP_ARRAY
  995. + 8 * n + LOG_CHECKPOINT_ARCHIVED_OFFSET);
  996. }
  997. /**********************************************************
  998. Writes the checkpoint info to a log group header. */
  999. static
  1000. void
  1001. log_group_checkpoint(
  1002. /*=================*/
  1003. log_group_t* group) /* in: log group */
  1004. {
  1005. log_group_t* group2;
  1006. dulint archived_lsn;
  1007. dulint next_archived_lsn;
  1008. ulint write_offset;
  1009. ulint fold;
  1010. byte* buf;
  1011. ulint i;
  1012. ut_ad(mutex_own(&(log_sys->mutex)));
  1013. ut_ad(LOG_CHECKPOINT_SIZE <= OS_FILE_LOG_BLOCK_SIZE);
  1014. buf = group->checkpoint_buf;
  1015. mach_write_to_8(buf + LOG_CHECKPOINT_NO, log_sys->next_checkpoint_no);
  1016. mach_write_to_8(buf + LOG_CHECKPOINT_LSN, log_sys->next_checkpoint_lsn);
  1017. mach_write_to_4(buf + LOG_CHECKPOINT_OFFSET,
  1018. log_group_calc_lsn_offset(
  1019. log_sys->next_checkpoint_lsn, group));
  1020. mach_write_to_4(buf + LOG_CHECKPOINT_LOG_BUF_SIZE, log_sys->buf_size);
  1021. if (log_sys->archiving_state == LOG_ARCH_OFF) {
  1022. archived_lsn = ut_dulint_max;
  1023. } else {
  1024. archived_lsn = log_sys->archived_lsn;
  1025. if (0 != ut_dulint_cmp(archived_lsn,
  1026. log_sys->next_archived_lsn)) {
  1027. next_archived_lsn = log_sys->next_archived_lsn;
  1028. /* For debugging only */
  1029. }
  1030. }
  1031. mach_write_to_8(buf + LOG_CHECKPOINT_ARCHIVED_LSN, archived_lsn);
  1032. for (i = 0; i < LOG_MAX_N_GROUPS; i++) {
  1033. log_checkpoint_set_nth_group_info(buf, i, 0, 0);
  1034. }
  1035. group2 = UT_LIST_GET_FIRST(log_sys->log_groups);
  1036. while (group2) {
  1037. log_checkpoint_set_nth_group_info(buf, group2->id,
  1038. group2->archived_file_no,
  1039. group2->archived_offset);
  1040. group2 = UT_LIST_GET_NEXT(log_groups, group2);
  1041. }
  1042. fold = ut_fold_binary(buf, LOG_CHECKPOINT_CHECKSUM_1);
  1043. mach_write_to_4(buf + LOG_CHECKPOINT_CHECKSUM_1, fold);
  1044. fold = ut_fold_binary(buf + LOG_CHECKPOINT_LSN,
  1045. LOG_CHECKPOINT_CHECKSUM_2 - LOG_CHECKPOINT_LSN);
  1046. mach_write_to_4(buf + LOG_CHECKPOINT_CHECKSUM_2, fold);
  1047. /* We alternate the physical place of the checkpoint info in the first
  1048. log file */
  1049. if (ut_dulint_get_low(log_sys->next_checkpoint_no) % 2 == 0) {
  1050. write_offset = LOG_CHECKPOINT_1;
  1051. } else {
  1052. write_offset = LOG_CHECKPOINT_2;
  1053. }
  1054. if (log_do_write) {
  1055. if (log_sys->n_pending_checkpoint_writes == 0) {
  1056. rw_lock_x_lock_gen(&(log_sys->checkpoint_lock),
  1057. LOG_CHECKPOINT);
  1058. }
  1059. log_sys->n_pending_checkpoint_writes++;
  1060. log_sys->n_log_ios++;
  1061. /* We send as the last parameter the group machine address
  1062. added with 1, as we want to distinguish between a normal log
  1063. file write and a checkpoint field write */
  1064. fil_io(OS_FILE_WRITE | OS_FILE_LOG, FALSE, group->space_id,
  1065. write_offset / UNIV_PAGE_SIZE,
  1066. write_offset % UNIV_PAGE_SIZE,
  1067. OS_FILE_LOG_BLOCK_SIZE,
  1068. buf, ((byte*)group + 1));
  1069. ut_ad(((ulint)group & 0x1) == 0);
  1070. }
  1071. }
  1072. /**********************************************************
  1073. Reads a checkpoint info from a log group header to log_sys->checkpoint_buf. */
  1074. void
  1075. log_group_read_checkpoint_info(
  1076. /*===========================*/
  1077. log_group_t* group, /* in: log group */
  1078. ulint field) /* in: LOG_CHECKPOINT_1 or LOG_CHECKPOINT_2 */
  1079. {
  1080. ut_ad(mutex_own(&(log_sys->mutex)));
  1081. log_sys->n_log_ios++;
  1082. fil_io(OS_FILE_READ | OS_FILE_LOG, TRUE, group->space_id,
  1083. field / UNIV_PAGE_SIZE, field % UNIV_PAGE_SIZE,
  1084. OS_FILE_LOG_BLOCK_SIZE, log_sys->checkpoint_buf, NULL);
  1085. }
  1086. /**********************************************************
  1087. Writes checkpoint info to groups. */
  1088. void
  1089. log_groups_write_checkpoint_info(void)
  1090. /*==================================*/
  1091. {
  1092. log_group_t* group;
  1093. ut_ad(mutex_own(&(log_sys->mutex)));
  1094. group = UT_LIST_GET_FIRST(log_sys->log_groups);
  1095. while (group) {
  1096. log_group_checkpoint(group);
  1097. group = UT_LIST_GET_NEXT(log_groups, group);
  1098. }
  1099. }
  1100. /**********************************************************
  1101. Makes a checkpoint. Note that this function does not flush dirty
  1102. blocks from the buffer pool: it only checks what is lsn of the oldest
  1103. modification in the pool, and writes information about the lsn in
  1104. log files. Use log_make_checkpoint_at to flush also the pool. */
  1105. ibool
  1106. log_checkpoint(
  1107. /*===========*/
  1108. /* out: TRUE if success, FALSE if a checkpoint
  1109. write was already running */
  1110. ibool sync, /* in: TRUE if synchronous operation is
  1111. desired */
  1112. ibool write_always) /* in: the function normally checks if the
  1113. the new checkpoint would have a greater
  1114. lsn than the previous one: if not, then no
  1115. physical write is done; by setting this
  1116. parameter TRUE, a physical write will always be
  1117. made to log files */
  1118. {
  1119. dulint oldest_lsn;
  1120. if (recv_recovery_is_on()) {
  1121. recv_apply_hashed_log_recs(TRUE);
  1122. }
  1123. fil_flush_file_spaces(FIL_TABLESPACE);
  1124. mutex_enter(&(log_sys->mutex));
  1125. oldest_lsn = log_buf_pool_get_oldest_modification();
  1126. mutex_exit(&(log_sys->mutex));
  1127. /* Because log also contains headers and dummy log records,
  1128. if the buffer pool contains no dirty buffers, oldest_lsn
  1129. gets the value log_sys->lsn from the previous function,
  1130. and we must make sure that the log is flushed up to that
  1131. lsn. If there are dirty buffers in the buffer pool, then our
  1132. write-ahead-logging algorithm ensures that the log has been flushed
  1133. up to oldest_lsn. */
  1134. log_flush_up_to(oldest_lsn, LOG_WAIT_ALL_GROUPS);
  1135. mutex_enter(&(log_sys->mutex));
  1136. if (!write_always && ut_dulint_cmp(
  1137. log_sys->last_checkpoint_lsn, oldest_lsn) >= 0) {
  1138. mutex_exit(&(log_sys->mutex));
  1139. return(TRUE);
  1140. }
  1141. ut_ad(ut_dulint_cmp(log_sys->written_to_all_lsn, oldest_lsn) >= 0);
  1142. if (log_sys->n_pending_checkpoint_writes > 0) {
  1143. /* A checkpoint write is running */
  1144. mutex_exit(&(log_sys->mutex));
  1145. if (sync) {
  1146. /* Wait for the checkpoint write to complete */
  1147. rw_lock_s_lock(&(log_sys->checkpoint_lock));
  1148. rw_lock_s_unlock(&(log_sys->checkpoint_lock));
  1149. }
  1150. return(FALSE);
  1151. }
  1152. log_sys->next_checkpoint_lsn = oldest_lsn;
  1153. if (log_debug_writes) {
  1154. printf("Making checkpoint no %lu at lsn %lu %lun",
  1155. ut_dulint_get_low(log_sys->next_checkpoint_no),
  1156. ut_dulint_get_high(oldest_lsn),
  1157. ut_dulint_get_low(oldest_lsn));
  1158. }
  1159. log_groups_write_checkpoint_info();
  1160. mutex_exit(&(log_sys->mutex));
  1161. if (sync) {
  1162. /* Wait for the checkpoint write to complete */
  1163. rw_lock_s_lock(&(log_sys->checkpoint_lock));
  1164. rw_lock_s_unlock(&(log_sys->checkpoint_lock));
  1165. }
  1166. return(TRUE);
  1167. }
  1168. /********************************************************************
  1169. Makes a checkpoint at a given lsn or later. */
  1170. void
  1171. log_make_checkpoint_at(
  1172. /*===================*/
  1173. dulint lsn, /* in: make a checkpoint at this or a later
  1174. lsn, if ut_dulint_max, makes a checkpoint at
  1175. the latest lsn */
  1176. ibool write_always) /* in: the function normally checks if the
  1177. the new checkpoint would have a greater
  1178. lsn than the previous one: if not, then no
  1179. physical write is done; by setting this
  1180. parameter TRUE, a physical write will always be
  1181. made to log files */
  1182. {
  1183. ibool success;
  1184. /* Preflush pages synchronously */
  1185. success = FALSE;
  1186. while (!success) {
  1187. success = log_preflush_pool_modified_pages(lsn, TRUE);
  1188. }
  1189. success = FALSE;
  1190. while (!success) {
  1191. success = log_checkpoint(TRUE, write_always);
  1192. }
  1193. }
  1194. /********************************************************************
  1195. Tries to establish a big enough margin of free space in the log groups, such
  1196. that a new log entry can be catenated without an immediate need for a
  1197. checkpoint. NOTE: this function may only be called if the calling thread
  1198. owns no synchronization objects! */
  1199. static
  1200. void
  1201. log_checkpoint_margin(void)
  1202. /*=======================*/
  1203. {
  1204. log_t* log = log_sys;
  1205. ulint age;
  1206. ulint checkpoint_age;
  1207. ulint advance;
  1208. dulint oldest_lsn;
  1209. dulint new_oldest;
  1210. ibool do_preflush;
  1211. ibool sync;
  1212. ibool checkpoint_sync;
  1213. ibool do_checkpoint;
  1214. ibool success;
  1215. loop:
  1216. sync = FALSE;
  1217. checkpoint_sync = FALSE;
  1218. do_preflush = FALSE;
  1219. do_checkpoint = FALSE;
  1220. mutex_enter(&(log->mutex));
  1221. if (log->check_flush_or_checkpoint == FALSE) {
  1222. mutex_exit(&(log->mutex));
  1223. return;
  1224. }
  1225. oldest_lsn = log_buf_pool_get_oldest_modification();
  1226. age = ut_dulint_minus(log->lsn, oldest_lsn);
  1227. if (age > log->max_modified_age_sync) {
  1228. /* A flush is urgent: we have to do a synchronous preflush */
  1229. sync = TRUE;
  1230. advance = 2 * (age - log->max_modified_age_sync);
  1231. new_oldest = ut_dulint_add(oldest_lsn, advance);
  1232. do_preflush = TRUE;
  1233. } else if (age > log->max_modified_age_async) {
  1234. /* A flush is not urgent: we do an asynchronous preflush */
  1235. advance = age - log->max_modified_age_async;
  1236. new_oldest = ut_dulint_add(oldest_lsn, advance);
  1237. do_preflush = TRUE;
  1238. }
  1239. checkpoint_age = ut_dulint_minus(log->lsn, log->last_checkpoint_lsn);
  1240. if (checkpoint_age > log->max_checkpoint_age) {
  1241. /* A checkpoint is urgent: we do it synchronously */
  1242. checkpoint_sync = TRUE;
  1243. do_checkpoint = TRUE;
  1244. } else if (checkpoint_age > log->max_checkpoint_age_async) {
  1245. /* A checkpoint is not urgent: do it asynchronously */
  1246. do_checkpoint = TRUE;
  1247. log->check_flush_or_checkpoint = FALSE;
  1248. } else {
  1249. log->check_flush_or_checkpoint = FALSE;
  1250. }
  1251. mutex_exit(&(log->mutex));
  1252. if (do_preflush) {
  1253. success = log_preflush_pool_modified_pages(new_oldest, sync);
  1254. /* If the flush succeeded, this thread has done its part
  1255. and can proceed. If it did not succeed, there was another
  1256. thread doing a flush at the same time. If sync was FALSE,
  1257. the flush was not urgent, and we let this thread proceed.
  1258. Otherwise, we let it start from the beginning again. */
  1259. if (sync && !success) {
  1260. mutex_enter(&(log->mutex));
  1261. log->check_flush_or_checkpoint = TRUE;
  1262. mutex_exit(&(log->mutex));
  1263. goto loop;
  1264. }
  1265. }
  1266. if (do_checkpoint) {
  1267. log_checkpoint(checkpoint_sync, FALSE);
  1268. if (checkpoint_sync) {
  1269. goto loop;
  1270. }
  1271. }
  1272. }
  1273. /**********************************************************
  1274. Reads a specified log segment to a buffer. */
  1275. void
  1276. log_group_read_log_seg(
  1277. /*===================*/
  1278. ulint type, /* in: LOG_ARCHIVE or LOG_RECOVER */
  1279. byte* buf, /* in: buffer where to read */
  1280. log_group_t* group, /* in: log group */
  1281. dulint start_lsn, /* in: read area start */
  1282. dulint end_lsn) /* in: read area end */
  1283. {
  1284. ulint len;
  1285. ulint source_offset;
  1286. ibool sync;
  1287. ut_ad(mutex_own(&(log_sys->mutex)));
  1288. sync = FALSE;
  1289. if (type == LOG_RECOVER) {
  1290. sync = TRUE;
  1291. }
  1292. loop:
  1293. source_offset = log_group_calc_lsn_offset(start_lsn, group);
  1294. len = ut_dulint_minus(end_lsn, start_lsn);
  1295. ut_ad(len != 0);
  1296. if ((source_offset % group->file_size) + len > group->file_size) {
  1297. len = group->file_size - (source_offset % group->file_size);
  1298. }
  1299. if (type == LOG_ARCHIVE) {
  1300. log_sys->n_pending_archive_ios++;
  1301. }
  1302. log_sys->n_log_ios++;
  1303. fil_io(OS_FILE_READ | OS_FILE_LOG, sync, group->space_id,
  1304. source_offset / UNIV_PAGE_SIZE, source_offset % UNIV_PAGE_SIZE,
  1305. len, buf, &log_archive_io);
  1306. start_lsn = ut_dulint_add(start_lsn, len);
  1307. buf += len;
  1308. if (ut_dulint_cmp(start_lsn, end_lsn) != 0) {
  1309. goto loop;
  1310. }
  1311. }
  1312. /**********************************************************
  1313. Generates an archived log file name. */
  1314. void
  1315. log_archived_file_name_gen(
  1316. /*=======================*/
  1317. char* buf, /* in: buffer where to write */
  1318. ulint id, /* in: group id */
  1319. ulint file_no)/* in: file number */
  1320. {
  1321. UT_NOT_USED(id); /* Currently we only archive the first group */
  1322. sprintf(buf, "%sib_arch_log_%010lu", srv_arch_dir, file_no);
  1323. }
  1324. /**********************************************************
  1325. Writes a log file header to a log file space. */
  1326. static
  1327. void
  1328. log_group_archive_file_header_write(
  1329. /*================================*/
  1330. log_group_t* group, /* in: log group */
  1331. ulint nth_file, /* in: header to the nth file in the
  1332. archive log file space */
  1333. ulint file_no, /* in: archived file number */
  1334. dulint start_lsn) /* in: log file data starts at this
  1335. lsn */
  1336. {
  1337. byte* buf;
  1338. ulint dest_offset;
  1339. ut_ad(mutex_own(&(log_sys->mutex)));
  1340. ut_a(nth_file < group->n_files);
  1341. buf = *(group->archive_file_header_bufs + nth_file);
  1342. mach_write_to_4(buf + LOG_GROUP_ID, group->id);
  1343. mach_write_to_8(buf + LOG_FILE_START_LSN, start_lsn);
  1344. mach_write_to_4(buf + LOG_FILE_NO, file_no);
  1345. mach_write_to_4(buf + LOG_FILE_ARCH_COMPLETED, FALSE);
  1346. dest_offset = nth_file * group->file_size;
  1347. log_sys->n_log_ios++;
  1348. fil_io(OS_FILE_WRITE | OS_FILE_LOG, TRUE, group->archive_space_id,
  1349. dest_offset / UNIV_PAGE_SIZE,
  1350. dest_offset % UNIV_PAGE_SIZE,
  1351. 2 * OS_FILE_LOG_BLOCK_SIZE,
  1352. buf, &log_archive_io);
  1353. }
  1354. /**********************************************************
  1355. Writes a log file header to a completed archived log file. */
  1356. static
  1357. void
  1358. log_group_archive_completed_header_write(
  1359. /*=====================================*/
  1360. log_group_t* group, /* in: log group */
  1361. ulint nth_file, /* in: header to the nth file in the
  1362. archive log file space */
  1363. dulint end_lsn) /* in: end lsn of the file */
  1364. {
  1365. byte* buf;
  1366. ulint dest_offset;
  1367. ut_ad(mutex_own(&(log_sys->mutex)));
  1368. ut_a(nth_file < group->n_files);
  1369. buf = *(group->archive_file_header_bufs + nth_file);
  1370. mach_write_to_4(buf + LOG_FILE_ARCH_COMPLETED, TRUE);
  1371. mach_write_to_8(buf + LOG_FILE_END_LSN, end_lsn);
  1372. dest_offset = nth_file * group->file_size + LOG_FILE_ARCH_COMPLETED;
  1373. log_sys->n_log_ios++;
  1374. fil_io(OS_FILE_WRITE | OS_FILE_LOG, TRUE, group->archive_space_id,
  1375. dest_offset / UNIV_PAGE_SIZE,
  1376. dest_offset % UNIV_PAGE_SIZE,
  1377. OS_FILE_LOG_BLOCK_SIZE,
  1378. buf + LOG_FILE_ARCH_COMPLETED,
  1379. &log_archive_io);
  1380. }
  1381. /**********************************************************
  1382. Does the archive writes for a single log group. */
  1383. static
  1384. void
  1385. log_group_archive(
  1386. /*==============*/
  1387. log_group_t* group) /* in: log group */
  1388. {
  1389. os_file_t file_handle;
  1390. dulint start_lsn;
  1391. dulint end_lsn;
  1392. char name[100];
  1393. byte* buf;
  1394. ulint len;
  1395. ibool ret;
  1396. ulint next_offset;
  1397. ulint n_files;
  1398. ulint open_mode;
  1399. ut_ad(mutex_own(&(log_sys->mutex)));
  1400. start_lsn = log_sys->archived_lsn;
  1401. ut_ad(ut_dulint_get_low(start_lsn) % OS_FILE_LOG_BLOCK_SIZE == 0);
  1402. end_lsn = log_sys->next_archived_lsn;
  1403. ut_ad(ut_dulint_get_low(end_lsn) % OS_FILE_LOG_BLOCK_SIZE == 0);
  1404. buf = log_sys->archive_buf;
  1405. n_files = 0;
  1406. next_offset = group->archived_offset;
  1407. loop:
  1408. if ((next_offset % group->file_size == 0)
  1409.     || (fil_space_get_size(group->archive_space_id) == 0)) {
  1410. /* Add the file to the archive file space; create or open the
  1411. file */
  1412. if (next_offset % group->file_size == 0) {
  1413. open_mode = OS_FILE_CREATE;
  1414. } else {
  1415. open_mode = OS_FILE_OPEN;
  1416. }
  1417. log_archived_file_name_gen(name, group->id,
  1418. group->archived_file_no + n_files);
  1419. fil_reserve_right_to_open();
  1420. file_handle = os_file_create(name, open_mode, OS_FILE_AIO,
  1421. &ret);
  1422. if (!ret && (open_mode == OS_FILE_CREATE)) {
  1423. file_handle = os_file_create(name, OS_FILE_OPEN,
  1424. OS_FILE_AIO, &ret);
  1425. }
  1426. ut_a(ret);
  1427. if (log_debug_writes) {
  1428. printf("Created archive file %sn", name);
  1429. }
  1430. ret = os_file_close(file_handle);
  1431. ut_a(ret);
  1432. fil_release_right_to_open();
  1433. /* Add the archive file as a node to the space */
  1434. fil_node_create(name, group->file_size / UNIV_PAGE_SIZE,
  1435. group->archive_space_id);
  1436. if (next_offset % group->file_size == 0) {
  1437. log_group_archive_file_header_write(group, n_files,
  1438. group->archived_file_no + n_files,
  1439. start_lsn);
  1440. next_offset += LOG_FILE_HDR_SIZE;
  1441. }
  1442. }
  1443. len = ut_dulint_minus(end_lsn, start_lsn);
  1444. if (group->file_size < (next_offset % group->file_size) + len) {
  1445. len = group->file_size - (next_offset % group->file_size);
  1446. }
  1447. if (log_debug_writes) {
  1448. printf(
  1449. "Archiving starting at lsn %lu %lu, len %lu to group %lun",
  1450. ut_dulint_get_high(start_lsn),
  1451. ut_dulint_get_low(start_lsn),
  1452. len, group->id);
  1453. }
  1454. log_sys->n_pending_archive_ios++;
  1455. log_sys->n_log_ios++;
  1456. fil_io(OS_FILE_WRITE | OS_FILE_LOG, FALSE, group->archive_space_id,
  1457. next_offset / UNIV_PAGE_SIZE, next_offset % UNIV_PAGE_SIZE,
  1458. ut_calc_align(len, OS_FILE_LOG_BLOCK_SIZE), buf,
  1459. &log_archive_io);
  1460. start_lsn = ut_dulint_add(start_lsn, len);
  1461. next_offset += len;
  1462. buf += len;
  1463. if (next_offset % group->file_size == 0) {
  1464. n_files++;
  1465. }
  1466. if (ut_dulint_cmp(end_lsn, start_lsn) != 0) {
  1467. goto loop;
  1468. }
  1469. group->next_archived_file_no = group->archived_file_no + n_files;
  1470. group->next_archived_offset = next_offset % group->file_size;
  1471. ut_ad(group->next_archived_offset % OS_FILE_LOG_BLOCK_SIZE == 0);
  1472. }
  1473. /*********************************************************
  1474. (Writes to the archive of each log group.) Currently, only the first
  1475. group is archived. */
  1476. static
  1477. void
  1478. log_archive_groups(void)
  1479. /*====================*/
  1480. {
  1481. log_group_t* group;
  1482. ut_ad(mutex_own(&(log_sys->mutex)));
  1483. group = UT_LIST_GET_FIRST(log_sys->log_groups);
  1484. log_group_archive(group);
  1485. }
  1486. /*********************************************************
  1487. Completes the archiving write phase for (each log group), currently,
  1488. the first log group. */
  1489. static
  1490. void
  1491. log_archive_write_complete_groups(void)
  1492. /*===================================*/
  1493. {
  1494. log_group_t* group;
  1495. ulint end_offset;
  1496. ulint trunc_files;
  1497. ulint n_files;
  1498. dulint start_lsn;
  1499. dulint end_lsn;
  1500. ulint i;
  1501. ut_ad(mutex_own(&(log_sys->mutex)));
  1502. group = UT_LIST_GET_FIRST(log_sys->log_groups);
  1503. group->archived_file_no = group->next_archived_file_no;
  1504. group->archived_offset = group->next_archived_offset;
  1505. /* Truncate from the archive file space all but the last
  1506. file, or if it has been written full, all files */
  1507. n_files = (UNIV_PAGE_SIZE
  1508.     * fil_space_get_size(group->archive_space_id))
  1509.      / group->file_size;
  1510. ut_ad(n_files > 0);
  1511. end_offset = group->archived_offset;
  1512. if (end_offset % group->file_size == 0) {
  1513. trunc_files = n_files;
  1514. } else {
  1515. trunc_files = n_files - 1;
  1516. }
  1517. if (log_debug_writes && trunc_files) {
  1518. printf("Complete file(s) archived to group %lun",
  1519. group->id);
  1520. }
  1521. /* Calculate the archive file space start lsn */
  1522. start_lsn = ut_dulint_subtract(log_sys->next_archived_lsn,
  1523. end_offset - LOG_FILE_HDR_SIZE
  1524. + trunc_files
  1525.   * (group->file_size - LOG_FILE_HDR_SIZE));
  1526. end_lsn = start_lsn;
  1527. for (i = 0; i < trunc_files; i++) {
  1528. end_lsn = ut_dulint_add(end_lsn,
  1529. group->file_size - LOG_FILE_HDR_SIZE);
  1530. /* Write a notice to the headers of archived log
  1531. files that the file write has been completed */
  1532. log_group_archive_completed_header_write(group, i, end_lsn);
  1533. }
  1534. fil_space_truncate_start(group->archive_space_id,
  1535. trunc_files * group->file_size);
  1536. if (log_debug_writes) {
  1537. printf("Archiving writes completedn");
  1538. }
  1539. }
  1540. /**********************************************************
  1541. Completes an archiving i/o. */
  1542. static
  1543. void
  1544. log_archive_check_completion_low(void)
  1545. /*==================================*/
  1546. {
  1547. ut_ad(mutex_own(&(log_sys->mutex)));
  1548. if (log_sys->n_pending_archive_ios == 0
  1549. && log_sys->archiving_phase == LOG_ARCHIVE_READ) {
  1550. if (log_debug_writes) {
  1551. printf("Archiving read completedn");
  1552. }
  1553.      /* Archive buffer has now been read in: start archive writes */
  1554. log_sys->archiving_phase = LOG_ARCHIVE_WRITE;
  1555. log_archive_groups();
  1556. }
  1557. if (log_sys->n_pending_archive_ios == 0
  1558. && log_sys->archiving_phase == LOG_ARCHIVE_WRITE) {
  1559.       log_archive_write_complete_groups();
  1560. log_sys->archived_lsn = log_sys->next_archived_lsn;
  1561. rw_lock_x_unlock_gen(&(log_sys->archive_lock), LOG_ARCHIVE);
  1562. }
  1563. }
  1564. /**********************************************************
  1565. Completes an archiving i/o. */
  1566. static
  1567. void
  1568. log_io_complete_archive(void)
  1569. /*=========================*/
  1570. {
  1571. log_group_t* group;
  1572. mutex_enter(&(log_sys->mutex));
  1573. group = UT_LIST_GET_FIRST(log_sys->log_groups);
  1574. mutex_exit(&(log_sys->mutex));
  1575. fil_flush(group->archive_space_id);
  1576. mutex_enter(&(log_sys->mutex));
  1577. ut_ad(log_sys->n_pending_archive_ios > 0);
  1578. log_sys->n_pending_archive_ios--;
  1579. log_archive_check_completion_low();
  1580. mutex_exit(&(log_sys->mutex));
  1581. }
  1582. /************************************************************************
  1583. Starts an archiving operation. */
  1584. ibool
  1585. log_archive_do(
  1586. /*===========*/
  1587. /* out: TRUE if succeed, FALSE if an archiving
  1588. operation was already running */
  1589. ibool sync, /* in: TRUE if synchronous operation is desired */
  1590. ulint* n_bytes)/* out: archive log buffer size, 0 if nothing to
  1591. archive */
  1592. {
  1593. ibool calc_new_limit;
  1594. dulint start_lsn;
  1595. dulint limit_lsn;
  1596. calc_new_limit = TRUE;
  1597. loop:
  1598. mutex_enter(&(log_sys->mutex));
  1599. if (log_sys->archiving_state == LOG_ARCH_OFF) {
  1600. mutex_exit(&(log_sys->mutex));
  1601. *n_bytes = 0;
  1602. return(TRUE);
  1603. } else if (log_sys->archiving_state == LOG_ARCH_STOPPED
  1604.            || log_sys->archiving_state == LOG_ARCH_STOPPING2) {
  1605. mutex_exit(&(log_sys->mutex));
  1606. os_event_wait(log_sys->archiving_on);
  1607. mutex_enter(&(log_sys->mutex));
  1608. goto loop;
  1609. }
  1610. start_lsn = log_sys->archived_lsn;
  1611. if (calc_new_limit) {
  1612. ut_ad(log_sys->archive_buf_size % OS_FILE_LOG_BLOCK_SIZE == 0);
  1613. limit_lsn = ut_dulint_add(start_lsn,
  1614. log_sys->archive_buf_size);
  1615. *n_bytes = log_sys->archive_buf_size;
  1616. if (ut_dulint_cmp(limit_lsn, log_sys->lsn) >= 0) {
  1617. limit_lsn = ut_dulint_align_down(log_sys->lsn,
  1618. OS_FILE_LOG_BLOCK_SIZE);
  1619. }
  1620. }
  1621. if (ut_dulint_cmp(log_sys->archived_lsn, limit_lsn) >= 0) {
  1622. mutex_exit(&(log_sys->mutex));
  1623. *n_bytes = 0;
  1624. return(TRUE);
  1625. }
  1626. if (ut_dulint_cmp(log_sys->written_to_all_lsn, limit_lsn) < 0) {
  1627. mutex_exit(&(log_sys->mutex));
  1628. log_flush_up_to(limit_lsn, LOG_WAIT_ALL_GROUPS);
  1629. calc_new_limit = FALSE;
  1630. goto loop;
  1631. }
  1632. if (log_sys->n_pending_archive_ios > 0) {
  1633. /* An archiving operation is running */
  1634. mutex_exit(&(log_sys->mutex));
  1635. if (sync) {
  1636. rw_lock_s_lock(&(log_sys->archive_lock));
  1637. rw_lock_s_unlock(&(log_sys->archive_lock));
  1638. }
  1639. *n_bytes = log_sys->archive_buf_size;
  1640. return(FALSE);
  1641. }
  1642. rw_lock_x_lock_gen(&(log_sys->archive_lock), LOG_ARCHIVE);
  1643. log_sys->archiving_phase = LOG_ARCHIVE_READ;
  1644. log_sys->next_archived_lsn = limit_lsn;
  1645. if (log_debug_writes) {
  1646. printf("Archiving from lsn %lu %lu to lsn %lu %lun",
  1647. ut_dulint_get_high(log_sys->archived_lsn),
  1648. ut_dulint_get_low(log_sys->archived_lsn),
  1649. ut_dulint_get_high(limit_lsn),
  1650. ut_dulint_get_low(limit_lsn));
  1651. }
  1652. /* Read the log segment to the archive buffer */
  1653. log_group_read_log_seg(LOG_ARCHIVE, log_sys->archive_buf,
  1654. UT_LIST_GET_FIRST(log_sys->log_groups),
  1655. start_lsn, limit_lsn);
  1656. mutex_exit(&(log_sys->mutex));
  1657. if (sync) {
  1658. rw_lock_s_lock(&(log_sys->archive_lock));
  1659. rw_lock_s_unlock(&(log_sys->archive_lock));
  1660. }
  1661. *n_bytes = log_sys->archive_buf_size;
  1662. return(TRUE);
  1663. }
  1664. /********************************************************************
  1665. Writes the log contents to the archive at least up to the lsn when this
  1666. function was called. */
  1667. static
  1668. void
  1669. log_archive_all(void)
  1670. /*=================*/
  1671. {
  1672. dulint present_lsn;
  1673. ulint dummy;
  1674. mutex_enter(&(log_sys->mutex));
  1675. if (log_sys->archiving_state == LOG_ARCH_OFF) {
  1676. mutex_exit(&(log_sys->mutex));
  1677. return;
  1678. }
  1679. present_lsn = log_sys->lsn;
  1680. mutex_exit(&(log_sys->mutex));
  1681. log_pad_current_log_block();
  1682. for (;;) {
  1683. mutex_enter(&(log_sys->mutex));
  1684. if (ut_dulint_cmp(present_lsn, log_sys->archived_lsn) <= 0) {
  1685. mutex_exit(&(log_sys->mutex));
  1686. return;
  1687. }
  1688. mutex_exit(&(log_sys->mutex));
  1689. log_archive_do(TRUE, &dummy);
  1690. }
  1691. }
  1692. /*********************************************************
  1693. Closes the possible open archive log file (for each group) the first group,
  1694. and if it was open, increments the group file count by 2, if desired. */
  1695. static
  1696. void
  1697. log_archive_close_groups(
  1698. /*=====================*/
  1699. ibool increment_file_count) /* in: TRUE if we want to increment
  1700. the file count */
  1701. {
  1702. log_group_t* group;
  1703. ulint trunc_len;
  1704. ut_ad(mutex_own(&(log_sys->mutex)));
  1705. group = UT_LIST_GET_FIRST(log_sys->log_groups);
  1706. trunc_len = UNIV_PAGE_SIZE
  1707.     * fil_space_get_size(group->archive_space_id);
  1708. if (trunc_len > 0) {
  1709. ut_a(trunc_len == group->file_size);
  1710.     
  1711. /* Write a notice to the headers of archived log
  1712. files that the file write has been completed */
  1713. log_group_archive_completed_header_write(group,
  1714. 0, log_sys->archived_lsn);
  1715. fil_space_truncate_start(group->archive_space_id,
  1716. trunc_len);
  1717. if (increment_file_count) {
  1718. group->archived_offset = 0;
  1719. group->archived_file_no += 2;
  1720. }
  1721. if (log_debug_writes) {
  1722. printf(
  1723. "Incrementing arch file no to %lu in log group %lun",
  1724. group->archived_file_no + 2, group->id);
  1725. }
  1726. }
  1727. }
  1728. /********************************************************************
  1729. Writes the log contents to the archive up to the lsn when this function was
  1730. called, and stops the archiving. When archiving is started again, the archived
  1731. log file numbers start from 2 higher, so that the archiving will
  1732. not write again to the archived log files which exist when this function
  1733. returns. */
  1734. ulint
  1735. log_archive_stop(void)
  1736. /*==================*/
  1737. /* out: DB_SUCCESS or DB_ERROR */
  1738. {
  1739. ibool success;
  1740. mutex_enter(&(log_sys->mutex));
  1741. if (log_sys->archiving_state != LOG_ARCH_ON) {
  1742. mutex_exit(&(log_sys->mutex));
  1743. return(DB_ERROR);
  1744. }
  1745. log_sys->archiving_state = LOG_ARCH_STOPPING;
  1746. mutex_exit(&(log_sys->mutex));
  1747. log_archive_all();
  1748. mutex_enter(&(log_sys->mutex));
  1749. log_sys->archiving_state = LOG_ARCH_STOPPING2;
  1750. os_event_reset(log_sys->archiving_on);
  1751. mutex_exit(&(log_sys->mutex));
  1752. /* Wait for a possible archiving operation to end */
  1753. rw_lock_s_lock(&(log_sys->archive_lock));
  1754. rw_lock_s_unlock(&(log_sys->archive_lock));
  1755. mutex_enter(&(log_sys->mutex));
  1756. /* Close all archived log files, incrementing the file count by 2,
  1757. if appropriate */
  1758. log_archive_close_groups(TRUE);
  1759. mutex_exit(&(log_sys->mutex));
  1760. /* Make a checkpoint, so that if recovery is needed, the file numbers
  1761. of new archived log files will start from the right value */
  1762. success = FALSE;
  1763. while (!success) {
  1764. success = log_checkpoint(TRUE, TRUE);
  1765. }
  1766. mutex_enter(&(log_sys->mutex));
  1767. log_sys->archiving_state = LOG_ARCH_STOPPED;
  1768. mutex_exit(&(log_sys->mutex));
  1769. return(DB_SUCCESS);
  1770. }
  1771. /********************************************************************
  1772. Starts again archiving which has been stopped. */
  1773. ulint
  1774. log_archive_start(void)
  1775. /*===================*/
  1776. /* out: DB_SUCCESS or DB_ERROR */
  1777. {
  1778. mutex_enter(&(log_sys->mutex));
  1779. if (log_sys->archiving_state != LOG_ARCH_STOPPED) {
  1780. mutex_exit(&(log_sys->mutex));
  1781. return(DB_ERROR);
  1782. }
  1783. log_sys->archiving_state = LOG_ARCH_ON;
  1784. os_event_set(log_sys->archiving_on);
  1785. mutex_exit(&(log_sys->mutex));
  1786. return(DB_SUCCESS);
  1787. }
  1788. /********************************************************************
  1789. Stop archiving the log so that a gap may occur in the archived log files. */
  1790. ulint
  1791. log_archive_noarchivelog(void)
  1792. /*==========================*/
  1793. /* out: DB_SUCCESS or DB_ERROR */
  1794. {
  1795. loop:
  1796. mutex_enter(&(log_sys->mutex));
  1797. if (log_sys->archiving_state == LOG_ARCH_STOPPED
  1798.     || log_sys->archiving_state == LOG_ARCH_OFF) {
  1799. log_sys->archiving_state = LOG_ARCH_OFF;
  1800. os_event_set(log_sys->archiving_on);
  1801. mutex_exit(&(log_sys->mutex));
  1802. return(DB_SUCCESS);
  1803. }
  1804. mutex_exit(&(log_sys->mutex));
  1805. log_archive_stop();
  1806. os_thread_sleep(500000);
  1807. goto loop;
  1808. }
  1809. /********************************************************************
  1810. Start archiving the log so that a gap may occur in the archived log files. */
  1811. ulint
  1812. log_archive_archivelog(void)
  1813. /*========================*/
  1814. /* out: DB_SUCCESS or DB_ERROR */
  1815. {
  1816. mutex_enter(&(log_sys->mutex));
  1817. if (log_sys->archiving_state == LOG_ARCH_OFF) {
  1818. log_sys->archiving_state = LOG_ARCH_ON;
  1819. log_sys->archived_lsn = ut_dulint_align_down(log_sys->lsn,
  1820. OS_FILE_LOG_BLOCK_SIZE);
  1821. mutex_exit(&(log_sys->mutex));
  1822. return(DB_SUCCESS);
  1823. }
  1824. mutex_exit(&(log_sys->mutex));
  1825. return(DB_ERROR);
  1826. }
  1827. /********************************************************************
  1828. Tries to establish a big enough margin of free space in the log groups, such
  1829. that a new log entry can be catenated without an immediate need for
  1830. archiving. */
  1831. static
  1832. void
  1833. log_archive_margin(void)
  1834. /*====================*/
  1835. {
  1836. log_t* log = log_sys;
  1837. ulint age;
  1838. ibool sync;
  1839. ulint dummy;
  1840. loop:
  1841. mutex_enter(&(log->mutex));
  1842. if (log->archiving_state == LOG_ARCH_OFF) {
  1843. mutex_exit(&(log->mutex));
  1844. return;
  1845. }
  1846. age = ut_dulint_minus(log->lsn, log->archived_lsn);
  1847. if (age > log->max_archived_lsn_age) {
  1848. /* An archiving is urgent: we have to do synchronous i/o */
  1849. sync = TRUE;
  1850. } else if (age > log->max_archived_lsn_age_async) {
  1851. /* An archiving is not urgent: we do asynchronous i/o */
  1852. sync = FALSE;
  1853. } else {
  1854. /* No archiving required yet */
  1855. mutex_exit(&(log->mutex));
  1856. return;
  1857. }
  1858. mutex_exit(&(log->mutex));
  1859. log_archive_do(sync, &dummy);
  1860. if (sync == TRUE) {
  1861. /* Check again that enough was written to the archive */
  1862. goto loop;
  1863. }
  1864. }
  1865. /************************************************************************
  1866. Checks that there is enough free space in the log to start a new query step.
  1867. Flushes the log buffer or makes a new checkpoint if necessary. NOTE: this
  1868. function may only be called if the calling thread owns no synchronization
  1869. objects! */
  1870. void
  1871. log_check_margins(void)
  1872. /*===================*/
  1873. {
  1874. loop:
  1875. log_flush_margin();
  1876. log_checkpoint_margin();
  1877. log_archive_margin();
  1878. mutex_enter(&(log_sys->mutex));
  1879. if (log_sys->check_flush_or_checkpoint) {
  1880. mutex_exit(&(log_sys->mutex));
  1881. goto loop;
  1882. }
  1883. mutex_exit(&(log_sys->mutex));
  1884. }
  1885. /**********************************************************
  1886. Switches the database to the online backup state. */
  1887. ulint
  1888. log_switch_backup_state_on(void)
  1889. /*============================*/
  1890. /* out: DB_SUCCESS or DB_ERROR */
  1891. {
  1892. dulint backup_lsn;
  1893. mutex_enter(&(log_sys->mutex));
  1894. if (log_sys->online_backup_state) {
  1895. /* The database is already in that state */
  1896. mutex_exit(&(log_sys->mutex));
  1897. return(DB_ERROR);
  1898. }
  1899. log_sys->online_backup_state = TRUE;
  1900. backup_lsn = log_sys->lsn;
  1901. log_sys->online_backup_lsn = backup_lsn;
  1902. mutex_exit(&(log_sys->mutex));
  1903. /* log_checkpoint_and_mark_file_spaces(); */
  1904. return(DB_SUCCESS);
  1905. }
  1906. /**********************************************************
  1907. Switches the online backup state off. */
  1908. ulint
  1909. log_switch_backup_state_off(void)
  1910. /*=============================*/
  1911. /* out: DB_SUCCESS or DB_ERROR */
  1912. {
  1913. mutex_enter(&(log_sys->mutex));
  1914. if (!log_sys->online_backup_state) {
  1915. /* The database is already in that state */
  1916. mutex_exit(&(log_sys->mutex));
  1917. return(DB_ERROR);
  1918. }
  1919. log_sys->online_backup_state = FALSE;
  1920. mutex_exit(&(log_sys->mutex));
  1921. return(DB_SUCCESS);
  1922. }
  1923. /********************************************************************
  1924. Makes a checkpoint at the latest lsn and writes it to first page of each
  1925. data file in the database, so that we know that the file spaces contain
  1926. all modifications up to that lsn. This can only be called at database
  1927. shutdown. This function also writes all log in log files to the log archive. */
  1928. void
  1929. logs_empty_and_mark_files_at_shutdown(void)
  1930. /*=======================================*/
  1931. {
  1932. dulint lsn;
  1933. ulint arch_log_no;
  1934. fprintf(stderr, "Innobase: Starting shutdown...n");
  1935. /* Wait until the master thread and all other operations are idle: our
  1936. algorithm only works if the server is idle at shutdown */
  1937. loop:
  1938. os_thread_sleep(100000);
  1939. mutex_enter(&kernel_mutex);
  1940. if (trx_n_mysql_transactions > 0
  1941. || UT_LIST_GET_LEN(trx_sys->trx_list) > 0) {
  1942. mutex_exit(&kernel_mutex);
  1943. goto loop;
  1944. }
  1945. if (srv_n_threads_active[SRV_MASTER] != 0) {
  1946. mutex_exit(&kernel_mutex);
  1947. goto loop;
  1948. }
  1949. mutex_exit(&kernel_mutex);
  1950. mutex_enter(&(log_sys->mutex));
  1951. if (log_sys->n_pending_archive_ios
  1952. + log_sys->n_pending_checkpoint_writes
  1953. + log_sys->n_pending_writes > 0) {
  1954. mutex_exit(&(log_sys->mutex));
  1955. goto loop;
  1956. }
  1957. mutex_exit(&(log_sys->mutex));
  1958. if (!buf_pool_check_no_pending_io()) {
  1959. goto loop;
  1960. }
  1961. log_archive_all();
  1962. log_make_checkpoint_at(ut_dulint_max, TRUE);
  1963. mutex_enter(&(log_sys->mutex));
  1964. lsn = log_sys->lsn;
  1965. if (ut_dulint_cmp(lsn, log_sys->last_checkpoint_lsn) != 0
  1966.    || (srv_log_archive_on
  1967. && ut_dulint_cmp(lsn,
  1968.     ut_dulint_add(log_sys->archived_lsn, LOG_BLOCK_HDR_SIZE)) != 0)) {
  1969.      mutex_exit(&(log_sys->mutex));
  1970.      goto loop;
  1971. }    
  1972. arch_log_no =
  1973. UT_LIST_GET_FIRST(log_sys->log_groups)->archived_file_no;
  1974. if (0 == UT_LIST_GET_FIRST(log_sys->log_groups)->archived_offset) {
  1975. arch_log_no--;
  1976. }
  1977. log_archive_close_groups(TRUE);
  1978. mutex_exit(&(log_sys->mutex));
  1979. fil_flush_file_spaces(FIL_TABLESPACE);
  1980. fil_flush_file_spaces(FIL_LOG);
  1981. /* The following fil_write_... will pass the buffer pool: therefore
  1982. it is essential that the buffer pool has been completely flushed
  1983. to disk! */
  1984. if (!buf_all_freed()) {
  1985. goto loop;
  1986. }
  1987. fil_write_flushed_lsn_to_data_files(lsn, arch_log_no);
  1988. fil_flush_file_spaces(FIL_TABLESPACE);
  1989. fprintf(stderr, "Innobase: Shutdown completedn");
  1990. }
  1991. /**********************************************************
  1992. Checks by parsing that the catenated log segment for a single mtr is
  1993. consistent. */
  1994. ibool
  1995. log_check_log_recs(
  1996. /*===============*/
  1997. byte* buf, /* in: pointer to the start of the log segment
  1998. in the log_sys->buf log buffer */
  1999. ulint len, /* in: segment length in bytes */
  2000. dulint buf_start_lsn) /* in: buffer start lsn */
  2001. {
  2002. dulint contiguous_lsn;
  2003. dulint scanned_lsn;
  2004. byte* start;
  2005. byte* end;
  2006. byte* buf1;
  2007. byte* scan_buf;
  2008. ut_ad(mutex_own(&(log_sys->mutex)));
  2009. if (len == 0) {
  2010. return(TRUE);
  2011. }
  2012. start = ut_align_down(buf, OS_FILE_LOG_BLOCK_SIZE);
  2013. end = ut_align(buf + len, OS_FILE_LOG_BLOCK_SIZE);
  2014. buf1 = mem_alloc((end - start) + OS_FILE_LOG_BLOCK_SIZE);
  2015. scan_buf = ut_align(buf1, OS_FILE_LOG_BLOCK_SIZE);
  2016. ut_memcpy(scan_buf, start, end - start);
  2017. recv_scan_log_recs(FALSE, scan_buf, end - start,
  2018. ut_dulint_align_down(buf_start_lsn,
  2019. OS_FILE_LOG_BLOCK_SIZE),
  2020. &contiguous_lsn, &scanned_lsn);
  2021. ut_a(ut_dulint_cmp(scanned_lsn, ut_dulint_add(buf_start_lsn, len))
  2022. == 0);
  2023. ut_a(ut_dulint_cmp(recv_sys->recovered_lsn, scanned_lsn) == 0);
  2024. mem_free(buf1);
  2025. return(TRUE);
  2026. }
  2027. /**********************************************************
  2028. Prints info of the log. */
  2029. void
  2030. log_print(void)
  2031. /*===========*/
  2032. {
  2033. printf("Log sequence number %lu %lun",
  2034. ut_dulint_get_high(log_sys->lsn),
  2035. ut_dulint_get_low(log_sys->lsn));
  2036. }