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

taosdata / TDengine / #4856

17 Nov 2025 09:53AM UTC coverage: 64.286% (+0.2%) from 64.039%
#4856

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

218 of 311 new or added lines in 32 files covered. (70.1%)

4657 existing lines in 112 files now uncovered.

151658 of 235910 relevant lines covered (64.29%)

116320814.6 hits per line

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

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

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

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

35
SClientHbMgr clientHbMgr = {0};
36

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

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

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

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

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

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

63
    TSC_ERR_JRET(catalogUpdateUserAuthInfo(pCatalog, rsp));
902,778✔
64
  }
65

66
  if (numOfBatchs > 0) {
885,939✔
67
    TSC_ERR_JRET(hbUpdateUserAuthInfo(pAppHbMgr, &batchRsp));
885,939✔
68
  }
69

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

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

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

85
    SClientHbReq    *pReq = NULL;
1,493,381✔
86
    SGetUserAuthRsp *pRsp = NULL;
1,493,381✔
87
    while ((pReq = taosHashIterate(hbMgr->activeInfo, pReq))) {
2,757,708✔
88
      STscObj *pTscObj = (STscObj *)acquireTscObj(pReq->connKey.tscRid);
1,502,636✔
89
      if (!pTscObj) {
1,502,636✔
90
        continue;
110,242✔
91
      }
92

93
      if (!pRsp) {
1,392,394✔
94
        for (int32_t j = 0; j < TARRAY_SIZE(batchRsp->pArray); ++j) {
1,425,058✔
95
          SGetUserAuthRsp *rsp = TARRAY_GET_ELEM(batchRsp->pArray, j);
1,186,749✔
96
          if (0 == strncmp(rsp->user, pTscObj->user, TSDB_USER_LEN)) {
1,186,749✔
97
            pRsp = rsp;
918,818✔
98
            break;
918,818✔
99
          }
100
        }
101
        if (!pRsp) {
1,157,127✔
102
          releaseTscObj(pReq->connKey.tscRid);
238,309✔
103
          taosHashCancelIterate(hbMgr->activeInfo, pReq);
238,309✔
104
          break;
238,309✔
105
        }
106
      }
107

108
      if (pRsp->dropped == 1) {
1,154,085✔
109
        if (atomic_val_compare_exchange_8(&pTscObj->dropped, 0, 1) == 0) {
16,900✔
110
          if (pTscObj->userDroppedInfo.fp) {
14,350✔
111
            SPassInfo *dropInfo = &pTscObj->userDroppedInfo;
12,750✔
112
            if (dropInfo->fp) {
12,750✔
113
              (*dropInfo->fp)(dropInfo->param, NULL, TAOS_NOTIFY_USER_DROPPED);
12,750✔
114
            }
115
          }
116
        }
117
        releaseTscObj(pReq->connKey.tscRid);
16,900✔
118
        continue;
16,900✔
119
      }
120

121
      pTscObj->authVer = pRsp->version;
1,137,185✔
122

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

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

142
      if (pTscObj->whiteListInfo.fp) {
1,137,185✔
143
        SWhiteListInfo *whiteListInfo = &pTscObj->whiteListInfo;
×
144
        int64_t         oldVer = atomic_load_64(&whiteListInfo->ver);
×
145
        if (oldVer != pRsp->whiteListVer) {
×
146
          atomic_store_64(&whiteListInfo->ver, pRsp->whiteListVer);
×
147
          if (whiteListInfo->fp) {
×
148
            (*whiteListInfo->fp)(whiteListInfo->param, &pRsp->whiteListVer, TAOS_NOTIFY_WHITELIST_VER);
×
149
          }
150
          tscDebug("update whitelist version of user %s from %" PRId64 " to %" PRId64 ", conn:%" PRIi64, pRsp->user,
×
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;
1,137,185✔
157
        int64_t         oldVer = atomic_load_64(&whiteListInfo->ver);
1,137,185✔
158
        atomic_store_64(&whiteListInfo->ver, pRsp->whiteListVer);
1,137,185✔
159
        tscDebug("update whitelist version of user %s from %" PRId64 " to %" PRId64 ", conn:%" PRIi64, pRsp->user,
1,137,185✔
160
                 oldVer, atomic_load_64(&whiteListInfo->ver), pTscObj->id);
161
      }
162
      releaseTscObj(pReq->connKey.tscRid);
1,137,185✔
163
    }
164
  }
165
  return 0;
885,939✔
166
}
167

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

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

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

197
_return:
1,300,928✔
198
  if (code) {
1,300,928✔
199
    taosHashCleanup(vgInfo->vgHash);
×
200
    taosMemoryFreeClear(vgInfo);
×
201
  }
202

203
  *pInfo = vgInfo;
1,300,928✔
204
  return code;
1,300,928✔
205
}
206

207
static int32_t hbProcessDBInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
5,312,937✔
208
  int32_t code = 0;
5,312,937✔
209

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

217
  int32_t numOfBatchs = taosArrayGetSize(batchRsp.pArray);
5,312,937✔
218
  for (int32_t i = 0; i < numOfBatchs; ++i) {
6,801,693✔
219
    SDbHbRsp *rsp = taosArrayGet(batchRsp.pArray, i);
1,488,756✔
220
    if (NULL == rsp) {
1,488,756✔
221
      code = terrno;
×
222
      goto _return;
×
223
    }
224
    if (rsp->useDbRsp) {
1,488,756✔
225
      tscDebug("hb use db rsp, db:%s, vgVersion:%d, stateTs:%" PRId64 ", uid:%" PRIx64, rsp->useDbRsp->db,
1,095,353✔
226
               rsp->useDbRsp->vgVersion, rsp->useDbRsp->stateTs, rsp->useDbRsp->uid);
227

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

238
        tscDebug("hb to update db vgInfo, db:%s", rsp->useDbRsp->db);
1,068,096✔
239

240
        TSC_ERR_JRET(catalogUpdateDBVgInfo(pCatalog, rsp->useDbRsp->db, rsp->useDbRsp->uid, vgInfo));
1,068,096✔
241

242
        if (IS_SYS_DBNAME(rsp->useDbRsp->db)) {
1,068,096✔
243
          code = hbGenerateVgInfoFromRsp(&vgInfo, rsp->useDbRsp);
232,832✔
244
          if (TSDB_CODE_SUCCESS != code) {
232,832✔
245
            goto _return;
×
246
          }
247

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

255
    if (rsp->cfgRsp) {
1,488,756✔
256
      tscDebug("hb db cfg rsp, db:%s, cfgVersion:%d", rsp->cfgRsp->db, rsp->cfgRsp->cfgVersion);
308,819✔
257
      code = catalogUpdateDbCfg(pCatalog, rsp->cfgRsp->db, rsp->cfgRsp->dbId, rsp->cfgRsp);
308,819✔
258
      rsp->cfgRsp = NULL;
308,819✔
259
    }
260
    if (rsp->pTsmaRsp) {
1,488,756✔
261
      if (rsp->pTsmaRsp->pTsmas) {
821,332✔
262
        for (int32_t i = 0; i < rsp->pTsmaRsp->pTsmas->size; ++i) {
2,880✔
263
          STableTSMAInfo *pTsma = taosArrayGetP(rsp->pTsmaRsp->pTsmas, i);
1,440✔
264
          if (NULL == pTsma) {
1,440✔
265
            TSC_ERR_JRET(TSDB_CODE_OUT_OF_RANGE);
×
266
          }
267
          TSC_ERR_JRET(catalogAsyncUpdateTSMA(pCatalog, &pTsma, rsp->dbTsmaVersion));
1,440✔
268
        }
269
        taosArrayClear(rsp->pTsmaRsp->pTsmas);
1,440✔
270
      } else {
271
        TSC_ERR_JRET(catalogAsyncUpdateDbTsmaVersion(pCatalog, rsp->dbTsmaVersion, rsp->db, rsp->dbId));
819,892✔
272
      }
273
    }
274
  }
275

276
_return:
5,312,937✔
277

278
  tFreeSDbHbBatchRsp(&batchRsp);
5,312,937✔
279
  return code;
5,312,937✔
280
}
281

282
static int32_t hbProcessStbInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
3,645,825✔
283
  int32_t code = TSDB_CODE_SUCCESS;
3,645,825✔
284

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

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

309
      TSC_ERR_JRET(catalogAsyncUpdateTableMeta(pCatalog, rsp));
20,857✔
310
    }
311
  }
312

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

323
_return:
3,645,825✔
324
  taosArrayDestroy(hbRsp.pIndexRsp);
3,645,825✔
325
  hbRsp.pIndexRsp = NULL;
3,645,825✔
326

327
  tFreeSSTbHbRsp(&hbRsp);
3,645,825✔
328
  return code;
3,645,825✔
329
}
330

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

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

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

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

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

373
_return:
5,037✔
374
  taosArrayDestroy(hbRsp.pViewRsp);
5,037✔
375
  return code;
5,037✔
376
}
377

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

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

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

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

403
_return:
94,320✔
404
  taosArrayDestroy(hbRsp.pTsmas);
94,320✔
405
  return code;
94,320✔
406
}
407

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

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

497
  if (pRsp->query) {
25,917,340✔
498
    STscObj *pTscObj = (STscObj *)acquireTscObj(pRsp->connKey.tscRid);
22,455,976✔
499
    if (NULL == pTscObj) {
22,455,976✔
500
      tscDebug("tscObj rid %" PRIx64 " not exist", pRsp->connKey.tscRid);
4,082✔
501
    } else {
502
      if (pRsp->query->totalDnodes > 1) {
22,451,894✔
503
        SEpSet  originEpset = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
6,497,142✔
504
        if (!isEpsetEqual(&originEpset, &pRsp->query->epSet)) {
6,497,142✔
505
          SEpSet *pOrig = &originEpset;
56,710✔
506
          SEp    *pOrigEp = &pOrig->eps[pOrig->inUse];
56,710✔
507
          SEp    *pNewEp = &pRsp->query->epSet.eps[pRsp->query->epSet.inUse];
56,710✔
508
          tscDebug("mnode epset updated from %d/%d=>%s:%d to %d/%d=>%s:%d in hb", pOrig->inUse, pOrig->numOfEps,
56,710✔
509
                   pOrigEp->fqdn, pOrigEp->port, pRsp->query->epSet.inUse, pRsp->query->epSet.numOfEps, pNewEp->fqdn,
510
                   pNewEp->port);
511

512
          updateEpSet_s(&pTscObj->pAppInfo->mgmtEp, &pRsp->query->epSet);
56,710✔
513
        }
514
      }
515

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

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

533
      if (pRsp->query->killConnection) {
22,451,894✔
534
        taos_close_internal(pTscObj);
×
535
      }
536

537
      if (pRsp->query->pQnodeList) {
22,451,894✔
538
        if (TSDB_CODE_SUCCESS != updateQnodeList(pTscObj->pAppInfo, pRsp->query->pQnodeList)) {
100,308✔
539
          tscWarn("update qnode list failed");
×
540
        }
541
      }
542

543
      releaseTscObj(pRsp->connKey.tscRid);
22,451,894✔
544
    }
545
  }
546

547
  int32_t kvNum = pRsp->info ? taosArrayGetSize(pRsp->info) : 0;
25,917,340✔
548

549
  tscDebug("hb got %d rsp kv", kvNum);
25,917,340✔
550

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

561
  taosHashRelease(pAppHbMgr->activeInfo, pReq);
25,917,340✔
562

563
  return TSDB_CODE_SUCCESS;
25,917,340✔
564
}
565

566
static int32_t hbAsyncCallBack(void *param, SDataBuf *pMsg, int32_t code) {
22,423,977✔
567
  if (0 == atomic_load_8(&clientHbMgr.inited)) {
22,423,977✔
568
    goto _return;
×
569
  }
570

571
  static int32_t    emptyRspNum = 0;
572
  int32_t           idx = *(int32_t *)param;
22,423,977✔
573
  SClientHbBatchRsp pRsp = {0};
22,424,001✔
574
  if (TSDB_CODE_SUCCESS == code) {
22,424,001✔
575
    code = tDeserializeSClientHbBatchRsp(pMsg->pData, pMsg->len, &pRsp);
21,890,235✔
576
    if (TSDB_CODE_SUCCESS != code) {
21,888,042✔
577
      tscError("deserialize hb rsp failed");
1,012✔
578
    }
579
    int32_t now = taosGetTimestampSec();
21,888,042✔
580
    int32_t delta = abs(now - pRsp.svrTimestamp);
21,887,221✔
581
    if (delta > timestampDeltaLimit) {
21,887,221✔
582
      code = TSDB_CODE_TIME_UNSYNCED;
1,012✔
583
      tscError("time diff:%ds is too big", delta);
1,012✔
584
    }
585
  }
586

587
  int32_t rspNum = taosArrayGetSize(pRsp.rsps);
22,420,987✔
588

589
  (void)taosThreadMutexLock(&clientHbMgr.lock);
22,421,486✔
590

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

601
  SAppInstInfo *pInst = pAppHbMgr->pAppInstInfo;
22,424,001✔
602

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

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

620
  if (rspNum) {
21,889,223✔
621
    tscDebug("hb got %d rsp, %d empty rsp received before", rspNum,
21,279,291✔
622
             atomic_val_compare_exchange_32(&emptyRspNum, emptyRspNum, 0));
623
  } else {
624
    (void)atomic_add_fetch_32(&emptyRspNum, 1);
609,932✔
625
  }
626

627
  for (int32_t i = 0; i < rspNum; ++i) {
47,816,816✔
628
    SClientHbRsp *rsp = taosArrayGet(pRsp.rsps, i);
25,927,593✔
629
    code = (*clientHbMgr.rspHandle[rsp->connKey.connType])(pAppHbMgr, rsp);
25,927,593✔
630
    if (code) {
25,927,593✔
631
      break;
×
632
    }
633
  }
634

635
  (void)taosThreadMutexUnlock(&clientHbMgr.lock);
21,889,223✔
636

637
  tFreeClientHbBatchRsp(&pRsp);
638

639
_return:
21,889,223✔
640
  taosMemoryFree(pMsg->pData);
21,889,223✔
641
  taosMemoryFree(pMsg->pEpSet);
21,889,223✔
642
  return code;
21,889,223✔
643
}
644

645
int32_t hbBuildQueryDesc(SQueryHbReqBasic *hbBasic, STscObj *pObj) {
24,525,549✔
646
  int64_t    now = taosGetTimestampUs();
24,525,549✔
647
  SQueryDesc desc = {0};
24,525,549✔
648
  int32_t    code = 0;
24,525,549✔
649

650
  void *pIter = taosHashIterate(pObj->pRequests, NULL);
24,525,549✔
651
  while (pIter != NULL) {
49,697,844✔
652
    int64_t     *rid = pIter;
25,172,295✔
653
    SRequestObj *pRequest = acquireRequest(*rid);
25,172,295✔
654
    if (NULL == pRequest) {
25,172,295✔
655
      pIter = taosHashIterate(pObj->pRequests, pIter);
19,006✔
656
      continue;
19,006✔
657
    }
658

659
    if (pRequest->killed || 0 == pRequest->body.queryJob) {
25,153,289✔
660
      (void)releaseRequest(*rid);
9,764,496✔
661
      pIter = taosHashIterate(pObj->pRequests, pIter);
9,764,496✔
662
      continue;
9,764,496✔
663
    }
664

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

680
    if (desc.subPlanNum) {
15,388,793✔
681
      desc.subDesc = taosArrayInit(desc.subPlanNum, sizeof(SQuerySubDesc));
15,388,793✔
682
      if (NULL == desc.subDesc) {
15,388,793✔
683
        (void)releaseRequest(*rid);
×
684
        return terrno;
×
685
      }
686

687
      code = schedulerGetTasksStatus(pRequest->body.queryJob, desc.subDesc);
15,388,793✔
688
      if (code) {
15,388,793✔
689
        taosArrayDestroy(desc.subDesc);
3,584,592✔
690
        desc.subDesc = NULL;
3,584,592✔
691
      }
692
      desc.subPlanNum = taosArrayGetSize(desc.subDesc);
15,388,793✔
693
    } else {
694
      desc.subDesc = NULL;
×
695
    }
696

697
    (void)releaseRequest(*rid);
15,388,793✔
698
    if (NULL == taosArrayPush(hbBasic->queryDesc, &desc)) {
30,777,586✔
699
      taosArrayDestroy(desc.subDesc);
×
700
      return terrno;
×
701
    }
702

703
    pIter = taosHashIterate(pObj->pRequests, pIter);
15,388,793✔
704
  }
705

706
  return code;
24,525,549✔
707
}
708

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

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

723
  hbBasic->connId = pTscObj->connId;
26,484,065✔
724

725
  int32_t numOfQueries = pTscObj->pRequests ? taosHashGetSize(pTscObj->pRequests) : 0;
26,484,065✔
726
  if (numOfQueries <= 0) {
26,484,065✔
727
    req->query = hbBasic;
1,958,516✔
728
    releaseTscObj(connKey->tscRid);
1,958,516✔
729
    tscDebug("no queries on connection");
1,958,516✔
730
    return TSDB_CODE_SUCCESS;
1,958,516✔
731
  }
732

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

741
  int32_t code = hbBuildQueryDesc(hbBasic, pTscObj);
24,525,549✔
742
  if (code) {
24,525,549✔
743
    releaseTscObj(connKey->tscRid);
3,494,230✔
744
    if (hbBasic->queryDesc) {
3,494,230✔
745
      taosArrayDestroyEx(hbBasic->queryDesc, tFreeClientHbQueryDesc);
3,494,230✔
746
    }
747
    taosMemoryFree(hbBasic);
3,494,230✔
748
    return code;
3,494,230✔
749
  }
750

751
  req->query = hbBasic;
21,031,319✔
752
  releaseTscObj(connKey->tscRid);
21,031,319✔
753

754
  return TSDB_CODE_SUCCESS;
21,031,319✔
755
}
756

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

764
  int32_t code = 0;
844,689✔
765

766
  SKv  kv = {.key = HEARTBEAT_KEY_USER_AUTHINFO};
844,689✔
767
  SKv *pKv = NULL;
844,689✔
768
  if ((pKv = taosHashGet(req->info, &kv.key, sizeof(kv.key)))) {
844,689✔
769
    int32_t           userNum = pKv->valueLen / sizeof(SUserAuthVersion);
376,114✔
770
    SUserAuthVersion *userAuths = (SUserAuthVersion *)pKv->value;
376,114✔
771
    for (int32_t i = 0; i < userNum; ++i) {
379,650✔
772
      SUserAuthVersion *pUserAuth = userAuths + i;
378,998✔
773
      // both key and user exist, update version
774
      if (strncmp(pUserAuth->user, pTscObj->user, TSDB_USER_LEN) == 0) {
378,998✔
775
        pUserAuth->version = htonl(-1);  // force get userAuthInfo
375,462✔
776
        goto _return;
375,462✔
777
      }
778
    }
779
    // key exists, user not exist, append user
780
    SUserAuthVersion *qUserAuth =
1,304✔
781
        (SUserAuthVersion *)taosMemoryRealloc(pKv->value, (userNum + 1) * sizeof(SUserAuthVersion));
652✔
782
    if (qUserAuth) {
652✔
783
      tstrncpy((qUserAuth + userNum)->user, pTscObj->user, TSDB_USER_LEN);
652✔
784
      (qUserAuth + userNum)->version = htonl(-1);  // force get userAuthInfo
652✔
785
      pKv->value = qUserAuth;
652✔
786
      pKv->valueLen += sizeof(SUserAuthVersion);
652✔
787
    } else {
788
      code = terrno;
×
789
    }
790
    goto _return;
652✔
791
  }
792

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

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

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

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

821
_return:
791,270✔
822
  releaseTscObj(connKey->tscRid);
844,689✔
823
  if (code) {
844,689✔
824
    tscError("hb got user auth info failed since %s", tstrerror(code));
×
825
  }
826

827
  return code;
844,689✔
828
}
829

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

835
  code = catalogGetExpiredUsers(pCatalog, &users, &userNum);
17,472,939✔
836
  if (TSDB_CODE_SUCCESS != code) {
17,472,939✔
837
    return code;
×
838
  }
839

840
  if (userNum <= 0) {
17,472,939✔
841
    taosMemoryFree(users);
631,758✔
842
    return TSDB_CODE_SUCCESS;
631,758✔
843
  }
844

845
  for (int32_t i = 0; i < userNum; ++i) {
34,080,281✔
846
    SUserAuthVersion *user = &users[i];
17,239,100✔
847
    user->version = htonl(user->version);
17,239,100✔
848
  }
849

850
  SKv kv = {
16,841,181✔
851
      .key = HEARTBEAT_KEY_USER_AUTHINFO,
852
      .valueLen = sizeof(SUserAuthVersion) * userNum,
16,841,181✔
853
      .value = users,
854
  };
855

856
  tscDebug("hb got %d expired users, valueLen:%d", userNum, kv.valueLen);
16,841,181✔
857

858
  if (NULL == req->info) {
16,841,181✔
859
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
16,841,181✔
860
    if (NULL == req->info) {
16,841,181✔
861
      taosMemoryFree(users);
×
862
      return terrno;
×
863
    }
864
  }
865

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

872
  return TSDB_CODE_SUCCESS;
16,841,181✔
873
}
874

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

880
  code = catalogGetExpiredDBs(pCatalog, &dbs, &dbNum);
18,451,289✔
881
  if (TSDB_CODE_SUCCESS != code) {
18,451,289✔
882
    return code;
×
883
  }
884

885
  if (dbNum <= 0) {
18,451,289✔
886
    taosMemoryFree(dbs);
13,108,543✔
887
    return TSDB_CODE_SUCCESS;
13,108,543✔
888
  }
889

890
  for (int32_t i = 0; i < dbNum; ++i) {
12,139,083✔
891
    SDbCacheInfo *db = &dbs[i];
6,796,337✔
892
    tscDebug("the %dth expired db:%s, dbId:%" PRId64
6,796,337✔
893
             ", vgVersion:%d, cfgVersion:%d, numOfTable:%d, startTs:%" PRId64,
894
             i, db->dbFName, db->dbId, db->vgVersion, db->cfgVersion, db->numOfTable, db->stateTs);
895

896
    db->dbId = htobe64(db->dbId);
6,796,337✔
897
    db->vgVersion = htonl(db->vgVersion);
6,796,337✔
898
    db->cfgVersion = htonl(db->cfgVersion);
6,796,337✔
899
    db->numOfTable = htonl(db->numOfTable);
6,796,337✔
900
    db->stateTs = htobe64(db->stateTs);
6,796,337✔
901
    db->tsmaVersion = htonl(db->tsmaVersion);
6,796,337✔
902
  }
903

904
  SKv kv = {
5,342,746✔
905
      .key = HEARTBEAT_KEY_DBINFO,
906
      .valueLen = sizeof(SDbCacheInfo) * dbNum,
5,342,746✔
907
      .value = dbs,
908
  };
909

910
  tscDebug("hb got %d expired db, valueLen:%d", dbNum, kv.valueLen);
5,342,746✔
911

912
  if (NULL == req->info) {
5,342,746✔
913
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
29,358✔
914
    if (NULL == req->info) {
29,358✔
915
      taosMemoryFree(dbs);
×
916
      return terrno;
×
917
    }
918
  }
919

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

926
  return TSDB_CODE_SUCCESS;
5,342,746✔
927
}
928

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

934
  code = catalogGetExpiredSTables(pCatalog, &stbs, &stbNum);
18,451,289✔
935
  if (TSDB_CODE_SUCCESS != code) {
18,451,289✔
936
    return code;
×
937
  }
938

939
  if (stbNum <= 0) {
18,451,289✔
940
    taosMemoryFree(stbs);
14,774,968✔
941
    return TSDB_CODE_SUCCESS;
14,774,968✔
942
  }
943

944
  for (int32_t i = 0; i < stbNum; ++i) {
11,518,810✔
945
    SSTableVersion *stb = &stbs[i];
7,842,489✔
946
    stb->suid = htobe64(stb->suid);
7,842,489✔
947
    stb->sversion = htonl(stb->sversion);
7,842,489✔
948
    stb->tversion = htonl(stb->tversion);
7,842,489✔
949
    stb->smaVer = htonl(stb->smaVer);
7,842,489✔
950
  }
951

952
  SKv kv = {
3,676,321✔
953
      .key = HEARTBEAT_KEY_STBINFO,
954
      .valueLen = sizeof(SSTableVersion) * stbNum,
3,676,321✔
955
      .value = stbs,
956
  };
957

958
  tscDebug("hb got %d expired stb, valueLen:%d", stbNum, kv.valueLen);
3,676,321✔
959

960
  if (NULL == req->info) {
3,676,321✔
961
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
1,785✔
962
    if (NULL == req->info) {
1,785✔
963
      taosMemoryFree(stbs);
×
964
      return terrno;
×
965
    }
966
  }
967

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

974
  return TSDB_CODE_SUCCESS;
3,676,321✔
975
}
976

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

983
  TSC_ERR_JRET(catalogGetExpiredViews(pCatalog, &views, &viewNum, &pDynViewVer));
18,451,289✔
984

985
  if (viewNum <= 0) {
18,451,289✔
986
    taosMemoryFree(views);
18,438,062✔
987
    taosMemoryFree(pDynViewVer);
18,438,062✔
988
    return TSDB_CODE_SUCCESS;
18,438,062✔
989
  }
990

991
  for (int32_t i = 0; i < viewNum; ++i) {
26,454✔
992
    SViewVersion *view = &views[i];
13,227✔
993
    view->dbId = htobe64(view->dbId);
13,227✔
994
    view->viewId = htobe64(view->viewId);
13,227✔
995
    view->version = htonl(view->version);
13,227✔
996
  }
997

998
  tscDebug("hb got %u expired view, valueLen:%lu", viewNum, sizeof(SViewVersion) * viewNum);
13,227✔
999

1000
  if (NULL == req->info) {
13,227✔
1001
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
×
1002
    if (NULL == req->info) {
×
1003
      TSC_ERR_JRET(terrno);
×
1004
    }
1005
  }
1006

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

1013
  TSC_ERR_JRET(taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)));
13,227✔
1014

1015
  kv.key = HEARTBEAT_KEY_VIEWINFO;
13,227✔
1016
  kv.valueLen = sizeof(SViewVersion) * viewNum;
13,227✔
1017
  kv.value = views;
13,227✔
1018

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

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

1032
  code = catalogGetExpiredTsmas(pCatalog, &tsmas, &tsmaNum);
18,451,289✔
1033
  if (code) {
18,451,289✔
1034
    taosMemoryFree(tsmas);
×
1035
    return code;
×
1036
  }
1037

1038
  if (tsmaNum <= 0) {
18,451,289✔
1039
    taosMemoryFree(tsmas);
18,356,969✔
1040
    return TSDB_CODE_SUCCESS;
18,356,969✔
1041
  }
1042

1043
  for (int32_t i = 0; i < tsmaNum; ++i) {
188,640✔
1044
    STSMAVersion *tsma = &tsmas[i];
94,320✔
1045
    tsma->dbId = htobe64(tsma->dbId);
94,320✔
1046
    tsma->tsmaId = htobe64(tsma->tsmaId);
94,320✔
1047
    tsma->version = htonl(tsma->version);
94,320✔
1048
  }
1049

1050
  tscDebug("hb got %d expred tsmas, valueLen:%lu", tsmaNum, sizeof(STSMAVersion) * tsmaNum);
94,320✔
1051

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

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

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

1080
  return TSDB_CODE_SUCCESS;
22,989,835✔
1081
}
1082

1083
int32_t hbQueryHbReqHandle(SClientHbKey *connKey, void *param, SClientHbReq *req) {
26,484,065✔
1084
  int32_t   code = 0;
26,484,065✔
1085
  SHbParam *hbParam = (SHbParam *)param;
26,484,065✔
1086
  SCatalog *pCatalog = NULL;
26,484,065✔
1087

1088
  code = hbGetQueryBasicInfo(connKey, req);
26,484,065✔
1089
  if (code != TSDB_CODE_SUCCESS) {
26,484,065✔
1090
    tscWarn("hbGetQueryBasicInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
3,494,230✔
1091
    return code;
3,494,230✔
1092
  }
1093

1094
  if (hbParam->reqCnt == 0) {
22,989,835✔
1095
    code = catalogGetHandle(hbParam->clusterId, &pCatalog);
18,451,496✔
1096
    if (code != TSDB_CODE_SUCCESS) {
18,451,496✔
1097
      tscWarn("catalogGetHandle failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1098
      return code;
×
1099
    }
1100

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

1107
    if (!taosHashGet(clientHbMgr.appHbHash, &hbParam->clusterId, sizeof(hbParam->clusterId))) {
18,451,496✔
1108
      code = hbGetExpiredUserInfo(connKey, pCatalog, req);
17,472,939✔
1109
      if (TSDB_CODE_SUCCESS != code) {
17,472,939✔
1110
        tscWarn("hbGetExpiredUserInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1111
        return code;
×
1112
      }
1113
      if (clientHbMgr.appHbHash) {
17,472,939✔
1114
        code = taosHashPut(clientHbMgr.appHbHash, &hbParam->clusterId, sizeof(uint64_t), NULL, 0);
1,974,672✔
1115
        if (TSDB_CODE_SUCCESS != code) {
1,974,672✔
1116
          tscWarn("hbQueryHbReqHandle put clusterId failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId,
×
1117
                  tstrerror(code));
1118
          return code;
×
1119
        }
1120
      }
1121
    }
1122

1123
    // invoke after hbGetExpiredUserInfo
1124
    if (2 != atomic_load_8(&hbParam->pAppHbMgr->connHbFlag)) {
18,451,496✔
1125
      code = hbGetUserAuthInfo(connKey, hbParam, req);
844,896✔
1126
      if (TSDB_CODE_SUCCESS != code) {
844,896✔
1127
        tscWarn("hbGetUserAuthInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
207✔
1128
        return code;
207✔
1129
      }
1130
      atomic_store_8(&hbParam->pAppHbMgr->connHbFlag, 1);
844,689✔
1131
    }
1132

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

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

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

1165
  ++hbParam->reqCnt;  // success to get catalog info
22,989,628✔
1166

1167
  return TSDB_CODE_SUCCESS;
22,989,628✔
1168
}
1169

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

1175
  clientHbMgr.rspHandle[CONN_TYPE__QUERY] = hbQueryHbRspHandle;
1,312,459✔
1176
  clientHbMgr.rspHandle[CONN_TYPE__TMQ] = hbMqHbRspHandle;
1,312,459✔
1177
}
1,312,459✔
1178

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

1191
  int64_t  maxIpWhiteVer = 0;
22,447,568✔
1192
  void    *pIter = NULL;
22,447,568✔
1193
  SHbParam param = {0};
22,447,568✔
1194
  while ((pIter = taosHashIterate(pAppHbMgr->activeInfo, pIter))) {
50,507,991✔
1195
    SClientHbReq *pOneReq = pIter;
28,060,423✔
1196
    SClientHbKey *connKey = &pOneReq->connKey;
28,060,423✔
1197
    STscObj      *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
28,060,423✔
1198

1199
    if (!pTscObj || atomic_load_8(&pTscObj->dropped) == 1) {
28,060,423✔
1200
      if (pTscObj) releaseTscObj(connKey->tscRid);
1,576,358✔
1201
      continue;
1,576,358✔
1202
    }
1203

1204
    tstrncpy(pOneReq->userApp, pTscObj->optionInfo.userApp, sizeof(pOneReq->userApp));
26,484,065✔
1205
    tstrncpy(pOneReq->cInfo, pTscObj->optionInfo.cInfo, sizeof(pOneReq->cInfo));
26,484,065✔
1206
    pOneReq->userIp = pTscObj->optionInfo.userIp;
26,484,065✔
1207
    pOneReq->userDualIp = pTscObj->optionInfo.userDualIp;
26,484,065✔
1208
    tstrncpy(pOneReq->sVer, td_version, TSDB_VERSION_LEN);
26,484,065✔
1209

1210
    pOneReq = taosArrayPush((*pBatchReq)->reqs, pOneReq);
26,484,065✔
1211
    if (NULL == pOneReq) {
26,484,065✔
1212
      releaseTscObj(connKey->tscRid);
×
1213
      continue;
×
1214
    }
1215

1216
    switch (connKey->connType) {
26,484,065✔
1217
      case CONN_TYPE__QUERY: {
26,484,065✔
1218
        if (param.clusterId == 0) {
26,484,065✔
1219
          // init
1220
          param.clusterId = pOneReq->clusterId;
21,835,763✔
1221
          param.pAppHbMgr = pAppHbMgr;
21,835,763✔
1222
          param.connHbFlag = atomic_load_8(&pAppHbMgr->connHbFlag);
21,835,763✔
1223
        }
1224
        break;
26,484,065✔
1225
      }
1226
      default:
×
1227
        break;
×
1228
    }
1229
    if (clientHbMgr.reqHandle[connKey->connType]) {
26,484,065✔
1230
      int32_t code = (*clientHbMgr.reqHandle[connKey->connType])(connKey, &param, pOneReq);
26,484,065✔
1231
      if (code) {
26,484,065✔
1232
        tscWarn("hbGatherAllInfo failed since %s, tscRid:%" PRIi64 ", connType:%" PRIi8, tstrerror(code),
3,494,437✔
1233
                connKey->tscRid, connKey->connType);
1234
      }
1235
    }
1236

1237
    int64_t ver = atomic_load_64(&pTscObj->whiteListInfo.ver);
26,484,065✔
1238
    maxIpWhiteVer = TMAX(maxIpWhiteVer, ver);
26,484,065✔
1239
    releaseTscObj(connKey->tscRid);
26,484,065✔
1240
  }
1241
  (*pBatchReq)->ipWhiteListVer = maxIpWhiteVer;
22,447,568✔
1242

1243
  return TSDB_CODE_SUCCESS;
22,447,568✔
1244
}
1245

1246
void hbThreadFuncUnexpectedStopped(void) { atomic_store_8(&clientHbMgr.threadStop, 2); }
×
1247

1248
void hbMergeSummary(SAppClusterSummary *dst, SAppClusterSummary *src) {
2,723,739✔
1249
  dst->numOfInsertsReq += src->numOfInsertsReq;
2,723,739✔
1250
  dst->numOfInsertRows += src->numOfInsertRows;
2,723,739✔
1251
  dst->insertElapsedTime += src->insertElapsedTime;
2,723,739✔
1252
  dst->insertBytes += src->insertBytes;
2,723,739✔
1253
  dst->fetchBytes += src->fetchBytes;
2,723,739✔
1254
  dst->queryElapsedTime += src->queryElapsedTime;
2,723,739✔
1255
  dst->numOfSlowQueries += src->numOfSlowQueries;
2,723,739✔
1256
  dst->totalRequests += src->totalRequests;
2,723,739✔
1257
  dst->currentRequests += src->currentRequests;
2,723,739✔
1258
}
2,723,739✔
1259

1260
int32_t hbGatherAppInfo(void) {
22,200,614✔
1261
  SAppHbReq req = {0};
22,200,614✔
1262
  int32_t   code = TSDB_CODE_SUCCESS;
22,200,614✔
1263
  int       sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
22,200,614✔
1264
  if (sz > 0) {
22,200,614✔
1265
    req.pid = taosGetPId();
22,200,614✔
1266
    req.appId = clientHbMgr.appId;
22,200,614✔
1267
    TSC_ERR_RET(taosGetAppName(req.name, NULL));
22,200,614✔
1268
  }
1269

1270
  taosHashClear(clientHbMgr.appSummary);
22,200,614✔
1271

1272
  for (int32_t i = 0; i < sz; ++i) {
47,424,868✔
1273
    SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
25,224,254✔
1274
    if (pAppHbMgr == NULL) continue;
25,224,254✔
1275

1276
    int64_t    clusterId = pAppHbMgr->pAppInstInfo->clusterId;
25,224,254✔
1277
    SAppHbReq *pApp = taosHashGet(clientHbMgr.appSummary, &clusterId, sizeof(clusterId));
25,224,254✔
1278
    if (NULL == pApp) {
25,224,254✔
1279
      (void)memcpy(&req.summary, &pAppHbMgr->pAppInstInfo->summary, sizeof(req.summary));
22,500,515✔
1280
      req.startTime = pAppHbMgr->startTime;
22,500,515✔
1281
      TSC_ERR_RET(taosHashPut(clientHbMgr.appSummary, &clusterId, sizeof(clusterId), &req, sizeof(req)));
22,500,515✔
1282
    } else {
1283
      if (pAppHbMgr->startTime < pApp->startTime) {
2,723,739✔
1284
        pApp->startTime = pAppHbMgr->startTime;
×
1285
      }
1286

1287
      hbMergeSummary(&pApp->summary, &pAppHbMgr->pAppInstInfo->summary);
2,723,739✔
1288
    }
1289
  }
1290

1291
  return TSDB_CODE_SUCCESS;
22,200,614✔
1292
}
1293

1294
static void *hbThreadFunc(void *param) {
1,312,459✔
1295
  setThreadName("hb");
1,312,459✔
1296
#ifdef WINDOWS
1297
  if (taosCheckCurrentInDll()) {
1298
    atexit(hbThreadFuncUnexpectedStopped);
1299
  }
1300
#endif
1301
  while (1) {
21,453,168✔
1302
    if (1 == clientHbMgr.threadStop) {
22,765,627✔
1303
      break;
557,704✔
1304
    }
1305

1306
    if (TSDB_CODE_SUCCESS != taosThreadMutexLock(&clientHbMgr.lock)) {
22,207,923✔
1307
      tscError("taosThreadMutexLock failed");
×
1308
      return NULL;
×
1309
    }
1310

1311
    int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
22,207,923✔
1312
    if (sz > 0) {
22,207,923✔
1313
      if (TSDB_CODE_SUCCESS != hbGatherAppInfo()) {
22,200,614✔
1314
        tscError("hbGatherAppInfo failed");
×
1315
        return NULL;
×
1316
      }
1317
      if (sz > 1 && !clientHbMgr.appHbHash) {
22,200,614✔
1318
        clientHbMgr.appHbHash = taosHashInit(0, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), true, HASH_NO_LOCK);
40,812✔
1319
        if (NULL == clientHbMgr.appHbHash) {
40,812✔
1320
          tscError("taosHashInit failed");
×
1321
          return NULL;
×
1322
        }
1323
      }
1324
      taosHashClear(clientHbMgr.appHbHash);
22,200,614✔
1325
    }
1326

1327
    for (int i = 0; i < sz; i++) {
47,432,177✔
1328
      SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
25,224,254✔
1329
      if (pAppHbMgr == NULL) {
25,224,254✔
1330
        continue;
726,386✔
1331
      }
1332

1333
      int32_t connCnt = atomic_load_32(&pAppHbMgr->connKeyCnt);
25,224,254✔
1334
      if (connCnt == 0) {
25,224,254✔
1335
        continue;
2,776,686✔
1336
      }
1337
      SClientHbBatchReq *pReq = NULL;
22,447,568✔
1338
      int32_t            code = hbGatherAllInfo(pAppHbMgr, &pReq);
22,447,568✔
1339
      if (TSDB_CODE_SUCCESS != code || taosArrayGetP(clientHbMgr.appHbMgrs, i) == NULL) {
22,447,568✔
1340
        terrno = code ? code : TSDB_CODE_OUT_OF_RANGE;
×
1341
        tFreeClientHbBatchReq(pReq);
×
1342
        continue;
×
1343
      }
1344
      int tlen = tSerializeSClientHbBatchReq(NULL, 0, pReq);
22,447,568✔
1345
      if (tlen == -1) {
22,447,568✔
1346
        tFreeClientHbBatchReq(pReq);
×
1347
        break;
×
1348
      }
1349
      void *buf = taosMemoryMalloc(tlen);
22,447,568✔
1350
      if (buf == NULL) {
22,447,568✔
1351
        tFreeClientHbBatchReq(pReq);
×
1352
        // hbClearReqInfo(pAppHbMgr);
1353
        break;
×
1354
      }
1355

1356
      if (tSerializeSClientHbBatchReq(buf, tlen, pReq) == -1) {
22,447,568✔
1357
        tFreeClientHbBatchReq(pReq);
×
1358
        taosMemoryFree(buf);
×
1359
        break;
×
1360
      }
1361
      SMsgSendInfo *pInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
22,447,568✔
1362

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

1386
      SAppInstInfo *pAppInstInfo = pAppHbMgr->pAppInstInfo;
22,447,568✔
1387
      SEpSet        epSet = getEpSet_s(&pAppInstInfo->mgmtEp);
22,447,568✔
1388
      if (TSDB_CODE_SUCCESS != asyncSendMsgToServer(pAppInstInfo->pTransporter, &epSet, NULL, pInfo)) {
22,447,568✔
1389
        tscWarn("failed to async send msg to server");
×
1390
      }
1391
      tFreeClientHbBatchReq(pReq);
22,447,568✔
1392
      // hbClearReqInfo(pAppHbMgr);
1393
      (void)atomic_add_fetch_32(&pAppHbMgr->reportCnt, 1);
22,447,568✔
1394
    }
1395

1396
    if (TSDB_CODE_SUCCESS != taosThreadMutexUnlock(&clientHbMgr.lock)) {
22,207,923✔
1397
      tscError("taosThreadMutexLock failed");
×
1398
      return NULL;
×
1399
    }
1400
    taosMsleep(HEARTBEAT_INTERVAL);
22,207,923✔
1401
  }
1402
  taosHashCleanup(clientHbMgr.appHbHash);
557,704✔
1403
  return NULL;
557,704✔
1404
}
1405

1406
static int32_t hbCreateThread() {
1,312,459✔
1407
  int32_t      code = TSDB_CODE_SUCCESS;
1,312,459✔
1408
  TdThreadAttr thAttr;
1,198,503✔
1409
  TSC_ERR_JRET(taosThreadAttrInit(&thAttr));
1,312,459✔
1410
  TSC_ERR_JRET(taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE));
1,312,459✔
1411
#ifdef TD_COMPACT_OS
1412
  TSC_ERR_JRET(taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL));
1413
#endif
1414

1415
  if (taosThreadCreate(&clientHbMgr.thread, &thAttr, hbThreadFunc, NULL) != 0) {
1,312,459✔
1416
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
1417
    TSC_ERR_RET(terrno);
×
1418
  }
1419
  (void)taosThreadAttrDestroy(&thAttr);
1,312,459✔
1420
_return:
1,312,459✔
1421

1422
  if (code) {
1,312,459✔
1423
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
1424
    TSC_ERR_RET(terrno);
×
1425
  }
1426

1427
  return code;
1,312,459✔
1428
}
1429

1430
static void hbStopThread() {
1,314,412✔
1431
  if (0 == atomic_load_8(&clientHbMgr.inited)) {
1,314,412✔
1432
    return;
1,953✔
1433
  }
1434
  if (atomic_val_compare_exchange_8(&clientHbMgr.threadStop, 0, 1)) {
1,312,459✔
1435
    tscDebug("hb thread already stopped");
×
1436
    return;
×
1437
  }
1438

1439
  int32_t code = TSDB_CODE_SUCCESS;
1,312,459✔
1440
  // thread quit mode kill or inner exit from self-thread
1441
  if (clientHbMgr.quitByKill) {
1,312,459✔
1442
    code = taosThreadKill(clientHbMgr.thread, 0);
805,736✔
1443
    if (TSDB_CODE_SUCCESS != code) {
805,736✔
1444
      tscError("taosThreadKill failed since %s", tstrerror(code));
×
1445
    }
1446
  } else {
1447
    code = taosThreadJoin(clientHbMgr.thread, NULL);
506,723✔
1448
    if (TSDB_CODE_SUCCESS != code) {
506,723✔
1449
      tscError("taosThreadJoin failed since %s", tstrerror(code));
×
1450
    }
1451
  }
1452

1453
  tscDebug("hb thread stopped");
1,312,459✔
1454
}
1455

1456
int32_t appHbMgrInit(SAppInstInfo *pAppInstInfo, char *key, SAppHbMgr **pAppHbMgr) {
1,381,950✔
1457
  int32_t code = TSDB_CODE_SUCCESS;
1,381,950✔
1458
  TSC_ERR_RET(hbMgrInit());
1,381,950✔
1459
  *pAppHbMgr = taosMemoryMalloc(sizeof(SAppHbMgr));
1,381,950✔
1460
  if (*pAppHbMgr == NULL) {
1,381,950✔
1461
    TSC_ERR_JRET(terrno);
×
1462
  }
1463
  // init stat
1464
  (*pAppHbMgr)->startTime = taosGetTimestampMs();
2,637,869✔
1465
  (*pAppHbMgr)->connKeyCnt = 0;
1,381,950✔
1466
  (*pAppHbMgr)->connHbFlag = 0;
1,381,950✔
1467
  (*pAppHbMgr)->reportCnt = 0;
1,381,950✔
1468
  (*pAppHbMgr)->reportBytes = 0;
1,381,950✔
1469
  (*pAppHbMgr)->key = taosStrdup(key);
1,381,950✔
1470
  if ((*pAppHbMgr)->key == NULL) {
1,381,950✔
1471
    TSC_ERR_JRET(terrno);
×
1472
  }
1473

1474
  // init app info
1475
  (*pAppHbMgr)->pAppInstInfo = pAppInstInfo;
1,381,950✔
1476

1477
  // init hash info
1478
  (*pAppHbMgr)->activeInfo = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
1,381,950✔
1479

1480
  if ((*pAppHbMgr)->activeInfo == NULL) {
1,381,950✔
1481
    TSC_ERR_JRET(terrno);
×
1482
  }
1483

1484
  // taosHashSetFreeFp(pAppHbMgr->activeInfo, tFreeClientHbReq);
1485

1486
  TSC_ERR_JRET(taosThreadMutexLock(&clientHbMgr.lock));
1,381,950✔
1487
  if (taosArrayPush(clientHbMgr.appHbMgrs, &(*pAppHbMgr)) == NULL) {
2,763,900✔
1488
    code = terrno;
×
1489
    (void)taosThreadMutexUnlock(&clientHbMgr.lock);
×
1490
    goto _return;
×
1491
  }
1492
  (*pAppHbMgr)->idx = taosArrayGetSize(clientHbMgr.appHbMgrs) - 1;
1,381,950✔
1493
  TSC_ERR_JRET(taosThreadMutexUnlock(&clientHbMgr.lock));
1,381,950✔
1494

1495
  return TSDB_CODE_SUCCESS;
1,381,950✔
1496
_return:
×
1497
  taosMemoryFree(*pAppHbMgr);
×
1498
  return code;
×
1499
}
1500

1501
void hbFreeAppHbMgr(SAppHbMgr *pTarget) {
1,381,950✔
1502
  void *pIter = taosHashIterate(pTarget->activeInfo, NULL);
1,381,950✔
1503
  while (pIter != NULL) {
1,617,961✔
1504
    SClientHbReq *pOneReq = pIter;
236,011✔
1505
    tFreeClientHbReq(pOneReq);
1506
    pIter = taosHashIterate(pTarget->activeInfo, pIter);
236,011✔
1507
  }
1508
  taosHashCleanup(pTarget->activeInfo);
1,381,950✔
1509
  pTarget->activeInfo = NULL;
1,381,950✔
1510

1511
  taosMemoryFree(pTarget->key);
1,381,950✔
1512
  taosMemoryFree(pTarget);
1,381,950✔
1513
}
1,381,950✔
1514

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

1537
void appHbMgrCleanup(void) {
1,312,459✔
1538
  int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
1,312,459✔
1539
  for (int i = 0; i < sz; i++) {
2,694,409✔
1540
    SAppHbMgr *pTarget = taosArrayGetP(clientHbMgr.appHbMgrs, i);
1,381,950✔
1541
    if (pTarget == NULL) continue;
1,381,950✔
1542
    hbFreeAppHbMgr(pTarget);
1,381,950✔
1543
  }
1544
}
1,312,459✔
1545

1546
int32_t hbMgrInit() {
1,381,950✔
1547
  // init once
1548
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 0, 1);
1,381,950✔
1549
  if (old == 1) return 0;
1,381,950✔
1550

1551
  clientHbMgr.appId = tGenIdPI64();
1,312,459✔
1552
  tscInfo("app initialized, appId:0x%" PRIx64, clientHbMgr.appId);
1,312,459✔
1553

1554
  clientHbMgr.appSummary = taosHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
1,312,459✔
1555
  if (NULL == clientHbMgr.appSummary) {
1,312,459✔
1556
    uError("hbMgrInit:taosHashInit error") return terrno;
×
1557
  }
1558
  clientHbMgr.appHbMgrs = taosArrayInit(0, sizeof(void *));
1,312,459✔
1559
  if (NULL == clientHbMgr.appHbMgrs) {
1,312,459✔
1560
    uError("hbMgrInit:taosArrayInit error") return terrno;
×
1561
  }
1562
  TdThreadMutexAttr attr = {0};
1,312,459✔
1563

1564
  int ret = taosThreadMutexAttrInit(&attr);
1,312,459✔
1565
  if (ret != 0) {
1,312,459✔
1566
    uError("hbMgrInit:taosThreadMutexAttrInit error") return ret;
×
1567
  }
1568

1569
  ret = taosThreadMutexAttrSetType(&attr, PTHREAD_MUTEX_RECURSIVE);
1,312,459✔
1570
  if (ret != 0) {
1,312,459✔
1571
    uError("hbMgrInit:taosThreadMutexAttrSetType error") return ret;
×
1572
  }
1573

1574
  ret = taosThreadMutexInit(&clientHbMgr.lock, &attr);
1,312,459✔
1575
  if (ret != 0) {
1,312,459✔
1576
    uError("hbMgrInit:taosThreadMutexInit error") return ret;
×
1577
  }
1578

1579
  ret = taosThreadMutexAttrDestroy(&attr);
1,312,459✔
1580
  if (ret != 0) {
1,312,459✔
1581
    uError("hbMgrInit:taosThreadMutexAttrDestroy error") return ret;
×
1582
  }
1583

1584
  // init handle funcs
1585
  hbMgrInitHandle();
1586

1587
  // init backgroud thread
1588
  ret = hbCreateThread();
1,312,459✔
1589
  if (ret != 0) {
1,312,459✔
1590
    uError("hbMgrInit:hbCreateThread error") return ret;
×
1591
  }
1592

1593
  return 0;
1,312,459✔
1594
}
1595

1596
void hbMgrCleanUp() {
1,314,412✔
1597
  hbStopThread();
1,314,412✔
1598

1599
  // destroy all appHbMgr
1600
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 1, 0);
1,314,412✔
1601
  if (old == 0) return;
1,314,412✔
1602

1603
  int32_t code = taosThreadMutexLock(&clientHbMgr.lock);
1,312,459✔
1604
  if (TSDB_CODE_SUCCESS != code) {
1,312,459✔
1605
    tscError("failed to lock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1606
  }
1607
  appHbMgrCleanup();
1,312,459✔
1608
  taosArrayDestroy(clientHbMgr.appHbMgrs);
1,312,459✔
1609
  clientHbMgr.appHbMgrs = NULL;
1,312,459✔
1610
  code = taosThreadMutexUnlock(&clientHbMgr.lock);
1,312,459✔
1611
  if (TSDB_CODE_SUCCESS != code) {
1,312,459✔
1612
    tscError("failed to unlock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1613
  }
1614
}
1615

1616
int32_t hbRegisterConnImpl(SAppHbMgr *pAppHbMgr, SClientHbKey connKey, int64_t clusterId) {
3,109,526✔
1617
  // init hash in activeinfo
1618
  void *data = taosHashGet(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
3,109,526✔
1619
  if (data != NULL) {
3,109,526✔
1620
    return 0;
×
1621
  }
1622
  SClientHbReq hbReq = {0};
3,109,526✔
1623
  hbReq.connKey = connKey;
3,109,526✔
1624
  hbReq.clusterId = clusterId;
3,109,526✔
1625
  // hbReq.info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
1626

1627
  TSC_ERR_RET(taosHashPut(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey), &hbReq, sizeof(SClientHbReq)));
3,109,526✔
1628

1629
  (void)atomic_add_fetch_32(&pAppHbMgr->connKeyCnt, 1);
3,109,526✔
1630
  return 0;
3,109,526✔
1631
}
1632

1633
int32_t hbRegisterConn(SAppHbMgr *pAppHbMgr, int64_t tscRefId, int64_t clusterId, int8_t connType) {
3,190,859✔
1634
  SClientHbKey connKey = {
3,190,859✔
1635
      .tscRid = tscRefId,
1636
      .connType = connType,
1637
  };
1638

1639
  switch (connType) {
3,190,859✔
1640
    case CONN_TYPE__QUERY: {
3,109,526✔
1641
      return hbRegisterConnImpl(pAppHbMgr, connKey, clusterId);
3,109,526✔
1642
    }
1643
    case CONN_TYPE__TMQ: {
81,333✔
1644
      return 0;
81,333✔
1645
    }
1646
    default:
×
1647
      return 0;
×
1648
  }
1649
}
1650

1651
void hbDeregisterConn(STscObj *pTscObj, SClientHbKey connKey) {
2,967,438✔
1652
  int32_t code = taosThreadMutexLock(&clientHbMgr.lock);
2,967,438✔
1653
  if (TSDB_CODE_SUCCESS != code) {
2,967,438✔
1654
    tscError("failed to lock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1655
  }
1656
  SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, pTscObj->appHbMgrIdx);
2,967,438✔
1657
  if (pAppHbMgr) {
2,967,438✔
1658
    SClientHbReq *pReq = taosHashAcquire(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
2,966,816✔
1659
    if (pReq) {
2,966,816✔
1660
      tFreeClientHbReq(pReq);
1661
      code = taosHashRemove(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
2,873,515✔
1662
      if (TSDB_CODE_SUCCESS != code) {
2,873,515✔
1663
        tscError("hbDeregisterConn taosHashRemove error, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1664
      }
1665
      taosHashRelease(pAppHbMgr->activeInfo, pReq);
2,873,515✔
1666
      (void)atomic_sub_fetch_32(&pAppHbMgr->connKeyCnt, 1);
2,873,515✔
1667
    }
1668
  }
1669
  code = taosThreadMutexUnlock(&clientHbMgr.lock);
2,967,438✔
1670
  if (TSDB_CODE_SUCCESS != code) {
2,967,438✔
1671
    tscError("failed to unlock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1672
  }
1673
}
2,967,438✔
1674

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