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

taosdata / TDengine / #3608

12 Feb 2025 05:57AM UTC coverage: 63.066% (+1.4%) from 61.715%
#3608

push

travis-ci

web-flow
Merge pull request #29746 from taosdata/merge/mainto3.02

merge: from main to 3.0 branch

140199 of 286257 branches covered (48.98%)

Branch coverage included in aggregate %.

89 of 161 new or added lines in 18 files covered. (55.28%)

3211 existing lines in 190 files now uncovered.

218998 of 283298 relevant lines covered (77.3%)

5949310.66 hits per line

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

61.03
/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 "scheduler.h"
21
#include "tglobal.h"
22
#include "trpc.h"
23

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

35
SClientHbMgr clientHbMgr = {0};
36

37
static int32_t hbCreateThread();
38
static void    hbStopThread();
39
static int32_t hbUpdateUserAuthInfo(SAppHbMgr *pAppHbMgr, SUserAuthBatchRsp *batchRsp);
40

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

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

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

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

54
  int32_t numOfBatchs = taosArrayGetSize(batchRsp.pArray);
2,599✔
55
  for (int32_t i = 0; i < numOfBatchs; ++i) {
5,358✔
56
    SGetUserAuthRsp *rsp = taosArrayGet(batchRsp.pArray, i);
2,759✔
57
    if (NULL == rsp) {
2,759!
58
      code = terrno;
×
59
      goto _return;
×
60
    }
61
    tscDebug("hb to update user auth, user:%s, version:%d", rsp->user, rsp->version);
2,759✔
62

63
    TSC_ERR_JRET(catalogUpdateUserAuthInfo(pCatalog, rsp));
2,759!
64
  }
65

66
  if (numOfBatchs > 0) {
2,599!
67
    TSC_ERR_JRET(hbUpdateUserAuthInfo(pAppHbMgr, &batchRsp));
2,599!
68
  }
69

70
  (void)atomic_val_compare_exchange_8(&pAppHbMgr->connHbFlag, 1, 2);
2,599✔
71

72
_return:
2,599✔
73
  taosArrayDestroy(batchRsp.pArray);
2,599✔
74
  return code;
2,599✔
75
}
76

77
static int32_t hbUpdateUserAuthInfo(SAppHbMgr *pAppHbMgr, SUserAuthBatchRsp *batchRsp) {
2,599✔
78
  int64_t clusterId = pAppHbMgr->pAppInstInfo->clusterId;
2,599✔
79
  for (int i = 0; i < TARRAY_SIZE(clientHbMgr.appHbMgrs); ++i) {
8,310✔
80
    SAppHbMgr *hbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
5,711✔
81
    if (!hbMgr || hbMgr->pAppInstInfo->clusterId != clusterId) {
5,711!
82
      continue;
2✔
83
    }
84

85
    SClientHbReq    *pReq = NULL;
5,709✔
86
    SGetUserAuthRsp *pRsp = NULL;
5,709✔
87
    while ((pReq = taosHashIterate(hbMgr->activeInfo, pReq))) {
11,073✔
88
      STscObj *pTscObj = (STscObj *)acquireTscObj(pReq->connKey.tscRid);
6,750✔
89
      if (!pTscObj) {
6,750✔
90
        continue;
684✔
91
      }
92

93
      if (!pRsp) {
6,066✔
94
        for (int32_t j = 0; j < TARRAY_SIZE(batchRsp->pArray); ++j) {
5,782✔
95
          SGetUserAuthRsp *rsp = TARRAY_GET_ELEM(batchRsp->pArray, j);
4,396✔
96
          if (0 == strncmp(rsp->user, pTscObj->user, TSDB_USER_LEN)) {
4,396✔
97
            pRsp = rsp;
2,837✔
98
            break;
2,837✔
99
          }
100
        }
101
        if (!pRsp) {
4,223✔
102
          releaseTscObj(pReq->connKey.tscRid);
1,386✔
103
          taosHashCancelIterate(hbMgr->activeInfo, pReq);
1,386✔
104
          break;
1,386✔
105
        }
106
      }
107

108
      if (pRsp->dropped == 1) {
4,680✔
109
        if (atomic_val_compare_exchange_8(&pTscObj->dropped, 0, 1) == 0) {
129✔
110
          if (pTscObj->userDroppedInfo.fp) {
105✔
111
            SPassInfo *dropInfo = &pTscObj->userDroppedInfo;
100✔
112
            if (dropInfo->fp) {
100!
113
              (*dropInfo->fp)(dropInfo->param, NULL, TAOS_NOTIFY_USER_DROPPED);
100✔
114
            }
115
          }
116
        }
117
        releaseTscObj(pReq->connKey.tscRid);
129✔
118
        continue;
129✔
119
      }
120

121
      pTscObj->authVer = pRsp->version;
4,551✔
122

123
      if (pTscObj->sysInfo != pRsp->sysInfo) {
4,551✔
124
        tscDebug("update sysInfo of user %s from %" PRIi8 " to %" PRIi8 ", tscRid:%" PRIi64, pRsp->user,
206✔
125
                 pTscObj->sysInfo, pRsp->sysInfo, pTscObj->id);
126
        pTscObj->sysInfo = pRsp->sysInfo;
206✔
127
      }
128

129
      if (pTscObj->passInfo.fp) {
4,551✔
130
        SPassInfo *passInfo = &pTscObj->passInfo;
40✔
131
        int32_t    oldVer = atomic_load_32(&passInfo->ver);
40✔
132
        if (oldVer < pRsp->passVer) {
40!
133
          atomic_store_32(&passInfo->ver, pRsp->passVer);
40✔
134
          if (passInfo->fp) {
40!
135
            (*passInfo->fp)(passInfo->param, &pRsp->passVer, TAOS_NOTIFY_PASSVER);
40✔
136
          }
137
          tscDebug("update passVer of user %s from %d to %d, tscRid:%" PRIi64, pRsp->user, oldVer,
40!
138
                   atomic_load_32(&passInfo->ver), pTscObj->id);
139
        }
140
      }
141

142
      if (pTscObj->whiteListInfo.fp) {
4,551✔
143
        SWhiteListInfo *whiteListInfo = &pTscObj->whiteListInfo;
10✔
144
        int64_t         oldVer = atomic_load_64(&whiteListInfo->ver);
10✔
145
        if (oldVer != pRsp->whiteListVer) {
10!
146
          atomic_store_64(&whiteListInfo->ver, pRsp->whiteListVer);
10✔
147
          if (whiteListInfo->fp) {
10!
148
            (*whiteListInfo->fp)(whiteListInfo->param, &pRsp->whiteListVer, TAOS_NOTIFY_WHITELIST_VER);
10✔
149
          }
150
          tscDebug("update whitelist version of user %s from %" PRId64 " to %" PRId64 ", tscRid:%" PRIi64, pRsp->user,
10!
151
                   oldVer, atomic_load_64(&whiteListInfo->ver), pTscObj->id);
152
        }
153
      } else {
154
        // Need to update version information to prevent frequent fetching of authentication
155
        // information.
156
        SWhiteListInfo *whiteListInfo = &pTscObj->whiteListInfo;
4,541✔
157
        int64_t         oldVer = atomic_load_64(&whiteListInfo->ver);
4,541✔
158
        atomic_store_64(&whiteListInfo->ver, pRsp->whiteListVer);
4,541✔
159
        tscDebug("update whitelist version of user %s from %" PRId64 " to %" PRId64 ", tscRid:%" PRIi64, pRsp->user,
4,541✔
160
                 oldVer, atomic_load_64(&whiteListInfo->ver), pTscObj->id);
161
      }
162
      releaseTscObj(pReq->connKey.tscRid);
4,551✔
163
    }
164
  }
165
  return 0;
2,599✔
166
}
167

168
static int32_t hbGenerateVgInfoFromRsp(SDBVgInfo **pInfo, SUseDbRsp *rsp) {
3,181✔
169
  int32_t    code = 0;
3,181✔
170
  SDBVgInfo *vgInfo = taosMemoryCalloc(1, sizeof(SDBVgInfo));
3,181!
171
  if (NULL == vgInfo) {
3,181!
172
    return terrno;
×
173
  }
174

175
  vgInfo->vgVersion = rsp->vgVersion;
3,181✔
176
  vgInfo->stateTs = rsp->stateTs;
3,181✔
177
  vgInfo->hashMethod = rsp->hashMethod;
3,181✔
178
  vgInfo->hashPrefix = rsp->hashPrefix;
3,181✔
179
  vgInfo->hashSuffix = rsp->hashSuffix;
3,181✔
180
  vgInfo->vgHash = taosHashInit(rsp->vgNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
3,181✔
181
  if (NULL == vgInfo->vgHash) {
3,181!
182
    tscError("hash init[%d] failed", rsp->vgNum);
×
183
    code = terrno;
×
184
    goto _return;
×
185
  }
186

187
  for (int32_t j = 0; j < rsp->vgNum; ++j) {
11,487✔
188
    SVgroupInfo *pInfo = taosArrayGet(rsp->pVgroupInfos, j);
8,306✔
189
    if (taosHashPut(vgInfo->vgHash, &pInfo->vgId, sizeof(int32_t), pInfo, sizeof(SVgroupInfo)) != 0) {
8,306!
190
      tscError("hash push failed, errno:%d", errno);
×
191
      code = terrno;
×
192
      goto _return;
×
193
    }
194
  }
195

196
_return:
3,181✔
197
  if (code) {
3,181!
198
    taosHashCleanup(vgInfo->vgHash);
×
199
    taosMemoryFreeClear(vgInfo);
×
200
  }
201

202
  *pInfo = vgInfo;
3,181✔
203
  return code;
3,181✔
204
}
205

206
static int32_t hbProcessDBInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
9,031✔
207
  int32_t code = 0;
9,031✔
208

209
  SDbHbBatchRsp batchRsp = {0};
9,031✔
210
  if (tDeserializeSDbHbBatchRsp(value, valueLen, &batchRsp) != 0) {
9,031!
211
    terrno = TSDB_CODE_INVALID_MSG;
×
212
    code = terrno;
×
213
    goto _return;
×
214
  }
215

216
  int32_t numOfBatchs = taosArrayGetSize(batchRsp.pArray);
9,031✔
217
  for (int32_t i = 0; i < numOfBatchs; ++i) {
12,679✔
218
    SDbHbRsp *rsp = taosArrayGet(batchRsp.pArray, i);
3,648✔
219
    if (NULL == rsp) {
3,648!
220
      code = terrno;
×
221
      goto _return;
×
222
    }
223
    if (rsp->useDbRsp) {
3,648✔
224
      tscDebug("hb use db rsp, db:%s, vgVersion:%d, stateTs:%" PRId64 ", uid:%" PRIx64, rsp->useDbRsp->db,
2,859✔
225
               rsp->useDbRsp->vgVersion, rsp->useDbRsp->stateTs, rsp->useDbRsp->uid);
226

227
      if (rsp->useDbRsp->vgVersion < 0) {
2,859✔
228
        tscDebug("hb to remove db, db:%s", rsp->useDbRsp->db);
193✔
229
        code = catalogRemoveDB(pCatalog, rsp->useDbRsp->db, rsp->useDbRsp->uid);
193✔
230
      } else {
231
        SDBVgInfo *vgInfo = NULL;
2,666✔
232
        code = hbGenerateVgInfoFromRsp(&vgInfo, rsp->useDbRsp);
2,666✔
233
        if (TSDB_CODE_SUCCESS != code) {
2,666!
234
          goto _return;
×
235
        }
236

237
        tscDebug("hb to update db vgInfo, db:%s", rsp->useDbRsp->db);
2,666✔
238

239
        TSC_ERR_JRET(catalogUpdateDBVgInfo(pCatalog, rsp->useDbRsp->db, rsp->useDbRsp->uid, vgInfo));
2,666!
240

241
        if (IS_SYS_DBNAME(rsp->useDbRsp->db)) {
2,666!
242
          code = hbGenerateVgInfoFromRsp(&vgInfo, rsp->useDbRsp);
515✔
243
          if (TSDB_CODE_SUCCESS != code) {
515!
244
            goto _return;
×
245
          }
246

247
          TSC_ERR_JRET(catalogUpdateDBVgInfo(
515!
248
              pCatalog, (rsp->useDbRsp->db[0] == 'i') ? TSDB_PERFORMANCE_SCHEMA_DB : TSDB_INFORMATION_SCHEMA_DB,
249
              rsp->useDbRsp->uid, vgInfo));
250
        }
251
      }
252
    }
253

254
    if (rsp->cfgRsp) {
3,648✔
255
      tscDebug("hb db cfg rsp, db:%s, cfgVersion:%d", rsp->cfgRsp->db, rsp->cfgRsp->cfgVersion);
414✔
256
      code = catalogUpdateDbCfg(pCatalog, rsp->cfgRsp->db, rsp->cfgRsp->dbId, rsp->cfgRsp);
414✔
257
      rsp->cfgRsp = NULL;
414✔
258
    }
259
    if (rsp->pTsmaRsp) {
3,648✔
260
      if (rsp->pTsmaRsp->pTsmas) {
2,079✔
261
        for (int32_t i = 0; i < rsp->pTsmaRsp->pTsmas->size; ++i) {
398✔
262
          STableTSMAInfo *pTsma = taosArrayGetP(rsp->pTsmaRsp->pTsmas, i);
247✔
263
          if (NULL == pTsma) {
247!
264
            TSC_ERR_JRET(TSDB_CODE_OUT_OF_RANGE);
×
265
          }
266
          TSC_ERR_JRET(catalogAsyncUpdateTSMA(pCatalog, &pTsma, rsp->dbTsmaVersion));
247!
267
        }
268
        taosArrayClear(rsp->pTsmaRsp->pTsmas);
151✔
269
      } else {
270
        TSC_ERR_JRET(catalogAsyncUpdateDbTsmaVersion(pCatalog, rsp->dbTsmaVersion, rsp->db, rsp->dbId));
1,928!
271
      }
272
    }
273
  }
274

275
_return:
9,031✔
276

277
  tFreeSDbHbBatchRsp(&batchRsp);
9,031✔
278
  return code;
9,031✔
279
}
280

281
static int32_t hbProcessStbInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
6,179✔
282
  int32_t code = TSDB_CODE_SUCCESS;
6,179✔
283

284
  SSTbHbRsp hbRsp = {0};
6,179✔
285
  if (tDeserializeSSTbHbRsp(value, valueLen, &hbRsp) != 0) {
6,179!
286
    terrno = TSDB_CODE_INVALID_MSG;
×
287
    return -1;
×
288
  }
289

290
  int32_t numOfMeta = taosArrayGetSize(hbRsp.pMetaRsp);
6,179✔
291
  for (int32_t i = 0; i < numOfMeta; ++i) {
6,699✔
292
    STableMetaRsp *rsp = taosArrayGet(hbRsp.pMetaRsp, i);
520✔
293
    if (NULL == rsp) {
520!
294
      code = terrno;
×
295
      goto _return;
×
296
    }
297
    if (rsp->numOfColumns < 0) {
520✔
298
      tscDebug("hb to remove stb, db:%s, stb:%s", rsp->dbFName, rsp->stbName);
499✔
299
      TSC_ERR_JRET(catalogRemoveStbMeta(pCatalog, rsp->dbFName, rsp->dbId, rsp->stbName, rsp->suid));
499!
300
    } else {
301
      tscDebug("hb to update stb, db:%s, stb:%s", rsp->dbFName, rsp->stbName);
21!
302
      if (rsp->pSchemas[0].colId != PRIMARYKEY_TIMESTAMP_COL_ID) {
21!
303
        tscError("invalid colId[%" PRIi16 "] for the first column in table meta rsp msg", rsp->pSchemas[0].colId);
×
304
        tFreeSSTbHbRsp(&hbRsp);
×
305
        return TSDB_CODE_TSC_INVALID_VALUE;
×
306
      }
307

308
      TSC_ERR_JRET(catalogAsyncUpdateTableMeta(pCatalog, rsp));
21!
309
    }
310
  }
311

312
  int32_t numOfIndex = taosArrayGetSize(hbRsp.pIndexRsp);
6,179✔
313
  for (int32_t i = 0; i < numOfIndex; ++i) {
6,179!
314
    STableIndexRsp *rsp = taosArrayGet(hbRsp.pIndexRsp, i);
×
315
    if (NULL == rsp) {
×
316
      code = terrno;
×
317
      goto _return;
×
318
    }
319
    TSC_ERR_JRET(catalogUpdateTableIndex(pCatalog, rsp));
×
320
  }
321

322
_return:
6,179✔
323
  taosArrayDestroy(hbRsp.pIndexRsp);
6,179✔
324
  hbRsp.pIndexRsp = NULL;
6,179✔
325

326
  tFreeSSTbHbRsp(&hbRsp);
6,179✔
327
  return code;
6,179✔
328
}
329

330
static int32_t hbProcessDynViewRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
4✔
331
  return catalogUpdateDynViewVer(pCatalog, (SDynViewVersion *)value);
4✔
332
}
333

334
static void hbFreeSViewMetaInRsp(void *p) {
×
335
  if (NULL == p || NULL == *(void **)p) {
×
336
    return;
×
337
  }
338
  SViewMetaRsp *pRsp = *(SViewMetaRsp **)p;
×
339
  tFreeSViewMetaRsp(pRsp);
×
340
  taosMemoryFreeClear(pRsp);
×
341
}
342

343
static int32_t hbProcessViewInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
4✔
344
  int32_t code = TSDB_CODE_SUCCESS;
4✔
345

346
  SViewHbRsp hbRsp = {0};
4✔
347
  if (tDeserializeSViewHbRsp(value, valueLen, &hbRsp) != 0) {
4!
348
    taosArrayDestroyEx(hbRsp.pViewRsp, hbFreeSViewMetaInRsp);
×
349
    terrno = TSDB_CODE_INVALID_MSG;
×
350
    return -1;
×
351
  }
352

353
  int32_t numOfMeta = taosArrayGetSize(hbRsp.pViewRsp);
4✔
354
  for (int32_t i = 0; i < numOfMeta; ++i) {
4!
355
    SViewMetaRsp *rsp = taosArrayGetP(hbRsp.pViewRsp, i);
×
356
    if (NULL == rsp) {
×
357
      code = terrno;
×
358
      goto _return;
×
359
    }
360
    if (rsp->numOfCols < 0) {
×
361
      tscDebug("hb to remove view, db:%s, view:%s", rsp->dbFName, rsp->name);
×
362
      code = catalogRemoveViewMeta(pCatalog, rsp->dbFName, rsp->dbId, rsp->name, rsp->viewId);
×
363
      tFreeSViewMetaRsp(rsp);
×
364
      taosMemoryFreeClear(rsp);
×
365
    } else {
366
      tscDebug("hb to update view, db:%s, view:%s", rsp->dbFName, rsp->name);
×
367
      code = catalogUpdateViewMeta(pCatalog, rsp);
×
368
    }
369
    TSC_ERR_JRET(code);
×
370
  }
371

372
_return:
4✔
373
  taosArrayDestroy(hbRsp.pViewRsp);
4✔
374
  return code;
4✔
375
}
376

377
static int32_t hbprocessTSMARsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
336✔
378
  int32_t code = 0;
336✔
379

380
  STSMAHbRsp hbRsp = {0};
336✔
381
  if (tDeserializeTSMAHbRsp(value, valueLen, &hbRsp)) {
336!
382
    terrno = TSDB_CODE_INVALID_MSG;
×
383
    return -1;
×
384
  }
385

386
  int32_t numOfTsma = taosArrayGetSize(hbRsp.pTsmas);
336✔
387
  for (int32_t i = 0; i < numOfTsma; ++i) {
342✔
388
    STableTSMAInfo *pTsmaInfo = taosArrayGetP(hbRsp.pTsmas, i);
6✔
389

390
    if (!pTsmaInfo->pFuncs) {
6!
391
      tscDebug("hb to remove tsma: %s.%s", pTsmaInfo->dbFName, pTsmaInfo->name);
6!
392
      code = catalogRemoveTSMA(pCatalog, pTsmaInfo);
6✔
393
      tFreeAndClearTableTSMAInfo(pTsmaInfo);
6✔
394
    } else {
395
      tscDebug("hb to update tsma: %s.%s", pTsmaInfo->dbFName, pTsmaInfo->name);
×
396
      code = catalogUpdateTSMA(pCatalog, &pTsmaInfo);
×
397
      tFreeAndClearTableTSMAInfo(pTsmaInfo);
×
398
    }
399
    TSC_ERR_JRET(code);
6!
400
  }
401

402
_return:
336✔
403
  taosArrayDestroy(hbRsp.pTsmas);
336✔
404
  return code;
336✔
405
}
406

407
static void hbProcessQueryRspKvs(int32_t kvNum, SArray *pKvs, struct SCatalog *pCatalog, SAppHbMgr *pAppHbMgr) {
14,868✔
408
  for (int32_t i = 0; i < kvNum; ++i) {
33,021✔
409
    SKv *kv = taosArrayGet(pKvs, i);
18,153✔
410
    if (NULL == kv) {
18,153!
411
      tscError("invalid hb kv, idx:%d", i);
×
412
      continue;
×
413
    }
414
    switch (kv->key) {
18,153!
415
      case HEARTBEAT_KEY_USER_AUTHINFO: {
2,599✔
416
        if (kv->valueLen <= 0 || NULL == kv->value) {
2,599!
417
          tscError("invalid hb user auth info, len:%d, value:%p", kv->valueLen, kv->value);
×
418
          break;
×
419
        }
420
        if (TSDB_CODE_SUCCESS != hbProcessUserAuthInfoRsp(kv->value, kv->valueLen, pCatalog, pAppHbMgr)) {
2,599!
421
          tscError("process user auth info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
422
          break;
×
423
        }
424
        break;
2,599✔
425
      }
426
      case HEARTBEAT_KEY_DBINFO: {
9,031✔
427
        if (kv->valueLen <= 0 || NULL == kv->value) {
9,031!
428
          tscError("invalid hb db info, len:%d, value:%p", kv->valueLen, kv->value);
×
429
          break;
×
430
        }
431
        if (TSDB_CODE_SUCCESS != hbProcessDBInfoRsp(kv->value, kv->valueLen, pCatalog)) {
9,031!
432
          tscError("process db info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
433
          break;
×
434
        }
435
        break;
9,031✔
436
      }
437
      case HEARTBEAT_KEY_STBINFO: {
6,179✔
438
        if (kv->valueLen <= 0 || NULL == kv->value) {
6,179!
439
          tscError("invalid hb stb info, len:%d, value:%p", kv->valueLen, kv->value);
×
440
          break;
×
441
        }
442
        if (TSDB_CODE_SUCCESS != hbProcessStbInfoRsp(kv->value, kv->valueLen, pCatalog)) {
6,179!
443
          tscError("process stb info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
444
          break;
×
445
        }
446
        break;
6,179✔
447
      }
448
#ifdef TD_ENTERPRISE
449
      case HEARTBEAT_KEY_DYN_VIEW: {
4✔
450
        if (kv->valueLen <= 0 || NULL == kv->value) {
4!
451
          tscError("invalid dyn view info, len:%d, value:%p", kv->valueLen, kv->value);
×
452
          break;
×
453
        }
454
        if (TSDB_CODE_SUCCESS != hbProcessDynViewRsp(kv->value, kv->valueLen, pCatalog)) {
4!
455
          tscError("Process dyn view response failed, len: %d, value: %p", kv->valueLen, kv->value);
×
456
          break;
×
457
        }
458
        break;
4✔
459
      }
460
      case HEARTBEAT_KEY_VIEWINFO: {
4✔
461
        if (kv->valueLen <= 0 || NULL == kv->value) {
4!
462
          tscError("invalid view info, len:%d, value:%p", kv->valueLen, kv->value);
×
463
          break;
×
464
        }
465
        if (TSDB_CODE_SUCCESS != hbProcessViewInfoRsp(kv->value, kv->valueLen, pCatalog)) {
4!
466
          tscError("Process view info response failed, len: %d, value: %p", kv->valueLen, kv->value);
×
467
          break;
×
468
        }
469
        break;
4✔
470
      }
471
#endif
472
      case HEARTBEAT_KEY_TSMA: {
336✔
473
        if (kv->valueLen <= 0 || !kv->value) {
336!
474
          tscError("Invalid tsma info, len: %d, value: %p", kv->valueLen, kv->value);
×
475
        }
476
        if (TSDB_CODE_SUCCESS != hbprocessTSMARsp(kv->value, kv->valueLen, pCatalog)) {
336!
477
          tscError("Process tsma info response failed, len: %d, value: %p", kv->valueLen, kv->value);
×
478
        }
479
        break;
336✔
480
      }
481
      default:
×
482
        tscError("invalid hb key type:%d", kv->key);
×
483
        break;
×
484
    }
485
  }
486
}
14,868✔
487

488
static int32_t hbQueryHbRspHandle(SAppHbMgr *pAppHbMgr, SClientHbRsp *pRsp) {
65,227✔
489
  SClientHbReq *pReq = taosHashAcquire(pAppHbMgr->activeInfo, &pRsp->connKey, sizeof(SClientHbKey));
65,227✔
490
  if (NULL == pReq) {
65,227✔
491
    tscWarn("pReq to get activeInfo, may be dropped, refId:%" PRIx64 ", type:%d", pRsp->connKey.tscRid,
95!
492
            pRsp->connKey.connType);
493
    return TSDB_CODE_SUCCESS;
95✔
494
  }
495

496
  if (pRsp->query) {
65,132✔
497
    STscObj *pTscObj = (STscObj *)acquireTscObj(pRsp->connKey.tscRid);
60,837✔
498
    if (NULL == pTscObj) {
60,837!
UNCOV
499
      tscDebug("tscObj rid %" PRIx64 " not exist", pRsp->connKey.tscRid);
×
500
    } else {
501
      if (pRsp->query->totalDnodes > 1 && !isEpsetEqual(&pTscObj->pAppInfo->mgmtEp.epSet, &pRsp->query->epSet)) {
60,837✔
502
        SEpSet *pOrig = &pTscObj->pAppInfo->mgmtEp.epSet;
133✔
503
        SEp    *pOrigEp = &pOrig->eps[pOrig->inUse];
133✔
504
        SEp    *pNewEp = &pRsp->query->epSet.eps[pRsp->query->epSet.inUse];
133✔
505
        tscDebug("mnode epset updated from %d/%d=>%s:%d to %d/%d=>%s:%d in hb", pOrig->inUse, pOrig->numOfEps,
133✔
506
                 pOrigEp->fqdn, pOrigEp->port, pRsp->query->epSet.inUse, pRsp->query->epSet.numOfEps, pNewEp->fqdn,
507
                 pNewEp->port);
508

509
        updateEpSet_s(&pTscObj->pAppInfo->mgmtEp, &pRsp->query->epSet);
133✔
510
      }
511

512
      pTscObj->pAppInfo->totalDnodes = pRsp->query->totalDnodes;
60,837✔
513
      pTscObj->pAppInfo->onlineDnodes = pRsp->query->onlineDnodes;
60,837✔
514
      pTscObj->connId = pRsp->query->connId;
60,837✔
515
      tscTrace("conn %u hb rsp, dnodes %d/%d", pTscObj->connId, pTscObj->pAppInfo->onlineDnodes,
60,837✔
516
               pTscObj->pAppInfo->totalDnodes);
517

518
      if (pRsp->query->killRid) {
60,837!
519
        tscDebug("request rid %" PRIx64 " need to be killed now", pRsp->query->killRid);
×
520
        SRequestObj *pRequest = acquireRequest(pRsp->query->killRid);
×
521
        if (NULL == pRequest) {
×
522
          tscDebug("request 0x%" PRIx64 " not exist to kill", pRsp->query->killRid);
×
523
        } else {
524
          taos_stop_query((TAOS_RES *)pRequest);
×
525
          (void)releaseRequest(pRsp->query->killRid);
×
526
        }
527
      }
528

529
      if (pRsp->query->killConnection) {
60,837!
530
        taos_close_internal(pTscObj);
×
531
      }
532

533
      if (pRsp->query->pQnodeList) {
60,837✔
534
        if (TSDB_CODE_SUCCESS != updateQnodeList(pTscObj->pAppInfo, pRsp->query->pQnodeList)) {
24,585!
535
          tscWarn("update qnode list failed");
×
536
        }
537
      }
538

539
      releaseTscObj(pRsp->connKey.tscRid);
60,837✔
540
    }
541
  }
542

543
  int32_t kvNum = pRsp->info ? taosArrayGetSize(pRsp->info) : 0;
65,132!
544

545
  tscDebug("hb got %d rsp kv", kvNum);
65,132✔
546

547
  if (kvNum > 0) {
65,132✔
548
    struct SCatalog *pCatalog = NULL;
14,868✔
549
    int32_t          code = catalogGetHandle(pReq->clusterId, &pCatalog);
14,868✔
550
    if (code != TSDB_CODE_SUCCESS) {
14,868!
551
      tscWarn("catalogGetHandle failed, clusterId:%" PRIx64 ", error:%s", pReq->clusterId, tstrerror(code));
×
552
    } else {
553
      hbProcessQueryRspKvs(kvNum, pRsp->info, pCatalog, pAppHbMgr);
14,868✔
554
    }
555
  }
556

557
  taosHashRelease(pAppHbMgr->activeInfo, pReq);
65,132✔
558

559
  return TSDB_CODE_SUCCESS;
65,132✔
560
}
561

562
static int32_t hbAsyncCallBack(void *param, SDataBuf *pMsg, int32_t code) {
37,709✔
563
  if (0 == atomic_load_8(&clientHbMgr.inited)) {
37,709!
564
    goto _return;
×
565
  }
566

567
  static int32_t    emptyRspNum = 0;
568
  int32_t           idx = *(int32_t *)param;
37,708✔
569
  SClientHbBatchRsp pRsp = {0};
37,708✔
570
  if (TSDB_CODE_SUCCESS == code) {
37,708✔
571
    code = tDeserializeSClientHbBatchRsp(pMsg->pData, pMsg->len, &pRsp);
37,014✔
572
    if (TSDB_CODE_SUCCESS != code) {
37,015✔
573
      tscError("deserialize hb rsp failed");
8!
574
    }
575
    int32_t now = taosGetTimestampSec();
37,015✔
576
    int32_t delta = abs(now - pRsp.svrTimestamp);
37,015✔
577
    if (delta > timestampDeltaLimit) {
37,015✔
578
      code = TSDB_CODE_TIME_UNSYNCED;
8✔
579
      tscError("time diff: %ds is too big", delta);
8!
580
    }
581
  }
582

583
  int32_t rspNum = taosArrayGetSize(pRsp.rsps);
37,709✔
584

585
  (void)taosThreadMutexLock(&clientHbMgr.lock);
37,708✔
586

587
  SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, idx);
37,709✔
588
  if (pAppHbMgr == NULL) {
37,709!
589
    (void)taosThreadMutexUnlock(&clientHbMgr.lock);
×
590
    tscError("appHbMgr not exist, idx:%d", idx);
×
591
    taosMemoryFree(pMsg->pData);
×
592
    taosMemoryFree(pMsg->pEpSet);
×
593
    tFreeClientHbBatchRsp(&pRsp);
594
    return TSDB_CODE_OUT_OF_RANGE;
×
595
  }
596

597
  SAppInstInfo *pInst = pAppHbMgr->pAppInstInfo;
37,709✔
598

599
  if (code != 0) {
37,709✔
600
    pInst->onlineDnodes = pInst->totalDnodes ? 0 : -1;
702✔
601
    tscDebug("hb rsp error %s, update server status %d/%d", tstrerror(code), pInst->onlineDnodes, pInst->totalDnodes);
702!
602
    (void)taosThreadMutexUnlock(&clientHbMgr.lock);
702✔
603
    taosMemoryFree(pMsg->pData);
702!
604
    taosMemoryFree(pMsg->pEpSet);
702!
605
    tFreeClientHbBatchRsp(&pRsp);
606
    return code;
702✔
607
  }
608

609
  pInst->serverCfg.monitorParas = pRsp.monitorParas;
37,007✔
610
  pInst->serverCfg.enableAuditDelete = pRsp.enableAuditDelete;
37,007✔
611
  tscDebug("[monitor] paras from hb, clusterId:%" PRIx64 " monitorParas threshold:%d scope:%d", pInst->clusterId,
37,007✔
612
           pRsp.monitorParas.tsSlowLogThreshold, pRsp.monitorParas.tsSlowLogScope);
613

614
  if (rspNum) {
37,007✔
615
    tscDebug("hb got %d rsp, %d empty rsp received before", rspNum,
36,761✔
616
             atomic_val_compare_exchange_32(&emptyRspNum, emptyRspNum, 0));
617
  } else {
618
    (void)atomic_add_fetch_32(&emptyRspNum, 1);
246✔
619
  }
620

621
  for (int32_t i = 0; i < rspNum; ++i) {
102,234✔
622
    SClientHbRsp *rsp = taosArrayGet(pRsp.rsps, i);
65,227✔
623
    code = (*clientHbMgr.rspHandle[rsp->connKey.connType])(pAppHbMgr, rsp);
65,227✔
624
    if (code) {
65,227!
625
      break;
×
626
    }
627
  }
628

629
  (void)taosThreadMutexUnlock(&clientHbMgr.lock);
37,007✔
630

631
  tFreeClientHbBatchRsp(&pRsp);
632

633
_return:
37,007✔
634
  taosMemoryFree(pMsg->pData);
37,007!
635
  taosMemoryFree(pMsg->pEpSet);
37,007!
636
  return code;
37,007✔
637
}
638

639
int32_t hbBuildQueryDesc(SQueryHbReqBasic *hbBasic, STscObj *pObj) {
40,515✔
640
  int64_t    now = taosGetTimestampUs();
40,515✔
641
  SQueryDesc desc = {0};
40,515✔
642
  int32_t    code = 0;
40,515✔
643

644
  void *pIter = taosHashIterate(pObj->pRequests, NULL);
40,515✔
645
  while (pIter != NULL) {
85,478✔
646
    int64_t     *rid = pIter;
44,963✔
647
    SRequestObj *pRequest = acquireRequest(*rid);
44,963✔
648
    if (NULL == pRequest) {
44,963✔
649
      pIter = taosHashIterate(pObj->pRequests, pIter);
65✔
650
      continue;
65✔
651
    }
652

653
    if (pRequest->killed || 0 == pRequest->body.queryJob) {
44,898✔
654
      (void)releaseRequest(*rid);
15,648✔
655
      pIter = taosHashIterate(pObj->pRequests, pIter);
15,648✔
656
      continue;
15,648✔
657
    }
658

659
    tstrncpy(desc.sql, pRequest->sqlstr, sizeof(desc.sql));
29,250✔
660
    desc.stime = pRequest->metric.start / 1000;
29,250✔
661
    desc.queryId = pRequest->requestId;
29,250✔
662
    desc.useconds = now - pRequest->metric.start;
29,250✔
663
    desc.reqRid = pRequest->self;
29,250✔
664
    desc.stableQuery = pRequest->stableQuery;
29,250✔
665
    desc.isSubQuery = pRequest->isSubReq;
29,250✔
666
    code = taosGetFqdn(desc.fqdn);
29,250✔
667
    if (TSDB_CODE_SUCCESS != code) {
29,250!
668
      (void)releaseRequest(*rid);
×
669
      tscError("get fqdn failed");
×
670
      return TSDB_CODE_FAILED;
×
671
    }
672
    desc.subPlanNum = pRequest->body.subplanNum;
29,250✔
673

674
    if (desc.subPlanNum) {
29,250!
675
      desc.subDesc = taosArrayInit(desc.subPlanNum, sizeof(SQuerySubDesc));
29,250✔
676
      if (NULL == desc.subDesc) {
29,250!
677
        (void)releaseRequest(*rid);
×
678
        return terrno;
×
679
      }
680

681
      code = schedulerGetTasksStatus(pRequest->body.queryJob, desc.subDesc);
29,250✔
682
      if (code) {
29,250✔
683
        taosArrayDestroy(desc.subDesc);
6,132✔
684
        desc.subDesc = NULL;
6,132✔
685
      }
686
      desc.subPlanNum = taosArrayGetSize(desc.subDesc);
29,250✔
687
    } else {
688
      desc.subDesc = NULL;
×
689
    }
690

691
    (void)releaseRequest(*rid);
29,250✔
692
    if (NULL == taosArrayPush(hbBasic->queryDesc, &desc)) {
58,500!
693
      taosArrayDestroy(desc.subDesc);
×
694
      return terrno;
×
695
    }
696

697
    pIter = taosHashIterate(pObj->pRequests, pIter);
29,250✔
698
  }
699

700
  return code;
40,515✔
701
}
702

703
int32_t hbGetQueryBasicInfo(SClientHbKey *connKey, SClientHbReq *req) {
67,797✔
704
  STscObj *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
67,797✔
705
  if (NULL == pTscObj) {
67,797!
706
    tscWarn("tscObj rid %" PRIx64 " not exist", connKey->tscRid);
×
707
    return terrno;
×
708
  }
709

710
  SQueryHbReqBasic *hbBasic = (SQueryHbReqBasic *)taosMemoryCalloc(1, sizeof(SQueryHbReqBasic));
67,797!
711
  if (NULL == hbBasic) {
67,797!
712
    tscError("calloc %d failed", (int32_t)sizeof(SQueryHbReqBasic));
×
713
    releaseTscObj(connKey->tscRid);
×
714
    return terrno;
×
715
  }
716

717
  hbBasic->connId = pTscObj->connId;
67,797✔
718

719
  int32_t numOfQueries = pTscObj->pRequests ? taosHashGetSize(pTscObj->pRequests) : 0;
67,797!
720
  if (numOfQueries <= 0) {
67,797✔
721
    req->query = hbBasic;
27,282✔
722
    releaseTscObj(connKey->tscRid);
27,282✔
723
    tscDebug("no queries on connection");
27,282✔
724
    return TSDB_CODE_SUCCESS;
27,282✔
725
  }
726

727
  hbBasic->queryDesc = taosArrayInit(numOfQueries, sizeof(SQueryDesc));
40,515✔
728
  if (NULL == hbBasic->queryDesc) {
40,515!
729
    tscWarn("taosArrayInit %d queryDesc failed", numOfQueries);
×
730
    releaseTscObj(connKey->tscRid);
×
731
    taosMemoryFree(hbBasic);
×
732
    return terrno;
×
733
  }
734

735
  int32_t code = hbBuildQueryDesc(hbBasic, pTscObj);
40,515✔
736
  if (code) {
40,515✔
737
    releaseTscObj(connKey->tscRid);
4,316✔
738
    if (hbBasic->queryDesc) {
4,316!
739
      taosArrayDestroyEx(hbBasic->queryDesc, tFreeClientHbQueryDesc);
4,316✔
740
    }
741
    taosMemoryFree(hbBasic);
4,316!
742
    return code;
4,316✔
743
  }
744

745
  req->query = hbBasic;
36,199✔
746
  releaseTscObj(connKey->tscRid);
36,199✔
747

748
  return TSDB_CODE_SUCCESS;
36,199✔
749
}
750

751
static int32_t hbGetUserAuthInfo(SClientHbKey *connKey, SHbParam *param, SClientHbReq *req) {
2,664✔
752
  STscObj *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
2,664✔
753
  if (!pTscObj) {
2,664!
754
    tscWarn("tscObj rid %" PRIx64 " not exist", connKey->tscRid);
×
755
    return terrno;
×
756
  }
757

758
  int32_t code = 0;
2,664✔
759

760
  SKv  kv = {.key = HEARTBEAT_KEY_USER_AUTHINFO};
2,664✔
761
  SKv *pKv = NULL;
2,664✔
762
  if ((pKv = taosHashGet(req->info, &kv.key, sizeof(kv.key)))) {
2,664✔
763
    int32_t           userNum = pKv->valueLen / sizeof(SUserAuthVersion);
1,737✔
764
    SUserAuthVersion *userAuths = (SUserAuthVersion *)pKv->value;
1,737✔
765
    for (int32_t i = 0; i < userNum; ++i) {
1,807✔
766
      SUserAuthVersion *pUserAuth = userAuths + i;
1,802✔
767
      // both key and user exist, update version
768
      if (strncmp(pUserAuth->user, pTscObj->user, TSDB_USER_LEN) == 0) {
1,802✔
769
        pUserAuth->version = htonl(-1);  // force get userAuthInfo
1,732✔
770
        goto _return;
1,732✔
771
      }
772
    }
773
    // key exists, user not exist, append user
774
    SUserAuthVersion *qUserAuth =
10✔
775
        (SUserAuthVersion *)taosMemoryRealloc(pKv->value, (userNum + 1) * sizeof(SUserAuthVersion));
5!
776
    if (qUserAuth) {
5!
777
      tstrncpy((qUserAuth + userNum)->user, pTscObj->user, TSDB_USER_LEN);
5✔
778
      (qUserAuth + userNum)->version = htonl(-1);  // force get userAuthInfo
5✔
779
      pKv->value = qUserAuth;
5✔
780
      pKv->valueLen += sizeof(SUserAuthVersion);
5✔
781
    } else {
782
      code = terrno;
×
783
    }
784
    goto _return;
5✔
785
  }
786

787
  // key/user not exist, add user
788
  SUserAuthVersion *user = taosMemoryMalloc(sizeof(SUserAuthVersion));
927!
789
  if (!user) {
927!
790
    code = terrno;
×
791
    goto _return;
×
792
  }
793
  tstrncpy(user->user, pTscObj->user, TSDB_USER_LEN);
927✔
794
  user->version = htonl(-1);  // force get userAuthInfo
927✔
795
  kv.valueLen = sizeof(SUserAuthVersion);
927✔
796
  kv.value = user;
927✔
797

798
  tscDebug("hb got user auth info, valueLen:%d, user:%s, authVer:%d, tscRid:%" PRIi64, kv.valueLen, user->user,
927✔
799
           pTscObj->authVer, connKey->tscRid);
800

801
  if (!req->info) {
927!
802
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
927✔
803
    if (NULL == req->info) {
927!
804
      code = terrno;
×
805
      goto _return;
×
806
    }
807
  }
808

809
  if (taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)) != 0) {
927!
810
    taosMemoryFree(user);
×
811
    code = terrno ? terrno : TSDB_CODE_APP_ERROR;
×
812
    goto _return;
×
813
  }
814

815
_return:
927✔
816
  releaseTscObj(connKey->tscRid);
2,664✔
817
  if (code) {
2,664!
818
    tscError("hb got user auth info failed since %s", tstrerror(code));
×
819
  }
820

821
  return code;
2,664✔
822
}
823

824
int32_t hbGetExpiredUserInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
31,216✔
825
  SUserAuthVersion *users = NULL;
31,216✔
826
  uint32_t          userNum = 0;
31,216✔
827
  int32_t           code = 0;
31,216✔
828

829
  code = catalogGetExpiredUsers(pCatalog, &users, &userNum);
31,216✔
830
  if (TSDB_CODE_SUCCESS != code) {
31,216!
831
    return code;
×
832
  }
833

834
  if (userNum <= 0) {
31,216✔
835
    taosMemoryFree(users);
784!
836
    return TSDB_CODE_SUCCESS;
784✔
837
  }
838

839
  for (int32_t i = 0; i < userNum; ++i) {
61,310✔
840
    SUserAuthVersion *user = &users[i];
30,878✔
841
    user->version = htonl(user->version);
30,878✔
842
  }
843

844
  SKv kv = {
30,432✔
845
      .key = HEARTBEAT_KEY_USER_AUTHINFO,
846
      .valueLen = sizeof(SUserAuthVersion) * userNum,
30,432✔
847
      .value = users,
848
  };
849

850
  tscDebug("hb got %d expired users, valueLen:%d", userNum, kv.valueLen);
30,432✔
851

852
  if (NULL == req->info) {
30,432!
853
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
30,432✔
854
    if (NULL == req->info) {
30,432!
855
      taosMemoryFree(users);
×
856
      return terrno;
×
857
    }
858
  }
859

860
  code = taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
30,432✔
861
  if (TSDB_CODE_SUCCESS != code) {
30,432!
862
    taosMemoryFree(users);
×
863
    return code;
×
864
  }
865

866
  return TSDB_CODE_SUCCESS;
30,432✔
867
}
868

869
int32_t hbGetExpiredDBInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
35,185✔
870
  SDbCacheInfo *dbs = NULL;
35,185✔
871
  uint32_t      dbNum = 0;
35,185✔
872
  int32_t       code = 0;
35,185✔
873

874
  code = catalogGetExpiredDBs(pCatalog, &dbs, &dbNum);
35,185✔
875
  if (TSDB_CODE_SUCCESS != code) {
35,185!
876
    return code;
×
877
  }
878

879
  if (dbNum <= 0) {
35,185✔
880
    taosMemoryFree(dbs);
25,414!
881
    return TSDB_CODE_SUCCESS;
25,414✔
882
  }
883

884
  for (int32_t i = 0; i < dbNum; ++i) {
21,378✔
885
    SDbCacheInfo *db = &dbs[i];
11,607✔
886
    tscDebug("the %dth expired dbFName:%s, dbId:%" PRId64
11,607✔
887
             ", vgVersion:%d, cfgVersion:%d, numOfTable:%d, startTs:%" PRId64,
888
             i, db->dbFName, db->dbId, db->vgVersion, db->cfgVersion, db->numOfTable, db->stateTs);
889

890
    db->dbId = htobe64(db->dbId);
11,607✔
891
    db->vgVersion = htonl(db->vgVersion);
11,607✔
892
    db->cfgVersion = htonl(db->cfgVersion);
11,607✔
893
    db->numOfTable = htonl(db->numOfTable);
11,607✔
894
    db->stateTs = htobe64(db->stateTs);
11,607✔
895
    db->tsmaVersion = htonl(db->tsmaVersion);
11,607✔
896
  }
897

898
  SKv kv = {
9,771✔
899
      .key = HEARTBEAT_KEY_DBINFO,
900
      .valueLen = sizeof(SDbCacheInfo) * dbNum,
9,771✔
901
      .value = dbs,
902
  };
903

904
  tscDebug("hb got %d expired db, valueLen:%d", dbNum, kv.valueLen);
9,771✔
905

906
  if (NULL == req->info) {
9,771✔
907
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
19✔
908
    if (NULL == req->info) {
19!
909
      taosMemoryFree(dbs);
×
910
      return terrno;
×
911
    }
912
  }
913

914
  code = taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
9,771✔
915
  if (TSDB_CODE_SUCCESS != code) {
9,771!
916
    taosMemoryFree(dbs);
×
917
    return code;
×
918
  }
919

920
  return TSDB_CODE_SUCCESS;
9,771✔
921
}
922

923
int32_t hbGetExpiredStbInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
35,185✔
924
  SSTableVersion *stbs = NULL;
35,185✔
925
  uint32_t        stbNum = 0;
35,185✔
926
  int32_t         code = 0;
35,185✔
927

928
  code = catalogGetExpiredSTables(pCatalog, &stbs, &stbNum);
35,185✔
929
  if (TSDB_CODE_SUCCESS != code) {
35,185!
930
    return code;
×
931
  }
932

933
  if (stbNum <= 0) {
35,185✔
934
    taosMemoryFree(stbs);
28,503!
935
    return TSDB_CODE_SUCCESS;
28,503✔
936
  }
937

938
  for (int32_t i = 0; i < stbNum; ++i) {
15,727✔
939
    SSTableVersion *stb = &stbs[i];
9,045✔
940
    stb->suid = htobe64(stb->suid);
9,045✔
941
    stb->sversion = htonl(stb->sversion);
9,045✔
942
    stb->tversion = htonl(stb->tversion);
9,045✔
943
    stb->smaVer = htonl(stb->smaVer);
9,045✔
944
  }
945

946
  SKv kv = {
6,682✔
947
      .key = HEARTBEAT_KEY_STBINFO,
948
      .valueLen = sizeof(SSTableVersion) * stbNum,
6,682✔
949
      .value = stbs,
950
  };
951

952
  tscDebug("hb got %d expired stb, valueLen:%d", stbNum, kv.valueLen);
6,682✔
953

954
  if (NULL == req->info) {
6,682!
UNCOV
955
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
×
UNCOV
956
    if (NULL == req->info) {
×
957
      taosMemoryFree(stbs);
×
958
      return terrno;
×
959
    }
960
  }
961

962
  code = taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
6,682✔
963
  if (TSDB_CODE_SUCCESS != code) {
6,682!
964
    taosMemoryFree(stbs);
×
965
    return code;
×
966
  }
967

968
  return TSDB_CODE_SUCCESS;
6,682✔
969
}
970

971
int32_t hbGetExpiredViewInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
35,185✔
972
  SViewVersion    *views = NULL;
35,185✔
973
  uint32_t         viewNum = 0;
35,185✔
974
  int32_t          code = 0;
35,185✔
975
  SDynViewVersion *pDynViewVer = NULL;
35,185✔
976

977
  TSC_ERR_JRET(catalogGetExpiredViews(pCatalog, &views, &viewNum, &pDynViewVer));
35,185!
978

979
  if (viewNum <= 0) {
35,185✔
980
    taosMemoryFree(views);
35,181!
981
    taosMemoryFree(pDynViewVer);
35,181!
982
    return TSDB_CODE_SUCCESS;
35,181✔
983
  }
984

985
  for (int32_t i = 0; i < viewNum; ++i) {
8✔
986
    SViewVersion *view = &views[i];
4✔
987
    view->dbId = htobe64(view->dbId);
4✔
988
    view->viewId = htobe64(view->viewId);
4✔
989
    view->version = htonl(view->version);
4✔
990
  }
991

992
  tscDebug("hb got %u expired view, valueLen:%lu", viewNum, sizeof(SViewVersion) * viewNum);
4!
993

994
  if (NULL == req->info) {
4!
995
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
×
996
    if (NULL == req->info) {
×
997
      TSC_ERR_JRET(terrno);
×
998
    }
999
  }
1000

1001
  SKv kv = {
4✔
1002
      .key = HEARTBEAT_KEY_DYN_VIEW,
1003
      .valueLen = sizeof(SDynViewVersion),
1004
      .value = pDynViewVer,
1005
  };
1006

1007
  TSC_ERR_JRET(taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)));
4!
1008

1009
  kv.key = HEARTBEAT_KEY_VIEWINFO;
4✔
1010
  kv.valueLen = sizeof(SViewVersion) * viewNum;
4✔
1011
  kv.value = views;
4✔
1012

1013
  TSC_ERR_JRET(taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)));
4!
1014
  return TSDB_CODE_SUCCESS;
4✔
1015
_return:
×
1016
  taosMemoryFree(views);
×
1017
  taosMemoryFree(pDynViewVer);
×
1018
  return code;
×
1019
}
1020

1021
int32_t hbGetExpiredTSMAInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *pReq) {
35,185✔
1022
  int32_t       code = 0;
35,185✔
1023
  uint32_t      tsmaNum = 0;
35,185✔
1024
  STSMAVersion *tsmas = NULL;
35,185✔
1025

1026
  code = catalogGetExpiredTsmas(pCatalog, &tsmas, &tsmaNum);
35,185✔
1027
  if (code) {
35,185!
1028
    taosMemoryFree(tsmas);
×
1029
    return code;
×
1030
  }
1031

1032
  if (tsmaNum <= 0) {
35,185✔
1033
    taosMemoryFree(tsmas);
34,849!
1034
    return TSDB_CODE_SUCCESS;
34,849✔
1035
  }
1036

1037
  for (int32_t i = 0; i < tsmaNum; ++i) {
708✔
1038
    STSMAVersion *tsma = &tsmas[i];
372✔
1039
    tsma->dbId = htobe64(tsma->dbId);
372✔
1040
    tsma->tsmaId = htobe64(tsma->tsmaId);
372✔
1041
    tsma->version = htonl(tsma->version);
372✔
1042
  }
1043

1044
  tscDebug("hb got %d expred tsmas, valueLen: %lu", tsmaNum, sizeof(STSMAVersion) * tsmaNum);
336!
1045

1046
  if (!pReq->info) {
336!
1047
    pReq->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
×
1048
    if (!pReq->info) {
×
1049
      taosMemoryFree(tsmas);
×
1050
      return terrno;
×
1051
    }
1052
  }
1053

1054
  SKv kv = {.key = HEARTBEAT_KEY_TSMA, .valueLen = sizeof(STSMAVersion) * tsmaNum, .value = tsmas};
336✔
1055
  code = taosHashPut(pReq->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
336✔
1056
  if (TSDB_CODE_SUCCESS != code) {
336!
1057
    taosMemoryFree(tsmas);
×
1058
    return code;
×
1059
  }
1060
  return TSDB_CODE_SUCCESS;
336✔
1061
}
1062

1063
int32_t hbGetAppInfo(int64_t clusterId, SClientHbReq *req) {
63,481✔
1064
  SAppHbReq *pApp = taosHashGet(clientHbMgr.appSummary, &clusterId, sizeof(clusterId));
63,481✔
1065
  if (NULL != pApp) {
63,481!
1066
    (void)memcpy(&req->app, pApp, sizeof(*pApp));
63,481✔
1067
  } else {
1068
    (void)memset(&req->app.summary, 0, sizeof(req->app.summary));
×
1069
    req->app.pid = taosGetPId();
×
1070
    req->app.appId = clientHbMgr.appId;
×
1071
    TSC_ERR_RET(taosGetAppName(req->app.name, NULL));
×
1072
  }
1073

1074
  return TSDB_CODE_SUCCESS;
63,481✔
1075
}
1076

1077
int32_t hbQueryHbReqHandle(SClientHbKey *connKey, void *param, SClientHbReq *req) {
67,797✔
1078
  int32_t   code = 0;
67,797✔
1079
  SHbParam *hbParam = (SHbParam *)param;
67,797✔
1080
  SCatalog *pCatalog = NULL;
67,797✔
1081

1082
  code = hbGetQueryBasicInfo(connKey, req);
67,797✔
1083
  if (code != TSDB_CODE_SUCCESS) {
67,797✔
1084
    tscWarn("hbGetQueryBasicInfo failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
4,316!
1085
    return code;
4,316✔
1086
  }
1087

1088
  if (hbParam->reqCnt == 0) {
63,481✔
1089
    code = catalogGetHandle(hbParam->clusterId, &pCatalog);
35,185✔
1090
    if (code != TSDB_CODE_SUCCESS) {
35,185!
1091
      tscWarn("catalogGetHandle failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1092
      return code;
×
1093
    }
1094

1095
    code = hbGetAppInfo(hbParam->clusterId, req);
35,185✔
1096
    if (TSDB_CODE_SUCCESS != code) {
35,185!
1097
      tscWarn("getAppInfo failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1098
      return code;
×
1099
    }
1100

1101
    if (!taosHashGet(clientHbMgr.appHbHash, &hbParam->clusterId, sizeof(hbParam->clusterId))) {
35,185✔
1102
      code = hbGetExpiredUserInfo(connKey, pCatalog, req);
31,216✔
1103
      if (TSDB_CODE_SUCCESS != code) {
31,216!
1104
        tscWarn("hbGetExpiredUserInfo failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1105
        return code;
×
1106
      }
1107
      if (clientHbMgr.appHbHash) {
31,216✔
1108
        code = taosHashPut(clientHbMgr.appHbHash, &hbParam->clusterId, sizeof(uint64_t), NULL, 0);
6,584✔
1109
        if (TSDB_CODE_SUCCESS != code) {
6,584!
1110
          tscWarn("hbQueryHbReqHandle put clusterId failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId,
×
1111
                  tstrerror(code));
1112
          return code;
×
1113
        }
1114
      }
1115
    }
1116

1117
    // invoke after hbGetExpiredUserInfo
1118
    if (2 != atomic_load_8(&hbParam->pAppHbMgr->connHbFlag)) {
35,185✔
1119
      code = hbGetUserAuthInfo(connKey, hbParam, req);
2,664✔
1120
      if (TSDB_CODE_SUCCESS != code) {
2,664!
1121
        tscWarn("hbGetUserAuthInfo failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1122
        return code;
×
1123
      }
1124
      atomic_store_8(&hbParam->pAppHbMgr->connHbFlag, 1);
2,664✔
1125
    }
1126

1127
    code = hbGetExpiredDBInfo(connKey, pCatalog, req);
35,185✔
1128
    if (TSDB_CODE_SUCCESS != code) {
35,185!
1129
      tscWarn("hbGetExpiredDBInfo failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1130
      return code;
×
1131
    }
1132

1133
    code = hbGetExpiredStbInfo(connKey, pCatalog, req);
35,185✔
1134
    if (TSDB_CODE_SUCCESS != code) {
35,185!
1135
      tscWarn("hbGetExpiredStbInfo failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1136
      return code;
×
1137
    }
1138

1139
#ifdef TD_ENTERPRISE
1140
    code = hbGetExpiredViewInfo(connKey, pCatalog, req);
35,185✔
1141
    if (TSDB_CODE_SUCCESS != code) {
35,185!
1142
      tscWarn("hbGetExpiredViewInfo failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1143
      return code;
×
1144
    }
1145
#endif
1146
    code = hbGetExpiredTSMAInfo(connKey, pCatalog, req);
35,185✔
1147
    if (TSDB_CODE_SUCCESS != code) {
35,185!
1148
      tscWarn("hbGetExpiredTSMAInfo failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1149
      return code;
×
1150
    }
1151
  } else {
1152
    code = hbGetAppInfo(hbParam->clusterId, req);
28,296✔
1153
    if (TSDB_CODE_SUCCESS != code) {
28,296!
1154
      tscWarn("hbGetAppInfo failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1155
      return code;
×
1156
    }
1157
  }
1158

1159
  ++hbParam->reqCnt;  // success to get catalog info
63,481✔
1160

1161
  return TSDB_CODE_SUCCESS;
63,481✔
1162
}
1163

1164
static FORCE_INLINE void hbMgrInitHandle() {
1165
  // init all handle
1166
  clientHbMgr.reqHandle[CONN_TYPE__QUERY] = hbQueryHbReqHandle;
2,317✔
1167
  clientHbMgr.reqHandle[CONN_TYPE__TMQ] = hbMqHbReqHandle;
2,317✔
1168

1169
  clientHbMgr.rspHandle[CONN_TYPE__QUERY] = hbQueryHbRspHandle;
2,317✔
1170
  clientHbMgr.rspHandle[CONN_TYPE__TMQ] = hbMqHbRspHandle;
2,317✔
1171
}
2,317✔
1172

1173
int32_t hbGatherAllInfo(SAppHbMgr *pAppHbMgr, SClientHbBatchReq **pBatchReq) {
39,472✔
1174
  *pBatchReq = taosMemoryCalloc(1, sizeof(SClientHbBatchReq));
39,472!
1175
  if (pBatchReq == NULL) {
39,472!
1176
    return terrno;
×
1177
  }
1178
  int32_t connKeyCnt = atomic_load_32(&pAppHbMgr->connKeyCnt);
39,472✔
1179
  (*pBatchReq)->reqs = taosArrayInit(connKeyCnt, sizeof(SClientHbReq));
39,472✔
1180
  if (!(*pBatchReq)->reqs) {
39,472!
1181
    tFreeClientHbBatchReq(*pBatchReq);
×
1182
    return terrno;
×
1183
  }
1184

1185
  int64_t  maxIpWhiteVer = 0;
39,472✔
1186
  void    *pIter = NULL;
39,472✔
1187
  SHbParam param = {0};
39,472✔
1188
  while ((pIter = taosHashIterate(pAppHbMgr->activeInfo, pIter))) {
108,992✔
1189
    SClientHbReq *pOneReq = pIter;
69,520✔
1190
    SClientHbKey *connKey = &pOneReq->connKey;
69,520✔
1191
    STscObj      *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
69,520✔
1192

1193
    if (!pTscObj || atomic_load_8(&pTscObj->dropped) == 1) {
69,520✔
1194
      if (pTscObj) releaseTscObj(connKey->tscRid);
1,723✔
1195
      continue;
1,723✔
1196
    }
1197

1198
    tstrncpy(pOneReq->userApp, pTscObj->optionInfo.userApp, sizeof(pOneReq->userApp));
67,797✔
1199
    pOneReq->userIp = pTscObj->optionInfo.userIp;
67,797✔
1200

1201
    pOneReq = taosArrayPush((*pBatchReq)->reqs, pOneReq);
67,797✔
1202
    if (NULL == pOneReq) {
67,797!
1203
      releaseTscObj(connKey->tscRid);
×
1204
      continue;
×
1205
    }
1206

1207
    switch (connKey->connType) {
67,797!
1208
      case CONN_TYPE__QUERY: {
67,797✔
1209
        if (param.clusterId == 0) {
67,797✔
1210
          // init
1211
          param.clusterId = pOneReq->clusterId;
39,222✔
1212
          param.pAppHbMgr = pAppHbMgr;
39,222✔
1213
          param.connHbFlag = atomic_load_8(&pAppHbMgr->connHbFlag);
39,222✔
1214
        }
1215
        break;
67,797✔
1216
      }
1217
      default:
×
1218
        break;
×
1219
    }
1220
    if (clientHbMgr.reqHandle[connKey->connType]) {
67,797!
1221
      int32_t code = (*clientHbMgr.reqHandle[connKey->connType])(connKey, &param, pOneReq);
67,797✔
1222
      if (code) {
67,797✔
1223
        tscWarn("hbGatherAllInfo failed since %s, tscRid:%" PRIi64 ", connType:%" PRIi8, tstrerror(code),
4,316!
1224
                connKey->tscRid, connKey->connType);
1225
      }
1226
    }
1227

1228
    int64_t ver = atomic_load_64(&pTscObj->whiteListInfo.ver);
67,797✔
1229
    maxIpWhiteVer = TMAX(maxIpWhiteVer, ver);
67,797✔
1230
    releaseTscObj(connKey->tscRid);
67,797✔
1231
  }
1232
  (*pBatchReq)->ipWhiteList = maxIpWhiteVer;
39,472✔
1233

1234
  return TSDB_CODE_SUCCESS;
39,472✔
1235
}
1236

1237
void hbThreadFuncUnexpectedStopped(void) { atomic_store_8(&clientHbMgr.threadStop, 2); }
×
1238

1239
void hbMergeSummary(SAppClusterSummary *dst, SAppClusterSummary *src) {
8,366✔
1240
  dst->numOfInsertsReq += src->numOfInsertsReq;
8,366✔
1241
  dst->numOfInsertRows += src->numOfInsertRows;
8,366✔
1242
  dst->insertElapsedTime += src->insertElapsedTime;
8,366✔
1243
  dst->insertBytes += src->insertBytes;
8,366✔
1244
  dst->fetchBytes += src->fetchBytes;
8,366✔
1245
  dst->queryElapsedTime += src->queryElapsedTime;
8,366✔
1246
  dst->numOfSlowQueries += src->numOfSlowQueries;
8,366✔
1247
  dst->totalRequests += src->totalRequests;
8,366✔
1248
  dst->currentRequests += src->currentRequests;
8,366✔
1249
}
8,366✔
1250

1251
int32_t hbGatherAppInfo(void) {
40,792✔
1252
  SAppHbReq req = {0};
40,792✔
1253
  int32_t   code = TSDB_CODE_SUCCESS;
40,792✔
1254
  int       sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
40,792✔
1255
  if (sz > 0) {
40,792!
1256
    req.pid = taosGetPId();
40,792✔
1257
    req.appId = clientHbMgr.appId;
40,792✔
1258
    TSC_ERR_RET(taosGetAppName(req.name, NULL));
40,792!
1259
  }
1260

1261
  taosHashClear(clientHbMgr.appSummary);
40,792✔
1262

1263
  for (int32_t i = 0; i < sz; ++i) {
89,991✔
1264
    SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
49,199✔
1265
    if (pAppHbMgr == NULL) continue;
49,199!
1266

1267
    int64_t    clusterId = pAppHbMgr->pAppInstInfo->clusterId;
49,199✔
1268
    SAppHbReq *pApp = taosHashGet(clientHbMgr.appSummary, &clusterId, sizeof(clusterId));
49,199✔
1269
    if (NULL == pApp) {
49,199✔
1270
      (void)memcpy(&req.summary, &pAppHbMgr->pAppInstInfo->summary, sizeof(req.summary));
40,833✔
1271
      req.startTime = pAppHbMgr->startTime;
40,833✔
1272
      TSC_ERR_RET(taosHashPut(clientHbMgr.appSummary, &clusterId, sizeof(clusterId), &req, sizeof(req)));
40,833!
1273
    } else {
1274
      if (pAppHbMgr->startTime < pApp->startTime) {
8,366!
1275
        pApp->startTime = pAppHbMgr->startTime;
×
1276
      }
1277

1278
      hbMergeSummary(&pApp->summary, &pAppHbMgr->pAppInstInfo->summary);
8,366✔
1279
    }
1280
  }
1281

1282
  return TSDB_CODE_SUCCESS;
40,792✔
1283
}
1284

1285
static void *hbThreadFunc(void *param) {
2,317✔
1286
  setThreadName("hb");
2,317✔
1287
#ifdef WINDOWS
1288
  if (taosCheckCurrentInDll()) {
1289
    atexit(hbThreadFuncUnexpectedStopped);
1290
  }
1291
#endif
1292
  while (1) {
40,525✔
1293
    if (1 == clientHbMgr.threadStop) {
42,842✔
1294
      break;
2,021✔
1295
    }
1296

1297
    if (TSDB_CODE_SUCCESS != taosThreadMutexLock(&clientHbMgr.lock)) {
40,821!
1298
      tscError("taosThreadMutexLock failed");
×
1299
      return NULL;
×
1300
    }
1301

1302
    int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
40,821✔
1303
    if (sz > 0) {
40,821✔
1304
      if (TSDB_CODE_SUCCESS != hbGatherAppInfo()) {
40,792!
1305
        tscError("hbGatherAppInfo failed");
×
1306
        return NULL;
×
1307
      }
1308
      if (sz > 1 && !clientHbMgr.appHbHash) {
40,792✔
1309
        clientHbMgr.appHbHash = taosHashInit(0, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), true, HASH_NO_LOCK);
177✔
1310
        if (NULL == clientHbMgr.appHbHash) {
177!
1311
          tscError("taosHashInit failed");
×
1312
          return NULL;
×
1313
        }
1314
      }
1315
      taosHashClear(clientHbMgr.appHbHash);
40,792✔
1316
    }
1317

1318
    for (int i = 0; i < sz; i++) {
90,020✔
1319
      SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
49,199✔
1320
      if (pAppHbMgr == NULL) {
49,199!
1321
        continue;
9,727✔
1322
      }
1323

1324
      int32_t connCnt = atomic_load_32(&pAppHbMgr->connKeyCnt);
49,199✔
1325
      if (connCnt == 0) {
49,199✔
1326
        continue;
9,727✔
1327
      }
1328
      SClientHbBatchReq *pReq = NULL;
39,472✔
1329
      int32_t            code = hbGatherAllInfo(pAppHbMgr, &pReq);
39,472✔
1330
      if (TSDB_CODE_SUCCESS != code || taosArrayGetP(clientHbMgr.appHbMgrs, i) == NULL) {
39,472!
1331
        terrno = code ? code : TSDB_CODE_OUT_OF_RANGE;
×
1332
        tFreeClientHbBatchReq(pReq);
×
1333
        continue;
×
1334
      }
1335
      int tlen = tSerializeSClientHbBatchReq(NULL, 0, pReq);
39,472✔
1336
      if (tlen == -1) {
39,472!
1337
        tFreeClientHbBatchReq(pReq);
×
1338
        break;
×
1339
      }
1340
      void *buf = taosMemoryMalloc(tlen);
39,472!
1341
      if (buf == NULL) {
39,472!
1342
        tFreeClientHbBatchReq(pReq);
×
1343
        // hbClearReqInfo(pAppHbMgr);
1344
        break;
×
1345
      }
1346

1347
      if (tSerializeSClientHbBatchReq(buf, tlen, pReq) == -1) {
39,472!
1348
        tFreeClientHbBatchReq(pReq);
×
1349
        taosMemoryFree(buf);
×
1350
        break;
×
1351
      }
1352
      SMsgSendInfo *pInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
39,472!
1353

1354
      if (pInfo == NULL) {
39,472!
1355
        tFreeClientHbBatchReq(pReq);
×
1356
        // hbClearReqInfo(pAppHbMgr);
1357
        taosMemoryFree(buf);
×
1358
        break;
×
1359
      }
1360
      pInfo->fp = hbAsyncCallBack;
39,472✔
1361
      pInfo->msgInfo.pData = buf;
39,472✔
1362
      pInfo->msgInfo.len = tlen;
39,472✔
1363
      pInfo->msgType = TDMT_MND_HEARTBEAT;
39,472✔
1364
      pInfo->param = taosMemoryMalloc(sizeof(int32_t));
39,472!
1365
      if (pInfo->param  == NULL) {
39,472!
1366
        tFreeClientHbBatchReq(pReq);
×
1367
        // hbClearReqInfo(pAppHbMgr);
1368
        taosMemoryFree(buf);
×
1369
        taosMemoryFree(pInfo);
×
1370
        break;
×
1371
      }
1372
      *(int32_t *)pInfo->param = i;
39,472✔
1373
      pInfo->paramFreeFp = taosAutoMemoryFree;
39,472✔
1374
      pInfo->requestId = generateRequestId();
39,472✔
1375
      pInfo->requestObjRefId = 0;
39,472✔
1376

1377
      SAppInstInfo *pAppInstInfo = pAppHbMgr->pAppInstInfo;
39,472✔
1378
      SEpSet        epSet = getEpSet_s(&pAppInstInfo->mgmtEp);
39,472✔
1379
      if (TSDB_CODE_SUCCESS != asyncSendMsgToServer(pAppInstInfo->pTransporter, &epSet, NULL, pInfo)) {
39,472!
1380
        tscWarn("failed to async send msg to server");
×
1381
      }
1382
      tFreeClientHbBatchReq(pReq);
39,472!
1383
      // hbClearReqInfo(pAppHbMgr);
1384
      (void)atomic_add_fetch_32(&pAppHbMgr->reportCnt, 1);
39,472✔
1385
    }
1386

1387
    if (TSDB_CODE_SUCCESS != taosThreadMutexUnlock(&clientHbMgr.lock)) {
40,821!
1388
      tscError("taosThreadMutexLock failed");
×
1389
      return NULL;
×
1390
    }
1391
    taosMsleep(HEARTBEAT_INTERVAL);
40,821✔
1392
  }
1393
  taosHashCleanup(clientHbMgr.appHbHash);
2,021✔
1394
  return NULL;
2,021✔
1395
}
1396

1397
static int32_t hbCreateThread() {
2,317✔
1398
  int32_t      code = TSDB_CODE_SUCCESS;
2,317✔
1399
  TdThreadAttr thAttr;
1400
  TSC_ERR_JRET(taosThreadAttrInit(&thAttr));
2,317!
1401
  TSC_ERR_JRET(taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE));
2,317!
1402

1403
  if (taosThreadCreate(&clientHbMgr.thread, &thAttr, hbThreadFunc, NULL) != 0) {
2,317!
1404
    terrno = TAOS_SYSTEM_ERROR(errno);
×
1405
    TSC_ERR_RET(terrno);
×
1406
  }
1407
  (void)taosThreadAttrDestroy(&thAttr);
2,317✔
1408
_return:
2,317✔
1409

1410
  if (code) {
2,317!
1411
    terrno = TAOS_SYSTEM_ERROR(errno);
×
1412
    TSC_ERR_RET(terrno);
×
1413
  }
1414

1415
  return code;
2,317✔
1416
}
1417

1418
static void hbStopThread() {
2,327✔
1419
  if (0 == atomic_load_8(&clientHbMgr.inited)) {
2,327✔
1420
    return;
10✔
1421
  }
1422
  if (atomic_val_compare_exchange_8(&clientHbMgr.threadStop, 0, 1)) {
2,317!
1423
    tscDebug("hb thread already stopped");
×
1424
    return;
×
1425
  }
1426

1427
  int32_t code = TSDB_CODE_SUCCESS;
2,317✔
1428
  // thread quit mode kill or inner exit from self-thread
1429
  if (clientHbMgr.quitByKill) {
2,317✔
1430
    code = taosThreadKill(clientHbMgr.thread, 0);
296✔
1431
    if (TSDB_CODE_SUCCESS != code) {
296!
1432
      tscError("taosThreadKill failed since %s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1433
    }
1434
  } else {
1435
    code = taosThreadJoin(clientHbMgr.thread, NULL);
2,021✔
1436
    if (TSDB_CODE_SUCCESS != code) {
2,021!
1437
      tscError("taosThreadJoin failed since %s", tstrerror(TAOS_SYSTEM_ERROR(errno)));
×
1438
    }
1439
  }
1440

1441
  tscDebug("hb thread stopped");
2,317✔
1442
}
1443

1444
int32_t appHbMgrInit(SAppInstInfo *pAppInstInfo, char *key, SAppHbMgr **pAppHbMgr) {
2,597✔
1445
  int32_t code = TSDB_CODE_SUCCESS;
2,597✔
1446
  TSC_ERR_RET(hbMgrInit());
2,597!
1447
  *pAppHbMgr = taosMemoryMalloc(sizeof(SAppHbMgr));
2,597!
1448
  if (*pAppHbMgr == NULL) {
2,597!
1449
    TSC_ERR_JRET(terrno);
×
1450
  }
1451
  // init stat
1452
  (*pAppHbMgr)->startTime = taosGetTimestampMs();
2,597✔
1453
  (*pAppHbMgr)->connKeyCnt = 0;
2,597✔
1454
  (*pAppHbMgr)->connHbFlag = 0;
2,597✔
1455
  (*pAppHbMgr)->reportCnt = 0;
2,597✔
1456
  (*pAppHbMgr)->reportBytes = 0;
2,597✔
1457
  (*pAppHbMgr)->key = taosStrdup(key);
2,597!
1458
  if ((*pAppHbMgr)->key == NULL) {
2,597!
1459
    TSC_ERR_JRET(terrno);
×
1460
  }
1461

1462
  // init app info
1463
  (*pAppHbMgr)->pAppInstInfo = pAppInstInfo;
2,597✔
1464

1465
  // init hash info
1466
  (*pAppHbMgr)->activeInfo = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
2,597✔
1467

1468
  if ((*pAppHbMgr)->activeInfo == NULL) {
2,597!
1469
    TSC_ERR_JRET(terrno);
×
1470
  }
1471

1472
  // taosHashSetFreeFp(pAppHbMgr->activeInfo, tFreeClientHbReq);
1473

1474
  TSC_ERR_JRET(taosThreadMutexLock(&clientHbMgr.lock));
2,597!
1475
  if (taosArrayPush(clientHbMgr.appHbMgrs, &(*pAppHbMgr)) == NULL) {
5,194!
1476
    code = terrno;
×
1477
    (void)taosThreadMutexUnlock(&clientHbMgr.lock);
×
1478
    goto _return;
×
1479
  }
1480
  (*pAppHbMgr)->idx = taosArrayGetSize(clientHbMgr.appHbMgrs) - 1;
2,597✔
1481
  TSC_ERR_JRET(taosThreadMutexUnlock(&clientHbMgr.lock));
2,597!
1482

1483
  return TSDB_CODE_SUCCESS;
2,597✔
1484
_return:
×
1485
  taosMemoryFree(*pAppHbMgr);
×
1486
  return code;
×
1487
}
1488

1489
void hbFreeAppHbMgr(SAppHbMgr *pTarget) {
2,597✔
1490
  void *pIter = taosHashIterate(pTarget->activeInfo, NULL);
2,597✔
1491
  while (pIter != NULL) {
3,581✔
1492
    SClientHbReq *pOneReq = pIter;
984!
1493
    tFreeClientHbReq(pOneReq);
1494
    pIter = taosHashIterate(pTarget->activeInfo, pIter);
984✔
1495
  }
1496
  taosHashCleanup(pTarget->activeInfo);
2,597✔
1497
  pTarget->activeInfo = NULL;
2,597✔
1498

1499
  taosMemoryFree(pTarget->key);
2,597!
1500
  taosMemoryFree(pTarget);
2,597!
1501
}
2,597✔
1502

1503
void hbRemoveAppHbMrg(SAppHbMgr **pAppHbMgr) {
2,597✔
1504
  int32_t code = TSDB_CODE_SUCCESS;
2,597✔
1505
  code = taosThreadMutexLock(&clientHbMgr.lock);
2,597✔
1506
  if (TSDB_CODE_SUCCESS != code) {
2,597!
1507
    tscError("failed to lock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1508
  }
1509
  int32_t mgrSize = taosArrayGetSize(clientHbMgr.appHbMgrs);
2,597✔
1510
  for (int32_t i = 0; i < mgrSize; ++i) {
2,597!
1511
    SAppHbMgr *pItem = taosArrayGetP(clientHbMgr.appHbMgrs, i);
×
1512
    if (pItem == *pAppHbMgr) {
×
1513
      hbFreeAppHbMgr(*pAppHbMgr);
×
1514
      *pAppHbMgr = NULL;
×
1515
      taosArraySet(clientHbMgr.appHbMgrs, i, pAppHbMgr);
×
1516
      break;
×
1517
    }
1518
  }
1519
  code = taosThreadMutexUnlock(&clientHbMgr.lock);
2,597✔
1520
  if (TSDB_CODE_SUCCESS != code) {
2,597!
1521
    tscError("failed to unlock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1522
  }
1523
}
2,597✔
1524

1525
void appHbMgrCleanup(void) {
2,317✔
1526
  int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
2,317✔
1527
  for (int i = 0; i < sz; i++) {
4,914✔
1528
    SAppHbMgr *pTarget = taosArrayGetP(clientHbMgr.appHbMgrs, i);
2,597✔
1529
    if (pTarget == NULL) continue;
2,597!
1530
    hbFreeAppHbMgr(pTarget);
2,597✔
1531
  }
1532
}
2,317✔
1533

1534
int32_t hbMgrInit() {
2,597✔
1535
  // init once
1536
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 0, 1);
2,597✔
1537
  if (old == 1) return 0;
2,597✔
1538

1539
  clientHbMgr.appId = tGenIdPI64();
2,317✔
1540
  tscDebug("app %" PRIx64 " initialized", clientHbMgr.appId);
2,317✔
1541

1542
  clientHbMgr.appSummary = taosHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
2,317✔
1543
  if (NULL == clientHbMgr.appSummary) {
2,317!
1544
    uError("hbMgrInit:taosHashInit error") return terrno;
×
1545
  }
1546
  clientHbMgr.appHbMgrs = taosArrayInit(0, sizeof(void *));
2,317✔
1547
  if (NULL == clientHbMgr.appHbMgrs) {
2,317!
1548
    uError("hbMgrInit:taosArrayInit error") return terrno;
×
1549
  }
1550
  TdThreadMutexAttr attr = {0};
2,317✔
1551

1552
  int ret = taosThreadMutexAttrInit(&attr);
2,317✔
1553
  if (ret != 0) {
2,317!
1554
    uError("hbMgrInit:taosThreadMutexAttrInit error") return ret;
×
1555
  }
1556

1557
  ret = taosThreadMutexAttrSetType(&attr, PTHREAD_MUTEX_RECURSIVE);
2,317✔
1558
  if (ret != 0) {
2,317!
1559
    uError("hbMgrInit:taosThreadMutexAttrSetType error") return ret;
×
1560
  }
1561

1562
  ret = taosThreadMutexInit(&clientHbMgr.lock, &attr);
2,317✔
1563
  if (ret != 0) {
2,317!
1564
    uError("hbMgrInit:taosThreadMutexInit error") return ret;
×
1565
  }
1566

1567
  ret = taosThreadMutexAttrDestroy(&attr);
2,317✔
1568
  if (ret != 0) {
2,317!
1569
    uError("hbMgrInit:taosThreadMutexAttrDestroy error") return ret;
×
1570
  }
1571

1572
  // init handle funcs
1573
  hbMgrInitHandle();
1574

1575
  // init backgroud thread
1576
  ret = hbCreateThread();
2,317✔
1577
  if (ret != 0) {
2,317!
1578
    uError("hbMgrInit:hbCreateThread error") return ret;
×
1579
  }
1580

1581
  return 0;
2,317✔
1582
}
1583

1584
void hbMgrCleanUp() {
2,327✔
1585
  hbStopThread();
2,327✔
1586

1587
  // destroy all appHbMgr
1588
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 1, 0);
2,327✔
1589
  if (old == 0) return;
2,327✔
1590

1591
  int32_t code = taosThreadMutexLock(&clientHbMgr.lock);
2,317✔
1592
  if (TSDB_CODE_SUCCESS != code) {
2,317!
1593
    tscError("failed to lock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1594
  }
1595
  appHbMgrCleanup();
2,317✔
1596
  taosArrayDestroy(clientHbMgr.appHbMgrs);
2,317✔
1597
  clientHbMgr.appHbMgrs = NULL;
2,317✔
1598
  code = taosThreadMutexUnlock(&clientHbMgr.lock);
2,317✔
1599
  if (TSDB_CODE_SUCCESS != code) {
2,317!
1600
    tscError("failed to unlock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1601
  }
1602
}
1603

1604
int32_t hbRegisterConnImpl(SAppHbMgr *pAppHbMgr, SClientHbKey connKey, int64_t clusterId) {
10,765✔
1605
  // init hash in activeinfo
1606
  void *data = taosHashGet(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
10,765✔
1607
  if (data != NULL) {
10,765!
1608
    return 0;
×
1609
  }
1610
  SClientHbReq hbReq = {0};
10,765✔
1611
  hbReq.connKey = connKey;
10,765✔
1612
  hbReq.clusterId = clusterId;
10,765✔
1613
  // hbReq.info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
1614

1615
  TSC_ERR_RET(taosHashPut(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey), &hbReq, sizeof(SClientHbReq)));
10,765!
1616

1617
  (void)atomic_add_fetch_32(&pAppHbMgr->connKeyCnt, 1);
10,765✔
1618
  return 0;
10,765✔
1619
}
1620

1621
int32_t hbRegisterConn(SAppHbMgr *pAppHbMgr, int64_t tscRefId, int64_t clusterId, int8_t connType) {
11,188✔
1622
  SClientHbKey connKey = {
11,188✔
1623
      .tscRid = tscRefId,
1624
      .connType = connType,
1625
  };
1626

1627
  switch (connType) {
11,188!
1628
    case CONN_TYPE__QUERY: {
10,765✔
1629
      return hbRegisterConnImpl(pAppHbMgr, connKey, clusterId);
10,765✔
1630
    }
1631
    case CONN_TYPE__TMQ: {
423✔
1632
      return 0;
423✔
1633
    }
1634
    default:
×
1635
      return 0;
×
1636
  }
1637
}
1638

1639
void hbDeregisterConn(STscObj *pTscObj, SClientHbKey connKey) {
10,237✔
1640
  int32_t code = taosThreadMutexLock(&clientHbMgr.lock);
10,237✔
1641
  if (TSDB_CODE_SUCCESS != code) {
10,238!
1642
    tscError("failed to lock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1643
  }
1644
  SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, pTscObj->appHbMgrIdx);
10,238✔
1645
  if (pAppHbMgr) {
10,238!
1646
    SClientHbReq *pReq = taosHashAcquire(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
10,238✔
1647
    if (pReq) {
10,238✔
1648
      tFreeClientHbReq(pReq);
1649
      code = taosHashRemove(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
9,781✔
1650
      if (TSDB_CODE_SUCCESS != code) {
9,781!
1651
        tscError("hbDeregisterConn taosHashRemove error, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1652
      }
1653
      taosHashRelease(pAppHbMgr->activeInfo, pReq);
9,781✔
1654
      (void)atomic_sub_fetch_32(&pAppHbMgr->connKeyCnt, 1);
9,781✔
1655
    }
1656
  }
1657
  code = taosThreadMutexUnlock(&clientHbMgr.lock);
10,238✔
1658
  if (TSDB_CODE_SUCCESS != code) {
10,238!
1659
    tscError("failed to unlock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1660
  }
1661
}
10,238✔
1662

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