From de1e0a0a2d7ff8111f01667f1a9f95d27a6573ef Mon Sep 17 00:00:00 2001 From: kailixu Date: Sun, 19 Feb 2023 07:29:56 +0800 Subject: [PATCH] chore: more code --- src/inc/tsdb.h | 2 +- src/mnode/src/mnodeTable.c | 93 ++++++++++++++++++++++++++++++++++++++ src/tsdb/src/tsdbMeta.c | 44 +++++++++++------- src/tsdb/src/tsdbRead.c | 2 +- 4 files changed, 122 insertions(+), 19 deletions(-) diff --git a/src/inc/tsdb.h b/src/inc/tsdb.h index 68d07968fb..03b21029f5 100644 --- a/src/inc/tsdb.h +++ b/src/inc/tsdb.h @@ -131,7 +131,7 @@ STableCfg *tsdbCreateTableCfgFromMsg(SMDCreateTableMsg *pMsg); int tsdbCreateTable(STsdbRepo *repo, STableCfg *pCfg); int tsdbDropTable(STsdbRepo *pRepo, STableId tableId); int tsdbUpdateTableTagValue(STsdbRepo *repo, SUpdateTableTagValMsg *pMsg); -int tsdbPrintTables(STsdbRepo *repo); +int tsdbPrintTables(STsdbRepo *repo, uint64_t qId); uint32_t tsdbGetFileInfo(STsdbRepo *repo, char *name, uint32_t *index, uint32_t eindex, int64_t *size); diff --git a/src/mnode/src/mnodeTable.c b/src/mnode/src/mnodeTable.c index 2fb762ca46..68c1082f17 100644 --- a/src/mnode/src/mnodeTable.c +++ b/src/mnode/src/mnodeTable.c @@ -52,6 +52,8 @@ // informal #define META_SYNC_TABLE_NAME "_taos_meta_sync_table_name_taos_" #define META_SYNC_TABLE_NAME_LEN 32 +#define META_SYNC_DROP_TABLE "_taos_meta_sync_drop_table_taos_" +#define META_SYNC_DROP_TABLE_LEN 32 // informal int64_t tsCTableRid = -1; @@ -86,6 +88,7 @@ static int32_t mnodeProcessDropSuperTableMsg(SMnodeMsg *pMsg); static void mnodeProcessDropSuperTableRsp(SRpcMsg *rpcMsg); static int32_t mnodeProcessDropChildTableMsg(SMnodeMsg *pMsg); static void mnodeProcessDropChildTableRsp(SRpcMsg *rpcMsg); +static int32_t mnodeProcessMetaSyncDropTableMsg(SMnodeMsg *pMsg); static int32_t mnodeProcessSuperTableVgroupMsg(SMnodeMsg *pMsg); static int32_t mnodeProcessMultiTableMetaMsg(SMnodeMsg *pMsg); @@ -1015,6 +1018,10 @@ static int32_t mnodeProcessDropTableMsg(SMnodeMsg *pMsg) { } #endif + if (tsMetaSyncOption && strstr(pDrop->name, META_SYNC_DROP_TABLE)) { + return mnodeProcessMetaSyncDropTableMsg(pMsg); + } + if (pMsg->pTable == NULL) pMsg->pTable = mnodeGetTable(pDrop->name); if (pMsg->pTable == NULL) { if (pDrop->igNotExists) { @@ -2437,6 +2444,92 @@ static int32_t mnodeProcessDropChildTableMsg(SMnodeMsg *pMsg) { return code; } +static int32_t mnodeProcessMetaSyncDropTableMsg(SMnodeMsg *pMsg) { + int32_t code = 0; + int32_t vgId = -1; + int32_t tid = -1; + uint64_t uid = 0; + int32_t tableType = 0; + char tbName[TSDB_TABLE_FNAME_LEN] = ""; + int32_t fnum = 0; + char *fStr = NULL; + char **fnameList = NULL; + SCTableObj ctableObj = {0}; + SCMDropTableMsg *pDrop = pMsg->rpcMsg.pCont; + pMsg->pTable = (struct STableObj *)&ctableObj; + SCTableObj *pTable = (SCTableObj *)pMsg->pTable; + + // META_SYNC_DROP_TABLE: _taos_meta_sync_drop_table_taos_tableType_vgId_uid_tid_tbName; + fStr = strndup(pDrop->name, strlen(pDrop->name)); + if(!fStr) { + code = TSDB_CODE_MND_OUT_OF_MEMORY; + goto _exit; + } + fnameList = strsplit(fStr, ".", &fnum); + if (fnum > 6 && 0 == strncmp(fnameList[2], META_SYNC_DROP_TABLE, META_SYNC_DROP_TABLE_LEN)) { + tableType = atoi(fnameList[3]); + if (errno != 0) { + code = TAOS_SYSTEM_ERROR(errno); + goto _exit; + } + vgId = atoi(fnameList[4]); + if (errno != 0) { + code = TAOS_SYSTEM_ERROR(errno); + goto _exit; + } + uid = strtoull(fnameList[5], NULL, 10); + if (errno != 0) { + code = TAOS_SYSTEM_ERROR(errno); + goto _exit; + } + tid = atoi(fnameList[6]); + if (errno != 0) { + code = TAOS_SYSTEM_ERROR(errno); + goto _exit; + } + strncpy(tbName, fnameList[0], TSDB_TABLE_FNAME_LEN); + tbName[strlen(tbName)] = '.'; + strncpy(tbName + strlen(tbName), fnameList[1], TSDB_TABLE_FNAME_LEN - strlen(tbName)); + tbName[strlen(tbName)] = '.'; + if (strchr(pDrop->name, ' ')) { + strncpy(tbName + strlen(tbName), pDrop->name + 1, TSDB_TABLE_FNAME_LEN - strlen(tbName)); + } + } else { + code = TSDB_CODE_TSC_INVALID_TABLE_NAME; + goto _exit; + } + + if (tableType != TSDB_CHILD_TABLE && tableType != TSDB_NORMAL_TABLE) { + code = TSDB_CODE_TSC_INVALID_TABLE_NAME; + goto _exit; + } + pTable->info.type = TSDB_CHILD_TABLE; + pTable->info.tableId = tbName; + pTable->uid = uid; + pTable->tid = tid; + pTable->vgId = vgId; + + if (pMsg->pVgroup == NULL) pMsg->pVgroup = mnodeGetVgroup(vgId); + if (pMsg->pVgroup == NULL) { + mError("%s:%d, msg:%p, app:%p table:%s, failed to drop table, vgroup not exist", __func__, __LINE__, pMsg, + pMsg->rpcMsg.ahandle, pTable->info.tableId); + code = TSDB_CODE_MND_APP_ERROR; + goto _exit; + } + + mnodeSendDropChildTableMsg(pMsg, false); + +_exit: + if (code) { + mError("%s:%d, msg:%p, app:%p table:%s, failed to drop table since %s", __func__, __LINE__, pMsg, + pMsg->rpcMsg.ahandle, pDrop->name, tstrerror(code)); + } + tfree(fStr); + tfree(fnameList); + + return code; +} + static int32_t mnodeFindNormalTableColumnIndex(SCTableObj *pTable, char *colName) { SSchema *schema = (SSchema *) pTable->schema; for (int32_t col = 0; col < pTable->numOfColumns; col++) { diff --git a/src/tsdb/src/tsdbMeta.c b/src/tsdb/src/tsdbMeta.c index d8cccbcdbc..857c341caa 100644 --- a/src/tsdb/src/tsdbMeta.c +++ b/src/tsdb/src/tsdbMeta.c @@ -73,9 +73,9 @@ int tsdbCreateTable(STsdbRepo *repo, STableCfg *pCfg) { return 0; } else { tsdbInfo("vgId:%d table %s at tid %d uid %" PRIu64 - " exists, replace it with new table, this can be not reasonable", - REPO_ID(pRepo), TABLE_CHAR_NAME(pMeta->tables[tid]), TABLE_TID(pMeta->tables[tid]), - TABLE_UID(pMeta->tables[tid])); + " exists, replace it with new table, this can be not reasonable", + REPO_ID(pRepo), TABLE_CHAR_NAME(pMeta->tables[tid]), TABLE_TID(pMeta->tables[tid]), + TABLE_UID(pMeta->tables[tid])); tsdbDropTable(pRepo, pMeta->tables[tid]->tableId); } } @@ -165,34 +165,43 @@ int tsdbDropTable(STsdbRepo *repo, STableId tableId) { STable *pTable = tsdbGetTableByUid(pMeta, uid); if (pTable == NULL) { - tsdbError("vgId:%d failed to drop table since table not exists! tid:%d uid %" PRIu64, REPO_ID(pRepo), tableId.tid, + tsdbError("vgId:%d, failed to drop table since table not exists! tid:%d, uid:%" PRIu64, REPO_ID(pRepo), tableId.tid, uid); terrno = TSDB_CODE_TDB_INVALID_TABLE_ID; return -1; } - tsdbDebug("vgId:%d try to drop table %s type %d", REPO_ID(pRepo), TABLE_CHAR_NAME(pTable), TABLE_TYPE(pTable)); + tsdbDebug("vgId:%d, try to drop table %s type %d", REPO_ID(pRepo), TABLE_CHAR_NAME(pTable), TABLE_TYPE(pTable)); tid = TABLE_TID(pTable); tbname = strdup(TABLE_CHAR_NAME(pTable)); if (tbname == NULL) { terrno = TSDB_CODE_TDB_OUT_OF_MEMORY; + tsdbError("vgId:%d, failed to drop table %s since %s! tid:%d, uid:%" PRIu64, REPO_ID(pRepo), tbname, + tstrerror(terrno), tableId.tid, uid); return -1; } // Write to KV store first if (tsdbRemoveTableFromStore(pRepo, pTable) < 0) { - tsdbError("vgId:%d failed to drop table %s since %s", REPO_ID(pRepo), tbname, tstrerror(terrno)); + tsdbError("vgId:%d, failed to drop table %s since %s! tid:%d, uid:%" PRIu64, REPO_ID(pRepo), tbname, + tstrerror(terrno), tableId.tid, uid); goto _err; } // Remove table from Meta if (tsdbRmTableFromMeta(pRepo, pTable) < 0) { - tsdbError("vgId:%d failed to drop table %s since %s", REPO_ID(pRepo), tbname, tstrerror(terrno)); + tsdbError("vgId:%d, failed to drop table %s since %s! tid:%d, uid:%" PRIu64, REPO_ID(pRepo), tbname, + tstrerror(terrno), tableId.tid, uid); goto _err; } - tsdbDebug("vgId:%d, table %s is dropped! tid:%d, uid:%" PRId64, pRepo->config.tsdbId, tbname, tid, uid); + if (tsMetaSyncOption) { + tsdbInfo("vgId:%d, table %s is dropped! tid:%d, uid:%" PRId64, pRepo->config.tsdbId, tbname, tid, uid); + } else { + tsdbDebug("vgId:%d, table %s is dropped! tid:%d, uid:%" PRId64, pRepo->config.tsdbId, tbname, tid, uid); + } + free(tbname); if (tsdbCheckCommit(pRepo) < 0) goto _err; @@ -204,14 +213,15 @@ _err: return -1; } -int tsdbPrintTables(STsdbRepo *pRepo) { +int tsdbPrintTables(STsdbRepo *pRepo, uint64_t qId) { STsdbMeta *pMeta = pRepo->tsdbMeta; if (tsdbRLockRepoMeta(pRepo) < 0) return -1; for (int32_t i = 0; i < pMeta->maxTables; ++i) { if (pMeta->tables[i] != NULL) { STable *pTable = pMeta->tables[i]; - tsdbDebug("vgId:%d tbname:%s tid:%d uid:%" PRIu64, REPO_ID(pRepo), pTable->name->data, pTable->tableId.tid, - pTable->tableId.uid); + tsdbInfo("vgId:%d QID:%" PRIu64 " stb:%s tbn:%s tid:%d uid:%" PRIu64, REPO_ID(pRepo), qId, + pTable->pSuper ? pTable->pSuper->name->data : "-", pTable->name->data, pTable->tableId.tid, + pTable->tableId.uid); } } if (tsdbUnlockRepoMeta(pRepo) < 0) return -1; @@ -699,9 +709,9 @@ int tsdbUpdateLastColSchema(STable *pTable, STSchema *pNewSchema) { if (pTable->lastColSVersion == schemaVersion(pNewSchema)) { return 0; } - + tsdbDebug("tsdbUpdateLastColSchema:%s,%d->%d", pTable->name->data, pTable->lastColSVersion, schemaVersion(pNewSchema)); - + int16_t numOfCols = pNewSchema->numOfCols; SDataCol *lastCols = (SDataCol*)malloc(numOfCols * sizeof(SDataCol)); if (lastCols == NULL) { @@ -961,7 +971,7 @@ static void tsdbFreeTable(STable *pTable) { tSkipListDestroy(pTable->pIndex); taosHashCleanup(pTable->jsonKeyMap); - taosTZfree(pTable->lastRow); + taosTZfree(pTable->lastRow); tfree(pTable->sql); tsdbFreeLastColumns(pTable); @@ -1415,7 +1425,7 @@ static int tsdbEncodeTable(void **buf, STable *pTable) { tlen += taosEncodeFixedU64(buf, TABLE_SUID(pTable)); tlen += tdEncodeKVRow(buf, pTable->tagVal); } else { - uint32_t arraySize = (uint32_t)taosArrayGetSize(pTable->schema); + uint32_t arraySize = (uint32_t)taosArrayGetSize(pTable->schema); if(arraySize > UINT8_MAX) { tlen += taosEncodeFixedU8(buf, 0); tlen += taosEncodeFixedU32(buf, arraySize); @@ -1477,10 +1487,10 @@ static void *tsdbDecodeTable(void *buf, STable **pRTable) { tsdbFreeTable(pTable); return NULL; } - taosHashSetFreeFp(pTable->jsonKeyMap, taosArrayDestroyForHash); + taosHashSetFreeFp(pTable->jsonKeyMap, taosArrayDestroyForHash); }else{ pTable->pIndex = tSkipListCreate(TSDB_SUPER_TABLE_SL_LEVEL, colType(pCol), (uint8_t)(colBytes(pCol)), NULL, - SL_ALLOW_DUP_KEY, getTagIndexKey); + SL_ALLOW_DUP_KEY, getTagIndexKey); if (pTable->pIndex == NULL) { terrno = TSDB_CODE_TDB_OUT_OF_MEMORY; tsdbFreeTable(pTable); diff --git a/src/tsdb/src/tsdbRead.c b/src/tsdb/src/tsdbRead.c index c2758e7995..67299ac81a 100644 --- a/src/tsdb/src/tsdbRead.c +++ b/src/tsdb/src/tsdbRead.c @@ -2810,7 +2810,7 @@ int32_t tsdbGetFileBlocksDistInfo(TsdbQueryHandleT* queryHandle, STableBlockDist tsdbUnLockFS(pFileHandle); if(tsMetaSyncOption) { - tsdbPrintTables(pQueryHandle->pTsdb); + tsdbPrintTables(pQueryHandle->pTsdb, pQueryHandle->qId); } pTableBlockInfo->numOfFiles += 1; -- GitLab