enh: support drop table with uid

This commit is contained in:
kailixu 2024-09-18 13:57:17 +08:00
parent d7fcf21f8b
commit 28edf40c2a
6 changed files with 98 additions and 10 deletions

View File

@ -123,6 +123,11 @@ typedef struct STableMeta {
// the schema content. // the schema content.
SSchema schema[]; SSchema schema[];
} STableMeta; } STableMeta;
typedef struct STableMetaEx {
STableMeta* pMeta;
// END: KEEP THIS PART SAME WITH STableMeta
char tbName[TSDB_TABLE_NAME_LEN];
} STableMetaEx;
#pragma pack(pop) #pragma pack(pop)
typedef struct SViewMeta { typedef struct SViewMeta {

View File

@ -75,7 +75,7 @@ int32_t vnodeGetTableMeta(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
(void)memcpy(metaRsp.dbFName, infoReq.dbFName, sizeof(metaRsp.dbFName)); (void)memcpy(metaRsp.dbFName, infoReq.dbFName, sizeof(metaRsp.dbFName));
if (!reqFromUid) { if (!reqFromUid) {
(void)sprintf(tableFName, "%s.%s", infoReq.dbFName, infoReq.tbName); TAOS_UNUSED(sprintf(tableFName, "%s.%s", infoReq.dbFName, infoReq.tbName));
code = vnodeValidateTableHash(pVnode, tableFName); code = vnodeValidateTableHash(pVnode, tableFName);
if (code) { if (code) {
goto _exit4; goto _exit4;
@ -95,6 +95,7 @@ int32_t vnodeGetTableMeta(SVnode *pVnode, SRpcMsg *pMsg, bool direct) {
code = terrno; code = terrno;
goto _exit3; goto _exit3;
} }
tstrncpy(metaRsp.tbName, tbName + VARSTR_HEADER_SIZE, TSDB_TABLE_NAME_LEN);
if (metaGetTableEntryByName(&mer1, tbName + VARSTR_HEADER_SIZE) < 0) { if (metaGetTableEntryByName(&mer1, tbName + VARSTR_HEADER_SIZE) < 0) {
code = terrno; code = terrno;
goto _exit3; goto _exit3;

View File

@ -2080,9 +2080,17 @@ static int32_t ctgHandleGetTbNamesRsp(SCtgTaskReq* tReq, int32_t reqType, const
CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR); CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
} }
pRes->code = 0; STableMetaEx* pMetaEx = taosMemoryMalloc(sizeof(STableMetaEx));
pRes->pRes = pOut->tbMeta; if (NULL == pMetaEx) {
CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
}
pMetaEx->pMeta = pOut->tbMeta;
pOut->tbMeta = NULL; pOut->tbMeta = NULL;
tstrncpy(pMetaEx->tbName, pOut->tbName, TSDB_TABLE_NAME_LEN);
pRes->code = 0;
pRes->pRes = pMetaEx;
if (0 == atomic_sub_fetch_32(&ctx->fetchNum, 1)) { if (0 == atomic_sub_fetch_32(&ctx->fetchNum, 1)) {
TSWAP(pTask->res, ctx->pResList); TSWAP(pTask->res, ctx->pResList);
taskDone = true; taskDone = true;

View File

@ -702,7 +702,7 @@ int32_t ctgAddBatch(SCatalog* pCtg, int32_t vgId, SRequestConnInfo* pConn, SCtgT
if (TDMT_VND_TABLE_CFG == msgType) { if (TDMT_VND_TABLE_CFG == msgType) {
SCtgTbCfgCtx* ctx = (SCtgTbCfgCtx*)pTask->taskCtx; SCtgTbCfgCtx* ctx = (SCtgTbCfgCtx*)pTask->taskCtx;
pName = ctx->pName; pName = ctx->pName;
} else if (TDMT_VND_TABLE_META == msgType) { } else if (TDMT_VND_TABLE_META == msgType || TDMT_VND_TABLE_NAME == msgType) {
if (CTG_TASK_GET_TB_META_BATCH == pTask->type) { if (CTG_TASK_GET_TB_META_BATCH == pTask->type) {
SCtgTbMetasCtx* ctx = (SCtgTbMetasCtx*)pTask->taskCtx; SCtgTbMetasCtx* ctx = (SCtgTbMetasCtx*)pTask->taskCtx;
SCtgFetch* fetch = taosArrayGet(ctx->pFetchs, tReq->msgIdx); SCtgFetch* fetch = taosArrayGet(ctx->pFetchs, tReq->msgIdx);
@ -716,6 +716,10 @@ int32_t ctgAddBatch(SCatalog* pCtg, int32_t vgId, SRequestConnInfo* pConn, SCtgT
ctgError("fail to get %d SName, totalTables:%d", pFetch->tbIdx, (int32_t)taosArrayGetSize(pTbReq->pTables)); ctgError("fail to get %d SName, totalTables:%d", pFetch->tbIdx, (int32_t)taosArrayGetSize(pTbReq->pTables));
CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR); CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
} }
} else if (CTG_TASK_GET_TB_NAME == pTask->type) {
SCtgTbMetasCtx* ctx = (SCtgTbMetasCtx*)pTask->taskCtx;
SCtgFetch* fetch = taosArrayGet(ctx->pFetchs, tReq->msgIdx);
CTG_ERR_JRET(ctgGetFetchName(ctx->pNames, fetch, &pName));
} else { } else {
SCtgTbMetaCtx* ctx = (SCtgTbMetaCtx*)pTask->taskCtx; SCtgTbMetaCtx* ctx = (SCtgTbMetaCtx*)pTask->taskCtx;
pName = ctx->pName; pName = ctx->pName;

View File

@ -112,7 +112,7 @@ typedef struct SParseMetaCache {
SHashObj* pViews; // key is viewFName, element is SViewMeta* SHashObj* pViews; // key is viewFName, element is SViewMeta*
SHashObj* pTableTSMAs; // key is tbFName, elements are SArray<STableTSMAInfo*> SHashObj* pTableTSMAs; // key is tbFName, elements are SArray<STableTSMAInfo*>
SHashObj* pTSMAs; // key is tsmaFName, elements are STableTSMAInfo* SHashObj* pTSMAs; // key is tsmaFName, elements are STableTSMAInfo*
SHashObj* pTableName; // key is tbFUid, elements is tbName SHashObj* pTableName; // key is tbFUid, elements is STableMetaEx*
SArray* pDnodes; // element is SEpSet SArray* pDnodes; // element is SEpSet
bool dnodeRequired; bool dnodeRequired;
bool qnodeRequired; bool qnodeRequired;

View File

@ -509,6 +509,71 @@ static int32_t getTargetName(STranslateContext* pCxt, const SName* pName, char*
return code; return code;
} }
static int32_t rewriteDropMetaCache(STranslateContext* pCxt) {
int32_t code = TSDB_CODE_SUCCESS;
SParseContext* pParCxt = pCxt->pParseCxt;
SParseMetaCache* pMetaCache = pCxt->pMetaCache;
int32_t tbMetaSize = taosHashGetSize(pMetaCache->pTableMeta);
int32_t tbMetaExSize = taosHashGetSize(pMetaCache->pTableName);
if (tbMetaSize > 0 || tbMetaExSize <= 0) {
return TSDB_CODE_PAR_INTERNAL_ERROR;
}
if (!pMetaCache->pTableMeta &&
!(pMetaCache->pTableMeta =
taosHashInit(tbMetaExSize, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK))) {
return TSDB_CODE_OUT_OF_MEMORY;
}
SMetaRes** ppMetaRes = NULL;
char dbName[TSDB_DB_NAME_LEN] = {0};
while ((ppMetaRes = taosHashIterate(pMetaCache->pTableName, ppMetaRes))) {
if (!(*ppMetaRes)) {
taosHashCancelIterate(pMetaCache->pTableName, ppMetaRes);
return TSDB_CODE_PAR_INTERNAL_ERROR;
}
char* pKey = taosHashGetKey(ppMetaRes, NULL);
STableMetaEx* pMetaEx = (STableMetaEx*)(*ppMetaRes)->pRes;
if (!pMetaEx || !pMetaEx->pMeta) {
taosHashCancelIterate(pMetaCache->pTableName, ppMetaRes);
return TSDB_CODE_PAR_INTERNAL_ERROR;
}
char* pDbStart = strstr(pKey, ".");
char* pDbEnd = pDbStart ? strstr(pDbStart + 1, ".") : NULL;
if (!pDbEnd) {
taosHashCancelIterate(pMetaCache->pTableName, ppMetaRes);
return TSDB_CODE_PAR_INTERNAL_ERROR;
}
tstrncpy(dbName, pDbStart + 1, pDbEnd - pDbStart);
SName name = {0};
toName(pParCxt->acctId, dbName, pMetaEx->tbName, &name);
char fullName[TSDB_TABLE_FNAME_LEN];
code = tNameExtractFullName(&name, fullName);
if (TSDB_CODE_SUCCESS != code) {
taosHashCancelIterate(pMetaCache->pTableName, ppMetaRes);
return code;
}
if ((code = taosHashPut(pMetaCache->pTableMeta, fullName, strlen(fullName), ppMetaRes, POINTER_BYTES))) {
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;
}
int32_t getTargetMetaImpl(SParseContext* pParCxt, SParseMetaCache* pMetaCache, const SName* pName, STableMeta** pMeta, int32_t getTargetMetaImpl(SParseContext* pParCxt, SParseMetaCache* pMetaCache, const SName* pName, STableMeta** pMeta,
bool couldBeView) { bool couldBeView) {
int32_t code = TSDB_CODE_SUCCESS; int32_t code = TSDB_CODE_SUCCESS;
@ -14459,8 +14524,10 @@ int32_t serializeVgroupsDropTableBatch(SHashObj* pVgroupHashmap, SArray** pOut)
return code; return code;
} }
static int32_t rewriteDropTablewithOpt(STranslateContext* pCxt, SDropTableStmt* pStmt) { static int32_t rewriteDropTablewithOpt(STranslateContext* pCxt, SQuery* pQuery) {
if (!pStmt->withOpt) return TSDB_CODE_SUCCESS; int32_t code = TSDB_CODE_SUCCESS;
SDropTableStmt* pStmt = (SDropTableStmt*)pQuery->pRoot;
if (!pStmt->withOpt) return code;
SNode* pNode = NULL; SNode* pNode = NULL;
char pTableName[TSDB_TABLE_NAME_LEN] = {0}; char pTableName[TSDB_TABLE_NAME_LEN] = {0};
@ -14473,9 +14540,12 @@ static int32_t rewriteDropTablewithOpt(STranslateContext* pCxt, SDropTableStmt*
if (TSDB_CODE_SUCCESS != code) { if (TSDB_CODE_SUCCESS != code) {
return generateSyntaxErrMsgExt(&pCxt->msgBuf, code, "Table uid does not exist: '%s'", pClause->tableName); return generateSyntaxErrMsgExt(&pCxt->msgBuf, code, "Table uid does not exist: '%s'", pClause->tableName);
} }
// tstrncpy(pClause->tableName, pTableMeta->, TSDB_TABLE_NAME_LEN); // rewrite table uid to table name tstrncpy(pClause->tableName, pTableName, TSDB_TABLE_NAME_LEN); // rewrite table uid to table name
} }
return TSDB_CODE_SUCCESS;
code = rewriteDropMetaCache(pCxt);
TAOS_RETURN(code);
} }
static int32_t rewriteDropTable(STranslateContext* pCxt, SQuery* pQuery) { static int32_t rewriteDropTable(STranslateContext* pCxt, SQuery* pQuery) {
@ -14484,7 +14554,7 @@ static int32_t rewriteDropTable(STranslateContext* pCxt, SQuery* pQuery) {
SNode* pNode; SNode* pNode;
SArray* pTsmas = NULL; SArray* pTsmas = NULL;
TAOS_CHECK_RETURN(rewriteDropTablewithOpt(pCxt, pStmt)); TAOS_CHECK_RETURN(rewriteDropTablewithOpt(pCxt, pQuery));
SHashObj* pVgroupHashmap = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK); SHashObj* pVgroupHashmap = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK);
if (NULL == pVgroupHashmap) { if (NULL == pVgroupHashmap) {