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

taosdata / TDengine / #3796

31 Mar 2025 10:39AM UTC coverage: 30.372% (-7.1%) from 37.443%
#3796

push

travis-ci

happyguoxy
test:add test cases

69287 of 309062 branches covered (22.42%)

Branch coverage included in aggregate %.

118044 of 307720 relevant lines covered (38.36%)

278592.15 hits per line

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

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

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

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

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

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

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

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

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

85
    SClientHbReq    *pReq = NULL;
1,944✔
86
    SGetUserAuthRsp *pRsp = NULL;
1,944✔
87
    while ((pReq = taosHashIterate(hbMgr->activeInfo, pReq))) {
2,467✔
88
      STscObj *pTscObj = (STscObj *)acquireTscObj(pReq->connKey.tscRid);
1,432✔
89
      if (!pTscObj) {
1,432✔
90
        continue;
51✔
91
      }
92

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

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

121
      pTscObj->authVer = pRsp->version;
412✔
122

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

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

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

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

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

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

202
  *pInfo = vgInfo;
32✔
203
  return code;
32✔
204
}
205

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

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

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

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

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

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

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

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

275
_return:
198✔
276

277
  tFreeSDbHbBatchRsp(&batchRsp);
198✔
278
  return code;
198✔
279
}
280

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

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

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

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

326
  tFreeSSTbHbRsp(&hbRsp);
110✔
327
  return code;
110✔
328
}
329

330
static int32_t hbProcessDynViewRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
×
331
  return catalogUpdateDynViewVer(pCatalog, (SDynViewVersion *)value);
×
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) {
×
344
  int32_t code = TSDB_CODE_SUCCESS;
×
345

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

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

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

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

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

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

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

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

407
static void hbProcessQueryRspKvs(int32_t kvNum, SArray *pKvs, struct SCatalog *pCatalog, SAppHbMgr *pAppHbMgr) {
538✔
408
  for (int32_t i = 0; i < kvNum; ++i) {
1,190✔
409
    SKv *kv = taosArrayGet(pKvs, i);
652✔
410
    if (NULL == kv) {
652!
411
      tscError("invalid hb kv, idx:%d", i);
×
412
      continue;
×
413
    }
414
    switch (kv->key) {
652!
415
      case HEARTBEAT_KEY_USER_AUTHINFO: {
344✔
416
        if (kv->valueLen <= 0 || NULL == kv->value) {
344!
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)) {
344!
421
          tscError("process user auth info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
422
          break;
×
423
        }
424
        break;
344✔
425
      }
426
      case HEARTBEAT_KEY_DBINFO: {
198✔
427
        if (kv->valueLen <= 0 || NULL == kv->value) {
198!
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)) {
198!
432
          tscError("process db info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
433
          break;
×
434
        }
435
        break;
198✔
436
      }
437
      case HEARTBEAT_KEY_STBINFO: {
110✔
438
        if (kv->valueLen <= 0 || NULL == kv->value) {
110!
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)) {
110!
443
          tscError("process stb info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
444
          break;
×
445
        }
446
        break;
110✔
447
      }
448
#ifdef TD_ENTERPRISE
449
      case HEARTBEAT_KEY_DYN_VIEW: {
×
450
        if (kv->valueLen <= 0 || NULL == kv->value) {
×
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)) {
×
455
          tscError("Process dyn view response failed, len:%d, value:%p", kv->valueLen, kv->value);
×
456
          break;
×
457
        }
458
        break;
×
459
      }
460
      case HEARTBEAT_KEY_VIEWINFO: {
×
461
        if (kv->valueLen <= 0 || NULL == kv->value) {
×
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)) {
×
466
          tscError("Process view info response failed, len:%d, value:%p", kv->valueLen, kv->value);
×
467
          break;
×
468
        }
469
        break;
×
470
      }
471
#endif
472
      case HEARTBEAT_KEY_TSMA: {
×
473
        if (kv->valueLen <= 0 || !kv->value) {
×
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)) {
×
477
          tscError("Process tsma info response failed, len:%d, value:%p", kv->valueLen, kv->value);
×
478
        }
479
        break;
×
480
      }
481
      default:
×
482
        tscError("invalid hb key type:%d", kv->key);
×
483
        break;
×
484
    }
485
  }
486
}
538✔
487

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

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

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

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

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

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

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

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

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

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

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

557
  taosHashRelease(pAppHbMgr->activeInfo, pReq);
1,669✔
558

559
  return TSDB_CODE_SUCCESS;
1,669✔
560
}
561

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

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

583
  int32_t rspNum = taosArrayGetSize(pRsp.rsps);
1,457✔
584

585
  (void)taosThreadMutexLock(&clientHbMgr.lock);
1,457✔
586

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

597
  SAppInstInfo *pInst = pAppHbMgr->pAppInstInfo;
1,457✔
598

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

609
  pInst->serverCfg.monitorParas = pRsp.monitorParas;
1,456✔
610
  pInst->serverCfg.enableAuditDelete = pRsp.enableAuditDelete;
1,456✔
611
  pInst->serverCfg.enableStrongPass = pRsp.enableStrongPass;
1,456✔
612
  tsEnableStrongPassword = pInst->serverCfg.enableStrongPass;
1,456✔
613
  tscDebug("monitor paras from hb, clusterId:0x%" PRIx64 ", threshold:%d scope:%d", pInst->clusterId,
1,456✔
614
           pRsp.monitorParas.tsSlowLogThreshold, pRsp.monitorParas.tsSlowLogScope);
615

616
  if (rspNum) {
1,456✔
617
    tscDebug("hb got %d rsp, %d empty rsp received before", rspNum,
1,454✔
618
             atomic_val_compare_exchange_32(&emptyRspNum, emptyRspNum, 0));
619
  } else {
620
    (void)atomic_add_fetch_32(&emptyRspNum, 1);
2✔
621
  }
622

623
  for (int32_t i = 0; i < rspNum; ++i) {
3,125✔
624
    SClientHbRsp *rsp = taosArrayGet(pRsp.rsps, i);
1,669✔
625
    code = (*clientHbMgr.rspHandle[rsp->connKey.connType])(pAppHbMgr, rsp);
1,669✔
626
    if (code) {
1,669!
627
      break;
×
628
    }
629
  }
630

631
  (void)taosThreadMutexUnlock(&clientHbMgr.lock);
1,456✔
632

633
  tFreeClientHbBatchRsp(&pRsp);
634

635
_return:
1,456✔
636
  taosMemoryFree(pMsg->pData);
1,456!
637
  taosMemoryFree(pMsg->pEpSet);
1,456!
638
  return code;
1,456✔
639
}
640

641
int32_t hbBuildQueryDesc(SQueryHbReqBasic *hbBasic, STscObj *pObj) {
1,243✔
642
  int64_t    now = taosGetTimestampUs();
1,243✔
643
  SQueryDesc desc = {0};
1,243✔
644
  int32_t    code = 0;
1,243✔
645

646
  void *pIter = taosHashIterate(pObj->pRequests, NULL);
1,243✔
647
  while (pIter != NULL) {
2,521✔
648
    int64_t     *rid = pIter;
1,278✔
649
    SRequestObj *pRequest = acquireRequest(*rid);
1,278✔
650
    if (NULL == pRequest) {
1,278!
651
      pIter = taosHashIterate(pObj->pRequests, pIter);
×
652
      continue;
×
653
    }
654

655
    if (pRequest->killed || 0 == pRequest->body.queryJob) {
1,278✔
656
      (void)releaseRequest(*rid);
384✔
657
      pIter = taosHashIterate(pObj->pRequests, pIter);
384✔
658
      continue;
384✔
659
    }
660

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

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

683
      code = schedulerGetTasksStatus(pRequest->body.queryJob, desc.subDesc);
894✔
684
      if (code) {
894✔
685
        taosArrayDestroy(desc.subDesc);
412✔
686
        desc.subDesc = NULL;
412✔
687
      }
688
      desc.subPlanNum = taosArrayGetSize(desc.subDesc);
894✔
689
    } else {
690
      desc.subDesc = NULL;
×
691
    }
692

693
    (void)releaseRequest(*rid);
894✔
694
    if (NULL == taosArrayPush(hbBasic->queryDesc, &desc)) {
1,788!
695
      taosArrayDestroy(desc.subDesc);
×
696
      return terrno;
×
697
    }
698

699
    pIter = taosHashIterate(pObj->pRequests, pIter);
894✔
700
  }
701

702
  return code;
1,243✔
703
}
704

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

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

719
  hbBasic->connId = pTscObj->connId;
1,679✔
720

721
  int32_t numOfQueries = pTscObj->pRequests ? taosHashGetSize(pTscObj->pRequests) : 0;
1,679!
722
  if (numOfQueries <= 0) {
1,679✔
723
    req->query = hbBasic;
436✔
724
    releaseTscObj(connKey->tscRid);
436✔
725
    tscDebug("no queries on connection");
436✔
726
    return TSDB_CODE_SUCCESS;
436✔
727
  }
728

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

737
  int32_t code = hbBuildQueryDesc(hbBasic, pTscObj);
1,243✔
738
  if (code) {
1,243✔
739
    releaseTscObj(connKey->tscRid);
412✔
740
    if (hbBasic->queryDesc) {
412!
741
      taosArrayDestroyEx(hbBasic->queryDesc, tFreeClientHbQueryDesc);
412✔
742
    }
743
    taosMemoryFree(hbBasic);
412!
744
    return code;
412✔
745
  }
746

747
  req->query = hbBasic;
831✔
748
  releaseTscObj(connKey->tscRid);
831✔
749

750
  return TSDB_CODE_SUCCESS;
831✔
751
}
752

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

760
  int32_t code = 0;
160✔
761

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

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

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

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

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

817
_return:
79✔
818
  releaseTscObj(connKey->tscRid);
160✔
819
  if (code) {
160!
820
    tscError("hb got user auth info failed since %s", tstrerror(code));
×
821
  }
822

823
  return code;
160✔
824
}
825

826
int32_t hbGetExpiredUserInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
791✔
827
  SUserAuthVersion *users = NULL;
791✔
828
  uint32_t          userNum = 0;
791✔
829
  int32_t           code = 0;
791✔
830

831
  code = catalogGetExpiredUsers(pCatalog, &users, &userNum);
791✔
832
  if (TSDB_CODE_SUCCESS != code) {
791!
833
    return code;
×
834
  }
835

836
  if (userNum <= 0) {
791✔
837
    taosMemoryFree(users);
3!
838
    return TSDB_CODE_SUCCESS;
3✔
839
  }
840

841
  for (int32_t i = 0; i < userNum; ++i) {
2,025✔
842
    SUserAuthVersion *user = &users[i];
1,237✔
843
    user->version = htonl(user->version);
1,237✔
844
  }
845

846
  SKv kv = {
788✔
847
      .key = HEARTBEAT_KEY_USER_AUTHINFO,
848
      .valueLen = sizeof(SUserAuthVersion) * userNum,
788✔
849
      .value = users,
850
  };
851

852
  tscDebug("hb got %d expired users, valueLen:%d", userNum, kv.valueLen);
788✔
853

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

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

868
  return TSDB_CODE_SUCCESS;
788✔
869
}
870

871
int32_t hbGetExpiredDBInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
1,052✔
872
  SDbCacheInfo *dbs = NULL;
1,052✔
873
  uint32_t      dbNum = 0;
1,052✔
874
  int32_t       code = 0;
1,052✔
875

876
  code = catalogGetExpiredDBs(pCatalog, &dbs, &dbNum);
1,052✔
877
  if (TSDB_CODE_SUCCESS != code) {
1,052!
878
    return code;
×
879
  }
880

881
  if (dbNum <= 0) {
1,052✔
882
    taosMemoryFree(dbs);
851!
883
    return TSDB_CODE_SUCCESS;
851✔
884
  }
885

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

892
    db->dbId = htobe64(db->dbId);
234✔
893
    db->vgVersion = htonl(db->vgVersion);
234✔
894
    db->cfgVersion = htonl(db->cfgVersion);
234✔
895
    db->numOfTable = htonl(db->numOfTable);
234✔
896
    db->stateTs = htobe64(db->stateTs);
234✔
897
    db->tsmaVersion = htonl(db->tsmaVersion);
234✔
898
  }
899

900
  SKv kv = {
201✔
901
      .key = HEARTBEAT_KEY_DBINFO,
902
      .valueLen = sizeof(SDbCacheInfo) * dbNum,
201✔
903
      .value = dbs,
904
  };
905

906
  tscDebug("hb got %d expired db, valueLen:%d", dbNum, kv.valueLen);
201✔
907

908
  if (NULL == req->info) {
201!
909
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
×
910
    if (NULL == req->info) {
×
911
      taosMemoryFree(dbs);
×
912
      return terrno;
×
913
    }
914
  }
915

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

922
  return TSDB_CODE_SUCCESS;
201✔
923
}
924

925
int32_t hbGetExpiredStbInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
1,052✔
926
  SSTableVersion *stbs = NULL;
1,052✔
927
  uint32_t        stbNum = 0;
1,052✔
928
  int32_t         code = 0;
1,052✔
929

930
  code = catalogGetExpiredSTables(pCatalog, &stbs, &stbNum);
1,052✔
931
  if (TSDB_CODE_SUCCESS != code) {
1,052!
932
    return code;
×
933
  }
934

935
  if (stbNum <= 0) {
1,052✔
936
    taosMemoryFree(stbs);
942!
937
    return TSDB_CODE_SUCCESS;
942✔
938
  }
939

940
  for (int32_t i = 0; i < stbNum; ++i) {
223✔
941
    SSTableVersion *stb = &stbs[i];
113✔
942
    stb->suid = htobe64(stb->suid);
113✔
943
    stb->sversion = htonl(stb->sversion);
113✔
944
    stb->tversion = htonl(stb->tversion);
113✔
945
    stb->smaVer = htonl(stb->smaVer);
113✔
946
  }
947

948
  SKv kv = {
110✔
949
      .key = HEARTBEAT_KEY_STBINFO,
950
      .valueLen = sizeof(SSTableVersion) * stbNum,
110✔
951
      .value = stbs,
952
  };
953

954
  tscDebug("hb got %d expired stb, valueLen:%d", stbNum, kv.valueLen);
110✔
955

956
  if (NULL == req->info) {
110!
957
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
×
958
    if (NULL == req->info) {
×
959
      taosMemoryFree(stbs);
×
960
      return terrno;
×
961
    }
962
  }
963

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

970
  return TSDB_CODE_SUCCESS;
110✔
971
}
972

973
int32_t hbGetExpiredViewInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
1,052✔
974
  SViewVersion    *views = NULL;
1,052✔
975
  uint32_t         viewNum = 0;
1,052✔
976
  int32_t          code = 0;
1,052✔
977
  SDynViewVersion *pDynViewVer = NULL;
1,052✔
978

979
  TSC_ERR_JRET(catalogGetExpiredViews(pCatalog, &views, &viewNum, &pDynViewVer));
1,052!
980

981
  if (viewNum <= 0) {
1,052!
982
    taosMemoryFree(views);
1,052!
983
    taosMemoryFree(pDynViewVer);
1,052!
984
    return TSDB_CODE_SUCCESS;
1,052✔
985
  }
986

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

994
  tscDebug("hb got %u expired view, valueLen:%lu", viewNum, sizeof(SViewVersion) * viewNum);
×
995

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

1003
  SKv kv = {
×
1004
      .key = HEARTBEAT_KEY_DYN_VIEW,
1005
      .valueLen = sizeof(SDynViewVersion),
1006
      .value = pDynViewVer,
1007
  };
1008

1009
  TSC_ERR_JRET(taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)));
×
1010

1011
  kv.key = HEARTBEAT_KEY_VIEWINFO;
×
1012
  kv.valueLen = sizeof(SViewVersion) * viewNum;
×
1013
  kv.value = views;
×
1014

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

1023
int32_t hbGetExpiredTSMAInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *pReq) {
1,052✔
1024
  int32_t       code = 0;
1,052✔
1025
  uint32_t      tsmaNum = 0;
1,052✔
1026
  STSMAVersion *tsmas = NULL;
1,052✔
1027

1028
  code = catalogGetExpiredTsmas(pCatalog, &tsmas, &tsmaNum);
1,052✔
1029
  if (code) {
1,052!
1030
    taosMemoryFree(tsmas);
×
1031
    return code;
×
1032
  }
1033

1034
  if (tsmaNum <= 0) {
1,052!
1035
    taosMemoryFree(tsmas);
1,052!
1036
    return TSDB_CODE_SUCCESS;
1,052✔
1037
  }
1038

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

1046
  tscDebug("hb got %d expred tsmas, valueLen:%lu", tsmaNum, sizeof(STSMAVersion) * tsmaNum);
×
1047

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

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

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

1076
  return TSDB_CODE_SUCCESS;
1,267✔
1077
}
1078

1079
int32_t hbQueryHbReqHandle(SClientHbKey *connKey, void *param, SClientHbReq *req) {
1,679✔
1080
  int32_t   code = 0;
1,679✔
1081
  SHbParam *hbParam = (SHbParam *)param;
1,679✔
1082
  SCatalog *pCatalog = NULL;
1,679✔
1083

1084
  code = hbGetQueryBasicInfo(connKey, req);
1,679✔
1085
  if (code != TSDB_CODE_SUCCESS) {
1,679✔
1086
    tscWarn("hbGetQueryBasicInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
412!
1087
    return code;
412✔
1088
  }
1089

1090
  if (hbParam->reqCnt == 0) {
1,267✔
1091
    code = catalogGetHandle(hbParam->clusterId, &pCatalog);
1,052✔
1092
    if (code != TSDB_CODE_SUCCESS) {
1,052!
1093
      tscWarn("catalogGetHandle failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1094
      return code;
×
1095
    }
1096

1097
    code = hbGetAppInfo(hbParam->clusterId, req);
1,052✔
1098
    if (TSDB_CODE_SUCCESS != code) {
1,052!
1099
      tscWarn("getAppInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1100
      return code;
×
1101
    }
1102

1103
    if (!taosHashGet(clientHbMgr.appHbHash, &hbParam->clusterId, sizeof(hbParam->clusterId))) {
1,052✔
1104
      code = hbGetExpiredUserInfo(connKey, pCatalog, req);
791✔
1105
      if (TSDB_CODE_SUCCESS != code) {
791!
1106
        tscWarn("hbGetExpiredUserInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1107
        return code;
×
1108
      }
1109
      if (clientHbMgr.appHbHash) {
791✔
1110
        code = taosHashPut(clientHbMgr.appHbHash, &hbParam->clusterId, sizeof(uint64_t), NULL, 0);
252✔
1111
        if (TSDB_CODE_SUCCESS != code) {
252!
1112
          tscWarn("hbQueryHbReqHandle put clusterId failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId,
×
1113
                  tstrerror(code));
1114
          return code;
×
1115
        }
1116
      }
1117
    }
1118

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

1129
    code = hbGetExpiredDBInfo(connKey, pCatalog, req);
1,052✔
1130
    if (TSDB_CODE_SUCCESS != code) {
1,052!
1131
      tscWarn("hbGetExpiredDBInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1132
      return code;
×
1133
    }
1134

1135
    code = hbGetExpiredStbInfo(connKey, pCatalog, req);
1,052✔
1136
    if (TSDB_CODE_SUCCESS != code) {
1,052!
1137
      tscWarn("hbGetExpiredStbInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1138
      return code;
×
1139
    }
1140

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

1161
  ++hbParam->reqCnt;  // success to get catalog info
1,267✔
1162

1163
  return TSDB_CODE_SUCCESS;
1,267✔
1164
}
1165

1166
static FORCE_INLINE void hbMgrInitHandle() {
1167
  // init all handle
1168
  clientHbMgr.reqHandle[CONN_TYPE__QUERY] = hbQueryHbReqHandle;
92✔
1169
  clientHbMgr.reqHandle[CONN_TYPE__TMQ] = hbMqHbReqHandle;
92✔
1170

1171
  clientHbMgr.rspHandle[CONN_TYPE__QUERY] = hbQueryHbRspHandle;
92✔
1172
  clientHbMgr.rspHandle[CONN_TYPE__TMQ] = hbMqHbRspHandle;
92✔
1173
}
92✔
1174

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

1187
  int64_t  maxIpWhiteVer = 0;
1,466✔
1188
  void    *pIter = NULL;
1,466✔
1189
  SHbParam param = {0};
1,466✔
1190
  while ((pIter = taosHashIterate(pAppHbMgr->activeInfo, pIter))) {
3,270✔
1191
    SClientHbReq *pOneReq = pIter;
1,804✔
1192
    SClientHbKey *connKey = &pOneReq->connKey;
1,804✔
1193
    STscObj      *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
1,804✔
1194

1195
    if (!pTscObj || atomic_load_8(&pTscObj->dropped) == 1) {
1,804!
1196
      if (pTscObj) releaseTscObj(connKey->tscRid);
125!
1197
      continue;
125✔
1198
    }
1199

1200
    tstrncpy(pOneReq->userApp, pTscObj->optionInfo.userApp, sizeof(pOneReq->userApp));
1,679✔
1201
    pOneReq->userIp = pTscObj->optionInfo.userIp;
1,679✔
1202

1203
    pOneReq = taosArrayPush((*pBatchReq)->reqs, pOneReq);
1,679✔
1204
    if (NULL == pOneReq) {
1,679!
1205
      releaseTscObj(connKey->tscRid);
×
1206
      continue;
×
1207
    }
1208

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

1230
    int64_t ver = atomic_load_64(&pTscObj->whiteListInfo.ver);
1,679✔
1231
    maxIpWhiteVer = TMAX(maxIpWhiteVer, ver);
1,679✔
1232
    releaseTscObj(connKey->tscRid);
1,679✔
1233
  }
1234
  (*pBatchReq)->ipWhiteList = maxIpWhiteVer;
1,466✔
1235

1236
  return TSDB_CODE_SUCCESS;
1,466✔
1237
}
1238

1239
void hbThreadFuncUnexpectedStopped(void) { atomic_store_8(&clientHbMgr.threadStop, 2); }
×
1240

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

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

1263
  taosHashClear(clientHbMgr.appSummary);
1,276✔
1264

1265
  for (int32_t i = 0; i < sz; ++i) {
3,148✔
1266
    SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
1,872✔
1267
    if (pAppHbMgr == NULL) continue;
1,872!
1268

1269
    int64_t    clusterId = pAppHbMgr->pAppInstInfo->clusterId;
1,872✔
1270
    SAppHbReq *pApp = taosHashGet(clientHbMgr.appSummary, &clusterId, sizeof(clusterId));
1,872✔
1271
    if (NULL == pApp) {
1,872✔
1272
      (void)memcpy(&req.summary, &pAppHbMgr->pAppInstInfo->summary, sizeof(req.summary));
1,287✔
1273
      req.startTime = pAppHbMgr->startTime;
1,287✔
1274
      TSC_ERR_RET(taosHashPut(clientHbMgr.appSummary, &clusterId, sizeof(clusterId), &req, sizeof(req)));
1,287!
1275
    } else {
1276
      if (pAppHbMgr->startTime < pApp->startTime) {
585!
1277
        pApp->startTime = pAppHbMgr->startTime;
×
1278
      }
1279

1280
      hbMergeSummary(&pApp->summary, &pAppHbMgr->pAppInstInfo->summary);
585✔
1281
    }
1282
  }
1283

1284
  return TSDB_CODE_SUCCESS;
1,276✔
1285
}
1286

1287
static void *hbThreadFunc(void *param) {
92✔
1288
  setThreadName("hb");
92✔
1289
#ifdef WINDOWS
1290
  if (taosCheckCurrentInDll()) {
1291
    atexit(hbThreadFuncUnexpectedStopped);
1292
  }
1293
#endif
1294
  while (1) {
1,214✔
1295
    if (1 == clientHbMgr.threadStop) {
1,306✔
1296
      break;
30✔
1297
    }
1298

1299
    if (TSDB_CODE_SUCCESS != taosThreadMutexLock(&clientHbMgr.lock)) {
1,276!
1300
      tscError("taosThreadMutexLock failed");
×
1301
      return NULL;
×
1302
    }
1303

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

1320
    for (int i = 0; i < sz; i++) {
3,148✔
1321
      SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
1,872✔
1322
      if (pAppHbMgr == NULL) {
1,872!
1323
        continue;
406✔
1324
      }
1325

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

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

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

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

1389
    if (TSDB_CODE_SUCCESS != taosThreadMutexUnlock(&clientHbMgr.lock)) {
1,276!
1390
      tscError("taosThreadMutexLock failed");
×
1391
      return NULL;
×
1392
    }
1393
    taosMsleep(HEARTBEAT_INTERVAL);
1,276✔
1394
  }
1395
  taosHashCleanup(clientHbMgr.appHbHash);
30✔
1396
  return NULL;
30✔
1397
}
1398

1399
static int32_t hbCreateThread() {
92✔
1400
  int32_t      code = TSDB_CODE_SUCCESS;
92✔
1401
  TdThreadAttr thAttr;
1402
  TSC_ERR_JRET(taosThreadAttrInit(&thAttr));
92!
1403
  TSC_ERR_JRET(taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE));
92!
1404
#ifdef TD_COMPACT_OS
1405
  TSC_ERR_JRET(taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL));
1406
#endif
1407

1408
  if (taosThreadCreate(&clientHbMgr.thread, &thAttr, hbThreadFunc, NULL) != 0) {
92!
1409
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
1410
    TSC_ERR_RET(terrno);
×
1411
  }
1412
  (void)taosThreadAttrDestroy(&thAttr);
92✔
1413
_return:
92✔
1414

1415
  if (code) {
92!
1416
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
1417
    TSC_ERR_RET(terrno);
×
1418
  }
1419

1420
  return code;
92✔
1421
}
1422

1423
static void hbStopThread() {
93✔
1424
  if (0 == atomic_load_8(&clientHbMgr.inited)) {
93✔
1425
    return;
1✔
1426
  }
1427
  if (atomic_val_compare_exchange_8(&clientHbMgr.threadStop, 0, 1)) {
92!
1428
    tscDebug("hb thread already stopped");
×
1429
    return;
×
1430
  }
1431

1432
  int32_t code = TSDB_CODE_SUCCESS;
92✔
1433
  // thread quit mode kill or inner exit from self-thread
1434
  if (clientHbMgr.quitByKill) {
92✔
1435
    code = taosThreadKill(clientHbMgr.thread, 0);
62✔
1436
    if (TSDB_CODE_SUCCESS != code) {
62!
1437
      tscError("taosThreadKill failed since %s", tstrerror(code));
×
1438
    }
1439
  } else {
1440
    code = taosThreadJoin(clientHbMgr.thread, NULL);
30✔
1441
    if (TSDB_CODE_SUCCESS != code) {
30!
1442
      tscError("taosThreadJoin failed since %s", tstrerror(code));
×
1443
    }
1444
  }
1445

1446
  tscDebug("hb thread stopped");
92✔
1447
}
1448

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

1467
  // init app info
1468
  (*pAppHbMgr)->pAppInstInfo = pAppInstInfo;
130✔
1469

1470
  // init hash info
1471
  (*pAppHbMgr)->activeInfo = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
130✔
1472

1473
  if ((*pAppHbMgr)->activeInfo == NULL) {
130!
1474
    TSC_ERR_JRET(terrno);
×
1475
  }
1476

1477
  // taosHashSetFreeFp(pAppHbMgr->activeInfo, tFreeClientHbReq);
1478

1479
  TSC_ERR_JRET(taosThreadMutexLock(&clientHbMgr.lock));
130!
1480
  if (taosArrayPush(clientHbMgr.appHbMgrs, &(*pAppHbMgr)) == NULL) {
260!
1481
    code = terrno;
×
1482
    (void)taosThreadMutexUnlock(&clientHbMgr.lock);
×
1483
    goto _return;
×
1484
  }
1485
  (*pAppHbMgr)->idx = taosArrayGetSize(clientHbMgr.appHbMgrs) - 1;
130✔
1486
  TSC_ERR_JRET(taosThreadMutexUnlock(&clientHbMgr.lock));
130!
1487

1488
  return TSDB_CODE_SUCCESS;
130✔
1489
_return:
×
1490
  taosMemoryFree(*pAppHbMgr);
×
1491
  return code;
×
1492
}
1493

1494
void hbFreeAppHbMgr(SAppHbMgr *pTarget) {
130✔
1495
  void *pIter = taosHashIterate(pTarget->activeInfo, NULL);
130✔
1496
  while (pIter != NULL) {
145✔
1497
    SClientHbReq *pOneReq = pIter;
15!
1498
    tFreeClientHbReq(pOneReq);
1499
    pIter = taosHashIterate(pTarget->activeInfo, pIter);
15✔
1500
  }
1501
  taosHashCleanup(pTarget->activeInfo);
130✔
1502
  pTarget->activeInfo = NULL;
130✔
1503

1504
  taosMemoryFree(pTarget->key);
130!
1505
  taosMemoryFree(pTarget);
130!
1506
}
130✔
1507

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

1530
void appHbMgrCleanup(void) {
92✔
1531
  int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
92✔
1532
  for (int i = 0; i < sz; i++) {
222✔
1533
    SAppHbMgr *pTarget = taosArrayGetP(clientHbMgr.appHbMgrs, i);
130✔
1534
    if (pTarget == NULL) continue;
130!
1535
    hbFreeAppHbMgr(pTarget);
130✔
1536
  }
1537
}
92✔
1538

1539
int32_t hbMgrInit() {
130✔
1540
  // init once
1541
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 0, 1);
130✔
1542
  if (old == 1) return 0;
130✔
1543

1544
  clientHbMgr.appId = tGenIdPI64();
92✔
1545
  tscInfo("app initialized, appId:0x%" PRIx64, clientHbMgr.appId);
92!
1546

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

1557
  int ret = taosThreadMutexAttrInit(&attr);
92✔
1558
  if (ret != 0) {
92!
1559
    uError("hbMgrInit:taosThreadMutexAttrInit error") return ret;
×
1560
  }
1561

1562
  ret = taosThreadMutexAttrSetType(&attr, PTHREAD_MUTEX_RECURSIVE);
92✔
1563
  if (ret != 0) {
92!
1564
    uError("hbMgrInit:taosThreadMutexAttrSetType error") return ret;
×
1565
  }
1566

1567
  ret = taosThreadMutexInit(&clientHbMgr.lock, &attr);
92✔
1568
  if (ret != 0) {
92!
1569
    uError("hbMgrInit:taosThreadMutexInit error") return ret;
×
1570
  }
1571

1572
  ret = taosThreadMutexAttrDestroy(&attr);
92✔
1573
  if (ret != 0) {
92!
1574
    uError("hbMgrInit:taosThreadMutexAttrDestroy error") return ret;
×
1575
  }
1576

1577
  // init handle funcs
1578
  hbMgrInitHandle();
1579

1580
  // init backgroud thread
1581
  ret = hbCreateThread();
92✔
1582
  if (ret != 0) {
92!
1583
    uError("hbMgrInit:hbCreateThread error") return ret;
×
1584
  }
1585

1586
  return 0;
92✔
1587
}
1588

1589
void hbMgrCleanUp() {
93✔
1590
  hbStopThread();
93✔
1591

1592
  // destroy all appHbMgr
1593
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 1, 0);
93✔
1594
  if (old == 0) return;
93✔
1595

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

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

1620
  TSC_ERR_RET(taosHashPut(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey), &hbReq, sizeof(SClientHbReq)));
288!
1621

1622
  (void)atomic_add_fetch_32(&pAppHbMgr->connKeyCnt, 1);
288✔
1623
  return 0;
288✔
1624
}
1625

1626
int32_t hbRegisterConn(SAppHbMgr *pAppHbMgr, int64_t tscRefId, int64_t clusterId, int8_t connType) {
294✔
1627
  SClientHbKey connKey = {
294✔
1628
      .tscRid = tscRefId,
1629
      .connType = connType,
1630
  };
1631

1632
  switch (connType) {
294!
1633
    case CONN_TYPE__QUERY: {
288✔
1634
      return hbRegisterConnImpl(pAppHbMgr, connKey, clusterId);
288✔
1635
    }
1636
    case CONN_TYPE__TMQ: {
6✔
1637
      return 0;
6✔
1638
    }
1639
    default:
×
1640
      return 0;
×
1641
  }
1642
}
1643

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

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