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

taosdata / TDengine / #4899

27 Dec 2025 07:32AM UTC coverage: 65.534% (+0.5%) from 65.061%
#4899

push

travis-ci

web-flow
test: remove semaphore test (#34071)

189567 of 289265 relevant lines covered (65.53%)

114701701.06 hits per line

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

72.47
/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 hbUpdateUserAuthInfo(SAppHbMgr *pAppHbMgr, SUserAuthBatchRsp *batchRsp);
41
static int32_t hbProcessUserAuthInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog,
631,218✔
42
                                        SAppHbMgr *pAppHbMgr) {
43
  int32_t code = TSDB_CODE_SUCCESS;
631,218✔
44

45
  SUserAuthBatchRsp batchRsp = {0};
631,218✔
46
  if (tDeserializeSUserAuthBatchRsp(value, valueLen, &batchRsp) != 0) {
631,218✔
47
    return TSDB_CODE_INVALID_MSG;
×
48
  }
49

50
  int32_t numOfBatchs = taosArrayGetSize(batchRsp.pArray);
631,218✔
51
  for (int32_t i = 0; i < numOfBatchs; ++i) {
1,297,022✔
52
    SGetUserAuthRsp *rsp = taosArrayGet(batchRsp.pArray, i);
665,804✔
53
    if (NULL == rsp) {
665,804✔
54
      code = terrno;
×
55
      goto _return;
×
56
    }
57
    tscDebug("hb to update user auth, user:%s, version:%d", rsp->user, rsp->version);
665,804✔
58

59
    TSC_ERR_JRET(catalogUpdateUserAuthInfo(pCatalog, rsp));
665,804✔
60
  }
61

62
  if (numOfBatchs > 0) {
631,218✔
63
    TSC_ERR_JRET(hbUpdateUserAuthInfo(pAppHbMgr, &batchRsp));
631,218✔
64
  }
65

66
  (void)atomic_val_compare_exchange_8(&pAppHbMgr->connHbFlag, 1, 2);
631,218✔
67

68
_return:
631,218✔
69
  taosArrayDestroy(batchRsp.pArray);
631,218✔
70
  return code;
631,218✔
71
}
72

73
static int32_t updateUserSessMetric(const char *user, SUserSessCfg *pCfg) {
881,640✔
74
  int32_t code = 0;
881,640✔
75
  int32_t lino = 0;
881,640✔
76
  if (user == NULL || pCfg == NULL) {
881,640✔
77
    return code;
×
78
  }
79

80
  SUserSessCfg cfg = {0, 0, 0, 0, 0};
881,640✔
81

82
  if (memcmp(pCfg, &cfg, sizeof(SUserSessCfg)) == 0) {
881,640✔
83
    return TSDB_CODE_SUCCESS;
881,640✔
84
  }
85
  tscInfo(
×
86
      "update session metric for user:%s, sessPerUser:%d, sessConnTime:%d, sessConnIdleTime:%d, sessMaxConcurrency:%d, "
87
      "sessMaxCallVnodeNum:%d",
88
      user, pCfg->sessPerUser, pCfg->sessConnTime, pCfg->sessConnIdleTime, pCfg->sessMaxConcurrency,
89
      pCfg->sessMaxCallVnodeNum);
90

91
  if (pCfg->sessPerUser != 0) {
×
92
    code = sessMgtUpdataLimit((char *)user, SESSION_PER_USER, pCfg->sessPerUser);
×
93
    TAOS_CHECK_GOTO(code, &lino, _error);
×
94
  }
95

96
  if (pCfg->sessConnTime != 0) {
×
97
    code = sessMgtUpdataLimit((char *)user, SESSION_CONN_TIME, pCfg->sessConnTime);
×
98
    TAOS_CHECK_GOTO(code, &lino, _error);
×
99
  }
100

101
  if (pCfg->sessConnIdleTime != 0) {
×
102
    code = sessMgtUpdataLimit((char *)user, SESSION_CONN_IDLE_TIME, pCfg->sessConnIdleTime);
×
103
    TAOS_CHECK_GOTO(code, &lino, _error);
×
104
  }
105

106
  if (pCfg->sessMaxConcurrency != 0) {
×
107
    code = sessMgtUpdataLimit((char *)user, SESSION_MAX_CONCURRENCY, pCfg->sessMaxConcurrency);
×
108
    TAOS_CHECK_GOTO(code, &lino, _error);
×
109
  }
110

111
  if (pCfg->sessMaxCallVnodeNum != 0) {
×
112
    code = sessMgtUpdataLimit((char *)user, SESSION_MAX_CALL_VNODE_NUM, pCfg->sessMaxCallVnodeNum);
×
113
    TAOS_CHECK_GOTO(code, &lino, _error);
×
114
  }
115
_error:
×
116
  return code;
×
117
}
118
static int32_t hbUpdateUserAuthInfo(SAppHbMgr *pAppHbMgr, SUserAuthBatchRsp *batchRsp) {
631,218✔
119
  int32_t code = 0;
631,218✔
120
  int64_t clusterId = pAppHbMgr->pAppInstInfo->clusterId;
631,218✔
121
  for (int i = 0; i < TARRAY_SIZE(clientHbMgr.appHbMgrs); ++i) {
1,509,848✔
122
    SAppHbMgr *hbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
878,630✔
123
    if (!hbMgr || hbMgr->pAppInstInfo->clusterId != clusterId) {
878,630✔
124
      continue;
5,896✔
125
    }
126

127
    SClientHbReq    *pReq = NULL;
872,734✔
128
    SGetUserAuthRsp *pRsp = NULL;
872,734✔
129
    while ((pReq = taosHashIterate(hbMgr->activeInfo, pReq))) {
1,951,641✔
130
      STscObj *pTscObj = (STscObj *)acquireTscObj(pReq->connKey.tscRid);
1,127,839✔
131
      if (!pTscObj) {
1,127,839✔
132
        continue;
195,369✔
133
      }
134

135
      if (!pRsp) {
932,470✔
136
        for (int32_t j = 0; j < TARRAY_SIZE(batchRsp->pArray); ++j) {
829,222✔
137
          SGetUserAuthRsp *rsp = TARRAY_GET_ELEM(batchRsp->pArray, j);
780,290✔
138
          if (0 == strncmp(rsp->user, pTscObj->user, TSDB_USER_LEN)) {
780,290✔
139
            pRsp = rsp;
711,124✔
140
            break;
711,124✔
141
          }
142
        }
143
        if (!pRsp) {
760,056✔
144
          releaseTscObj(pReq->connKey.tscRid);
48,932✔
145
          taosHashCancelIterate(hbMgr->activeInfo, pReq);
48,932✔
146
          break;
48,932✔
147
        }
148
      }
149

150
      if (pRsp->dropped == 1) {
883,538✔
151
        if (atomic_val_compare_exchange_8(&pTscObj->dropped, 0, 1) == 0) {
1,898✔
152
          if (pTscObj->userDroppedInfo.fp) {
1,443✔
153
            SPassInfo *dropInfo = &pTscObj->userDroppedInfo;
900✔
154
            if (dropInfo->fp) {
900✔
155
              (*dropInfo->fp)(dropInfo->param, NULL, TAOS_NOTIFY_USER_DROPPED);
900✔
156
            }
157
          }
158
        }
159
        code = sessMgtRemoveUser(pTscObj->user);
1,898✔
160
        if (code != 0) {
1,898✔
161
          tscError("failed to remove user session metric, user:%s, code:%d", pTscObj->user, code);  
×
162
        }
163
        releaseTscObj(pReq->connKey.tscRid);
1,898✔
164
        continue;
1,898✔
165
      }
166

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

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

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

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

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

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

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

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

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

271
  *pInfo = vgInfo;
1,250,567✔
272
  return code;
1,250,567✔
273
}
274

275
static int32_t hbProcessDBInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
6,080,679✔
276
  int32_t code = 0;
6,080,679✔
277

278
  SDbHbBatchRsp batchRsp = {0};
6,080,679✔
279
  if (tDeserializeSDbHbBatchRsp(value, valueLen, &batchRsp) != 0) {
6,080,679✔
280
    terrno = TSDB_CODE_INVALID_MSG;
×
281
    code = terrno;
×
282
    goto _return;
×
283
  }
284

285
  int32_t numOfBatchs = taosArrayGetSize(batchRsp.pArray);
6,080,679✔
286
  for (int32_t i = 0; i < numOfBatchs; ++i) {
7,417,919✔
287
    SDbHbRsp *rsp = taosArrayGet(batchRsp.pArray, i);
1,337,240✔
288
    if (NULL == rsp) {
1,337,240✔
289
      code = terrno;
×
290
      goto _return;
×
291
    }
292
    if (rsp->useDbRsp) {
1,337,240✔
293
      tscDebug("hb use db rsp, db:%s, vgVersion:%d, stateTs:%" PRId64 ", uid:%" PRIx64, rsp->useDbRsp->db,
1,060,485✔
294
               rsp->useDbRsp->vgVersion, rsp->useDbRsp->stateTs, rsp->useDbRsp->uid);
295

296
      if (rsp->useDbRsp->vgVersion < 0) {
1,060,485✔
297
        tscDebug("hb to remove db, db:%s", rsp->useDbRsp->db);
38,632✔
298
        code = catalogRemoveDB(pCatalog, rsp->useDbRsp->db, rsp->useDbRsp->uid);
38,632✔
299
      } else {
300
        SDBVgInfo *vgInfo = NULL;
1,021,853✔
301
        code = hbGenerateVgInfoFromRsp(&vgInfo, rsp->useDbRsp);
1,021,853✔
302
        if (TSDB_CODE_SUCCESS != code) {
1,021,853✔
303
          goto _return;
×
304
        }
305

306
        tscDebug("hb to update db vgInfo, db:%s", rsp->useDbRsp->db);
1,021,853✔
307

308
        TSC_ERR_JRET(catalogUpdateDBVgInfo(pCatalog, rsp->useDbRsp->db, rsp->useDbRsp->uid, vgInfo));
1,021,853✔
309

310
        if (IS_SYS_DBNAME(rsp->useDbRsp->db)) {
1,021,853✔
311
          code = hbGenerateVgInfoFromRsp(&vgInfo, rsp->useDbRsp);
228,714✔
312
          if (TSDB_CODE_SUCCESS != code) {
228,714✔
313
            goto _return;
×
314
          }
315

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

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

344
_return:
6,080,679✔
345

346
  tFreeSDbHbBatchRsp(&batchRsp);
6,080,679✔
347
  return code;
6,080,679✔
348
}
349

350
static int32_t hbProcessStbInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
4,262,266✔
351
  int32_t code = TSDB_CODE_SUCCESS;
4,262,266✔
352

353
  SSTbHbRsp hbRsp = {0};
4,262,266✔
354
  if (tDeserializeSSTbHbRsp(value, valueLen, &hbRsp) != 0) {
4,262,266✔
355
    terrno = TSDB_CODE_INVALID_MSG;
×
356
    return -1;
×
357
  }
358

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

377
      TSC_ERR_JRET(catalogAsyncUpdateTableMeta(pCatalog, rsp));
16,405✔
378
    }
379
  }
380

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

391
_return:
4,262,266✔
392
  taosArrayDestroy(hbRsp.pIndexRsp);
4,262,266✔
393
  hbRsp.pIndexRsp = NULL;
4,262,266✔
394

395
  tFreeSSTbHbRsp(&hbRsp);
4,262,266✔
396
  return code;
4,262,266✔
397
}
398

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

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

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

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

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

441
_return:
3,635✔
442
  taosArrayDestroy(hbRsp.pViewRsp);
3,635✔
443
  return code;
3,635✔
444
}
445

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

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

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

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

471
_return:
41,730✔
472
  taosArrayDestroy(hbRsp.pTsmas);
41,730✔
473
  return code;
41,730✔
474
}
475

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

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

565
  if (pRsp->query) {
25,458,333✔
566
    STscObj *pTscObj = (STscObj *)acquireTscObj(pRsp->connKey.tscRid);
25,458,333✔
567
    if (NULL == pTscObj) {
25,458,333✔
568
      tscDebug("tscObj rid %" PRIx64 " not exist", pRsp->connKey.tscRid);
1,223✔
569
    } else {
570
      if (pRsp->query->totalDnodes > 1) {
25,457,110✔
571
        SEpSet  originEpset = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
6,590,310✔
572
        if (!isEpsetEqual(&originEpset, &pRsp->query->epSet)) {
6,590,310✔
573
          SEpSet *pOrig = &originEpset;
23,589✔
574
          SEp    *pOrigEp = &pOrig->eps[pOrig->inUse];
23,589✔
575
          SEp    *pNewEp = &pRsp->query->epSet.eps[pRsp->query->epSet.inUse];
23,589✔
576
          tscDebug("mnode epset updated from %d/%d=>%s:%d to %d/%d=>%s:%d in hb", pOrig->inUse, pOrig->numOfEps,
23,589✔
577
                   pOrigEp->fqdn, pOrigEp->port, pRsp->query->epSet.inUse, pRsp->query->epSet.numOfEps, pNewEp->fqdn,
578
                   pNewEp->port);
579

580
          updateEpSet_s(&pTscObj->pAppInfo->mgmtEp, &pRsp->query->epSet);
23,589✔
581
        }
582
      }
583

584
      pTscObj->pAppInfo->totalDnodes = pRsp->query->totalDnodes;
25,457,110✔
585
      pTscObj->pAppInfo->onlineDnodes = pRsp->query->onlineDnodes;
25,457,110✔
586
      pTscObj->connId = pRsp->query->connId;
25,457,110✔
587
      tscTrace("connId:%u, hb rsp, dnodes %d/%d", pTscObj->connId, pTscObj->pAppInfo->onlineDnodes,
25,457,110✔
588
               pTscObj->pAppInfo->totalDnodes);
589

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

601
      if (pRsp->query->killConnection) {
25,457,110✔
602
        taos_close_internal(pTscObj);
×
603
      }
604

605
      if (pRsp->query->pQnodeList) {
25,457,110✔
606
        if (TSDB_CODE_SUCCESS != updateQnodeList(pTscObj->pAppInfo, pRsp->query->pQnodeList)) {
20,615✔
607
          tscWarn("update qnode list failed");
×
608
        }
609
      }
610

611
      releaseTscObj(pRsp->connKey.tscRid);
25,457,110✔
612
    }
613
  }
614

615
  int32_t kvNum = pRsp->info ? taosArrayGetSize(pRsp->info) : 0;
25,458,333✔
616

617
  tscDebug("hb got %d rsp kv", kvNum);
25,458,333✔
618

619
  if (kvNum > 0) {
25,458,333✔
620
    struct SCatalog *pCatalog = NULL;
9,140,641✔
621
    int32_t          code = catalogGetHandle(pReq->clusterId, &pCatalog);
9,140,641✔
622
    if (code != TSDB_CODE_SUCCESS) {
9,140,641✔
623
      tscWarn("catalogGetHandle failed, clusterId:0x%" PRIx64 ", error:%s", pReq->clusterId, tstrerror(code));
×
624
    } else {
625
      hbProcessQueryRspKvs(kvNum, pRsp->info, pCatalog, pAppHbMgr);
9,140,641✔
626
    }
627
  }
628

629
  taosHashRelease(pAppHbMgr->activeInfo, pReq);
25,458,333✔
630

631
  return TSDB_CODE_SUCCESS;
25,458,333✔
632
}
633

634
static int32_t hbAsyncCallBack(void *param, SDataBuf *pMsg, int32_t code) {
22,003,891✔
635
  if (0 == atomic_load_8(&clientHbMgr.inited)) {
22,003,891✔
636
    goto _return;
×
637
  }
638

639
  static int32_t    emptyRspNum = 0;
640
  int32_t           idx = *(int32_t *)param;
22,003,891✔
641
  SClientHbBatchRsp pRsp = {0};
22,003,891✔
642
  if (TSDB_CODE_SUCCESS == code) {
22,003,564✔
643
    code = tDeserializeSClientHbBatchRsp(pMsg->pData, pMsg->len, &pRsp);
21,654,501✔
644
    if (TSDB_CODE_SUCCESS != code) {
21,652,538✔
645
      tscError("deserialize hb rsp failed");
70✔
646
    }
647
    int32_t now = taosGetTimestampSec();
21,652,538✔
648
    int32_t delta = abs(now - pRsp.svrTimestamp);
21,652,226✔
649
    if (delta > tsTimestampDeltaLimit) {
21,652,226✔
650
      code = TSDB_CODE_TIME_UNSYNCED;
70✔
651
      tscError("time diff:%ds is too big", delta);
70✔
652
    }
653
  }
654

655
  int32_t rspNum = taosArrayGetSize(pRsp.rsps);
22,001,289✔
656

657
  (void)taosThreadMutexLock(&clientHbMgr.lock);
22,000,073✔
658

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

669
  SAppInstInfo *pInst = pAppHbMgr->pAppInstInfo;
22,003,891✔
670

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

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

691
  if (rspNum) {
21,654,758✔
692
    tscDebug("hb got %d rsp, %d empty rsp received before", rspNum,
21,169,026✔
693
             atomic_val_compare_exchange_32(&emptyRspNum, emptyRspNum, 0));
694
  } else {
695
    (void)atomic_add_fetch_32(&emptyRspNum, 1);
485,732✔
696
  }
697

698
  for (int32_t i = 0; i < rspNum; ++i) {
47,121,715✔
699
    SClientHbRsp *rsp = taosArrayGet(pRsp.rsps, i);
25,466,957✔
700
    code = (*clientHbMgr.rspHandle[rsp->connKey.connType])(pAppHbMgr, rsp);
25,466,957✔
701
    if (code) {
25,466,957✔
702
      break;
×
703
    }
704
  }
705

706
  (void)taosThreadMutexUnlock(&clientHbMgr.lock);
21,654,758✔
707

708
  tFreeClientHbBatchRsp(&pRsp);
709

710
_return:
21,654,758✔
711
  taosMemoryFree(pMsg->pData);
21,654,758✔
712
  taosMemoryFree(pMsg->pEpSet);
21,654,758✔
713
  return code;
21,654,758✔
714
}
715

716
int32_t hbBuildQueryDesc(SQueryHbReqBasic *hbBasic, STscObj *pObj) {
22,995,235✔
717
  int64_t    now = taosGetTimestampUs();
22,995,235✔
718
  SQueryDesc desc = {0};
22,995,235✔
719
  int32_t    code = 0;
22,995,235✔
720

721
  void *pIter = taosHashIterate(pObj->pRequests, NULL);
22,995,235✔
722
  while (pIter != NULL) {
46,567,492✔
723
    int64_t     *rid = pIter;
23,572,257✔
724
    SRequestObj *pRequest = acquireRequest(*rid);
23,572,257✔
725
    if (NULL == pRequest) {
23,572,257✔
726
      pIter = taosHashIterate(pObj->pRequests, pIter);
21,085✔
727
      continue;
21,085✔
728
    }
729

730
    if (pRequest->killed || 0 == pRequest->body.queryJob) {
23,551,172✔
731
      (void)releaseRequest(*rid);
8,848,833✔
732
      pIter = taosHashIterate(pObj->pRequests, pIter);
8,848,833✔
733
      continue;
8,848,833✔
734
    }
735

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

751
    if (desc.subPlanNum) {
14,702,339✔
752
      desc.subDesc = taosArrayInit(desc.subPlanNum, sizeof(SQuerySubDesc));
14,702,339✔
753
      if (NULL == desc.subDesc) {
14,702,339✔
754
        (void)releaseRequest(*rid);
×
755
        return terrno;
×
756
      }
757

758
      code = schedulerGetTasksStatus(pRequest->body.queryJob, desc.subDesc);
14,702,339✔
759
      if (code) {
14,702,339✔
760
        taosArrayDestroy(desc.subDesc);
4,153,438✔
761
        desc.subDesc = NULL;
4,153,438✔
762
        code = TSDB_CODE_SUCCESS;
4,153,438✔
763
      }
764
      desc.subPlanNum = taosArrayGetSize(desc.subDesc);
14,702,339✔
765
    } else {
766
      desc.subDesc = NULL;
×
767
    }
768

769
    (void)releaseRequest(*rid);
14,702,339✔
770
    if (NULL == taosArrayPush(hbBasic->queryDesc, &desc)) {
29,404,678✔
771
      taosArrayDestroy(desc.subDesc);
×
772
      return terrno;
×
773
    }
774

775
    pIter = taosHashIterate(pObj->pRequests, pIter);
14,702,339✔
776
  }
777

778
  return code;
22,995,235✔
779
}
780

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

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

795
  hbBasic->connId = pTscObj->connId;
25,838,026✔
796

797
  int32_t numOfQueries = pTscObj->pRequests ? taosHashGetSize(pTscObj->pRequests) : 0;
25,838,026✔
798
  if (numOfQueries <= 0) {
25,838,026✔
799
    req->query = hbBasic;
2,842,791✔
800
    releaseTscObj(connKey->tscRid);
2,842,791✔
801
    tscDebug("no queries on connection");
2,842,791✔
802
    return TSDB_CODE_SUCCESS;
2,842,791✔
803
  }
804

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

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

823
  req->query = hbBasic;
22,995,235✔
824
  releaseTscObj(connKey->tscRid);
22,995,235✔
825

826
  return TSDB_CODE_SUCCESS;
22,995,235✔
827
}
828

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

836
  int32_t code = 0;
782,198✔
837

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

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

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

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

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

893
_return:
775,803✔
894
  releaseTscObj(connKey->tscRid);
782,198✔
895
  if (code) {
782,198✔
896
    tscError("hb got user auth info failed since %s", tstrerror(code));
×
897
  }
898

899
  return code;
782,198✔
900
}
901

902
int32_t hbGetExpiredUserInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
19,826,169✔
903
  SUserAuthVersion *users = NULL;
19,826,169✔
904
  uint32_t          userNum = 0;
19,826,169✔
905
  int32_t           code = 0;
19,826,169✔
906

907
  code = catalogGetExpiredUsers(pCatalog, &users, &userNum);
19,826,169✔
908
  if (TSDB_CODE_SUCCESS != code) {
19,826,169✔
909
    return code;
×
910
  }
911

912
  if (userNum <= 0) {
19,826,169✔
913
    taosMemoryFree(users);
660,480✔
914
    return TSDB_CODE_SUCCESS;
660,480✔
915
  }
916

917
  for (int32_t i = 0; i < userNum; ++i) {
38,506,633✔
918
    SUserAuthVersion *user = &users[i];
19,340,944✔
919
    user->version = htonl(user->version);
19,340,944✔
920
  }
921

922
  SKv kv = {
19,165,689✔
923
      .key = HEARTBEAT_KEY_USER_AUTHINFO,
924
      .valueLen = sizeof(SUserAuthVersion) * userNum,
19,165,689✔
925
      .value = users,
926
  };
927

928
  tscDebug("hb got %d expired users, valueLen:%d", userNum, kv.valueLen);
19,165,689✔
929

930
  if (NULL == req->info) {
19,165,689✔
931
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
19,165,689✔
932
    if (NULL == req->info) {
19,165,689✔
933
      taosMemoryFree(users);
×
934
      return terrno;
×
935
    }
936
  }
937

938
  code = taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
19,165,689✔
939
  if (TSDB_CODE_SUCCESS != code) {
19,165,689✔
940
    taosMemoryFree(users);
×
941
    return code;
×
942
  }
943

944
  return TSDB_CODE_SUCCESS;
19,165,689✔
945
}
946

947
int32_t hbGetExpiredDBInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
21,537,367✔
948
  SDbCacheInfo *dbs = NULL;
21,537,367✔
949
  uint32_t      dbNum = 0;
21,537,367✔
950
  int32_t       code = 0;
21,537,367✔
951

952
  code = catalogGetExpiredDBs(pCatalog, &dbs, &dbNum);
21,537,367✔
953
  if (TSDB_CODE_SUCCESS != code) {
21,537,367✔
954
    return code;
×
955
  }
956

957
  if (dbNum <= 0) {
21,537,367✔
958
    taosMemoryFree(dbs);
15,424,269✔
959
    return TSDB_CODE_SUCCESS;
15,424,269✔
960
  }
961

962
  for (int32_t i = 0; i < dbNum; ++i) {
13,785,900✔
963
    SDbCacheInfo *db = &dbs[i];
7,672,802✔
964
    tscDebug("the %dth expired db:%s, dbId:%" PRId64
7,672,802✔
965
             ", vgVersion:%d, cfgVersion:%d, numOfTable:%d, startTs:%" PRId64,
966
             i, db->dbFName, db->dbId, db->vgVersion, db->cfgVersion, db->numOfTable, db->stateTs);
967

968
    db->dbId = htobe64(db->dbId);
7,672,802✔
969
    db->vgVersion = htonl(db->vgVersion);
7,672,802✔
970
    db->cfgVersion = htonl(db->cfgVersion);
7,672,802✔
971
    db->numOfTable = htonl(db->numOfTable);
7,672,802✔
972
    db->stateTs = htobe64(db->stateTs);
7,672,802✔
973
    db->tsmaVersion = htonl(db->tsmaVersion);
7,672,802✔
974
  }
975

976
  SKv kv = {
6,113,098✔
977
      .key = HEARTBEAT_KEY_DBINFO,
978
      .valueLen = sizeof(SDbCacheInfo) * dbNum,
6,113,098✔
979
      .value = dbs,
980
  };
981

982
  tscDebug("hb got %d expired db, valueLen:%d", dbNum, kv.valueLen);
6,113,098✔
983

984
  if (NULL == req->info) {
6,113,098✔
985
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
20,873✔
986
    if (NULL == req->info) {
20,873✔
987
      taosMemoryFree(dbs);
×
988
      return terrno;
×
989
    }
990
  }
991

992
  code = taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
6,113,098✔
993
  if (TSDB_CODE_SUCCESS != code) {
6,113,098✔
994
    taosMemoryFree(dbs);
×
995
    return code;
×
996
  }
997

998
  return TSDB_CODE_SUCCESS;
6,113,098✔
999
}
1000

1001
int32_t hbGetExpiredStbInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
21,537,367✔
1002
  SSTableVersion *stbs = NULL;
21,537,367✔
1003
  uint32_t        stbNum = 0;
21,537,367✔
1004
  int32_t         code = 0;
21,537,367✔
1005

1006
  code = catalogGetExpiredSTables(pCatalog, &stbs, &stbNum);
21,537,367✔
1007
  if (TSDB_CODE_SUCCESS != code) {
21,537,367✔
1008
    return code;
×
1009
  }
1010

1011
  if (stbNum <= 0) {
21,537,367✔
1012
    taosMemoryFree(stbs);
17,259,849✔
1013
    return TSDB_CODE_SUCCESS;
17,259,849✔
1014
  }
1015

1016
  for (int32_t i = 0; i < stbNum; ++i) {
10,138,593✔
1017
    SSTableVersion *stb = &stbs[i];
5,861,075✔
1018
    stb->suid = htobe64(stb->suid);
5,861,075✔
1019
    stb->sversion = htonl(stb->sversion);
5,861,075✔
1020
    stb->tversion = htonl(stb->tversion);
5,861,075✔
1021
    stb->smaVer = htonl(stb->smaVer);
5,861,075✔
1022
  }
1023

1024
  SKv kv = {
4,277,518✔
1025
      .key = HEARTBEAT_KEY_STBINFO,
1026
      .valueLen = sizeof(SSTableVersion) * stbNum,
4,277,518✔
1027
      .value = stbs,
1028
  };
1029

1030
  tscDebug("hb got %d expired stb, valueLen:%d", stbNum, kv.valueLen);
4,277,518✔
1031

1032
  if (NULL == req->info) {
4,277,518✔
1033
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
40✔
1034
    if (NULL == req->info) {
40✔
1035
      taosMemoryFree(stbs);
×
1036
      return terrno;
×
1037
    }
1038
  }
1039

1040
  code = taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
4,277,518✔
1041
  if (TSDB_CODE_SUCCESS != code) {
4,277,518✔
1042
    taosMemoryFree(stbs);
×
1043
    return code;
×
1044
  }
1045

1046
  return TSDB_CODE_SUCCESS;
4,277,518✔
1047
}
1048

1049
int32_t hbGetExpiredViewInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
21,537,367✔
1050
  SViewVersion    *views = NULL;
21,537,367✔
1051
  uint32_t         viewNum = 0;
21,537,367✔
1052
  int32_t          code = 0;
21,537,367✔
1053
  SDynViewVersion *pDynViewVer = NULL;
21,537,367✔
1054

1055
  TSC_ERR_JRET(catalogGetExpiredViews(pCatalog, &views, &viewNum, &pDynViewVer));
21,537,367✔
1056

1057
  if (viewNum <= 0) {
21,537,367✔
1058
    taosMemoryFree(views);
21,530,880✔
1059
    taosMemoryFree(pDynViewVer);
21,530,880✔
1060
    return TSDB_CODE_SUCCESS;
21,530,880✔
1061
  }
1062

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

1070
  tscDebug("hb got %u expired view, valueLen:%lu", viewNum, sizeof(SViewVersion) * viewNum);
6,487✔
1071

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

1079
  SKv kv = {
6,487✔
1080
      .key = HEARTBEAT_KEY_DYN_VIEW,
1081
      .valueLen = sizeof(SDynViewVersion),
1082
      .value = pDynViewVer,
1083
  };
1084

1085
  TSC_ERR_JRET(taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)));
6,487✔
1086

1087
  kv.key = HEARTBEAT_KEY_VIEWINFO;
6,487✔
1088
  kv.valueLen = sizeof(SViewVersion) * viewNum;
6,487✔
1089
  kv.value = views;
6,487✔
1090

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

1099
int32_t hbGetExpiredTSMAInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *pReq) {
21,537,367✔
1100
  int32_t       code = 0;
21,537,367✔
1101
  uint32_t      tsmaNum = 0;
21,537,367✔
1102
  STSMAVersion *tsmas = NULL;
21,537,367✔
1103

1104
  code = catalogGetExpiredTsmas(pCatalog, &tsmas, &tsmaNum);
21,537,367✔
1105
  if (code) {
21,537,367✔
1106
    taosMemoryFree(tsmas);
×
1107
    return code;
×
1108
  }
1109

1110
  if (tsmaNum <= 0) {
21,537,367✔
1111
    taosMemoryFree(tsmas);
21,495,637✔
1112
    return TSDB_CODE_SUCCESS;
21,495,637✔
1113
  }
1114

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

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

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

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

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

1152
  return TSDB_CODE_SUCCESS;
25,838,026✔
1153
}
1154

1155
int32_t hbQueryHbReqHandle(SClientHbKey *connKey, void *param, SClientHbReq *req) {
25,838,026✔
1156
  int32_t   code = 0;
25,838,026✔
1157
  SHbParam *hbParam = (SHbParam *)param;
25,838,026✔
1158
  SCatalog *pCatalog = NULL;
25,838,026✔
1159

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

1166
  if (hbParam->reqCnt == 0) {
25,838,026✔
1167
    code = catalogGetHandle(hbParam->clusterId, &pCatalog);
21,537,367✔
1168
    if (code != TSDB_CODE_SUCCESS) {
21,537,367✔
1169
      tscWarn("catalogGetHandle failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1170
      return code;
×
1171
    }
1172

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

1179
    if (!taosHashGet(clientHbMgr.appHbHash, &hbParam->clusterId, sizeof(hbParam->clusterId))) {
21,537,367✔
1180
      code = hbGetExpiredUserInfo(connKey, pCatalog, req);
19,826,169✔
1181
      if (TSDB_CODE_SUCCESS != code) {
19,826,169✔
1182
        tscWarn("hbGetExpiredUserInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1183
        return code;
×
1184
      }
1185
      if (clientHbMgr.appHbHash) {
19,826,169✔
1186
        code = taosHashPut(clientHbMgr.appHbHash, &hbParam->clusterId, sizeof(uint64_t), NULL, 0);
2,735,177✔
1187
        if (TSDB_CODE_SUCCESS != code) {
2,735,177✔
1188
          tscWarn("hbQueryHbReqHandle put clusterId failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId,
×
1189
                  tstrerror(code));
1190
          return code;
×
1191
        }
1192
      }
1193
    }
1194

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

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

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

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

1237
  ++hbParam->reqCnt;  // success to get catalog info
25,838,026✔
1238

1239
  return TSDB_CODE_SUCCESS;
25,838,026✔
1240
}
1241

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

1247
  clientHbMgr.rspHandle[CONN_TYPE__QUERY] = hbQueryHbRspHandle;
1,137,225✔
1248
  clientHbMgr.rspHandle[CONN_TYPE__TMQ] = hbQueryHbRspHandle;
1,137,225✔
1249
}
1,137,225✔
1250

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

1263
  int64_t  maxIpWhiteVer = 0;
22,025,343✔
1264
  void    *pIter = NULL;
22,025,343✔
1265
  SHbParam param = {0};
22,025,343✔
1266
  while ((pIter = taosHashIterate(pAppHbMgr->activeInfo, pIter))) {
49,152,996✔
1267
    SClientHbReq *pOneReq = pIter;
27,127,653✔
1268
    SClientHbKey *connKey = &pOneReq->connKey;
27,127,653✔
1269
    STscObj      *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
27,127,653✔
1270

1271
    if (!pTscObj || atomic_load_8(&pTscObj->dropped) == 1) {
27,127,653✔
1272
      if (pTscObj) releaseTscObj(connKey->tscRid);
1,289,627✔
1273
      continue;
1,289,627✔
1274
    }
1275

1276
    tstrncpy(pOneReq->userApp, pTscObj->optionInfo.userApp, sizeof(pOneReq->userApp));
25,838,026✔
1277
    tstrncpy(pOneReq->cInfo, pTscObj->optionInfo.cInfo, sizeof(pOneReq->cInfo));
25,838,026✔
1278
    pOneReq->userIp = pTscObj->optionInfo.userIp;
25,838,026✔
1279
    pOneReq->userDualIp = pTscObj->optionInfo.userDualIp;
25,838,026✔
1280
    tstrncpy(pOneReq->sVer, td_version, TSDB_VERSION_LEN);
25,838,026✔
1281

1282
    pOneReq = taosArrayPush((*pBatchReq)->reqs, pOneReq);
25,838,026✔
1283
    if (NULL == pOneReq) {
25,838,026✔
1284
      releaseTscObj(connKey->tscRid);
×
1285
      continue;
×
1286
    }
1287

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

1310
    int64_t ver = atomic_load_64(&pTscObj->whiteListInfo.ver);
25,838,026✔
1311
    maxIpWhiteVer = TMAX(maxIpWhiteVer, ver);
25,838,026✔
1312
    releaseTscObj(connKey->tscRid);
25,838,026✔
1313
  }
1314
  (*pBatchReq)->ipWhiteListVer = maxIpWhiteVer;
22,025,343✔
1315

1316
  return TSDB_CODE_SUCCESS;
22,025,343✔
1317
}
1318

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

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

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

1343
  taosHashClear(clientHbMgr.appSummary);
21,208,295✔
1344

1345
  for (int32_t i = 0; i < sz; ++i) {
45,468,882✔
1346
    SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
24,260,587✔
1347
    if (pAppHbMgr == NULL) continue;
24,260,587✔
1348

1349
    int64_t    clusterId = pAppHbMgr->pAppInstInfo->clusterId;
24,260,587✔
1350
    SAppHbReq *pApp = taosHashGet(clientHbMgr.appSummary, &clusterId, sizeof(clusterId));
24,260,587✔
1351
    if (NULL == pApp) {
24,260,587✔
1352
      (void)memcpy(&req.summary, &pAppHbMgr->pAppInstInfo->summary, sizeof(req.summary));
21,216,585✔
1353
      req.startTime = pAppHbMgr->startTime;
21,216,585✔
1354
      TSC_ERR_RET(taosHashPut(clientHbMgr.appSummary, &clusterId, sizeof(clusterId), &req, sizeof(req)));
21,216,585✔
1355
    } else {
1356
      if (pAppHbMgr->startTime < pApp->startTime) {
3,044,002✔
1357
        pApp->startTime = pAppHbMgr->startTime;
×
1358
      }
1359

1360
      hbMergeSummary(&pApp->summary, &pAppHbMgr->pAppInstInfo->summary);
3,044,002✔
1361
    }
1362
  }
1363

1364
  return TSDB_CODE_SUCCESS;
21,208,295✔
1365
}
1366

1367
static void *hbThreadFunc(void *param) {
1,137,225✔
1368
  setThreadName("hb");
1,137,225✔
1369
#ifdef WINDOWS
1370
  if (taosCheckCurrentInDll()) {
1371
    atexit(hbThreadFuncUnexpectedStopped);
1372
  }
1373
#endif
1374
  while (1) {
20,494,598✔
1375
    if (1 == clientHbMgr.threadStop) {
21,631,823✔
1376
      break;
418,709✔
1377
    }
1378

1379
    if (TSDB_CODE_SUCCESS != taosThreadMutexLock(&clientHbMgr.lock)) {
21,213,114✔
1380
      tscError("taosThreadMutexLock failed");
×
1381
      return NULL;
×
1382
    }
1383

1384
    int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
21,213,114✔
1385
    if (sz > 0) {
21,213,114✔
1386
      if (TSDB_CODE_SUCCESS != hbGatherAppInfo()) {
21,208,295✔
1387
        tscError("hbGatherAppInfo failed");
×
1388
        return NULL;
×
1389
      }
1390
      if (sz > 1 && !clientHbMgr.appHbHash) {
21,208,295✔
1391
        clientHbMgr.appHbHash = taosHashInit(0, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), true, HASH_NO_LOCK);
42,441✔
1392
        if (NULL == clientHbMgr.appHbHash) {
42,441✔
1393
          tscError("taosHashInit failed");
×
1394
          return NULL;
×
1395
        }
1396
      }
1397
      taosHashClear(clientHbMgr.appHbHash);
21,208,295✔
1398
    }
1399

1400
    for (int i = 0; i < sz; i++) {
45,473,701✔
1401
      SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
24,260,587✔
1402
      if (pAppHbMgr == NULL) {
24,260,587✔
1403
        continue;
18,666✔
1404
      }
1405

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

1429
      if (tSerializeSClientHbBatchReq(buf, tlen, pReq) == -1) {
22,025,343✔
1430
        tFreeClientHbBatchReq(pReq);
×
1431
        taosMemoryFree(buf);
×
1432
        break;
×
1433
      }
1434
      SMsgSendInfo *pInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
22,025,343✔
1435

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

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

1469
    if (TSDB_CODE_SUCCESS != taosThreadMutexUnlock(&clientHbMgr.lock)) {
21,213,114✔
1470
      tscError("taosThreadMutexLock failed");
×
1471
      return NULL;
×
1472
    }
1473
    taosMsleep(HEARTBEAT_INTERVAL);
21,213,114✔
1474
  }
1475
  taosHashCleanup(clientHbMgr.appHbHash);
418,709✔
1476
  return NULL;
418,709✔
1477
}
1478

1479
static int32_t hbCreateThread() {
1,137,225✔
1480
  int32_t      code = TSDB_CODE_SUCCESS;
1,137,225✔
1481
  TdThreadAttr thAttr;
1,127,708✔
1482
  TSC_ERR_JRET(taosThreadAttrInit(&thAttr));
1,137,225✔
1483
  TSC_ERR_JRET(taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE));
1,137,225✔
1484
#ifdef TD_COMPACT_OS
1485
  TSC_ERR_JRET(taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL));
1486
#endif
1487

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

1495
  if (code) {
1,137,225✔
1496
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
1497
    TSC_ERR_RET(terrno);
×
1498
  }
1499

1500
  return code;
1,137,225✔
1501
}
1502

1503
static void hbStopThread() {
1,137,542✔
1504
  if (0 == atomic_load_8(&clientHbMgr.inited)) {
1,137,542✔
1505
    return;
317✔
1506
  }
1507
  if (atomic_val_compare_exchange_8(&clientHbMgr.threadStop, 0, 1)) {
1,137,225✔
1508
    tscDebug("hb thread already stopped");
×
1509
    return;
×
1510
  }
1511

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

1526
  tscDebug("hb thread stopped");
1,137,225✔
1527
}
1528

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

1547
  // init app info
1548
  (*pAppHbMgr)->pAppInstInfo = pAppInstInfo;
1,194,965✔
1549

1550
  // init hash info
1551
  (*pAppHbMgr)->activeInfo = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
1,194,965✔
1552

1553
  if ((*pAppHbMgr)->activeInfo == NULL) {
1,194,965✔
1554
    TSC_ERR_JRET(terrno);
×
1555
  }
1556

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

1559
  TSC_ERR_JRET(taosThreadMutexLock(&clientHbMgr.lock));
1,194,965✔
1560
  if (taosArrayPush(clientHbMgr.appHbMgrs, &(*pAppHbMgr)) == NULL) {
2,389,930✔
1561
    code = terrno;
×
1562
    (void)taosThreadMutexUnlock(&clientHbMgr.lock);
×
1563
    goto _return;
×
1564
  }
1565
  (*pAppHbMgr)->idx = taosArrayGetSize(clientHbMgr.appHbMgrs) - 1;
1,194,965✔
1566
  TSC_ERR_JRET(taosThreadMutexUnlock(&clientHbMgr.lock));
1,194,965✔
1567

1568
  return TSDB_CODE_SUCCESS;
1,194,965✔
1569
_return:
×
1570
  taosMemoryFree(*pAppHbMgr);
×
1571
  return code;
×
1572
}
1573

1574
void hbFreeAppHbMgr(SAppHbMgr *pTarget) {
1,194,965✔
1575
  void *pIter = taosHashIterate(pTarget->activeInfo, NULL);
1,194,965✔
1576
  while (pIter != NULL) {
1,356,010✔
1577
    SClientHbReq *pOneReq = pIter;
161,045✔
1578
    tFreeClientHbReq(pOneReq);
1579
    pIter = taosHashIterate(pTarget->activeInfo, pIter);
161,045✔
1580
  }
1581
  taosHashCleanup(pTarget->activeInfo);
1,194,965✔
1582
  pTarget->activeInfo = NULL;
1,194,965✔
1583

1584
  taosMemoryFree(pTarget->key);
1,194,965✔
1585
  taosMemoryFree(pTarget);
1,194,965✔
1586
}
1,194,965✔
1587

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

1610
void appHbMgrCleanup(void) {
1,137,225✔
1611
  int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
1,137,225✔
1612
  for (int i = 0; i < sz; i++) {
2,332,190✔
1613
    SAppHbMgr *pTarget = taosArrayGetP(clientHbMgr.appHbMgrs, i);
1,194,965✔
1614
    if (pTarget == NULL) continue;
1,194,965✔
1615
    hbFreeAppHbMgr(pTarget);
1,194,965✔
1616
  }
1617
}
1,137,225✔
1618

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

1624
  clientHbMgr.appId = tGenIdPI64();
1,137,225✔
1625
  tscInfo("app initialized, appId:0x%" PRIx64, clientHbMgr.appId);
1,137,225✔
1626

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

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

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

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

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

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

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

1666
  return 0;
1,137,225✔
1667
}
1668

1669
void hbMgrCleanUp() {
1,137,542✔
1670
  hbStopThread();
1,137,542✔
1671

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

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

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

1702
  TSC_ERR_RET(taosHashPut(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey), &hbReq, sizeof(SClientHbReq)));
2,041,078✔
1703

1704
  (void)atomic_add_fetch_32(&pAppHbMgr->connKeyCnt, 1);
2,041,078✔
1705
  return 0;
2,041,078✔
1706
}
1707

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

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

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

1748
// set heart beat thread quit mode , if quicByKill 1 then kill thread else quit from inner
1749
void taos_set_hb_quit(int8_t quitByKill) { clientHbMgr.quitByKill = quitByKill; }
757,849✔
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