共计 4284 个字符,预计需要花费 11 分钟才能阅读完成。
1、首先读取 ControlFile->checkPoint 指向的 checkpoint
2、如果读取失败,slave 直接 abort 退出,master 再次读取 ControlFile->prevCheckPoint 指向的 checkpoint
StartupXLOG->
|–checkPointLoc = ControlFile->checkPoint;
|–record = ReadCheckpointRecord(xlogreader, checkPointLoc, 1, true):
|– if (record != NULL){
…
}else if (StandbyMode){
ereport(PANIC,(errmsg(“could not locate a valid checkpoint record”)));
}else{
checkPointLoc = ControlFile->prevCheckPoint;
record = ReadCheckpointRecord(xlogreader, checkPointLoc, 2, true);
if (record != NULL){
InRecovery = true;// 标记下面进入 recovery
}else{
ereport(PANIC,(errmsg(“could not locate a valid checkpoint record”)));
}
}
一、那么什么条件下读取的 checkpoint 记录 record==NULL?
1、ControlFile->checkPoint % XLOG_BLCKSZ < SizeOfXLogShortPHD
2、ReadRecord(xlogreader, ControlFile->checkPoint, LOG, true)返回 NULL
3、ReadRecord 读到的 record!=NULL && record->xl_rmid != RM_XLOG_ID
4、ReadRecord 读到的 record!=NULL && info != XLOG_CHECKPOINT_SHUTDOWN && info != XLOG_CHECKPOINT_ONLINE
5、ReadRecord 读到的 record!=NULL && record->xl_tot_len != SizeOfXLogRecord + SizeOfXLogRecordDataHeaderShort + sizeof(CheckPoint)
二、ReadRecord 函数返回 NULL 的条件
ReadRecord(xlogreader, ControlFile->checkPoint, LOG, true)
|–record = XLogReadRecord(xlogreader, ControlFile->checkPoint, &errormsg);
|– 2.1 record==NULL &&!StandbyMode
|– 2.2 record!=NULL && !tliInHistory(xlogreader->latestPageTLI, expectedTLEs)
/*—–
note: 只要读取了一页 xlog, 就会赋值为该页第一个记录的时间线
XLogReaderValidatePageHeader
–>xlogreader->latestPageTLI=hdr->xlp_tli;
——*/
三、XlogReadRecord 读取 checkpoint 返回 NULL 的条件?
XLogReadRecord(xlogreader, ControlFile->checkPoint, &errormsg)
targetPagePtr = ControlFile->checkPoint – (ControlFile->checkPoint % XLOG_BLCKSZ);
targetRecOff = ControlFile->checkPoint % XLOG_BLCKSZ;
readOff = ReadPageInternal(state,targetPagePtr, Min(targetRecOff + SizeOfXLogRecord, XLOG_BLCKSZ));
pageHeaderSize = XLogPageHeaderSize((XLogPageHeader) state->readBuf);
record = (XLogRecord *) (state->readBuf + RecPtr % XLOG_BLCKSZ);
total_len = record->xl_tot_len;
————-
1、readOff < 0
2、0< targetRecOff < pageHeaderSize
3、(((XLogPageHeader) state->readBuf)->xlp_info & XLP_FIRST_IS_CONTRECORD) && targetRecOff == pageHeaderSize
page 头有跨页的 record 并且 checkpoint 定位的偏移正好在页头尾部
4、targetRecOff <= XLOG_BLCKSZ – SizeOfXLogRecord &&
!ValidXLogRecordHeader(state, ControlFile->checkPoint, state->ReadRecPtr, record,randAccess)
—(record->xl_tot_len < SizeOfXLogRecord || record->xl_rmid > RM_MAX_ID || record->xl_prev != state->ReadRecPtr)
5、targetRecOff > XLOG_BLCKSZ – SizeOfXLogRecord && total_len < SizeOfXLogRecord
6、total_len > state->readRecordBufSize && !allocate_recordbuf(state, total_len)
一旦该记录损坏,total_len 的长度非常大的话,就需要 allocate_recordbuf 扩展 state->readbuf,可能因此分配失败 abort
记录的 checksum 需要等待全部读取完整记录后才校验
————-
三、ReadPageInternal 返回的 readOff 返回小于 0 的条件
ReadPageInternal(state,targetPagePtr, Min(targetRecOff + SizeOfXLogRecord, XLOG_BLCKSZ))
1、第一次 read wal 文件,readLen = state->read_page:读取第一页。readLen < 0
2、readLen>0 && !XLogReaderValidatePageHeader(state, targetSegmentPtr, state->readBuf)
—
3、读取 checkpoint 所在页 readLen = state->read_page: readLen < 0
4、readLen > 0 && readLen <= SizeOfXLogShortPHD
5、!XLogReaderValidatePageHeader(state, pageptr, (char *) hdr)
四、XLogPageRead 何时返回值 <0 ?
/*
1、WaitForWALToBecomeAvailable open 失败
2、lseek 失败 &&!StandbyMode
3、read 失败 &&!StandbyMode
4、校验 page 头失败 &&!StandbyMode
如果是 StandbyMode,则会重新 retry->WaitForWALToBecomeAvailable, 切换日志源进行 open
*/
!WaitForWALToBecomeAvailable(targetPagePtr + reqLen,private->randAccess,1,targetRecPtr)//open
|– return -1
readOff = targetPageOff;
if (lseek(readFile, (off_t) readOff, SEEK_SET) < 0){
!StandbyMode:: return -1
}
if (read(readFile, readBuf, XLOG_BLCKSZ) != XLOG_BLCKSZ){
!StandbyMode:: return -1
}
XLogReaderValidatePageHeader(xlogreader, targetPagePtr, readBuf)
!StandbyMode:: return -1
五、WaitForWALToBecomeAvailable 何时返回 false?
–XLOG_FROM_ARCHIVE | XLOG_FROM_PG_WAL
1、先 XLogFileReadAnyTLI open 日志:
1、遍历时间线列表里的每一个时间线,从最新的开始
2、当读取 checkpoint 的时候,source 是 XLOG_FROM_ANY
3、先找归档的日志进行 open;如果 open 失败再找 WAL 日志进行 open
4、如果都没有 open 成功,则向前找时间线,open 前一个时间线 segno 和文件号相同的文件进行 open
5、open 成功后 expectedTLEs 被赋值为当前时间线列表的所有值
2、如果 open 失败,则切换日志源:XLOG_FROM_ARCHIVE | XLOG_FROM_PG_WAL -> XLOG_FROM_STREAM
3、切换日志源后,XLOG_FROM_ARCHIVE | XLOG_FROM_PG_WAL 则:
slave && promote:return false
!StandbyMode:return false
–XLOG_FROM_STREAM
1、!WalRcvStreaming() 即 receiver 进程挂了,切换日志源
2、CheckForStandbyTrigger() 切换日志源
3、XLOG_FROM_STREAM->XLOG_FROM_ARCHIVE
: