1、首先读取ControlFile->checkPoint指向的checkpoint2、如果读取失败,slave直接abort退出,master再次读取ControlFile->prevCheckPoint指向的checkpointStartupXLOG-> |--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"))); } }
One, then what condition reads the checkpoint record Record==null?
1、ControlFile->checkPoint % XLOG_BLCKSZ < SizeOfXLogShortPHD2、ReadRecord(xlogreader, ControlFile->checkPoint, LOG, true)返回NULL3、ReadRecord读到的record!=NULL && record->xl_rmid != RM_XLOG_ID4、ReadRecord读到的record!=NULL && info != XLOG_CHECKPOINT_SHUTDOWN && info != XLOG_CHECKPOINT_ONLINE5、ReadRecord读到的record!=NULL && record->xl_tot_len != SizeOfXLogRecord + SizeOfXLogRecordDataHeaderShort + sizeof(CheckPoint)
Second, the Readrecord function returns null condition
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; ------*/
Three, Xlogreadrecord read checkpoint return null condition?
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)-& Gt;xlp_info & Xlp_first_is_contrecord) && Targetrecoff = = pageheadersize The page header has a record of the spread and checkpoint-positioned offset Move exactly at the end of the page 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) once If the record is corrupted and the length of the Total_len is very large, then the allocate_recordbuf extension state->readbuf may be required, so the checksum of the abort record for the allocation failure will need to wait for the full record to be read before verifying the----- --------
Iii. Readpageinternal returned Readoff returns less than 0 of the condition
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)
Iv. when Xlogpageread return value <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
V. When does waitforwaltobecomeavailable return 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
PostgreSQL start recovery Read checkpoint record failed condition