From 4de8d28f639b502b038247d01d2fca8648c7acbc Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Fri, 29 Jul 2022 18:35:23 +0800 Subject: [PATCH 1/7] fix: fix ucs4 convertion issue --- include/os/osString.h | 2 + source/client/src/clientEnv.c | 2 + source/client/src/clientHb.c | 1 + source/client/src/clientMain.c | 2 + source/libs/scheduler/src/schJob.c | 2 +- source/os/src/osString.c | 68 ++++++++++++++++++++++++++++-- 6 files changed, 72 insertions(+), 5 deletions(-) diff --git a/include/os/osString.h b/include/os/osString.h index 3a4ff18694..74d5e52a1b 100644 --- a/include/os/osString.h +++ b/include/os/osString.h @@ -62,6 +62,8 @@ typedef int32_t TdUcs4; int32_t taosUcs4len(TdUcs4 *ucs4); int64_t taosStr2int64(const char *str); +int32_t taosConvInit(int32_t maxNum); +void taosConvDestroy(); int32_t taosUcs4ToMbs(TdUcs4 *ucs4, int32_t ucs4_max_len, char *mbs); bool taosMbsToUcs4(const char *mbs, size_t mbs_len, TdUcs4 *ucs4, int32_t ucs4_max_len, int32_t *len); int32_t tasoUcs4Compare(TdUcs4 *f1_ucs4, TdUcs4 *f2_ucs4, int32_t bytes); diff --git a/source/client/src/clientEnv.c b/source/client/src/clientEnv.c index 893aa39ce3..c012533056 100644 --- a/source/client/src/clientEnv.c +++ b/source/client/src/clientEnv.c @@ -361,6 +361,8 @@ void taos_init_imp(void) { initQueryModuleMsgHandle(); + taosConvInit(256); + rpcInit(); SCatalogCfg cfg = {.maxDBCacheNum = 100, .maxTblCacheNum = 100}; diff --git a/source/client/src/clientHb.c b/source/client/src/clientHb.c index 3d6cf1c626..06bd3f3887 100644 --- a/source/client/src/clientHb.c +++ b/source/client/src/clientHb.c @@ -353,6 +353,7 @@ int32_t hbBuildQueryDesc(SQueryHbReqBasic *hbBasic, STscObj *pObj) { desc.subDesc = NULL; desc.subPlanNum = 0; } + desc.subPlanNum = taosArrayGetSize(desc.subDesc); ASSERT(desc.subPlanNum == taosArrayGetSize(desc.subDesc)); } else { desc.subDesc = NULL; diff --git a/source/client/src/clientMain.c b/source/client/src/clientMain.c index b04b4ea2aa..a4eaf057d1 100644 --- a/source/client/src/clientMain.c +++ b/source/client/src/clientMain.c @@ -75,6 +75,8 @@ void taos_cleanup(void) { cleanupTaskQueue(); + taosConvDestroy(); + tscInfo("all local resources released"); taosCleanupCfg(); taosCloseLog(); diff --git a/source/libs/scheduler/src/schJob.c b/source/libs/scheduler/src/schJob.c index 46140ccdff..82146c3741 100644 --- a/source/libs/scheduler/src/schJob.c +++ b/source/libs/scheduler/src/schJob.c @@ -278,7 +278,7 @@ int32_t schValidateAndBuildJob(SQueryPlan *pDag, SSchJob *pJob) { } SHashObj *planToTask = taosHashInit( - SCHEDULE_DEFAULT_MAX_TASK_NUM, + pDag->numOfSubplans, taosGetDefaultHashFunction(POINTER_BYTES == sizeof(int64_t) ? TSDB_DATA_TYPE_BIGINT : TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK); if (NULL == planToTask) { diff --git a/source/os/src/osString.c b/source/os/src/osString.c index 32f0fdf7b3..d6b0bafe8f 100644 --- a/source/os/src/osString.c +++ b/source/os/src/osString.c @@ -134,21 +134,81 @@ int32_t taosUcs4ToMbs(TdUcs4 *ucs4, int32_t ucs4_max_len, char *mbs) { #endif } +typedef struct { + iconv_t conv; + int8_t inUse; +} SConv; + +SConv *gConv = NULL; +int32_t convUsed = 0; +int32_t gConvMaxNum = 0; + +int32_t taosConvInit(int32_t maxNum) { + gConvMaxNum = maxNum * 2; + gConv = taosMemoryCalloc(gConvMaxNum, sizeof(SConv)); + for (int32_t i = 0; i < gConvMaxNum; ++i) { + gConv[i].conv = iconv_open(DEFAULT_UNICODE_ENCODEC, tsCharset); + } + + return 0; +} + +void taosConvDestroy() { + for (int32_t i = 0; i < gConvMaxNum; ++i) { + iconv_close(gConv[i].conv); + } + taosMemoryFreeClear(gConv); +} + +void taosAcquireConv(int32_t *idx) { + while (true) { + int32_t used = atomic_add_fetch_32(&convUsed, 1); + if (used > gConvMaxNum) { + used = atomic_sub_fetch_32(&convUsed, 1); + sched_yield(); + continue; + } + + break; + } + + int32_t startId = taosGetSelfPthreadId() % gConvMaxNum; + while (true) { + if (gConv[startId].inUse) { + startId = (startId + 1) % gConvMaxNum; + continue; + } + + int8_t old = atomic_val_compare_exchange_8(&gConv[startId].inUse, 0, 1); + if (0 == old) { + break; + } + } + + *idx = startId; +} + +void taosReleaseConv(int32_t idx) { + atomic_store_8(&gConv[idx].inUse, 0); +} + bool taosMbsToUcs4(const char *mbs, size_t mbsLength, TdUcs4 *ucs4, int32_t ucs4_max_len, int32_t *len) { #ifdef DISALLOW_NCHAR_WITHOUT_ICONV printf("Nchar cannot be read and written without iconv, please install iconv library and recompile TDengine.\n"); return -1; #else memset(ucs4, 0, ucs4_max_len); - iconv_t cd = iconv_open(DEFAULT_UNICODE_ENCODEC, tsCharset); + + int32_t idx = 0; + taosAcquireConv(&idx); size_t ucs4_input_len = mbsLength; size_t outLeft = ucs4_max_len; - if (iconv(cd, (char **)&mbs, &ucs4_input_len, (char **)&ucs4, &outLeft) == -1) { - iconv_close(cd); + if (iconv(gConv[idx].conv, (char **)&mbs, &ucs4_input_len, (char **)&ucs4, &outLeft) == -1) { + taosReleaseConv(idx); return false; } - iconv_close(cd); + taosReleaseConv(idx); if (len != NULL) { *len = (int32_t)(ucs4_max_len - outLeft); if (*len < 0) { From c2e4110ae1f43fad70fded02031408d7533c88b6 Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Fri, 29 Jul 2022 19:49:45 +0800 Subject: [PATCH 2/7] fix: fix mbs2ucs4 issue --- source/os/src/osString.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/source/os/src/osString.c b/source/os/src/osString.c index d6b0bafe8f..0642bd768b 100644 --- a/source/os/src/osString.c +++ b/source/os/src/osString.c @@ -161,6 +161,13 @@ void taosConvDestroy() { } void taosAcquireConv(int32_t *idx) { + if (0 == gConvMaxNum) { + gConv = taosMemoryCalloc(1, sizeof(SConv)); + gConv[0].conv = iconv_open(DEFAULT_UNICODE_ENCODEC, tsCharset); + *idx = 0; + return; + } + while (true) { int32_t used = atomic_add_fetch_32(&convUsed, 1); if (used > gConvMaxNum) { @@ -189,6 +196,12 @@ void taosAcquireConv(int32_t *idx) { } void taosReleaseConv(int32_t idx) { + if (0 == gConvMaxNum) { + iconv_close(gConv[0].conv); + taosMemoryFreeClear(gConv); + return; + } + atomic_store_8(&gConv[idx].inUse, 0); } From e3b73ba5849f9e7ecf4366a7ae0687044224f695 Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Sat, 30 Jul 2022 08:34:16 +0800 Subject: [PATCH 3/7] fix: fix release conv handle issue --- source/os/src/osString.c | 1 + 1 file changed, 1 insertion(+) diff --git a/source/os/src/osString.c b/source/os/src/osString.c index 0642bd768b..329b039084 100644 --- a/source/os/src/osString.c +++ b/source/os/src/osString.c @@ -203,6 +203,7 @@ void taosReleaseConv(int32_t idx) { } atomic_store_8(&gConv[idx].inUse, 0); + atomic_sub_fetch_32(&convUsed, 1); } bool taosMbsToUcs4(const char *mbs, size_t mbsLength, TdUcs4 *ucs4, int32_t ucs4_max_len, int32_t *len) { From 9fbefbb16748355b58842980dc2bb83666d17f3c Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Sat, 30 Jul 2022 09:00:21 +0800 Subject: [PATCH 4/7] fix: fix task queue thread issue --- source/common/src/tglobal.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/source/common/src/tglobal.c b/source/common/src/tglobal.c index 0cc4e31aed..ba7f9f93c1 100644 --- a/source/common/src/tglobal.c +++ b/source/common/src/tglobal.c @@ -49,7 +49,7 @@ int32_t tsNumOfShmThreads = 1; // queue & threads int32_t tsNumOfRpcThreads = 1; int32_t tsNumOfCommitThreads = 2; -int32_t tsNumOfTaskQueueThreads = 1; +int32_t tsNumOfTaskQueueThreads = 4; int32_t tsNumOfMnodeQueryThreads = 4; int32_t tsNumOfMnodeFetchThreads = 1; int32_t tsNumOfMnodeReadThreads = 1; @@ -317,9 +317,9 @@ static int32_t taosAddClientCfg(SConfig *pCfg) { if (cfgAddString(pCfg, "smlTagName", tsSmlTagName, 1) != 0) return -1; if (cfgAddBool(pCfg, "smlDataFormat", tsSmlDataFormat, 1) != 0) return -1; - tsNumOfTaskQueueThreads = tsNumOfCores / 4; - tsNumOfTaskQueueThreads = TRANGE(tsNumOfTaskQueueThreads, 1, 2); - if (cfgAddInt32(pCfg, "numOfTaskQueueThreads", tsNumOfTaskQueueThreads, 1, 1024, 0) != 0) return -1; + tsNumOfTaskQueueThreads = tsNumOfCores / 2; + tsNumOfTaskQueueThreads = TMAX(tsNumOfTaskQueueThreads, 4); + if (cfgAddInt32(pCfg, "numOfTaskQueueThreads", tsNumOfTaskQueueThreads, 4, 1024, 0) != 0) return -1; return 0; } From 2f1c9b3c1673dc6db848775cfa3936e9025a44dc Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Sat, 30 Jul 2022 09:50:19 +0800 Subject: [PATCH 5/7] enh: add local forbidden cfg processing --- include/common/tglobal.h | 1 + source/common/src/tglobal.c | 14 ++++++++++++++ source/libs/command/src/command.c | 7 +++++++ 3 files changed, 22 insertions(+) diff --git a/include/common/tglobal.h b/include/common/tglobal.h index 57d1199e17..9111728e1a 100644 --- a/include/common/tglobal.h +++ b/include/common/tglobal.h @@ -146,6 +146,7 @@ struct SConfig *taosGetCfg(); void taosSetAllDebugFlag(int32_t flag); void taosSetDebugFlag(int32_t *pFlagPtr, const char *flagName, int32_t flagVal); int32_t taosSetCfg(SConfig *pCfg, char *name); +void taosLocalCfgForbiddenToChange(char* name, bool* forbidden); #ifdef __cplusplus } diff --git a/source/common/src/tglobal.c b/source/common/src/tglobal.c index ba7f9f93c1..6f4a3060ed 100644 --- a/source/common/src/tglobal.c +++ b/source/common/src/tglobal.c @@ -594,6 +594,20 @@ static int32_t taosSetServerCfg(SConfig *pCfg) { return 0; } +void taosLocalCfgForbiddenToChange(char* name, bool* forbidden) { + int32_t len = strlen(name); + char lowcaseName[CFG_NAME_MAX_LEN + 1] = {0}; + strntolower(lowcaseName, name, TMIN(CFG_NAME_MAX_LEN, len)); + + if (strcasecmp("charset", name) == 0) { + *forbidden = true; + return; + } + + *forbidden = false; +} + + int32_t taosSetCfg(SConfig *pCfg, char *name) { int32_t len = strlen(name); char lowcaseName[CFG_NAME_MAX_LEN + 1] = {0}; diff --git a/source/libs/command/src/command.c b/source/libs/command/src/command.c index 7c95e71823..a76b457422 100644 --- a/source/libs/command/src/command.c +++ b/source/libs/command/src/command.c @@ -517,6 +517,13 @@ static int32_t execAlterLocal(SAlterLocalStmt* pStmt) { goto _return; } + bool forbidden = false; + taosLocalCfgForbiddenToChange(pStmt->config, &forbidden); + if (forbidden) { + terrno = TSDB_CODE_OPS_NOT_SUPPORT; + return terrno; + } + if (cfgSetItem(tsCfg, pStmt->config, pStmt->value, CFG_STYPE_ALTER_CMD)) { return terrno; } From 80a32d623535afdf99111c8cf0debafcbe127998 Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Sat, 30 Jul 2022 11:31:05 +0800 Subject: [PATCH 6/7] fix: fix mbs2ucs4 crash issue in taosd --- include/os/osString.h | 1 - source/client/src/clientEnv.c | 2 -- source/dnode/mgmt/node_mgmt/src/dmMgmt.c | 1 + source/os/src/osString.c | 14 +++++++------- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/include/os/osString.h b/include/os/osString.h index 74d5e52a1b..2a22c6d8ff 100644 --- a/include/os/osString.h +++ b/include/os/osString.h @@ -62,7 +62,6 @@ typedef int32_t TdUcs4; int32_t taosUcs4len(TdUcs4 *ucs4); int64_t taosStr2int64(const char *str); -int32_t taosConvInit(int32_t maxNum); void taosConvDestroy(); int32_t taosUcs4ToMbs(TdUcs4 *ucs4, int32_t ucs4_max_len, char *mbs); bool taosMbsToUcs4(const char *mbs, size_t mbs_len, TdUcs4 *ucs4, int32_t ucs4_max_len, int32_t *len); diff --git a/source/client/src/clientEnv.c b/source/client/src/clientEnv.c index c012533056..893aa39ce3 100644 --- a/source/client/src/clientEnv.c +++ b/source/client/src/clientEnv.c @@ -361,8 +361,6 @@ void taos_init_imp(void) { initQueryModuleMsgHandle(); - taosConvInit(256); - rpcInit(); SCatalogCfg cfg = {.maxDBCacheNum = 100, .maxTblCacheNum = 100}; diff --git a/source/dnode/mgmt/node_mgmt/src/dmMgmt.c b/source/dnode/mgmt/node_mgmt/src/dmMgmt.c index c2e8a55271..582b16ce99 100644 --- a/source/dnode/mgmt/node_mgmt/src/dmMgmt.c +++ b/source/dnode/mgmt/node_mgmt/src/dmMgmt.c @@ -215,6 +215,7 @@ void dmCleanupDnode(SDnode *pDnode) { dmClearVars(pDnode); rpcCleanup(); indexCleanup(); + taosConvDestroy(); dDebug("dnode is closed, ptr:%p", pDnode); } diff --git a/source/os/src/osString.c b/source/os/src/osString.c index 329b039084..503b8e0c6b 100644 --- a/source/os/src/osString.c +++ b/source/os/src/osString.c @@ -143,14 +143,17 @@ SConv *gConv = NULL; int32_t convUsed = 0; int32_t gConvMaxNum = 0; -int32_t taosConvInit(int32_t maxNum) { - gConvMaxNum = maxNum * 2; +void taosConvInitImpl(void) { + gConvMaxNum = 512; gConv = taosMemoryCalloc(gConvMaxNum, sizeof(SConv)); for (int32_t i = 0; i < gConvMaxNum; ++i) { gConv[i].conv = iconv_open(DEFAULT_UNICODE_ENCODEC, tsCharset); } +} - return 0; +static TdThreadOnce convInit = PTHREAD_ONCE_INIT; +void taosConvInit() { + taosThreadOnce(&convInit, taosConvInitImpl); } void taosConvDestroy() { @@ -162,10 +165,7 @@ void taosConvDestroy() { void taosAcquireConv(int32_t *idx) { if (0 == gConvMaxNum) { - gConv = taosMemoryCalloc(1, sizeof(SConv)); - gConv[0].conv = iconv_open(DEFAULT_UNICODE_ENCODEC, tsCharset); - *idx = 0; - return; + taosConvInit(); } while (true) { From 77837f6e6a9be731a3efd4b16c9131aaf8f77735 Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Sat, 30 Jul 2022 15:10:24 +0800 Subject: [PATCH 7/7] fix: fix crash issue --- include/os/osString.h | 1 + source/client/src/clientEnv.c | 2 ++ source/dnode/mgmt/exe/dmMain.c | 2 ++ source/os/src/osString.c | 36 +++++++++++++++++----------------- 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/include/os/osString.h b/include/os/osString.h index 2a22c6d8ff..8eb341faa7 100644 --- a/include/os/osString.h +++ b/include/os/osString.h @@ -62,6 +62,7 @@ typedef int32_t TdUcs4; int32_t taosUcs4len(TdUcs4 *ucs4); int64_t taosStr2int64(const char *str); +void taosConvInit(void); void taosConvDestroy(); int32_t taosUcs4ToMbs(TdUcs4 *ucs4, int32_t ucs4_max_len, char *mbs); bool taosMbsToUcs4(const char *mbs, size_t mbs_len, TdUcs4 *ucs4, int32_t ucs4_max_len, int32_t *len); diff --git a/source/client/src/clientEnv.c b/source/client/src/clientEnv.c index 893aa39ce3..3d539ea251 100644 --- a/source/client/src/clientEnv.c +++ b/source/client/src/clientEnv.c @@ -361,6 +361,8 @@ void taos_init_imp(void) { initQueryModuleMsgHandle(); + taosConvInit(); + rpcInit(); SCatalogCfg cfg = {.maxDBCacheNum = 100, .maxTblCacheNum = 100}; diff --git a/source/dnode/mgmt/exe/dmMain.c b/source/dnode/mgmt/exe/dmMain.c index 34c3b40556..4030eaa6fe 100644 --- a/source/dnode/mgmt/exe/dmMain.c +++ b/source/dnode/mgmt/exe/dmMain.c @@ -218,6 +218,8 @@ int mainWindows(int argc,char** argv) { taosCleanupArgs(); return -1; } + + taosConvInit(); if (global.dumpConfig) { dmDumpCfg(); diff --git a/source/os/src/osString.c b/source/os/src/osString.c index 503b8e0c6b..26aafd743f 100644 --- a/source/os/src/osString.c +++ b/source/os/src/osString.c @@ -143,29 +143,29 @@ SConv *gConv = NULL; int32_t convUsed = 0; int32_t gConvMaxNum = 0; -void taosConvInitImpl(void) { +void taosConvInit(void) { gConvMaxNum = 512; gConv = taosMemoryCalloc(gConvMaxNum, sizeof(SConv)); for (int32_t i = 0; i < gConvMaxNum; ++i) { gConv[i].conv = iconv_open(DEFAULT_UNICODE_ENCODEC, tsCharset); + if ((iconv_t)-1 == gConv[i].conv || (iconv_t)0 == gConv[i].conv) { + ASSERT(0); + } } } -static TdThreadOnce convInit = PTHREAD_ONCE_INIT; -void taosConvInit() { - taosThreadOnce(&convInit, taosConvInitImpl); -} - void taosConvDestroy() { for (int32_t i = 0; i < gConvMaxNum; ++i) { iconv_close(gConv[i].conv); } taosMemoryFreeClear(gConv); + gConvMaxNum = -1; } -void taosAcquireConv(int32_t *idx) { - if (0 == gConvMaxNum) { - taosConvInit(); +iconv_t taosAcquireConv(int32_t *idx) { + if (gConvMaxNum <= 0) { + *idx = -1; + return iconv_open(DEFAULT_UNICODE_ENCODEC, tsCharset); } while (true) { @@ -193,12 +193,12 @@ void taosAcquireConv(int32_t *idx) { } *idx = startId; + return gConv[startId].conv; } -void taosReleaseConv(int32_t idx) { - if (0 == gConvMaxNum) { - iconv_close(gConv[0].conv); - taosMemoryFreeClear(gConv); +void taosReleaseConv(int32_t idx, iconv_t conv) { + if (idx < 0) { + iconv_close(conv); return; } @@ -213,16 +213,16 @@ bool taosMbsToUcs4(const char *mbs, size_t mbsLength, TdUcs4 *ucs4, int32_t ucs4 #else memset(ucs4, 0, ucs4_max_len); - int32_t idx = 0; - taosAcquireConv(&idx); + int32_t idx = -1; + iconv_t conv = taosAcquireConv(&idx); size_t ucs4_input_len = mbsLength; size_t outLeft = ucs4_max_len; - if (iconv(gConv[idx].conv, (char **)&mbs, &ucs4_input_len, (char **)&ucs4, &outLeft) == -1) { - taosReleaseConv(idx); + if (iconv(conv, (char **)&mbs, &ucs4_input_len, (char **)&ucs4, &outLeft) == -1) { + taosReleaseConv(idx, conv); return false; } - taosReleaseConv(idx); + taosReleaseConv(idx, conv); if (len != NULL) { *len = (int32_t)(ucs4_max_len - outLeft); if (*len < 0) {