/
log0recv.cc
4050 lines (3241 loc) · 103 KB
/
log0recv.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) 1997, 2016, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2012, Facebook Inc.
Copyright (c) 2013, 2017, MariaDB Corporation. All Rights Reserved.
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/log0recv.cc
Recovery
Created 9/20/1997 Heikki Tuuri
*******************************************************/
// First include (the generated) my_config.h, to get correct platform defines.
#include "my_config.h"
#include <stdio.h> // Solaris/x86 header file bug
#include <vector>
#include <my_systemd.h>
#include "log0recv.h"
#ifdef UNIV_NONINL
#include "log0recv.ic"
#endif
#include "log0crypt.h"
#include "mem0mem.h"
#include "buf0buf.h"
#include "buf0flu.h"
#include "mtr0mtr.h"
#include "mtr0log.h"
#include "page0cur.h"
#include "page0zip.h"
#include "btr0btr.h"
#include "btr0cur.h"
#include "ibuf0ibuf.h"
#include "trx0undo.h"
#include "trx0rec.h"
#include "fil0fil.h"
#include "fil0crypt.h"
#ifndef UNIV_HOTBACKUP
# include "buf0rea.h"
# include "srv0srv.h"
# include "srv0start.h"
# include "trx0roll.h"
# include "row0merge.h"
# include "sync0sync.h"
#else /* !UNIV_HOTBACKUP */
/** This is set to FALSE if the backup was originally taken with the
mysqlbackup --include regexp option: then we do not want to create tables in
directories which were not included */
UNIV_INTERN ibool recv_replay_file_ops = TRUE;
#endif /* !UNIV_HOTBACKUP */
/** Log records are stored in the hash table in chunks at most of this size;
this must be less than UNIV_PAGE_SIZE as it is stored in the buffer pool */
#define RECV_DATA_BLOCK_SIZE (MEM_MAX_ALLOC_IN_BUF - sizeof(recv_data_t))
/** Read-ahead area in applying log records to file pages */
#define RECV_READ_AHEAD_AREA 32
/** The recovery system */
UNIV_INTERN recv_sys_t* recv_sys = NULL;
/** TRUE when applying redo log records during crash recovery; FALSE
otherwise. Note that this is FALSE while a background thread is
rolling back incomplete transactions. */
UNIV_INTERN ibool recv_recovery_on;
#ifdef UNIV_LOG_ARCHIVE
/** TRUE when applying redo log records from an archived log file */
UNIV_INTERN ibool recv_recovery_from_backup_on;
#endif /* UNIV_LOG_ARCHIVE */
#ifndef UNIV_HOTBACKUP
/** TRUE when recv_init_crash_recovery() has been called. */
UNIV_INTERN ibool recv_needed_recovery;
# ifdef UNIV_DEBUG
/** TRUE if writing to the redo log (mtr_commit) is forbidden.
Protected by log_sys->mutex. */
UNIV_INTERN ibool recv_no_log_write = FALSE;
# endif /* UNIV_DEBUG */
/** TRUE if buf_page_is_corrupted() should check if the log sequence
number (FIL_PAGE_LSN) is in the future. Initially FALSE, and set by
recv_recovery_from_checkpoint_start_func(). */
UNIV_INTERN ibool recv_lsn_checks_on;
/** There are two conditions under which we scan the logs, the first
is normal startup and the second is when we do a recovery from an
archive.
This flag is set if we are doing a scan from the last checkpoint during
startup. If we find log entries that were written after the last checkpoint
we know that the server was not cleanly shutdown. We must then initialize
the crash recovery environment before attempting to store these entries in
the log hash table. */
static ibool recv_log_scan_is_startup_type;
/** If the following is TRUE, the buffer pool file pages must be invalidated
after recovery and no ibuf operations are allowed; this becomes TRUE if
the log record hash table becomes too full, and log records must be merged
to file pages already before the recovery is finished: in this case no
ibuf operations are allowed, as they could modify the pages read in the
buffer pool before the pages have been recovered to the up-to-date state.
TRUE means that recovery is running and no operations on the log files
are allowed yet: the variable name is misleading. */
UNIV_INTERN ibool recv_no_ibuf_operations;
/** TRUE when the redo log is being backed up */
# define recv_is_making_a_backup FALSE
/** TRUE when recovering from a backed up redo log file */
# define recv_is_from_backup FALSE
#else /* !UNIV_HOTBACKUP */
# define recv_needed_recovery FALSE
/** TRUE when the redo log is being backed up */
UNIV_INTERN ibool recv_is_making_a_backup = FALSE;
/** TRUE when recovering from a backed up redo log file */
UNIV_INTERN ibool recv_is_from_backup = FALSE;
# define buf_pool_get_curr_size() (5 * 1024 * 1024)
#endif /* !UNIV_HOTBACKUP */
/** The following counter is used to decide when to print info on
log scan */
static ulint recv_scan_print_counter;
/** The type of the previous parsed redo log record */
static ulint recv_previous_parsed_rec_type;
/** The offset of the previous parsed redo log record */
static ulint recv_previous_parsed_rec_offset;
/** The 'multi' flag of the previous parsed redo log record */
static ulint recv_previous_parsed_rec_is_multi;
/** Maximum page number encountered in the redo log */
UNIV_INTERN ulint recv_max_parsed_page_no;
/** This many frames must be left free in the buffer pool when we scan
the log and store the scanned log records in the buffer pool: we will
use these free frames to read in pages when we start applying the
log records to the database.
This is the default value. If the actual size of the buffer pool is
larger than 10 MB we'll set this value to 512. */
UNIV_INTERN ulint recv_n_pool_free_frames;
/** The maximum lsn we see for a page during the recovery process. If this
is bigger than the lsn we are able to scan up to, that is an indication that
the recovery failed and the database may be corrupt. */
UNIV_INTERN lsn_t recv_max_page_lsn;
#ifdef UNIV_PFS_THREAD
UNIV_INTERN mysql_pfs_key_t trx_rollback_clean_thread_key;
#endif /* UNIV_PFS_THREAD */
#ifdef UNIV_PFS_MUTEX
UNIV_INTERN mysql_pfs_key_t recv_sys_mutex_key;
#endif /* UNIV_PFS_MUTEX */
#ifndef UNIV_HOTBACKUP
# ifdef UNIV_PFS_THREAD
UNIV_INTERN mysql_pfs_key_t recv_writer_thread_key;
# endif /* UNIV_PFS_THREAD */
# ifdef UNIV_PFS_MUTEX
UNIV_INTERN mysql_pfs_key_t recv_writer_mutex_key;
# endif /* UNIV_PFS_MUTEX */
/** Flag indicating if recv_writer thread is active. */
static volatile bool recv_writer_thread_active;
UNIV_INTERN os_thread_t recv_writer_thread_handle = 0;
#endif /* !UNIV_HOTBACKUP */
/* prototypes */
#ifndef UNIV_HOTBACKUP
/*******************************************************//**
Initialize crash recovery environment. Can be called iff
recv_needed_recovery == FALSE. */
static
void
recv_init_crash_recovery(void);
/*===========================*/
#endif /* !UNIV_HOTBACKUP */
/********************************************************//**
Creates the recovery system. */
UNIV_INTERN
void
recv_sys_create(void)
/*=================*/
{
if (recv_sys != NULL) {
return;
}
recv_sys = static_cast<recv_sys_t*>(mem_zalloc(sizeof(*recv_sys)));
mutex_create(recv_sys_mutex_key, &recv_sys->mutex, SYNC_RECV);
#ifndef UNIV_HOTBACKUP
mutex_create(recv_writer_mutex_key, &recv_sys->writer_mutex,
SYNC_LEVEL_VARYING);
#endif /* !UNIV_HOTBACKUP */
recv_sys->heap = NULL;
recv_sys->addr_hash = NULL;
}
/********************************************************//**
Release recovery system mutexes. */
UNIV_INTERN
void
recv_sys_close(void)
/*================*/
{
if (recv_sys != NULL) {
if (recv_sys->addr_hash != NULL) {
hash_table_free(recv_sys->addr_hash);
}
if (recv_sys->heap != NULL) {
mem_heap_free(recv_sys->heap);
}
if (recv_sys->buf != NULL) {
ut_free(recv_sys->buf);
}
if (recv_sys->last_block_buf_start != NULL) {
mem_free(recv_sys->last_block_buf_start);
}
#ifndef UNIV_HOTBACKUP
ut_ad(!recv_writer_thread_active);
mutex_free(&recv_sys->writer_mutex);
#endif /* !UNIV_HOTBACKUP */
mutex_free(&recv_sys->mutex);
mem_free(recv_sys);
recv_sys = NULL;
}
}
/********************************************************//**
Frees the recovery system memory. */
UNIV_INTERN
void
recv_sys_mem_free(void)
/*===================*/
{
if (recv_sys != NULL) {
if (recv_sys->addr_hash != NULL) {
hash_table_free(recv_sys->addr_hash);
}
if (recv_sys->heap != NULL) {
mem_heap_free(recv_sys->heap);
}
if (recv_sys->buf != NULL) {
ut_free(recv_sys->buf);
}
if (recv_sys->last_block_buf_start != NULL) {
mem_free(recv_sys->last_block_buf_start);
}
mem_free(recv_sys);
recv_sys = NULL;
}
}
#ifndef UNIV_HOTBACKUP
/************************************************************
Reset the state of the recovery system variables. */
UNIV_INTERN
void
recv_sys_var_init(void)
/*===================*/
{
recv_lsn_checks_on = FALSE;
recv_n_pool_free_frames = 256;
recv_recovery_on = FALSE;
#ifdef UNIV_LOG_ARCHIVE
recv_recovery_from_backup_on = FALSE;
#endif /* UNIV_LOG_ARCHIVE */
recv_needed_recovery = FALSE;
recv_lsn_checks_on = FALSE;
recv_log_scan_is_startup_type = FALSE;
recv_no_ibuf_operations = FALSE;
recv_scan_print_counter = 0;
recv_previous_parsed_rec_type = 999999;
recv_previous_parsed_rec_offset = 0;
recv_previous_parsed_rec_is_multi = 0;
recv_max_parsed_page_no = 0;
recv_n_pool_free_frames = 256;
recv_max_page_lsn = 0;
}
/******************************************************************//**
recv_writer thread tasked with flushing dirty pages from the buffer
pools.
@return a dummy parameter */
extern "C" UNIV_INTERN
os_thread_ret_t
DECLARE_THREAD(recv_writer_thread)(
/*===============================*/
void* arg MY_ATTRIBUTE((unused)))
/*!< in: a dummy parameter required by
os_thread_create */
{
ut_ad(!srv_read_only_mode);
#ifdef UNIV_PFS_THREAD
pfs_register_thread(recv_writer_thread_key);
#endif /* UNIV_PFS_THREAD */
#ifdef UNIV_DEBUG_THREAD_CREATION
fprintf(stderr, "InnoDB: recv_writer thread running, id %lu\n",
os_thread_pf(os_thread_get_curr_id()));
#endif /* UNIV_DEBUG_THREAD_CREATION */
while (srv_shutdown_state == SRV_SHUTDOWN_NONE) {
/* Wait till we get a signal to clean the LRU list.
Bounded by max wait time of 100ms. */
ib_int64_t sig_count = os_event_reset(buf_flush_event);
os_event_wait_time_low(buf_flush_event, 100000, sig_count);
mutex_enter(&recv_sys->writer_mutex);
if (!recv_recovery_on) {
mutex_exit(&recv_sys->writer_mutex);
break;
}
/* Flush pages from end of LRU if required */
buf_flush_LRU_tail();
mutex_exit(&recv_sys->writer_mutex);
}
recv_writer_thread_active = false;
/* We count the number of threads in os_thread_exit().
A created thread should always use that to exit and not
use return() to exit. */
os_thread_exit(NULL);
OS_THREAD_DUMMY_RETURN;
}
#endif /* !UNIV_HOTBACKUP */
/************************************************************
Inits the recovery system for a recovery operation. */
UNIV_INTERN
void
recv_sys_init(
/*==========*/
ulint available_memory) /*!< in: available memory in bytes */
{
if (recv_sys->heap != NULL) {
return;
}
#ifndef UNIV_HOTBACKUP
mutex_enter(&(recv_sys->mutex));
recv_sys->heap = mem_heap_create_typed(256,
MEM_HEAP_FOR_RECV_SYS);
#else /* !UNIV_HOTBACKUP */
recv_sys->heap = mem_heap_create(256);
recv_is_from_backup = TRUE;
#endif /* !UNIV_HOTBACKUP */
/* Set appropriate value of recv_n_pool_free_frames. */
if (buf_pool_get_curr_size() >= (10 * 1024 * 1024)) {
/* Buffer pool of size greater than 10 MB. */
recv_n_pool_free_frames = 512;
}
recv_sys->buf = static_cast<byte*>(ut_malloc(RECV_PARSING_BUF_SIZE));
recv_sys->len = 0;
recv_sys->recovered_offset = 0;
recv_sys->addr_hash = hash_create(available_memory / 512);
recv_sys->n_addrs = 0;
recv_sys->apply_log_recs = FALSE;
recv_sys->apply_batch_on = FALSE;
recv_sys->last_block_buf_start = static_cast<byte*>(
mem_alloc(2 * OS_FILE_LOG_BLOCK_SIZE));
recv_sys->last_block = static_cast<byte*>(ut_align(
recv_sys->last_block_buf_start, OS_FILE_LOG_BLOCK_SIZE));
recv_sys->found_corrupt_log = FALSE;
recv_max_page_lsn = 0;
/* Call the constructor for recv_sys_t::dblwr member */
new (&recv_sys->dblwr) recv_dblwr_t();
mutex_exit(&(recv_sys->mutex));
}
/********************************************************//**
Empties the hash table when it has been fully processed.
@return DB_SUCCESS when successfull or DB_ERROR when fails. */
static
dberr_t
recv_sys_empty_hash(void)
/*=====================*/
{
ut_ad(mutex_own(&(recv_sys->mutex)));
if (recv_sys->n_addrs != 0) {
fprintf(stderr,
"InnoDB: Error: %lu pages with log records"
" were left unprocessed!\n"
"InnoDB: Maximum page number with"
" log records on it %lu\n",
(ulong) recv_sys->n_addrs,
(ulong) recv_max_parsed_page_no);
return DB_ERROR;
}
hash_table_free(recv_sys->addr_hash);
mem_heap_empty(recv_sys->heap);
recv_sys->addr_hash = hash_create(buf_pool_get_curr_size() / 512);
return DB_SUCCESS;
}
#ifndef UNIV_HOTBACKUP
# ifndef UNIV_LOG_DEBUG
/********************************************************//**
Frees the recovery system. */
static
void
recv_sys_debug_free(void)
/*=====================*/
{
mutex_enter(&(recv_sys->mutex));
hash_table_free(recv_sys->addr_hash);
mem_heap_free(recv_sys->heap);
ut_free(recv_sys->buf);
mem_free(recv_sys->last_block_buf_start);
recv_sys->buf = NULL;
recv_sys->heap = NULL;
recv_sys->addr_hash = NULL;
recv_sys->last_block_buf_start = NULL;
mutex_exit(&(recv_sys->mutex));
/* Free up the flush_rbt. */
buf_flush_free_flush_rbt();
}
# endif /* UNIV_LOG_DEBUG */
# ifdef UNIV_LOG_ARCHIVE
/********************************************************//**
Truncates possible corrupted or extra records from a log group. */
static
void
recv_truncate_group(
/*================*/
log_group_t* group, /*!< in: log group */
lsn_t recovered_lsn, /*!< in: recovery succeeded up to this
lsn */
lsn_t limit_lsn, /*!< in: this was the limit for
recovery */
lsn_t checkpoint_lsn, /*!< in: recovery was started from this
checkpoint */
lsn_t archived_lsn) /*!< in: the log has been archived up to
this lsn */
{
lsn_t start_lsn;
lsn_t end_lsn;
lsn_t finish_lsn1;
lsn_t finish_lsn2;
lsn_t finish_lsn;
if (archived_lsn == LSN_MAX) {
/* Checkpoint was taken in the NOARCHIVELOG mode */
archived_lsn = checkpoint_lsn;
}
finish_lsn1 = ut_uint64_align_down(archived_lsn,
OS_FILE_LOG_BLOCK_SIZE)
+ log_group_get_capacity(group);
finish_lsn2 = ut_uint64_align_up(recovered_lsn,
OS_FILE_LOG_BLOCK_SIZE)
+ recv_sys->last_log_buf_size;
if (limit_lsn != LSN_MAX) {
/* We do not know how far we should erase log records: erase
as much as possible */
finish_lsn = finish_lsn1;
} else {
/* It is enough to erase the length of the log buffer */
finish_lsn = finish_lsn1 < finish_lsn2
? finish_lsn1 : finish_lsn2;
}
ut_a(RECV_SCAN_SIZE <= log_sys->buf_size);
memset(log_sys->buf, 0, RECV_SCAN_SIZE);
start_lsn = ut_uint64_align_down(recovered_lsn,
OS_FILE_LOG_BLOCK_SIZE);
if (start_lsn != recovered_lsn) {
/* Copy the last incomplete log block to the log buffer and
edit its data length: */
lsn_t diff = recovered_lsn - start_lsn;
ut_a(diff <= 0xFFFFUL);
ut_memcpy(log_sys->buf, recv_sys->last_block,
OS_FILE_LOG_BLOCK_SIZE);
log_block_set_data_len(log_sys->buf, (ulint) diff);
}
if (start_lsn >= finish_lsn) {
return;
}
for (;;) {
ulint len;
end_lsn = start_lsn + RECV_SCAN_SIZE;
if (end_lsn > finish_lsn) {
end_lsn = finish_lsn;
}
len = (ulint) (end_lsn - start_lsn);
log_group_write_buf(group, log_sys->buf, len, start_lsn, 0);
if (end_lsn >= finish_lsn) {
return;
}
memset(log_sys->buf, 0, RECV_SCAN_SIZE);
start_lsn = end_lsn;
}
}
/********************************************************//**
Copies the log segment between group->recovered_lsn and recovered_lsn from the
most up-to-date log group to group, so that it contains the latest log data. */
static
void
recv_copy_group(
/*============*/
log_group_t* up_to_date_group, /*!< in: the most up-to-date log
group */
log_group_t* group, /*!< in: copy to this log
group */
lsn_t recovered_lsn) /*!< in: recovery succeeded up
to this lsn */
{
lsn_t start_lsn;
lsn_t end_lsn;
if (group->scanned_lsn >= recovered_lsn) {
return;
}
ut_a(RECV_SCAN_SIZE <= log_sys->buf_size);
start_lsn = ut_uint64_align_down(group->scanned_lsn,
OS_FILE_LOG_BLOCK_SIZE);
for (;;) {
ulint len;
end_lsn = start_lsn + RECV_SCAN_SIZE;
if (end_lsn > recovered_lsn) {
end_lsn = ut_uint64_align_up(recovered_lsn,
OS_FILE_LOG_BLOCK_SIZE);
}
log_group_read_log_seg(LOG_RECOVER, log_sys->buf,
up_to_date_group, start_lsn, end_lsn);
len = (ulint) (end_lsn - start_lsn);
log_group_write_buf(group, log_sys->buf, len, start_lsn, 0);
if (end_lsn >= recovered_lsn) {
return;
}
start_lsn = end_lsn;
}
}
# endif /* UNIV_LOG_ARCHIVE */
/********************************************************//**
Copies a log segment from the most up-to-date log group to the other log
groups, so that they all contain the latest log data. Also writes the info
about the latest checkpoint to the groups, and inits the fields in the group
memory structs to up-to-date values. */
static
void
recv_synchronize_groups(
/*====================*/
#ifdef UNIV_LOG_ARCHIVE
log_group_t* up_to_date_group /*!< in: the most up-to-date
log group */
#endif
)
{
lsn_t start_lsn;
lsn_t end_lsn;
lsn_t recovered_lsn;
recovered_lsn = recv_sys->recovered_lsn;
/* Read the last recovered log block to the recovery system buffer:
the block is always incomplete */
start_lsn = ut_uint64_align_down(recovered_lsn,
OS_FILE_LOG_BLOCK_SIZE);
end_lsn = ut_uint64_align_up(recovered_lsn, OS_FILE_LOG_BLOCK_SIZE);
ut_a(start_lsn != end_lsn);
log_group_read_log_seg(LOG_RECOVER, recv_sys->last_block,
#ifdef UNIV_LOG_ARCHIVE
up_to_date_group,
#else /* UNIV_LOG_ARCHIVE */
UT_LIST_GET_FIRST(log_sys->log_groups),
#endif /* UNIV_LOG_ARCHIVE */
start_lsn, end_lsn);
for (log_group_t* group = UT_LIST_GET_FIRST(log_sys->log_groups);
group;
group = UT_LIST_GET_NEXT(log_groups, group)) {
#ifdef UNIV_LOG_ARCHIVE
if (group != up_to_date_group) {
/* Copy log data if needed */
recv_copy_group(group, up_to_date_group,
recovered_lsn);
}
#endif /* UNIV_LOG_ARCHIVE */
/* Update the fields in the group struct to correspond to
recovered_lsn */
log_group_set_fields(group, recovered_lsn);
ut_a(log_sys);
}
/* Copy the checkpoint info to the groups; remember that we have
incremented checkpoint_no by one, and the info will not be written
over the max checkpoint info, thus making the preservation of max
checkpoint info on disk certain */
log_groups_write_checkpoint_info();
mutex_exit(&(log_sys->mutex));
/* Wait for the checkpoint write to complete */
rw_lock_s_lock(&(log_sys->checkpoint_lock));
rw_lock_s_unlock(&(log_sys->checkpoint_lock));
mutex_enter(&(log_sys->mutex));
}
#endif /* !UNIV_HOTBACKUP */
/***********************************************************************//**
Checks the consistency of the checkpoint info
@return TRUE if ok */
static
ibool
recv_check_cp_is_consistent(
/*========================*/
const byte* buf) /*!< in: buffer containing checkpoint info */
{
ulint fold;
fold = ut_fold_binary(buf, LOG_CHECKPOINT_CHECKSUM_1);
if ((fold & 0xFFFFFFFFUL) != mach_read_from_4(
buf + LOG_CHECKPOINT_CHECKSUM_1)) {
return(FALSE);
}
fold = ut_fold_binary(buf + LOG_CHECKPOINT_LSN,
LOG_CHECKPOINT_CHECKSUM_2 - LOG_CHECKPOINT_LSN);
if ((fold & 0xFFFFFFFFUL) != mach_read_from_4(
buf + LOG_CHECKPOINT_CHECKSUM_2)) {
return(FALSE);
}
return(TRUE);
}
#ifndef UNIV_HOTBACKUP
/********************************************************//**
Looks for the maximum consistent checkpoint from the log groups.
@return error code or DB_SUCCESS */
static MY_ATTRIBUTE((nonnull, warn_unused_result))
dberr_t
recv_find_max_checkpoint(
/*=====================*/
log_group_t** max_group, /*!< out: max group */
ulint* max_field) /*!< out: LOG_CHECKPOINT_1 or
LOG_CHECKPOINT_2 */
{
log_group_t* group;
ib_uint64_t max_no;
ib_uint64_t checkpoint_no;
ulint field;
byte* buf;
group = UT_LIST_GET_FIRST(log_sys->log_groups);
max_no = 0;
*max_group = NULL;
*max_field = 0;
buf = log_sys->checkpoint_buf;
while (group) {
group->state = LOG_GROUP_CORRUPTED;
for (field = LOG_CHECKPOINT_1; field <= LOG_CHECKPOINT_2;
field += LOG_CHECKPOINT_2 - LOG_CHECKPOINT_1) {
log_group_read_checkpoint_info(group, field);
if (!recv_check_cp_is_consistent(buf)) {
#ifdef UNIV_DEBUG
if (log_debug_writes) {
fprintf(stderr,
"InnoDB: Checkpoint in group"
" %lu at %lu invalid, %lu\n",
(ulong) group->id,
(ulong) field,
(ulong) mach_read_from_4(
buf
+ LOG_CHECKPOINT_CHECKSUM_1));
}
#endif /* UNIV_DEBUG */
goto not_consistent;
}
group->state = LOG_GROUP_OK;
group->lsn = mach_read_from_8(
buf + LOG_CHECKPOINT_LSN);
group->lsn_offset = mach_read_from_4(
buf + LOG_CHECKPOINT_OFFSET_LOW32);
group->lsn_offset |= ((lsn_t) mach_read_from_4(
buf + LOG_CHECKPOINT_OFFSET_HIGH32)) << 32;
checkpoint_no = mach_read_from_8(
buf + LOG_CHECKPOINT_NO);
if (!log_crypt_read_checkpoint_buf(buf)) {
return DB_ERROR;
}
#ifdef UNIV_DEBUG
if (log_debug_writes) {
fprintf(stderr,
"InnoDB: Checkpoint number %lu"
" found in group %lu\n",
(ulong) checkpoint_no,
(ulong) group->id);
}
#endif /* UNIV_DEBUG */
if (checkpoint_no >= max_no) {
*max_group = group;
*max_field = field;
max_no = checkpoint_no;
}
not_consistent:
;
}
group = UT_LIST_GET_NEXT(log_groups, group);
}
if (*max_group == NULL) {
fprintf(stderr,
"InnoDB: No valid checkpoint found.\n"
"InnoDB: If you are attempting downgrade"
" from MySQL 5.7.9 or later,\n"
"InnoDB: please refer to " REFMAN
"upgrading-downgrading.html\n"
"InnoDB: If this error appears when you are"
" creating an InnoDB database,\n"
"InnoDB: the problem may be that during"
" an earlier attempt you managed\n"
"InnoDB: to create the InnoDB data files,"
" but log file creation failed.\n"
"InnoDB: If that is the case, please refer to\n"
"InnoDB: " REFMAN "error-creating-innodb.html\n");
return(DB_ERROR);
}
return(DB_SUCCESS);
}
#else /* !UNIV_HOTBACKUP */
/*******************************************************************//**
Reads the checkpoint info needed in hot backup.
@return TRUE if success */
UNIV_INTERN
ibool
recv_read_checkpoint_info_for_backup(
/*=================================*/
const byte* hdr, /*!< in: buffer containing the log group
header */
lsn_t* lsn, /*!< out: checkpoint lsn */
lsn_t* offset, /*!< out: checkpoint offset in the log group */
lsn_t* cp_no, /*!< out: checkpoint number */
lsn_t* first_header_lsn)
/*!< out: lsn of of the start of the
first log file */
{
ulint max_cp = 0;
ib_uint64_t max_cp_no = 0;
const byte* cp_buf;
cp_buf = hdr + LOG_CHECKPOINT_1;
if (recv_check_cp_is_consistent(cp_buf)) {
max_cp_no = mach_read_from_8(cp_buf + LOG_CHECKPOINT_NO);
max_cp = LOG_CHECKPOINT_1;
}
cp_buf = hdr + LOG_CHECKPOINT_2;
if (recv_check_cp_is_consistent(cp_buf)) {
if (mach_read_from_8(cp_buf + LOG_CHECKPOINT_NO) > max_cp_no) {
max_cp = LOG_CHECKPOINT_2;
}
}
if (max_cp == 0) {
return(FALSE);
}
cp_buf = hdr + max_cp;
*lsn = mach_read_from_8(cp_buf + LOG_CHECKPOINT_LSN);
*offset = mach_read_from_4(
cp_buf + LOG_CHECKPOINT_OFFSET_LOW32);
*offset |= ((lsn_t) mach_read_from_4(
cp_buf + LOG_CHECKPOINT_OFFSET_HIGH32)) << 32;
*cp_no = mach_read_from_8(cp_buf + LOG_CHECKPOINT_NO);
*first_header_lsn = mach_read_from_8(hdr + LOG_FILE_START_LSN);
return(TRUE);
}
#endif /* !UNIV_HOTBACKUP */
/******************************************************//**
Checks the 4-byte checksum to the trailer checksum field of a log
block. We also accept a log block in the old format before
InnoDB-3.23.52 where the checksum field contains the log block number.
@return TRUE if ok, or if the log block may be in the format of InnoDB
version predating 3.23.52 */
ibool
log_block_checksum_is_ok_or_old_format(
/*===================================*/
const byte* block, /*!< in: pointer to a log block */
bool print_err) /*!< in print error ? */
{
#ifdef UNIV_LOG_DEBUG
return(TRUE);
#endif /* UNIV_LOG_DEBUG */
if (log_block_calc_checksum(block) == log_block_get_checksum(block)) {
return(TRUE);
}
if (log_block_get_hdr_no(block) == log_block_get_checksum(block)) {
/* We assume the log block is in the format of
InnoDB version < 3.23.52 and the block is ok */
#if 0
fprintf(stderr,
"InnoDB: Scanned old format < InnoDB-3.23.52"
" log block number %lu\n",
log_block_get_hdr_no(block));
#endif
return(TRUE);
}
if (print_err) {
fprintf(stderr, "BROKEN: block: %lu checkpoint: %lu %.8lx %.8lx\n",
log_block_get_hdr_no(block),
log_block_get_checkpoint_no(block),
log_block_calc_checksum(block),
log_block_get_checksum(block));
}
return(FALSE);
}
#ifdef UNIV_HOTBACKUP
/*******************************************************************//**
Scans the log segment and n_bytes_scanned is set to the length of valid
log scanned. */
UNIV_INTERN
void
recv_scan_log_seg_for_backup(
/*=========================*/
byte* buf, /*!< in: buffer containing log data */
ulint buf_len, /*!< in: data length in that buffer */
lsn_t* scanned_lsn, /*!< in/out: lsn of buffer start,
we return scanned lsn */
ulint* scanned_checkpoint_no,
/*!< in/out: 4 lowest bytes of the
highest scanned checkpoint number so
far */
ulint* n_bytes_scanned)/*!< out: how much we were able to
scan, smaller than buf_len if log
data ended here */
{
ulint data_len;
byte* log_block;
ulint no;
*n_bytes_scanned = 0;
for (log_block = buf; log_block < buf + buf_len;
log_block += OS_FILE_LOG_BLOCK_SIZE) {
no = log_block_get_hdr_no(log_block);
#if 0
fprintf(stderr, "Log block header no %lu\n", no);
#endif
if (no != log_block_convert_lsn_to_no(*scanned_lsn)
|| !log_block_checksum_is_ok_or_old_format(log_block)) {
#if 0
fprintf(stderr,
"Log block n:o %lu, scanned lsn n:o %lu\n",
no, log_block_convert_lsn_to_no(*scanned_lsn));
#endif
/* Garbage or an incompletely written log block */
log_block += OS_FILE_LOG_BLOCK_SIZE;