checkPoint指向的checkpoint 2、如果讀取失敗,slave直接abort退出,master再次讀取ControlFile->prevCheckPoint指向的checkpoint Star..."/>
溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

PostgreSQL啟動恢復(fù)讀取checkpoint記錄失敗的條件

發(fā)布時間:2020-07-21 04:53:56 來源:網(wǎng)絡(luò) 閱讀:7474 作者:yzs的專欄 欄目:數(shù)據(jù)庫
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)記下面進(jìn)入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擴(kuò)展state->readbuf,可能因此分配失敗abort
       記錄的checksum需要等待全部讀取完整記錄后才校驗(yàn)
    -------------

三、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、校驗(yàn)page頭失敗 && !StandbyMode
    如果是StandbyMode,則會重新retry->WaitForWALToBecomeAvailable,切換日志源進(jìn)行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、先找歸檔的日志進(jìn)行open;如果open失敗再找WAL日志進(jìn)行open
        4、如果都沒有open成功,則向前找時間線,open前一個時間線segno和文件號相同的文件進(jìn)行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進(jìn)程掛了,切換日志源
    2、CheckForStandbyTrigger()切換日志源
    3、XLOG_FROM_STREAM->XLOG_FROM_ARCHIVE
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI