diff --git a/include/libs/executor/storageapi.h b/include/libs/executor/storageapi.h index 6721d72996..a2be706dc0 100644 --- a/include/libs/executor/storageapi.h +++ b/include/libs/executor/storageapi.h @@ -62,7 +62,7 @@ typedef struct SMetaEntry { struct { int64_t btime; int32_t ttlDays; - int32_t commentLen; + int32_t commentLen; // not include '\0' char* comment; tb_uid_t suid; uint8_t* pTags; diff --git a/include/libs/wal/wal.h b/include/libs/wal/wal.h index 999adc2eff..68f1de643d 100644 --- a/include/libs/wal/wal.h +++ b/include/libs/wal/wal.h @@ -36,6 +36,7 @@ extern "C" { #define WAL_FILE_LEN (WAL_PATH_LEN + 32) #define WAL_MAGIC 0xFAFBFCFDF4F3F2F1ULL #define WAL_SCAN_BUF_SIZE (1024 * 1024 * 3) +#define WAL_JSON_BUF_SIZE 30 typedef enum { TAOS_WAL_SKIP = 0, diff --git a/include/util/tenv.h b/include/util/tenv.h index 1410e23f90..266debfcbb 100644 --- a/include/util/tenv.h +++ b/include/util/tenv.h @@ -24,7 +24,7 @@ extern "C" { #endif int32_t taosEnvNameToCfgName(const char *envNameStr, char *cfgNameStr, int32_t cfgNameMaxLen); -int32_t taosEnvToCfg(const char *envStr, char *cfgStr); +int32_t taosEnvToCfg(const char *envStr, char *cfgStr,int32_t cfgStrLen); #ifdef __cplusplus } diff --git a/include/util/tutil.h b/include/util/tutil.h index 01977e27b6..32fc9f215a 100644 --- a/include/util/tutil.h +++ b/include/util/tutil.h @@ -122,10 +122,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/common/src/tglobal.c b/source/common/src/tglobal.c index 8529c5b690..59ed0e386d 100644 --- a/source/common/src/tglobal.c +++ b/source/common/src/tglobal.c @@ -14,12 +14,12 @@ */ #define _DEFAULT_SOURCE -#include "tglobal.h" #include "cJSON.h" #include "defines.h" #include "os.h" #include "osString.h" #include "tconfig.h" +#include "tglobal.h" #include "tgrant.h" #include "tjson.h" #include "tlog.h" @@ -359,6 +359,8 @@ int32_t tsMaxTsmaCalcDelay = 600; int64_t tsmaDataDeleteMark = 1000 * 60 * 60 * 24; // in ms, default to 1d void *pTimezoneNameMap = NULL; +int32_t taosCheckCfgStrValueLen(const char *name, const char *value, int32_t len); + #define TAOS_CHECK_GET_CFG_ITEM(pCfg, pItem, pName) \ if ((pItem = cfgGetItem(pCfg, pName)) == NULL) { \ TAOS_RETURN(TSDB_CODE_CFG_NOT_FOUND); \ @@ -449,6 +451,7 @@ int32_t taosSetS3Cfg(SConfig *pCfg) { SConfigItem *pItem = NULL; TAOS_CHECK_GET_CFG_ITEM(pCfg, pItem, "s3BucketName"); + TAOS_CHECK_RETURN(taosCheckCfgStrValueLen("s3BucketName", pItem->str, TSDB_FQDN_LEN)); tstrncpy(tsS3BucketName, pItem->str, TSDB_FQDN_LEN); for (int i = 0; i < tsS3EpNum; ++i) { @@ -1153,6 +1156,7 @@ static int32_t taosSetClientLogCfg(SConfig *pCfg) { SConfigItem *pItem = NULL; TAOS_CHECK_GET_CFG_ITEM(pCfg, pItem, "logDir"); + TAOS_CHECK_RETURN(taosCheckCfgStrValueLen(pItem->name, pItem->str, PATH_MAX)); tstrncpy(tsLogDir, pItem->str, PATH_MAX); TAOS_CHECK_RETURN(taosExpandDir(tsLogDir, tsLogDir, PATH_MAX)); TAOS_CHECK_RETURN(taosSetLogOutput(pCfg)); @@ -1303,6 +1307,7 @@ static int32_t taosSetClientCfg(SConfig *pCfg) { SConfigItem *pItem = NULL; TAOS_CHECK_GET_CFG_ITEM(pCfg, pItem, "fqdn"); + TAOS_CHECK_RETURN(taosCheckCfgStrValueLen(pItem->name, pItem->str, TSDB_FQDN_LEN)); tstrncpy(tsLocalFqdn, pItem->str, TSDB_FQDN_LEN); TAOS_CHECK_GET_CFG_ITEM(pCfg, pItem, "forceReadConfig"); @@ -1328,6 +1333,7 @@ static int32_t taosSetClientCfg(SConfig *pCfg) { TAOS_CHECK_RETURN(cfgSetItem(pCfg, "secondEp", tsSecond, pItem->stype, true)); TAOS_CHECK_GET_CFG_ITEM(pCfg, pItem, "tempDir"); + TAOS_CHECK_RETURN(taosCheckCfgStrValueLen(pItem->name, pItem->str, PATH_MAX)); tstrncpy(tsTempDir, pItem->str, PATH_MAX); TAOS_CHECK_RETURN(taosExpandDir(tsTempDir, tsTempDir, PATH_MAX)); @@ -1340,15 +1346,19 @@ static int32_t taosSetClientCfg(SConfig *pCfg) { } TAOS_CHECK_GET_CFG_ITEM(pCfg, pItem, "smlAutoChildTableNameDelimiter"); + TAOS_CHECK_RETURN(taosCheckCfgStrValueLen(pItem->name, pItem->str, TSDB_TABLE_NAME_LEN)); tstrncpy(tsSmlAutoChildTableNameDelimiter, pItem->str, TSDB_TABLE_NAME_LEN); TAOS_CHECK_GET_CFG_ITEM(pCfg, pItem, "smlChildTableName"); + TAOS_CHECK_RETURN(taosCheckCfgStrValueLen(pItem->name, pItem->str, TSDB_TABLE_NAME_LEN)); tstrncpy(tsSmlChildTableName, pItem->str, TSDB_TABLE_NAME_LEN); TAOS_CHECK_GET_CFG_ITEM(pCfg, pItem, "smlTagName"); + TAOS_CHECK_RETURN(taosCheckCfgStrValueLen(pItem->name, pItem->str, TSDB_COL_NAME_LEN)); tstrncpy(tsSmlTagName, pItem->str, TSDB_COL_NAME_LEN); TAOS_CHECK_GET_CFG_ITEM(pCfg, pItem, "smlTsDefaultName"); + TAOS_CHECK_RETURN(taosCheckCfgStrValueLen(pItem->name, pItem->str, TSDB_COL_NAME_LEN)); tstrncpy(tsSmlTsDefaultName, pItem->str, TSDB_COL_NAME_LEN); TAOS_CHECK_GET_CFG_ITEM(pCfg, pItem, "smlDot2Underline"); @@ -1495,9 +1505,11 @@ static int32_t taosSetServerCfg(SConfig *pCfg) { tsQueryBufferSize = pItem->i32; TAOS_CHECK_GET_CFG_ITEM(pCfg, pItem, "encryptAlgorithm"); + TAOS_CHECK_RETURN(taosCheckCfgStrValueLen(pItem->name, pItem->str, 16)); tstrncpy(tsEncryptAlgorithm, pItem->str, 16); TAOS_CHECK_GET_CFG_ITEM(pCfg, pItem, "encryptScope"); + TAOS_CHECK_RETURN(taosCheckCfgStrValueLen(pItem->name, pItem->str, 100)); tstrncpy(tsEncryptScope, pItem->str, 100); TAOS_CHECK_GET_CFG_ITEM(pCfg, pItem, "numOfRpcThreads"); @@ -1561,6 +1573,7 @@ static int32_t taosSetServerCfg(SConfig *pCfg) { tsTagFilterCache = (bool)pItem->bval; TAOS_CHECK_GET_CFG_ITEM(pCfg, pItem, "slowLogExceptDb"); + TAOS_CHECK_RETURN(taosCheckCfgStrValueLen(pItem->name, pItem->str, TSDB_DB_NAME_LEN)); tstrncpy(tsSlowLogExceptDb, pItem->str, TSDB_DB_NAME_LEN); TAOS_CHECK_GET_CFG_ITEM(pCfg, pItem, "slowLogThreshold"); @@ -1581,6 +1594,7 @@ static int32_t taosSetServerCfg(SConfig *pCfg) { tsMonitorInterval = pItem->i32; TAOS_CHECK_GET_CFG_ITEM(pCfg, pItem, "monitorFqdn"); + TAOS_CHECK_RETURN(taosCheckCfgStrValueLen(pItem->name, pItem->str, TSDB_FQDN_LEN)); tstrncpy(tsMonitorFqdn, pItem->str, TSDB_FQDN_LEN); TAOS_CHECK_GET_CFG_ITEM(pCfg, pItem, "monitorPort"); @@ -1632,12 +1646,15 @@ static int32_t taosSetServerCfg(SConfig *pCfg) { tsRsyncPort = pItem->i32; TAOS_CHECK_GET_CFG_ITEM(pCfg, pItem, "telemetryServer"); + TAOS_CHECK_RETURN(taosCheckCfgStrValueLen(pItem->name, pItem->str, TSDB_FQDN_LEN)); tstrncpy(tsTelemServer, pItem->str, TSDB_FQDN_LEN); TAOS_CHECK_GET_CFG_ITEM(pCfg, pItem, "snodeAddress"); + TAOS_CHECK_RETURN(taosCheckCfgStrValueLen(pItem->name, pItem->str, TSDB_FQDN_LEN)); tstrncpy(tsSnodeAddress, pItem->str, TSDB_FQDN_LEN); TAOS_CHECK_GET_CFG_ITEM(pCfg, pItem, "checkpointBackupDir"); + TAOS_CHECK_RETURN(taosCheckCfgStrValueLen(pItem->name, pItem->str, PATH_MAX)); tstrncpy(tsCheckpointBackupDir, pItem->str, PATH_MAX); TAOS_CHECK_GET_CFG_ITEM(pCfg, pItem, "telemetryPort"); @@ -1728,9 +1745,11 @@ static int32_t taosSetServerCfg(SConfig *pCfg) { tsStartUdfd = pItem->bval; TAOS_CHECK_GET_CFG_ITEM(pCfg, pItem, "udfdResFuncs"); + TAOS_CHECK_RETURN(taosCheckCfgStrValueLen(pItem->name, pItem->str, sizeof(tsUdfdResFuncs))); tstrncpy(tsUdfdResFuncs, pItem->str, sizeof(tsUdfdResFuncs)); TAOS_CHECK_GET_CFG_ITEM(pCfg, pItem, "udfdLdLibPath"); + TAOS_CHECK_RETURN(taosCheckCfgStrValueLen(pItem->name, pItem->str, sizeof(tsUdfdLdLibPath))); tstrncpy(tsUdfdLdLibPath, pItem->str, sizeof(tsUdfdLdLibPath)); if (tsQueryBufferSize >= 0) { tsQueryBufferSizeBytes = tsQueryBufferSize * 1048576UL; @@ -1755,6 +1774,7 @@ static int32_t taosSetServerCfg(SConfig *pCfg) { tsIfAdtFse = pItem->bval; TAOS_CHECK_GET_CFG_ITEM(pCfg, pItem, "compressor"); + TAOS_CHECK_RETURN(taosCheckCfgStrValueLen(pItem->name, pItem->str, sizeof(tsCompressor))); tstrncpy(tsCompressor, pItem->str, sizeof(tsCompressor)); TAOS_CHECK_GET_CFG_ITEM(pCfg, pItem, "disableStream"); @@ -2295,9 +2315,15 @@ static int32_t taosCfgSetOption(OptionNameAndVar *pOptions, int32_t optionSize, case CFG_DTYPE_LOCALE: case CFG_DTYPE_CHARSET: case CFG_DTYPE_TIMEZONE: { - char *pVar = pOptions[d].optionVar; - tstrncpy(pVar, pItem->str, strlen(pItem->str)); - uInfo("%s set to %s", optName, pVar); + if (strcasecmp(pItem->name, "slowLogExceptDb") == 0) { + TAOS_CHECK_RETURN(taosCheckCfgStrValueLen(pItem->name, pItem->str, TSDB_DB_NAME_LEN)); + tstrncpy(tsSlowLogExceptDb, pItem->str, TSDB_DB_NAME_LEN); + } else { + uError("not support string type for %s", optName); + code = TSDB_CODE_INVALID_CFG; + break; + } + uInfo("%s set to %s", optName, pItem->str); } break; default: code = TSDB_CODE_INVALID_CFG; @@ -2439,9 +2465,6 @@ static int32_t taosCfgDynamicOptionsForServer(SConfig *pCfg, const char *name) { {"ttlFlushThreshold", &tsTtlFlushThreshold}, {"ttlPushInterval", &tsTtlPushIntervalSec}, {"ttlUnit", &tsTtlUnit}, - {"s3Accesskey", &tsS3AccessKey}, - {"s3BucketName", &tsS3BucketName}, - {"s3Endpoint", &tsS3Endpoint}, {"s3MigrateIntervalSec", &tsS3MigrateIntervalSec}, {"s3MigrateEnabled", &tsS3MigrateEnabled}, //{"s3BlockSize", &tsS3BlockSize}, @@ -2610,18 +2633,22 @@ static int32_t taosCfgDynamicOptionsForClient(SConfig *pCfg, const char *name) { uInfo("%s set to %s", name, tsSecond); matched = true; } else if (strcasecmp("smlChildTableName", name) == 0) { + TAOS_CHECK_GOTO(taosCheckCfgStrValueLen(pItem->name, pItem->str, TSDB_TABLE_NAME_LEN), &lino, _out); uInfo("%s set from %s to %s", name, tsSmlChildTableName, pItem->str); tstrncpy(tsSmlChildTableName, pItem->str, TSDB_TABLE_NAME_LEN); matched = true; } else if (strcasecmp("smlAutoChildTableNameDelimiter", name) == 0) { + TAOS_CHECK_GOTO(taosCheckCfgStrValueLen(pItem->name, pItem->str, TSDB_TABLE_NAME_LEN), &lino, _out); uInfo("%s set from %s to %s", name, tsSmlAutoChildTableNameDelimiter, pItem->str); tstrncpy(tsSmlAutoChildTableNameDelimiter, pItem->str, TSDB_TABLE_NAME_LEN); matched = true; } else if (strcasecmp("smlTagName", name) == 0) { + TAOS_CHECK_GOTO(taosCheckCfgStrValueLen(pItem->name, pItem->str, TSDB_COL_NAME_LEN), &lino, _out); uInfo("%s set from %s to %s", name, tsSmlTagName, pItem->str); tstrncpy(tsSmlTagName, pItem->str, TSDB_COL_NAME_LEN); matched = true; } else if (strcasecmp("smlTsDefaultName", name) == 0) { + TAOS_CHECK_GOTO(taosCheckCfgStrValueLen(pItem->name, pItem->str, TSDB_COL_NAME_LEN), &lino, _out); uInfo("%s set from %s to %s", name, tsSmlTsDefaultName, pItem->str); tstrncpy(tsSmlTsDefaultName, pItem->str, TSDB_COL_NAME_LEN); matched = true; @@ -2635,6 +2662,7 @@ static int32_t taosCfgDynamicOptionsForClient(SConfig *pCfg, const char *name) { goto _out; } + TAOS_CHECK_GOTO(taosCheckCfgStrValueLen(pFqdnItem->name, pFqdnItem->str, TSDB_FQDN_LEN), &lino, _out); tstrncpy(tsLocalFqdn, pFqdnItem->str, TSDB_FQDN_LEN); tsServerPort = (uint16_t)pServerPortItem->i32; (void)snprintf(tsLocalEp, sizeof(tsLocalEp), "%s:%u", tsLocalFqdn, tsServerPort); @@ -2656,6 +2684,7 @@ static int32_t taosCfgDynamicOptionsForClient(SConfig *pCfg, const char *name) { } case 't': { if (strcasecmp("tempDir", name) == 0) { + TAOS_CHECK_GOTO(taosCheckCfgStrValueLen(pItem->name, pItem->str, PATH_MAX), &lino, _out); uInfo("%s set from %s to %s", name, tsTempDir, pItem->str); tstrncpy(tsTempDir, pItem->str, PATH_MAX); TAOS_CHECK_GOTO(taosExpandDir(tsTempDir, tsTempDir, PATH_MAX), &lino, _out); @@ -3222,4 +3251,12 @@ bool isConifgItemLazyMode(SConfigItem *item) { return true; } return false; +} + +int32_t taosCheckCfgStrValueLen(const char *name, const char *value, int32_t len) { + if (strlen(value) > len) { + uError("invalid config:%s, value:%s, length should be less than %d", name, value, len); + TAOS_RETURN(TSDB_CODE_INVALID_CFG_VALUE); + } + TAOS_RETURN(TSDB_CODE_SUCCESS); } \ No newline at end of file diff --git a/source/dnode/mnode/impl/inc/mndDnode.h b/source/dnode/mnode/impl/inc/mndDnode.h index 7708b954b7..b0b5c13fdd 100644 --- a/source/dnode/mnode/impl/inc/mndDnode.h +++ b/source/dnode/mnode/impl/inc/mndDnode.h @@ -31,6 +31,7 @@ SEpSet mndGetDnodeEpsetById(SMnode *pMnode, int32_t dnodeId); int32_t mndGetDnodeSize(SMnode *pMnode); bool mndIsDnodeOnline(SDnodeObj *pDnode, int64_t curMs); int32_t mndGetDnodeData(SMnode *pMnode, SArray *pDnodeInfo); +void getSlowLogScopeString(int32_t scope, char *result); #ifdef __cplusplus } diff --git a/source/dnode/mnode/impl/inc/mndStream.h b/source/dnode/mnode/impl/inc/mndStream.h index 27d3e3c9be..ec04aa3111 100644 --- a/source/dnode/mnode/impl/inc/mndStream.h +++ b/source/dnode/mnode/impl/inc/mndStream.h @@ -23,8 +23,9 @@ extern "C" { #endif -#define MND_STREAM_RESERVE_SIZE 64 -#define MND_STREAM_VER_NUMBER 5 +#define MND_STREAM_RESERVE_SIZE 64 +#define MND_STREAM_VER_NUMBER 5 +#define MND_STREAM_TRIGGER_NAME_SIZE 20 #define MND_STREAM_CREATE_NAME "stream-create" #define MND_STREAM_CHECKPOINT_NAME "stream-checkpoint" @@ -60,7 +61,7 @@ typedef struct SStreamTaskResetMsg { } SStreamTaskResetMsg; typedef struct SChkptReportInfo { - SArray* pTaskList; + SArray *pTaskList; int64_t reportChkpt; int64_t streamId; } SChkptReportInfo; @@ -106,7 +107,7 @@ typedef struct STaskChkptInfo { int64_t ts; int32_t transId; int8_t dropHTask; -}STaskChkptInfo; +} STaskChkptInfo; int32_t mndInitStream(SMnode *pMnode); void mndCleanupStream(SMnode *pMnode); @@ -121,7 +122,7 @@ int32_t mndStreamGetRelTrans(SMnode *pMnode, int64_t streamId); int32_t mndGetNumOfStreams(SMnode *pMnode, char *dbName, int32_t *pNumOfStreams); int32_t mndGetNumOfStreamTasks(const SStreamObj *pStream); -int32_t mndTakeVgroupSnapshot(SMnode *pMnode, bool *allReady, SArray** pList); +int32_t mndTakeVgroupSnapshot(SMnode *pMnode, bool *allReady, SArray **pList); void mndDestroyVgroupChangeInfo(SVgroupChangeInfo *pInfo); void mndKillTransImpl(SMnode *pMnode, int32_t transId, const char *pDbName); int32_t setTransAction(STrans *pTrans, void *pCont, int32_t contLen, int32_t msgType, const SEpSet *pEpset, @@ -132,7 +133,7 @@ int32_t mndPersistTransLog(SStreamObj *pStream, STrans *pTrans, int32_t status) SSdbRaw *mndStreamActionEncode(SStreamObj *pStream); int32_t mndStreamSetUpdateEpsetAction(SMnode *pMnode, SStreamObj *pStream, SVgroupChangeInfo *pInfo, STrans *pTrans); -int32_t mndGetStreamObj(SMnode *pMnode, int64_t streamId, SStreamObj** pStream); +int32_t mndGetStreamObj(SMnode *pMnode, int64_t streamId, SStreamObj **pStream); bool mndStreamNodeIsUpdated(SMnode *pMnode); int32_t mndCheckForSnode(SMnode *pMnode, SDbObj *pSrcDb); @@ -146,8 +147,8 @@ int32_t mndStreamSetDropActionFromList(SMnode *pMnode, STrans *pTrans, SArray *p int32_t mndStreamSetResetTaskAction(SMnode *pMnode, STrans *pTrans, SStreamObj *pStream, int64_t chkptId); int32_t mndStreamSetUpdateChkptAction(SMnode *pMnode, STrans *pTrans, SStreamObj *pStream); int32_t mndCreateStreamResetStatusTrans(SMnode *pMnode, SStreamObj *pStream, int64_t chkptId); -int32_t mndStreamSetChkptIdAction(SMnode *pMnode, STrans *pTrans, SStreamTask* pTask, int64_t checkpointId, int64_t ts); -int32_t mndStreamSetRestartAction(SMnode* pMnode, STrans *pTrans, SStreamObj* pStream); +int32_t mndStreamSetChkptIdAction(SMnode *pMnode, STrans *pTrans, SStreamTask *pTask, int64_t checkpointId, int64_t ts); +int32_t mndStreamSetRestartAction(SMnode *pMnode, STrans *pTrans, SStreamObj *pStream); int32_t mndStreamSetCheckpointAction(SMnode *pMnode, STrans *pTrans, SStreamTask *pTask, int64_t checkpointId, int8_t mndTrigger); int32_t mndCreateStreamChkptInfoUpdateTrans(SMnode *pMnode, SStreamObj *pStream, SArray *pChkptInfoList); @@ -174,9 +175,9 @@ void removeStreamTasksInBuf(SStreamObj *pStream, SStreamExecInfo *pExecNode); int32_t mndGetConsensusInfo(SHashObj *pHash, int64_t streamId, int32_t numOfTasks, SCheckpointConsensusInfo **pInfo); void mndAddConsensusTasks(SCheckpointConsensusInfo *pInfo, const SRestoreCheckpointInfo *pRestoreInfo); void mndClearConsensusRspEntry(SCheckpointConsensusInfo *pInfo); -int64_t mndClearConsensusCheckpointId(SHashObj* pHash, int64_t streamId); -int64_t mndClearChkptReportInfo(SHashObj* pHash, int64_t streamId); -int32_t mndResetChkptReportInfo(SHashObj* pHash, int64_t streamId); +int64_t mndClearConsensusCheckpointId(SHashObj *pHash, int64_t streamId); +int64_t mndClearChkptReportInfo(SHashObj *pHash, int64_t streamId); +int32_t mndResetChkptReportInfo(SHashObj *pHash, int64_t streamId); int32_t setStreamAttrInResBlock(SStreamObj *pStream, SSDataBlock *pBlock, int32_t numOfRows); int32_t setTaskAttrInResBlock(SStreamObj *pStream, SStreamTask *pTask, SSDataBlock *pBlock, int32_t nRows, int32_t p); diff --git a/source/dnode/mnode/impl/src/mndCluster.c b/source/dnode/mnode/impl/src/mndCluster.c index 50bba755c9..cbc8c0e298 100644 --- a/source/dnode/mnode/impl/src/mndCluster.c +++ b/source/dnode/mnode/impl/src/mndCluster.c @@ -243,7 +243,7 @@ static int32_t mndCreateDefaultCluster(SMnode *pMnode) { int32_t code = taosGetSystemUUIDLen(clusterObj.name, TSDB_CLUSTER_ID_LEN); if (code != 0) { - (void)strcpy(clusterObj.name, "tdengine3.0"); + tstrncpy(clusterObj.name, "tdengine3.0", sizeof(clusterObj.name)); mError("failed to get name from system, set to default val %s", clusterObj.name); } diff --git a/source/dnode/mnode/impl/src/mndCompact.c b/source/dnode/mnode/impl/src/mndCompact.c index 402c4321a1..e8f7202986 100644 --- a/source/dnode/mnode/impl/src/mndCompact.c +++ b/source/dnode/mnode/impl/src/mndCompact.c @@ -12,8 +12,8 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -#include "mndCompact.h" #include "audit.h" +#include "mndCompact.h" #include "mndCompactDetail.h" #include "mndDb.h" #include "mndDnode.h" @@ -254,7 +254,7 @@ int32_t mndAddCompactToTran(SMnode *pMnode, STrans *pTrans, SCompactObj *pCompac int32_t code = 0; pCompact->compactId = tGenIdPI32(); - (void)strcpy(pCompact->dbname, pDb->name); + tstrncpy(pCompact->dbname, pDb->name, sizeof(pCompact->dbname)); pCompact->startTime = taosGetTimestampMs(); diff --git a/source/dnode/mnode/impl/src/mndConfig.c b/source/dnode/mnode/impl/src/mndConfig.c index 06555d47a6..0247a1c88c 100644 --- a/source/dnode/mnode/impl/src/mndConfig.c +++ b/source/dnode/mnode/impl/src/mndConfig.c @@ -418,17 +418,17 @@ static int32_t mndMCfg2DCfg(SMCfgDnodeReq *pMCfgReq, SDCfgDnodeReq *pDCfgReq) { } size_t optLen = p - pMCfgReq->config; - strncpy(pDCfgReq->config, pMCfgReq->config, optLen); + tstrncpy(pDCfgReq->config, pMCfgReq->config, sizeof(pDCfgReq->config)); pDCfgReq->config[optLen] = 0; if (' ' == pMCfgReq->config[optLen]) { // 'key value' if (strlen(pMCfgReq->value) != 0) goto _err; - (void)strcpy(pDCfgReq->value, p + 1); + tstrncpy(pDCfgReq->value, p + 1, sizeof(pDCfgReq->value)); } else { // 'key' 'value' if (strlen(pMCfgReq->value) == 0) goto _err; - (void)strcpy(pDCfgReq->value, pMCfgReq->value); + tstrncpy(pDCfgReq->value, pMCfgReq->value, sizeof(pDCfgReq->value)); } TAOS_RETURN(code); @@ -576,7 +576,7 @@ _send_req : { // audit char obj[50] = {0}; - (void)sprintf(obj, "%d", cfgReq.dnodeId); + (void)tsnprintf(obj, sizeof(obj), "%d", cfgReq.dnodeId); auditRecord(pReq, pMnode->clusterId, "alterDnode", obj, "", cfgReq.sql, cfgReq.sqlLen); } @@ -785,59 +785,59 @@ SArray *initVariablesFromItems(SArray *pItems) { for (int32_t i = 0; i < sz; ++i) { SConfigItem *pItem = taosArrayGet(pItems, i); SVariablesInfo info = {0}; - strcpy(info.name, pItem->name); + tstrncpy(info.name, pItem->name, sizeof(info.name)); // init info value switch (pItem->dtype) { case CFG_DTYPE_NONE: break; case CFG_DTYPE_BOOL: - sprintf(info.value, "%d", pItem->bval); + tsnprintf(info.value, sizeof(info.value), "%d", pItem->bval); break; case CFG_DTYPE_INT32: - sprintf(info.value, "%d", pItem->i32); + tsnprintf(info.value, sizeof(info.value), "%d", pItem->i32); break; case CFG_DTYPE_INT64: - sprintf(info.value, "%" PRId64, pItem->i64); + tsnprintf(info.value, sizeof(info.value), "%" PRId64, pItem->i64); break; case CFG_DTYPE_FLOAT: case CFG_DTYPE_DOUBLE: - sprintf(info.value, "%f", pItem->fval); + tsnprintf(info.value, sizeof(info.value), "%f", pItem->fval); break; case CFG_DTYPE_STRING: case CFG_DTYPE_DIR: case CFG_DTYPE_LOCALE: case CFG_DTYPE_CHARSET: case CFG_DTYPE_TIMEZONE: - sprintf(info.value, "%s", pItem->str); + tsnprintf(info.value, sizeof(info.value), "%s", pItem->str); break; } // init info scope switch (pItem->scope) { case CFG_SCOPE_SERVER: - strcpy(info.scope, "server"); + tstrncpy(info.scope, "server", sizeof(info.scope)); break; case CFG_SCOPE_CLIENT: - strcpy(info.scope, "client"); + tstrncpy(info.scope, "client", sizeof(info.scope)); break; case CFG_SCOPE_BOTH: - strcpy(info.scope, "both"); + tstrncpy(info.scope, "both", sizeof(info.scope)); break; default: - strcpy(info.scope, "unknown"); + tstrncpy(info.scope, "unknown", sizeof(info.scope)); break; } // init info category switch (pItem->category) { case CFG_CATEGORY_GLOBAL: - strcpy(info.category, "global"); + tstrncpy(info.category, "global", sizeof(info.category)); break; case CFG_CATEGORY_LOCAL: - strcpy(info.category, "local"); + tstrncpy(info.category, "local", sizeof(info.category)); break; default: - strcpy(info.category, "unknown"); + tstrncpy(info.category, "unknown", sizeof(info.category)); break; } if (NULL == taosArrayPush(pInfos, &info)) { diff --git a/source/dnode/mnode/impl/src/mndDb.c b/source/dnode/mnode/impl/src/mndDb.c index 9d02fdf115..c28fd343f0 100644 --- a/source/dnode/mnode/impl/src/mndDb.c +++ b/source/dnode/mnode/impl/src/mndDb.c @@ -889,22 +889,6 @@ _OVER: TAOS_RETURN(code); } -static void mndBuildAuditDetailInt32(char *detail, char *tmp, char *format, int32_t para) { - if (para > 0) { - if (strlen(detail) > 0) (void)strcat(detail, ", "); - (void)sprintf(tmp, format, para); - (void)strcat(detail, tmp); - } -} - -static void mndBuildAuditDetailInt64(char *detail, char *tmp, char *format, int64_t para) { - if (para > 0) { - if (strlen(detail) > 0) (void)strcat(detail, ", "); - (void)sprintf(tmp, format, para); - (void)strcat(detail, tmp); - } -} - static int32_t mndCheckDbEncryptKey(SMnode *pMnode, SCreateDbReq *pReq) { int32_t code = 0; SSdb *pSdb = pMnode->pSdb; @@ -1381,7 +1365,7 @@ _OVER: } static void mndDumpDbCfgInfo(SDbCfgRsp *cfgRsp, SDbObj *pDb) { - (void)strcpy(cfgRsp->db, pDb->name); + tstrncpy(cfgRsp->db, pDb->name, sizeof(cfgRsp->db)); cfgRsp->dbId = pDb->uid; cfgRsp->cfgVersion = pDb->cfgVersion; cfgRsp->numOfVgroups = pDb->cfg.numOfVgroups; @@ -2280,24 +2264,24 @@ static char *buildRetension(SArray *pRetension) { int64_t v1 = getValOfDiffPrecision(p->freqUnit, p->freq); int64_t v2 = getValOfDiffPrecision(p->keepUnit, p->keep); - len += sprintf(p1 + len, "%" PRId64 "%c:%" PRId64 "%c", v1, p->freqUnit, v2, p->keepUnit); + len += tsnprintf(p1 + len, 100 - len, "%" PRId64 "%c:%" PRId64 "%c", v1, p->freqUnit, v2, p->keepUnit); if (size > 1) { - len += sprintf(p1 + len, ","); + len += tsnprintf(p1 + len, 100 - len, ","); p = taosArrayGet(pRetension, 1); v1 = getValOfDiffPrecision(p->freqUnit, p->freq); v2 = getValOfDiffPrecision(p->keepUnit, p->keep); - len += sprintf(p1 + len, "%" PRId64 "%c:%" PRId64 "%c", v1, p->freqUnit, v2, p->keepUnit); + len += tsnprintf(p1 + len, 100 - len, "%" PRId64 "%c:%" PRId64 "%c", v1, p->freqUnit, v2, p->keepUnit); } if (size > 2) { - len += sprintf(p1 + len, ","); + len += tsnprintf(p1 + len, 100 - len, ","); p = taosArrayGet(pRetension, 2); v1 = getValOfDiffPrecision(p->freqUnit, p->freq); v2 = getValOfDiffPrecision(p->keepUnit, p->keep); - len += sprintf(p1 + len, "%" PRId64 "%c:%" PRId64 "%c", v1, p->freqUnit, v2, p->keepUnit); + len += tsnprintf(p1 + len, 100 - len, "%" PRId64 "%c:%" PRId64 "%c", v1, p->freqUnit, v2, p->keepUnit); } varDataSetLen(p1, len); @@ -2466,9 +2450,9 @@ static void mndDumpDbInfoData(SMnode *pMnode, SSDataBlock *pBlock, SDbObj *pDb, int32_t lenKeep2 = formatDurationOrKeep(keep2Str, sizeof(keep2Str), pDb->cfg.daysToKeep2); if (pDb->cfg.daysToKeep0 > pDb->cfg.daysToKeep1 || pDb->cfg.daysToKeep0 > pDb->cfg.daysToKeep2) { - len = sprintf(&keepVstr[VARSTR_HEADER_SIZE], "%s,%s,%s", keep1Str, keep2Str, keep0Str); + len = tsnprintf(&keepVstr[VARSTR_HEADER_SIZE], sizeof(keepVstr), "%s,%s,%s", keep1Str, keep2Str, keep0Str); } else { - len = sprintf(&keepVstr[VARSTR_HEADER_SIZE], "%s,%s,%s", keep0Str, keep1Str, keep2Str); + len = tsnprintf(&keepVstr[VARSTR_HEADER_SIZE], sizeof(keepVstr), "%s,%s,%s", keep0Str, keep1Str, keep2Str); } varDataSetLen(keepVstr, len); pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); @@ -2556,7 +2540,7 @@ static void mndDumpDbInfoData(SMnode *pMnode, SSDataBlock *pBlock, SDbObj *pDb, TAOS_CHECK_GOTO(colDataSetVal(pColInfo, rows, (const char *)&pDb->cfg.s3ChunkSize, false), &lino, _OVER); char keeplocalVstr[128] = {0}; - len = sprintf(&keeplocalVstr[VARSTR_HEADER_SIZE], "%dm", pDb->cfg.s3KeepLocal); + len = tsnprintf(&keeplocalVstr[VARSTR_HEADER_SIZE], sizeof(keeplocalVstr), "%dm", pDb->cfg.s3KeepLocal); varDataSetLen(keeplocalVstr, len); pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); TAOS_CHECK_GOTO(colDataSetVal(pColInfo, rows, (const char *)keeplocalVstr, false), &lino, _OVER); diff --git a/source/dnode/mnode/impl/src/mndDef.c b/source/dnode/mnode/impl/src/mndDef.c index 7ea0ba67bd..92ad4eb5b8 100644 --- a/source/dnode/mnode/impl/src/mndDef.c +++ b/source/dnode/mnode/impl/src/mndDef.c @@ -735,7 +735,7 @@ SConfigObj *mndInitConfigObj(SConfigItem *pItem) { if (pObj == NULL) { return NULL; } - strncpy(pObj->name, pItem->name, CFG_NAME_MAX_LEN); + tstrncpy(pObj->name, pItem->name, CFG_NAME_MAX_LEN); pObj->dtype = pItem->dtype; switch (pItem->dtype) { case CFG_DTYPE_NONE: @@ -776,7 +776,7 @@ int32_t mndUpdateObj(SConfigObj *pObjNew, const char *name, char *value) { if (strcasecmp(value, "true") == 0) { tmp = true; } - if (atoi(value) > 0) { + if (taosStr2Int32(value, NULL, 10) > 0) { tmp = true; } pObjNew->bval = tmp; diff --git a/source/dnode/mnode/impl/src/mndDnode.c b/source/dnode/mnode/impl/src/mndDnode.c index 409aa4c9c9..ca119191eb 100644 --- a/source/dnode/mnode/impl/src/mndDnode.c +++ b/source/dnode/mnode/impl/src/mndDnode.c @@ -14,11 +14,11 @@ */ #define _DEFAULT_SOURCE -#include "mndDnode.h" #include #include "audit.h" #include "mndCluster.h" #include "mndDb.h" +#include "mndDnode.h" #include "mndMnode.h" #include "mndPrivilege.h" #include "mndQnode.h" @@ -1051,20 +1051,20 @@ _OVER: TAOS_RETURN(code); } -static void getSlowLogScopeString(int32_t scope, char *result) { +void getSlowLogScopeString(int32_t scope, char *result) { if (scope == SLOW_LOG_TYPE_NULL) { - (void)strcat(result, "NONE"); + (void)strncat(result, "NONE", 64); return; } while (scope > 0) { if (scope & SLOW_LOG_TYPE_QUERY) { - (void)strcat(result, "QUERY"); + (void)strncat(result, "QUERY", 64); scope &= ~SLOW_LOG_TYPE_QUERY; } else if (scope & SLOW_LOG_TYPE_INSERT) { - (void)strcat(result, "INSERT"); + (void)strncat(result, "INSERT", 64); scope &= ~SLOW_LOG_TYPE_INSERT; } else if (scope & SLOW_LOG_TYPE_OTHERS) { - (void)strcat(result, "OTHERS"); + (void)strncat(result, "OTHERS", 64); scope &= ~SLOW_LOG_TYPE_OTHERS; } else { (void)printf("invalid slow log scope:%d", scope); @@ -1072,7 +1072,7 @@ static void getSlowLogScopeString(int32_t scope, char *result) { } if (scope > 0) { - (void)strcat(result, "|"); + (void)strncat(result, "|", 64); } } } @@ -1112,7 +1112,7 @@ static int32_t mndProcessCreateDnodeReq(SRpcMsg *pReq) { } char obj[200] = {0}; - (void)sprintf(obj, "%s:%d", createReq.fqdn, createReq.port); + (void)tsnprintf(obj, sizeof(obj), "%s:%d", createReq.fqdn, createReq.port); auditRecord(pReq, pMnode->clusterId, "createDnode", "", obj, createReq.sql, createReq.sqlLen); @@ -1296,7 +1296,7 @@ static int32_t mndProcessDropDnodeReq(SRpcMsg *pReq) { if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS; char obj1[30] = {0}; - (void)sprintf(obj1, "%d", dropReq.dnodeId); + (void)tsnprintf(obj1, sizeof(obj1), "%d", dropReq.dnodeId); auditRecord(pReq, pMnode->clusterId, "dropDnode", "", obj1, dropReq.sql, dropReq.sqlLen); @@ -1406,8 +1406,8 @@ static int32_t mndProcessCreateEncryptKeyReq(SRpcMsg *pReq) { const STraceId *trace = &pReq->info.traceId; SDCfgDnodeReq dcfgReq = {0}; if (strncasecmp(cfgReq.config, "encrypt_key", 12) == 0) { - strcpy(dcfgReq.config, cfgReq.config); - strcpy(dcfgReq.value, cfgReq.value); + tstrncpy(dcfgReq.config, cfgReq.config, sizeof(dcfgReq.config)); + tstrncpy(dcfgReq.value, cfgReq.value, sizeof(dcfgReq.value)); tFreeSMCfgDnodeReq(&cfgReq); return mndProcessCreateEncryptKeyReqImpl(pReq, cfgReq.dnodeId, &dcfgReq); } else { diff --git a/source/dnode/mnode/impl/src/mndFunc.c b/source/dnode/mnode/impl/src/mndFunc.c index db4d842662..1b0132ce9c 100644 --- a/source/dnode/mnode/impl/src/mndFunc.c +++ b/source/dnode/mnode/impl/src/mndFunc.c @@ -723,7 +723,7 @@ static int32_t mndRetrieveFuncs(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBl } char varLang[TSDB_TYPE_STR_MAX_LEN + 1] = {0}; varDataSetLen(varLang, strlen(language)); - strcpy(varDataVal(varLang), language); + tstrncpy(varDataVal(varLang), language, sizeof(varLang) - VARSTR_HEADER_SIZE); TAOS_CHECK_RETURN_WITH_RELEASE(colDataSetVal(pColInfo, numOfRows, (const char *)varLang, false), pSdb, pFunc); pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); diff --git a/source/dnode/mnode/impl/src/mndInfoSchema.c b/source/dnode/mnode/impl/src/mndInfoSchema.c index f72f17684e..8774f75521 100644 --- a/source/dnode/mnode/impl/src/mndInfoSchema.c +++ b/source/dnode/mnode/impl/src/mndInfoSchema.c @@ -122,9 +122,9 @@ int32_t mndBuildInsTableCfg(SMnode *pMnode, const char *dbFName, const char *tbN TAOS_RETURN(code); } - strcpy(pRsp->tbName, pMeta->tbName); - strcpy(pRsp->stbName, pMeta->stbName); - strcpy(pRsp->dbFName, pMeta->dbFName); + tstrncpy(pRsp->tbName, pMeta->tbName, sizeof(pRsp->tbName)); + tstrncpy(pRsp->stbName, pMeta->stbName, sizeof(pRsp->stbName)); + tstrncpy(pRsp->dbFName, pMeta->dbFName, sizeof(pRsp->dbFName)); pRsp->numOfTags = pMeta->numOfTags; pRsp->numOfColumns = pMeta->numOfColumns; pRsp->tableType = pMeta->tableType; diff --git a/source/dnode/mnode/impl/src/mndMnode.c b/source/dnode/mnode/impl/src/mndMnode.c index 4df5727c66..a73a19c2a1 100644 --- a/source/dnode/mnode/impl/src/mndMnode.c +++ b/source/dnode/mnode/impl/src/mndMnode.c @@ -14,10 +14,10 @@ */ #define _DEFAULT_SOURCE -#include "mndMnode.h" #include "audit.h" #include "mndCluster.h" #include "mndDnode.h" +#include "mndMnode.h" #include "mndPrivilege.h" #include "mndShow.h" #include "mndSync.h" @@ -884,7 +884,7 @@ static int32_t mndProcessDropMnodeReq(SRpcMsg *pReq) { if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS; char obj[40] = {0}; - sprintf(obj, "%d", dropReq.dnodeId); + (void)tsnprintf(obj, sizeof(obj), "%d", dropReq.dnodeId); auditRecord(pReq, pMnode->clusterId, "dropMnode", "", obj, dropReq.sql, dropReq.sqlLen); diff --git a/source/dnode/mnode/impl/src/mndPerfSchema.c b/source/dnode/mnode/impl/src/mndPerfSchema.c index f36ddf7493..9bd823816a 100644 --- a/source/dnode/mnode/impl/src/mndPerfSchema.c +++ b/source/dnode/mnode/impl/src/mndPerfSchema.c @@ -107,9 +107,9 @@ int32_t mndBuildPerfsTableCfg(SMnode *pMnode, const char *dbFName, const char *t TAOS_RETURN(code); } - strcpy(pRsp->tbName, pMeta->tbName); - strcpy(pRsp->stbName, pMeta->stbName); - strcpy(pRsp->dbFName, pMeta->dbFName); + tstrncpy(pRsp->tbName, pMeta->tbName, sizeof(pRsp->tbName)); + tstrncpy(pRsp->stbName, pMeta->stbName, sizeof(pRsp->stbName)); + tstrncpy(pRsp->dbFName, pMeta->dbFName, sizeof(pRsp->dbFName)); pRsp->numOfTags = pMeta->numOfTags; pRsp->numOfColumns = pMeta->numOfColumns; pRsp->tableType = pMeta->tableType; diff --git a/source/dnode/mnode/impl/src/mndProfile.c b/source/dnode/mnode/impl/src/mndProfile.c index e691ecb904..8fe36ca0c4 100644 --- a/source/dnode/mnode/impl/src/mndProfile.c +++ b/source/dnode/mnode/impl/src/mndProfile.c @@ -14,12 +14,12 @@ */ #define _DEFAULT_SOURCE -#include "mndProfile.h" #include "audit.h" #include "mndDb.h" #include "mndDnode.h" #include "mndMnode.h" #include "mndPrivilege.h" +#include "mndProfile.h" #include "mndQnode.h" #include "mndShow.h" #include "mndSma.h" @@ -137,12 +137,12 @@ void mndCleanupProfile(SMnode *pMnode) { } } -static void setUserInfo2Conn(SConnObj* connObj, char* userApp, uint32_t userIp){ - if (connObj == NULL){ +static void setUserInfo2Conn(SConnObj *connObj, char *userApp, uint32_t userIp) { + if (connObj == NULL) { return; } tstrncpy(connObj->userApp, userApp, sizeof(connObj->userApp)); - connObj->userIp = userIp; + connObj->userIp = userIp; } static SConnObj *mndCreateConn(SMnode *pMnode, const char *user, int8_t connType, uint32_t ip, uint16_t port, int32_t pid, const char *app, int64_t startTime) { @@ -384,7 +384,7 @@ static SAppObj *mndCreateApp(SMnode *pMnode, uint32_t clientIp, SAppHbReq *pReq) app.appId = pReq->appId; app.ip = clientIp; app.pid = pReq->pid; - (void)strcpy(app.name, pReq->name); + tstrncpy(app.name, pReq->name, sizeof(app.name)); app.startTime = pReq->startTime; (void)memcpy(&app.summary, &pReq->summary, sizeof(pReq->summary)); app.lastAccessTimeMs = taosGetTimestampMs(); @@ -911,7 +911,8 @@ static int32_t mndRetrieveConns(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBl char endpoint[TD_IP_LEN + 6 + VARSTR_HEADER_SIZE] = {0}; taosInetNtoa(varDataVal(endpoint), pConn->ip); - (void)sprintf(varDataVal(endpoint) + strlen(varDataVal(endpoint)), ":%d", pConn->port); + (void)tsnprintf(varDataVal(endpoint) + strlen(varDataVal(endpoint)), + sizeof(endpoint) - VARSTR_HEADER_SIZE - strlen(varDataVal(endpoint)), ":%d", pConn->port); varDataLen(endpoint) = strlen(varDataVal(endpoint)); pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); code = colDataSetVal(pColInfo, numOfRows, (const char *)endpoint, false); @@ -944,7 +945,7 @@ static int32_t mndRetrieveConns(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBl } char userIp[TD_IP_LEN + 6 + VARSTR_HEADER_SIZE] = {0}; - if (pConn->userIp != 0 && pConn->userIp != INADDR_NONE){ + if (pConn->userIp != 0 && pConn->userIp != INADDR_NONE) { taosInetNtoa(varDataVal(userIp), pConn->userIp); varDataLen(userIp) = strlen(varDataVal(userIp)); } @@ -987,7 +988,8 @@ static int32_t packQueriesIntoBlock(SShowObj *pShow, SConnObj *pConn, SSDataBloc cols = 0; char queryId[26 + VARSTR_HEADER_SIZE] = {0}; - (void)sprintf(&queryId[VARSTR_HEADER_SIZE], "%x:%" PRIx64, pConn->id, pQuery->reqRid); + (void)tsnprintf(&queryId[VARSTR_HEADER_SIZE], sizeof(queryId) - VARSTR_HEADER_SIZE, "%x:%" PRIx64, pConn->id, + pQuery->reqRid); varDataLen(queryId) = strlen(&queryId[VARSTR_HEADER_SIZE]); SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); code = colDataSetVal(pColInfo, curRowIndex, (const char *)queryId, false); @@ -1043,7 +1045,8 @@ static int32_t packQueriesIntoBlock(SShowObj *pShow, SConnObj *pConn, SSDataBloc char endpoint[TD_IP_LEN + 6 + VARSTR_HEADER_SIZE] = {0}; taosInetNtoa(varDataVal(endpoint), pConn->ip); - (void)sprintf(varDataVal(endpoint) + strlen(varDataVal(endpoint)), ":%d", pConn->port); + (void)tsnprintf(varDataVal(endpoint) + strlen(varDataVal(endpoint)), + sizeof(endpoint) - VARSTR_HEADER_SIZE - strlen(varDataVal(endpoint)), ":%d", pConn->port); varDataLen(endpoint) = strlen(&endpoint[VARSTR_HEADER_SIZE]); pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); code = colDataSetVal(pColInfo, curRowIndex, (const char *)endpoint, false); @@ -1099,11 +1102,12 @@ static int32_t packQueriesIntoBlock(SShowObj *pShow, SConnObj *pConn, SSDataBloc int32_t offset = VARSTR_HEADER_SIZE; for (int32_t i = 0; i < pQuery->subPlanNum && offset + reserve < strSize; ++i) { if (i) { - offset += sprintf(subStatus + offset, ","); + offset += tsnprintf(subStatus + offset, sizeof(subStatus) - offset, ","); } if (offset + reserve < strSize) { SQuerySubDesc *pDesc = taosArrayGet(pQuery->subDesc, i); - offset += sprintf(subStatus + offset, "%" PRIu64 ":%s", pDesc->tid, pDesc->status); + offset += + tsnprintf(subStatus + offset, sizeof(subStatus) - offset, "%" PRIu64 ":%s", pDesc->tid, pDesc->status); } else { break; } @@ -1138,7 +1142,7 @@ static int32_t packQueriesIntoBlock(SShowObj *pShow, SConnObj *pConn, SSDataBloc } char userIp[TD_IP_LEN + 6 + VARSTR_HEADER_SIZE] = {0}; - if (pConn->userIp != 0 && pConn->userIp != INADDR_NONE){ + if (pConn->userIp != 0 && pConn->userIp != INADDR_NONE) { taosInetNtoa(varDataVal(userIp), pConn->userIp); varDataLen(userIp) = strlen(varDataVal(userIp)); } @@ -1242,7 +1246,7 @@ static int32_t mndRetrieveApps(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlo } char name[TSDB_APP_NAME_LEN + 6 + VARSTR_HEADER_SIZE] = {0}; - (void)sprintf(&name[VARSTR_HEADER_SIZE], "%s", pApp->name); + (void)tsnprintf(&name[VARSTR_HEADER_SIZE], sizeof(name) - VARSTR_HEADER_SIZE, "%s", pApp->name); varDataLen(name) = strlen(&name[VARSTR_HEADER_SIZE]); pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); code = colDataSetVal(pColInfo, numOfRows, (const char *)name, false); diff --git a/source/dnode/mnode/impl/src/mndQnode.c b/source/dnode/mnode/impl/src/mndQnode.c index ba1a88aab3..ea42551fa5 100644 --- a/source/dnode/mnode/impl/src/mndQnode.c +++ b/source/dnode/mnode/impl/src/mndQnode.c @@ -333,7 +333,7 @@ static int32_t mndProcessCreateQnodeReq(SRpcMsg *pReq) { if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS; char obj[33] = {0}; - (void)sprintf(obj, "%d", createReq.dnodeId); + (void)tsnprintf(obj, sizeof(obj), "%d", createReq.dnodeId); auditRecord(pReq, pMnode->clusterId, "createQnode", "", obj, createReq.sql, createReq.sqlLen); _OVER: @@ -465,7 +465,7 @@ static int32_t mndProcessDropQnodeReq(SRpcMsg *pReq) { if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS; char obj[33] = {0}; - (void)sprintf(obj, "%d", dropReq.dnodeId); + (void)tsnprintf(obj, sizeof(obj), "%d", dropReq.dnodeId); auditRecord(pReq, pMnode->clusterId, "dropQnode", "", obj, dropReq.sql, dropReq.sqlLen); diff --git a/source/dnode/mnode/impl/src/mndStb.c b/source/dnode/mnode/impl/src/mndStb.c index ae1afdb0db..2db76f6312 100644 --- a/source/dnode/mnode/impl/src/mndStb.c +++ b/source/dnode/mnode/impl/src/mndStb.c @@ -14,7 +14,6 @@ */ #define _DEFAULT_SOURCE -#include "mndStb.h" #include "audit.h" #include "mndDb.h" #include "mndDnode.h" @@ -27,6 +26,7 @@ #include "mndScheduler.h" #include "mndShow.h" #include "mndSma.h" +#include "mndStb.h" #include "mndTopic.h" #include "mndTrans.h" #include "mndUser.h" @@ -1368,7 +1368,7 @@ static int32_t mndProcessCreateStbReq(SRpcMsg *pReq) { if (createReq.sql == NULL && createReq.sqlLen == 0) { char detail[1000] = {0}; - sprintf(detail, "dbname:%s, stable name:%s", name.dbname, name.tname); + (void)tsnprintf(detail, sizeof(detail), "dbname:%s, stable name:%s", name.dbname, name.tname); auditRecord(pReq, pMnode->clusterId, "createStb", name.dbname, name.tname, detail, strlen(detail)); } else { @@ -3148,7 +3148,7 @@ int32_t mndValidateStbInfo(SMnode *pMnode, SSTableVersion *pStbVersions, int32_t TAOS_RETURN(code); } - sprintf(tbFName, "%s.%s", pStbVersion->dbFName, pStbVersion->stbName); + (void)tsnprintf(tbFName, sizeof(tbFName), "%s.%s", pStbVersion->dbFName, pStbVersion->stbName); int32_t code = mndGetTableSma(pMnode, tbFName, &indexRsp, &exist); if (code || !exist) { indexRsp.suid = pStbVersion->suid; @@ -3156,8 +3156,8 @@ int32_t mndValidateStbInfo(SMnode *pMnode, SSTableVersion *pStbVersions, int32_t indexRsp.pIndex = NULL; } - strcpy(indexRsp.dbFName, pStbVersion->dbFName); - strcpy(indexRsp.tbName, pStbVersion->stbName); + tstrncpy(indexRsp.dbFName, pStbVersion->dbFName, sizeof(indexRsp.dbFName)); + tstrncpy(indexRsp.tbName, pStbVersion->stbName, sizeof(indexRsp.tbName)); if (taosArrayPush(hbRsp.pIndexRsp, &indexRsp) == NULL) { code = terrno; @@ -3256,282 +3256,6 @@ void mndExtractTbNameFromStbFullName(const char *stbFullName, char *dst, int32_t } } -// static int32_t mndProcessRetrieveStbReq(SRpcMsg *pReq) { -// SMnode *pMnode = pReq->info.node; -// SShowMgmt *pMgmt = &pMnode->showMgmt; -// SShowObj *pShow = NULL; -// int32_t rowsToRead = SHOW_STEP_SIZE; -// int32_t rowsRead = 0; -// -// SRetrieveTableReq retrieveReq = {0}; -// if (tDeserializeSRetrieveTableReq(pReq->pCont, pReq->contLen, &retrieveReq) != 0) { -// terrno = TSDB_CODE_INVALID_MSG; -// return -1; -// } -// -// SMnode *pMnode = pReq->info.node; -// SSdb *pSdb = pMnode->pSdb; -// int32_t numOfRows = 0; -// SDbObj *pDb = NULL; -// ESdbStatus objStatus = 0; -// -// SUserObj *pUser = mndAcquireUser(pMnode, pReq->info.conn.user); -// if (pUser == NULL) return 0; -// bool sysinfo = pUser->sysInfo; -// -// // Append the information_schema database into the result. -//// if (!pShow->sysDbRsp) { -//// SDbObj infoschemaDb = {0}; -//// setInformationSchemaDbCfg(pMnode, &infoschemaDb); -//// size_t numOfTables = 0; -//// getVisibleInfosTablesNum(sysinfo, &numOfTables); -//// mndDumpDbInfoData(pMnode, pBlock, &infoschemaDb, pShow, numOfRows, numOfTables, true, 0, 1); -//// -//// numOfRows += 1; -//// -//// SDbObj perfschemaDb = {0}; -//// setPerfSchemaDbCfg(pMnode, &perfschemaDb); -//// numOfTables = 0; -//// getPerfDbMeta(NULL, &numOfTables); -//// mndDumpDbInfoData(pMnode, pBlock, &perfschemaDb, pShow, numOfRows, numOfTables, true, 0, 1); -//// -//// numOfRows += 1; -//// pShow->sysDbRsp = true; -//// } -// -// SSDataBlock* p = buildInfoSchemaTableMetaBlock(TSDB_INS_TABLE_COLS); -// blockDataEnsureCapacity(p, rowsToRead); -// -// size_t size = 0; -// const SSysTableMeta* pSysDbTableMeta = NULL; -// -// getInfosDbMeta(&pSysDbTableMeta, &size); -// p->info.rows = buildDbColsInfoBlock(sysinfo, p, pSysDbTableMeta, size, TSDB_INFORMATION_SCHEMA_DB); -// -// getPerfDbMeta(&pSysDbTableMeta, &size); -// p->info.rows = buildDbColsInfoBlock(sysinfo, p, pSysDbTableMeta, size, TSDB_PERFORMANCE_SCHEMA_DB); -// -// blockDataDestroy(p); -// -// -// while (numOfRows < rowsToRead) { -// pShow->pIter = sdbFetchAll(pSdb, SDB_DB, pShow->pIter, (void **)&pDb, &objStatus, true); -// if (pShow->pIter == NULL) break; -// if (strncmp(retrieveReq.db, pDb->name, strlen(retrieveReq.db)) != 0){ -// continue; -// } -// if (mndCheckDbPrivilege(pMnode, pReq->info.conn.user, MND_OPER_READ_OR_WRITE_DB, pDb) != 0) { -// continue; -// } -// -// while (numOfRows < rowsToRead) { -// pShow->pIter = sdbFetch(pSdb, SDB_STB, pShow->pIter, (void **)&pStb); -// if (pShow->pIter == NULL) break; -// -// if (pDb != NULL && pStb->dbUid != pDb->uid) { -// sdbRelease(pSdb, pStb); -// continue; -// } -// -// cols = 0; -// -// SName name = {0}; -// char stbName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0}; -// mndExtractTbNameFromStbFullName(pStb->name, &stbName[VARSTR_HEADER_SIZE], TSDB_TABLE_NAME_LEN); -// varDataSetLen(stbName, strlen(&stbName[VARSTR_HEADER_SIZE])); -// -// SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); -// colDataSetVal(pColInfo, numOfRows, (const char *)stbName, false); -// -// char db[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0}; -// tNameFromString(&name, pStb->db, T_NAME_ACCT | T_NAME_DB); -// tNameGetDbName(&name, varDataVal(db)); -// varDataSetLen(db, strlen(varDataVal(db))); -// -// pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); -// colDataSetVal(pColInfo, numOfRows, (const char *)db, false); -// -// pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); -// colDataSetVal(pColInfo, numOfRows, (const char *)&pStb->createdTime, false); -// -// pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); -// colDataSetVal(pColInfo, numOfRows, (const char *)&pStb->numOfColumns, false); -// -// pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); -// colDataSetVal(pColInfo, numOfRows, (const char *)&pStb->numOfTags, false); -// -// pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); -// colDataSetVal(pColInfo, numOfRows, (const char *)&pStb->updateTime, false); // number of tables -// -// pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); -// if (pStb->commentLen > 0) { -// char comment[TSDB_TB_COMMENT_LEN + VARSTR_HEADER_SIZE] = {0}; -// STR_TO_VARSTR(comment, pStb->comment); -// colDataSetVal(pColInfo, numOfRows, comment, false); -// } else if (pStb->commentLen == 0) { -// char comment[VARSTR_HEADER_SIZE + VARSTR_HEADER_SIZE] = {0}; -// STR_TO_VARSTR(comment, ""); -// colDataSetVal(pColInfo, numOfRows, comment, false); -// } else { -// colDataSetNULL(pColInfo, numOfRows); -// } -// -// char watermark[64 + VARSTR_HEADER_SIZE] = {0}; -// sprintf(varDataVal(watermark), "%" PRId64 "a,%" PRId64 "a", pStb->watermark[0], pStb->watermark[1]); -// varDataSetLen(watermark, strlen(varDataVal(watermark))); -// -// pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); -// colDataSetVal(pColInfo, numOfRows, (const char *)watermark, false); -// -// char maxDelay[64 + VARSTR_HEADER_SIZE] = {0}; -// sprintf(varDataVal(maxDelay), "%" PRId64 "a,%" PRId64 "a", pStb->maxdelay[0], pStb->maxdelay[1]); -// varDataSetLen(maxDelay, strlen(varDataVal(maxDelay))); -// -// pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); -// colDataSetVal(pColInfo, numOfRows, (const char *)maxDelay, false); -// -// char rollup[160 + VARSTR_HEADER_SIZE] = {0}; -// int32_t rollupNum = (int32_t)taosArrayGetSize(pStb->pFuncs); -// char *sep = ", "; -// int32_t sepLen = strlen(sep); -// int32_t rollupLen = sizeof(rollup) - VARSTR_HEADER_SIZE - 2; -// for (int32_t i = 0; i < rollupNum; ++i) { -// char *funcName = taosArrayGet(pStb->pFuncs, i); -// if (i) { -// strncat(varDataVal(rollup), sep, rollupLen); -// rollupLen -= sepLen; -// } -// strncat(varDataVal(rollup), funcName, rollupLen); -// rollupLen -= strlen(funcName); -// } -// varDataSetLen(rollup, strlen(varDataVal(rollup))); -// -// pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); -// colDataSetVal(pColInfo, numOfRows, (const char *)rollup, false); -// -// numOfRows++; -// sdbRelease(pSdb, pStb); -// } -// -// if (pDb != NULL) { -// mndReleaseDb(pMnode, pDb); -// } -// -// sdbRelease(pSdb, pDb); -// } -// -// pShow->numOfRows += numOfRows; -// mndReleaseUser(pMnode, pUser); -// -// -// -// -// -// -// -// -// ShowRetrieveFp retrieveFp = pMgmt->retrieveFps[pShow->type]; -// if (retrieveFp == NULL) { -// mndReleaseShowObj(pShow, false); -// terrno = TSDB_CODE_MSG_NOT_PROCESSED; -// mError("show:0x%" PRIx64 ", failed to retrieve data since %s", pShow->id, terrstr()); -// return -1; -// } -// -// mDebug("show:0x%" PRIx64 ", start retrieve data, type:%d", pShow->id, pShow->type); -// if (retrieveReq.user[0] != 0) { -// memcpy(pReq->info.conn.user, retrieveReq.user, TSDB_USER_LEN); -// } else { -// memcpy(pReq->info.conn.user, TSDB_DEFAULT_USER, strlen(TSDB_DEFAULT_USER) + 1); -// } -// if (retrieveReq.db[0] && mndCheckShowPrivilege(pMnode, pReq->info.conn.user, pShow->type, retrieveReq.db) != 0) { -// return -1; -// } -// -// int32_t numOfCols = pShow->pMeta->numOfColumns; -// -// SSDataBlock *pBlock = createDataBlock(); -// for (int32_t i = 0; i < numOfCols; ++i) { -// SColumnInfoData idata = {0}; -// -// SSchema *p = &pShow->pMeta->pSchemas[i]; -// -// idata.info.bytes = p->bytes; -// idata.info.type = p->type; -// idata.info.colId = p->colId; -// blockDataAppendColInfo(pBlock, &idata); -// } -// -// blockDataEnsureCapacity(pBlock, rowsToRead); -// -// if (mndCheckRetrieveFinished(pShow)) { -// mDebug("show:0x%" PRIx64 ", read finished, numOfRows:%d", pShow->id, pShow->numOfRows); -// rowsRead = 0; -// } else { -// rowsRead = (*retrieveFp)(pReq, pShow, pBlock, rowsToRead); -// if (rowsRead < 0) { -// terrno = rowsRead; -// mDebug("show:0x%" PRIx64 ", retrieve completed", pShow->id); -// mndReleaseShowObj(pShow, true); -// blockDataDestroy(pBlock); -// return -1; -// } -// -// pBlock->info.rows = rowsRead; -// mDebug("show:0x%" PRIx64 ", stop retrieve data, rowsRead:%d numOfRows:%d", pShow->id, rowsRead, pShow->numOfRows); -// } -// -// size = sizeof(SRetrieveMetaTableRsp) + sizeof(int32_t) + sizeof(SSysTableSchema) * pShow->pMeta->numOfColumns + -// blockDataGetSize(pBlock) + blockDataGetSerialMetaSize(taosArrayGetSize(pBlock->pDataBlock)); -// -// SRetrieveMetaTableRsp *pRsp = rpcMallocCont(size); -// if (pRsp == NULL) { -// mndReleaseShowObj(pShow, false); -// terrno = TSDB_CODE_OUT_OF_MEMORY; -// mError("show:0x%" PRIx64 ", failed to retrieve data since %s", pShow->id, terrstr()); -// blockDataDestroy(pBlock); -// return -1; -// } -// -// pRsp->handle = htobe64(pShow->id); -// -// if (rowsRead > 0) { -// char *pStart = pRsp->data; -// SSchema *ps = pShow->pMeta->pSchemas; -// -// *(int32_t *)pStart = htonl(pShow->pMeta->numOfColumns); -// pStart += sizeof(int32_t); // number of columns -// -// for (int32_t i = 0; i < pShow->pMeta->numOfColumns; ++i) { -// SSysTableSchema *pSchema = (SSysTableSchema *)pStart; -// pSchema->bytes = htonl(ps[i].bytes); -// pSchema->colId = htons(ps[i].colId); -// pSchema->type = ps[i].type; -// -// pStart += sizeof(SSysTableSchema); -// } -// -// int32_t len = blockEncode(pBlock, pStart, pShow->pMeta->numOfColumns); -// } -// -// pRsp->numOfRows = htonl(rowsRead); -// pRsp->precision = TSDB_TIME_PRECISION_MILLI; // millisecond time precision -// pReq->info.rsp = pRsp; -// pReq->info.rspLen = size; -// -// if (rowsRead == 0 || rowsRead < rowsToRead) { -// pRsp->completed = 1; -// mDebug("show:0x%" PRIx64 ", retrieve completed", pShow->id); -// mndReleaseShowObj(pShow, true); -// } else { -// mDebug("show:0x%" PRIx64 ", retrieve not completed yet", pShow->id); -// mndReleaseShowObj(pShow, false); -// } -// -// blockDataDestroy(pBlock); -// return TSDB_CODE_SUCCESS; -//} - static int32_t mndRetrieveStb(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) { SMnode *pMnode = pReq->info.node; SSdb *pSdb = pMnode->pSdb; @@ -3607,14 +3331,16 @@ static int32_t mndRetrieveStb(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBloc } char watermark[64 + VARSTR_HEADER_SIZE] = {0}; - sprintf(varDataVal(watermark), "%" PRId64 "a,%" PRId64 "a", pStb->watermark[0], pStb->watermark[1]); + (void)tsnprintf(varDataVal(watermark), sizeof(watermark) - VARSTR_HEADER_SIZE, "%" PRId64 "a,%" PRId64 "a", + pStb->watermark[0], pStb->watermark[1]); varDataSetLen(watermark, strlen(varDataVal(watermark))); pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)watermark, false), pStb, &lino, _ERROR); char maxDelay[64 + VARSTR_HEADER_SIZE] = {0}; - sprintf(varDataVal(maxDelay), "%" PRId64 "a,%" PRId64 "a", pStb->maxdelay[0], pStb->maxdelay[1]); + (void)tsnprintf(varDataVal(maxDelay), sizeof(maxDelay) - VARSTR_HEADER_SIZE, "%" PRId64 "a,%" PRId64 "a", + pStb->maxdelay[0], pStb->maxdelay[1]); varDataSetLen(maxDelay, strlen(varDataVal(maxDelay))); pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); @@ -3708,13 +3434,16 @@ static int32_t buildDbColsInfoBlock(const SSDataBlock *p, const SSysTableMeta *p int8_t colType = pm->schema[j].type; pColInfoData = taosArrayGet(p->pDataBlock, 4); char colTypeStr[VARSTR_HEADER_SIZE + 32]; - int colTypeLen = sprintf(varDataVal(colTypeStr), "%s", tDataTypes[colType].name); + int colTypeLen = + tsnprintf(varDataVal(colTypeStr), sizeof(colTypeStr) - VARSTR_HEADER_SIZE, "%s", tDataTypes[colType].name); if (colType == TSDB_DATA_TYPE_VARCHAR) { colTypeLen += - sprintf(varDataVal(colTypeStr) + colTypeLen, "(%d)", (int32_t)(pm->schema[j].bytes - VARSTR_HEADER_SIZE)); + tsnprintf(varDataVal(colTypeStr) + colTypeLen, sizeof(colTypeStr) - colTypeLen - VARSTR_HEADER_SIZE, "(%d)", + (int32_t)(pm->schema[j].bytes - VARSTR_HEADER_SIZE)); } else if (colType == TSDB_DATA_TYPE_NCHAR) { - colTypeLen += sprintf(varDataVal(colTypeStr) + colTypeLen, "(%d)", - (int32_t)((pm->schema[j].bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE)); + colTypeLen += + tsnprintf(varDataVal(colTypeStr) + colTypeLen, sizeof(colTypeStr) - colTypeLen - VARSTR_HEADER_SIZE, "(%d)", + (int32_t)((pm->schema[j].bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE)); } varDataSetLen(colTypeStr, colTypeLen); TAOS_CHECK_GOTO(colDataSetVal(pColInfoData, numOfRows, (char *)colTypeStr, false), &lino, _OVER); @@ -3864,13 +3593,16 @@ static int32_t mndRetrieveStbCol(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pB int8_t colType = pStb->pColumns[i].type; pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); char colTypeStr[VARSTR_HEADER_SIZE + 32]; - int colTypeLen = sprintf(varDataVal(colTypeStr), "%s", tDataTypes[colType].name); + int colTypeLen = + tsnprintf(varDataVal(colTypeStr), sizeof(colTypeStr) - VARSTR_HEADER_SIZE, "%s", tDataTypes[colType].name); if (colType == TSDB_DATA_TYPE_VARCHAR) { - colTypeLen += sprintf(varDataVal(colTypeStr) + colTypeLen, "(%d)", - (int32_t)(pStb->pColumns[i].bytes - VARSTR_HEADER_SIZE)); + colTypeLen += + tsnprintf(varDataVal(colTypeStr) + colTypeLen, sizeof(colTypeStr) - colTypeLen - VARSTR_HEADER_SIZE, + "(%d)", (int32_t)(pStb->pColumns[i].bytes - VARSTR_HEADER_SIZE)); } else if (colType == TSDB_DATA_TYPE_NCHAR) { - colTypeLen += sprintf(varDataVal(colTypeStr) + colTypeLen, "(%d)", - (int32_t)((pStb->pColumns[i].bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE)); + colTypeLen += + tsnprintf(varDataVal(colTypeStr) + colTypeLen, sizeof(colTypeStr) - colTypeLen - VARSTR_HEADER_SIZE, + "(%d)", (int32_t)((pStb->pColumns[i].bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE)); } varDataSetLen(colTypeStr, colTypeLen); RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (char *)colTypeStr, false), pStb, &lino, _OVER); @@ -4089,11 +3821,10 @@ typedef struct SMDropTbTsmaInfos { } SMDropTbTsmaInfos; typedef struct SMndDropTbsWithTsmaCtx { - SHashObj *pVgMap; // + SHashObj *pVgMap; // } SMndDropTbsWithTsmaCtx; -static int32_t mndDropTbForSingleVg(SMnode *pMnode, SMndDropTbsWithTsmaCtx *pCtx, SArray *pTbs, - int32_t vgId); +static int32_t mndDropTbForSingleVg(SMnode *pMnode, SMndDropTbsWithTsmaCtx *pCtx, SArray *pTbs, int32_t vgId); static void destroySVDropTbBatchReqs(void *p); static void mndDestroyDropTbsWithTsmaCtx(SMndDropTbsWithTsmaCtx *p) { @@ -4300,8 +4031,7 @@ static int32_t mndDropTbAdd(SMnode *pMnode, SHashObj *pVgHashMap, const SVgroupI return 0; } -static int32_t mndDropTbForSingleVg(SMnode *pMnode, SMndDropTbsWithTsmaCtx *pCtx, SArray *pTbs, - int32_t vgId) { +static int32_t mndDropTbForSingleVg(SMnode *pMnode, SMndDropTbsWithTsmaCtx *pCtx, SArray *pTbs, int32_t vgId) { int32_t code = 0; SVgObj *pVgObj = mndAcquireVgroup(pMnode, vgId); diff --git a/source/dnode/mnode/impl/src/mndStream.c b/source/dnode/mnode/impl/src/mndStream.c index cab61648e1..3bee82e3e7 100644 --- a/source/dnode/mnode/impl/src/mndStream.c +++ b/source/dnode/mnode/impl/src/mndStream.c @@ -13,13 +13,13 @@ * along with this program. If not, see . */ -#include "mndStream.h" #include "audit.h" #include "mndDb.h" #include "mndPrivilege.h" #include "mndScheduler.h" #include "mndShow.h" #include "mndStb.h" +#include "mndStream.h" #include "mndTrans.h" #include "osMemory.h" #include "parser.h" @@ -159,16 +159,16 @@ void mndCleanupStream(SMnode *pMnode) { taosHashCleanup(execInfo.pTransferStateStreams); taosHashCleanup(execInfo.pChkptStreams); taosHashCleanup(execInfo.pStreamConsensus); - (void) taosThreadMutexDestroy(&execInfo.lock); + (void)taosThreadMutexDestroy(&execInfo.lock); mDebug("mnd stream exec info cleanup"); } SSdbRow *mndStreamActionDecode(SSdbRaw *pRaw) { int32_t code = 0; int32_t lino = 0; - SSdbRow * pRow = NULL; + SSdbRow *pRow = NULL; SStreamObj *pStream = NULL; - void * buf = NULL; + void *buf = NULL; int8_t sver = 0; int32_t tlen; int32_t dataPos = 0; @@ -237,7 +237,7 @@ static int32_t mndStreamActionDelete(SSdb *pSdb, SStreamObj *pStream) { static int32_t mndStreamActionUpdate(SSdb *pSdb, SStreamObj *pOldStream, SStreamObj *pNewStream) { mTrace("stream:%s, perform update action", pOldStream->name); - (void) atomic_exchange_32(&pOldStream->version, pNewStream->version); + (void)atomic_exchange_32(&pOldStream->version, pNewStream->version); taosWLockLatch(&pOldStream->lock); @@ -252,7 +252,7 @@ static int32_t mndStreamActionUpdate(SSdb *pSdb, SStreamObj *pOldStream, SStream int32_t mndAcquireStream(SMnode *pMnode, char *streamName, SStreamObj **pStream) { int32_t code = 0; - SSdb *pSdb = pMnode->pSdb; + SSdb *pSdb = pMnode->pSdb; (*pStream) = sdbAcquire(pSdb, SDB_STREAM, streamName); if ((*pStream) == NULL && terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) { code = TSDB_CODE_MND_STREAM_NOT_EXIST; @@ -301,7 +301,7 @@ static int32_t createSchemaByFields(const SArray *pFields, SSchemaWrapper *pWrap pWrapper->pSchema[index].bytes = pField->bytes; } pWrapper->pSchema[index].colId = index + 1; - strcpy(pWrapper->pSchema[index].name, pField->name); + tstrncpy(pWrapper->pSchema[index].name, pField->name, sizeof(pWrapper->pSchema[index].name)); pWrapper->pSchema[index].flags = pField->flags; index += 1; } @@ -359,7 +359,8 @@ static int32_t mndBuildStreamObjFromCreateReq(SMnode *pMnode, SStreamObj *pObj, SDbObj *pSourceDb = mndAcquireDb(pMnode, pCreate->sourceDB); if (pSourceDb == NULL) { code = terrno; - mInfo("stream:%s failed to create, source db %s not exist since %s", pCreate->name, pObj->sourceDb, tstrerror(code)); + mInfo("stream:%s failed to create, source db %s not exist since %s", pCreate->name, pObj->sourceDb, + tstrerror(code)); goto FAIL; } @@ -371,7 +372,8 @@ static int32_t mndBuildStreamObjFromCreateReq(SMnode *pMnode, SStreamObj *pObj, SDbObj *pTargetDb = mndAcquireDbByStb(pMnode, pObj->targetSTbName); if (pTargetDb == NULL) { code = terrno; - mError("stream:%s failed to create, target db %s not exist since %s", pCreate->name, pObj->targetDb, tstrerror(code)); + mError("stream:%s failed to create, target db %s not exist since %s", pCreate->name, pObj->targetDb, + tstrerror(code)); goto FAIL; } @@ -417,7 +419,7 @@ static int32_t mndBuildStreamObjFromCreateReq(SMnode *pMnode, SStreamObj *pObj, pFullSchema[i].bytes = pObj->outputSchema.pSchema[dataIndex].bytes; pFullSchema[i].colId = i + 1; // pObj->outputSchema.pSchema[dataIndex].colId; pFullSchema[i].flags = pObj->outputSchema.pSchema[dataIndex].flags; - strcpy(pFullSchema[i].name, pObj->outputSchema.pSchema[dataIndex].name); + tstrncpy(pFullSchema[i].name, pObj->outputSchema.pSchema[dataIndex].name, sizeof(pFullSchema[i].name)); pFullSchema[i].type = pObj->outputSchema.pSchema[dataIndex].type; dataIndex++; } else { @@ -435,7 +437,7 @@ static int32_t mndBuildStreamObjFromCreateReq(SMnode *pMnode, SStreamObj *pObj, pFullSchema[i].bytes = pObj->outputSchema.pSchema[dataIndex].bytes; pFullSchema[i].colId = i + 1; // pObj->outputSchema.pSchema[dataIndex].colId; pFullSchema[i].flags = pObj->outputSchema.pSchema[dataIndex].flags; - strcpy(pFullSchema[i].name, pObj->outputSchema.pSchema[dataIndex].name); + tstrncpy(pFullSchema[i].name, pObj->outputSchema.pSchema[dataIndex].name, sizeof(pFullSchema[i].name)); pFullSchema[i].type = pObj->outputSchema.pSchema[dataIndex].type; dataIndex++; } else { @@ -457,7 +459,8 @@ static int32_t mndBuildStreamObjFromCreateReq(SMnode *pMnode, SStreamObj *pObj, .pAstRoot = pAst, .topicQuery = false, .streamQuery = true, - .triggerType = (pObj->conf.trigger == STREAM_TRIGGER_MAX_DELAY)? STREAM_TRIGGER_WINDOW_CLOSE : pObj->conf.trigger, + .triggerType = + (pObj->conf.trigger == STREAM_TRIGGER_MAX_DELAY) ? STREAM_TRIGGER_WINDOW_CLOSE : pObj->conf.trigger, .watermark = pObj->conf.watermark, .igExpired = pObj->conf.igExpired, .deleteMark = pObj->deleteMark, @@ -540,7 +543,8 @@ int32_t mndPersistTaskDeployReq(STrans *pTrans, SStreamTask *pTask) { return code; } - code = setTransAction(pTrans, buf, tlen, TDMT_STREAM_TASK_DEPLOY, &pTask->info.epSet, 0, TSDB_CODE_VND_INVALID_VGROUP_ID); + code = setTransAction(pTrans, buf, tlen, TDMT_STREAM_TASK_DEPLOY, &pTask->info.epSet, 0, + TSDB_CODE_VND_INVALID_VGROUP_ID); if (code) { taosMemoryFree(buf); } @@ -550,7 +554,7 @@ int32_t mndPersistTaskDeployReq(STrans *pTrans, SStreamTask *pTask) { int32_t mndPersistStreamTasks(STrans *pTrans, SStreamObj *pStream) { SStreamTaskIter *pIter = NULL; - int32_t code = createStreamTaskIter(pStream, &pIter); + int32_t code = createStreamTaskIter(pStream, &pIter); if (code) { mError("failed to create task iter for stream:%s", pStream->name); return code; @@ -637,7 +641,7 @@ static int32_t mndCreateStbForStream(SMnode *pMnode, STrans *pTrans, const SStre SField *pField = taosArrayGet(createReq.pTags, 0); TSDB_CHECK_NULL(pField, code, lino, _OVER, terrno); - strcpy(pField->name, "group_id"); + tstrncpy(pField->name, "group_id", sizeof(pField->name)); pField->type = TSDB_DATA_TYPE_UBIGINT; pField->flags = 0; pField->bytes = 8; @@ -963,7 +967,8 @@ static int32_t mndProcessRestartStreamReq(SRpcMsg *pReq) { } STrans *pTrans = NULL; - code = doCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_NOTHING, MND_STREAM_RESTART_NAME, "restart the stream", &pTrans); + code = doCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_NOTHING, MND_STREAM_RESTART_NAME, "restart the stream", + &pTrans); if (pTrans == NULL || code) { mError("stream:%s failed to pause stream since %s", pauseReq.name, tstrerror(code)); sdbRelease(pMnode->pSdb, pStream); @@ -1289,7 +1294,7 @@ static int32_t mndProcessStreamCheckpoint(SRpcMsg *pReq) { streamMutexUnlock(&execInfo.lock); SCheckpointInterval in = {.streamId = pStream->uid, .duration = duration}; - void* p = taosArrayPush(pList, &in); + void *p = taosArrayPush(pList, &in); if (p) { int32_t currentSize = taosArrayGetSize(pList); mDebug("stream:%s (uid:0x%" PRIx64 ") total %d stream(s) beyond chpt interval threshold: %ds(%" PRId64 @@ -1366,7 +1371,7 @@ static int32_t mndProcessStreamCheckpoint(SRpcMsg *pReq) { } static int32_t mndProcessDropStreamReq(SRpcMsg *pReq) { - SMnode * pMnode = pReq->info.node; + SMnode *pMnode = pReq->info.node; SStreamObj *pStream = NULL; int32_t code = 0; @@ -1396,7 +1401,7 @@ static int32_t mndProcessDropStreamReq(SRpcMsg *pReq) { if (pStream->smaId != 0) { mDebug("stream:%s, uid:0x%" PRIx64 " try to drop sma related stream", dropReq.name, pStream->uid); - void * pIter = NULL; + void *pIter = NULL; SSmaObj *pSma = NULL; pIter = sdbFetch(pMnode->pSdb, SDB_SMA, pIter, (void **)&pSma); while (pIter) { @@ -1925,7 +1930,8 @@ static int32_t mndProcessVgroupChange(SMnode *pMnode, SVgroupChangeInfo *pChange // here create only one trans if (pTrans == NULL) { - code = doCreateTrans(pMnode, pStream, NULL, TRN_CONFLICT_NOTHING, MND_STREAM_TASK_UPDATE_NAME, "update task epsets", &pTrans); + code = doCreateTrans(pMnode, pStream, NULL, TRN_CONFLICT_NOTHING, MND_STREAM_TASK_UPDATE_NAME, + "update task epsets", &pTrans); if (pTrans == NULL || code) { sdbRelease(pSdb, pStream); sdbCancelFetch(pSdb, pIter); @@ -2056,9 +2062,9 @@ static int32_t refreshNodeListFromExistedStreams(SMnode *pMnode, SArray *pNodeLi continue; } - char buf[256] = {0}; + char buf[256] = {0}; int32_t ret = epsetToStr(&pEntry->epset, buf, tListLen(buf)); // ignore this error since it is only for log file - if (ret != 0) { // print error and continue + if (ret != 0) { // print error and continue mError("failed to convert epset to str, code:%s", tstrerror(ret)); } @@ -2178,7 +2184,7 @@ static int32_t mndProcessNodeCheckReq(SRpcMsg *pMsg) { mndDestroyVgroupChangeInfo(&changeInfo); - _end: +_end: streamMutexUnlock(&execInfo.lock); taosArrayDestroy(pNodeSnapshot); @@ -2227,7 +2233,7 @@ void saveTaskAndNodeInfoIntoBuf(SStreamObj *pStream, SStreamExecInfo *pExecNode) code = taosHashPut(pExecNode->pTaskMap, &id, sizeof(id), &entry, sizeof(entry)); if (code == 0) { - void * px = taosArrayPush(pExecNode->pTaskList, &id); + void *px = taosArrayPush(pExecNode->pTaskList, &id); int32_t num = (int32_t)taosArrayGetSize(pExecNode->pTaskList); if (px) { mInfo("s-task:0x%x add into task buffer, total:%d", (int32_t)entry.id.taskId, num); @@ -2235,7 +2241,7 @@ void saveTaskAndNodeInfoIntoBuf(SStreamObj *pStream, SStreamExecInfo *pExecNode) mError("s-task:0x%x failed to add into task buffer, total:%d", (int32_t)entry.id.taskId, num); } } else { - mError("s-task:0x%x failed to add into task map, since out of memory", (int32_t) entry.id.taskId); + mError("s-task:0x%x failed to add into task map, since out of memory", (int32_t)entry.id.taskId); } // add the new vgroups if not added yet @@ -2252,11 +2258,12 @@ void saveTaskAndNodeInfoIntoBuf(SStreamObj *pStream, SStreamExecInfo *pExecNode) SNodeEntry nodeEntry = {.hbTimestamp = -1, .nodeId = pTask->info.nodeId, .lastHbMsgId = -1}; epsetAssign(&nodeEntry.epset, &pTask->info.epSet); - void* px = taosArrayPush(pExecNode->pNodeList, &nodeEntry); + void *px = taosArrayPush(pExecNode->pNodeList, &nodeEntry); if (px) { mInfo("vgId:%d added into nodeList, total:%d", nodeEntry.nodeId, (int)taosArrayGetSize(pExecNode->pNodeList)); } else { - mError("vgId:%d failed to add into nodeList, total:%d", nodeEntry.nodeId, (int)taosArrayGetSize(pExecNode->pNodeList)) + mError("vgId:%d failed to add into nodeList, total:%d", nodeEntry.nodeId, + (int)taosArrayGetSize(pExecNode->pNodeList)) } } } @@ -2308,7 +2315,7 @@ int32_t mndProcessStreamReqCheckpoint(SRpcMsg *pReq) { streamMutexLock(&execInfo.lock); SStreamObj *pStream = NULL; - int32_t code = mndGetStreamObj(pMnode, req.streamId, &pStream); + int32_t code = mndGetStreamObj(pMnode, req.streamId, &pStream); if (pStream == NULL || code != 0) { mWarn("failed to find the stream:0x%" PRIx64 ", not handle the checkpoint req, try to acquire in buf", req.streamId); @@ -2470,7 +2477,7 @@ static void doAddReportStreamTask(SArray *pList, int64_t reportChkptId, const SC mError("failed to put into task list, taskId:0x%x", pReport->taskId); } else { int32_t size = taosArrayGetSize(pList); - mDebug("stream:0x%"PRIx64" %d tasks has send checkpoint-report", pReport->streamId, size); + mDebug("stream:0x%" PRIx64 " %d tasks has send checkpoint-report", pReport->streamId, size); } } @@ -2500,7 +2507,7 @@ int32_t mndProcessCheckpointReport(SRpcMsg *pReq) { streamMutexLock(&execInfo.lock); SStreamObj *pStream = NULL; - int32_t code = mndGetStreamObj(pMnode, req.streamId, &pStream); + int32_t code = mndGetStreamObj(pMnode, req.streamId, &pStream); if (pStream == NULL || code != 0) { mWarn("failed to find the stream:0x%" PRIx64 ", not handle checkpoint-report, try to acquire in buf", req.streamId); @@ -2520,7 +2527,8 @@ int32_t mndProcessCheckpointReport(SRpcMsg *pReq) { int32_t numOfTasks = (pStream == NULL) ? 0 : mndGetNumOfStreamTasks(pStream); - SChkptReportInfo *pInfo = (SChkptReportInfo*)taosHashGet(execInfo.pChkptStreams, &req.streamId, sizeof(req.streamId)); + SChkptReportInfo *pInfo = + (SChkptReportInfo *)taosHashGet(execInfo.pChkptStreams, &req.streamId, sizeof(req.streamId)); if (pInfo == NULL) { SChkptReportInfo info = {.pTaskList = taosArrayInit(4, sizeof(STaskChkptInfo)), .streamId = req.streamId}; if (info.pTaskList != NULL) { @@ -2553,14 +2561,14 @@ int32_t mndProcessCheckpointReport(SRpcMsg *pReq) { return code; } -static int64_t getConsensusId(int64_t streamId, int32_t numOfTasks, int32_t* pExistedTasks, bool *pAllSame) { +static int64_t getConsensusId(int64_t streamId, int32_t numOfTasks, int32_t *pExistedTasks, bool *pAllSame) { int32_t num = 0; int64_t chkId = INT64_MAX; *pExistedTasks = 0; *pAllSame = true; - for(int32_t i = 0; i < taosArrayGetSize(execInfo.pTaskList); ++i) { - STaskId* p = taosArrayGet(execInfo.pTaskList, i); + for (int32_t i = 0; i < taosArrayGetSize(execInfo.pTaskList); ++i) { + STaskId *p = taosArrayGet(execInfo.pTaskList, i); if (p == NULL) { continue; } @@ -2570,7 +2578,7 @@ static int64_t getConsensusId(int64_t streamId, int32_t numOfTasks, int32_t* pEx } num += 1; - STaskStatusEntry* pe = taosHashGet(execInfo.pTaskMap, p, sizeof(*p)); + STaskStatusEntry *pe = taosHashGet(execInfo.pTaskMap, p, sizeof(*p)); if (chkId > pe->checkpointInfo.latestId) { if (chkId != INT64_MAX) { *pAllSame = false; @@ -2580,7 +2588,7 @@ static int64_t getConsensusId(int64_t streamId, int32_t numOfTasks, int32_t* pEx } *pExistedTasks = num; - if (num < numOfTasks) { // not all task send info to mnode through hbMsg, no valid checkpoint Id + if (num < numOfTasks) { // not all task send info to mnode through hbMsg, no valid checkpoint Id return -1; } @@ -2677,7 +2685,7 @@ int32_t mndProcessConsensusInTmr(SRpcMsg *pMsg) { mError("failed to create consensus-checkpoint trans, stream:0x%" PRIx64, pStream->uid); } - void* p = taosArrayPush(pList, &pe->req.taskId); + void *p = taosArrayPush(pList, &pe->req.taskId); if (p == NULL) { mError("failed to put into task list, taskId:0x%x", pe->req.taskId); } @@ -2717,7 +2725,7 @@ int32_t mndProcessConsensusInTmr(SRpcMsg *pMsg) { mError("streamId is -1, streamId:%" PRIx64, pInfo->streamId); return TSDB_CODE_FAILED; } - void* p = taosArrayPush(pStreamList, &streamId); + void *p = taosArrayPush(pStreamList, &streamId); if (p == NULL) { mError("failed to put into stream list, stream:0x%" PRIx64, streamId); } @@ -2784,7 +2792,7 @@ void mndStreamResetInitTaskListLoadFlag() { execInfo.initTaskList = false; } -void mndUpdateStreamExecInfoRole(SMnode* pMnode, int32_t role) { +void mndUpdateStreamExecInfoRole(SMnode *pMnode, int32_t role) { execInfo.switchFromFollower = false; if (execInfo.role == NODE_ROLE_UNINIT) { @@ -2833,14 +2841,14 @@ void addAllStreamTasksIntoBuf(SMnode *pMnode, SStreamExecInfo *pExecInfo) { int32_t mndCreateStreamChkptInfoUpdateTrans(SMnode *pMnode, SStreamObj *pStream, SArray *pChkptInfoList) { STrans *pTrans = NULL; int32_t code = doCreateTrans(pMnode, pStream, NULL, TRN_CONFLICT_NOTHING, MND_STREAM_CHKPT_UPDATE_NAME, - "update checkpoint-info", &pTrans); + "update checkpoint-info", &pTrans); if (pTrans == NULL || code) { sdbRelease(pMnode->pSdb, pStream); return code; } code = mndStreamRegisterTrans(pTrans, MND_STREAM_CHKPT_UPDATE_NAME, pStream->uid); - if (code){ + if (code) { sdbRelease(pMnode->pSdb, pStream); mndTransDrop(pTrans); return code; diff --git a/source/dnode/mnode/impl/src/mndStreamHb.c b/source/dnode/mnode/impl/src/mndStreamHb.c index b7b2764442..1923587711 100644 --- a/source/dnode/mnode/impl/src/mndStreamHb.c +++ b/source/dnode/mnode/impl/src/mndStreamHb.c @@ -64,7 +64,7 @@ void addIntoFailedChkptList(SArray *pList, const SFailedCheckpointInfo *pInfo) { } } - void* p = taosArrayPush(pList, pInfo); + void *p = taosArrayPush(pList, pInfo); if (p == NULL) { mError("failed to push failed checkpoint info checkpointId:%" PRId64 " in list", pInfo->checkpointId); } @@ -121,8 +121,8 @@ int32_t mndSendResetFromCheckpointMsg(SMnode *pMnode, int64_t streamId, int32_t int32_t size = sizeof(SStreamTaskResetMsg); int32_t num = taosArrayGetSize(execInfo.pKilledChkptTrans); - for(int32_t i = 0; i < num; ++i) { - SStreamTaskResetMsg* p = taosArrayGet(execInfo.pKilledChkptTrans, i); + for (int32_t i = 0; i < num; ++i) { + SStreamTaskResetMsg *p = taosArrayGet(execInfo.pKilledChkptTrans, i); if (p == NULL) { continue; } @@ -219,11 +219,11 @@ int32_t mndProcessResetStatusReq(SRpcMsg *pReq) { int32_t code = TSDB_CODE_SUCCESS; SStreamObj *pStream = NULL; - SStreamTaskResetMsg* pMsg = pReq->pCont; + SStreamTaskResetMsg *pMsg = pReq->pCont; mndKillTransImpl(pMnode, pMsg->transId, ""); streamMutexLock(&execInfo.lock); - code = mndResetChkptReportInfo(execInfo.pChkptStreams, pMsg->streamId); // do thing if failed + code = mndResetChkptReportInfo(execInfo.pChkptStreams, pMsg->streamId); // do thing if failed streamMutexUnlock(&execInfo.lock); code = mndGetStreamObj(pMnode, pMsg->streamId, &pStream); @@ -291,7 +291,7 @@ int32_t suspendAllStreams(SMnode *pMnode, SRpcHandleInfo *info) { if (pStream->status != STREAM_STATUS__PAUSE) { SMPauseStreamReq reqPause = {0}; - strcpy(reqPause.name, pStream->name); + tstrncpy(reqPause.name, pStream->name, sizeof(reqPause.name)); reqPause.igNotExists = 1; int32_t contLen = tSerializeSMPauseStreamReq(NULL, 0, &reqPause); @@ -375,8 +375,8 @@ int32_t mndProcessStreamHb(SRpcMsg *pReq) { TAOS_RETURN(TSDB_CODE_INVALID_MSG); } - for(int32_t i = 0; i < taosArrayGetSize(execInfo.pNodeList); ++i) { - SNodeEntry* pEntry = taosArrayGet(execInfo.pNodeList, i); + for (int32_t i = 0; i < taosArrayGetSize(execInfo.pNodeList); ++i) { + SNodeEntry *pEntry = taosArrayGet(execInfo.pNodeList, i); if (pEntry == NULL) { continue; } @@ -469,7 +469,7 @@ int32_t mndProcessStreamHb(SRpcMsg *pReq) { // remove failed trans from pChkptStreams code = mndResetChkptReportInfo(execInfo.pChkptStreams, p->id.streamId); if (code) { - mError("failed to remove stream:0x%"PRIx64" in checkpoint stream list", p->id.streamId); + mError("failed to remove stream:0x%" PRIx64 " in checkpoint stream list", p->id.streamId); } } } @@ -576,8 +576,8 @@ void doSendHbMsgRsp(int32_t code, SRpcHandleInfo *pRpcInfo, SEpSet* pMndEpset, i return; } - ((SMStreamHbRspMsg*)buf)->head.vgId = htonl(vgId); - void* abuf = POINTER_SHIFT(buf, sizeof(SMsgHead)); + ((SMStreamHbRspMsg *)buf)->head.vgId = htonl(vgId); + void *abuf = POINTER_SHIFT(buf, sizeof(SMsgHead)); SEncoder encoder; tEncoderInit(&encoder, abuf, tlen); @@ -595,7 +595,7 @@ void doSendHbMsgRsp(int32_t code, SRpcHandleInfo *pRpcInfo, SEpSet* pMndEpset, i pRpcInfo->handle = NULL; // disable auto rsp } -void checkforOrphanTask(SMnode* pMnode, STaskStatusEntry* p, SArray* pOrphanTasks) { +void checkforOrphanTask(SMnode *pMnode, STaskStatusEntry *p, SArray *pOrphanTasks) { SStreamObj *pStream = NULL; int32_t code = mndGetStreamObj(pMnode, p->id.streamId, &pStream); @@ -606,7 +606,7 @@ void checkforOrphanTask(SMnode* pMnode, STaskStatusEntry* p, SArray* pOrphanTask SOrphanTask oTask = {.streamId = p->id.streamId, .taskId = p->id.taskId, .nodeId = p->nodeId}; void *px = taosArrayPush(pOrphanTasks, &oTask); if (px == NULL) { - mError("failed to put task into orphan list, taskId:0x%" PRIx64", code:%s", p->id.taskId, tstrerror(terrno)); + mError("failed to put task into orphan list, taskId:0x%" PRIx64 ", code:%s", p->id.taskId, tstrerror(terrno)); } } else { if (pStream != NULL) { diff --git a/source/dnode/mnode/impl/src/mndStreamUtil.c b/source/dnode/mnode/impl/src/mndStreamUtil.c index 5de8487ced..a625ca81c9 100644 --- a/source/dnode/mnode/impl/src/mndStreamUtil.c +++ b/source/dnode/mnode/impl/src/mndStreamUtil.c @@ -31,7 +31,7 @@ struct SStreamTaskIter { int32_t doRemoveTasks(SStreamExecInfo *pExecNode, STaskId *pRemovedId); -int32_t createStreamTaskIter(SStreamObj* pStream, SStreamTaskIter** pIter) { +int32_t createStreamTaskIter(SStreamObj *pStream, SStreamTaskIter **pIter) { *pIter = taosMemoryCalloc(1, sizeof(SStreamTaskIter)); if (*pIter == NULL) { return terrno; @@ -46,7 +46,7 @@ int32_t createStreamTaskIter(SStreamObj* pStream, SStreamTaskIter** pIter) { return 0; } -bool streamTaskIterNextTask(SStreamTaskIter* pIter) { +bool streamTaskIterNextTask(SStreamTaskIter *pIter) { if (pIter->level >= pIter->totalLevel) { pIter->pTask = NULL; return false; @@ -56,7 +56,7 @@ bool streamTaskIterNextTask(SStreamTaskIter* pIter) { pIter->level += 1; } - while(pIter->level < pIter->totalLevel) { + while (pIter->level < pIter->totalLevel) { SArray *pList = taosArrayGetP(pIter->pStream->tasks, pIter->level); if (pIter->ordinalIndex >= taosArrayGetSize(pList)) { pIter->level += 1; @@ -74,7 +74,7 @@ bool streamTaskIterNextTask(SStreamTaskIter* pIter) { return false; } -int32_t streamTaskIterGetCurrent(SStreamTaskIter* pIter, SStreamTask** pTask) { +int32_t streamTaskIterGetCurrent(SStreamTaskIter *pIter, SStreamTask **pTask) { if (pTask) { *pTask = pIter->pTask; if (*pTask != NULL) { @@ -85,9 +85,7 @@ int32_t streamTaskIterGetCurrent(SStreamTaskIter* pIter, SStreamTask** pTask) { return TSDB_CODE_INVALID_PARA; } -void destroyStreamTaskIter(SStreamTaskIter* pIter) { - taosMemoryFree(pIter); -} +void destroyStreamTaskIter(SStreamTaskIter *pIter) { taosMemoryFree(pIter); } static bool checkStatusForEachReplica(SVgObj *pVgroup) { for (int32_t i = 0; i < pVgroup->replica; ++i) { @@ -396,8 +394,8 @@ int32_t mndGetStreamTask(STaskId *pId, SStreamObj *pStream, SStreamTask **pTask) int32_t mndGetNumOfStreamTasks(const SStreamObj *pStream) { int32_t num = 0; - for(int32_t i = 0; i < taosArrayGetSize(pStream->tasks); ++i) { - SArray* pLevel = taosArrayGetP(pStream->tasks, i); + for (int32_t i = 0; i < taosArrayGetSize(pStream->tasks); ++i) { + SArray *pLevel = taosArrayGetP(pStream->tasks, i); num += taosArrayGetSize(pLevel); } @@ -430,8 +428,8 @@ int32_t mndGetNumOfStreams(SMnode *pMnode, char *dbName, int32_t *pNumOfStreams) return 0; } -static void freeTaskList(void* param) { - SArray** pList = (SArray **)param; +static void freeTaskList(void *param) { + SArray **pList = (SArray **)param; taosArrayDestroy(*pList); } @@ -492,7 +490,7 @@ void removeExpiredNodeInfo(const SArray *pNodeSnapshot) { if (pEntry->nodeId == p->nodeId) { p->hbTimestamp = pEntry->hbTimestamp; - void* px = taosArrayPush(pValidList, p); + void *px = taosArrayPush(pValidList, p); if (px == NULL) { mError("failed to put node into list, nodeId:%d", p->nodeId); } else { @@ -539,7 +537,7 @@ int32_t doRemoveTasks(SStreamExecInfo *pExecNode, STaskId *pRemovedId) { return TSDB_CODE_SUCCESS; } -void removeTasksInBuf(SArray *pTaskIds, SStreamExecInfo* pExecInfo) { +void removeTasksInBuf(SArray *pTaskIds, SStreamExecInfo *pExecInfo) { for (int32_t i = 0; i < taosArrayGetSize(pTaskIds); ++i) { STaskId *pId = taosArrayGet(pTaskIds, i); if (pId == NULL) { @@ -548,7 +546,7 @@ void removeTasksInBuf(SArray *pTaskIds, SStreamExecInfo* pExecInfo) { int32_t code = doRemoveTasks(pExecInfo, pId); if (code) { - mError("failed to remove task in buffer list, 0x%"PRIx64, pId->taskId); + mError("failed to remove task in buffer list, 0x%" PRIx64, pId->taskId); } } } @@ -575,7 +573,7 @@ void removeStreamTasksInBuf(SStreamObj *pStream, SStreamExecInfo *pExecNode) { STaskId id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId}; code = doRemoveTasks(pExecNode, &id); if (code) { - mError("failed to remove task in buffer list, 0x%"PRIx64, id.taskId); + mError("failed to remove task in buffer list, 0x%" PRIx64, id.taskId); } } @@ -642,7 +640,7 @@ int32_t removeExpiredNodeEntryAndTaskInBuf(SArray *pNodeSnapshot) { bool existed = taskNodeExists(pNodeSnapshot, pEntry->nodeId); if (!existed) { - void* p = taosArrayPush(pRemovedTasks, pId); + void *p = taosArrayPush(pRemovedTasks, pId); if (p == NULL) { mError("failed to put task entry into remove list, taskId:0x%" PRIx64, pId->taskId); } @@ -673,7 +671,7 @@ int32_t mndScanCheckpointReportInfo(SRpcMsg *pReq) { streamMutexLock(&execInfo.lock); while ((pIter = taosHashIterate(execInfo.pChkptStreams, pIter)) != NULL) { - SChkptReportInfo* px = (SChkptReportInfo *)pIter; + SChkptReportInfo *px = (SChkptReportInfo *)pIter; if (taosArrayGetSize(px->pTaskList) == 0) { continue; } @@ -727,14 +725,14 @@ int32_t mndScanCheckpointReportInfo(SRpcMsg *pReq) { int32_t size = taosArrayGetSize(pDropped); if (size > 0) { for (int32_t i = 0; i < size; ++i) { - int64_t* pStreamId = (int64_t *)taosArrayGet(pDropped, i); + int64_t *pStreamId = (int64_t *)taosArrayGet(pDropped, i); if (pStreamId == NULL) { continue; } code = taosHashRemove(execInfo.pChkptStreams, pStreamId, sizeof(*pStreamId)); if (code) { - mError("failed to remove stream in buf:0x%"PRIx64, *pStreamId); + mError("failed to remove stream in buf:0x%" PRIx64, *pStreamId); } } @@ -802,10 +800,10 @@ int32_t mndCreateSetConsensusChkptIdTrans(SMnode *pMnode, SStreamObj *pStream, i return TSDB_CODE_ACTION_IN_PROGRESS; } -int32_t mndGetConsensusInfo(SHashObj* pHash, int64_t streamId, int32_t numOfTasks, SCheckpointConsensusInfo **pInfo) { +int32_t mndGetConsensusInfo(SHashObj *pHash, int64_t streamId, int32_t numOfTasks, SCheckpointConsensusInfo **pInfo) { *pInfo = NULL; - void* px = taosHashGet(pHash, &streamId, sizeof(streamId)); + void *px = taosHashGet(pHash, &streamId, sizeof(streamId)); if (px != NULL) { *pInfo = px; return 0; @@ -865,12 +863,12 @@ void mndAddConsensusTasks(SCheckpointConsensusInfo *pInfo, const SRestoreCheckpo } } -void mndClearConsensusRspEntry(SCheckpointConsensusInfo* pInfo) { +void mndClearConsensusRspEntry(SCheckpointConsensusInfo *pInfo) { taosArrayDestroy(pInfo->pTaskList); pInfo->pTaskList = NULL; } -int64_t mndClearConsensusCheckpointId(SHashObj* pHash, int64_t streamId) { +int64_t mndClearConsensusCheckpointId(SHashObj *pHash, int64_t streamId) { int32_t code = 0; int32_t numOfStreams = taosHashGetSize(pHash); if (numOfStreams == 0) { @@ -881,13 +879,13 @@ int64_t mndClearConsensusCheckpointId(SHashObj* pHash, int64_t streamId) { if (code == 0) { mDebug("drop stream:0x%" PRIx64 " in consensus-checkpointId list, remain:%d", streamId, numOfStreams); } else { - mError("failed to remove stream:0x%"PRIx64" in consensus-checkpointId list, remain:%d", streamId, numOfStreams); + mError("failed to remove stream:0x%" PRIx64 " in consensus-checkpointId list, remain:%d", streamId, numOfStreams); } return code; } -int64_t mndClearChkptReportInfo(SHashObj* pHash, int64_t streamId) { +int64_t mndClearChkptReportInfo(SHashObj *pHash, int64_t streamId) { int32_t code = 0; int32_t numOfStreams = taosHashGetSize(pHash); if (numOfStreams == 0) { @@ -898,14 +896,14 @@ int64_t mndClearChkptReportInfo(SHashObj* pHash, int64_t streamId) { if (code == 0) { mDebug("drop stream:0x%" PRIx64 " in chkpt-report list, remain:%d", streamId, numOfStreams); } else { - mError("failed to remove stream:0x%"PRIx64" in chkpt-report list, remain:%d", streamId, numOfStreams); + mError("failed to remove stream:0x%" PRIx64 " in chkpt-report list, remain:%d", streamId, numOfStreams); } return code; } -int32_t mndResetChkptReportInfo(SHashObj* pHash, int64_t streamId) { - SChkptReportInfo* pInfo = taosHashGet(pHash, &streamId, sizeof(streamId)); +int32_t mndResetChkptReportInfo(SHashObj *pHash, int64_t streamId) { + SChkptReportInfo *pInfo = taosHashGet(pHash, &streamId, sizeof(streamId)); if (pInfo != NULL) { taosArrayClear(pInfo->pTaskList); mDebug("stream:0x%" PRIx64 " checkpoint-report list cleared, prev report checkpointId:%" PRId64, streamId, @@ -919,28 +917,28 @@ int32_t mndResetChkptReportInfo(SHashObj* pHash, int64_t streamId) { static void mndShowStreamStatus(char *dst, SStreamObj *pStream) { int8_t status = atomic_load_8(&pStream->status); if (status == STREAM_STATUS__NORMAL) { - strcpy(dst, "ready"); + tstrncpy(dst, "ready", MND_STREAM_TRIGGER_NAME_SIZE); } else if (status == STREAM_STATUS__STOP) { - strcpy(dst, "stop"); + tstrncpy(dst, "stop", MND_STREAM_TRIGGER_NAME_SIZE); } else if (status == STREAM_STATUS__FAILED) { - strcpy(dst, "failed"); + tstrncpy(dst, "failed", MND_STREAM_TRIGGER_NAME_SIZE); } else if (status == STREAM_STATUS__RECOVER) { - strcpy(dst, "recover"); + tstrncpy(dst, "recover", MND_STREAM_TRIGGER_NAME_SIZE); } else if (status == STREAM_STATUS__PAUSE) { - strcpy(dst, "paused"); + tstrncpy(dst, "paused", MND_STREAM_TRIGGER_NAME_SIZE); } } static void mndShowStreamTrigger(char *dst, SStreamObj *pStream) { int8_t trigger = pStream->conf.trigger; if (trigger == STREAM_TRIGGER_AT_ONCE) { - strcpy(dst, "at once"); + tstrncpy(dst, "at once", MND_STREAM_TRIGGER_NAME_SIZE); } else if (trigger == STREAM_TRIGGER_WINDOW_CLOSE) { - strcpy(dst, "window close"); + tstrncpy(dst, "window close", MND_STREAM_TRIGGER_NAME_SIZE); } else if (trigger == STREAM_TRIGGER_MAX_DELAY) { - strcpy(dst, "max delay"); + tstrncpy(dst, "max delay", MND_STREAM_TRIGGER_NAME_SIZE); } else if (trigger == STREAM_TRIGGER_FORCE_WINDOW_CLOSE) { - strcpy(dst, "force window close"); + tstrncpy(dst, "force window close", MND_STREAM_TRIGGER_NAME_SIZE); } } @@ -1000,7 +998,7 @@ int32_t setStreamAttrInResBlock(SStreamObj *pStream, SSDataBlock *pBlock, int32_ TSDB_CHECK_CODE(code, lino, _end); char status[20 + VARSTR_HEADER_SIZE] = {0}; - char status2[20] = {0}; + char status2[MND_STREAM_TRIGGER_NAME_SIZE] = {0}; mndShowStreamStatus(status2, pStream); STR_WITH_MAXSIZE_TO_VARSTR(status, status2, sizeof(status)); pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); @@ -1047,7 +1045,7 @@ int32_t setStreamAttrInResBlock(SStreamObj *pStream, SSDataBlock *pBlock, int32_ TSDB_CHECK_CODE(code, lino, _end); char trigger[20 + VARSTR_HEADER_SIZE] = {0}; - char trigger2[20] = {0}; + char trigger2[MND_STREAM_TRIGGER_NAME_SIZE] = {0}; mndShowStreamTrigger(trigger2, pStream); STR_WITH_MAXSIZE_TO_VARSTR(trigger, trigger2, sizeof(trigger)); pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); @@ -1069,7 +1067,7 @@ int32_t setStreamAttrInResBlock(SStreamObj *pStream, SSDataBlock *pBlock, int32_ // checkpoint interval char tmp[20 + VARSTR_HEADER_SIZE] = {0}; - sprintf(varDataVal(tmp), "%d sec", tsStreamCheckpointInterval); + (void)tsnprintf(varDataVal(tmp), sizeof(tmp) - VARSTR_HEADER_SIZE, "%d sec", tsStreamCheckpointInterval); varDataSetLen(tmp, strlen(varDataVal(tmp))); pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); @@ -1089,7 +1087,7 @@ int32_t setStreamAttrInResBlock(SStreamObj *pStream, SSDataBlock *pBlock, int32_ // history scan idle char scanHistoryIdle[20 + VARSTR_HEADER_SIZE] = {0}; - strcpy(scanHistoryIdle, "100a"); + tstrncpy(scanHistoryIdle, "100a", sizeof(scanHistoryIdle)); memset(dstStr, 0, tListLen(dstStr)); STR_TO_VARSTR(dstStr, scanHistoryIdle) @@ -1105,7 +1103,8 @@ _end: return code; } -int32_t setTaskAttrInResBlock(SStreamObj *pStream, SStreamTask *pTask, SSDataBlock *pBlock, int32_t numOfRows, int32_t precision) { +int32_t setTaskAttrInResBlock(SStreamObj *pStream, SStreamTask *pTask, SSDataBlock *pBlock, int32_t numOfRows, + int32_t precision) { SColumnInfoData *pColInfo = NULL; int32_t cols = 0; int32_t code = 0; @@ -1116,7 +1115,7 @@ int32_t setTaskAttrInResBlock(SStreamObj *pStream, SStreamTask *pTask, SSDataBlo STaskStatusEntry *pe = taosHashGet(execInfo.pTaskMap, &id, sizeof(id)); if (pe == NULL) { mError("task:0x%" PRIx64 " not exists in any vnodes, streamName:%s, streamId:0x%" PRIx64 " createTs:%" PRId64 - " no valid status/stage info", + " no valid status/stage info", id.taskId, pStream->name, pStream->uid, pStream->createTime); return TSDB_CODE_STREAM_TASK_NOT_EXIST; } @@ -1254,7 +1253,7 @@ int32_t setTaskAttrInResBlock(SStreamObj *pStream, SStreamTask *pTask, SSDataBlo if (pTask->info.taskLevel == TASK_LEVEL__SINK) { colDataSetNULL(pColInfo, numOfRows); } else { - sprintf(buf, formatTotalMb, pe->outputTotal); + (void)tsnprintf(buf, sizeof(buf), formatTotalMb, pe->outputTotal); memset(vbuf, 0, tListLen(vbuf)); STR_TO_VARSTR(vbuf, buf); @@ -1281,19 +1280,11 @@ int32_t setTaskAttrInResBlock(SStreamObj *pStream, SStreamTask *pTask, SSDataBlo code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false); TSDB_CHECK_CODE(code, lino, _end); } - - // output queue - // sprintf(buf, queueInfoStr, pe->outputQUsed, pe->outputRate); - // STR_TO_VARSTR(vbuf, buf); - - // pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); - // colDataSetVal(pColInfo, numOfRows, (const char*)vbuf, false); - // info if (pTask->info.taskLevel == TASK_LEVEL__SINK) { const char *sinkStr = "%.2f MiB"; snprintf(buf, tListLen(buf), sinkStr, pe->sinkDataSize); - } else if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) { // offset info + } else if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) { // offset info if (pTask->info.trigger == STREAM_TRIGGER_FORCE_WINDOW_CLOSE) { int32_t ret = taosFormatUtcTime(buf, tListLen(buf), pe->processedVer, precision); if (ret != 0) { @@ -1401,7 +1392,7 @@ int32_t setTaskAttrInResBlock(SStreamObj *pStream, SStreamTask *pTask, SSDataBlo code = colDataSetVal(pColInfo, numOfRows, 0, true); TSDB_CHECK_CODE(code, lino, _end); - _end: +_end: if (code) { mError("error happens during build task attr result blocks, lino:%d, code:%s", lino, tstrerror(code)); } @@ -1418,7 +1409,7 @@ static bool isNodeEpsetChanged(const SEpSet *pPrevEpset, const SEpSet *pCurrent) return true; } -void mndDestroyVgroupChangeInfo(SVgroupChangeInfo* pInfo) { +void mndDestroyVgroupChangeInfo(SVgroupChangeInfo *pInfo) { if (pInfo != NULL) { taosArrayDestroy(pInfo->pUpdateNodeList); taosHashCleanup(pInfo->pDBMap); @@ -1440,7 +1431,7 @@ int32_t mndFindChangedNodeInfo(SMnode *pMnode, const SArray *pPrevNodeList, cons } pInfo->pUpdateNodeList = taosArrayInit(4, sizeof(SNodeUpdateInfo)), - pInfo->pDBMap = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), true, HASH_NO_LOCK); + pInfo->pDBMap = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), true, HASH_NO_LOCK); if (pInfo->pUpdateNodeList == NULL || pInfo->pDBMap == NULL) { mndDestroyVgroupChangeInfo(pInfo); @@ -1457,7 +1448,7 @@ int32_t mndFindChangedNodeInfo(SMnode *pMnode, const SArray *pPrevNodeList, cons int32_t num = taosArrayGetSize(pNodeList); for (int32_t j = 0; j < num; ++j) { SNodeEntry *pCurrent = taosArrayGet(pNodeList, j); - if(pCurrent == NULL) { + if (pCurrent == NULL) { continue; } @@ -1479,7 +1470,7 @@ int32_t mndFindChangedNodeInfo(SMnode *pMnode, const SArray *pPrevNodeList, cons epsetAssign(&updateInfo.prevEp, &pPrevEntry->epset); epsetAssign(&updateInfo.newEp, &pCurrent->epset); - void* p = taosArrayPush(pInfo->pUpdateNodeList, &updateInfo); + void *p = taosArrayPush(pInfo->pUpdateNodeList, &updateInfo); TSDB_CHECK_NULL(p, code, lino, _err, terrno); } @@ -1498,11 +1489,11 @@ int32_t mndFindChangedNodeInfo(SMnode *pMnode, const SArray *pPrevNodeList, cons return code; - _err: +_err: mError("failed to find node change info, code:%s at %s line:%d", tstrerror(code), __func__, lino); mndDestroyVgroupChangeInfo(pInfo); return code; - } +} static int32_t doCheckForUpdated(SMnode *pMnode, SArray **ppNodeSnapshot) { bool allReady = false; diff --git a/source/dnode/mnode/impl/src/mndUser.c b/source/dnode/mnode/impl/src/mndUser.c index b281ddba86..e1518d3752 100644 --- a/source/dnode/mnode/impl/src/mndUser.c +++ b/source/dnode/mnode/impl/src/mndUser.c @@ -691,7 +691,7 @@ static void ipRangeToStr(SIpV4Range *range, char *buf) { (void)uv_inet_ntop(AF_INET, &addr, buf, 32); if (range->mask != 32) { - (void)sprintf(buf + strlen(buf), "/%d", range->mask); + (void)tsnprintf(buf + strlen(buf), 36 - strlen(buf), "/%d", range->mask); } return; } @@ -699,14 +699,14 @@ static bool isDefaultRange(SIpV4Range *pRange) { static SIpV4Range val = {.ip = 16777343, .mask = 32}; return pRange->ip == val.ip && pRange->mask == val.mask; } -static int32_t ipRangeListToStr(SIpV4Range *range, int32_t num, char *buf) { +static int32_t ipRangeListToStr(SIpV4Range *range, int32_t num, char *buf, int64_t bufLen) { int32_t len = 0; for (int i = 0; i < num; i++) { char tbuf[36] = {0}; SIpV4Range *pRange = &range[i]; ipRangeToStr(&range[i], tbuf); - len += sprintf(buf + len, "%s,", tbuf); + len += tsnprintf(buf + len, bufLen - len, "%s,", tbuf); } if (len > 0) buf[len - 1] = 0; return len; @@ -738,11 +738,13 @@ int32_t convertIpWhiteListToStr(SIpWhiteList *pList, char **buf) { *buf = NULL; return 0; } - *buf = taosMemoryCalloc(1, pList->num * 36); + int64_t bufLen = pList->num * 36; + *buf = taosMemoryCalloc(1, bufLen); if (*buf == NULL) { return 0; } - int32_t len = ipRangeListToStr(pList->pIpRange, pList->num, *buf); + + int32_t len = ipRangeListToStr(pList->pIpRange, pList->num, *buf, bufLen); if (len == 0) { taosMemoryFreeClear(*buf); return 0; @@ -1899,8 +1901,8 @@ static int32_t mndProcessCreateUserReq(SRpcMsg *pReq) { if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS; char detail[1000] = {0}; - TAOS_UNUSED(snprintf(detail, sizeof(detail), "enable:%d, superUser:%d, sysInfo:%d, password:xxx", createReq.enable, - createReq.superUser, createReq.sysInfo)); + (void)tsnprintf(detail, sizeof(detail), "enable:%d, superUser:%d, sysInfo:%d, password:xxx", createReq.enable, + createReq.superUser, createReq.sysInfo); char operation[15] = {0}; if (createReq.isImport == 1) { tstrncpy(operation, "importUser", sizeof(operation)); @@ -2502,10 +2504,10 @@ static int32_t mndProcessAlterUserReq(SRpcMsg *pReq) { if (alterReq.alterType == TSDB_ALTER_USER_PASSWD) { char detail[1000] = {0}; - (void)snprintf(detail, sizeof(detail), - "alterType:%s, enable:%d, superUser:%d, sysInfo:%d, createdb:%d, tabName:%s, password:xxx", - mndUserAuditTypeStr(alterReq.alterType), alterReq.enable, alterReq.superUser, alterReq.sysInfo, - alterReq.createdb ? 1 : 0, alterReq.tabName); + (void)tsnprintf(detail, sizeof(detail), + "alterType:%s, enable:%d, superUser:%d, sysInfo:%d, createdb:%d, tabName:%s, password:xxx", + mndUserAuditTypeStr(alterReq.alterType), alterReq.enable, alterReq.superUser, alterReq.sysInfo, + alterReq.createdb ? 1 : 0, alterReq.tabName); auditRecord(pReq, pMnode->clusterId, "alterUser", "", alterReq.user, detail, strlen(detail)); } else if (alterReq.alterType == TSDB_ALTER_USER_SUPERUSER || alterReq.alterType == TSDB_ALTER_USER_ENABLE || alterReq.alterType == TSDB_ALTER_USER_SYSINFO || alterReq.alterType == TSDB_ALTER_USER_CREATEDB) { @@ -2893,12 +2895,12 @@ static int32_t mndLoopHash(SHashObj *hash, char *priType, SSDataBlock *pBlock, i if (nodesStringToNode(value, &pAst) == 0) { if (nodesNodeToSQL(pAst, *sql, bufSz, &sqlLen) != 0) { sqlLen = 5; - (void)snprintf(*sql, bufSz, "error"); + (void)tsnprintf(*sql, bufSz, "error"); } nodesDestroyNode(pAst); } else { sqlLen = 5; - (void)snprintf(*sql, bufSz, "error"); + (void)tsnprintf(*sql, bufSz, "error"); } STR_WITH_MAXSIZE_TO_VARSTR((*condition), (*sql), pShow->pMeta->pSchemas[cols].bytes); diff --git a/source/dnode/mnode/impl/src/mndVgroup.c b/source/dnode/mnode/impl/src/mndVgroup.c index 389b04b1d6..0bce21290b 100644 --- a/source/dnode/mnode/impl/src/mndVgroup.c +++ b/source/dnode/mnode/impl/src/mndVgroup.c @@ -14,7 +14,6 @@ */ #define _DEFAULT_SOURCE -#include "mndVgroup.h" #include "audit.h" #include "mndArbGroup.h" #include "mndDb.h" @@ -27,6 +26,7 @@ #include "mndTopic.h" #include "mndTrans.h" #include "mndUser.h" +#include "mndVgroup.h" #include "tmisce.h" #define VGROUP_VER_NUMBER 1 @@ -1077,7 +1077,7 @@ static int32_t mndRetrieveVgroups(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *p char buf1[20] = {0}; char role[20] = "offline"; if (!exist) { - strcpy(role, "dropping"); + tstrncpy(role, "dropping", sizeof(role)); } else if (online) { char *star = ""; if (pVgroup->vnodeGid[i].syncState == TAOS_SYNC_STATE_LEADER || @@ -2561,7 +2561,7 @@ static int32_t mndProcessRedistributeVgroupMsg(SRpcMsg *pReq) { if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS; char obj[33] = {0}; - sprintf(obj, "%d", req.vgId); + (void)tsnprintf(obj, sizeof(obj), "%d", req.vgId); auditRecord(pReq, pMnode->clusterId, "RedistributeVgroup", "", obj, req.sql, req.sqlLen); diff --git a/source/dnode/vnode/CMakeLists.txt b/source/dnode/vnode/CMakeLists.txt index c377e69f03..8f63cc8779 100644 --- a/source/dnode/vnode/CMakeLists.txt +++ b/source/dnode/vnode/CMakeLists.txt @@ -32,6 +32,8 @@ set( "src/meta/metaSnapshot.c" "src/meta/metaCache.c" "src/meta/metaTtl.c" + "src/meta/metaEntry2.c" + "src/meta/metaTable2.c" # sma "src/sma/smaEnv.c" diff --git a/source/dnode/vnode/inc/vnode.h b/source/dnode/vnode/inc/vnode.h index 11b3cdf3c6..5d4ffc604a 100644 --- a/source/dnode/vnode/inc/vnode.h +++ b/source/dnode/vnode/inc/vnode.h @@ -162,25 +162,25 @@ typedef struct STsdbReader STsdbReader; #define CACHESCAN_RETRIEVE_LAST_ROW 0x4 #define CACHESCAN_RETRIEVE_LAST 0x8 -int32_t tsdbReaderOpen2(void *pVnode, SQueryTableDataCond *pCond, void *pTableList, int32_t numOfTables, - SSDataBlock *pResBlock, void **ppReader, const char *idstr, SHashObj **pIgnoreTables); -int32_t tsdbSetTableList2(STsdbReader *pReader, const void *pTableList, int32_t num); -int32_t tsdbReaderSetId(void *pReader, const char *idstr); -void tsdbReaderClose2(STsdbReader *pReader); -int32_t tsdbNextDataBlock2(STsdbReader *pReader, bool *hasNext); -int32_t tsdbRetrieveDatablockSMA2(STsdbReader *pReader, SSDataBlock *pDataBlock, bool *allHave, bool *hasNullSMA); -void tsdbReleaseDataBlock2(STsdbReader *pReader); -int32_t tsdbRetrieveDataBlock2(STsdbReader *pReader, SSDataBlock **pBlock, SArray *pIdList); -int32_t tsdbReaderReset2(STsdbReader *pReader, SQueryTableDataCond *pCond); -int32_t tsdbGetFileBlocksDistInfo2(STsdbReader *pReader, STableBlockDistInfo *pTableBlockInfo); -int64_t tsdbGetNumOfRowsInMemTable2(STsdbReader *pHandle, uint32_t *rows); -void *tsdbGetIdx2(SMeta *pMeta); -void *tsdbGetIvtIdx2(SMeta *pMeta); -uint64_t tsdbGetReaderMaxVersion2(STsdbReader *pReader); -void tsdbReaderSetCloseFlag(STsdbReader *pReader); -int64_t tsdbGetLastTimestamp2(SVnode *pVnode, void *pTableList, int32_t numOfTables, const char *pIdStr); -void tsdbSetFilesetDelimited(STsdbReader *pReader); -void tsdbReaderSetNotifyCb(STsdbReader *pReader, TsdReaderNotifyCbFn notifyFn, void *param); +int32_t tsdbReaderOpen2(void *pVnode, SQueryTableDataCond *pCond, void *pTableList, int32_t numOfTables, + SSDataBlock *pResBlock, void **ppReader, const char *idstr, SHashObj **pIgnoreTables); +int32_t tsdbSetTableList2(STsdbReader *pReader, const void *pTableList, int32_t num); +int32_t tsdbReaderSetId(void *pReader, const char *idstr); +void tsdbReaderClose2(STsdbReader *pReader); +int32_t tsdbNextDataBlock2(STsdbReader *pReader, bool *hasNext); +int32_t tsdbRetrieveDatablockSMA2(STsdbReader *pReader, SSDataBlock *pDataBlock, bool *allHave, bool *hasNullSMA); +void tsdbReleaseDataBlock2(STsdbReader *pReader); +int32_t tsdbRetrieveDataBlock2(STsdbReader *pReader, SSDataBlock **pBlock, SArray *pIdList); +int32_t tsdbReaderReset2(STsdbReader *pReader, SQueryTableDataCond *pCond); +int32_t tsdbGetFileBlocksDistInfo2(STsdbReader *pReader, STableBlockDistInfo *pTableBlockInfo); +int64_t tsdbGetNumOfRowsInMemTable2(STsdbReader *pHandle, uint32_t *rows); +void *tsdbGetIdx2(SMeta *pMeta); +void *tsdbGetIvtIdx2(SMeta *pMeta); +uint64_t tsdbGetReaderMaxVersion2(STsdbReader *pReader); +void tsdbReaderSetCloseFlag(STsdbReader *pReader); +int64_t tsdbGetLastTimestamp2(SVnode *pVnode, void *pTableList, int32_t numOfTables, const char *pIdStr); +void tsdbSetFilesetDelimited(STsdbReader *pReader); +void tsdbReaderSetNotifyCb(STsdbReader *pReader, TsdReaderNotifyCbFn notifyFn, void *param); int32_t tsdbReuseCacherowsReader(void *pReader, void *pTableIdList, int32_t numOfTables); int32_t tsdbCacherowsReaderOpen(void *pVnode, int32_t type, void *pTableIdList, int32_t numOfTables, int32_t numOfCols, diff --git a/source/dnode/vnode/src/inc/meta.h b/source/dnode/vnode/src/inc/meta.h index 87d7d2150e..c329bc8812 100644 --- a/source/dnode/vnode/src/inc/meta.h +++ b/source/dnode/vnode/src/inc/meta.h @@ -60,7 +60,7 @@ int metaRemoveTableFromIdx(SMeta* pMeta, tb_uid_t uid); static FORCE_INLINE tb_uid_t metaGenerateUid(SMeta* pMeta) { return tGenIdPI64(); } // metaTable ================== -int metaHandleEntry(SMeta* pMeta, const SMetaEntry* pME); +int32_t metaHandleEntry2(SMeta* pMeta, const SMetaEntry* pEntry); // metaCache ================== int32_t metaCacheOpen(SMeta* pMeta); diff --git a/source/dnode/vnode/src/inc/vnodeInt.h b/source/dnode/vnode/src/inc/vnodeInt.h index 0ac475f41b..ef4b233f94 100644 --- a/source/dnode/vnode/src/inc/vnodeInt.h +++ b/source/dnode/vnode/src/inc/vnodeInt.h @@ -155,13 +155,13 @@ int metaCommit(SMeta* pMeta, TXN* txn); int metaFinishCommit(SMeta* pMeta, TXN* txn); int metaPrepareAsyncCommit(SMeta* pMeta); int metaAbort(SMeta* pMeta); -int metaCreateSTable(SMeta* pMeta, int64_t version, SVCreateStbReq* pReq); -int metaAlterSTable(SMeta* pMeta, int64_t version, SVCreateStbReq* pReq); -int metaDropSTable(SMeta* pMeta, int64_t verison, SVDropStbReq* pReq, SArray* tbUidList); -int metaCreateTable(SMeta* pMeta, int64_t version, SVCreateTbReq* pReq, STableMetaRsp** pMetaRsp); -int metaDropTable(SMeta* pMeta, int64_t version, SVDropTbReq* pReq, SArray* tbUids, int64_t* tbUid); +int metaCreateSuperTable(SMeta* pMeta, int64_t version, SVCreateStbReq* pReq); +int32_t metaAlterSuperTable(SMeta* pMeta, int64_t version, SVCreateStbReq* pReq); +int32_t metaDropSuperTable(SMeta* pMeta, int64_t verison, SVDropStbReq* pReq); +int32_t metaCreateTable2(SMeta* pMeta, int64_t version, SVCreateTbReq* pReq, STableMetaRsp** ppRsp); +int32_t metaDropTable2(SMeta* pMeta, int64_t version, SVDropTbReq* pReq); int32_t metaTrimTables(SMeta* pMeta); -int32_t metaDropTables(SMeta* pMeta, SArray* tbUids); +int32_t metaDropMultipleTables(SMeta* pMeta, int64_t version, SArray* tbUids); int metaTtlFindExpired(SMeta* pMeta, int64_t timePointMs, SArray* tbUids, int32_t ttlDropMaxCount); int metaAlterTable(SMeta* pMeta, int64_t version, SVAlterTbReq* pReq, STableMetaRsp* pMetaRsp); int metaUpdateChangeTimeWithLock(SMeta* pMeta, tb_uid_t uid, int64_t changeTimeMs); @@ -176,8 +176,8 @@ int metaAlterCache(SMeta* pMeta, int32_t nPage); int32_t metaUidCacheClear(SMeta* pMeta, uint64_t suid); int32_t metaTbGroupCacheClear(SMeta* pMeta, uint64_t suid); -int metaAddIndexToSTable(SMeta* pMeta, int64_t version, SVCreateStbReq* pReq); -int metaDropIndexFromSTable(SMeta* pMeta, int64_t version, SDropIndexReq* pReq); +int32_t metaAddIndexToSuperTable(SMeta* pMeta, int64_t version, SVCreateStbReq* pReq); +int32_t metaDropIndexFromSuperTable(SMeta* pMeta, int64_t version, SDropIndexReq* pReq); int64_t metaGetTimeSeriesNum(SMeta* pMeta, int type); void metaUpdTimeSeriesNum(SMeta* pMeta); @@ -217,7 +217,7 @@ int32_t tsdbBegin(STsdb* pTsdb); // int32_t tsdbPrepareCommit(STsdb* pTsdb); // int32_t tsdbCommit(STsdb* pTsdb, SCommitInfo* pInfo); int32_t tsdbCacheCommit(STsdb* pTsdb); -int32_t tsdbCacheNewTable(STsdb* pTsdb, int64_t uid, tb_uid_t suid, SSchemaWrapper* pSchemaRow); +int32_t tsdbCacheNewTable(STsdb* pTsdb, int64_t uid, tb_uid_t suid, const SSchemaWrapper* pSchemaRow); int32_t tsdbCacheDropTable(STsdb* pTsdb, int64_t uid, tb_uid_t suid, SSchemaWrapper* pSchemaRow); int32_t tsdbCacheDropSubTables(STsdb* pTsdb, SArray* uids, tb_uid_t suid); int32_t tsdbCacheNewSTableColumn(STsdb* pTsdb, SArray* uids, int16_t cid, int8_t col_type); diff --git a/source/dnode/vnode/src/meta/metaEntry.c b/source/dnode/vnode/src/meta/metaEntry.c index 3b6eaf45d3..302a1eb04a 100644 --- a/source/dnode/vnode/src/meta/metaEntry.c +++ b/source/dnode/vnode/src/meta/metaEntry.c @@ -65,50 +65,71 @@ static FORCE_INLINE int32_t metatInitDefaultSColCmprWrapper(SDecoder *pDecoder, return 0; } +static int32_t metaCloneColCmpr(const SColCmprWrapper *pSrc, SColCmprWrapper *pDst) { + if (pSrc->nCols > 0) { + pDst->nCols = pSrc->nCols; + pDst->version = pSrc->version; + pDst->pColCmpr = (SColCmpr *)taosMemoryCalloc(pSrc->nCols, sizeof(SColCmpr)); + if (NULL == pDst->pColCmpr) { + return terrno; + } + memcpy(pDst->pColCmpr, pSrc->pColCmpr, pSrc->nCols * sizeof(SColCmpr)); + } + return 0; +} + +static void metaCloneColCmprFree(SColCmprWrapper *pCmpr) { + if (pCmpr) { + taosMemoryFreeClear(pCmpr->pColCmpr); + } +} + int metaEncodeEntry(SEncoder *pCoder, const SMetaEntry *pME) { TAOS_CHECK_RETURN(tStartEncode(pCoder)); TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->version)); TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->type)); TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->uid)); - if (pME->name == NULL) { - return TSDB_CODE_INVALID_PARA; - } + if (pME->type > 0) { + if (pME->name == NULL) { + return TSDB_CODE_INVALID_PARA; + } - TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->name)); + TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->name)); - if (pME->type == TSDB_SUPER_TABLE) { - TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->flags)); - TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaRow)); - TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaTag)); - if (TABLE_IS_ROLLUP(pME->flags)) { - TAOS_CHECK_RETURN(tEncodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam)); + if (pME->type == TSDB_SUPER_TABLE) { + TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->flags)); + TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaRow)); + TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaTag)); + if (TABLE_IS_ROLLUP(pME->flags)) { + TAOS_CHECK_RETURN(tEncodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam)); + } + } else if (pME->type == TSDB_CHILD_TABLE) { + TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.btime)); + TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ctbEntry.ttlDays)); + TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ctbEntry.commentLen)); + if (pME->ctbEntry.commentLen > 0) { + TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ctbEntry.comment)); + } + TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.suid)); + TAOS_CHECK_RETURN(tEncodeTag(pCoder, (const STag *)pME->ctbEntry.pTags)); + } else if (pME->type == TSDB_NORMAL_TABLE) { + TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.btime)); + TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ntbEntry.ttlDays)); + TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.commentLen)); + if (pME->ntbEntry.commentLen > 0) { + TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ntbEntry.comment)); + } + TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.ncid)); + TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->ntbEntry.schemaRow)); + } else if (pME->type == TSDB_TSMA_TABLE) { + TAOS_CHECK_RETURN(tEncodeTSma(pCoder, pME->smaEntry.tsma)); + } else { + metaError("meta/entry: invalide table type: %" PRId8 " encode failed.", pME->type); + return TSDB_CODE_INVALID_PARA; } - } else if (pME->type == TSDB_CHILD_TABLE) { - TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.btime)); - TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ctbEntry.ttlDays)); - TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ctbEntry.commentLen)); - if (pME->ctbEntry.commentLen > 0) { - TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ctbEntry.comment)); - } - TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.suid)); - TAOS_CHECK_RETURN(tEncodeTag(pCoder, (const STag *)pME->ctbEntry.pTags)); - } else if (pME->type == TSDB_NORMAL_TABLE) { - TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.btime)); - TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ntbEntry.ttlDays)); - TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.commentLen)); - if (pME->ntbEntry.commentLen > 0) { - TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ntbEntry.comment)); - } - TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.ncid)); - TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->ntbEntry.schemaRow)); - } else if (pME->type == TSDB_TSMA_TABLE) { - TAOS_CHECK_RETURN(tEncodeTSma(pCoder, pME->smaEntry.tsma)); - } else { - metaError("meta/entry: invalide table type: %" PRId8 " encode failed.", pME->type); - return TSDB_CODE_INVALID_PARA; + TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME)); } - TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME)); tEndEncode(pCoder); return 0; @@ -119,68 +140,227 @@ int metaDecodeEntry(SDecoder *pCoder, SMetaEntry *pME) { TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->version)); TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->type)); TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->uid)); - TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->name)); - if (pME->type == TSDB_SUPER_TABLE) { - TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->flags)); - TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaRow)); - TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaTag)); - if (TABLE_IS_ROLLUP(pME->flags)) { - TAOS_CHECK_RETURN(tDecodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam)); - } - } else if (pME->type == TSDB_CHILD_TABLE) { - TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.btime)); - TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ctbEntry.ttlDays)); - TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ctbEntry.commentLen)); - if (pME->ctbEntry.commentLen > 0) { - TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ctbEntry.comment)); - } - TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.suid)); - TAOS_CHECK_RETURN(tDecodeTag(pCoder, (STag **)&pME->ctbEntry.pTags)); - } else if (pME->type == TSDB_NORMAL_TABLE) { - TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.btime)); - TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ntbEntry.ttlDays)); - TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.commentLen)); - if (pME->ntbEntry.commentLen > 0) { - TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ntbEntry.comment)); - } - TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.ncid)); - TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->ntbEntry.schemaRow)); - } else if (pME->type == TSDB_TSMA_TABLE) { - pME->smaEntry.tsma = tDecoderMalloc(pCoder, sizeof(STSma)); - if (!pME->smaEntry.tsma) { - return terrno; - } - TAOS_CHECK_RETURN(tDecodeTSma(pCoder, pME->smaEntry.tsma, true)); - } else { - metaError("meta/entry: invalide table type: %" PRId8 " decode failed.", pME->type); - return TSDB_CODE_INVALID_PARA; - } - if (pME->type == TSDB_SUPER_TABLE) { - if (TABLE_IS_COL_COMPRESSED(pME->flags)) { - TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME)); + if (pME->type > 0) { + TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->name)); - if (pME->colCmpr.nCols == 0) { - TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->stbEntry.schemaRow)); + if (pME->type == TSDB_SUPER_TABLE) { + TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->flags)); + TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaRow)); + TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaTag)); + if (TABLE_IS_ROLLUP(pME->flags)) { + TAOS_CHECK_RETURN(tDecodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam)); } + } else if (pME->type == TSDB_CHILD_TABLE) { + TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.btime)); + TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ctbEntry.ttlDays)); + TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ctbEntry.commentLen)); + if (pME->ctbEntry.commentLen > 0) { + TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ctbEntry.comment)); + } + TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.suid)); + TAOS_CHECK_RETURN(tDecodeTag(pCoder, (STag **)&pME->ctbEntry.pTags)); + } else if (pME->type == TSDB_NORMAL_TABLE) { + TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.btime)); + TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ntbEntry.ttlDays)); + TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.commentLen)); + if (pME->ntbEntry.commentLen > 0) { + TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ntbEntry.comment)); + } + TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.ncid)); + TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->ntbEntry.schemaRow)); + } else if (pME->type == TSDB_TSMA_TABLE) { + pME->smaEntry.tsma = tDecoderMalloc(pCoder, sizeof(STSma)); + if (!pME->smaEntry.tsma) { + return terrno; + } + TAOS_CHECK_RETURN(tDecodeTSma(pCoder, pME->smaEntry.tsma, true)); } else { - TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->stbEntry.schemaRow)); - TABLE_SET_COL_COMPRESSED(pME->flags); + metaError("meta/entry: invalide table type: %" PRId8 " decode failed.", pME->type); + return TSDB_CODE_INVALID_PARA; } - } else if (pME->type == TSDB_NORMAL_TABLE) { - if (!tDecodeIsEnd(pCoder)) { - uDebug("set type: %d, tableName:%s", pME->type, pME->name); - TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME)); - if (pME->colCmpr.nCols == 0) { + if (pME->type == TSDB_SUPER_TABLE) { + if (TABLE_IS_COL_COMPRESSED(pME->flags)) { + TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME)); + + if (pME->colCmpr.nCols == 0) { + TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->stbEntry.schemaRow)); + } + } else { + TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->stbEntry.schemaRow)); + TABLE_SET_COL_COMPRESSED(pME->flags); + } + } else if (pME->type == TSDB_NORMAL_TABLE) { + if (!tDecodeIsEnd(pCoder)) { + uDebug("set type: %d, tableName:%s", pME->type, pME->name); + TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME)); + if (pME->colCmpr.nCols == 0) { + TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow)); + } + } else { + uDebug("set default type: %d, tableName:%s", pME->type, pME->name); TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow)); } - } else { - uDebug("set default type: %d, tableName:%s", pME->type, pME->name); - TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow)); + TABLE_SET_COL_COMPRESSED(pME->flags); } - TABLE_SET_COL_COMPRESSED(pME->flags); } tEndDecode(pCoder); return 0; } + +static int32_t metaCloneSchema(const SSchemaWrapper *pSrc, SSchemaWrapper *pDst) { + if (pSrc == NULL || pDst == NULL) { + return TSDB_CODE_INVALID_PARA; + } + + pDst->nCols = pSrc->nCols; + pDst->version = pSrc->version; + pDst->pSchema = (SSchema *)taosMemoryMalloc(pSrc->nCols * sizeof(SSchema)); + if (pDst->pSchema == NULL) { + return terrno; + } + memcpy(pDst->pSchema, pSrc->pSchema, pSrc->nCols * sizeof(SSchema)); + return TSDB_CODE_SUCCESS; +} + +static void metaCloneSchemaFree(SSchemaWrapper *pSchema) { + if (pSchema) { + taosMemoryFreeClear(pSchema->pSchema); + } +} + +void metaCloneEntryFree(SMetaEntry **ppEntry) { + if (ppEntry == NULL || *ppEntry == NULL) { + return; + } + + taosMemoryFreeClear((*ppEntry)->name); + + if ((*ppEntry)->type < 0) { + taosMemoryFreeClear(*ppEntry); + return; + } + + if (TSDB_SUPER_TABLE == (*ppEntry)->type) { + metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaRow); + metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaTag); + } else if (TSDB_CHILD_TABLE == (*ppEntry)->type) { + taosMemoryFreeClear((*ppEntry)->ctbEntry.comment); + taosMemoryFreeClear((*ppEntry)->ctbEntry.pTags); + } else if (TSDB_NORMAL_TABLE == (*ppEntry)->type) { + metaCloneSchemaFree(&(*ppEntry)->ntbEntry.schemaRow); + taosMemoryFreeClear((*ppEntry)->ntbEntry.comment); + } else { + return; + } + metaCloneColCmprFree(&(*ppEntry)->colCmpr); + + taosMemoryFreeClear(*ppEntry); + return; +} + +int32_t metaCloneEntry(const SMetaEntry *pEntry, SMetaEntry **ppEntry) { + int32_t code = TSDB_CODE_SUCCESS; + + if (NULL == pEntry || NULL == ppEntry) { + return TSDB_CODE_INVALID_PARA; + } + + *ppEntry = (SMetaEntry *)taosMemoryCalloc(1, sizeof(SMetaEntry)); + if (NULL == *ppEntry) { + return terrno; + } + + (*ppEntry)->version = pEntry->version; + (*ppEntry)->type = pEntry->type; + (*ppEntry)->uid = pEntry->uid; + + if (pEntry->type < 0) { + return TSDB_CODE_SUCCESS; + } + + if (pEntry->name) { + (*ppEntry)->name = tstrdup(pEntry->name); + if (NULL == (*ppEntry)->name) { + code = terrno; + metaCloneEntryFree(ppEntry); + return code; + } + } + + if (pEntry->type == TSDB_SUPER_TABLE) { + (*ppEntry)->flags = pEntry->flags; + + code = metaCloneSchema(&pEntry->stbEntry.schemaRow, &(*ppEntry)->stbEntry.schemaRow); + if (code) { + metaCloneEntryFree(ppEntry); + return code; + } + + code = metaCloneSchema(&pEntry->stbEntry.schemaTag, &(*ppEntry)->stbEntry.schemaTag); + if (code) { + metaCloneEntryFree(ppEntry); + return code; + } + } else if (pEntry->type == TSDB_CHILD_TABLE) { + (*ppEntry)->ctbEntry.btime = pEntry->ctbEntry.btime; + (*ppEntry)->ctbEntry.ttlDays = pEntry->ctbEntry.ttlDays; + (*ppEntry)->ctbEntry.suid = pEntry->ctbEntry.suid; + + // comment + (*ppEntry)->ctbEntry.commentLen = pEntry->ctbEntry.commentLen; + if (pEntry->ctbEntry.commentLen > 0) { + (*ppEntry)->ctbEntry.comment = taosMemoryMalloc(pEntry->ctbEntry.commentLen + 1); + if (NULL == (*ppEntry)->ctbEntry.comment) { + code = terrno; + metaCloneEntryFree(ppEntry); + return code; + } + memcpy((*ppEntry)->ctbEntry.comment, pEntry->ctbEntry.comment, pEntry->ctbEntry.commentLen + 1); + } + + // tags + STag *pTags = (STag *)pEntry->ctbEntry.pTags; + (*ppEntry)->ctbEntry.pTags = taosMemoryCalloc(1, pTags->len); + if (NULL == (*ppEntry)->ctbEntry.pTags) { + code = terrno; + metaCloneEntryFree(ppEntry); + return code; + } + memcpy((*ppEntry)->ctbEntry.pTags, pEntry->ctbEntry.pTags, pTags->len); + } else if (pEntry->type == TSDB_NORMAL_TABLE) { + (*ppEntry)->ntbEntry.btime = pEntry->ntbEntry.btime; + (*ppEntry)->ntbEntry.ttlDays = pEntry->ntbEntry.ttlDays; + (*ppEntry)->ntbEntry.ncid = pEntry->ntbEntry.ncid; + + // schema + code = metaCloneSchema(&pEntry->ntbEntry.schemaRow, &(*ppEntry)->ntbEntry.schemaRow); + if (code) { + metaCloneEntryFree(ppEntry); + return code; + } + + // comment + (*ppEntry)->ntbEntry.commentLen = pEntry->ntbEntry.commentLen; + if (pEntry->ntbEntry.commentLen > 0) { + (*ppEntry)->ntbEntry.comment = taosMemoryMalloc(pEntry->ntbEntry.commentLen + 1); + if (NULL == (*ppEntry)->ntbEntry.comment) { + code = terrno; + metaCloneEntryFree(ppEntry); + return code; + } + memcpy((*ppEntry)->ntbEntry.comment, pEntry->ntbEntry.comment, pEntry->ntbEntry.commentLen + 1); + } + } else { + return TSDB_CODE_INVALID_PARA; + } + + code = metaCloneColCmpr(&pEntry->colCmpr, &(*ppEntry)->colCmpr); + if (code) { + metaCloneEntryFree(ppEntry); + return code; + } + + return code; +} diff --git a/source/dnode/vnode/src/meta/metaEntry2.c b/source/dnode/vnode/src/meta/metaEntry2.c new file mode 100644 index 0000000000..7aa57edf73 --- /dev/null +++ b/source/dnode/vnode/src/meta/metaEntry2.c @@ -0,0 +1,1919 @@ +/* + * Copyright (c) 2023 Hongze Cheng . + * All rights reserved. + * + * This code is the intellectual property of Hongze Cheng. + * Any reproduction or distribution, in whole or in part, + * without the express written permission of Hongze Cheng is + * strictly prohibited. + */ + +#include "meta.h" + +int32_t metaCloneEntry(const SMetaEntry *pEntry, SMetaEntry **ppEntry); +void metaCloneEntryFree(SMetaEntry **ppEntry); +void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey); +int metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema); +int metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema); +void metaTimeSeriesNotifyCheck(SMeta *pMeta); +int tagIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2); + +static int32_t metaGetChildUidsOfSuperTable(SMeta *pMeta, tb_uid_t suid, SArray **childList); +static int32_t metaFetchTagIdxKey(SMeta *pMeta, const SMetaEntry *pEntry, const SSchema *pTagColumn, + STagIdxKey **ppTagIdxKey, int32_t *pTagIdxKeySize); +static void metaFetchTagIdxKeyFree(STagIdxKey **ppTagIdxKey); + +#define metaErr(VGID, ERRNO) \ + do { \ + metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " type:%d uid:%" PRId64 " name:%s", VGID, \ + __func__, __FILE__, __LINE__, tstrerror(ERRNO), pEntry->version, pEntry->type, pEntry->uid, \ + pEntry->type > 0 ? pEntry->name : NULL); \ + } while (0) + +typedef enum { + META_ENTRY_TABLE = 0, + META_SCHEMA_TABLE, + META_UID_IDX, + META_NAME_IDX, + META_SUID_IDX, + META_CHILD_IDX, + META_TAG_IDX, + META_BTIME_IDX, + META_TTL_IDX, + META_TABLE_MAX, +} EMetaTable; + +typedef enum { + META_TABLE_OP_INSERT = 0, + META_TABLE_OP_UPDATA, + META_TABLE_OP_DELETE, + META_TABLE_OP_MAX, +} EMetaTableOp; + +typedef struct { + const SMetaEntry *pEntry; + const SMetaEntry *pSuperEntry; + const SMetaEntry *pOldEntry; +} SMetaHandleParam; + +typedef struct { + EMetaTable table; + EMetaTableOp op; +} SMetaTableOp; + +int32_t metaFetchEntryByUid(SMeta *pMeta, int64_t uid, SMetaEntry **ppEntry) { + int32_t code = TSDB_CODE_SUCCESS; + void *value = NULL; + int32_t valueSize = 0; + + // search uid index + code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &value, &valueSize); + if (TSDB_CODE_SUCCESS != code) { + metaError("vgId:%d, failed to get entry by uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), uid, tstrerror(code)); + return code; + } + + // search entry table + STbDbKey key = { + .version = ((SUidIdxVal *)value)->version, + .uid = uid, + }; + tdbFreeClear(value); + + code = tdbTbGet(pMeta->pTbDb, &key, sizeof(key), &value, &valueSize); + if (TSDB_CODE_SUCCESS != code) { + metaError("vgId:%d, failed to get entry by uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), uid, tstrerror(code)); + code = TSDB_CODE_INTERNAL_ERROR; + return code; + } + + // decode entry + SDecoder decoder = {0}; + SMetaEntry entry = {0}; + + tDecoderInit(&decoder, value, valueSize); + code = metaDecodeEntry(&decoder, &entry); + if (code) { + metaError("vgId:%d, failed to decode entry by uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), uid, + tstrerror(code)); + tDecoderClear(&decoder); + tdbFreeClear(value); + return code; + } + + code = metaCloneEntry(&entry, ppEntry); + if (code) { + metaError("vgId:%d, failed to clone entry by uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), uid, + tstrerror(code)); + tDecoderClear(&decoder); + tdbFreeClear(value); + return code; + } + + tdbFreeClear(value); + tDecoderClear(&decoder); + return code; +} + +int32_t metaFetchEntryByName(SMeta *pMeta, const char *name, SMetaEntry **ppEntry) { + int32_t code = TSDB_CODE_SUCCESS; + void *value = NULL; + int32_t valueSize = 0; + + code = tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &value, &valueSize); + if (TSDB_CODE_SUCCESS != code) { + metaError("vgId:%d, failed to get entry by name:%s since %s", TD_VID(pMeta->pVnode), name, tstrerror(code)); + return code; + } + int64_t uid = *(int64_t *)value; + tdbFreeClear(value); + + code = metaFetchEntryByUid(pMeta, uid, ppEntry); + if (TSDB_CODE_SUCCESS != code) { + metaError("vgId:%d, failed to get entry by uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), uid, tstrerror(code)); + code = TSDB_CODE_INTERNAL_ERROR; + } + return code; +} + +void metaFetchEntryFree(SMetaEntry **ppEntry) { metaCloneEntryFree(ppEntry); } + +// Entry Table +static int32_t metaEntryTableUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) { + const SMetaEntry *pEntry = pParam->pEntry; + + int32_t code = TSDB_CODE_SUCCESS; + int32_t vgId = TD_VID(pMeta->pVnode); + void *value = NULL; + int32_t valueSize = 0; + SEncoder encoder = {0}; + STbDbKey key = { + .version = pEntry->version, + .uid = pEntry->uid, + }; + + // encode entry + tEncodeSize(metaEncodeEntry, pEntry, valueSize, code); + if (code != 0) { + metaErr(vgId, code); + return code; + } + + value = taosMemoryMalloc(valueSize); + if (NULL == value) { + metaErr(vgId, terrno); + return terrno; + } + + tEncoderInit(&encoder, value, valueSize); + code = metaEncodeEntry(&encoder, pEntry); + if (code) { + metaErr(vgId, code); + tEncoderClear(&encoder); + taosMemoryFree(value); + return code; + } + tEncoderClear(&encoder); + + // put to tdb + if (META_TABLE_OP_INSERT == op) { + code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn); + } else if (META_TABLE_OP_UPDATA == op) { + code = tdbTbUpsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn); + } else if (META_TABLE_OP_DELETE == op) { + code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn); + } else { + code = TSDB_CODE_INVALID_PARA; + } + if (TSDB_CODE_SUCCESS != code) { + metaErr(vgId, code); + } + taosMemoryFree(value); + return code; +} + +static int32_t metaEntryTableInsert(SMeta *pMeta, const SMetaHandleParam *pParam) { + return metaEntryTableUpsert(pMeta, pParam, META_TABLE_OP_INSERT); +} + +static int32_t metaEntryTableUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) { + return metaEntryTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA); +} + +static int32_t metaEntryTableDelete(SMeta *pMeta, const SMetaHandleParam *pParam) { + return metaEntryTableUpsert(pMeta, pParam, META_TABLE_OP_DELETE); +} + +// Schema Table +static int32_t metaSchemaTableUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) { + int32_t code = TSDB_CODE_SUCCESS; + int32_t vgId = TD_VID(pMeta->pVnode); + SEncoder encoder = {0}; + void *value = NULL; + int32_t valueSize = 0; + + const SMetaEntry *pEntry = pParam->pEntry; + const SSchemaWrapper *pSchema = NULL; + if (pEntry->type == TSDB_SUPER_TABLE) { + pSchema = &pEntry->stbEntry.schemaRow; + } else if (pEntry->type == TSDB_NORMAL_TABLE) { + pSchema = &pEntry->ntbEntry.schemaRow; + } else { + return TSDB_CODE_INVALID_PARA; + } + SSkmDbKey key = { + .uid = pEntry->uid, + .sver = pSchema->version, + }; + + // encode schema + tEncodeSize(tEncodeSSchemaWrapper, pSchema, valueSize, code); + if (TSDB_CODE_SUCCESS != code) { + metaErr(vgId, code); + return code; + } + + value = taosMemoryMalloc(valueSize); + if (NULL == value) { + metaErr(vgId, terrno); + return terrno; + } + + tEncoderInit(&encoder, value, valueSize); + code = tEncodeSSchemaWrapper(&encoder, pSchema); + if (TSDB_CODE_SUCCESS != code) { + metaErr(vgId, code); + tEncoderClear(&encoder); + taosMemoryFree(value); + return code; + } + tEncoderClear(&encoder); + + // put to tdb + if (META_TABLE_OP_INSERT == op) { + code = tdbTbInsert(pMeta->pSkmDb, &key, sizeof(key), value, valueSize, pMeta->txn); + } else if (META_TABLE_OP_UPDATA == op) { + code = tdbTbUpsert(pMeta->pSkmDb, &key, sizeof(key), value, valueSize, pMeta->txn); + } else { + code = TSDB_CODE_INVALID_PARA; + } + if (TSDB_CODE_SUCCESS != code) { + metaErr(vgId, code); + } + taosMemoryFree(value); + return code; +} + +static int32_t metaSchemaTableInsert(SMeta *pMeta, const SMetaHandleParam *pParam) { + return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_INSERT); +} + +static int32_t metaAddOrDropTagIndexOfSuperTable(SMeta *pMeta, const SMetaHandleParam *pParam, + const SSchema *pOldColumn, const SSchema *pNewColumn) { + int32_t code = TSDB_CODE_SUCCESS; + + const SMetaEntry *pEntry = pParam->pEntry; + const SMetaEntry *pOldEntry = pParam->pOldEntry; + enum { ADD_INDEX, DROP_INDEX } action; + + if (pOldColumn && pNewColumn) { + if (IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) { + return TSDB_CODE_SUCCESS; + } else if (IS_IDX_ON(pOldColumn) && !IS_IDX_ON(pNewColumn)) { + action = DROP_INDEX; + } else if (!IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) { + action = ADD_INDEX; + } else { + return TSDB_CODE_SUCCESS; + } + } else if (pOldColumn) { + if (IS_IDX_ON(pOldColumn)) { + action = DROP_INDEX; + } else { + return TSDB_CODE_SUCCESS; + } + } else { + if (IS_IDX_ON(pNewColumn)) { + action = ADD_INDEX; + } else { + return TSDB_CODE_SUCCESS; + } + } + + // fetch all child tables + SArray *childTables = 0; + code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &childTables); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + return code; + } + + // do drop or add index + for (int32_t i = 0; i < taosArrayGetSize(childTables); i++) { + int64_t uid = *(int64_t *)taosArrayGet(childTables, i); + + // fetch child entry + SMetaEntry *pChildEntry = NULL; + code = metaFetchEntryByUid(pMeta, uid, &pChildEntry); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + taosArrayDestroy(childTables); + return code; + } + + STagIdxKey *pTagIdxKey = NULL; + int32_t tagIdxKeySize = 0; + + if (action == ADD_INDEX) { + code = metaFetchTagIdxKey(pMeta, pChildEntry, pNewColumn, &pTagIdxKey, &tagIdxKeySize); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + taosArrayDestroy(childTables); + metaFetchEntryFree(&pChildEntry); + return code; + } + + code = tdbTbInsert(pMeta->pTagIdx, pTagIdxKey, tagIdxKeySize, NULL, 0, pMeta->txn); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + taosArrayDestroy(childTables); + metaFetchEntryFree(&pChildEntry); + metaFetchTagIdxKeyFree(&pTagIdxKey); + return code; + } + } else { + code = metaFetchTagIdxKey(pMeta, pChildEntry, pOldColumn, &pTagIdxKey, &tagIdxKeySize); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + taosArrayDestroy(childTables); + metaFetchEntryFree(&pChildEntry); + return code; + } + + code = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, tagIdxKeySize, pMeta->txn); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + taosArrayDestroy(childTables); + metaFetchEntryFree(&pChildEntry); + metaFetchTagIdxKeyFree(&pTagIdxKey); + return code; + } + } + + metaFetchTagIdxKeyFree(&pTagIdxKey); + metaFetchEntryFree(&pChildEntry); + } + + taosArrayDestroy(childTables); + return code; +} + +static int32_t metaUpdateSuperTableTagSchema(SMeta *pMeta, const SMetaHandleParam *pParam) { + int32_t code = TSDB_CODE_SUCCESS; + const SMetaEntry *pEntry = pParam->pEntry; + const SMetaEntry *pOldEntry = pParam->pOldEntry; + const SSchemaWrapper *pNewTagSchema = &pEntry->stbEntry.schemaTag; + const SSchemaWrapper *pOldTagSchema = &pOldEntry->stbEntry.schemaTag; + + int32_t iOld = 0, iNew = 0; + for (; iOld < pOldTagSchema->nCols && iNew < pNewTagSchema->nCols;) { + SSchema *pOldColumn = pOldTagSchema->pSchema + iOld; + SSchema *pNewColumn = pNewTagSchema->pSchema + iNew; + + if (pOldColumn->colId == pNewColumn->colId) { + code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, pNewColumn); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + return code; + } + + iOld++; + iNew++; + } else if (pOldColumn->colId < pNewColumn->colId) { + code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, NULL); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + return code; + } + + iOld++; + } else { + code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, NULL, pNewColumn); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + return code; + } + + iNew++; + } + } + + for (; iOld < pOldTagSchema->nCols; iOld++) { + SSchema *pOldColumn = pOldTagSchema->pSchema + iOld; + code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, NULL); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + return code; + } + } + + for (; iNew < pNewTagSchema->nCols; iNew++) { + SSchema *pNewColumn = pNewTagSchema->pSchema + iNew; + code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, NULL, pNewColumn); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + return code; + } + } + + return code; +} + +static int32_t metaSchemaTableUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) { + int32_t code = TSDB_CODE_SUCCESS; + + const SMetaEntry *pEntry = pParam->pEntry; + const SMetaEntry *pOldEntry = pParam->pOldEntry; + + if (NULL == pOldEntry) { + return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA); + } + + if (pEntry->type == TSDB_NORMAL_TABLE) { + // check row schema + if (pOldEntry->ntbEntry.schemaRow.version != pEntry->ntbEntry.schemaRow.version) { + return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA); + } + } else if (pEntry->type == TSDB_SUPER_TABLE) { + // check row schema + if (pOldEntry->stbEntry.schemaRow.version != pEntry->stbEntry.schemaRow.version) { + return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA); + } + + // check tag schema + code = metaUpdateSuperTableTagSchema(pMeta, pParam); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + return code; + } + + } else { + return TSDB_CODE_INVALID_PARA; + } + + return TSDB_CODE_SUCCESS; +} + +static int32_t metaSchemaTableDelete(SMeta *pMeta, const SMetaHandleParam *pEntry) { + // TODO + return TSDB_CODE_SUCCESS; +} + +// Uid Index +static void metaBuildEntryInfo(const SMetaEntry *pEntry, SMetaInfo *pInfo) { + pInfo->uid = pEntry->uid; + pInfo->version = pEntry->version; + if (pEntry->type == TSDB_SUPER_TABLE) { + pInfo->suid = pEntry->uid; + pInfo->skmVer = pEntry->stbEntry.schemaRow.version; + } else if (pEntry->type == TSDB_CHILD_TABLE) { + pInfo->suid = pEntry->ctbEntry.suid; + pInfo->skmVer = 0; + } else if (pEntry->type == TSDB_NORMAL_TABLE) { + pInfo->suid = 0; + pInfo->skmVer = pEntry->ntbEntry.schemaRow.version; + } +} + +static int32_t metaUidIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) { + int32_t code = TSDB_CODE_SUCCESS; + int32_t vgId = TD_VID(pMeta->pVnode); + + const SMetaEntry *pEntry = pParam->pEntry; + + // update cache + SMetaInfo info = {0}; + metaBuildEntryInfo(pEntry, &info); + code = metaCacheUpsert(pMeta, &info); + if (code) { + metaErr(vgId, code); + } + + // put to tdb + SUidIdxVal value = { + .suid = info.suid, + .skmVer = info.skmVer, + .version = pEntry->version, + }; + if (META_TABLE_OP_INSERT == op) { + code = tdbTbInsert(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), &value, sizeof(value), pMeta->txn); + } else if (META_TABLE_OP_UPDATA == op) { + code = tdbTbUpsert(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), &value, sizeof(value), pMeta->txn); + } + return code; +} + +static int32_t metaUidIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) { + return metaUidIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT); +} + +static int32_t metaUidIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) { + return metaUidIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA); +} + +static int32_t metaUidIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) { + int32_t code = 0; + + const SMetaEntry *pEntry = pParam->pOldEntry; + + // 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 SMetaHandleParam *pParam, EMetaTableOp op) { + int32_t code = TSDB_CODE_SUCCESS; + + const SMetaEntry *pEntry = pParam->pEntry; + + if (META_TABLE_OP_INSERT == op) { + code = tdbTbInsert(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, &pEntry->uid, sizeof(pEntry->uid), + pMeta->txn); + } else if (META_TABLE_OP_UPDATA == op) { + code = tdbTbUpsert(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, &pEntry->uid, sizeof(pEntry->uid), + pMeta->txn); + } else { + code = TSDB_CODE_INVALID_PARA; + } + return code; +} + +static int32_t metaNameIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) { + int32_t code = TSDB_CODE_SUCCESS; + return metaNameIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT); +} + +static int32_t metaNameIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) { + return metaNameIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA); +} + +static int32_t metaNameIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) { + int32_t code = TSDB_CODE_SUCCESS; + + const SMetaEntry *pEntry = pParam->pOldEntry; + code = tdbTbDelete(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, pMeta->txn); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + } + return code; +} + +// Suid Index +static int32_t metaSUidIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) { + const SMetaEntry *pEntry = pParam->pEntry; + + int32_t code = tdbTbInsert(pMeta->pSuidIdx, &pEntry->uid, sizeof(pEntry->uid), NULL, 0, pMeta->txn); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + } + return code; +} + +static int32_t metaSUidIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) { + const SMetaEntry *pEntry = pParam->pOldEntry; + + 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 SMetaHandleParam *pParam, EMetaTableOp op) { + int32_t code = TSDB_CODE_SUCCESS; + + const SMetaEntry *pEntry = pParam->pEntry; + + 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 SMetaHandleParam *pParam) { + return metaChildIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT); +} + +static int32_t metaChildIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) { + const SMetaEntry *pEntry = pParam->pEntry; + const SMetaEntry *pOldEntry = pParam->pOldEntry; + const SMetaEntry *pSuperEntry = pParam->pSuperEntry; + + const STag *pNewTags = (const STag *)pEntry->ctbEntry.pTags; + const STag *pOldTags = (const STag *)pOldEntry->ctbEntry.pTags; + if (pNewTags->len != pOldTags->len || memcmp(pNewTags, pOldTags, pNewTags->len)) { + return metaChildIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA); + } + return 0; +} + +static int32_t metaChildIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) { + const SMetaEntry *pEntry = pParam->pOldEntry; + + SCtbIdxKey key = { + .suid = pEntry->ctbEntry.suid, + .uid = pEntry->uid, + }; + return tdbTbDelete(pMeta->pCtbIdx, &key, sizeof(key), pMeta->txn); +} + +// Tag Index +static int32_t metaFetchTagIdxKey(SMeta *pMeta, const SMetaEntry *pEntry, const SSchema *pTagColumn, + STagIdxKey **ppTagIdxKey, int32_t *pTagIdxKeySize) { + int32_t code = TSDB_CODE_SUCCESS; + + STagIdxKey *pTagIdxKey = NULL; + int32_t nTagIdxKey; + const void *pTagData = NULL; + int32_t nTagData = 0; + + STagVal tagVal = { + .cid = pTagColumn->colId, + }; + + if (tTagGet((const STag *)pEntry->ctbEntry.pTags, &tagVal)) { + if (IS_VAR_DATA_TYPE(pTagColumn->type)) { + pTagData = tagVal.pData; + nTagData = (int32_t)tagVal.nData; + } else { + pTagData = &(tagVal.i64); + nTagData = tDataTypes[pTagColumn->type].bytes; + } + } else { + if (!IS_VAR_DATA_TYPE(pTagColumn->type)) { + nTagData = tDataTypes[pTagColumn->type].bytes; + } + } + + code = metaCreateTagIdxKey(pEntry->ctbEntry.suid, pTagColumn->colId, pTagData, nTagData, pTagColumn->type, + pEntry->uid, &pTagIdxKey, &nTagIdxKey); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + return code; + } + + *ppTagIdxKey = pTagIdxKey; + *pTagIdxKeySize = nTagIdxKey; + return code; +} + +static void metaFetchTagIdxKeyFree(STagIdxKey **ppTagIdxKey) { + metaDestroyTagIdxKey(*ppTagIdxKey); + *ppTagIdxKey = NULL; +} + +static int32_t metaTagIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) { + int32_t code = TSDB_CODE_SUCCESS; + + const SMetaEntry *pEntry = pParam->pEntry; + const SMetaEntry *pSuperEntry = pParam->pSuperEntry; + + const SSchemaWrapper *pTagSchema = &pSuperEntry->stbEntry.schemaTag; + if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) { + const SSchema *pTagColumn = &pTagSchema->pSchema[0]; + + STagVal tagVal = { + .cid = pTagColumn->colId, + }; + + const void *pTagData = pEntry->ctbEntry.pTags; + int32_t nTagData = ((const STag *)pEntry->ctbEntry.pTags)->len; + code = metaSaveJsonVarToIdx(pMeta, pEntry, pTagColumn); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + } + } else { + for (int32_t i = 0; i < pTagSchema->nCols; i++) { + STagIdxKey *pTagIdxKey = NULL; + int32_t nTagIdxKey; + const SSchema *pTagColumn = &pTagSchema->pSchema[i]; + + if (!IS_IDX_ON(pTagColumn)) { + continue; + } + + code = metaFetchTagIdxKey(pMeta, pEntry, pTagColumn, &pTagIdxKey, &nTagIdxKey); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + return code; + } + + code = tdbTbInsert(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, NULL, 0, pMeta->txn); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + metaFetchTagIdxKeyFree(&pTagIdxKey); + return code; + } + metaFetchTagIdxKeyFree(&pTagIdxKey); + } + } + return code; +} + +static int32_t metaTagIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) { + int32_t code = TSDB_CODE_SUCCESS; + + const SMetaEntry *pEntry = pParam->pEntry; + const SMetaEntry *pOldEntry = pParam->pOldEntry; + const SMetaEntry *pSuperEntry = pParam->pSuperEntry; + const SSchemaWrapper *pTagSchema = &pSuperEntry->stbEntry.schemaTag; + const STag *pNewTags = (const STag *)pEntry->ctbEntry.pTags; + const STag *pOldTags = (const STag *)pOldEntry->ctbEntry.pTags; + + if (pNewTags->len == pOldTags->len && !memcmp(pNewTags, pOldTags, pNewTags->len)) { + return code; + } + + if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) { + code = metaDelJsonVarFromIdx(pMeta, pOldEntry, &pTagSchema->pSchema[0]); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + return code; + } + + code = metaSaveJsonVarToIdx(pMeta, pEntry, &pTagSchema->pSchema[0]); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + return code; + } + } else { + for (int32_t i = 0; i < pTagSchema->nCols; i++) { + const SSchema *pTagColumn = &pTagSchema->pSchema[i]; + + if (!IS_IDX_ON(pTagColumn)) { + continue; + } + + STagIdxKey *pOldTagIdxKey = NULL; + int32_t oldTagIdxKeySize = 0; + STagIdxKey *pNewTagIdxKey = NULL; + int32_t newTagIdxKeySize = 0; + + code = metaFetchTagIdxKey(pMeta, pOldEntry, pTagColumn, &pOldTagIdxKey, &oldTagIdxKeySize); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + return code; + } + + code = metaFetchTagIdxKey(pMeta, pEntry, pTagColumn, &pNewTagIdxKey, &newTagIdxKeySize); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + metaFetchTagIdxKeyFree(&pOldTagIdxKey); + return code; + } + + if (tagIdxKeyCmpr(pOldTagIdxKey, oldTagIdxKeySize, pNewTagIdxKey, newTagIdxKeySize)) { + code = tdbTbDelete(pMeta->pTagIdx, pOldTagIdxKey, oldTagIdxKeySize, pMeta->txn); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + metaFetchTagIdxKeyFree(&pOldTagIdxKey); + metaFetchTagIdxKeyFree(&pNewTagIdxKey); + return code; + } + + code = tdbTbInsert(pMeta->pTagIdx, pNewTagIdxKey, newTagIdxKeySize, NULL, 0, pMeta->txn); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + metaFetchTagIdxKeyFree(&pOldTagIdxKey); + metaFetchTagIdxKeyFree(&pNewTagIdxKey); + return code; + } + } + + metaFetchTagIdxKeyFree(&pOldTagIdxKey); + metaFetchTagIdxKeyFree(&pNewTagIdxKey); + } + } + return code; +} + +static int32_t metaTagIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) { + int32_t code = TSDB_CODE_SUCCESS; + + const SMetaEntry *pEntry = pParam->pEntry; + const SMetaEntry *pChild = pParam->pOldEntry; + const SMetaEntry *pSuper = pParam->pSuperEntry; + const SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag; + const SSchema *pTagColumn = NULL; + const STag *pTags = (const STag *)pChild->ctbEntry.pTags; + + if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) { + pTagColumn = &pTagSchema->pSchema[0]; + code = metaDelJsonVarFromIdx(pMeta, pChild, pTagColumn); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + } + } else { + for (int32_t i = 0; i < pTagSchema->nCols; i++) { + pTagColumn = &pTagSchema->pSchema[i]; + if (!IS_IDX_ON(pTagColumn)) { + continue; + } + + STagIdxKey *pTagIdxKey = NULL; + int32_t nTagIdxKey; + + code = metaFetchTagIdxKey(pMeta, pChild, pTagColumn, &pTagIdxKey, &nTagIdxKey); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + return code; + } + + code = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, pMeta->txn); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + metaFetchTagIdxKeyFree(&pTagIdxKey); + return code; + } + metaFetchTagIdxKeyFree(&pTagIdxKey); + } + } + return code; +} + +// Btime Index +static int32_t metaBtimeIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) { + int32_t code = TSDB_CODE_SUCCESS; + + const SMetaEntry *pEntry; + if (META_TABLE_OP_DELETE == op) { + pEntry = pParam->pOldEntry; + } else { + pEntry = pParam->pEntry; + } + + 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; + } + + if (META_TABLE_OP_INSERT == op) { + code = tdbTbInsert(pMeta->pBtimeIdx, &key, sizeof(key), NULL, 0, pMeta->txn); + } else if (META_TABLE_OP_UPDATA == op) { + code = tdbTbUpsert(pMeta->pBtimeIdx, &key, sizeof(key), NULL, 0, pMeta->txn); + } else if (META_TABLE_OP_DELETE == op) { + code = tdbTbDelete(pMeta->pBtimeIdx, &key, sizeof(key), pMeta->txn); + } else { + code = TSDB_CODE_INVALID_PARA; + } + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + } + return code; +} + +static int32_t metaBtimeIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) { + return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT); +} + +static int32_t metaBtimeIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) { + return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA); +} + +static int32_t metaBtimeIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) { + return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_DELETE); +} + +// TTL Index +static int32_t metaTtlIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) { + const SMetaEntry *pEntry = pParam->pEntry; + + STtlUpdTtlCtx ctx = { + .uid = pEntry->uid, + .pTxn = pMeta->txn, + }; + if (TSDB_CHILD_TABLE == pEntry->type) { + ctx.ttlDays = pEntry->ctbEntry.ttlDays; + ctx.changeTimeMs = pEntry->ctbEntry.btime; + } else if (TSDB_NORMAL_TABLE == pEntry->type) { + ctx.ttlDays = pEntry->ntbEntry.ttlDays; + ctx.changeTimeMs = pEntry->ntbEntry.btime; + } else { + return TSDB_CODE_INVALID_PARA; + } + + int32_t ret = ttlMgrInsertTtl(pMeta->pTtlMgr, &ctx); + if (ret < 0) { + metaError("vgId:%d, failed to insert ttl, uid: %" PRId64 " %s", TD_VID(pMeta->pVnode), pEntry->uid, tstrerror(ret)); + } + return TSDB_CODE_SUCCESS; +} + +static int32_t metaTtlIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) { + return metaTtlIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT); +} + +static int32_t metaTtlIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam); + +static int32_t metaTtlIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) { + int32_t code = TSDB_CODE_SUCCESS; + + const SMetaEntry *pEntry = pParam->pEntry; + const SMetaEntry *pOldEntry = pParam->pOldEntry; + + if ((pEntry->type == TSDB_CHILD_TABLE && pOldEntry->ctbEntry.ttlDays != pEntry->ctbEntry.ttlDays) || + (pEntry->type == TSDB_NORMAL_TABLE && pOldEntry->ntbEntry.ttlDays != pEntry->ntbEntry.ttlDays)) { + code = metaTtlIdxDelete(pMeta, pParam); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + } + + code = metaTtlIdxInsert(pMeta, pParam); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + } + } + + return TSDB_CODE_SUCCESS; +} + +static int32_t metaTtlIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) { + int32_t code = TSDB_CODE_SUCCESS; + + const SMetaEntry *pEntry = pParam->pOldEntry; + STtlDelTtlCtx ctx = { + .uid = pEntry->uid, + .pTxn = pMeta->txn, + }; + + if (TSDB_CHILD_TABLE == pEntry->type) { + ctx.ttlDays = pEntry->ctbEntry.ttlDays; + } else if (TSDB_NORMAL_TABLE == pEntry->type) { + ctx.ttlDays = pEntry->ntbEntry.ttlDays; + } else { + code = TSDB_CODE_INVALID_PARA; + } + + if (TSDB_CODE_SUCCESS == code) { + int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx); + if (ret < 0) { + metaError("vgId:%d, failed to delete ttl, uid: %" PRId64 " %s", TD_VID(pMeta->pVnode), pEntry->uid, + tstrerror(ret)); + } + } + return code; +} + +static int32_t (*metaTableOpFn[META_TABLE_MAX][META_TABLE_OP_MAX])(SMeta *pMeta, const SMetaHandleParam *pParam) = + { + [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) { + int32_t code = TSDB_CODE_SUCCESS; + + SMetaTableOp ops[] = { + {META_ENTRY_TABLE, META_TABLE_OP_INSERT}, // + {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA}, // TODO: here should be insert + {META_UID_IDX, META_TABLE_OP_INSERT}, // + {META_NAME_IDX, META_TABLE_OP_INSERT}, // + {META_SUID_IDX, META_TABLE_OP_INSERT}, // + }; + + for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) { + SMetaTableOp *op = &ops[i]; + const SMetaHandleParam param = { + .pEntry = pEntry, + }; + + code = metaTableOpFn[op->table][op->op](pMeta, ¶m); + if (TSDB_CODE_SUCCESS != code) { + metaErr(TD_VID(pMeta->pVnode), code); + return code; + } + } + + return code; +} +static int32_t metaHandleSuperTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) { + int32_t code = TSDB_CODE_SUCCESS; + + metaWLock(pMeta); + code = metaHandleSuperTableCreateImpl(pMeta, pEntry); + metaULock(pMeta); + + if (TSDB_CODE_SUCCESS == code) { + pMeta->pVnode->config.vndStats.numOfSTables++; + + metaInfo("vgId:%d, %s success, version:%" PRId64 " type:%d uid:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), + __func__, pEntry->version, pEntry->type, pEntry->uid, pEntry->name); + } else { + metaErr(TD_VID(pMeta->pVnode), code); + } + return code; +} + +static int32_t metaHandleNormalTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) { + int32_t code = TSDB_CODE_SUCCESS; + + SMetaTableOp ops[] = { + {META_ENTRY_TABLE, META_TABLE_OP_INSERT}, // + {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA}, // TODO: need to be insert + {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}, // + }; + + for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) { + SMetaTableOp *op = &ops[i]; + + SMetaHandleParam param = { + .pEntry = pEntry, + }; + + code = metaTableOpFn[op->table][op->op](pMeta, ¶m); + if (TSDB_CODE_SUCCESS != code) { + metaErr(TD_VID(pMeta->pVnode), code); + return code; + } + } + + return code; +} +static int32_t metaHandleNormalTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) { + int32_t code = TSDB_CODE_SUCCESS; + + // update TDB + metaWLock(pMeta); + code = metaHandleNormalTableCreateImpl(pMeta, pEntry); + metaULock(pMeta); + + // update other stuff + if (TSDB_CODE_SUCCESS == code) { + pMeta->pVnode->config.vndStats.numOfNTables++; + pMeta->pVnode->config.vndStats.numOfNTimeSeries += pEntry->ntbEntry.schemaRow.nCols - 1; + + if (!TSDB_CACHE_NO(pMeta->pVnode->config)) { + int32_t rc = tsdbCacheNewTable(pMeta->pVnode->pTsdb, pEntry->uid, -1, &pEntry->ntbEntry.schemaRow); + if (rc < 0) { + metaError("vgId:%d, failed to create table:%s since %s", TD_VID(pMeta->pVnode), pEntry->name, tstrerror(rc)); + } + } + } else { + metaErr(TD_VID(pMeta->pVnode), code); + } + return code; +} + +static int32_t metaHandleChildTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry, const SMetaEntry *pSuperEntry) { + int32_t code = TSDB_CODE_SUCCESS; + + 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]; + + SMetaHandleParam param = { + .pEntry = pEntry, + .pSuperEntry = pSuperEntry, + }; + + code = metaTableOpFn[op->table][op->op](pMeta, ¶m); + if (TSDB_CODE_SUCCESS != code) { + metaErr(TD_VID(pMeta->pVnode), code); + return code; + } + } + + if (TSDB_CODE_SUCCESS == code) { + metaUpdateStbStats(pMeta, pSuperEntry->uid, 1, 0); + int32_t ret = metaUidCacheClear(pMeta, pSuperEntry->uid); + if (ret < 0) { + metaErr(TD_VID(pMeta->pVnode), ret); + } + + ret = metaTbGroupCacheClear(pMeta, pSuperEntry->uid); + if (ret < 0) { + metaErr(TD_VID(pMeta->pVnode), ret); + } + } + return code; +} + +static int32_t metaHandleChildTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) { + int32_t code = TSDB_CODE_SUCCESS; + SMetaEntry *pSuperEntry = NULL; + + // get the super table entry + code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + return code; + } + + // update TDB + metaWLock(pMeta); + code = metaHandleChildTableCreateImpl(pMeta, pEntry, pSuperEntry); + metaULock(pMeta); + + // update other stuff + if (TSDB_CODE_SUCCESS == code) { + pMeta->pVnode->config.vndStats.numOfCTables++; + + if (!metaTbInFilterCache(pMeta, pSuperEntry->name, 1)) { + int32_t nCols = 0; + int32_t ret = metaGetStbStats(pMeta->pVnode, pSuperEntry->uid, 0, &nCols); + if (ret < 0) { + metaErr(TD_VID(pMeta->pVnode), ret); + } + pMeta->pVnode->config.vndStats.numOfNTimeSeries += (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)); + } + } + + } else { + metaErr(TD_VID(pMeta->pVnode), code); + } + + metaFetchEntryFree(&pSuperEntry); + return code; +} + +static int32_t metaHandleNormalTableDropImpl(SMeta *pMeta, SMetaHandleParam *pParam) { + 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_TTL_IDX, META_TABLE_OP_DELETE}, // + + // {META_SCHEMA_TABLE, META_TABLE_OP_DELETE}, // + }; + + for (int32_t i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) { + SMetaTableOp *op = &ops[i]; + code = metaTableOpFn[op->table][op->op](pMeta, pParam); + if (code) { + const SMetaEntry *pEntry = pParam->pEntry; + metaErr(TD_VID(pMeta->pVnode), code); + } + } + + return code; +} + +static int32_t metaHandleNormalTableDrop(SMeta *pMeta, const SMetaEntry *pEntry) { + int32_t code = TSDB_CODE_SUCCESS; + SMetaEntry *pOldEntry = NULL; + + // fetch the entry + code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + return code; + } + + SMetaHandleParam param = { + .pEntry = pEntry, + .pOldEntry = pOldEntry, + }; + + // do the drop + metaWLock(pMeta); + code = metaHandleNormalTableDropImpl(pMeta, ¶m); + metaULock(pMeta); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + metaFetchEntryFree(&pOldEntry); + return code; + } + + // update other stuff + pMeta->pVnode->config.vndStats.numOfNTables--; + pMeta->pVnode->config.vndStats.numOfNTimeSeries -= (pOldEntry->ntbEntry.schemaRow.nCols - 1); + +#if 0 + if (tbUids) { + if (taosArrayPush(tbUids, &uid) == NULL) { + rc = terrno; + goto _exit; + } + } +#endif + + if (!TSDB_CACHE_NO(pMeta->pVnode->config)) { + int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pOldEntry->uid, 0, NULL); + if (ret < 0) { + metaErr(TD_VID(pMeta->pVnode), ret); + } + } + + metaFetchEntryFree(&pOldEntry); + return code; +} + +static int32_t metaHandleChildTableDropImpl(SMeta *pMeta, const SMetaHandleParam *pParam, bool superDropped) { + int32_t code = TSDB_CODE_SUCCESS; + + const SMetaEntry *pEntry = pParam->pEntry; + const SMetaEntry *pChild = pParam->pOldEntry; + const SMetaEntry *pSuper = pParam->pSuperEntry; + + SMetaTableOp ops[] = { + {META_ENTRY_TABLE, META_TABLE_OP_DELETE}, // + {META_UID_IDX, META_TABLE_OP_DELETE}, // + {META_NAME_IDX, META_TABLE_OP_DELETE}, // + {META_CHILD_IDX, META_TABLE_OP_DELETE}, // + {META_TAG_IDX, META_TABLE_OP_DELETE}, // + {META_BTIME_IDX, META_TABLE_OP_DELETE}, // + {META_TTL_IDX, META_TABLE_OP_DELETE}, // + }; + + for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) { + SMetaTableOp *op = &ops[i]; + + if (op->table == META_ENTRY_TABLE && superDropped) { + continue; + } + + code = metaTableOpFn[op->table][op->op](pMeta, pParam); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + return code; + } + } + + --pMeta->pVnode->config.vndStats.numOfCTables; + metaUpdateStbStats(pMeta, pParam->pSuperEntry->uid, -1, 0); + int32_t ret = metaUidCacheClear(pMeta, pSuper->uid); + if (ret < 0) { + metaErr(TD_VID(pMeta->pVnode), ret); + } + + ret = metaTbGroupCacheClear(pMeta, pSuper->uid); + if (ret < 0) { + metaErr(TD_VID(pMeta->pVnode), ret); + } + return code; +} + +static int32_t metaHandleChildTableDrop(SMeta *pMeta, const SMetaEntry *pEntry, bool superDropped) { + int32_t code = TSDB_CODE_SUCCESS; + SMetaEntry *pChild = NULL; + SMetaEntry *pSuper = NULL; + + // fetch old entry + code = metaFetchEntryByUid(pMeta, pEntry->uid, &pChild); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + return code; + } + + // fetch super entry + code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + metaFetchEntryFree(&pChild); + return code; + } + + SMetaHandleParam param = { + .pEntry = pEntry, + .pOldEntry = pChild, + .pSuperEntry = pSuper, + }; + + // do the drop + metaWLock(pMeta); + code = metaHandleChildTableDropImpl(pMeta, ¶m, superDropped); + metaULock(pMeta); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + metaFetchEntryFree(&pChild); + metaFetchEntryFree(&pSuper); + return code; + } + + // do other stuff + if (!metaTbInFilterCache(pMeta, pSuper->name, 1)) { + int32_t nCols = 0; + SVnodeStats *pStats = &pMeta->pVnode->config.vndStats; + if (metaGetStbStats(pMeta->pVnode, pSuper->uid, NULL, &nCols) == 0) { + pStats->numOfTimeSeries -= nCols - 1; + } + } + + if (!TSDB_CACHE_NO(pMeta->pVnode->config)) { + int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pChild->uid, pSuper->uid, NULL); + if (ret < 0) { + metaErr(TD_VID(pMeta->pVnode), ret); + } + } + +#if 0 + if (tbUids) { + if (taosArrayPush(tbUids, &uid) == NULL) { + rc = terrno; + goto _exit; + } + } + + if ((type == TSDB_CHILD_TABLE) && tbUid) { + *tbUid = uid; + } +#endif + metaFetchEntryFree(&pChild); + metaFetchEntryFree(&pSuper); + return code; +} + +static int32_t metaGetChildUidsOfSuperTable(SMeta *pMeta, tb_uid_t suid, SArray **childList) { + int32_t code = TSDB_CODE_SUCCESS; + void *key = NULL; + int32_t keySize = 0; + int32_t c; + + *childList = taosArrayInit(64, sizeof(tb_uid_t)); + if (*childList == NULL) { + return terrno; + } + + TBC *cursor = NULL; + code = tdbTbcOpen(pMeta->pCtbIdx, &cursor, NULL); + if (code) { + taosArrayDestroy(*childList); + *childList = NULL; + return code; + } + + int32_t rc = tdbTbcMoveTo(cursor, + &(SCtbIdxKey){ + .suid = suid, + .uid = INT64_MIN, + }, + sizeof(SCtbIdxKey), &c); + if (rc < 0) { + tdbTbcClose(cursor); + return 0; + } + + for (;;) { + if (tdbTbcNext(cursor, &key, &keySize, NULL, NULL) < 0) { + break; + } + + if (((SCtbIdxKey *)key)->suid < suid) { + continue; + } else if (((SCtbIdxKey *)key)->suid > suid) { + break; + } + + if (taosArrayPush(*childList, &(((SCtbIdxKey *)key)->uid)) == NULL) { + tdbFreeClear(key); + tdbTbcClose(cursor); + taosArrayDestroy(*childList); + *childList = NULL; + return terrno; + } + } + + tdbTbcClose(cursor); + tdbFreeClear(key); + return code; +} + +static int32_t metaHandleSuperTableDropImpl(SMeta *pMeta, const SMetaHandleParam *pParam) { + int32_t code = TSDB_CODE_SUCCESS; + const SMetaEntry *pEntry = pParam->pEntry; + + SMetaTableOp ops[] = { + {META_ENTRY_TABLE, META_TABLE_OP_DELETE}, // + {META_UID_IDX, META_TABLE_OP_DELETE}, // + {META_NAME_IDX, META_TABLE_OP_DELETE}, // + {META_SUID_IDX, META_TABLE_OP_DELETE}, // + + // {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA}, // TODO: here should be insert + }; + + for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) { + SMetaTableOp *op = &ops[i]; + + code = metaTableOpFn[op->table][op->op](pMeta, pParam); + if (TSDB_CODE_SUCCESS != code) { + metaErr(TD_VID(pMeta->pVnode), code); + return code; + } + } + + int32_t ret = metaStatsCacheDrop(pMeta, pEntry->uid); + if (ret < 0) { + metaErr(TD_VID(pMeta->pVnode), ret); + } + return code; +} + +static int32_t metaHandleNormalTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) { + int32_t code = TSDB_CODE_SUCCESS; + + const SMetaEntry *pEntry = pParam->pEntry; + + SMetaTableOp ops[] = { + {META_ENTRY_TABLE, META_TABLE_OP_UPDATA}, // + {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA}, // + {META_UID_IDX, META_TABLE_OP_UPDATA}, // + {META_TTL_IDX, META_TABLE_OP_UPDATA}, // + }; + for (int32_t i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) { + SMetaTableOp *op = &ops[i]; + code = metaTableOpFn[op->table][op->op](pMeta, pParam); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + return code; + } + } +#if 0 + if (metaUpdateChangeTime(pMeta, entry.uid, pAlterTbReq->ctimeMs) < 0) { + metaError("vgId:%d, failed to update change time:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid); + } +#endif + return code; +} + +static int32_t metaHandleChildTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) { + int32_t code = TSDB_CODE_SUCCESS; + + const SMetaEntry *pEntry = pParam->pEntry; + const SMetaEntry *pOldEntry = pParam->pOldEntry; + const SMetaEntry *pSuperEntry = pParam->pSuperEntry; + + SMetaTableOp ops[] = { + {META_ENTRY_TABLE, META_TABLE_OP_UPDATA}, // + {META_UID_IDX, META_TABLE_OP_UPDATA}, // + {META_TAG_IDX, META_TABLE_OP_UPDATA}, // + {META_CHILD_IDX, META_TABLE_OP_UPDATA}, // + {META_TTL_IDX, META_TABLE_OP_UPDATA}, // + }; + + for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) { + SMetaTableOp *op = &ops[i]; + code = metaTableOpFn[op->table][op->op](pMeta, pParam); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + return code; + } + } + + if (metaUidCacheClear(pMeta, pSuperEntry->uid) < 0) { + metaErr(TD_VID(pMeta->pVnode), code); + } + + if (metaTbGroupCacheClear(pMeta, pSuperEntry->uid) < 0) { + metaErr(TD_VID(pMeta->pVnode), code); + } + return code; +#if 0 + if (metaUpdateChangeTime(pMeta, ctbEntry.uid, pReq->ctimeMs) < 0) { + metaError("meta/table: failed to update change time:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid); + } +#endif +} + +static int32_t metaHandleSuperTableUpdateImpl(SMeta *pMeta, SMetaHandleParam *pParam) { + int32_t code = TSDB_CODE_SUCCESS; + + const SMetaEntry *pEntry = pParam->pEntry; + const SMetaEntry *pOldEntry = pParam->pOldEntry; + + SMetaTableOp ops[] = { + {META_ENTRY_TABLE, META_TABLE_OP_UPDATA}, // + {META_UID_IDX, META_TABLE_OP_UPDATA}, // + {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA}, // + }; + + for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) { + SMetaTableOp *op = &ops[i]; + code = metaTableOpFn[op->table][op->op](pMeta, pParam); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + return code; + } + } + + return code; +} + +static int32_t metaHandleSuperTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) { + int32_t code = TSDB_CODE_SUCCESS; + + SMetaEntry *pOldEntry = NULL; + + code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + return code; + } + + SMetaHandleParam param = { + .pEntry = pEntry, + .pOldEntry = pOldEntry, + }; + metaWLock(pMeta); + code = metaHandleSuperTableUpdateImpl(pMeta, ¶m); + metaULock(pMeta); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + metaFetchEntryFree(&pOldEntry); + return code; + } + + int nCols = pEntry->stbEntry.schemaRow.nCols; + int onCols = pOldEntry->stbEntry.schemaRow.nCols; + int32_t deltaCol = nCols - onCols; + bool updStat = deltaCol != 0 && !metaTbInFilterCache(pMeta, pEntry->name, 1); + + if (!TSDB_CACHE_NO(pMeta->pVnode->config)) { + STsdb *pTsdb = pMeta->pVnode->pTsdb; + SArray *uids = NULL; /*taosArrayInit(8, sizeof(int64_t)); + if (uids == NULL) { + metaErr(TD_VID(pMeta->pVnode), code); + metaFetchEntryFree(&pOldEntry); + return terrno; + }*/ + if (deltaCol == 1) { + int16_t cid = pEntry->stbEntry.schemaRow.pSchema[nCols - 1].colId; + int8_t col_type = pEntry->stbEntry.schemaRow.pSchema[nCols - 1].type; + + code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &uids); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + metaFetchEntryFree(&pOldEntry); + return code; + } + // TAOS_CHECK_RETURN(metaGetSubtables(pMeta, pEntry->uid, uids)); + TAOS_CHECK_RETURN(tsdbCacheNewSTableColumn(pTsdb, uids, cid, col_type)); + } else if (deltaCol == -1) { + int16_t cid = -1; + bool hasPrimaryKey = false; + if (onCols >= 2) { + hasPrimaryKey = (pOldEntry->stbEntry.schemaRow.pSchema[1].flags & COL_IS_KEY) ? true : false; + } + for (int i = 0, j = 0; i < nCols && j < onCols; ++i, ++j) { + if (pEntry->stbEntry.schemaRow.pSchema[i].colId != pOldEntry->stbEntry.schemaRow.pSchema[j].colId) { + cid = pOldEntry->stbEntry.schemaRow.pSchema[j].colId; + break; + } + } + + if (cid != -1) { + code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &uids); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + metaFetchEntryFree(&pOldEntry); + return code; + } + // TAOS_CHECK_RETURN(metaGetSubtables(pMeta, pEntry->uid, uids)); + TAOS_CHECK_RETURN(tsdbCacheDropSTableColumn(pTsdb, uids, cid, hasPrimaryKey)); + } + } + if (uids) taosArrayDestroy(uids); + + tsdbCacheInvalidateSchema(pTsdb, pEntry->uid, -1, pEntry->stbEntry.schemaRow.version); + } + + metaFetchEntryFree(&pOldEntry); + return code; +} + +static int32_t metaHandleChildTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) { + int32_t code = TSDB_CODE_SUCCESS; + + SMetaEntry *pOldEntry = NULL; + SMetaEntry *pSuperEntry = NULL; + + code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + return code; + } + + code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + metaFetchEntryFree(&pOldEntry); + return code; + } + + SMetaHandleParam param = { + .pEntry = pEntry, + .pOldEntry = pOldEntry, + .pSuperEntry = pSuperEntry, + }; + + metaWLock(pMeta); + code = metaHandleChildTableUpdateImpl(pMeta, ¶m); + metaULock(pMeta); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + metaFetchEntryFree(&pOldEntry); + metaFetchEntryFree(&pSuperEntry); + return code; + } + + metaFetchEntryFree(&pOldEntry); + metaFetchEntryFree(&pSuperEntry); + return code; +} + +static int32_t metaHandleNormalTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) { + int32_t code = TSDB_CODE_SUCCESS; + SMetaEntry *pOldEntry = NULL; + + // fetch old entry + code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + return code; + } + + // handle update + SMetaHandleParam param = { + .pEntry = pEntry, + .pOldEntry = pOldEntry, + }; + metaWLock(pMeta); + code = metaHandleNormalTableUpdateImpl(pMeta, ¶m); + metaULock(pMeta); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + metaFetchEntryFree(&pOldEntry); + return code; + } + + // do other stuff + if (!TSDB_CACHE_NO(pMeta->pVnode->config) && + pEntry->ntbEntry.schemaRow.version != pOldEntry->ntbEntry.schemaRow.version) { +#if 0 + { // for add column + int16_t cid = pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].colId; + int8_t col_type = pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].type; + int32_t ret = tsdbCacheNewNTableColumn(pMeta->pVnode->pTsdb, entry.uid, cid, col_type); + if (ret < 0) { + terrno = ret; + goto _err; + } + } + { // for drop column + + if (!TSDB_CACHE_NO(pMeta->pVnode->config)) { + int16_t cid = pColumn->colId; + + if (tsdbCacheDropNTableColumn(pMeta->pVnode->pTsdb, entry.uid, cid, hasPrimayKey) != 0) { + metaError("vgId:%d, failed to drop ntable column:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, + entry.uid); + } + tsdbCacheInvalidateSchema(pMeta->pVnode->pTsdb, 0, entry.uid, pSchema->version); + } + } + } +#endif + tsdbCacheInvalidateSchema(pMeta->pVnode->pTsdb, 0, pEntry->uid, pEntry->ntbEntry.schemaRow.version); + } + metaTimeSeriesNotifyCheck(pMeta); + metaFetchEntryFree(&pOldEntry); + return code; +} + +static int32_t metaHandleSuperTableDrop(SMeta *pMeta, const SMetaEntry *pEntry) { + int32_t code = TSDB_CODE_SUCCESS; + SArray *childList = NULL; + SMetaEntry *pOldEntry = NULL; + + code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + return code; + } + + code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &childList); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + metaFetchEntryFree(&pOldEntry); + return code; + } + + if (tsdbCacheDropSubTables(pMeta->pVnode->pTsdb, childList, pEntry->uid) < 0) { + metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pEntry->name, + pEntry->uid, tstrerror(terrno)); + } + + // loop to drop all child tables + for (int32_t i = 0; i < taosArrayGetSize(childList); i++) { + SMetaEntry childEntry = { + .version = pEntry->version, + .uid = *(tb_uid_t *)taosArrayGet(childList, i), + .type = -TSDB_CHILD_TABLE, + }; + + code = metaHandleChildTableDrop(pMeta, &childEntry, true); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + } + } + + // do drop super table + SMetaHandleParam param = { + .pEntry = pEntry, + .pOldEntry = pOldEntry, + }; + metaWLock(pMeta); + code = metaHandleSuperTableDropImpl(pMeta, ¶m); + metaULock(pMeta); + if (code) { + metaErr(TD_VID(pMeta->pVnode), code); + taosArrayDestroy(childList); + metaFetchEntryFree(&pOldEntry); + return code; + } + + // do other stuff + metaUpdTimeSeriesNum(pMeta); + + // free resource and return + taosArrayDestroy(childList); + metaFetchEntryFree(&pOldEntry); + return code; +} + +int32_t metaHandleEntry2(SMeta *pMeta, const SMetaEntry *pEntry) { + int32_t code = TSDB_CODE_SUCCESS; + int32_t vgId = TD_VID(pMeta->pVnode); + SMetaInfo info = {0}; + int8_t type = pEntry->type > 0 ? pEntry->type : -pEntry->type; + + if (NULL == pMeta || NULL == pEntry) { + metaError("%s failed at %s:%d since invalid parameter", __func__, __FILE__, __LINE__); + return TSDB_CODE_INVALID_PARA; + } + + if (pEntry->type > 0) { + bool isExist = false; + if (TSDB_CODE_SUCCESS == metaGetInfo(pMeta, pEntry->uid, &info, NULL)) { + isExist = true; + } + + switch (type) { + case TSDB_SUPER_TABLE: { + if (isExist) { + code = metaHandleSuperTableUpdate(pMeta, pEntry); + } else { + code = metaHandleSuperTableCreate(pMeta, pEntry); + } + break; + } + case TSDB_CHILD_TABLE: { + if (isExist) { + code = metaHandleChildTableUpdate(pMeta, pEntry); + } else { + code = metaHandleChildTableCreate(pMeta, pEntry); + } + break; + } + case TSDB_NORMAL_TABLE: { + if (isExist) { + code = metaHandleNormalTableUpdate(pMeta, pEntry); + } else { + code = metaHandleNormalTableCreate(pMeta, pEntry); + } + break; + } + default: { + code = TSDB_CODE_INVALID_PARA; + break; + } + } + } else { + switch (type) { + case TSDB_SUPER_TABLE: { + code = metaHandleSuperTableDrop(pMeta, pEntry); + break; + } + case TSDB_CHILD_TABLE: { + code = metaHandleChildTableDrop(pMeta, pEntry, false); + break; + } + case TSDB_NORMAL_TABLE: { + code = metaHandleNormalTableDrop(pMeta, pEntry); + break; + } + default: { + code = TSDB_CODE_INVALID_PARA; + break; + } + } + } + + if (TSDB_CODE_SUCCESS == code) { + pMeta->changed = true; + metaDebug("vgId:%d, %s success, version:%" PRId64 " type:%d uid:%" PRId64 " name:%s", vgId, __func__, + pEntry->version, pEntry->type, pEntry->uid, pEntry->type > 0 ? pEntry->name : ""); + } else { + metaErr(vgId, code); + } + TAOS_RETURN(code); +} diff --git a/source/dnode/vnode/src/meta/metaOpen.c b/source/dnode/vnode/src/meta/metaOpen.c index cc440fd73b..35e761e51e 100644 --- a/source/dnode/vnode/src/meta/metaOpen.c +++ b/source/dnode/vnode/src/meta/metaOpen.c @@ -19,7 +19,7 @@ static int tbDbKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2); static int skmDbKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2); static int ctbIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2); -static int tagIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2); +int tagIdxKeyCmpr(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 smaIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2); static int taskIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2); @@ -330,7 +330,7 @@ static int32_t metaGenerateNewMeta(SMeta **ppMeta) { tdbTbGet(pMeta->pUidIdx, &me.ctbEntry.suid, sizeof(me.ctbEntry.suid), NULL, NULL) != 0) { metaError("vgId:%d failed to get super table uid:%" PRId64 " for child table uid:%" PRId64, TD_VID(pVnode), me.ctbEntry.suid, uid); - } else if (metaHandleEntry(pNewMeta, &me) != 0) { + } else if (metaHandleEntry2(pNewMeta, &me) != 0) { metaError("vgId:%d failed to handle entry, uid:%" PRId64, TD_VID(pVnode), uid); } } @@ -598,7 +598,7 @@ static int ctbIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kL return 0; } -static int tagIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) { +int tagIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) { STagIdxKey *pTagIdxKey1 = (STagIdxKey *)pKey1; STagIdxKey *pTagIdxKey2 = (STagIdxKey *)pKey2; tb_uid_t uid1 = 0, uid2 = 0; diff --git a/source/dnode/vnode/src/meta/metaSnapshot.c b/source/dnode/vnode/src/meta/metaSnapshot.c index b2826ec45a..30a20cd68d 100644 --- a/source/dnode/vnode/src/meta/metaSnapshot.c +++ b/source/dnode/vnode/src/meta/metaSnapshot.c @@ -72,7 +72,6 @@ int32_t metaSnapRead(SMetaSnapReader* pReader, uint8_t** ppData) { int32_t nKey = 0; int32_t nData = 0; STbDbKey key; - SMetaInfo info; *ppData = NULL; for (;;) { @@ -85,8 +84,7 @@ int32_t metaSnapRead(SMetaSnapReader* pReader, uint8_t** ppData) { goto _exit; } - if (key.version < pReader->sver // - || metaGetInfo(pReader->pMeta, key.uid, &info, NULL) == TSDB_CODE_NOT_FOUND) { + if (key.version < pReader->sver) { if (tdbTbcMoveToNext(pReader->pTbc) != 0) { metaTrace("vgId:%d, vnode snapshot meta read data done", TD_VID(pReader->pMeta->pVnode)); } @@ -199,7 +197,7 @@ int32_t metaSnapWrite(SMetaSnapWriter* pWriter, uint8_t* pData, uint32_t nData) code = metaDecodeEntry(pDecoder, &metaEntry); TSDB_CHECK_CODE(code, lino, _exit); - code = metaHandleEntry(pMeta, &metaEntry); + code = metaHandleEntry2(pMeta, &metaEntry); TSDB_CHECK_CODE(code, lino, _exit); _exit: diff --git a/source/dnode/vnode/src/meta/metaTable.c b/source/dnode/vnode/src/meta/metaTable.c index c32d4b30b0..ee37dd1f90 100644 --- a/source/dnode/vnode/src/meta/metaTable.c +++ b/source/dnode/vnode/src/meta/metaTable.c @@ -17,8 +17,18 @@ extern SDmNotifyHandle dmNotifyHdl; -static int metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema); -static int metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema); +int32_t metaAddTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp); +int32_t metaDropTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp); +int32_t metaAlterTableColumnName(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp); +int32_t metaAlterTableColumnBytes(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp); +int32_t metaUpdateTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq); +int32_t metaUpdateTableMultiTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq); +int32_t metaUpdateTableOptions2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq); +int32_t metaUpdateTableColCompress2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq); + +int32_t metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema); + +int32_t metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema); static int metaSaveToTbDb(SMeta *pMeta, const SMetaEntry *pME); static int metaUpdateUidIdx(SMeta *pMeta, const SMetaEntry *pME); static int metaUpdateNameIdx(SMeta *pMeta, const SMetaEntry *pME); @@ -29,30 +39,30 @@ static int metaUpdateCtbIdx(SMeta *pMeta, const SMetaEntry *pME); static int metaUpdateSuidIdx(SMeta *pMeta, const SMetaEntry *pME); static int metaUpdateTagIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry); static int metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type, tb_uid_t *pSuid, int8_t *pSysTbl); -static void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey); +void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey); // opt ins_tables query static int metaUpdateBtimeIdx(SMeta *pMeta, const SMetaEntry *pME); static int metaDeleteBtimeIdx(SMeta *pMeta, const SMetaEntry *pME); static int metaUpdateNcolIdx(SMeta *pMeta, const SMetaEntry *pME); static int metaDeleteNcolIdx(SMeta *pMeta, const SMetaEntry *pME); -static int32_t updataTableColCmpr(SColCmprWrapper *pWp, SSchema *pSchema, int8_t add, uint32_t compress) { +int metaHandleEntry(SMeta *pMeta, const SMetaEntry *pME); + +int32_t updataTableColCmpr(SColCmprWrapper *pWp, SSchema *pSchema, int8_t add, uint32_t compress) { int32_t nCols = pWp->nCols; int32_t ver = pWp->version; if (add) { - SColCmpr *p = taosMemoryCalloc(1, sizeof(SColCmpr) * (nCols + 1)); + SColCmpr *p = taosMemoryRealloc(pWp->pColCmpr, sizeof(SColCmpr) * (nCols + 1)); if (p == NULL) { return terrno; } - - memcpy(p, pWp->pColCmpr, sizeof(SColCmpr) * nCols); + pWp->pColCmpr = p; SColCmpr *pCol = p + nCols; pCol->id = pSchema->colId; pCol->alg = compress; pWp->nCols = nCols + 1; pWp->version = ver; - pWp->pColCmpr = p; } else { for (int32_t i = 0; i < nCols; i++) { SColCmpr *pOCmpr = &pWp->pColCmpr[i]; @@ -87,7 +97,7 @@ static void metaGetEntryInfo(const SMetaEntry *pEntry, SMetaInfo *pInfo) { } } -static int metaUpdateMetaRsp(tb_uid_t uid, char *tbName, SSchemaWrapper *pSchema, STableMetaRsp *pMetaRsp) { +int metaUpdateMetaRsp(tb_uid_t uid, char *tbName, SSchemaWrapper *pSchema, STableMetaRsp *pMetaRsp) { pMetaRsp->pSchemas = taosMemoryMalloc(pSchema->nCols * sizeof(SSchema)); if (NULL == pMetaRsp->pSchemas) { return terrno; @@ -110,7 +120,7 @@ static int metaUpdateMetaRsp(tb_uid_t uid, char *tbName, SSchemaWrapper *pSchema return 0; } -static int metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) { +int metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) { int32_t code = 0; #ifdef USE_INVERTED_INDEX @@ -279,7 +289,7 @@ _exception: return code; } -static inline void metaTimeSeriesNotifyCheck(SMeta *pMeta) { +void metaTimeSeriesNotifyCheck(SMeta *pMeta) { #if defined(TD_ENTERPRISE) int64_t nTimeSeries = metaGetTimeSeriesNum(pMeta, 0); int64_t deltaTS = nTimeSeries - pMeta->pVnode->config.vndStats.numOfReportedTimeSeries; @@ -293,7 +303,7 @@ static inline void metaTimeSeriesNotifyCheck(SMeta *pMeta) { #endif } -int metaCreateSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) { +static int metaCreateSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) { SMetaEntry me = {0}; int kLen = 0; int vLen = 0; @@ -353,7 +363,7 @@ _err: return code; } -int metaDropSTable(SMeta *pMeta, int64_t verison, SVDropStbReq *pReq, SArray *tbUidList) { +static int metaDropSTable(SMeta *pMeta, int64_t verison, SVDropStbReq *pReq, SArray *tbUidList) { void *pKey = NULL; int nKey = 0; void *pData = NULL; @@ -437,6 +447,12 @@ _drop_super_table: tstrerror(terrno)); } + ret = metaCacheDrop(pMeta, pReq->suid); + if (ret < 0) { + metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid, + tstrerror(terrno)); + } + ret = tdbTbDelete(pMeta->pUidIdx, &pReq->suid, sizeof(tb_uid_t), pMeta->txn); if (ret < 0) { metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid, @@ -507,7 +523,7 @@ static int32_t metaGetSubtables(SMeta *pMeta, int64_t suid, SArray *uids) { return 0; } -int metaAlterSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) { +static int metaAlterSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) { SMetaEntry oStbEntry = {0}; SMetaEntry nStbEntry = {0}; TBC *pUidIdxc = NULL; @@ -675,7 +691,8 @@ _exit: tdbTbcClose(pUidIdxc); return 0; } -int metaAddIndexToSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) { + +static int metaAddIndexToSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) { SMetaEntry oStbEntry = {0}; SMetaEntry nStbEntry = {0}; STbDbKey tbDbKey = {0}; @@ -860,7 +877,8 @@ _err: return code; } -int metaDropIndexFromSTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq) { + +static int metaDropIndexFromSTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq) { int32_t code = 0; SMetaEntry oStbEntry = {0}; SMetaEntry nStbEntry = {0}; @@ -1047,7 +1065,7 @@ _err: return code; } -int metaCreateTable(SMeta *pMeta, int64_t ver, SVCreateTbReq *pReq, STableMetaRsp **pMetaRsp) { +static int metaCreateTable(SMeta *pMeta, int64_t ver, SVCreateTbReq *pReq, STableMetaRsp **pMetaRsp) { SMetaEntry me = {0}; SMetaReader mr = {0}; int32_t ret; @@ -1216,7 +1234,7 @@ _err: return TSDB_CODE_FAILED; } -int metaDropTable(SMeta *pMeta, int64_t version, SVDropTbReq *pReq, SArray *tbUids, tb_uid_t *tbUid) { +static int metaDropTable(SMeta *pMeta, int64_t version, SVDropTbReq *pReq, SArray *tbUids, tb_uid_t *tbUid) { void *pData = NULL; int nData = 0; int rc = 0; @@ -1270,7 +1288,7 @@ _exit: return rc; } -int32_t metaDropTables(SMeta *pMeta, SArray *tbUids) { +static int32_t metaDropTables(SMeta *pMeta, SArray *tbUids) { int32_t code = 0; if (taosArrayGetSize(tbUids) == 0) return TSDB_CODE_SUCCESS; @@ -1662,7 +1680,7 @@ static int metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type, tb_uid_t *p return 0; } // opt ins_tables -int metaUpdateBtimeIdx(SMeta *pMeta, const SMetaEntry *pME) { +static int metaUpdateBtimeIdx(SMeta *pMeta, const SMetaEntry *pME) { SBtimeIdxKey btimeKey = {0}; if (metaBuildBtimeIdxKey(&btimeKey, pME) < 0) { return 0; @@ -1673,13 +1691,14 @@ int metaUpdateBtimeIdx(SMeta *pMeta, const SMetaEntry *pME) { return tdbTbUpsert(pMeta->pBtimeIdx, &btimeKey, sizeof(btimeKey), NULL, 0, pMeta->txn); } -int metaDeleteBtimeIdx(SMeta *pMeta, const SMetaEntry *pME) { +static int metaDeleteBtimeIdx(SMeta *pMeta, const SMetaEntry *pME) { SBtimeIdxKey btimeKey = {0}; if (metaBuildBtimeIdxKey(&btimeKey, pME) < 0) { return 0; } return tdbTbDelete(pMeta->pBtimeIdx, &btimeKey, sizeof(btimeKey), pMeta->txn); } + int metaUpdateNcolIdx(SMeta *pMeta, const SMetaEntry *pME) { SNcolIdxKey ncolKey = {0}; if (metaBuildNColIdxKey(&ncolKey, pME) < 0) { @@ -2611,144 +2630,6 @@ static int metaUpdateTableOptions(SMeta *pMeta, int64_t version, SVAlterTbReq *p return 0; } -static int metaAddTagIndex(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq) { - SMetaEntry stbEntry = {0}; - void *pVal = NULL; - int nVal = 0; - int ret; - int c; - tb_uid_t uid, suid; - int64_t oversion; - const void *pData = NULL; - int nData = 0; - SDecoder dc = {0}; - - if (pAlterTbReq->tagName == NULL) { - return terrno = TSDB_CODE_INVALID_MSG; - } - - // search name index - ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal); - if (ret < 0) { - return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST; - } else { - uid = *(tb_uid_t *)pVal; - tdbFree(pVal); - pVal = NULL; - } - - if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(tb_uid_t), &pVal, &nVal) == -1) { - ret = -1; - goto _err; - } - suid = ((SUidIdxVal *)pVal)[0].suid; - - STbDbKey tbDbKey = {0}; - tbDbKey.uid = suid; - tbDbKey.version = ((SUidIdxVal *)pVal)[0].version; - ret = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pVal, &nVal); - if (ret < 0) { - goto _err; - } - tDecoderInit(&dc, pVal, nVal); - ret = metaDecodeEntry(&dc, &stbEntry); - if (ret < 0) { - tDecoderClear(&dc); - goto _err; - } - - // Get target schema info - SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag; - if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) { - terrno = TSDB_CODE_VND_COL_ALREADY_EXISTS; - goto _err; - } - SSchema *pCol = NULL; - int32_t iCol = 0; - for (;;) { - pCol = NULL; - if (iCol >= pTagSchema->nCols) break; - pCol = &pTagSchema->pSchema[iCol]; - if (strcmp(pCol->name, pAlterTbReq->tagName) == 0) break; - iCol++; - } - - if (iCol == 0) { - terrno = TSDB_CODE_VND_COL_ALREADY_EXISTS; - goto _err; - } - if (pCol == NULL) { - terrno = TSDB_CODE_VND_COL_NOT_EXISTS; - goto _err; - } - - /* - * iterator all pTdDbc by uid and version - */ - TBC *pCtbIdxc = NULL; - TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pCtbIdx, &pCtbIdxc, NULL)); - int rc = tdbTbcMoveTo(pCtbIdxc, &(SCtbIdxKey){.suid = suid, .uid = INT64_MIN}, sizeof(SCtbIdxKey), &c); - if (rc < 0) { - tdbTbcClose(pCtbIdxc); - goto _err; - } - for (;;) { - void *pKey, *pVal; - int nKey, nVal; - rc = tdbTbcNext(pCtbIdxc, &pKey, &nKey, &pVal, &nVal); - if (rc < 0) break; - if (((SCtbIdxKey *)pKey)->suid != uid) { - tdbFree(pKey); - tdbFree(pVal); - continue; - } - STagIdxKey *pTagIdxKey = NULL; - int32_t nTagIdxKey; - - const void *pTagData = NULL; - int32_t nTagData = 0; - - STagVal tagVal = {.cid = pCol->colId}; - if (tTagGet((const STag *)pVal, &tagVal)) { - if (IS_VAR_DATA_TYPE(pCol->type)) { - pTagData = tagVal.pData; - nTagData = (int32_t)tagVal.nData; - } else { - pTagData = &(tagVal.i64); - nTagData = tDataTypes[pCol->type].bytes; - } - } else { - if (!IS_VAR_DATA_TYPE(pCol->type)) { - nTagData = tDataTypes[pCol->type].bytes; - } - } - if (metaCreateTagIdxKey(suid, pCol->colId, pTagData, nTagData, pCol->type, uid, &pTagIdxKey, &nTagIdxKey) < 0) { - tdbFree(pKey); - tdbFree(pVal); - metaDestroyTagIdxKey(pTagIdxKey); - tdbTbcClose(pCtbIdxc); - goto _err; - } - ret = tdbTbUpsert(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, NULL, 0, pMeta->txn); - if (ret < 0) { - metaError("meta/table: failed to upsert tag idx:%s uid:%" PRId64, stbEntry.name, stbEntry.uid); - } - metaDestroyTagIdxKey(pTagIdxKey); - pTagIdxKey = NULL; - } - tdbTbcClose(pCtbIdxc); - return 0; - -_err: - // tDecoderClear(&dc1); - // tDecoderClear(&dc2); - // if (ctbEntry.pBuf) taosMemoryFree(ctbEntry.pBuf); - // if (stbEntry.pBuf) tdbFree(stbEntry.pBuf); - // tdbTbcClose(pTbDbc); - // tdbTbcClose(pUidIdxc); - return TSDB_CODE_FAILED; -} - typedef struct SMetaPair { void *key; int nkey; @@ -2873,7 +2754,7 @@ static int metaDropTagIndex(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterT _err: return TSDB_CODE_FAILED; } -int32_t metaUpdateTableColCompress(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) { +static int32_t metaUpdateTableColCompress(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) { // impl later SMetaEntry tbEntry = {0}; void *pVal = NULL; @@ -2976,23 +2857,21 @@ int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMeta switch (pReq->action) { case TSDB_ALTER_TABLE_ADD_COLUMN: case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION: + return metaAddTableColumn(pMeta, version, pReq, pMetaRsp); case TSDB_ALTER_TABLE_DROP_COLUMN: + return metaDropTableColumn(pMeta, version, pReq, pMetaRsp); case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES: + return metaAlterTableColumnBytes(pMeta, version, pReq, pMetaRsp); case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME: - return metaAlterTableColumn(pMeta, version, pReq, pMetaRsp); + return metaAlterTableColumnName(pMeta, version, pReq, pMetaRsp); case TSDB_ALTER_TABLE_UPDATE_TAG_VAL: - return metaUpdateTableTagVal(pMeta, version, pReq); + return metaUpdateTableTagValue(pMeta, version, pReq); case TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL: - return metaUpdateTableMultiTagVal(pMeta, version, pReq); - return terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION; + return metaUpdateTableMultiTagValue(pMeta, version, pReq); case TSDB_ALTER_TABLE_UPDATE_OPTIONS: - return metaUpdateTableOptions(pMeta, version, pReq); - case TSDB_ALTER_TABLE_ADD_TAG_INDEX: - return metaAddTagIndex(pMeta, version, pReq); - case TSDB_ALTER_TABLE_DROP_TAG_INDEX: - return metaDropTagIndex(pMeta, version, pReq); + return metaUpdateTableOptions2(pMeta, version, pReq); case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS: - return metaUpdateTableColCompress(pMeta, version, pReq); + return metaUpdateTableColCompress2(pMeta, version, pReq); default: return terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION; break; @@ -3155,7 +3034,7 @@ int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_ return 0; } -static void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) { +void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) { if (pTagIdxKey) taosMemoryFree(pTagIdxKey); } diff --git a/source/dnode/vnode/src/meta/metaTable2.c b/source/dnode/vnode/src/meta/metaTable2.c new file mode 100644 index 0000000000..6ff4cd6fdc --- /dev/null +++ b/source/dnode/vnode/src/meta/metaTable2.c @@ -0,0 +1,1827 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include "meta.h" + +extern int32_t metaHandleEntry2(SMeta *pMeta, const SMetaEntry *pEntry); +extern int32_t metaUpdateMetaRsp(tb_uid_t uid, char *tbName, SSchemaWrapper *pSchema, STableMetaRsp *pMetaRsp); +extern int32_t metaFetchEntryByUid(SMeta *pMeta, int64_t uid, SMetaEntry **ppEntry); +extern int32_t metaFetchEntryByName(SMeta *pMeta, const char *name, SMetaEntry **ppEntry); +extern void metaFetchEntryFree(SMetaEntry **ppEntry); +extern int32_t updataTableColCmpr(SColCmprWrapper *pWp, SSchema *pSchema, int8_t add, uint32_t compress); + +static int32_t metaCheckCreateSuperTableReq(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) { + int32_t vgId = TD_VID(pMeta->pVnode); + void *value = NULL; + int32_t valueSize = 0; + SMetaInfo info; + + // check name + if (NULL == pReq->name || strlen(pReq->name) == 0) { + metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, vgId, __func__, __FILE__, __LINE__, + pReq->name, version); + return TSDB_CODE_INVALID_MSG; + } + + int32_t r = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize); + if (r == 0) { // name exists, check uid and type + int64_t uid = *(tb_uid_t *)value; + tdbFree(value); + + if (pReq->suid != uid) { + metaError("vgId:%d, %s failed at %s:%d since table %s uid:%" PRId64 " already exists, request uid:%" PRId64 + " version:%" PRId64, + vgId, __func__, __FILE__, __LINE__, pReq->name, uid, pReq->suid, version); + return TSDB_CODE_TDB_TABLE_ALREADY_EXIST; + } + + if (metaGetInfo(pMeta, uid, &info, NULL) == TSDB_CODE_NOT_FOUND) { + metaError("vgId:%d, %s failed at %s:%d since table %s uid:%" PRId64 + " not found, this is an internal error in meta, version:%" PRId64, + vgId, __func__, __FILE__, __LINE__, pReq->name, uid, version); + return TSDB_CODE_PAR_TABLE_NOT_EXIST; + } + + if (info.uid == info.suid) { + return TSDB_CODE_TDB_STB_ALREADY_EXIST; + } else { + metaError("vgId:%d, %s failed at %s:%d since table %s uid:%" PRId64 + " already exists but not a super table, version:%" PRId64, + vgId, __func__, __FILE__, __LINE__, pReq->name, uid, version); + return TSDB_CODE_TDB_TABLE_ALREADY_EXIST; + } + } + + // check suid + if (metaGetInfo(pMeta, pReq->suid, &info, NULL) != TSDB_CODE_NOT_FOUND) { + metaError("vgId:%d, %s failed at %s:%d since table with uid:%" PRId64 " already exist, name:%s version:%" PRId64, + vgId, __func__, __FILE__, __LINE__, pReq->suid, pReq->name, version); + return TSDB_CODE_INVALID_MSG; + } + + return TSDB_CODE_SUCCESS; +} + +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) { + 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 TSDB_CODE_INVALID_MSG; + } + + 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; +} + +static int32_t metaCheckDropSuperTableReq(SMeta *pMeta, int64_t version, SVDropStbReq *pReq) { + int32_t code = TSDB_CODE_SUCCESS; + void *value = NULL; + int32_t valueSize = 0; + SMetaInfo info; + + if (NULL == pReq->name || strlen(pReq->name) == 0) { + 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 TSDB_CODE_INVALID_MSG; + } + + code = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize); + if (code) { + 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_STB_NOT_EXIST; + } else { + int64_t uid = *(int64_t *)value; + tdbFreeClear(value); + + if (uid != pReq->suid) { + metaError("vgId:%d, %s failed at %s:%d since table %s uid:%" PRId64 " not match, version:%" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pReq->suid, version); + return TSDB_CODE_TDB_STB_NOT_EXIST; + } + } + + code = metaGetInfo(pMeta, pReq->suid, &info, NULL); + if (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->suid, version); + return TSDB_CODE_INTERNAL_ERROR; + } + if (info.suid != info.uid) { + metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " is not a super table, version:%" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pReq->suid, version); + return TSDB_CODE_INVALID_MSG; + } + return code; +} + +// Create Super Table +int32_t metaCreateSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) { + int32_t code = TSDB_CODE_SUCCESS; + + // check request + code = metaCheckCreateSuperTableReq(pMeta, version, pReq); + if (code != TSDB_CODE_SUCCESS) { + if (code == TSDB_CODE_TDB_STB_ALREADY_EXIST) { + metaWarn("vgId:%d, super table %s uid:%" PRId64 " already exists, version:%" PRId64, TD_VID(pMeta->pVnode), + pReq->name, pReq->suid, version); + TAOS_RETURN(TSDB_CODE_SUCCESS); + } else { + TAOS_RETURN(code); + } + } + + // handle entry + SMetaEntry entry = { + .version = version, + .type = TSDB_SUPER_TABLE, + .uid = pReq->suid, + .name = pReq->name, + .stbEntry.schemaRow = pReq->schemaRow, + .stbEntry.schemaTag = pReq->schemaTag, + }; + if (pReq->rollup) { + TABLE_SET_ROLLUP(entry.flags); + entry.stbEntry.rsmaParam = pReq->rsmaParam; + } + if (pReq->colCmpred) { + TABLE_SET_COL_COMPRESSED(entry.flags); + entry.colCmpr = pReq->colCmpr; + } + + code = metaHandleEntry2(pMeta, &entry); + if (TSDB_CODE_SUCCESS == code) { + metaInfo("vgId:%d, super table %s suid:%" PRId64 " is created, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name, + pReq->suid, version); + } else { + metaError("vgId:%d, failed to create stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, + pReq->suid, tstrerror(code)); + } + TAOS_RETURN(code); +} + +// Drop Super Table +int32_t metaDropSuperTable(SMeta *pMeta, int64_t verison, SVDropStbReq *pReq) { + int32_t code = TSDB_CODE_SUCCESS; + + // check request + code = metaCheckDropSuperTableReq(pMeta, verison, pReq); + if (code) { + TAOS_RETURN(code); + } + + // handle entry + SMetaEntry entry = { + .version = verison, + .type = -TSDB_SUPER_TABLE, + .uid = pReq->suid, + }; + code = metaHandleEntry2(pMeta, &entry); + if (code) { + metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid, + tstrerror(code)); + } else { + metaInfo("vgId:%d, super table %s uid:%" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name, + pReq->suid, verison); + } + TAOS_RETURN(code); +} + +// 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 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 + if (info.suid == info.uid || info.suid == 0) { + 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->uid, version); + return TSDB_CODE_TDB_TABLE_IN_OTHER_STABLE; + } + + // check 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 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 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; + + // 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 + metaTimeSeriesNotifyCheck(pMeta); +#endif +} + +// Drop Child Table + +// Alter Child Table + +// Create Normal Table +static int32_t metaCheckCreateNormalTableReq(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq) { + int32_t code = 0; + void *value = NULL; + int32_t valueSize = 0; + + if (NULL == pReq->name || strlen(pReq->name) == 0) { + 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 TSDB_CODE_INVALID_MSG; + } + + // check name + if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize) == 0) { + // for auto create table, we return the uid of the existing table + pReq->uid = *(tb_uid_t *)value; + tdbFree(value); + return TSDB_CODE_TDB_TABLE_ALREADY_EXIST; + } + + // grant check + code = grantCheck(TSDB_GRANT_TIMESERIES); + if (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 metaBuildCreateNormalTableRsp(SMeta *pMeta, 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; + } + + code = metaUpdateMetaRsp(pEntry->uid, pEntry->name, &pEntry->ntbEntry.schemaRow, *ppRsp); + if (code) { + taosMemoryFreeClear(*ppRsp); + return code; + } + + for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) { + SColCmpr *p = &pEntry->colCmpr.pColCmpr[i]; + (*ppRsp)->pSchemaExt[i].colId = p->id; + (*ppRsp)->pSchemaExt[i].compress = p->alg; + } + + return code; +} + +static int32_t metaCreateNormalTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) { + int32_t code = TSDB_CODE_SUCCESS; + + // check request + code = metaCheckCreateNormalTableReq(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); + } + TAOS_RETURN(code); + } + + SMetaEntry entry = { + .version = version, + .type = TSDB_NORMAL_TABLE, + .uid = pReq->uid, + .name = pReq->name, + .ntbEntry.btime = pReq->btime, + .ntbEntry.ttlDays = pReq->ttl, + .ntbEntry.commentLen = pReq->commentLen, + .ntbEntry.comment = pReq->comment, + .ntbEntry.schemaRow = pReq->ntb.schemaRow, + .ntbEntry.ncid = pReq->ntb.schemaRow.pSchema[pReq->ntb.schemaRow.nCols - 1].colId + 1, + .colCmpr = pReq->colCmpr, + }; + TABLE_SET_COL_COMPRESSED(entry.flags); + + // build response + code = metaBuildCreateNormalTableRsp(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, normal table:%s uid %" PRId64 " is created, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name, + pReq->uid, version); + } else { + 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); +#if 0 + metaTimeSeriesNotifyCheck(pMeta); +#endif +} + +// Drop Normal Table + +// Alter Normal Table + +int32_t metaCreateTable2(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) { + int32_t code = TSDB_CODE_SUCCESS; + if (TSDB_CHILD_TABLE == pReq->type) { + code = metaCreateChildTable(pMeta, version, pReq, ppRsp); + } else if (TSDB_NORMAL_TABLE == pReq->type) { + code = metaCreateNormalTable(pMeta, version, pReq, ppRsp); + } else { + code = TSDB_CODE_INVALID_MSG; + } + TAOS_RETURN(code); +} + +int32_t metaDropTable2(SMeta *pMeta, int64_t version, SVDropTbReq *pReq) { + 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_PARA; + 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); +} + +static int32_t metaCheckAlterTableColumnReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) { + int32_t code = 0; + + if (NULL == pReq->colName || strlen(pReq->colName) == 0) { + metaError("vgId:%d, %s failed at %s:%d since invalid column name:%s, version:%" PRId64, TD_VID(pMeta->pVnode), + __func__, __FILE__, __LINE__, pReq->colName, version); + TAOS_RETURN(TSDB_CODE_INVALID_MSG); + } + + // check name + void *value = NULL; + int32_t valueSize = 0; + code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize); + if (code) { + metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, + __FILE__, __LINE__, pReq->tbName, version); + code = TSDB_CODE_TDB_TABLE_NOT_EXIST; + TAOS_RETURN(code); + } + int64_t uid = *(int64_t *)value; + tdbFreeClear(value); + + // check table type + SMetaInfo info; + if (metaGetInfo(pMeta, uid, &info, NULL) != 0) { + metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 + " not found, this is an internal error in meta, version:%" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, uid, version); + code = TSDB_CODE_INTERNAL_ERROR; + TAOS_RETURN(code); + } + if (info.suid != 0) { + metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " is not a normal table, version:%" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, uid, version); + code = TSDB_CODE_VND_INVALID_TABLE_ACTION; + TAOS_RETURN(code); + } + + // check grant + code = grantCheck(TSDB_GRANT_TIMESERIES); + if (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->tbName); + TAOS_RETURN(code); + } + TAOS_RETURN(code); +} + +int32_t metaAddTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) { + int32_t code = TSDB_CODE_SUCCESS; + + // check request + code = metaCheckAlterTableColumnReq(pMeta, version, pReq); + if (code) { + TAOS_RETURN(code); + } + + // fetch old entry + SMetaEntry *pEntry = NULL; + code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry); + if (code) { + metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, + __FILE__, __LINE__, pReq->tbName, version); + TAOS_RETURN(code); + } + if (pEntry->version >= version) { + metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(TSDB_CODE_INVALID_PARA); + } + + // do add column + int32_t rowSize = 0; + SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow; + SSchema *pColumn; + pEntry->version = version; + for (int32_t i = 0; i < pSchema->nCols; i++) { + pColumn = &pSchema->pSchema[i]; + if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) { + metaError("vgId:%d, %s failed at %s:%d since column %s already exists in table %s, version:%" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(TSDB_CODE_VND_COL_ALREADY_EXISTS); + } + rowSize += pColumn->bytes; + } + + if (rowSize + pReq->bytes > TSDB_MAX_BYTES_PER_ROW) { + metaError("vgId:%d, %s failed at %s:%d since row size %d + %d > %d, version:%" PRId64, TD_VID(pMeta->pVnode), + __func__, __FILE__, __LINE__, rowSize, pReq->bytes, TSDB_MAX_BYTES_PER_ROW, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(TSDB_CODE_PAR_INVALID_ROW_LENGTH); + } + + SSchema *pNewSchema = taosMemoryRealloc(pSchema->pSchema, sizeof(SSchema) * (pSchema->nCols + 1)); + if (NULL == pNewSchema) { + metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__, + __LINE__, tstrerror(terrno), version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(terrno); + } + pSchema->pSchema = pNewSchema; + pSchema->version++; + pSchema->nCols++; + pColumn = &pSchema->pSchema[pSchema->nCols - 1]; + pColumn->bytes = pReq->bytes; + pColumn->type = pReq->type; + pColumn->flags = pReq->flags; + pColumn->colId = pEntry->ntbEntry.ncid++; + tstrncpy(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN); + uint32_t compress; + if (TSDB_ALTER_TABLE_ADD_COLUMN == pReq->action) { + compress = createDefaultColCmprByType(pColumn->type); + } else { + compress = pReq->compress; + } + code = updataTableColCmpr(&pEntry->colCmpr, pColumn, 1, compress); + if (code) { + metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__, + __LINE__, tstrerror(code), version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(code); + } + + // do handle entry + code = metaHandleEntry2(pMeta, pEntry); + 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), pEntry->uid, pReq->tbName, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(code); + } else { + metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName, + pEntry->uid, version); + } + + if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) { + 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), pEntry->uid, pReq->tbName, version); + } else { + for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) { + SColCmpr *p = &pEntry->colCmpr.pColCmpr[i]; + pRsp->pSchemaExt[i].colId = p->id; + pRsp->pSchemaExt[i].compress = p->alg; + } + } + + metaFetchEntryFree(&pEntry); + TAOS_RETURN(code); +} + +int32_t metaDropTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) { + int32_t code = TSDB_CODE_SUCCESS; + + // check request + code = metaCheckAlterTableColumnReq(pMeta, version, pReq); + if (code) { + TAOS_RETURN(code); + } + + // fetch old entry + SMetaEntry *pEntry = NULL; + code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry); + if (code) { + metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, + __FILE__, __LINE__, pReq->tbName, version); + TAOS_RETURN(code); + } + + if (pEntry->version >= version) { + metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(TSDB_CODE_INVALID_PARA); + } + + // search the column to drop + SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow; + SSchema *pColumn = NULL; + SSchema tColumn; + int32_t iColumn = 0; + for (; iColumn < pSchema->nCols; iColumn++) { + pColumn = &pSchema->pSchema[iColumn]; + if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) { + break; + } + } + + if (iColumn == pSchema->nCols) { + metaError("vgId:%d, %s failed at %s:%d since column %s not found in table %s, version:%" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS); + } + + if (pColumn->colId == 0 || pColumn->flags & COL_IS_KEY) { + metaError("vgId:%d, %s failed at %s:%d since column %s is primary key, version:%" PRId64, TD_VID(pMeta->pVnode), + __func__, __FILE__, __LINE__, pReq->colName, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION); + } + + if (tqCheckColModifiable(pMeta->pVnode->pTq, pEntry->uid, pColumn->colId) != 0) { + metaError("vgId:%d, %s failed at %s:%d since column %s is not modifiable, version:%" PRId64, TD_VID(pMeta->pVnode), + __func__, __FILE__, __LINE__, pReq->colName, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION); + } + tColumn = *pColumn; + + // do drop column + pEntry->version = version; + if (pSchema->nCols - iColumn - 1 > 0) { + memmove(pColumn, pColumn + 1, (pSchema->nCols - iColumn - 1) * sizeof(SSchema)); + } + pSchema->nCols--; + pSchema->version++; + code = updataTableColCmpr(&pEntry->colCmpr, &tColumn, 0, 0); + if (code) { + metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__, + __LINE__, tstrerror(code), version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(code); + } + if (pEntry->colCmpr.nCols != pSchema->nCols) { + metaError("vgId:%d, %s failed at %s:%d since column count mismatch, version:%" PRId64, TD_VID(pMeta->pVnode), + __func__, __FILE__, __LINE__, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION); + } + + // do handle entry + code = metaHandleEntry2(pMeta, pEntry); + 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), pEntry->uid, pReq->tbName, version); + metaFetchEntryFree(&pEntry); + } else { + metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName, + pEntry->uid, version); + } + + // build response + if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) { + 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), pEntry->uid, pReq->tbName, version); + } else { + for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) { + SColCmpr *p = &pEntry->colCmpr.pColCmpr[i]; + pRsp->pSchemaExt[i].colId = p->id; + pRsp->pSchemaExt[i].compress = p->alg; + } + } + + metaFetchEntryFree(&pEntry); + TAOS_RETURN(code); +} + +int32_t metaAlterTableColumnName(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) { + int32_t code = TSDB_CODE_SUCCESS; + + // check request + code = metaCheckAlterTableColumnReq(pMeta, version, pReq); + if (code) { + TAOS_RETURN(code); + } + + if (NULL == pReq->colNewName) { + metaError("vgId:%d, %s failed at %s:%d since invalid new column name, version:%" PRId64, TD_VID(pMeta->pVnode), + __func__, __FILE__, __LINE__, version); + TAOS_RETURN(TSDB_CODE_INVALID_MSG); + } + + // fetch old entry + SMetaEntry *pEntry = NULL; + code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry); + if (code) { + metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, + __FILE__, __LINE__, pReq->tbName, version); + TAOS_RETURN(code); + } + + if (pEntry->version >= version) { + metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(TSDB_CODE_INVALID_PARA); + } + + // search the column to update + SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow; + SSchema *pColumn = NULL; + int32_t iColumn = 0; + for (int32_t i = 0; i < pSchema->nCols; i++) { + if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) { + pColumn = &pSchema->pSchema[i]; + iColumn = i; + break; + } + } + + if (NULL == pColumn) { + metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, pReq->tbName, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS); + } + + if (tqCheckColModifiable(pMeta->pVnode->pTq, pEntry->uid, pColumn->colId) != 0) { + metaError("vgId:%d, %s failed at %s:%d since column %s is not modifiable, version:%" PRId64, TD_VID(pMeta->pVnode), + __func__, __FILE__, __LINE__, pColumn->name, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(TSDB_CODE_VND_COL_SUBSCRIBED); + } + + // do update column name + pEntry->version = version; + tstrncpy(pColumn->name, pReq->colNewName, TSDB_COL_NAME_LEN); + pSchema->version++; + + // do handle entry + code = metaHandleEntry2(pMeta, pEntry); + 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), pEntry->uid, pReq->tbName, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(code); + } else { + metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName, + pEntry->uid, version); + } + + // build response + if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) { + 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), pEntry->uid, pReq->tbName, version); + } else { + for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) { + SColCmpr *p = &pEntry->colCmpr.pColCmpr[i]; + pRsp->pSchemaExt[i].colId = p->id; + pRsp->pSchemaExt[i].compress = p->alg; + } + } + + metaFetchEntryFree(&pEntry); + TAOS_RETURN(code); +} + +int32_t metaAlterTableColumnBytes(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) { + int32_t code = TSDB_CODE_SUCCESS; + + // check request + code = metaCheckAlterTableColumnReq(pMeta, version, pReq); + if (code) { + TAOS_RETURN(code); + } + + // fetch old entry + SMetaEntry *pEntry = NULL; + code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry); + if (code) { + metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, + __FILE__, __LINE__, pReq->tbName, version); + TAOS_RETURN(code); + } + + if (pEntry->version >= version) { + metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(TSDB_CODE_INVALID_PARA); + } + + // search the column to update + SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow; + SSchema *pColumn = NULL; + int32_t iColumn = 0; + int32_t rowSize = 0; + for (int32_t i = 0; i < pSchema->nCols; i++) { + if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) { + pColumn = &pSchema->pSchema[i]; + iColumn = i; + } + rowSize += pSchema->pSchema[i].bytes; + } + + if (NULL == pColumn) { + metaError("vgId:%d, %s failed at %s:%d since column %s not found in table %s, version:%" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS); + } + + if (!IS_VAR_DATA_TYPE(pColumn->type) || pColumn->bytes >= pReq->colModBytes) { + metaError("vgId:%d, %s failed at %s:%d since column %s is not var data type or bytes %d >= %d, version:%" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pColumn->bytes, pReq->colModBytes, + version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION); + } + + if (tqCheckColModifiable(pMeta->pVnode->pTq, pEntry->uid, pColumn->colId) != 0) { + metaError("vgId:%d, %s failed at %s:%d since column %s is not modifiable, version:%" PRId64, TD_VID(pMeta->pVnode), + __func__, __FILE__, __LINE__, pReq->colName, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(TSDB_CODE_VND_COL_SUBSCRIBED); + } + + if (rowSize + pReq->colModBytes - pColumn->bytes > TSDB_MAX_BYTES_PER_ROW) { + metaError("vgId:%d, %s failed at %s:%d since row size %d + %d - %d > %d, version:%" PRId64, TD_VID(pMeta->pVnode), + __func__, __FILE__, __LINE__, rowSize, pReq->colModBytes, pColumn->bytes, TSDB_MAX_BYTES_PER_ROW, + version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(TSDB_CODE_PAR_INVALID_ROW_LENGTH); + } + + // do change the column bytes + pEntry->version = version; + pSchema->version++; + pColumn->bytes = pReq->colModBytes; + + // do handle entry + code = metaHandleEntry2(pMeta, pEntry); + 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), pEntry->uid, pReq->tbName, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(code); + } else { + metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName, + pEntry->uid, version); + } + + // build response + if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) { + 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), pEntry->uid, pReq->tbName, version); + } else { + for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) { + SColCmpr *p = &pEntry->colCmpr.pColCmpr[i]; + pRsp->pSchemaExt[i].colId = p->id; + pRsp->pSchemaExt[i].compress = p->alg; + } + } + + metaFetchEntryFree(&pEntry); + TAOS_RETURN(code); +} + +static int32_t metaCheckUpdateTableTagValReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) { + int32_t code = 0; + + // check tag name + if (NULL == pReq->tagName || strlen(pReq->tagName) == 0) { + metaError("vgId:%d, %s failed at %s:%d since invalid tag name:%s, version:%" PRId64, TD_VID(pMeta->pVnode), + __func__, __FILE__, __LINE__, pReq->tagName, version); + TAOS_RETURN(TSDB_CODE_INVALID_MSG); + } + + // check name + void *value = NULL; + int32_t valueSize = 0; + code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize); + if (code) { + metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, + __FILE__, __LINE__, pReq->tbName, version); + code = TSDB_CODE_TDB_TABLE_NOT_EXIST; + TAOS_RETURN(code); + } + tdbFreeClear(value); + + TAOS_RETURN(code); +} + +int32_t metaUpdateTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) { + int32_t code = TSDB_CODE_SUCCESS; + + // check request + code = metaCheckUpdateTableTagValReq(pMeta, version, pReq); + if (code) { + TAOS_RETURN(code); + } + + // fetch child entry + SMetaEntry *pChild = NULL; + code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild); + if (code) { + metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, + __FILE__, __LINE__, pReq->tbName, version); + TAOS_RETURN(code); + } + + if (pChild->type != TSDB_CHILD_TABLE) { + metaError("vgId:%d, %s failed at %s:%d since table %s is not a child table, version:%" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version); + metaFetchEntryFree(&pChild); + TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION); + } + + // fetch super entry + SMetaEntry *pSuper = NULL; + code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper); + if (code) { + metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pChild->ctbEntry.suid, version); + metaFetchEntryFree(&pChild); + TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR); + } + + // search the tag to update + SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag; + SSchema *pColumn = NULL; + int32_t iColumn = 0; + for (int32_t i = 0; i < pTagSchema->nCols; i++) { + if (strncmp(pTagSchema->pSchema[i].name, pReq->tagName, TSDB_COL_NAME_LEN) == 0) { + pColumn = &pTagSchema->pSchema[i]; + iColumn = i; + break; + } + } + + if (NULL == pColumn) { + metaError("vgId:%d, %s failed at %s:%d since tag %s not found in table %s, version:%" PRId64, TD_VID(pMeta->pVnode), + __func__, __FILE__, __LINE__, pReq->tagName, pReq->tbName, version); + metaFetchEntryFree(&pChild); + metaFetchEntryFree(&pSuper); + TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS); + } + + // do change tag value + pChild->version = version; + if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) { + void *pNewTag = taosMemoryRealloc(pChild->ctbEntry.pTags, pReq->nTagVal); + if (NULL == pNewTag) { + metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__, + __LINE__, tstrerror(terrno), version); + metaFetchEntryFree(&pChild); + metaFetchEntryFree(&pSuper); + TAOS_RETURN(terrno); + } + pChild->ctbEntry.pTags = pNewTag; + memcpy(pChild->ctbEntry.pTags, pReq->pTagVal, pReq->nTagVal); + } else { + STag *pOldTag = (STag *)pChild->ctbEntry.pTags; + + SArray *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal)); + if (NULL == pTagArray) { + metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__, + __LINE__, tstrerror(terrno), version); + metaFetchEntryFree(&pChild); + metaFetchEntryFree(&pSuper); + TAOS_RETURN(terrno); + } + + for (int32_t i = 0; i < pTagSchema->nCols; i++) { + STagVal value = { + .type = pTagSchema->pSchema[i].type, + .cid = pTagSchema->pSchema[i].colId, + }; + + if (iColumn == i) { + if (pReq->isNull) { + continue; + } + if (IS_VAR_DATA_TYPE(value.type)) { + value.pData = pReq->pTagVal; + value.nData = pReq->nTagVal; + } else { + memcpy(&value.i64, pReq->pTagVal, pReq->nTagVal); + } + } else if (!tTagGet(pOldTag, &value)) { + continue; + } + + if (NULL == taosArrayPush(pTagArray, &value)) { + metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__, + __LINE__, tstrerror(terrno), version); + taosArrayDestroy(pTagArray); + metaFetchEntryFree(&pChild); + metaFetchEntryFree(&pSuper); + TAOS_RETURN(terrno); + } + } + + STag *pNewTag = NULL; + code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag); + if (code) { + metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__, + __LINE__, tstrerror(code), version); + taosArrayDestroy(pTagArray); + metaFetchEntryFree(&pChild); + metaFetchEntryFree(&pSuper); + TAOS_RETURN(code); + } + taosArrayDestroy(pTagArray); + taosMemoryFree(pChild->ctbEntry.pTags); + pChild->ctbEntry.pTags = (uint8_t *)pNewTag; + } + + // do handle entry + code = metaHandleEntry2(pMeta, pChild); + 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), pChild->uid, pReq->tbName, version); + metaFetchEntryFree(&pChild); + metaFetchEntryFree(&pSuper); + TAOS_RETURN(code); + } else { + metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName, + pChild->uid, version); + } + + // free resource and return + metaFetchEntryFree(&pChild); + metaFetchEntryFree(&pSuper); + TAOS_RETURN(code); +} + +static int32_t metaCheckUpdateTableMultiTagValueReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) { + int32_t code = 0; + + // check tag name + if (NULL == pReq->pMultiTag || taosArrayGetSize(pReq->pMultiTag) == 0) { + metaError("vgId:%d, %s failed at %s:%d since invalid tag name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, + __FILE__, __LINE__, version); + TAOS_RETURN(TSDB_CODE_INVALID_MSG); + } + + // check name + void *value = NULL; + int32_t valueSize = 0; + code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize); + if (code) { + metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, + __FILE__, __LINE__, pReq->tbName, version); + code = TSDB_CODE_TDB_TABLE_NOT_EXIST; + TAOS_RETURN(code); + } + tdbFreeClear(value); + + if (taosArrayGetSize(pReq->pMultiTag) == 0) { + metaError("vgId:%d, %s failed at %s:%d since invalid tag name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, + __FILE__, __LINE__, version); + TAOS_RETURN(TSDB_CODE_INVALID_MSG); + } + + TAOS_RETURN(code); +} + +int32_t metaUpdateTableMultiTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) { + int32_t code = TSDB_CODE_SUCCESS; + + code = metaCheckUpdateTableMultiTagValueReq(pMeta, version, pReq); + if (code) { + TAOS_RETURN(code); + } + + // fetch child entry + SMetaEntry *pChild = NULL; + code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild); + if (code) { + metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, + __FILE__, __LINE__, pReq->tbName, version); + TAOS_RETURN(code); + } + + if (pChild->type != TSDB_CHILD_TABLE) { + metaError("vgId:%d, %s failed at %s:%d since table %s is not a child table, version:%" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version); + metaFetchEntryFree(&pChild); + TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION); + } + + // fetch super entry + SMetaEntry *pSuper = NULL; + code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper); + if (code) { + metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pChild->ctbEntry.suid, version); + metaFetchEntryFree(&pChild); + TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR); + } + + // search the tags to update + SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag; + + if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) { + metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode), + __func__, __FILE__, __LINE__, pReq->tbName, version); + metaFetchEntryFree(&pChild); + metaFetchEntryFree(&pSuper); + TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS); + } + + // do check if tag name exists + SHashObj *pTagTable = + taosHashInit(pTagSchema->nCols, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK); + if (pTagTable == NULL) { + metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__, + __LINE__, tstrerror(terrno), version); + metaFetchEntryFree(&pChild); + metaFetchEntryFree(&pSuper); + TAOS_RETURN(terrno); + } + + for (int32_t i = 0; i < taosArrayGetSize(pReq->pMultiTag); i++) { + SMultiTagUpateVal *pTagVal = taosArrayGet(pReq->pMultiTag, i); + if (taosHashPut(pTagTable, pTagVal->tagName, strlen(pTagVal->tagName), pTagVal, sizeof(*pTagVal)) != 0) { + metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__, + __LINE__, tstrerror(terrno), version); + taosHashCleanup(pTagTable); + metaFetchEntryFree(&pChild); + metaFetchEntryFree(&pSuper); + TAOS_RETURN(terrno); + } + } + + int32_t numOfChangedTags = 0; + for (int32_t i = 0; i < pTagSchema->nCols; i++) { + taosHashGet(pTagTable, pTagSchema->pSchema[i].name, strlen(pTagSchema->pSchema[i].name)) != NULL + ? numOfChangedTags++ + : 0; + } + if (numOfChangedTags < taosHashGetSize(pTagTable)) { + metaError("vgId:%d, %s failed at %s:%d since tag count mismatch, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, + __FILE__, __LINE__, version); + taosHashCleanup(pTagTable); + metaFetchEntryFree(&pChild); + metaFetchEntryFree(&pSuper); + TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS); + } + + // do change tag value + pChild->version = version; + const STag *pOldTag = (const STag *)pChild->ctbEntry.pTags; + SArray *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal)); + if (NULL == pTagArray) { + metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__, + __LINE__, tstrerror(terrno), version); + taosHashCleanup(pTagTable); + metaFetchEntryFree(&pChild); + metaFetchEntryFree(&pSuper); + TAOS_RETURN(terrno); + } + + for (int32_t i = 0; i < pTagSchema->nCols; i++) { + SSchema *pCol = &pTagSchema->pSchema[i]; + STagVal value = { + .cid = pCol->colId, + }; + + SMultiTagUpateVal *pTagVal = taosHashGet(pTagTable, pCol->name, strlen(pCol->name)); + if (pTagVal == NULL) { + if (!tTagGet(pOldTag, &value)) { + continue; + } + } else { + value.type = pCol->type; + if (pTagVal->isNull) { + continue; + } + + if (IS_VAR_DATA_TYPE(pCol->type)) { + value.pData = pTagVal->pTagVal; + value.nData = pTagVal->nTagVal; + } else { + memcpy(&value.i64, pTagVal->pTagVal, pTagVal->nTagVal); + } + } + + if (taosArrayPush(pTagArray, &value) == NULL) { + metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__, + __LINE__, tstrerror(terrno), version); + taosHashCleanup(pTagTable); + taosArrayDestroy(pTagArray); + metaFetchEntryFree(&pChild); + metaFetchEntryFree(&pSuper); + TAOS_RETURN(terrno); + } + } + + STag *pNewTag = NULL; + code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag); + if (code) { + metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__, + __LINE__, tstrerror(code), version); + taosHashCleanup(pTagTable); + taosArrayDestroy(pTagArray); + metaFetchEntryFree(&pChild); + metaFetchEntryFree(&pSuper); + TAOS_RETURN(code); + } + taosArrayDestroy(pTagArray); + taosMemoryFree(pChild->ctbEntry.pTags); + pChild->ctbEntry.pTags = (uint8_t *)pNewTag; + + // do handle entry + code = metaHandleEntry2(pMeta, pChild); + 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), pChild->uid, pReq->tbName, version); + taosHashCleanup(pTagTable); + metaFetchEntryFree(&pChild); + metaFetchEntryFree(&pSuper); + TAOS_RETURN(code); + } else { + metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName, + pChild->uid, version); + } + + taosHashCleanup(pTagTable); + metaFetchEntryFree(&pChild); + metaFetchEntryFree(&pSuper); + TAOS_RETURN(code); +} + +static int32_t metaCheckUpdateTableOptionsReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) { + int32_t code = TSDB_CODE_SUCCESS; + + if (pReq->tbName == NULL || strlen(pReq->tbName) == 0) { + metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, + __FILE__, __LINE__, version); + TAOS_RETURN(TSDB_CODE_INVALID_MSG); + } + + return code; +} + +int32_t metaUpdateTableOptions2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) { + int32_t code = 0; + + code = metaCheckUpdateTableOptionsReq(pMeta, version, pReq); + if (code) { + TAOS_RETURN(code); + } + + // fetch entry + SMetaEntry *pEntry = NULL; + code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry); + if (code) { + metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, + __FILE__, __LINE__, pReq->tbName, version); + TAOS_RETURN(code); + } + + // do change the entry + pEntry->version = version; + if (pEntry->type == TSDB_CHILD_TABLE) { + if (pReq->updateTTL) { + pEntry->ctbEntry.ttlDays = pReq->newTTL; + } + if (pReq->newCommentLen >= 0) { + char *pNewComment = NULL; + if (pReq->newCommentLen) { + pNewComment = taosMemoryRealloc(pEntry->ctbEntry.comment, pReq->newCommentLen + 1); + if (NULL == pNewComment) { + metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__, + __LINE__, tstrerror(terrno), version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(terrno); + } + memcpy(pNewComment, pReq->newComment, pReq->newCommentLen + 1); + } else { + taosMemoryFreeClear(pEntry->ctbEntry.comment); + } + pEntry->ctbEntry.comment = pNewComment; + pEntry->ctbEntry.commentLen = pReq->newCommentLen; + } + } else if (pEntry->type == TSDB_NORMAL_TABLE) { + if (pReq->updateTTL) { + pEntry->ntbEntry.ttlDays = pReq->newTTL; + } + if (pReq->newCommentLen >= 0) { + char *pNewComment = NULL; + if (pReq->newCommentLen > 0) { + pNewComment = taosMemoryRealloc(pEntry->ntbEntry.comment, pReq->newCommentLen + 1); + if (NULL == pNewComment) { + metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__, + __LINE__, tstrerror(terrno), version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(terrno); + } + memcpy(pNewComment, pReq->newComment, pReq->newCommentLen + 1); + } else { + taosMemoryFreeClear(pEntry->ntbEntry.comment); + } + pEntry->ntbEntry.comment = pNewComment; + pEntry->ntbEntry.commentLen = pReq->newCommentLen; + } + } else { + metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode), + __func__, __FILE__, __LINE__, pReq->tbName, pEntry->type, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION); + } + + // do handle entry + code = metaHandleEntry2(pMeta, pEntry); + 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), pEntry->uid, pReq->tbName, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(code); + } else { + metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName, + pEntry->uid, version); + } + + metaFetchEntryFree(&pEntry); + TAOS_RETURN(code); +} + +int32_t metaUpdateTableColCompress2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) { + int32_t code = TSDB_CODE_SUCCESS; + + if (NULL == pReq->tbName || strlen(pReq->tbName) == 0) { + metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, + __FILE__, __LINE__, version); + TAOS_RETURN(TSDB_CODE_INVALID_MSG); + } + + SMetaEntry *pEntry = NULL; + code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry); + if (code) { + metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, + __FILE__, __LINE__, pReq->tbName, version); + TAOS_RETURN(code); + } + + if (pEntry->version >= version) { + metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(TSDB_CODE_INVALID_PARA); + } + + if (pEntry->type != TSDB_NORMAL_TABLE && pEntry->type != TSDB_SUPER_TABLE) { + metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode), + __func__, __FILE__, __LINE__, pReq->tbName, pEntry->type, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION); + } + + // do change the entry + int8_t updated = 0; + SColCmprWrapper *wp = &pEntry->colCmpr; + for (int32_t i = 0; i < wp->nCols; i++) { + SColCmpr *p = &wp->pColCmpr[i]; + if (p->id == pReq->colId) { + uint32_t dst = 0; + updated = tUpdateCompress(p->alg, pReq->compress, TSDB_COLVAL_COMPRESS_DISABLED, TSDB_COLVAL_LEVEL_DISABLED, + TSDB_COLVAL_LEVEL_MEDIUM, &dst); + if (updated > 0) { + p->alg = dst; + } + } + } + + if (updated == 0) { + code = TSDB_CODE_VND_COLUMN_COMPRESS_ALREADY_EXIST; + metaError("vgId:%d, %s failed at %s:%d since column %d compress level is not changed, version:%" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(code); + } else if (updated < 0) { + code = TSDB_CODE_TSC_COMPRESS_LEVEL_ERROR; + metaError("vgId:%d, %s failed at %s:%d since column %d compress level is invalid, version:%" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(code); + } + + pEntry->version = version; + + // do handle entry + code = metaHandleEntry2(pMeta, pEntry); + 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), pEntry->uid, pReq->tbName, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(code); + } else { + metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName, + pEntry->uid, version); + } + + metaFetchEntryFree(&pEntry); + TAOS_RETURN(code); +} + +int32_t metaAddIndexToSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) { + int32_t code = TSDB_CODE_SUCCESS; + + if (NULL == pReq->name || strlen(pReq->name) == 0) { + metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, + __FILE__, __LINE__, version); + TAOS_RETURN(TSDB_CODE_INVALID_MSG); + } + + SMetaEntry *pEntry = NULL; + code = metaFetchEntryByName(pMeta, pReq->name, &pEntry); + if (code) { + 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); + TAOS_RETURN(code); + } + + if (pEntry->type != TSDB_SUPER_TABLE) { + metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode), + __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION); + } + + if (pEntry->uid != pReq->suid) { + metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " is not equal to %" PRId64 + ", version:%" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pEntry->uid, pReq->suid, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(TSDB_CODE_INVALID_MSG); + } + + // if (pEntry->stbEntry.schemaTag.version >= pReq->schemaTag.version) { + // metaError("vgId:%d, %s failed at %s:%d since table %s tag schema version %d is not less than %d, version:%" + // PRId64, + // TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pEntry->stbEntry.schemaTag.version, + // pReq->schemaTag.version, version); + // metaFetchEntryFree(&pEntry); + // TAOS_RETURN(TSDB_CODE_INVALID_MSG); + // } + + // do change the entry + SSchemaWrapper *pOldTagSchema = &pEntry->stbEntry.schemaTag; + SSchemaWrapper *pNewTagSchema = &pReq->schemaTag; + if (pOldTagSchema->nCols == 1 && pOldTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) { + metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode), + __func__, __FILE__, __LINE__, pReq->name, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS); + } + + if (pOldTagSchema->nCols != pNewTagSchema->nCols) { + metaError( + "vgId:%d, %s failed at %s:%d since table %s tag schema column count %d is not equal to %d, version:%" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pOldTagSchema->nCols, pNewTagSchema->nCols, + version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(TSDB_CODE_INVALID_MSG); + } + + // if (pOldTagSchema->version >= pNewTagSchema->version) { + // metaError("vgId:%d, %s failed at %s:%d since table %s tag schema version %d is not less than %d, version:%" + // PRId64, + // TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pOldTagSchema->version, + // pNewTagSchema->version, version); + // metaFetchEntryFree(&pEntry); + // TAOS_RETURN(TSDB_CODE_INVALID_MSG); + // } + + int32_t numOfChangedTags = 0; + for (int32_t i = 0; i < pOldTagSchema->nCols; i++) { + SSchema *pOldColumn = pOldTagSchema->pSchema + i; + SSchema *pNewColumn = pNewTagSchema->pSchema + i; + + if (pOldColumn->type != pNewColumn->type || pOldColumn->colId != pNewColumn->colId || + strncmp(pOldColumn->name, pNewColumn->name, sizeof(pNewColumn->name))) { + metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(TSDB_CODE_INVALID_MSG); + } + + if (IS_IDX_ON(pNewColumn) && !IS_IDX_ON(pOldColumn)) { + numOfChangedTags++; + SSCHMEA_SET_IDX_ON(pOldColumn); + } else if (!IS_IDX_ON(pNewColumn) && IS_IDX_ON(pOldColumn)) { + metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(TSDB_CODE_INVALID_MSG); + } + } + + if (numOfChangedTags != 1) { + metaError( + "vgId:%d, %s failed at %s:%d since table %s tag schema column count %d is not equal to 1, version:%" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, numOfChangedTags, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(TSDB_CODE_INVALID_MSG); + } + + pEntry->version = version; + pEntry->stbEntry.schemaTag.version = pNewTagSchema->version; + + // do handle the entry + code = metaHandleEntry2(pMeta, pEntry); + 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), pEntry->uid, pReq->name, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(TSDB_CODE_INVALID_MSG); + } else { + metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name, + pEntry->uid, version); + } + + metaFetchEntryFree(&pEntry); + TAOS_RETURN(code); +} + +int32_t metaDropIndexFromSuperTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq) { + int32_t code = TSDB_CODE_SUCCESS; + + if (strlen(pReq->colName) == 0 || strlen(pReq->stb) == 0) { + metaError("vgId:%d, %s failed at %s:%d since invalid table name or column name, version:%" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, version); + TAOS_RETURN(TSDB_CODE_INVALID_MSG); + } + + SMetaEntry *pEntry = NULL; + code = metaFetchEntryByUid(pMeta, pReq->stbUid, &pEntry); + if (code) { + metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, + __FILE__, __LINE__, pReq->stb, version); + TAOS_RETURN(code); + } + + if (TSDB_SUPER_TABLE != pEntry->type) { + metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode), + __func__, __FILE__, __LINE__, pReq->stb, pEntry->type, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION); + } + + SSchemaWrapper *pTagSchema = &pEntry->stbEntry.schemaTag; + if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) { + metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode), + __func__, __FILE__, __LINE__, pReq->stb, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION); + } + + // search and set the tag index off + int32_t numOfChangedTags = 0; + for (int32_t i = 0; i < pTagSchema->nCols; i++) { + SSchema *pCol = pTagSchema->pSchema + i; + if (0 == strncmp(pCol->name, pReq->colName, sizeof(pReq->colName))) { + if (!IS_IDX_ON(pCol)) { + metaError("vgId:%d, %s failed at %s:%d since table %s column %s is not indexed, version:%" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->stb, pReq->colName, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION); + } + numOfChangedTags++; + SSCHMEA_SET_IDX_OFF(pCol); + break; + } + } + + if (numOfChangedTags != 1) { + metaError("vgId:%d, %s failed at %s:%d since table %s column %s is not found, version:%" PRId64, + TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->stb, pReq->colName, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS); + } + + // do handle the entry + pEntry->version = version; + pTagSchema->version++; + code = metaHandleEntry2(pMeta, pEntry); + 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), pEntry->uid, pReq->stb, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(code); + } else { + metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->stb, + pEntry->uid, version); + } + + metaFetchEntryFree(&pEntry); + TAOS_RETURN(code); +} + +int32_t metaAlterSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) { + int32_t code = TSDB_CODE_SUCCESS; + + if (NULL == pReq->name || strlen(pReq->name) == 0) { + metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, + __FILE__, __LINE__, version); + TAOS_RETURN(TSDB_CODE_INVALID_MSG); + } + + SMetaEntry *pEntry = NULL; + code = metaFetchEntryByName(pMeta, pReq->name, &pEntry); + if (code) { + 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); + TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST); + } + + if (pEntry->type != TSDB_SUPER_TABLE) { + metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode), + __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION); + } + + SMetaEntry entry = { + .version = version, + .type = TSDB_SUPER_TABLE, + .uid = pReq->suid, + .name = pReq->name, + .stbEntry.schemaRow = pReq->schemaRow, + .stbEntry.schemaTag = pReq->schemaTag, + .colCmpr = pReq->colCmpr, + }; + TABLE_SET_COL_COMPRESSED(entry.flags); + + // do handle the entry + 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->suid, pReq->name, version); + metaFetchEntryFree(&pEntry); + TAOS_RETURN(code); + } else { + metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name, + pReq->suid, version); + } + + metaFetchEntryFree(&pEntry); + TAOS_RETURN(code); +} + +int32_t metaDropMultipleTables(SMeta *pMeta, int64_t version, SArray *uidArray) { + int32_t code = 0; + + if (taosArrayGetSize(uidArray) == 0) { + return TSDB_CODE_SUCCESS; + } + + for (int32_t i = 0; i < taosArrayGetSize(uidArray); i++) { + tb_uid_t uid = *(tb_uid_t *)taosArrayGet(uidArray, i); + SMetaInfo info; + code = metaGetInfo(pMeta, uid, &info, NULL); + if (code) { + metaError("vgId:%d, %s failed at %s:%d since table uid %" PRId64 " not found, code:%d", TD_VID(pMeta->pVnode), + __func__, __FILE__, __LINE__, uid, code); + return code; + } + + SMetaEntry entry = { + .version = version, + .uid = uid, + }; + + if (info.suid == 0) { + entry.type = -TSDB_NORMAL_TABLE; + } else if (info.suid == uid) { + entry.type = -TSDB_SUPER_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 " version:%" PRId64, TD_VID(pMeta->pVnode), + __func__, __FILE__, __LINE__, tstrerror(code), uid, version); + return code; + } + } + return code; +} \ No newline at end of file diff --git a/source/dnode/vnode/src/sma/smaTimeRange.c b/source/dnode/vnode/src/sma/smaTimeRange.c index 86246aace1..c9868f0398 100644 --- a/source/dnode/vnode/src/sma/smaTimeRange.c +++ b/source/dnode/vnode/src/sma/smaTimeRange.c @@ -129,7 +129,7 @@ static int32_t tdProcessTSmaCreateImpl(SSma *pSma, int64_t ver, const char *pMsg pReq.schemaRow = pCfg->schemaRow; pReq.schemaTag = pCfg->schemaTag; - TAOS_CHECK_EXIT(metaCreateSTable(SMA_META(pSma), ver, &pReq)); + TAOS_CHECK_EXIT(metaCreateSuperTable(SMA_META(pSma), ver, &pReq)); } else { TAOS_CHECK_EXIT(TSDB_CODE_TSMA_INVALID_STAT); } @@ -204,7 +204,7 @@ int32_t smaBlockToSubmit(SVnode *pVnode, const SArray *pBlocks, const STSchema * continue; } - if( taosArrayPush(pReq->aSubmitTbData, &tbData) == NULL) { + if (taosArrayPush(pReq->aSubmitTbData, &tbData) == NULL) { code = terrno; continue; } diff --git a/source/dnode/vnode/src/tsdb/tsdbCache.c b/source/dnode/vnode/src/tsdb/tsdbCache.c index ce84f08d3a..8fd0d47969 100644 --- a/source/dnode/vnode/src/tsdb/tsdbCache.c +++ b/source/dnode/vnode/src/tsdb/tsdbCache.c @@ -1050,7 +1050,7 @@ _exit: TAOS_RETURN(code); } -int32_t tsdbCacheNewTable(STsdb *pTsdb, tb_uid_t uid, tb_uid_t suid, SSchemaWrapper *pSchemaRow) { +int32_t tsdbCacheNewTable(STsdb *pTsdb, tb_uid_t uid, tb_uid_t suid, const SSchemaWrapper *pSchemaRow) { int32_t code = 0; (void)taosThreadMutexLock(&pTsdb->lruMutex); diff --git a/source/dnode/vnode/src/vnd/vnodeSvr.c b/source/dnode/vnode/src/vnd/vnodeSvr.c index d9b41869c7..5ecea42d2f 100644 --- a/source/dnode/vnode/src/vnd/vnodeSvr.c +++ b/source/dnode/vnode/src/vnd/vnodeSvr.c @@ -491,7 +491,7 @@ static int32_t vnodePreProcessArbCheckSyncMsg(SVnode *pVnode, SRpcMsg *pMsg) { return code; } -int32_t vnodePreProcessDropTbMsg(SVnode* pVnode, SRpcMsg* pMsg) { +int32_t vnodePreProcessDropTbMsg(SVnode *pVnode, SRpcMsg *pMsg) { int32_t code = TSDB_CODE_SUCCESS; int32_t lino = 0; int32_t size = 0; @@ -514,14 +514,14 @@ int32_t vnodePreProcessDropTbMsg(SVnode* pVnode, SRpcMsg* pMsg) { } for (int32_t i = 0; i < receivedBatchReqs.nReqs; ++i) { - SVDropTbReq* pReq = receivedBatchReqs.pReqs + i; - tb_uid_t uid = metaGetTableEntryUidByName(pVnode->pMeta, pReq->name); + SVDropTbReq *pReq = receivedBatchReqs.pReqs + i; + tb_uid_t uid = metaGetTableEntryUidByName(pVnode->pMeta, pReq->name); if (uid == 0) { vWarn("vgId:%d, preprocess drop ctb: %s not found", TD_VID(pVnode), pReq->name); continue; } pReq->uid = uid; - vDebug("vgId:%d %s for: %s, uid: %"PRId64, TD_VID(pVnode), __func__, pReq->name, pReq->uid); + vDebug("vgId:%d %s for: %s, uid: %" PRId64, TD_VID(pVnode), __func__, pReq->name, pReq->uid); if (taosArrayPush(sentBatchReqs.pArray, pReq) == NULL) { code = terrno; goto _exit; @@ -1038,7 +1038,7 @@ static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, } if (ttlReq.nUids > 0) { - int32_t code = metaDropTables(pVnode->pMeta, ttlReq.pTbUids); + int32_t code = metaDropMultipleTables(pVnode->pMeta, ver, ttlReq.pTbUids); if (code) return code; code = tqUpdateTbUidList(pVnode->pTq, ttlReq.pTbUids, false); @@ -1150,7 +1150,7 @@ static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, goto _err; } - code = metaCreateSTable(pVnode->pMeta, ver, &req); + code = metaCreateSuperTable(pVnode->pMeta, ver, &req); if (code) { pRsp->code = code; goto _err; @@ -1238,7 +1238,7 @@ static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, } // do create table - if (metaCreateTable(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) { + if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) { if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) { cRsp.code = TSDB_CODE_SUCCESS; } else { @@ -1344,7 +1344,7 @@ static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, return code; } - code = metaAlterSTable(pVnode->pMeta, ver, &req); + code = metaAlterSuperTable(pVnode->pMeta, ver, &req); if (code) { pRsp->code = code; tDecoderClear(&dc); @@ -1376,7 +1376,7 @@ static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, i // process request tbUidList = taosArrayInit(8, sizeof(int64_t)); if (tbUidList == NULL) goto _exit; - if (metaDropSTable(pVnode->pMeta, ver, &req, tbUidList) < 0) { + if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) { rcode = terrno; goto _exit; } @@ -1490,7 +1490,7 @@ static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, in tb_uid_t tbUid = 0; /* code */ - ret = metaDropTable(pVnode->pMeta, ver, pDropTbReq, tbUids, &tbUid); + ret = metaDropTable2(pVnode->pMeta, ver, pDropTbReq); if (ret < 0) { if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) { dropTbRsp.code = TSDB_CODE_SUCCESS; @@ -1966,7 +1966,7 @@ static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, in SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pSubmitRsp->aCreateTbRsp, 1); // create table - if (metaCreateTable(pVnode->pMeta, ver, pSubmitTbData->pCreateTbReq, &pCreateTbRsp->pMeta) == 0) { + if (metaCreateTable2(pVnode->pMeta, ver, pSubmitTbData->pCreateTbReq, &pCreateTbRsp->pMeta) == 0) { // create table success if (newTbUids == NULL && @@ -2433,7 +2433,7 @@ static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pRe return terrno = TSDB_CODE_INVALID_MSG; } - code = metaAddIndexToSTable(pVnode->pMeta, ver, &req); + code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req); if (code) { pRsp->code = code; goto _err; @@ -2458,7 +2458,7 @@ static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, return code; } - code = metaDropIndexFromSTable(pVnode->pMeta, ver, &req); + code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req); if (code) { pRsp->code = code; return code; @@ -2584,4 +2584,3 @@ _OVER: int32_t vnodeAsyncCompact(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) { return 0; } int32_t tsdbAsyncCompact(STsdb *tsdb, const STimeWindow *tw, bool sync) { return 0; } #endif - diff --git a/source/libs/monitor/src/monFramework.c b/source/libs/monitor/src/monFramework.c index 0dbf6e091a..56743ca5d7 100644 --- a/source/libs/monitor/src/monFramework.c +++ b/source/libs/monitor/src/monFramework.c @@ -778,7 +778,7 @@ void monGenVnodeRoleTable(SMonInfo *pMonitor){ void monSendPromReport() { char ts[50] = {0}; - sprintf(ts, "%" PRId64, taosGetTimestamp(TSDB_TIME_PRECISION_MILLI)); + (void)tsnprintf(ts, 50, "%" PRId64, taosGetTimestamp(TSDB_TIME_PRECISION_MILLI)); char* promStr = NULL; char* pCont = (char *)taos_collector_registry_bridge_new(TAOS_COLLECTOR_REGISTRY_DEFAULT, ts, "%" PRId64, &promStr); @@ -794,7 +794,7 @@ void monSendPromReport() { if (pCont != NULL) { EHttpCompFlag flag = tsMonitor.cfg.comp ? HTTP_GZIP : HTTP_FLAT; char tmp[100] = {0}; - (void)sprintf(tmp, "0x%" PRIxLEAST64, tGenQid64(tsMonitor.dnodeId)); + (void)tsnprintf(tmp, 100, "0x%" PRIxLEAST64, tGenQid64(tsMonitor.dnodeId)); uDebug("report cont with QID:%s", tmp); if (taosSendHttpReportWithQID(tsMonitor.cfg.server, tsMonFwUri, tsMonitor.cfg.port, pCont, strlen(pCont), flag, tmp) != 0) { diff --git a/source/libs/monitor/src/monMain.c b/source/libs/monitor/src/monMain.c index 744177b7a1..ef112d5c18 100644 --- a/source/libs/monitor/src/monMain.c +++ b/source/libs/monitor/src/monMain.c @@ -248,7 +248,7 @@ static void monGenBasicJsonBasic(SMonInfo *pMonitor) { SJson *pJson = pMonitor->pJson; char buf[40] = {0}; - sprintf(buf, "%" PRId64, taosGetTimestamp(TSDB_TIME_PRECISION_MILLI)); + (void)tsnprintf(buf, 40, "%" PRId64, taosGetTimestamp(TSDB_TIME_PRECISION_MILLI)); if (tjsonAddStringToObject(pJson, "ts", buf) != 0) uError("failed to add ts"); if (tjsonAddDoubleToObject(pJson, "dnode_id", pInfo->dnode_id) != 0) uError("failed to add dnode_id"); if (tjsonAddStringToObject(pJson, "dnode_ep", pInfo->dnode_ep) != 0) uError("failed to add dnode_ep"); @@ -643,7 +643,7 @@ void monSendReportBasic(SMonInfo *pMonitor) { if (pCont != NULL) { EHttpCompFlag flag = tsMonitor.cfg.comp ? HTTP_GZIP : HTTP_FLAT; char tmp[100] = {0}; - (void)sprintf(tmp, "0x%" PRIxLEAST64, tGenQid64(tsMonitor.dnodeId)); + (void)tsnprintf(tmp, 100, "0x%" PRIxLEAST64, tGenQid64(tsMonitor.dnodeId)); uDebug("report cont basic with QID:%s", tmp); if (taosSendHttpReportWithQID(tsMonitor.cfg.server, tsMonFwBasicUri, tsMonitor.cfg.port, pCont, strlen(pCont), flag, tmp) != 0) { @@ -698,7 +698,7 @@ void monSendContent(char *pCont, const char *uri) { } if (pCont != NULL) { char tmp[100] = {0}; - (void)sprintf(tmp, "0x%" PRIxLEAST64, tGenQid64(tsMonitor.dnodeId)); + (void)tsnprintf(tmp, 100, "0x%" PRIxLEAST64, tGenQid64(tsMonitor.dnodeId)); uInfoL("report client cont with QID:%s", tmp); EHttpCompFlag flag = tsMonitor.cfg.comp ? HTTP_GZIP : HTTP_FLAT; if (taosSendHttpReportWithQID(tsMonitor.cfg.server, uri, tsMonitor.cfg.port, pCont, strlen(pCont), flag, tmp) != 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; diff --git a/source/libs/wal/src/walMeta.c b/source/libs/wal/src/walMeta.c index 78f13a58ab..3faeb53499 100644 --- a/source/libs/wal/src/walMeta.c +++ b/source/libs/wal/src/walMeta.c @@ -39,11 +39,11 @@ int64_t FORCE_INLINE walGetCommittedVer(SWal* pWal) { return pWal->vers.commitVe int64_t FORCE_INLINE walGetAppliedVer(SWal* pWal) { return pWal->vers.appliedVer; } static FORCE_INLINE int walBuildMetaName(SWal* pWal, int metaVer, char* buf) { - return sprintf(buf, "%s/meta-ver%d", pWal->path, metaVer); + return snprintf(buf, WAL_FILE_LEN, "%s/meta-ver%d", pWal->path, metaVer); } static FORCE_INLINE int walBuildTmpMetaName(SWal* pWal, char* buf) { - return sprintf(buf, "%s/meta-ver.tmp", pWal->path); + return snprintf(buf, WAL_FILE_LEN, "%s/meta-ver.tmp", pWal->path); } static FORCE_INLINE int32_t walScanLogGetLastVer(SWal* pWal, int32_t fileIdx, int64_t* lastVer) { @@ -819,7 +819,7 @@ int32_t walRollFileInfo(SWal* pWal) { } int32_t walMetaSerialize(SWal* pWal, char** serialized) { - char buf[30]; + char buf[WAL_JSON_BUF_SIZE]; int sz = taosArrayGetSize(pWal->fileInfoSet); cJSON* pRoot = cJSON_CreateObject(); cJSON* pMeta = cJSON_CreateObject(); @@ -841,19 +841,19 @@ int32_t walMetaSerialize(SWal* pWal, char** serialized) { if (!cJSON_AddItemToObject(pRoot, "meta", pMeta)) { wInfo("vgId:%d, failed to add meta to root", pWal->cfg.vgId); } - (void)sprintf(buf, "%" PRId64, pWal->vers.firstVer); + snprintf(buf, WAL_JSON_BUF_SIZE, "%" PRId64, pWal->vers.firstVer); if (cJSON_AddStringToObject(pMeta, "firstVer", buf) == NULL) { wInfo("vgId:%d, failed to add firstVer to meta", pWal->cfg.vgId); } - (void)sprintf(buf, "%" PRId64, pWal->vers.snapshotVer); + (void)snprintf(buf, WAL_JSON_BUF_SIZE, "%" PRId64, pWal->vers.snapshotVer); if (cJSON_AddStringToObject(pMeta, "snapshotVer", buf) == NULL) { wInfo("vgId:%d, failed to add snapshotVer to meta", pWal->cfg.vgId); } - (void)sprintf(buf, "%" PRId64, pWal->vers.commitVer); + (void)snprintf(buf, WAL_JSON_BUF_SIZE, "%" PRId64, pWal->vers.commitVer); if (cJSON_AddStringToObject(pMeta, "commitVer", buf) == NULL) { wInfo("vgId:%d, failed to add commitVer to meta", pWal->cfg.vgId); } - (void)sprintf(buf, "%" PRId64, pWal->vers.lastVer); + (void)snprintf(buf, WAL_JSON_BUF_SIZE, "%" PRId64, pWal->vers.lastVer); if (cJSON_AddStringToObject(pMeta, "lastVer", buf) == NULL) { wInfo("vgId:%d, failed to add lastVer to meta", pWal->cfg.vgId); } @@ -874,23 +874,23 @@ int32_t walMetaSerialize(SWal* pWal, char** serialized) { } // cjson only support int32_t or double // string are used to prohibit the loss of precision - (void)sprintf(buf, "%" PRId64, pInfo->firstVer); + (void)snprintf(buf, WAL_JSON_BUF_SIZE, "%" PRId64, pInfo->firstVer); if (cJSON_AddStringToObject(pField, "firstVer", buf) == NULL) { wInfo("vgId:%d, failed to add firstVer to field", pWal->cfg.vgId); } - (void)sprintf(buf, "%" PRId64, pInfo->lastVer); + (void)snprintf(buf, WAL_JSON_BUF_SIZE, "%" PRId64, pInfo->lastVer); if (cJSON_AddStringToObject(pField, "lastVer", buf) == NULL) { wInfo("vgId:%d, failed to add lastVer to field", pWal->cfg.vgId); } - (void)sprintf(buf, "%" PRId64, pInfo->createTs); + (void)snprintf(buf, WAL_JSON_BUF_SIZE, "%" PRId64, pInfo->createTs); if (cJSON_AddStringToObject(pField, "createTs", buf) == NULL) { wInfo("vgId:%d, failed to add createTs to field", pWal->cfg.vgId); } - (void)sprintf(buf, "%" PRId64, pInfo->closeTs); + (void)snprintf(buf, WAL_JSON_BUF_SIZE, "%" PRId64, pInfo->closeTs); if (cJSON_AddStringToObject(pField, "closeTs", buf) == NULL) { wInfo("vgId:%d, failed to add closeTs to field", pWal->cfg.vgId); } - (void)sprintf(buf, "%" PRId64, pInfo->fileSize); + (void)snprintf(buf, WAL_JSON_BUF_SIZE, "%" PRId64, pInfo->fileSize); if (cJSON_AddStringToObject(pField, "fileSize", buf) == NULL) { wInfo("vgId:%d, failed to add fileSize to field", pWal->cfg.vgId); } diff --git a/source/util/src/tconfig.c b/source/util/src/tconfig.c index b91334944d..ee88996c29 100644 --- a/source/util/src/tconfig.c +++ b/source/util/src/tconfig.c @@ -1201,7 +1201,7 @@ int32_t cfgLoadFromEnvVar(SConfig *pConfig) { tstrncpy(line, *pEnv, sizeof(line)); pEnv++; - if (taosEnvToCfg(line, line) < 0) { + if (taosEnvToCfg(line, line, 1024) < 0) { uTrace("failed to convert env to cfg:%s", line); } @@ -1246,7 +1246,7 @@ int32_t cfgLoadFromEnvCmd(SConfig *pConfig, const char **envCmd) { while (envCmd[index] != NULL) { tstrncpy(buf, envCmd[index], sizeof(buf)); buf[sizeof(buf) - 1] = 0; - if (taosEnvToCfg(buf, buf) < 0) { + if (taosEnvToCfg(buf, buf, 1024) < 0) { uTrace("failed to convert env to cfg:%s", buf); } index++; @@ -1320,7 +1320,7 @@ int32_t cfgLoadFromEnvFile(SConfig *pConfig, const char *envFile) { break; } if (line[_bytes - 1] == '\n') line[_bytes - 1] = 0; - if (taosEnvToCfg(line, line) < 0) { + if (taosEnvToCfg(line, line, 1024) < 0) { uTrace("failed to convert env to cfg:%s", line); } diff --git a/source/util/src/tenv.c b/source/util/src/tenv.c index 2108f05c5b..967b1282a9 100644 --- a/source/util/src/tenv.c +++ b/source/util/src/tenv.c @@ -14,8 +14,8 @@ */ #define _DEFAULT_SOURCE -#include "tenv.h" #include "tconfig.h" +#include "tenv.h" static char toLowChar(char c) { return (c > 'Z' || c < 'A' ? c : (c - 'A' + 'a')); } @@ -50,12 +50,12 @@ int32_t taosEnvNameToCfgName(const char *envNameStr, char *cfgNameStr, int32_t c return strlen(cfgNameStr); } -int32_t taosEnvToCfg(const char *envStr, char *cfgStr) { +int32_t taosEnvToCfg(const char *envStr, char *cfgStr, int32_t cfgStrLen) { if (envStr == NULL || cfgStr == NULL) { return TSDB_CODE_INVALID_PARA; } if (cfgStr != envStr) { - tstrncpy(cfgStr, envStr, strlen(envStr) + 1); + tstrncpy(cfgStr, envStr, cfgStrLen); } char *p = strchr(cfgStr, '=');