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

taosdata / TDengine / #4854

17 Nov 2025 09:53AM UTC coverage: 64.06% (+0.1%) from 63.951%
#4854

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%)

571 existing lines in 112 files now uncovered.

151123 of 235910 relevant lines covered (64.06%)

114377315.5 hits per line

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

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

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

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

63
    TSC_ERR_JRET(catalogUpdateUserAuthInfo(pCatalog, rsp));
900,514✔
64
  }
65

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

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

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

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

85
    SClientHbReq    *pReq = NULL;
1,520,241✔
86
    SGetUserAuthRsp *pRsp = NULL;
1,520,241✔
87
    while ((pReq = taosHashIterate(hbMgr->activeInfo, pReq))) {
2,769,645✔
88
      STscObj *pTscObj = (STscObj *)acquireTscObj(pReq->connKey.tscRid);
1,501,233✔
89
      if (!pTscObj) {
1,501,233✔
90
        continue;
125,171✔
91
      }
92

93
      if (!pRsp) {
1,376,062✔
94
        for (int32_t j = 0; j < TARRAY_SIZE(batchRsp->pArray); ++j) {
1,447,035✔
95
          SGetUserAuthRsp *rsp = TARRAY_GET_ELEM(batchRsp->pArray, j);
1,195,206✔
96
          if (0 == strncmp(rsp->user, pTscObj->user, TSDB_USER_LEN)) {
1,195,206✔
97
            pRsp = rsp;
909,694✔
98
            break;
909,694✔
99
          }
100
        }
101
        if (!pRsp) {
1,161,523✔
102
          releaseTscObj(pReq->connKey.tscRid);
251,829✔
103
          taosHashCancelIterate(hbMgr->activeInfo, pReq);
251,829✔
104
          break;
251,829✔
105
        }
106
      }
107

108
      if (pRsp->dropped == 1) {
1,124,233✔
109
        if (atomic_val_compare_exchange_8(&pTscObj->dropped, 0, 1) == 0) {
17,660✔
110
          if (pTscObj->userDroppedInfo.fp) {
14,694✔
111
            SPassInfo *dropInfo = &pTscObj->userDroppedInfo;
13,050✔
112
            if (dropInfo->fp) {
13,050✔
113
              (*dropInfo->fp)(dropInfo->param, NULL, TAOS_NOTIFY_USER_DROPPED);
13,050✔
114
            }
115
          }
116
        }
117
        releaseTscObj(pReq->connKey.tscRid);
17,660✔
118
        continue;
17,660✔
119
      }
120

121
      pTscObj->authVer = pRsp->version;
1,106,573✔
122

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

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

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

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

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

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

203
  *pInfo = vgInfo;
1,297,707✔
204
  return code;
1,297,707✔
205
}
206

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

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

217
  int32_t numOfBatchs = taosArrayGetSize(batchRsp.pArray);
5,246,428✔
218
  for (int32_t i = 0; i < numOfBatchs; ++i) {
6,721,689✔
219
    SDbHbRsp *rsp = taosArrayGet(batchRsp.pArray, i);
1,475,261✔
220
    if (NULL == rsp) {
1,475,261✔
221
      code = terrno;
×
222
      goto _return;
×
223
    }
224
    if (rsp->useDbRsp) {
1,475,261✔
225
      tscDebug("hb use db rsp, db:%s, vgVersion:%d, stateTs:%" PRId64 ", uid:%" PRIx64, rsp->useDbRsp->db,
1,092,102✔
226
               rsp->useDbRsp->vgVersion, rsp->useDbRsp->stateTs, rsp->useDbRsp->uid);
227

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

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

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

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

248
          TSC_ERR_JRET(catalogUpdateDBVgInfo(
232,339✔
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,475,261✔
256
      tscDebug("hb db cfg rsp, db:%s, cfgVersion:%d", rsp->cfgRsp->db, rsp->cfgRsp->cfgVersion);
298,242✔
257
      code = catalogUpdateDbCfg(pCatalog, rsp->cfgRsp->db, rsp->cfgRsp->dbId, rsp->cfgRsp);
298,242✔
258
      rsp->cfgRsp = NULL;
298,242✔
259
    }
260
    if (rsp->pTsmaRsp) {
1,475,261✔
261
      if (rsp->pTsmaRsp->pTsmas) {
821,000✔
262
        for (int32_t i = 0; i < rsp->pTsmaRsp->pTsmas->size; ++i) {
2,856✔
263
          STableTSMAInfo *pTsma = taosArrayGetP(rsp->pTsmaRsp->pTsmas, i);
1,428✔
264
          if (NULL == pTsma) {
1,428✔
265
            TSC_ERR_JRET(TSDB_CODE_OUT_OF_RANGE);
×
266
          }
267
          TSC_ERR_JRET(catalogAsyncUpdateTSMA(pCatalog, &pTsma, rsp->dbTsmaVersion));
1,428✔
268
        }
269
        taosArrayClear(rsp->pTsmaRsp->pTsmas);
1,428✔
270
      } else {
271
        TSC_ERR_JRET(catalogAsyncUpdateDbTsmaVersion(pCatalog, rsp->dbTsmaVersion, rsp->db, rsp->dbId));
819,572✔
272
      }
273
    }
274
  }
275

276
_return:
5,246,428✔
277

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

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

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

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

313
  int32_t numOfIndex = taosArrayGetSize(hbRsp.pIndexRsp);
3,548,078✔
314
  for (int32_t i = 0; i < numOfIndex; ++i) {
3,548,078✔
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,548,078✔
324
  taosArrayDestroy(hbRsp.pIndexRsp);
3,548,078✔
325
  hbRsp.pIndexRsp = NULL;
3,548,078✔
326

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

331
static int32_t hbProcessDynViewRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
5,099✔
332
  return catalogUpdateDynViewVer(pCatalog, (SDynViewVersion *)value);
5,099✔
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,099✔
345
  int32_t code = TSDB_CODE_SUCCESS;
5,099✔
346

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

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

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

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

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

387
  int32_t numOfTsma = taosArrayGetSize(hbRsp.pTsmas);
92,820✔
388
  for (int32_t i = 0; i < numOfTsma; ++i) {
92,820✔
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:
92,820✔
404
  taosArrayDestroy(hbRsp.pTsmas);
92,820✔
405
  return code;
92,820✔
406
}
407

408
static void hbProcessQueryRspKvs(int32_t kvNum, SArray *pKvs, struct SCatalog *pCatalog, SAppHbMgr *pAppHbMgr) {
8,189,425✔
409
  for (int32_t i = 0; i < kvNum; ++i) {
17,967,556✔
410
    SKv *kv = taosArrayGet(pKvs, i);
9,778,131✔
411
    if (NULL == kv) {
9,778,131✔
412
      tscError("invalid hb kv, idx:%d", i);
×
413
      continue;
×
414
    }
415
    switch (kv->key) {
9,778,131✔
416
      case HEARTBEAT_KEY_USER_AUTHINFO: {
880,607✔
417
        if (kv->valueLen <= 0 || NULL == kv->value) {
880,607✔
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)) {
880,607✔
422
          tscError("process user auth info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
423
          break;
×
424
        }
425
        break;
880,607✔
426
      }
427
      case HEARTBEAT_KEY_DBINFO: {
5,246,428✔
428
        if (kv->valueLen <= 0 || NULL == kv->value) {
5,246,428✔
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,246,428✔
433
          tscError("process db info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
434
          break;
×
435
        }
436
        break;
5,246,428✔
437
      }
438
      case HEARTBEAT_KEY_STBINFO: {
3,548,078✔
439
        if (kv->valueLen <= 0 || NULL == kv->value) {
3,548,078✔
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,548,078✔
444
          tscError("process stb info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
445
          break;
×
446
        }
447
        break;
3,548,078✔
448
      }
449
#ifdef TD_ENTERPRISE
450
      case HEARTBEAT_KEY_DYN_VIEW: {
5,099✔
451
        if (kv->valueLen <= 0 || NULL == kv->value) {
5,099✔
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,099✔
456
          tscError("Process dyn view response failed, len:%d, value:%p", kv->valueLen, kv->value);
×
457
          break;
×
458
        }
459
        break;
5,099✔
460
      }
461
      case HEARTBEAT_KEY_VIEWINFO: {
5,099✔
462
        if (kv->valueLen <= 0 || NULL == kv->value) {
5,099✔
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,099✔
467
          tscError("Process view info response failed, len:%d, value:%p", kv->valueLen, kv->value);
×
468
          break;
×
469
        }
470
        break;
5,099✔
471
      }
472
#endif
473
      case HEARTBEAT_KEY_TSMA: {
92,820✔
474
        if (kv->valueLen <= 0 || !kv->value) {
92,820✔
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)) {
92,820✔
478
          tscError("Process tsma info response failed, len:%d, value:%p", kv->valueLen, kv->value);
×
479
        }
480
        break;
92,820✔
481
      }
482
      default:
×
483
        tscError("invalid hb key type:%d", kv->key);
×
484
        break;
×
485
    }
486
  }
487
}
8,189,425✔
488

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

497
  if (pRsp->query) {
25,386,577✔
498
    STscObj *pTscObj = (STscObj *)acquireTscObj(pRsp->connKey.tscRid);
21,956,988✔
499
    if (NULL == pTscObj) {
21,956,988✔
500
      tscDebug("tscObj rid %" PRIx64 " not exist", pRsp->connKey.tscRid);
3,354✔
501
    } else {
502
      if (pRsp->query->totalDnodes > 1) {
21,953,634✔
503
        SEpSet  originEpset = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
6,343,596✔
504
        if (!isEpsetEqual(&originEpset, &pRsp->query->epSet)) {
6,343,596✔
505
          SEpSet *pOrig = &originEpset;
61,058✔
506
          SEp    *pOrigEp = &pOrig->eps[pOrig->inUse];
61,058✔
507
          SEp    *pNewEp = &pRsp->query->epSet.eps[pRsp->query->epSet.inUse];
61,058✔
508
          tscDebug("mnode epset updated from %d/%d=>%s:%d to %d/%d=>%s:%d in hb", pOrig->inUse, pOrig->numOfEps,
61,058✔
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);
61,058✔
513
        }
514
      }
515

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

522
      if (pRsp->query->killRid) {
21,953,634✔
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) {
21,953,634✔
534
        taos_close_internal(pTscObj);
×
535
      }
536

537
      if (pRsp->query->pQnodeList) {
21,953,634✔
538
        if (TSDB_CODE_SUCCESS != updateQnodeList(pTscObj->pAppInfo, pRsp->query->pQnodeList)) {
103,154✔
539
          tscWarn("update qnode list failed");
×
540
        }
541
      }
542

543
      releaseTscObj(pRsp->connKey.tscRid);
21,953,634✔
544
    }
545
  }
546

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

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

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

561
  taosHashRelease(pAppHbMgr->activeInfo, pReq);
25,386,577✔
562

563
  return TSDB_CODE_SUCCESS;
25,386,577✔
564
}
565

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

571
  static int32_t    emptyRspNum = 0;
572
  int32_t           idx = *(int32_t *)param;
22,188,975✔
573
  SClientHbBatchRsp pRsp = {0};
22,188,975✔
574
  if (TSDB_CODE_SUCCESS == code) {
22,188,975✔
575
    code = tDeserializeSClientHbBatchRsp(pMsg->pData, pMsg->len, &pRsp);
21,721,525✔
576
    if (TSDB_CODE_SUCCESS != code) {
21,720,037✔
577
      tscError("deserialize hb rsp failed");
1,432✔
578
    }
579
    int32_t now = taosGetTimestampSec();
21,720,037✔
580
    int32_t delta = abs(now - pRsp.svrTimestamp);
21,716,794✔
581
    if (delta > timestampDeltaLimit) {
21,716,794✔
582
      code = TSDB_CODE_TIME_UNSYNCED;
1,432✔
583
      tscError("time diff:%ds is too big", delta);
1,432✔
584
    }
585
  }
586

587
  int32_t rspNum = taosArrayGetSize(pRsp.rsps);
22,184,244✔
588

589
  (void)taosThreadMutexLock(&clientHbMgr.lock);
22,186,982✔
590

591
  SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, idx);
22,188,975✔
592
  if (pAppHbMgr == NULL) {
22,188,975✔
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,188,975✔
602

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

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

620
  if (rspNum) {
21,720,093✔
621
    tscDebug("hb got %d rsp, %d empty rsp received before", rspNum,
21,126,173✔
622
             atomic_val_compare_exchange_32(&emptyRspNum, emptyRspNum, 0));
623
  } else {
624
    (void)atomic_add_fetch_32(&emptyRspNum, 1);
593,920✔
625
  }
626

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

635
  (void)taosThreadMutexUnlock(&clientHbMgr.lock);
21,720,093✔
636

637
  tFreeClientHbBatchRsp(&pRsp);
638

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

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

650
  void *pIter = taosHashIterate(pObj->pRequests, NULL);
23,931,079✔
651
  while (pIter != NULL) {
48,472,278✔
652
    int64_t     *rid = pIter;
24,541,199✔
653
    SRequestObj *pRequest = acquireRequest(*rid);
24,541,199✔
654
    if (NULL == pRequest) {
24,541,199✔
655
      pIter = taosHashIterate(pObj->pRequests, pIter);
15,026✔
656
      continue;
15,026✔
657
    }
658

659
    if (pRequest->killed || 0 == pRequest->body.queryJob) {
24,526,173✔
660
      (void)releaseRequest(*rid);
9,296,114✔
661
      pIter = taosHashIterate(pObj->pRequests, pIter);
9,296,114✔
662
      continue;
9,296,114✔
663
    }
664

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

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

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

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

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

706
  return code;
23,931,079✔
707
}
708

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

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

723
  hbBasic->connId = pTscObj->connId;
25,891,412✔
724

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

733
  hbBasic->queryDesc = taosArrayInit(numOfQueries, sizeof(SQueryDesc));
23,931,079✔
734
  if (NULL == hbBasic->queryDesc) {
23,931,079✔
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);
23,931,079✔
742
  if (code) {
23,931,079✔
743
    releaseTscObj(connKey->tscRid);
3,463,130✔
744
    if (hbBasic->queryDesc) {
3,463,130✔
745
      taosArrayDestroyEx(hbBasic->queryDesc, tFreeClientHbQueryDesc);
3,463,130✔
746
    }
747
    taosMemoryFree(hbBasic);
3,463,130✔
748
    return code;
3,463,130✔
749
  }
750

751
  req->query = hbBasic;
20,467,949✔
752
  releaseTscObj(connKey->tscRid);
20,467,949✔
753

754
  return TSDB_CODE_SUCCESS;
20,467,949✔
755
}
756

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

764
  int32_t code = 0;
834,620✔
765

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

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

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

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

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

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

827
  return code;
834,620✔
828
}
829

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

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

840
  if (userNum <= 0) {
17,280,022✔
841
    taosMemoryFree(users);
637,829✔
842
    return TSDB_CODE_SUCCESS;
637,829✔
843
  }
844

845
  for (int32_t i = 0; i < userNum; ++i) {
33,708,099✔
846
    SUserAuthVersion *user = &users[i];
17,065,906✔
847
    user->version = htonl(user->version);
17,065,906✔
848
  }
849

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

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

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

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

872
  return TSDB_CODE_SUCCESS;
16,642,193✔
873
}
874

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

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

885
  if (dbNum <= 0) {
18,268,544✔
886
    taosMemoryFree(dbs);
12,989,112✔
887
    return TSDB_CODE_SUCCESS;
12,989,112✔
888
  }
889

890
  for (int32_t i = 0; i < dbNum; ++i) {
11,872,500✔
891
    SDbCacheInfo *db = &dbs[i];
6,593,068✔
892
    tscDebug("the %dth expired db:%s, dbId:%" PRId64
6,593,068✔
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,593,068✔
897
    db->vgVersion = htonl(db->vgVersion);
6,593,068✔
898
    db->cfgVersion = htonl(db->cfgVersion);
6,593,068✔
899
    db->numOfTable = htonl(db->numOfTable);
6,593,068✔
900
    db->stateTs = htobe64(db->stateTs);
6,593,068✔
901
    db->tsmaVersion = htonl(db->tsmaVersion);
6,593,068✔
902
  }
903

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

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

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

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

926
  return TSDB_CODE_SUCCESS;
5,279,432✔
927
}
928

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

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

939
  if (stbNum <= 0) {
18,268,544✔
940
    taosMemoryFree(stbs);
14,689,498✔
941
    return TSDB_CODE_SUCCESS;
14,689,498✔
942
  }
943

944
  for (int32_t i = 0; i < stbNum; ++i) {
12,232,365✔
945
    SSTableVersion *stb = &stbs[i];
8,653,319✔
946
    stb->suid = htobe64(stb->suid);
8,653,319✔
947
    stb->sversion = htonl(stb->sversion);
8,653,319✔
948
    stb->tversion = htonl(stb->tversion);
8,653,319✔
949
    stb->smaVer = htonl(stb->smaVer);
8,653,319✔
950
  }
951

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

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

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

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

974
  return TSDB_CODE_SUCCESS;
3,579,046✔
975
}
976

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

983
  TSC_ERR_JRET(catalogGetExpiredViews(pCatalog, &views, &viewNum, &pDynViewVer));
18,268,544✔
984

985
  if (viewNum <= 0) {
18,268,544✔
986
    taosMemoryFree(views);
18,234,430✔
987
    taosMemoryFree(pDynViewVer);
18,234,430✔
988
    return TSDB_CODE_SUCCESS;
18,234,430✔
989
  }
990

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

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

1000
  if (NULL == req->info) {
34,114✔
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 = {
34,114✔
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)));
34,114✔
1014

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

1019
  TSC_ERR_JRET(taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)));
34,114✔
1020
  return TSDB_CODE_SUCCESS;
34,114✔
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,268,544✔
1028
  int32_t       code = 0;
18,268,544✔
1029
  uint32_t      tsmaNum = 0;
18,268,544✔
1030
  STSMAVersion *tsmas = NULL;
18,268,544✔
1031

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

1038
  if (tsmaNum <= 0) {
18,268,544✔
1039
    taosMemoryFree(tsmas);
18,175,724✔
1040
    return TSDB_CODE_SUCCESS;
18,175,724✔
1041
  }
1042

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

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

1052
  if (!pReq->info) {
92,820✔
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};
92,820✔
1061
  code = taosHashPut(pReq->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
92,820✔
1062
  if (TSDB_CODE_SUCCESS != code) {
92,820✔
1063
    taosMemoryFree(tsmas);
×
1064
    return code;
×
1065
  }
1066
  return TSDB_CODE_SUCCESS;
92,820✔
1067
}
1068

1069
int32_t hbGetAppInfo(int64_t clusterId, SClientHbReq *req) {
22,428,282✔
1070
  SAppHbReq *pApp = taosHashGet(clientHbMgr.appSummary, &clusterId, sizeof(clusterId));
22,428,282✔
1071
  if (NULL != pApp) {
22,428,282✔
1072
    (void)memcpy(&req->app, pApp, sizeof(*pApp));
22,428,282✔
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,428,282✔
1081
}
1082

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

1088
  code = hbGetQueryBasicInfo(connKey, req);
25,891,412✔
1089
  if (code != TSDB_CODE_SUCCESS) {
25,891,412✔
1090
    tscWarn("hbGetQueryBasicInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
3,463,130✔
1091
    return code;
3,463,130✔
1092
  }
1093

1094
  if (hbParam->reqCnt == 0) {
22,428,282✔
1095
    code = catalogGetHandle(hbParam->clusterId, &pCatalog);
18,268,544✔
1096
    if (code != TSDB_CODE_SUCCESS) {
18,268,544✔
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,268,544✔
1102
    if (TSDB_CODE_SUCCESS != code) {
18,268,544✔
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,268,544✔
1108
      code = hbGetExpiredUserInfo(connKey, pCatalog, req);
17,280,022✔
1109
      if (TSDB_CODE_SUCCESS != code) {
17,280,022✔
1110
        tscWarn("hbGetExpiredUserInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1111
        return code;
×
1112
      }
1113
      if (clientHbMgr.appHbHash) {
17,280,022✔
1114
        code = taosHashPut(clientHbMgr.appHbHash, &hbParam->clusterId, sizeof(uint64_t), NULL, 0);
1,932,448✔
1115
        if (TSDB_CODE_SUCCESS != code) {
1,932,448✔
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,268,544✔
1125
      code = hbGetUserAuthInfo(connKey, hbParam, req);
834,620✔
1126
      if (TSDB_CODE_SUCCESS != code) {
834,620✔
UNCOV
1127
        tscWarn("hbGetUserAuthInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
UNCOV
1128
        return code;
×
1129
      }
1130
      atomic_store_8(&hbParam->pAppHbMgr->connHbFlag, 1);
834,620✔
1131
    }
1132

1133
    code = hbGetExpiredDBInfo(connKey, pCatalog, req);
18,268,544✔
1134
    if (TSDB_CODE_SUCCESS != code) {
18,268,544✔
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,268,544✔
1140
    if (TSDB_CODE_SUCCESS != code) {
18,268,544✔
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,268,544✔
1147
    if (TSDB_CODE_SUCCESS != code) {
18,268,544✔
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,268,544✔
1153
    if (TSDB_CODE_SUCCESS != code) {
18,268,544✔
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,159,738✔
1159
    if (TSDB_CODE_SUCCESS != code) {
4,159,738✔
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,428,282✔
1166

1167
  return TSDB_CODE_SUCCESS;
22,428,282✔
1168
}
1169

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

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

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

1191
  int64_t  maxIpWhiteVer = 0;
22,212,624✔
1192
  void    *pIter = NULL;
22,212,624✔
1193
  SHbParam param = {0};
22,212,624✔
1194
  while ((pIter = taosHashIterate(pAppHbMgr->activeInfo, pIter))) {
49,636,248✔
1195
    SClientHbReq *pOneReq = pIter;
27,423,624✔
1196
    SClientHbKey *connKey = &pOneReq->connKey;
27,423,624✔
1197
    STscObj      *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
27,423,624✔
1198

1199
    if (!pTscObj || atomic_load_8(&pTscObj->dropped) == 1) {
27,423,624✔
1200
      if (pTscObj) releaseTscObj(connKey->tscRid);
1,532,212✔
1201
      continue;
1,532,212✔
1202
    }
1203

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

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

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

1237
    int64_t ver = atomic_load_64(&pTscObj->whiteListInfo.ver);
25,891,412✔
1238
    maxIpWhiteVer = TMAX(maxIpWhiteVer, ver);
25,891,412✔
1239
    releaseTscObj(connKey->tscRid);
25,891,412✔
1240
  }
1241
  (*pBatchReq)->ipWhiteListVer = maxIpWhiteVer;
22,212,624✔
1242

1243
  return TSDB_CODE_SUCCESS;
22,212,624✔
1244
}
1245

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

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

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

1270
  taosHashClear(clientHbMgr.appSummary);
22,132,340✔
1271

1272
  for (int32_t i = 0; i < sz; ++i) {
47,307,675✔
1273
    SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
25,175,335✔
1274
    if (pAppHbMgr == NULL) continue;
25,175,335✔
1275

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

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

1291
  return TSDB_CODE_SUCCESS;
22,132,340✔
1292
}
1293

1294
static void *hbThreadFunc(void *param) {
1,342,140✔
1295
  setThreadName("hb");
1,342,140✔
1296
#ifdef WINDOWS
1297
  if (taosCheckCurrentInDll()) {
1298
    atexit(hbThreadFuncUnexpectedStopped);
1299
  }
1300
#endif
1301
  while (1) {
21,350,681✔
1302
    if (1 == clientHbMgr.threadStop) {
22,692,821✔
1303
      break;
549,273✔
1304
    }
1305

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

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

1327
    for (int i = 0; i < sz; i++) {
47,318,883✔
1328
      SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
25,175,335✔
1329
      if (pAppHbMgr == NULL) {
25,175,335✔
1330
        continue;
854,330✔
1331
      }
1332

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

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

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

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

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

1406
static int32_t hbCreateThread() {
1,342,140✔
1407
  int32_t      code = TSDB_CODE_SUCCESS;
1,342,140✔
1408
  TdThreadAttr thAttr;
1,221,534✔
1409
  TSC_ERR_JRET(taosThreadAttrInit(&thAttr));
1,342,140✔
1410
  TSC_ERR_JRET(taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE));
1,342,140✔
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,342,140✔
1416
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
1417
    TSC_ERR_RET(terrno);
×
1418
  }
1419
  (void)taosThreadAttrDestroy(&thAttr);
1,342,140✔
1420
_return:
1,342,140✔
1421

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

1427
  return code;
1,342,140✔
1428
}
1429

1430
static void hbStopThread() {
1,344,200✔
1431
  if (0 == atomic_load_8(&clientHbMgr.inited)) {
1,344,200✔
1432
    return;
2,060✔
1433
  }
1434
  if (atomic_val_compare_exchange_8(&clientHbMgr.threadStop, 0, 1)) {
1,342,140✔
1435
    tscDebug("hb thread already stopped");
×
1436
    return;
×
1437
  }
1438

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

1453
  tscDebug("hb thread stopped");
1,342,140✔
1454
}
1455

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

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

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

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

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

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

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

1501
void hbFreeAppHbMgr(SAppHbMgr *pTarget) {
1,412,488✔
1502
  void *pIter = taosHashIterate(pTarget->activeInfo, NULL);
1,412,488✔
1503
  while (pIter != NULL) {
1,659,999✔
1504
    SClientHbReq *pOneReq = pIter;
247,511✔
1505
    tFreeClientHbReq(pOneReq);
1506
    pIter = taosHashIterate(pTarget->activeInfo, pIter);
247,511✔
1507
  }
1508
  taosHashCleanup(pTarget->activeInfo);
1,412,488✔
1509
  pTarget->activeInfo = NULL;
1,412,488✔
1510

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

1515
void hbRemoveAppHbMrg(SAppHbMgr **pAppHbMgr) {
1,412,488✔
1516
  int32_t code = TSDB_CODE_SUCCESS;
1,412,488✔
1517
  code = taosThreadMutexLock(&clientHbMgr.lock);
1,412,488✔
1518
  if (TSDB_CODE_SUCCESS != code) {
1,412,488✔
1519
    tscError("failed to lock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1520
  }
1521
  int32_t mgrSize = taosArrayGetSize(clientHbMgr.appHbMgrs);
1,412,488✔
1522
  for (int32_t i = 0; i < mgrSize; ++i) {
1,412,488✔
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,412,488✔
1532
  if (TSDB_CODE_SUCCESS != code) {
1,412,488✔
1533
    tscError("failed to unlock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1534
  }
1535
}
1,412,488✔
1536

1537
void appHbMgrCleanup(void) {
1,342,140✔
1538
  int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
1,342,140✔
1539
  for (int i = 0; i < sz; i++) {
2,754,628✔
1540
    SAppHbMgr *pTarget = taosArrayGetP(clientHbMgr.appHbMgrs, i);
1,412,488✔
1541
    if (pTarget == NULL) continue;
1,412,488✔
1542
    hbFreeAppHbMgr(pTarget);
1,412,488✔
1543
  }
1544
}
1,342,140✔
1545

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

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

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

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

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

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

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

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

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

1593
  return 0;
1,342,140✔
1594
}
1595

1596
void hbMgrCleanUp() {
1,344,200✔
1597
  hbStopThread();
1,344,200✔
1598

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

1603
  int32_t code = taosThreadMutexLock(&clientHbMgr.lock);
1,342,140✔
1604
  if (TSDB_CODE_SUCCESS != code) {
1,342,140✔
1605
    tscError("failed to lock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1606
  }
1607
  appHbMgrCleanup();
1,342,140✔
1608
  taosArrayDestroy(clientHbMgr.appHbMgrs);
1,342,140✔
1609
  clientHbMgr.appHbMgrs = NULL;
1,342,140✔
1610
  code = taosThreadMutexUnlock(&clientHbMgr.lock);
1,342,140✔
1611
  if (TSDB_CODE_SUCCESS != code) {
1,342,140✔
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,238,158✔
1617
  // init hash in activeinfo
1618
  void *data = taosHashGet(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
3,238,158✔
1619
  if (data != NULL) {
3,238,158✔
1620
    return 0;
×
1621
  }
1622
  SClientHbReq hbReq = {0};
3,238,158✔
1623
  hbReq.connKey = connKey;
3,238,158✔
1624
  hbReq.clusterId = clusterId;
3,238,158✔
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,238,158✔
1628

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

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

1639
  switch (connType) {
3,324,182✔
1640
    case CONN_TYPE__QUERY: {
3,238,158✔
1641
      return hbRegisterConnImpl(pAppHbMgr, connKey, clusterId);
3,238,158✔
1642
    }
1643
    case CONN_TYPE__TMQ: {
86,024✔
1644
      return 0;
86,024✔
1645
    }
1646
    default:
×
1647
      return 0;
×
1648
  }
1649
}
1650

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