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;//標(biāo)記下面進入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函數(shù)返回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、當(dāng)讀取checkpoint的時候,source是XLOG_FROM_ANY
3、先找歸檔的日志進行open;如果open失敗再找WAL日志進行open
4、如果都沒有open成功,則向前找時間線,open前一個時間線segno和文件號相同的文件進行open
5、open成功后expectedTLEs被賦值為當(dāng)前時間線列表的所有值
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
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
您可能感興趣的文章:- PostgreSQL中的template0和template1庫使用實戰(zhàn)
- PostgreSQL存儲過程用法實戰(zhàn)詳解
- postgresql影子用戶實踐場景分析