-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
log0log.cc
2373 lines (1875 loc) · 61.3 KB
/
log0log.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
Copyright (c) 1995, 2017, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2009, Google Inc.
Copyright (c) 2014, 2017, MariaDB Corporation.
Portions of this file contain modifications contributed and copyrighted by
Google, Inc. Those modifications are gratefully acknowledged and are described
briefly in the InnoDB documentation. The contributions by Google are
incorporated with their permission, and subject to the conditions contained in
the file COPYING.Google.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
*****************************************************************************/
/**************************************************//**
@file log/log0log.cc
Database log
Created 12/9/1995 Heikki Tuuri
*******************************************************/
#include "ha_prototypes.h"
#include <debug_sync.h>
#include "log0log.h"
#include "log0crypt.h"
#include "mem0mem.h"
#include "buf0buf.h"
#include "buf0flu.h"
#include "lock0lock.h"
#include "log0recv.h"
#include "fil0fil.h"
#include "dict0boot.h"
#include "dict0stats_bg.h"
#include "btr0defragment.h"
#include "srv0srv.h"
#include "srv0start.h"
#include "trx0sys.h"
#include "trx0trx.h"
#include "trx0roll.h"
#include "srv0mon.h"
#include "sync0sync.h"
/*
General philosophy of InnoDB redo-logs:
1) Every change to a contents of a data page must be done
through mtr, which in mtr_commit() writes log records
to the InnoDB redo log.
2) Normally these changes are performed using a mlog_write_ulint()
or similar function.
3) In some page level operations only a code number of a
c-function and its parameters are written to the log to
reduce the size of the log.
3a) You should not add parameters to these kind of functions
(e.g. trx_undo_header_create(), trx_undo_insert_header_reuse())
3b) You should not add such functionality which either change
working when compared with the old or are dependent on data
outside of the page. These kind of functions should implement
self-contained page transformation and it should be unchanged
if you don't have very essential reasons to change log
semantics or format.
*/
/** Redo log system */
log_t* log_sys = NULL;
/** Whether to generate and require checksums on the redo log pages */
my_bool innodb_log_checksums;
/** Pointer to the log checksum calculation function */
log_checksum_func_t log_checksum_algorithm_ptr;
/* Next log block number to do dummy record filling if no log records written
for a while */
static ulint next_lbn_to_pad = 0;
/* These control how often we print warnings if the last checkpoint is too
old */
static bool log_has_printed_chkp_warning = false;
static time_t log_last_warning_time;
static bool log_has_printed_chkp_margine_warning = false;
static time_t log_last_margine_warning_time;
/* A margin for free space in the log buffer before a log entry is catenated */
#define LOG_BUF_WRITE_MARGIN (4 * OS_FILE_LOG_BLOCK_SIZE)
/* Margins for free space in the log buffer after a log entry is catenated */
#define LOG_BUF_FLUSH_RATIO 2
#define LOG_BUF_FLUSH_MARGIN (LOG_BUF_WRITE_MARGIN + 4 * UNIV_PAGE_SIZE)
/* This parameter controls asynchronous making of a new checkpoint; the value
should be bigger than LOG_POOL_PREFLUSH_RATIO_SYNC */
#define LOG_POOL_CHECKPOINT_RATIO_ASYNC 32
/* This parameter controls synchronous preflushing of modified buffer pages */
#define LOG_POOL_PREFLUSH_RATIO_SYNC 16
/* The same ratio for asynchronous preflushing; this value should be less than
the previous */
#define LOG_POOL_PREFLUSH_RATIO_ASYNC 8
/* Codes used in unlocking flush latches */
#define LOG_UNLOCK_NONE_FLUSHED_LOCK 1
#define LOG_UNLOCK_FLUSH_LOCK 2
/** Event to wake up log_scrub_thread */
os_event_t log_scrub_event;
/** Whether log_scrub_thread is active */
bool log_scrub_thread_active;
extern "C" UNIV_INTERN
os_thread_ret_t
DECLARE_THREAD(log_scrub_thread)(void*);
/******************************************************//**
Completes a checkpoint write i/o to a log file. */
static
void
log_io_complete_checkpoint(void);
/*============================*/
/****************************************************************//**
Returns the oldest modified block lsn in the pool, or log_sys->lsn if none
exists.
@return LSN of oldest modification */
static
lsn_t
log_buf_pool_get_oldest_modification(void)
/*======================================*/
{
lsn_t lsn;
ut_ad(log_mutex_own());
lsn = buf_pool_get_oldest_modification();
if (!lsn) {
lsn = log_sys->lsn;
}
return(lsn);
}
/** Extends the log buffer.
@param[in] len requested minimum size in bytes */
void
log_buffer_extend(
ulint len)
{
ulint move_start;
ulint move_end;
byte tmp_buf[OS_FILE_LOG_BLOCK_SIZE];
log_mutex_enter_all();
while (log_sys->is_extending) {
/* Another thread is trying to extend already.
Needs to wait for. */
log_mutex_exit_all();
log_buffer_flush_to_disk();
log_mutex_enter_all();
if (srv_log_buffer_size > len / UNIV_PAGE_SIZE) {
/* Already extended enough by the others */
log_mutex_exit_all();
return;
}
}
if (len >= log_sys->buf_size / 2) {
DBUG_EXECUTE_IF("ib_log_buffer_is_short_crash",
DBUG_SUICIDE(););
/* log_buffer is too small. try to extend instead of crash. */
ib::warn() << "The transaction log size is too large"
" for innodb_log_buffer_size (" << len << " >= "
<< LOG_BUFFER_SIZE << " / 2). Trying to extend it.";
}
log_sys->is_extending = true;
while (ut_calc_align_down(log_sys->buf_free,
OS_FILE_LOG_BLOCK_SIZE)
!= ut_calc_align_down(log_sys->buf_next_to_write,
OS_FILE_LOG_BLOCK_SIZE)) {
/* Buffer might have >1 blocks to write still. */
log_mutex_exit_all();
log_buffer_flush_to_disk();
log_mutex_enter_all();
}
move_start = ut_calc_align_down(
log_sys->buf_free,
OS_FILE_LOG_BLOCK_SIZE);
move_end = log_sys->buf_free;
/* store the last log block in buffer */
ut_memcpy(tmp_buf, log_sys->buf + move_start,
move_end - move_start);
log_sys->buf_free -= move_start;
log_sys->buf_next_to_write -= move_start;
/* reallocate log buffer */
srv_log_buffer_size = len / UNIV_PAGE_SIZE + 1;
ut_free(log_sys->buf_ptr);
log_sys->buf_size = LOG_BUFFER_SIZE;
log_sys->buf_ptr = static_cast<byte*>(
ut_zalloc_nokey(log_sys->buf_size * 2 + OS_FILE_LOG_BLOCK_SIZE));
log_sys->buf = static_cast<byte*>(
ut_align(log_sys->buf_ptr, OS_FILE_LOG_BLOCK_SIZE));
log_sys->first_in_use = true;
log_sys->max_buf_free = log_sys->buf_size / LOG_BUF_FLUSH_RATIO
- LOG_BUF_FLUSH_MARGIN;
/* restore the last log block */
ut_memcpy(log_sys->buf, tmp_buf, move_end - move_start);
ut_ad(log_sys->is_extending);
log_sys->is_extending = false;
log_mutex_exit_all();
ib::info() << "innodb_log_buffer_size was extended to "
<< LOG_BUFFER_SIZE << ".";
}
/** Calculate actual length in redo buffer and file including
block header and trailer.
@param[in] len length to write
@return actual length to write including header and trailer. */
static inline
ulint
log_calculate_actual_len(
ulint len)
{
ut_ad(log_mutex_own());
/* actual length stored per block */
const ulint len_per_blk = OS_FILE_LOG_BLOCK_SIZE
- (LOG_BLOCK_HDR_SIZE + LOG_BLOCK_TRL_SIZE);
/* actual data length in last block already written */
ulint extra_len = (log_sys->buf_free % OS_FILE_LOG_BLOCK_SIZE);
ut_ad(extra_len >= LOG_BLOCK_HDR_SIZE);
extra_len -= LOG_BLOCK_HDR_SIZE;
/* total extra length for block header and trailer */
extra_len = ((len + extra_len) / len_per_blk)
* (LOG_BLOCK_HDR_SIZE + LOG_BLOCK_TRL_SIZE);
return(len + extra_len);
}
/** Check margin not to overwrite transaction log from the last checkpoint.
If would estimate the log write to exceed the log_group_capacity,
waits for the checkpoint is done enough.
@param[in] len length of the data to be written */
void
log_margin_checkpoint_age(
ulint len)
{
ulint margin = log_calculate_actual_len(len);
ut_ad(log_mutex_own());
if (margin > log_sys->log_group_capacity) {
/* return with warning output to avoid deadlock */
if (!log_has_printed_chkp_margine_warning
|| difftime(time(NULL),
log_last_margine_warning_time) > 15) {
log_has_printed_chkp_margine_warning = true;
log_last_margine_warning_time = time(NULL);
ib::error() << "The transaction log files are too"
" small for the single transaction log (size="
<< len << "). So, the last checkpoint age"
" might exceed the log group capacity "
<< log_sys->log_group_capacity << ".";
}
return;
}
/* Our margin check should ensure that we never reach this condition.
Try to do checkpoint once. We cannot keep waiting here as it might
result in hang in case the current mtr has latch on oldest lsn */
if (log_sys->lsn - log_sys->last_checkpoint_lsn + margin
> log_sys->log_group_capacity) {
/* The log write of 'len' might overwrite the transaction log
after the last checkpoint. Makes checkpoint. */
bool flushed_enough = false;
if (log_sys->lsn - log_buf_pool_get_oldest_modification()
+ margin
<= log_sys->log_group_capacity) {
flushed_enough = true;
}
log_sys->check_flush_or_checkpoint = true;
log_mutex_exit();
DEBUG_SYNC_C("margin_checkpoint_age_rescue");
if (!flushed_enough) {
os_thread_sleep(100000);
}
log_checkpoint(true, false);
log_mutex_enter();
}
return;
}
/** Open the log for log_write_low. The log must be closed with log_close.
@param[in] len length of the data to be written
@return start lsn of the log record */
lsn_t
log_reserve_and_open(
ulint len)
{
ulint len_upper_limit;
#ifdef UNIV_DEBUG
ulint count = 0;
#endif /* UNIV_DEBUG */
loop:
ut_ad(log_mutex_own());
if (log_sys->is_extending) {
log_mutex_exit();
/* Log buffer size is extending. Writing up to the next block
should wait for the extending finished. */
os_thread_sleep(100000);
ut_ad(++count < 50);
log_mutex_enter();
goto loop;
}
/* Calculate an upper limit for the space the string may take in the
log buffer */
len_upper_limit = LOG_BUF_WRITE_MARGIN + srv_log_write_ahead_size
+ (5 * len) / 4;
if (log_sys->buf_free + len_upper_limit > log_sys->buf_size) {
log_mutex_exit();
DEBUG_SYNC_C("log_buf_size_exceeded");
/* Not enough free space, do a write of the log buffer */
log_buffer_sync_in_background(false);
srv_stats.log_waits.inc();
ut_ad(++count < 50);
log_mutex_enter();
goto loop;
}
return(log_sys->lsn);
}
/************************************************************//**
Writes to the log the string given. It is assumed that the caller holds the
log mutex. */
void
log_write_low(
/*==========*/
const byte* str, /*!< in: string */
ulint str_len) /*!< in: string length */
{
log_t* log = log_sys;
ulint len;
ulint data_len;
byte* log_block;
ut_ad(log_mutex_own());
part_loop:
/* Calculate a part length */
data_len = (log->buf_free % OS_FILE_LOG_BLOCK_SIZE) + str_len;
if (data_len <= OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_TRL_SIZE) {
/* The string fits within the current log block */
len = str_len;
} else {
data_len = OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_TRL_SIZE;
len = OS_FILE_LOG_BLOCK_SIZE
- (log->buf_free % OS_FILE_LOG_BLOCK_SIZE)
- LOG_BLOCK_TRL_SIZE;
}
ut_memcpy(log->buf + log->buf_free, str, len);
str_len -= len;
str = str + len;
log_block = static_cast<byte*>(
ut_align_down(
log->buf + log->buf_free, OS_FILE_LOG_BLOCK_SIZE));
log_block_set_data_len(log_block, data_len);
if (data_len == OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_TRL_SIZE) {
/* This block became full */
log_block_set_data_len(log_block, OS_FILE_LOG_BLOCK_SIZE);
log_block_set_checkpoint_no(log_block,
log_sys->next_checkpoint_no);
len += LOG_BLOCK_HDR_SIZE + LOG_BLOCK_TRL_SIZE;
log->lsn += len;
/* Initialize the next block header */
log_block_init(log_block + OS_FILE_LOG_BLOCK_SIZE, log->lsn);
} else {
log->lsn += len;
}
log->buf_free += len;
ut_ad(log->buf_free <= log->buf_size);
if (str_len > 0) {
goto part_loop;
}
srv_stats.log_write_requests.inc();
}
/************************************************************//**
Closes the log.
@return lsn */
lsn_t
log_close(void)
/*===========*/
{
byte* log_block;
ulint first_rec_group;
lsn_t oldest_lsn;
lsn_t lsn;
log_t* log = log_sys;
lsn_t checkpoint_age;
ut_ad(log_mutex_own());
ut_ad(!recv_no_log_write);
lsn = log->lsn;
log_block = static_cast<byte*>(
ut_align_down(
log->buf + log->buf_free, OS_FILE_LOG_BLOCK_SIZE));
first_rec_group = log_block_get_first_rec_group(log_block);
if (first_rec_group == 0) {
/* We initialized a new log block which was not written
full by the current mtr: the next mtr log record group
will start within this block at the offset data_len */
log_block_set_first_rec_group(
log_block, log_block_get_data_len(log_block));
}
if (log->buf_free > log->max_buf_free) {
log->check_flush_or_checkpoint = true;
}
checkpoint_age = lsn - log->last_checkpoint_lsn;
if (checkpoint_age >= log->log_group_capacity) {
DBUG_EXECUTE_IF(
"print_all_chkp_warnings",
log_has_printed_chkp_warning = false;);
if (!log_has_printed_chkp_warning
|| difftime(time(NULL), log_last_warning_time) > 15) {
log_has_printed_chkp_warning = true;
log_last_warning_time = time(NULL);
ib::error() << "The age of the last checkpoint is "
<< checkpoint_age << ", which exceeds the log"
" group capacity " << log->log_group_capacity
<< ".";
}
}
if (checkpoint_age <= log->max_modified_age_sync) {
goto function_exit;
}
oldest_lsn = buf_pool_get_oldest_modification();
if (!oldest_lsn
|| lsn - oldest_lsn > log->max_modified_age_sync
|| checkpoint_age > log->max_checkpoint_age_async) {
log->check_flush_or_checkpoint = true;
}
function_exit:
return(lsn);
}
/******************************************************//**
Calculates the offset within a log group, when the log file headers are not
included.
@return size offset (<= offset) */
UNIV_INLINE
lsn_t
log_group_calc_size_offset(
/*=======================*/
lsn_t offset, /*!< in: real offset within the
log group */
const log_group_t* group) /*!< in: log group */
{
/* The lsn parameters are updated while holding both the mutexes
and it is ok to have either of them while reading */
ut_ad(log_mutex_own() || log_write_mutex_own());
return(offset - LOG_FILE_HDR_SIZE * (1 + offset / group->file_size));
}
/******************************************************//**
Calculates the offset within a log group, when the log file headers are
included.
@return real offset (>= offset) */
UNIV_INLINE
lsn_t
log_group_calc_real_offset(
/*=======================*/
lsn_t offset, /*!< in: size offset within the
log group */
const log_group_t* group) /*!< in: log group */
{
/* The lsn parameters are updated while holding both the mutexes
and it is ok to have either of them while reading */
ut_ad(log_mutex_own() || log_write_mutex_own());
return(offset + LOG_FILE_HDR_SIZE
* (1 + offset / (group->file_size - LOG_FILE_HDR_SIZE)));
}
/** Calculate the offset of an lsn within a log group.
@param[in] lsn log sequence number
@param[in] group log group
@return offset within the log group */
lsn_t
log_group_calc_lsn_offset(
lsn_t lsn,
const log_group_t* group)
{
lsn_t gr_lsn;
lsn_t gr_lsn_size_offset;
lsn_t difference;
lsn_t group_size;
lsn_t offset;
/* The lsn parameters are updated while holding both the mutexes
and it is ok to have either of them while reading */
ut_ad(log_mutex_own() || log_write_mutex_own());
gr_lsn = group->lsn;
gr_lsn_size_offset = log_group_calc_size_offset(
group->lsn_offset, group);
group_size = group->capacity();
if (lsn >= gr_lsn) {
difference = lsn - gr_lsn;
} else {
difference = gr_lsn - lsn;
difference = difference % group_size;
difference = group_size - difference;
}
offset = (gr_lsn_size_offset + difference) % group_size;
/* fprintf(stderr,
"Offset is " LSN_PF " gr_lsn_offset is " LSN_PF
" difference is " LSN_PF "\n",
offset, gr_lsn_size_offset, difference);
*/
return(log_group_calc_real_offset(offset, group));
}
/********************************************************//**
Sets the field values in group to correspond to a given lsn. For this function
to work, the values must already be correctly initialized to correspond to
some lsn, for instance, a checkpoint lsn. */
void
log_group_set_fields(
/*=================*/
log_group_t* group, /*!< in/out: group */
lsn_t lsn) /*!< in: lsn for which the values should be
set */
{
group->lsn_offset = log_group_calc_lsn_offset(lsn, group);
group->lsn = lsn;
}
/** Calculate the recommended highest values for lsn - last_checkpoint_lsn
and lsn - buf_get_oldest_modification().
@param[in] file_size requested innodb_log_file_size
@retval true on success
@retval false if the smallest log group is too small to
accommodate the number of OS threads in the database server */
bool
log_set_capacity(ulonglong file_size)
{
lsn_t margin;
ulint free;
lsn_t smallest_capacity = (file_size - LOG_FILE_HDR_SIZE)
* srv_n_log_files;
/* Add extra safety */
smallest_capacity -= smallest_capacity / 10;
/* For each OS thread we must reserve so much free space in the
smallest log group that it can accommodate the log entries produced
by single query steps: running out of free log space is a serious
system error which requires rebooting the database. */
free = LOG_CHECKPOINT_FREE_PER_THREAD * (10 + srv_thread_concurrency)
+ LOG_CHECKPOINT_EXTRA_FREE;
if (free >= smallest_capacity / 2) {
ib::error() << "Cannot continue operation. ib_logfiles are too"
" small for innodb_thread_concurrency="
<< srv_thread_concurrency << ". The combined size of"
" ib_logfiles should be bigger than"
" 200 kB * innodb_thread_concurrency. "
<< INNODB_PARAMETERS_MSG;
return(false);
}
margin = smallest_capacity - free;
margin = margin - margin / 10; /* Add still some extra safety */
log_mutex_enter();
log_sys->log_group_capacity = smallest_capacity;
log_sys->max_modified_age_async = margin
- margin / LOG_POOL_PREFLUSH_RATIO_ASYNC;
log_sys->max_modified_age_sync = margin
- margin / LOG_POOL_PREFLUSH_RATIO_SYNC;
log_sys->max_checkpoint_age_async = margin - margin
/ LOG_POOL_CHECKPOINT_RATIO_ASYNC;
log_sys->max_checkpoint_age = margin;
log_mutex_exit();
return(true);
}
/** Initializes the redo logging subsystem. */
void
log_sys_init()
{
log_sys = static_cast<log_t*>(ut_zalloc_nokey(sizeof(log_t)));
mutex_create(LATCH_ID_LOG_SYS, &log_sys->mutex);
mutex_create(LATCH_ID_LOG_WRITE, &log_sys->write_mutex);
mutex_create(LATCH_ID_LOG_FLUSH_ORDER, &log_sys->log_flush_order_mutex);
/* Start the lsn from one log block from zero: this way every
log record has a start lsn != zero, a fact which we will use */
log_sys->lsn = LOG_START_LSN;
ut_a(LOG_BUFFER_SIZE >= 16 * OS_FILE_LOG_BLOCK_SIZE);
ut_a(LOG_BUFFER_SIZE >= 4 * UNIV_PAGE_SIZE);
log_sys->buf_size = LOG_BUFFER_SIZE;
log_sys->buf_ptr = static_cast<byte*>(
ut_zalloc_nokey(log_sys->buf_size * 2 + OS_FILE_LOG_BLOCK_SIZE));
log_sys->buf = static_cast<byte*>(
ut_align(log_sys->buf_ptr, OS_FILE_LOG_BLOCK_SIZE));
log_sys->first_in_use = true;
log_sys->max_buf_free = log_sys->buf_size / LOG_BUF_FLUSH_RATIO
- LOG_BUF_FLUSH_MARGIN;
log_sys->check_flush_or_checkpoint = true;
log_sys->n_log_ios_old = log_sys->n_log_ios;
log_sys->last_printout_time = time(NULL);
/*----------------------------*/
log_sys->write_lsn = log_sys->lsn;
log_sys->flush_event = os_event_create(0);
os_event_set(log_sys->flush_event);
/*----------------------------*/
log_sys->last_checkpoint_lsn = log_sys->lsn;
rw_lock_create(
checkpoint_lock_key, &log_sys->checkpoint_lock,
SYNC_NO_ORDER_CHECK);
log_sys->checkpoint_buf_ptr = static_cast<byte*>(
ut_zalloc_nokey(2 * OS_FILE_LOG_BLOCK_SIZE));
log_sys->checkpoint_buf = static_cast<byte*>(
ut_align(log_sys->checkpoint_buf_ptr, OS_FILE_LOG_BLOCK_SIZE));
/*----------------------------*/
log_block_init(log_sys->buf, log_sys->lsn);
log_block_set_first_rec_group(log_sys->buf, LOG_BLOCK_HDR_SIZE);
log_sys->buf_free = LOG_BLOCK_HDR_SIZE;
log_sys->lsn = LOG_START_LSN + LOG_BLOCK_HDR_SIZE; // TODO(minliz): ensure various LOG_START_LSN?
MONITOR_SET(MONITOR_LSN_CHECKPOINT_AGE,
log_sys->lsn - log_sys->last_checkpoint_lsn);
log_scrub_thread_active = !srv_read_only_mode && srv_scrub_log;
if (log_scrub_thread_active) {
log_scrub_event = os_event_create("log_scrub_event");
os_thread_create(log_scrub_thread, NULL, NULL);
}
}
/** Initialize the redo log.
@param[in] n_files number of files */
void
log_init(ulint n_files)
{
ulint i;
log_group_t* group = &log_sys->log;
group->n_files = n_files;
group->format = srv_encrypt_log
? LOG_HEADER_FORMAT_CURRENT | LOG_HEADER_FORMAT_ENCRYPTED
: LOG_HEADER_FORMAT_CURRENT;
group->file_size = srv_log_file_size;
group->state = LOG_GROUP_OK;
group->lsn = LOG_START_LSN;
group->lsn_offset = LOG_FILE_HDR_SIZE;
group->file_header_bufs_ptr = static_cast<byte**>(
ut_zalloc_nokey(sizeof(byte*) * n_files));
group->file_header_bufs = static_cast<byte**>(
ut_zalloc_nokey(sizeof(byte**) * n_files));
for (i = 0; i < n_files; i++) {
group->file_header_bufs_ptr[i] = static_cast<byte*>(
ut_zalloc_nokey(LOG_FILE_HDR_SIZE
+ OS_FILE_LOG_BLOCK_SIZE));
group->file_header_bufs[i] = static_cast<byte*>(
ut_align(group->file_header_bufs_ptr[i],
OS_FILE_LOG_BLOCK_SIZE));
}
group->checkpoint_buf_ptr = static_cast<byte*>(
ut_zalloc_nokey(2 * OS_FILE_LOG_BLOCK_SIZE));
group->checkpoint_buf = static_cast<byte*>(
ut_align(group->checkpoint_buf_ptr,OS_FILE_LOG_BLOCK_SIZE));
}
/******************************************************//**
Completes an i/o to a log file. */
void
log_io_complete(
/*============*/
log_group_t* group) /*!< in: log group or a dummy pointer */
{
if ((ulint) group & 0x1UL) {
/* It was a checkpoint write */
group = (log_group_t*)((ulint) group - 1);
switch (srv_file_flush_method) {
case SRV_O_DSYNC:
case SRV_NOSYNC:
break;
case SRV_FSYNC:
case SRV_LITTLESYNC:
case SRV_O_DIRECT:
case SRV_O_DIRECT_NO_FSYNC:
case SRV_ALL_O_DIRECT_FSYNC:
fil_flush(SRV_LOG_SPACE_FIRST_ID);
}
DBUG_PRINT("ib_log", ("checkpoint info written"));
log_io_complete_checkpoint();
return;
}
ut_error; /*!< We currently use synchronous writing of the
logs and cannot end up here! */
}
/******************************************************//**
Writes a log file header to a log file space. */
static
void
log_group_file_header_flush(
/*========================*/
log_group_t* group, /*!< in: log group */
ulint nth_file, /*!< in: header to the nth file in the
log file space */
lsn_t start_lsn) /*!< in: log file data starts at this
lsn */
{
byte* buf;
lsn_t dest_offset;
ut_ad(log_write_mutex_own());
ut_ad(!recv_no_log_write);
ut_a(nth_file < group->n_files);
ut_ad((group->format & ~LOG_HEADER_FORMAT_ENCRYPTED)
== LOG_HEADER_FORMAT_CURRENT);
buf = *(group->file_header_bufs + nth_file);
memset(buf, 0, OS_FILE_LOG_BLOCK_SIZE);
mach_write_to_4(buf + LOG_HEADER_FORMAT, group->format);
mach_write_to_8(buf + LOG_HEADER_START_LSN, start_lsn);
strcpy(reinterpret_cast<char*>(buf) + LOG_HEADER_CREATOR,
LOG_HEADER_CREATOR_CURRENT);
ut_ad(LOG_HEADER_CREATOR_END - LOG_HEADER_CREATOR
>= sizeof LOG_HEADER_CREATOR_CURRENT);
log_block_set_checksum(buf, log_block_calc_checksum_crc32(buf));
dest_offset = nth_file * group->file_size;
DBUG_PRINT("ib_log", ("write " LSN_PF
" file " ULINTPF " header",
start_lsn, nth_file));
log_sys->n_log_ios++;
MONITOR_INC(MONITOR_LOG_IO);
srv_stats.os_log_pending_writes.inc();
const ulint page_no
= (ulint) (dest_offset / univ_page_size.physical());
fil_io(IORequestLogWrite, true,
page_id_t(SRV_LOG_SPACE_FIRST_ID, page_no),
univ_page_size,
(ulint) (dest_offset % univ_page_size.physical()),
OS_FILE_LOG_BLOCK_SIZE, buf, group);
srv_stats.os_log_pending_writes.dec();
}
/******************************************************//**
Stores a 4-byte checksum to the trailer checksum field of a log block
before writing it to a log file. This checksum is used in recovery to
check the consistency of a log block. */
static
void
log_block_store_checksum(
/*=====================*/
byte* block) /*!< in/out: pointer to a log block */
{
log_block_set_checksum(block, log_block_calc_checksum(block));
}
/******************************************************//**
Writes a buffer to a log file group. */
static
void
log_group_write_buf(
/*================*/
log_group_t* group, /*!< in: log group */
byte* buf, /*!< in: buffer */
ulint len, /*!< in: buffer len; must be divisible
by OS_FILE_LOG_BLOCK_SIZE */
#ifdef UNIV_DEBUG
ulint pad_len, /*!< in: pad len in the buffer len */
#endif /* UNIV_DEBUG */
lsn_t start_lsn, /*!< in: start lsn of the buffer; must
be divisible by
OS_FILE_LOG_BLOCK_SIZE */
ulint new_data_offset)/*!< in: start offset of new data in
buf: this parameter is used to decide
if we have to write a new log file
header */
{
ulint write_len;
bool write_header = new_data_offset == 0;
lsn_t next_offset;
ulint i;
ut_ad(log_write_mutex_own());
ut_ad(!recv_no_log_write);
ut_a(len % OS_FILE_LOG_BLOCK_SIZE == 0);
ut_a(start_lsn % OS_FILE_LOG_BLOCK_SIZE == 0);
loop:
if (len == 0) {
return;
}
next_offset = log_group_calc_lsn_offset(start_lsn, group);
if (write_header
&& next_offset % group->file_size == LOG_FILE_HDR_SIZE) {
/* We start to write a new log file instance in the group */
ut_a(next_offset / group->file_size <= ULINT_MAX);
log_group_file_header_flush(group, (ulint)
(next_offset / group->file_size),
start_lsn);
srv_stats.os_log_written.add(OS_FILE_LOG_BLOCK_SIZE);
srv_stats.log_writes.inc();
}
if ((next_offset % group->file_size) + len > group->file_size) {
/* if the above condition holds, then the below expression
is < len which is ulint, so the typecast is ok */
write_len = (ulint)
(group->file_size - (next_offset % group->file_size));
} else {
write_len = len;
}
DBUG_PRINT("ib_log",
("write " LSN_PF " to " LSN_PF
": len " ULINTPF
" blocks " ULINTPF ".." ULINTPF,
start_lsn, next_offset,
write_len,
log_block_get_hdr_no(buf),
log_block_get_hdr_no(
buf + write_len
- OS_FILE_LOG_BLOCK_SIZE)));
ut_ad(pad_len >= len
|| log_block_get_hdr_no(buf)
== log_block_convert_lsn_to_no(start_lsn));