From 107ad05273a75afabb3d8c8a712abb65804a929d Mon Sep 17 00:00:00 2001 From: kailixu Date: Wed, 18 Sep 2024 19:04:44 +0800 Subject: [PATCH] enh: support drop table with uid --- include/common/tmsg.h | 1 + include/libs/catalog/catalog.h | 2 ++ source/common/src/tmsg.c | 6 +++++ source/dnode/mnode/impl/src/mndDb.c | 1 + source/libs/parser/inc/parUtil.h | 1 + source/libs/parser/src/parAstParser.c | 6 +++++ source/libs/parser/src/parTranslater.c | 22 +++++++--------- source/libs/parser/src/parUtil.c | 36 ++++++++++++++++++++++++++ 8 files changed, 62 insertions(+), 13 deletions(-) diff --git a/include/common/tmsg.h b/include/common/tmsg.h index 6b15441855..98e0f02522 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -1460,6 +1460,7 @@ typedef struct { int32_t walFsyncPeriod; int16_t hashPrefix; int16_t hashSuffix; + int8_t hashMethod; int8_t walLevel; int8_t precision; int8_t compression; diff --git a/include/libs/catalog/catalog.h b/include/libs/catalog/catalog.h index 49d1f5fa0b..df3f87973f 100644 --- a/include/libs/catalog/catalog.h +++ b/include/libs/catalog/catalog.h @@ -418,6 +418,8 @@ int32_t catalogGetTsma(SCatalog* pCtg, SRequestConnInfo* pConn, const SName* pTs int32_t catalogAsyncUpdateDbTsmaVersion(SCatalog* pCtg, int32_t tsmaVersion, const char* dbFName, int64_t dbId); +int32_t ctgHashValueComp(void const* lp, void const* rp); + /** * Destroy catalog and relase all resources */ diff --git a/source/common/src/tmsg.c b/source/common/src/tmsg.c index 4a469bba49..48f504b050 100644 --- a/source/common/src/tmsg.c +++ b/source/common/src/tmsg.c @@ -4912,6 +4912,7 @@ int32_t tSerializeSDbCfgRspImpl(SEncoder *encoder, const SDbCfgRsp *pRsp) { TAOS_CHECK_RETURN(tEncodeI32(encoder, pRsp->s3ChunkSize)); TAOS_CHECK_RETURN(tEncodeI32(encoder, pRsp->s3KeepLocal)); TAOS_CHECK_RETURN(tEncodeI8(encoder, pRsp->s3Compact)); + TAOS_CHECK_RETURN(tEncodeI8(encoder, pRsp->hashMethod)); return 0; } @@ -5003,6 +5004,11 @@ int32_t tDeserializeSDbCfgRspImpl(SDecoder *decoder, SDbCfgRsp *pRsp) { TAOS_CHECK_RETURN(tDecodeI32(decoder, &pRsp->s3KeepLocal)); TAOS_CHECK_RETURN(tDecodeI8(decoder, &pRsp->s3Compact)); } + if (!tDecodeIsEnd(decoder)) { + TAOS_CHECK_RETURN(tDecodeI8(decoder, &pRsp->hashMethod)); + } else { + pRsp->hashMethod = 1; // default value + } return 0; } diff --git a/source/dnode/mnode/impl/src/mndDb.c b/source/dnode/mnode/impl/src/mndDb.c index efa98e3b40..8dce7f375d 100644 --- a/source/dnode/mnode/impl/src/mndDb.c +++ b/source/dnode/mnode/impl/src/mndDb.c @@ -1321,6 +1321,7 @@ static void mndDumpDbCfgInfo(SDbCfgRsp *cfgRsp, SDbObj *pDb) { cfgRsp->walFsyncPeriod = pDb->cfg.walFsyncPeriod; cfgRsp->hashPrefix = pDb->cfg.hashPrefix; cfgRsp->hashSuffix = pDb->cfg.hashSuffix; + cfgRsp->hashMethod = pDb->cfg.hashMethod; cfgRsp->walLevel = pDb->cfg.walLevel; cfgRsp->precision = pDb->cfg.precision; cfgRsp->compression = pDb->cfg.compression; diff --git a/source/libs/parser/inc/parUtil.h b/source/libs/parser/inc/parUtil.h index 676c1130a1..7a20eeb90a 100644 --- a/source/libs/parser/inc/parUtil.h +++ b/source/libs/parser/inc/parUtil.h @@ -167,6 +167,7 @@ int32_t getViewMetaFromCache(SParseMetaCache* pMetaCache, const SName* pName, ST int32_t buildTableMetaFromViewMeta(STableMeta** pMeta, SViewMeta* pViewMeta); int32_t getDbVgInfoFromCache(SParseMetaCache* pMetaCache, const char* pDbFName, SArray** pVgInfo); int32_t getTableVgroupFromCache(SParseMetaCache* pMetaCache, const SName* pName, SVgroupInfo* pVgroup); +int32_t getDbTableVgroupFromCache(SParseMetaCache* pMetaCache, const SName* pName, SVgroupInfo* pVgroup); int32_t getDbVgVersionFromCache(SParseMetaCache* pMetaCache, const char* pDbFName, int32_t* pVersion, int64_t* pDbId, int32_t* pTableNum, int64_t* pStateTs); int32_t getDbCfgFromCache(SParseMetaCache* pMetaCache, const char* pDbFName, SDbCfgInfo* pInfo); diff --git a/source/libs/parser/src/parAstParser.c b/source/libs/parser/src/parAstParser.c index b9a63155f4..a7481fda8a 100644 --- a/source/libs/parser/src/parAstParser.c +++ b/source/libs/parser/src/parAstParser.c @@ -382,6 +382,12 @@ static int32_t collectMetaKeyFromDropTable(SCollectMetaKeyCxt* pCxt, SDropTableS SDropTableClause* pClause = (SDropTableClause*)pNode; if (pStmt->withOpt) { code = reserveTableUidInCache(pCxt->pParseCxt->acctId, pClause->dbName, pClause->tableName, pCxt->pMetaCache); + if (TSDB_CODE_SUCCESS == code) { + code = reserveDbVgInfoInCache(pCxt->pParseCxt->acctId, pClause->dbName, pCxt->pMetaCache); + } + if (TSDB_CODE_SUCCESS == code) { + code = reserveDbCfgInCache(pCxt->pParseCxt->acctId, pClause->dbName, pCxt->pMetaCache); + } } else { code = reserveTableMetaInCache(pCxt->pParseCxt->acctId, pClause->dbName, pClause->tableName, pCxt->pMetaCache); diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index 375a610f64..b4e2ce4525 100755 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -509,7 +509,7 @@ static int32_t getTargetName(STranslateContext* pCxt, const SName* pName, char* return code; } -static int32_t rewriteDropMetaCache(STranslateContext* pCxt) { +static int32_t rewriteDropTableWithMetaCache(STranslateContext* pCxt) { int32_t code = TSDB_CODE_SUCCESS; SParseContext* pParCxt = pCxt->pParseCxt; SParseMetaCache* pMetaCache = pCxt->pMetaCache; @@ -567,16 +567,6 @@ static int32_t rewriteDropMetaCache(STranslateContext* pCxt) { taosHashCancelIterate(pMetaCache->pTableName, ppMetaRes); return code; } - - SMetaRes** qqMetaRes = taosHashGet(pMetaCache->pTableMeta, fullName, strlen(fullName)); - if (!qqMetaRes) { - taosHashCancelIterate(pMetaCache->pTableName, ppMetaRes); - return TSDB_CODE_OUT_OF_MEMORY; - } - } - taosHashCleanup(pMetaCache->pTableName); - if (code) { - assert(0); } return code; } @@ -720,7 +710,11 @@ static int32_t getTableHashVgroupImpl(STranslateContext* pCxt, const SName* pNam } if (TSDB_CODE_SUCCESS == code) { if (pParCxt->async) { - code = getTableVgroupFromCache(pCxt->pMetaCache, pName, pInfo); + if(pCxt->withOpt) { + code = getDbTableVgroupFromCache(pCxt->pMetaCache, pName, pInfo); + } else { + code = getTableVgroupFromCache(pCxt->pMetaCache, pName, pInfo); + } } else { SRequestConnInfo conn = {.pTrans = pParCxt->pTransporter, .requestId = pParCxt->requestId, @@ -14535,6 +14529,7 @@ static int32_t rewriteDropTablewithOpt(STranslateContext* pCxt, SQuery* pQuery) int32_t code = TSDB_CODE_SUCCESS; SDropTableStmt* pStmt = (SDropTableStmt*)pQuery->pRoot; if (!pStmt->withOpt) return code; + pCxt->withOpt = true; SNode* pNode = NULL; char pTableName[TSDB_TABLE_NAME_LEN] = {0}; @@ -14550,7 +14545,7 @@ static int32_t rewriteDropTablewithOpt(STranslateContext* pCxt, SQuery* pQuery) tstrncpy(pClause->tableName, pTableName, TSDB_TABLE_NAME_LEN); // rewrite table uid to table name } - code = rewriteDropMetaCache(pCxt); + code = rewriteDropTableWithMetaCache(pCxt); TAOS_RETURN(code); } @@ -14581,6 +14576,7 @@ static int32_t rewriteDropTable(STranslateContext* pCxt, SQuery* pQuery) { if (tableType == TSDB_SUPER_TABLE && LIST_LENGTH(pStmt->pTables) > 1) { return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_DROP_STABLE); } + if (pCxt->withOpt) continue; if (pCxt->pMetaCache) code = getTableTsmasFromCache(pCxt->pMetaCache, &name, &pTsmas); if (TSDB_CODE_SUCCESS != code) { taosHashCleanup(pVgroupHashmap); diff --git a/source/libs/parser/src/parUtil.c b/source/libs/parser/src/parUtil.c index 0f01142579..1111a6b0b7 100644 --- a/source/libs/parser/src/parUtil.c +++ b/source/libs/parser/src/parUtil.c @@ -1152,6 +1152,42 @@ int32_t getTableVgroupFromCache(SParseMetaCache* pMetaCache, const SName* pName, return code; } +int32_t getDbTableVgroupFromCache(SParseMetaCache* pMetaCache, const SName* pName, SVgroupInfo* pVgroup) { + char fullName[TSDB_TABLE_FNAME_LEN]; + int32_t code = tNameExtractFullName(pName, fullName); + if (TSDB_CODE_SUCCESS != code) { + return code; + } + const char* pDb = strstr(fullName, "."); + if (pDb == NULL) return TSDB_CODE_PAR_INTERNAL_ERROR; + pDb = strstr(pDb + 1, "."); + if (pDb == NULL) return TSDB_CODE_PAR_INTERNAL_ERROR; + int32_t fullDbLen = pDb - fullName; + int32_t fullTbLen = strlen(fullName); + + SArray* pVgArray = NULL; + SDbCfgInfo* pDbCfg = NULL; + code = getMetaDataFromHash(fullName, fullDbLen, pMetaCache->pDbVgroup, (void**)&pVgArray); + if (TSDB_CODE_SUCCESS == code) { + code = getMetaDataFromHash(fullName, fullDbLen, pMetaCache->pDbCfg, (void**)&pDbCfg); + } + if (TSDB_CODE_SUCCESS == code) { + code = TSDB_CODE_PAR_INTERNAL_ERROR; + int32_t vgSize = taosArrayGetSize(pVgArray); + for (int32_t i = 0; i < vgSize; ++i) { + uint32_t hashValue = + taosGetTbHashVal(fullName, fullTbLen, pDbCfg->hashMethod, pDbCfg->hashPrefix, pDbCfg->hashSuffix); + void* pVg = taosArraySearch(pVgArray, &hashValue, ctgHashValueComp, TD_EQ); + if (pVg) { + memcpy(pVgroup, pVg, sizeof(SVgroupInfo)); + code = TSDB_CODE_SUCCESS; + break; + } + } + } + return code; +} + int32_t reserveDbVgVersionInCache(int32_t acctId, const char* pDb, SParseMetaCache* pMetaCache) { return reserveDbReqInCache(acctId, pDb, &pMetaCache->pDbInfo); }