drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
log0log.cc
1/*****************************************************************************
2
3Copyright (C) 1995, 2010, Innobase Oy. All Rights Reserved.
4Copyright (C) 2009 Google Inc.
5
6Portions of this file contain modifications contributed and copyrighted by
7Google, Inc. Those modifications are gratefully acknowledged and are described
8briefly in the InnoDB documentation. The contributions by Google are
9incorporated with their permission, and subject to the conditions contained in
10the file COPYING.Google.
11
12This program is free software; you can redistribute it and/or modify it under
13the terms of the GNU General Public License as published by the Free Software
14Foundation; version 2 of the License.
15
16This program is distributed in the hope that it will be useful, but WITHOUT
17ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License along with
21this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
22St, Fifth Floor, Boston, MA 02110-1301 USA
23
24*****************************************************************************/
25
26/**************************************************/
32
33#include "log0log.h"
34
35#ifdef UNIV_NONINL
36#include "log0log.ic"
37#endif
38
39#ifndef UNIV_HOTBACKUP
40#include "mem0mem.h"
41#include "buf0buf.h"
42#include "buf0flu.h"
43#include "srv0srv.h"
44#include "log0recv.h"
45#include "fil0fil.h"
46#include "dict0boot.h"
47#include "srv0srv.h"
48#include "srv0start.h"
49#include "trx0sys.h"
50#include "trx0trx.h"
51#include "ha_prototypes.h"
52
53#include <drizzled/errmsg_print.h>
54
55/*
56General philosophy of InnoDB redo-logs:
57
581) Every change to a contents of a data page must be done
59through mtr, which in mtr_commit() writes log records
60to the InnoDB redo log.
61
622) Normally these changes are performed using a mlog_write_ulint()
63or similar function.
64
653) In some page level operations only a code number of a
66c-function and its parameters are written to the log to
67reduce the size of the log.
68
69 3a) You should not add parameters to these kind of functions
70 (e.g. trx_undo_header_create(), trx_undo_insert_header_reuse())
71
72 3b) You should not add such functionality which either change
73 working when compared with the old or are dependent on data
74 outside of the page. These kind of functions should implement
75 self-contained page transformation and it should be unchanged
76 if you don't have very essential reasons to change log
77 semantics or format.
78
79*/
80
81/* Current free limit of space 0; protected by the log sys mutex; 0 means
82uninitialized */
83UNIV_INTERN ulint log_fsp_current_free_limit = 0;
84
85/* Global log system variable */
86UNIV_INTERN log_t* log_sys = NULL;
87
88#ifdef UNIV_PFS_RWLOCK
89UNIV_INTERN mysql_pfs_key_t checkpoint_lock_key;
90# ifdef UNIV_LOG_ARCHIVE
91UNIV_INTERN mysql_pfs_key_t archive_lock_key;
92# endif
93#endif /* UNIV_PFS_RWLOCK */
94
95#ifdef UNIV_PFS_MUTEX
96UNIV_INTERN mysql_pfs_key_t log_sys_mutex_key;
97UNIV_INTERN mysql_pfs_key_t log_flush_order_mutex_key;
98#endif /* UNIV_PFS_MUTEX */
99
100#ifdef UNIV_DEBUG
101UNIV_INTERN ibool log_do_write = TRUE;
102#endif /* UNIV_DEBUG */
103
104/* These control how often we print warnings if the last checkpoint is too
105old */
106UNIV_INTERN ibool log_has_printed_chkp_warning = FALSE;
107UNIV_INTERN time_t log_last_warning_time;
108
109#ifdef UNIV_LOG_ARCHIVE
110/* Pointer to this variable is used as the i/o-message when we do i/o to an
111archive */
112UNIV_INTERN byte log_archive_io;
113#endif /* UNIV_LOG_ARCHIVE */
114
115/* A margin for free space in the log buffer before a log entry is catenated */
116#define LOG_BUF_WRITE_MARGIN (4 * OS_FILE_LOG_BLOCK_SIZE)
117
118/* Margins for free space in the log buffer after a log entry is catenated */
119#define LOG_BUF_FLUSH_RATIO 2
120#define LOG_BUF_FLUSH_MARGIN (LOG_BUF_WRITE_MARGIN + 4 * UNIV_PAGE_SIZE)
121
122/* Margin for the free space in the smallest log group, before a new query
123step which modifies the database, is started */
124
125#define LOG_CHECKPOINT_FREE_PER_THREAD (4 * UNIV_PAGE_SIZE)
126#define LOG_CHECKPOINT_EXTRA_FREE (8 * UNIV_PAGE_SIZE)
127
128/* This parameter controls asynchronous making of a new checkpoint; the value
129should be bigger than LOG_POOL_PREFLUSH_RATIO_SYNC */
130
131#define LOG_POOL_CHECKPOINT_RATIO_ASYNC 32
132
133/* This parameter controls synchronous preflushing of modified buffer pages */
134#define LOG_POOL_PREFLUSH_RATIO_SYNC 16
135
136/* The same ratio for asynchronous preflushing; this value should be less than
137the previous */
138#define LOG_POOL_PREFLUSH_RATIO_ASYNC 8
139
140/* Extra margin, in addition to one log file, used in archiving */
141#define LOG_ARCHIVE_EXTRA_MARGIN (4 * UNIV_PAGE_SIZE)
142
143/* This parameter controls asynchronous writing to the archive */
144#define LOG_ARCHIVE_RATIO_ASYNC 16
145
146/* Codes used in unlocking flush latches */
147#define LOG_UNLOCK_NONE_FLUSHED_LOCK 1
148#define LOG_UNLOCK_FLUSH_LOCK 2
149
150/* States of an archiving operation */
151#define LOG_ARCHIVE_READ 1
152#define LOG_ARCHIVE_WRITE 2
153
154/******************************************************/
156static
157void
158log_io_complete_checkpoint(void);
159/*============================*/
160#ifdef UNIV_LOG_ARCHIVE
161/******************************************************/
163static
164void
165log_io_complete_archive(void);
166/*=========================*/
167#endif /* UNIV_LOG_ARCHIVE */
168
169/****************************************************************/
173UNIV_INTERN
174void
176/*==========================================*/
177 ulint limit)
178{
179 ibool success;
180
181 mutex_enter(&(log_sys->mutex));
182
183 log_fsp_current_free_limit = limit;
184
185 mutex_exit(&(log_sys->mutex));
186
187 /* Try to make a synchronous checkpoint */
188
189 success = FALSE;
190
191 while (!success) {
192 success = log_checkpoint(TRUE, TRUE);
193 }
194}
195
196/****************************************************************/
200static
201ib_uint64_t
202log_buf_pool_get_oldest_modification(void)
203/*======================================*/
204{
205 ib_uint64_t lsn;
206
207 ut_ad(mutex_own(&(log_sys->mutex)));
208
210
211 if (!lsn) {
212
213 lsn = log_sys->lsn;
214 }
215
216 return(lsn);
217}
218
219/************************************************************/
223UNIV_INTERN
224ib_uint64_t
226/*=================*/
227 ulint len)
228{
229 log_t* log = log_sys;
230 ulint len_upper_limit;
231#ifdef UNIV_LOG_ARCHIVE
232 ulint archived_lsn_age;
233 ulint dummy;
234#endif /* UNIV_LOG_ARCHIVE */
235#ifdef UNIV_DEBUG
236 ulint count = 0;
237#endif /* UNIV_DEBUG */
238
239 ut_a(len < log->buf_size / 2);
240loop:
241 mutex_enter(&(log->mutex));
242 ut_ad(!recv_no_log_write);
243
244 /* Calculate an upper limit for the space the string may take in the
245 log buffer */
246
247 len_upper_limit = LOG_BUF_WRITE_MARGIN + (5 * len) / 4;
248
249 if (log->buf_free + len_upper_limit > log->buf_size) {
250
251 mutex_exit(&(log->mutex));
252
253 /* Not enough free space, do a syncronous flush of the log
254 buffer */
255
257
258 srv_log_waits++;
259
260 ut_ad(++count < 50);
261
262 goto loop;
263 }
264
265#ifdef UNIV_LOG_ARCHIVE
266 if (log->archiving_state != LOG_ARCH_OFF) {
267
268 archived_lsn_age = log->lsn - log->archived_lsn;
269 if (archived_lsn_age + len_upper_limit
270 > log->max_archived_lsn_age) {
271 /* Not enough free archived space in log groups: do a
272 synchronous archive write batch: */
273
274 mutex_exit(&(log->mutex));
275
276 ut_ad(len_upper_limit <= log->max_archived_lsn_age);
277
278 log_archive_do(TRUE, &dummy);
279
280 ut_ad(++count < 50);
281
282 goto loop;
283 }
284 }
285#endif /* UNIV_LOG_ARCHIVE */
286
287#ifdef UNIV_LOG_DEBUG
288 log->old_buf_free = log->buf_free;
289 log->old_lsn = log->lsn;
290#endif
291 return(log->lsn);
292}
293
294/************************************************************/
297UNIV_INTERN
298void
300/*==========*/
301 byte* str,
302 ulint str_len)
303{
304 log_t* log = log_sys;
305 ulint len;
306 ulint data_len;
307 byte* log_block;
308
309 ut_ad(mutex_own(&(log->mutex)));
310part_loop:
311 ut_ad(!recv_no_log_write);
312 /* Calculate a part length */
313
314 data_len = (log->buf_free % OS_FILE_LOG_BLOCK_SIZE) + str_len;
315
316 if (data_len <= OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_TRL_SIZE) {
317
318 /* The string fits within the current log block */
319
320 len = str_len;
321 } else {
322 data_len = OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_TRL_SIZE;
323
326 - LOG_BLOCK_TRL_SIZE;
327 }
328
329 ut_memcpy(log->buf + log->buf_free, str, len);
330
331 str_len -= len;
332 str = str + len;
333
334 log_block = static_cast<unsigned char *>(ut_align_down(log->buf + log->buf_free,
336 log_block_set_data_len(log_block, data_len);
337
338 if (data_len == OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_TRL_SIZE) {
339 /* This block became full */
341 log_block_set_checkpoint_no(log_block,
342 log_sys->next_checkpoint_no);
343 len += LOG_BLOCK_HDR_SIZE + LOG_BLOCK_TRL_SIZE;
344
345 log->lsn += len;
346
347 /* Initialize the next block header */
348 log_block_init(log_block + OS_FILE_LOG_BLOCK_SIZE, log->lsn);
349 } else {
350 log->lsn += len;
351 }
352
353 log->buf_free += len;
354
355 ut_ad(log->buf_free <= log->buf_size);
356
357 if (str_len > 0) {
358 goto part_loop;
359 }
360
361 srv_log_write_requests++;
362}
363
364/************************************************************/
366UNIV_INLINE
367ulint
368log_max_modified_age_async()
369/*========================*/
370{
371 if (srv_checkpoint_age_target) {
372 return(ut_min(log_sys->max_modified_age_async,
373 srv_checkpoint_age_target
374 - srv_checkpoint_age_target / 8));
375 } else {
376 return(log_sys->max_modified_age_async);
377 }
378}
379
380/************************************************************/
382UNIV_INLINE
383ulint
384log_max_checkpoint_age_async()
385/*==========================*/
386{
387 if (srv_checkpoint_age_target) {
388 return(ut_min(log_sys->max_checkpoint_age_async,
389 srv_checkpoint_age_target));
390 } else {
391 return(log_sys->max_checkpoint_age_async);
392 }
393}
394
395
396
397/************************************************************/
400UNIV_INTERN
401ib_uint64_t
403/*===========*/
404{
405 byte* log_block;
406 ulint first_rec_group;
407 ib_uint64_t oldest_lsn;
408 ib_uint64_t lsn;
409 log_t* log = log_sys;
410 ib_uint64_t checkpoint_age;
411
412 ut_ad(mutex_own(&(log->mutex)));
413 ut_ad(!recv_no_log_write);
414
415 lsn = log->lsn;
416
417 log_block = static_cast<unsigned char *>(ut_align_down(log->buf + log->buf_free,
419 first_rec_group = log_block_get_first_rec_group(log_block);
420
421 if (first_rec_group == 0) {
422 /* We initialized a new log block which was not written
423 full by the current mtr: the next mtr log record group
424 will start within this block at the offset data_len */
425
427 log_block, log_block_get_data_len(log_block));
428 }
429
430 if (log->buf_free > log->max_buf_free) {
431
432 log->check_flush_or_checkpoint = TRUE;
433 }
434
435 checkpoint_age = lsn - log->last_checkpoint_lsn;
436
437 if (checkpoint_age >= log->log_group_capacity) {
438 /* TODO: split btr_store_big_rec_extern_fields() into small
439 steps so that we can release all latches in the middle, and
440 call log_free_check() to ensure we never write over log written
441 after the latest checkpoint. In principle, we should split all
442 big_rec operations, but other operations are smaller. */
443
444 if (!log_has_printed_chkp_warning
445 || difftime(time(NULL), log_last_warning_time) > 15) {
446
447 log_has_printed_chkp_warning = TRUE;
448 log_last_warning_time = time(NULL);
449
450 ut_print_timestamp(stderr);
451 fprintf(stderr,
452 " InnoDB: ERROR: the age of the last"
453 " checkpoint is %lu,\n"
454 "InnoDB: which exceeds the log group"
455 " capacity %lu.\n"
456 "InnoDB: If you are using big"
457 " BLOB or TEXT rows, you must set the\n"
458 "InnoDB: combined size of log files"
459 " at least 10 times bigger than the\n"
460 "InnoDB: largest such row.\n",
461 (ulong) checkpoint_age,
462 (ulong) log->log_group_capacity);
463 }
464 }
465
466 if (checkpoint_age <= log_max_modified_age_async()) {
467
468 goto function_exit;
469 }
470
472
473 if (!oldest_lsn
474 || lsn - oldest_lsn > log_max_modified_age_async()
475 || checkpoint_age > log_max_checkpoint_age_async()) {
476
477 log->check_flush_or_checkpoint = TRUE;
478 }
479function_exit:
480
481#ifdef UNIV_LOG_DEBUG
482 log_check_log_recs(log->buf + log->old_buf_free,
483 log->buf_free - log->old_buf_free, log->old_lsn);
484#endif
485
486 return(lsn);
487}
488
489#ifdef UNIV_LOG_ARCHIVE
490/******************************************************/
493static
494void
495log_pad_current_log_block(void)
496/*===========================*/
497{
498 byte b = MLOG_DUMMY_RECORD;
499 ulint pad_length;
500 ulint i;
501
503
504 pad_length = OS_FILE_LOG_BLOCK_SIZE
505 - (log_sys->buf_free % OS_FILE_LOG_BLOCK_SIZE)
506 - LOG_BLOCK_TRL_SIZE;
507
508 for (i = 0; i < pad_length; i++) {
509 log_write_low(&b, 1);
510 }
511
512 const ib_uint64_t lsn = log_sys->lsn;
513
514 log_close();
515 log_release();
516
517 ut_a(lsn % OS_FILE_LOG_BLOCK_SIZE == LOG_BLOCK_HDR_SIZE);
518}
519#endif /* UNIV_LOG_ARCHIVE */
520
521/******************************************************/
525UNIV_INTERN
526ulint
528/*===================*/
529 const log_group_t* group)
530{
531 ut_ad(mutex_own(&(log_sys->mutex)));
532
533 return((group->file_size - LOG_FILE_HDR_SIZE) * group->n_files);
534}
535
536/******************************************************/
540UNIV_INLINE
541ulint
542log_group_calc_size_offset(
543/*=======================*/
544 ulint offset,
546 const log_group_t* group)
547{
548 ut_ad(mutex_own(&(log_sys->mutex)));
549
550 return(offset - LOG_FILE_HDR_SIZE * (1 + offset / group->file_size));
551}
552
553/******************************************************/
557UNIV_INLINE
558ulint
559log_group_calc_real_offset(
560/*=======================*/
561 ulint offset,
563 const log_group_t* group)
564{
565 ut_ad(mutex_own(&(log_sys->mutex)));
566
567 return(offset + LOG_FILE_HDR_SIZE
568 * (1 + offset / (group->file_size - LOG_FILE_HDR_SIZE)));
569}
570
571/******************************************************/
574static
575ulint
576log_group_calc_lsn_offset(
577/*======================*/
578 ib_uint64_t lsn,
580 const log_group_t* group)
581{
582 ib_uint64_t gr_lsn;
583 ib_int64_t gr_lsn_size_offset;
584 ib_int64_t difference;
585 ib_int64_t group_size;
586 ib_int64_t offset;
587
588 ut_ad(mutex_own(&(log_sys->mutex)));
589
590 /* If total log file size is > 2 GB we can easily get overflows
591 with 32-bit integers. Use 64-bit integers instead. */
592
593 gr_lsn = group->lsn;
594
595 gr_lsn_size_offset = (ib_int64_t)
596 log_group_calc_size_offset(group->lsn_offset, group);
597
598 group_size = (ib_int64_t) log_group_get_capacity(group);
599
600 if (lsn >= gr_lsn) {
601
602 difference = (ib_int64_t) (lsn - gr_lsn);
603 } else {
604 difference = (ib_int64_t) (gr_lsn - lsn);
605
606 difference = difference % group_size;
607
608 difference = group_size - difference;
609 }
610
611 offset = (gr_lsn_size_offset + difference) % group_size;
612
613 /* Offset must be < 4 GB on 32 bit systems */
614 ut_a((sizeof(ulint) != 4) || (offset < (((ib_int64_t) 1) << 32)));
615
616 /* fprintf(stderr,
617 "Offset is %lu gr_lsn_offset is %lu difference is %lu\n",
618 (ulint)offset,(ulint)gr_lsn_size_offset, (ulint)difference);
619 */
620
621 return(log_group_calc_real_offset((ulint)offset, group));
622}
623#endif /* !UNIV_HOTBACKUP */
624
625#ifdef UNIV_DEBUG
626UNIV_INTERN ibool log_debug_writes = FALSE;
627#endif /* UNIV_DEBUG */
628
629/*******************************************************************/
632UNIV_INTERN
633ulint
635/*==================*/
636 ib_int64_t* log_file_offset,
638 ib_uint64_t first_header_lsn,
640 ib_uint64_t lsn,
642 ulint n_log_files,
644 ib_int64_t log_file_size)
646{
647 ib_int64_t capacity = log_file_size - LOG_FILE_HDR_SIZE;
648 ulint file_no;
649 ib_int64_t add_this_many;
650
651 if (lsn < first_header_lsn) {
652 add_this_many = 1 + (first_header_lsn - lsn)
653 / (capacity * (ib_int64_t)n_log_files);
654 lsn += add_this_many
655 * capacity * (ib_int64_t)n_log_files;
656 }
657
658 ut_a(lsn >= first_header_lsn);
659
660 file_no = ((ulint)((lsn - first_header_lsn) / capacity))
661 % n_log_files;
662 *log_file_offset = (lsn - first_header_lsn) % capacity;
663
664 *log_file_offset = *log_file_offset + LOG_FILE_HDR_SIZE;
665
666 return(file_no);
667}
668
669#ifndef UNIV_HOTBACKUP
670/********************************************************/
674UNIV_INTERN
675void
677/*=================*/
678 log_group_t* group,
679 ib_uint64_t lsn)
681{
682 group->lsn_offset = log_group_calc_lsn_offset(lsn, group);
683 group->lsn = lsn;
684}
685
686/*****************************************************************/
691static
692ibool
693log_calc_max_ages(void)
694/*===================*/
695{
696 log_group_t* group;
697 ulint margin;
698 ulint free;
699 ibool success = TRUE;
700 ulint smallest_capacity;
701 ulint archive_margin;
702 ulint smallest_archive_margin;
703
704 mutex_enter(&(log_sys->mutex));
705
706 group = UT_LIST_GET_FIRST(log_sys->log_groups);
707
708 ut_ad(group);
709
710 smallest_capacity = ULINT_MAX;
711 smallest_archive_margin = ULINT_MAX;
712
713 while (group) {
714 if (log_group_get_capacity(group) < smallest_capacity) {
715
716 smallest_capacity = log_group_get_capacity(group);
717 }
718
719 archive_margin = log_group_get_capacity(group)
720 - (group->file_size - LOG_FILE_HDR_SIZE)
721 - LOG_ARCHIVE_EXTRA_MARGIN;
722
723 if (archive_margin < smallest_archive_margin) {
724
725 smallest_archive_margin = archive_margin;
726 }
727
728 group = UT_LIST_GET_NEXT(log_groups, group);
729 }
730
731 /* Add extra safety */
732 smallest_capacity = smallest_capacity - smallest_capacity / 10;
733
734 /* For each OS thread we must reserve so much free space in the
735 smallest log group that it can accommodate the log entries produced
736 by single query steps: running out of free log space is a serious
737 system error which requires rebooting the database. */
738
739 free = LOG_CHECKPOINT_FREE_PER_THREAD * (10 + srv_thread_concurrency)
740 + LOG_CHECKPOINT_EXTRA_FREE;
741 if (free >= smallest_capacity / 2) {
742 success = FALSE;
743
744 goto failure;
745 } else {
746 margin = smallest_capacity - free;
747 }
748
749 margin = ut_min(margin, log_sys->adm_checkpoint_interval);
750
751 margin = margin - margin / 10; /* Add still some extra safety */
752
753 log_sys->log_group_capacity = smallest_capacity;
754
755 log_sys->max_modified_age_async = margin
756 - margin / LOG_POOL_PREFLUSH_RATIO_ASYNC;
757 log_sys->max_modified_age_sync = margin
758 - margin / LOG_POOL_PREFLUSH_RATIO_SYNC;
759
760 log_sys->max_checkpoint_age_async = margin - margin
761 / LOG_POOL_CHECKPOINT_RATIO_ASYNC;
762 log_sys->max_checkpoint_age = margin;
763
764#ifdef UNIV_LOG_ARCHIVE
765 log_sys->max_archived_lsn_age = smallest_archive_margin;
766
767 log_sys->max_archived_lsn_age_async = smallest_archive_margin
768 - smallest_archive_margin / LOG_ARCHIVE_RATIO_ASYNC;
769#endif /* UNIV_LOG_ARCHIVE */
770failure:
771 mutex_exit(&(log_sys->mutex));
772
773 if (!success) {
774 fprintf(stderr,
775 "InnoDB: Error: ib_logfiles are too small"
776 " for innodb_thread_concurrency %lu.\n"
777 "InnoDB: The combined size of ib_logfiles"
778 " should be bigger than\n"
779 "InnoDB: 200 kB * innodb_thread_concurrency.\n"
780 "InnoDB: To get mysqld to start up, set"
781 " innodb_thread_concurrency in my.cnf\n"
782 "InnoDB: to a lower value, for example, to 8."
783 " After an ERROR-FREE shutdown\n"
784 "InnoDB: of mysqld you can adjust the size of"
785 " ib_logfiles, as explained in\n"
786 "InnoDB: " REFMAN "adding-and-removing.html\n"
787 "InnoDB: Cannot continue operation."
788 " Calling exit(1).\n",
789 (ulong)srv_thread_concurrency);
790
791 exit(1);
792 }
793
794 return(success);
795}
796
797/******************************************************/
799UNIV_INTERN
800void
802/*==========*/
803{
804 log_sys = static_cast<log_t *>(mem_alloc(sizeof(log_t)));
805
806 mutex_create(log_sys_mutex_key, &log_sys->mutex, SYNC_LOG);
807
808 mutex_create(log_flush_order_mutex_key,
809 &log_sys->log_flush_order_mutex,
810 SYNC_LOG_FLUSH_ORDER);
811
812 mutex_enter(&(log_sys->mutex));
813
814 /* Start the lsn from one log block from zero: this way every
815 log record has a start lsn != zero, a fact which we will use */
816
817 log_sys->lsn = LOG_START_LSN;
818
819 ut_a(LOG_BUFFER_SIZE >= 16 * OS_FILE_LOG_BLOCK_SIZE);
820 ut_a(LOG_BUFFER_SIZE >= 4 * UNIV_PAGE_SIZE);
821
822 log_sys->buf_ptr = static_cast<unsigned char *>(mem_alloc(LOG_BUFFER_SIZE + OS_FILE_LOG_BLOCK_SIZE));
823 log_sys->buf = static_cast<unsigned char *>(ut_align(log_sys->buf_ptr, OS_FILE_LOG_BLOCK_SIZE));
824
825 log_sys->buf_size = LOG_BUFFER_SIZE;
826
827 memset(log_sys->buf, '\0', LOG_BUFFER_SIZE);
828
829 log_sys->max_buf_free = log_sys->buf_size / LOG_BUF_FLUSH_RATIO
830 - LOG_BUF_FLUSH_MARGIN;
831 log_sys->check_flush_or_checkpoint = TRUE;
832 UT_LIST_INIT(log_sys->log_groups);
833
834 log_sys->n_log_ios = 0;
835
836 log_sys->n_log_ios_old = log_sys->n_log_ios;
837 log_sys->last_printout_time = time(NULL);
838 /*----------------------------*/
839
840 log_sys->buf_next_to_write = 0;
841
842 log_sys->write_lsn = 0;
843 log_sys->current_flush_lsn = 0;
844 log_sys->flushed_to_disk_lsn = 0;
845
846 log_sys->written_to_some_lsn = log_sys->lsn;
847 log_sys->written_to_all_lsn = log_sys->lsn;
848
849 log_sys->n_pending_writes = 0;
850
851 log_sys->no_flush_event = os_event_create(NULL);
852
853 os_event_set(log_sys->no_flush_event);
854
855 log_sys->one_flushed_event = os_event_create(NULL);
856
857 os_event_set(log_sys->one_flushed_event);
858
859 /*----------------------------*/
860 log_sys->adm_checkpoint_interval = ULINT_MAX;
861
862 log_sys->next_checkpoint_no = 0;
863 log_sys->last_checkpoint_lsn = log_sys->lsn;
864 log_sys->n_pending_checkpoint_writes = 0;
865
866 rw_lock_create(checkpoint_lock_key, &log_sys->checkpoint_lock,
867 SYNC_NO_ORDER_CHECK);
868
869 log_sys->checkpoint_buf_ptr = static_cast<unsigned char *>(mem_alloc(2 * OS_FILE_LOG_BLOCK_SIZE));
870 log_sys->checkpoint_buf = static_cast<unsigned char *>(ut_align(log_sys->checkpoint_buf_ptr,
872 memset(log_sys->checkpoint_buf, '\0', OS_FILE_LOG_BLOCK_SIZE);
873 /*----------------------------*/
874
875#ifdef UNIV_LOG_ARCHIVE
876 /* Under MySQL, log archiving is always off */
877 log_sys->archiving_state = LOG_ARCH_OFF;
878 log_sys->archived_lsn = log_sys->lsn;
879 log_sys->next_archived_lsn = 0;
880
881 log_sys->n_pending_archive_ios = 0;
882
883 rw_lock_create(archive_lock_key, &log_sys->archive_lock,
884 SYNC_NO_ORDER_CHECK);
885
886 log_sys->archive_buf = NULL;
887
888 /* ut_align(
889 ut_malloc(LOG_ARCHIVE_BUF_SIZE
890 + OS_FILE_LOG_BLOCK_SIZE),
891 OS_FILE_LOG_BLOCK_SIZE); */
892 log_sys->archive_buf_size = 0;
893
894 /* memset(log_sys->archive_buf, '\0', LOG_ARCHIVE_BUF_SIZE); */
895
896 log_sys->archiving_on = os_event_create(NULL);
897#endif /* UNIV_LOG_ARCHIVE */
898
899 /*----------------------------*/
900
901 log_block_init(log_sys->buf, log_sys->lsn);
902 log_block_set_first_rec_group(log_sys->buf, LOG_BLOCK_HDR_SIZE);
903
904 log_sys->buf_free = LOG_BLOCK_HDR_SIZE;
905 log_sys->lsn = LOG_START_LSN + LOG_BLOCK_HDR_SIZE;
906
907 mutex_exit(&(log_sys->mutex));
908
909#ifdef UNIV_LOG_DEBUG
912
913 recv_sys->parse_start_lsn = log_sys->lsn;
914 recv_sys->scanned_lsn = log_sys->lsn;
915 recv_sys->scanned_checkpoint_no = 0;
916 recv_sys->recovered_lsn = log_sys->lsn;
917 recv_sys->limit_lsn = IB_ULONGLONG_MAX;
918#endif
919}
920
921/******************************************************************/
923UNIV_INTERN
924void
926/*===========*/
927 ulint id,
928 ulint n_files,
929 ulint file_size,
930 ulint space_id,
933 ulint /*archive_space_id __attribute__((unused))*/)
939{
940 ulint i;
941
942 log_group_t* group;
943
944 group = static_cast<log_group_t *>(mem_alloc(sizeof(log_group_t)));
945
946 group->id = id;
947 group->n_files = n_files;
948 group->file_size = file_size;
949 group->space_id = space_id;
950 group->state = LOG_GROUP_OK;
951 group->lsn = LOG_START_LSN;
952 group->lsn_offset = LOG_FILE_HDR_SIZE;
953 group->n_pending_writes = 0;
954
955 group->file_header_bufs_ptr = static_cast<unsigned char **>(mem_alloc(sizeof(byte*) * n_files));
956 group->file_header_bufs = static_cast<unsigned char **>(mem_alloc(sizeof(byte*) * n_files));
957#ifdef UNIV_LOG_ARCHIVE
958 group->archive_file_header_bufs_ptr = mem_alloc(
959 sizeof(byte*) * n_files);
960 group->archive_file_header_bufs = mem_alloc(sizeof(byte*) * n_files);
961#endif /* UNIV_LOG_ARCHIVE */
962
963 for (i = 0; i < n_files; i++) {
964 group->file_header_bufs_ptr[i] = static_cast<unsigned char *>(mem_alloc(
965 LOG_FILE_HDR_SIZE + OS_FILE_LOG_BLOCK_SIZE));
966
967 group->file_header_bufs[i] = static_cast<unsigned char *>(ut_align(
968 group->file_header_bufs_ptr[i],
970
971 memset(*(group->file_header_bufs + i), '\0',
972 LOG_FILE_HDR_SIZE);
973
974#ifdef UNIV_LOG_ARCHIVE
975 group->archive_file_header_bufs_ptr[i] = mem_alloc(
976 LOG_FILE_HDR_SIZE + OS_FILE_LOG_BLOCK_SIZE);
977
978 group->archive_file_header_bufs[i] = ut_align(
979 group->archive_file_header_bufs_ptr[i],
981
982 memset(*(group->archive_file_header_bufs + i), '\0',
983 LOG_FILE_HDR_SIZE);
984#endif /* UNIV_LOG_ARCHIVE */
985 }
986
987#ifdef UNIV_LOG_ARCHIVE
988 group->archive_space_id = archive_space_id;
989
990 group->archived_file_no = 0;
991 group->archived_offset = 0;
992#endif /* UNIV_LOG_ARCHIVE */
993
994 group->checkpoint_buf_ptr = static_cast<unsigned char *>(mem_alloc(2 * OS_FILE_LOG_BLOCK_SIZE));
995 group->checkpoint_buf = static_cast<unsigned char*>(ut_align(group->checkpoint_buf_ptr,
997
998 memset(group->checkpoint_buf, '\0', OS_FILE_LOG_BLOCK_SIZE);
999
1000 UT_LIST_ADD_LAST(log_groups, log_sys->log_groups, group);
1001
1002 ut_a(log_calc_max_ages());
1003}
1004
1005/******************************************************************/
1007UNIV_INLINE
1008void
1009log_flush_do_unlocks(
1010/*=================*/
1011 ulint code)
1013{
1014 ut_ad(mutex_own(&(log_sys->mutex)));
1015
1016 /* NOTE that we must own the log mutex when doing the setting of the
1017 events: this is because transactions will wait for these events to
1018 be set, and at that moment the log flush they were waiting for must
1019 have ended. If the log mutex were not reserved here, the i/o-thread
1020 calling this function might be preempted for a while, and when it
1021 resumed execution, it might be that a new flush had been started, and
1022 this function would erroneously signal the NEW flush as completed.
1023 Thus, the changes in the state of these events are performed
1024 atomically in conjunction with the changes in the state of
1025 log_sys->n_pending_writes etc. */
1026
1027 if (code & LOG_UNLOCK_NONE_FLUSHED_LOCK) {
1028 os_event_set(log_sys->one_flushed_event);
1029 }
1030
1031 if (code & LOG_UNLOCK_FLUSH_LOCK) {
1032 os_event_set(log_sys->no_flush_event);
1033 }
1034}
1035
1036/******************************************************************/
1040UNIV_INLINE
1041ulint
1042log_group_check_flush_completion(
1043/*=============================*/
1044 log_group_t* group)
1045{
1046 ut_ad(mutex_own(&(log_sys->mutex)));
1047
1048 if (!log_sys->one_flushed && group->n_pending_writes == 0) {
1049#ifdef UNIV_DEBUG
1050 if (log_debug_writes) {
1051 fprintf(stderr,
1052 "Log flushed first to group %lu\n",
1053 (ulong) group->id);
1054 }
1055#endif /* UNIV_DEBUG */
1056 log_sys->written_to_some_lsn = log_sys->write_lsn;
1057 log_sys->one_flushed = TRUE;
1058
1059 return(LOG_UNLOCK_NONE_FLUSHED_LOCK);
1060 }
1061
1062#ifdef UNIV_DEBUG
1063 if (log_debug_writes && (group->n_pending_writes == 0)) {
1064
1065 fprintf(stderr, "Log flushed to group %lu\n",
1066 (ulong) group->id);
1067 }
1068#endif /* UNIV_DEBUG */
1069 return(0);
1070}
1071
1072/******************************************************/
1075static
1076ulint
1077log_sys_check_flush_completion(void)
1078/*================================*/
1079{
1080 ulint move_start;
1081 ulint move_end;
1082
1083 ut_ad(mutex_own(&(log_sys->mutex)));
1084
1085 if (log_sys->n_pending_writes == 0) {
1086
1087 log_sys->written_to_all_lsn = log_sys->write_lsn;
1088 log_sys->buf_next_to_write = log_sys->write_end_offset;
1089
1090 if (log_sys->write_end_offset > log_sys->max_buf_free / 2) {
1091 /* Move the log buffer content to the start of the
1092 buffer */
1093
1094 move_start = ut_calc_align_down(
1095 log_sys->write_end_offset,
1097 move_end = ut_calc_align(log_sys->buf_free,
1099
1100 ut_memmove(log_sys->buf, log_sys->buf + move_start,
1101 move_end - move_start);
1102 log_sys->buf_free -= move_start;
1103
1104 log_sys->buf_next_to_write -= move_start;
1105 }
1106
1107 return(LOG_UNLOCK_FLUSH_LOCK);
1108 }
1109
1110 return(0);
1111}
1112
1113/******************************************************/
1115UNIV_INTERN
1116void
1118/*============*/
1119 log_group_t* group)
1120{
1121 ulint unlock;
1122
1123#ifdef UNIV_LOG_ARCHIVE
1124 if ((byte*)group == &log_archive_io) {
1125 /* It was an archive write */
1126
1127 log_io_complete_archive();
1128
1129 return;
1130 }
1131#endif /* UNIV_LOG_ARCHIVE */
1132
1133 if ((ulint)group & 0x1UL) {
1134 /* It was a checkpoint write */
1135 group = (log_group_t*)((ulint)group - 1);
1136
1137 if (srv_unix_file_flush_method != SRV_UNIX_O_DSYNC
1138 && srv_unix_file_flush_method != SRV_UNIX_ALL_O_DIRECT
1139 && srv_unix_file_flush_method != SRV_UNIX_NOSYNC) {
1140
1141 fil_flush(group->space_id);
1142 }
1143
1144#ifdef UNIV_DEBUG
1145 if (log_debug_writes) {
1146 fprintf(stderr,
1147 "Checkpoint info written to group %lu\n",
1148 group->id);
1149 }
1150#endif /* UNIV_DEBUG */
1151 log_io_complete_checkpoint();
1152
1153 return;
1154 }
1155
1156 ut_error;
1158
1159 if (srv_unix_file_flush_method != SRV_UNIX_O_DSYNC
1160 && srv_unix_file_flush_method != SRV_UNIX_ALL_O_DIRECT
1161 && srv_unix_file_flush_method != SRV_UNIX_NOSYNC
1162 && srv_flush_log_at_trx_commit != 2) {
1163
1164 fil_flush(group->space_id);
1165 }
1166
1167 mutex_enter(&(log_sys->mutex));
1168 ut_ad(!recv_no_log_write);
1169
1170 ut_a(group->n_pending_writes > 0);
1171 ut_a(log_sys->n_pending_writes > 0);
1172
1173 group->n_pending_writes--;
1174 log_sys->n_pending_writes--;
1175
1176 unlock = log_group_check_flush_completion(group);
1177 unlock = unlock | log_sys_check_flush_completion();
1178
1179 log_flush_do_unlocks(unlock);
1180
1181 mutex_exit(&(log_sys->mutex));
1182}
1183
1184/******************************************************/
1186static
1187void
1188log_group_file_header_flush(
1189/*========================*/
1190 log_group_t* group,
1191 ulint nth_file,
1193 ib_uint64_t start_lsn)
1195{
1196 byte* buf;
1197 ulint dest_offset;
1198
1199 ut_ad(mutex_own(&(log_sys->mutex)));
1200 ut_ad(!recv_no_log_write);
1201 ut_a(nth_file < group->n_files);
1202
1203 buf = *(group->file_header_bufs + nth_file);
1204
1205 mach_write_to_4(buf + LOG_GROUP_ID, group->id);
1206 mach_write_to_8(buf + LOG_FILE_START_LSN, start_lsn);
1207
1208 /* Wipe over possible label of ibbackup --restore */
1209 memcpy(buf + LOG_FILE_WAS_CREATED_BY_HOT_BACKUP, " ", 4);
1210
1211 mach_write_to_4(buf + LOG_FILE_OS_FILE_LOG_BLOCK_SIZE,
1212 srv_log_block_size);
1213
1214 dest_offset = nth_file * group->file_size;
1215
1216#ifdef UNIV_DEBUG
1217 if (log_debug_writes) {
1218 fprintf(stderr,
1219 "Writing log file header to group %lu file %lu\n",
1220 (ulong) group->id, (ulong) nth_file);
1221 }
1222#endif /* UNIV_DEBUG */
1223 if (log_do_write) {
1224 log_sys->n_log_ios++;
1225
1226 srv_os_log_pending_writes++;
1227
1228 fil_io(OS_FILE_WRITE | OS_FILE_LOG, TRUE, group->space_id, 0,
1229 dest_offset / UNIV_PAGE_SIZE,
1230 dest_offset % UNIV_PAGE_SIZE,
1232 buf, group);
1233
1234 srv_os_log_pending_writes--;
1235 }
1236}
1237
1238/******************************************************/
1242static
1243void
1244log_block_store_checksum(
1245/*=====================*/
1246 byte* block)
1247{
1249}
1250
1251/******************************************************/
1253UNIV_INTERN
1254void
1256/*================*/
1257 log_group_t* group,
1258 byte* buf,
1259 ulint len,
1261 ib_uint64_t start_lsn,
1264 ulint new_data_offset)
1268{
1269 ulint write_len;
1270 ibool write_header;
1271 ulint next_offset;
1272 ulint i;
1273
1274 ut_ad(mutex_own(&(log_sys->mutex)));
1275 ut_ad(!recv_no_log_write);
1276 ut_a(len % OS_FILE_LOG_BLOCK_SIZE == 0);
1277 ut_a(((ulint) start_lsn) % OS_FILE_LOG_BLOCK_SIZE == 0);
1278
1279 if (new_data_offset == 0) {
1280 write_header = TRUE;
1281 } else {
1282 write_header = FALSE;
1283 }
1284loop:
1285 if (len == 0) {
1286
1287 return;
1288 }
1289
1290 next_offset = log_group_calc_lsn_offset(start_lsn, group);
1291
1292 if ((next_offset % group->file_size == LOG_FILE_HDR_SIZE)
1293 && write_header) {
1294 /* We start to write a new log file instance in the group */
1295
1296 log_group_file_header_flush(group,
1297 next_offset / group->file_size,
1298 start_lsn);
1299 srv_os_log_written+= OS_FILE_LOG_BLOCK_SIZE;
1300 srv_log_writes++;
1301 }
1302
1303 if ((next_offset % group->file_size) + len > group->file_size) {
1304
1305 write_len = group->file_size
1306 - (next_offset % group->file_size);
1307 } else {
1308 write_len = len;
1309 }
1310
1311#ifdef UNIV_DEBUG
1312 if (log_debug_writes) {
1313
1314 fprintf(stderr,
1315 "Writing log file segment to group %lu"
1316 " offset %lu len %lu\n"
1317 "start lsn %" PRIu64 "\n"
1318 "First block n:o %lu last block n:o %lu\n",
1319 (ulong) group->id, (ulong) next_offset,
1320 (ulong) write_len,
1321 start_lsn,
1322 (ulong) log_block_get_hdr_no(buf),
1323 (ulong) log_block_get_hdr_no(
1324 buf + write_len - OS_FILE_LOG_BLOCK_SIZE));
1326 == log_block_convert_lsn_to_no(start_lsn));
1327
1328 for (i = 0; i < write_len / OS_FILE_LOG_BLOCK_SIZE; i++) {
1329
1330 ut_a(log_block_get_hdr_no(buf) + i
1332 buf + i * OS_FILE_LOG_BLOCK_SIZE));
1333 }
1334 }
1335#endif /* UNIV_DEBUG */
1336 /* Calculate the checksums for each log block and write them to
1337 the trailer fields of the log blocks */
1338
1339 for (i = 0; i < write_len / OS_FILE_LOG_BLOCK_SIZE; i++) {
1340 log_block_store_checksum(buf + i * OS_FILE_LOG_BLOCK_SIZE);
1341 }
1342
1343 if (log_do_write) {
1344 log_sys->n_log_ios++;
1345
1346 srv_os_log_pending_writes++;
1347
1348 fil_io(OS_FILE_WRITE | OS_FILE_LOG, TRUE, group->space_id, 0,
1349 next_offset / UNIV_PAGE_SIZE,
1350 next_offset % UNIV_PAGE_SIZE, write_len, buf, group);
1351
1352 srv_os_log_pending_writes--;
1353
1354 srv_os_log_written+= write_len;
1355 srv_log_writes++;
1356 }
1357
1358 if (write_len < len) {
1359 start_lsn += write_len;
1360 len -= write_len;
1361 buf += write_len;
1362
1363 write_header = TRUE;
1364
1365 goto loop;
1366 }
1367}
1368
1369/******************************************************/
1374UNIV_INTERN
1375void
1377/*============*/
1378 ib_uint64_t lsn,
1381 ulint wait,
1383 ibool flush_to_disk)
1386{
1387 log_group_t* group;
1388 ulint start_offset;
1389 ulint end_offset;
1390 ulint area_start;
1391 ulint area_end;
1392#ifdef UNIV_DEBUG
1393 ulint loop_count = 0;
1394#endif /* UNIV_DEBUG */
1395 ulint unlock;
1396
1397 if (recv_no_ibuf_operations || srv_fake_write) {
1398 /* Recovery is running and no operations on the log files are
1399 allowed yet (the variable name .._no_ibuf_.. is misleading) */
1400
1401 return;
1402 }
1403
1404loop:
1405#ifdef UNIV_DEBUG
1406 loop_count++;
1407
1408 ut_ad(loop_count < 5);
1409
1410# if 0
1411 if (loop_count > 2) {
1412 fprintf(stderr, "Log loop count %lu\n", loop_count);
1413 }
1414# endif
1415#endif
1416
1417 mutex_enter(&(log_sys->mutex));
1418 ut_ad(!recv_no_log_write);
1419
1420 if (flush_to_disk
1421 && log_sys->flushed_to_disk_lsn >= lsn) {
1422
1423 mutex_exit(&(log_sys->mutex));
1424
1425 return;
1426 }
1427
1428 if (!flush_to_disk
1429 && (log_sys->written_to_all_lsn >= lsn
1430 || (log_sys->written_to_some_lsn >= lsn
1431 && wait != LOG_WAIT_ALL_GROUPS))) {
1432
1433 mutex_exit(&(log_sys->mutex));
1434
1435 return;
1436 }
1437
1438 if (log_sys->n_pending_writes > 0) {
1439 /* A write (+ possibly flush to disk) is running */
1440
1441 if (flush_to_disk
1442 && log_sys->current_flush_lsn >= lsn) {
1443 /* The write + flush will write enough: wait for it to
1444 complete */
1445
1446 goto do_waits;
1447 }
1448
1449 if (!flush_to_disk
1450 && log_sys->write_lsn >= lsn) {
1451 /* The write will write enough: wait for it to
1452 complete */
1453
1454 goto do_waits;
1455 }
1456
1457 mutex_exit(&(log_sys->mutex));
1458
1459 /* Wait for the write to complete and try to start a new
1460 write */
1461
1462 os_event_wait(log_sys->no_flush_event);
1463
1464 goto loop;
1465 }
1466
1467 if (!flush_to_disk
1468 && log_sys->buf_free == log_sys->buf_next_to_write) {
1469 /* Nothing to write and no flush to disk requested */
1470
1471 mutex_exit(&(log_sys->mutex));
1472
1473 return;
1474 }
1475
1476#ifdef UNIV_DEBUG
1477 if (log_debug_writes) {
1478 fprintf(stderr,
1479 "Writing log from %" PRIu64 " up to lsn %" PRIu64 "\n",
1480 log_sys->written_to_all_lsn,
1481 log_sys->lsn);
1482 }
1483#endif /* UNIV_DEBUG */
1484 log_sys->n_pending_writes++;
1485
1486 group = UT_LIST_GET_FIRST(log_sys->log_groups);
1487 group->n_pending_writes++;
1489
1490 os_event_reset(log_sys->no_flush_event);
1491 os_event_reset(log_sys->one_flushed_event);
1492
1493 start_offset = log_sys->buf_next_to_write;
1494 end_offset = log_sys->buf_free;
1495
1496 area_start = ut_calc_align_down(start_offset, OS_FILE_LOG_BLOCK_SIZE);
1497 area_end = ut_calc_align(end_offset, OS_FILE_LOG_BLOCK_SIZE);
1498
1499 ut_ad(area_end - area_start > 0);
1500
1501 log_sys->write_lsn = log_sys->lsn;
1502
1503 if (flush_to_disk) {
1504 log_sys->current_flush_lsn = log_sys->lsn;
1505 }
1506
1507 log_sys->one_flushed = FALSE;
1508
1509 log_block_set_flush_bit(log_sys->buf + area_start, TRUE);
1510 log_block_set_checkpoint_no(
1511 log_sys->buf + area_end - OS_FILE_LOG_BLOCK_SIZE,
1512 log_sys->next_checkpoint_no);
1513
1514 /* Copy the last, incompletely written, log block a log block length
1515 up, so that when the flush operation writes from the log buffer, the
1516 segment to write will not be changed by writers to the log */
1517
1518 ut_memcpy(log_sys->buf + area_end,
1519 log_sys->buf + area_end - OS_FILE_LOG_BLOCK_SIZE,
1521
1522 log_sys->buf_free += OS_FILE_LOG_BLOCK_SIZE;
1523 log_sys->write_end_offset = log_sys->buf_free;
1524
1525 group = UT_LIST_GET_FIRST(log_sys->log_groups);
1526
1527 /* Do the write to the log files */
1528
1529 while (group) {
1531 group, log_sys->buf + area_start,
1532 area_end - area_start,
1533 ut_uint64_align_down(log_sys->written_to_all_lsn,
1535 start_offset - area_start);
1536
1537 log_group_set_fields(group, log_sys->write_lsn);
1538
1539 group = UT_LIST_GET_NEXT(log_groups, group);
1540 }
1541
1542 mutex_exit(&(log_sys->mutex));
1543
1544 if (srv_unix_file_flush_method == SRV_UNIX_O_DSYNC
1545 || srv_unix_file_flush_method == SRV_UNIX_ALL_O_DIRECT) {
1546 /* O_DSYNC means the OS did not buffer the log file at all:
1547 so we have also flushed to disk what we have written */
1548
1549 log_sys->flushed_to_disk_lsn = log_sys->write_lsn;
1550
1551 } else if (flush_to_disk) {
1552
1553 group = UT_LIST_GET_FIRST(log_sys->log_groups);
1554
1555 fil_flush(group->space_id);
1556 log_sys->flushed_to_disk_lsn = log_sys->write_lsn;
1557 }
1558
1559 mutex_enter(&(log_sys->mutex));
1560
1561 group = UT_LIST_GET_FIRST(log_sys->log_groups);
1562
1563 ut_a(group->n_pending_writes == 1);
1564 ut_a(log_sys->n_pending_writes == 1);
1565
1566 group->n_pending_writes--;
1567 log_sys->n_pending_writes--;
1568
1569 unlock = log_group_check_flush_completion(group);
1570 unlock = unlock | log_sys_check_flush_completion();
1571
1572 log_flush_do_unlocks(unlock);
1573
1574 mutex_exit(&(log_sys->mutex));
1575
1576 return;
1577
1578do_waits:
1579 mutex_exit(&(log_sys->mutex));
1580
1581 switch (wait) {
1582 case LOG_WAIT_ONE_GROUP:
1583 os_event_wait(log_sys->one_flushed_event);
1584 break;
1585 case LOG_WAIT_ALL_GROUPS:
1586 os_event_wait(log_sys->no_flush_event);
1587 break;
1588#ifdef UNIV_DEBUG
1589 case LOG_NO_WAIT:
1590 break;
1591 default:
1592 ut_error;
1593#endif /* UNIV_DEBUG */
1594 }
1595}
1596
1597/****************************************************************/
1599UNIV_INTERN
1600void
1602/*==========================*/
1603{
1604 ib_uint64_t lsn;
1605
1606 mutex_enter(&(log_sys->mutex));
1607
1608 lsn = log_sys->lsn;
1609
1610 mutex_exit(&(log_sys->mutex));
1611
1612 log_write_up_to(lsn, LOG_WAIT_ALL_GROUPS, TRUE);
1613}
1614
1615/****************************************************************/
1620UNIV_INTERN
1621void
1623/*==========================*/
1624 ibool flush)
1625{
1626 ib_uint64_t lsn;
1627
1628 mutex_enter(&(log_sys->mutex));
1629
1630 lsn = log_sys->lsn;
1631
1632 mutex_exit(&(log_sys->mutex));
1633
1634 log_write_up_to(lsn, LOG_NO_WAIT, flush);
1635}
1636
1637/********************************************************************
1638
1639Tries to establish a big enough margin of free space in the log buffer, such
1640that a new log entry can be catenated without an immediate need for a flush. */
1641static
1642void
1643log_flush_margin(void)
1644/*==================*/
1645{
1646 log_t* log = log_sys;
1647 ib_uint64_t lsn = 0;
1648
1649 mutex_enter(&(log->mutex));
1650
1651 if (log->buf_free > log->max_buf_free) {
1652
1653 if (log->n_pending_writes > 0) {
1654 /* A flush is running: hope that it will provide enough
1655 free space */
1656 } else {
1657 lsn = log->lsn;
1658 }
1659 }
1660
1661 mutex_exit(&(log->mutex));
1662
1663 if (lsn) {
1664 log_write_up_to(lsn, LOG_NO_WAIT, FALSE);
1665 }
1666}
1667
1668/****************************************************************/
1674UNIV_INTERN
1675ibool
1677/*=============================*/
1678 ib_uint64_t new_oldest,
1681 ibool sync)
1683{
1684 ulint n_pages;
1685
1686 if (recv_recovery_on) {
1687 /* If the recovery is running, we must first apply all
1688 log records to their respective file pages to get the
1689 right modify lsn values to these pages: otherwise, there
1690 might be pages on disk which are not yet recovered to the
1691 current lsn, and even after calling this function, we could
1692 not know how up-to-date the disk version of the database is,
1693 and we could not make a new checkpoint on the basis of the
1694 info on the buffer pool only. */
1695
1697 }
1698
1699 n_pages = buf_flush_list(ULINT_MAX, new_oldest);
1700
1701 if (sync) {
1702 buf_flush_wait_batch_end(NULL, BUF_FLUSH_LIST);
1703 }
1704
1705 if (n_pages == ULINT_UNDEFINED) {
1706
1707 return(FALSE);
1708 }
1709
1710 return(TRUE);
1711}
1712
1713/******************************************************/
1715static
1716void
1717log_complete_checkpoint(void)
1718/*=========================*/
1719{
1720 ut_ad(mutex_own(&(log_sys->mutex)));
1721 ut_ad(log_sys->n_pending_checkpoint_writes == 0);
1722
1723 log_sys->next_checkpoint_no++;
1724
1725 log_sys->last_checkpoint_lsn = log_sys->next_checkpoint_lsn;
1726
1727 rw_lock_x_unlock_gen(&(log_sys->checkpoint_lock), LOG_CHECKPOINT);
1728}
1729
1730/******************************************************/
1732static
1733void
1734log_io_complete_checkpoint(void)
1735/*============================*/
1736{
1737 mutex_enter(&(log_sys->mutex));
1738
1739 ut_ad(log_sys->n_pending_checkpoint_writes > 0);
1740
1741 log_sys->n_pending_checkpoint_writes--;
1742
1743 if (log_sys->n_pending_checkpoint_writes == 0) {
1744 log_complete_checkpoint();
1745 }
1746
1747 mutex_exit(&(log_sys->mutex));
1748}
1749
1750/*******************************************************************/
1752static
1753void
1754log_checkpoint_set_nth_group_info(
1755/*==============================*/
1756 byte* buf,
1757 ulint n,
1758 ulint file_no,
1759 ulint offset)
1760{
1762
1763 mach_write_to_4(buf + LOG_CHECKPOINT_GROUP_ARRAY
1764 + 8 * n + LOG_CHECKPOINT_ARCHIVED_FILE_NO, file_no);
1765 mach_write_to_4(buf + LOG_CHECKPOINT_GROUP_ARRAY
1766 + 8 * n + LOG_CHECKPOINT_ARCHIVED_OFFSET, offset);
1767}
1768
1769/*******************************************************************/
1771UNIV_INTERN
1772void
1774/*==============================*/
1775 const byte* buf,
1776 ulint n,
1777 ulint* file_no,
1778 ulint* offset)
1779{
1781
1782 *file_no = mach_read_from_4(buf + LOG_CHECKPOINT_GROUP_ARRAY
1783 + 8 * n + LOG_CHECKPOINT_ARCHIVED_FILE_NO);
1784 *offset = mach_read_from_4(buf + LOG_CHECKPOINT_GROUP_ARRAY
1785 + 8 * n + LOG_CHECKPOINT_ARCHIVED_OFFSET);
1786}
1787
1788/******************************************************/
1790static
1791void
1792log_group_checkpoint(
1793/*=================*/
1794 log_group_t* group)
1795{
1796 log_group_t* group2;
1797#ifdef UNIV_LOG_ARCHIVE
1798 ib_uint64_t archived_lsn;
1799 ib_uint64_t next_archived_lsn;
1800#endif /* UNIV_LOG_ARCHIVE */
1801 ulint write_offset;
1802 ulint fold;
1803 byte* buf;
1804 ulint i;
1805
1806 ut_ad(mutex_own(&(log_sys->mutex)));
1807 ut_a(LOG_CHECKPOINT_SIZE <= OS_FILE_LOG_BLOCK_SIZE);
1808
1809 buf = group->checkpoint_buf;
1810
1811 mach_write_to_8(buf + LOG_CHECKPOINT_NO, log_sys->next_checkpoint_no);
1812 mach_write_to_8(buf + LOG_CHECKPOINT_LSN, log_sys->next_checkpoint_lsn);
1813
1814 mach_write_to_4(buf + LOG_CHECKPOINT_OFFSET,
1815 log_group_calc_lsn_offset(
1816 log_sys->next_checkpoint_lsn, group));
1817
1818 mach_write_to_4(buf + LOG_CHECKPOINT_LOG_BUF_SIZE, log_sys->buf_size);
1819
1820#ifdef UNIV_LOG_ARCHIVE
1821 if (log_sys->archiving_state == LOG_ARCH_OFF) {
1822 archived_lsn = IB_ULONGLONG_MAX;
1823 } else {
1824 archived_lsn = log_sys->archived_lsn;
1825
1826 if (archived_lsn != log_sys->next_archived_lsn) {
1827 next_archived_lsn = log_sys->next_archived_lsn;
1828 /* For debugging only */
1829 }
1830 }
1831
1832 mach_write_to_8(buf + LOG_CHECKPOINT_ARCHIVED_LSN, archived_lsn);
1833#else /* UNIV_LOG_ARCHIVE */
1834 mach_write_to_8(buf + LOG_CHECKPOINT_ARCHIVED_LSN,
1835 (ib_uint64_t)log_group_calc_lsn_offset(
1836 log_sys->next_checkpoint_lsn, group));
1837
1838#endif /* UNIV_LOG_ARCHIVE */
1839
1840 for (i = 0; i < LOG_MAX_N_GROUPS; i++) {
1841 log_checkpoint_set_nth_group_info(buf, i, 0, 0);
1842 }
1843
1844 group2 = UT_LIST_GET_FIRST(log_sys->log_groups);
1845
1846 while (group2) {
1847 log_checkpoint_set_nth_group_info(buf, group2->id,
1848#ifdef UNIV_LOG_ARCHIVE
1849 group2->archived_file_no,
1850 group2->archived_offset
1851#else /* UNIV_LOG_ARCHIVE */
1852 0, 0
1853#endif /* UNIV_LOG_ARCHIVE */
1854 );
1855
1856 group2 = UT_LIST_GET_NEXT(log_groups, group2);
1857 }
1858
1859 fold = ut_fold_binary(buf, LOG_CHECKPOINT_CHECKSUM_1);
1860 mach_write_to_4(buf + LOG_CHECKPOINT_CHECKSUM_1, fold);
1861
1862 fold = ut_fold_binary(buf + LOG_CHECKPOINT_LSN,
1863 LOG_CHECKPOINT_CHECKSUM_2 - LOG_CHECKPOINT_LSN);
1864 mach_write_to_4(buf + LOG_CHECKPOINT_CHECKSUM_2, fold);
1865
1866 /* Starting from InnoDB-3.23.50, we also write info on allocated
1867 size in the tablespace */
1868
1869 mach_write_to_4(buf + LOG_CHECKPOINT_FSP_FREE_LIMIT,
1870 log_fsp_current_free_limit);
1871
1872 mach_write_to_4(buf + LOG_CHECKPOINT_FSP_MAGIC_N,
1873 LOG_CHECKPOINT_FSP_MAGIC_N_VAL);
1874
1875 /* We alternate the physical place of the checkpoint info in the first
1876 log file */
1877
1878 if ((log_sys->next_checkpoint_no & 1) == 0) {
1879 write_offset = LOG_CHECKPOINT_1;
1880 } else {
1881 write_offset = LOG_CHECKPOINT_2;
1882 }
1883
1884 if (log_do_write) {
1885 if (log_sys->n_pending_checkpoint_writes == 0) {
1886
1887 rw_lock_x_lock_gen(&(log_sys->checkpoint_lock),
1888 LOG_CHECKPOINT);
1889 }
1890
1891 log_sys->n_pending_checkpoint_writes++;
1892
1893 log_sys->n_log_ios++;
1894
1895 /* We send as the last parameter the group machine address
1896 added with 1, as we want to distinguish between a normal log
1897 file write and a checkpoint field write */
1898
1899 fil_io(OS_FILE_WRITE | OS_FILE_LOG, FALSE, group->space_id, 0,
1900 write_offset / UNIV_PAGE_SIZE,
1901 write_offset % UNIV_PAGE_SIZE,
1903 buf, ((byte*)group + 1));
1904
1905 ut_ad(((ulint)group & 0x1UL) == 0);
1906 }
1907}
1908#endif /* !UNIV_HOTBACKUP */
1909
1910#ifdef UNIV_HOTBACKUP
1911/******************************************************/
1914UNIV_INTERN
1915void
1916log_reset_first_header_and_checkpoint(
1917/*==================================*/
1918 byte* hdr_buf,
1920 ib_uint64_t start)
1923{
1924 ulint fold;
1925 byte* buf;
1926 ib_uint64_t lsn;
1927
1928 mach_write_to_4(hdr_buf + LOG_GROUP_ID, 0);
1929 mach_write_to_8(hdr_buf + LOG_FILE_START_LSN, start);
1930
1931 lsn = start + LOG_BLOCK_HDR_SIZE;
1932
1933 /* Write the label of ibbackup --restore */
1934 strcpy((char*) hdr_buf + LOG_FILE_WAS_CREATED_BY_HOT_BACKUP,
1935 "ibbackup ");
1936 ut_sprintf_timestamp((char*) hdr_buf
1937 + (LOG_FILE_WAS_CREATED_BY_HOT_BACKUP
1938 + (sizeof "ibbackup ") - 1));
1939 buf = hdr_buf + LOG_CHECKPOINT_1;
1940
1941 mach_write_to_8(buf + LOG_CHECKPOINT_NO, 0);
1942 mach_write_to_8(buf + LOG_CHECKPOINT_LSN, lsn);
1943
1944 mach_write_to_4(buf + LOG_CHECKPOINT_OFFSET,
1945 LOG_FILE_HDR_SIZE + LOG_BLOCK_HDR_SIZE);
1946
1947 mach_write_to_4(buf + LOG_CHECKPOINT_LOG_BUF_SIZE, 2 * 1024 * 1024);
1948
1949 mach_write_to_8(buf + LOG_CHECKPOINT_ARCHIVED_LSN, IB_ULONGLONG_MAX);
1950
1951 fold = ut_fold_binary(buf, LOG_CHECKPOINT_CHECKSUM_1);
1952 mach_write_to_4(buf + LOG_CHECKPOINT_CHECKSUM_1, fold);
1953
1954 fold = ut_fold_binary(buf + LOG_CHECKPOINT_LSN,
1955 LOG_CHECKPOINT_CHECKSUM_2 - LOG_CHECKPOINT_LSN);
1956 mach_write_to_4(buf + LOG_CHECKPOINT_CHECKSUM_2, fold);
1957
1958 /* Starting from InnoDB-3.23.50, we should also write info on
1959 allocated size in the tablespace, but unfortunately we do not
1960 know it here */
1961}
1962#endif /* UNIV_HOTBACKUP */
1963
1964#ifndef UNIV_HOTBACKUP
1965/******************************************************/
1967UNIV_INTERN
1968void
1970/*===========================*/
1971 log_group_t* group,
1972 ulint field)
1973{
1974 ut_ad(mutex_own(&(log_sys->mutex)));
1975
1976 log_sys->n_log_ios++;
1977
1978 fil_io(OS_FILE_READ | OS_FILE_LOG, TRUE, group->space_id, 0,
1979 field / UNIV_PAGE_SIZE, field % UNIV_PAGE_SIZE,
1980 OS_FILE_LOG_BLOCK_SIZE, log_sys->checkpoint_buf, NULL);
1981}
1982
1983/******************************************************/
1985UNIV_INTERN
1986void
1988/*==================================*/
1989{
1990 log_group_t* group;
1991
1992 ut_ad(mutex_own(&(log_sys->mutex)));
1993
1994 group = UT_LIST_GET_FIRST(log_sys->log_groups);
1995
1996 while (group) {
1997 log_group_checkpoint(group);
1998
1999 group = UT_LIST_GET_NEXT(log_groups, group);
2000 }
2001}
2002
2003/******************************************************/
2009UNIV_INTERN
2010ibool
2012/*===========*/
2013 ibool sync,
2015 ibool write_always)
2021{
2022 ib_uint64_t oldest_lsn;
2023
2024 if (recv_recovery_is_on()) {
2026 }
2027
2028 if (srv_unix_file_flush_method != SRV_UNIX_NOSYNC) {
2029 fil_flush_file_spaces(FIL_TABLESPACE);
2030 }
2031
2032 mutex_enter(&(log_sys->mutex));
2033
2034 ut_ad(!recv_no_log_write);
2035 oldest_lsn = log_buf_pool_get_oldest_modification();
2036
2037 mutex_exit(&(log_sys->mutex));
2038
2039 /* Because log also contains headers and dummy log records,
2040 if the buffer pool contains no dirty buffers, oldest_lsn
2041 gets the value log_sys->lsn from the previous function,
2042 and we must make sure that the log is flushed up to that
2043 lsn. If there are dirty buffers in the buffer pool, then our
2044 write-ahead-logging algorithm ensures that the log has been flushed
2045 up to oldest_lsn. */
2046
2047 log_write_up_to(oldest_lsn, LOG_WAIT_ALL_GROUPS, TRUE);
2048
2049 mutex_enter(&(log_sys->mutex));
2050
2051 if (!write_always
2052 && log_sys->last_checkpoint_lsn >= oldest_lsn) {
2053
2054 mutex_exit(&(log_sys->mutex));
2055
2056 return(TRUE);
2057 }
2058
2059 ut_ad(log_sys->flushed_to_disk_lsn >= oldest_lsn);
2060
2061 if (log_sys->n_pending_checkpoint_writes > 0) {
2062 /* A checkpoint write is running */
2063
2064 mutex_exit(&(log_sys->mutex));
2065
2066 if (sync) {
2067 /* Wait for the checkpoint write to complete */
2068 rw_lock_s_lock(&(log_sys->checkpoint_lock));
2069 rw_lock_s_unlock(&(log_sys->checkpoint_lock));
2070 }
2071
2072 return(FALSE);
2073 }
2074
2075 log_sys->next_checkpoint_lsn = oldest_lsn;
2076
2077#ifdef UNIV_DEBUG
2078 if (log_debug_writes) {
2079 fprintf(stderr, "Making checkpoint no %lu at lsn %" PRIu64 "\n",
2080 (ulong) log_sys->next_checkpoint_no,
2081 oldest_lsn);
2082 }
2083#endif /* UNIV_DEBUG */
2084
2086
2087 mutex_exit(&(log_sys->mutex));
2088
2089 if (sync) {
2090 /* Wait for the checkpoint write to complete */
2091 rw_lock_s_lock(&(log_sys->checkpoint_lock));
2092 rw_lock_s_unlock(&(log_sys->checkpoint_lock));
2093 }
2094
2095 return(TRUE);
2096}
2097
2098/****************************************************************/
2100UNIV_INTERN
2101void
2103/*===================*/
2104 ib_uint64_t lsn,
2107 ibool write_always)
2114{
2115 /* Preflush pages synchronously */
2116
2117 while (!log_preflush_pool_modified_pages(lsn, TRUE)) {}
2118
2119 while (!log_checkpoint(TRUE, write_always)) {}
2120}
2121
2122/****************************************************************/
2127static
2128void
2129log_checkpoint_margin(void)
2130/*=======================*/
2131{
2132 log_t* log = log_sys;
2133 ib_uint64_t age;
2134 ib_uint64_t checkpoint_age;
2135 ib_uint64_t advance;
2136 ib_uint64_t oldest_lsn;
2137 ibool sync;
2138 ibool checkpoint_sync;
2139 ibool do_checkpoint;
2140 ibool success;
2141loop:
2142 sync = FALSE;
2143 checkpoint_sync = FALSE;
2144 do_checkpoint = FALSE;
2145
2146 mutex_enter(&(log->mutex));
2147 ut_ad(!recv_no_log_write);
2148
2149 if (log->check_flush_or_checkpoint == FALSE) {
2150 mutex_exit(&(log->mutex));
2151
2152 return;
2153 }
2154
2155 oldest_lsn = log_buf_pool_get_oldest_modification();
2156
2157 age = log->lsn - oldest_lsn;
2158
2159 if (age > log->max_modified_age_sync) {
2160
2161 /* A flush is urgent: we have to do a synchronous preflush */
2162
2163 sync = TRUE;
2164 advance = 2 * (age - log->max_modified_age_sync);
2165 } else if (age > log_max_modified_age_async()) {
2166
2167 /* A flush is not urgent: we do an asynchronous preflush */
2168 advance = age - log_max_modified_age_async();
2169 } else {
2170 advance = 0;
2171 }
2172
2173 checkpoint_age = log->lsn - log->last_checkpoint_lsn;
2174
2175 if (checkpoint_age > log->max_checkpoint_age) {
2176 /* A checkpoint is urgent: we do it synchronously */
2177
2178 checkpoint_sync = TRUE;
2179
2180 do_checkpoint = TRUE;
2181
2182 } else if (checkpoint_age > log_max_checkpoint_age_async()) {
2183 /* A checkpoint is not urgent: do it asynchronously */
2184
2185 do_checkpoint = TRUE;
2186
2187 log->check_flush_or_checkpoint = FALSE;
2188 } else {
2189 log->check_flush_or_checkpoint = FALSE;
2190 }
2191
2192 mutex_exit(&(log->mutex));
2193
2194 if (advance) {
2195 ib_uint64_t new_oldest = oldest_lsn + advance;
2196
2197 success = log_preflush_pool_modified_pages(new_oldest, sync);
2198
2199 /* If the flush succeeded, this thread has done its part
2200 and can proceed. If it did not succeed, there was another
2201 thread doing a flush at the same time. If sync was FALSE,
2202 the flush was not urgent, and we let this thread proceed.
2203 Otherwise, we let it start from the beginning again. */
2204
2205 if (sync && !success) {
2206 mutex_enter(&(log->mutex));
2207
2208 log->check_flush_or_checkpoint = TRUE;
2209
2210 mutex_exit(&(log->mutex));
2211 goto loop;
2212 }
2213 }
2214
2215 if (do_checkpoint) {
2216 log_checkpoint(checkpoint_sync, FALSE);
2217
2218 if (checkpoint_sync) {
2219
2220 goto loop;
2221 }
2222 }
2223}
2224
2225/******************************************************/
2227UNIV_INTERN
2228void
2230/*===================*/
2231 ulint type,
2232 byte* buf,
2233 log_group_t* group,
2234 ib_uint64_t start_lsn,
2235 ib_uint64_t end_lsn)
2236{
2237 ulint len;
2238 ulint source_offset;
2239 ibool sync;
2240
2241 ut_ad(mutex_own(&(log_sys->mutex)));
2242
2243 sync = (type == LOG_RECOVER);
2244loop:
2245 source_offset = log_group_calc_lsn_offset(start_lsn, group);
2246
2247 len = (ulint) (end_lsn - start_lsn);
2248
2249 ut_ad(len != 0);
2250
2251 if ((source_offset % group->file_size) + len > group->file_size) {
2252
2253 len = group->file_size - (source_offset % group->file_size);
2254 }
2255
2256#ifdef UNIV_LOG_ARCHIVE
2257 if (type == LOG_ARCHIVE) {
2258
2259 log_sys->n_pending_archive_ios++;
2260 }
2261#endif /* UNIV_LOG_ARCHIVE */
2262
2263 log_sys->n_log_ios++;
2264
2265 fil_io(OS_FILE_READ | OS_FILE_LOG, sync, group->space_id, 0,
2266 source_offset / UNIV_PAGE_SIZE, source_offset % UNIV_PAGE_SIZE,
2267 len, buf, NULL);
2268
2269 start_lsn += len;
2270 buf += len;
2271
2272 if (start_lsn != end_lsn) {
2273
2274 goto loop;
2275 }
2276}
2277
2278#ifdef UNIV_LOG_ARCHIVE
2279/******************************************************/
2281UNIV_INTERN
2282void
2284/*=======================*/
2285 char* buf,
2286 ulint /*id __attribute__((unused))*/,
2289 ulint file_no)
2290{
2291 sprintf(buf, "%sib_arch_log_%010lu", srv_arch_dir, (ulong) file_no);
2292}
2293
2294/******************************************************/
2296static
2297void
2298log_group_archive_file_header_write(
2299/*================================*/
2300 log_group_t* group,
2301 ulint nth_file,
2303 ulint file_no,
2304 ib_uint64_t start_lsn)
2306{
2307 byte* buf;
2308 ulint dest_offset;
2309
2310 ut_ad(mutex_own(&(log_sys->mutex)));
2311
2312 ut_a(nth_file < group->n_files);
2313
2314 buf = *(group->archive_file_header_bufs + nth_file);
2315
2316 mach_write_to_4(buf + LOG_GROUP_ID, group->id);
2317 mach_write_to_8(buf + LOG_FILE_START_LSN, start_lsn);
2318 mach_write_to_4(buf + LOG_FILE_NO, file_no);
2319
2320 mach_write_to_4(buf + LOG_FILE_ARCH_COMPLETED, FALSE);
2321
2322 dest_offset = nth_file * group->file_size;
2323
2324 log_sys->n_log_ios++;
2325
2326 fil_io(OS_FILE_WRITE | OS_FILE_LOG, TRUE, group->archive_space_id,
2327 dest_offset / UNIV_PAGE_SIZE,
2328 dest_offset % UNIV_PAGE_SIZE,
2330 buf, &log_archive_io);
2331}
2332
2333/******************************************************/
2335static
2336void
2337log_group_archive_completed_header_write(
2338/*=====================================*/
2339 log_group_t* group,
2340 ulint nth_file,
2342 ib_uint64_t end_lsn)
2343{
2344 byte* buf;
2345 ulint dest_offset;
2346
2347 ut_ad(mutex_own(&(log_sys->mutex)));
2348 ut_a(nth_file < group->n_files);
2349
2350 buf = *(group->archive_file_header_bufs + nth_file);
2351
2352 mach_write_to_4(buf + LOG_FILE_ARCH_COMPLETED, TRUE);
2353 mach_write_to_8(buf + LOG_FILE_END_LSN, end_lsn);
2354
2355 dest_offset = nth_file * group->file_size + LOG_FILE_ARCH_COMPLETED;
2356
2357 log_sys->n_log_ios++;
2358
2359 fil_io(OS_FILE_WRITE | OS_FILE_LOG, TRUE, group->archive_space_id,
2360 dest_offset / UNIV_PAGE_SIZE,
2361 dest_offset % UNIV_PAGE_SIZE,
2363 buf + LOG_FILE_ARCH_COMPLETED,
2364 &log_archive_io);
2365}
2366
2367/******************************************************/
2369static
2370void
2371log_group_archive(
2372/*==============*/
2373 log_group_t* group)
2374{
2375 os_file_t file_handle;
2376 ib_uint64_t start_lsn;
2377 ib_uint64_t end_lsn;
2378 char name[1024];
2379 byte* buf;
2380 ulint len;
2381 ibool ret;
2382 ulint next_offset;
2383 ulint n_files;
2384 ulint open_mode;
2385
2386 ut_ad(mutex_own(&(log_sys->mutex)));
2387
2388 start_lsn = log_sys->archived_lsn;
2389
2390 ut_a(start_lsn % OS_FILE_LOG_BLOCK_SIZE == 0);
2391
2392 end_lsn = log_sys->next_archived_lsn;
2393
2394 ut_a(end_lsn % OS_FILE_LOG_BLOCK_SIZE == 0);
2395
2396 buf = log_sys->archive_buf;
2397
2398 n_files = 0;
2399
2400 next_offset = group->archived_offset;
2401loop:
2402 if ((next_offset % group->file_size == 0)
2403 || (fil_space_get_size(group->archive_space_id) == 0)) {
2404
2405 /* Add the file to the archive file space; create or open the
2406 file */
2407
2408 if (next_offset % group->file_size == 0) {
2409 open_mode = OS_FILE_CREATE;
2410 } else {
2411 open_mode = OS_FILE_OPEN;
2412 }
2413
2414 log_archived_file_name_gen(name, group->id,
2415 group->archived_file_no + n_files);
2416
2417 file_handle = os_file_create(innodb_file_log_key,
2418 name, open_mode,
2419 OS_FILE_AIO,
2420 OS_DATA_FILE, &ret);
2421
2422 if (!ret && (open_mode == OS_FILE_CREATE)) {
2423 file_handle = os_file_create(
2424 innodb_file_log_key, name, OS_FILE_OPEN,
2425 OS_FILE_AIO, OS_DATA_FILE, &ret);
2426 }
2427
2428 if (!ret) {
2429 fprintf(stderr,
2430 "InnoDB: Cannot create or open"
2431 " archive log file %s.\n"
2432 "InnoDB: Cannot continue operation.\n"
2433 "InnoDB: Check that the log archive"
2434 " directory exists,\n"
2435 "InnoDB: you have access rights to it, and\n"
2436 "InnoDB: there is space available.\n", name);
2437 exit(1);
2438 }
2439
2440#ifdef UNIV_DEBUG
2441 if (log_debug_writes) {
2442 fprintf(stderr, "Created archive file %s\n", name);
2443 }
2444#endif /* UNIV_DEBUG */
2445
2446 ret = os_file_close(file_handle);
2447
2448 ut_a(ret);
2449
2450 /* Add the archive file as a node to the space */
2451
2452 fil_node_create(name, group->file_size / UNIV_PAGE_SIZE,
2453 group->archive_space_id, FALSE);
2454
2455 if (next_offset % group->file_size == 0) {
2456 log_group_archive_file_header_write(
2457 group, n_files,
2458 group->archived_file_no + n_files,
2459 start_lsn);
2460
2461 next_offset += LOG_FILE_HDR_SIZE;
2462 }
2463 }
2464
2465 len = end_lsn - start_lsn;
2466
2467 if (group->file_size < (next_offset % group->file_size) + len) {
2468
2469 len = group->file_size - (next_offset % group->file_size);
2470 }
2471
2472#ifdef UNIV_DEBUG
2473 if (log_debug_writes) {
2474 fprintf(stderr,
2475 "Archiving starting at lsn %" PRIu64 ", len %lu"
2476 " to group %lu\n",
2477 start_lsn,
2478 (ulong) len, (ulong) group->id);
2479 }
2480#endif /* UNIV_DEBUG */
2481
2482 log_sys->n_pending_archive_ios++;
2483
2484 log_sys->n_log_ios++;
2485
2486 fil_io(OS_FILE_WRITE | OS_FILE_LOG, FALSE, group->archive_space_id,
2487 next_offset / UNIV_PAGE_SIZE, next_offset % UNIV_PAGE_SIZE,
2489 &log_archive_io);
2490
2491 start_lsn += len;
2492 next_offset += len;
2493 buf += len;
2494
2495 if (next_offset % group->file_size == 0) {
2496 n_files++;
2497 }
2498
2499 if (end_lsn != start_lsn) {
2500
2501 goto loop;
2502 }
2503
2504 group->next_archived_file_no = group->archived_file_no + n_files;
2505 group->next_archived_offset = next_offset % group->file_size;
2506
2507 ut_a(group->next_archived_offset % OS_FILE_LOG_BLOCK_SIZE == 0);
2508}
2509
2510/*****************************************************/
2513static
2514void
2515log_archive_groups(void)
2516/*====================*/
2517{
2518 log_group_t* group;
2519
2520 ut_ad(mutex_own(&(log_sys->mutex)));
2521
2522 group = UT_LIST_GET_FIRST(log_sys->log_groups);
2523
2524 log_group_archive(group);
2525}
2526
2527/*****************************************************/
2530static
2531void
2532log_archive_write_complete_groups(void)
2533/*===================================*/
2534{
2535 log_group_t* group;
2536 ulint end_offset;
2537 ulint trunc_files;
2538 ulint n_files;
2539 ib_uint64_t start_lsn;
2540 ib_uint64_t end_lsn;
2541 ulint i;
2542
2543 ut_ad(mutex_own(&(log_sys->mutex)));
2544
2545 group = UT_LIST_GET_FIRST(log_sys->log_groups);
2546
2547 group->archived_file_no = group->next_archived_file_no;
2548 group->archived_offset = group->next_archived_offset;
2549
2550 /* Truncate from the archive file space all but the last
2551 file, or if it has been written full, all files */
2552
2553 n_files = (UNIV_PAGE_SIZE
2554 * fil_space_get_size(group->archive_space_id))
2555 / group->file_size;
2556 ut_ad(n_files > 0);
2557
2558 end_offset = group->archived_offset;
2559
2560 if (end_offset % group->file_size == 0) {
2561
2562 trunc_files = n_files;
2563 } else {
2564 trunc_files = n_files - 1;
2565 }
2566
2567#ifdef UNIV_DEBUG
2568 if (log_debug_writes && trunc_files) {
2569 fprintf(stderr,
2570 "Complete file(s) archived to group %lu\n",
2571 (ulong) group->id);
2572 }
2573#endif /* UNIV_DEBUG */
2574
2575 /* Calculate the archive file space start lsn */
2576 start_lsn = log_sys->next_archived_lsn
2577 - (end_offset - LOG_FILE_HDR_SIZE + trunc_files
2578 * (group->file_size - LOG_FILE_HDR_SIZE));
2579 end_lsn = start_lsn;
2580
2581 for (i = 0; i < trunc_files; i++) {
2582
2583 end_lsn += group->file_size - LOG_FILE_HDR_SIZE;
2584
2585 /* Write a notice to the headers of archived log
2586 files that the file write has been completed */
2587
2588 log_group_archive_completed_header_write(group, i, end_lsn);
2589 }
2590
2591 fil_space_truncate_start(group->archive_space_id,
2592 trunc_files * group->file_size);
2593
2594#ifdef UNIV_DEBUG
2595 if (log_debug_writes) {
2596 fputs("Archiving writes completed\n", stderr);
2597 }
2598#endif /* UNIV_DEBUG */
2599}
2600
2601/******************************************************/
2603static
2604void
2605log_archive_check_completion_low(void)
2606/*==================================*/
2607{
2608 ut_ad(mutex_own(&(log_sys->mutex)));
2609
2610 if (log_sys->n_pending_archive_ios == 0
2611 && log_sys->archiving_phase == LOG_ARCHIVE_READ) {
2612
2613#ifdef UNIV_DEBUG
2614 if (log_debug_writes) {
2615 fputs("Archiving read completed\n", stderr);
2616 }
2617#endif /* UNIV_DEBUG */
2618
2619 /* Archive buffer has now been read in: start archive writes */
2620
2621 log_sys->archiving_phase = LOG_ARCHIVE_WRITE;
2622
2623 log_archive_groups();
2624 }
2625
2626 if (log_sys->n_pending_archive_ios == 0
2627 && log_sys->archiving_phase == LOG_ARCHIVE_WRITE) {
2628
2629 log_archive_write_complete_groups();
2630
2631 log_sys->archived_lsn = log_sys->next_archived_lsn;
2632
2633 rw_lock_x_unlock_gen(&(log_sys->archive_lock), LOG_ARCHIVE);
2634 }
2635}
2636
2637/******************************************************/
2639static
2640void
2641log_io_complete_archive(void)
2642/*=========================*/
2643{
2644 log_group_t* group;
2645
2646 mutex_enter(&(log_sys->mutex));
2647
2648 group = UT_LIST_GET_FIRST(log_sys->log_groups);
2649
2650 mutex_exit(&(log_sys->mutex));
2651
2652 fil_flush(group->archive_space_id);
2653
2654 mutex_enter(&(log_sys->mutex));
2655
2656 ut_ad(log_sys->n_pending_archive_ios > 0);
2657
2658 log_sys->n_pending_archive_ios--;
2659
2660 log_archive_check_completion_low();
2661
2662 mutex_exit(&(log_sys->mutex));
2663}
2664
2665/********************************************************************/
2668UNIV_INTERN
2669ibool
2671/*===========*/
2672 ibool sync,
2673 ulint* n_bytes)
2675{
2676 ibool calc_new_limit;
2677 ib_uint64_t start_lsn;
2678 ib_uint64_t limit_lsn;
2679
2680 calc_new_limit = TRUE;
2681loop:
2682 mutex_enter(&(log_sys->mutex));
2683
2684 switch (log_sys->archiving_state) {
2685 case LOG_ARCH_OFF:
2686arch_none:
2687 mutex_exit(&(log_sys->mutex));
2688
2689 *n_bytes = 0;
2690
2691 return(TRUE);
2692 case LOG_ARCH_STOPPED:
2693 case LOG_ARCH_STOPPING2:
2694 mutex_exit(&(log_sys->mutex));
2695
2696 os_event_wait(log_sys->archiving_on);
2697
2698 goto loop;
2699 }
2700
2701 start_lsn = log_sys->archived_lsn;
2702
2703 if (calc_new_limit) {
2704 ut_a(log_sys->archive_buf_size % OS_FILE_LOG_BLOCK_SIZE == 0);
2705 limit_lsn = start_lsn + log_sys->archive_buf_size;
2706
2707 *n_bytes = log_sys->archive_buf_size;
2708
2709 if (limit_lsn >= log_sys->lsn) {
2710
2711 limit_lsn = ut_uint64_align_down(
2712 log_sys->lsn, OS_FILE_LOG_BLOCK_SIZE);
2713 }
2714 }
2715
2716 if (log_sys->archived_lsn >= limit_lsn) {
2717
2718 goto arch_none;
2719 }
2720
2721 if (log_sys->written_to_all_lsn < limit_lsn) {
2722
2723 mutex_exit(&(log_sys->mutex));
2724
2725 log_write_up_to(limit_lsn, LOG_WAIT_ALL_GROUPS, TRUE);
2726
2727 calc_new_limit = FALSE;
2728
2729 goto loop;
2730 }
2731
2732 if (log_sys->n_pending_archive_ios > 0) {
2733 /* An archiving operation is running */
2734
2735 mutex_exit(&(log_sys->mutex));
2736
2737 if (sync) {
2738 rw_lock_s_lock(&(log_sys->archive_lock));
2739 rw_lock_s_unlock(&(log_sys->archive_lock));
2740 }
2741
2742 *n_bytes = log_sys->archive_buf_size;
2743
2744 return(FALSE);
2745 }
2746
2747 rw_lock_x_lock_gen(&(log_sys->archive_lock), LOG_ARCHIVE);
2748
2749 log_sys->archiving_phase = LOG_ARCHIVE_READ;
2750
2751 log_sys->next_archived_lsn = limit_lsn;
2752
2753#ifdef UNIV_DEBUG
2754 if (log_debug_writes) {
2755 fprintf(stderr,
2756 "Archiving from lsn %" PRIu64 " to lsn %" PRIu64 "\n",
2757 log_sys->archived_lsn, limit_lsn);
2758 }
2759#endif /* UNIV_DEBUG */
2760
2761 /* Read the log segment to the archive buffer */
2762
2763 log_group_read_log_seg(LOG_ARCHIVE, log_sys->archive_buf,
2764 UT_LIST_GET_FIRST(log_sys->log_groups),
2765 start_lsn, limit_lsn);
2766
2767 mutex_exit(&(log_sys->mutex));
2768
2769 if (sync) {
2770 rw_lock_s_lock(&(log_sys->archive_lock));
2771 rw_lock_s_unlock(&(log_sys->archive_lock));
2772 }
2773
2774 *n_bytes = log_sys->archive_buf_size;
2775
2776 return(TRUE);
2777}
2778
2779/****************************************************************/
2782static
2783void
2784log_archive_all(void)
2785/*=================*/
2786{
2787 ib_uint64_t present_lsn;
2788 ulint dummy;
2789
2790 mutex_enter(&(log_sys->mutex));
2791
2792 if (log_sys->archiving_state == LOG_ARCH_OFF) {
2793 mutex_exit(&(log_sys->mutex));
2794
2795 return;
2796 }
2797
2798 present_lsn = log_sys->lsn;
2799
2800 mutex_exit(&(log_sys->mutex));
2801
2802 log_pad_current_log_block();
2803
2804 for (;;) {
2805 mutex_enter(&(log_sys->mutex));
2806
2807 if (present_lsn <= log_sys->archived_lsn) {
2808
2809 mutex_exit(&(log_sys->mutex));
2810
2811 return;
2812 }
2813
2814 mutex_exit(&(log_sys->mutex));
2815
2816 log_archive_do(TRUE, &dummy);
2817 }
2818}
2819
2820/*****************************************************/
2823static
2824void
2825log_archive_close_groups(
2826/*=====================*/
2827 ibool increment_file_count)
2829{
2830 log_group_t* group;
2831 ulint trunc_len;
2832
2833 ut_ad(mutex_own(&(log_sys->mutex)));
2834
2835 if (log_sys->archiving_state == LOG_ARCH_OFF) {
2836
2837 return;
2838 }
2839
2840 group = UT_LIST_GET_FIRST(log_sys->log_groups);
2841
2842 trunc_len = UNIV_PAGE_SIZE
2843 * fil_space_get_size(group->archive_space_id);
2844 if (trunc_len > 0) {
2845 ut_a(trunc_len == group->file_size);
2846
2847 /* Write a notice to the headers of archived log
2848 files that the file write has been completed */
2849
2850 log_group_archive_completed_header_write(
2851 group, 0, log_sys->archived_lsn);
2852
2853 fil_space_truncate_start(group->archive_space_id,
2854 trunc_len);
2855 if (increment_file_count) {
2856 group->archived_offset = 0;
2857 group->archived_file_no += 2;
2858 }
2859
2860#ifdef UNIV_DEBUG
2861 if (log_debug_writes) {
2862 fprintf(stderr,
2863 "Incrementing arch file no to %lu"
2864 " in log group %lu\n",
2865 (ulong) group->archived_file_no + 2,
2866 (ulong) group->id);
2867 }
2868#endif /* UNIV_DEBUG */
2869 }
2870}
2871
2872/****************************************************************/
2878UNIV_INTERN
2879ulint
2880log_archive_stop(void)
2881/*==================*/
2882{
2883 ibool success;
2884
2885 mutex_enter(&(log_sys->mutex));
2886
2887 if (log_sys->archiving_state != LOG_ARCH_ON) {
2888
2889 mutex_exit(&(log_sys->mutex));
2890
2891 return(DB_ERROR);
2892 }
2893
2894 log_sys->archiving_state = LOG_ARCH_STOPPING;
2895
2896 mutex_exit(&(log_sys->mutex));
2897
2898 log_archive_all();
2899
2900 mutex_enter(&(log_sys->mutex));
2901
2902 log_sys->archiving_state = LOG_ARCH_STOPPING2;
2903 os_event_reset(log_sys->archiving_on);
2904
2905 mutex_exit(&(log_sys->mutex));
2906
2907 /* Wait for a possible archiving operation to end */
2908
2909 rw_lock_s_lock(&(log_sys->archive_lock));
2910 rw_lock_s_unlock(&(log_sys->archive_lock));
2911
2912 mutex_enter(&(log_sys->mutex));
2913
2914 /* Close all archived log files, incrementing the file count by 2,
2915 if appropriate */
2916
2917 log_archive_close_groups(TRUE);
2918
2919 mutex_exit(&(log_sys->mutex));
2920
2921 /* Make a checkpoint, so that if recovery is needed, the file numbers
2922 of new archived log files will start from the right value */
2923
2924 success = FALSE;
2925
2926 while (!success) {
2927 success = log_checkpoint(TRUE, TRUE);
2928 }
2929
2930 mutex_enter(&(log_sys->mutex));
2931
2932 log_sys->archiving_state = LOG_ARCH_STOPPED;
2933
2934 mutex_exit(&(log_sys->mutex));
2935
2936 return(DB_SUCCESS);
2937}
2938
2939/****************************************************************/
2942UNIV_INTERN
2943ulint
2945/*===================*/
2946{
2947 mutex_enter(&(log_sys->mutex));
2948
2949 if (log_sys->archiving_state != LOG_ARCH_STOPPED) {
2950
2951 mutex_exit(&(log_sys->mutex));
2952
2953 return(DB_ERROR);
2954 }
2955
2956 log_sys->archiving_state = LOG_ARCH_ON;
2957
2958 os_event_set(log_sys->archiving_on);
2959
2960 mutex_exit(&(log_sys->mutex));
2961
2962 return(DB_SUCCESS);
2963}
2964
2965/****************************************************************/
2968UNIV_INTERN
2969ulint
2971/*==========================*/
2972{
2973loop:
2974 mutex_enter(&(log_sys->mutex));
2975
2976 if (log_sys->archiving_state == LOG_ARCH_STOPPED
2977 || log_sys->archiving_state == LOG_ARCH_OFF) {
2978
2979 log_sys->archiving_state = LOG_ARCH_OFF;
2980
2981 os_event_set(log_sys->archiving_on);
2982
2983 mutex_exit(&(log_sys->mutex));
2984
2985 return(DB_SUCCESS);
2986 }
2987
2988 mutex_exit(&(log_sys->mutex));
2989
2991
2992 os_thread_sleep(500000);
2993
2994 goto loop;
2995}
2996
2997/****************************************************************/
3000UNIV_INTERN
3001ulint
3003/*========================*/
3004{
3005 mutex_enter(&(log_sys->mutex));
3006
3007 if (log_sys->archiving_state == LOG_ARCH_OFF) {
3008
3009 log_sys->archiving_state = LOG_ARCH_ON;
3010
3011 log_sys->archived_lsn
3012 = ut_uint64_align_down(log_sys->lsn,
3014 mutex_exit(&(log_sys->mutex));
3015
3016 return(DB_SUCCESS);
3017 }
3018
3019 mutex_exit(&(log_sys->mutex));
3020
3021 return(DB_ERROR);
3022}
3023
3024/****************************************************************/
3028static
3029void
3030log_archive_margin(void)
3031/*====================*/
3032{
3033 log_t* log = log_sys;
3034 ulint age;
3035 ibool sync;
3036 ulint dummy;
3037loop:
3038 mutex_enter(&(log->mutex));
3039
3040 if (log->archiving_state == LOG_ARCH_OFF) {
3041 mutex_exit(&(log->mutex));
3042
3043 return;
3044 }
3045
3046 age = log->lsn - log->archived_lsn;
3047
3048 if (age > log->max_archived_lsn_age) {
3049
3050 /* An archiving is urgent: we have to do synchronous i/o */
3051
3052 sync = TRUE;
3053
3054 } else if (age > log->max_archived_lsn_age_async) {
3055
3056 /* An archiving is not urgent: we do asynchronous i/o */
3057
3058 sync = FALSE;
3059 } else {
3060 /* No archiving required yet */
3061
3062 mutex_exit(&(log->mutex));
3063
3064 return;
3065 }
3066
3067 mutex_exit(&(log->mutex));
3068
3069 log_archive_do(sync, &dummy);
3070
3071 if (sync == TRUE) {
3072 /* Check again that enough was written to the archive */
3073
3074 goto loop;
3075 }
3076}
3077#endif /* UNIV_LOG_ARCHIVE */
3078
3079/********************************************************************/
3084UNIV_INTERN
3085void
3087/*===================*/
3088{
3089loop:
3090 log_flush_margin();
3091
3092 log_checkpoint_margin();
3093
3094#ifdef UNIV_LOG_ARCHIVE
3095 log_archive_margin();
3096#endif /* UNIV_LOG_ARCHIVE */
3097
3098 mutex_enter(&(log_sys->mutex));
3099 ut_ad(!recv_no_log_write);
3100
3101 if (log_sys->check_flush_or_checkpoint) {
3102
3103 mutex_exit(&(log_sys->mutex));
3104
3105 goto loop;
3106 }
3107
3108 mutex_exit(&(log_sys->mutex));
3109}
3110
3111/****************************************************************/
3116UNIV_INTERN
3117void
3119/*=======================================*/
3120{
3121 ib_uint64_t lsn;
3122 ulint arch_log_no;
3123
3124 if (srv_print_verbose_log) {
3125 ut_print_timestamp(stderr);
3126 fprintf(stderr, " InnoDB: Starting shutdown...\n");
3127 }
3128 /* Wait until the master thread and all other operations are idle: our
3129 algorithm only works if the server is idle at shutdown */
3130
3132loop:
3133 os_thread_sleep(100000);
3134
3135 mutex_enter(&kernel_mutex);
3136
3137 /* We need the monitor threads to stop before we proceed with a
3138 normal shutdown. In case of very fast shutdown, however, we can
3139 proceed without waiting for monitor threads. */
3140
3141 if (srv_fast_shutdown < 2
3142 && (srv_error_monitor_active
3143 || srv_lock_timeout_active
3144 || srv_monitor_active)) {
3145
3146 mutex_exit(&kernel_mutex);
3147
3148 os_event_set(srv_error_event);
3149 os_event_set(srv_monitor_event);
3150 os_event_set(srv_timeout_event);
3151
3152 goto loop;
3153 }
3154
3155 /* Check that there are no longer transactions. We need this wait even
3156 for the 'very fast' shutdown, because the InnoDB layer may have
3157 committed or prepared transactions and we don't want to lose them. */
3158
3159 if (! srv_apply_log_only) {
3161 || UT_LIST_GET_LEN(trx_sys->trx_list) > 0) {
3162
3163 mutex_exit(&kernel_mutex);
3164
3165 goto loop;
3166 }
3167 }
3168
3169 if (srv_fast_shutdown == 2) {
3170 /* In this fastest shutdown we do not flush the buffer pool:
3171 it is essentially a 'crash' of the InnoDB server. Make sure
3172 that the log is all flushed to disk, so that we can recover
3173 all committed transactions in a crash recovery. We must not
3174 write the lsn stamps to the data files, since at a startup
3175 InnoDB deduces from the stamps if the previous shutdown was
3176 clean. */
3177
3179
3180 mutex_exit(&kernel_mutex);
3181
3182 return; /* We SKIP ALL THE REST !! */
3183 }
3184
3185 mutex_exit(&kernel_mutex);
3186
3187 /* Check that the background threads are suspended */
3188
3190 goto loop;
3191 }
3192
3193 mutex_enter(&(log_sys->mutex));
3194
3195 if (log_sys->n_pending_checkpoint_writes
3196#ifdef UNIV_LOG_ARCHIVE
3197 || log_sys->n_pending_archive_ios
3198#endif /* UNIV_LOG_ARCHIVE */
3199 || log_sys->n_pending_writes) {
3200
3201 mutex_exit(&(log_sys->mutex));
3202
3203 goto loop;
3204 }
3205
3206 mutex_exit(&(log_sys->mutex));
3207
3209
3210 goto loop;
3211 }
3212
3213#ifdef UNIV_LOG_ARCHIVE
3214 log_archive_all();
3215#endif /* UNIV_LOG_ARCHIVE */
3216
3217 log_make_checkpoint_at(IB_ULONGLONG_MAX, TRUE);
3218
3219 mutex_enter(&(log_sys->mutex));
3220
3221 lsn = log_sys->lsn;
3222
3223 if (lsn != log_sys->last_checkpoint_lsn
3224#ifdef UNIV_LOG_ARCHIVE
3225 || (srv_log_archive_on
3226 && lsn != log_sys->archived_lsn + LOG_BLOCK_HDR_SIZE)
3227#endif /* UNIV_LOG_ARCHIVE */
3228 ) {
3229
3230 mutex_exit(&(log_sys->mutex));
3231
3232 goto loop;
3233 }
3234
3235 arch_log_no = 0;
3236
3237#ifdef UNIV_LOG_ARCHIVE
3238 UT_LIST_GET_FIRST(log_sys->log_groups)->archived_file_no;
3239
3240 if (0 == UT_LIST_GET_FIRST(log_sys->log_groups)->archived_offset) {
3241
3242 arch_log_no--;
3243 }
3244
3245 log_archive_close_groups(TRUE);
3246#endif /* UNIV_LOG_ARCHIVE */
3247
3248 mutex_exit(&(log_sys->mutex));
3249
3250 /* Check that the background threads stay suspended */
3252 fprintf(stderr,
3253 "InnoDB: Warning: some background thread woke up"
3254 " during shutdown\n");
3255
3256 goto loop;
3257 }
3258
3259 fil_flush_file_spaces(FIL_TABLESPACE);
3260 fil_flush_file_spaces(FIL_LOG);
3261
3262 /* The call fil_write_flushed_lsn_to_data_files() will pass the buffer
3263 pool: therefore it is essential that the buffer pool has been
3264 completely flushed to disk! (We do not call fil_write... if the
3265 'very fast' shutdown is enabled.) */
3266
3267 if (!buf_all_freed()) {
3268
3269 goto loop;
3270 }
3271
3273
3274 /* Make some checks that the server really is quiet */
3276
3278 ut_a(lsn == log_sys->lsn);
3279
3280 if (lsn < srv_start_lsn) {
3281 drizzled::errmsg_printf(drizzled::error::ERROR,
3282 "InnoDB: Error: log sequence number at shutdown %" PRIu64 " is lower than at startup %" PRIu64 "!",
3283 lsn, srv_start_lsn);
3284 }
3285
3286 srv_shutdown_lsn = lsn;
3287
3288 fil_write_flushed_lsn_to_data_files(lsn, arch_log_no);
3289
3290 fil_flush_file_spaces(FIL_TABLESPACE);
3291
3293
3294 /* Make some checks that the server really is quiet */
3296
3298 ut_a(lsn == log_sys->lsn);
3299}
3300
3301#ifdef UNIV_LOG_DEBUG
3302/******************************************************/
3305UNIV_INTERN
3306ibool
3307log_check_log_recs(
3308/*===============*/
3309 const byte* buf,
3312 ulint len,
3313 ib_uint64_t buf_start_lsn)
3314{
3315 ib_uint64_t contiguous_lsn;
3316 ib_uint64_t scanned_lsn;
3317 const byte* start;
3318 const byte* end;
3319 byte* buf1;
3320 byte* scan_buf;
3321
3322 ut_ad(mutex_own(&(log_sys->mutex)));
3323
3324 if (len == 0) {
3325
3326 return(TRUE);
3327 }
3328
3330 end = ut_align(buf + len, OS_FILE_LOG_BLOCK_SIZE);
3331
3332 buf1 = mem_alloc((end - start) + OS_FILE_LOG_BLOCK_SIZE);
3333 scan_buf = ut_align(buf1, OS_FILE_LOG_BLOCK_SIZE);
3334
3335 ut_memcpy(scan_buf, start, end - start);
3336
3339 * UNIV_PAGE_SIZE, FALSE, scan_buf, end - start,
3340 ut_uint64_align_down(buf_start_lsn,
3342 &contiguous_lsn, &scanned_lsn);
3343
3344 ut_a(scanned_lsn == buf_start_lsn + len);
3345 ut_a(recv_sys->recovered_lsn == scanned_lsn);
3346
3347 mem_free(buf1);
3348
3349 return(TRUE);
3350}
3351#endif /* UNIV_LOG_DEBUG */
3352
3353/******************************************************/
3356UNIV_INTERN
3357ibool
3359/*=========*/
3360 ib_uint64_t* lsn)
3361{
3362 if (0 == mutex_enter_nowait(&(log_sys->mutex))) {
3363 *lsn = log_sys->lsn;
3364
3365 mutex_exit(&(log_sys->mutex));
3366
3367 return(TRUE);
3368 }
3369
3370 return(FALSE);
3371}
3372
3373/******************************************************/
3375UNIV_INTERN
3376void
3378/*======*/
3379 FILE* file)
3380{
3381 double time_elapsed;
3382 time_t current_time;
3383
3384 mutex_enter(&(log_sys->mutex));
3385
3386 fprintf(file,
3387 "Log sequence number %" PRIu64 "\n"
3388 "Log flushed up to %" PRIu64 "\n"
3389 "Last checkpoint at %" PRIu64 "\n",
3390 log_sys->lsn,
3391 log_sys->flushed_to_disk_lsn,
3392 log_sys->last_checkpoint_lsn);
3393
3394 fprintf(file,
3395 "Max checkpoint age %lu\n"
3396 "Checkpoint age target %lu\n"
3397 "Modified age %" PRIu64 "\n"
3398 "Checkpoint age %" PRIu64 "\n",
3399 log_sys->max_checkpoint_age,
3400 log_max_checkpoint_age_async(),
3401 log_sys->lsn - log_buf_pool_get_oldest_modification(),
3402 log_sys->lsn - log_sys->last_checkpoint_lsn);
3403
3404 current_time = time(NULL);
3405
3406 time_elapsed = 0.001 + difftime(current_time,
3407 log_sys->last_printout_time);
3408 fprintf(file,
3409 "%lu pending log writes, %lu pending chkp writes\n"
3410 "%lu log i/o's done, %.2f log i/o's/second\n",
3411 (ulong) log_sys->n_pending_writes,
3412 (ulong) log_sys->n_pending_checkpoint_writes,
3413 (ulong) log_sys->n_log_ios,
3414 ((log_sys->n_log_ios - log_sys->n_log_ios_old)
3415 / time_elapsed));
3416
3417 log_sys->n_log_ios_old = log_sys->n_log_ios;
3418 log_sys->last_printout_time = current_time;
3419
3420 mutex_exit(&(log_sys->mutex));
3421}
3422
3423/**********************************************************************/
3425UNIV_INTERN
3426void
3428/*===================*/
3429{
3430 log_sys->n_log_ios_old = log_sys->n_log_ios;
3431 log_sys->last_printout_time = time(NULL);
3432}
3433
3434/**********************************************************************
3435Closes a log group. */
3436static
3437void
3438log_group_close(
3439/*===========*/
3440 log_group_t* group) /* in,own: log group to close */
3441{
3442 ulint i;
3443
3444 for (i = 0; i < group->n_files; i++) {
3445 mem_free(group->file_header_bufs_ptr[i]);
3446#ifdef UNIV_LOG_ARCHIVE
3447 mem_free(group->archive_file_header_bufs_ptr[i]);
3448#endif /* UNIV_LOG_ARCHIVE */
3449 }
3450
3452 mem_free(group->file_header_bufs);
3453
3454#ifdef UNIV_LOG_ARCHIVE
3455 mem_free(group->archive_file_header_bufs_ptr);
3456 mem_free(group->archive_file_header_bufs);
3457#endif /* UNIV_LOG_ARCHIVE */
3458
3460
3461 mem_free(group);
3462}
3463
3464/**********************************************************
3465Shutdown the log system but do not release all the memory. */
3466UNIV_INTERN
3467void
3468log_shutdown(void)
3469/*==============*/
3470{
3471 log_group_t* group;
3472
3473 group = UT_LIST_GET_FIRST(log_sys->log_groups);
3474
3475 while (UT_LIST_GET_LEN(log_sys->log_groups) > 0) {
3476 log_group_t* prev_group = group;
3477
3478 group = UT_LIST_GET_NEXT(log_groups, group);
3479 UT_LIST_REMOVE(log_groups, log_sys->log_groups, prev_group);
3480
3481 log_group_close(prev_group);
3482 }
3483
3484 mem_free(log_sys->buf_ptr);
3485 log_sys->buf_ptr = NULL;
3486 log_sys->buf = NULL;
3487 mem_free(log_sys->checkpoint_buf_ptr);
3488 log_sys->checkpoint_buf_ptr = NULL;
3489 log_sys->checkpoint_buf = NULL;
3490
3491 os_event_free(log_sys->no_flush_event);
3492 os_event_free(log_sys->one_flushed_event);
3493
3494 rw_lock_free(&log_sys->checkpoint_lock);
3495
3496 mutex_free(&log_sys->mutex);
3497
3498#ifdef UNIV_LOG_ARCHIVE
3499 rw_lock_free(&log_sys->archive_lock);
3500 os_event_create(log_sys->archiving_on);
3501#endif /* UNIV_LOG_ARCHIVE */
3502
3503#ifdef UNIV_LOG_DEBUG
3504 recv_sys_debug_free();
3505#endif
3506
3508}
3509
3510/**********************************************************
3511Free the log system data structures. */
3512UNIV_INTERN
3513void
3514log_mem_free(void)
3515/*==============*/
3516{
3517 if (log_sys != NULL) {
3519 mem_free(log_sys);
3520
3521 log_sys = NULL;
3522 }
3523}
3524#endif /* !UNIV_HOTBACKUP */
UNIV_INTERN ibool buf_pool_check_no_pending_io(void)
Definition buf0buf.cc:5197
UNIV_INLINE ulint buf_pool_get_curr_size(void)
UNIV_INTERN ib_uint64_t buf_pool_get_oldest_modification(void)
Definition buf0buf.cc:315
UNIV_INTERN ibool buf_all_freed(void)
Definition buf0buf.cc:5173
UNIV_INLINE ulint buf_pool_get_n_pages(void)
@ BUF_FLUSH_LIST
Definition buf0types.h:50
#define FIL_TABLESPACE
Definition fil0fil.h:149
UNIV_INTERN void fil_close_all_files(void)
Definition fil0fil.cc:1660
#define FIL_LOG
Definition fil0fil.h:150
UNIV_INTERN void log_group_set_fields(log_group_t *group, ib_uint64_t lsn)
Definition log0log.cc:676
UNIV_INTERN void log_refresh_stats(void)
Definition log0log.cc:3427
UNIV_INTERN void log_group_read_checkpoint_info(log_group_t *group, ulint field)
Definition log0log.cc:1969
UNIV_INTERN void log_make_checkpoint_at(ib_uint64_t lsn, ibool write_always)
Definition log0log.cc:2102
UNIV_INLINE ulint log_block_get_data_len(const byte *log_block)
UNIV_INTERN ulint log_group_get_capacity(const log_group_t *group)
Definition log0log.cc:527
#define log_do_write
Definition log0log.h:57
UNIV_INTERN void log_write_low(byte *str, ulint str_len)
Definition log0log.cc:299
UNIV_INTERN void log_check_margins(void)
Definition log0log.cc:3086
UNIV_INTERN ulint log_archive_start(void)
UNIV_INTERN ibool log_archive_do(ibool sync, ulint *n_bytes)
UNIV_INTERN ibool log_preflush_pool_modified_pages(ib_uint64_t new_oldest, ibool sync)
Definition log0log.cc:1676
UNIV_INTERN void logs_empty_and_mark_files_at_shutdown(void)
Definition log0log.cc:3118
UNIV_INLINE void log_release(void)
UNIV_INTERN void log_write_up_to(ib_uint64_t lsn, ulint wait, ibool flush_to_disk)
Definition log0log.cc:1376
UNIV_INLINE ulint log_block_get_hdr_no(const byte *log_block)
UNIV_INLINE ulint log_block_convert_lsn_to_no(ib_uint64_t lsn)
UNIV_INLINE ulint log_block_calc_checksum(const byte *block)
UNIV_INLINE void log_block_set_checksum(byte *log_block, ulint checksum)
UNIV_INTERN void log_checkpoint_get_nth_group_info(const byte *buf, ulint n, ulint *file_no, ulint *offset)
Definition log0log.cc:1773
UNIV_INTERN ulint log_archive_archivelog(void)
#define LOG_NO_WAIT
Definition log0log.h:61
UNIV_INTERN void log_print(FILE *file)
Definition log0log.cc:3377
UNIV_INLINE void log_block_init(byte *log_block, ib_uint64_t lsn)
UNIV_INLINE void log_block_set_data_len(byte *log_block, ulint len)
UNIV_INTERN void log_buffer_flush_to_disk(void)
Definition log0log.cc:1601
UNIV_INTERN ulint log_archive_stop(void)
UNIV_INTERN void log_group_write_buf(log_group_t *group, byte *buf, ulint len, ib_uint64_t start_lsn, ulint new_data_offset)
Definition log0log.cc:1255
UNIV_INTERN void log_fsp_current_free_limit_set_and_checkpoint(ulint limit)
Definition log0log.cc:175
UNIV_INLINE ulint log_block_get_first_rec_group(const byte *log_block)
UNIV_INTERN void log_buffer_sync_in_background(ibool flush)
Definition log0log.cc:1622
UNIV_INTERN ibool log_peek_lsn(ib_uint64_t *lsn)
Definition log0log.cc:3358
struct log_struct log_t
Definition log0log.h:46
UNIV_INTERN void log_io_complete(log_group_t *group)
Definition log0log.cc:1117
UNIV_INTERN ulint log_calc_where_lsn_is(ib_int64_t *log_file_offset, ib_uint64_t first_header_lsn, ib_uint64_t lsn, ulint n_log_files, ib_int64_t log_file_size)
Definition log0log.cc:634
UNIV_INTERN ib_uint64_t log_close(void)
Definition log0log.cc:402
UNIV_INTERN void log_group_read_log_seg(ulint type, byte *buf, log_group_t *group, ib_uint64_t start_lsn, ib_uint64_t end_lsn)
Definition log0log.cc:2229
UNIV_INTERN void log_group_init(ulint id, ulint n_files, ulint file_size, ulint space_id, ulint archive_space_id)
Definition log0log.cc:925
UNIV_INTERN ulint log_archive_noarchivelog(void)
UNIV_INTERN ib_uint64_t log_reserve_and_open(ulint len)
Definition log0log.cc:225
#define LOG_MAX_N_GROUPS
Definition log0log.h:66
struct log_group_struct log_group_t
Definition log0log.h:48
UNIV_INTERN void log_archived_file_name_gen(char *buf, ulint id, ulint file_no)
UNIV_INLINE void log_block_set_first_rec_group(byte *log_block, ulint offset)
UNIV_INTERN ibool log_checkpoint(ibool sync, ibool write_always)
Definition log0log.cc:2011
UNIV_INTERN void log_init(void)
Definition log0log.cc:801
UNIV_INTERN void log_groups_write_checkpoint_info(void)
Definition log0log.cc:1987
UNIV_INTERN void recv_sys_mem_free(void)
Definition log0recv.cc:232
UNIV_INTERN void recv_sys_close(void)
Definition log0recv.cc:201
UNIV_INTERN void recv_apply_hashed_log_recs(ibool allow_ibuf)
Definition log0recv.cc:1738
recv_sys_t * recv_sys
Definition log0recv.cc:73
UNIV_INTERN void recv_sys_create(void)
Definition log0recv.cc:180
UNIV_INTERN ibool recv_scan_log_recs(ulint available_memory, ibool store_to_hash, const byte *buf, ulint len, ib_uint64_t start_lsn, ib_uint64_t *contiguous_lsn, ib_uint64_t *group_scanned_lsn)
Definition log0recv.cc:2562
ibool recv_no_ibuf_operations
Definition log0recv.cc:116
ulint recv_n_pool_free_frames
Definition log0recv.cc:149
ibool recv_recovery_on
Definition log0recv.cc:77
UNIV_INTERN void recv_sys_init(ulint available_memory)
Definition log0recv.cc:303
UNIV_INLINE ibool recv_recovery_is_on(void)
UNIV_INLINE void mach_write_to_8(byte *b, ib_uint64_t n)
UNIV_INLINE void mach_write_to_4(byte *b, ulint n)
UNIV_INLINE ulint mach_read_from_4(const byte *b) __attribute__((nonnull
#define mem_free(PTR)
Definition mem0mem.h:243
#define MLOG_DUMMY_RECORD
Definition mtr0mtr.h:126
#define OS_DATA_FILE
Definition os0file.h:124
int os_file_t
Definition os0file.h:87
#define OS_FILE_LOG_BLOCK_SIZE
Definition os0file.h:104
#define OS_FILE_OPEN
Definition os0file.h:107
#define OS_FILE_READ
Definition os0file.h:142
UNIV_INTERN ib_int64_t os_event_reset(os_event_t event)
Definition os0sync.cc:472
UNIV_INTERN os_event_t os_event_create(const char *name)
Definition os0sync.cc:365
UNIV_INTERN void os_event_set(os_event_t event)
Definition os0sync.cc:434
UNIV_INTERN void os_event_free(os_event_t event)
Definition os0sync.cc:535
UNIV_INTERN void os_thread_sleep(ulint tm)
Definition os0thread.cc:265
@ SRV_UNIX_ALL_O_DIRECT
Definition srv0srv.h:423
@ SRV_UNIX_O_DSYNC
Definition srv0srv.h:416
@ SRV_UNIX_NOSYNC
Definition srv0srv.h:420
ulint srv_buf_pool_instances
Definition srv0srv.cc:255
UNIV_INTERN ibool srv_is_any_background_thread_active(void)
Definition srv0srv.cc:2589
ib_uint64_t srv_shutdown_lsn
Definition srv0start.cc:101
srv_shutdown_state
Definition srv0start.h:113
@ SRV_SHUTDOWN_CLEANUP
Definition srv0start.h:115
@ SRV_SHUTDOWN_LAST_PHASE
Definition srv0start.h:117
ib_uint64_t srv_start_lsn
Definition srv0start.cc:99
ib_uint64_t lsn
Definition log0log.h:690
byte * checkpoint_buf_ptr
Definition log0log.h:724
byte ** file_header_bufs
Definition log0log.h:696
ulint n_pending_writes
Definition log0log.h:693
byte * checkpoint_buf
Definition log0log.h:725
byte ** file_header_bufs_ptr
Definition log0log.h:695
ulint max_checkpoint_age
Definition log0log.h:880
ulint buf_size
Definition log0log.h:753
byte * buf
Definition log0log.h:752
ib_uint64_t last_checkpoint_lsn
Definition log0log.h:886
ibool check_flush_or_checkpoint
Definition log0log.h:763
ulint old_buf_free
Definition log0log.h:757
ulint log_group_capacity
Definition log0log.h:853
ib_uint64_t old_lsn
Definition log0log.h:760
ulint max_buf_free
Definition log0log.h:754
mutex_t mutex
Definition log0log.h:740
ulint max_modified_age_sync
Definition log0log.h:864
ulint n_pending_writes
Definition log0log.h:819
ulint buf_free
Definition log0log.h:737
ib_uint64_t lsn
Definition log0log.h:736
#define rw_lock_s_lock(M)
Definition sync0rw.h:155
#define rw_lock_create(K, L, level)
Definition sync0rw.h:147
trx_sys_t * trx_sys
Definition trx0sys.cc:61
ulint trx_n_mysql_transactions
Definition trx0trx.cc:52
UNIV_INLINE void * ut_align(const void *ptr, ulint align_no)
UNIV_INLINE void * ut_align_down(const void *ptr, ulint align_no) __attribute__((const))
UNIV_INLINE ib_uint64_t ut_uint64_align_down(ib_uint64_t n, ulint align_no)
#define ut_error
Definition ut0dbg.h:115
#define ut_ad(EXPR)
Definition ut0dbg.h:127
#define ut_a(EXPR)
Definition ut0dbg.h:105
#define UT_LIST_GET_LEN(BASE)
Definition ut0lst.h:216
#define UT_LIST_REMOVE(NAME, BASE, N)
Definition ut0lst.h:177
#define UT_LIST_GET_FIRST(BASE)
Definition ut0lst.h:223
#define UT_LIST_ADD_LAST(NAME, BASE, N)
Definition ut0lst.h:118
#define UT_LIST_GET_NEXT(NAME, N)
Definition ut0lst.h:200
#define UT_LIST_INIT(BASE)
Definition ut0lst.h:83
UNIV_INLINE void * ut_memcpy(void *dest, const void *sour, ulint n)
UNIV_INLINE void * ut_memmove(void *dest, const void *sour, ulint n)
UNIV_INLINE ulint ut_fold_binary(const byte *str, ulint len) __attribute__((pure))
UNIV_INLINE ulint ut_min(ulint n1, ulint n2)
UNIV_INTERN void ut_print_timestamp(FILE *file)
Definition ut0ut.cc:239
UNIV_INTERN void ut_sprintf_timestamp(char *buf)
Definition ut0ut.cc:282
#define ut_calc_align_down(n, m)
Definition ut0ut.h:180
#define ut_calc_align(n, m)
Definition ut0ut.h:187