非易失性WAL buffer实现解析(三)

2020-10-28 09:35:13 浏览数 (1)

本文接上文介绍函数WaitForWALToBecomeAvailable打开WAL日志文件并切换日志源的实现。该函数不涉及关于NVM WAL BUFFER的修改。

1)主机重启恢复时:日志源currentSource = XLOG_FROM_PG_WAL,之后由于第一次进来,调用函数XLogFileReadAnyTLI打开日志所在WAL文件。如果open成功则返回TRUE,否则lastSourceFailed=TRUE,然后跳到1,因为是主机直接返回false

2)备机重启恢复时:日志源currentSource = XLOG_FROM_ARCHIVE,之后由于第一次进来,调用函数XLogFileReadAnyTLI打开日志所在WAL文件。如果open成功则返回TRUE,否则lastSourceFailed=TRUE,然后跳到1,切换日志源:currentSource = XLOG_FROM_STREAM;请求的日志已接收到则返回TRUE,未接收到则sleep一会,重新返回1循环,切到XLOG_FROM_ARCHIVE往次循环

关于函数XLogFileReadAnyTLI的实现参考我之前总结的:https://blog.51cto.com/yanzongshuai/2306988:XLOG_FROM_ARCHIVE出错后还会切到XLOG_FROM_PG_WAL进行open的,只是currentSource 状态机没有改变而已。

代码语言:javascript复制
static int
XLogFileReadAnyTLI(XLogSegNo segno, int emode, int source)
{
 
  if (expectedTLEs)
    tles = expectedTLEs;
  else
    tles = readTimeLineHistory(recoveryTargetTLI);
  /*
  1、WaitForWALToBecomeAvailable调用时,source是XLOG_FROM_ARCHIVE时,会使用XLOG_FROM_ANY
  2、使用XLOG_FROM_ANY,会首先从归档中读取xlog,如果open失败,则会使用XLOG_FROM_PG_WAL
  3、外部日志源变量并没有切换
  */
  foreach(cell, tles){
    TimeLineID  tli = ((TimeLineHistoryEntry *) lfirst(cell))->tli;
    if (tli < curFileTLI)
      break;        /* don't bother looking at too-old TLIs */
    if (source == XLOG_FROM_ANY || source == XLOG_FROM_ARCHIVE){
      fd = XLogFileRead(segno, emode, tli,XLOG_FROM_ARCHIVE, true);
      if (fd != -1){
        if (!expectedTLEs)
          expectedTLEs = tles;
        return fd;
      }
    }
    if (source == XLOG_FROM_ANY || source == XLOG_FROM_PG_WAL){
      fd = XLogFileRead(segno, emode, tli, XLOG_FROM_PG_WAL, true);
      if (fd != -1){
        if (!expectedTLEs)
          expectedTLEs = tles;
        return fd;
      }
    }
  }
  return -1;
}

0 人点赞