From 431d4380bfee4a385b120ca9a147b1c3f623af10 Mon Sep 17 00:00:00 2001 From: xsren <285808407@qq.com> Date: Tue, 12 Sep 2023 14:12:21 +0800 Subject: [PATCH 1/5] fix read line on windows --- source/os/src/osFile.c | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/source/os/src/osFile.c b/source/os/src/osFile.c index 1177ff562e..ff0737b9ad 100644 --- a/source/os/src/osFile.c +++ b/source/os/src/osFile.c @@ -819,14 +819,33 @@ int64_t taosGetLineFile(TdFilePtr pFile, char **__restrict ptrBuf) { return -1; } #ifdef WINDOWS - *ptrBuf = taosMemoryMalloc(1024); + size_t bufferSize = 512; + *ptrBuf = taosMemoryMalloc(bufferSize); if (*ptrBuf == NULL) return -1; - if (fgets(*ptrBuf, 1023, pFile->fp) == NULL) { - taosMemoryFreeClear(*ptrBuf); - return -1; + + size_t bytesRead = 0; + size_t totalBytesRead = 0; + + while (1) { + char *result = fgets(*ptrBuf + totalBytesRead, bufferSize - totalBytesRead, pFile->fp); + if (result == NULL) { + taosMemoryFreeClear(*ptrBuf); + return -1; + } + bytesRead = strlen(*ptrBuf + totalBytesRead); + totalBytesRead += bytesRead; + + if (totalBytesRead < bufferSize - 1 || (*ptrBuf)[totalBytesRead - 1] == '\n') { + break; + } + + bufferSize += 512; + *ptrBuf = taosMemoryRealloc(*ptrBuf, bufferSize); + if (*ptrBuf == NULL) return -1; } - (*ptrBuf)[1023] = 0; - return strlen(*ptrBuf); + + (*ptrBuf)[totalBytesRead] = '\0'; + return totalBytesRead; #else size_t len = 0; return getline(ptrBuf, &len, pFile->fp); From fe6f8d60f60fb2e1254a4240b724121f499a1df0 Mon Sep 17 00:00:00 2001 From: xsren <285808407@qq.com> Date: Tue, 12 Sep 2023 18:24:50 +0800 Subject: [PATCH 2/5] free old pointer as realloc failed. --- source/os/src/osFile.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/source/os/src/osFile.c b/source/os/src/osFile.c index ff0737b9ad..a453a03994 100644 --- a/source/os/src/osFile.c +++ b/source/os/src/osFile.c @@ -840,8 +840,13 @@ int64_t taosGetLineFile(TdFilePtr pFile, char **__restrict ptrBuf) { } bufferSize += 512; - *ptrBuf = taosMemoryRealloc(*ptrBuf, bufferSize); - if (*ptrBuf == NULL) return -1; + void* newBuf = taosMemoryRealloc(*ptrBuf, bufferSize); + if (newBuf == NULL) { + taosMemoryFreeClear(*ptrBuf); + return -1; + } + + *ptrBuf = newBuf; } (*ptrBuf)[totalBytesRead] = '\0'; From 08ca751483f76f233fc348bb5a97ce150440c717 Mon Sep 17 00:00:00 2001 From: xsren <285808407@qq.com> Date: Tue, 12 Sep 2023 10:44:11 +0800 Subject: [PATCH 3/5] reset file handles limit on windows --- source/common/src/tglobal.c | 1 + source/os/src/osFile.c | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/source/common/src/tglobal.c b/source/common/src/tglobal.c index ff9e922ee1..2bcbeb5a74 100644 --- a/source/common/src/tglobal.c +++ b/source/common/src/tglobal.c @@ -1598,6 +1598,7 @@ int32_t taosInitCfg(const char *cfgDir, const char **envCmd, const char *envFile if (taosSetS3Cfg(tsCfg) != 0) return -1; } taosSetSystemCfg(tsCfg); + if (taosSetFileHandlesLimit() != 0) return -1; cfgDumpCfg(tsCfg, tsc, false); diff --git a/source/os/src/osFile.c b/source/os/src/osFile.c index a453a03994..2ecd011a95 100644 --- a/source/os/src/osFile.c +++ b/source/os/src/osFile.c @@ -956,3 +956,12 @@ cmp_end: return ret; } + +int32_t taosSetFileHandlesLimit() { +#ifdef WINDOWS + const int max_handles = 8192; + int res = _setmaxstdio(max_handles); + return res == max_handles ? 0 : -1; +#endif + return 0; +} From d14f1ebfcbe766e436e495f190f574a2dbc61a22 Mon Sep 17 00:00:00 2001 From: xsren <285808407@qq.com> Date: Tue, 12 Sep 2023 14:29:42 +0800 Subject: [PATCH 4/5] build failed on linux --- include/os/osFile.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/os/osFile.h b/include/os/osFile.h index da1f8f8b57..63483dc906 100644 --- a/include/os/osFile.h +++ b/include/os/osFile.h @@ -112,6 +112,8 @@ int32_t taosGetErrorFile(TdFilePtr pFile); int32_t taosCompressFile(char *srcFileName, char *destFileName); +int32_t taosSetFileHandlesLimit(); + #ifdef __cplusplus } #endif From 16dee56b4d81108a80b8f894aef5af130f3c57f1 Mon Sep 17 00:00:00 2001 From: kailixu Date: Tue, 12 Sep 2023 13:33:12 +0800 Subject: [PATCH 5/5] fix: set stack size to 8 MB for windows and dup fd for gzdopen --- cmake/cmake.platform | 8 ++++++++ source/os/src/osFile.c | 10 ++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/cmake/cmake.platform b/cmake/cmake.platform index 76ac6ba004..30a33fcdb4 100644 --- a/cmake/cmake.platform +++ b/cmake/cmake.platform @@ -176,6 +176,14 @@ IF(APPLE) set(THREADS_PREFER_PTHREAD_FLAG ON) ENDIF() +IF(TD_WINDOWS) + IF(MSVC) + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:8388608") + ELSEIF(MINGW) + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--stack,8388608") + ENDIF() +ENDIF() + MESSAGE(STATUS "Platform arch:" ${PLATFORM_ARCH_STR}) set(TD_DEPS_DIR "x86") diff --git a/source/os/src/osFile.c b/source/os/src/osFile.c index 2ecd011a95..d5cc272726 100644 --- a/source/os/src/osFile.c +++ b/source/os/src/osFile.c @@ -928,10 +928,16 @@ int32_t taosCompressFile(char *srcFileName, char *destFileName) { goto cmp_end; } - dstFp = gzdopen(pFile->fd, "wb6f"); + // Both gzclose() and fclose() will close the associated fd, so they need to have different fds. + FileFd gzFd = dup(pFile->fd); + if (gzFd < 0) { + ret = -4; + goto cmp_end; + } + dstFp = gzdopen(gzFd, "wb6f"); if (dstFp == NULL) { ret = -3; - taosCloseFile(&pFile); + close(gzFd); goto cmp_end; }