fix: crash while write wal in multi-threaded way

This commit is contained in:
Shengliang Guan 2022-07-04 17:54:00 +08:00
parent 630eebecd5
commit 3cc215bdcc
1 changed files with 5 additions and 2 deletions

View File

@ -332,21 +332,25 @@ int32_t walWriteWithSyncInfo(SWal *pWal, int64_t index, tmsg_t msgType, SSyncLog
terrno = TSDB_CODE_WAL_SIZE_LIMIT; terrno = TSDB_CODE_WAL_SIZE_LIMIT;
return -1; return -1;
} }
taosThreadMutexLock(&pWal->mutex);
if (index == pWal->vers.lastVer + 1) { if (index == pWal->vers.lastVer + 1) {
if (taosArrayGetSize(pWal->fileInfoSet) == 0) { if (taosArrayGetSize(pWal->fileInfoSet) == 0) {
pWal->vers.firstVer = index; pWal->vers.firstVer = index;
if (walRoll(pWal) < 0) { if (walRoll(pWal) < 0) {
taosThreadMutexUnlock(&pWal->mutex);
return -1; return -1;
} }
} else { } else {
int64_t passed = walGetSeq() - pWal->lastRollSeq; int64_t passed = walGetSeq() - pWal->lastRollSeq;
if (pWal->cfg.rollPeriod != -1 && pWal->cfg.rollPeriod != 0 && passed > pWal->cfg.rollPeriod) { if (pWal->cfg.rollPeriod != -1 && pWal->cfg.rollPeriod != 0 && passed > pWal->cfg.rollPeriod) {
if (walRoll(pWal) < 0) { if (walRoll(pWal) < 0) {
taosThreadMutexUnlock(&pWal->mutex);
return -1; return -1;
} }
} else if (pWal->cfg.segSize != -1 && pWal->cfg.segSize != 0 && walGetLastFileSize(pWal) > pWal->cfg.segSize) { } else if (pWal->cfg.segSize != -1 && pWal->cfg.segSize != 0 && walGetLastFileSize(pWal) > pWal->cfg.segSize) {
if (walRoll(pWal) < 0) { if (walRoll(pWal) < 0) {
taosThreadMutexUnlock(&pWal->mutex);
return -1; return -1;
} }
} }
@ -355,6 +359,7 @@ int32_t walWriteWithSyncInfo(SWal *pWal, int64_t index, tmsg_t msgType, SSyncLog
// reject skip log or rewrite log // reject skip log or rewrite log
// must truncate explicitly first // must truncate explicitly first
terrno = TSDB_CODE_WAL_INVALID_VER; terrno = TSDB_CODE_WAL_INVALID_VER;
taosThreadMutexUnlock(&pWal->mutex);
return -1; return -1;
} }
@ -362,8 +367,6 @@ int32_t walWriteWithSyncInfo(SWal *pWal, int64_t index, tmsg_t msgType, SSyncLog
ASSERT(pWal->writeCur >= 0); ASSERT(pWal->writeCur >= 0);
taosThreadMutexLock(&pWal->mutex);
if (pWal->pWriteIdxTFile == NULL || pWal->pWriteLogTFile == NULL) { if (pWal->pWriteIdxTFile == NULL || pWal->pWriteLogTFile == NULL) {
walSetWrite(pWal); walSetWrite(pWal);
taosLSeekFile(pWal->pWriteLogTFile, 0, SEEK_END); taosLSeekFile(pWal->pWriteLogTFile, 0, SEEK_END);