From 2a11363e0c57af6e4060a0072283cd1736259078 Mon Sep 17 00:00:00 2001 From: slguan Date: Mon, 11 Nov 2019 13:04:09 +0800 Subject: [PATCH] [TBASE-1060] --- src/client/src/tscProfile.c | 45 ++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/src/client/src/tscProfile.c b/src/client/src/tscProfile.c index 770e61e278..b6de98e712 100644 --- a/src/client/src/tscProfile.c +++ b/src/client/src/tscProfile.c @@ -23,6 +23,27 @@ #include "ttimer.h" #include "tutil.h" +void tscSaveSlowQueryFp(void *handle, void *tmrId); +void *tscSlowQueryConn = NULL; +bool tscSlowQueryConnInitialized = false; +TAOS *taos_connect_a(char *ip, char *user, char *pass, char *db, int port, void (*fp)(void *, TAOS_RES *, int), + void *param, void **taos); + +void tscInitConnCb(void *param, TAOS_RES *result, int code) { + char *sql = param; + if (code < 0) { + tscError("taos:%p, slow query connect failed, code:%d", tscSlowQueryConn, code); + taos_close(tscSlowQueryConn); + tscSlowQueryConn = NULL; + tscSlowQueryConnInitialized = false; + free(sql); + } else { + tscTrace("taos:%p, slow query connect success, code:%d", tscSlowQueryConn, code); + tscSlowQueryConnInitialized = true; + tscSaveSlowQueryFp(sql, NULL); + } +} + void tscAddIntoSqlList(SSqlObj *pSql) { static uint32_t queryId = 1; @@ -47,26 +68,28 @@ void tscAddIntoSqlList(SSqlObj *pSql) { void tscSaveSlowQueryFpCb(void *param, TAOS_RES *result, int code) { if (code < 0) { - tscError("failed to save slowquery, code:%d", code); + tscError("failed to save slow query, code:%d", code); + } else { + tscTrace("success to save slow query, code:%d", code); } } void tscSaveSlowQueryFp(void *handle, void *tmrId) { char *sql = handle; - static void *taos = NULL; - if (taos == NULL) { - taos = taos_connect(NULL, "monitor", tsInternalPass, NULL, 0); - if (taos == NULL) { - tscError("failed to save slow query, can't connect to server"); + if (!tscSlowQueryConnInitialized) { + if (tscSlowQueryConn == NULL) { + tscTrace("start to init slow query connect"); + taos_connect_a(NULL, "monitor", tsInternalPass, "", 0, tscInitConnCb, sql, &tscSlowQueryConn); + } else { + tscError("taos:%p, slow query connect is already initialized", tscSlowQueryConn); free(sql); - return; } + } else { + tscTrace("taos:%p, save slow query:%s", tscSlowQueryConn, sql); + taos_query_a(tscSlowQueryConn, sql, tscSaveSlowQueryFpCb, NULL); + free(sql); } - - tscTrace("save slow query:sql", sql); - taos_query_a(taos, sql, tscSaveSlowQueryFpCb, NULL); - free(sql); } void tscSaveSlowQuery(SSqlObj *pSql) { -- GitLab