From 25c0b928854a9ee958e0fb83d2bc82fddf796b1b Mon Sep 17 00:00:00 2001 From: lyh250-666 Date: Wed, 11 Sep 2024 01:11:56 +0000 Subject: [PATCH] feat:Optimising the duration and keep --- include/libs/command/command.h | 1 + source/dnode/mnode/impl/src/mndDb.c | 20 +++++--- source/libs/command/src/command.c | 52 ++++++--------------- tests/script/tsim/db/create_all_options.sim | 4 +- 4 files changed, 32 insertions(+), 45 deletions(-) diff --git a/include/libs/command/command.h b/include/libs/command/command.h index b788b03386..284f54e5ae 100644 --- a/include/libs/command/command.h +++ b/include/libs/command/command.h @@ -29,5 +29,6 @@ int32_t qExecExplainBegin(SQueryPlan *pDag, SExplainCtx **pCtx, int64_t startTs) int32_t qExecExplainEnd(SExplainCtx *pCtx, SRetrieveTableRsp **pRsp); int32_t qExplainUpdateExecInfo(SExplainCtx *pCtx, SExplainRsp *pRspMsg, int32_t groupId, SRetrieveTableRsp **pRsp); void qExplainFreeCtx(SExplainCtx *pCtx); +int32_t formatDurationOrKeep(char* buffer, int32_t timeInMinutes); #endif diff --git a/source/dnode/mnode/impl/src/mndDb.c b/source/dnode/mnode/impl/src/mndDb.c index 20c5d09c9e..9c02fd6e5d 100644 --- a/source/dnode/mnode/impl/src/mndDb.c +++ b/source/dnode/mnode/impl/src/mndDb.c @@ -34,6 +34,7 @@ #include "systable.h" #include "thttp.h" #include "tjson.h" +#include "command.h" #define DB_VER_NUMBER 1 #define DB_RESERVE_SIZE 27 @@ -2321,18 +2322,25 @@ static void mndDumpDbInfoData(SMnode *pMnode, SSDataBlock *pBlock, SDbObj *pDb, (void)colDataSetVal(pColInfo, rows, (const char *)strictVstr, false); char durationVstr[128] = {0}; - int32_t len = sprintf(&durationVstr[VARSTR_HEADER_SIZE], "%dm", pDb->cfg.daysPerFile); + int32_t len = formatDurationOrKeep(&durationVstr[VARSTR_HEADER_SIZE], pDb->cfg.daysPerFile); + varDataSetLen(durationVstr, len); pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); (void)colDataSetVal(pColInfo, rows, (const char *)durationVstr, false); - char keepVstr[128] = {0}; + char keepVstr[512] = {0}; + char keep0Str[128] = {0}; + char keep1Str[128] = {0}; + char keep2Str[128] = {0}; + + formatDurationOrKeep(keep0Str, pDb->cfg.daysToKeep0); + formatDurationOrKeep(keep1Str, pDb->cfg.daysToKeep1); + formatDurationOrKeep(keep2Str, pDb->cfg.daysToKeep2); + if (pDb->cfg.daysToKeep0 > pDb->cfg.daysToKeep1 || pDb->cfg.daysToKeep0 > pDb->cfg.daysToKeep2) { - len = sprintf(&keepVstr[VARSTR_HEADER_SIZE], "%dm,%dm,%dm", pDb->cfg.daysToKeep1, pDb->cfg.daysToKeep2, - pDb->cfg.daysToKeep0); + len = sprintf(&keepVstr[VARSTR_HEADER_SIZE], "%s,%s,%s", keep1Str, keep2Str, keep0Str); } else { - len = sprintf(&keepVstr[VARSTR_HEADER_SIZE], "%dm,%dm,%dm", pDb->cfg.daysToKeep0, pDb->cfg.daysToKeep1, - pDb->cfg.daysToKeep2); + len = sprintf(&keepVstr[VARSTR_HEADER_SIZE], "%s,%s,%s", keep0Str, keep1Str, keep2Str); } varDataSetLen(keepVstr, len); pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); diff --git a/source/libs/command/src/command.c b/source/libs/command/src/command.c index 138782495f..0e5f2f4cbd 100644 --- a/source/libs/command/src/command.c +++ b/source/libs/command/src/command.c @@ -344,27 +344,18 @@ static const char* encryptAlgorithmStr(int8_t encryptAlgorithm) { return TSDB_CACHE_MODEL_NONE_STR; } -static int32_t formatDurationOrKeep(char** buffer, int32_t timeInMinutes) { +int32_t formatDurationOrKeep(char* buffer, int32_t timeInMinutes) { int len = 0; if (timeInMinutes % 1440 == 0) { int days = timeInMinutes / 1440; - len = snprintf(NULL, 0, "%dd", days); - *buffer = (char*)taosMemoryCalloc(len + 1, sizeof(char)); - if(*buffer == NULL) return terrno; - sprintf(*buffer, "%dd", days); + len = sprintf(buffer, "%dd", days); } else if (timeInMinutes % 60 == 0) { int hours = timeInMinutes / 60; - len = snprintf(NULL, 0, "%dh", hours); - *buffer = (char*)taosMemoryCalloc(len + 1, sizeof(char)); - if(*buffer == NULL) return terrno; - sprintf(*buffer, "%dh", hours); + len = sprintf(buffer, "%dh", hours); } else { - len = snprintf(NULL, 0, "%dm", timeInMinutes); - *buffer = (char*)taosMemoryCalloc(len + 1, sizeof(char)); - if(*buffer == NULL) return terrno; - sprintf(*buffer, "%dm", timeInMinutes); + len = sprintf(buffer, "%dm", timeInMinutes); } - return TSDB_CODE_SUCCESS; + return len; } static int32_t setCreateDBResultIntoDataBlock(SSDataBlock* pBlock, char* dbName, char* dbFName, SDbCfgInfo* pCfg) { @@ -404,25 +395,16 @@ static int32_t setCreateDBResultIntoDataBlock(SSDataBlock* pBlock, char* dbName, } else if (pCfg->hashPrefix < 0) { hashPrefix = pCfg->hashPrefix + dbFNameLen + 1; } - char* durationStr = NULL; - char* keep0Str = NULL; - char* keep1Str = NULL; - char* keep2Str = NULL; - int32_t codeDuration = formatDurationOrKeep(&durationStr, pCfg->daysPerFile); - int32_t codeKeep0 = formatDurationOrKeep(&keep0Str, pCfg->daysToKeep0); - int32_t codeKeep1 = formatDurationOrKeep(&keep1Str, pCfg->daysToKeep1); - int32_t codeKeep2 = formatDurationOrKeep(&keep2Str, pCfg->daysToKeep2); - if(codeDuration != TSDB_CODE_SUCCESS || codeKeep0 != TSDB_CODE_SUCCESS || codeKeep1 != TSDB_CODE_SUCCESS || codeKeep2 != TSDB_CODE_SUCCESS) { - int32_t firstErrorCode = codeDuration != TSDB_CODE_SUCCESS ? codeDuration : - codeKeep0 != TSDB_CODE_SUCCESS ? codeKeep0 : - codeKeep1 != TSDB_CODE_SUCCESS ? codeKeep1 : codeKeep2; - taosMemoryFree(pRetentions); - taosMemoryFree(durationStr); - taosMemoryFree(keep0Str); - taosMemoryFree(keep1Str); - taosMemoryFree(keep2Str); - return firstErrorCode; - } + char durationStr[128] = {0}; + char keep0Str[128] = {0}; + char keep1Str[128] = {0}; + char keep2Str[128] = {0}; + + int32_t lenDuration = formatDurationOrKeep(durationStr, pCfg->daysPerFile); + int32_t lenKeep0 = formatDurationOrKeep(keep0Str, pCfg->daysToKeep0); + int32_t lenKeep1 = formatDurationOrKeep(keep1Str, pCfg->daysToKeep1); + int32_t lenKeep2 = formatDurationOrKeep(keep2Str, pCfg->daysToKeep2); + if (IS_SYS_DBNAME(dbName)) { len += sprintf(buf2 + VARSTR_HEADER_SIZE, "CREATE DATABASE `%s`", dbName); } else { @@ -449,10 +431,6 @@ static int32_t setCreateDBResultIntoDataBlock(SSDataBlock* pBlock, char* dbName, } taosMemoryFree(pRetentions); - taosMemoryFree(durationStr); - taosMemoryFree(keep0Str); - taosMemoryFree(keep1Str); - taosMemoryFree(keep2Str); (varDataLen(buf2)) = len; diff --git a/tests/script/tsim/db/create_all_options.sim b/tests/script/tsim/db/create_all_options.sim index e4f29cc74e..e402223d93 100644 --- a/tests/script/tsim/db/create_all_options.sim +++ b/tests/script/tsim/db/create_all_options.sim @@ -92,10 +92,10 @@ endi if $data5_db != on then # strict return -1 endi -if $data6_db != 14400m then # duration +if $data6_db != 10d then # duration return -1 endi -if $data7_db != 5256000m,5256000m,5256000m then # keep +if $data7_db != 3650d,3650d,3650d then # keep return -1 endi if $data8_db != 256 then # buffer