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

taosdata / TDengine / #4910

30 Dec 2025 10:52AM UTC coverage: 65.864% (+0.3%) from 65.542%
#4910

push

travis-ci

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

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

999 existing lines in 108 files now uncovered.

194877 of 295877 relevant lines covered (65.86%)

121300574.4 hits per line

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

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

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

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

36
SClientHbMgr clientHbMgr = {0};
37

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

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

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

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

61
    TSC_ERR_JRET(catalogUpdateUserAuthInfo(pCatalog, rsp));
784,360✔
62

63
  }
64

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

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

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

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

83
  SUserSessCfg cfg = {0, 0, 0, 0, 0};
1,030,663✔
84

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

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

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

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

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

114
  if (pCfg->sessMaxCallVnodeNum != 0) {
1,030,663✔
115
    code = sessMgtUpdataLimit((char *)user, SESSION_MAX_CALL_VNODE_NUM, pCfg->sessMaxCallVnodeNum);
1,030,663✔
116
    TAOS_CHECK_GOTO(code, &lino, _error);
1,030,663✔
117
  }
118
_error:
1,030,663✔
119
  return code;
1,030,663✔
120
}
121
static int32_t hbUpdateUserAuthInfo(SAppHbMgr *pAppHbMgr, SUserAuthBatchRsp *batchRsp) {
750,014✔
122
  int32_t code = 0;
750,014✔
123
  int64_t clusterId = pAppHbMgr->pAppInstInfo->clusterId;
750,014✔
124
  for (int i = 0; i < TARRAY_SIZE(clientHbMgr.appHbMgrs); ++i) {
1,692,692✔
125
    SAppHbMgr *hbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
942,678✔
126
    if (!hbMgr || hbMgr->pAppInstInfo->clusterId != clusterId) {
942,678✔
127
      continue;
88✔
128
    }
129

130
    SClientHbReq    *pReq = NULL;
942,590✔
131
    SGetUserAuthRsp *pRsp = NULL;
942,590✔
132
    while ((pReq = taosHashIterate(hbMgr->activeInfo, pReq))) {
2,167,219✔
133
      STscObj *pTscObj = (STscObj *)acquireTscObj(pReq->connKey.tscRid);
1,252,205✔
134
      if (!pTscObj) {
1,252,205✔
135
        continue;
192,560✔
136
      }
137

138
      if (!pRsp) {
1,059,645✔
139
        for (int32_t j = 0; j < TARRAY_SIZE(batchRsp->pArray); ++j) {
913,414✔
140
          SGetUserAuthRsp *rsp = TARRAY_GET_ELEM(batchRsp->pArray, j);
885,838✔
141
          if (0 == strncmp(rsp->user, pTscObj->user, TSDB_USER_LEN)) {
885,838✔
142
            pRsp = rsp;
842,512✔
143
            break;
842,512✔
144
          }
145
        }
146
        if (!pRsp) {
870,088✔
147
          releaseTscObj(pReq->connKey.tscRid);
27,576✔
148
          taosHashCancelIterate(hbMgr->activeInfo, pReq);
27,576✔
149
          break;
27,576✔
150
        }
151
      }
152

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

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

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

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

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

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

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

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

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

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

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

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

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

284
  int32_t numOfBatchs = taosArrayGetSize(batchRsp.pArray);
5,855,643✔
285
  for (int32_t i = 0; i < numOfBatchs; ++i) {
7,175,309✔
286
    SDbHbRsp *rsp = taosArrayGet(batchRsp.pArray, i);
1,319,666✔
287
    if (NULL == rsp) {
1,319,666✔
288
      code = terrno;
×
289
      goto _return;
×
290
    }
291
    if (rsp->useDbRsp) {
1,319,666✔
292
      tscDebug("hb use db rsp, db:%s, vgVersion:%d, stateTs:%" PRId64 ", uid:%" PRIx64, rsp->useDbRsp->db,
1,061,460✔
293
               rsp->useDbRsp->vgVersion, rsp->useDbRsp->stateTs, rsp->useDbRsp->uid);
294

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

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

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

309
        if (IS_SYS_DBNAME(rsp->useDbRsp->db)) {
1,027,139✔
310
          code = hbGenerateVgInfoFromRsp(&vgInfo, rsp->useDbRsp);
231,315✔
311
          if (TSDB_CODE_SUCCESS != code) {
231,315✔
312
            goto _return;
×
313
          }
314

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

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

343
_return:
5,855,643✔
344

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

349
static int32_t hbProcessStbInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
4,190,413✔
350
  int32_t code = TSDB_CODE_SUCCESS;
4,190,413✔
351

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

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

376
      TSC_ERR_JRET(catalogAsyncUpdateTableMeta(pCatalog, rsp));
19,003✔
377
    }
378
  }
379

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

390
_return:
4,190,413✔
391
  taosArrayDestroy(hbRsp.pIndexRsp);
4,190,413✔
392
  hbRsp.pIndexRsp = NULL;
4,190,413✔
393

394
  tFreeSSTbHbRsp(&hbRsp);
4,190,413✔
395
  return code;
4,190,413✔
396
}
397

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

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

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

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

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

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

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

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

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

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

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

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

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

564
  if (pRsp->query) {
24,057,313✔
565
    STscObj *pTscObj = (STscObj *)acquireTscObj(pRsp->connKey.tscRid);
24,057,313✔
566
    if (NULL == pTscObj) {
24,057,313✔
567
      tscDebug("tscObj rid %" PRIx64 " not exist", pRsp->connKey.tscRid);
711✔
568
    } else {
569
      if (pRsp->query->totalDnodes > 1) {
24,056,602✔
570
        SEpSet  originEpset = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
6,302,260✔
571
        if (!isEpsetEqual(&originEpset, &pRsp->query->epSet)) {
6,302,260✔
572
          SEpSet *pOrig = &originEpset;
22,982✔
573
          SEp    *pOrigEp = &pOrig->eps[pOrig->inUse];
22,982✔
574
          SEp    *pNewEp = &pRsp->query->epSet.eps[pRsp->query->epSet.inUse];
22,982✔
575
          tscDebug("mnode epset updated from %d/%d=>%s:%d to %d/%d=>%s:%d in hb", pOrig->inUse, pOrig->numOfEps,
22,982✔
576
                   pOrigEp->fqdn, pOrigEp->port, pRsp->query->epSet.inUse, pRsp->query->epSet.numOfEps, pNewEp->fqdn,
577
                   pNewEp->port);
578

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

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

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

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

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

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

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

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

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

628
  taosHashRelease(pAppHbMgr->activeInfo, pReq);
24,057,313✔
629

630
  return TSDB_CODE_SUCCESS;
24,057,313✔
631
}
632

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

638
  static int32_t    emptyRspNum = 0;
639
  int32_t           idx = *(int32_t *)param;
20,606,649✔
640
  SClientHbBatchRsp pRsp = {0};
20,606,903✔
641
  if (TSDB_CODE_SUCCESS == code) {
20,607,277✔
642
    code = tDeserializeSClientHbBatchRsp(pMsg->pData, pMsg->len, &pRsp);
20,439,582✔
643
    if (TSDB_CODE_SUCCESS != code) {
20,435,394✔
644
      tscError("deserialize hb rsp failed");
56✔
645
    }
646
    int32_t now = taosGetTimestampSec();
20,435,394✔
647
    int32_t delta = abs(now - pRsp.svrTimestamp);
20,435,821✔
648
    if (delta > tsTimestampDeltaLimit) {
20,435,821✔
649
      code = TSDB_CODE_TIME_UNSYNCED;
56✔
650
      tscError("time diff:%ds is too big", delta);
56✔
651
    }
652
  }
653

654
  int32_t rspNum = taosArrayGetSize(pRsp.rsps);
20,603,516✔
655

656
  (void)taosThreadMutexLock(&clientHbMgr.lock);
20,604,951✔
657

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

668
  SAppInstInfo *pInst = pAppHbMgr->pAppInstInfo;
20,607,277✔
669

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

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

690
  if (rspNum) {
20,439,526✔
691
    tscDebug("hb got %d rsp, %d empty rsp received before", rspNum,
19,958,441✔
692
             atomic_val_compare_exchange_32(&emptyRspNum, emptyRspNum, 0));
693
  } else {
694
    (void)atomic_add_fetch_32(&emptyRspNum, 1);
481,085✔
695
  }
696

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

705
  (void)taosThreadMutexUnlock(&clientHbMgr.lock);
20,439,526✔
706

707
  tFreeClientHbBatchRsp(&pRsp);
708

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

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

720
  void *pIter = taosHashIterate(pObj->pRequests, NULL);
21,597,270✔
721
  while (pIter != NULL) {
43,720,140✔
722
    int64_t     *rid = pIter;
22,122,870✔
723
    SRequestObj *pRequest = acquireRequest(*rid);
22,122,870✔
724
    if (NULL == pRequest) {
22,122,870✔
725
      pIter = taosHashIterate(pObj->pRequests, pIter);
16,557✔
726
      continue;
16,557✔
727
    }
728

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

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

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

757
      code = schedulerGetTasksStatus(pRequest->body.queryJob, desc.subDesc);
13,768,108✔
758
      if (code) {
13,768,108✔
759
        taosArrayDestroy(desc.subDesc);
4,029,946✔
760
        desc.subDesc = NULL;
4,029,946✔
761
        code = TSDB_CODE_SUCCESS;
4,029,946✔
762
      }
763
      desc.subPlanNum = taosArrayGetSize(desc.subDesc);
13,768,108✔
764
    } else {
765
      desc.subDesc = NULL;
×
766
    }
767

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

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

777
  return code;
21,597,270✔
778
}
779

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

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

794
  hbBasic->connId = pTscObj->connId;
24,240,714✔
795

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

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

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

822
  req->query = hbBasic;
21,597,270✔
823
  releaseTscObj(connKey->tscRid);
21,597,270✔
824

825
  return TSDB_CODE_SUCCESS;
21,597,270✔
826
}
827

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

835
  int32_t code = 0;
649,974✔
836

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

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

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

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

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

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

898
  return code;
649,974✔
899
}
900

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

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

911
  if (userNum <= 0) {
18,595,205✔
912
    taosMemoryFree(users);
184,288✔
913
    return TSDB_CODE_SUCCESS;
184,288✔
914
  }
915

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

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

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

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

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

943
  return TSDB_CODE_SUCCESS;
18,410,917✔
944
}
945

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

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

956
  if (dbNum <= 0) {
20,133,274✔
957
    taosMemoryFree(dbs);
14,252,766✔
958
    return TSDB_CODE_SUCCESS;
14,252,766✔
959
  }
960

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

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

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

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

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

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

997
  return TSDB_CODE_SUCCESS;
5,880,508✔
998
}
999

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

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

1010
  if (stbNum <= 0) {
20,133,274✔
1011
    taosMemoryFree(stbs);
15,928,448✔
1012
    return TSDB_CODE_SUCCESS;
15,928,448✔
1013
  }
1014

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

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

1029
  tscDebug("hb got %d expired stb, valueLen:%d", stbNum, kv.valueLen);
4,204,826✔
1030

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

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

1045
  return TSDB_CODE_SUCCESS;
4,204,826✔
1046
}
1047

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

1054
  TSC_ERR_JRET(catalogGetExpiredViews(pCatalog, &views, &viewNum, &pDynViewVer));
20,133,274✔
1055

1056
  if (viewNum <= 0) {
20,133,274✔
1057
    taosMemoryFree(views);
20,130,644✔
1058
    taosMemoryFree(pDynViewVer);
20,130,644✔
1059
    return TSDB_CODE_SUCCESS;
20,130,644✔
1060
  }
1061

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

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

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

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

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

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

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

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

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

1109
  if (tsmaNum <= 0) {
20,133,274✔
1110
    taosMemoryFree(tsmas);
20,092,389✔
1111
    return TSDB_CODE_SUCCESS;
20,092,389✔
1112
  }
1113

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

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

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

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

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

1151
  return TSDB_CODE_SUCCESS;
24,240,714✔
1152
}
1153

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

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

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

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

1178
    if (!taosHashGet(clientHbMgr.appHbHash, &hbParam->clusterId, sizeof(hbParam->clusterId))) {
20,133,274✔
1179
      code = hbGetExpiredUserInfo(connKey, pCatalog, req);
18,595,205✔
1180
      if (TSDB_CODE_SUCCESS != code) {
18,595,205✔
1181
        tscWarn("hbGetExpiredUserInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1182
        return code;
×
1183
      }
1184
      if (clientHbMgr.appHbHash) {
18,595,205✔
1185
        code = taosHashPut(clientHbMgr.appHbHash, &hbParam->clusterId, sizeof(uint64_t), NULL, 0);
2,500,832✔
1186
        if (TSDB_CODE_SUCCESS != code) {
2,500,832✔
1187
          tscWarn("hbQueryHbReqHandle put clusterId failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId,
×
1188
                  tstrerror(code));
1189
          return code;
×
1190
        }
1191
      }
1192
    }
1193

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

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

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

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

1236
  ++hbParam->reqCnt;  // success to get catalog info
24,240,714✔
1237

1238
  return TSDB_CODE_SUCCESS;
24,240,714✔
1239
}
1240

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

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

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

1262
  int64_t  maxIpWhiteVer = 0;
20,615,448✔
1263
  void    *pIter = NULL;
20,615,448✔
1264
  SHbParam param = {0};
20,615,448✔
1265
  while ((pIter = taosHashIterate(pAppHbMgr->activeInfo, pIter))) {
46,124,612✔
1266
    SClientHbReq *pOneReq = pIter;
25,509,164✔
1267
    SClientHbKey *connKey = &pOneReq->connKey;
25,509,164✔
1268
    STscObj      *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
25,509,164✔
1269

1270
    if (!pTscObj || atomic_load_8(&pTscObj->dropped) == 1) {
25,509,164✔
1271
      if (pTscObj) releaseTscObj(connKey->tscRid);
1,268,450✔
1272
      continue;
1,268,450✔
1273
    }
1274

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

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

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

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

1315
  return TSDB_CODE_SUCCESS;
20,615,448✔
1316
}
1317

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

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

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

1342
  taosHashClear(clientHbMgr.appSummary);
19,906,485✔
1343

1344
  for (int32_t i = 0; i < sz; ++i) {
42,476,901✔
1345
    SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
22,570,416✔
1346
    if (pAppHbMgr == NULL) continue;
22,570,416✔
1347

1348
    int64_t    clusterId = pAppHbMgr->pAppInstInfo->clusterId;
22,570,416✔
1349
    SAppHbReq *pApp = taosHashGet(clientHbMgr.appSummary, &clusterId, sizeof(clusterId));
22,570,416✔
1350
    if (NULL == pApp) {
22,570,416✔
1351
      (void)memcpy(&req.summary, &pAppHbMgr->pAppInstInfo->summary, sizeof(req.summary));
19,907,453✔
1352
      req.startTime = pAppHbMgr->startTime;
19,907,453✔
1353
      TSC_ERR_RET(taosHashPut(clientHbMgr.appSummary, &clusterId, sizeof(clusterId), &req, sizeof(req)));
19,907,453✔
1354
    } else {
1355
      if (pAppHbMgr->startTime < pApp->startTime) {
2,662,963✔
1356
        pApp->startTime = pAppHbMgr->startTime;
×
1357
      }
1358

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

1363
  return TSDB_CODE_SUCCESS;
19,906,485✔
1364
}
1365

1366
static void *hbThreadFunc(void *param) {
1,114,654✔
1367
  setThreadName("hb");
1,114,654✔
1368
#ifdef WINDOWS
1369
  if (taosCheckCurrentInDll()) {
1370
    atexit(hbThreadFuncUnexpectedStopped);
1371
  }
1372
#endif
1373
  while (1) {
19,215,651✔
1374
    if (1 == clientHbMgr.threadStop) {
20,330,305✔
1375
      break;
420,739✔
1376
    }
1377

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

1383
    int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
19,909,566✔
1384
    if (sz > 0) {
19,909,566✔
1385
      if (TSDB_CODE_SUCCESS != hbGatherAppInfo()) {
19,906,485✔
1386
        tscError("hbGatherAppInfo failed");
×
1387
        return NULL;
×
1388
      }
1389
      if (sz > 1 && !clientHbMgr.appHbHash) {
19,906,485✔
1390
        clientHbMgr.appHbHash = taosHashInit(0, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), true, HASH_NO_LOCK);
40,515✔
1391
        if (NULL == clientHbMgr.appHbHash) {
40,515✔
1392
          tscError("taosHashInit failed");
×
1393
          return NULL;
×
1394
        }
1395
      }
1396
      taosHashClear(clientHbMgr.appHbHash);
19,906,485✔
1397
    }
1398

1399
    for (int i = 0; i < sz; i++) {
42,479,982✔
1400
      SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
22,570,416✔
1401
      if (pAppHbMgr == NULL) {
22,570,416✔
1402
        continue;
18,707✔
1403
      }
1404

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

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

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

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

1468
    if (TSDB_CODE_SUCCESS != taosThreadMutexUnlock(&clientHbMgr.lock)) {
19,909,566✔
1469
      tscError("taosThreadMutexLock failed");
×
1470
      return NULL;
×
1471
    }
1472
    taosMsleep(HEARTBEAT_INTERVAL);
19,909,566✔
1473
  }
1474
  taosHashCleanup(clientHbMgr.appHbHash);
420,739✔
1475
  return NULL;
420,739✔
1476
}
1477

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

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

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

1499
  return code;
1,114,654✔
1500
}
1501

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

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

1525
  tscDebug("hb thread stopped");
1,114,654✔
1526
}
1527

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

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

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

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

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

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

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

1573
void hbFreeAppHbMgr(SAppHbMgr *pTarget) {
1,166,513✔
1574
  void *pIter = taosHashIterate(pTarget->activeInfo, NULL);
1,166,513✔
1575
  while (pIter != NULL) {
1,325,323✔
1576
    SClientHbReq *pOneReq = pIter;
158,810✔
1577
    tFreeClientHbReq(pOneReq);
1578
    pIter = taosHashIterate(pTarget->activeInfo, pIter);
158,810✔
1579
  }
1580
  taosHashCleanup(pTarget->activeInfo);
1,166,513✔
1581
  pTarget->activeInfo = NULL;
1,166,513✔
1582

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

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

1609
void appHbMgrCleanup(void) {
1,114,654✔
1610
  int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
1,114,654✔
1611
  for (int i = 0; i < sz; i++) {
2,281,167✔
1612
    SAppHbMgr *pTarget = taosArrayGetP(clientHbMgr.appHbMgrs, i);
1,166,513✔
1613
    if (pTarget == NULL) continue;
1,166,513✔
1614
    hbFreeAppHbMgr(pTarget);
1,166,513✔
1615
  }
1616
}
1,114,654✔
1617

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

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

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

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

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

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

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

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

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

1665
  return 0;
1,114,654✔
1666
}
1667

1668
void hbMgrCleanUp() {
1,114,960✔
1669
  hbStopThread();
1,114,960✔
1670

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

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

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

1701
  TSC_ERR_RET(taosHashPut(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey), &hbReq, sizeof(SClientHbReq)));
1,937,252✔
1702

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

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

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

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

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