diff --git a/source/libs/function/src/udfd.c b/source/libs/function/src/udfd.c index 23a95320fbd5a972e0d4de6f8978e6b01aeb42f3..f8f44a9816f65db62e08f04a6a8b9a0ef84672e1 100644 --- a/source/libs/function/src/udfd.c +++ b/source/libs/function/src/udfd.c @@ -29,8 +29,6 @@ #include "trpc.h" // clang-foramt on -SArray* udfdResidentFuncs = NULL; - typedef struct SUdfdContext { uv_loop_t * loop; uv_pipe_t ctrlPipe; @@ -43,6 +41,8 @@ typedef struct SUdfdContext { uv_mutex_t udfsMutex; SHashObj * udfsHash; + SArray* residentFuncs; + bool printVersion; } SUdfdContext; @@ -204,8 +204,8 @@ void udfdProcessSetupRequest(SUvUdfWork *uvUdf, SUdfRequest *request) { udf->initFunc(); } udf->resident = false; - for (int32_t i = 0; i < taosArrayGetSize(udfdResidentFuncs); ++i) { - char* funcName = taosArrayGet(udfdResidentFuncs, i); + for (int32_t i = 0; i < taosArrayGetSize(global.residentFuncs); ++i) { + char* funcName = taosArrayGet(global.residentFuncs, i); if (strcmp(setup->udfName, funcName) == 0) { udf->resident = true; break; @@ -930,8 +930,6 @@ static int32_t udfdRun() { uv_run(global.loop, UV_RUN_DEFAULT); uv_loop_close(global.loop); - uv_mutex_destroy(&global.udfsMutex); - taosHashCleanup(global.udfsHash); return 0; } @@ -953,17 +951,17 @@ void udfdConnectMnodeThreadFunc(void *args) { } int32_t udfdInitResidentFuncs() { - udfdResidentFuncs = taosArrayInit(2, TSDB_FUNC_NAME_LEN); + global.residentFuncs = taosArrayInit(2, TSDB_FUNC_NAME_LEN); char gpd[TSDB_FUNC_NAME_LEN] = "gpd"; - taosArrayPush(udfdResidentFuncs, gpd); + taosArrayPush(global.residentFuncs, gpd); char gpdBatch[TSDB_FUNC_NAME_LEN] = "gpdbatch"; - taosArrayPush(udfdResidentFuncs, gpdBatch); + taosArrayPush(global.residentFuncs, gpdBatch); return TSDB_CODE_SUCCESS; } int32_t udfdDeinitResidentFuncs() { - for (int32_t i = 0; i < taosArrayGetSize(udfdResidentFuncs); ++i) { - char* funcName = taosArrayGet(udfdResidentFuncs, i); + for (int32_t i = 0; i < taosArrayGetSize(global.residentFuncs); ++i) { + char* funcName = taosArrayGet(global.residentFuncs, i); SUdf** udfInHash = taosHashGet(global.udfsHash, funcName, strlen(funcName)); if (udfInHash) { taosHashRemove(global.udfsHash, funcName, strlen(funcName)); @@ -975,9 +973,16 @@ int32_t udfdDeinitResidentFuncs() { taosMemoryFree(udf); } } + taosArrayDestroy(global.residentFuncs); return TSDB_CODE_SUCCESS; } +int32_t udfdCleanup() { + uv_mutex_destroy(&global.udfsMutex); + taosHashCleanup(global.udfsHash); + return 0; +} + int main(int argc, char *argv[]) { if (!taosCheckSystemIsLittleEnd()) { printf("failed to start since on non-little-end machines\n"); @@ -1026,5 +1031,6 @@ int main(int argc, char *argv[]) { udfdCloseClientRpc(); udfdDeinitResidentFuncs(); + udfdCleanup(); return 0; } diff --git a/tests/script/sh/gpd.c b/tests/script/sh/gpd.c index 56c3388f3ea33cf76e67fccffcb6bf2fc91a0393..8d69bacb5edac29783ce860fb3a8dd5b407541d8 100644 --- a/tests/script/sh/gpd.c +++ b/tests/script/sh/gpd.c @@ -9,12 +9,16 @@ #endif #include "taosudf.h" +TAOS* taos = NULL; DLL_EXPORT int32_t gpd_init() { + taos = taos_connect("localhost", "root", "taosdata", "", 7100); return 0; } DLL_EXPORT int32_t gpd_destroy() { + taos_close(taos); + taos_cleanup(); return 0; } @@ -40,11 +44,6 @@ DLL_EXPORT int32_t gpd(SUdfDataBlock* block, SUdfColumn *resultCol) { udfColDataSet(resultCol, i, (char *)&luckyNum, false); } } - taos_init(); - TAOS* taos = taos_connect("localhost", "root", "taosdata", "", 7100); - if (taos == NULL) { - char* errstr = "can not connect"; - } TAOS_RES* res = taos_query(taos, "create database if not exists gpd"); if (taos_errno(res) != 0) { char* errstr = taos_errstr(res); @@ -64,8 +63,6 @@ DLL_EXPORT int32_t gpd(SUdfDataBlock* block, SUdfColumn *resultCol) { char* errstr = taos_errstr(res); } - taos_close(taos); - taos_cleanup(); //to simulate actual processing delay by udf #ifdef LINUX usleep(1 * 1000); // usleep takes sleep time in us (1 millionth of a second)