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

taosdata / TDengine / #4881

14 Dec 2025 03:48AM UTC coverage: 60.617% (+0.5%) from 60.092%
#4881

push

travis-ci

web-flow
test: update coverage workflow time (#33918)

156854 of 258761 relevant lines covered (60.62%)

75258957.81 hits per line

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

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

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

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

63
    TSC_ERR_JRET(catalogUpdateUserAuthInfo(pCatalog, rsp));
355,869✔
64
  }
65

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

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

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

77
static int32_t hbUpdateUserAuthInfo(SAppHbMgr *pAppHbMgr, SUserAuthBatchRsp *batchRsp) {
350,532✔
78
  int64_t clusterId = pAppHbMgr->pAppInstInfo->clusterId;
350,532✔
79
  for (int i = 0; i < TARRAY_SIZE(clientHbMgr.appHbMgrs); ++i) {
834,409✔
80
    SAppHbMgr *hbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
483,877✔
81
    if (!hbMgr || hbMgr->pAppInstInfo->clusterId != clusterId) {
483,877✔
82
      continue;
8,685✔
83
    }
84

85
    SClientHbReq    *pReq = NULL;
475,192✔
86
    SGetUserAuthRsp *pRsp = NULL;
475,192✔
87
    while ((pReq = taosHashIterate(hbMgr->activeInfo, pReq))) {
965,044✔
88
      STscObj *pTscObj = (STscObj *)acquireTscObj(pReq->connKey.tscRid);
534,923✔
89
      if (!pTscObj) {
534,923✔
90
        continue;
42,975✔
91
      }
92

93
      if (!pRsp) {
491,948✔
94
        for (int32_t j = 0; j < TARRAY_SIZE(batchRsp->pArray); ++j) {
458,655✔
95
          SGetUserAuthRsp *rsp = TARRAY_GET_ELEM(batchRsp->pArray, j);
413,584✔
96
          if (0 == strncmp(rsp->user, pTscObj->user, TSDB_USER_LEN)) {
413,584✔
97
            pRsp = rsp;
361,286✔
98
            break;
361,286✔
99
          }
100
        }
101
        if (!pRsp) {
406,357✔
102
          releaseTscObj(pReq->connKey.tscRid);
45,071✔
103
          taosHashCancelIterate(hbMgr->activeInfo, pReq);
45,071✔
104
          break;
45,071✔
105
        }
106
      }
107

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

121
      pTscObj->authVer = pRsp->version;
444,690✔
122

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

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

142
      if (pTscObj->whiteListInfo.fp) {
444,690✔
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;
444,690✔
157
        int64_t         oldVer = atomic_load_64(&whiteListInfo->ver);
444,690✔
158
        atomic_store_64(&whiteListInfo->ver, pRsp->whiteListVer);
444,690✔
159
        tscDebug("update whitelist version of user %s from %" PRId64 " to %" PRId64 ", conn:%" PRIi64, pRsp->user,
444,690✔
160
                 oldVer, atomic_load_64(&whiteListInfo->ver), pTscObj->id);
161
      }
162

163
      if (pTscObj->dateTimeWhiteListInfo.fp) {
444,690✔
164
        SWhiteListInfo *whiteListInfo = &pTscObj->dateTimeWhiteListInfo;
×
165
        int64_t         oldVer = atomic_load_64(&whiteListInfo->ver);
×
166
        if (oldVer != pRsp->timeWhiteListVer) {
×
167
          atomic_store_64(&whiteListInfo->ver, pRsp->timeWhiteListVer);
×
168
          if (whiteListInfo->fp) {
×
169
            (*whiteListInfo->fp)(whiteListInfo->param, &pRsp->timeWhiteListVer, TAOS_NOTIFY_DATETIME_WHITELIST_VER);
×
170
          }
171
          tscDebug("update date time whitelist version of user %s from %" PRId64 " to %" PRId64 ", conn:%" PRIi64, pRsp->user,
×
172
                   oldVer, atomic_load_64(&whiteListInfo->ver), pTscObj->id);
173
        }
174
      } else {
175
        SWhiteListInfo *whiteListInfo = &pTscObj->dateTimeWhiteListInfo;
444,690✔
176
        int64_t         oldVer = atomic_load_64(&whiteListInfo->ver);
444,690✔
177
        atomic_store_64(&whiteListInfo->ver, pRsp->timeWhiteListVer);
444,690✔
178
        tscDebug("update date time whitelist version of user %s from %" PRId64 " to %" PRId64 ", conn:%" PRIi64, pRsp->user,
444,690✔
179
                 oldVer, atomic_load_64(&whiteListInfo->ver), pTscObj->id);
180
      }
181
      releaseTscObj(pReq->connKey.tscRid);
444,690✔
182
    }
183
  }
184
  return 0;
350,532✔
185
}
186

187
static int32_t hbGenerateVgInfoFromRsp(SDBVgInfo **pInfo, SUseDbRsp *rsp) {
659,757✔
188
  int32_t    code = 0;
659,757✔
189
  SDBVgInfo *vgInfo = taosMemoryCalloc(1, sizeof(SDBVgInfo));
659,757✔
190
  if (NULL == vgInfo) {
659,757✔
191
    return terrno;
×
192
  }
193

194
  vgInfo->vgVersion = rsp->vgVersion;
659,757✔
195
  vgInfo->stateTs = rsp->stateTs;
659,757✔
196
  vgInfo->flags = rsp->flags;
659,757✔
197
  vgInfo->hashMethod = rsp->hashMethod;
659,757✔
198
  vgInfo->hashPrefix = rsp->hashPrefix;
659,757✔
199
  vgInfo->hashSuffix = rsp->hashSuffix;
659,757✔
200
  vgInfo->vgHash = taosHashInit(rsp->vgNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
659,757✔
201
  if (NULL == vgInfo->vgHash) {
659,757✔
202
    tscError("hash init[%d] failed", rsp->vgNum);
×
203
    code = terrno;
×
204
    goto _return;
×
205
  }
206

207
  for (int32_t j = 0; j < rsp->vgNum; ++j) {
2,384,318✔
208
    SVgroupInfo *pInfo = taosArrayGet(rsp->pVgroupInfos, j);
1,724,561✔
209
    if (taosHashPut(vgInfo->vgHash, &pInfo->vgId, sizeof(int32_t), pInfo, sizeof(SVgroupInfo)) != 0) {
1,724,561✔
210
      tscError("hash push failed, terrno:%d", terrno);
×
211
      code = terrno;
×
212
      goto _return;
×
213
    }
214
  }
215

216
_return:
659,757✔
217
  if (code) {
659,757✔
218
    taosHashCleanup(vgInfo->vgHash);
×
219
    taosMemoryFreeClear(vgInfo);
×
220
  }
221

222
  *pInfo = vgInfo;
659,757✔
223
  return code;
659,757✔
224
}
225

226
static int32_t hbProcessDBInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
2,015,479✔
227
  int32_t code = 0;
2,015,479✔
228

229
  SDbHbBatchRsp batchRsp = {0};
2,015,479✔
230
  if (tDeserializeSDbHbBatchRsp(value, valueLen, &batchRsp) != 0) {
2,015,479✔
231
    terrno = TSDB_CODE_INVALID_MSG;
×
232
    code = terrno;
×
233
    goto _return;
×
234
  }
235

236
  int32_t numOfBatchs = taosArrayGetSize(batchRsp.pArray);
2,015,479✔
237
  for (int32_t i = 0; i < numOfBatchs; ++i) {
2,723,729✔
238
    SDbHbRsp *rsp = taosArrayGet(batchRsp.pArray, i);
708,250✔
239
    if (NULL == rsp) {
708,250✔
240
      code = terrno;
×
241
      goto _return;
×
242
    }
243
    if (rsp->useDbRsp) {
708,250✔
244
      tscDebug("hb use db rsp, db:%s, vgVersion:%d, stateTs:%" PRId64 ", uid:%" PRIx64, rsp->useDbRsp->db,
529,408✔
245
               rsp->useDbRsp->vgVersion, rsp->useDbRsp->stateTs, rsp->useDbRsp->uid);
246

247
      if (rsp->useDbRsp->vgVersion < 0) {
529,408✔
248
        tscDebug("hb to remove db, db:%s", rsp->useDbRsp->db);
12,304✔
249
        code = catalogRemoveDB(pCatalog, rsp->useDbRsp->db, rsp->useDbRsp->uid);
12,304✔
250
      } else {
251
        SDBVgInfo *vgInfo = NULL;
517,104✔
252
        code = hbGenerateVgInfoFromRsp(&vgInfo, rsp->useDbRsp);
517,104✔
253
        if (TSDB_CODE_SUCCESS != code) {
517,104✔
254
          goto _return;
×
255
        }
256

257
        tscDebug("hb to update db vgInfo, db:%s", rsp->useDbRsp->db);
517,104✔
258

259
        TSC_ERR_JRET(catalogUpdateDBVgInfo(pCatalog, rsp->useDbRsp->db, rsp->useDbRsp->uid, vgInfo));
517,104✔
260

261
        if (IS_SYS_DBNAME(rsp->useDbRsp->db)) {
517,104✔
262
          code = hbGenerateVgInfoFromRsp(&vgInfo, rsp->useDbRsp);
142,653✔
263
          if (TSDB_CODE_SUCCESS != code) {
142,653✔
264
            goto _return;
×
265
          }
266

267
          TSC_ERR_JRET(catalogUpdateDBVgInfo(
142,653✔
268
              pCatalog, (rsp->useDbRsp->db[0] == 'i') ? TSDB_PERFORMANCE_SCHEMA_DB : TSDB_INFORMATION_SCHEMA_DB,
269
              rsp->useDbRsp->uid, vgInfo));
270
        }
271
      }
272
    }
273

274
    if (rsp->cfgRsp) {
708,250✔
275
      tscDebug("hb db cfg rsp, db:%s, cfgVersion:%d", rsp->cfgRsp->db, rsp->cfgRsp->cfgVersion);
136,216✔
276
      code = catalogUpdateDbCfg(pCatalog, rsp->cfgRsp->db, rsp->cfgRsp->dbId, rsp->cfgRsp);
136,216✔
277
      rsp->cfgRsp = NULL;
136,216✔
278
    }
279
    if (rsp->pTsmaRsp) {
708,250✔
280
      if (rsp->pTsmaRsp->pTsmas) {
335,101✔
281
        for (int32_t i = 0; i < rsp->pTsmaRsp->pTsmas->size; ++i) {
924✔
282
          STableTSMAInfo *pTsma = taosArrayGetP(rsp->pTsmaRsp->pTsmas, i);
462✔
283
          if (NULL == pTsma) {
462✔
284
            TSC_ERR_JRET(TSDB_CODE_OUT_OF_RANGE);
×
285
          }
286
          TSC_ERR_JRET(catalogAsyncUpdateTSMA(pCatalog, &pTsma, rsp->dbTsmaVersion));
462✔
287
        }
288
        taosArrayClear(rsp->pTsmaRsp->pTsmas);
462✔
289
      } else {
290
        TSC_ERR_JRET(catalogAsyncUpdateDbTsmaVersion(pCatalog, rsp->dbTsmaVersion, rsp->db, rsp->dbId));
334,639✔
291
      }
292
    }
293
  }
294

295
_return:
2,015,479✔
296

297
  tFreeSDbHbBatchRsp(&batchRsp);
2,015,479✔
298
  return code;
2,015,479✔
299
}
300

301
static int32_t hbProcessStbInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
1,307,296✔
302
  int32_t code = TSDB_CODE_SUCCESS;
1,307,296✔
303

304
  SSTbHbRsp hbRsp = {0};
1,307,296✔
305
  if (tDeserializeSSTbHbRsp(value, valueLen, &hbRsp) != 0) {
1,307,296✔
306
    terrno = TSDB_CODE_INVALID_MSG;
×
307
    return -1;
×
308
  }
309

310
  int32_t numOfMeta = taosArrayGetSize(hbRsp.pMetaRsp);
1,307,296✔
311
  for (int32_t i = 0; i < numOfMeta; ++i) {
1,337,117✔
312
    STableMetaRsp *rsp = taosArrayGet(hbRsp.pMetaRsp, i);
29,821✔
313
    if (NULL == rsp) {
29,821✔
314
      code = terrno;
×
315
      goto _return;
×
316
    }
317
    if (rsp->numOfColumns < 0) {
29,821✔
318
      tscDebug("hb to remove stb, db:%s, stb:%s", rsp->dbFName, rsp->stbName);
14,595✔
319
      TSC_ERR_JRET(catalogRemoveStbMeta(pCatalog, rsp->dbFName, rsp->dbId, rsp->stbName, rsp->suid));
14,595✔
320
    } else {
321
      tscDebug("hb to update stb, db:%s, stb:%s", rsp->dbFName, rsp->stbName);
15,226✔
322
      if (rsp->pSchemas[0].colId != PRIMARYKEY_TIMESTAMP_COL_ID) {
15,226✔
323
        tscError("invalid colId[%" PRIi16 "] for the first column in table meta rsp msg", rsp->pSchemas[0].colId);
×
324
        tFreeSSTbHbRsp(&hbRsp);
×
325
        return TSDB_CODE_TSC_INVALID_VALUE;
×
326
      }
327

328
      TSC_ERR_JRET(catalogAsyncUpdateTableMeta(pCatalog, rsp));
15,226✔
329
    }
330
  }
331

332
  int32_t numOfIndex = taosArrayGetSize(hbRsp.pIndexRsp);
1,307,296✔
333
  for (int32_t i = 0; i < numOfIndex; ++i) {
1,307,296✔
334
    STableIndexRsp *rsp = taosArrayGet(hbRsp.pIndexRsp, i);
×
335
    if (NULL == rsp) {
×
336
      code = terrno;
×
337
      goto _return;
×
338
    }
339
    TSC_ERR_JRET(catalogUpdateTableIndex(pCatalog, rsp));
×
340
  }
341

342
_return:
1,307,296✔
343
  taosArrayDestroy(hbRsp.pIndexRsp);
1,307,296✔
344
  hbRsp.pIndexRsp = NULL;
1,307,296✔
345

346
  tFreeSSTbHbRsp(&hbRsp);
1,307,296✔
347
  return code;
1,307,296✔
348
}
349

350
static int32_t hbProcessDynViewRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
856✔
351
  return catalogUpdateDynViewVer(pCatalog, (SDynViewVersion *)value);
856✔
352
}
353

354
static void hbFreeSViewMetaInRsp(void *p) {
×
355
  if (NULL == p || NULL == *(void **)p) {
×
356
    return;
×
357
  }
358
  SViewMetaRsp *pRsp = *(SViewMetaRsp **)p;
×
359
  tFreeSViewMetaRsp(pRsp);
×
360
  taosMemoryFreeClear(pRsp);
×
361
}
362

363
static int32_t hbProcessViewInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
856✔
364
  int32_t code = TSDB_CODE_SUCCESS;
856✔
365

366
  SViewHbRsp hbRsp = {0};
856✔
367
  if (tDeserializeSViewHbRsp(value, valueLen, &hbRsp) != 0) {
856✔
368
    taosArrayDestroyEx(hbRsp.pViewRsp, hbFreeSViewMetaInRsp);
×
369
    terrno = TSDB_CODE_INVALID_MSG;
×
370
    return -1;
×
371
  }
372

373
  int32_t numOfMeta = taosArrayGetSize(hbRsp.pViewRsp);
856✔
374
  for (int32_t i = 0; i < numOfMeta; ++i) {
856✔
375
    SViewMetaRsp *rsp = taosArrayGetP(hbRsp.pViewRsp, i);
×
376
    if (NULL == rsp) {
×
377
      code = terrno;
×
378
      goto _return;
×
379
    }
380
    if (rsp->numOfCols < 0) {
×
381
      tscDebug("hb to remove view, db:%s, view:%s", rsp->dbFName, rsp->name);
×
382
      code = catalogRemoveViewMeta(pCatalog, rsp->dbFName, rsp->dbId, rsp->name, rsp->viewId);
×
383
      tFreeSViewMetaRsp(rsp);
×
384
      taosMemoryFreeClear(rsp);
×
385
    } else {
386
      tscDebug("hb to update view, db:%s, view:%s", rsp->dbFName, rsp->name);
×
387
      code = catalogUpdateViewMeta(pCatalog, rsp);
×
388
    }
389
    TSC_ERR_JRET(code);
×
390
  }
391

392
_return:
856✔
393
  taosArrayDestroy(hbRsp.pViewRsp);
856✔
394
  return code;
856✔
395
}
396

397
static int32_t hbprocessTSMARsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
30,030✔
398
  int32_t code = 0;
30,030✔
399

400
  STSMAHbRsp hbRsp = {0};
30,030✔
401
  if (tDeserializeTSMAHbRsp(value, valueLen, &hbRsp)) {
30,030✔
402
    terrno = TSDB_CODE_INVALID_MSG;
×
403
    return -1;
×
404
  }
405

406
  int32_t numOfTsma = taosArrayGetSize(hbRsp.pTsmas);
30,030✔
407
  for (int32_t i = 0; i < numOfTsma; ++i) {
30,030✔
408
    STableTSMAInfo *pTsmaInfo = taosArrayGetP(hbRsp.pTsmas, i);
×
409

410
    if (!pTsmaInfo->pFuncs) {
×
411
      tscDebug("hb to remove tsma:%s.%s", pTsmaInfo->dbFName, pTsmaInfo->name);
×
412
      code = catalogRemoveTSMA(pCatalog, pTsmaInfo);
×
413
      tFreeAndClearTableTSMAInfo(pTsmaInfo);
×
414
    } else {
415
      tscDebug("hb to update tsma:%s.%s", pTsmaInfo->dbFName, pTsmaInfo->name);
×
416
      code = catalogUpdateTSMA(pCatalog, &pTsmaInfo);
×
417
      tFreeAndClearTableTSMAInfo(pTsmaInfo);
×
418
    }
419
    TSC_ERR_JRET(code);
×
420
  }
421

422
_return:
30,030✔
423
  taosArrayDestroy(hbRsp.pTsmas);
30,030✔
424
  return code;
30,030✔
425
}
426

427
static void hbProcessQueryRspKvs(int32_t kvNum, SArray *pKvs, struct SCatalog *pCatalog, SAppHbMgr *pAppHbMgr) {
3,130,970✔
428
  for (int32_t i = 0; i < kvNum; ++i) {
6,836,019✔
429
    SKv *kv = taosArrayGet(pKvs, i);
3,705,049✔
430
    if (NULL == kv) {
3,705,049✔
431
      tscError("invalid hb kv, idx:%d", i);
×
432
      continue;
×
433
    }
434
    switch (kv->key) {
3,705,049✔
435
      case HEARTBEAT_KEY_USER_AUTHINFO: {
350,532✔
436
        if (kv->valueLen <= 0 || NULL == kv->value) {
350,532✔
437
          tscError("invalid hb user auth info, len:%d, value:%p", kv->valueLen, kv->value);
×
438
          break;
×
439
        }
440
        if (TSDB_CODE_SUCCESS != hbProcessUserAuthInfoRsp(kv->value, kv->valueLen, pCatalog, pAppHbMgr)) {
350,532✔
441
          tscError("process user auth info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
442
          break;
×
443
        }
444
        break;
350,532✔
445
      }
446
      case HEARTBEAT_KEY_DBINFO: {
2,015,479✔
447
        if (kv->valueLen <= 0 || NULL == kv->value) {
2,015,479✔
448
          tscError("invalid hb db info, len:%d, value:%p", kv->valueLen, kv->value);
×
449
          break;
×
450
        }
451
        if (TSDB_CODE_SUCCESS != hbProcessDBInfoRsp(kv->value, kv->valueLen, pCatalog)) {
2,015,479✔
452
          tscError("process db info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
453
          break;
×
454
        }
455
        break;
2,015,479✔
456
      }
457
      case HEARTBEAT_KEY_STBINFO: {
1,307,296✔
458
        if (kv->valueLen <= 0 || NULL == kv->value) {
1,307,296✔
459
          tscError("invalid hb stb info, len:%d, value:%p", kv->valueLen, kv->value);
×
460
          break;
×
461
        }
462
        if (TSDB_CODE_SUCCESS != hbProcessStbInfoRsp(kv->value, kv->valueLen, pCatalog)) {
1,307,296✔
463
          tscError("process stb info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
464
          break;
×
465
        }
466
        break;
1,307,296✔
467
      }
468
#ifdef TD_ENTERPRISE
469
      case HEARTBEAT_KEY_DYN_VIEW: {
856✔
470
        if (kv->valueLen <= 0 || NULL == kv->value) {
856✔
471
          tscError("invalid dyn view info, len:%d, value:%p", kv->valueLen, kv->value);
×
472
          break;
×
473
        }
474
        if (TSDB_CODE_SUCCESS != hbProcessDynViewRsp(kv->value, kv->valueLen, pCatalog)) {
856✔
475
          tscError("Process dyn view response failed, len:%d, value:%p", kv->valueLen, kv->value);
×
476
          break;
×
477
        }
478
        break;
856✔
479
      }
480
      case HEARTBEAT_KEY_VIEWINFO: {
856✔
481
        if (kv->valueLen <= 0 || NULL == kv->value) {
856✔
482
          tscError("invalid view info, len:%d, value:%p", kv->valueLen, kv->value);
×
483
          break;
×
484
        }
485
        if (TSDB_CODE_SUCCESS != hbProcessViewInfoRsp(kv->value, kv->valueLen, pCatalog)) {
856✔
486
          tscError("Process view info response failed, len:%d, value:%p", kv->valueLen, kv->value);
×
487
          break;
×
488
        }
489
        break;
856✔
490
      }
491
#endif
492
      case HEARTBEAT_KEY_TSMA: {
30,030✔
493
        if (kv->valueLen <= 0 || !kv->value) {
30,030✔
494
          tscError("Invalid tsma info, len:%d, value:%p", kv->valueLen, kv->value);
×
495
        }
496
        if (TSDB_CODE_SUCCESS != hbprocessTSMARsp(kv->value, kv->valueLen, pCatalog)) {
30,030✔
497
          tscError("Process tsma info response failed, len:%d, value:%p", kv->valueLen, kv->value);
×
498
        }
499
        break;
30,030✔
500
      }
501
      default:
×
502
        tscError("invalid hb key type:%d", kv->key);
×
503
        break;
×
504
    }
505
  }
506
}
3,130,970✔
507

508
static int32_t hbQueryHbRspHandle(SAppHbMgr *pAppHbMgr, SClientHbRsp *pRsp) {
10,654,081✔
509
  SClientHbReq *pReq = taosHashAcquire(pAppHbMgr->activeInfo, &pRsp->connKey, sizeof(SClientHbKey));
10,654,081✔
510
  if (NULL == pReq) {
10,654,081✔
511
    tscWarn("pReq to get activeInfo, may be dropped, refId:%" PRIx64 ", type:%d", pRsp->connKey.tscRid,
3,969✔
512
            pRsp->connKey.connType);
513
    return TSDB_CODE_SUCCESS;
3,969✔
514
  }
515

516
  if (pRsp->query) {
10,650,112✔
517
    STscObj *pTscObj = (STscObj *)acquireTscObj(pRsp->connKey.tscRid);
9,669,903✔
518
    if (NULL == pTscObj) {
9,669,903✔
519
      tscDebug("tscObj rid %" PRIx64 " not exist", pRsp->connKey.tscRid);
846✔
520
    } else {
521
      if (pRsp->query->totalDnodes > 1) {
9,669,057✔
522
        SEpSet  originEpset = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
3,041,241✔
523
        if (!isEpsetEqual(&originEpset, &pRsp->query->epSet)) {
3,041,241✔
524
          SEpSet *pOrig = &originEpset;
8,016✔
525
          SEp    *pOrigEp = &pOrig->eps[pOrig->inUse];
8,016✔
526
          SEp    *pNewEp = &pRsp->query->epSet.eps[pRsp->query->epSet.inUse];
8,016✔
527
          tscDebug("mnode epset updated from %d/%d=>%s:%d to %d/%d=>%s:%d in hb", pOrig->inUse, pOrig->numOfEps,
8,016✔
528
                   pOrigEp->fqdn, pOrigEp->port, pRsp->query->epSet.inUse, pRsp->query->epSet.numOfEps, pNewEp->fqdn,
529
                   pNewEp->port);
530

531
          updateEpSet_s(&pTscObj->pAppInfo->mgmtEp, &pRsp->query->epSet);
8,016✔
532
        }
533
      }
534

535
      pTscObj->pAppInfo->totalDnodes = pRsp->query->totalDnodes;
9,669,057✔
536
      pTscObj->pAppInfo->onlineDnodes = pRsp->query->onlineDnodes;
9,669,057✔
537
      pTscObj->connId = pRsp->query->connId;
9,669,057✔
538
      tscTrace("connId:%u, hb rsp, dnodes %d/%d", pTscObj->connId, pTscObj->pAppInfo->onlineDnodes,
9,669,057✔
539
               pTscObj->pAppInfo->totalDnodes);
540

541
      if (pRsp->query->killRid) {
9,669,057✔
542
        tscDebug("QID:0x%" PRIx64 ", need to be killed now", pRsp->query->killRid);
×
543
        SRequestObj *pRequest = acquireRequest(pRsp->query->killRid);
×
544
        if (NULL == pRequest) {
×
545
          tscDebug("QID:0x%" PRIx64 ", not exist to kill", pRsp->query->killRid);
×
546
        } else {
547
          taos_stop_query((TAOS_RES *)pRequest);
×
548
          (void)releaseRequest(pRsp->query->killRid);
×
549
        }
550
      }
551

552
      if (pRsp->query->killConnection) {
9,669,057✔
553
        taos_close_internal(pTscObj);
×
554
      }
555

556
      if (pRsp->query->pQnodeList) {
9,669,057✔
557
        if (TSDB_CODE_SUCCESS != updateQnodeList(pTscObj->pAppInfo, pRsp->query->pQnodeList)) {
8,395✔
558
          tscWarn("update qnode list failed");
×
559
        }
560
      }
561

562
      releaseTscObj(pRsp->connKey.tscRid);
9,669,057✔
563
    }
564
  }
565

566
  int32_t kvNum = pRsp->info ? taosArrayGetSize(pRsp->info) : 0;
10,650,112✔
567

568
  tscDebug("hb got %d rsp kv", kvNum);
10,650,112✔
569

570
  if (kvNum > 0) {
10,650,112✔
571
    struct SCatalog *pCatalog = NULL;
3,130,970✔
572
    int32_t          code = catalogGetHandle(pReq->clusterId, &pCatalog);
3,130,970✔
573
    if (code != TSDB_CODE_SUCCESS) {
3,130,970✔
574
      tscWarn("catalogGetHandle failed, clusterId:0x%" PRIx64 ", error:%s", pReq->clusterId, tstrerror(code));
×
575
    } else {
576
      hbProcessQueryRspKvs(kvNum, pRsp->info, pCatalog, pAppHbMgr);
3,130,970✔
577
    }
578
  }
579

580
  taosHashRelease(pAppHbMgr->activeInfo, pReq);
10,650,112✔
581

582
  return TSDB_CODE_SUCCESS;
10,650,112✔
583
}
584

585
static int32_t hbAsyncCallBack(void *param, SDataBuf *pMsg, int32_t code) {
8,602,509✔
586
  if (0 == atomic_load_8(&clientHbMgr.inited)) {
8,602,509✔
587
    goto _return;
×
588
  }
589

590
  static int32_t    emptyRspNum = 0;
591
  int32_t           idx = *(int32_t *)param;
8,602,509✔
592
  SClientHbBatchRsp pRsp = {0};
8,602,509✔
593
  if (TSDB_CODE_SUCCESS == code) {
8,602,509✔
594
    code = tDeserializeSClientHbBatchRsp(pMsg->pData, pMsg->len, &pRsp);
8,502,698✔
595
    if (TSDB_CODE_SUCCESS != code) {
8,502,238✔
596
      tscError("deserialize hb rsp failed");
100✔
597
    }
598
    int32_t now = taosGetTimestampSec();
8,502,238✔
599
    int32_t delta = abs(now - pRsp.svrTimestamp);
8,502,178✔
600
    if (delta > tsTimestampDeltaLimit) {
8,502,178✔
601
      code = TSDB_CODE_TIME_UNSYNCED;
100✔
602
      tscError("time diff:%ds is too big", delta);
100✔
603
    }
604
  }
605

606
  int32_t rspNum = taosArrayGetSize(pRsp.rsps);
8,601,989✔
607

608
  (void)taosThreadMutexLock(&clientHbMgr.lock);
8,602,432✔
609

610
  SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, idx);
8,602,547✔
611
  if (pAppHbMgr == NULL) {
8,602,547✔
612
    (void)taosThreadMutexUnlock(&clientHbMgr.lock);
×
613
    tscError("appHbMgr not exist, idx:%d", idx);
×
614
    taosMemoryFree(pMsg->pData);
×
615
    taosMemoryFree(pMsg->pEpSet);
×
616
    tFreeClientHbBatchRsp(&pRsp);
617
    return TSDB_CODE_OUT_OF_RANGE;
×
618
  }
619

620
  SAppInstInfo *pInst = pAppHbMgr->pAppInstInfo;
8,602,547✔
621

622
  if (code != 0) {
8,602,547✔
623
    pInst->onlineDnodes = pInst->totalDnodes ? 0 : -1;
99,930✔
624
    tscDebug("hb rsp error %s, update server status %d/%d", tstrerror(code), pInst->onlineDnodes, pInst->totalDnodes);
99,930✔
625
    (void)taosThreadMutexUnlock(&clientHbMgr.lock);
99,930✔
626
    taosMemoryFree(pMsg->pData);
99,930✔
627
    taosMemoryFree(pMsg->pEpSet);
99,930✔
628
    tFreeClientHbBatchRsp(&pRsp);
629
    return code;
99,930✔
630
  }
631

632
  pInst->serverCfg.monitorParas = pRsp.monitorParas;
8,502,617✔
633
  pInst->serverCfg.enableAuditDelete = pRsp.enableAuditDelete;
8,502,617✔
634
  pInst->serverCfg.enableStrongPass = pRsp.enableStrongPass;
8,502,617✔
635
  tsEnableStrongPassword = pInst->serverCfg.enableStrongPass;
8,502,617✔
636
  tscDebug("monitor paras from hb, clusterId:0x%" PRIx64 ", threshold:%d scope:%d", pInst->clusterId,
8,502,617✔
637
           pRsp.monitorParas.tsSlowLogThreshold, pRsp.monitorParas.tsSlowLogScope);
638

639
  if (rspNum) {
8,502,617✔
640
    tscDebug("hb got %d rsp, %d empty rsp received before", rspNum,
8,189,757✔
641
             atomic_val_compare_exchange_32(&emptyRspNum, emptyRspNum, 0));
642
  } else {
643
    (void)atomic_add_fetch_32(&emptyRspNum, 1);
312,860✔
644
  }
645

646
  for (int32_t i = 0; i < rspNum; ++i) {
19,156,698✔
647
    SClientHbRsp *rsp = taosArrayGet(pRsp.rsps, i);
10,654,081✔
648
    code = (*clientHbMgr.rspHandle[rsp->connKey.connType])(pAppHbMgr, rsp);
10,654,081✔
649
    if (code) {
10,654,081✔
650
      break;
×
651
    }
652
  }
653

654
  (void)taosThreadMutexUnlock(&clientHbMgr.lock);
8,502,617✔
655

656
  tFreeClientHbBatchRsp(&pRsp);
657

658
_return:
8,502,617✔
659
  taosMemoryFree(pMsg->pData);
8,502,617✔
660
  taosMemoryFree(pMsg->pEpSet);
8,502,617✔
661
  return code;
8,502,617✔
662
}
663

664
int32_t hbBuildQueryDesc(SQueryHbReqBasic *hbBasic, STscObj *pObj) {
9,969,299✔
665
  int64_t    now = taosGetTimestampUs();
9,969,299✔
666
  SQueryDesc desc = {0};
9,969,299✔
667
  int32_t    code = 0;
9,969,299✔
668

669
  void *pIter = taosHashIterate(pObj->pRequests, NULL);
9,969,299✔
670
  while (pIter != NULL) {
20,091,105✔
671
    int64_t     *rid = pIter;
10,121,806✔
672
    SRequestObj *pRequest = acquireRequest(*rid);
10,121,806✔
673
    if (NULL == pRequest) {
10,121,806✔
674
      pIter = taosHashIterate(pObj->pRequests, pIter);
6,510✔
675
      continue;
6,510✔
676
    }
677

678
    if (pRequest->killed || 0 == pRequest->body.queryJob) {
10,115,296✔
679
      (void)releaseRequest(*rid);
4,857,330✔
680
      pIter = taosHashIterate(pObj->pRequests, pIter);
4,857,330✔
681
      continue;
4,857,330✔
682
    }
683

684
    tstrncpy(desc.sql, pRequest->sqlstr, sizeof(desc.sql));
5,257,966✔
685
    desc.stime = pRequest->metric.start / 1000;
5,257,966✔
686
    desc.queryId = pRequest->requestId;
5,257,966✔
687
    desc.useconds = now - pRequest->metric.start;
5,257,966✔
688
    desc.reqRid = pRequest->self;
5,257,966✔
689
    desc.stableQuery = pRequest->stableQuery;
5,257,966✔
690
    desc.isSubQuery = pRequest->isSubReq;
5,257,966✔
691
    code = taosGetFqdn(desc.fqdn);
5,257,966✔
692
    if (TSDB_CODE_SUCCESS != code) {
5,257,966✔
693
      (void)releaseRequest(*rid);
×
694
      tscError("get fqdn failed");
×
695
      return TSDB_CODE_FAILED;
×
696
    }
697
    desc.subPlanNum = pRequest->body.subplanNum;
5,257,966✔
698

699
    if (desc.subPlanNum) {
5,257,966✔
700
      desc.subDesc = taosArrayInit(desc.subPlanNum, sizeof(SQuerySubDesc));
5,257,966✔
701
      if (NULL == desc.subDesc) {
5,257,966✔
702
        (void)releaseRequest(*rid);
×
703
        return terrno;
×
704
      }
705

706
      code = schedulerGetTasksStatus(pRequest->body.queryJob, desc.subDesc);
5,257,966✔
707
      if (code) {
5,257,966✔
708
        taosArrayDestroy(desc.subDesc);
999,296✔
709
        desc.subDesc = NULL;
999,296✔
710
      }
711
      desc.subPlanNum = taosArrayGetSize(desc.subDesc);
5,257,966✔
712
    } else {
713
      desc.subDesc = NULL;
×
714
    }
715

716
    (void)releaseRequest(*rid);
5,257,966✔
717
    if (NULL == taosArrayPush(hbBasic->queryDesc, &desc)) {
10,515,932✔
718
      taosArrayDestroy(desc.subDesc);
×
719
      return terrno;
×
720
    }
721

722
    pIter = taosHashIterate(pObj->pRequests, pIter);
5,257,966✔
723
  }
724

725
  return code;
9,969,299✔
726
}
727

728
int32_t hbGetQueryBasicInfo(SClientHbKey *connKey, SClientHbReq *req) {
10,759,692✔
729
  STscObj *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
10,759,692✔
730
  if (NULL == pTscObj) {
10,759,692✔
731
    tscWarn("tscObj rid 0x%" PRIx64 " not exist", connKey->tscRid);
133✔
732
    return terrno;
133✔
733
  }
734

735
  SQueryHbReqBasic *hbBasic = (SQueryHbReqBasic *)taosMemoryCalloc(1, sizeof(SQueryHbReqBasic));
10,759,559✔
736
  if (NULL == hbBasic) {
10,759,559✔
737
    tscError("calloc %d failed", (int32_t)sizeof(SQueryHbReqBasic));
×
738
    releaseTscObj(connKey->tscRid);
×
739
    return terrno;
×
740
  }
741

742
  hbBasic->connId = pTscObj->connId;
10,759,559✔
743

744
  int32_t numOfQueries = pTscObj->pRequests ? taosHashGetSize(pTscObj->pRequests) : 0;
10,759,559✔
745
  if (numOfQueries <= 0) {
10,759,559✔
746
    req->query = hbBasic;
790,260✔
747
    releaseTscObj(connKey->tscRid);
790,260✔
748
    tscDebug("no queries on connection");
790,260✔
749
    return TSDB_CODE_SUCCESS;
790,260✔
750
  }
751

752
  hbBasic->queryDesc = taosArrayInit(numOfQueries, sizeof(SQueryDesc));
9,969,299✔
753
  if (NULL == hbBasic->queryDesc) {
9,969,299✔
754
    tscWarn("taosArrayInit %d queryDesc failed", numOfQueries);
×
755
    releaseTscObj(connKey->tscRid);
×
756
    taosMemoryFree(hbBasic);
×
757
    return terrno;
×
758
  }
759

760
  int32_t code = hbBuildQueryDesc(hbBasic, pTscObj);
9,969,299✔
761
  if (code) {
9,969,299✔
762
    releaseTscObj(connKey->tscRid);
996,808✔
763
    if (hbBasic->queryDesc) {
996,808✔
764
      taosArrayDestroyEx(hbBasic->queryDesc, tFreeClientHbQueryDesc);
996,808✔
765
    }
766
    taosMemoryFree(hbBasic);
996,808✔
767
    return code;
996,808✔
768
  }
769

770
  req->query = hbBasic;
8,972,491✔
771
  releaseTscObj(connKey->tscRid);
8,972,491✔
772

773
  return TSDB_CODE_SUCCESS;
8,972,491✔
774
}
775

776
static int32_t hbGetUserAuthInfo(SClientHbKey *connKey, SHbParam *param, SClientHbReq *req) {
339,680✔
777
  STscObj *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
339,680✔
778
  if (!pTscObj) {
339,680✔
779
    tscWarn("tscObj rid 0x%" PRIx64 " not exist", connKey->tscRid);
344✔
780
    return terrno;
344✔
781
  }
782

783
  int32_t code = 0;
339,336✔
784

785
  SKv  kv = {.key = HEARTBEAT_KEY_USER_AUTHINFO};
339,336✔
786
  SKv *pKv = NULL;
339,336✔
787
  if ((pKv = taosHashGet(req->info, &kv.key, sizeof(kv.key)))) {
339,336✔
788
    int32_t           userNum = pKv->valueLen / sizeof(SUserAuthVersion);
129,639✔
789
    SUserAuthVersion *userAuths = (SUserAuthVersion *)pKv->value;
129,639✔
790
    for (int32_t i = 0; i < userNum; ++i) {
131,151✔
791
      SUserAuthVersion *pUserAuth = userAuths + i;
130,965✔
792
      // both key and user exist, update version
793
      if (strncmp(pUserAuth->user, pTscObj->user, TSDB_USER_LEN) == 0) {
130,965✔
794
        pUserAuth->version = htonl(-1);  // force get userAuthInfo
129,453✔
795
        goto _return;
129,453✔
796
      }
797
    }
798
    // key exists, user not exist, append user
799
    SUserAuthVersion *qUserAuth =
372✔
800
        (SUserAuthVersion *)taosMemoryRealloc(pKv->value, (userNum + 1) * sizeof(SUserAuthVersion));
186✔
801
    if (qUserAuth) {
186✔
802
      tstrncpy((qUserAuth + userNum)->user, pTscObj->user, TSDB_USER_LEN);
186✔
803
      (qUserAuth + userNum)->version = htonl(-1);  // force get userAuthInfo
186✔
804
      pKv->value = qUserAuth;
186✔
805
      pKv->valueLen += sizeof(SUserAuthVersion);
186✔
806
    } else {
807
      code = terrno;
×
808
    }
809
    goto _return;
186✔
810
  }
811

812
  // key/user not exist, add user
813
  SUserAuthVersion *user = taosMemoryMalloc(sizeof(SUserAuthVersion));
209,697✔
814
  if (!user) {
209,697✔
815
    code = terrno;
×
816
    goto _return;
×
817
  }
818
  tstrncpy(user->user, pTscObj->user, TSDB_USER_LEN);
209,697✔
819
  user->version = htonl(-1);  // force get userAuthInfo
209,697✔
820
  kv.valueLen = sizeof(SUserAuthVersion);
209,697✔
821
  kv.value = user;
209,697✔
822

823
  tscDebug("hb got user auth info, valueLen:%d, user:%s, authVer:%d, tscRid:%" PRIi64, kv.valueLen, user->user,
209,697✔
824
           pTscObj->authVer, connKey->tscRid);
825

826
  if (!req->info) {
209,697✔
827
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
209,697✔
828
    if (NULL == req->info) {
209,697✔
829
      code = terrno;
×
830
      goto _return;
×
831
    }
832
  }
833

834
  if (taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)) != 0) {
209,697✔
835
    taosMemoryFree(user);
×
836
    code = terrno ? terrno : TSDB_CODE_APP_ERROR;
×
837
    goto _return;
×
838
  }
839

840
_return:
338,409✔
841
  releaseTscObj(connKey->tscRid);
339,336✔
842
  if (code) {
339,336✔
843
    tscError("hb got user auth info failed since %s", tstrerror(code));
×
844
  }
845

846
  return code;
339,336✔
847
}
848

849
int32_t hbGetExpiredUserInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
7,085,030✔
850
  SUserAuthVersion *users = NULL;
7,085,030✔
851
  uint32_t          userNum = 0;
7,085,030✔
852
  int32_t           code = 0;
7,085,030✔
853

854
  code = catalogGetExpiredUsers(pCatalog, &users, &userNum);
7,085,030✔
855
  if (TSDB_CODE_SUCCESS != code) {
7,085,030✔
856
    return code;
×
857
  }
858

859
  if (userNum <= 0) {
7,085,030✔
860
    taosMemoryFree(users);
307,591✔
861
    return TSDB_CODE_SUCCESS;
307,591✔
862
  }
863

864
  for (int32_t i = 0; i < userNum; ++i) {
13,703,601✔
865
    SUserAuthVersion *user = &users[i];
6,926,162✔
866
    user->version = htonl(user->version);
6,926,162✔
867
  }
868

869
  SKv kv = {
6,777,439✔
870
      .key = HEARTBEAT_KEY_USER_AUTHINFO,
871
      .valueLen = sizeof(SUserAuthVersion) * userNum,
6,777,439✔
872
      .value = users,
873
  };
874

875
  tscDebug("hb got %d expired users, valueLen:%d", userNum, kv.valueLen);
6,777,439✔
876

877
  if (NULL == req->info) {
6,777,439✔
878
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
6,777,439✔
879
    if (NULL == req->info) {
6,777,439✔
880
      taosMemoryFree(users);
×
881
      return terrno;
×
882
    }
883
  }
884

885
  code = taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
6,777,439✔
886
  if (TSDB_CODE_SUCCESS != code) {
6,777,439✔
887
    taosMemoryFree(users);
×
888
    return code;
×
889
  }
890

891
  return TSDB_CODE_SUCCESS;
6,777,439✔
892
}
893

894
int32_t hbGetExpiredDBInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
7,324,328✔
895
  SDbCacheInfo *dbs = NULL;
7,324,328✔
896
  uint32_t      dbNum = 0;
7,324,328✔
897
  int32_t       code = 0;
7,324,328✔
898

899
  code = catalogGetExpiredDBs(pCatalog, &dbs, &dbNum);
7,324,328✔
900
  if (TSDB_CODE_SUCCESS != code) {
7,324,328✔
901
    return code;
×
902
  }
903

904
  if (dbNum <= 0) {
7,324,328✔
905
    taosMemoryFree(dbs);
5,297,912✔
906
    return TSDB_CODE_SUCCESS;
5,297,912✔
907
  }
908

909
  for (int32_t i = 0; i < dbNum; ++i) {
4,473,804✔
910
    SDbCacheInfo *db = &dbs[i];
2,447,388✔
911
    tscDebug("the %dth expired db:%s, dbId:%" PRId64
2,447,388✔
912
             ", vgVersion:%d, cfgVersion:%d, numOfTable:%d, startTs:%" PRId64,
913
             i, db->dbFName, db->dbId, db->vgVersion, db->cfgVersion, db->numOfTable, db->stateTs);
914

915
    db->dbId = htobe64(db->dbId);
2,447,388✔
916
    db->vgVersion = htonl(db->vgVersion);
2,447,388✔
917
    db->cfgVersion = htonl(db->cfgVersion);
2,447,388✔
918
    db->numOfTable = htonl(db->numOfTable);
2,447,388✔
919
    db->stateTs = htobe64(db->stateTs);
2,447,388✔
920
    db->tsmaVersion = htonl(db->tsmaVersion);
2,447,388✔
921
  }
922

923
  SKv kv = {
2,026,416✔
924
      .key = HEARTBEAT_KEY_DBINFO,
925
      .valueLen = sizeof(SDbCacheInfo) * dbNum,
2,026,416✔
926
      .value = dbs,
927
  };
928

929
  tscDebug("hb got %d expired db, valueLen:%d", dbNum, kv.valueLen);
2,026,416✔
930

931
  if (NULL == req->info) {
2,026,416✔
932
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
9,951✔
933
    if (NULL == req->info) {
9,951✔
934
      taosMemoryFree(dbs);
×
935
      return terrno;
×
936
    }
937
  }
938

939
  code = taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
2,026,416✔
940
  if (TSDB_CODE_SUCCESS != code) {
2,026,416✔
941
    taosMemoryFree(dbs);
×
942
    return code;
×
943
  }
944

945
  return TSDB_CODE_SUCCESS;
2,026,416✔
946
}
947

948
int32_t hbGetExpiredStbInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
7,324,328✔
949
  SSTableVersion *stbs = NULL;
7,324,328✔
950
  uint32_t        stbNum = 0;
7,324,328✔
951
  int32_t         code = 0;
7,324,328✔
952

953
  code = catalogGetExpiredSTables(pCatalog, &stbs, &stbNum);
7,324,328✔
954
  if (TSDB_CODE_SUCCESS != code) {
7,324,328✔
955
    return code;
×
956
  }
957

958
  if (stbNum <= 0) {
7,324,328✔
959
    taosMemoryFree(stbs);
6,009,629✔
960
    return TSDB_CODE_SUCCESS;
6,009,629✔
961
  }
962

963
  for (int32_t i = 0; i < stbNum; ++i) {
2,909,697✔
964
    SSTableVersion *stb = &stbs[i];
1,594,998✔
965
    stb->suid = htobe64(stb->suid);
1,594,998✔
966
    stb->sversion = htonl(stb->sversion);
1,594,998✔
967
    stb->tversion = htonl(stb->tversion);
1,594,998✔
968
    stb->smaVer = htonl(stb->smaVer);
1,594,998✔
969
  }
970

971
  SKv kv = {
1,314,699✔
972
      .key = HEARTBEAT_KEY_STBINFO,
973
      .valueLen = sizeof(SSTableVersion) * stbNum,
1,314,699✔
974
      .value = stbs,
975
  };
976

977
  tscDebug("hb got %d expired stb, valueLen:%d", stbNum, kv.valueLen);
1,314,699✔
978

979
  if (NULL == req->info) {
1,314,699✔
980
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
306✔
981
    if (NULL == req->info) {
306✔
982
      taosMemoryFree(stbs);
×
983
      return terrno;
×
984
    }
985
  }
986

987
  code = taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
1,314,699✔
988
  if (TSDB_CODE_SUCCESS != code) {
1,314,699✔
989
    taosMemoryFree(stbs);
×
990
    return code;
×
991
  }
992

993
  return TSDB_CODE_SUCCESS;
1,314,699✔
994
}
995

996
int32_t hbGetExpiredViewInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
7,324,328✔
997
  SViewVersion    *views = NULL;
7,324,328✔
998
  uint32_t         viewNum = 0;
7,324,328✔
999
  int32_t          code = 0;
7,324,328✔
1000
  SDynViewVersion *pDynViewVer = NULL;
7,324,328✔
1001

1002
  TSC_ERR_JRET(catalogGetExpiredViews(pCatalog, &views, &viewNum, &pDynViewVer));
7,324,328✔
1003

1004
  if (viewNum <= 0) {
7,324,328✔
1005
    taosMemoryFree(views);
7,323,472✔
1006
    taosMemoryFree(pDynViewVer);
7,323,472✔
1007
    return TSDB_CODE_SUCCESS;
7,323,472✔
1008
  }
1009

1010
  for (int32_t i = 0; i < viewNum; ++i) {
1,937✔
1011
    SViewVersion *view = &views[i];
1,081✔
1012
    view->dbId = htobe64(view->dbId);
1,081✔
1013
    view->viewId = htobe64(view->viewId);
1,081✔
1014
    view->version = htonl(view->version);
1,081✔
1015
  }
1016

1017
  tscDebug("hb got %u expired view, valueLen:%lu", viewNum, sizeof(SViewVersion) * viewNum);
856✔
1018

1019
  if (NULL == req->info) {
856✔
1020
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
×
1021
    if (NULL == req->info) {
×
1022
      TSC_ERR_JRET(terrno);
×
1023
    }
1024
  }
1025

1026
  SKv kv = {
856✔
1027
      .key = HEARTBEAT_KEY_DYN_VIEW,
1028
      .valueLen = sizeof(SDynViewVersion),
1029
      .value = pDynViewVer,
1030
  };
1031

1032
  TSC_ERR_JRET(taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)));
856✔
1033

1034
  kv.key = HEARTBEAT_KEY_VIEWINFO;
856✔
1035
  kv.valueLen = sizeof(SViewVersion) * viewNum;
856✔
1036
  kv.value = views;
856✔
1037

1038
  TSC_ERR_JRET(taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)));
856✔
1039
  return TSDB_CODE_SUCCESS;
856✔
1040
_return:
×
1041
  taosMemoryFree(views);
×
1042
  taosMemoryFree(pDynViewVer);
×
1043
  return code;
×
1044
}
1045

1046
int32_t hbGetExpiredTSMAInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *pReq) {
7,324,328✔
1047
  int32_t       code = 0;
7,324,328✔
1048
  uint32_t      tsmaNum = 0;
7,324,328✔
1049
  STSMAVersion *tsmas = NULL;
7,324,328✔
1050

1051
  code = catalogGetExpiredTsmas(pCatalog, &tsmas, &tsmaNum);
7,324,328✔
1052
  if (code) {
7,324,328✔
1053
    taosMemoryFree(tsmas);
×
1054
    return code;
×
1055
  }
1056

1057
  if (tsmaNum <= 0) {
7,324,328✔
1058
    taosMemoryFree(tsmas);
7,294,298✔
1059
    return TSDB_CODE_SUCCESS;
7,294,298✔
1060
  }
1061

1062
  for (int32_t i = 0; i < tsmaNum; ++i) {
60,060✔
1063
    STSMAVersion *tsma = &tsmas[i];
30,030✔
1064
    tsma->dbId = htobe64(tsma->dbId);
30,030✔
1065
    tsma->tsmaId = htobe64(tsma->tsmaId);
30,030✔
1066
    tsma->version = htonl(tsma->version);
30,030✔
1067
  }
1068

1069
  tscDebug("hb got %d expred tsmas, valueLen:%lu", tsmaNum, sizeof(STSMAVersion) * tsmaNum);
30,030✔
1070

1071
  if (!pReq->info) {
30,030✔
1072
    pReq->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
×
1073
    if (!pReq->info) {
×
1074
      taosMemoryFree(tsmas);
×
1075
      return terrno;
×
1076
    }
1077
  }
1078

1079
  SKv kv = {.key = HEARTBEAT_KEY_TSMA, .valueLen = sizeof(STSMAVersion) * tsmaNum, .value = tsmas};
30,030✔
1080
  code = taosHashPut(pReq->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
30,030✔
1081
  if (TSDB_CODE_SUCCESS != code) {
30,030✔
1082
    taosMemoryFree(tsmas);
×
1083
    return code;
×
1084
  }
1085
  return TSDB_CODE_SUCCESS;
30,030✔
1086
}
1087

1088
int32_t hbGetAppInfo(int64_t clusterId, SClientHbReq *req) {
9,762,751✔
1089
  SAppHbReq *pApp = taosHashGet(clientHbMgr.appSummary, &clusterId, sizeof(clusterId));
9,762,751✔
1090
  if (NULL != pApp) {
9,762,751✔
1091
    (void)memcpy(&req->app, pApp, sizeof(*pApp));
9,762,751✔
1092
  } else {
1093
    (void)memset(&req->app.summary, 0, sizeof(req->app.summary));
×
1094
    req->app.pid = taosGetPId();
×
1095
    req->app.appId = clientHbMgr.appId;
×
1096
    TSC_ERR_RET(taosGetAppName(req->app.name, NULL));
×
1097
  }
1098

1099
  return TSDB_CODE_SUCCESS;
9,762,751✔
1100
}
1101

1102
int32_t hbQueryHbReqHandle(SClientHbKey *connKey, void *param, SClientHbReq *req) {
10,759,692✔
1103
  int32_t   code = 0;
10,759,692✔
1104
  SHbParam *hbParam = (SHbParam *)param;
10,759,692✔
1105
  SCatalog *pCatalog = NULL;
10,759,692✔
1106

1107
  code = hbGetQueryBasicInfo(connKey, req);
10,759,692✔
1108
  if (code != TSDB_CODE_SUCCESS) {
10,759,692✔
1109
    tscWarn("hbGetQueryBasicInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
996,941✔
1110
    return code;
996,941✔
1111
  }
1112

1113
  if (hbParam->reqCnt == 0) {
9,762,751✔
1114
    code = catalogGetHandle(hbParam->clusterId, &pCatalog);
7,324,672✔
1115
    if (code != TSDB_CODE_SUCCESS) {
7,324,672✔
1116
      tscWarn("catalogGetHandle failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1117
      return code;
×
1118
    }
1119

1120
    code = hbGetAppInfo(hbParam->clusterId, req);
7,324,672✔
1121
    if (TSDB_CODE_SUCCESS != code) {
7,324,672✔
1122
      tscWarn("getAppInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1123
      return code;
×
1124
    }
1125

1126
    if (!taosHashGet(clientHbMgr.appHbHash, &hbParam->clusterId, sizeof(hbParam->clusterId))) {
7,324,672✔
1127
      code = hbGetExpiredUserInfo(connKey, pCatalog, req);
7,085,030✔
1128
      if (TSDB_CODE_SUCCESS != code) {
7,085,030✔
1129
        tscWarn("hbGetExpiredUserInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1130
        return code;
×
1131
      }
1132
      if (clientHbMgr.appHbHash) {
7,085,030✔
1133
        code = taosHashPut(clientHbMgr.appHbHash, &hbParam->clusterId, sizeof(uint64_t), NULL, 0);
480,841✔
1134
        if (TSDB_CODE_SUCCESS != code) {
480,841✔
1135
          tscWarn("hbQueryHbReqHandle put clusterId failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId,
×
1136
                  tstrerror(code));
1137
          return code;
×
1138
        }
1139
      }
1140
    }
1141

1142
    // invoke after hbGetExpiredUserInfo
1143
    if (2 != atomic_load_8(&hbParam->pAppHbMgr->connHbFlag)) {
7,324,672✔
1144
      code = hbGetUserAuthInfo(connKey, hbParam, req);
339,680✔
1145
      if (TSDB_CODE_SUCCESS != code) {
339,680✔
1146
        tscWarn("hbGetUserAuthInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
344✔
1147
        return code;
344✔
1148
      }
1149
      atomic_store_8(&hbParam->pAppHbMgr->connHbFlag, 1);
339,336✔
1150
    }
1151

1152
    code = hbGetExpiredDBInfo(connKey, pCatalog, req);
7,324,328✔
1153
    if (TSDB_CODE_SUCCESS != code) {
7,324,328✔
1154
      tscWarn("hbGetExpiredDBInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1155
      return code;
×
1156
    }
1157

1158
    code = hbGetExpiredStbInfo(connKey, pCatalog, req);
7,324,328✔
1159
    if (TSDB_CODE_SUCCESS != code) {
7,324,328✔
1160
      tscWarn("hbGetExpiredStbInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1161
      return code;
×
1162
    }
1163

1164
#ifdef TD_ENTERPRISE
1165
    code = hbGetExpiredViewInfo(connKey, pCatalog, req);
7,324,328✔
1166
    if (TSDB_CODE_SUCCESS != code) {
7,324,328✔
1167
      tscWarn("hbGetExpiredViewInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1168
      return code;
×
1169
    }
1170
#endif
1171
    code = hbGetExpiredTSMAInfo(connKey, pCatalog, req);
7,324,328✔
1172
    if (TSDB_CODE_SUCCESS != code) {
7,324,328✔
1173
      tscWarn("hbGetExpiredTSMAInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1174
      return code;
×
1175
    }
1176
  } else {
1177
    code = hbGetAppInfo(hbParam->clusterId, req);
2,438,079✔
1178
    if (TSDB_CODE_SUCCESS != code) {
2,438,079✔
1179
      tscWarn("hbGetAppInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1180
      return code;
×
1181
    }
1182
  }
1183

1184
  ++hbParam->reqCnt;  // success to get catalog info
9,762,407✔
1185

1186
  return TSDB_CODE_SUCCESS;
9,762,407✔
1187
}
1188

1189
static FORCE_INLINE void hbMgrInitHandle() {
1190
  // init all handle
1191
  clientHbMgr.reqHandle[CONN_TYPE__QUERY] = hbQueryHbReqHandle;
708,015✔
1192
  clientHbMgr.reqHandle[CONN_TYPE__TMQ] = hbMqHbReqHandle;
708,015✔
1193

1194
  clientHbMgr.rspHandle[CONN_TYPE__QUERY] = hbQueryHbRspHandle;
708,015✔
1195
  clientHbMgr.rspHandle[CONN_TYPE__TMQ] = hbMqHbRspHandle;
708,015✔
1196
}
708,015✔
1197

1198
int32_t hbGatherAllInfo(SAppHbMgr *pAppHbMgr, SClientHbBatchReq **pBatchReq) {
8,609,691✔
1199
  *pBatchReq = taosMemoryCalloc(1, sizeof(SClientHbBatchReq));
8,609,691✔
1200
  if (pBatchReq == NULL) {
8,609,691✔
1201
    return terrno;
×
1202
  }
1203
  int32_t connKeyCnt = atomic_load_32(&pAppHbMgr->connKeyCnt);
8,609,691✔
1204
  (*pBatchReq)->reqs = taosArrayInit(connKeyCnt, sizeof(SClientHbReq));
8,609,691✔
1205
  if (!(*pBatchReq)->reqs) {
8,609,691✔
1206
    tFreeClientHbBatchReq(*pBatchReq);
×
1207
    return terrno;
×
1208
  }
1209

1210
  int64_t  maxIpWhiteVer = 0;
8,609,691✔
1211
  void    *pIter = NULL;
8,609,691✔
1212
  SHbParam param = {0};
8,609,691✔
1213
  while ((pIter = taosHashIterate(pAppHbMgr->activeInfo, pIter))) {
20,192,029✔
1214
    SClientHbReq *pOneReq = pIter;
11,582,338✔
1215
    SClientHbKey *connKey = &pOneReq->connKey;
11,582,338✔
1216
    STscObj      *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
11,582,338✔
1217

1218
    if (!pTscObj || atomic_load_8(&pTscObj->dropped) == 1) {
11,582,338✔
1219
      if (pTscObj) releaseTscObj(connKey->tscRid);
822,646✔
1220
      continue;
822,646✔
1221
    }
1222

1223
    tstrncpy(pOneReq->userApp, pTscObj->optionInfo.userApp, sizeof(pOneReq->userApp));
10,759,692✔
1224
    tstrncpy(pOneReq->cInfo, pTscObj->optionInfo.cInfo, sizeof(pOneReq->cInfo));
10,759,692✔
1225
    pOneReq->userIp = pTscObj->optionInfo.userIp;
10,759,692✔
1226
    pOneReq->userDualIp = pTscObj->optionInfo.userDualIp;
10,759,692✔
1227
    tstrncpy(pOneReq->sVer, td_version, TSDB_VERSION_LEN);
10,759,692✔
1228

1229
    pOneReq = taosArrayPush((*pBatchReq)->reqs, pOneReq);
10,759,692✔
1230
    if (NULL == pOneReq) {
10,759,692✔
1231
      releaseTscObj(connKey->tscRid);
×
1232
      continue;
×
1233
    }
1234

1235
    switch (connKey->connType) {
10,759,692✔
1236
      case CONN_TYPE__QUERY: {
10,759,692✔
1237
        if (param.clusterId == 0) {
10,759,692✔
1238
          // init
1239
          param.clusterId = pOneReq->clusterId;
8,295,368✔
1240
          param.pAppHbMgr = pAppHbMgr;
8,295,368✔
1241
          param.connHbFlag = atomic_load_8(&pAppHbMgr->connHbFlag);
8,295,368✔
1242
        }
1243
        break;
10,759,692✔
1244
      }
1245
      default:
×
1246
        break;
×
1247
    }
1248
    if (clientHbMgr.reqHandle[connKey->connType]) {
10,759,692✔
1249
      int32_t code = (*clientHbMgr.reqHandle[connKey->connType])(connKey, &param, pOneReq);
10,759,692✔
1250
      if (code) {
10,759,692✔
1251
        tscWarn("hbGatherAllInfo failed since %s, tscRid:%" PRIi64 ", connType:%" PRIi8, tstrerror(code),
997,285✔
1252
                connKey->tscRid, connKey->connType);
1253
      }
1254
    }
1255

1256
    int64_t ver = atomic_load_64(&pTscObj->whiteListInfo.ver);
10,759,692✔
1257
    maxIpWhiteVer = TMAX(maxIpWhiteVer, ver);
10,759,692✔
1258
    releaseTscObj(connKey->tscRid);
10,759,692✔
1259
  }
1260
  (*pBatchReq)->ipWhiteListVer = maxIpWhiteVer;
8,609,691✔
1261

1262
  return TSDB_CODE_SUCCESS;
8,609,691✔
1263
}
1264

1265
void hbThreadFuncUnexpectedStopped(void) { atomic_store_8(&clientHbMgr.threadStop, 2); }
×
1266

1267
void hbMergeSummary(SAppClusterSummary *dst, SAppClusterSummary *src) {
743,002✔
1268
  dst->numOfInsertsReq += src->numOfInsertsReq;
743,002✔
1269
  dst->numOfInsertRows += src->numOfInsertRows;
743,002✔
1270
  dst->insertElapsedTime += src->insertElapsedTime;
743,002✔
1271
  dst->insertBytes += src->insertBytes;
743,002✔
1272
  dst->fetchBytes += src->fetchBytes;
743,002✔
1273
  dst->queryElapsedTime += src->queryElapsedTime;
743,002✔
1274
  dst->numOfSlowQueries += src->numOfSlowQueries;
743,002✔
1275
  dst->totalRequests += src->totalRequests;
743,002✔
1276
  dst->currentRequests += src->currentRequests;
743,002✔
1277
}
743,002✔
1278

1279
int32_t hbGatherAppInfo(void) {
8,885,479✔
1280
  SAppHbReq req = {0};
8,885,479✔
1281
  int32_t   code = TSDB_CODE_SUCCESS;
8,885,479✔
1282
  int       sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
8,885,479✔
1283
  if (sz > 0) {
8,885,479✔
1284
    req.pid = taosGetPId();
8,885,479✔
1285
    req.appId = clientHbMgr.appId;
8,885,479✔
1286
    TSC_ERR_RET(taosGetAppName(req.name, NULL));
8,885,479✔
1287
  }
1288

1289
  taosHashClear(clientHbMgr.appSummary);
8,885,479✔
1290

1291
  for (int32_t i = 0; i < sz; ++i) {
18,523,977✔
1292
    SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
9,638,498✔
1293
    if (pAppHbMgr == NULL) continue;
9,638,498✔
1294

1295
    int64_t    clusterId = pAppHbMgr->pAppInstInfo->clusterId;
9,638,498✔
1296
    SAppHbReq *pApp = taosHashGet(clientHbMgr.appSummary, &clusterId, sizeof(clusterId));
9,638,498✔
1297
    if (NULL == pApp) {
9,638,498✔
1298
      (void)memcpy(&req.summary, &pAppHbMgr->pAppInstInfo->summary, sizeof(req.summary));
8,895,496✔
1299
      req.startTime = pAppHbMgr->startTime;
8,895,496✔
1300
      TSC_ERR_RET(taosHashPut(clientHbMgr.appSummary, &clusterId, sizeof(clusterId), &req, sizeof(req)));
8,895,496✔
1301
    } else {
1302
      if (pAppHbMgr->startTime < pApp->startTime) {
743,002✔
1303
        pApp->startTime = pAppHbMgr->startTime;
×
1304
      }
1305

1306
      hbMergeSummary(&pApp->summary, &pAppHbMgr->pAppInstInfo->summary);
743,002✔
1307
    }
1308
  }
1309

1310
  return TSDB_CODE_SUCCESS;
8,885,479✔
1311
}
1312

1313
static void *hbThreadFunc(void *param) {
708,015✔
1314
  setThreadName("hb");
708,015✔
1315
#ifdef WINDOWS
1316
  if (taosCheckCurrentInDll()) {
1317
    atexit(hbThreadFuncUnexpectedStopped);
1318
  }
1319
#endif
1320
  while (1) {
8,425,029✔
1321
    if (1 == clientHbMgr.threadStop) {
9,133,044✔
1322
      break;
246,326✔
1323
    }
1324

1325
    if (TSDB_CODE_SUCCESS != taosThreadMutexLock(&clientHbMgr.lock)) {
8,886,718✔
1326
      tscError("taosThreadMutexLock failed");
×
1327
      return NULL;
×
1328
    }
1329

1330
    int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
8,886,718✔
1331
    if (sz > 0) {
8,886,718✔
1332
      if (TSDB_CODE_SUCCESS != hbGatherAppInfo()) {
8,885,479✔
1333
        tscError("hbGatherAppInfo failed");
×
1334
        return NULL;
×
1335
      }
1336
      if (sz > 1 && !clientHbMgr.appHbHash) {
8,885,479✔
1337
        clientHbMgr.appHbHash = taosHashInit(0, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), true, HASH_NO_LOCK);
11,771✔
1338
        if (NULL == clientHbMgr.appHbHash) {
11,771✔
1339
          tscError("taosHashInit failed");
×
1340
          return NULL;
×
1341
        }
1342
      }
1343
      taosHashClear(clientHbMgr.appHbHash);
8,885,479✔
1344
    }
1345

1346
    for (int i = 0; i < sz; i++) {
18,525,216✔
1347
      SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
9,638,498✔
1348
      if (pAppHbMgr == NULL) {
9,638,498✔
1349
        continue;
12,807✔
1350
      }
1351

1352
      int32_t connCnt = atomic_load_32(&pAppHbMgr->connKeyCnt);
9,638,498✔
1353
      if (connCnt == 0) {
9,638,498✔
1354
        continue;
1,028,807✔
1355
      }
1356
      SClientHbBatchReq *pReq = NULL;
8,609,691✔
1357
      int32_t            code = hbGatherAllInfo(pAppHbMgr, &pReq);
8,609,691✔
1358
      if (TSDB_CODE_SUCCESS != code || taosArrayGetP(clientHbMgr.appHbMgrs, i) == NULL) {
8,609,691✔
1359
        terrno = code ? code : TSDB_CODE_OUT_OF_RANGE;
×
1360
        tFreeClientHbBatchReq(pReq);
×
1361
        continue;
×
1362
      }
1363
      int tlen = tSerializeSClientHbBatchReq(NULL, 0, pReq);
8,609,691✔
1364
      if (tlen == -1) {
8,609,691✔
1365
        tFreeClientHbBatchReq(pReq);
×
1366
        break;
×
1367
      }
1368
      void *buf = taosMemoryMalloc(tlen);
8,609,691✔
1369
      if (buf == NULL) {
8,609,691✔
1370
        tFreeClientHbBatchReq(pReq);
×
1371
        // hbClearReqInfo(pAppHbMgr);
1372
        break;
×
1373
      }
1374

1375
      if (tSerializeSClientHbBatchReq(buf, tlen, pReq) == -1) {
8,609,691✔
1376
        tFreeClientHbBatchReq(pReq);
×
1377
        taosMemoryFree(buf);
×
1378
        break;
×
1379
      }
1380
      SMsgSendInfo *pInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
8,609,691✔
1381

1382
      if (pInfo == NULL) {
8,609,691✔
1383
        tFreeClientHbBatchReq(pReq);
×
1384
        // hbClearReqInfo(pAppHbMgr);
1385
        taosMemoryFree(buf);
×
1386
        break;
×
1387
      }
1388
      pInfo->fp = hbAsyncCallBack;
8,609,691✔
1389
      pInfo->msgInfo.pData = buf;
8,609,691✔
1390
      pInfo->msgInfo.len = tlen;
8,609,691✔
1391
      pInfo->msgType = TDMT_MND_HEARTBEAT;
8,609,691✔
1392
      pInfo->param = taosMemoryMalloc(sizeof(int32_t));
8,609,691✔
1393
      if (pInfo->param  == NULL) {
8,609,691✔
1394
        tFreeClientHbBatchReq(pReq);
×
1395
        // hbClearReqInfo(pAppHbMgr);
1396
        taosMemoryFree(buf);
×
1397
        taosMemoryFree(pInfo);
×
1398
        break;
×
1399
      }
1400
      *(int32_t *)pInfo->param = i;
8,609,691✔
1401
      pInfo->paramFreeFp = taosAutoMemoryFree;
8,609,691✔
1402
      pInfo->requestId = generateRequestId();
8,609,691✔
1403
      pInfo->requestObjRefId = 0;
8,609,691✔
1404

1405
      SAppInstInfo *pAppInstInfo = pAppHbMgr->pAppInstInfo;
8,609,691✔
1406
      SEpSet        epSet = getEpSet_s(&pAppInstInfo->mgmtEp);
8,609,691✔
1407
      if (TSDB_CODE_SUCCESS != asyncSendMsgToServer(pAppInstInfo->pTransporter, &epSet, NULL, pInfo)) {
8,609,691✔
1408
        tscWarn("failed to async send msg to server");
×
1409
      }
1410
      tFreeClientHbBatchReq(pReq);
8,609,691✔
1411
      // hbClearReqInfo(pAppHbMgr);
1412
      (void)atomic_add_fetch_32(&pAppHbMgr->reportCnt, 1);
8,609,691✔
1413
    }
1414

1415
    if (TSDB_CODE_SUCCESS != taosThreadMutexUnlock(&clientHbMgr.lock)) {
8,886,718✔
1416
      tscError("taosThreadMutexLock failed");
×
1417
      return NULL;
×
1418
    }
1419
    taosMsleep(HEARTBEAT_INTERVAL);
8,886,718✔
1420
  }
1421
  taosHashCleanup(clientHbMgr.appHbHash);
246,326✔
1422
  return NULL;
246,326✔
1423
}
1424

1425
static int32_t hbCreateThread() {
708,015✔
1426
  int32_t      code = TSDB_CODE_SUCCESS;
708,015✔
1427
  TdThreadAttr thAttr;
702,952✔
1428
  TSC_ERR_JRET(taosThreadAttrInit(&thAttr));
708,015✔
1429
  TSC_ERR_JRET(taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE));
708,015✔
1430
#ifdef TD_COMPACT_OS
1431
  TSC_ERR_JRET(taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL));
1432
#endif
1433

1434
  if (taosThreadCreate(&clientHbMgr.thread, &thAttr, hbThreadFunc, NULL) != 0) {
708,015✔
1435
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
1436
    TSC_ERR_RET(terrno);
×
1437
  }
1438
  (void)taosThreadAttrDestroy(&thAttr);
708,015✔
1439
_return:
708,015✔
1440

1441
  if (code) {
708,015✔
1442
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
1443
    TSC_ERR_RET(terrno);
×
1444
  }
1445

1446
  return code;
708,015✔
1447
}
1448

1449
static void hbStopThread() {
708,670✔
1450
  if (0 == atomic_load_8(&clientHbMgr.inited)) {
708,670✔
1451
    return;
655✔
1452
  }
1453
  if (atomic_val_compare_exchange_8(&clientHbMgr.threadStop, 0, 1)) {
708,015✔
1454
    tscDebug("hb thread already stopped");
×
1455
    return;
×
1456
  }
1457

1458
  int32_t code = TSDB_CODE_SUCCESS;
708,015✔
1459
  // thread quit mode kill or inner exit from self-thread
1460
  if (clientHbMgr.quitByKill) {
708,015✔
1461
    code = taosThreadKill(clientHbMgr.thread, 0);
485,784✔
1462
    if (TSDB_CODE_SUCCESS != code) {
485,784✔
1463
      tscError("taosThreadKill failed since %s", tstrerror(code));
×
1464
    }
1465
  } else {
1466
    code = taosThreadJoin(clientHbMgr.thread, NULL);
222,231✔
1467
    if (TSDB_CODE_SUCCESS != code) {
222,231✔
1468
      tscError("taosThreadJoin failed since %s", tstrerror(code));
×
1469
    }
1470
  }
1471

1472
  tscDebug("hb thread stopped");
708,015✔
1473
}
1474

1475
int32_t appHbMgrInit(SAppInstInfo *pAppInstInfo, char *key, SAppHbMgr **pAppHbMgr) {
727,623✔
1476
  int32_t code = TSDB_CODE_SUCCESS;
727,623✔
1477
  TSC_ERR_RET(hbMgrInit());
727,623✔
1478
  *pAppHbMgr = taosMemoryMalloc(sizeof(SAppHbMgr));
727,623✔
1479
  if (*pAppHbMgr == NULL) {
727,623✔
1480
    TSC_ERR_JRET(terrno);
×
1481
  }
1482
  // init stat
1483
  (*pAppHbMgr)->startTime = taosGetTimestampMs();
1,449,592✔
1484
  (*pAppHbMgr)->connKeyCnt = 0;
727,623✔
1485
  (*pAppHbMgr)->connHbFlag = 0;
727,623✔
1486
  (*pAppHbMgr)->reportCnt = 0;
727,623✔
1487
  (*pAppHbMgr)->reportBytes = 0;
727,623✔
1488
  (*pAppHbMgr)->key = taosStrdup(key);
727,623✔
1489
  if ((*pAppHbMgr)->key == NULL) {
727,623✔
1490
    TSC_ERR_JRET(terrno);
×
1491
  }
1492

1493
  // init app info
1494
  (*pAppHbMgr)->pAppInstInfo = pAppInstInfo;
727,623✔
1495

1496
  // init hash info
1497
  (*pAppHbMgr)->activeInfo = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
727,623✔
1498

1499
  if ((*pAppHbMgr)->activeInfo == NULL) {
727,623✔
1500
    TSC_ERR_JRET(terrno);
×
1501
  }
1502

1503
  // taosHashSetFreeFp(pAppHbMgr->activeInfo, tFreeClientHbReq);
1504

1505
  TSC_ERR_JRET(taosThreadMutexLock(&clientHbMgr.lock));
727,623✔
1506
  if (taosArrayPush(clientHbMgr.appHbMgrs, &(*pAppHbMgr)) == NULL) {
1,455,246✔
1507
    code = terrno;
×
1508
    (void)taosThreadMutexUnlock(&clientHbMgr.lock);
×
1509
    goto _return;
×
1510
  }
1511
  (*pAppHbMgr)->idx = taosArrayGetSize(clientHbMgr.appHbMgrs) - 1;
727,623✔
1512
  TSC_ERR_JRET(taosThreadMutexUnlock(&clientHbMgr.lock));
727,623✔
1513

1514
  return TSDB_CODE_SUCCESS;
727,623✔
1515
_return:
×
1516
  taosMemoryFree(*pAppHbMgr);
×
1517
  return code;
×
1518
}
1519

1520
void hbFreeAppHbMgr(SAppHbMgr *pTarget) {
727,623✔
1521
  void *pIter = taosHashIterate(pTarget->activeInfo, NULL);
727,623✔
1522
  while (pIter != NULL) {
799,857✔
1523
    SClientHbReq *pOneReq = pIter;
72,234✔
1524
    tFreeClientHbReq(pOneReq);
1525
    pIter = taosHashIterate(pTarget->activeInfo, pIter);
72,234✔
1526
  }
1527
  taosHashCleanup(pTarget->activeInfo);
727,623✔
1528
  pTarget->activeInfo = NULL;
727,623✔
1529

1530
  taosMemoryFree(pTarget->key);
727,623✔
1531
  taosMemoryFree(pTarget);
727,623✔
1532
}
727,623✔
1533

1534
void hbRemoveAppHbMrg(SAppHbMgr **pAppHbMgr) {
727,623✔
1535
  int32_t code = TSDB_CODE_SUCCESS;
727,623✔
1536
  code = taosThreadMutexLock(&clientHbMgr.lock);
727,623✔
1537
  if (TSDB_CODE_SUCCESS != code) {
727,623✔
1538
    tscError("failed to lock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1539
  }
1540
  int32_t mgrSize = taosArrayGetSize(clientHbMgr.appHbMgrs);
727,623✔
1541
  for (int32_t i = 0; i < mgrSize; ++i) {
727,623✔
1542
    SAppHbMgr *pItem = taosArrayGetP(clientHbMgr.appHbMgrs, i);
×
1543
    if (pItem == *pAppHbMgr) {
×
1544
      hbFreeAppHbMgr(*pAppHbMgr);
×
1545
      *pAppHbMgr = NULL;
×
1546
      taosArraySet(clientHbMgr.appHbMgrs, i, pAppHbMgr);
×
1547
      break;
×
1548
    }
1549
  }
1550
  code = taosThreadMutexUnlock(&clientHbMgr.lock);
727,623✔
1551
  if (TSDB_CODE_SUCCESS != code) {
727,623✔
1552
    tscError("failed to unlock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1553
  }
1554
}
727,623✔
1555

1556
void appHbMgrCleanup(void) {
708,015✔
1557
  int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
708,015✔
1558
  for (int i = 0; i < sz; i++) {
1,435,638✔
1559
    SAppHbMgr *pTarget = taosArrayGetP(clientHbMgr.appHbMgrs, i);
727,623✔
1560
    if (pTarget == NULL) continue;
727,623✔
1561
    hbFreeAppHbMgr(pTarget);
727,623✔
1562
  }
1563
}
708,015✔
1564

1565
int32_t hbMgrInit() {
727,623✔
1566
  // init once
1567
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 0, 1);
727,623✔
1568
  if (old == 1) return 0;
727,623✔
1569

1570
  clientHbMgr.appId = tGenIdPI64();
708,015✔
1571
  tscInfo("app initialized, appId:0x%" PRIx64, clientHbMgr.appId);
708,015✔
1572

1573
  clientHbMgr.appSummary = taosHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
708,015✔
1574
  if (NULL == clientHbMgr.appSummary) {
708,015✔
1575
    uError("hbMgrInit:taosHashInit error") return terrno;
×
1576
  }
1577
  clientHbMgr.appHbMgrs = taosArrayInit(0, sizeof(void *));
708,015✔
1578
  if (NULL == clientHbMgr.appHbMgrs) {
708,015✔
1579
    uError("hbMgrInit:taosArrayInit error") return terrno;
×
1580
  }
1581
  TdThreadMutexAttr attr = {0};
708,015✔
1582

1583
  int ret = taosThreadMutexAttrInit(&attr);
708,015✔
1584
  if (ret != 0) {
708,015✔
1585
    uError("hbMgrInit:taosThreadMutexAttrInit error") return ret;
×
1586
  }
1587

1588
  ret = taosThreadMutexAttrSetType(&attr, PTHREAD_MUTEX_RECURSIVE);
708,015✔
1589
  if (ret != 0) {
708,015✔
1590
    uError("hbMgrInit:taosThreadMutexAttrSetType error") return ret;
×
1591
  }
1592

1593
  ret = taosThreadMutexInit(&clientHbMgr.lock, &attr);
708,015✔
1594
  if (ret != 0) {
708,015✔
1595
    uError("hbMgrInit:taosThreadMutexInit error") return ret;
×
1596
  }
1597

1598
  ret = taosThreadMutexAttrDestroy(&attr);
708,015✔
1599
  if (ret != 0) {
708,015✔
1600
    uError("hbMgrInit:taosThreadMutexAttrDestroy error") return ret;
×
1601
  }
1602

1603
  // init handle funcs
1604
  hbMgrInitHandle();
1605

1606
  // init backgroud thread
1607
  ret = hbCreateThread();
708,015✔
1608
  if (ret != 0) {
708,015✔
1609
    uError("hbMgrInit:hbCreateThread error") return ret;
×
1610
  }
1611

1612
  return 0;
708,015✔
1613
}
1614

1615
void hbMgrCleanUp() {
708,670✔
1616
  hbStopThread();
708,670✔
1617

1618
  // destroy all appHbMgr
1619
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 1, 0);
708,670✔
1620
  if (old == 0) return;
708,670✔
1621

1622
  int32_t code = taosThreadMutexLock(&clientHbMgr.lock);
708,015✔
1623
  if (TSDB_CODE_SUCCESS != code) {
708,015✔
1624
    tscError("failed to lock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1625
  }
1626
  appHbMgrCleanup();
708,015✔
1627
  taosArrayDestroy(clientHbMgr.appHbMgrs);
708,015✔
1628
  clientHbMgr.appHbMgrs = NULL;
708,015✔
1629
  code = taosThreadMutexUnlock(&clientHbMgr.lock);
708,015✔
1630
  if (TSDB_CODE_SUCCESS != code) {
708,015✔
1631
    tscError("failed to unlock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1632
  }
1633
}
1634

1635
int32_t hbRegisterConnImpl(SAppHbMgr *pAppHbMgr, SClientHbKey connKey, int64_t clusterId) {
1,385,641✔
1636
  // init hash in activeinfo
1637
  void *data = taosHashGet(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
1,385,641✔
1638
  if (data != NULL) {
1,385,641✔
1639
    return 0;
×
1640
  }
1641
  SClientHbReq hbReq = {0};
1,385,641✔
1642
  hbReq.connKey = connKey;
1,385,641✔
1643
  hbReq.clusterId = clusterId;
1,385,641✔
1644
  // hbReq.info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
1645

1646
  TSC_ERR_RET(taosHashPut(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey), &hbReq, sizeof(SClientHbReq)));
1,385,641✔
1647

1648
  (void)atomic_add_fetch_32(&pAppHbMgr->connKeyCnt, 1);
1,385,641✔
1649
  return 0;
1,385,641✔
1650
}
1651

1652
int32_t hbRegisterConn(SAppHbMgr *pAppHbMgr, int64_t tscRefId, int64_t clusterId, int8_t connType) {
1,396,984✔
1653
  SClientHbKey connKey = {
1,396,984✔
1654
      .tscRid = tscRefId,
1655
      .connType = connType,
1656
  };
1657

1658
  switch (connType) {
1,396,984✔
1659
    case CONN_TYPE__QUERY: {
1,385,641✔
1660
      return hbRegisterConnImpl(pAppHbMgr, connKey, clusterId);
1,385,641✔
1661
    }
1662
    case CONN_TYPE__TMQ: {
11,343✔
1663
      return 0;
11,343✔
1664
    }
1665
    default:
×
1666
      return 0;
×
1667
  }
1668
}
1669

1670
void hbDeregisterConn(STscObj *pTscObj, SClientHbKey connKey) {
1,330,527✔
1671
  int32_t code = taosThreadMutexLock(&clientHbMgr.lock);
1,330,527✔
1672
  if (TSDB_CODE_SUCCESS != code) {
1,330,663✔
1673
    tscError("failed to lock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1674
  }
1675
  SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, pTscObj->appHbMgrIdx);
1,330,663✔
1676
  if (pAppHbMgr) {
1,330,663✔
1677
    SClientHbReq *pReq = taosHashAcquire(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
1,330,663✔
1678
    if (pReq) {
1,330,663✔
1679
      tFreeClientHbReq(pReq);
1680
      code = taosHashRemove(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
1,313,407✔
1681
      if (TSDB_CODE_SUCCESS != code) {
1,313,407✔
1682
        tscError("hbDeregisterConn taosHashRemove error, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1683
      }
1684
      taosHashRelease(pAppHbMgr->activeInfo, pReq);
1,313,407✔
1685
      (void)atomic_sub_fetch_32(&pAppHbMgr->connKeyCnt, 1);
1,313,407✔
1686
    }
1687
  }
1688
  code = taosThreadMutexUnlock(&clientHbMgr.lock);
1,330,663✔
1689
  if (TSDB_CODE_SUCCESS != code) {
1,330,663✔
1690
    tscError("failed to unlock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1691
  }
1692
}
1,330,663✔
1693

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