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

taosdata / TDengine / #4897

25 Dec 2025 10:17AM UTC coverage: 65.717% (-0.2%) from 65.929%
#4897

push

travis-ci

web-flow
fix: [6622889291] Fix invalid rowSize. (#34043)

186011 of 283047 relevant lines covered (65.72%)

113853896.64 hits per line

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

72.12
/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

42
static int32_t hbMqHbReqHandle(SClientHbKey *connKey, void *param, SClientHbReq *req) { return 0; }
×
43

44
static int32_t hbMqHbRspHandle(SAppHbMgr *pAppHbMgr, SClientHbRsp *pRsp) { return 0; }
×
45

46
static int32_t hbProcessUserAuthInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog,
696,965✔
47
                                        SAppHbMgr *pAppHbMgr) {
48
  int32_t code = TSDB_CODE_SUCCESS;
696,965✔
49

50
  SUserAuthBatchRsp batchRsp = {0};
696,965✔
51
  if (tDeserializeSUserAuthBatchRsp(value, valueLen, &batchRsp) != 0) {
696,965✔
52
    return TSDB_CODE_INVALID_MSG;
×
53
  }
54

55
  int32_t numOfBatchs = taosArrayGetSize(batchRsp.pArray);
696,965✔
56
  for (int32_t i = 0; i < numOfBatchs; ++i) {
1,410,954✔
57
    SGetUserAuthRsp *rsp = taosArrayGet(batchRsp.pArray, i);
713,989✔
58
    if (NULL == rsp) {
713,989✔
59
      code = terrno;
×
60
      goto _return;
×
61
    }
62
    tscDebug("hb to update user auth, user:%s, version:%d", rsp->user, rsp->version);
713,989✔
63

64
    TSC_ERR_JRET(catalogUpdateUserAuthInfo(pCatalog, rsp));
713,989✔
65
  }
66

67
  if (numOfBatchs > 0) {
696,965✔
68
    TSC_ERR_JRET(hbUpdateUserAuthInfo(pAppHbMgr, &batchRsp));
696,965✔
69
  }
70

71
  (void)atomic_val_compare_exchange_8(&pAppHbMgr->connHbFlag, 1, 2);
696,965✔
72

73
_return:
696,965✔
74
  taosArrayDestroy(batchRsp.pArray);
696,965✔
75
  return code;
696,965✔
76
}
77

78
static int32_t updateUserSessMetric(const char *user, SUserSessCfg *pCfg) {
920,358✔
79
  int32_t code = 0;
920,358✔
80
  int32_t lino = 0;
920,358✔
81
  if (user == NULL || pCfg == NULL) {
920,358✔
82
    return code;
×
83
  }
84

85
  SUserSessCfg cfg = {0, 0, 0, 0, 0};
920,358✔
86

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

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

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

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

111
  if (pCfg->sessMaxConcurrency != 0) {
×
112
    code = sessMgtUpdataLimit((char *)user, SESSION_MAX_CONCURRENCY, pCfg->sessMaxConcurrency);
×
113
    TAOS_CHECK_GOTO(code, &lino, _error);
×
114
  }
115

116
  if (pCfg->sessMaxCallVnodeNum != 0) {
×
117
    code = sessMgtUpdataLimit((char *)user, SESSION_MAX_CALL_VNODE_NUM, pCfg->sessMaxCallVnodeNum);
×
118
    TAOS_CHECK_GOTO(code, &lino, _error);
×
119
  }
120
_error:
×
121
  return code;
×
122
}
123
static int32_t hbUpdateUserAuthInfo(SAppHbMgr *pAppHbMgr, SUserAuthBatchRsp *batchRsp) {
696,965✔
124
  int32_t code = 0;
696,965✔
125
  int64_t clusterId = pAppHbMgr->pAppInstInfo->clusterId;
696,965✔
126
  for (int i = 0; i < TARRAY_SIZE(clientHbMgr.appHbMgrs); ++i) {
1,705,099✔
127
    SAppHbMgr *hbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
1,008,134✔
128
    if (!hbMgr || hbMgr->pAppInstInfo->clusterId != clusterId) {
1,008,134✔
129
      continue;
15,023✔
130
    }
131

132
    SClientHbReq    *pReq = NULL;
993,111✔
133
    SGetUserAuthRsp *pRsp = NULL;
993,111✔
134
    while ((pReq = taosHashIterate(hbMgr->activeInfo, pReq))) {
2,007,853✔
135
      STscObj *pTscObj = (STscObj *)acquireTscObj(pReq->connKey.tscRid);
1,125,475✔
136
      if (!pTscObj) {
1,125,475✔
137
        continue;
89,822✔
138
      }
139

140
      if (!pRsp) {
1,035,653✔
141
        for (int32_t j = 0; j < TARRAY_SIZE(batchRsp->pArray); ++j) {
971,975✔
142
          SGetUserAuthRsp *rsp = TARRAY_GET_ELEM(batchRsp->pArray, j);
861,242✔
143
          if (0 == strncmp(rsp->user, pTscObj->user, TSDB_USER_LEN)) {
861,242✔
144
            pRsp = rsp;
731,400✔
145
            break;
731,400✔
146
          }
147
        }
148
        if (!pRsp) {
842,133✔
149
          releaseTscObj(pReq->connKey.tscRid);
110,733✔
150
          taosHashCancelIterate(hbMgr->activeInfo, pReq);
110,733✔
151
          break;
110,733✔
152
        }
153
      }
154

155
      if (pRsp->dropped == 1) {
924,920✔
156
        if (atomic_val_compare_exchange_8(&pTscObj->dropped, 0, 1) == 0) {
4,562✔
157
          if (pTscObj->userDroppedInfo.fp) {
3,835✔
158
            SPassInfo *dropInfo = &pTscObj->userDroppedInfo;
1,900✔
159
            if (dropInfo->fp) {
1,900✔
160
              (*dropInfo->fp)(dropInfo->param, NULL, TAOS_NOTIFY_USER_DROPPED);
1,900✔
161
            }
162
          }
163
        }
164
        code = sessMgtRemoveUser(pTscObj->user);
4,562✔
165
        if (code != 0) {
4,562✔
166
          tscError("failed to remove user session metric, user:%s, code:%d", pTscObj->user, code);  
×
167
        }
168
        releaseTscObj(pReq->connKey.tscRid);
4,562✔
169
        continue;
4,562✔
170
      }
171

172
      pTscObj->authVer = pRsp->version;
920,358✔
173
      if (updateUserSessMetric(pTscObj->user, &pRsp->sessCfg) != 0) {
920,358✔
174
        tscError("failed to update user session metric, user:%s", pTscObj->user);
×
175
      }
176

177
      if (pTscObj->sysInfo != pRsp->sysInfo) {
920,358✔
178
        tscDebug("update sysInfo of user %s from %" PRIi8 " to %" PRIi8 ", conn:%" PRIi64, pRsp->user,
4,143✔
179
                 pTscObj->sysInfo, pRsp->sysInfo, pTscObj->id);
180
        pTscObj->sysInfo = pRsp->sysInfo;
4,143✔
181
      }
182

183
      if (pTscObj->passInfo.fp) {
920,358✔
184
        SPassInfo *passInfo = &pTscObj->passInfo;
1,140✔
185
        int32_t    oldVer = atomic_load_32(&passInfo->ver);
1,140✔
186
        if (oldVer < pRsp->passVer) {
1,140✔
187
          atomic_store_32(&passInfo->ver, pRsp->passVer);
760✔
188
          if (passInfo->fp) {
760✔
189
            (*passInfo->fp)(passInfo->param, &pRsp->passVer, TAOS_NOTIFY_PASSVER);
760✔
190
          }
191
          tscDebug("update passVer of user %s from %d to %d, conn:%" PRIi64, pRsp->user, oldVer,
760✔
192
                   atomic_load_32(&passInfo->ver), pTscObj->id);
193
        }
194
      }
195

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

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

241
static int32_t hbGenerateVgInfoFromRsp(SDBVgInfo **pInfo, SUseDbRsp *rsp) {
1,280,892✔
242
  int32_t    code = 0;
1,280,892✔
243
  SDBVgInfo *vgInfo = taosMemoryCalloc(1, sizeof(SDBVgInfo));
1,280,892✔
244
  if (NULL == vgInfo) {
1,280,892✔
245
    return terrno;
×
246
  }
247

248
  vgInfo->vgVersion = rsp->vgVersion;
1,280,892✔
249
  vgInfo->stateTs = rsp->stateTs;
1,280,892✔
250
  vgInfo->flags = rsp->flags;
1,280,892✔
251
  vgInfo->hashMethod = rsp->hashMethod;
1,280,892✔
252
  vgInfo->hashPrefix = rsp->hashPrefix;
1,280,892✔
253
  vgInfo->hashSuffix = rsp->hashSuffix;
1,280,892✔
254
  vgInfo->vgHash = taosHashInit(rsp->vgNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
1,280,892✔
255
  if (NULL == vgInfo->vgHash) {
1,280,892✔
256
    tscError("hash init[%d] failed", rsp->vgNum);
×
257
    code = terrno;
×
258
    goto _return;
×
259
  }
260

261
  for (int32_t j = 0; j < rsp->vgNum; ++j) {
4,660,575✔
262
    SVgroupInfo *pInfo = taosArrayGet(rsp->pVgroupInfos, j);
3,379,683✔
263
    if (taosHashPut(vgInfo->vgHash, &pInfo->vgId, sizeof(int32_t), pInfo, sizeof(SVgroupInfo)) != 0) {
3,379,683✔
264
      tscError("hash push failed, terrno:%d", terrno);
×
265
      code = terrno;
×
266
      goto _return;
×
267
    }
268
  }
269

270
_return:
1,280,892✔
271
  if (code) {
1,280,892✔
272
    taosHashCleanup(vgInfo->vgHash);
×
273
    taosMemoryFreeClear(vgInfo);
×
274
  }
275

276
  *pInfo = vgInfo;
1,280,892✔
277
  return code;
1,280,892✔
278
}
279

280
static int32_t hbProcessDBInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
5,495,854✔
281
  int32_t code = 0;
5,495,854✔
282

283
  SDbHbBatchRsp batchRsp = {0};
5,495,854✔
284
  if (tDeserializeSDbHbBatchRsp(value, valueLen, &batchRsp) != 0) {
5,495,854✔
285
    terrno = TSDB_CODE_INVALID_MSG;
×
286
    code = terrno;
×
287
    goto _return;
×
288
  }
289

290
  int32_t numOfBatchs = taosArrayGetSize(batchRsp.pArray);
5,495,854✔
291
  for (int32_t i = 0; i < numOfBatchs; ++i) {
6,851,292✔
292
    SDbHbRsp *rsp = taosArrayGet(batchRsp.pArray, i);
1,355,438✔
293
    if (NULL == rsp) {
1,355,438✔
294
      code = terrno;
×
295
      goto _return;
×
296
    }
297
    if (rsp->useDbRsp) {
1,355,438✔
298
      tscDebug("hb use db rsp, db:%s, vgVersion:%d, stateTs:%" PRId64 ", uid:%" PRIx64, rsp->useDbRsp->db,
1,076,644✔
299
               rsp->useDbRsp->vgVersion, rsp->useDbRsp->stateTs, rsp->useDbRsp->uid);
300

301
      if (rsp->useDbRsp->vgVersion < 0) {
1,076,644✔
302
        tscDebug("hb to remove db, db:%s", rsp->useDbRsp->db);
24,456✔
303
        code = catalogRemoveDB(pCatalog, rsp->useDbRsp->db, rsp->useDbRsp->uid);
24,456✔
304
      } else {
305
        SDBVgInfo *vgInfo = NULL;
1,052,188✔
306
        code = hbGenerateVgInfoFromRsp(&vgInfo, rsp->useDbRsp);
1,052,188✔
307
        if (TSDB_CODE_SUCCESS != code) {
1,052,188✔
308
          goto _return;
×
309
        }
310

311
        tscDebug("hb to update db vgInfo, db:%s", rsp->useDbRsp->db);
1,052,188✔
312

313
        TSC_ERR_JRET(catalogUpdateDBVgInfo(pCatalog, rsp->useDbRsp->db, rsp->useDbRsp->uid, vgInfo));
1,052,188✔
314

315
        if (IS_SYS_DBNAME(rsp->useDbRsp->db)) {
1,052,188✔
316
          code = hbGenerateVgInfoFromRsp(&vgInfo, rsp->useDbRsp);
228,704✔
317
          if (TSDB_CODE_SUCCESS != code) {
228,704✔
318
            goto _return;
×
319
          }
320

321
          TSC_ERR_JRET(catalogUpdateDBVgInfo(
228,704✔
322
              pCatalog, (rsp->useDbRsp->db[0] == 'i') ? TSDB_PERFORMANCE_SCHEMA_DB : TSDB_INFORMATION_SCHEMA_DB,
323
              rsp->useDbRsp->uid, vgInfo));
324
        }
325
      }
326
    }
327

328
    if (rsp->cfgRsp) {
1,355,438✔
329
      tscDebug("hb db cfg rsp, db:%s, cfgVersion:%d", rsp->cfgRsp->db, rsp->cfgRsp->cfgVersion);
220,274✔
330
      code = catalogUpdateDbCfg(pCatalog, rsp->cfgRsp->db, rsp->cfgRsp->dbId, rsp->cfgRsp);
220,274✔
331
      rsp->cfgRsp = NULL;
220,274✔
332
    }
333
    if (rsp->pTsmaRsp) {
1,355,438✔
334
      if (rsp->pTsmaRsp->pTsmas) {
673,371✔
335
        for (int32_t i = 0; i < rsp->pTsmaRsp->pTsmas->size; ++i) {
1,432✔
336
          STableTSMAInfo *pTsma = taosArrayGetP(rsp->pTsmaRsp->pTsmas, i);
716✔
337
          if (NULL == pTsma) {
716✔
338
            TSC_ERR_JRET(TSDB_CODE_OUT_OF_RANGE);
×
339
          }
340
          TSC_ERR_JRET(catalogAsyncUpdateTSMA(pCatalog, &pTsma, rsp->dbTsmaVersion));
716✔
341
        }
342
        taosArrayClear(rsp->pTsmaRsp->pTsmas);
716✔
343
      } else {
344
        TSC_ERR_JRET(catalogAsyncUpdateDbTsmaVersion(pCatalog, rsp->dbTsmaVersion, rsp->db, rsp->dbId));
672,655✔
345
      }
346
    }
347
  }
348

349
_return:
5,495,854✔
350

351
  tFreeSDbHbBatchRsp(&batchRsp);
5,495,854✔
352
  return code;
5,495,854✔
353
}
354

355
static int32_t hbProcessStbInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
3,819,278✔
356
  int32_t code = TSDB_CODE_SUCCESS;
3,819,278✔
357

358
  SSTbHbRsp hbRsp = {0};
3,819,278✔
359
  if (tDeserializeSSTbHbRsp(value, valueLen, &hbRsp) != 0) {
3,819,278✔
360
    terrno = TSDB_CODE_INVALID_MSG;
×
361
    return -1;
×
362
  }
363

364
  int32_t numOfMeta = taosArrayGetSize(hbRsp.pMetaRsp);
3,819,278✔
365
  for (int32_t i = 0; i < numOfMeta; ++i) {
3,869,315✔
366
    STableMetaRsp *rsp = taosArrayGet(hbRsp.pMetaRsp, i);
50,037✔
367
    if (NULL == rsp) {
50,037✔
368
      code = terrno;
×
369
      goto _return;
×
370
    }
371
    if (rsp->numOfColumns < 0) {
50,037✔
372
      tscDebug("hb to remove stb, db:%s, stb:%s", rsp->dbFName, rsp->stbName);
31,693✔
373
      TSC_ERR_JRET(catalogRemoveStbMeta(pCatalog, rsp->dbFName, rsp->dbId, rsp->stbName, rsp->suid));
31,693✔
374
    } else {
375
      tscDebug("hb to update stb, db:%s, stb:%s", rsp->dbFName, rsp->stbName);
18,344✔
376
      if (rsp->pSchemas[0].colId != PRIMARYKEY_TIMESTAMP_COL_ID) {
18,344✔
377
        tscError("invalid colId[%" PRIi16 "] for the first column in table meta rsp msg", rsp->pSchemas[0].colId);
×
378
        tFreeSSTbHbRsp(&hbRsp);
×
379
        return TSDB_CODE_TSC_INVALID_VALUE;
×
380
      }
381

382
      TSC_ERR_JRET(catalogAsyncUpdateTableMeta(pCatalog, rsp));
18,344✔
383
    }
384
  }
385

386
  int32_t numOfIndex = taosArrayGetSize(hbRsp.pIndexRsp);
3,819,278✔
387
  for (int32_t i = 0; i < numOfIndex; ++i) {
3,819,278✔
388
    STableIndexRsp *rsp = taosArrayGet(hbRsp.pIndexRsp, i);
×
389
    if (NULL == rsp) {
×
390
      code = terrno;
×
391
      goto _return;
×
392
    }
393
    TSC_ERR_JRET(catalogUpdateTableIndex(pCatalog, rsp));
×
394
  }
395

396
_return:
3,819,278✔
397
  taosArrayDestroy(hbRsp.pIndexRsp);
3,819,278✔
398
  hbRsp.pIndexRsp = NULL;
3,819,278✔
399

400
  tFreeSSTbHbRsp(&hbRsp);
3,819,278✔
401
  return code;
3,819,278✔
402
}
403

404
static int32_t hbProcessDynViewRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
5,955✔
405
  return catalogUpdateDynViewVer(pCatalog, (SDynViewVersion *)value);
5,955✔
406
}
407

408
static void hbFreeSViewMetaInRsp(void *p) {
×
409
  if (NULL == p || NULL == *(void **)p) {
×
410
    return;
×
411
  }
412
  SViewMetaRsp *pRsp = *(SViewMetaRsp **)p;
×
413
  tFreeSViewMetaRsp(pRsp);
×
414
  taosMemoryFreeClear(pRsp);
×
415
}
416

417
static int32_t hbProcessViewInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
5,955✔
418
  int32_t code = TSDB_CODE_SUCCESS;
5,955✔
419

420
  SViewHbRsp hbRsp = {0};
5,955✔
421
  if (tDeserializeSViewHbRsp(value, valueLen, &hbRsp) != 0) {
5,955✔
422
    taosArrayDestroyEx(hbRsp.pViewRsp, hbFreeSViewMetaInRsp);
×
423
    terrno = TSDB_CODE_INVALID_MSG;
×
424
    return -1;
×
425
  }
426

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

446
_return:
5,955✔
447
  taosArrayDestroy(hbRsp.pViewRsp);
5,955✔
448
  return code;
5,955✔
449
}
450

451
static int32_t hbprocessTSMARsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
46,540✔
452
  int32_t code = 0;
46,540✔
453

454
  STSMAHbRsp hbRsp = {0};
46,540✔
455
  if (tDeserializeTSMAHbRsp(value, valueLen, &hbRsp)) {
46,540✔
456
    terrno = TSDB_CODE_INVALID_MSG;
×
457
    return -1;
×
458
  }
459

460
  int32_t numOfTsma = taosArrayGetSize(hbRsp.pTsmas);
46,540✔
461
  for (int32_t i = 0; i < numOfTsma; ++i) {
46,540✔
462
    STableTSMAInfo *pTsmaInfo = taosArrayGetP(hbRsp.pTsmas, i);
×
463

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

476
_return:
46,540✔
477
  taosArrayDestroy(hbRsp.pTsmas);
46,540✔
478
  return code;
46,540✔
479
}
480

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

562
static int32_t hbQueryHbRspHandle(SAppHbMgr *pAppHbMgr, SClientHbRsp *pRsp) {
24,568,076✔
563
  SClientHbReq *pReq = taosHashAcquire(pAppHbMgr->activeInfo, &pRsp->connKey, sizeof(SClientHbKey));
24,568,076✔
564
  if (NULL == pReq) {
24,568,076✔
565
    tscWarn("pReq to get activeInfo, may be dropped, refId:%" PRIx64 ", type:%d", pRsp->connKey.tscRid,
12,252✔
566
            pRsp->connKey.connType);
567
    return TSDB_CODE_SUCCESS;
12,252✔
568
  }
569

570
  if (pRsp->query) {
24,555,824✔
571
    STscObj *pTscObj = (STscObj *)acquireTscObj(pRsp->connKey.tscRid);
24,555,824✔
572
    if (NULL == pTscObj) {
24,555,824✔
573
      tscDebug("tscObj rid %" PRIx64 " not exist", pRsp->connKey.tscRid);
2,231✔
574
    } else {
575
      if (pRsp->query->totalDnodes > 1) {
24,553,593✔
576
        SEpSet  originEpset = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
6,528,114✔
577
        if (!isEpsetEqual(&originEpset, &pRsp->query->epSet)) {
6,528,114✔
578
          SEpSet *pOrig = &originEpset;
67,782✔
579
          SEp    *pOrigEp = &pOrig->eps[pOrig->inUse];
67,782✔
580
          SEp    *pNewEp = &pRsp->query->epSet.eps[pRsp->query->epSet.inUse];
67,782✔
581
          tscDebug("mnode epset updated from %d/%d=>%s:%d to %d/%d=>%s:%d in hb", pOrig->inUse, pOrig->numOfEps,
67,782✔
582
                   pOrigEp->fqdn, pOrigEp->port, pRsp->query->epSet.inUse, pRsp->query->epSet.numOfEps, pNewEp->fqdn,
583
                   pNewEp->port);
584

585
          updateEpSet_s(&pTscObj->pAppInfo->mgmtEp, &pRsp->query->epSet);
67,782✔
586
        }
587
      }
588

589
      pTscObj->pAppInfo->totalDnodes = pRsp->query->totalDnodes;
24,553,593✔
590
      pTscObj->pAppInfo->onlineDnodes = pRsp->query->onlineDnodes;
24,553,593✔
591
      pTscObj->connId = pRsp->query->connId;
24,553,593✔
592
      tscTrace("connId:%u, hb rsp, dnodes %d/%d", pTscObj->connId, pTscObj->pAppInfo->onlineDnodes,
24,553,593✔
593
               pTscObj->pAppInfo->totalDnodes);
594

595
      if (pRsp->query->killRid) {
24,553,593✔
596
        tscDebug("QID:0x%" PRIx64 ", need to be killed now", pRsp->query->killRid);
191✔
597
        SRequestObj *pRequest = acquireRequest(pRsp->query->killRid);
191✔
598
        if (NULL == pRequest) {
191✔
599
          tscDebug("QID:0x%" PRIx64 ", not exist to kill", pRsp->query->killRid);
×
600
        } else {
601
          taos_stop_query((TAOS_RES *)pRequest);
191✔
602
          (void)releaseRequest(pRsp->query->killRid);
191✔
603
        }
604
      }
605

606
      if (pRsp->query->killConnection) {
24,553,593✔
607
        taos_close_internal(pTscObj);
×
608
      }
609

610
      if (pRsp->query->pQnodeList) {
24,553,593✔
611
        if (TSDB_CODE_SUCCESS != updateQnodeList(pTscObj->pAppInfo, pRsp->query->pQnodeList)) {
55,606✔
612
          tscWarn("update qnode list failed");
×
613
        }
614
      }
615

616
      releaseTscObj(pRsp->connKey.tscRid);
24,553,593✔
617
    }
618
  }
619

620
  int32_t kvNum = pRsp->info ? taosArrayGetSize(pRsp->info) : 0;
24,555,824✔
621

622
  tscDebug("hb got %d rsp kv", kvNum);
24,555,824✔
623

624
  if (kvNum > 0) {
24,555,824✔
625
    struct SCatalog *pCatalog = NULL;
8,454,550✔
626
    int32_t          code = catalogGetHandle(pReq->clusterId, &pCatalog);
8,454,550✔
627
    if (code != TSDB_CODE_SUCCESS) {
8,454,550✔
628
      tscWarn("catalogGetHandle failed, clusterId:0x%" PRIx64 ", error:%s", pReq->clusterId, tstrerror(code));
×
629
    } else {
630
      hbProcessQueryRspKvs(kvNum, pRsp->info, pCatalog, pAppHbMgr);
8,454,550✔
631
    }
632
  }
633

634
  taosHashRelease(pAppHbMgr->activeInfo, pReq);
24,555,824✔
635

636
  return TSDB_CODE_SUCCESS;
24,555,824✔
637
}
638

639
static int32_t hbAsyncCallBack(void *param, SDataBuf *pMsg, int32_t code) {
20,526,712✔
640
  if (0 == atomic_load_8(&clientHbMgr.inited)) {
20,526,712✔
641
    goto _return;
×
642
  }
643

644
  static int32_t    emptyRspNum = 0;
645
  int32_t           idx = *(int32_t *)param;
20,526,712✔
646
  SClientHbBatchRsp pRsp = {0};
20,526,712✔
647
  if (TSDB_CODE_SUCCESS == code) {
20,526,712✔
648
    code = tDeserializeSClientHbBatchRsp(pMsg->pData, pMsg->len, &pRsp);
20,187,269✔
649
    if (TSDB_CODE_SUCCESS != code) {
20,184,973✔
650
      tscError("deserialize hb rsp failed");
×
651
    }
652
    int32_t now = taosGetTimestampSec();
20,184,973✔
653
    int32_t delta = abs(now - pRsp.svrTimestamp);
20,186,262✔
654
    if (delta > tsTimestampDeltaLimit) {
20,186,262✔
655
      code = TSDB_CODE_TIME_UNSYNCED;
×
656
      tscError("time diff:%ds is too big", delta);
×
657
    }
658
  }
659

660
  int32_t rspNum = taosArrayGetSize(pRsp.rsps);
20,525,705✔
661

662
  (void)taosThreadMutexLock(&clientHbMgr.lock);
20,526,633✔
663

664
  SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, idx);
20,526,712✔
665
  if (pAppHbMgr == NULL) {
20,526,712✔
666
    (void)taosThreadMutexUnlock(&clientHbMgr.lock);
×
667
    tscError("appHbMgr not exist, idx:%d", idx);
×
668
    taosMemoryFree(pMsg->pData);
×
669
    taosMemoryFree(pMsg->pEpSet);
×
670
    tFreeClientHbBatchRsp(&pRsp);
671
    return TSDB_CODE_OUT_OF_RANGE;
×
672
  }
673

674
  SAppInstInfo *pInst = pAppHbMgr->pAppInstInfo;
20,526,712✔
675

676
  if (code != 0) {
20,526,712✔
677
    pInst->onlineDnodes = pInst->totalDnodes ? 0 : -1;
339,443✔
678
    tscDebug("hb rsp error %s, update server status %d/%d", tstrerror(code), pInst->onlineDnodes, pInst->totalDnodes);
339,443✔
679
    (void)taosThreadMutexUnlock(&clientHbMgr.lock);
339,443✔
680
    taosMemoryFree(pMsg->pData);
339,443✔
681
    taosMemoryFree(pMsg->pEpSet);
339,443✔
682
    tFreeClientHbBatchRsp(&pRsp);
683
    return code;
339,443✔
684
  }
685

686
  pInst->serverCfg.monitorParas = pRsp.monitorParas;
20,187,269✔
687
  pInst->serverCfg.enableAuditDelete = pRsp.enableAuditDelete;
20,187,269✔
688
  pInst->serverCfg.enableAuditSelect = pRsp.enableAuditSelect;
20,187,269✔
689
  pInst->serverCfg.enableAuditInsert = pRsp.enableAuditInsert;
20,187,269✔
690
  pInst->serverCfg.auditLevel = pRsp.auditLevel;
20,187,269✔
691
  pInst->serverCfg.enableStrongPass = pRsp.enableStrongPass;
20,187,269✔
692
  tsEnableStrongPassword = pInst->serverCfg.enableStrongPass;
20,187,269✔
693
  tscDebug("monitor paras from hb, clusterId:0x%" PRIx64 ", threshold:%d scope:%d", pInst->clusterId,
20,187,269✔
694
           pRsp.monitorParas.tsSlowLogThreshold, pRsp.monitorParas.tsSlowLogScope);
695

696
  if (rspNum) {
20,187,269✔
697
    tscDebug("hb got %d rsp, %d empty rsp received before", rspNum,
19,608,415✔
698
             atomic_val_compare_exchange_32(&emptyRspNum, emptyRspNum, 0));
699
  } else {
700
    (void)atomic_add_fetch_32(&emptyRspNum, 1);
578,854✔
701
  }
702

703
  for (int32_t i = 0; i < rspNum; ++i) {
44,755,345✔
704
    SClientHbRsp *rsp = taosArrayGet(pRsp.rsps, i);
24,568,076✔
705
    code = (*clientHbMgr.rspHandle[rsp->connKey.connType])(pAppHbMgr, rsp);
24,568,076✔
706
    if (code) {
24,568,076✔
707
      break;
×
708
    }
709
  }
710

711
  (void)taosThreadMutexUnlock(&clientHbMgr.lock);
20,187,269✔
712

713
  tFreeClientHbBatchRsp(&pRsp);
714

715
_return:
20,187,269✔
716
  taosMemoryFree(pMsg->pData);
20,187,269✔
717
  taosMemoryFree(pMsg->pEpSet);
20,187,269✔
718
  return code;
20,187,269✔
719
}
720

721
int32_t hbBuildQueryDesc(SQueryHbReqBasic *hbBasic, STscObj *pObj) {
23,110,536✔
722
  int64_t    now = taosGetTimestampUs();
23,110,536✔
723
  SQueryDesc desc = {0};
23,110,536✔
724
  int32_t    code = 0;
23,110,536✔
725

726
  void *pIter = taosHashIterate(pObj->pRequests, NULL);
23,110,536✔
727
  while (pIter != NULL) {
46,721,724✔
728
    int64_t     *rid = pIter;
23,611,188✔
729
    SRequestObj *pRequest = acquireRequest(*rid);
23,611,188✔
730
    if (NULL == pRequest) {
23,611,188✔
731
      pIter = taosHashIterate(pObj->pRequests, pIter);
15,391✔
732
      continue;
15,391✔
733
    }
734

735
    if (pRequest->killed || 0 == pRequest->body.queryJob) {
23,595,797✔
736
      (void)releaseRequest(*rid);
10,130,361✔
737
      pIter = taosHashIterate(pObj->pRequests, pIter);
10,130,361✔
738
      continue;
10,130,361✔
739
    }
740

741
    tstrncpy(desc.sql, pRequest->sqlstr, sizeof(desc.sql));
13,465,436✔
742
    desc.stime = pRequest->metric.start / 1000;
13,465,436✔
743
    desc.queryId = pRequest->requestId;
13,465,436✔
744
    desc.useconds = now - pRequest->metric.start;
13,465,436✔
745
    desc.reqRid = pRequest->self;
13,465,436✔
746
    desc.stableQuery = pRequest->stableQuery;
13,465,436✔
747
    desc.isSubQuery = pRequest->isSubReq;
13,465,436✔
748
    code = taosGetFqdn(desc.fqdn);
13,465,436✔
749
    if (TSDB_CODE_SUCCESS != code) {
13,465,436✔
750
      (void)releaseRequest(*rid);
×
751
      tscError("get fqdn failed");
×
752
      return TSDB_CODE_FAILED;
×
753
    }
754
    desc.subPlanNum = pRequest->body.subplanNum;
13,465,436✔
755

756
    if (desc.subPlanNum) {
13,465,436✔
757
      desc.subDesc = taosArrayInit(desc.subPlanNum, sizeof(SQuerySubDesc));
13,465,436✔
758
      if (NULL == desc.subDesc) {
13,465,436✔
759
        (void)releaseRequest(*rid);
×
760
        return terrno;
×
761
      }
762

763
      code = schedulerGetTasksStatus(pRequest->body.queryJob, desc.subDesc);
13,465,436✔
764
      if (code) {
13,465,436✔
765
        taosArrayDestroy(desc.subDesc);
3,189,414✔
766
        desc.subDesc = NULL;
3,189,414✔
767
        code = TSDB_CODE_SUCCESS;
3,189,414✔
768
      }
769
      desc.subPlanNum = taosArrayGetSize(desc.subDesc);
13,465,436✔
770
    } else {
771
      desc.subDesc = NULL;
×
772
    }
773

774
    (void)releaseRequest(*rid);
13,465,436✔
775
    if (NULL == taosArrayPush(hbBasic->queryDesc, &desc)) {
26,930,872✔
776
      taosArrayDestroy(desc.subDesc);
×
777
      return terrno;
×
778
    }
779

780
    pIter = taosHashIterate(pObj->pRequests, pIter);
13,465,436✔
781
  }
782

783
  return code;
23,110,536✔
784
}
785

786
int32_t hbGetQueryBasicInfo(SClientHbKey *connKey, SClientHbReq *req) {
24,930,929✔
787
  STscObj *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
24,930,929✔
788
  if (NULL == pTscObj) {
24,930,929✔
789
    tscWarn("tscObj rid 0x%" PRIx64 " not exist", connKey->tscRid);
×
790
    return terrno;
×
791
  }
792

793
  SQueryHbReqBasic *hbBasic = (SQueryHbReqBasic *)taosMemoryCalloc(1, sizeof(SQueryHbReqBasic));
24,930,929✔
794
  if (NULL == hbBasic) {
24,930,929✔
795
    tscError("calloc %d failed", (int32_t)sizeof(SQueryHbReqBasic));
×
796
    releaseTscObj(connKey->tscRid);
×
797
    return terrno;
×
798
  }
799

800
  hbBasic->connId = pTscObj->connId;
24,930,929✔
801

802
  int32_t numOfQueries = pTscObj->pRequests ? taosHashGetSize(pTscObj->pRequests) : 0;
24,930,929✔
803
  if (numOfQueries <= 0) {
24,930,929✔
804
    req->query = hbBasic;
1,820,393✔
805
    releaseTscObj(connKey->tscRid);
1,820,393✔
806
    tscDebug("no queries on connection");
1,820,393✔
807
    return TSDB_CODE_SUCCESS;
1,820,393✔
808
  }
809

810
  hbBasic->queryDesc = taosArrayInit(numOfQueries, sizeof(SQueryDesc));
23,110,536✔
811
  if (NULL == hbBasic->queryDesc) {
23,110,536✔
812
    tscWarn("taosArrayInit %d queryDesc failed", numOfQueries);
×
813
    releaseTscObj(connKey->tscRid);
×
814
    taosMemoryFree(hbBasic);
×
815
    return terrno;
×
816
  }
817

818
  int32_t code = hbBuildQueryDesc(hbBasic, pTscObj);
23,110,536✔
819
  if (code) {
23,110,536✔
820
    releaseTscObj(connKey->tscRid);
×
821
    if (hbBasic->queryDesc) {
×
822
      taosArrayDestroyEx(hbBasic->queryDesc, tFreeClientHbQueryDesc);
×
823
    }
824
    taosMemoryFree(hbBasic);
×
825
    return code;
×
826
  }
827

828
  req->query = hbBasic;
23,110,536✔
829
  releaseTscObj(connKey->tscRid);
23,110,536✔
830

831
  return TSDB_CODE_SUCCESS;
23,110,536✔
832
}
833

834
static int32_t hbGetUserAuthInfo(SClientHbKey *connKey, SHbParam *param, SClientHbReq *req) {
841,495✔
835
  STscObj *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
841,495✔
836
  if (!pTscObj) {
841,495✔
837
    tscWarn("tscObj rid 0x%" PRIx64 " not exist", connKey->tscRid);
×
838
    return terrno;
×
839
  }
840

841
  int32_t code = 0;
841,495✔
842

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

870
  // key/user not exist, add user
871
  SUserAuthVersion *user = taosMemoryMalloc(sizeof(SUserAuthVersion));
567,300✔
872
  if (!user) {
567,300✔
873
    code = terrno;
×
874
    goto _return;
×
875
  }
876
  tstrncpy(user->user, pTscObj->user, TSDB_USER_LEN);
567,300✔
877
  user->version = htonl(-1);  // force get userAuthInfo
567,300✔
878
  kv.valueLen = sizeof(SUserAuthVersion);
567,300✔
879
  kv.value = user;
567,300✔
880

881
  tscDebug("hb got user auth info, valueLen:%d, user:%s, authVer:%d, tscRid:%" PRIi64, kv.valueLen, user->user,
567,300✔
882
           pTscObj->authVer, connKey->tscRid);
883

884
  if (!req->info) {
567,300✔
885
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
567,300✔
886
    if (NULL == req->info) {
567,300✔
887
      code = terrno;
×
888
      goto _return;
×
889
    }
890
  }
891

892
  if (taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)) != 0) {
567,300✔
893
    taosMemoryFree(user);
×
894
    code = terrno ? terrno : TSDB_CODE_APP_ERROR;
×
895
    goto _return;
×
896
  }
897

898
_return:
833,384✔
899
  releaseTscObj(connKey->tscRid);
841,495✔
900
  if (code) {
841,495✔
901
    tscError("hb got user auth info failed since %s", tstrerror(code));
×
902
  }
903

904
  return code;
841,495✔
905
}
906

907
int32_t hbGetExpiredUserInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
18,886,884✔
908
  SUserAuthVersion *users = NULL;
18,886,884✔
909
  uint32_t          userNum = 0;
18,886,884✔
910
  int32_t           code = 0;
18,886,884✔
911

912
  code = catalogGetExpiredUsers(pCatalog, &users, &userNum);
18,886,884✔
913
  if (TSDB_CODE_SUCCESS != code) {
18,886,884✔
914
    return code;
×
915
  }
916

917
  if (userNum <= 0) {
18,886,884✔
918
    taosMemoryFree(users);
724,999✔
919
    return TSDB_CODE_SUCCESS;
724,999✔
920
  }
921

922
  for (int32_t i = 0; i < userNum; ++i) {
36,659,794✔
923
    SUserAuthVersion *user = &users[i];
18,497,909✔
924
    user->version = htonl(user->version);
18,497,909✔
925
  }
926

927
  SKv kv = {
18,161,885✔
928
      .key = HEARTBEAT_KEY_USER_AUTHINFO,
929
      .valueLen = sizeof(SUserAuthVersion) * userNum,
18,161,885✔
930
      .value = users,
931
  };
932

933
  tscDebug("hb got %d expired users, valueLen:%d", userNum, kv.valueLen);
18,161,885✔
934

935
  if (NULL == req->info) {
18,161,885✔
936
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
18,161,885✔
937
    if (NULL == req->info) {
18,161,885✔
938
      taosMemoryFree(users);
×
939
      return terrno;
×
940
    }
941
  }
942

943
  code = taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
18,161,885✔
944
  if (TSDB_CODE_SUCCESS != code) {
18,161,885✔
945
    taosMemoryFree(users);
×
946
    return code;
×
947
  }
948

949
  return TSDB_CODE_SUCCESS;
18,161,885✔
950
}
951

952
int32_t hbGetExpiredDBInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
19,971,268✔
953
  SDbCacheInfo *dbs = NULL;
19,971,268✔
954
  uint32_t      dbNum = 0;
19,971,268✔
955
  int32_t       code = 0;
19,971,268✔
956

957
  code = catalogGetExpiredDBs(pCatalog, &dbs, &dbNum);
19,971,268✔
958
  if (TSDB_CODE_SUCCESS != code) {
19,971,268✔
959
    return code;
×
960
  }
961

962
  if (dbNum <= 0) {
19,971,268✔
963
    taosMemoryFree(dbs);
14,450,067✔
964
    return TSDB_CODE_SUCCESS;
14,450,067✔
965
  }
966

967
  for (int32_t i = 0; i < dbNum; ++i) {
12,063,305✔
968
    SDbCacheInfo *db = &dbs[i];
6,542,104✔
969
    tscDebug("the %dth expired db:%s, dbId:%" PRId64
6,542,104✔
970
             ", vgVersion:%d, cfgVersion:%d, numOfTable:%d, startTs:%" PRId64,
971
             i, db->dbFName, db->dbId, db->vgVersion, db->cfgVersion, db->numOfTable, db->stateTs);
972

973
    db->dbId = htobe64(db->dbId);
6,542,104✔
974
    db->vgVersion = htonl(db->vgVersion);
6,542,104✔
975
    db->cfgVersion = htonl(db->cfgVersion);
6,542,104✔
976
    db->numOfTable = htonl(db->numOfTable);
6,542,104✔
977
    db->stateTs = htobe64(db->stateTs);
6,542,104✔
978
    db->tsmaVersion = htonl(db->tsmaVersion);
6,542,104✔
979
  }
980

981
  SKv kv = {
5,521,201✔
982
      .key = HEARTBEAT_KEY_DBINFO,
983
      .valueLen = sizeof(SDbCacheInfo) * dbNum,
5,521,201✔
984
      .value = dbs,
985
  };
986

987
  tscDebug("hb got %d expired db, valueLen:%d", dbNum, kv.valueLen);
5,521,201✔
988

989
  if (NULL == req->info) {
5,521,201✔
990
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
32,424✔
991
    if (NULL == req->info) {
32,424✔
992
      taosMemoryFree(dbs);
×
993
      return terrno;
×
994
    }
995
  }
996

997
  code = taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
5,521,201✔
998
  if (TSDB_CODE_SUCCESS != code) {
5,521,201✔
999
    taosMemoryFree(dbs);
×
1000
    return code;
×
1001
  }
1002

1003
  return TSDB_CODE_SUCCESS;
5,521,201✔
1004
}
1005

1006
int32_t hbGetExpiredStbInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
19,971,268✔
1007
  SSTableVersion *stbs = NULL;
19,971,268✔
1008
  uint32_t        stbNum = 0;
19,971,268✔
1009
  int32_t         code = 0;
19,971,268✔
1010

1011
  code = catalogGetExpiredSTables(pCatalog, &stbs, &stbNum);
19,971,268✔
1012
  if (TSDB_CODE_SUCCESS != code) {
19,971,268✔
1013
    return code;
×
1014
  }
1015

1016
  if (stbNum <= 0) {
19,971,268✔
1017
    taosMemoryFree(stbs);
16,137,486✔
1018
    return TSDB_CODE_SUCCESS;
16,137,486✔
1019
  }
1020

1021
  for (int32_t i = 0; i < stbNum; ++i) {
10,318,815✔
1022
    SSTableVersion *stb = &stbs[i];
6,485,033✔
1023
    stb->suid = htobe64(stb->suid);
6,485,033✔
1024
    stb->sversion = htonl(stb->sversion);
6,485,033✔
1025
    stb->tversion = htonl(stb->tversion);
6,485,033✔
1026
    stb->smaVer = htonl(stb->smaVer);
6,485,033✔
1027
  }
1028

1029
  SKv kv = {
3,833,782✔
1030
      .key = HEARTBEAT_KEY_STBINFO,
1031
      .valueLen = sizeof(SSTableVersion) * stbNum,
3,833,782✔
1032
      .value = stbs,
1033
  };
1034

1035
  tscDebug("hb got %d expired stb, valueLen:%d", stbNum, kv.valueLen);
3,833,782✔
1036

1037
  if (NULL == req->info) {
3,833,782✔
1038
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
783✔
1039
    if (NULL == req->info) {
783✔
1040
      taosMemoryFree(stbs);
×
1041
      return terrno;
×
1042
    }
1043
  }
1044

1045
  code = taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
3,833,782✔
1046
  if (TSDB_CODE_SUCCESS != code) {
3,833,782✔
1047
    taosMemoryFree(stbs);
×
1048
    return code;
×
1049
  }
1050

1051
  return TSDB_CODE_SUCCESS;
3,833,782✔
1052
}
1053

1054
int32_t hbGetExpiredViewInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
19,971,268✔
1055
  SViewVersion    *views = NULL;
19,971,268✔
1056
  uint32_t         viewNum = 0;
19,971,268✔
1057
  int32_t          code = 0;
19,971,268✔
1058
  SDynViewVersion *pDynViewVer = NULL;
19,971,268✔
1059

1060
  TSC_ERR_JRET(catalogGetExpiredViews(pCatalog, &views, &viewNum, &pDynViewVer));
19,971,268✔
1061

1062
  if (viewNum <= 0) {
19,971,268✔
1063
    taosMemoryFree(views);
19,963,661✔
1064
    taosMemoryFree(pDynViewVer);
19,963,661✔
1065
    return TSDB_CODE_SUCCESS;
19,963,661✔
1066
  }
1067

1068
  for (int32_t i = 0; i < viewNum; ++i) {
17,034✔
1069
    SViewVersion *view = &views[i];
9,427✔
1070
    view->dbId = htobe64(view->dbId);
9,427✔
1071
    view->viewId = htobe64(view->viewId);
9,427✔
1072
    view->version = htonl(view->version);
9,427✔
1073
  }
1074

1075
  tscDebug("hb got %u expired view, valueLen:%lu", viewNum, sizeof(SViewVersion) * viewNum);
7,607✔
1076

1077
  if (NULL == req->info) {
7,607✔
1078
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
×
1079
    if (NULL == req->info) {
×
1080
      TSC_ERR_JRET(terrno);
×
1081
    }
1082
  }
1083

1084
  SKv kv = {
7,607✔
1085
      .key = HEARTBEAT_KEY_DYN_VIEW,
1086
      .valueLen = sizeof(SDynViewVersion),
1087
      .value = pDynViewVer,
1088
  };
1089

1090
  TSC_ERR_JRET(taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)));
7,607✔
1091

1092
  kv.key = HEARTBEAT_KEY_VIEWINFO;
7,607✔
1093
  kv.valueLen = sizeof(SViewVersion) * viewNum;
7,607✔
1094
  kv.value = views;
7,607✔
1095

1096
  TSC_ERR_JRET(taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)));
7,607✔
1097
  return TSDB_CODE_SUCCESS;
7,607✔
1098
_return:
×
1099
  taosMemoryFree(views);
×
1100
  taosMemoryFree(pDynViewVer);
×
1101
  return code;
×
1102
}
1103

1104
int32_t hbGetExpiredTSMAInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *pReq) {
19,971,268✔
1105
  int32_t       code = 0;
19,971,268✔
1106
  uint32_t      tsmaNum = 0;
19,971,268✔
1107
  STSMAVersion *tsmas = NULL;
19,971,268✔
1108

1109
  code = catalogGetExpiredTsmas(pCatalog, &tsmas, &tsmaNum);
19,971,268✔
1110
  if (code) {
19,971,268✔
1111
    taosMemoryFree(tsmas);
×
1112
    return code;
×
1113
  }
1114

1115
  if (tsmaNum <= 0) {
19,971,268✔
1116
    taosMemoryFree(tsmas);
19,924,728✔
1117
    return TSDB_CODE_SUCCESS;
19,924,728✔
1118
  }
1119

1120
  for (int32_t i = 0; i < tsmaNum; ++i) {
93,080✔
1121
    STSMAVersion *tsma = &tsmas[i];
46,540✔
1122
    tsma->dbId = htobe64(tsma->dbId);
46,540✔
1123
    tsma->tsmaId = htobe64(tsma->tsmaId);
46,540✔
1124
    tsma->version = htonl(tsma->version);
46,540✔
1125
  }
1126

1127
  tscDebug("hb got %d expred tsmas, valueLen:%lu", tsmaNum, sizeof(STSMAVersion) * tsmaNum);
46,540✔
1128

1129
  if (!pReq->info) {
46,540✔
1130
    pReq->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
×
1131
    if (!pReq->info) {
×
1132
      taosMemoryFree(tsmas);
×
1133
      return terrno;
×
1134
    }
1135
  }
1136

1137
  SKv kv = {.key = HEARTBEAT_KEY_TSMA, .valueLen = sizeof(STSMAVersion) * tsmaNum, .value = tsmas};
46,540✔
1138
  code = taosHashPut(pReq->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
46,540✔
1139
  if (TSDB_CODE_SUCCESS != code) {
46,540✔
1140
    taosMemoryFree(tsmas);
×
1141
    return code;
×
1142
  }
1143
  return TSDB_CODE_SUCCESS;
46,540✔
1144
}
1145

1146
int32_t hbGetAppInfo(int64_t clusterId, SClientHbReq *req) {
24,930,929✔
1147
  SAppHbReq *pApp = taosHashGet(clientHbMgr.appSummary, &clusterId, sizeof(clusterId));
24,930,929✔
1148
  if (NULL != pApp) {
24,930,929✔
1149
    (void)memcpy(&req->app, pApp, sizeof(*pApp));
24,930,929✔
1150
  } else {
1151
    (void)memset(&req->app.summary, 0, sizeof(req->app.summary));
×
1152
    req->app.pid = taosGetPId();
×
1153
    req->app.appId = clientHbMgr.appId;
×
1154
    TSC_ERR_RET(taosGetAppName(req->app.name, NULL));
×
1155
  }
1156

1157
  return TSDB_CODE_SUCCESS;
24,930,929✔
1158
}
1159

1160
int32_t hbQueryHbReqHandle(SClientHbKey *connKey, void *param, SClientHbReq *req) {
24,930,929✔
1161
  int32_t   code = 0;
24,930,929✔
1162
  SHbParam *hbParam = (SHbParam *)param;
24,930,929✔
1163
  SCatalog *pCatalog = NULL;
24,930,929✔
1164

1165
  code = hbGetQueryBasicInfo(connKey, req);
24,930,929✔
1166
  if (code != TSDB_CODE_SUCCESS) {
24,930,929✔
1167
    tscWarn("hbGetQueryBasicInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1168
    return code;
×
1169
  }
1170

1171
  if (hbParam->reqCnt == 0) {
24,930,929✔
1172
    code = catalogGetHandle(hbParam->clusterId, &pCatalog);
19,971,268✔
1173
    if (code != TSDB_CODE_SUCCESS) {
19,971,268✔
1174
      tscWarn("catalogGetHandle failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1175
      return code;
×
1176
    }
1177

1178
    code = hbGetAppInfo(hbParam->clusterId, req);
19,971,268✔
1179
    if (TSDB_CODE_SUCCESS != code) {
19,971,268✔
1180
      tscWarn("getAppInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1181
      return code;
×
1182
    }
1183

1184
    if (!taosHashGet(clientHbMgr.appHbHash, &hbParam->clusterId, sizeof(hbParam->clusterId))) {
19,971,268✔
1185
      code = hbGetExpiredUserInfo(connKey, pCatalog, req);
18,886,884✔
1186
      if (TSDB_CODE_SUCCESS != code) {
18,886,884✔
1187
        tscWarn("hbGetExpiredUserInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1188
        return code;
×
1189
      }
1190
      if (clientHbMgr.appHbHash) {
18,886,884✔
1191
        code = taosHashPut(clientHbMgr.appHbHash, &hbParam->clusterId, sizeof(uint64_t), NULL, 0);
1,718,350✔
1192
        if (TSDB_CODE_SUCCESS != code) {
1,718,350✔
1193
          tscWarn("hbQueryHbReqHandle put clusterId failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId,
×
1194
                  tstrerror(code));
1195
          return code;
×
1196
        }
1197
      }
1198
    }
1199

1200
    // invoke after hbGetExpiredUserInfo
1201
    if (2 != atomic_load_8(&hbParam->pAppHbMgr->connHbFlag)) {
19,971,268✔
1202
      code = hbGetUserAuthInfo(connKey, hbParam, req);
841,495✔
1203
      if (TSDB_CODE_SUCCESS != code) {
841,495✔
1204
        tscWarn("hbGetUserAuthInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1205
        return code;
×
1206
      }
1207
      atomic_store_8(&hbParam->pAppHbMgr->connHbFlag, 1);
841,495✔
1208
    }
1209

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

1216
    code = hbGetExpiredStbInfo(connKey, pCatalog, req);
19,971,268✔
1217
    if (TSDB_CODE_SUCCESS != code) {
19,971,268✔
1218
      tscWarn("hbGetExpiredStbInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1219
      return code;
×
1220
    }
1221

1222
#ifdef TD_ENTERPRISE
1223
    code = hbGetExpiredViewInfo(connKey, pCatalog, req);
19,971,268✔
1224
    if (TSDB_CODE_SUCCESS != code) {
19,971,268✔
1225
      tscWarn("hbGetExpiredViewInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1226
      return code;
×
1227
    }
1228
#endif
1229
    code = hbGetExpiredTSMAInfo(connKey, pCatalog, req);
19,971,268✔
1230
    if (TSDB_CODE_SUCCESS != code) {
19,971,268✔
1231
      tscWarn("hbGetExpiredTSMAInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1232
      return code;
×
1233
    }
1234
  } else {
1235
    code = hbGetAppInfo(hbParam->clusterId, req);
4,959,661✔
1236
    if (TSDB_CODE_SUCCESS != code) {
4,959,661✔
1237
      tscWarn("hbGetAppInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1238
      return code;
×
1239
    }
1240
  }
1241

1242
  ++hbParam->reqCnt;  // success to get catalog info
24,930,929✔
1243

1244
  return TSDB_CODE_SUCCESS;
24,930,929✔
1245
}
1246

1247
static FORCE_INLINE void hbMgrInitHandle() {
1248
  // init all handle
1249
  clientHbMgr.reqHandle[CONN_TYPE__QUERY] = hbQueryHbReqHandle;
1,195,274✔
1250
  clientHbMgr.reqHandle[CONN_TYPE__TMQ] = hbMqHbReqHandle;
1,195,274✔
1251

1252
  clientHbMgr.rspHandle[CONN_TYPE__QUERY] = hbQueryHbRspHandle;
1,195,274✔
1253
  clientHbMgr.rspHandle[CONN_TYPE__TMQ] = hbMqHbRspHandle;
1,195,274✔
1254
}
1,195,274✔
1255

1256
int32_t hbGatherAllInfo(SAppHbMgr *pAppHbMgr, SClientHbBatchReq **pBatchReq) {
20,552,025✔
1257
  *pBatchReq = taosMemoryCalloc(1, sizeof(SClientHbBatchReq));
20,552,025✔
1258
  if (pBatchReq == NULL) {
20,552,025✔
1259
    return terrno;
×
1260
  }
1261
  int32_t connKeyCnt = atomic_load_32(&pAppHbMgr->connKeyCnt);
20,552,025✔
1262
  (*pBatchReq)->reqs = taosArrayInit(connKeyCnt, sizeof(SClientHbReq));
20,552,025✔
1263
  if (!(*pBatchReq)->reqs) {
20,552,025✔
1264
    tFreeClientHbBatchReq(*pBatchReq);
×
1265
    return terrno;
×
1266
  }
1267

1268
  int64_t  maxIpWhiteVer = 0;
20,552,025✔
1269
  void    *pIter = NULL;
20,552,025✔
1270
  SHbParam param = {0};
20,552,025✔
1271
  while ((pIter = taosHashIterate(pAppHbMgr->activeInfo, pIter))) {
46,934,253✔
1272
    SClientHbReq *pOneReq = pIter;
26,382,228✔
1273
    SClientHbKey *connKey = &pOneReq->connKey;
26,382,228✔
1274
    STscObj      *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
26,382,228✔
1275

1276
    if (!pTscObj || atomic_load_8(&pTscObj->dropped) == 1) {
26,382,228✔
1277
      if (pTscObj) releaseTscObj(connKey->tscRid);
1,451,299✔
1278
      continue;
1,451,299✔
1279
    }
1280

1281
    tstrncpy(pOneReq->userApp, pTscObj->optionInfo.userApp, sizeof(pOneReq->userApp));
24,930,929✔
1282
    tstrncpy(pOneReq->cInfo, pTscObj->optionInfo.cInfo, sizeof(pOneReq->cInfo));
24,930,929✔
1283
    pOneReq->userIp = pTscObj->optionInfo.userIp;
24,930,929✔
1284
    pOneReq->userDualIp = pTscObj->optionInfo.userDualIp;
24,930,929✔
1285
    tstrncpy(pOneReq->sVer, td_version, TSDB_VERSION_LEN);
24,930,929✔
1286

1287
    pOneReq = taosArrayPush((*pBatchReq)->reqs, pOneReq);
24,930,929✔
1288
    if (NULL == pOneReq) {
24,930,929✔
1289
      releaseTscObj(connKey->tscRid);
×
1290
      continue;
×
1291
    }
1292

1293
    switch (connKey->connType) {
24,930,929✔
1294
      case CONN_TYPE__QUERY: {
24,930,929✔
1295
        if (param.clusterId == 0) {
24,930,929✔
1296
          // init
1297
          param.clusterId = pOneReq->clusterId;
19,971,268✔
1298
          param.pAppHbMgr = pAppHbMgr;
19,971,268✔
1299
          param.connHbFlag = atomic_load_8(&pAppHbMgr->connHbFlag);
19,971,268✔
1300
        }
1301
        break;
24,930,929✔
1302
      }
1303
      default:
×
1304
        break;
×
1305
    }
1306
    if (clientHbMgr.reqHandle[connKey->connType]) {
24,930,929✔
1307
      int32_t code = (*clientHbMgr.reqHandle[connKey->connType])(connKey, &param, pOneReq);
24,930,929✔
1308
      if (code) {
24,930,929✔
1309
        tscWarn("hbGatherAllInfo failed since %s, tscRid:%" PRIi64 ", connType:%" PRIi8, tstrerror(code),
×
1310
                connKey->tscRid, connKey->connType);
1311
      }
1312
    }
1313

1314
    int64_t ver = atomic_load_64(&pTscObj->whiteListInfo.ver);
24,930,929✔
1315
    maxIpWhiteVer = TMAX(maxIpWhiteVer, ver);
24,930,929✔
1316
    releaseTscObj(connKey->tscRid);
24,930,929✔
1317
  }
1318
  (*pBatchReq)->ipWhiteListVer = maxIpWhiteVer;
20,552,025✔
1319

1320
  return TSDB_CODE_SUCCESS;
20,552,025✔
1321
}
1322

1323
void hbThreadFuncUnexpectedStopped(void) { atomic_store_8(&clientHbMgr.threadStop, 2); }
×
1324

1325
void hbMergeSummary(SAppClusterSummary *dst, SAppClusterSummary *src) {
2,307,882✔
1326
  dst->numOfInsertsReq += src->numOfInsertsReq;
2,307,882✔
1327
  dst->numOfInsertRows += src->numOfInsertRows;
2,307,882✔
1328
  dst->insertElapsedTime += src->insertElapsedTime;
2,307,882✔
1329
  dst->insertBytes += src->insertBytes;
2,307,882✔
1330
  dst->fetchBytes += src->fetchBytes;
2,307,882✔
1331
  dst->queryElapsedTime += src->queryElapsedTime;
2,307,882✔
1332
  dst->numOfSlowQueries += src->numOfSlowQueries;
2,307,882✔
1333
  dst->totalRequests += src->totalRequests;
2,307,882✔
1334
  dst->currentRequests += src->currentRequests;
2,307,882✔
1335
}
2,307,882✔
1336

1337
int32_t hbGatherAppInfo(void) {
20,395,635✔
1338
  SAppHbReq req = {0};
20,395,635✔
1339
  int32_t   code = TSDB_CODE_SUCCESS;
20,395,635✔
1340
  int       sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
20,395,635✔
1341
  if (sz > 0) {
20,395,635✔
1342
    req.pid = taosGetPId();
20,395,635✔
1343
    req.appId = clientHbMgr.appId;
20,395,635✔
1344
    TSC_ERR_RET(taosGetAppName(req.name, NULL));
20,395,635✔
1345
  }
1346

1347
  taosHashClear(clientHbMgr.appSummary);
20,395,635✔
1348

1349
  for (int32_t i = 0; i < sz; ++i) {
43,121,656✔
1350
    SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
22,726,021✔
1351
    if (pAppHbMgr == NULL) continue;
22,726,021✔
1352

1353
    int64_t    clusterId = pAppHbMgr->pAppInstInfo->clusterId;
22,726,021✔
1354
    SAppHbReq *pApp = taosHashGet(clientHbMgr.appSummary, &clusterId, sizeof(clusterId));
22,726,021✔
1355
    if (NULL == pApp) {
22,726,021✔
1356
      (void)memcpy(&req.summary, &pAppHbMgr->pAppInstInfo->summary, sizeof(req.summary));
20,418,139✔
1357
      req.startTime = pAppHbMgr->startTime;
20,418,139✔
1358
      TSC_ERR_RET(taosHashPut(clientHbMgr.appSummary, &clusterId, sizeof(clusterId), &req, sizeof(req)));
20,418,139✔
1359
    } else {
1360
      if (pAppHbMgr->startTime < pApp->startTime) {
2,307,882✔
1361
        pApp->startTime = pAppHbMgr->startTime;
×
1362
      }
1363

1364
      hbMergeSummary(&pApp->summary, &pAppHbMgr->pAppInstInfo->summary);
2,307,882✔
1365
    }
1366
  }
1367

1368
  return TSDB_CODE_SUCCESS;
20,395,635✔
1369
}
1370

1371
static void *hbThreadFunc(void *param) {
1,195,274✔
1372
  setThreadName("hb");
1,195,274✔
1373
#ifdef WINDOWS
1374
  if (taosCheckCurrentInDll()) {
1375
    atexit(hbThreadFuncUnexpectedStopped);
1376
  }
1377
#endif
1378
  while (1) {
19,714,413✔
1379
    if (1 == clientHbMgr.threadStop) {
20,909,687✔
1380
      break;
512,067✔
1381
    }
1382

1383
    if (TSDB_CODE_SUCCESS != taosThreadMutexLock(&clientHbMgr.lock)) {
20,397,620✔
1384
      tscError("taosThreadMutexLock failed");
×
1385
      return NULL;
×
1386
    }
1387

1388
    int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
20,397,620✔
1389
    if (sz > 0) {
20,397,620✔
1390
      if (TSDB_CODE_SUCCESS != hbGatherAppInfo()) {
20,395,635✔
1391
        tscError("hbGatherAppInfo failed");
×
1392
        return NULL;
×
1393
      }
1394
      if (sz > 1 && !clientHbMgr.appHbHash) {
20,395,635✔
1395
        clientHbMgr.appHbHash = taosHashInit(0, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), true, HASH_NO_LOCK);
36,229✔
1396
        if (NULL == clientHbMgr.appHbHash) {
36,229✔
1397
          tscError("taosHashInit failed");
×
1398
          return NULL;
×
1399
        }
1400
      }
1401
      taosHashClear(clientHbMgr.appHbHash);
20,395,635✔
1402
    }
1403

1404
    for (int i = 0; i < sz; i++) {
43,123,641✔
1405
      SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
22,726,021✔
1406
      if (pAppHbMgr == NULL) {
22,726,021✔
1407
        continue;
32,126✔
1408
      }
1409

1410
      int32_t connCnt = atomic_load_32(&pAppHbMgr->connKeyCnt);
22,726,021✔
1411
      if (connCnt == 0) {
22,726,021✔
1412
        continue;
2,173,996✔
1413
      }
1414
      SClientHbBatchReq *pReq = NULL;
20,552,025✔
1415
      int32_t            code = hbGatherAllInfo(pAppHbMgr, &pReq);
20,552,025✔
1416
      if (TSDB_CODE_SUCCESS != code || taosArrayGetP(clientHbMgr.appHbMgrs, i) == NULL) {
20,552,025✔
1417
        terrno = code ? code : TSDB_CODE_OUT_OF_RANGE;
×
1418
        tFreeClientHbBatchReq(pReq);
×
1419
        continue;
×
1420
      }
1421
      int tlen = tSerializeSClientHbBatchReq(NULL, 0, pReq);
20,552,025✔
1422
      if (tlen == -1) {
20,552,025✔
1423
        tFreeClientHbBatchReq(pReq);
×
1424
        break;
×
1425
      }
1426
      void *buf = taosMemoryMalloc(tlen);
20,552,025✔
1427
      if (buf == NULL) {
20,552,025✔
1428
        tFreeClientHbBatchReq(pReq);
×
1429
        // hbClearReqInfo(pAppHbMgr);
1430
        break;
×
1431
      }
1432

1433
      if (tSerializeSClientHbBatchReq(buf, tlen, pReq) == -1) {
20,552,025✔
1434
        tFreeClientHbBatchReq(pReq);
×
1435
        taosMemoryFree(buf);
×
1436
        break;
×
1437
      }
1438
      SMsgSendInfo *pInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
20,552,025✔
1439

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

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

1473
    if (TSDB_CODE_SUCCESS != taosThreadMutexUnlock(&clientHbMgr.lock)) {
20,397,620✔
1474
      tscError("taosThreadMutexLock failed");
×
1475
      return NULL;
×
1476
    }
1477
    taosMsleep(HEARTBEAT_INTERVAL);
20,397,620✔
1478
  }
1479
  taosHashCleanup(clientHbMgr.appHbHash);
512,067✔
1480
  return NULL;
512,067✔
1481
}
1482

1483
static int32_t hbCreateThread() {
1,195,274✔
1484
  int32_t      code = TSDB_CODE_SUCCESS;
1,195,274✔
1485
  TdThreadAttr thAttr;
1,178,346✔
1486
  TSC_ERR_JRET(taosThreadAttrInit(&thAttr));
1,195,274✔
1487
  TSC_ERR_JRET(taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE));
1,195,274✔
1488
#ifdef TD_COMPACT_OS
1489
  TSC_ERR_JRET(taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL));
1490
#endif
1491

1492
  if (taosThreadCreate(&clientHbMgr.thread, &thAttr, hbThreadFunc, NULL) != 0) {
1,195,274✔
1493
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
1494
    TSC_ERR_RET(terrno);
×
1495
  }
1496
  (void)taosThreadAttrDestroy(&thAttr);
1,195,274✔
1497
_return:
1,195,274✔
1498

1499
  if (code) {
1,195,274✔
1500
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
1501
    TSC_ERR_RET(terrno);
×
1502
  }
1503

1504
  return code;
1,195,274✔
1505
}
1506

1507
static void hbStopThread() {
1,196,350✔
1508
  if (0 == atomic_load_8(&clientHbMgr.inited)) {
1,196,350✔
1509
    return;
1,076✔
1510
  }
1511
  if (atomic_val_compare_exchange_8(&clientHbMgr.threadStop, 0, 1)) {
1,195,274✔
1512
    tscDebug("hb thread already stopped");
×
1513
    return;
×
1514
  }
1515

1516
  int32_t code = TSDB_CODE_SUCCESS;
1,195,274✔
1517
  // thread quit mode kill or inner exit from self-thread
1518
  if (clientHbMgr.quitByKill) {
1,195,274✔
1519
    code = taosThreadKill(clientHbMgr.thread, 0);
729,635✔
1520
    if (TSDB_CODE_SUCCESS != code) {
729,635✔
1521
      tscError("taosThreadKill failed since %s", tstrerror(code));
×
1522
    }
1523
  } else {
1524
    code = taosThreadJoin(clientHbMgr.thread, NULL);
465,639✔
1525
    if (TSDB_CODE_SUCCESS != code) {
465,639✔
1526
      tscError("taosThreadJoin failed since %s", tstrerror(code));
×
1527
    }
1528
  }
1529

1530
  tscDebug("hb thread stopped");
1,195,274✔
1531
}
1532

1533
int32_t appHbMgrInit(SAppInstInfo *pAppInstInfo, char *key, SAppHbMgr **pAppHbMgr) {
1,253,644✔
1534
  int32_t code = TSDB_CODE_SUCCESS;
1,253,644✔
1535
  TSC_ERR_RET(hbMgrInit());
1,253,644✔
1536
  *pAppHbMgr = taosMemoryMalloc(sizeof(SAppHbMgr));
1,253,644✔
1537
  if (*pAppHbMgr == NULL) {
1,253,644✔
1538
    TSC_ERR_JRET(terrno);
×
1539
  }
1540
  // init stat
1541
  (*pAppHbMgr)->startTime = taosGetTimestampMs();
2,488,982✔
1542
  (*pAppHbMgr)->connKeyCnt = 0;
1,253,644✔
1543
  (*pAppHbMgr)->connHbFlag = 0;
1,253,644✔
1544
  (*pAppHbMgr)->reportCnt = 0;
1,253,644✔
1545
  (*pAppHbMgr)->reportBytes = 0;
1,253,644✔
1546
  (*pAppHbMgr)->key = taosStrdup(key);
1,253,644✔
1547
  if ((*pAppHbMgr)->key == NULL) {
1,253,644✔
1548
    TSC_ERR_JRET(terrno);
×
1549
  }
1550

1551
  // init app info
1552
  (*pAppHbMgr)->pAppInstInfo = pAppInstInfo;
1,253,644✔
1553

1554
  // init hash info
1555
  (*pAppHbMgr)->activeInfo = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
1,253,644✔
1556

1557
  if ((*pAppHbMgr)->activeInfo == NULL) {
1,253,644✔
1558
    TSC_ERR_JRET(terrno);
×
1559
  }
1560

1561
  // taosHashSetFreeFp(pAppHbMgr->activeInfo, tFreeClientHbReq);
1562

1563
  TSC_ERR_JRET(taosThreadMutexLock(&clientHbMgr.lock));
1,253,644✔
1564
  if (taosArrayPush(clientHbMgr.appHbMgrs, &(*pAppHbMgr)) == NULL) {
2,507,288✔
1565
    code = terrno;
×
1566
    (void)taosThreadMutexUnlock(&clientHbMgr.lock);
×
1567
    goto _return;
×
1568
  }
1569
  (*pAppHbMgr)->idx = taosArrayGetSize(clientHbMgr.appHbMgrs) - 1;
1,253,644✔
1570
  TSC_ERR_JRET(taosThreadMutexUnlock(&clientHbMgr.lock));
1,253,644✔
1571

1572
  return TSDB_CODE_SUCCESS;
1,253,644✔
1573
_return:
×
1574
  taosMemoryFree(*pAppHbMgr);
×
1575
  return code;
×
1576
}
1577

1578
void hbFreeAppHbMgr(SAppHbMgr *pTarget) {
1,253,644✔
1579
  void *pIter = taosHashIterate(pTarget->activeInfo, NULL);
1,253,644✔
1580
  while (pIter != NULL) {
1,399,925✔
1581
    SClientHbReq *pOneReq = pIter;
146,281✔
1582
    tFreeClientHbReq(pOneReq);
1583
    pIter = taosHashIterate(pTarget->activeInfo, pIter);
146,281✔
1584
  }
1585
  taosHashCleanup(pTarget->activeInfo);
1,253,644✔
1586
  pTarget->activeInfo = NULL;
1,253,644✔
1587

1588
  taosMemoryFree(pTarget->key);
1,253,644✔
1589
  taosMemoryFree(pTarget);
1,253,644✔
1590
}
1,253,644✔
1591

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

1614
void appHbMgrCleanup(void) {
1,195,274✔
1615
  int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
1,195,274✔
1616
  for (int i = 0; i < sz; i++) {
2,448,918✔
1617
    SAppHbMgr *pTarget = taosArrayGetP(clientHbMgr.appHbMgrs, i);
1,253,644✔
1618
    if (pTarget == NULL) continue;
1,253,644✔
1619
    hbFreeAppHbMgr(pTarget);
1,253,644✔
1620
  }
1621
}
1,195,274✔
1622

1623
int32_t hbMgrInit() {
1,253,644✔
1624
  // init once
1625
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 0, 1);
1,253,644✔
1626
  if (old == 1) return 0;
1,253,644✔
1627

1628
  clientHbMgr.appId = tGenIdPI64();
1,195,274✔
1629
  tscInfo("app initialized, appId:0x%" PRIx64, clientHbMgr.appId);
1,195,274✔
1630

1631
  clientHbMgr.appSummary = taosHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
1,195,274✔
1632
  if (NULL == clientHbMgr.appSummary) {
1,195,274✔
1633
    uError("hbMgrInit:taosHashInit error") return terrno;
×
1634
  }
1635
  clientHbMgr.appHbMgrs = taosArrayInit(0, sizeof(void *));
1,195,274✔
1636
  if (NULL == clientHbMgr.appHbMgrs) {
1,195,274✔
1637
    uError("hbMgrInit:taosArrayInit error") return terrno;
×
1638
  }
1639
  TdThreadMutexAttr attr = {0};
1,195,274✔
1640

1641
  int ret = taosThreadMutexAttrInit(&attr);
1,195,274✔
1642
  if (ret != 0) {
1,195,274✔
1643
    uError("hbMgrInit:taosThreadMutexAttrInit error") return ret;
×
1644
  }
1645

1646
  ret = taosThreadMutexAttrSetType(&attr, PTHREAD_MUTEX_RECURSIVE);
1,195,274✔
1647
  if (ret != 0) {
1,195,274✔
1648
    uError("hbMgrInit:taosThreadMutexAttrSetType error") return ret;
×
1649
  }
1650

1651
  ret = taosThreadMutexInit(&clientHbMgr.lock, &attr);
1,195,274✔
1652
  if (ret != 0) {
1,195,274✔
1653
    uError("hbMgrInit:taosThreadMutexInit error") return ret;
×
1654
  }
1655

1656
  ret = taosThreadMutexAttrDestroy(&attr);
1,195,274✔
1657
  if (ret != 0) {
1,195,274✔
1658
    uError("hbMgrInit:taosThreadMutexAttrDestroy error") return ret;
×
1659
  }
1660

1661
  // init handle funcs
1662
  hbMgrInitHandle();
1663

1664
  // init backgroud thread
1665
  ret = hbCreateThread();
1,195,274✔
1666
  if (ret != 0) {
1,195,274✔
1667
    uError("hbMgrInit:hbCreateThread error") return ret;
×
1668
  }
1669

1670
  return 0;
1,195,274✔
1671
}
1672

1673
void hbMgrCleanUp() {
1,196,350✔
1674
  hbStopThread();
1,196,350✔
1675

1676
  // destroy all appHbMgr
1677
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 1, 0);
1,196,350✔
1678
  if (old == 0) return;
1,196,350✔
1679

1680
  int32_t code = taosThreadMutexLock(&clientHbMgr.lock);
1,195,274✔
1681
  if (TSDB_CODE_SUCCESS != code) {
1,195,274✔
1682
    tscError("failed to lock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1683
  }
1684
  appHbMgrCleanup();
1,195,274✔
1685
  taosArrayDestroy(clientHbMgr.appHbMgrs);
1,195,274✔
1686
  clientHbMgr.appHbMgrs = NULL;
1,195,274✔
1687
  code = taosThreadMutexUnlock(&clientHbMgr.lock);
1,195,274✔
1688
  if (TSDB_CODE_SUCCESS != code) {
1,195,274✔
1689
    tscError("failed to unlock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1690
  }
1691
}
1692

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

1706
  TSC_ERR_RET(taosHashPut(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey), &hbReq, sizeof(SClientHbReq)));
2,647,391✔
1707

1708
  (void)atomic_add_fetch_32(&pAppHbMgr->connKeyCnt, 1);
2,647,391✔
1709
  return 0;
2,647,391✔
1710
}
1711

1712
int32_t hbRegisterConn(SAppHbMgr *pAppHbMgr, int64_t tscRefId, const char* user, const char* tokenName, int64_t clusterId, int8_t connType) {
2,697,106✔
1713
  SClientHbKey connKey = {
2,697,106✔
1714
      .tscRid = tscRefId,
1715
      .connType = connType,
1716
  };
1717

1718
  switch (connType) {
2,697,106✔
1719
    case CONN_TYPE__QUERY: {
2,647,391✔
1720
      return hbRegisterConnImpl(pAppHbMgr, connKey, user, tokenName, clusterId);
2,647,391✔
1721
    }
1722
    case CONN_TYPE__TMQ: {
49,715✔
1723
      return 0;
49,715✔
1724
    }
1725
    default:
×
1726
      return 0;
×
1727
  }
1728
}
1729

1730
void hbDeregisterConn(STscObj *pTscObj, SClientHbKey connKey) {
2,562,971✔
1731
  int32_t code = taosThreadMutexLock(&clientHbMgr.lock);
2,562,971✔
1732
  if (TSDB_CODE_SUCCESS != code) {
2,564,177✔
1733
    tscError("failed to lock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1734
  }
1735
  SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, pTscObj->appHbMgrIdx);
2,564,177✔
1736
  if (pAppHbMgr) {
2,564,177✔
1737
    SClientHbReq *pReq = taosHashAcquire(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
2,564,177✔
1738
    if (pReq) {
2,564,177✔
1739
      tFreeClientHbReq(pReq);
1740
      code = taosHashRemove(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
2,501,110✔
1741
      if (TSDB_CODE_SUCCESS != code) {
2,501,110✔
1742
        tscError("hbDeregisterConn taosHashRemove error, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1743
      }
1744
      taosHashRelease(pAppHbMgr->activeInfo, pReq);
2,501,110✔
1745
      (void)atomic_sub_fetch_32(&pAppHbMgr->connKeyCnt, 1);
2,501,110✔
1746
    }
1747
  }
1748
  code = taosThreadMutexUnlock(&clientHbMgr.lock);
2,564,177✔
1749
  if (TSDB_CODE_SUCCESS != code) {
2,564,177✔
1750
    tscError("failed to unlock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1751
  }
1752
}
2,564,177✔
1753

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