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

taosdata / TDengine / #4469

08 Jul 2025 09:38AM UTC coverage: 62.22% (-1.2%) from 63.381%
#4469

push

travis-ci

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

merge: from main to 3.0 branch

153678 of 316510 branches covered (48.55%)

Branch coverage included in aggregate %.

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

5035 existing lines in 221 files now uncovered.

238955 of 314529 relevant lines covered (75.97%)

6273248.0 hits per line

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

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

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

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

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

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

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

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

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

85
    SClientHbReq    *pReq = NULL;
6,845✔
86
    SGetUserAuthRsp *pRsp = NULL;
6,845✔
87
    while ((pReq = taosHashIterate(hbMgr->activeInfo, pReq))) {
13,126✔
88
      STscObj *pTscObj = (STscObj *)acquireTscObj(pReq->connKey.tscRid);
7,880✔
89
      if (!pTscObj) {
7,880✔
90
        continue;
1,144✔
91
      }
92

93
      if (!pRsp) {
6,736✔
94
        for (int32_t j = 0; j < TARRAY_SIZE(batchRsp->pArray); ++j) {
6,915✔
95
          SGetUserAuthRsp *rsp = TARRAY_GET_ELEM(batchRsp->pArray, j);
5,316✔
96
          if (0 == strncmp(rsp->user, pTscObj->user, TSDB_USER_LEN)) {
5,316✔
97
            pRsp = rsp;
3,519✔
98
            break;
3,519✔
99
          }
100
        }
101
        if (!pRsp) {
5,118✔
102
          releaseTscObj(pReq->connKey.tscRid);
1,599✔
103
          taosHashCancelIterate(hbMgr->activeInfo, pReq);
1,599✔
104
          break;
1,599✔
105
        }
106
      }
107

108
      if (pRsp->dropped == 1) {
5,137✔
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;
4,997✔
122

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

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

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

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

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

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

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

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

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

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

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

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

247
          TSC_ERR_JRET(catalogUpdateDBVgInfo(
583!
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,441✔
255
      tscDebug("hb db cfg rsp, db:%s, cfgVersion:%d", rsp->cfgRsp->db, rsp->cfgRsp->cfgVersion);
632✔
256
      code = catalogUpdateDbCfg(pCatalog, rsp->cfgRsp->db, rsp->cfgRsp->dbId, rsp->cfgRsp);
632✔
257
      rsp->cfgRsp = NULL;
632✔
258
    }
259
    if (rsp->pTsmaRsp) {
4,441✔
260
      if (rsp->pTsmaRsp->pTsmas) {
2,578✔
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,416!
271
      }
272
    }
273
  }
274

275
_return:
10,990✔
276

277
  tFreeSDbHbBatchRsp(&batchRsp);
10,990✔
278
  return code;
10,990✔
279
}
280

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

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

290
  int32_t numOfMeta = taosArrayGetSize(hbRsp.pMetaRsp);
7,899✔
291
  for (int32_t i = 0; i < numOfMeta; ++i) {
8,534✔
292
    STableMetaRsp *rsp = taosArrayGet(hbRsp.pMetaRsp, i);
635✔
293
    if (NULL == rsp) {
635!
294
      code = terrno;
×
295
      goto _return;
×
296
    }
297
    if (rsp->numOfColumns < 0) {
635✔
298
      tscDebug("hb to remove stb, db:%s, stb:%s", rsp->dbFName, rsp->stbName);
612✔
299
      TSC_ERR_JRET(catalogRemoveStbMeta(pCatalog, rsp->dbFName, rsp->dbId, rsp->stbName, rsp->suid));
612!
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);
7,899✔
313
  for (int32_t i = 0; i < numOfIndex; ++i) {
7,899!
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:
7,899✔
323
  taosArrayDestroy(hbRsp.pIndexRsp);
7,899✔
324
  hbRsp.pIndexRsp = NULL;
7,899✔
325

326
  tFreeSSTbHbRsp(&hbRsp);
7,899✔
327
  return code;
7,899✔
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) {
18,465✔
408
  for (int32_t i = 0; i < kvNum; ++i) {
40,855✔
409
    SKv *kv = taosArrayGet(pKvs, i);
22,390✔
410
    if (NULL == kv) {
22,390!
411
      tscError("invalid hb kv, idx:%d", i);
×
412
      continue;
×
413
    }
414
    switch (kv->key) {
22,390!
415
      case HEARTBEAT_KEY_USER_AUTHINFO: {
3,199✔
416
        if (kv->valueLen <= 0 || NULL == kv->value) {
3,199!
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,199!
421
          tscError("process user auth info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
422
          break;
×
423
        }
424
        break;
3,199✔
425
      }
426
      case HEARTBEAT_KEY_DBINFO: {
10,990✔
427
        if (kv->valueLen <= 0 || NULL == kv->value) {
10,990!
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)) {
10,990!
432
          tscError("process db info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
433
          break;
×
434
        }
435
        break;
10,990✔
436
      }
437
      case HEARTBEAT_KEY_STBINFO: {
7,899✔
438
        if (kv->valueLen <= 0 || NULL == kv->value) {
7,899!
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)) {
7,899!
443
          tscError("process stb info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
444
          break;
×
445
        }
446
        break;
7,899✔
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
}
18,465✔
487

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

496
  if (pRsp->query) {
63,863✔
497
    STscObj *pTscObj = (STscObj *)acquireTscObj(pRsp->connKey.tscRid);
57,554✔
498
    if (NULL == pTscObj) {
57,554✔
499
      tscDebug("tscObj rid %" PRIx64 " not exist", pRsp->connKey.tscRid);
2!
500
    } else {
501
      if (pRsp->query->totalDnodes > 1) {
57,552✔
502
        SEpSet  originEpset = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
13,476✔
503
        if (!isEpsetEqual(&originEpset, &pRsp->query->epSet)) {
13,476✔
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;
57,552✔
516
      pTscObj->pAppInfo->onlineDnodes = pRsp->query->onlineDnodes;
57,552✔
517
      pTscObj->connId = pRsp->query->connId;
57,552✔
518
      tscTrace("connId:%u, hb rsp, dnodes %d/%d", pTscObj->connId, pTscObj->pAppInfo->onlineDnodes,
57,552✔
519
               pTscObj->pAppInfo->totalDnodes);
520

521
      if (pRsp->query->killRid) {
57,552!
522
        tscDebug("QID:0x%" PRIx64 ", need to be killed now", pRsp->query->killRid);
×
523
        SRequestObj *pRequest = acquireRequest(pRsp->query->killRid);
×
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) {
57,552!
533
        taos_close_internal(pTscObj);
×
534
      }
535

536
      if (pRsp->query->pQnodeList) {
57,552✔
537
        if (TSDB_CODE_SUCCESS != updateQnodeList(pTscObj->pAppInfo, pRsp->query->pQnodeList)) {
17,930!
538
          tscWarn("update qnode list failed");
×
539
        }
540
      }
541

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

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

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

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

560
  taosHashRelease(pAppHbMgr->activeInfo, pReq);
63,863✔
561

562
  return TSDB_CODE_SUCCESS;
63,863✔
563
}
564

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

570
  static int32_t    emptyRspNum = 0;
571
  int32_t           idx = *(int32_t *)param;
48,948✔
572
  SClientHbBatchRsp pRsp = {0};
48,948✔
573
  if (TSDB_CODE_SUCCESS == code) {
48,948✔
574
    code = tDeserializeSClientHbBatchRsp(pMsg->pData, pMsg->len, &pRsp);
47,386✔
575
    if (TSDB_CODE_SUCCESS != code) {
47,385✔
576
      tscError("deserialize hb rsp failed");
5!
577
    }
578
    int32_t now = taosGetTimestampSec();
47,385✔
579
    int32_t delta = abs(now - pRsp.svrTimestamp);
47,385✔
580
    if (delta > timestampDeltaLimit) {
47,385✔
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);
48,947✔
587

588
  (void)taosThreadMutexLock(&clientHbMgr.lock);
48,945✔
589

590
  SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, idx);
48,948✔
591
  if (pAppHbMgr == NULL) {
48,948!
592
    (void)taosThreadMutexUnlock(&clientHbMgr.lock);
×
593
    tscError("appHbMgr not exist, idx:%d", idx);
×
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;
48,948✔
601

602
  if (code != 0) {
48,948✔
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;
47,381✔
613
  pInst->serverCfg.enableAuditDelete = pRsp.enableAuditDelete;
47,381✔
614
  pInst->serverCfg.enableStrongPass = pRsp.enableStrongPass;
47,381✔
615
  tsEnableStrongPassword = pInst->serverCfg.enableStrongPass;
47,381✔
616
  tscDebug("monitor paras from hb, clusterId:0x%" PRIx64 ", threshold:%d scope:%d", pInst->clusterId,
47,381✔
617
           pRsp.monitorParas.tsSlowLogThreshold, pRsp.monitorParas.tsSlowLogScope);
618

619
  if (rspNum) {
47,381✔
620
    tscDebug("hb got %d rsp, %d empty rsp received before", rspNum,
46,891✔
621
             atomic_val_compare_exchange_32(&emptyRspNum, emptyRspNum, 0));
622
  } else {
623
    (void)atomic_add_fetch_32(&emptyRspNum, 1);
490✔
624
  }
625

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

634
  (void)taosThreadMutexUnlock(&clientHbMgr.lock);
47,381✔
635

636
  tFreeClientHbBatchRsp(&pRsp);
637

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

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

649
  void *pIter = taosHashIterate(pObj->pRequests, NULL);
45,328✔
650
  while (pIter != NULL) {
92,838✔
651
    int64_t     *rid = pIter;
47,510✔
652
    SRequestObj *pRequest = acquireRequest(*rid);
47,510✔
653
    if (NULL == pRequest) {
47,510✔
654
      pIter = taosHashIterate(pObj->pRequests, pIter);
8✔
655
      continue;
8✔
656
    }
657

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

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

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

686
      code = schedulerGetTasksStatus(pRequest->body.queryJob, desc.subDesc);
32,313✔
687
      if (code) {
32,313✔
688
        taosArrayDestroy(desc.subDesc);
7,933✔
689
        desc.subDesc = NULL;
7,933✔
690
      }
691
      desc.subPlanNum = taosArrayGetSize(desc.subDesc);
32,313✔
692
    } else {
693
      desc.subDesc = NULL;
×
694
    }
695

696
    (void)releaseRequest(*rid);
32,313✔
697
    if (NULL == taosArrayPush(hbBasic->queryDesc, &desc)) {
64,626!
698
      taosArrayDestroy(desc.subDesc);
×
699
      return terrno;
×
700
    }
701

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

705
  return code;
45,328✔
706
}
707

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

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

722
  hbBasic->connId = pTscObj->connId;
67,796✔
723

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

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

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

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

753
  return TSDB_CODE_SUCCESS;
38,951✔
754
}
755

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

763
  int32_t code = 0;
3,228✔
764

765
  SKv  kv = {.key = HEARTBEAT_KEY_USER_AUTHINFO};
3,228✔
766
  SKv *pKv = NULL;
3,228✔
767
  if ((pKv = taosHashGet(req->info, &kv.key, sizeof(kv.key)))) {
3,228✔
768
    int32_t           userNum = pKv->valueLen / sizeof(SUserAuthVersion);
2,038✔
769
    SUserAuthVersion *userAuths = (SUserAuthVersion *)pKv->value;
2,038✔
770
    for (int32_t i = 0; i < userNum; ++i) {
2,130✔
771
      SUserAuthVersion *pUserAuth = userAuths + i;
2,127✔
772
      // both key and user exist, update version
773
      if (strncmp(pUserAuth->user, pTscObj->user, TSDB_USER_LEN) == 0) {
2,127✔
774
        pUserAuth->version = htonl(-1);  // force get userAuthInfo
2,035✔
775
        goto _return;
2,035✔
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 {
787
      code = terrno;
×
788
    }
789
    goto _return;
3✔
790
  }
791

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

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

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

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

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

826
  return code;
3,228✔
827
}
828

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

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

839
  if (userNum <= 0) {
39,669✔
840
    taosMemoryFree(users);
972!
841
    return TSDB_CODE_SUCCESS;
972✔
842
  }
843

844
  for (int32_t i = 0; i < userNum; ++i) {
78,239✔
845
    SUserAuthVersion *user = &users[i];
39,542✔
846
    user->version = htonl(user->version);
39,542✔
847
  }
848

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

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

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

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

871
  return TSDB_CODE_SUCCESS;
38,697✔
872
}
873

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

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

884
  if (dbNum <= 0) {
44,245✔
885
    taosMemoryFree(dbs);
31,998!
886
    return TSDB_CODE_SUCCESS;
31,998✔
887
  }
888

889
  for (int32_t i = 0; i < dbNum; ++i) {
26,915✔
890
    SDbCacheInfo *db = &dbs[i];
14,668✔
891
    tscDebug("the %dth expired db:%s, dbId:%" PRId64
14,668✔
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);
14,668✔
896
    db->vgVersion = htonl(db->vgVersion);
14,668✔
897
    db->cfgVersion = htonl(db->cfgVersion);
14,668✔
898
    db->numOfTable = htonl(db->numOfTable);
14,668✔
899
    db->stateTs = htobe64(db->stateTs);
14,668✔
900
    db->tsmaVersion = htonl(db->tsmaVersion);
14,668✔
901
  }
902

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

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

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

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

925
  return TSDB_CODE_SUCCESS;
12,247✔
926
}
927

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

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

938
  if (stbNum <= 0) {
44,245✔
939
    taosMemoryFree(stbs);
35,627!
940
    return TSDB_CODE_SUCCESS;
35,627✔
941
  }
942

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

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

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

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

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

973
  return TSDB_CODE_SUCCESS;
8,618✔
974
}
975

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

982
  TSC_ERR_JRET(catalogGetExpiredViews(pCatalog, &views, &viewNum, &pDynViewVer));
44,245!
983

984
  if (viewNum <= 0) {
44,245✔
985
    taosMemoryFree(views);
44,241!
986
    taosMemoryFree(pDynViewVer);
44,241!
987
    return TSDB_CODE_SUCCESS;
44,241✔
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!
1000
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
×
1001
    if (NULL == req->info) {
×
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✔
1020
_return:
×
1021
  taosMemoryFree(views);
×
1022
  taosMemoryFree(pDynViewVer);
×
1023
  return code;
×
1024
}
1025

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

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

1037
  if (tsmaNum <= 0) {
44,245✔
1038
    taosMemoryFree(tsmas);
43,941!
1039
    return TSDB_CODE_SUCCESS;
43,941✔
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!
1052
    pReq->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
×
1053
    if (!pReq->info) {
×
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!
1062
    taosMemoryFree(tsmas);
×
1063
    return code;
×
1064
  }
1065
  return TSDB_CODE_SUCCESS;
304✔
1066
}
1067

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

1079
  return TSDB_CODE_SUCCESS;
61,419✔
1080
}
1081

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

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

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

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

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

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

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

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

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

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

1166
  return TSDB_CODE_SUCCESS;
61,419✔
1167
}
1168

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

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

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

1190
  int64_t  maxIpWhiteVer = 0;
50,941✔
1191
  void    *pIter = NULL;
50,941✔
1192
  SHbParam param = {0};
50,941✔
1193
  while ((pIter = taosHashIterate(pAppHbMgr->activeInfo, pIter))) {
120,941✔
1194
    SClientHbReq *pOneReq = pIter;
70,000✔
1195
    SClientHbKey *connKey = &pOneReq->connKey;
70,000✔
1196
    STscObj      *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
70,000✔
1197

1198
    if (!pTscObj || atomic_load_8(&pTscObj->dropped) == 1) {
70,000✔
1199
      if (pTscObj) releaseTscObj(connKey->tscRid);
2,204✔
1200
      continue;
2,204✔
1201
    }
1202

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

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

1233
    int64_t ver = atomic_load_64(&pTscObj->whiteListInfo.ver);
67,796✔
1234
    maxIpWhiteVer = TMAX(maxIpWhiteVer, ver);
67,796✔
1235
    releaseTscObj(connKey->tscRid);
67,796✔
1236
  }
1237
  (*pBatchReq)->ipWhiteListVer = maxIpWhiteVer;
50,941✔
1238

1239
  return TSDB_CODE_SUCCESS;
50,941✔
1240
}
1241

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

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

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

1266
  taosHashClear(clientHbMgr.appSummary);
68,147✔
1267

1268
  for (int32_t i = 0; i < sz; ++i) {
145,840✔
1269
    SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
77,693✔
1270
    if (pAppHbMgr == NULL) continue;
77,693!
1271

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

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

1287
  return TSDB_CODE_SUCCESS;
68,147✔
1288
}
1289

1290
static void *hbThreadFunc(void *param) {
16,958✔
1291
  setThreadName("hb");
16,958✔
1292
#ifdef WINDOWS
1293
  if (taosCheckCurrentInDll()) {
1294
    atexit(hbThreadFuncUnexpectedStopped);
1295
  }
1296
#endif
1297
  while (1) {
53,369✔
1298
    if (1 == clientHbMgr.threadStop) {
70,327✔
1299
      break;
2,011✔
1300
    }
1301

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

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

1323
    for (int i = 0; i < sz; i++) {
146,009✔
1324
      SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
77,693✔
1325
      if (pAppHbMgr == NULL) {
77,693!
1326
        continue;
26,752✔
1327
      }
1328

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

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

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

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

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

1402
static int32_t hbCreateThread() {
16,958✔
1403
  int32_t      code = TSDB_CODE_SUCCESS;
16,958✔
1404
  TdThreadAttr thAttr;
1405
  TSC_ERR_JRET(taosThreadAttrInit(&thAttr));
16,958!
1406
  TSC_ERR_JRET(taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE));
16,958!
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) {
16,958!
1412
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
1413
    TSC_ERR_RET(terrno);
×
1414
  }
1415
  (void)taosThreadAttrDestroy(&thAttr);
16,958✔
1416
_return:
16,958✔
1417

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

1423
  return code;
16,958✔
1424
}
1425

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

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

1449
  tscDebug("hb thread stopped");
16,958✔
1450
}
1451

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

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

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

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

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

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

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

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

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

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

1533
void appHbMgrCleanup(void) {
16,958✔
1534
  int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
16,958✔
1535
  for (int i = 0; i < sz; i++) {
34,216✔
1536
    SAppHbMgr *pTarget = taosArrayGetP(clientHbMgr.appHbMgrs, i);
17,258✔
1537
    if (pTarget == NULL) continue;
17,258!
1538
    hbFreeAppHbMgr(pTarget);
17,258✔
1539
  }
1540
}
16,958✔
1541

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

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

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

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

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

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

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

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

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

1589
  return 0;
16,958✔
1590
}
1591

1592
void hbMgrCleanUp() {
16,968✔
1593
  hbStopThread();
16,968✔
1594

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

1599
  int32_t code = taosThreadMutexLock(&clientHbMgr.lock);
16,958✔
1600
  if (TSDB_CODE_SUCCESS != code) {
16,958!
1601
    tscError("failed to lock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1602
  }
1603
  appHbMgrCleanup();
16,958✔
1604
  taosArrayDestroy(clientHbMgr.appHbMgrs);
16,958✔
1605
  clientHbMgr.appHbMgrs = NULL;
16,958✔
1606
  code = taosThreadMutexUnlock(&clientHbMgr.lock);
16,958✔
1607
  if (TSDB_CODE_SUCCESS != code) {
16,958!
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) {
23,995✔
1613
  // init hash in activeinfo
1614
  void *data = taosHashGet(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
23,995✔
1615
  if (data != NULL) {
23,995!
1616
    return 0;
×
1617
  }
1618
  SClientHbReq hbReq = {0};
23,995✔
1619
  hbReq.connKey = connKey;
23,995✔
1620
  hbReq.clusterId = clusterId;
23,995✔
1621
  // hbReq.info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
1622

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

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

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

1635
  switch (connType) {
24,413!
1636
    case CONN_TYPE__QUERY: {
23,995✔
1637
      return hbRegisterConnImpl(pAppHbMgr, connKey, clusterId);
23,995✔
1638
    }
1639
    case CONN_TYPE__TMQ: {
418✔
1640
      return 0;
418✔
1641
    }
1642
    default:
×
1643
      return 0;
×
1644
  }
1645
}
1646

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