From 5c8ce60dd02e38b5f3624078638a88173c6eb222 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sun, 8 Dec 2024 16:54:30 +0800 Subject: [PATCH] more code --- include/util/tutil.h | 4 +- source/dnode/vnode/src/meta/metaEntry2.c | 402 +++++++++++++++++++---- source/dnode/vnode/src/meta/metaTable2.c | 393 ++++++++++++---------- source/libs/tdb/inc/tdb.h | 5 + 4 files changed, 574 insertions(+), 230 deletions(-) diff --git a/include/util/tutil.h b/include/util/tutil.h index c1b6b2505b..07c47009c6 100644 --- a/include/util/tutil.h +++ b/include/util/tutil.h @@ -127,10 +127,10 @@ static FORCE_INLINE int32_t taosGetTbHashVal(const char *tbname, int32_t tblen, * only in very hot code paths. Misuse or abuse can lead to performance degradation. */ #if __GNUC__ >= 3 -#define LIKELY(x) __builtin_expect((x) != 0, 1) +#define LIKELY(x) __builtin_expect((x) != 0, 1) #define UNLIKELY(x) __builtin_expect((x) != 0, 0) #else -#define LIKELY(x) ((x) != 0) +#define LIKELY(x) ((x) != 0) #define UNLIKELY(x) ((x) != 0) #endif diff --git a/source/dnode/vnode/src/meta/metaEntry2.c b/source/dnode/vnode/src/meta/metaEntry2.c index cb385d449a..77effc7e13 100644 --- a/source/dnode/vnode/src/meta/metaEntry2.c +++ b/source/dnode/vnode/src/meta/metaEntry2.c @@ -23,6 +23,7 @@ typedef enum { META_UID_IDX, META_NAME_IDX, META_SUID_IDX, + META_CHILD_IDX, META_TAG_IDX, META_BTIME_IDX, META_TTL_IDX, @@ -101,7 +102,8 @@ static int32_t metaEntryTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) { static int32_t metaEntryTableDelete(SMeta *pMeta, const SMetaEntry *pEntry) { int32_t code = TSDB_CODE_SUCCESS; - int32_t vgId = TD_VID(pMeta->pVnode); + + // SMetaEntry entry { .version = ; }; // TODO @@ -175,6 +177,11 @@ static int32_t metaSchemaTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) { return metaSchemaTableUpsert(pMeta, pEntry, META_TABLE_OP_UPDATA); } +static int32_t metaSchemaTableDelete(SMeta *pMeta, const SMetaEntry *pEntry) { + // TODO + return TSDB_CODE_SUCCESS; +} + // Uid Index static void metaBuildEntryInfo(const SMetaEntry *pEntry, SMetaInfo *pInfo) { pInfo->uid = pEntry->uid; @@ -225,6 +232,20 @@ static int32_t metaUidIdxUpdate(SMeta *pMeta, const SMetaEntry *pEntry) { return metaUidIdxUpsert(pMeta, pEntry, META_TABLE_OP_UPDATA); } +static int32_t metaUidIdxDelete(SMeta *pMeta, const SMetaEntry *pEntry) { + int32_t code = 0; + + // delete tdb + code = tdbTbDelete(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), pMeta->txn); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + } + + // delete cache + (void)metaCacheDrop(pMeta, pEntry->uid); + return code; +} + // Name Index static int32_t metaNameIdxUpsert(SMeta *pMeta, const SMetaEntry *pEntry, EMetaTableOp op) { int32_t code = TSDB_CODE_SUCCESS; @@ -241,12 +262,30 @@ static int32_t metaNameIdxUpsert(SMeta *pMeta, const SMetaEntry *pEntry, EMetaTa } static int32_t metaNameIdxInsert(SMeta *pMeta, const SMetaEntry *pEntry) { - return metaNameIdxUpsert(pMeta, pEntry, META_TABLE_OP_INSERT); + int32_t code = TSDB_CODE_SUCCESS; + code = metaNameIdxUpsert(pMeta, pEntry, META_TABLE_OP_INSERT); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + } + return code; +} + +static int32_t metaNameIdxUpdate(SMeta *pMeta, const SMetaEntry *pEntry) { + int32_t code = TSDB_CODE_SUCCESS; + code = metaNameIdxUpsert(pMeta, pEntry, META_TABLE_OP_UPDATA); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + } + return code; } static int32_t metaNameIdxDelete(SMeta *pMeta, const SMetaEntry *pEntry) { - // TODO - return 0; + int32_t code = TSDB_CODE_SUCCESS; + code = tdbTbDelete(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, pMeta->txn); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + } + return code; } // Suid Index @@ -258,7 +297,66 @@ static int32_t metaSUidIdxInsert(SMeta *pMeta, const SMetaEntry *pEntry) { return code; } +static int32_t metaSUidIdxDelete(SMeta *pMeta, const SMetaEntry *pEntry) { + int32_t code = tdbTbDelete(pMeta->pSuidIdx, &pEntry->uid, sizeof(pEntry->uid), pMeta->txn); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + } + return code; +} + +// Child Index +static int32_t metaChildIdxUpsert(SMeta *pMeta, const SMetaEntry *pEntry, EMetaTableOp op) { + int32_t code = TSDB_CODE_SUCCESS; + + SCtbIdxKey key = { + .suid = pEntry->ctbEntry.suid, + .uid = pEntry->uid, + }; + + if (META_TABLE_OP_INSERT == op) { + code = tdbTbInsert(pMeta->pCtbIdx, &key, sizeof(key), pEntry->ctbEntry.pTags, + ((STag *)(pEntry->ctbEntry.pTags))->len, pMeta->txn); + } else if (META_TABLE_OP_UPDATA == op) { + code = tdbTbUpsert(pMeta->pCtbIdx, &key, sizeof(key), pEntry->ctbEntry.pTags, + ((STag *)(pEntry->ctbEntry.pTags))->len, pMeta->txn); + } else { + code = TSDB_CODE_INVALID_PARA; + } + return code; +} + +static int32_t metaChildIdxInsert(SMeta *pMeta, const SMetaEntry *pEntry) { + return metaChildIdxUpsert(pMeta, pEntry, META_TABLE_OP_INSERT); +} + +static int32_t metaChildIdxUpdate(SMeta *pMeta, const SMetaEntry *pEntry) { + return metaChildIdxUpsert(pMeta, pEntry, META_TABLE_OP_UPDATA); +} + +static int32_t metaChildIdxDelete(SMeta *pMeta, const SMetaEntry *pEntry) { + SCtbIdxKey key = { + .suid = pEntry->ctbEntry.suid, + .uid = pEntry->uid, + }; + return tdbTbDelete(pMeta->pCtbIdx, &key, sizeof(key), pMeta->txn); +} + // Tag Index +static int32_t metaTagIdxInsert(SMeta *pMeta, const SMetaEntry *pEntry) { + // TODO + return 0; +} + +static int32_t metaTagIdxUpdate(SMeta *pMeta, const SMetaEntry *pEntry) { + // TODO + return 0; +} + +static int32_t metaTagIdxDelete(SMeta *pMeta, const SMetaEntry *pEntry) { + // TODO + return 0; +} // Btime Index static int32_t metaBtimeIdxUpsert(SMeta *pMeta, const SMetaEntry *pEntry, EMetaTableOp op) { @@ -287,55 +385,101 @@ static int32_t metaBtimeIdxInsert(SMeta *pMeta, const SMetaEntry *pEntry) { return metaBtimeIdxUpsert(pMeta, pEntry, META_TABLE_OP_INSERT); } -static int32_t (*metaTableOpFn[META_TABLE_MAX][META_TABLE_OP_MAX])(SMeta *pMeta, const SMetaEntry *pEntry) = { - [META_ENTRY_TABLE] = - { - [META_TABLE_OP_INSERT] = metaEntryTableInsert, - [META_TABLE_OP_UPDATA] = metaEntryTableUpdate, - [META_TABLE_OP_DELETE] = metaEntryTableDelete, - }, - [META_SCHEMA_TABLE] = - { - [META_TABLE_OP_INSERT] = metaSchemaTableInsert, - [META_TABLE_OP_UPDATA] = metaSchemaTableUpdate, - [META_TABLE_OP_DELETE] = NULL, - }, - [META_UID_IDX] = - { - [META_TABLE_OP_INSERT] = metaUidIdxInsert, - [META_TABLE_OP_UPDATA] = metaUidIdxUpdate, - [META_TABLE_OP_DELETE] = NULL, - }, - [META_NAME_IDX] = - { - [META_TABLE_OP_INSERT] = metaNameIdxInsert, - [META_TABLE_OP_UPDATA] = NULL, - [META_TABLE_OP_DELETE] = NULL, - }, - [META_SUID_IDX] = - { - [META_TABLE_OP_INSERT] = metaSUidIdxInsert, - [META_TABLE_OP_UPDATA] = NULL, - [META_TABLE_OP_DELETE] = NULL, - }, - [META_TAG_IDX] = - { - [META_TABLE_OP_INSERT] = NULL, - [META_TABLE_OP_UPDATA] = NULL, - [META_TABLE_OP_DELETE] = NULL, - }, - [META_BTIME_IDX] = - { - [META_TABLE_OP_INSERT] = metaBtimeIdxInsert, - [META_TABLE_OP_UPDATA] = NULL, - [META_TABLE_OP_DELETE] = NULL, - }, - [META_TTL_IDX] = - { - [META_TABLE_OP_INSERT] = NULL, - [META_TABLE_OP_UPDATA] = NULL, - [META_TABLE_OP_DELETE] = NULL, - }, +static int32_t metaBtimeIdxUpdate(SMeta *pMeta, const SMetaEntry *pEntry) { + return metaBtimeIdxUpsert(pMeta, pEntry, META_TABLE_OP_UPDATA); +} + +static int32_t metaBtimeIdxDelete(SMeta *pMeta, const SMetaEntry *pEntry) { + int32_t code = TSDB_CODE_SUCCESS; + SBtimeIdxKey key = { + .uid = pEntry->uid, + }; + + if (TSDB_CHILD_TABLE == pEntry->type) { + key.btime = pEntry->ctbEntry.btime; + } else if (TSDB_NORMAL_TABLE == pEntry->type) { + key.btime = pEntry->ntbEntry.btime; + } else { + return TSDB_CODE_INVALID_PARA; + } + return tdbTbDelete(pMeta->pBtimeIdx, &key, sizeof(key), pMeta->txn); +} + +// TTL Index +static int32_t metaTtlIdxUpsert(SMeta *pMeta, const SMetaEntry *pEntry, EMetaTableOp op) { + // TODO + return 0; +} + +static int32_t metaTtlIdxInsert(SMeta *pMeta, const SMetaEntry *pEntry) { + return metaTtlIdxUpsert(pMeta, pEntry, META_TABLE_OP_INSERT); +} + +static int32_t metaTtlIdxUpdate(SMeta *pMeta, const SMetaEntry *pEntry) { + return metaTtlIdxUpsert(pMeta, pEntry, META_TABLE_OP_UPDATA); +} + +static int32_t metaTtlIdxDelete(SMeta *pMeta, const SMetaEntry *pEntry) { + // TODO + return 0; +} + +static int32_t (*metaTableOpFn[META_TABLE_MAX][META_TABLE_OP_MAX])(SMeta *pMeta, const SMetaEntry *pEntry) = + { + [META_ENTRY_TABLE] = + { + [META_TABLE_OP_INSERT] = metaEntryTableInsert, + [META_TABLE_OP_UPDATA] = metaEntryTableUpdate, + [META_TABLE_OP_DELETE] = metaEntryTableDelete, + }, + [META_SCHEMA_TABLE] = + { + [META_TABLE_OP_INSERT] = metaSchemaTableInsert, + [META_TABLE_OP_UPDATA] = metaSchemaTableUpdate, + [META_TABLE_OP_DELETE] = metaSchemaTableDelete, + }, + [META_UID_IDX] = + { + [META_TABLE_OP_INSERT] = metaUidIdxInsert, + [META_TABLE_OP_UPDATA] = metaUidIdxUpdate, + [META_TABLE_OP_DELETE] = metaUidIdxDelete, + }, + [META_NAME_IDX] = + { + [META_TABLE_OP_INSERT] = metaNameIdxInsert, + [META_TABLE_OP_UPDATA] = metaNameIdxUpdate, + [META_TABLE_OP_DELETE] = metaNameIdxDelete, + }, + [META_SUID_IDX] = + { + [META_TABLE_OP_INSERT] = metaSUidIdxInsert, + [META_TABLE_OP_UPDATA] = NULL, + [META_TABLE_OP_DELETE] = metaSUidIdxDelete, + }, + [META_CHILD_IDX] = + { + [META_TABLE_OP_INSERT] = metaChildIdxInsert, + [META_TABLE_OP_UPDATA] = metaChildIdxUpdate, + [META_TABLE_OP_DELETE] = metaChildIdxDelete, + }, + [META_TAG_IDX] = + { + [META_TABLE_OP_INSERT] = metaTagIdxInsert, + [META_TABLE_OP_UPDATA] = metaTagIdxUpdate, + [META_TABLE_OP_DELETE] = metaTagIdxDelete, + }, + [META_BTIME_IDX] = + { + [META_TABLE_OP_INSERT] = metaBtimeIdxInsert, + [META_TABLE_OP_UPDATA] = metaBtimeIdxUpdate, + [META_TABLE_OP_DELETE] = metaBtimeIdxDelete, + }, + [META_TTL_IDX] = + { + [META_TABLE_OP_INSERT] = metaTtlIdxInsert, + [META_TABLE_OP_UPDATA] = metaTtlIdxUpdate, + [META_TABLE_OP_DELETE] = metaTtlIdxDelete, + }, }; static int32_t metaHandleSuperTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) { @@ -389,8 +533,8 @@ static int32_t metaHandleNormalTableCreateImpl(SMeta *pMeta, const SMetaEntry *p {META_UID_IDX, META_TABLE_OP_INSERT}, // {META_NAME_IDX, META_TABLE_OP_INSERT}, // {META_BTIME_IDX, META_TABLE_OP_INSERT}, // - {META_TTL_IDX, META_TABLE_OP_INSERT}, // - // TODO: need ncol idx + + // {META_TTL_IDX, META_TABLE_OP_INSERT}, // }; for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) { @@ -418,6 +562,7 @@ static int32_t metaHandleNormalTableCreate(SMeta *pMeta, const SMetaEntry *pEntr pMeta->pVnode->config.vndStats.numOfNTables++; pMeta->pVnode->config.vndStats.numOfNTimeSeries += pEntry->ntbEntry.schemaRow.nCols - 1; pMeta->changed = true; + if (!TSDB_CACHE_NO(pMeta->pVnode->config)) { int32_t rc = tsdbCacheNewTable(pMeta->pVnode->pTsdb, pEntry->uid, -1, &pEntry->ntbEntry.schemaRow); if (rc < 0) { @@ -430,9 +575,148 @@ static int32_t metaHandleNormalTableCreate(SMeta *pMeta, const SMetaEntry *pEntr return code; } -static int32_t metaHandleNormalTableDrop(SMeta *pMeta, const SMetaEntry *pEntry) { +static int32_t metaHandleChildTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) { int32_t code = TSDB_CODE_SUCCESS; - // TODO + + SMetaTableOp ops[] = { + {META_ENTRY_TABLE, META_TABLE_OP_INSERT}, // + {META_UID_IDX, META_TABLE_OP_INSERT}, // + {META_NAME_IDX, META_TABLE_OP_INSERT}, // + {META_CHILD_IDX, META_TABLE_OP_INSERT}, // + {META_TAG_IDX, META_TABLE_OP_INSERT}, // + {META_BTIME_IDX, META_TABLE_OP_INSERT}, // + {META_TTL_IDX, META_TABLE_OP_INSERT}, // + }; + + for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) { + SMetaTableOp *op = &ops[i]; + + code = metaTableOpFn[op->table][op->op](pMeta, pEntry); + if (TSDB_CODE_SUCCESS != code) { + metaErr(TD_VID(pMeta->pVnode), code); + return code; + } + } + + return code; +} + +static int32_t metaHandleChildTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) { + int32_t code = TSDB_CODE_SUCCESS; + + // update TDB + metaWLock(pMeta); + code = metaHandleChildTableCreateImpl(pMeta, pEntry); + + if (TSDB_CODE_SUCCESS == code) { + // metaUpdateStbStats(pMeta, me.ctbEntry.suid, 1, 0); + // ret = metaUidCacheClear(pMeta, me.ctbEntry.suid); + // if (ret < 0) { + // metaError("vgId:%d, failed to clear uid cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, + // pReq->ctb.suid, tstrerror(ret)); + // } + // ret = metaTbGroupCacheClear(pMeta, me.ctbEntry.suid); + // if (ret < 0) { + // metaError("vgId:%d, failed to clear group cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), + // pReq->name, + // pReq->ctb.suid, tstrerror(ret)); + // } + } + metaULock(pMeta); + + // update other stuff + if (TSDB_CODE_SUCCESS != code) { + pMeta->pVnode->config.vndStats.numOfCTables++; + + // if (!metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1)) { + // int32_t nCols = 0; + // ret = metaGetStbStats(pMeta->pVnode, me.ctbEntry.suid, 0, &nCols); + // if (ret < 0) { + // metaError("vgId:%d, failed to get stb stats:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), + // pReq->name, + // pReq->ctb.suid, tstrerror(ret)); + // } + // pStats->numOfTimeSeries += nCols - 1; + // } + + if (!TSDB_CACHE_NO(pMeta->pVnode->config)) { + int32_t rc = tsdbCacheNewTable(pMeta->pVnode->pTsdb, pEntry->uid, pEntry->ctbEntry.suid, NULL); + if (rc < 0) { + metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, + tstrerror(rc)); + } + } + pMeta->changed = true; + } else { + metaErr(TD_VID(pMeta->pVnode), code); + } + return code; +} + +static int32_t metaHandleNormalTableDropImpl(SMeta *pMeta, const SMetaEntry *pEntry) { + int32_t code = TSDB_CODE_SUCCESS; + + SMetaTableOp ops[] = { + {META_ENTRY_TABLE, META_TABLE_OP_DELETE}, // + {META_UID_IDX, META_TABLE_OP_DELETE}, // + {META_NAME_IDX, META_TABLE_OP_DELETE}, // + {META_BTIME_IDX, META_TABLE_OP_DELETE}, // + + // {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA}, // TODO: need to be insert + // {META_TTL_IDX, META_TABLE_OP_INSERT}, // + }; + + for (int32_t i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) { + SMetaTableOp *op = &ops[i]; + code = metaTableOpFn[op->table][op->op](pMeta, pEntry); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + } + } + + return code; +} + +static int32_t metaHandleNormalTableDrop(SMeta *pMeta, const SMetaEntry *pEntry) { + int32_t code = TSDB_CODE_SUCCESS; + SMetaEntry entry = {0}; + + // TODO: get entry + + metaWLock(pMeta); + code = metaHandleNormalTableDropImpl(pMeta, &entry); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + } else { + code = metaTableOpFn[META_ENTRY_TABLE][META_TABLE_OP_INSERT](pMeta, pEntry); + } + metaULock(pMeta); + + if (TSDB_CODE_SUCCESS == code) { + pMeta->pVnode->config.vndStats.numOfNTables--; + pMeta->pVnode->config.vndStats.numOfNTimeSeries -= (entry.ntbEntry.schemaRow.nCols - 1); + + // if ((type == TSDB_CHILD_TABLE || type == TSDB_NORMAL_TABLE) && tbUids) { + // if (taosArrayPush(tbUids, &uid) == NULL) { + // rc = terrno; + // goto _exit; + // } + + // if (!TSDB_CACHE_NO(pMeta->pVnode->config)) { + // int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, uid, suid, NULL); + // if (ret < 0) { + // metaError("vgId:%d, failed to drop table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, + // uid, + // tstrerror(ret)); + // } + // } + // } + + pMeta->changed = true; + } else { + metaErr(TD_VID(pMeta->pVnode), code); + } + return code; } @@ -478,7 +762,7 @@ int32_t metaHandleEntry2(SMeta *pMeta, const SMetaEntry *pEntry) { if (isExist) { // code = metaHandleChildTableUpdate(pMeta, pEntry); } else { - // code = metaHandleChildTableCreate(pMeta, pEntry); + code = metaHandleChildTableCreate(pMeta, pEntry); } break; } @@ -506,7 +790,7 @@ int32_t metaHandleEntry2(SMeta *pMeta, const SMetaEntry *pEntry) { break; } case TSDB_NORMAL_TABLE: { - // code = metaHandleNormalTableDrop(pMeta, pEntry); + code = metaHandleNormalTableDrop(pMeta, pEntry); break; } default: { diff --git a/source/dnode/vnode/src/meta/metaTable2.c b/source/dnode/vnode/src/meta/metaTable2.c index b0e4923d22..37116f9f89 100644 --- a/source/dnode/vnode/src/meta/metaTable2.c +++ b/source/dnode/vnode/src/meta/metaTable2.c @@ -120,178 +120,149 @@ int32_t metaCreateSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq // Alter Super Table // Create Child Table +static int32_t metaCheckCreateChildTableReq(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq) { + int32_t code = TSDB_CODE_SUCCESS; + void *value = NULL; + int32_t valueSize = 0; + SMetaInfo info; + + if (NULL == pReq->name || strlen(pReq->name) == 0 || NULL == pReq->ctb.stbName || strlen(pReq->ctb.stbName) == 0 || + pReq->ctb.suid == 0) { + metaError("vgId:%d, %s failed at %s:%d since invalid name:%s stb name:%s, version:%" PRId64, TD_VID(pMeta->pVnode), + __func__, __FILE__, __LINE__, pReq->name, pReq->ctb.stbName, version); + return TSDB_CODE_INVALID_MSG; + } + + // check super table existence + if (tdbTbGet(pMeta->pNameIdx, pReq->ctb.stbName, strlen(pReq->ctb.stbName) + 1, &value, &valueSize) == 0) { + int64_t suid = *(int64_t *)value; + tdbFreeClear(value); + if (suid != pReq->ctb.suid) { + metaError("vgId:%d, %s failed at %s:%d since super table %s has uid %" PRId64 " instead of %" PRId64 + ", version:%" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->ctb.stbName, suid, pReq->ctb.suid, version); + return TSDB_CODE_PAR_TABLE_NOT_EXIST; + } + } else { + metaError("vgId:%d, %s failed at %s:%d since super table %s does not eixst, version:%" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->ctb.stbName, version); + return TSDB_CODE_PAR_TABLE_NOT_EXIST; + } + + // check super table is a super table + if (metaGetInfo(pMeta, pReq->ctb.suid, &info, NULL) != TSDB_CODE_SUCCESS) { + metaError("vgId:%d, %s failed at %s:%d since cannot find table with uid %" PRId64 + ", which is an internal error, version:%" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->ctb.suid, version); + return TSDB_CODE_INTERNAL_ERROR; + } else if (info.suid != info.uid) { + metaError("vgId:%d, %s failed at %s:%d since table with uid %" PRId64 " is not a super table, version:%" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->ctb.suid, version); + return TSDB_CODE_INVALID_MSG; + } + + // check table existence + if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize) == 0) { + pReq->uid = *(int64_t *)value; + tdbFreeClear(value); + + if (metaGetInfo(pMeta, pReq->uid, &info, NULL) != 0) { + metaError("vgId:%d, %s failed at %s:%d since cannot find table with uid %" PRId64 + ", which is an internal error, version:%" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->uid, version); + return TSDB_CODE_INTERNAL_ERROR; + } + + // check table type and suid + if (info.suid != pReq->ctb.suid) { + metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " exists in another stable with uid %" PRId64 + " instead of stable with uid %" PRId64 " version:%" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pReq->uid, info.suid, pReq->ctb.suid, + version); + return TSDB_CODE_TDB_TABLE_IN_OTHER_STABLE; + } + + return TSDB_CODE_TDB_TABLE_ALREADY_EXIST; + } + + // check grant + if (!metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1)) { + code = grantCheck(TSDB_GRANT_TIMESERIES); + if (TSDB_CODE_SUCCESS != code) { + metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__, + __FILE__, __LINE__, tstrerror(code), version, pReq->name); + } + } + return code; +} + +static int32_t metaBuildCreateChildTableRsp(SMeta *pMeta, const SMetaEntry *pEntry, STableMetaRsp **ppRsp) { + int32_t code = TSDB_CODE_SUCCESS; + + if (NULL == ppRsp) { + return code; + } + + *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp)); + if (NULL == ppRsp) { + return terrno; + } + + (*ppRsp)->tableType = TSDB_CHILD_TABLE; + (*ppRsp)->tuid = pEntry->uid; + (*ppRsp)->suid = pEntry->ctbEntry.suid; + tstrncpy((*ppRsp)->tbName, pEntry->name, TSDB_TABLE_NAME_LEN); + + return code; +} + static int32_t metaCreateChildTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) { int32_t code = TSDB_CODE_SUCCESS; - // TODO + + // check request + code = metaCheckCreateChildTableReq(pMeta, version, pReq); + if (code) { + if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) { + metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__, + __FILE__, __LINE__, tstrerror(code), version, pReq->name); + } + return code; + } + + SMetaEntry entry = { + .version = version, + .type = TSDB_CHILD_TABLE, + .uid = pReq->uid, + .name = pReq->name, + .ctbEntry.btime = pReq->btime, + .ctbEntry.ttlDays = pReq->ttl, + .ctbEntry.commentLen = pReq->commentLen, + .ctbEntry.comment = pReq->comment, + .ctbEntry.suid = pReq->ctb.suid, + .ctbEntry.pTags = pReq->ctb.pTag, + }; + + // build response + code = metaBuildCreateChildTableRsp(pMeta, &entry, ppRsp); + if (code) { + metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, + tstrerror(code)); + } + + // handle entry + code = metaHandleEntry2(pMeta, &entry); + if (TSDB_CODE_SUCCESS == code) { + metaInfo("vgId:%d, child table:%s uid %" PRId64 " suid:%" PRId64 " is created, version:%" PRId64, + TD_VID(pMeta->pVnode), pReq->name, pReq->uid, pReq->ctb.suid, version); + } else { + metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s suid:%" PRId64 " version:%" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, + pReq->ctb.suid, version); + } return code; #if 0 - SMetaEntry me = {0}; - SMetaReader mr = {0}; - int32_t ret; - - // validate message - if (pReq->type != TSDB_CHILD_TABLE && pReq->type != TSDB_NORMAL_TABLE) { - terrno = TSDB_CODE_INVALID_MSG; - goto _err; - } - - if (pReq->type == TSDB_CHILD_TABLE) { - tb_uid_t suid = metaGetTableEntryUidByName(pMeta, pReq->ctb.stbName); - if (suid != pReq->ctb.suid) { - return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST; - } - } - - // validate req - metaReaderDoInit(&mr, pMeta, META_READER_LOCK); - if (metaGetTableEntryByName(&mr, pReq->name) == 0) { - if (pReq->type == TSDB_CHILD_TABLE && pReq->ctb.suid != mr.me.ctbEntry.suid) { - metaReaderClear(&mr); - return terrno = TSDB_CODE_TDB_TABLE_IN_OTHER_STABLE; - } - pReq->uid = mr.me.uid; - if (pReq->type == TSDB_CHILD_TABLE) { - pReq->ctb.suid = mr.me.ctbEntry.suid; - } - metaReaderClear(&mr); - return terrno = TSDB_CODE_TDB_TABLE_ALREADY_EXIST; - } else if (terrno == TSDB_CODE_PAR_TABLE_NOT_EXIST) { - terrno = TSDB_CODE_SUCCESS; - } - metaReaderClear(&mr); - - bool sysTbl = (pReq->type == TSDB_CHILD_TABLE) && metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1); - - if (!sysTbl && ((terrno = grantCheck(TSDB_GRANT_TIMESERIES)) < 0)) goto _err; - - // build SMetaEntry - SVnodeStats *pStats = &pMeta->pVnode->config.vndStats; - me.version = ver; - me.type = pReq->type; - me.uid = pReq->uid; - me.name = pReq->name; - if (me.type == TSDB_CHILD_TABLE) { - me.ctbEntry.btime = pReq->btime; - me.ctbEntry.ttlDays = pReq->ttl; - me.ctbEntry.commentLen = pReq->commentLen; - me.ctbEntry.comment = pReq->comment; - me.ctbEntry.suid = pReq->ctb.suid; - me.ctbEntry.pTags = pReq->ctb.pTag; - -#ifdef TAG_FILTER_DEBUG - SArray *pTagVals = NULL; - int32_t code = tTagToValArray((STag *)pReq->ctb.pTag, &pTagVals); - for (int i = 0; i < taosArrayGetSize(pTagVals); i++) { - STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i); - - if (IS_VAR_DATA_TYPE(pTagVal->type)) { - char *buf = taosMemoryCalloc(pTagVal->nData + 1, 1); - memcpy(buf, pTagVal->pData, pTagVal->nData); - metaDebug("metaTag table:%s varchar index:%d cid:%d type:%d value:%s", pReq->name, i, pTagVal->cid, - pTagVal->type, buf); - taosMemoryFree(buf); - } else { - double val = 0; - GET_TYPED_DATA(val, double, pTagVal->type, &pTagVal->i64); - metaDebug("metaTag table:%s number index:%d cid:%d type:%d value:%f", pReq->name, i, pTagVal->cid, - pTagVal->type, val); - } - } -#endif - - ++pStats->numOfCTables; - - if (!sysTbl) { - int32_t nCols = 0; - ret = metaGetStbStats(pMeta->pVnode, me.ctbEntry.suid, 0, &nCols); - if (ret < 0) { - metaError("vgId:%d, failed to get stb stats:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, - pReq->ctb.suid, tstrerror(ret)); - } - pStats->numOfTimeSeries += nCols - 1; - } - - metaWLock(pMeta); - metaUpdateStbStats(pMeta, me.ctbEntry.suid, 1, 0); - ret = metaUidCacheClear(pMeta, me.ctbEntry.suid); - if (ret < 0) { - metaError("vgId:%d, failed to clear uid cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, - pReq->ctb.suid, tstrerror(ret)); - } - ret = metaTbGroupCacheClear(pMeta, me.ctbEntry.suid); - if (ret < 0) { - metaError("vgId:%d, failed to clear group cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, - pReq->ctb.suid, tstrerror(ret)); - } - metaULock(pMeta); - - if (!TSDB_CACHE_NO(pMeta->pVnode->config)) { - ret = tsdbCacheNewTable(pMeta->pVnode->pTsdb, me.uid, me.ctbEntry.suid, NULL); - if (ret < 0) { - metaError("vgId:%d, failed to create table:%s since %s", TD_VID(pMeta->pVnode), pReq->name, tstrerror(ret)); - goto _err; - } - } - } else { - me.ntbEntry.btime = pReq->btime; - me.ntbEntry.ttlDays = pReq->ttl; - me.ntbEntry.commentLen = pReq->commentLen; - me.ntbEntry.comment = pReq->comment; - me.ntbEntry.schemaRow = pReq->ntb.schemaRow; - me.ntbEntry.ncid = me.ntbEntry.schemaRow.pSchema[me.ntbEntry.schemaRow.nCols - 1].colId + 1; - me.colCmpr = pReq->colCmpr; - TABLE_SET_COL_COMPRESSED(me.flags); - - ++pStats->numOfNTables; - pStats->numOfNTimeSeries += me.ntbEntry.schemaRow.nCols - 1; - - if (!TSDB_CACHE_NO(pMeta->pVnode->config)) { - ret = tsdbCacheNewTable(pMeta->pVnode->pTsdb, me.uid, -1, &me.ntbEntry.schemaRow); - if (ret < 0) { - metaError("vgId:%d, failed to create table:%s since %s", TD_VID(pMeta->pVnode), pReq->name, tstrerror(ret)); - goto _err; - } - } - } - - if (metaHandleEntry(pMeta, &me) < 0) goto _err; - metaTimeSeriesNotifyCheck(pMeta); - - if (pMetaRsp) { - *pMetaRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp)); - - if (*pMetaRsp) { - if (me.type == TSDB_CHILD_TABLE) { - (*pMetaRsp)->tableType = TSDB_CHILD_TABLE; - (*pMetaRsp)->tuid = pReq->uid; - (*pMetaRsp)->suid = pReq->ctb.suid; - strcpy((*pMetaRsp)->tbName, pReq->name); - } else { - ret = metaUpdateMetaRsp(pReq->uid, pReq->name, &pReq->ntb.schemaRow, *pMetaRsp); - if (ret < 0) { - metaError("vgId:%d, failed to update meta rsp:%s since %s", TD_VID(pMeta->pVnode), pReq->name, - tstrerror(ret)); - } - for (int32_t i = 0; i < pReq->colCmpr.nCols; i++) { - SColCmpr *p = &pReq->colCmpr.pColCmpr[i]; - (*pMetaRsp)->pSchemaExt[i].colId = p->id; - (*pMetaRsp)->pSchemaExt[i].compress = p->alg; - } - } - } - } - - pMeta->changed = true; - metaDebug("vgId:%d, table:%s uid %" PRId64 " is created, type:%" PRId8, TD_VID(pMeta->pVnode), pReq->name, pReq->uid, - pReq->type); - return 0; - -_err: - metaError("vgId:%d, failed to create table:%s type:%s since %s", TD_VID(pMeta->pVnode), pReq->name, - pReq->type == TSDB_CHILD_TABLE ? "child table" : "normal table", tstrerror(terrno)); - return TSDB_CODE_FAILED; #endif } @@ -368,7 +339,6 @@ static int32_t metaCreateNormalTable(SMeta *pMeta, int64_t version, SVCreateTbRe TAOS_RETURN(code); } - // handle entry SMetaEntry entry = { .version = version, .type = TSDB_NORMAL_TABLE, @@ -391,6 +361,7 @@ static int32_t metaCreateNormalTable(SMeta *pMeta, int64_t version, SVCreateTbRe tstrerror(code)); } + // handle entry code = metaHandleEntry2(pMeta, &entry); if (TSDB_CODE_SUCCESS == code) { metaInfo("vgId:%d, normal table:%s uid %" PRId64 " is created, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name, @@ -400,19 +371,103 @@ static int32_t metaCreateNormalTable(SMeta *pMeta, int64_t version, SVCreateTbRe __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version); } TAOS_RETURN(code); +#if 0 + metaTimeSeriesNotifyCheck(pMeta); +#endif } // Drop Normal Table // Alter Normal Table +static int32_t metaCheckDropTableReq(SMeta *pMeta, int64_t version, SVDropTbReq *pReq) { + int32_t code = TSDB_CODE_SUCCESS; + void *value = NULL; + int32_t valueSize = 0; + SMetaInfo info; + + if (NULL == pReq->name || strlen(pReq->name) == 0) { + code = TSDB_CODE_INVALID_MSG; + metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, + __FILE__, __LINE__, pReq->name, version); + return code; + } + + code = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize); + if (TSDB_CODE_SUCCESS != code) { + if (pReq->igNotExists) { + metaTrace("vgId:%d, %s success since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, + pReq->name, version); + } else { + metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), + __func__, __FILE__, __LINE__, pReq->name, version); + } + return TSDB_CODE_TDB_TABLE_NOT_EXIST; + } + pReq->uid = *(tb_uid_t *)value; + tdbFreeClear(value); + + code = metaGetInfo(pMeta, pReq->uid, &info, NULL); + if (TSDB_CODE_SUCCESS != code) { + metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 + " not found, this is an internal error, version:%" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pReq->uid, version); + code = TSDB_CODE_INTERNAL_ERROR; + return code; + } + pReq->suid = info.suid; + + return code; +} + +int32_t metaDropTable2(SMeta *pMeta, int64_t version, SVDropTbReq *pReq, SArray *tbUids, tb_uid_t *tbUid) { + int32_t code = TSDB_CODE_SUCCESS; + + // check request + code = metaCheckDropTableReq(pMeta, version, pReq); + if (code) { + if (TSDB_CODE_TDB_TABLE_NOT_EXIST != code) { + metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__, + __FILE__, __LINE__, tstrerror(code), version, pReq->name); + } + TAOS_RETURN(code); + } + + if (pReq->suid == pReq->uid) { + code = TSDB_CODE_INVALID_MSG; + metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode), + __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version); + TAOS_RETURN(code); + } + + SMetaEntry entry = { + .version = version, + .uid = pReq->uid, + }; + + if (pReq->suid == 0) { + entry.type = -TSDB_NORMAL_TABLE; + } else { + entry.type = -TSDB_CHILD_TABLE; + } + code = metaHandleEntry2(pMeta, &entry); + if (code) { + metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode), + __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version); + } else { + metaInfo("vgId:%d, table %s uid %" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name, + pReq->uid, version); + } + + TAOS_RETURN(code); +} + int32_t metaCreateTable2(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) { int32_t code = TSDB_CODE_SUCCESS; if (TSDB_CHILD_TABLE == pReq->type) { - // TODO code = metaCreateTable(pMeta, version, pReq, ppRsp); } else if (TSDB_NORMAL_TABLE == pReq->type) { - code = metaCreateTable(pMeta, version, pReq, ppRsp); + code = metaCreateNormalTable(pMeta, version, pReq, ppRsp); } else { code = TSDB_CODE_INVALID_MSG; } diff --git a/source/libs/tdb/inc/tdb.h b/source/libs/tdb/inc/tdb.h index 52ff749191..01d5daf9f5 100644 --- a/source/libs/tdb/inc/tdb.h +++ b/source/libs/tdb/inc/tdb.h @@ -88,6 +88,11 @@ void tdbTxnCloseImpl(TXN *pTxn); // other void tdbFree(void *); +#define tdbFreeClear(p) \ + do { \ + tdbFree(p); \ + p = NULL; \ + } while (0) typedef struct hashset_st *hashset_t;