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

taosdata / TDengine / #4913

06 Jan 2026 01:30AM UTC coverage: 64.884% (-0.004%) from 64.888%
#4913

push

travis-ci

web-flow
merge: from main to 3.0 branch #34167

180 of 319 new or added lines in 14 files covered. (56.43%)

571 existing lines in 128 files now uncovered.

195016 of 300563 relevant lines covered (64.88%)

117540852.85 hits per line

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

73.77
/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,
894,936✔
44
                                        SAppHbMgr *pAppHbMgr) {
45
  int32_t code = TSDB_CODE_SUCCESS;
894,936✔
46

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

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

61
    TSC_ERR_JRET(catalogUpdateUserAuthInfo(pCatalog, rsp));
928,813✔
62

63
  }
64

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

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

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

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

83
  SUserSessCfg cfg = {0, 0, 0, 0, 0};
1,166,364✔
84

85
  if (memcmp(pCfg, &cfg, sizeof(SUserSessCfg)) == 0) {
1,166,364✔
86
    return TSDB_CODE_SUCCESS;
×
87
  }
88
  tscInfo(
1,166,364✔
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,166,364✔
95
    code = sessMgtUpdataLimit((char *)user, SESSION_PER_USER, pCfg->sessPerUser);
1,166,364✔
96
    TAOS_CHECK_GOTO(code, &lino, _error);
1,166,364✔
97
  }
98

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

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

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

114
  if (pCfg->sessMaxCallVnodeNum != 0) {
1,166,364✔
115
    code = sessMgtUpdataLimit((char *)user, SESSION_MAX_CALL_VNODE_NUM, pCfg->sessMaxCallVnodeNum);
1,166,364✔
116
    TAOS_CHECK_GOTO(code, &lino, _error);
1,166,364✔
117
  }
118
_error:
1,166,364✔
119
  return code;
1,166,364✔
120
}
121
static int32_t hbUpdateUserAuthInfo(SAppHbMgr *pAppHbMgr, SUserAuthBatchRsp *batchRsp) {
894,936✔
122
  int32_t code = 0;
894,936✔
123
  int64_t clusterId = pAppHbMgr->pAppInstInfo->clusterId;
894,936✔
124
  for (int i = 0; i < TARRAY_SIZE(clientHbMgr.appHbMgrs); ++i) {
1,978,221✔
125
    SAppHbMgr *hbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
1,083,285✔
126
    if (!hbMgr || hbMgr->pAppInstInfo->clusterId != clusterId) {
1,083,285✔
127
      continue;
92✔
128
    }
129

130
    SClientHbReq    *pReq = NULL;
1,083,193✔
131
    SGetUserAuthRsp *pRsp = NULL;
1,083,193✔
132
    while ((pReq = taosHashIterate(hbMgr->activeInfo, pReq))) {
2,459,796✔
133
      STscObj *pTscObj = (STscObj *)acquireTscObj(pReq->connKey.tscRid);
1,401,412✔
134
      if (!pTscObj) {
1,401,412✔
135
        continue;
209,071✔
136
      }
137

138
      if (!pRsp) {
1,192,341✔
139
        for (int32_t j = 0; j < TARRAY_SIZE(batchRsp->pArray); ++j) {
1,050,819✔
140
          SGetUserAuthRsp *rsp = TARRAY_GET_ELEM(batchRsp->pArray, j);
1,026,010✔
141
          if (0 == strncmp(rsp->user, pTscObj->user, TSDB_USER_LEN)) {
1,026,010✔
142
            pRsp = rsp;
986,335✔
143
            break;
986,335✔
144
          }
145
        }
146
        if (!pRsp) {
1,011,144✔
147
          releaseTscObj(pReq->connKey.tscRid);
24,809✔
148
          taosHashCancelIterate(hbMgr->activeInfo, pReq);
24,809✔
149
          break;
24,809✔
150
        }
151
      }
152

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

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

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

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

190
      if (pTscObj->whiteListInfo.fp) {
1,166,364✔
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,166,364✔
205
        int64_t         oldVer = atomic_load_64(&whiteListInfo->ver);
1,166,364✔
206
        atomic_store_64(&whiteListInfo->ver, pRsp->whiteListVer);
1,166,364✔
207
        tscDebug("update whitelist version of user %s from %" PRId64 " to %" PRId64 ", conn:%" PRIi64, pRsp->user,
1,166,364✔
208
                 oldVer, atomic_load_64(&whiteListInfo->ver), pTscObj->id);
209
      }
210

211
      if (pTscObj->dateTimeWhiteListInfo.fp) {
1,166,364✔
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,166,364✔
224
        int64_t         oldVer = atomic_load_64(&whiteListInfo->ver);
1,166,364✔
225
        atomic_store_64(&whiteListInfo->ver, pRsp->timeWhiteListVer);
1,166,364✔
226
        tscDebug("update date time whitelist version of user %s from %" PRId64 " to %" PRId64 ", conn:%" PRIi64, pRsp->user,
1,166,364✔
227
                 oldVer, atomic_load_64(&whiteListInfo->ver), pTscObj->id);
228
      }
229
      releaseTscObj(pReq->connKey.tscRid);
1,166,364✔
230
    }
231
  }
232
  return 0;
894,936✔
233
}
234

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

242
  vgInfo->vgVersion = rsp->vgVersion;
1,270,951✔
243
  vgInfo->stateTs = rsp->stateTs;
1,270,951✔
244
  vgInfo->flags = rsp->flags;
1,270,951✔
245
  vgInfo->hashMethod = rsp->hashMethod;
1,270,951✔
246
  vgInfo->hashPrefix = rsp->hashPrefix;
1,270,951✔
247
  vgInfo->hashSuffix = rsp->hashSuffix;
1,270,951✔
248
  vgInfo->vgHash = taosHashInit(rsp->vgNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
1,270,951✔
249
  if (NULL == vgInfo->vgHash) {
1,270,951✔
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) {
4,573,885✔
256
    SVgroupInfo *pInfo = taosArrayGet(rsp->pVgroupInfos, j);
3,302,934✔
257
    if (taosHashPut(vgInfo->vgHash, &pInfo->vgId, sizeof(int32_t), pInfo, sizeof(SVgroupInfo)) != 0) {
3,302,934✔
258
      tscError("hash push failed, terrno:%d", terrno);
×
259
      code = terrno;
×
260
      goto _return;
×
261
    }
262
  }
263

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

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

274
static int32_t hbProcessDBInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
5,759,365✔
275
  int32_t code = 0;
5,759,365✔
276

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

284
  int32_t numOfBatchs = taosArrayGetSize(batchRsp.pArray);
5,759,365✔
285
  for (int32_t i = 0; i < numOfBatchs; ++i) {
7,091,117✔
286
    SDbHbRsp *rsp = taosArrayGet(batchRsp.pArray, i);
1,331,752✔
287
    if (NULL == rsp) {
1,331,752✔
288
      code = terrno;
×
289
      goto _return;
×
290
    }
291
    if (rsp->useDbRsp) {
1,331,752✔
292
      tscDebug("hb use db rsp, db:%s, vgVersion:%d, stateTs:%" PRId64 ", uid:%" PRIx64, rsp->useDbRsp->db,
1,076,420✔
293
               rsp->useDbRsp->vgVersion, rsp->useDbRsp->stateTs, rsp->useDbRsp->uid);
294

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

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

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

309
        if (IS_SYS_DBNAME(rsp->useDbRsp->db)) {
1,045,597✔
310
          code = hbGenerateVgInfoFromRsp(&vgInfo, rsp->useDbRsp);
225,354✔
311
          if (TSDB_CODE_SUCCESS != code) {
225,354✔
312
            goto _return;
×
313
          }
314

315
          TSC_ERR_JRET(catalogUpdateDBVgInfo(
225,354✔
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,331,752✔
323
      tscDebug("hb db cfg rsp, db:%s, cfgVersion:%d", rsp->cfgRsp->db, rsp->cfgRsp->cfgVersion);
43,625✔
324
      code = catalogUpdateDbCfg(pCatalog, rsp->cfgRsp->db, rsp->cfgRsp->dbId, rsp->cfgRsp);
43,625✔
325
      rsp->cfgRsp = NULL;
43,625✔
326
    }
327
    if (rsp->pTsmaRsp) {
1,331,752✔
328
      if (rsp->pTsmaRsp->pTsmas) {
643,125✔
329
        for (int32_t i = 0; i < rsp->pTsmaRsp->pTsmas->size; ++i) {
1,290✔
330
          STableTSMAInfo *pTsma = taosArrayGetP(rsp->pTsmaRsp->pTsmas, i);
645✔
331
          if (NULL == pTsma) {
645✔
332
            TSC_ERR_JRET(TSDB_CODE_OUT_OF_RANGE);
×
333
          }
334
          TSC_ERR_JRET(catalogAsyncUpdateTSMA(pCatalog, &pTsma, rsp->dbTsmaVersion));
645✔
335
        }
336
        taosArrayClear(rsp->pTsmaRsp->pTsmas);
645✔
337
      } else {
338
        TSC_ERR_JRET(catalogAsyncUpdateDbTsmaVersion(pCatalog, rsp->dbTsmaVersion, rsp->db, rsp->dbId));
642,480✔
339
      }
340
    }
341
  }
342

343
_return:
5,759,365✔
344

345
  tFreeSDbHbBatchRsp(&batchRsp);
5,759,365✔
346
  return code;
5,759,365✔
347
}
348

349
static int32_t hbProcessStbInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
3,913,067✔
350
  int32_t code = TSDB_CODE_SUCCESS;
3,913,067✔
351

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

358
  int32_t numOfMeta = taosArrayGetSize(hbRsp.pMetaRsp);
3,913,067✔
359
  for (int32_t i = 0; i < numOfMeta; ++i) {
3,957,785✔
360
    STableMetaRsp *rsp = taosArrayGet(hbRsp.pMetaRsp, i);
44,718✔
361
    if (NULL == rsp) {
44,718✔
362
      code = terrno;
×
363
      goto _return;
×
364
    }
365
    if (rsp->numOfColumns < 0) {
44,718✔
366
      tscDebug("hb to remove stb, db:%s, stb:%s", rsp->dbFName, rsp->stbName);
28,687✔
367
      TSC_ERR_JRET(catalogRemoveStbMeta(pCatalog, rsp->dbFName, rsp->dbId, rsp->stbName, rsp->suid));
28,687✔
368
    } else {
369
      tscDebug("hb to update stb, db:%s, stb:%s", rsp->dbFName, rsp->stbName);
16,031✔
370
      if (rsp->pSchemas[0].colId != PRIMARYKEY_TIMESTAMP_COL_ID) {
16,031✔
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));
16,031✔
377
    }
378
  }
379

380
  int32_t numOfIndex = taosArrayGetSize(hbRsp.pIndexRsp);
3,913,067✔
381
  for (int32_t i = 0; i < numOfIndex; ++i) {
3,913,067✔
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:
3,913,067✔
391
  taosArrayDestroy(hbRsp.pIndexRsp);
3,913,067✔
392
  hbRsp.pIndexRsp = NULL;
3,913,067✔
393

394
  tFreeSSTbHbRsp(&hbRsp);
3,913,067✔
395
  return code;
3,913,067✔
396
}
397

398
static int32_t hbProcessDynViewRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
1,097✔
399
  return catalogUpdateDynViewVer(pCatalog, (SDynViewVersion *)value);
1,097✔
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) {
1,097✔
412
  int32_t code = TSDB_CODE_SUCCESS;
1,097✔
413

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

421
  int32_t numOfMeta = taosArrayGetSize(hbRsp.pViewRsp);
1,097✔
422
  for (int32_t i = 0; i < numOfMeta; ++i) {
1,097✔
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:
1,097✔
441
  taosArrayDestroy(hbRsp.pViewRsp);
1,097✔
442
  return code;
1,097✔
443
}
444

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

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

454
  int32_t numOfTsma = taosArrayGetSize(hbRsp.pTsmas);
41,925✔
455
  for (int32_t i = 0; i < numOfTsma; ++i) {
41,925✔
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:
41,925✔
471
  taosArrayDestroy(hbRsp.pTsmas);
41,925✔
472
  return code;
41,925✔
473
}
474

475
static void hbProcessQueryRspKvs(int32_t kvNum, SArray *pKvs, struct SCatalog *pCatalog, SAppHbMgr *pAppHbMgr) {
8,877,020✔
476
  for (int32_t i = 0; i < kvNum; ++i) {
19,488,507✔
477
    SKv *kv = taosArrayGet(pKvs, i);
10,611,487✔
478
    if (NULL == kv) {
10,611,487✔
479
      tscError("invalid hb kv, idx:%d", i);
×
480
      continue;
×
481
    }
482
    switch (kv->key) {
10,611,487✔
483
      case HEARTBEAT_KEY_USER_AUTHINFO: {
894,936✔
484
        if (kv->valueLen <= 0 || NULL == kv->value) {
894,936✔
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)) {
894,936✔
489
          tscError("process user auth info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
490
          break;
×
491
        }
492
        break;
894,936✔
493
      }
494
      case HEARTBEAT_KEY_DBINFO: {
5,759,365✔
495
        if (kv->valueLen <= 0 || NULL == kv->value) {
5,759,365✔
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)) {
5,759,365✔
500
          tscError("process db info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
501
          break;
×
502
        }
503
        break;
5,759,365✔
504
      }
505
      case HEARTBEAT_KEY_STBINFO: {
3,913,067✔
506
        if (kv->valueLen <= 0 || NULL == kv->value) {
3,913,067✔
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)) {
3,913,067✔
511
          tscError("process stb info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
512
          break;
×
513
        }
514
        break;
3,913,067✔
515
      }
516
#ifdef TD_ENTERPRISE
517
      case HEARTBEAT_KEY_DYN_VIEW: {
1,097✔
518
        if (kv->valueLen <= 0 || NULL == kv->value) {
1,097✔
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)) {
1,097✔
523
          tscError("Process dyn view response failed, len:%d, value:%p", kv->valueLen, kv->value);
×
524
          break;
×
525
        }
526
        break;
1,097✔
527
      }
528
      case HEARTBEAT_KEY_VIEWINFO: {
1,097✔
529
        if (kv->valueLen <= 0 || NULL == kv->value) {
1,097✔
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)) {
1,097✔
534
          tscError("Process view info response failed, len:%d, value:%p", kv->valueLen, kv->value);
×
535
          break;
×
536
        }
537
        break;
1,097✔
538
      }
539
#endif
540
      case HEARTBEAT_KEY_TSMA: {
41,925✔
541
        if (kv->valueLen <= 0 || !kv->value) {
41,925✔
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)) {
41,925✔
545
          tscError("Process tsma info response failed, len:%d, value:%p", kv->valueLen, kv->value);
×
546
        }
547
        break;
41,925✔
548
      }
549
      default:
×
550
        tscError("invalid hb key type:%d", kv->key);
×
551
        break;
×
552
    }
553
  }
554
}
8,877,020✔
555

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

564
  if (pRsp->query) {
24,181,506✔
565
    STscObj *pTscObj = (STscObj *)acquireTscObj(pRsp->connKey.tscRid);
24,181,506✔
566
    if (NULL == pTscObj) {
24,181,506✔
567
      tscDebug("tscObj rid %" PRIx64 " not exist", pRsp->connKey.tscRid);
1,408✔
568
    } else {
569
      if (pRsp->query->totalDnodes > 1) {
24,180,098✔
570
        SEpSet  originEpset = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
6,708,440✔
571
        if (!isEpsetEqual(&originEpset, &pRsp->query->epSet)) {
6,708,440✔
572
          SEpSet *pOrig = &originEpset;
25,425✔
573
          SEp    *pOrigEp = &pOrig->eps[pOrig->inUse];
25,425✔
574
          SEp    *pNewEp = &pRsp->query->epSet.eps[pRsp->query->epSet.inUse];
25,425✔
575
          tscDebug("mnode epset updated from %d/%d=>%s:%d to %d/%d=>%s:%d in hb", pOrig->inUse, pOrig->numOfEps,
25,425✔
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);
25,425✔
580
        }
581
      }
582

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

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

600
      if (pRsp->query->killConnection) {
24,180,098✔
601
        taos_close_internal(pTscObj);
×
602
      }
603

604
      if (pRsp->query->pQnodeList) {
24,180,098✔
605
        if (TSDB_CODE_SUCCESS != updateQnodeList(pTscObj->pAppInfo, pRsp->query->pQnodeList)) {
20,196✔
606
          tscWarn("update qnode list failed");
×
607
        }
608
      }
609

610
      releaseTscObj(pRsp->connKey.tscRid);
24,180,098✔
611
    }
612
  }
613

614
  int32_t kvNum = pRsp->info ? taosArrayGetSize(pRsp->info) : 0;
24,181,506✔
615

616
  tscDebug("hb got %d rsp kv", kvNum);
24,181,506✔
617

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

628
  taosHashRelease(pAppHbMgr->activeInfo, pReq);
24,181,506✔
629

630
  return TSDB_CODE_SUCCESS;
24,181,506✔
631
}
632

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

638
  static int32_t    emptyRspNum = 0;
639
  int32_t           idx = *(int32_t *)param;
20,618,961✔
640
  SClientHbBatchRsp pRsp = {0};
20,619,221✔
641
  if (TSDB_CODE_SUCCESS == code) {
20,619,119✔
642
    code = tDeserializeSClientHbBatchRsp(pMsg->pData, pMsg->len, &pRsp);
20,444,045✔
643
    if (TSDB_CODE_SUCCESS != code) {
20,441,701✔
644
      tscError("deserialize hb rsp failed");
65✔
645
    }
646
    int32_t now = taosGetTimestampSec();
20,441,701✔
647
    int32_t delta = abs(now - pRsp.svrTimestamp);
20,439,855✔
648
    if (delta > tsTimestampDeltaLimit) {
20,439,855✔
649
      code = TSDB_CODE_TIME_UNSYNCED;
65✔
650
      tscError("time diff:%ds is too big", delta);
65✔
651
    }
652
  }
653

654
  int32_t rspNum = taosArrayGetSize(pRsp.rsps);
20,614,929✔
655

656
  (void)taosThreadMutexLock(&clientHbMgr.lock);
20,615,598✔
657

658
  SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, idx);
20,619,486✔
659
  if (pAppHbMgr == NULL) {
20,619,486✔
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;
20,619,486✔
669

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

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

690
  if (rspNum) {
20,444,082✔
691
    tscDebug("hb got %d rsp, %d empty rsp received before", rspNum,
19,965,633✔
692
             atomic_val_compare_exchange_32(&emptyRspNum, emptyRspNum, 0));
693
  } else {
694
    (void)atomic_add_fetch_32(&emptyRspNum, 1);
478,449✔
695
  }
696

697
  for (int32_t i = 0; i < rspNum; ++i) {
44,634,796✔
698
    SClientHbRsp *rsp = taosArrayGet(pRsp.rsps, i);
24,190,714✔
699
    code = (*clientHbMgr.rspHandle[rsp->connKey.connType])(pAppHbMgr, rsp);
24,190,714✔
700
    if (code) {
24,190,714✔
701
      break;
×
702
    }
703
  }
704

705
  (void)taosThreadMutexUnlock(&clientHbMgr.lock);
20,444,082✔
706

707
  tFreeClientHbBatchRsp(&pRsp);
708

709
_return:
20,444,082✔
710
  taosMemoryFree(pMsg->pData);
20,444,082✔
711
  taosMemoryFree(pMsg->pEpSet);
20,444,082✔
712
  return code;
20,444,082✔
713
}
714

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

720
  void *pIter = taosHashIterate(pObj->pRequests, NULL);
21,599,922✔
721
  while (pIter != NULL) {
43,758,353✔
722
    int64_t     *rid = pIter;
22,158,431✔
723
    SRequestObj *pRequest = acquireRequest(*rid);
22,158,431✔
724
    if (NULL == pRequest) {
22,158,431✔
725
      pIter = taosHashIterate(pObj->pRequests, pIter);
15,402✔
726
      continue;
15,402✔
727
    }
728

729
    if (pRequest->killed || 0 == pRequest->body.queryJob) {
22,143,029✔
730
      (void)releaseRequest(*rid);
8,595,226✔
731
      pIter = taosHashIterate(pObj->pRequests, pIter);
8,595,226✔
732
      continue;
8,595,226✔
733
    }
734

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

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

757
      code = schedulerGetTasksStatus(pRequest->body.queryJob, desc.subDesc);
13,547,803✔
758
      if (code) {
13,547,803✔
759
        taosArrayDestroy(desc.subDesc);
3,948,270✔
760
        desc.subDesc = NULL;
3,948,270✔
761
        code = TSDB_CODE_SUCCESS;
3,948,270✔
762
      }
763
      desc.subPlanNum = taosArrayGetSize(desc.subDesc);
13,547,803✔
764
    } else {
765
      desc.subDesc = NULL;
×
766
    }
767

768
    (void)releaseRequest(*rid);
13,547,803✔
769
    if (NULL == taosArrayPush(hbBasic->queryDesc, &desc)) {
27,095,606✔
770
      taosArrayDestroy(desc.subDesc);
×
771
      return terrno;
×
772
    }
773

774
    pIter = taosHashIterate(pObj->pRequests, pIter);
13,547,803✔
775
  }
776

777
  return code;
21,599,922✔
778
}
779

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

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

794
  hbBasic->connId = pTscObj->connId;
24,376,134✔
795

796
  int32_t numOfQueries = pTscObj->pRequests ? taosHashGetSize(pTscObj->pRequests) : 0;
24,376,134✔
797
  if (numOfQueries <= 0) {
24,376,134✔
798
    req->query = hbBasic;
2,776,212✔
799
    releaseTscObj(connKey->tscRid);
2,776,212✔
800
    tscDebug("no queries on connection");
2,776,212✔
801
    return TSDB_CODE_SUCCESS;
2,776,212✔
802
  }
803

804
  hbBasic->queryDesc = taosArrayInit(numOfQueries, sizeof(SQueryDesc));
21,599,922✔
805
  if (NULL == hbBasic->queryDesc) {
21,599,922✔
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);
21,599,922✔
813
  if (code) {
21,599,922✔
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;
21,599,922✔
823
  releaseTscObj(connKey->tscRid);
21,599,922✔
824

825
  return TSDB_CODE_SUCCESS;
21,599,922✔
826
}
827

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

835
  int32_t code = 0;
797,208✔
836

837
  SKv  kv = {.key = HEARTBEAT_KEY_USER_AUTHINFO};
797,208✔
838
  SKv *pKv = NULL;
797,208✔
839
  if ((pKv = taosHashGet(req->info, &kv.key, sizeof(kv.key)))) {
797,208✔
840
    int32_t           userNum = pKv->valueLen / sizeof(SUserAuthVersion);
441,565✔
841
    SUserAuthVersion *userAuths = (SUserAuthVersion *)pKv->value;
441,565✔
842
    for (int32_t i = 0; i < userNum; ++i) {
455,513✔
843
      SUserAuthVersion *pUserAuth = userAuths + i;
455,513✔
844
      // both key and user exist, update version
845
      if (strncmp(pUserAuth->user, pTscObj->user, TSDB_USER_LEN) == 0) {
455,513✔
846
        pUserAuth->version = htonl(-1);  // force get userAuthInfo
441,565✔
847
        goto _return;
441,565✔
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));
355,643✔
866
  if (!user) {
355,643✔
867
    code = terrno;
×
868
    goto _return;
×
869
  }
870
  tstrncpy(user->user, pTscObj->user, TSDB_USER_LEN);
355,643✔
871
  user->version = htonl(-1);  // force get userAuthInfo
355,643✔
872
  kv.valueLen = sizeof(SUserAuthVersion);
355,643✔
873
  kv.value = user;
355,643✔
874

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

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

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

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

898
  return code;
797,208✔
899
}
900

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

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

911
  if (userNum <= 0) {
18,542,157✔
912
    taosMemoryFree(users);
289,396✔
913
    return TSDB_CODE_SUCCESS;
289,396✔
914
  }
915

916
  for (int32_t i = 0; i < userNum; ++i) {
36,558,352✔
917
    SUserAuthVersion *user = &users[i];
18,305,591✔
918
    user->version = htonl(user->version);
18,305,591✔
919
  }
920

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

927
  tscDebug("hb got %d expired users, valueLen:%d", userNum, kv.valueLen);
18,252,761✔
928

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

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

943
  return TSDB_CODE_SUCCESS;
18,252,761✔
944
}
945

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

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

956
  if (dbNum <= 0) {
20,149,538✔
957
    taosMemoryFree(dbs);
14,361,295✔
958
    return TSDB_CODE_SUCCESS;
14,361,295✔
959
  }
960

961
  for (int32_t i = 0; i < dbNum; ++i) {
12,794,105✔
962
    SDbCacheInfo *db = &dbs[i];
7,005,862✔
963
    tscDebug("the %dth expired db:%s, dbId:%" PRId64
7,005,862✔
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);
7,005,862✔
968
    db->vgVersion = htonl(db->vgVersion);
7,005,862✔
969
    db->cfgVersion = htonl(db->cfgVersion);
7,005,862✔
970
    db->numOfTable = htonl(db->numOfTable);
7,005,862✔
971
    db->stateTs = htobe64(db->stateTs);
7,005,862✔
972
    db->tsmaVersion = htonl(db->tsmaVersion);
7,005,862✔
973
  }
974

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

981
  tscDebug("hb got %d expired db, valueLen:%d", dbNum, kv.valueLen);
5,788,243✔
982

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

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

997
  return TSDB_CODE_SUCCESS;
5,788,243✔
998
}
999

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

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

1010
  if (stbNum <= 0) {
20,149,538✔
1011
    taosMemoryFree(stbs);
16,218,046✔
1012
    return TSDB_CODE_SUCCESS;
16,218,046✔
1013
  }
1014

1015
  for (int32_t i = 0; i < stbNum; ++i) {
9,535,479✔
1016
    SSTableVersion *stb = &stbs[i];
5,603,987✔
1017
    stb->suid = htobe64(stb->suid);
5,603,987✔
1018
    stb->sversion = htonl(stb->sversion);
5,603,987✔
1019
    stb->tversion = htonl(stb->tversion);
5,603,987✔
1020
    stb->smaVer = htonl(stb->smaVer);
5,603,987✔
1021
  }
1022

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

1029
  tscDebug("hb got %d expired stb, valueLen:%d", stbNum, kv.valueLen);
3,931,492✔
1030

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

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

1045
  return TSDB_CODE_SUCCESS;
3,931,492✔
1046
}
1047

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

1054
  TSC_ERR_JRET(catalogGetExpiredViews(pCatalog, &views, &viewNum, &pDynViewVer));
20,149,538✔
1055

1056
  if (viewNum <= 0) {
20,149,538✔
1057
    taosMemoryFree(views);
20,146,229✔
1058
    taosMemoryFree(pDynViewVer);
20,146,229✔
1059
    return TSDB_CODE_SUCCESS;
20,146,229✔
1060
  }
1061

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

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

1071
  if (NULL == req->info) {
3,309✔
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 = {
3,309✔
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)));
3,309✔
1085

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

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

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

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

1109
  if (tsmaNum <= 0) {
20,149,538✔
1110
    taosMemoryFree(tsmas);
20,107,613✔
1111
    return TSDB_CODE_SUCCESS;
20,107,613✔
1112
  }
1113

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

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

1123
  if (!pReq->info) {
41,925✔
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};
41,925✔
1132
  code = taosHashPut(pReq->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
41,925✔
1133
  if (TSDB_CODE_SUCCESS != code) {
41,925✔
1134
    taosMemoryFree(tsmas);
×
1135
    return code;
×
1136
  }
1137
  return TSDB_CODE_SUCCESS;
41,925✔
1138
}
1139

1140
int32_t hbGetAppInfo(int64_t clusterId, SClientHbReq *req) {
24,376,134✔
1141
  SAppHbReq *pApp = taosHashGet(clientHbMgr.appSummary, &clusterId, sizeof(clusterId));
24,376,134✔
1142
  if (NULL != pApp) {
24,376,134✔
1143
    (void)memcpy(&req->app, pApp, sizeof(*pApp));
24,376,134✔
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;
24,376,134✔
1152
}
1153

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

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

1165
  if (hbParam->reqCnt == 0) {
24,376,134✔
1166
    code = catalogGetHandle(hbParam->clusterId, &pCatalog);
20,149,878✔
1167
    if (code != TSDB_CODE_SUCCESS) {
20,149,878✔
1168
      tscWarn("catalogGetHandle failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1169
      return code;
×
1170
    }
1171

1172
    code = hbGetAppInfo(hbParam->clusterId, req);
20,149,878✔
1173
    if (TSDB_CODE_SUCCESS != code) {
20,149,878✔
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))) {
20,149,878✔
1179
      code = hbGetExpiredUserInfo(connKey, pCatalog, req);
18,542,157✔
1180
      if (TSDB_CODE_SUCCESS != code) {
18,542,157✔
1181
        tscWarn("hbGetExpiredUserInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1182
        return code;
×
1183
      }
1184
      if (clientHbMgr.appHbHash) {
18,542,157✔
1185
        code = taosHashPut(clientHbMgr.appHbHash, &hbParam->clusterId, sizeof(uint64_t), NULL, 0);
2,671,166✔
1186
        if (TSDB_CODE_SUCCESS != code) {
2,671,166✔
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)) {
20,149,878✔
1196
      code = hbGetUserAuthInfo(connKey, hbParam, req);
797,548✔
1197
      if (TSDB_CODE_SUCCESS != code) {
797,548✔
1198
        tscWarn("hbGetUserAuthInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
340✔
1199
        return code;
340✔
1200
      }
1201
      atomic_store_8(&hbParam->pAppHbMgr->connHbFlag, 1);
797,208✔
1202
    }
1203

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

1210
    code = hbGetExpiredStbInfo(connKey, pCatalog, req);
20,149,538✔
1211
    if (TSDB_CODE_SUCCESS != code) {
20,149,538✔
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);
20,149,538✔
1218
    if (TSDB_CODE_SUCCESS != code) {
20,149,538✔
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);
20,149,538✔
1224
    if (TSDB_CODE_SUCCESS != code) {
20,149,538✔
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);
4,226,256✔
1230
    if (TSDB_CODE_SUCCESS != code) {
4,226,256✔
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
24,375,794✔
1237

1238
  return TSDB_CODE_SUCCESS;
24,375,794✔
1239
}
1240

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

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

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

1262
  int64_t  maxIpWhiteVer = 0;
20,628,806✔
1263
  void    *pIter = NULL;
20,628,806✔
1264
  SHbParam param = {0};
20,628,806✔
1265
  while ((pIter = taosHashIterate(pAppHbMgr->activeInfo, pIter))) {
46,302,209✔
1266
    SClientHbReq *pOneReq = pIter;
25,673,403✔
1267
    SClientHbKey *connKey = &pOneReq->connKey;
25,673,403✔
1268
    STscObj      *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
25,673,403✔
1269

1270
    if (!pTscObj || atomic_load_8(&pTscObj->dropped) == 1) {
25,673,403✔
1271
      if (pTscObj) releaseTscObj(connKey->tscRid);
1,297,269✔
1272
      continue;
1,297,269✔
1273
    }
1274

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

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

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

1309
    int64_t ver = atomic_load_64(&pTscObj->whiteListInfo.ver);
24,376,134✔
1310
    maxIpWhiteVer = TMAX(maxIpWhiteVer, ver);
24,376,134✔
1311
    releaseTscObj(connKey->tscRid);
24,376,134✔
1312
  }
1313
  (*pBatchReq)->ipWhiteListVer = maxIpWhiteVer;
20,628,806✔
1314

1315
  return TSDB_CODE_SUCCESS;
20,628,806✔
1316
}
1317

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

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

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

1342
  taosHashClear(clientHbMgr.appSummary);
20,072,032✔
1343

1344
  for (int32_t i = 0; i < sz; ++i) {
42,986,715✔
1345
    SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
22,914,683✔
1346
    if (pAppHbMgr == NULL) continue;
22,914,683✔
1347

1348
    int64_t    clusterId = pAppHbMgr->pAppInstInfo->clusterId;
22,914,683✔
1349
    SAppHbReq *pApp = taosHashGet(clientHbMgr.appSummary, &clusterId, sizeof(clusterId));
22,914,683✔
1350
    if (NULL == pApp) {
22,914,683✔
1351
      (void)memcpy(&req.summary, &pAppHbMgr->pAppInstInfo->summary, sizeof(req.summary));
20,073,350✔
1352
      req.startTime = pAppHbMgr->startTime;
20,073,350✔
1353
      TSC_ERR_RET(taosHashPut(clientHbMgr.appSummary, &clusterId, sizeof(clusterId), &req, sizeof(req)));
20,073,350✔
1354
    } else {
1355
      if (pAppHbMgr->startTime < pApp->startTime) {
2,841,333✔
1356
        pApp->startTime = pAppHbMgr->startTime;
×
1357
      }
1358

1359
      hbMergeSummary(&pApp->summary, &pAppHbMgr->pAppInstInfo->summary);
2,841,333✔
1360
    }
1361
  }
1362

1363
  return TSDB_CODE_SUCCESS;
20,072,032✔
1364
}
1365

1366
static void *hbThreadFunc(void *param) {
1,500,925✔
1367
  setThreadName("hb");
1,500,925✔
1368
#ifdef WINDOWS
1369
  if (taosCheckCurrentInDll()) {
1370
    atexit(hbThreadFuncUnexpectedStopped);
1371
  }
1372
#endif
1373
  while (1) {
19,004,891✔
1374
    if (1 == clientHbMgr.threadStop) {
20,505,816✔
1375
      break;
426,802✔
1376
    }
1377

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

1383
    int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
20,079,014✔
1384
    if (sz > 0) {
20,079,014✔
1385
      if (TSDB_CODE_SUCCESS != hbGatherAppInfo()) {
20,072,032✔
1386
        tscError("hbGatherAppInfo failed");
×
1387
        return NULL;
×
1388
      }
1389
      if (sz > 1 && !clientHbMgr.appHbHash) {
20,072,032✔
1390
        clientHbMgr.appHbHash = taosHashInit(0, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), true, HASH_NO_LOCK);
41,312✔
1391
        if (NULL == clientHbMgr.appHbHash) {
41,312✔
1392
          tscError("taosHashInit failed");
×
1393
          return NULL;
×
1394
        }
1395
      }
1396
      taosHashClear(clientHbMgr.appHbHash);
20,072,032✔
1397
    }
1398

1399
    for (int i = 0; i < sz; i++) {
42,993,697✔
1400
      SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
22,914,683✔
1401
      if (pAppHbMgr == NULL) {
22,914,683✔
1402
        continue;
18,402✔
1403
      }
1404

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

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

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

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

1468
    if (TSDB_CODE_SUCCESS != taosThreadMutexUnlock(&clientHbMgr.lock)) {
20,079,014✔
1469
      tscError("taosThreadMutexLock failed");
×
1470
      return NULL;
×
1471
    }
1472
    taosMsleep(HEARTBEAT_INTERVAL);
20,079,014✔
1473
  }
1474
  taosHashCleanup(clientHbMgr.appHbHash);
426,802✔
1475
  return NULL;
426,802✔
1476
}
1477

1478
static int32_t hbCreateThread() {
1,500,925✔
1479
  int32_t      code = TSDB_CODE_SUCCESS;
1,500,925✔
1480
  TdThreadAttr thAttr;
1,492,098✔
1481
  TSC_ERR_JRET(taosThreadAttrInit(&thAttr));
1,500,925✔
1482
  TSC_ERR_JRET(taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE));
1,500,925✔
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,500,925✔
1488
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
1489
    TSC_ERR_RET(terrno);
×
1490
  }
1491
  (void)taosThreadAttrDestroy(&thAttr);
1,500,925✔
1492
_return:
1,500,925✔
1493

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

1499
  return code;
1,500,925✔
1500
}
1501

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

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

1525
  tscDebug("hb thread stopped");
1,500,925✔
1526
}
1527

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

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

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

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

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

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

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

1573
void hbFreeAppHbMgr(SAppHbMgr *pTarget) {
1,553,789✔
1574
  void *pIter = taosHashIterate(pTarget->activeInfo, NULL);
1,553,789✔
1575
  while (pIter != NULL) {
1,717,743✔
1576
    SClientHbReq *pOneReq = pIter;
163,954✔
1577
    tFreeClientHbReq(pOneReq);
1578
    pIter = taosHashIterate(pTarget->activeInfo, pIter);
163,954✔
1579
  }
1580
  taosHashCleanup(pTarget->activeInfo);
1,553,789✔
1581
  pTarget->activeInfo = NULL;
1,553,789✔
1582

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

1587
void hbRemoveAppHbMrg(SAppHbMgr **pAppHbMgr) {
1,553,789✔
1588
  int32_t code = TSDB_CODE_SUCCESS;
1,553,789✔
1589
  code = taosThreadMutexLock(&clientHbMgr.lock);
1,553,789✔
1590
  if (TSDB_CODE_SUCCESS != code) {
1,553,789✔
1591
    tscError("failed to lock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1592
  }
1593
  int32_t mgrSize = taosArrayGetSize(clientHbMgr.appHbMgrs);
1,553,789✔
1594
  for (int32_t i = 0; i < mgrSize; ++i) {
1,553,789✔
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,553,789✔
1604
  if (TSDB_CODE_SUCCESS != code) {
1,553,789✔
1605
    tscError("failed to unlock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1606
  }
1607
}
1,553,789✔
1608

1609
void appHbMgrCleanup(void) {
1,500,925✔
1610
  int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
1,500,925✔
1611
  for (int i = 0; i < sz; i++) {
3,054,714✔
1612
    SAppHbMgr *pTarget = taosArrayGetP(clientHbMgr.appHbMgrs, i);
1,553,789✔
1613
    if (pTarget == NULL) continue;
1,553,789✔
1614
    hbFreeAppHbMgr(pTarget);
1,553,789✔
1615
  }
1616
}
1,500,925✔
1617

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

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

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

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

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

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

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

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

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

1665
  return 0;
1,500,925✔
1666
}
1667

1668
void hbMgrCleanUp() {
1,501,359✔
1669
  hbStopThread();
1,501,359✔
1670

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

1675
  int32_t code = taosThreadMutexLock(&clientHbMgr.lock);
1,500,925✔
1676
  if (TSDB_CODE_SUCCESS != code) {
1,500,925✔
1677
    tscError("failed to lock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1678
  }
1679
  appHbMgrCleanup();
1,500,925✔
1680
  taosArrayDestroy(clientHbMgr.appHbMgrs);
1,500,925✔
1681
  clientHbMgr.appHbMgrs = NULL;
1,500,925✔
1682
  code = taosThreadMutexUnlock(&clientHbMgr.lock);
1,500,925✔
1683
  if (TSDB_CODE_SUCCESS != code) {
1,500,925✔
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,346,460✔
1689
  // init hash in activeinfo
1690
  void *data = taosHashGet(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
2,346,460✔
1691
  if (data != NULL) {
2,346,460✔
1692
    return 0;
×
1693
  }
1694
  SClientHbReq hbReq = {0};
2,346,460✔
1695
  hbReq.connKey = connKey;
2,346,460✔
1696
  hbReq.clusterId = clusterId;
2,346,460✔
1697
  tstrncpy(hbReq.user, user, sizeof(hbReq.user));
2,346,460✔
1698
  tstrncpy(hbReq.tokenName, tokenName, sizeof(hbReq.tokenName));
2,346,460✔
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,346,460✔
1702

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

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

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

1723
void hbDeregisterConn(STscObj *pTscObj, SClientHbKey connKey) {
2,193,996✔
1724
  int32_t code = taosThreadMutexLock(&clientHbMgr.lock);
2,193,996✔
1725
  if (TSDB_CODE_SUCCESS != code) {
2,193,996✔
1726
    tscError("failed to lock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1727
  }
1728
  SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, pTscObj->appHbMgrIdx);
2,193,996✔
1729
  if (pAppHbMgr) {
2,193,996✔
1730
    SClientHbReq *pReq = taosHashAcquire(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
2,192,636✔
1731
    if (pReq) {
2,192,636✔
1732
      tFreeClientHbReq(pReq);
1733
      code = taosHashRemove(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
2,182,506✔
1734
      if (TSDB_CODE_SUCCESS != code) {
2,182,506✔
1735
        tscError("hbDeregisterConn taosHashRemove error, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1736
      }
1737
      taosHashRelease(pAppHbMgr->activeInfo, pReq);
2,182,506✔
1738
      (void)atomic_sub_fetch_32(&pAppHbMgr->connKeyCnt, 1);
2,182,506✔
1739
    }
1740
  }
1741
  code = taosThreadMutexUnlock(&clientHbMgr.lock);
2,193,996✔
1742
  if (TSDB_CODE_SUCCESS != code) {
2,193,996✔
1743
    tscError("failed to unlock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1744
  }
1745
}
2,193,996✔
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,121,872✔
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