Merge pull request #27314 from taosdata/fix/TD-31163-3.0

fix: memory leak of geos
This commit is contained in:
Hongze Cheng 2024-08-21 10:18:31 +08:00 committed by GitHub
commit 5c376d4629
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 129 additions and 45 deletions

View File

@ -32,14 +32,16 @@ typedef struct SGeosContext {
GEOSWKBReader *WKBReader; GEOSWKBReader *WKBReader;
GEOSWKBWriter *WKBWriter; GEOSWKBWriter *WKBWriter;
pcre2_code *WKTRegex; pcre2_code *WKTRegex;
pcre2_match_data *WKTMatchData; pcre2_match_data *WKTMatchData;
char errMsg[512]; char errMsg[512];
} SGeosContext; } SGeosContext;
SGeosContext* getThreadLocalGeosCtx(); SGeosContext *acquireThreadLocalGeosCtx();
void destroyThreadLocalGeosCtx(); SGeosContext *getThreadLocalGeosCtx();
const char *getGeosErrMsg(int32_t code);
void taosGeosDestroy();
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -23,6 +23,7 @@
#include "query.h" #include "query.h"
#include "scheduler.h" #include "scheduler.h"
#include "tdatablock.h" #include "tdatablock.h"
#include "tgeosctx.h"
#include "tglobal.h" #include "tglobal.h"
#include "tmsg.h" #include "tmsg.h"
#include "tref.h" #include "tref.h"
@ -86,6 +87,7 @@ void taos_cleanup(void) {
tscDebug("rpc cleanup"); tscDebug("rpc cleanup");
taosConvDestroy(); taosConvDestroy();
taosGeosDestroy();
tmqMgmtClose(); tmqMgmtClose();

View File

@ -19,6 +19,7 @@
#include "index.h" #include "index.h"
#include "qworker.h" #include "qworker.h"
#include "tcompression.h" #include "tcompression.h"
#include "tgeosctx.h"
#include "tglobal.h" #include "tglobal.h"
#include "tgrant.h" #include "tgrant.h"
#include "tstream.h" #include "tstream.h"
@ -121,6 +122,7 @@ void dmCleanupDnode(SDnode *pDnode) {
streamMetaCleanup(); streamMetaCleanup();
indexCleanup(); indexCleanup();
taosConvDestroy(); taosConvDestroy();
taosGeosDestroy();
// compress destroy // compress destroy
tsCompressExit(); tsCompressExit();

View File

@ -979,7 +979,7 @@ static int32_t sysTableGetGeomText(char* iGeom, int32_t nGeom, char** output, in
if (TSDB_CODE_SUCCESS != (code = initCtxAsText()) || if (TSDB_CODE_SUCCESS != (code = initCtxAsText()) ||
TSDB_CODE_SUCCESS != (code = doAsText(iGeom, nGeom, &outputWKT))) { TSDB_CODE_SUCCESS != (code = doAsText(iGeom, nGeom, &outputWKT))) {
qError("geo text for systable failed:%s", getThreadLocalGeosCtx()->errMsg); qError("geo text for systable failed:%s", getGeosErrMsg(code));
*output = NULL; *output = NULL;
*nOutput = 0; *nOutput = 0;
return code; return code;

View File

@ -23,7 +23,8 @@ typedef char (*_geosPreparedRelationFunc_t)(GEOSContextHandle_t handle, const GE
void geosFreeBuffer(void *buffer) { void geosFreeBuffer(void *buffer) {
if (buffer) { if (buffer) {
GEOSFree_r(getThreadLocalGeosCtx()->handle, buffer); SGeosContext *pCtx = acquireThreadLocalGeosCtx();
if (pCtx) GEOSFree_r(pCtx->handle, buffer);
} }
} }
@ -36,6 +37,8 @@ int32_t initCtxMakePoint() {
int32_t code = TSDB_CODE_FAILED; int32_t code = TSDB_CODE_FAILED;
SGeosContext *geosCtx = getThreadLocalGeosCtx(); SGeosContext *geosCtx = getThreadLocalGeosCtx();
if (!geosCtx) return TSDB_CODE_OUT_OF_MEMORY;
if (geosCtx->handle == NULL) { if (geosCtx->handle == NULL) {
geosCtx->handle = GEOS_init_r(); geosCtx->handle = GEOS_init_r();
if (geosCtx->handle == NULL) { if (geosCtx->handle == NULL) {
@ -61,6 +64,8 @@ int32_t doMakePoint(double x, double y, unsigned char **outputGeom, size_t *size
int32_t code = TSDB_CODE_FAILED; int32_t code = TSDB_CODE_FAILED;
SGeosContext *geosCtx = getThreadLocalGeosCtx(); SGeosContext *geosCtx = getThreadLocalGeosCtx();
if (!geosCtx) return TSDB_CODE_OUT_OF_MEMORY;
GEOSGeometry *geom = NULL; GEOSGeometry *geom = NULL;
unsigned char *wkb = NULL; unsigned char *wkb = NULL;
@ -166,6 +171,8 @@ int32_t initCtxGeomFromText() {
int32_t code = TSDB_CODE_FAILED; int32_t code = TSDB_CODE_FAILED;
SGeosContext *geosCtx = getThreadLocalGeosCtx(); SGeosContext *geosCtx = getThreadLocalGeosCtx();
if (!geosCtx) return TSDB_CODE_OUT_OF_MEMORY;
if (geosCtx->handle == NULL) { if (geosCtx->handle == NULL) {
geosCtx->handle = GEOS_init_r(); geosCtx->handle = GEOS_init_r();
if (geosCtx->handle == NULL) { if (geosCtx->handle == NULL) {
@ -202,6 +209,8 @@ int32_t doGeomFromText(const char *inputWKT, unsigned char **outputGeom, size_t
int32_t code = TSDB_CODE_FAILED; int32_t code = TSDB_CODE_FAILED;
SGeosContext *geosCtx = getThreadLocalGeosCtx(); SGeosContext *geosCtx = getThreadLocalGeosCtx();
if (!geosCtx) return TSDB_CODE_OUT_OF_MEMORY;
GEOSGeometry *geom = NULL; GEOSGeometry *geom = NULL;
unsigned char *wkb = NULL; unsigned char *wkb = NULL;
@ -237,6 +246,8 @@ int32_t initCtxAsText() {
int32_t code = TSDB_CODE_FAILED; int32_t code = TSDB_CODE_FAILED;
SGeosContext *geosCtx = getThreadLocalGeosCtx(); SGeosContext *geosCtx = getThreadLocalGeosCtx();
if (!geosCtx) return TSDB_CODE_OUT_OF_MEMORY;
if (geosCtx->handle == NULL) { if (geosCtx->handle == NULL) {
geosCtx->handle = GEOS_init_r(); geosCtx->handle = GEOS_init_r();
if (geosCtx->handle == NULL) { if (geosCtx->handle == NULL) {
@ -273,6 +284,8 @@ int32_t doAsText(const unsigned char *inputGeom, size_t size, char **outputWKT)
int32_t code = TSDB_CODE_FAILED; int32_t code = TSDB_CODE_FAILED;
SGeosContext *geosCtx = getThreadLocalGeosCtx(); SGeosContext *geosCtx = getThreadLocalGeosCtx();
if (!geosCtx) return TSDB_CODE_OUT_OF_MEMORY;
GEOSGeometry *geom = NULL; GEOSGeometry *geom = NULL;
char *wkt = NULL; char *wkt = NULL;
@ -304,6 +317,8 @@ int32_t initCtxRelationFunc() {
int32_t code = TSDB_CODE_FAILED; int32_t code = TSDB_CODE_FAILED;
SGeosContext *geosCtx = getThreadLocalGeosCtx(); SGeosContext *geosCtx = getThreadLocalGeosCtx();
if (!geosCtx) return TSDB_CODE_OUT_OF_MEMORY;
if (geosCtx->handle == NULL) { if (geosCtx->handle == NULL) {
geosCtx->handle = GEOS_init_r(); geosCtx->handle = GEOS_init_r();
if (geosCtx->handle == NULL) { if (geosCtx->handle == NULL) {
@ -329,6 +344,8 @@ int32_t doGeosRelation(const GEOSGeometry *geom1, const GEOSPreparedGeometry *pr
_geosPreparedRelationFunc_t swappedPreparedRelationFn) { _geosPreparedRelationFunc_t swappedPreparedRelationFn) {
SGeosContext *geosCtx = getThreadLocalGeosCtx(); SGeosContext *geosCtx = getThreadLocalGeosCtx();
if (!geosCtx) return TSDB_CODE_OUT_OF_MEMORY;
if (!preparedGeom1) { if (!preparedGeom1) {
if (!swapped) { if (!swapped) {
ASSERT(relationFn); ASSERT(relationFn);
@ -389,8 +406,6 @@ int32_t doContainsProperly(const GEOSGeometry *geom1, const GEOSPreparedGeometry
// need to call destroyGeometry(outputGeom, outputPreparedGeom) later // need to call destroyGeometry(outputGeom, outputPreparedGeom) later
int32_t readGeometry(const unsigned char *input, GEOSGeometry **outputGeom, int32_t readGeometry(const unsigned char *input, GEOSGeometry **outputGeom,
const GEOSPreparedGeometry **outputPreparedGeom) { const GEOSPreparedGeometry **outputPreparedGeom) {
SGeosContext *geosCtx = getThreadLocalGeosCtx();
ASSERT(outputGeom); // it is not allowed if outputGeom is NULL ASSERT(outputGeom); // it is not allowed if outputGeom is NULL
*outputGeom = NULL; *outputGeom = NULL;
@ -402,6 +417,10 @@ int32_t readGeometry(const unsigned char *input, GEOSGeometry **outputGeom,
return TSDB_CODE_SUCCESS; return TSDB_CODE_SUCCESS;
} }
SGeosContext *geosCtx = getThreadLocalGeosCtx();
if (!geosCtx) return TSDB_CODE_OUT_OF_MEMORY;
*outputGeom = GEOSWKBReader_read_r(geosCtx->handle, geosCtx->WKBReader, varDataVal(input), varDataLen(input)); *outputGeom = GEOSWKBReader_read_r(geosCtx->handle, geosCtx->WKBReader, varDataVal(input), varDataLen(input));
if (*outputGeom == NULL) { if (*outputGeom == NULL) {
return TSDB_CODE_FUNC_FUNTION_PARA_VALUE; return TSDB_CODE_FUNC_FUNTION_PARA_VALUE;
@ -418,7 +437,8 @@ int32_t readGeometry(const unsigned char *input, GEOSGeometry **outputGeom,
} }
void destroyGeometry(GEOSGeometry **geom, const GEOSPreparedGeometry **preparedGeom) { void destroyGeometry(GEOSGeometry **geom, const GEOSPreparedGeometry **preparedGeom) {
SGeosContext *geosCtx = getThreadLocalGeosCtx(); SGeosContext *geosCtx = acquireThreadLocalGeosCtx();
if (!geosCtx) return;
if (preparedGeom && *preparedGeom) { if (preparedGeom && *preparedGeom) {
GEOSPreparedGeom_destroy_r(geosCtx->handle, *preparedGeom); GEOSPreparedGeom_destroy_r(geosCtx->handle, *preparedGeom);

View File

@ -655,7 +655,7 @@ static int32_t parseTagToken(const char** end, SToken* pToken, SSchema* pSchema,
code = parseGeometry(pToken, &output, &size); code = parseGeometry(pToken, &output, &size);
if (code != TSDB_CODE_SUCCESS) { if (code != TSDB_CODE_SUCCESS) {
code = buildSyntaxErrMsg(pMsgBuf, getThreadLocalGeosCtx()->errMsg, pToken->z); code = buildSyntaxErrMsg(pMsgBuf, getGeosErrMsg(code), pToken->z);
} else if (size + VARSTR_HEADER_SIZE > pSchema->bytes) { } else if (size + VARSTR_HEADER_SIZE > pSchema->bytes) {
// Too long values will raise the invalid sql error message // Too long values will raise the invalid sql error message
code = generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pSchema->name); code = generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pSchema->name);
@ -1646,7 +1646,7 @@ static int32_t parseValueTokenImpl(SInsertParseContext* pCxt, const char** pSql,
code = parseGeometry(pToken, &output, &size); code = parseGeometry(pToken, &output, &size);
if (code != TSDB_CODE_SUCCESS) { if (code != TSDB_CODE_SUCCESS) {
code = buildSyntaxErrMsg(&pCxt->msg, getThreadLocalGeosCtx()->errMsg, pToken->z); code = buildSyntaxErrMsg(&pCxt->msg, getGeosErrMsg(code), pToken->z);
} }
// Too long values will raise the invalid sql error message // Too long values will raise the invalid sql error message
else if (size + VARSTR_HEADER_SIZE > pSchema->bytes) { else if (size + VARSTR_HEADER_SIZE > pSchema->bytes) {

View File

@ -446,12 +446,12 @@ static FORCE_INLINE int32_t varToGeometry(char *buf, SScalarParam *pOut, int32_t
unsigned char *t = NULL; unsigned char *t = NULL;
char *output = NULL; char *output = NULL;
if (initCtxGeomFromText()) { if ((code = initCtxGeomFromText()) != 0) {
sclError("failed to init geometry ctx, %s", getThreadLocalGeosCtx()->errMsg); sclError("failed to init geometry ctx, %s", getGeosErrMsg(code));
SCL_ERR_JRET(TSDB_CODE_APP_ERROR); SCL_ERR_JRET(TSDB_CODE_APP_ERROR);
} }
if (doGeomFromText(buf, &t, &len)) { if ((code = doGeomFromText(buf, &t, &len)) != 0) {
sclInfo("failed to convert text to geometry, %s", getThreadLocalGeosCtx()->errMsg); sclError("failed to convert text to geometry, %s", getGeosErrMsg(code));
SCL_ERR_JRET(TSDB_CODE_SCALAR_CONVERT_ERROR); SCL_ERR_JRET(TSDB_CODE_SCALAR_CONVERT_ERROR);
} }

View File

@ -14,39 +14,102 @@
*/ */
#include "tgeosctx.h" #include "tgeosctx.h"
#include "tarray.h"
#include "tdef.h" #include "tdef.h"
#include "tlockfree.h"
#include "tlog.h"
static threadlocal SGeosContext tlGeosCtx = {0}; #define GEOS_POOL_CAPACITY 64
typedef struct {
SArray *poolArray; // totalSize: (GEOS_POOL_CAPACITY * (taosArrayGetSize(poolArray) - 1)) + size
SGeosContext *pool; // current SGeosContext pool
int32_t size; // size of current SGeosContext pool, size <= GEOS_POOL_CAPACITY
SRWLatch lock;
} SGeosContextPool;
SGeosContext* getThreadLocalGeosCtx() { return &tlGeosCtx; } static SGeosContextPool sGeosPool = {0};
static threadlocal SGeosContext *tlGeosCtx = NULL;
void destroyThreadLocalGeosCtx() { SGeosContext *acquireThreadLocalGeosCtx() { return tlGeosCtx; }
if (tlGeosCtx.WKTReader) {
GEOSWKTReader_destroy_r(tlGeosCtx.handle, tlGeosCtx.WKTReader); SGeosContext *getThreadLocalGeosCtx() {
tlGeosCtx.WKTReader = NULL; if (tlGeosCtx) return tlGeosCtx;
taosWLockLatch(&sGeosPool.lock);
if (!sGeosPool.pool || sGeosPool.size >= GEOS_POOL_CAPACITY) {
if (!(sGeosPool.pool = (SGeosContext *)taosMemoryCalloc(GEOS_POOL_CAPACITY, sizeof(SGeosContext)))) {
taosWUnLockLatch(&sGeosPool.lock);
return NULL;
}
if (!sGeosPool.poolArray) {
if (!(sGeosPool.poolArray = taosArrayInit(16, POINTER_BYTES))) {
taosMemoryFreeClear(sGeosPool.pool);
taosWUnLockLatch(&sGeosPool.lock);
return NULL;
}
}
if (!taosArrayPush(sGeosPool.poolArray, &sGeosPool.pool)) {
taosMemoryFreeClear(sGeosPool.pool);
taosWUnLockLatch(&sGeosPool.lock);
return NULL;
}
sGeosPool.size = 0;
} }
tlGeosCtx = sGeosPool.pool + sGeosPool.size;
++sGeosPool.size;
taosWUnLockLatch(&sGeosPool.lock);
if (tlGeosCtx.WKTWriter) { return tlGeosCtx;
GEOSWKTWriter_destroy_r(tlGeosCtx.handle, tlGeosCtx.WKTWriter); }
tlGeosCtx.WKTWriter = NULL;
}
if (tlGeosCtx.WKBReader) { const char *getGeosErrMsg(int32_t code) { return tlGeosCtx ? tlGeosCtx->errMsg : (code != 0 ? tstrerror(code) : ""); }
GEOSWKBReader_destroy_r(tlGeosCtx.handle, tlGeosCtx.WKBReader);
tlGeosCtx.WKBReader = NULL;
}
if (tlGeosCtx.WKBWriter) { static void destroyGeosCtx(SGeosContext *pCtx) {
GEOSWKBWriter_destroy_r(tlGeosCtx.handle, tlGeosCtx.WKBWriter); if (pCtx) {
tlGeosCtx.WKBWriter = NULL; if (pCtx->WKTReader) {
} GEOSWKTReader_destroy_r(pCtx->handle, pCtx->WKTReader);
pCtx->WKTReader = NULL;
}
if (tlGeosCtx.WKTRegex) { if (pCtx->WKTWriter) {
destroyRegexes(tlGeosCtx.WKTRegex, tlGeosCtx.WKTMatchData); GEOSWKTWriter_destroy_r(pCtx->handle, pCtx->WKTWriter);
} pCtx->WKTWriter = NULL;
}
if (tlGeosCtx.handle) { if (pCtx->WKBReader) {
GEOS_finish_r(tlGeosCtx.handle); GEOSWKBReader_destroy_r(pCtx->handle, pCtx->WKBReader);
tlGeosCtx.handle = NULL; pCtx->WKBReader = NULL;
}
if (pCtx->WKBWriter) {
GEOSWKBWriter_destroy_r(pCtx->handle, pCtx->WKBWriter);
pCtx->WKBWriter = NULL;
}
if (pCtx->WKTRegex) {
destroyRegexes(pCtx->WKTRegex, pCtx->WKTMatchData);
pCtx->WKTRegex = NULL;
pCtx->WKTMatchData = NULL;
}
if (pCtx->handle) {
GEOS_finish_r(pCtx->handle);
pCtx->handle = NULL;
}
} }
} }
void taosGeosDestroy() {
uInfo("geos is cleaned up");
int32_t size = taosArrayGetSize(sGeosPool.poolArray);
for (int32_t i = 0; i < size; ++i) {
SGeosContext *pool = *(SGeosContext **)TARRAY_GET_ELEM(sGeosPool.poolArray, i);
int32_t poolSize = i == size - 1 ? sGeosPool.size : GEOS_POOL_CAPACITY;
for (int32_t j = 0; j < poolSize; ++j) {
destroyGeosCtx(pool + j);
}
taosMemoryFree(pool);
}
taosArrayDestroy(sGeosPool.poolArray);
sGeosPool.poolArray = NULL;
}

View File

@ -178,8 +178,6 @@ void *taosProcessSchedQueue(void *scheduler) {
(*(msg.tfp))(msg.ahandle, msg.thandle); (*(msg.tfp))(msg.ahandle, msg.thandle);
} }
destroyThreadLocalGeosCtx();
return NULL; return NULL;
} }

View File

@ -105,7 +105,6 @@ static void *tQWorkerThreadFp(SQueueWorker *worker) {
taosUpdateItemSize(qinfo.queue, 1); taosUpdateItemSize(qinfo.queue, 1);
} }
destroyThreadLocalGeosCtx();
DestoryThreadLocalRegComp(); DestoryThreadLocalRegComp();
return NULL; return NULL;
@ -665,7 +664,6 @@ static void *tQueryAutoQWorkerThreadFp(SQueryAutoQWorker *worker) {
} }
} }
destroyThreadLocalGeosCtx();
DestoryThreadLocalRegComp(); DestoryThreadLocalRegComp();
return NULL; return NULL;

View File

@ -611,14 +611,14 @@ void shellPrintGeometry(const unsigned char *val, int32_t length, int32_t width)
code = initCtxAsText(); code = initCtxAsText();
if (code != TSDB_CODE_SUCCESS) { if (code != TSDB_CODE_SUCCESS) {
shellPrintString(getThreadLocalGeosCtx()->errMsg, width); shellPrintString(getGeosErrMsg(code), width);
return; return;
} }
char *outputWKT = NULL; char *outputWKT = NULL;
code = doAsText(val, length, &outputWKT); code = doAsText(val, length, &outputWKT);
if (code != TSDB_CODE_SUCCESS) { if (code != TSDB_CODE_SUCCESS) {
shellPrintString(getThreadLocalGeosCtx()->errMsg, width); // should NOT happen shellPrintString(getGeosErrMsg(code), width); // should NOT happen
return; return;
} }
@ -1282,7 +1282,6 @@ void *shellThreadLoop(void *arg) {
taosResetTerminalMode(); taosResetTerminalMode();
} while (shellRunCommand(command, true) == 0); } while (shellRunCommand(command, true) == 0);
destroyThreadLocalGeosCtx();
taosMemoryFreeClear(command); taosMemoryFreeClear(command);
shellWriteHistory(); shellWriteHistory();
shellExit(); shellExit();