• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #4907

30 Dec 2025 10:52AM UTC coverage: 65.541% (+0.03%) from 65.514%
#4907

push

travis-ci

web-flow
enh: drop multi-stream (#33962)

60 of 106 new or added lines in 4 files covered. (56.6%)

808 existing lines in 106 files now uncovered.

193920 of 295877 relevant lines covered (65.54%)

118520209.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

73.31
/source/client/src/clientHb.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "catalog.h"
17
#include "clientInt.h"
18
#include "clientLog.h"
19
#include "clientMonitor.h"
20
#include "clientSession.h"
21
#include "scheduler.h"
22
#include "tglobal.h"
23
#include "trpc.h"
24

25
typedef struct {
26
  union {
27
    struct {
28
      SAppHbMgr *pAppHbMgr;
29
      int64_t    clusterId;
30
      int32_t    reqCnt;
31
      int8_t     connHbFlag;
32
    };
33
  };
34
} SHbParam;
35

36
SClientHbMgr clientHbMgr = {0};
37

38
static int32_t hbCreateThread();
39
static void    hbStopThread();
40
static int32_t hbUpdateUserSessMertric(const char *user, SUserSessCfg *pCfg);
41
static int32_t hbUpdateUserAuthInfo(SAppHbMgr *pAppHbMgr, SUserAuthBatchRsp *batchRsp);
42

43
static int32_t hbProcessUserAuthInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog,
955,541✔
44
                                        SAppHbMgr *pAppHbMgr) {
45
  int32_t code = TSDB_CODE_SUCCESS;
955,541✔
46

47
  SUserAuthBatchRsp batchRsp = {0};
955,541✔
48
  if (tDeserializeSUserAuthBatchRsp(value, valueLen, &batchRsp) != 0) {
955,541✔
49
    return TSDB_CODE_INVALID_MSG;
×
50
  }
51

52
  int32_t numOfBatchs = taosArrayGetSize(batchRsp.pArray);
955,541✔
53
  for (int32_t i = 0; i < numOfBatchs; ++i) {
1,950,473✔
54
    SGetUserAuthRsp *rsp = taosArrayGet(batchRsp.pArray, i);
994,932✔
55
    if (NULL == rsp) {
994,932✔
56
      code = terrno;
×
57
      goto _return;
×
58
    }
59
    tscDebug("hb to update user auth, user:%s, version:%d", rsp->user, rsp->version);
994,932✔
60

61
    TSC_ERR_JRET(catalogUpdateUserAuthInfo(pCatalog, rsp));
994,932✔
62

63
  }
64

65
  if (numOfBatchs > 0) {
955,541✔
66
    TSC_ERR_JRET(hbUpdateUserAuthInfo(pAppHbMgr, &batchRsp));
955,541✔
67
  }
68

69
  (void)atomic_val_compare_exchange_8(&pAppHbMgr->connHbFlag, 1, 2);
955,541✔
70

71
_return:
955,541✔
72
  taosArrayDestroy(batchRsp.pArray);
955,541✔
73
  return code;
955,541✔
74
}
75

76
static int32_t hbUpdateUserSessMertric(const char *user, SUserSessCfg *pCfg) {
1,275,615✔
77
  int32_t code = 0;
1,275,615✔
78
  int32_t lino = 0;
1,275,615✔
79
  if (user == NULL || pCfg == NULL) {
1,275,615✔
80
    return code;
×
81
  }
82

83
  SUserSessCfg cfg = {0, 0, 0, 0, 0};
1,275,615✔
84

85
  if (memcmp(pCfg, &cfg, sizeof(SUserSessCfg)) == 0) {
1,275,615✔
86
    return TSDB_CODE_SUCCESS;
×
87
  }
88
  tscInfo(
1,275,615✔
89
      "update session metric for user:%s, sessPerUser:%d, sessConnTime:%d, sessConnIdleTime:%d, sessMaxConcurrency:%d, "
90
      "sessMaxCallVnodeNum:%d",
91
      user, pCfg->sessPerUser, pCfg->sessConnTime, pCfg->sessConnIdleTime, pCfg->sessMaxConcurrency,
92
      pCfg->sessMaxCallVnodeNum);
93

94
  if (pCfg->sessPerUser != 0) {
1,275,615✔
95
    code = sessMgtUpdataLimit((char *)user, SESSION_PER_USER, pCfg->sessPerUser);
1,275,615✔
96
    TAOS_CHECK_GOTO(code, &lino, _error);
1,275,615✔
97
  }
98

99
  if (pCfg->sessConnTime != 0) {
1,275,615✔
100
    code = sessMgtUpdataLimit((char *)user, SESSION_CONN_TIME, pCfg->sessConnTime);
1,275,615✔
101
    TAOS_CHECK_GOTO(code, &lino, _error);
1,275,615✔
102
  }
103

104
  if (pCfg->sessConnIdleTime != 0) {
1,275,615✔
105
    code = sessMgtUpdataLimit((char *)user, SESSION_CONN_IDLE_TIME, pCfg->sessConnIdleTime);
1,275,615✔
106
    TAOS_CHECK_GOTO(code, &lino, _error);
1,275,615✔
107
  }
108

109
  if (pCfg->sessMaxConcurrency != 0) {
1,275,615✔
110
    code = sessMgtUpdataLimit((char *)user, SESSION_MAX_CONCURRENCY, pCfg->sessMaxConcurrency);
1,275,615✔
111
    TAOS_CHECK_GOTO(code, &lino, _error);
1,275,615✔
112
  }
113

114
  if (pCfg->sessMaxCallVnodeNum != 0) {
1,275,615✔
115
    code = sessMgtUpdataLimit((char *)user, SESSION_MAX_CALL_VNODE_NUM, pCfg->sessMaxCallVnodeNum);
1,275,615✔
116
    TAOS_CHECK_GOTO(code, &lino, _error);
1,275,615✔
117
  }
118
_error:
1,275,615✔
119
  return code;
1,275,615✔
120
}
121
static int32_t hbUpdateUserAuthInfo(SAppHbMgr *pAppHbMgr, SUserAuthBatchRsp *batchRsp) {
955,541✔
122
  int32_t code = 0;
955,541✔
123
  int64_t clusterId = pAppHbMgr->pAppInstInfo->clusterId;
955,541✔
124
  for (int i = 0; i < TARRAY_SIZE(clientHbMgr.appHbMgrs); ++i) {
2,115,580✔
125
    SAppHbMgr *hbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
1,160,039✔
126
    if (!hbMgr || hbMgr->pAppInstInfo->clusterId != clusterId) {
1,160,039✔
127
      continue;
265✔
128
    }
129

130
    SClientHbReq    *pReq = NULL;
1,159,774✔
131
    SGetUserAuthRsp *pRsp = NULL;
1,159,774✔
132
    while ((pReq = taosHashIterate(hbMgr->activeInfo, pReq))) {
2,682,170✔
133
      STscObj *pTscObj = (STscObj *)acquireTscObj(pReq->connKey.tscRid);
1,555,442✔
134
      if (!pTscObj) {
1,555,442✔
135
        continue;
245,397✔
136
      }
137

138
      if (!pRsp) {
1,310,045✔
139
        for (int32_t j = 0; j < TARRAY_SIZE(batchRsp->pArray); ++j) {
1,128,467✔
140
          SGetUserAuthRsp *rsp = TARRAY_GET_ELEM(batchRsp->pArray, j);
1,095,421✔
141
          if (0 == strncmp(rsp->user, pTscObj->user, TSDB_USER_LEN)) {
1,095,421✔
142
            pRsp = rsp;
1,045,405✔
143
            break;
1,045,405✔
144
          }
145
        }
146
        if (!pRsp) {
1,078,451✔
147
          releaseTscObj(pReq->connKey.tscRid);
33,046✔
148
          taosHashCancelIterate(hbMgr->activeInfo, pReq);
33,046✔
149
          break;
33,046✔
150
        }
151
      }
152

153
      if (pRsp->dropped == 1) {
1,276,999✔
154
        if (atomic_val_compare_exchange_8(&pTscObj->dropped, 0, 1) == 0) {
1,384✔
155
          if (pTscObj->userDroppedInfo.fp) {
1,194✔
156
            SPassInfo *dropInfo = &pTscObj->userDroppedInfo;
950✔
157
            if (dropInfo->fp) {
950✔
158
              (*dropInfo->fp)(dropInfo->param, NULL, TAOS_NOTIFY_USER_DROPPED);
950✔
159
            }
160
          }
161
        }
162
        releaseTscObj(pReq->connKey.tscRid);
1,384✔
163
        continue;
1,384✔
164
      }
165

166
      pTscObj->authVer = pRsp->version;
1,275,615✔
167
      if (hbUpdateUserSessMertric(pTscObj->user, &pRsp->sessCfg) != 0) {
1,275,615✔
168
        tscError("failed to update user session metric, user:%s", pTscObj->user);
×
169
      }
170

171
      if (pTscObj->sysInfo != pRsp->sysInfo) {
1,275,615✔
172
        tscDebug("update sysInfo of user %s from %" PRIi8 " to %" PRIi8 ", conn:%" PRIi64, pRsp->user,
1,900✔
173
                 pTscObj->sysInfo, pRsp->sysInfo, pTscObj->id);
174
        pTscObj->sysInfo = pRsp->sysInfo;
1,900✔
175
      }
176

177
      if (pTscObj->passInfo.fp) {
1,275,615✔
178
        SPassInfo *passInfo = &pTscObj->passInfo;
570✔
179
        int32_t    oldVer = atomic_load_32(&passInfo->ver);
570✔
180
        if (oldVer < pRsp->passVer) {
570✔
181
          atomic_store_32(&passInfo->ver, pRsp->passVer);
380✔
182
          if (passInfo->fp) {
380✔
183
            (*passInfo->fp)(passInfo->param, &pRsp->passVer, TAOS_NOTIFY_PASSVER);
380✔
184
          }
185
          tscDebug("update passVer of user %s from %d to %d, conn:%" PRIi64, pRsp->user, oldVer,
380✔
186
                   atomic_load_32(&passInfo->ver), pTscObj->id);
187
        }
188
      }
189

190
      if (pTscObj->whiteListInfo.fp) {
1,275,615✔
191
        SWhiteListInfo *whiteListInfo = &pTscObj->whiteListInfo;
×
192
        int64_t         oldVer = atomic_load_64(&whiteListInfo->ver);
×
193
        if (oldVer != pRsp->whiteListVer) {
×
194
          atomic_store_64(&whiteListInfo->ver, pRsp->whiteListVer);
×
195
          if (whiteListInfo->fp) {
×
196
            (*whiteListInfo->fp)(whiteListInfo->param, &pRsp->whiteListVer, TAOS_NOTIFY_WHITELIST_VER);
×
197
          }
198
          tscDebug("update whitelist version of user %s from %" PRId64 " to %" PRId64 ", conn:%" PRIi64, pRsp->user,
×
199
                   oldVer, atomic_load_64(&whiteListInfo->ver), pTscObj->id);
200
        }
201
      } else {
202
        // Need to update version information to prevent frequent fetching of authentication
203
        // information.
204
        SWhiteListInfo *whiteListInfo = &pTscObj->whiteListInfo;
1,275,615✔
205
        int64_t         oldVer = atomic_load_64(&whiteListInfo->ver);
1,275,615✔
206
        atomic_store_64(&whiteListInfo->ver, pRsp->whiteListVer);
1,275,615✔
207
        tscDebug("update whitelist version of user %s from %" PRId64 " to %" PRId64 ", conn:%" PRIi64, pRsp->user,
1,275,615✔
208
                 oldVer, atomic_load_64(&whiteListInfo->ver), pTscObj->id);
209
      }
210

211
      if (pTscObj->dateTimeWhiteListInfo.fp) {
1,275,615✔
212
        SWhiteListInfo *whiteListInfo = &pTscObj->dateTimeWhiteListInfo;
×
213
        int64_t         oldVer = atomic_load_64(&whiteListInfo->ver);
×
214
        if (oldVer != pRsp->timeWhiteListVer) {
×
215
          atomic_store_64(&whiteListInfo->ver, pRsp->timeWhiteListVer);
×
216
          if (whiteListInfo->fp) {
×
217
            (*whiteListInfo->fp)(whiteListInfo->param, &pRsp->timeWhiteListVer, TAOS_NOTIFY_DATETIME_WHITELIST_VER);
×
218
          }
219
          tscDebug("update date time whitelist version of user %s from %" PRId64 " to %" PRId64 ", conn:%" PRIi64, pRsp->user,
×
220
                   oldVer, atomic_load_64(&whiteListInfo->ver), pTscObj->id);
221
        }
222
      } else {
223
        SWhiteListInfo *whiteListInfo = &pTscObj->dateTimeWhiteListInfo;
1,275,615✔
224
        int64_t         oldVer = atomic_load_64(&whiteListInfo->ver);
1,275,615✔
225
        atomic_store_64(&whiteListInfo->ver, pRsp->timeWhiteListVer);
1,275,615✔
226
        tscDebug("update date time whitelist version of user %s from %" PRId64 " to %" PRId64 ", conn:%" PRIi64, pRsp->user,
1,275,615✔
227
                 oldVer, atomic_load_64(&whiteListInfo->ver), pTscObj->id);
228
      }
229
      releaseTscObj(pReq->connKey.tscRid);
1,275,615✔
230
    }
231
  }
232
  return 0;
955,541✔
233
}
234

235
static int32_t hbGenerateVgInfoFromRsp(SDBVgInfo **pInfo, SUseDbRsp *rsp) {
1,441,293✔
236
  int32_t    code = 0;
1,441,293✔
237
  SDBVgInfo *vgInfo = taosMemoryCalloc(1, sizeof(SDBVgInfo));
1,441,293✔
238
  if (NULL == vgInfo) {
1,441,293✔
239
    return terrno;
×
240
  }
241

242
  vgInfo->vgVersion = rsp->vgVersion;
1,441,293✔
243
  vgInfo->stateTs = rsp->stateTs;
1,441,293✔
244
  vgInfo->flags = rsp->flags;
1,441,293✔
245
  vgInfo->hashMethod = rsp->hashMethod;
1,441,293✔
246
  vgInfo->hashPrefix = rsp->hashPrefix;
1,441,293✔
247
  vgInfo->hashSuffix = rsp->hashSuffix;
1,441,293✔
248
  vgInfo->vgHash = taosHashInit(rsp->vgNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
1,441,293✔
249
  if (NULL == vgInfo->vgHash) {
1,441,293✔
250
    tscError("hash init[%d] failed", rsp->vgNum);
×
251
    code = terrno;
×
252
    goto _return;
×
253
  }
254

255
  for (int32_t j = 0; j < rsp->vgNum; ++j) {
5,147,441✔
256
    SVgroupInfo *pInfo = taosArrayGet(rsp->pVgroupInfos, j);
3,706,148✔
257
    if (taosHashPut(vgInfo->vgHash, &pInfo->vgId, sizeof(int32_t), pInfo, sizeof(SVgroupInfo)) != 0) {
3,706,148✔
258
      tscError("hash push failed, terrno:%d", terrno);
×
259
      code = terrno;
×
260
      goto _return;
×
261
    }
262
  }
263

264
_return:
1,441,293✔
265
  if (code) {
1,441,293✔
266
    taosHashCleanup(vgInfo->vgHash);
×
267
    taosMemoryFreeClear(vgInfo);
×
268
  }
269

270
  *pInfo = vgInfo;
1,441,293✔
271
  return code;
1,441,293✔
272
}
273

274
static int32_t hbProcessDBInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
10,662,027✔
275
  int32_t code = 0;
10,662,027✔
276

277
  SDbHbBatchRsp batchRsp = {0};
10,662,027✔
278
  if (tDeserializeSDbHbBatchRsp(value, valueLen, &batchRsp) != 0) {
10,662,027✔
279
    terrno = TSDB_CODE_INVALID_MSG;
×
280
    code = terrno;
×
281
    goto _return;
×
282
  }
283

284
  int32_t numOfBatchs = taosArrayGetSize(batchRsp.pArray);
10,662,027✔
285
  for (int32_t i = 0; i < numOfBatchs; ++i) {
12,397,895✔
286
    SDbHbRsp *rsp = taosArrayGet(batchRsp.pArray, i);
1,735,868✔
287
    if (NULL == rsp) {
1,735,868✔
288
      code = terrno;
×
289
      goto _return;
×
290
    }
291
    if (rsp->useDbRsp) {
1,735,868✔
292
      tscDebug("hb use db rsp, db:%s, vgVersion:%d, stateTs:%" PRId64 ", uid:%" PRIx64, rsp->useDbRsp->db,
1,238,626✔
293
               rsp->useDbRsp->vgVersion, rsp->useDbRsp->stateTs, rsp->useDbRsp->uid);
294

295
      if (rsp->useDbRsp->vgVersion < 0) {
1,238,626✔
296
        tscDebug("hb to remove db, db:%s", rsp->useDbRsp->db);
53,956✔
297
        code = catalogRemoveDB(pCatalog, rsp->useDbRsp->db, rsp->useDbRsp->uid);
53,956✔
298
      } else {
299
        SDBVgInfo *vgInfo = NULL;
1,184,670✔
300
        code = hbGenerateVgInfoFromRsp(&vgInfo, rsp->useDbRsp);
1,184,670✔
301
        if (TSDB_CODE_SUCCESS != code) {
1,184,670✔
302
          goto _return;
×
303
        }
304

305
        tscDebug("hb to update db vgInfo, db:%s", rsp->useDbRsp->db);
1,184,670✔
306

307
        TSC_ERR_JRET(catalogUpdateDBVgInfo(pCatalog, rsp->useDbRsp->db, rsp->useDbRsp->uid, vgInfo));
1,184,670✔
308

309
        if (IS_SYS_DBNAME(rsp->useDbRsp->db)) {
1,184,670✔
310
          code = hbGenerateVgInfoFromRsp(&vgInfo, rsp->useDbRsp);
256,623✔
311
          if (TSDB_CODE_SUCCESS != code) {
256,623✔
312
            goto _return;
×
313
          }
314

315
          TSC_ERR_JRET(catalogUpdateDBVgInfo(
256,623✔
316
              pCatalog, (rsp->useDbRsp->db[0] == 'i') ? TSDB_PERFORMANCE_SCHEMA_DB : TSDB_INFORMATION_SCHEMA_DB,
317
              rsp->useDbRsp->uid, vgInfo));
318
        }
319
      }
320
    }
321

322
    if (rsp->cfgRsp) {
1,735,868✔
323
      tscDebug("hb db cfg rsp, db:%s, cfgVersion:%d", rsp->cfgRsp->db, rsp->cfgRsp->cfgVersion);
51,794✔
324
      code = catalogUpdateDbCfg(pCatalog, rsp->cfgRsp->db, rsp->cfgRsp->dbId, rsp->cfgRsp);
51,794✔
325
      rsp->cfgRsp = NULL;
51,794✔
326
    }
327
    if (rsp->pTsmaRsp) {
1,735,868✔
328
      if (rsp->pTsmaRsp->pTsmas) {
929,283✔
329
        for (int32_t i = 0; i < rsp->pTsmaRsp->pTsmas->size; ++i) {
1,400✔
330
          STableTSMAInfo *pTsma = taosArrayGetP(rsp->pTsmaRsp->pTsmas, i);
700✔
331
          if (NULL == pTsma) {
700✔
332
            TSC_ERR_JRET(TSDB_CODE_OUT_OF_RANGE);
×
333
          }
334
          TSC_ERR_JRET(catalogAsyncUpdateTSMA(pCatalog, &pTsma, rsp->dbTsmaVersion));
700✔
335
        }
336
        taosArrayClear(rsp->pTsmaRsp->pTsmas);
700✔
337
      } else {
338
        TSC_ERR_JRET(catalogAsyncUpdateDbTsmaVersion(pCatalog, rsp->dbTsmaVersion, rsp->db, rsp->dbId));
928,583✔
339
      }
340
    }
341
  }
342

343
_return:
10,662,027✔
344

345
  tFreeSDbHbBatchRsp(&batchRsp);
10,662,027✔
346
  return code;
10,662,027✔
347
}
348

349
static int32_t hbProcessStbInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
7,020,065✔
350
  int32_t code = TSDB_CODE_SUCCESS;
7,020,065✔
351

352
  SSTbHbRsp hbRsp = {0};
7,020,065✔
353
  if (tDeserializeSSTbHbRsp(value, valueLen, &hbRsp) != 0) {
7,020,065✔
354
    terrno = TSDB_CODE_INVALID_MSG;
×
355
    return -1;
×
356
  }
357

358
  int32_t numOfMeta = taosArrayGetSize(hbRsp.pMetaRsp);
7,020,065✔
359
  for (int32_t i = 0; i < numOfMeta; ++i) {
7,097,726✔
360
    STableMetaRsp *rsp = taosArrayGet(hbRsp.pMetaRsp, i);
77,661✔
361
    if (NULL == rsp) {
77,661✔
362
      code = terrno;
×
363
      goto _return;
×
364
    }
365
    if (rsp->numOfColumns < 0) {
77,661✔
366
      tscDebug("hb to remove stb, db:%s, stb:%s", rsp->dbFName, rsp->stbName);
49,357✔
367
      TSC_ERR_JRET(catalogRemoveStbMeta(pCatalog, rsp->dbFName, rsp->dbId, rsp->stbName, rsp->suid));
49,357✔
368
    } else {
369
      tscDebug("hb to update stb, db:%s, stb:%s", rsp->dbFName, rsp->stbName);
28,304✔
370
      if (rsp->pSchemas[0].colId != PRIMARYKEY_TIMESTAMP_COL_ID) {
28,304✔
371
        tscError("invalid colId[%" PRIi16 "] for the first column in table meta rsp msg", rsp->pSchemas[0].colId);
×
372
        tFreeSSTbHbRsp(&hbRsp);
×
373
        return TSDB_CODE_TSC_INVALID_VALUE;
×
374
      }
375

376
      TSC_ERR_JRET(catalogAsyncUpdateTableMeta(pCatalog, rsp));
28,304✔
377
    }
378
  }
379

380
  int32_t numOfIndex = taosArrayGetSize(hbRsp.pIndexRsp);
7,020,065✔
381
  for (int32_t i = 0; i < numOfIndex; ++i) {
7,020,065✔
382
    STableIndexRsp *rsp = taosArrayGet(hbRsp.pIndexRsp, i);
×
383
    if (NULL == rsp) {
×
384
      code = terrno;
×
385
      goto _return;
×
386
    }
387
    TSC_ERR_JRET(catalogUpdateTableIndex(pCatalog, rsp));
×
388
  }
389

390
_return:
7,020,065✔
391
  taosArrayDestroy(hbRsp.pIndexRsp);
7,020,065✔
392
  hbRsp.pIndexRsp = NULL;
7,020,065✔
393

394
  tFreeSSTbHbRsp(&hbRsp);
7,020,065✔
395
  return code;
7,020,065✔
396
}
397

398
static int32_t hbProcessDynViewRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
598✔
399
  return catalogUpdateDynViewVer(pCatalog, (SDynViewVersion *)value);
598✔
400
}
401

402
static void hbFreeSViewMetaInRsp(void *p) {
×
403
  if (NULL == p || NULL == *(void **)p) {
×
404
    return;
×
405
  }
406
  SViewMetaRsp *pRsp = *(SViewMetaRsp **)p;
×
407
  tFreeSViewMetaRsp(pRsp);
×
408
  taosMemoryFreeClear(pRsp);
×
409
}
410

411
static int32_t hbProcessViewInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
598✔
412
  int32_t code = TSDB_CODE_SUCCESS;
598✔
413

414
  SViewHbRsp hbRsp = {0};
598✔
415
  if (tDeserializeSViewHbRsp(value, valueLen, &hbRsp) != 0) {
598✔
416
    taosArrayDestroyEx(hbRsp.pViewRsp, hbFreeSViewMetaInRsp);
×
417
    terrno = TSDB_CODE_INVALID_MSG;
×
418
    return -1;
×
419
  }
420

421
  int32_t numOfMeta = taosArrayGetSize(hbRsp.pViewRsp);
598✔
422
  for (int32_t i = 0; i < numOfMeta; ++i) {
598✔
423
    SViewMetaRsp *rsp = taosArrayGetP(hbRsp.pViewRsp, i);
×
424
    if (NULL == rsp) {
×
425
      code = terrno;
×
426
      goto _return;
×
427
    }
428
    if (rsp->numOfCols < 0) {
×
429
      tscDebug("hb to remove view, db:%s, view:%s", rsp->dbFName, rsp->name);
×
430
      code = catalogRemoveViewMeta(pCatalog, rsp->dbFName, rsp->dbId, rsp->name, rsp->viewId);
×
431
      tFreeSViewMetaRsp(rsp);
×
432
      taosMemoryFreeClear(rsp);
×
433
    } else {
434
      tscDebug("hb to update view, db:%s, view:%s", rsp->dbFName, rsp->name);
×
435
      code = catalogUpdateViewMeta(pCatalog, rsp);
×
436
    }
437
    TSC_ERR_JRET(code);
×
438
  }
439

440
_return:
598✔
441
  taosArrayDestroy(hbRsp.pViewRsp);
598✔
442
  return code;
598✔
443
}
444

445
static int32_t hbprocessTSMARsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
46,200✔
446
  int32_t code = 0;
46,200✔
447

448
  STSMAHbRsp hbRsp = {0};
46,200✔
449
  if (tDeserializeTSMAHbRsp(value, valueLen, &hbRsp)) {
46,200✔
450
    terrno = TSDB_CODE_INVALID_MSG;
×
451
    return -1;
×
452
  }
453

454
  int32_t numOfTsma = taosArrayGetSize(hbRsp.pTsmas);
46,200✔
455
  for (int32_t i = 0; i < numOfTsma; ++i) {
46,200✔
456
    STableTSMAInfo *pTsmaInfo = taosArrayGetP(hbRsp.pTsmas, i);
×
457

458
    if (!pTsmaInfo->pFuncs) {
×
459
      tscDebug("hb to remove tsma:%s.%s", pTsmaInfo->dbFName, pTsmaInfo->name);
×
460
      code = catalogRemoveTSMA(pCatalog, pTsmaInfo);
×
461
      tFreeAndClearTableTSMAInfo(pTsmaInfo);
×
462
    } else {
463
      tscDebug("hb to update tsma:%s.%s", pTsmaInfo->dbFName, pTsmaInfo->name);
×
464
      code = catalogUpdateTSMA(pCatalog, &pTsmaInfo);
×
465
      tFreeAndClearTableTSMAInfo(pTsmaInfo);
×
466
    }
467
    TSC_ERR_JRET(code);
×
468
  }
469

470
_return:
46,200✔
471
  taosArrayDestroy(hbRsp.pTsmas);
46,200✔
472
  return code;
46,200✔
473
}
474

475
static void hbProcessQueryRspKvs(int32_t kvNum, SArray *pKvs, struct SCatalog *pCatalog, SAppHbMgr *pAppHbMgr) {
15,674,330✔
476
  for (int32_t i = 0; i < kvNum; ++i) {
34,359,359✔
477
    SKv *kv = taosArrayGet(pKvs, i);
18,685,029✔
478
    if (NULL == kv) {
18,685,029✔
479
      tscError("invalid hb kv, idx:%d", i);
×
480
      continue;
×
481
    }
482
    switch (kv->key) {
18,685,029✔
483
      case HEARTBEAT_KEY_USER_AUTHINFO: {
955,541✔
484
        if (kv->valueLen <= 0 || NULL == kv->value) {
955,541✔
485
          tscError("invalid hb user auth info, len:%d, value:%p", kv->valueLen, kv->value);
×
486
          break;
×
487
        }
488
        if (TSDB_CODE_SUCCESS != hbProcessUserAuthInfoRsp(kv->value, kv->valueLen, pCatalog, pAppHbMgr)) {
955,541✔
489
          tscError("process user auth info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
490
          break;
×
491
        }
492
        break;
955,541✔
493
      }
494
      case HEARTBEAT_KEY_DBINFO: {
10,662,027✔
495
        if (kv->valueLen <= 0 || NULL == kv->value) {
10,662,027✔
496
          tscError("invalid hb db info, len:%d, value:%p", kv->valueLen, kv->value);
×
497
          break;
×
498
        }
499
        if (TSDB_CODE_SUCCESS != hbProcessDBInfoRsp(kv->value, kv->valueLen, pCatalog)) {
10,662,027✔
500
          tscError("process db info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
501
          break;
×
502
        }
503
        break;
10,662,027✔
504
      }
505
      case HEARTBEAT_KEY_STBINFO: {
7,020,065✔
506
        if (kv->valueLen <= 0 || NULL == kv->value) {
7,020,065✔
507
          tscError("invalid hb stb info, len:%d, value:%p", kv->valueLen, kv->value);
×
508
          break;
×
509
        }
510
        if (TSDB_CODE_SUCCESS != hbProcessStbInfoRsp(kv->value, kv->valueLen, pCatalog)) {
7,020,065✔
511
          tscError("process stb info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
512
          break;
×
513
        }
514
        break;
7,020,065✔
515
      }
516
#ifdef TD_ENTERPRISE
517
      case HEARTBEAT_KEY_DYN_VIEW: {
598✔
518
        if (kv->valueLen <= 0 || NULL == kv->value) {
598✔
519
          tscError("invalid dyn view info, len:%d, value:%p", kv->valueLen, kv->value);
×
520
          break;
×
521
        }
522
        if (TSDB_CODE_SUCCESS != hbProcessDynViewRsp(kv->value, kv->valueLen, pCatalog)) {
598✔
523
          tscError("Process dyn view response failed, len:%d, value:%p", kv->valueLen, kv->value);
×
524
          break;
×
525
        }
526
        break;
598✔
527
      }
528
      case HEARTBEAT_KEY_VIEWINFO: {
598✔
529
        if (kv->valueLen <= 0 || NULL == kv->value) {
598✔
530
          tscError("invalid view info, len:%d, value:%p", kv->valueLen, kv->value);
×
531
          break;
×
532
        }
533
        if (TSDB_CODE_SUCCESS != hbProcessViewInfoRsp(kv->value, kv->valueLen, pCatalog)) {
598✔
534
          tscError("Process view info response failed, len:%d, value:%p", kv->valueLen, kv->value);
×
535
          break;
×
536
        }
537
        break;
598✔
538
      }
539
#endif
540
      case HEARTBEAT_KEY_TSMA: {
46,200✔
541
        if (kv->valueLen <= 0 || !kv->value) {
46,200✔
542
          tscError("Invalid tsma info, len:%d, value:%p", kv->valueLen, kv->value);
×
543
        }
544
        if (TSDB_CODE_SUCCESS != hbprocessTSMARsp(kv->value, kv->valueLen, pCatalog)) {
46,200✔
545
          tscError("Process tsma info response failed, len:%d, value:%p", kv->valueLen, kv->value);
×
546
        }
547
        break;
46,200✔
548
      }
549
      default:
×
550
        tscError("invalid hb key type:%d", kv->key);
×
551
        break;
×
552
    }
553
  }
554
}
15,674,330✔
555

556
static int32_t hbQueryHbRspHandle(SAppHbMgr *pAppHbMgr, SClientHbRsp *pRsp) {
42,138,259✔
557
  SClientHbReq *pReq = taosHashAcquire(pAppHbMgr->activeInfo, &pRsp->connKey, sizeof(SClientHbKey));
42,138,259✔
558
  if (NULL == pReq) {
42,138,259✔
559
    tscWarn("pReq to get activeInfo, may be dropped, refId:%" PRIx64 ", type:%d", pRsp->connKey.tscRid,
14,158✔
560
            pRsp->connKey.connType);
561
    return TSDB_CODE_SUCCESS;
14,158✔
562
  }
563

564
  if (pRsp->query) {
42,124,101✔
565
    STscObj *pTscObj = (STscObj *)acquireTscObj(pRsp->connKey.tscRid);
42,124,101✔
566
    if (NULL == pTscObj) {
42,124,101✔
567
      tscDebug("tscObj rid %" PRIx64 " not exist", pRsp->connKey.tscRid);
1,454✔
568
    } else {
569
      if (pRsp->query->totalDnodes > 1) {
42,122,647✔
570
        SEpSet  originEpset = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
9,552,333✔
571
        if (!isEpsetEqual(&originEpset, &pRsp->query->epSet)) {
9,552,333✔
572
          SEpSet *pOrig = &originEpset;
29,788✔
573
          SEp    *pOrigEp = &pOrig->eps[pOrig->inUse];
29,788✔
574
          SEp    *pNewEp = &pRsp->query->epSet.eps[pRsp->query->epSet.inUse];
29,788✔
575
          tscDebug("mnode epset updated from %d/%d=>%s:%d to %d/%d=>%s:%d in hb", pOrig->inUse, pOrig->numOfEps,
29,788✔
576
                   pOrigEp->fqdn, pOrigEp->port, pRsp->query->epSet.inUse, pRsp->query->epSet.numOfEps, pNewEp->fqdn,
577
                   pNewEp->port);
578

579
          updateEpSet_s(&pTscObj->pAppInfo->mgmtEp, &pRsp->query->epSet);
29,788✔
580
        }
581
      }
582

583
      pTscObj->pAppInfo->totalDnodes = pRsp->query->totalDnodes;
42,122,647✔
584
      pTscObj->pAppInfo->onlineDnodes = pRsp->query->onlineDnodes;
42,122,647✔
585
      pTscObj->connId = pRsp->query->connId;
42,122,647✔
586
      tscTrace("connId:%u, hb rsp, dnodes %d/%d", pTscObj->connId, pTscObj->pAppInfo->onlineDnodes,
42,122,647✔
587
               pTscObj->pAppInfo->totalDnodes);
588

589
      if (pRsp->query->killRid) {
42,122,647✔
590
        tscDebug("QID:0x%" PRIx64 ", need to be killed now", pRsp->query->killRid);
4✔
591
        SRequestObj *pRequest = acquireRequest(pRsp->query->killRid);
4✔
592
        if (NULL == pRequest) {
4✔
593
          tscDebug("QID:0x%" PRIx64 ", not exist to kill", pRsp->query->killRid);
×
594
        } else {
595
          taos_stop_query((TAOS_RES *)pRequest);
4✔
596
          (void)releaseRequest(pRsp->query->killRid);
4✔
597
        }
598
      }
599

600
      if (pRsp->query->killConnection) {
42,122,647✔
601
        taos_close_internal(pTscObj);
×
602
      }
603

604
      if (pRsp->query->pQnodeList) {
42,122,647✔
605
        if (TSDB_CODE_SUCCESS != updateQnodeList(pTscObj->pAppInfo, pRsp->query->pQnodeList)) {
31,708✔
606
          tscWarn("update qnode list failed");
×
607
        }
608
      }
609

610
      releaseTscObj(pRsp->connKey.tscRid);
42,122,647✔
611
    }
612
  }
613

614
  int32_t kvNum = pRsp->info ? taosArrayGetSize(pRsp->info) : 0;
42,124,101✔
615

616
  tscDebug("hb got %d rsp kv", kvNum);
42,124,101✔
617

618
  if (kvNum > 0) {
42,124,101✔
619
    struct SCatalog *pCatalog = NULL;
15,674,330✔
620
    int32_t          code = catalogGetHandle(pReq->clusterId, &pCatalog);
15,674,330✔
621
    if (code != TSDB_CODE_SUCCESS) {
15,674,330✔
622
      tscWarn("catalogGetHandle failed, clusterId:0x%" PRIx64 ", error:%s", pReq->clusterId, tstrerror(code));
×
623
    } else {
624
      hbProcessQueryRspKvs(kvNum, pRsp->info, pCatalog, pAppHbMgr);
15,674,330✔
625
    }
626
  }
627

628
  taosHashRelease(pAppHbMgr->activeInfo, pReq);
42,124,101✔
629

630
  return TSDB_CODE_SUCCESS;
42,124,101✔
631
}
632

633
static int32_t hbAsyncCallBack(void *param, SDataBuf *pMsg, int32_t code) {
35,700,223✔
634
  if (0 == atomic_load_8(&clientHbMgr.inited)) {
35,700,223✔
635
    goto _return;
×
636
  }
637

638
  static int32_t    emptyRspNum = 0;
639
  int32_t           idx = *(int32_t *)param;
35,699,902✔
640
  SClientHbBatchRsp pRsp = {0};
35,700,223✔
641
  if (TSDB_CODE_SUCCESS == code) {
35,700,223✔
642
    code = tDeserializeSClientHbBatchRsp(pMsg->pData, pMsg->len, &pRsp);
35,454,773✔
643
    if (TSDB_CODE_SUCCESS != code) {
35,453,444✔
644
      tscError("deserialize hb rsp failed");
80✔
645
    }
646
    int32_t now = taosGetTimestampSec();
35,453,444✔
647
    int32_t delta = abs(now - pRsp.svrTimestamp);
35,453,347✔
648
    if (delta > tsTimestampDeltaLimit) {
35,453,347✔
649
      code = TSDB_CODE_TIME_UNSYNCED;
80✔
650
      tscError("time diff:%ds is too big", delta);
80✔
651
    }
652
  }
653

654
  int32_t rspNum = taosArrayGetSize(pRsp.rsps);
35,698,797✔
655

656
  (void)taosThreadMutexLock(&clientHbMgr.lock);
35,697,468✔
657

658
  SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, idx);
35,700,231✔
659
  if (pAppHbMgr == NULL) {
35,700,231✔
660
    (void)taosThreadMutexUnlock(&clientHbMgr.lock);
×
661
    tscError("appHbMgr not exist, idx:%d", idx);
×
662
    taosMemoryFree(pMsg->pData);
×
663
    taosMemoryFree(pMsg->pEpSet);
×
664
    tFreeClientHbBatchRsp(&pRsp);
665
    return TSDB_CODE_OUT_OF_RANGE;
×
666
  }
667

668
  SAppInstInfo *pInst = pAppHbMgr->pAppInstInfo;
35,700,231✔
669

670
  if (code != 0) {
35,700,231✔
671
    pInst->onlineDnodes = pInst->totalDnodes ? 0 : -1;
245,538✔
672
    tscDebug("hb rsp error %s, update server status %d/%d", tstrerror(code), pInst->onlineDnodes, pInst->totalDnodes);
245,538✔
673
    (void)taosThreadMutexUnlock(&clientHbMgr.lock);
245,538✔
674
    taosMemoryFree(pMsg->pData);
245,538✔
675
    taosMemoryFree(pMsg->pEpSet);
245,538✔
676
    tFreeClientHbBatchRsp(&pRsp);
677
    return code;
245,538✔
678
  }
679

680
  pInst->serverCfg.monitorParas = pRsp.monitorParas;
35,454,693✔
681
  pInst->serverCfg.enableAuditDelete = pRsp.enableAuditDelete;
35,454,693✔
682
  pInst->serverCfg.enableAuditSelect = pRsp.enableAuditSelect;
35,454,693✔
683
  pInst->serverCfg.enableAuditInsert = pRsp.enableAuditInsert;
35,454,693✔
684
  pInst->serverCfg.auditLevel = pRsp.auditLevel;
35,454,693✔
685
  pInst->serverCfg.enableStrongPass = pRsp.enableStrongPass;
35,454,693✔
686
  tsEnableStrongPassword = pInst->serverCfg.enableStrongPass;
35,454,693✔
687
  tscDebug("monitor paras from hb, clusterId:0x%" PRIx64 ", threshold:%d scope:%d", pInst->clusterId,
35,454,693✔
688
           pRsp.monitorParas.tsSlowLogThreshold, pRsp.monitorParas.tsSlowLogScope);
689

690
  if (rspNum) {
35,454,693✔
691
    tscDebug("hb got %d rsp, %d empty rsp received before", rspNum,
34,669,827✔
692
             atomic_val_compare_exchange_32(&emptyRspNum, emptyRspNum, 0));
693
  } else {
694
    (void)atomic_add_fetch_32(&emptyRspNum, 1);
784,866✔
695
  }
696

697
  for (int32_t i = 0; i < rspNum; ++i) {
77,592,952✔
698
    SClientHbRsp *rsp = taosArrayGet(pRsp.rsps, i);
42,138,259✔
699
    code = (*clientHbMgr.rspHandle[rsp->connKey.connType])(pAppHbMgr, rsp);
42,138,259✔
700
    if (code) {
42,138,259✔
701
      break;
×
702
    }
703
  }
704

705
  (void)taosThreadMutexUnlock(&clientHbMgr.lock);
35,454,693✔
706

707
  tFreeClientHbBatchRsp(&pRsp);
708

709
_return:
35,454,693✔
710
  taosMemoryFree(pMsg->pData);
35,454,693✔
711
  taosMemoryFree(pMsg->pEpSet);
35,454,693✔
712
  return code;
35,454,693✔
713
}
714

715
int32_t hbBuildQueryDesc(SQueryHbReqBasic *hbBasic, STscObj *pObj) {
38,283,212✔
716
  int64_t    now = taosGetTimestampUs();
38,283,212✔
717
  SQueryDesc desc = {0};
38,283,212✔
718
  int32_t    code = 0;
38,283,212✔
719

720
  void *pIter = taosHashIterate(pObj->pRequests, NULL);
38,283,212✔
721
  while (pIter != NULL) {
77,331,141✔
722
    int64_t     *rid = pIter;
39,047,929✔
723
    SRequestObj *pRequest = acquireRequest(*rid);
39,047,929✔
724
    if (NULL == pRequest) {
39,047,929✔
725
      pIter = taosHashIterate(pObj->pRequests, pIter);
49,873✔
726
      continue;
49,873✔
727
    }
728

729
    if (pRequest->killed || 0 == pRequest->body.queryJob) {
38,998,056✔
730
      (void)releaseRequest(*rid);
14,651,781✔
731
      pIter = taosHashIterate(pObj->pRequests, pIter);
14,651,781✔
732
      continue;
14,651,781✔
733
    }
734

735
    tstrncpy(desc.sql, pRequest->sqlstr, sizeof(desc.sql));
24,346,275✔
736
    desc.stime = pRequest->metric.start / 1000;
24,346,275✔
737
    desc.queryId = pRequest->requestId;
24,346,275✔
738
    desc.useconds = now - pRequest->metric.start;
24,346,275✔
739
    desc.reqRid = pRequest->self;
24,346,275✔
740
    desc.stableQuery = pRequest->stableQuery;
24,346,275✔
741
    desc.isSubQuery = pRequest->isSubReq;
24,346,275✔
742
    code = taosGetFqdn(desc.fqdn);
24,346,275✔
743
    if (TSDB_CODE_SUCCESS != code) {
24,346,275✔
744
      (void)releaseRequest(*rid);
×
745
      tscError("get fqdn failed");
×
746
      return TSDB_CODE_FAILED;
×
747
    }
748
    desc.subPlanNum = pRequest->body.subplanNum;
24,346,275✔
749

750
    if (desc.subPlanNum) {
24,346,275✔
751
      desc.subDesc = taosArrayInit(desc.subPlanNum, sizeof(SQuerySubDesc));
24,346,275✔
752
      if (NULL == desc.subDesc) {
24,346,275✔
753
        (void)releaseRequest(*rid);
×
754
        return terrno;
×
755
      }
756

757
      code = schedulerGetTasksStatus(pRequest->body.queryJob, desc.subDesc);
24,346,275✔
758
      if (code) {
24,346,275✔
759
        taosArrayDestroy(desc.subDesc);
7,082,188✔
760
        desc.subDesc = NULL;
7,082,188✔
761
        code = TSDB_CODE_SUCCESS;
7,082,188✔
762
      }
763
      desc.subPlanNum = taosArrayGetSize(desc.subDesc);
24,346,275✔
764
    } else {
765
      desc.subDesc = NULL;
×
766
    }
767

768
    (void)releaseRequest(*rid);
24,346,275✔
769
    if (NULL == taosArrayPush(hbBasic->queryDesc, &desc)) {
48,692,550✔
770
      taosArrayDestroy(desc.subDesc);
×
771
      return terrno;
×
772
    }
773

774
    pIter = taosHashIterate(pObj->pRequests, pIter);
24,346,275✔
775
  }
776

777
  return code;
38,283,212✔
778
}
779

780
int32_t hbGetQueryBasicInfo(SClientHbKey *connKey, SClientHbReq *req) {
42,399,866✔
781
  STscObj *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
42,399,866✔
782
  if (NULL == pTscObj) {
42,399,866✔
783
    tscWarn("tscObj rid 0x%" PRIx64 " not exist", connKey->tscRid);
×
784
    return terrno;
×
785
  }
786

787
  SQueryHbReqBasic *hbBasic = (SQueryHbReqBasic *)taosMemoryCalloc(1, sizeof(SQueryHbReqBasic));
42,399,866✔
788
  if (NULL == hbBasic) {
42,399,866✔
789
    tscError("calloc %d failed", (int32_t)sizeof(SQueryHbReqBasic));
×
790
    releaseTscObj(connKey->tscRid);
×
791
    return terrno;
×
792
  }
793

794
  hbBasic->connId = pTscObj->connId;
42,399,866✔
795

796
  int32_t numOfQueries = pTscObj->pRequests ? taosHashGetSize(pTscObj->pRequests) : 0;
42,399,866✔
797
  if (numOfQueries <= 0) {
42,399,866✔
798
    req->query = hbBasic;
4,116,654✔
799
    releaseTscObj(connKey->tscRid);
4,116,654✔
800
    tscDebug("no queries on connection");
4,116,654✔
801
    return TSDB_CODE_SUCCESS;
4,116,654✔
802
  }
803

804
  hbBasic->queryDesc = taosArrayInit(numOfQueries, sizeof(SQueryDesc));
38,283,212✔
805
  if (NULL == hbBasic->queryDesc) {
38,283,212✔
806
    tscWarn("taosArrayInit %d queryDesc failed", numOfQueries);
×
807
    releaseTscObj(connKey->tscRid);
×
808
    taosMemoryFree(hbBasic);
×
809
    return terrno;
×
810
  }
811

812
  int32_t code = hbBuildQueryDesc(hbBasic, pTscObj);
38,283,212✔
813
  if (code) {
38,283,212✔
814
    releaseTscObj(connKey->tscRid);
×
815
    if (hbBasic->queryDesc) {
×
816
      taosArrayDestroyEx(hbBasic->queryDesc, tFreeClientHbQueryDesc);
×
817
    }
818
    taosMemoryFree(hbBasic);
×
819
    return code;
×
820
  }
821

822
  req->query = hbBasic;
38,283,212✔
823
  releaseTscObj(connKey->tscRid);
38,283,212✔
824

825
  return TSDB_CODE_SUCCESS;
38,283,212✔
826
}
827

828
static int32_t hbGetUserAuthInfo(SClientHbKey *connKey, SHbParam *param, SClientHbReq *req) {
841,716✔
829
  STscObj *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
841,716✔
830
  if (!pTscObj) {
841,716✔
UNCOV
831
    tscWarn("tscObj rid 0x%" PRIx64 " not exist", connKey->tscRid);
×
UNCOV
832
    return terrno;
×
833
  }
834

835
  int32_t code = 0;
841,716✔
836

837
  SKv  kv = {.key = HEARTBEAT_KEY_USER_AUTHINFO};
841,716✔
838
  SKv *pKv = NULL;
841,716✔
839
  if ((pKv = taosHashGet(req->info, &kv.key, sizeof(kv.key)))) {
841,716✔
840
    int32_t           userNum = pKv->valueLen / sizeof(SUserAuthVersion);
471,674✔
841
    SUserAuthVersion *userAuths = (SUserAuthVersion *)pKv->value;
471,674✔
842
    for (int32_t i = 0; i < userNum; ++i) {
487,298✔
843
      SUserAuthVersion *pUserAuth = userAuths + i;
487,298✔
844
      // both key and user exist, update version
845
      if (strncmp(pUserAuth->user, pTscObj->user, TSDB_USER_LEN) == 0) {
487,298✔
846
        pUserAuth->version = htonl(-1);  // force get userAuthInfo
471,674✔
847
        goto _return;
471,674✔
848
      }
849
    }
850
    // key exists, user not exist, append user
851
    SUserAuthVersion *qUserAuth =
×
852
        (SUserAuthVersion *)taosMemoryRealloc(pKv->value, (userNum + 1) * sizeof(SUserAuthVersion));
×
853
    if (qUserAuth) {
×
854
      tstrncpy((qUserAuth + userNum)->user, pTscObj->user, TSDB_USER_LEN);
×
855
      (qUserAuth + userNum)->version = htonl(-1);  // force get userAuthInfo
×
856
      pKv->value = qUserAuth;
×
857
      pKv->valueLen += sizeof(SUserAuthVersion);
×
858
    } else {
859
      code = terrno;
×
860
    }
861
    goto _return;
×
862
  }
863

864
  // key/user not exist, add user
865
  SUserAuthVersion *user = taosMemoryMalloc(sizeof(SUserAuthVersion));
370,042✔
866
  if (!user) {
370,042✔
867
    code = terrno;
×
868
    goto _return;
×
869
  }
870
  tstrncpy(user->user, pTscObj->user, TSDB_USER_LEN);
370,042✔
871
  user->version = htonl(-1);  // force get userAuthInfo
370,042✔
872
  kv.valueLen = sizeof(SUserAuthVersion);
370,042✔
873
  kv.value = user;
370,042✔
874

875
  tscDebug("hb got user auth info, valueLen:%d, user:%s, authVer:%d, tscRid:%" PRIi64, kv.valueLen, user->user,
370,042✔
876
           pTscObj->authVer, connKey->tscRid);
877

878
  if (!req->info) {
370,042✔
879
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
370,042✔
880
    if (NULL == req->info) {
370,042✔
881
      code = terrno;
×
882
      goto _return;
×
883
    }
884
  }
885

886
  if (taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)) != 0) {
370,042✔
887
    taosMemoryFree(user);
×
888
    code = terrno ? terrno : TSDB_CODE_APP_ERROR;
×
889
    goto _return;
×
890
  }
891

892
_return:
834,122✔
893
  releaseTscObj(connKey->tscRid);
841,716✔
894
  if (code) {
841,716✔
895
    tscError("hb got user auth info failed since %s", tstrerror(code));
×
896
  }
897

898
  return code;
841,716✔
899
}
900

901
int32_t hbGetExpiredUserInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
32,224,933✔
902
  SUserAuthVersion *users = NULL;
32,224,933✔
903
  uint32_t          userNum = 0;
32,224,933✔
904
  int32_t           code = 0;
32,224,933✔
905

906
  code = catalogGetExpiredUsers(pCatalog, &users, &userNum);
32,224,933✔
907
  if (TSDB_CODE_SUCCESS != code) {
32,224,933✔
908
    return code;
×
909
  }
910

911
  if (userNum <= 0) {
32,224,933✔
912
    taosMemoryFree(users);
307,163✔
913
    return TSDB_CODE_SUCCESS;
307,163✔
914
  }
915

916
  for (int32_t i = 0; i < userNum; ++i) {
63,901,602✔
917
    SUserAuthVersion *user = &users[i];
31,983,832✔
918
    user->version = htonl(user->version);
31,983,832✔
919
  }
920

921
  SKv kv = {
31,917,770✔
922
      .key = HEARTBEAT_KEY_USER_AUTHINFO,
923
      .valueLen = sizeof(SUserAuthVersion) * userNum,
31,917,770✔
924
      .value = users,
925
  };
926

927
  tscDebug("hb got %d expired users, valueLen:%d", userNum, kv.valueLen);
31,917,770✔
928

929
  if (NULL == req->info) {
31,917,770✔
930
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
31,917,770✔
931
    if (NULL == req->info) {
31,917,770✔
932
      taosMemoryFree(users);
×
933
      return terrno;
×
934
    }
935
  }
936

937
  code = taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
31,917,770✔
938
  if (TSDB_CODE_SUCCESS != code) {
31,917,770✔
939
    taosMemoryFree(users);
×
940
    return code;
×
941
  }
942

943
  return TSDB_CODE_SUCCESS;
31,917,770✔
944
}
945

946
int32_t hbGetExpiredDBInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
34,923,197✔
947
  SDbCacheInfo *dbs = NULL;
34,923,197✔
948
  uint32_t      dbNum = 0;
34,923,197✔
949
  int32_t       code = 0;
34,923,197✔
950

951
  code = catalogGetExpiredDBs(pCatalog, &dbs, &dbNum);
34,923,197✔
952
  if (TSDB_CODE_SUCCESS != code) {
34,923,197✔
953
    return code;
×
954
  }
955

956
  if (dbNum <= 0) {
34,923,197✔
957
    taosMemoryFree(dbs);
24,208,441✔
958
    return TSDB_CODE_SUCCESS;
24,208,441✔
959
  }
960

961
  for (int32_t i = 0; i < dbNum; ++i) {
23,221,016✔
962
    SDbCacheInfo *db = &dbs[i];
12,506,260✔
963
    tscDebug("the %dth expired db:%s, dbId:%" PRId64
12,506,260✔
964
             ", vgVersion:%d, cfgVersion:%d, numOfTable:%d, startTs:%" PRId64,
965
             i, db->dbFName, db->dbId, db->vgVersion, db->cfgVersion, db->numOfTable, db->stateTs);
966

967
    db->dbId = htobe64(db->dbId);
12,506,260✔
968
    db->vgVersion = htonl(db->vgVersion);
12,506,260✔
969
    db->cfgVersion = htonl(db->cfgVersion);
12,506,260✔
970
    db->numOfTable = htonl(db->numOfTable);
12,506,260✔
971
    db->stateTs = htobe64(db->stateTs);
12,506,260✔
972
    db->tsmaVersion = htonl(db->tsmaVersion);
12,506,260✔
973
  }
974

975
  SKv kv = {
10,714,756✔
976
      .key = HEARTBEAT_KEY_DBINFO,
977
      .valueLen = sizeof(SDbCacheInfo) * dbNum,
10,714,756✔
978
      .value = dbs,
979
  };
980

981
  tscDebug("hb got %d expired db, valueLen:%d", dbNum, kv.valueLen);
10,714,756✔
982

983
  if (NULL == req->info) {
10,714,756✔
984
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
260✔
985
    if (NULL == req->info) {
260✔
986
      taosMemoryFree(dbs);
×
987
      return terrno;
×
988
    }
989
  }
990

991
  code = taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
10,714,756✔
992
  if (TSDB_CODE_SUCCESS != code) {
10,714,756✔
993
    taosMemoryFree(dbs);
×
994
    return code;
×
995
  }
996

997
  return TSDB_CODE_SUCCESS;
10,714,756✔
998
}
999

1000
int32_t hbGetExpiredStbInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
34,923,197✔
1001
  SSTableVersion *stbs = NULL;
34,923,197✔
1002
  uint32_t        stbNum = 0;
34,923,197✔
1003
  int32_t         code = 0;
34,923,197✔
1004

1005
  code = catalogGetExpiredSTables(pCatalog, &stbs, &stbNum);
34,923,197✔
1006
  if (TSDB_CODE_SUCCESS != code) {
34,923,197✔
1007
    return code;
×
1008
  }
1009

1010
  if (stbNum <= 0) {
34,923,197✔
1011
    taosMemoryFree(stbs);
27,878,263✔
1012
    return TSDB_CODE_SUCCESS;
27,878,263✔
1013
  }
1014

1015
  for (int32_t i = 0; i < stbNum; ++i) {
16,341,508✔
1016
    SSTableVersion *stb = &stbs[i];
9,296,574✔
1017
    stb->suid = htobe64(stb->suid);
9,296,574✔
1018
    stb->sversion = htonl(stb->sversion);
9,296,574✔
1019
    stb->tversion = htonl(stb->tversion);
9,296,574✔
1020
    stb->smaVer = htonl(stb->smaVer);
9,296,574✔
1021
  }
1022

1023
  SKv kv = {
7,044,934✔
1024
      .key = HEARTBEAT_KEY_STBINFO,
1025
      .valueLen = sizeof(SSTableVersion) * stbNum,
7,044,934✔
1026
      .value = stbs,
1027
  };
1028

1029
  tscDebug("hb got %d expired stb, valueLen:%d", stbNum, kv.valueLen);
7,044,934✔
1030

1031
  if (NULL == req->info) {
7,044,934✔
1032
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
87✔
1033
    if (NULL == req->info) {
87✔
1034
      taosMemoryFree(stbs);
×
1035
      return terrno;
×
1036
    }
1037
  }
1038

1039
  code = taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
7,044,934✔
1040
  if (TSDB_CODE_SUCCESS != code) {
7,044,934✔
1041
    taosMemoryFree(stbs);
×
1042
    return code;
×
1043
  }
1044

1045
  return TSDB_CODE_SUCCESS;
7,044,934✔
1046
}
1047

1048
int32_t hbGetExpiredViewInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
34,923,197✔
1049
  SViewVersion    *views = NULL;
34,923,197✔
1050
  uint32_t         viewNum = 0;
34,923,197✔
1051
  int32_t          code = 0;
34,923,197✔
1052
  SDynViewVersion *pDynViewVer = NULL;
34,923,197✔
1053

1054
  TSC_ERR_JRET(catalogGetExpiredViews(pCatalog, &views, &viewNum, &pDynViewVer));
34,923,197✔
1055

1056
  if (viewNum <= 0) {
34,923,197✔
1057
    taosMemoryFree(views);
34,920,409✔
1058
    taosMemoryFree(pDynViewVer);
34,920,409✔
1059
    return TSDB_CODE_SUCCESS;
34,920,409✔
1060
  }
1061

1062
  for (int32_t i = 0; i < viewNum; ++i) {
5,736✔
1063
    SViewVersion *view = &views[i];
2,948✔
1064
    view->dbId = htobe64(view->dbId);
2,948✔
1065
    view->viewId = htobe64(view->viewId);
2,948✔
1066
    view->version = htonl(view->version);
2,948✔
1067
  }
1068

1069
  tscDebug("hb got %u expired view, valueLen:%lu", viewNum, sizeof(SViewVersion) * viewNum);
2,788✔
1070

1071
  if (NULL == req->info) {
2,788✔
1072
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
×
1073
    if (NULL == req->info) {
×
1074
      TSC_ERR_JRET(terrno);
×
1075
    }
1076
  }
1077

1078
  SKv kv = {
2,788✔
1079
      .key = HEARTBEAT_KEY_DYN_VIEW,
1080
      .valueLen = sizeof(SDynViewVersion),
1081
      .value = pDynViewVer,
1082
  };
1083

1084
  TSC_ERR_JRET(taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)));
2,788✔
1085

1086
  kv.key = HEARTBEAT_KEY_VIEWINFO;
2,788✔
1087
  kv.valueLen = sizeof(SViewVersion) * viewNum;
2,788✔
1088
  kv.value = views;
2,788✔
1089

1090
  TSC_ERR_JRET(taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)));
2,788✔
1091
  return TSDB_CODE_SUCCESS;
2,788✔
1092
_return:
×
1093
  taosMemoryFree(views);
×
1094
  taosMemoryFree(pDynViewVer);
×
1095
  return code;
×
1096
}
1097

1098
int32_t hbGetExpiredTSMAInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *pReq) {
34,923,197✔
1099
  int32_t       code = 0;
34,923,197✔
1100
  uint32_t      tsmaNum = 0;
34,923,197✔
1101
  STSMAVersion *tsmas = NULL;
34,923,197✔
1102

1103
  code = catalogGetExpiredTsmas(pCatalog, &tsmas, &tsmaNum);
34,923,197✔
1104
  if (code) {
34,923,197✔
1105
    taosMemoryFree(tsmas);
×
1106
    return code;
×
1107
  }
1108

1109
  if (tsmaNum <= 0) {
34,923,197✔
1110
    taosMemoryFree(tsmas);
34,876,997✔
1111
    return TSDB_CODE_SUCCESS;
34,876,997✔
1112
  }
1113

1114
  for (int32_t i = 0; i < tsmaNum; ++i) {
92,400✔
1115
    STSMAVersion *tsma = &tsmas[i];
46,200✔
1116
    tsma->dbId = htobe64(tsma->dbId);
46,200✔
1117
    tsma->tsmaId = htobe64(tsma->tsmaId);
46,200✔
1118
    tsma->version = htonl(tsma->version);
46,200✔
1119
  }
1120

1121
  tscDebug("hb got %d expred tsmas, valueLen:%lu", tsmaNum, sizeof(STSMAVersion) * tsmaNum);
46,200✔
1122

1123
  if (!pReq->info) {
46,200✔
1124
    pReq->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
×
1125
    if (!pReq->info) {
×
1126
      taosMemoryFree(tsmas);
×
1127
      return terrno;
×
1128
    }
1129
  }
1130

1131
  SKv kv = {.key = HEARTBEAT_KEY_TSMA, .valueLen = sizeof(STSMAVersion) * tsmaNum, .value = tsmas};
46,200✔
1132
  code = taosHashPut(pReq->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
46,200✔
1133
  if (TSDB_CODE_SUCCESS != code) {
46,200✔
1134
    taosMemoryFree(tsmas);
×
1135
    return code;
×
1136
  }
1137
  return TSDB_CODE_SUCCESS;
46,200✔
1138
}
1139

1140
int32_t hbGetAppInfo(int64_t clusterId, SClientHbReq *req) {
42,399,866✔
1141
  SAppHbReq *pApp = taosHashGet(clientHbMgr.appSummary, &clusterId, sizeof(clusterId));
42,399,866✔
1142
  if (NULL != pApp) {
42,399,866✔
1143
    (void)memcpy(&req->app, pApp, sizeof(*pApp));
42,399,866✔
1144
  } else {
1145
    (void)memset(&req->app.summary, 0, sizeof(req->app.summary));
×
1146
    req->app.pid = taosGetPId();
×
1147
    req->app.appId = clientHbMgr.appId;
×
1148
    TSC_ERR_RET(taosGetAppName(req->app.name, NULL));
×
1149
  }
1150

1151
  return TSDB_CODE_SUCCESS;
42,399,866✔
1152
}
1153

1154
int32_t hbQueryHbReqHandle(SClientHbKey *connKey, void *param, SClientHbReq *req) {
42,399,866✔
1155
  int32_t   code = 0;
42,399,866✔
1156
  SHbParam *hbParam = (SHbParam *)param;
42,399,866✔
1157
  SCatalog *pCatalog = NULL;
42,399,866✔
1158

1159
  code = hbGetQueryBasicInfo(connKey, req);
42,399,866✔
1160
  if (code != TSDB_CODE_SUCCESS) {
42,399,866✔
1161
    tscWarn("hbGetQueryBasicInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1162
    return code;
×
1163
  }
1164

1165
  if (hbParam->reqCnt == 0) {
42,399,866✔
1166
    code = catalogGetHandle(hbParam->clusterId, &pCatalog);
34,923,197✔
1167
    if (code != TSDB_CODE_SUCCESS) {
34,923,197✔
1168
      tscWarn("catalogGetHandle failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1169
      return code;
×
1170
    }
1171

1172
    code = hbGetAppInfo(hbParam->clusterId, req);
34,923,197✔
1173
    if (TSDB_CODE_SUCCESS != code) {
34,923,197✔
1174
      tscWarn("getAppInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1175
      return code;
×
1176
    }
1177

1178
    if (!taosHashGet(clientHbMgr.appHbHash, &hbParam->clusterId, sizeof(hbParam->clusterId))) {
34,923,197✔
1179
      code = hbGetExpiredUserInfo(connKey, pCatalog, req);
32,224,933✔
1180
      if (TSDB_CODE_SUCCESS != code) {
32,224,933✔
1181
        tscWarn("hbGetExpiredUserInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1182
        return code;
×
1183
      }
1184
      if (clientHbMgr.appHbHash) {
32,224,933✔
1185
        code = taosHashPut(clientHbMgr.appHbHash, &hbParam->clusterId, sizeof(uint64_t), NULL, 0);
4,100,426✔
1186
        if (TSDB_CODE_SUCCESS != code) {
4,100,426✔
1187
          tscWarn("hbQueryHbReqHandle put clusterId failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId,
×
1188
                  tstrerror(code));
1189
          return code;
×
1190
        }
1191
      }
1192
    }
1193

1194
    // invoke after hbGetExpiredUserInfo
1195
    if (2 != atomic_load_8(&hbParam->pAppHbMgr->connHbFlag)) {
34,923,197✔
1196
      code = hbGetUserAuthInfo(connKey, hbParam, req);
841,716✔
1197
      if (TSDB_CODE_SUCCESS != code) {
841,716✔
UNCOV
1198
        tscWarn("hbGetUserAuthInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
UNCOV
1199
        return code;
×
1200
      }
1201
      atomic_store_8(&hbParam->pAppHbMgr->connHbFlag, 1);
841,716✔
1202
    }
1203

1204
    code = hbGetExpiredDBInfo(connKey, pCatalog, req);
34,923,197✔
1205
    if (TSDB_CODE_SUCCESS != code) {
34,923,197✔
1206
      tscWarn("hbGetExpiredDBInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1207
      return code;
×
1208
    }
1209

1210
    code = hbGetExpiredStbInfo(connKey, pCatalog, req);
34,923,197✔
1211
    if (TSDB_CODE_SUCCESS != code) {
34,923,197✔
1212
      tscWarn("hbGetExpiredStbInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1213
      return code;
×
1214
    }
1215

1216
#ifdef TD_ENTERPRISE
1217
    code = hbGetExpiredViewInfo(connKey, pCatalog, req);
34,923,197✔
1218
    if (TSDB_CODE_SUCCESS != code) {
34,923,197✔
1219
      tscWarn("hbGetExpiredViewInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1220
      return code;
×
1221
    }
1222
#endif
1223
    code = hbGetExpiredTSMAInfo(connKey, pCatalog, req);
34,923,197✔
1224
    if (TSDB_CODE_SUCCESS != code) {
34,923,197✔
1225
      tscWarn("hbGetExpiredTSMAInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1226
      return code;
×
1227
    }
1228
  } else {
1229
    code = hbGetAppInfo(hbParam->clusterId, req);
7,476,669✔
1230
    if (TSDB_CODE_SUCCESS != code) {
7,476,669✔
1231
      tscWarn("hbGetAppInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1232
      return code;
×
1233
    }
1234
  }
1235

1236
  ++hbParam->reqCnt;  // success to get catalog info
42,399,866✔
1237

1238
  return TSDB_CODE_SUCCESS;
42,399,866✔
1239
}
1240

1241
static FORCE_INLINE void hbMgrInitHandle() {
1242
  // init all handle
1243
  clientHbMgr.reqHandle[CONN_TYPE__QUERY] = hbQueryHbReqHandle;
1,633,207✔
1244
  clientHbMgr.reqHandle[CONN_TYPE__TMQ] = hbQueryHbReqHandle;
1,633,207✔
1245

1246
  clientHbMgr.rspHandle[CONN_TYPE__QUERY] = hbQueryHbRspHandle;
1,633,207✔
1247
  clientHbMgr.rspHandle[CONN_TYPE__TMQ] = hbQueryHbRspHandle;
1,633,207✔
1248
}
1,633,207✔
1249

1250
int32_t hbGatherAllInfo(SAppHbMgr *pAppHbMgr, SClientHbBatchReq **pBatchReq) {
35,709,735✔
1251
  *pBatchReq = taosMemoryCalloc(1, sizeof(SClientHbBatchReq));
35,709,735✔
1252
  if (pBatchReq == NULL) {
35,709,735✔
1253
    return terrno;
×
1254
  }
1255
  int32_t connKeyCnt = atomic_load_32(&pAppHbMgr->connKeyCnt);
35,709,735✔
1256
  (*pBatchReq)->reqs = taosArrayInit(connKeyCnt, sizeof(SClientHbReq));
35,709,735✔
1257
  if (!(*pBatchReq)->reqs) {
35,709,735✔
1258
    tFreeClientHbBatchReq(*pBatchReq);
×
1259
    return terrno;
×
1260
  }
1261

1262
  int64_t  maxIpWhiteVer = 0;
35,709,735✔
1263
  void    *pIter = NULL;
35,709,735✔
1264
  SHbParam param = {0};
35,709,735✔
1265
  while ((pIter = taosHashIterate(pAppHbMgr->activeInfo, pIter))) {
80,384,252✔
1266
    SClientHbReq *pOneReq = pIter;
44,674,517✔
1267
    SClientHbKey *connKey = &pOneReq->connKey;
44,674,517✔
1268
    STscObj      *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
44,674,517✔
1269

1270
    if (!pTscObj || atomic_load_8(&pTscObj->dropped) == 1) {
44,674,517✔
1271
      if (pTscObj) releaseTscObj(connKey->tscRid);
2,274,651✔
1272
      continue;
2,274,651✔
1273
    }
1274

1275
    tstrncpy(pOneReq->userApp, pTscObj->optionInfo.userApp, sizeof(pOneReq->userApp));
42,399,866✔
1276
    tstrncpy(pOneReq->cInfo, pTscObj->optionInfo.cInfo, sizeof(pOneReq->cInfo));
42,399,866✔
1277
    pOneReq->userIp = pTscObj->optionInfo.userIp;
42,399,866✔
1278
    pOneReq->userDualIp = pTscObj->optionInfo.userDualIp;
42,399,866✔
1279
    tstrncpy(pOneReq->sVer, td_version, TSDB_VERSION_LEN);
42,399,866✔
1280

1281
    pOneReq = taosArrayPush((*pBatchReq)->reqs, pOneReq);
42,399,866✔
1282
    if (NULL == pOneReq) {
42,399,866✔
1283
      releaseTscObj(connKey->tscRid);
×
1284
      continue;
×
1285
    }
1286

1287
    switch (connKey->connType) {
42,399,866✔
1288
      case CONN_TYPE__QUERY:
42,399,866✔
1289
      case CONN_TYPE__TMQ: {
1290
        if (param.clusterId == 0) {
42,399,866✔
1291
          // init
1292
          param.clusterId = pOneReq->clusterId;
34,923,197✔
1293
          param.pAppHbMgr = pAppHbMgr;
34,923,197✔
1294
          param.connHbFlag = atomic_load_8(&pAppHbMgr->connHbFlag);
34,923,197✔
1295
        }
1296
        break;
42,399,866✔
1297
      }
1298
      default:
×
1299
        break;
×
1300
    }
1301
    if (clientHbMgr.reqHandle[connKey->connType]) {
42,399,866✔
1302
      int32_t code = (*clientHbMgr.reqHandle[connKey->connType])(connKey, &param, pOneReq);
42,399,866✔
1303
      if (code) {
42,399,866✔
UNCOV
1304
        tscWarn("hbGatherAllInfo failed since %s, tscRid:%" PRIi64 ", connType:%" PRIi8, tstrerror(code),
×
1305
                connKey->tscRid, connKey->connType);
1306
      }
1307
    }
1308

1309
    int64_t ver = atomic_load_64(&pTscObj->whiteListInfo.ver);
42,399,866✔
1310
    maxIpWhiteVer = TMAX(maxIpWhiteVer, ver);
42,399,866✔
1311
    releaseTscObj(connKey->tscRid);
42,399,866✔
1312
  }
1313
  (*pBatchReq)->ipWhiteListVer = maxIpWhiteVer;
35,709,735✔
1314

1315
  return TSDB_CODE_SUCCESS;
35,709,735✔
1316
}
1317

1318
void hbThreadFuncUnexpectedStopped(void) { atomic_store_8(&clientHbMgr.threadStop, 2); }
×
1319

1320
void hbMergeSummary(SAppClusterSummary *dst, SAppClusterSummary *src) {
4,354,622✔
1321
  dst->numOfInsertsReq += src->numOfInsertsReq;
4,354,622✔
1322
  dst->numOfInsertRows += src->numOfInsertRows;
4,354,622✔
1323
  dst->insertElapsedTime += src->insertElapsedTime;
4,354,622✔
1324
  dst->insertBytes += src->insertBytes;
4,354,622✔
1325
  dst->fetchBytes += src->fetchBytes;
4,354,622✔
1326
  dst->queryElapsedTime += src->queryElapsedTime;
4,354,622✔
1327
  dst->numOfSlowQueries += src->numOfSlowQueries;
4,354,622✔
1328
  dst->totalRequests += src->totalRequests;
4,354,622✔
1329
  dst->currentRequests += src->currentRequests;
4,354,622✔
1330
}
4,354,622✔
1331

1332
int32_t hbGatherAppInfo(void) {
34,162,584✔
1333
  SAppHbReq req = {0};
34,162,584✔
1334
  int32_t   code = TSDB_CODE_SUCCESS;
34,162,584✔
1335
  int       sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
34,162,584✔
1336
  if (sz > 0) {
34,162,584✔
1337
    req.pid = taosGetPId();
34,162,584✔
1338
    req.appId = clientHbMgr.appId;
34,162,584✔
1339
    TSC_ERR_RET(taosGetAppName(req.name, NULL));
34,162,584✔
1340
  }
1341

1342
  taosHashClear(clientHbMgr.appSummary);
34,162,584✔
1343

1344
  for (int32_t i = 0; i < sz; ++i) {
72,681,274✔
1345
    SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
38,518,690✔
1346
    if (pAppHbMgr == NULL) continue;
38,518,690✔
1347

1348
    int64_t    clusterId = pAppHbMgr->pAppInstInfo->clusterId;
38,518,690✔
1349
    SAppHbReq *pApp = taosHashGet(clientHbMgr.appSummary, &clusterId, sizeof(clusterId));
38,518,690✔
1350
    if (NULL == pApp) {
38,518,690✔
1351
      (void)memcpy(&req.summary, &pAppHbMgr->pAppInstInfo->summary, sizeof(req.summary));
34,164,068✔
1352
      req.startTime = pAppHbMgr->startTime;
34,164,068✔
1353
      TSC_ERR_RET(taosHashPut(clientHbMgr.appSummary, &clusterId, sizeof(clusterId), &req, sizeof(req)));
34,164,068✔
1354
    } else {
1355
      if (pAppHbMgr->startTime < pApp->startTime) {
4,354,622✔
1356
        pApp->startTime = pAppHbMgr->startTime;
×
1357
      }
1358

1359
      hbMergeSummary(&pApp->summary, &pAppHbMgr->pAppInstInfo->summary);
4,354,622✔
1360
    }
1361
  }
1362

1363
  return TSDB_CODE_SUCCESS;
34,162,584✔
1364
}
1365

1366
static void *hbThreadFunc(void *param) {
1,633,207✔
1367
  setThreadName("hb");
1,633,207✔
1368
#ifdef WINDOWS
1369
  if (taosCheckCurrentInDll()) {
1370
    atexit(hbThreadFuncUnexpectedStopped);
1371
  }
1372
#endif
1373
  while (1) {
32,967,912✔
1374
    if (1 == clientHbMgr.threadStop) {
34,601,119✔
1375
      break;
432,516✔
1376
    }
1377

1378
    if (TSDB_CODE_SUCCESS != taosThreadMutexLock(&clientHbMgr.lock)) {
34,168,603✔
1379
      tscError("taosThreadMutexLock failed");
×
1380
      return NULL;
×
1381
    }
1382

1383
    int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
34,168,603✔
1384
    if (sz > 0) {
34,168,603✔
1385
      if (TSDB_CODE_SUCCESS != hbGatherAppInfo()) {
34,162,584✔
1386
        tscError("hbGatherAppInfo failed");
×
1387
        return NULL;
×
1388
      }
1389
      if (sz > 1 && !clientHbMgr.appHbHash) {
34,162,584✔
1390
        clientHbMgr.appHbHash = taosHashInit(0, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), true, HASH_NO_LOCK);
43,453✔
1391
        if (NULL == clientHbMgr.appHbHash) {
43,453✔
1392
          tscError("taosHashInit failed");
×
1393
          return NULL;
×
1394
        }
1395
      }
1396
      taosHashClear(clientHbMgr.appHbHash);
34,162,584✔
1397
    }
1398

1399
    for (int i = 0; i < sz; i++) {
72,687,293✔
1400
      SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
38,518,690✔
1401
      if (pAppHbMgr == NULL) {
38,518,690✔
1402
        continue;
22,847✔
1403
      }
1404

1405
      int32_t connCnt = atomic_load_32(&pAppHbMgr->connKeyCnt);
38,518,690✔
1406
      if (connCnt == 0) {
38,518,690✔
1407
        continue;
2,808,955✔
1408
      }
1409
      SClientHbBatchReq *pReq = NULL;
35,709,735✔
1410
      int32_t            code = hbGatherAllInfo(pAppHbMgr, &pReq);
35,709,735✔
1411
      if (TSDB_CODE_SUCCESS != code || taosArrayGetP(clientHbMgr.appHbMgrs, i) == NULL) {
35,709,735✔
1412
        terrno = code ? code : TSDB_CODE_OUT_OF_RANGE;
×
1413
        tFreeClientHbBatchReq(pReq);
×
1414
        continue;
×
1415
      }
1416
      int tlen = tSerializeSClientHbBatchReq(NULL, 0, pReq);
35,709,735✔
1417
      if (tlen == -1) {
35,709,735✔
1418
        tFreeClientHbBatchReq(pReq);
×
1419
        break;
×
1420
      }
1421
      void *buf = taosMemoryMalloc(tlen);
35,709,735✔
1422
      if (buf == NULL) {
35,709,735✔
1423
        tFreeClientHbBatchReq(pReq);
×
1424
        // hbClearReqInfo(pAppHbMgr);
1425
        break;
×
1426
      }
1427

1428
      if (tSerializeSClientHbBatchReq(buf, tlen, pReq) == -1) {
35,709,735✔
1429
        tFreeClientHbBatchReq(pReq);
×
1430
        taosMemoryFree(buf);
×
1431
        break;
×
1432
      }
1433
      SMsgSendInfo *pInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
35,709,735✔
1434

1435
      if (pInfo == NULL) {
35,709,735✔
1436
        tFreeClientHbBatchReq(pReq);
×
1437
        // hbClearReqInfo(pAppHbMgr);
1438
        taosMemoryFree(buf);
×
1439
        break;
×
1440
      }
1441
      pInfo->fp = hbAsyncCallBack;
35,709,735✔
1442
      pInfo->msgInfo.pData = buf;
35,709,735✔
1443
      pInfo->msgInfo.len = tlen;
35,709,735✔
1444
      pInfo->msgType = TDMT_MND_HEARTBEAT;
35,709,735✔
1445
      pInfo->param = taosMemoryMalloc(sizeof(int32_t));
35,709,735✔
1446
      if (pInfo->param  == NULL) {
35,709,735✔
1447
        tFreeClientHbBatchReq(pReq);
×
1448
        // hbClearReqInfo(pAppHbMgr);
1449
        taosMemoryFree(buf);
×
1450
        taosMemoryFree(pInfo);
×
1451
        break;
×
1452
      }
1453
      *(int32_t *)pInfo->param = i;
35,709,735✔
1454
      pInfo->paramFreeFp = taosAutoMemoryFree;
35,709,735✔
1455
      pInfo->requestId = generateRequestId();
35,709,735✔
1456
      pInfo->requestObjRefId = 0;
35,709,735✔
1457

1458
      SAppInstInfo *pAppInstInfo = pAppHbMgr->pAppInstInfo;
35,709,735✔
1459
      SEpSet        epSet = getEpSet_s(&pAppInstInfo->mgmtEp);
35,709,735✔
1460
      if (TSDB_CODE_SUCCESS != asyncSendMsgToServer(pAppInstInfo->pTransporter, &epSet, NULL, pInfo)) {
35,709,735✔
1461
        tscWarn("failed to async send msg to server");
×
1462
      }
1463
      tFreeClientHbBatchReq(pReq);
35,709,735✔
1464
      // hbClearReqInfo(pAppHbMgr);
1465
      (void)atomic_add_fetch_32(&pAppHbMgr->reportCnt, 1);
35,709,735✔
1466
    }
1467

1468
    if (TSDB_CODE_SUCCESS != taosThreadMutexUnlock(&clientHbMgr.lock)) {
34,168,603✔
1469
      tscError("taosThreadMutexLock failed");
×
1470
      return NULL;
×
1471
    }
1472
    taosMsleep(HEARTBEAT_INTERVAL);
34,168,603✔
1473
  }
1474
  taosHashCleanup(clientHbMgr.appHbHash);
432,516✔
1475
  return NULL;
432,516✔
1476
}
1477

1478
static int32_t hbCreateThread() {
1,633,207✔
1479
  int32_t      code = TSDB_CODE_SUCCESS;
1,633,207✔
1480
  TdThreadAttr thAttr;
1,622,632✔
1481
  TSC_ERR_JRET(taosThreadAttrInit(&thAttr));
1,633,207✔
1482
  TSC_ERR_JRET(taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE));
1,633,207✔
1483
#ifdef TD_COMPACT_OS
1484
  TSC_ERR_JRET(taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL));
1485
#endif
1486

1487
  if (taosThreadCreate(&clientHbMgr.thread, &thAttr, hbThreadFunc, NULL) != 0) {
1,633,207✔
1488
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
1489
    TSC_ERR_RET(terrno);
×
1490
  }
1491
  (void)taosThreadAttrDestroy(&thAttr);
1,633,207✔
1492
_return:
1,633,207✔
1493

1494
  if (code) {
1,633,207✔
1495
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
1496
    TSC_ERR_RET(terrno);
×
1497
  }
1498

1499
  return code;
1,633,207✔
1500
}
1501

1502
static void hbStopThread() {
1,633,665✔
1503
  if (0 == atomic_load_8(&clientHbMgr.inited)) {
1,633,665✔
1504
    return;
458✔
1505
  }
1506
  if (atomic_val_compare_exchange_8(&clientHbMgr.threadStop, 0, 1)) {
1,633,207✔
1507
    tscDebug("hb thread already stopped");
×
1508
    return;
×
1509
  }
1510

1511
  int32_t code = TSDB_CODE_SUCCESS;
1,633,207✔
1512
  // thread quit mode kill or inner exit from self-thread
1513
  if (clientHbMgr.quitByKill) {
1,633,207✔
1514
    code = taosThreadKill(clientHbMgr.thread, 0);
1,238,446✔
1515
    if (TSDB_CODE_SUCCESS != code) {
1,238,446✔
1516
      tscError("taosThreadKill failed since %s", tstrerror(code));
×
1517
    }
1518
  } else {
1519
    code = taosThreadJoin(clientHbMgr.thread, NULL);
394,761✔
1520
    if (TSDB_CODE_SUCCESS != code) {
394,761✔
1521
      tscError("taosThreadJoin failed since %s", tstrerror(code));
×
1522
    }
1523
  }
1524

1525
  tscDebug("hb thread stopped");
1,633,207✔
1526
}
1527

1528
int32_t appHbMgrInit(SAppInstInfo *pAppInstInfo, char *key, SAppHbMgr **pAppHbMgr) {
1,688,279✔
1529
  int32_t code = TSDB_CODE_SUCCESS;
1,688,279✔
1530
  TSC_ERR_RET(hbMgrInit());
1,688,279✔
1531
  *pAppHbMgr = taosMemoryMalloc(sizeof(SAppHbMgr));
1,688,279✔
1532
  if (*pAppHbMgr == NULL) {
1,688,279✔
1533
    TSC_ERR_JRET(terrno);
×
1534
  }
1535
  // init stat
1536
  (*pAppHbMgr)->startTime = taosGetTimestampMs();
3,364,873✔
1537
  (*pAppHbMgr)->connKeyCnt = 0;
1,688,279✔
1538
  (*pAppHbMgr)->connHbFlag = 0;
1,688,279✔
1539
  (*pAppHbMgr)->reportCnt = 0;
1,688,279✔
1540
  (*pAppHbMgr)->reportBytes = 0;
1,688,279✔
1541
  (*pAppHbMgr)->key = taosStrdup(key);
1,688,279✔
1542
  if ((*pAppHbMgr)->key == NULL) {
1,688,279✔
1543
    TSC_ERR_JRET(terrno);
×
1544
  }
1545

1546
  // init app info
1547
  (*pAppHbMgr)->pAppInstInfo = pAppInstInfo;
1,688,279✔
1548

1549
  // init hash info
1550
  (*pAppHbMgr)->activeInfo = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
1,688,279✔
1551

1552
  if ((*pAppHbMgr)->activeInfo == NULL) {
1,688,279✔
1553
    TSC_ERR_JRET(terrno);
×
1554
  }
1555

1556
  // taosHashSetFreeFp(pAppHbMgr->activeInfo, tFreeClientHbReq);
1557

1558
  TSC_ERR_JRET(taosThreadMutexLock(&clientHbMgr.lock));
1,688,279✔
1559
  if (taosArrayPush(clientHbMgr.appHbMgrs, &(*pAppHbMgr)) == NULL) {
3,376,558✔
1560
    code = terrno;
×
1561
    (void)taosThreadMutexUnlock(&clientHbMgr.lock);
×
1562
    goto _return;
×
1563
  }
1564
  (*pAppHbMgr)->idx = taosArrayGetSize(clientHbMgr.appHbMgrs) - 1;
1,688,279✔
1565
  TSC_ERR_JRET(taosThreadMutexUnlock(&clientHbMgr.lock));
1,688,279✔
1566

1567
  return TSDB_CODE_SUCCESS;
1,688,279✔
1568
_return:
×
1569
  taosMemoryFree(*pAppHbMgr);
×
1570
  return code;
×
1571
}
1572

1573
void hbFreeAppHbMgr(SAppHbMgr *pTarget) {
1,688,279✔
1574
  void *pIter = taosHashIterate(pTarget->activeInfo, NULL);
1,688,279✔
1575
  while (pIter != NULL) {
1,864,886✔
1576
    SClientHbReq *pOneReq = pIter;
176,607✔
1577
    tFreeClientHbReq(pOneReq);
1578
    pIter = taosHashIterate(pTarget->activeInfo, pIter);
176,607✔
1579
  }
1580
  taosHashCleanup(pTarget->activeInfo);
1,688,279✔
1581
  pTarget->activeInfo = NULL;
1,688,279✔
1582

1583
  taosMemoryFree(pTarget->key);
1,688,279✔
1584
  taosMemoryFree(pTarget);
1,688,279✔
1585
}
1,688,279✔
1586

1587
void hbRemoveAppHbMrg(SAppHbMgr **pAppHbMgr) {
1,688,279✔
1588
  int32_t code = TSDB_CODE_SUCCESS;
1,688,279✔
1589
  code = taosThreadMutexLock(&clientHbMgr.lock);
1,688,279✔
1590
  if (TSDB_CODE_SUCCESS != code) {
1,688,279✔
1591
    tscError("failed to lock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1592
  }
1593
  int32_t mgrSize = taosArrayGetSize(clientHbMgr.appHbMgrs);
1,688,279✔
1594
  for (int32_t i = 0; i < mgrSize; ++i) {
1,688,279✔
1595
    SAppHbMgr *pItem = taosArrayGetP(clientHbMgr.appHbMgrs, i);
×
1596
    if (pItem == *pAppHbMgr) {
×
1597
      hbFreeAppHbMgr(*pAppHbMgr);
×
1598
      *pAppHbMgr = NULL;
×
1599
      taosArraySet(clientHbMgr.appHbMgrs, i, pAppHbMgr);
×
1600
      break;
×
1601
    }
1602
  }
1603
  code = taosThreadMutexUnlock(&clientHbMgr.lock);
1,688,279✔
1604
  if (TSDB_CODE_SUCCESS != code) {
1,688,279✔
1605
    tscError("failed to unlock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1606
  }
1607
}
1,688,279✔
1608

1609
void appHbMgrCleanup(void) {
1,633,207✔
1610
  int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
1,633,207✔
1611
  for (int i = 0; i < sz; i++) {
3,321,486✔
1612
    SAppHbMgr *pTarget = taosArrayGetP(clientHbMgr.appHbMgrs, i);
1,688,279✔
1613
    if (pTarget == NULL) continue;
1,688,279✔
1614
    hbFreeAppHbMgr(pTarget);
1,688,279✔
1615
  }
1616
}
1,633,207✔
1617

1618
int32_t hbMgrInit() {
1,688,279✔
1619
  // init once
1620
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 0, 1);
1,688,279✔
1621
  if (old == 1) return 0;
1,688,279✔
1622

1623
  clientHbMgr.appId = tGenIdPI64();
1,633,207✔
1624
  tscInfo("app initialized, appId:0x%" PRIx64, clientHbMgr.appId);
1,633,207✔
1625

1626
  clientHbMgr.appSummary = taosHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
1,633,207✔
1627
  if (NULL == clientHbMgr.appSummary) {
1,633,207✔
1628
    uError("hbMgrInit:taosHashInit error") return terrno;
×
1629
  }
1630
  clientHbMgr.appHbMgrs = taosArrayInit(0, sizeof(void *));
1,633,207✔
1631
  if (NULL == clientHbMgr.appHbMgrs) {
1,633,207✔
1632
    uError("hbMgrInit:taosArrayInit error") return terrno;
×
1633
  }
1634
  TdThreadMutexAttr attr = {0};
1,633,207✔
1635

1636
  int ret = taosThreadMutexAttrInit(&attr);
1,633,207✔
1637
  if (ret != 0) {
1,633,207✔
1638
    uError("hbMgrInit:taosThreadMutexAttrInit error") return ret;
×
1639
  }
1640

1641
  ret = taosThreadMutexAttrSetType(&attr, PTHREAD_MUTEX_RECURSIVE);
1,633,207✔
1642
  if (ret != 0) {
1,633,207✔
1643
    uError("hbMgrInit:taosThreadMutexAttrSetType error") return ret;
×
1644
  }
1645

1646
  ret = taosThreadMutexInit(&clientHbMgr.lock, &attr);
1,633,207✔
1647
  if (ret != 0) {
1,633,207✔
1648
    uError("hbMgrInit:taosThreadMutexInit error") return ret;
×
1649
  }
1650

1651
  ret = taosThreadMutexAttrDestroy(&attr);
1,633,207✔
1652
  if (ret != 0) {
1,633,207✔
1653
    uError("hbMgrInit:taosThreadMutexAttrDestroy error") return ret;
×
1654
  }
1655

1656
  // init handle funcs
1657
  hbMgrInitHandle();
1658

1659
  // init backgroud thread
1660
  ret = hbCreateThread();
1,633,207✔
1661
  if (ret != 0) {
1,633,207✔
1662
    uError("hbMgrInit:hbCreateThread error") return ret;
×
1663
  }
1664

1665
  return 0;
1,633,207✔
1666
}
1667

1668
void hbMgrCleanUp() {
1,633,665✔
1669
  hbStopThread();
1,633,665✔
1670

1671
  // destroy all appHbMgr
1672
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 1, 0);
1,633,665✔
1673
  if (old == 0) return;
1,633,665✔
1674

1675
  int32_t code = taosThreadMutexLock(&clientHbMgr.lock);
1,633,207✔
1676
  if (TSDB_CODE_SUCCESS != code) {
1,633,207✔
1677
    tscError("failed to lock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1678
  }
1679
  appHbMgrCleanup();
1,633,207✔
1680
  taosArrayDestroy(clientHbMgr.appHbMgrs);
1,633,207✔
1681
  clientHbMgr.appHbMgrs = NULL;
1,633,207✔
1682
  code = taosThreadMutexUnlock(&clientHbMgr.lock);
1,633,207✔
1683
  if (TSDB_CODE_SUCCESS != code) {
1,633,207✔
1684
    tscError("failed to unlock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1685
  }
1686
}
1687

1688
int32_t hbRegisterConnImpl(SAppHbMgr *pAppHbMgr, SClientHbKey connKey, const char* user, const char* tokenName, int64_t clusterId) {
2,544,917✔
1689
  // init hash in activeinfo
1690
  void *data = taosHashGet(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
2,544,917✔
1691
  if (data != NULL) {
2,544,917✔
1692
    return 0;
×
1693
  }
1694
  SClientHbReq hbReq = {0};
2,544,917✔
1695
  hbReq.connKey = connKey;
2,544,917✔
1696
  hbReq.clusterId = clusterId;
2,544,917✔
1697
  tstrncpy(hbReq.user, user, sizeof(hbReq.user));
2,544,917✔
1698
  tstrncpy(hbReq.tokenName, tokenName, sizeof(hbReq.tokenName));
2,544,917✔
1699
  // hbReq.info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
1700

1701
  TSC_ERR_RET(taosHashPut(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey), &hbReq, sizeof(SClientHbReq)));
2,544,917✔
1702

1703
  (void)atomic_add_fetch_32(&pAppHbMgr->connKeyCnt, 1);
2,544,917✔
1704
  return 0;
2,544,917✔
1705
}
1706

1707
int32_t hbRegisterConn(SAppHbMgr *pAppHbMgr, int64_t tscRefId, const char* user, const char* tokenName, int64_t clusterId, int8_t connType) {
2,544,917✔
1708
  SClientHbKey connKey = {
2,544,917✔
1709
      .tscRid = tscRefId,
1710
      .connType = connType,
1711
  };
1712

1713
  switch (connType) {
2,544,917✔
1714
    case CONN_TYPE__QUERY:
2,544,917✔
1715
    case CONN_TYPE__TMQ: {
1716
      return hbRegisterConnImpl(pAppHbMgr, connKey, user, tokenName, clusterId);
2,544,917✔
1717
    }
1718
    default:
×
1719
      return 0;
×
1720
  }
1721
}
1722

1723
void hbDeregisterConn(STscObj *pTscObj, SClientHbKey connKey) {
2,369,928✔
1724
  int32_t code = taosThreadMutexLock(&clientHbMgr.lock);
2,369,928✔
1725
  if (TSDB_CODE_SUCCESS != code) {
2,370,009✔
1726
    tscError("failed to lock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1727
  }
1728
  SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, pTscObj->appHbMgrIdx);
2,370,009✔
1729
  if (pAppHbMgr) {
2,370,009✔
1730
    SClientHbReq *pReq = taosHashAcquire(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
2,369,282✔
1731
    if (pReq) {
2,369,282✔
1732
      tFreeClientHbReq(pReq);
1733
      code = taosHashRemove(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
2,368,310✔
1734
      if (TSDB_CODE_SUCCESS != code) {
2,368,310✔
1735
        tscError("hbDeregisterConn taosHashRemove error, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1736
      }
1737
      taosHashRelease(pAppHbMgr->activeInfo, pReq);
2,368,310✔
1738
      (void)atomic_sub_fetch_32(&pAppHbMgr->connKeyCnt, 1);
2,368,310✔
1739
    }
1740
  }
1741
  code = taosThreadMutexUnlock(&clientHbMgr.lock);
2,370,009✔
1742
  if (TSDB_CODE_SUCCESS != code) {
2,370,009✔
1743
    tscError("failed to unlock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1744
  }
1745
}
2,370,009✔
1746

1747
// set heart beat thread quit mode , if quicByKill 1 then kill thread else quit from inner
1748
void taos_set_hb_quit(int8_t quitByKill) { clientHbMgr.quitByKill = quitByKill; }
1,238,845✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc