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

taosdata / TDengine / #4473

08 Jul 2025 09:38AM UTC coverage: 62.922% (+0.7%) from 62.22%
#4473

push

travis-ci

web-flow
Merge pull request #31712 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

158525 of 321496 branches covered (49.31%)

Branch coverage included in aggregate %.

56 of 60 new or added lines in 13 files covered. (93.33%)

1333 existing lines in 67 files now uncovered.

245526 of 320647 relevant lines covered (76.57%)

17689640.25 hits per line

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

62.41
/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,
3,426✔
46
                                        SAppHbMgr *pAppHbMgr) {
47
  int32_t code = TSDB_CODE_SUCCESS;
3,426✔
48

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

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

63
    TSC_ERR_JRET(catalogUpdateUserAuthInfo(pCatalog, rsp));
3,657!
64
  }
65

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

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

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

77
static int32_t hbUpdateUserAuthInfo(SAppHbMgr *pAppHbMgr, SUserAuthBatchRsp *batchRsp) {
3,426✔
78
  int64_t clusterId = pAppHbMgr->pAppInstInfo->clusterId;
3,426✔
79
  for (int i = 0; i < TARRAY_SIZE(clientHbMgr.appHbMgrs); ++i) {
10,570✔
80
    SAppHbMgr *hbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
7,144✔
81
    if (!hbMgr || hbMgr->pAppInstInfo->clusterId != clusterId) {
7,144!
82
      continue;
22✔
83
    }
84

85
    SClientHbReq    *pReq = NULL;
7,122✔
86
    SGetUserAuthRsp *pRsp = NULL;
7,122✔
87
    while ((pReq = taosHashIterate(hbMgr->activeInfo, pReq))) {
15,649✔
88
      STscObj *pTscObj = (STscObj *)acquireTscObj(pReq->connKey.tscRid);
10,176✔
89
      if (!pTscObj) {
10,176✔
90
        continue;
1,174✔
91
      }
92

93
      if (!pRsp) {
9,002✔
94
        for (int32_t j = 0; j < TARRAY_SIZE(batchRsp->pArray); ++j) {
7,241✔
95
          SGetUserAuthRsp *rsp = TARRAY_GET_ELEM(batchRsp->pArray, j);
5,592✔
96
          if (0 == strncmp(rsp->user, pTscObj->user, TSDB_USER_LEN)) {
5,592✔
97
            pRsp = rsp;
3,745✔
98
            break;
3,745✔
99
          }
100
        }
101
        if (!pRsp) {
5,394✔
102
          releaseTscObj(pReq->connKey.tscRid);
1,649✔
103
          taosHashCancelIterate(hbMgr->activeInfo, pReq);
1,649✔
104
          break;
1,649✔
105
        }
106
      }
107

108
      if (pRsp->dropped == 1) {
7,353✔
109
        if (atomic_val_compare_exchange_8(&pTscObj->dropped, 0, 1) == 0) {
140✔
110
          if (pTscObj->userDroppedInfo.fp) {
113✔
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);
140✔
118
        continue;
140✔
119
      }
120

121
      pTscObj->authVer = pRsp->version;
7,213✔
122

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

129
      if (pTscObj->passInfo.fp) {
7,213✔
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, conn:%" PRIi64, pRsp->user, oldVer,
40!
138
                   atomic_load_32(&passInfo->ver), pTscObj->id);
139
        }
140
      }
141

142
      if (pTscObj->whiteListInfo.fp) {
7,213✔
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 ", conn:%" 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;
7,203✔
157
        int64_t         oldVer = atomic_load_64(&whiteListInfo->ver);
7,203✔
158
        atomic_store_64(&whiteListInfo->ver, pRsp->whiteListVer);
7,203✔
159
        tscDebug("update whitelist version of user %s from %" PRId64 " to %" PRId64 ", conn:%" PRIi64, pRsp->user,
7,203✔
160
                 oldVer, atomic_load_64(&whiteListInfo->ver), pTscObj->id);
161
      }
162
      releaseTscObj(pReq->connKey.tscRid);
7,213✔
163
    }
164
  }
165
  return 0;
3,426✔
166
}
167

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

175
  vgInfo->vgVersion = rsp->vgVersion;
3,876✔
176
  vgInfo->stateTs = rsp->stateTs;
3,876✔
177
  vgInfo->hashMethod = rsp->hashMethod;
3,876✔
178
  vgInfo->hashPrefix = rsp->hashPrefix;
3,876✔
179
  vgInfo->hashSuffix = rsp->hashSuffix;
3,876✔
180
  vgInfo->vgHash = taosHashInit(rsp->vgNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
3,876✔
181
  if (NULL == vgInfo->vgHash) {
3,876!
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) {
20,501✔
188
    SVgroupInfo *pInfo = taosArrayGet(rsp->pVgroupInfos, j);
16,625✔
189
    if (taosHashPut(vgInfo->vgHash, &pInfo->vgId, sizeof(int32_t), pInfo, sizeof(SVgroupInfo)) != 0) {
16,625!
190
      tscError("hash push failed, terrno:%d", terrno);
×
191
      code = terrno;
×
192
      goto _return;
×
193
    }
194
  }
195

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

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

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

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

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

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

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

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

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

247
          TSC_ERR_JRET(catalogUpdateDBVgInfo(
651!
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) {
4,657✔
255
      tscDebug("hb db cfg rsp, db:%s, cfgVersion:%d", rsp->cfgRsp->db, rsp->cfgRsp->cfgVersion);
667✔
256
      code = catalogUpdateDbCfg(pCatalog, rsp->cfgRsp->db, rsp->cfgRsp->dbId, rsp->cfgRsp);
667✔
257
      rsp->cfgRsp = NULL;
667✔
258
    }
259
    if (rsp->pTsmaRsp) {
4,657✔
260
      if (rsp->pTsmaRsp->pTsmas) {
2,648✔
261
        for (int32_t i = 0; i < rsp->pTsmaRsp->pTsmas->size; ++i) {
412✔
262
          STableTSMAInfo *pTsma = taosArrayGetP(rsp->pTsmaRsp->pTsmas, i);
250✔
263
          if (NULL == pTsma) {
250!
264
            TSC_ERR_JRET(TSDB_CODE_OUT_OF_RANGE);
×
265
          }
266
          TSC_ERR_JRET(catalogAsyncUpdateTSMA(pCatalog, &pTsma, rsp->dbTsmaVersion));
250!
267
        }
268
        taosArrayClear(rsp->pTsmaRsp->pTsmas);
162✔
269
      } else {
270
        TSC_ERR_JRET(catalogAsyncUpdateDbTsmaVersion(pCatalog, rsp->dbTsmaVersion, rsp->db, rsp->dbId));
2,486!
271
      }
272
    }
273
  }
274

275
_return:
12,132✔
276

277
  tFreeSDbHbBatchRsp(&batchRsp);
12,132✔
278
  return code;
12,132✔
279
}
280

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

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

290
  int32_t numOfMeta = taosArrayGetSize(hbRsp.pMetaRsp);
8,719✔
291
  for (int32_t i = 0; i < numOfMeta; ++i) {
9,376✔
292
    STableMetaRsp *rsp = taosArrayGet(hbRsp.pMetaRsp, i);
657✔
293
    if (NULL == rsp) {
657!
294
      code = terrno;
×
295
      goto _return;
×
296
    }
297
    if (rsp->numOfColumns < 0) {
657✔
298
      tscDebug("hb to remove stb, db:%s, stb:%s", rsp->dbFName, rsp->stbName);
634✔
299
      TSC_ERR_JRET(catalogRemoveStbMeta(pCatalog, rsp->dbFName, rsp->dbId, rsp->stbName, rsp->suid));
634!
300
    } else {
301
      tscDebug("hb to update stb, db:%s, stb:%s", rsp->dbFName, rsp->stbName);
23✔
302
      if (rsp->pSchemas[0].colId != PRIMARYKEY_TIMESTAMP_COL_ID) {
23!
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));
23!
309
    }
310
  }
311

312
  int32_t numOfIndex = taosArrayGetSize(hbRsp.pIndexRsp);
8,719✔
313
  for (int32_t i = 0; i < numOfIndex; ++i) {
8,719!
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:
8,719✔
323
  taosArrayDestroy(hbRsp.pIndexRsp);
8,719✔
324
  hbRsp.pIndexRsp = NULL;
8,719✔
325

326
  tFreeSSTbHbRsp(&hbRsp);
8,719✔
327
  return code;
8,719✔
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) {
5✔
355
    SViewMetaRsp *rsp = taosArrayGetP(hbRsp.pViewRsp, i);
1✔
356
    if (NULL == rsp) {
1!
357
      code = terrno;
×
358
      goto _return;
×
359
    }
360
    if (rsp->numOfCols < 0) {
1!
361
      tscDebug("hb to remove view, db:%s, view:%s", rsp->dbFName, rsp->name);
1!
362
      code = catalogRemoveViewMeta(pCatalog, rsp->dbFName, rsp->dbId, rsp->name, rsp->viewId);
1✔
363
      tFreeSViewMetaRsp(rsp);
1✔
364
      taosMemoryFreeClear(rsp);
1!
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);
1!
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) {
294✔
378
  int32_t code = 0;
294✔
379

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

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

390
    if (!pTsmaInfo->pFuncs) {
1!
391
      tscDebug("hb to remove tsma:%s.%s", pTsmaInfo->dbFName, pTsmaInfo->name);
1!
392
      code = catalogRemoveTSMA(pCatalog, pTsmaInfo);
1✔
393
      tFreeAndClearTableTSMAInfo(pTsmaInfo);
1✔
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);
1!
400
  }
401

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

407
static void hbProcessQueryRspKvs(int32_t kvNum, SArray *pKvs, struct SCatalog *pCatalog, SAppHbMgr *pAppHbMgr) {
20,226✔
408
  for (int32_t i = 0; i < kvNum; ++i) {
44,805✔
409
    SKv *kv = taosArrayGet(pKvs, i);
24,579✔
410
    if (NULL == kv) {
24,579!
411
      tscError("invalid hb kv, idx:%d", i);
×
412
      continue;
×
413
    }
414
    switch (kv->key) {
24,579!
415
      case HEARTBEAT_KEY_USER_AUTHINFO: {
3,426✔
416
        if (kv->valueLen <= 0 || NULL == kv->value) {
3,426!
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)) {
3,426!
421
          tscError("process user auth info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
422
          break;
×
423
        }
424
        break;
3,426✔
425
      }
426
      case HEARTBEAT_KEY_DBINFO: {
12,132✔
427
        if (kv->valueLen <= 0 || NULL == kv->value) {
12,132!
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)) {
12,132!
432
          tscError("process db info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
433
          break;
×
434
        }
435
        break;
12,132✔
436
      }
437
      case HEARTBEAT_KEY_STBINFO: {
8,719✔
438
        if (kv->valueLen <= 0 || NULL == kv->value) {
8,719!
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)) {
8,719!
443
          tscError("process stb info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
444
          break;
×
445
        }
446
        break;
8,719✔
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: {
294✔
473
        if (kv->valueLen <= 0 || !kv->value) {
294!
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)) {
294!
477
          tscError("Process tsma info response failed, len:%d, value:%p", kv->valueLen, kv->value);
×
478
        }
479
        break;
294✔
480
      }
481
      default:
×
482
        tscError("invalid hb key type:%d", kv->key);
×
483
        break;
×
484
    }
485
  }
486
}
20,226✔
487

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

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

511
          updateEpSet_s(&pTscObj->pAppInfo->mgmtEp, &pRsp->query->epSet);
145✔
512
        }
513
      }
514

515
      pTscObj->pAppInfo->totalDnodes = pRsp->query->totalDnodes;
133,649✔
516
      pTscObj->pAppInfo->onlineDnodes = pRsp->query->onlineDnodes;
133,649✔
517
      pTscObj->connId = pRsp->query->connId;
133,649✔
518
      tscTrace("connId:%u, hb rsp, dnodes %d/%d", pTscObj->connId, pTscObj->pAppInfo->onlineDnodes,
133,649✔
519
               pTscObj->pAppInfo->totalDnodes);
520

521
      if (pRsp->query->killRid) {
133,649!
UNCOV
522
        tscDebug("QID:0x%" PRIx64 ", need to be killed now", pRsp->query->killRid);
×
UNCOV
523
        SRequestObj *pRequest = acquireRequest(pRsp->query->killRid);
×
UNCOV
524
        if (NULL == pRequest) {
×
525
          tscDebug("QID:0x%" PRIx64 ", not exist to kill", pRsp->query->killRid);
×
526
        } else {
527
          taos_stop_query((TAOS_RES *)pRequest);
×
528
          (void)releaseRequest(pRsp->query->killRid);
×
529
        }
530
      }
531

532
      if (pRsp->query->killConnection) {
133,649!
UNCOV
533
        taos_close_internal(pTscObj);
×
534
      }
535

536
      if (pRsp->query->pQnodeList) {
133,649✔
537
        if (TSDB_CODE_SUCCESS != updateQnodeList(pTscObj->pAppInfo, pRsp->query->pQnodeList)) {
87,006!
UNCOV
538
          tscWarn("update qnode list failed");
×
539
        }
540
      }
541

542
      releaseTscObj(pRsp->connKey.tscRid);
133,649✔
543
    }
544
  }
545

546
  int32_t kvNum = pRsp->info ? taosArrayGetSize(pRsp->info) : 0;
140,647!
547

548
  tscDebug("hb got %d rsp kv", kvNum);
140,647✔
549

550
  if (kvNum > 0) {
140,647✔
551
    struct SCatalog *pCatalog = NULL;
20,226✔
552
    int32_t          code = catalogGetHandle(pReq->clusterId, &pCatalog);
20,226✔
553
    if (code != TSDB_CODE_SUCCESS) {
20,226!
UNCOV
554
      tscWarn("catalogGetHandle failed, clusterId:0x%" PRIx64 ", error:%s", pReq->clusterId, tstrerror(code));
×
555
    } else {
556
      hbProcessQueryRspKvs(kvNum, pRsp->info, pCatalog, pAppHbMgr);
20,226✔
557
    }
558
  }
559

560
  taosHashRelease(pAppHbMgr->activeInfo, pReq);
140,647✔
561

562
  return TSDB_CODE_SUCCESS;
140,647✔
563
}
564

565
static int32_t hbAsyncCallBack(void *param, SDataBuf *pMsg, int32_t code) {
53,735✔
566
  if (0 == atomic_load_8(&clientHbMgr.inited)) {
53,735!
UNCOV
567
    goto _return;
×
568
  }
569

570
  static int32_t    emptyRspNum = 0;
571
  int32_t           idx = *(int32_t *)param;
53,735✔
572
  SClientHbBatchRsp pRsp = {0};
53,735✔
573
  if (TSDB_CODE_SUCCESS == code) {
53,735✔
574
    code = tDeserializeSClientHbBatchRsp(pMsg->pData, pMsg->len, &pRsp);
52,173✔
575
    if (TSDB_CODE_SUCCESS != code) {
52,172✔
576
      tscError("deserialize hb rsp failed");
5!
577
    }
578
    int32_t now = taosGetTimestampSec();
52,172✔
579
    int32_t delta = abs(now - pRsp.svrTimestamp);
52,172✔
580
    if (delta > timestampDeltaLimit) {
52,172✔
581
      code = TSDB_CODE_TIME_UNSYNCED;
5✔
582
      tscError("time diff:%ds is too big", delta);
5!
583
    }
584
  }
585

586
  int32_t rspNum = taosArrayGetSize(pRsp.rsps);
53,734✔
587

588
  (void)taosThreadMutexLock(&clientHbMgr.lock);
53,732✔
589

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

600
  SAppInstInfo *pInst = pAppHbMgr->pAppInstInfo;
53,735✔
601

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

612
  pInst->serverCfg.monitorParas = pRsp.monitorParas;
52,168✔
613
  pInst->serverCfg.enableAuditDelete = pRsp.enableAuditDelete;
52,168✔
614
  pInst->serverCfg.enableStrongPass = pRsp.enableStrongPass;
52,168✔
615
  tsEnableStrongPassword = pInst->serverCfg.enableStrongPass;
52,168✔
616
  tscDebug("monitor paras from hb, clusterId:0x%" PRIx64 ", threshold:%d scope:%d", pInst->clusterId,
52,168✔
617
           pRsp.monitorParas.tsSlowLogThreshold, pRsp.monitorParas.tsSlowLogScope);
618

619
  if (rspNum) {
52,168✔
620
    tscDebug("hb got %d rsp, %d empty rsp received before", rspNum,
51,666✔
621
             atomic_val_compare_exchange_32(&emptyRspNum, emptyRspNum, 0));
622
  } else {
623
    (void)atomic_add_fetch_32(&emptyRspNum, 1);
502✔
624
  }
625

626
  for (int32_t i = 0; i < rspNum; ++i) {
193,031✔
627
    SClientHbRsp *rsp = taosArrayGet(pRsp.rsps, i);
140,863✔
628
    code = (*clientHbMgr.rspHandle[rsp->connKey.connType])(pAppHbMgr, rsp);
140,863✔
629
    if (code) {
140,863!
UNCOV
630
      break;
×
631
    }
632
  }
633

634
  (void)taosThreadMutexUnlock(&clientHbMgr.lock);
52,168✔
635

636
  tFreeClientHbBatchRsp(&pRsp);
637

638
_return:
52,168✔
639
  taosMemoryFree(pMsg->pData);
52,168!
640
  taosMemoryFree(pMsg->pEpSet);
52,168!
641
  return code;
52,168✔
642
}
643

644
int32_t hbBuildQueryDesc(SQueryHbReqBasic *hbBasic, STscObj *pObj) {
58,246✔
645
  int64_t    now = taosGetTimestampUs();
58,246✔
646
  SQueryDesc desc = {0};
58,246✔
647
  int32_t    code = 0;
58,246✔
648

649
  void *pIter = taosHashIterate(pObj->pRequests, NULL);
58,246✔
650
  while (pIter != NULL) {
119,432✔
651
    int64_t     *rid = pIter;
61,186✔
652
    SRequestObj *pRequest = acquireRequest(*rid);
61,186✔
653
    if (NULL == pRequest) {
61,186✔
654
      pIter = taosHashIterate(pObj->pRequests, pIter);
100✔
655
      continue;
100✔
656
    }
657

658
    if (pRequest->killed || 0 == pRequest->body.queryJob) {
61,086✔
659
      (void)releaseRequest(*rid);
22,584✔
660
      pIter = taosHashIterate(pObj->pRequests, pIter);
22,584✔
661
      continue;
22,584✔
662
    }
663

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

679
    if (desc.subPlanNum) {
38,502!
680
      desc.subDesc = taosArrayInit(desc.subPlanNum, sizeof(SQuerySubDesc));
38,502✔
681
      if (NULL == desc.subDesc) {
38,502!
UNCOV
682
        (void)releaseRequest(*rid);
×
UNCOV
683
        return terrno;
×
684
      }
685

686
      code = schedulerGetTasksStatus(pRequest->body.queryJob, desc.subDesc);
38,502✔
687
      if (code) {
38,502✔
688
        taosArrayDestroy(desc.subDesc);
8,624✔
689
        desc.subDesc = NULL;
8,624✔
690
      }
691
      desc.subPlanNum = taosArrayGetSize(desc.subDesc);
38,502✔
692
    } else {
UNCOV
693
      desc.subDesc = NULL;
×
694
    }
695

696
    (void)releaseRequest(*rid);
38,502✔
697
    if (NULL == taosArrayPush(hbBasic->queryDesc, &desc)) {
77,004!
UNCOV
698
      taosArrayDestroy(desc.subDesc);
×
UNCOV
699
      return terrno;
×
700
    }
701

702
    pIter = taosHashIterate(pObj->pRequests, pIter);
38,502✔
703
  }
704

705
  return code;
58,246✔
706
}
707

708
int32_t hbGetQueryBasicInfo(SClientHbKey *connKey, SClientHbReq *req) {
144,586✔
709
  STscObj *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
144,586✔
710
  if (NULL == pTscObj) {
144,586!
UNCOV
711
    tscWarn("tscObj rid 0x%" PRIx64 " not exist", connKey->tscRid);
×
UNCOV
712
    return terrno;
×
713
  }
714

715
  SQueryHbReqBasic *hbBasic = (SQueryHbReqBasic *)taosMemoryCalloc(1, sizeof(SQueryHbReqBasic));
144,586!
716
  if (NULL == hbBasic) {
144,586!
UNCOV
717
    tscError("calloc %d failed", (int32_t)sizeof(SQueryHbReqBasic));
×
UNCOV
718
    releaseTscObj(connKey->tscRid);
×
UNCOV
719
    return terrno;
×
720
  }
721

722
  hbBasic->connId = pTscObj->connId;
144,586✔
723

724
  int32_t numOfQueries = pTscObj->pRequests ? taosHashGetSize(pTscObj->pRequests) : 0;
144,586!
725
  if (numOfQueries <= 0) {
144,586✔
726
    req->query = hbBasic;
86,340✔
727
    releaseTscObj(connKey->tscRid);
86,340✔
728
    tscDebug("no queries on connection");
86,340✔
729
    return TSDB_CODE_SUCCESS;
86,340✔
730
  }
731

732
  hbBasic->queryDesc = taosArrayInit(numOfQueries, sizeof(SQueryDesc));
58,246✔
733
  if (NULL == hbBasic->queryDesc) {
58,246!
UNCOV
734
    tscWarn("taosArrayInit %d queryDesc failed", numOfQueries);
×
UNCOV
735
    releaseTscObj(connKey->tscRid);
×
UNCOV
736
    taosMemoryFree(hbBasic);
×
737
    return terrno;
×
738
  }
739

740
  int32_t code = hbBuildQueryDesc(hbBasic, pTscObj);
58,246✔
741
  if (code) {
58,246✔
742
    releaseTscObj(connKey->tscRid);
7,064✔
743
    if (hbBasic->queryDesc) {
7,064!
744
      taosArrayDestroyEx(hbBasic->queryDesc, tFreeClientHbQueryDesc);
7,064✔
745
    }
746
    taosMemoryFree(hbBasic);
7,064!
747
    return code;
7,064✔
748
  }
749

750
  req->query = hbBasic;
51,182✔
751
  releaseTscObj(connKey->tscRid);
51,182✔
752

753
  return TSDB_CODE_SUCCESS;
51,182✔
754
}
755

756
static int32_t hbGetUserAuthInfo(SClientHbKey *connKey, SHbParam *param, SClientHbReq *req) {
3,412✔
757
  STscObj *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
3,412✔
758
  if (!pTscObj) {
3,412✔
759
    tscWarn("tscObj rid 0x%" PRIx64 " not exist", connKey->tscRid);
1!
760
    return terrno;
1✔
761
  }
762

763
  int32_t code = 0;
3,411✔
764

765
  SKv  kv = {.key = HEARTBEAT_KEY_USER_AUTHINFO};
3,411✔
766
  SKv *pKv = NULL;
3,411✔
767
  if ((pKv = taosHashGet(req->info, &kv.key, sizeof(kv.key)))) {
3,411✔
768
    int32_t           userNum = pKv->valueLen / sizeof(SUserAuthVersion);
2,102✔
769
    SUserAuthVersion *userAuths = (SUserAuthVersion *)pKv->value;
2,102✔
770
    for (int32_t i = 0; i < userNum; ++i) {
2,194✔
771
      SUserAuthVersion *pUserAuth = userAuths + i;
2,191✔
772
      // both key and user exist, update version
773
      if (strncmp(pUserAuth->user, pTscObj->user, TSDB_USER_LEN) == 0) {
2,191✔
774
        pUserAuth->version = htonl(-1);  // force get userAuthInfo
2,099✔
775
        goto _return;
2,099✔
776
      }
777
    }
778
    // key exists, user not exist, append user
779
    SUserAuthVersion *qUserAuth =
6✔
780
        (SUserAuthVersion *)taosMemoryRealloc(pKv->value, (userNum + 1) * sizeof(SUserAuthVersion));
3!
781
    if (qUserAuth) {
3!
782
      tstrncpy((qUserAuth + userNum)->user, pTscObj->user, TSDB_USER_LEN);
3✔
783
      (qUserAuth + userNum)->version = htonl(-1);  // force get userAuthInfo
3✔
784
      pKv->value = qUserAuth;
3✔
785
      pKv->valueLen += sizeof(SUserAuthVersion);
3✔
786
    } else {
UNCOV
787
      code = terrno;
×
788
    }
789
    goto _return;
3✔
790
  }
791

792
  // key/user not exist, add user
793
  SUserAuthVersion *user = taosMemoryMalloc(sizeof(SUserAuthVersion));
1,309!
794
  if (!user) {
1,309!
UNCOV
795
    code = terrno;
×
UNCOV
796
    goto _return;
×
797
  }
798
  tstrncpy(user->user, pTscObj->user, TSDB_USER_LEN);
1,309✔
799
  user->version = htonl(-1);  // force get userAuthInfo
1,309✔
800
  kv.valueLen = sizeof(SUserAuthVersion);
1,309✔
801
  kv.value = user;
1,309✔
802

803
  tscDebug("hb got user auth info, valueLen:%d, user:%s, authVer:%d, tscRid:%" PRIi64, kv.valueLen, user->user,
1,309✔
804
           pTscObj->authVer, connKey->tscRid);
805

806
  if (!req->info) {
1,309!
807
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
1,309✔
808
    if (NULL == req->info) {
1,309!
UNCOV
809
      code = terrno;
×
UNCOV
810
      goto _return;
×
811
    }
812
  }
813

814
  if (taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)) != 0) {
1,309!
UNCOV
815
    taosMemoryFree(user);
×
UNCOV
816
    code = terrno ? terrno : TSDB_CODE_APP_ERROR;
×
UNCOV
817
    goto _return;
×
818
  }
819

820
_return:
1,309✔
821
  releaseTscObj(connKey->tscRid);
3,411✔
822
  if (code) {
3,411!
UNCOV
823
    tscError("hb got user auth info failed since %s", tstrerror(code));
×
824
  }
825

826
  return code;
3,411✔
827
}
828

829
int32_t hbGetExpiredUserInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
44,263✔
830
  SUserAuthVersion *users = NULL;
44,263✔
831
  uint32_t          userNum = 0;
44,263✔
832
  int32_t           code = 0;
44,263✔
833

834
  code = catalogGetExpiredUsers(pCatalog, &users, &userNum);
44,263✔
835
  if (TSDB_CODE_SUCCESS != code) {
44,263!
UNCOV
836
    return code;
×
837
  }
838

839
  if (userNum <= 0) {
44,263✔
840
    taosMemoryFree(users);
1,112!
841
    return TSDB_CODE_SUCCESS;
1,112✔
842
  }
843

844
  for (int32_t i = 0; i < userNum; ++i) {
87,212✔
845
    SUserAuthVersion *user = &users[i];
44,061✔
846
    user->version = htonl(user->version);
44,061✔
847
  }
848

849
  SKv kv = {
43,151✔
850
      .key = HEARTBEAT_KEY_USER_AUTHINFO,
851
      .valueLen = sizeof(SUserAuthVersion) * userNum,
43,151✔
852
      .value = users,
853
  };
854

855
  tscDebug("hb got %d expired users, valueLen:%d", userNum, kv.valueLen);
43,151✔
856

857
  if (NULL == req->info) {
43,151!
858
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
43,151✔
859
    if (NULL == req->info) {
43,151!
UNCOV
860
      taosMemoryFree(users);
×
UNCOV
861
      return terrno;
×
862
    }
863
  }
864

865
  code = taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
43,151✔
866
  if (TSDB_CODE_SUCCESS != code) {
43,151!
UNCOV
867
    taosMemoryFree(users);
×
UNCOV
868
    return code;
×
869
  }
870

871
  return TSDB_CODE_SUCCESS;
43,151✔
872
}
873

874
int32_t hbGetExpiredDBInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
48,904✔
875
  SDbCacheInfo *dbs = NULL;
48,904✔
876
  uint32_t      dbNum = 0;
48,904✔
877
  int32_t       code = 0;
48,904✔
878

879
  code = catalogGetExpiredDBs(pCatalog, &dbs, &dbNum);
48,904✔
880
  if (TSDB_CODE_SUCCESS != code) {
48,904!
UNCOV
881
    return code;
×
882
  }
883

884
  if (dbNum <= 0) {
48,904✔
885
    taosMemoryFree(dbs);
35,515!
886
    return TSDB_CODE_SUCCESS;
35,515✔
887
  }
888

889
  for (int32_t i = 0; i < dbNum; ++i) {
29,253✔
890
    SDbCacheInfo *db = &dbs[i];
15,864✔
891
    tscDebug("the %dth expired db:%s, dbId:%" PRId64
15,864✔
892
             ", vgVersion:%d, cfgVersion:%d, numOfTable:%d, startTs:%" PRId64,
893
             i, db->dbFName, db->dbId, db->vgVersion, db->cfgVersion, db->numOfTable, db->stateTs);
894

895
    db->dbId = htobe64(db->dbId);
15,864✔
896
    db->vgVersion = htonl(db->vgVersion);
15,864✔
897
    db->cfgVersion = htonl(db->cfgVersion);
15,864✔
898
    db->numOfTable = htonl(db->numOfTable);
15,864✔
899
    db->stateTs = htobe64(db->stateTs);
15,864✔
900
    db->tsmaVersion = htonl(db->tsmaVersion);
15,864✔
901
  }
902

903
  SKv kv = {
13,389✔
904
      .key = HEARTBEAT_KEY_DBINFO,
905
      .valueLen = sizeof(SDbCacheInfo) * dbNum,
13,389✔
906
      .value = dbs,
907
  };
908

909
  tscDebug("hb got %d expired db, valueLen:%d", dbNum, kv.valueLen);
13,389✔
910

911
  if (NULL == req->info) {
13,389✔
912
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
23✔
913
    if (NULL == req->info) {
23!
UNCOV
914
      taosMemoryFree(dbs);
×
UNCOV
915
      return terrno;
×
916
    }
917
  }
918

919
  code = taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
13,389✔
920
  if (TSDB_CODE_SUCCESS != code) {
13,389!
UNCOV
921
    taosMemoryFree(dbs);
×
UNCOV
922
    return code;
×
923
  }
924

925
  return TSDB_CODE_SUCCESS;
13,389✔
926
}
927

928
int32_t hbGetExpiredStbInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
48,904✔
929
  SSTableVersion *stbs = NULL;
48,904✔
930
  uint32_t        stbNum = 0;
48,904✔
931
  int32_t         code = 0;
48,904✔
932

933
  code = catalogGetExpiredSTables(pCatalog, &stbs, &stbNum);
48,904✔
934
  if (TSDB_CODE_SUCCESS != code) {
48,904!
UNCOV
935
    return code;
×
936
  }
937

938
  if (stbNum <= 0) {
48,904✔
939
    taosMemoryFree(stbs);
39,466!
940
    return TSDB_CODE_SUCCESS;
39,466✔
941
  }
942

943
  for (int32_t i = 0; i < stbNum; ++i) {
23,621✔
944
    SSTableVersion *stb = &stbs[i];
14,183✔
945
    stb->suid = htobe64(stb->suid);
14,183✔
946
    stb->sversion = htonl(stb->sversion);
14,183✔
947
    stb->tversion = htonl(stb->tversion);
14,183✔
948
    stb->smaVer = htonl(stb->smaVer);
14,183✔
949
  }
950

951
  SKv kv = {
9,438✔
952
      .key = HEARTBEAT_KEY_STBINFO,
953
      .valueLen = sizeof(SSTableVersion) * stbNum,
9,438✔
954
      .value = stbs,
955
  };
956

957
  tscDebug("hb got %d expired stb, valueLen:%d", stbNum, kv.valueLen);
9,438✔
958

959
  if (NULL == req->info) {
9,438!
UNCOV
960
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
×
UNCOV
961
    if (NULL == req->info) {
×
UNCOV
962
      taosMemoryFree(stbs);
×
963
      return terrno;
×
964
    }
965
  }
966

967
  code = taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
9,438✔
968
  if (TSDB_CODE_SUCCESS != code) {
9,438!
UNCOV
969
    taosMemoryFree(stbs);
×
UNCOV
970
    return code;
×
971
  }
972

973
  return TSDB_CODE_SUCCESS;
9,438✔
974
}
975

976
int32_t hbGetExpiredViewInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
48,904✔
977
  SViewVersion    *views = NULL;
48,904✔
978
  uint32_t         viewNum = 0;
48,904✔
979
  int32_t          code = 0;
48,904✔
980
  SDynViewVersion *pDynViewVer = NULL;
48,904✔
981

982
  TSC_ERR_JRET(catalogGetExpiredViews(pCatalog, &views, &viewNum, &pDynViewVer));
48,904!
983

984
  if (viewNum <= 0) {
48,904✔
985
    taosMemoryFree(views);
48,900!
986
    taosMemoryFree(pDynViewVer);
48,900!
987
    return TSDB_CODE_SUCCESS;
48,900✔
988
  }
989

990
  for (int32_t i = 0; i < viewNum; ++i) {
8✔
991
    SViewVersion *view = &views[i];
4✔
992
    view->dbId = htobe64(view->dbId);
4✔
993
    view->viewId = htobe64(view->viewId);
4✔
994
    view->version = htonl(view->version);
4✔
995
  }
996

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

999
  if (NULL == req->info) {
4!
UNCOV
1000
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
×
UNCOV
1001
    if (NULL == req->info) {
×
UNCOV
1002
      TSC_ERR_JRET(terrno);
×
1003
    }
1004
  }
1005

1006
  SKv kv = {
4✔
1007
      .key = HEARTBEAT_KEY_DYN_VIEW,
1008
      .valueLen = sizeof(SDynViewVersion),
1009
      .value = pDynViewVer,
1010
  };
1011

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

1014
  kv.key = HEARTBEAT_KEY_VIEWINFO;
4✔
1015
  kv.valueLen = sizeof(SViewVersion) * viewNum;
4✔
1016
  kv.value = views;
4✔
1017

1018
  TSC_ERR_JRET(taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)));
4!
1019
  return TSDB_CODE_SUCCESS;
4✔
UNCOV
1020
_return:
×
UNCOV
1021
  taosMemoryFree(views);
×
UNCOV
1022
  taosMemoryFree(pDynViewVer);
×
1023
  return code;
×
1024
}
1025

1026
int32_t hbGetExpiredTSMAInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *pReq) {
48,904✔
1027
  int32_t       code = 0;
48,904✔
1028
  uint32_t      tsmaNum = 0;
48,904✔
1029
  STSMAVersion *tsmas = NULL;
48,904✔
1030

1031
  code = catalogGetExpiredTsmas(pCatalog, &tsmas, &tsmaNum);
48,904✔
1032
  if (code) {
48,904!
UNCOV
1033
    taosMemoryFree(tsmas);
×
UNCOV
1034
    return code;
×
1035
  }
1036

1037
  if (tsmaNum <= 0) {
48,904✔
1038
    taosMemoryFree(tsmas);
48,600!
1039
    return TSDB_CODE_SUCCESS;
48,600✔
1040
  }
1041

1042
  for (int32_t i = 0; i < tsmaNum; ++i) {
631✔
1043
    STSMAVersion *tsma = &tsmas[i];
327✔
1044
    tsma->dbId = htobe64(tsma->dbId);
327✔
1045
    tsma->tsmaId = htobe64(tsma->tsmaId);
327✔
1046
    tsma->version = htonl(tsma->version);
327✔
1047
  }
1048

1049
  tscDebug("hb got %d expred tsmas, valueLen:%lu", tsmaNum, sizeof(STSMAVersion) * tsmaNum);
304!
1050

1051
  if (!pReq->info) {
304!
UNCOV
1052
    pReq->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
×
UNCOV
1053
    if (!pReq->info) {
×
UNCOV
1054
      taosMemoryFree(tsmas);
×
1055
      return terrno;
×
1056
    }
1057
  }
1058

1059
  SKv kv = {.key = HEARTBEAT_KEY_TSMA, .valueLen = sizeof(STSMAVersion) * tsmaNum, .value = tsmas};
304✔
1060
  code = taosHashPut(pReq->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
304✔
1061
  if (TSDB_CODE_SUCCESS != code) {
304!
UNCOV
1062
    taosMemoryFree(tsmas);
×
UNCOV
1063
    return code;
×
1064
  }
1065
  return TSDB_CODE_SUCCESS;
304✔
1066
}
1067

1068
int32_t hbGetAppInfo(int64_t clusterId, SClientHbReq *req) {
137,522✔
1069
  SAppHbReq *pApp = taosHashGet(clientHbMgr.appSummary, &clusterId, sizeof(clusterId));
137,522✔
1070
  if (NULL != pApp) {
137,522!
1071
    (void)memcpy(&req->app, pApp, sizeof(*pApp));
137,522✔
1072
  } else {
UNCOV
1073
    (void)memset(&req->app.summary, 0, sizeof(req->app.summary));
×
UNCOV
1074
    req->app.pid = taosGetPId();
×
UNCOV
1075
    req->app.appId = clientHbMgr.appId;
×
1076
    TSC_ERR_RET(taosGetAppName(req->app.name, NULL));
×
1077
  }
1078

1079
  return TSDB_CODE_SUCCESS;
137,522✔
1080
}
1081

1082
int32_t hbQueryHbReqHandle(SClientHbKey *connKey, void *param, SClientHbReq *req) {
144,586✔
1083
  int32_t   code = 0;
144,586✔
1084
  SHbParam *hbParam = (SHbParam *)param;
144,586✔
1085
  SCatalog *pCatalog = NULL;
144,586✔
1086

1087
  code = hbGetQueryBasicInfo(connKey, req);
144,586✔
1088
  if (code != TSDB_CODE_SUCCESS) {
144,586✔
1089
    tscWarn("hbGetQueryBasicInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
7,064!
1090
    return code;
7,064✔
1091
  }
1092

1093
  if (hbParam->reqCnt == 0) {
137,522✔
1094
    code = catalogGetHandle(hbParam->clusterId, &pCatalog);
48,905✔
1095
    if (code != TSDB_CODE_SUCCESS) {
48,905!
UNCOV
1096
      tscWarn("catalogGetHandle failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
UNCOV
1097
      return code;
×
1098
    }
1099

1100
    code = hbGetAppInfo(hbParam->clusterId, req);
48,905✔
1101
    if (TSDB_CODE_SUCCESS != code) {
48,905!
UNCOV
1102
      tscWarn("getAppInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
UNCOV
1103
      return code;
×
1104
    }
1105

1106
    if (!taosHashGet(clientHbMgr.appHbHash, &hbParam->clusterId, sizeof(hbParam->clusterId))) {
48,905✔
1107
      code = hbGetExpiredUserInfo(connKey, pCatalog, req);
44,263✔
1108
      if (TSDB_CODE_SUCCESS != code) {
44,263!
UNCOV
1109
        tscWarn("hbGetExpiredUserInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
UNCOV
1110
        return code;
×
1111
      }
1112
      if (clientHbMgr.appHbHash) {
44,263✔
1113
        code = taosHashPut(clientHbMgr.appHbHash, &hbParam->clusterId, sizeof(uint64_t), NULL, 0);
7,190✔
1114
        if (TSDB_CODE_SUCCESS != code) {
7,190!
UNCOV
1115
          tscWarn("hbQueryHbReqHandle put clusterId failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId,
×
1116
                  tstrerror(code));
UNCOV
1117
          return code;
×
1118
        }
1119
      }
1120
    }
1121

1122
    // invoke after hbGetExpiredUserInfo
1123
    if (2 != atomic_load_8(&hbParam->pAppHbMgr->connHbFlag)) {
48,905✔
1124
      code = hbGetUserAuthInfo(connKey, hbParam, req);
3,412✔
1125
      if (TSDB_CODE_SUCCESS != code) {
3,412✔
1126
        tscWarn("hbGetUserAuthInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
1!
1127
        return code;
1✔
1128
      }
1129
      atomic_store_8(&hbParam->pAppHbMgr->connHbFlag, 1);
3,411✔
1130
    }
1131

1132
    code = hbGetExpiredDBInfo(connKey, pCatalog, req);
48,904✔
1133
    if (TSDB_CODE_SUCCESS != code) {
48,904!
UNCOV
1134
      tscWarn("hbGetExpiredDBInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
UNCOV
1135
      return code;
×
1136
    }
1137

1138
    code = hbGetExpiredStbInfo(connKey, pCatalog, req);
48,904✔
1139
    if (TSDB_CODE_SUCCESS != code) {
48,904!
UNCOV
1140
      tscWarn("hbGetExpiredStbInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
UNCOV
1141
      return code;
×
1142
    }
1143

1144
#ifdef TD_ENTERPRISE
1145
    code = hbGetExpiredViewInfo(connKey, pCatalog, req);
48,904✔
1146
    if (TSDB_CODE_SUCCESS != code) {
48,904!
UNCOV
1147
      tscWarn("hbGetExpiredViewInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
UNCOV
1148
      return code;
×
1149
    }
1150
#endif
1151
    code = hbGetExpiredTSMAInfo(connKey, pCatalog, req);
48,904✔
1152
    if (TSDB_CODE_SUCCESS != code) {
48,904!
UNCOV
1153
      tscWarn("hbGetExpiredTSMAInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
UNCOV
1154
      return code;
×
1155
    }
1156
  } else {
1157
    code = hbGetAppInfo(hbParam->clusterId, req);
88,617✔
1158
    if (TSDB_CODE_SUCCESS != code) {
88,617!
UNCOV
1159
      tscWarn("hbGetAppInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
UNCOV
1160
      return code;
×
1161
    }
1162
  }
1163

1164
  ++hbParam->reqCnt;  // success to get catalog info
137,521✔
1165

1166
  return TSDB_CODE_SUCCESS;
137,521✔
1167
}
1168

1169
static FORCE_INLINE void hbMgrInitHandle() {
1170
  // init all handle
1171
  clientHbMgr.reqHandle[CONN_TYPE__QUERY] = hbQueryHbReqHandle;
18,101✔
1172
  clientHbMgr.reqHandle[CONN_TYPE__TMQ] = hbMqHbReqHandle;
18,101✔
1173

1174
  clientHbMgr.rspHandle[CONN_TYPE__QUERY] = hbQueryHbRspHandle;
18,101✔
1175
  clientHbMgr.rspHandle[CONN_TYPE__TMQ] = hbMqHbRspHandle;
18,101✔
1176
}
18,101✔
1177

1178
int32_t hbGatherAllInfo(SAppHbMgr *pAppHbMgr, SClientHbBatchReq **pBatchReq) {
55,728✔
1179
  *pBatchReq = taosMemoryCalloc(1, sizeof(SClientHbBatchReq));
55,728!
1180
  if (pBatchReq == NULL) {
55,728!
UNCOV
1181
    return terrno;
×
1182
  }
1183
  int32_t connKeyCnt = atomic_load_32(&pAppHbMgr->connKeyCnt);
55,728✔
1184
  (*pBatchReq)->reqs = taosArrayInit(connKeyCnt, sizeof(SClientHbReq));
55,728✔
1185
  if (!(*pBatchReq)->reqs) {
55,728!
UNCOV
1186
    tFreeClientHbBatchReq(*pBatchReq);
×
UNCOV
1187
    return terrno;
×
1188
  }
1189

1190
  int64_t  maxIpWhiteVer = 0;
55,728✔
1191
  void    *pIter = NULL;
55,728✔
1192
  SHbParam param = {0};
55,728✔
1193
  while ((pIter = taosHashIterate(pAppHbMgr->activeInfo, pIter))) {
204,379✔
1194
    SClientHbReq *pOneReq = pIter;
148,651✔
1195
    SClientHbKey *connKey = &pOneReq->connKey;
148,651✔
1196
    STscObj      *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
148,651✔
1197

1198
    if (!pTscObj || atomic_load_8(&pTscObj->dropped) == 1) {
148,651✔
1199
      if (pTscObj) releaseTscObj(connKey->tscRid);
4,065✔
1200
      continue;
4,065✔
1201
    }
1202

1203
    tstrncpy(pOneReq->userApp, pTscObj->optionInfo.userApp, sizeof(pOneReq->userApp));
144,586✔
1204
    pOneReq->userIp = pTscObj->optionInfo.userIp;
144,586✔
1205
    pOneReq->userDualIp = pTscObj->optionInfo.userDualIp;
144,586✔
1206
    pOneReq = taosArrayPush((*pBatchReq)->reqs, pOneReq);
144,586✔
1207
    if (NULL == pOneReq) {
144,586!
UNCOV
1208
      releaseTscObj(connKey->tscRid);
×
UNCOV
1209
      continue;
×
1210
    }
1211

1212
    switch (connKey->connType) {
144,586!
1213
      case CONN_TYPE__QUERY: {
144,586✔
1214
        if (param.clusterId == 0) {
144,586✔
1215
          // init
1216
          param.clusterId = pOneReq->clusterId;
55,206✔
1217
          param.pAppHbMgr = pAppHbMgr;
55,206✔
1218
          param.connHbFlag = atomic_load_8(&pAppHbMgr->connHbFlag);
55,206✔
1219
        }
1220
        break;
144,586✔
1221
      }
UNCOV
1222
      default:
×
UNCOV
1223
        break;
×
1224
    }
1225
    if (clientHbMgr.reqHandle[connKey->connType]) {
144,586!
1226
      int32_t code = (*clientHbMgr.reqHandle[connKey->connType])(connKey, &param, pOneReq);
144,586✔
1227
      if (code) {
144,586✔
1228
        tscWarn("hbGatherAllInfo failed since %s, tscRid:%" PRIi64 ", connType:%" PRIi8, tstrerror(code),
7,065!
1229
                connKey->tscRid, connKey->connType);
1230
      }
1231
    }
1232

1233
    int64_t ver = atomic_load_64(&pTscObj->whiteListInfo.ver);
144,586✔
1234
    maxIpWhiteVer = TMAX(maxIpWhiteVer, ver);
144,586✔
1235
    releaseTscObj(connKey->tscRid);
144,586✔
1236
  }
1237
  (*pBatchReq)->ipWhiteListVer = maxIpWhiteVer;
55,728✔
1238

1239
  return TSDB_CODE_SUCCESS;
55,728✔
1240
}
1241

UNCOV
1242
void hbThreadFuncUnexpectedStopped(void) { atomic_store_8(&clientHbMgr.threadStop, 2); }
×
1243

1244
void hbMergeSummary(SAppClusterSummary *dst, SAppClusterSummary *src) {
9,525✔
1245
  dst->numOfInsertsReq += src->numOfInsertsReq;
9,525✔
1246
  dst->numOfInsertRows += src->numOfInsertRows;
9,525✔
1247
  dst->insertElapsedTime += src->insertElapsedTime;
9,525✔
1248
  dst->insertBytes += src->insertBytes;
9,525✔
1249
  dst->fetchBytes += src->fetchBytes;
9,525✔
1250
  dst->queryElapsedTime += src->queryElapsedTime;
9,525✔
1251
  dst->numOfSlowQueries += src->numOfSlowQueries;
9,525✔
1252
  dst->totalRequests += src->totalRequests;
9,525✔
1253
  dst->currentRequests += src->currentRequests;
9,525✔
1254
}
9,525✔
1255

1256
int32_t hbGatherAppInfo(void) {
73,954✔
1257
  SAppHbReq req = {0};
73,954✔
1258
  int32_t   code = TSDB_CODE_SUCCESS;
73,954✔
1259
  int       sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
73,954✔
1260
  if (sz > 0) {
73,954!
1261
    req.pid = taosGetPId();
73,954✔
1262
    req.appId = clientHbMgr.appId;
73,954✔
1263
    TSC_ERR_RET(taosGetAppName(req.name, NULL));
73,954!
1264
  }
1265

1266
  taosHashClear(clientHbMgr.appSummary);
73,954✔
1267

1268
  for (int32_t i = 0; i < sz; ++i) {
157,520✔
1269
    SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
83,566✔
1270
    if (pAppHbMgr == NULL) continue;
83,566!
1271

1272
    int64_t    clusterId = pAppHbMgr->pAppInstInfo->clusterId;
83,566✔
1273
    SAppHbReq *pApp = taosHashGet(clientHbMgr.appSummary, &clusterId, sizeof(clusterId));
83,566✔
1274
    if (NULL == pApp) {
83,566✔
1275
      (void)memcpy(&req.summary, &pAppHbMgr->pAppInstInfo->summary, sizeof(req.summary));
74,041✔
1276
      req.startTime = pAppHbMgr->startTime;
74,041✔
1277
      TSC_ERR_RET(taosHashPut(clientHbMgr.appSummary, &clusterId, sizeof(clusterId), &req, sizeof(req)));
74,041!
1278
    } else {
1279
      if (pAppHbMgr->startTime < pApp->startTime) {
9,525!
UNCOV
1280
        pApp->startTime = pAppHbMgr->startTime;
×
1281
      }
1282

1283
      hbMergeSummary(&pApp->summary, &pAppHbMgr->pAppInstInfo->summary);
9,525✔
1284
    }
1285
  }
1286

1287
  return TSDB_CODE_SUCCESS;
73,954✔
1288
}
1289

1290
static void *hbThreadFunc(void *param) {
18,101✔
1291
  setThreadName("hb");
18,101✔
1292
#ifdef WINDOWS
1293
  if (taosCheckCurrentInDll()) {
1294
    atexit(hbThreadFuncUnexpectedStopped);
1295
  }
1296
#endif
1297
  while (1) {
58,463✔
1298
    if (1 == clientHbMgr.threadStop) {
76,564✔
1299
      break;
2,380✔
1300
    }
1301

1302
    if (TSDB_CODE_SUCCESS != taosThreadMutexLock(&clientHbMgr.lock)) {
74,184!
UNCOV
1303
      tscError("taosThreadMutexLock failed");
×
UNCOV
1304
      return NULL;
×
1305
    }
1306

1307
    int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
74,184✔
1308
    if (sz > 0) {
74,184✔
1309
      if (TSDB_CODE_SUCCESS != hbGatherAppInfo()) {
73,954!
UNCOV
1310
        tscError("hbGatherAppInfo failed");
×
UNCOV
1311
        return NULL;
×
1312
      }
1313
      if (sz > 1 && !clientHbMgr.appHbHash) {
73,954✔
1314
        clientHbMgr.appHbHash = taosHashInit(0, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), true, HASH_NO_LOCK);
196✔
1315
        if (NULL == clientHbMgr.appHbHash) {
196!
UNCOV
1316
          tscError("taosHashInit failed");
×
UNCOV
1317
          return NULL;
×
1318
        }
1319
      }
1320
      taosHashClear(clientHbMgr.appHbHash);
73,954✔
1321
    }
1322

1323
    for (int i = 0; i < sz; i++) {
157,750✔
1324
      SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
83,566✔
1325
      if (pAppHbMgr == NULL) {
83,566!
1326
        continue;
27,838✔
1327
      }
1328

1329
      int32_t connCnt = atomic_load_32(&pAppHbMgr->connKeyCnt);
83,566✔
1330
      if (connCnt == 0) {
83,566✔
1331
        continue;
27,838✔
1332
      }
1333
      SClientHbBatchReq *pReq = NULL;
55,728✔
1334
      int32_t            code = hbGatherAllInfo(pAppHbMgr, &pReq);
55,728✔
1335
      if (TSDB_CODE_SUCCESS != code || taosArrayGetP(clientHbMgr.appHbMgrs, i) == NULL) {
55,728!
UNCOV
1336
        terrno = code ? code : TSDB_CODE_OUT_OF_RANGE;
×
UNCOV
1337
        tFreeClientHbBatchReq(pReq);
×
UNCOV
1338
        continue;
×
1339
      }
1340
      int tlen = tSerializeSClientHbBatchReq(NULL, 0, pReq);
55,728✔
1341
      if (tlen == -1) {
55,728!
UNCOV
1342
        tFreeClientHbBatchReq(pReq);
×
UNCOV
1343
        break;
×
1344
      }
1345
      void *buf = taosMemoryMalloc(tlen);
55,728!
1346
      if (buf == NULL) {
55,728!
UNCOV
1347
        tFreeClientHbBatchReq(pReq);
×
1348
        // hbClearReqInfo(pAppHbMgr);
UNCOV
1349
        break;
×
1350
      }
1351

1352
      if (tSerializeSClientHbBatchReq(buf, tlen, pReq) == -1) {
55,728!
UNCOV
1353
        tFreeClientHbBatchReq(pReq);
×
UNCOV
1354
        taosMemoryFree(buf);
×
UNCOV
1355
        break;
×
1356
      }
1357
      SMsgSendInfo *pInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
55,728!
1358

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

1382
      SAppInstInfo *pAppInstInfo = pAppHbMgr->pAppInstInfo;
55,728✔
1383
      SEpSet        epSet = getEpSet_s(&pAppInstInfo->mgmtEp);
55,728✔
1384
      if (TSDB_CODE_SUCCESS != asyncSendMsgToServer(pAppInstInfo->pTransporter, &epSet, NULL, pInfo)) {
55,728!
UNCOV
1385
        tscWarn("failed to async send msg to server");
×
1386
      }
1387
      tFreeClientHbBatchReq(pReq);
55,728!
1388
      // hbClearReqInfo(pAppHbMgr);
1389
      (void)atomic_add_fetch_32(&pAppHbMgr->reportCnt, 1);
55,728✔
1390
    }
1391

1392
    if (TSDB_CODE_SUCCESS != taosThreadMutexUnlock(&clientHbMgr.lock)) {
74,184!
UNCOV
1393
      tscError("taosThreadMutexLock failed");
×
UNCOV
1394
      return NULL;
×
1395
    }
1396
    taosMsleep(HEARTBEAT_INTERVAL);
74,184✔
1397
  }
1398
  taosHashCleanup(clientHbMgr.appHbHash);
2,380✔
1399
  return NULL;
2,380✔
1400
}
1401

1402
static int32_t hbCreateThread() {
18,101✔
1403
  int32_t      code = TSDB_CODE_SUCCESS;
18,101✔
1404
  TdThreadAttr thAttr;
1405
  TSC_ERR_JRET(taosThreadAttrInit(&thAttr));
18,101!
1406
  TSC_ERR_JRET(taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE));
18,101!
1407
#ifdef TD_COMPACT_OS
1408
  TSC_ERR_JRET(taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL));
1409
#endif
1410

1411
  if (taosThreadCreate(&clientHbMgr.thread, &thAttr, hbThreadFunc, NULL) != 0) {
18,101!
UNCOV
1412
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
UNCOV
1413
    TSC_ERR_RET(terrno);
×
1414
  }
1415
  (void)taosThreadAttrDestroy(&thAttr);
18,101✔
1416
_return:
18,101✔
1417

1418
  if (code) {
18,101!
UNCOV
1419
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
UNCOV
1420
    TSC_ERR_RET(terrno);
×
1421
  }
1422

1423
  return code;
18,101✔
1424
}
1425

1426
static void hbStopThread() {
18,111✔
1427
  if (0 == atomic_load_8(&clientHbMgr.inited)) {
18,111✔
1428
    return;
10✔
1429
  }
1430
  if (atomic_val_compare_exchange_8(&clientHbMgr.threadStop, 0, 1)) {
18,101!
UNCOV
1431
    tscDebug("hb thread already stopped");
×
UNCOV
1432
    return;
×
1433
  }
1434

1435
  int32_t code = TSDB_CODE_SUCCESS;
18,101✔
1436
  // thread quit mode kill or inner exit from self-thread
1437
  if (clientHbMgr.quitByKill) {
18,101✔
1438
    code = taosThreadKill(clientHbMgr.thread, 0);
15,729✔
1439
    if (TSDB_CODE_SUCCESS != code) {
15,729!
UNCOV
1440
      tscError("taosThreadKill failed since %s", tstrerror(code));
×
1441
    }
1442
  } else {
1443
    code = taosThreadJoin(clientHbMgr.thread, NULL);
2,372✔
1444
    if (TSDB_CODE_SUCCESS != code) {
2,372!
UNCOV
1445
      tscError("taosThreadJoin failed since %s", tstrerror(code));
×
1446
    }
1447
  }
1448

1449
  tscDebug("hb thread stopped");
18,101✔
1450
}
1451

1452
int32_t appHbMgrInit(SAppInstInfo *pAppInstInfo, char *key, SAppHbMgr **pAppHbMgr) {
18,402✔
1453
  int32_t code = TSDB_CODE_SUCCESS;
18,402✔
1454
  TSC_ERR_RET(hbMgrInit());
18,402!
1455
  *pAppHbMgr = taosMemoryMalloc(sizeof(SAppHbMgr));
18,402!
1456
  if (*pAppHbMgr == NULL) {
18,402!
UNCOV
1457
    TSC_ERR_JRET(terrno);
×
1458
  }
1459
  // init stat
1460
  (*pAppHbMgr)->startTime = taosGetTimestampMs();
18,402✔
1461
  (*pAppHbMgr)->connKeyCnt = 0;
18,402✔
1462
  (*pAppHbMgr)->connHbFlag = 0;
18,402✔
1463
  (*pAppHbMgr)->reportCnt = 0;
18,402✔
1464
  (*pAppHbMgr)->reportBytes = 0;
18,402✔
1465
  (*pAppHbMgr)->key = taosStrdup(key);
18,402!
1466
  if ((*pAppHbMgr)->key == NULL) {
18,402!
UNCOV
1467
    TSC_ERR_JRET(terrno);
×
1468
  }
1469

1470
  // init app info
1471
  (*pAppHbMgr)->pAppInstInfo = pAppInstInfo;
18,402✔
1472

1473
  // init hash info
1474
  (*pAppHbMgr)->activeInfo = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
18,402✔
1475

1476
  if ((*pAppHbMgr)->activeInfo == NULL) {
18,402!
UNCOV
1477
    TSC_ERR_JRET(terrno);
×
1478
  }
1479

1480
  // taosHashSetFreeFp(pAppHbMgr->activeInfo, tFreeClientHbReq);
1481

1482
  TSC_ERR_JRET(taosThreadMutexLock(&clientHbMgr.lock));
18,402!
1483
  if (taosArrayPush(clientHbMgr.appHbMgrs, &(*pAppHbMgr)) == NULL) {
36,804!
UNCOV
1484
    code = terrno;
×
UNCOV
1485
    (void)taosThreadMutexUnlock(&clientHbMgr.lock);
×
UNCOV
1486
    goto _return;
×
1487
  }
1488
  (*pAppHbMgr)->idx = taosArrayGetSize(clientHbMgr.appHbMgrs) - 1;
18,402✔
1489
  TSC_ERR_JRET(taosThreadMutexUnlock(&clientHbMgr.lock));
18,402!
1490

1491
  return TSDB_CODE_SUCCESS;
18,402✔
UNCOV
1492
_return:
×
UNCOV
1493
  taosMemoryFree(*pAppHbMgr);
×
UNCOV
1494
  return code;
×
1495
}
1496

1497
void hbFreeAppHbMgr(SAppHbMgr *pTarget) {
18,402✔
1498
  void *pIter = taosHashIterate(pTarget->activeInfo, NULL);
18,402✔
1499
  while (pIter != NULL) {
20,320✔
1500
    SClientHbReq *pOneReq = pIter;
1,918!
1501
    tFreeClientHbReq(pOneReq);
1502
    pIter = taosHashIterate(pTarget->activeInfo, pIter);
1,918✔
1503
  }
1504
  taosHashCleanup(pTarget->activeInfo);
18,402✔
1505
  pTarget->activeInfo = NULL;
18,402✔
1506

1507
  taosMemoryFree(pTarget->key);
18,402!
1508
  taosMemoryFree(pTarget);
18,402!
1509
}
18,402✔
1510

1511
void hbRemoveAppHbMrg(SAppHbMgr **pAppHbMgr) {
18,402✔
1512
  int32_t code = TSDB_CODE_SUCCESS;
18,402✔
1513
  code = taosThreadMutexLock(&clientHbMgr.lock);
18,402✔
1514
  if (TSDB_CODE_SUCCESS != code) {
18,402!
UNCOV
1515
    tscError("failed to lock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1516
  }
1517
  int32_t mgrSize = taosArrayGetSize(clientHbMgr.appHbMgrs);
18,402✔
1518
  for (int32_t i = 0; i < mgrSize; ++i) {
18,402!
UNCOV
1519
    SAppHbMgr *pItem = taosArrayGetP(clientHbMgr.appHbMgrs, i);
×
UNCOV
1520
    if (pItem == *pAppHbMgr) {
×
UNCOV
1521
      hbFreeAppHbMgr(*pAppHbMgr);
×
1522
      *pAppHbMgr = NULL;
×
1523
      taosArraySet(clientHbMgr.appHbMgrs, i, pAppHbMgr);
×
1524
      break;
×
1525
    }
1526
  }
1527
  code = taosThreadMutexUnlock(&clientHbMgr.lock);
18,402✔
1528
  if (TSDB_CODE_SUCCESS != code) {
18,402!
UNCOV
1529
    tscError("failed to unlock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1530
  }
1531
}
18,402✔
1532

1533
void appHbMgrCleanup(void) {
18,101✔
1534
  int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
18,101✔
1535
  for (int i = 0; i < sz; i++) {
36,503✔
1536
    SAppHbMgr *pTarget = taosArrayGetP(clientHbMgr.appHbMgrs, i);
18,402✔
1537
    if (pTarget == NULL) continue;
18,402!
1538
    hbFreeAppHbMgr(pTarget);
18,402✔
1539
  }
1540
}
18,101✔
1541

1542
int32_t hbMgrInit() {
18,402✔
1543
  // init once
1544
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 0, 1);
18,402✔
1545
  if (old == 1) return 0;
18,402✔
1546

1547
  clientHbMgr.appId = tGenIdPI64();
18,101✔
1548
  tscInfo("app initialized, appId:0x%" PRIx64, clientHbMgr.appId);
18,101!
1549

1550
  clientHbMgr.appSummary = taosHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
18,101✔
1551
  if (NULL == clientHbMgr.appSummary) {
18,101!
UNCOV
1552
    uError("hbMgrInit:taosHashInit error") return terrno;
×
1553
  }
1554
  clientHbMgr.appHbMgrs = taosArrayInit(0, sizeof(void *));
18,101✔
1555
  if (NULL == clientHbMgr.appHbMgrs) {
18,101!
UNCOV
1556
    uError("hbMgrInit:taosArrayInit error") return terrno;
×
1557
  }
1558
  TdThreadMutexAttr attr = {0};
18,101✔
1559

1560
  int ret = taosThreadMutexAttrInit(&attr);
18,101✔
1561
  if (ret != 0) {
18,101!
UNCOV
1562
    uError("hbMgrInit:taosThreadMutexAttrInit error") return ret;
×
1563
  }
1564

1565
  ret = taosThreadMutexAttrSetType(&attr, PTHREAD_MUTEX_RECURSIVE);
18,101✔
1566
  if (ret != 0) {
18,101!
UNCOV
1567
    uError("hbMgrInit:taosThreadMutexAttrSetType error") return ret;
×
1568
  }
1569

1570
  ret = taosThreadMutexInit(&clientHbMgr.lock, &attr);
18,101✔
1571
  if (ret != 0) {
18,101!
UNCOV
1572
    uError("hbMgrInit:taosThreadMutexInit error") return ret;
×
1573
  }
1574

1575
  ret = taosThreadMutexAttrDestroy(&attr);
18,101✔
1576
  if (ret != 0) {
18,101!
UNCOV
1577
    uError("hbMgrInit:taosThreadMutexAttrDestroy error") return ret;
×
1578
  }
1579

1580
  // init handle funcs
1581
  hbMgrInitHandle();
1582

1583
  // init backgroud thread
1584
  ret = hbCreateThread();
18,101✔
1585
  if (ret != 0) {
18,101!
UNCOV
1586
    uError("hbMgrInit:hbCreateThread error") return ret;
×
1587
  }
1588

1589
  return 0;
18,101✔
1590
}
1591

1592
void hbMgrCleanUp() {
18,111✔
1593
  hbStopThread();
18,111✔
1594

1595
  // destroy all appHbMgr
1596
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 1, 0);
18,111✔
1597
  if (old == 0) return;
18,111✔
1598

1599
  int32_t code = taosThreadMutexLock(&clientHbMgr.lock);
18,101✔
1600
  if (TSDB_CODE_SUCCESS != code) {
18,101!
UNCOV
1601
    tscError("failed to lock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1602
  }
1603
  appHbMgrCleanup();
18,101✔
1604
  taosArrayDestroy(clientHbMgr.appHbMgrs);
18,101✔
1605
  clientHbMgr.appHbMgrs = NULL;
18,101✔
1606
  code = taosThreadMutexUnlock(&clientHbMgr.lock);
18,101✔
1607
  if (TSDB_CODE_SUCCESS != code) {
18,101!
UNCOV
1608
    tscError("failed to unlock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1609
  }
1610
}
1611

1612
int32_t hbRegisterConnImpl(SAppHbMgr *pAppHbMgr, SClientHbKey connKey, int64_t clusterId) {
32,240✔
1613
  // init hash in activeinfo
1614
  void *data = taosHashGet(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
32,240✔
1615
  if (data != NULL) {
32,240!
UNCOV
1616
    return 0;
×
1617
  }
1618
  SClientHbReq hbReq = {0};
32,240✔
1619
  hbReq.connKey = connKey;
32,240✔
1620
  hbReq.clusterId = clusterId;
32,240✔
1621
  // hbReq.info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
1622

1623
  TSC_ERR_RET(taosHashPut(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey), &hbReq, sizeof(SClientHbReq)));
32,240!
1624

1625
  (void)atomic_add_fetch_32(&pAppHbMgr->connKeyCnt, 1);
32,240✔
1626
  return 0;
32,240✔
1627
}
1628

1629
int32_t hbRegisterConn(SAppHbMgr *pAppHbMgr, int64_t tscRefId, int64_t clusterId, int8_t connType) {
32,672✔
1630
  SClientHbKey connKey = {
32,672✔
1631
      .tscRid = tscRefId,
1632
      .connType = connType,
1633
  };
1634

1635
  switch (connType) {
32,672!
1636
    case CONN_TYPE__QUERY: {
32,240✔
1637
      return hbRegisterConnImpl(pAppHbMgr, connKey, clusterId);
32,240✔
1638
    }
1639
    case CONN_TYPE__TMQ: {
432✔
1640
      return 0;
432✔
1641
    }
UNCOV
1642
    default:
×
UNCOV
1643
      return 0;
×
1644
  }
1645
}
1646

1647
void hbDeregisterConn(STscObj *pTscObj, SClientHbKey connKey) {
30,806✔
1648
  int32_t code = taosThreadMutexLock(&clientHbMgr.lock);
30,806✔
1649
  if (TSDB_CODE_SUCCESS != code) {
30,809!
UNCOV
1650
    tscError("failed to lock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1651
  }
1652
  SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, pTscObj->appHbMgrIdx);
30,809✔
1653
  if (pAppHbMgr) {
30,809!
1654
    SClientHbReq *pReq = taosHashAcquire(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
30,809✔
1655
    if (pReq) {
30,809✔
1656
      tFreeClientHbReq(pReq);
1657
      code = taosHashRemove(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
30,322✔
1658
      if (TSDB_CODE_SUCCESS != code) {
30,322!
UNCOV
1659
        tscError("hbDeregisterConn taosHashRemove error, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1660
      }
1661
      taosHashRelease(pAppHbMgr->activeInfo, pReq);
30,322✔
1662
      (void)atomic_sub_fetch_32(&pAppHbMgr->connKeyCnt, 1);
30,322✔
1663
    }
1664
  }
1665
  code = taosThreadMutexUnlock(&clientHbMgr.lock);
30,809✔
1666
  if (TSDB_CODE_SUCCESS != code) {
30,809!
UNCOV
1667
    tscError("failed to unlock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1668
  }
1669
}
30,809✔
1670

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

© 2025 Coveralls, Inc