Merge pull request #12354 from taosdata/fix/hzcheng_3.0

fix: concurrent r/w meta
This commit is contained in:
Hongze Cheng 2022-05-11 23:01:28 +08:00 committed by GitHub
commit 5987f7509b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 7 deletions

View File

@ -37,6 +37,9 @@ typedef struct SMSmaCursor SMSmaCursor;
// clang-format on // clang-format on
// metaOpen ================== // metaOpen ==================
int32_t metaRLock(SMeta* pMeta);
int32_t metaWLock(SMeta* pMeta);
int32_t metaULock(SMeta* pMeta);
// metaEntry ================== // metaEntry ==================
int metaEncodeEntry(SEncoder* pCoder, const SMetaEntry* pME); int metaEncodeEntry(SEncoder* pCoder, const SMetaEntry* pME);
@ -57,6 +60,8 @@ int metaRemoveTableFromIdx(SMeta* pMeta, tb_uid_t uid);
static FORCE_INLINE tb_uid_t metaGenerateUid(SMeta* pMeta) { return tGenIdPI64(); } static FORCE_INLINE tb_uid_t metaGenerateUid(SMeta* pMeta) { return tGenIdPI64(); }
struct SMeta { struct SMeta {
TdThreadRwlock lock;
char* path; char* path;
SVnode* pVnode; SVnode* pVnode;
TENV* pEnv; TENV* pEnv;

View File

@ -22,6 +22,9 @@ static int tagIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kL
static int ttlIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2); static int ttlIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
static int uidIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2); static int uidIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
static int32_t metaInitLock(SMeta *pMeta) { return taosThreadRwlockInit(&pMeta->lock, NULL); }
static int32_t metaDestroyLock(SMeta *pMeta) { return taosThreadRwlockDestroy(&pMeta->lock); }
int metaOpen(SVnode *pVnode, SMeta **ppMeta) { int metaOpen(SVnode *pVnode, SMeta **ppMeta) {
SMeta *pMeta = NULL; SMeta *pMeta = NULL;
int ret; int ret;
@ -36,6 +39,7 @@ int metaOpen(SVnode *pVnode, SMeta **ppMeta) {
return -1; return -1;
} }
metaInitLock(pMeta);
pMeta->path = (char *)&pMeta[1]; pMeta->path = (char *)&pMeta[1];
sprintf(pMeta->path, "%s%s%s%s%s", tfsGetPrimaryPath(pVnode->pTfs), TD_DIRSEP, pVnode->path, TD_DIRSEP, sprintf(pMeta->path, "%s%s%s%s%s", tfsGetPrimaryPath(pVnode->pTfs), TD_DIRSEP, pVnode->path, TD_DIRSEP,
VNODE_META_DIR); VNODE_META_DIR);
@ -121,6 +125,7 @@ _err:
if (pMeta->pSkmDb) tdbDbClose(pMeta->pSkmDb); if (pMeta->pSkmDb) tdbDbClose(pMeta->pSkmDb);
if (pMeta->pTbDb) tdbDbClose(pMeta->pTbDb); if (pMeta->pTbDb) tdbDbClose(pMeta->pTbDb);
if (pMeta->pEnv) tdbEnvClose(pMeta->pEnv); if (pMeta->pEnv) tdbEnvClose(pMeta->pEnv);
metaDestroyLock(pMeta);
taosMemoryFree(pMeta); taosMemoryFree(pMeta);
return -1; return -1;
} }
@ -136,12 +141,19 @@ int metaClose(SMeta *pMeta) {
if (pMeta->pSkmDb) tdbDbClose(pMeta->pSkmDb); if (pMeta->pSkmDb) tdbDbClose(pMeta->pSkmDb);
if (pMeta->pTbDb) tdbDbClose(pMeta->pTbDb); if (pMeta->pTbDb) tdbDbClose(pMeta->pTbDb);
if (pMeta->pEnv) tdbEnvClose(pMeta->pEnv); if (pMeta->pEnv) tdbEnvClose(pMeta->pEnv);
metaDestroyLock(pMeta);
taosMemoryFree(pMeta); taosMemoryFree(pMeta);
} }
return 0; return 0;
} }
int32_t metaRLock(SMeta *pMeta) { return taosThreadRwlockRdlock(&pMeta->lock); }
int32_t metaWLock(SMeta *pMeta) { return taosThreadRwlockWrlock(&pMeta->lock); }
int32_t metaULock(SMeta *pMeta) { return taosThreadRwlockUnlock(&pMeta->lock); }
static int tbDbKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) { static int tbDbKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
STbDbKey *pTbDbKey1 = (STbDbKey *)pKey1; STbDbKey *pTbDbKey1 = (STbDbKey *)pKey1;
STbDbKey *pTbDbKey2 = (STbDbKey *)pKey2; STbDbKey *pTbDbKey2 = (STbDbKey *)pKey2;

View File

@ -19,9 +19,13 @@ void metaReaderInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
memset(pReader, 0, sizeof(*pReader)); memset(pReader, 0, sizeof(*pReader));
pReader->flags = flags; pReader->flags = flags;
pReader->pMeta = pMeta; pReader->pMeta = pMeta;
metaRLock(pMeta);
} }
void metaReaderClear(SMetaReader *pReader) { void metaReaderClear(SMetaReader *pReader) {
if (pReader->pMeta) {
metaULock(pReader->pMeta);
}
tDecoderClear(&pReader->coder); tDecoderClear(&pReader->coder);
tdbFree(pReader->pBuf); tdbFree(pReader->pBuf);
} }

View File

@ -439,29 +439,36 @@ _exit:
} }
static int metaHandleEntry(SMeta *pMeta, const SMetaEntry *pME) { static int metaHandleEntry(SMeta *pMeta, const SMetaEntry *pME) {
metaWLock(pMeta);
// save to table.db // save to table.db
if (metaSaveToTbDb(pMeta, pME) < 0) return -1; if (metaSaveToTbDb(pMeta, pME) < 0) goto _err;
// update uid.idx // update uid.idx
if (metaUpdateUidIdx(pMeta, pME) < 0) return -1; if (metaUpdateUidIdx(pMeta, pME) < 0) goto _err;
// update name.idx // update name.idx
if (metaUpdateNameIdx(pMeta, pME) < 0) return -1; if (metaUpdateNameIdx(pMeta, pME) < 0) goto _err;
if (pME->type == TSDB_CHILD_TABLE) { if (pME->type == TSDB_CHILD_TABLE) {
// update ctb.idx // update ctb.idx
if (metaUpdateCtbIdx(pMeta, pME) < 0) return -1; if (metaUpdateCtbIdx(pMeta, pME) < 0) goto _err;
// update tag.idx // update tag.idx
if (metaUpdateTagIdx(pMeta, pME) < 0) return -1; if (metaUpdateTagIdx(pMeta, pME) < 0) goto _err;
} else { } else {
// update schema.db // update schema.db
if (metaSaveToSkmDb(pMeta, pME) < 0) return -1; if (metaSaveToSkmDb(pMeta, pME) < 0) goto _err;
} }
if (pME->type != TSDB_SUPER_TABLE) { if (pME->type != TSDB_SUPER_TABLE) {
if (metaUpdateTtlIdx(pMeta, pME) < 0) return -1; if (metaUpdateTtlIdx(pMeta, pME) < 0) goto _err;
} }
metaULock(pMeta);
return 0; return 0;
_err:
metaULock(pMeta);
return -1;
} }

View File

@ -3870,6 +3870,7 @@ int32_t tsdbQuerySTableByTagCond(void* pMeta, uint64_t uid, TSKEY skey, const ch
if (metaGetTableEntryByUid(&mr, uid) < 0) { if (metaGetTableEntryByUid(&mr, uid) < 0) {
tsdbError("%p failed to get stable, uid:%" PRIu64 ", TID:0x%" PRIx64 " QID:0x%" PRIx64, pMeta, uid, taskId, reqId); tsdbError("%p failed to get stable, uid:%" PRIu64 ", TID:0x%" PRIx64 " QID:0x%" PRIx64, pMeta, uid, taskId, reqId);
metaReaderClear(&mr);
terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST; terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
goto _error; goto _error;
} else { } else {
@ -3880,6 +3881,7 @@ int32_t tsdbQuerySTableByTagCond(void* pMeta, uint64_t uid, TSKEY skey, const ch
tsdbError("%p query normal tag not allowed, uid:%" PRIu64 ", TID:0x%" PRIx64 " QID:0x%" PRIx64, pMeta, uid, taskId, tsdbError("%p query normal tag not allowed, uid:%" PRIu64 ", TID:0x%" PRIx64 " QID:0x%" PRIx64, pMeta, uid, taskId,
reqId); reqId);
terrno = TSDB_CODE_OPS_NOT_SUPPORT; // basically, this error is caused by invalid sql issued by client terrno = TSDB_CODE_OPS_NOT_SUPPORT; // basically, this error is caused by invalid sql issued by client
metaReaderClear(&mr);
goto _error; goto _error;
} }