Merge pull request #19578 from taosdata/FIX/TD-21942-main
enh: trim the last WAL idx file which is not fsynced
This commit is contained in:
commit
98e174606c
|
@ -325,6 +325,35 @@ bool walLogEntriesComplete(const SWal* pWal) {
|
||||||
return complete;
|
return complete;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int walTrimIdxFile(SWal* pWal, int32_t fileIdx) {
|
||||||
|
SWalFileInfo* pFileInfo = taosArrayGet(pWal->fileInfoSet, fileIdx);
|
||||||
|
ASSERT(pFileInfo != NULL);
|
||||||
|
char fnameStr[WAL_FILE_LEN];
|
||||||
|
walBuildIdxName(pWal, pFileInfo->firstVer, fnameStr);
|
||||||
|
|
||||||
|
int64_t fileSize = 0;
|
||||||
|
taosStatFile(fnameStr, &fileSize, NULL);
|
||||||
|
int64_t records = TMAX(0, pFileInfo->lastVer - pFileInfo->firstVer + 1);
|
||||||
|
int64_t lastEndOffset = records * sizeof(SWalIdxEntry);
|
||||||
|
|
||||||
|
if (fileSize <= lastEndOffset) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
TdFilePtr pFile = taosOpenFile(fnameStr, TD_FILE_READ | TD_FILE_WRITE);
|
||||||
|
if (pFile == NULL) {
|
||||||
|
terrno = TAOS_SYSTEM_ERROR(errno);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
wInfo("vgId:%d, trim idx file. file: %s, size: %" PRId64 ", offset: %" PRId64, pWal->cfg.vgId, fnameStr, fileSize,
|
||||||
|
lastEndOffset);
|
||||||
|
|
||||||
|
taosFtruncateFile(pFile, lastEndOffset);
|
||||||
|
taosCloseFile(&pFile);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int walCheckAndRepairMeta(SWal* pWal) {
|
int walCheckAndRepairMeta(SWal* pWal) {
|
||||||
// load log files, get first/snapshot/last version info
|
// load log files, get first/snapshot/last version info
|
||||||
const char* logPattern = "^[0-9]+.log$";
|
const char* logPattern = "^[0-9]+.log$";
|
||||||
|
@ -402,6 +431,8 @@ int walCheckAndRepairMeta(SWal* pWal) {
|
||||||
}
|
}
|
||||||
updateMeta = true;
|
updateMeta = true;
|
||||||
|
|
||||||
|
(void)walTrimIdxFile(pWal, fileIdx);
|
||||||
|
|
||||||
int64_t lastVer = walScanLogGetLastVer(pWal, fileIdx);
|
int64_t lastVer = walScanLogGetLastVer(pWal, fileIdx);
|
||||||
if (lastVer < 0) {
|
if (lastVer < 0) {
|
||||||
if (terrno != TSDB_CODE_WAL_LOG_NOT_EXIST) {
|
if (terrno != TSDB_CODE_WAL_LOG_NOT_EXIST) {
|
||||||
|
@ -567,6 +598,7 @@ int walCheckAndRepairIdxFile(SWal* pWal, int32_t fileIdx) {
|
||||||
goto _err;
|
goto _err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int64_t count = 0;
|
||||||
while (idxEntry.ver < pFileInfo->lastVer) {
|
while (idxEntry.ver < pFileInfo->lastVer) {
|
||||||
ASSERT(idxEntry.ver == ckHead.head.version);
|
ASSERT(idxEntry.ver == ckHead.head.version);
|
||||||
|
|
||||||
|
@ -578,11 +610,11 @@ int walCheckAndRepairIdxFile(SWal* pWal, int32_t fileIdx) {
|
||||||
idxEntry.offset, fLogNameStr);
|
idxEntry.offset, fLogNameStr);
|
||||||
goto _err;
|
goto _err;
|
||||||
}
|
}
|
||||||
wWarn("vgId:%d, wal idx append new entry %" PRId64 " %" PRId64, pWal->cfg.vgId, idxEntry.ver, idxEntry.offset);
|
|
||||||
if (taosWriteFile(pIdxFile, &idxEntry, sizeof(SWalIdxEntry)) < 0) {
|
if (taosWriteFile(pIdxFile, &idxEntry, sizeof(SWalIdxEntry)) < 0) {
|
||||||
wError("vgId:%d, failed to append file since %s. file:%s", pWal->cfg.vgId, terrstr(), fnameStr);
|
wError("vgId:%d, failed to append file since %s. file:%s", pWal->cfg.vgId, terrstr(), fnameStr);
|
||||||
goto _err;
|
goto _err;
|
||||||
}
|
}
|
||||||
|
count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (taosFsyncFile(pIdxFile) < 0) {
|
if (taosFsyncFile(pIdxFile) < 0) {
|
||||||
|
@ -590,6 +622,11 @@ int walCheckAndRepairIdxFile(SWal* pWal, int32_t fileIdx) {
|
||||||
goto _err;
|
goto _err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (count > 0) {
|
||||||
|
wInfo("vgId:%d, rebuilt %" PRId64 " wal idx entries until lastVer: %" PRId64, pWal->cfg.vgId, count,
|
||||||
|
pFileInfo->lastVer);
|
||||||
|
}
|
||||||
|
|
||||||
(void)taosCloseFile(&pLogFile);
|
(void)taosCloseFile(&pLogFile);
|
||||||
(void)taosCloseFile(&pIdxFile);
|
(void)taosCloseFile(&pIdxFile);
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Reference in New Issue