fix/TD-32622-add-lock-for-vnodes

This commit is contained in:
dmchen 2024-10-25 10:10:00 +00:00
parent f7843a29ab
commit dde9ae82f2
3 changed files with 18 additions and 19 deletions

View File

@ -41,7 +41,7 @@ typedef struct SVnodeMgmt {
STfs *pTfs; STfs *pTfs;
TdThread thread; TdThread thread;
bool stop; bool stop;
TdThreadMutex createLock; TdThreadMutex fileLock;
} SVnodeMgmt; } SVnodeMgmt;
typedef struct { typedef struct {

View File

@ -233,35 +233,47 @@ int32_t vmWriteVnodeListToFile(SVnodeMgmt *pMgmt) {
goto _OVER; goto _OVER;
} }
code = taosThreadMutexLock(&pMgmt->fileLock);
if (code != 0) {
lino = __LINE__;
goto _OVER;
}
pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_WRITE_THROUGH); pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_WRITE_THROUGH);
if (pFile == NULL) { if (pFile == NULL) {
code = terrno; code = terrno;
lino = __LINE__; lino = __LINE__;
goto _OVER; goto _OVER1;
} }
int32_t len = strlen(buffer); int32_t len = strlen(buffer);
if (taosWriteFile(pFile, buffer, len) <= 0) { if (taosWriteFile(pFile, buffer, len) <= 0) {
code = terrno; code = terrno;
lino = __LINE__; lino = __LINE__;
goto _OVER; goto _OVER1;
} }
if (taosFsyncFile(pFile) < 0) { if (taosFsyncFile(pFile) < 0) {
code = TAOS_SYSTEM_ERROR(errno); code = TAOS_SYSTEM_ERROR(errno);
lino = __LINE__; lino = __LINE__;
goto _OVER; goto _OVER1;
} }
code = taosCloseFile(&pFile); code = taosCloseFile(&pFile);
if (code != 0) { if (code != 0) {
code = TAOS_SYSTEM_ERROR(errno); code = TAOS_SYSTEM_ERROR(errno);
lino = __LINE__; lino = __LINE__;
goto _OVER; goto _OVER1;
} }
TAOS_CHECK_GOTO(taosRenameFile(file, realfile), &lino, _OVER); TAOS_CHECK_GOTO(taosRenameFile(file, realfile), &lino, _OVER1);
dInfo("succeed to write vnodes file:%s, vnodes:%d", realfile, numOfVnodes); dInfo("succeed to write vnodes file:%s, vnodes:%d", realfile, numOfVnodes);
_OVER1:
int32_t ret = taosThreadMutexUnlock(&pMgmt->fileLock);
if (ret != 0) {
dError("failed to unlock since %s", tstrerror(ret));
}
_OVER: _OVER:
if (pJson != NULL) tjsonDelete(pJson); if (pJson != NULL) tjsonDelete(pJson);
if (buffer != NULL) taosMemoryFree(buffer); if (buffer != NULL) taosMemoryFree(buffer);

View File

@ -415,24 +415,11 @@ int32_t vmProcessCreateVnodeReq(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) {
goto _OVER; goto _OVER;
} }
code = taosThreadMutexLock(&pMgmt->createLock);
if (code != 0) {
dError("vgId:%d, failed to lock since %s", req.vgId, tstrerror(code));
goto _OVER;
}
code = vmWriteVnodeListToFile(pMgmt); code = vmWriteVnodeListToFile(pMgmt);
if (code != 0) { if (code != 0) {
code = terrno != 0 ? terrno : code; code = terrno != 0 ? terrno : code;
int32_t ret = taosThreadMutexUnlock(&pMgmt->createLock);
if (ret != 0) {
dError("vgId:%d, failed to unlock since %s", req.vgId, tstrerror(ret));
}
goto _OVER; goto _OVER;
} }
int32_t ret = taosThreadMutexUnlock(&pMgmt->createLock);
if (ret != 0) {
dError("vgId:%d, failed to unlock since %s", req.vgId, tstrerror(ret));
}
_OVER: _OVER:
if (code != 0) { if (code != 0) {