本文主要是介绍lsn mysql_mysql源码片段 innodb启动checkpoint flush脏页 lsn几个关键点检查过程整理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
mysql innodb引擎如何判断进程是正常停止(shutdown停止)
还是异常停止(crash 或者 被kill 或者主机宕机导致的进程停止)呢?代码是如何判断的呢
注:代码来源于mysql5.7.27
innodb 启动入口函数 innobase_start_or_create_for_mysql
调用recv_recovery_from_checkpoint_start 检查redo日志该如何启动
概括几个点
1 如果 checkpoint_lsn 与 flush_lsn 不相等 则表示mysql 非正常停止
2 checkpoint 写redo日志的检查点 recv_sys->mlog_checkpoint_lsn == 0
3 redo日志文件循环使用,启动时如何判断myql服务停止前redo日志记录的位置
详细描述
mysql 是否正常停止
判断 checkpoint_lsn 与 flush_lsn 是否相等
如果相等则正常停止
如果不相等,则非正常停止
/*
函数 recv_recovery_from_checkpoint_start
日志扫描完成后判断是否正常停止
checkpoint_lsn 为log_checkpoint 的lsn
flush_lsn 为redo日志写到磁盘的lsn,仅仅在正常停止的时候才更新
*/
文件log0recv.cc的代码片段
/*设置标志位,正式明确需要恢复redo日志*/
0358 /*******************************************************//**
0359 Initialize crash recovery environment. Can be called iff
0360 recv_needed_recovery == false. */
0361 static
0362 void
0363 recv_init_crash_recovery(void)
0364 {
0365 ut_ad(!srv_read_only_mode);
0366 ut_a(!recv_needed_recovery);
0367 /*设置标志位,说明非正常停止,需要恢复日志*/
0368 recv_needed_recovery = true;
0369 }
flush_lsn读取
函数 innobase_start_or_create_for_mysql(void)
/*读取ibdata1文件中记录第一个page FIL_PAGE_FILE_FLUSH_LSN的位置存储的 lsn*/
fsp0sysspace.cc
1934 err = srv_sys_space.open_or_create(
1935false, create_new_db, &sum_of_new_sizes, &flushed_lsn);
fsp0file.cc
561 *flush_lsn = mach_read_from_8(
562m_first_page + FIL_PAGE_FILE_FLUSH_LSN);
/*读取flushed_lsn堆栈信息*/
#0 Datafile::validate_first_page (this=0x2deb438, flush_lsn=0x7fffffff9150, for_import=false) at storage/innobase/fsp/fsp0file.cc:559
#1 0x0000000001c0931c in SysTablespace::read_lsn_and_check_flags (this=0x2d8d460 , flushed_lsn=0x7fffffff9150) at storage/innobase/fsp/fsp0sysspace.cc:580
#2 0x0000000001c0a961 in SysTablespace::open_or_create (this=0x2d8d460 , is_temp=false, create_new_db=false, sum_new_sizes=0x7fffffff9110, flush_lsn=0x7fffffff9150) at storage/innobase/fsp/fsp0sysspace.cc:935
#3 0x0000000001a9c2df in innobase_start_or_create_for_mysql () at storage/innobase/srv/srv0start.cc:1935
#4 0x00000000018c21d9 in innobase_init (p=0x2de9fc0) at storage/innobase/handler/ha_innodb.cc:4080
#5 0x0000000000f2ee02 in ha_initialize_handlerton (plugin=0x2f4db90) at sql/handler.cc:840
#6 0x0000000001568fd4 in plugin_initialize (plugin=0x2f4db90) at sql/sql_plugin.cc:1226
#7 0x0000000001569c3a in plugin_register_builtin_and_init_core_se (argc=0x2d1b050 , argv=0x2de82d8) at sql/sql_plugin.cc:1589
#8 0x0000000000ea8fdf in init_server_components () at sql/mysqld.cc:4079
#9 0x0000000000eaa5b4 in mysqld_main (argc=92, argv=0x2de82d8) at sql/mysqld.cc:4773
#10 0x0000000000ea21bd in main (argc=3, argv=0x7fffffffe4b8) at sql/main.cc:25
checkpoint_lsn 读取
在函数recv_recovery_from_checkpoint_start读取
读取第一个redo日志文件(即ib_logfile0)前2048字节(4*512)
在第一个512与第三个512位置读取最大一个lsn,即为checkpoint_lsn
代码如下
/* Look for the latest checkpoint from any of the log groups */
/*通过比较第2个512字节(512~1024)与第4个512字节(1536~2048)中存储的checkpoint lsn,读取最大值得所有内容*/
4065err = recv_find_max_checkpoint(&max_cp_group, &max_cp_field);
4066
4067if (err != DB_SUCCESS) {
4068
4069log_mutex_exit();
4070
4071return(err);
4072}
4073
4074log_group_header_read(max_cp_group, max_cp_field);
4075
4076buf = log_sys->checkpoint_buf;
4077 /*读取最大值所在的buf中,取出8字节,并转换long,获取checkpoint时的lsn,lsn通过512对齐后,得到新的lsn的起始位置,从ib_logfile(0,1...)中开始读取第一个block(512字节)redo记录,从block读取出第一个mtr起始记录,开始解析redo日志*/
4078checkpoint_lsn = mach_read_from_8(buf + LOG_CHECKPOINT_LSN);
4079checkpoint_no = mach_read_from_8(buf + LOG_CHECKPOINT_NO);
/*recv_find_max_checkpoint*/
/*
读取第2个与第4个
for (field = 512; field <= 3 * 512;field += 3 * 512 - 512)
*/
1338 for (field = LOG_CHECKPOINT_1; field <= LOG_CHECKPOINT_2;
1339 field += LOG_CHECKPOINT_2 - LOG_CHECKPOINT_1) {
1340
1341log_group_header_read(group, field);
1342
1343if (!recv_check_log_header_checksum(buf)) {
1344DBUG_PRINT("ib_log",
1345 ("invalid checkpoint,"
1346 " group " ULINTPF " at " ULINTPF
1347 ", checksum %x",
1348 group->id, field,
1349 (unsigned) log_block_get_checksum(
1350 buf)));
1351continue;
1352}
1353
1354group->state = LOG_GROUP_OK;
1355 /*读取checkpoint lsn*/
1356group->lsn = mach_read_from_8(
1357buf + LOG_CHECKPOINT_LSN);
1358group->lsn_offset = mach_read_from_8(
1359buf + LOG_CHECKPOINT_OFFSET);
1360checkpoint_no = mach_read_from_8(
1361buf + LOG_CHECKPOINT_NO);
1368if (checkpoint_no >= max_no) {
1369*max_group = group;
1370*max_field = field;
1371max_no = checkpoint_no;
1372}
1373}
1374
1375group = UT_LIST_GET_NEXT(log_groups, group);
1376}
checkpoint_lsn 之后的redo日志都接解析完成后,判断checkpoint_lsn 判断与flush_lsn 是否相等
执行上述代码,判断后续流程是否恢复解析redo日志
log_checkpoint 9字节redo日志
如果在redo日志中找不到9字节checkpoint日志则报错无法正常启动,需要dba特殊处理
代码检查点为
/*
recv_sys->mlog_checkpoint_lsn
如果为0 则说明在redo日志没有找到9字节的checkpoint的redo日志的记录
如果不为0 则为checkpoint_lsn的值
如果为0 且 checkpoint_lsn又不是redo日志的最后记录(group->scanned_lsn != checkpoint_lsn),则为异常情况
*/
4161if (recv_sys->mlog_checkpoint_lsn == 0) {
4162if (!srv_read_only_mode
4163 && group->scanned_lsn != checkpoint_lsn) {
4164ib::error() << "Ignoring the redo log due to missing"
4165" MLOG_CHECKPOINT between the checkpoint "
4166<< checkpoint_lsn << " and the end "
4167<< group->scanned_lsn << ".";
4168if (srv_force_recovery < SRV_FORCE_NO_LOG_REDO) {
4169log_mutex_exit();
4170return(DB_ERROR);
4171}
4172}
4173
4174group->scanned_lsn = checkpoint_lsn;
4175rescan = false;
4176}
解析redo日志,recv_sys->mlog_checkpoint_lsn赋值的地方
log0recv.cc 中函数recv_parse_log_recs中
3182 if (single_rec) {
3224 switch (type) {
3229 case MLOG_CHECKPOINT:
3230 #if SIZE_OF_MLOG_CHECKPOINT != 1 + 8
3231 # error SIZE_OF_MLOG_CHECKPOINT != 1 + 8
3232 #endif /*从redo日志读取8字节,转换成lsn,(当时做checkpoint时的lsn)*/
3233lsn = mach_read_from_8(ptr + 1);
3234
3235DBUG_PRINT("ib_log",
3236 ("MLOG_CHECKPOINT(" LSN_PF ") %s at "
3237 LSN_PF,
3238 lsn,
3239 lsn != checkpoint_lsn ? "ignored"
3240 : recv_sys->mlog_checkpoint_lsn
3241 ? "reread" : "read",
3242 recv_sys->recovered_lsn));
3243 /*lsn redo日志中去读的lsn 是否是当前的 checkpoint_lsn*/
3244if (lsn == checkpoint_lsn) {
3245if (recv_sys->mlog_checkpoint_lsn) {
3246/* At recv_reset_logs() we may
3247write a duplicate MLOG_CHECKPOINT
3248for the same checkpoint LSN. Thus
3249recv_sys->mlog_checkpoint_lsn
3250can differ from the current LSN. */
3251ut_ad(recv_sys->mlog_checkpoint_lsn
3252 <= recv_sys->recovered_lsn);
3253break;
3254}
/*把当前lsn赋值给recv_sys->mlog_checkpoint_lsn,使其不为0*/
3255recv_sys->mlog_checkpoint_lsn
3256= recv_sys->recovered_lsn;
3257}
解析redo日志终止的判断
读取redo日志的一个block(512字节),解析12字节的头(LOG_BLOCK_HDR_DATA_LEN),读取当前block中redo日志的长度
如果为512,则此block不为最后一个redo记录的block
如果小于512 则此block为mysql服务停止后,最后一个redo记录的block
在log0recv.cc 函数中
recv_group_scan_log_recs->recv_scan_log_recs
3595do {
/*读取当前block中redo的长度*/
3639data_len = log_block_get_data_len(log_block);
/*如果当前block中redo的长度不足512,则redo全部读取完成,则退出循环*/
3729if (data_len < OS_FILE_LOG_BLOCK_SIZE) {
3730/* Log data for this group ends here */
3731finished = true;
3732break;
3733} else {
3734log_block += OS_FILE_LOG_BLOCK_SIZE;
3735}
3736} while (log_block < buf + len);
3737 /*记录当前redo日志的最大长度*/
3738*group_scanned_lsn = scanned_lsn;
这篇关于lsn mysql_mysql源码片段 innodb启动checkpoint flush脏页 lsn几个关键点检查过程整理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!