-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
log0log.cc
2134 lines (1699 loc) · 57.5 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, 2022, 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, Fifth Floor, Boston, MA 02110-1335 USA
*****************************************************************************/
/**************************************************//**
@file log/log0log.cc
Database log
Created 12/9/1995 Heikki Tuuri
*******************************************************/
#include "univ.i"
#include <debug_sync.h>
#include <my_service_manager.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())
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;
/** Whether to require checksums on the redo log pages */
my_bool innodb_log_checksums;
/* 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 \
+ (4U << srv_page_size_shift))
/* 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*);
/****************************************************************//**
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(ulong len)
{
const ulong new_buf_size = ut_calc_align(len, srv_page_size);
byte* new_buf = static_cast<byte*>(ut_malloc_dontdump(new_buf_size));
TRASH_ALLOC(new_buf, new_buf_size);
byte* new_flush_buf =
static_cast<byte*>(ut_malloc_dontdump(new_buf_size));
TRASH_ALLOC(new_flush_buf, new_buf_size);
log_mutex_enter();
if (len <= srv_log_buffer_size) {
/* Already extended enough by the others */
log_mutex_exit();
ut_free_dodump(new_buf, new_buf_size);
ut_free_dodump(new_flush_buf, new_buf_size);
return;
}
ib::warn() << "The redo log transaction size " << len <<
" exceeds innodb_log_buffer_size="
<< srv_log_buffer_size << " / 2). Trying to extend it.";
byte* old_buf = log_sys.buf;
byte* old_flush_buf = log_sys.flush_buf;
const ulong old_buf_size = srv_log_buffer_size;
srv_log_buffer_size = new_buf_size;
log_sys.buf = new_buf;
log_sys.flush_buf = new_flush_buf;
memcpy(new_buf, old_buf, log_sys.buf_free);
log_sys.max_buf_free = new_buf_size / LOG_BUF_FLUSH_RATIO
- LOG_BUF_FLUSH_MARGIN;
log_mutex_exit();
ut_free_dodump(old_buf, old_buf_size);
ut_free_dodump(old_flush_buf, old_buf_size);
ib::info() << "innodb_log_buffer_size was extended to "
<< new_buf_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());
const ulint framing_size = log_sys.framing_size();
/* actual length stored per block */
const ulint len_per_blk = OS_FILE_LOG_BLOCK_SIZE - framing_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) * framing_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);
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());
/* 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 > srv_log_buffer_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 */
{
ulint len;
ut_ad(log_mutex_own());
const ulint trailer_offset = log_sys.trailer_offset();
part_loop:
/* Calculate a part length */
ulint data_len = (log_sys.buf_free % OS_FILE_LOG_BLOCK_SIZE) + str_len;
if (data_len <= trailer_offset) {
/* The string fits within the current log block */
len = str_len;
} else {
data_len = trailer_offset;
len = trailer_offset
- log_sys.buf_free % OS_FILE_LOG_BLOCK_SIZE;
}
memcpy(log_sys.buf + log_sys.buf_free, str, len);
str_len -= len;
str = str + len;
byte* log_block = static_cast<byte*>(
ut_align_down(log_sys.buf + log_sys.buf_free,
OS_FILE_LOG_BLOCK_SIZE));
log_block_set_data_len(log_block, data_len);
if (data_len == trailer_offset) {
/* 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_sys.framing_size();
log_sys.lsn += len;
/* Initialize the next block header */
log_block_init(log_block + OS_FILE_LOG_BLOCK_SIZE,
log_sys.lsn);
} else {
log_sys.lsn += len;
}
log_sys.buf_free += ulong(len);
ut_ad(log_sys.buf_free <= srv_log_buffer_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;
lsn_t checkpoint_age;
ut_ad(log_mutex_own());
lsn = log_sys.lsn;
log_block = static_cast<byte*>(
ut_align_down(log_sys.buf + log_sys.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_sys.buf_free > log_sys.max_buf_free) {
log_sys.check_flush_or_checkpoint = true;
}
checkpoint_age = lsn - log_sys.last_checkpoint_lsn;
if (checkpoint_age >= log_sys.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_sys.log_group_capacity
<< ".";
}
}
if (checkpoint_age <= log_sys.max_modified_age_sync) {
goto function_exit;
}
oldest_lsn = buf_pool_get_oldest_modification();
if (!oldest_lsn
|| lsn - oldest_lsn > log_sys.max_modified_age_sync
|| checkpoint_age > log_sys.max_checkpoint_age_async) {
log_sys.check_flush_or_checkpoint = true;
}
function_exit:
return(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);
}
/** Initialize the redo log subsystem. */
void log_t::create()
{
ut_ad(this == &log_sys);
ut_ad(!is_initialised());
m_initialised= true;
mutex_create(LATCH_ID_LOG_SYS, &mutex);
mutex_create(LATCH_ID_LOG_WRITE, &write_mutex);
mutex_create(LATCH_ID_LOG_FLUSH_ORDER, &log_flush_order_mutex);
/* Start the lsn from one log block from zero: this way every
log record has a non-zero start lsn, a fact which we will use */
lsn= LOG_START_LSN;
ut_ad(srv_log_buffer_size >= 16 * OS_FILE_LOG_BLOCK_SIZE);
ut_ad(srv_log_buffer_size >= 4U << srv_page_size_shift);
buf= static_cast<byte*>(ut_malloc_dontdump(srv_log_buffer_size));
TRASH_ALLOC(buf, srv_log_buffer_size);
flush_buf= static_cast<byte*>(ut_malloc_dontdump(srv_log_buffer_size));
TRASH_ALLOC(flush_buf, srv_log_buffer_size);
max_buf_free= srv_log_buffer_size / LOG_BUF_FLUSH_RATIO -
LOG_BUF_FLUSH_MARGIN;
check_flush_or_checkpoint= true;
n_log_ios_old= n_log_ios;
last_printout_time= time(NULL);
buf_next_to_write= 0;
write_lsn= lsn;
flushed_to_disk_lsn= 0;
n_pending_flushes= 0;
flush_event = os_event_create("log_flush_event");
os_event_set(flush_event);
n_log_ios= 0;
n_log_ios_old= 0;
log_group_capacity= 0;
max_modified_age_async= 0;
max_modified_age_sync= 0;
max_checkpoint_age_async= 0;
max_checkpoint_age= 0;
next_checkpoint_no= 0;
next_checkpoint_lsn= 0;
append_on_checkpoint= NULL;
n_pending_checkpoint_writes= 0;
last_checkpoint_lsn= lsn;
rw_lock_create(checkpoint_lock_key, &checkpoint_lock, SYNC_NO_ORDER_CHECK);
log_block_init(buf, lsn);
log_block_set_first_rec_group(buf, LOG_BLOCK_HDR_SIZE);
buf_free= LOG_BLOCK_HDR_SIZE;
lsn= LOG_START_LSN + LOG_BLOCK_HDR_SIZE;
MONITOR_SET(MONITOR_LSN_CHECKPOINT_AGE, lsn - 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_t::files::create(ulint n_files)
{
ut_ad(n_files <= SRV_N_LOG_FILES_MAX);
ut_ad(this == &log_sys.log);
ut_ad(log_sys.is_initialised());
this->n_files= n_files;
format= srv_encrypt_log ? log_t::FORMAT_ENC_10_4 : log_t::FORMAT_10_4;
subformat= 2;
file_size= srv_log_file_size;
lsn= LOG_START_LSN;
lsn_offset= LOG_FILE_HDR_SIZE;
}
/******************************************************//**
Writes a log file header to a log file space. */
static
void
log_file_header_flush(
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 */
{
lsn_t dest_offset;
ut_ad(log_write_mutex_own());
ut_ad(!recv_no_log_write);
ut_a(nth_file < log_sys.log.n_files);
ut_ad(log_sys.log.format == log_t::FORMAT_10_4
|| log_sys.log.format == log_t::FORMAT_ENC_10_4);
// man 2 open suggests this buffer to be aligned by 512 for O_DIRECT
MY_ALIGNED(OS_FILE_LOG_BLOCK_SIZE)
byte buf[OS_FILE_LOG_BLOCK_SIZE] = {0};
mach_write_to_4(buf + LOG_HEADER_FORMAT, log_sys.log.format);
mach_write_to_4(buf + LOG_HEADER_SUBFORMAT, log_sys.log.subformat);
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 * log_sys.log.file_size;
DBUG_PRINT("ib_log", ("write " LSN_PF
" file " ULINTPF " header",
start_lsn, nth_file));
log_sys.n_log_ios++;
srv_stats.os_log_pending_writes.inc();
const ulint page_no = ulint(dest_offset >> srv_page_size_shift);
fil_io(IORequestLogWrite, true,
page_id_t(SRV_LOG_SPACE_FIRST_ID, page_no),
0,
ulint(dest_offset & (srv_page_size - 1)),
OS_FILE_LOG_BLOCK_SIZE, buf, NULL);
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_crc32(block));
}
/******************************************************//**
Writes a buffer to a log file. */
static
void
log_write_buf(
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_sys.log.calc_lsn_offset(start_lsn);
if (write_header
&& next_offset % log_sys.log.file_size == LOG_FILE_HDR_SIZE) {
/* We start to write a new log file instance in the group */
ut_a(next_offset / log_sys.log.file_size <= ULINT_MAX);
log_file_header_flush(
ulint(next_offset / log_sys.log.file_size), start_lsn);
srv_stats.os_log_written.add(OS_FILE_LOG_BLOCK_SIZE);
srv_stats.log_writes.inc();
}
if ((next_offset % log_sys.log.file_size) + len
> log_sys.log.file_size) {
/* if the above condition holds, then the below expression
is < len which is ulint, so the typecast is ok */
write_len = ulint(log_sys.log.file_size
- (next_offset % log_sys.log.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));
/* Calculate the checksums for each log block and write them to
the trailer fields of the log blocks */
for (i = 0; i < write_len / OS_FILE_LOG_BLOCK_SIZE; i++) {
#ifdef UNIV_DEBUG
ulint hdr_no_2 = log_block_get_hdr_no(buf) + i;
DBUG_EXECUTE_IF("innodb_small_log_block_no_limit",
hdr_no_2 = ((hdr_no_2 - 1) & 0xFUL) + 1;);
#endif
ut_ad(pad_len >= len
|| i * OS_FILE_LOG_BLOCK_SIZE >= len - pad_len
|| log_block_get_hdr_no(buf + i * OS_FILE_LOG_BLOCK_SIZE) == hdr_no_2);
log_block_store_checksum(buf + i * OS_FILE_LOG_BLOCK_SIZE);
}
log_sys.n_log_ios++;
srv_stats.os_log_pending_writes.inc();
ut_a((next_offset >> srv_page_size_shift) <= ULINT_MAX);
const ulint page_no = ulint(next_offset >> srv_page_size_shift);
fil_io(IORequestLogWrite, true,
page_id_t(SRV_LOG_SPACE_FIRST_ID, page_no),
0,
ulint(next_offset & (srv_page_size - 1)), write_len, buf, NULL);
srv_stats.os_log_pending_writes.dec();
srv_stats.os_log_written.add(write_len);
srv_stats.log_writes.inc();
if (write_len < len) {
start_lsn += write_len;
len -= write_len;
buf += write_len;
write_header = true;
goto loop;
}
}
/** Flush the recently written changes to the log file.
and invoke log_mutex_enter(). */
static void log_write_flush_to_disk_low()
{
ut_a(log_sys.n_pending_flushes);
bool do_flush = srv_file_flush_method != SRV_O_DSYNC;
if (do_flush) {
fil_flush(SRV_LOG_SPACE_FIRST_ID);
}
log_mutex_enter();
if (do_flush) {
log_sys.flushed_to_disk_lsn = log_sys.current_flush_lsn;
}
log_sys.n_pending_flushes--;
os_event_set(log_sys.flush_event);
}
/** Swap log buffers, and copy the content of last block
from old buf to the head of the new buf. Thus, buf_free and
buf_next_to_write would be changed accordingly */
static inline
void
log_buffer_switch()
{
ut_ad(log_mutex_own());
ut_ad(log_write_mutex_own());
ulong area_end = ut_calc_align(
log_sys.buf_free, ulong(OS_FILE_LOG_BLOCK_SIZE));
/* Copy the last block to new buf */
ut_memcpy(log_sys.flush_buf,
log_sys.buf + area_end - OS_FILE_LOG_BLOCK_SIZE,
OS_FILE_LOG_BLOCK_SIZE);
std::swap(log_sys.buf, log_sys.flush_buf);
log_sys.buf_free %= OS_FILE_LOG_BLOCK_SIZE;
log_sys.buf_next_to_write = log_sys.buf_free;
}
/** Ensure that the log has been written to the log file up to a given
log entry (such as that of a transaction commit). Start a new write, or
wait and check if an already running write is covering the request.
@param[in] lsn log sequence number that should be
included in the redo log file write
@param[in] flush_to_disk whether the written log should also
be flushed to the file system
@param[in] rotate_key whether to rotate the encryption key */
void log_write_up_to(lsn_t lsn, bool flush_to_disk, bool rotate_key)
{
#ifdef UNIV_DEBUG
ulint loop_count = 0;
#endif /* UNIV_DEBUG */
byte* write_buf;
lsn_t write_lsn;
ut_ad(!srv_read_only_mode);
ut_ad(!rotate_key || flush_to_disk);
if (recv_no_ibuf_operations) {
/* Recovery is running and no operations on the log files are
allowed yet (the variable name .._no_ibuf_.. is misleading) */
return;
}
loop:
ut_ad(++loop_count < 128);
#if UNIV_WORD_SIZE > 7
/* We can do a dirty read of LSN. */
/* NOTE: Currently doesn't do dirty read for
(flush_to_disk == true) case, because the log_mutex
contention also works as the arbitrator for write-IO
(fsync) bandwidth between log files and data files. */
if (!flush_to_disk && log_sys.write_lsn >= lsn) {
return;
}
#endif
log_write_mutex_enter();
lsn_t limit_lsn = flush_to_disk
? log_sys.flushed_to_disk_lsn
: log_sys.write_lsn;
if (limit_lsn >= lsn) {
log_write_mutex_exit();
return;
}
ut_ad(!recv_no_log_write);
/* If it is a write call we should just go ahead and do it
as we checked that write_lsn is not where we'd like it to
be. If we have to flush as well then we check if there is a
pending flush and based on that we wait for it to finish
before proceeding further. */
if (flush_to_disk
&& (log_sys.n_pending_flushes > 0
|| !os_event_is_set(log_sys.flush_event))) {
/* Figure out if the current flush will do the job
for us. */
bool work_done = log_sys.current_flush_lsn >= lsn;
log_write_mutex_exit();
os_event_wait(log_sys.flush_event);
if (work_done) {
return;
} else {
goto loop;
}
}
log_mutex_enter();
if (!flush_to_disk
&& log_sys.buf_free == log_sys.buf_next_to_write) {
/* Nothing to write and no flush to disk requested */
log_mutex_exit_all();
return;
}
ulint start_offset;
ulint end_offset;
ulint area_start;
ulint area_end;
ulong write_ahead_size = srv_log_write_ahead_size;
ulint pad_size;
DBUG_PRINT("ib_log", ("write " LSN_PF " to " LSN_PF,
log_sys.write_lsn,
log_sys.lsn));
if (flush_to_disk) {
log_sys.n_pending_flushes++;
log_sys.current_flush_lsn = log_sys.lsn;
os_event_reset(log_sys.flush_event);
if (log_sys.buf_free == log_sys.buf_next_to_write) {
/* Nothing to write, flush only */
log_mutex_exit_all();
log_write_flush_to_disk_low();
log_mutex_exit();
return;
}
}
start_offset = log_sys.buf_next_to_write;
end_offset = log_sys.buf_free;
area_start = ut_2pow_round(start_offset,
ulint(OS_FILE_LOG_BLOCK_SIZE));
area_end = ut_calc_align(end_offset, ulint(OS_FILE_LOG_BLOCK_SIZE));
ut_ad(area_end - area_start > 0);
log_block_set_flush_bit(log_sys.buf + area_start, TRUE);
log_block_set_checkpoint_no(
log_sys.buf + area_end - OS_FILE_LOG_BLOCK_SIZE,
log_sys.next_checkpoint_no);
write_lsn = log_sys.lsn;
write_buf = log_sys.buf;
log_buffer_switch();
log_sys.log.set_fields(log_sys.write_lsn);
log_mutex_exit();
/* Erase the end of the last log block. */
memset(write_buf + end_offset, 0,
~end_offset & (OS_FILE_LOG_BLOCK_SIZE - 1));
/* Calculate pad_size if needed. */
pad_size = 0;
if (write_ahead_size > OS_FILE_LOG_BLOCK_SIZE) {
ulint end_offset_in_unit;
lsn_t end_offset = log_sys.log.calc_lsn_offset(
ut_uint64_align_up(write_lsn, OS_FILE_LOG_BLOCK_SIZE));
end_offset_in_unit = (ulint) (end_offset % write_ahead_size);
if (end_offset_in_unit > 0
&& (area_end - area_start) > end_offset_in_unit) {
/* The first block in the unit was initialized
after the last writing.
Needs to be written padded data once. */
pad_size = std::min<ulint>(
ulint(write_ahead_size) - end_offset_in_unit,
srv_log_buffer_size - area_end);
::memset(write_buf + area_end, 0, pad_size);
}
}
if (UNIV_UNLIKELY(srv_shutdown_state > SRV_SHUTDOWN_INITIATED)) {
service_manager_extend_timeout(INNODB_EXTEND_TIMEOUT_INTERVAL,
"InnoDB log write: "
LSN_PF "," LSN_PF,
log_sys.write_lsn, lsn);