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

taosdata / TDengine / #4877

11 Dec 2025 02:43AM UTC coverage: 64.586% (-0.05%) from 64.632%
#4877

push

travis-ci

guanshengliang
feat(TS-7270): internal dependence

307 of 617 new or added lines in 24 files covered. (49.76%)

673 existing lines in 130 files now uncovered.

163673 of 253417 relevant lines covered (64.59%)

105540806.95 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,
768,360✔
46
                                        SAppHbMgr *pAppHbMgr) {
47
  int32_t code = TSDB_CODE_SUCCESS;
768,360✔
48

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

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

63
    TSC_ERR_JRET(catalogUpdateUserAuthInfo(pCatalog, rsp));
788,289✔
64
  }
65

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

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

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

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

85
    SClientHbReq    *pReq = NULL;
1,311,959✔
86
    SGetUserAuthRsp *pRsp = NULL;
1,311,959✔
87
    while ((pReq = taosHashIterate(hbMgr->activeInfo, pReq))) {
2,454,037✔
88
      STscObj *pTscObj = (STscObj *)acquireTscObj(pReq->connKey.tscRid);
1,357,266✔
89
      if (!pTscObj) {
1,357,266✔
90
        continue;
116,305✔
91
      }
92

93
      if (!pRsp) {
1,240,961✔
94
        for (int32_t j = 0; j < TARRAY_SIZE(batchRsp->pArray); ++j) {
1,274,589✔
95
          SGetUserAuthRsp *rsp = TARRAY_GET_ELEM(batchRsp->pArray, j);
1,059,401✔
96
          if (0 == strncmp(rsp->user, pTscObj->user, TSDB_USER_LEN)) {
1,059,401✔
97
            pRsp = rsp;
815,773✔
98
            break;
815,773✔
99
          }
100
        }
101
        if (!pRsp) {
1,030,961✔
102
          releaseTscObj(pReq->connKey.tscRid);
215,188✔
103
          taosHashCancelIterate(hbMgr->activeInfo, pReq);
215,188✔
104
          break;
215,188✔
105
        }
106
      }
107

108
      if (pRsp->dropped == 1) {
1,025,773✔
109
        if (atomic_val_compare_exchange_8(&pTscObj->dropped, 0, 1) == 0) {
15,727✔
110
          if (pTscObj->userDroppedInfo.fp) {
13,752✔
111
            SPassInfo *dropInfo = &pTscObj->userDroppedInfo;
12,200✔
112
            if (dropInfo->fp) {
12,200✔
113
              (*dropInfo->fp)(dropInfo->param, NULL, TAOS_NOTIFY_USER_DROPPED);
12,200✔
114
            }
115
          }
116
        }
117
        releaseTscObj(pReq->connKey.tscRid);
15,727✔
118
        continue;
15,727✔
119
      }
120

121
      pTscObj->authVer = pRsp->version;
1,010,046✔
122

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

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

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

163
      if (pTscObj->dateTimeWhiteListInfo.fp) {
1,010,046✔
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;
1,010,046✔
176
        int64_t         oldVer = atomic_load_64(&whiteListInfo->ver);
1,010,046✔
177
        atomic_store_64(&whiteListInfo->ver, pRsp->timeWhiteListVer);
1,010,046✔
178
        tscDebug("update date time whitelist version of user %s from %" PRId64 " to %" PRId64 ", conn:%" PRIi64, pRsp->user,
1,010,046✔
179
                 oldVer, atomic_load_64(&whiteListInfo->ver), pTscObj->id);
180
      }
181
      releaseTscObj(pReq->connKey.tscRid);
1,010,046✔
182
    }
183
  }
184
  return 0;
768,360✔
185
}
186

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

194
  vgInfo->vgVersion = rsp->vgVersion;
1,430,971✔
195
  vgInfo->stateTs = rsp->stateTs;
1,430,971✔
196
  vgInfo->flags = rsp->flags;
1,430,971✔
197
  vgInfo->hashMethod = rsp->hashMethod;
1,430,971✔
198
  vgInfo->hashPrefix = rsp->hashPrefix;
1,430,971✔
199
  vgInfo->hashSuffix = rsp->hashSuffix;
1,430,971✔
200
  vgInfo->vgHash = taosHashInit(rsp->vgNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
1,430,971✔
201
  if (NULL == vgInfo->vgHash) {
1,430,971✔
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) {
5,236,753✔
208
    SVgroupInfo *pInfo = taosArrayGet(rsp->pVgroupInfos, j);
3,805,782✔
209
    if (taosHashPut(vgInfo->vgHash, &pInfo->vgId, sizeof(int32_t), pInfo, sizeof(SVgroupInfo)) != 0) {
3,805,782✔
210
      tscError("hash push failed, terrno:%d", terrno);
×
211
      code = terrno;
×
212
      goto _return;
×
213
    }
214
  }
215

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

222
  *pInfo = vgInfo;
1,430,971✔
223
  return code;
1,430,971✔
224
}
225

226
static int32_t hbProcessDBInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
4,729,862✔
227
  int32_t code = 0;
4,729,862✔
228

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

236
  int32_t numOfBatchs = taosArrayGetSize(batchRsp.pArray);
4,729,862✔
237
  for (int32_t i = 0; i < numOfBatchs; ++i) {
6,173,061✔
238
    SDbHbRsp *rsp = taosArrayGet(batchRsp.pArray, i);
1,443,199✔
239
    if (NULL == rsp) {
1,443,199✔
240
      code = terrno;
×
241
      goto _return;
×
242
    }
243
    if (rsp->useDbRsp) {
1,443,199✔
244
      tscDebug("hb use db rsp, db:%s, vgVersion:%d, stateTs:%" PRId64 ", uid:%" PRIx64, rsp->useDbRsp->db,
1,204,731✔
245
               rsp->useDbRsp->vgVersion, rsp->useDbRsp->stateTs, rsp->useDbRsp->uid);
246

247
      if (rsp->useDbRsp->vgVersion < 0) {
1,204,731✔
248
        tscDebug("hb to remove db, db:%s", rsp->useDbRsp->db);
50,556✔
249
        code = catalogRemoveDB(pCatalog, rsp->useDbRsp->db, rsp->useDbRsp->uid);
50,556✔
250
      } else {
251
        SDBVgInfo *vgInfo = NULL;
1,154,175✔
252
        code = hbGenerateVgInfoFromRsp(&vgInfo, rsp->useDbRsp);
1,154,175✔
253
        if (TSDB_CODE_SUCCESS != code) {
1,154,175✔
254
          goto _return;
×
255
        }
256

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

259
        TSC_ERR_JRET(catalogUpdateDBVgInfo(pCatalog, rsp->useDbRsp->db, rsp->useDbRsp->uid, vgInfo));
1,154,175✔
260

261
        if (IS_SYS_DBNAME(rsp->useDbRsp->db)) {
1,154,175✔
262
          code = hbGenerateVgInfoFromRsp(&vgInfo, rsp->useDbRsp);
276,796✔
263
          if (TSDB_CODE_SUCCESS != code) {
276,796✔
264
            goto _return;
×
265
          }
266

267
          TSC_ERR_JRET(catalogUpdateDBVgInfo(
276,796✔
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) {
1,443,199✔
275
      tscDebug("hb db cfg rsp, db:%s, cfgVersion:%d", rsp->cfgRsp->db, rsp->cfgRsp->cfgVersion);
184,134✔
276
      code = catalogUpdateDbCfg(pCatalog, rsp->cfgRsp->db, rsp->cfgRsp->dbId, rsp->cfgRsp);
184,134✔
277
      rsp->cfgRsp = NULL;
184,134✔
278
    }
279
    if (rsp->pTsmaRsp) {
1,443,199✔
280
      if (rsp->pTsmaRsp->pTsmas) {
661,124✔
281
        for (int32_t i = 0; i < rsp->pTsmaRsp->pTsmas->size; ++i) {
2,908✔
282
          STableTSMAInfo *pTsma = taosArrayGetP(rsp->pTsmaRsp->pTsmas, i);
1,454✔
283
          if (NULL == pTsma) {
1,454✔
284
            TSC_ERR_JRET(TSDB_CODE_OUT_OF_RANGE);
×
285
          }
286
          TSC_ERR_JRET(catalogAsyncUpdateTSMA(pCatalog, &pTsma, rsp->dbTsmaVersion));
1,454✔
287
        }
288
        taosArrayClear(rsp->pTsmaRsp->pTsmas);
1,454✔
289
      } else {
290
        TSC_ERR_JRET(catalogAsyncUpdateDbTsmaVersion(pCatalog, rsp->dbTsmaVersion, rsp->db, rsp->dbId));
659,670✔
291
      }
292
    }
293
  }
294

295
_return:
4,729,862✔
296

297
  tFreeSDbHbBatchRsp(&batchRsp);
4,729,862✔
298
  return code;
4,729,862✔
299
}
300

301
static int32_t hbProcessStbInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
3,427,645✔
302
  int32_t code = TSDB_CODE_SUCCESS;
3,427,645✔
303

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

310
  int32_t numOfMeta = taosArrayGetSize(hbRsp.pMetaRsp);
3,427,645✔
311
  for (int32_t i = 0; i < numOfMeta; ++i) {
3,492,799✔
312
    STableMetaRsp *rsp = taosArrayGet(hbRsp.pMetaRsp, i);
65,154✔
313
    if (NULL == rsp) {
65,154✔
314
      code = terrno;
×
315
      goto _return;
×
316
    }
317
    if (rsp->numOfColumns < 0) {
65,154✔
318
      tscDebug("hb to remove stb, db:%s, stb:%s", rsp->dbFName, rsp->stbName);
45,779✔
319
      TSC_ERR_JRET(catalogRemoveStbMeta(pCatalog, rsp->dbFName, rsp->dbId, rsp->stbName, rsp->suid));
45,779✔
320
    } else {
321
      tscDebug("hb to update stb, db:%s, stb:%s", rsp->dbFName, rsp->stbName);
19,375✔
322
      if (rsp->pSchemas[0].colId != PRIMARYKEY_TIMESTAMP_COL_ID) {
19,375✔
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));
19,375✔
329
    }
330
  }
331

332
  int32_t numOfIndex = taosArrayGetSize(hbRsp.pIndexRsp);
3,427,645✔
333
  for (int32_t i = 0; i < numOfIndex; ++i) {
3,427,645✔
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:
3,427,645✔
343
  taosArrayDestroy(hbRsp.pIndexRsp);
3,427,645✔
344
  hbRsp.pIndexRsp = NULL;
3,427,645✔
345

346
  tFreeSSTbHbRsp(&hbRsp);
3,427,645✔
347
  return code;
3,427,645✔
348
}
349

350
static int32_t hbProcessDynViewRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
4,987✔
351
  return catalogUpdateDynViewVer(pCatalog, (SDynViewVersion *)value);
4,987✔
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) {
4,987✔
364
  int32_t code = TSDB_CODE_SUCCESS;
4,987✔
365

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

373
  int32_t numOfMeta = taosArrayGetSize(hbRsp.pViewRsp);
4,987✔
374
  for (int32_t i = 0; i < numOfMeta; ++i) {
5,881✔
375
    SViewMetaRsp *rsp = taosArrayGetP(hbRsp.pViewRsp, i);
894✔
376
    if (NULL == rsp) {
894✔
377
      code = terrno;
×
378
      goto _return;
×
379
    }
380
    if (rsp->numOfCols < 0) {
894✔
381
      tscDebug("hb to remove view, db:%s, view:%s", rsp->dbFName, rsp->name);
894✔
382
      code = catalogRemoveViewMeta(pCatalog, rsp->dbFName, rsp->dbId, rsp->name, rsp->viewId);
894✔
383
      tFreeSViewMetaRsp(rsp);
894✔
384
      taosMemoryFreeClear(rsp);
894✔
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);
894✔
390
  }
391

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

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

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

406
  int32_t numOfTsma = taosArrayGetSize(hbRsp.pTsmas);
93,783✔
407
  for (int32_t i = 0; i < numOfTsma; ++i) {
93,783✔
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:
93,783✔
423
  taosArrayDestroy(hbRsp.pTsmas);
93,783✔
424
  return code;
93,783✔
425
}
426

427
static void hbProcessQueryRspKvs(int32_t kvNum, SArray *pKvs, struct SCatalog *pCatalog, SAppHbMgr *pAppHbMgr) {
7,517,430✔
428
  for (int32_t i = 0; i < kvNum; ++i) {
16,547,054✔
429
    SKv *kv = taosArrayGet(pKvs, i);
9,029,624✔
430
    if (NULL == kv) {
9,029,624✔
431
      tscError("invalid hb kv, idx:%d", i);
×
432
      continue;
×
433
    }
434
    switch (kv->key) {
9,029,624✔
435
      case HEARTBEAT_KEY_USER_AUTHINFO: {
768,360✔
436
        if (kv->valueLen <= 0 || NULL == kv->value) {
768,360✔
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)) {
768,360✔
441
          tscError("process user auth info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
442
          break;
×
443
        }
444
        break;
768,360✔
445
      }
446
      case HEARTBEAT_KEY_DBINFO: {
4,729,862✔
447
        if (kv->valueLen <= 0 || NULL == kv->value) {
4,729,862✔
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)) {
4,729,862✔
452
          tscError("process db info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
453
          break;
×
454
        }
455
        break;
4,729,862✔
456
      }
457
      case HEARTBEAT_KEY_STBINFO: {
3,427,645✔
458
        if (kv->valueLen <= 0 || NULL == kv->value) {
3,427,645✔
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)) {
3,427,645✔
463
          tscError("process stb info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
464
          break;
×
465
        }
466
        break;
3,427,645✔
467
      }
468
#ifdef TD_ENTERPRISE
469
      case HEARTBEAT_KEY_DYN_VIEW: {
4,987✔
470
        if (kv->valueLen <= 0 || NULL == kv->value) {
4,987✔
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)) {
4,987✔
475
          tscError("Process dyn view response failed, len:%d, value:%p", kv->valueLen, kv->value);
×
476
          break;
×
477
        }
478
        break;
4,987✔
479
      }
480
      case HEARTBEAT_KEY_VIEWINFO: {
4,987✔
481
        if (kv->valueLen <= 0 || NULL == kv->value) {
4,987✔
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)) {
4,987✔
486
          tscError("Process view info response failed, len:%d, value:%p", kv->valueLen, kv->value);
×
487
          break;
×
488
        }
489
        break;
4,987✔
490
      }
491
#endif
492
      case HEARTBEAT_KEY_TSMA: {
93,783✔
493
        if (kv->valueLen <= 0 || !kv->value) {
93,783✔
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)) {
93,783✔
497
          tscError("Process tsma info response failed, len:%d, value:%p", kv->valueLen, kv->value);
×
498
        }
499
        break;
93,783✔
500
      }
501
      default:
×
502
        tscError("invalid hb key type:%d", kv->key);
×
503
        break;
×
504
    }
505
  }
506
}
7,517,430✔
507

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

516
  if (pRsp->query) {
23,706,506✔
517
    STscObj *pTscObj = (STscObj *)acquireTscObj(pRsp->connKey.tscRid);
21,827,361✔
518
    if (NULL == pTscObj) {
21,827,361✔
519
      tscDebug("tscObj rid %" PRIx64 " not exist", pRsp->connKey.tscRid);
772✔
520
    } else {
521
      if (pRsp->query->totalDnodes > 1) {
21,826,589✔
522
        SEpSet  originEpset = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
6,783,359✔
523
        if (!isEpsetEqual(&originEpset, &pRsp->query->epSet)) {
6,783,359✔
524
          SEpSet *pOrig = &originEpset;
55,408✔
525
          SEp    *pOrigEp = &pOrig->eps[pOrig->inUse];
55,408✔
526
          SEp    *pNewEp = &pRsp->query->epSet.eps[pRsp->query->epSet.inUse];
55,408✔
527
          tscDebug("mnode epset updated from %d/%d=>%s:%d to %d/%d=>%s:%d in hb", pOrig->inUse, pOrig->numOfEps,
55,408✔
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);
55,408✔
532
        }
533
      }
534

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

541
      if (pRsp->query->killRid) {
21,826,589✔
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) {
21,826,589✔
553
        taos_close_internal(pTscObj);
×
554
      }
555

556
      if (pRsp->query->pQnodeList) {
21,826,589✔
557
        if (TSDB_CODE_SUCCESS != updateQnodeList(pTscObj->pAppInfo, pRsp->query->pQnodeList)) {
78,185✔
558
          tscWarn("update qnode list failed");
×
559
        }
560
      }
561

562
      releaseTscObj(pRsp->connKey.tscRid);
21,826,589✔
563
    }
564
  }
565

566
  int32_t kvNum = pRsp->info ? taosArrayGetSize(pRsp->info) : 0;
23,706,506✔
567

568
  tscDebug("hb got %d rsp kv", kvNum);
23,706,506✔
569

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

580
  taosHashRelease(pAppHbMgr->activeInfo, pReq);
23,706,506✔
581

582
  return TSDB_CODE_SUCCESS;
23,706,506✔
583
}
584

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

590
  static int32_t    emptyRspNum = 0;
591
  int32_t           idx = *(int32_t *)param;
20,697,026✔
592
  SClientHbBatchRsp pRsp = {0};
20,697,026✔
593
  if (TSDB_CODE_SUCCESS == code) {
20,697,038✔
594
    code = tDeserializeSClientHbBatchRsp(pMsg->pData, pMsg->len, &pRsp);
20,003,138✔
595
    if (TSDB_CODE_SUCCESS != code) {
20,001,612✔
596
      tscError("deserialize hb rsp failed");
1,041✔
597
    }
598
    int32_t now = taosGetTimestampSec();
20,001,612✔
599
    int32_t delta = abs(now - pRsp.svrTimestamp);
20,000,126✔
600
    if (delta > tsTimestampDeltaLimit) {
20,000,126✔
601
      code = TSDB_CODE_TIME_UNSYNCED;
1,041✔
602
      tscError("time diff:%ds is too big", delta);
1,041✔
603
    }
604
  }
605

606
  int32_t rspNum = taosArrayGetSize(pRsp.rsps);
20,694,026✔
607

608
  (void)taosThreadMutexLock(&clientHbMgr.lock);
20,695,790✔
609

610
  SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, idx);
20,697,157✔
611
  if (pAppHbMgr == NULL) {
20,697,157✔
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;
20,697,157✔
621

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

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

639
  if (rspNum) {
20,002,097✔
640
    tscDebug("hb got %d rsp, %d empty rsp received before", rspNum,
19,479,423✔
641
             atomic_val_compare_exchange_32(&emptyRspNum, emptyRspNum, 0));
642
  } else {
643
    (void)atomic_add_fetch_32(&emptyRspNum, 1);
522,674✔
644
  }
645

646
  for (int32_t i = 0; i < rspNum; ++i) {
43,722,370✔
647
    SClientHbRsp *rsp = taosArrayGet(pRsp.rsps, i);
23,720,273✔
648
    code = (*clientHbMgr.rspHandle[rsp->connKey.connType])(pAppHbMgr, rsp);
23,720,273✔
649
    if (code) {
23,720,273✔
650
      break;
×
651
    }
652
  }
653

654
  (void)taosThreadMutexUnlock(&clientHbMgr.lock);
20,002,097✔
655

656
  tFreeClientHbBatchRsp(&pRsp);
657

658
_return:
20,002,097✔
659
  taosMemoryFree(pMsg->pData);
20,002,097✔
660
  taosMemoryFree(pMsg->pEpSet);
20,002,097✔
661
  return code;
20,002,097✔
662
}
663

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

669
  void *pIter = taosHashIterate(pObj->pRequests, NULL);
22,492,197✔
670
  while (pIter != NULL) {
45,599,479✔
671
    int64_t     *rid = pIter;
23,107,282✔
672
    SRequestObj *pRequest = acquireRequest(*rid);
23,107,282✔
673
    if (NULL == pRequest) {
23,107,282✔
674
      pIter = taosHashIterate(pObj->pRequests, pIter);
16,441✔
675
      continue;
16,441✔
676
    }
677

678
    if (pRequest->killed || 0 == pRequest->body.queryJob) {
23,090,841✔
679
      (void)releaseRequest(*rid);
10,303,440✔
680
      pIter = taosHashIterate(pObj->pRequests, pIter);
10,303,440✔
681
      continue;
10,303,440✔
682
    }
683

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

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

706
      code = schedulerGetTasksStatus(pRequest->body.queryJob, desc.subDesc);
12,787,401✔
707
      if (code) {
12,787,401✔
708
        taosArrayDestroy(desc.subDesc);
2,006,228✔
709
        desc.subDesc = NULL;
2,006,228✔
710
      }
711
      desc.subPlanNum = taosArrayGetSize(desc.subDesc);
12,787,401✔
712
    } else {
713
      desc.subDesc = NULL;
×
714
    }
715

716
    (void)releaseRequest(*rid);
12,787,401✔
717
    if (NULL == taosArrayPush(hbBasic->queryDesc, &desc)) {
25,574,802✔
718
      taosArrayDestroy(desc.subDesc);
×
719
      return terrno;
×
720
    }
721

722
    pIter = taosHashIterate(pObj->pRequests, pIter);
12,787,401✔
723
  }
724

725
  return code;
22,492,197✔
726
}
727

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

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

742
  hbBasic->connId = pTscObj->connId;
24,425,576✔
743

744
  int32_t numOfQueries = pTscObj->pRequests ? taosHashGetSize(pTscObj->pRequests) : 0;
24,425,576✔
745
  if (numOfQueries <= 0) {
24,425,576✔
746
    req->query = hbBasic;
1,933,379✔
747
    releaseTscObj(connKey->tscRid);
1,933,379✔
748
    tscDebug("no queries on connection");
1,933,379✔
749
    return TSDB_CODE_SUCCESS;
1,933,379✔
750
  }
751

752
  hbBasic->queryDesc = taosArrayInit(numOfQueries, sizeof(SQueryDesc));
22,492,197✔
753
  if (NULL == hbBasic->queryDesc) {
22,492,197✔
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);
22,492,197✔
761
  if (code) {
22,492,197✔
762
    releaseTscObj(connKey->tscRid);
1,920,612✔
763
    if (hbBasic->queryDesc) {
1,920,612✔
764
      taosArrayDestroyEx(hbBasic->queryDesc, tFreeClientHbQueryDesc);
1,920,612✔
765
    }
766
    taosMemoryFree(hbBasic);
1,920,612✔
767
    return code;
1,920,612✔
768
  }
769

770
  req->query = hbBasic;
20,571,585✔
771
  releaseTscObj(connKey->tscRid);
20,571,585✔
772

773
  return TSDB_CODE_SUCCESS;
20,571,585✔
774
}
775

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

783
  int32_t code = 0;
739,532✔
784

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

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

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

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

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

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

846
  return code;
739,532✔
847
}
848

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

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

859
  if (userNum <= 0) {
17,400,449✔
860
    taosMemoryFree(users);
673,500✔
861
    return TSDB_CODE_SUCCESS;
673,500✔
862
  }
863

864
  for (int32_t i = 0; i < userNum; ++i) {
33,821,089✔
865
    SUserAuthVersion *user = &users[i];
17,094,140✔
866
    user->version = htonl(user->version);
17,094,140✔
867
  }
868

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

875
  tscDebug("hb got %d expired users, valueLen:%d", userNum, kv.valueLen);
16,726,949✔
876

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

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

891
  return TSDB_CODE_SUCCESS;
16,726,949✔
892
}
893

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

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

904
  if (dbNum <= 0) {
18,362,578✔
905
    taosMemoryFree(dbs);
13,595,678✔
906
    return TSDB_CODE_SUCCESS;
13,595,678✔
907
  }
908

909
  for (int32_t i = 0; i < dbNum; ++i) {
10,779,016✔
910
    SDbCacheInfo *db = &dbs[i];
6,012,116✔
911
    tscDebug("the %dth expired db:%s, dbId:%" PRId64
6,012,116✔
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);
6,012,116✔
916
    db->vgVersion = htonl(db->vgVersion);
6,012,116✔
917
    db->cfgVersion = htonl(db->cfgVersion);
6,012,116✔
918
    db->numOfTable = htonl(db->numOfTable);
6,012,116✔
919
    db->stateTs = htobe64(db->stateTs);
6,012,116✔
920
    db->tsmaVersion = htonl(db->tsmaVersion);
6,012,116✔
921
  }
922

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

929
  tscDebug("hb got %d expired db, valueLen:%d", dbNum, kv.valueLen);
4,766,900✔
930

931
  if (NULL == req->info) {
4,766,900✔
932
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
26,826✔
933
    if (NULL == req->info) {
26,826✔
934
      taosMemoryFree(dbs);
×
935
      return terrno;
×
936
    }
937
  }
938

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

945
  return TSDB_CODE_SUCCESS;
4,766,900✔
946
}
947

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

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

958
  if (stbNum <= 0) {
18,362,578✔
959
    taosMemoryFree(stbs);
14,910,155✔
960
    return TSDB_CODE_SUCCESS;
14,910,155✔
961
  }
962

963
  for (int32_t i = 0; i < stbNum; ++i) {
9,664,030✔
964
    SSTableVersion *stb = &stbs[i];
6,211,607✔
965
    stb->suid = htobe64(stb->suid);
6,211,607✔
966
    stb->sversion = htonl(stb->sversion);
6,211,607✔
967
    stb->tversion = htonl(stb->tversion);
6,211,607✔
968
    stb->smaVer = htonl(stb->smaVer);
6,211,607✔
969
  }
970

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

977
  tscDebug("hb got %d expired stb, valueLen:%d", stbNum, kv.valueLen);
3,452,423✔
978

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

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

993
  return TSDB_CODE_SUCCESS;
3,452,423✔
994
}
995

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

1002
  TSC_ERR_JRET(catalogGetExpiredViews(pCatalog, &views, &viewNum, &pDynViewVer));
18,362,578✔
1003

1004
  if (viewNum <= 0) {
18,362,578✔
1005
    taosMemoryFree(views);
18,349,225✔
1006
    taosMemoryFree(pDynViewVer);
18,349,225✔
1007
    return TSDB_CODE_SUCCESS;
18,349,225✔
1008
  }
1009

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

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

1019
  if (NULL == req->info) {
13,353✔
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 = {
13,353✔
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)));
13,353✔
1033

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

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

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

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

1057
  if (tsmaNum <= 0) {
18,362,578✔
1058
    taosMemoryFree(tsmas);
18,268,795✔
1059
    return TSDB_CODE_SUCCESS;
18,268,795✔
1060
  }
1061

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

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

1071
  if (!pReq->info) {
93,783✔
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};
93,783✔
1080
  code = taosHashPut(pReq->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
93,783✔
1081
  if (TSDB_CODE_SUCCESS != code) {
93,783✔
1082
    taosMemoryFree(tsmas);
×
1083
    return code;
×
1084
  }
1085
  return TSDB_CODE_SUCCESS;
93,783✔
1086
}
1087

1088
int32_t hbGetAppInfo(int64_t clusterId, SClientHbReq *req) {
22,504,964✔
1089
  SAppHbReq *pApp = taosHashGet(clientHbMgr.appSummary, &clusterId, sizeof(clusterId));
22,504,964✔
1090
  if (NULL != pApp) {
22,504,964✔
1091
    (void)memcpy(&req->app, pApp, sizeof(*pApp));
22,504,964✔
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;
22,504,964✔
1100
}
1101

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

1107
  code = hbGetQueryBasicInfo(connKey, req);
24,425,576✔
1108
  if (code != TSDB_CODE_SUCCESS) {
24,425,576✔
1109
    tscWarn("hbGetQueryBasicInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
1,920,612✔
1110
    return code;
1,920,612✔
1111
  }
1112

1113
  if (hbParam->reqCnt == 0) {
22,504,964✔
1114
    code = catalogGetHandle(hbParam->clusterId, &pCatalog);
18,362,816✔
1115
    if (code != TSDB_CODE_SUCCESS) {
18,362,816✔
1116
      tscWarn("catalogGetHandle failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1117
      return code;
×
1118
    }
1119

1120
    code = hbGetAppInfo(hbParam->clusterId, req);
18,362,816✔
1121
    if (TSDB_CODE_SUCCESS != code) {
18,362,816✔
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))) {
18,362,816✔
1127
      code = hbGetExpiredUserInfo(connKey, pCatalog, req);
17,400,449✔
1128
      if (TSDB_CODE_SUCCESS != code) {
17,400,449✔
1129
        tscWarn("hbGetExpiredUserInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1130
        return code;
×
1131
      }
1132
      if (clientHbMgr.appHbHash) {
17,400,449✔
1133
        code = taosHashPut(clientHbMgr.appHbHash, &hbParam->clusterId, sizeof(uint64_t), NULL, 0);
2,083,313✔
1134
        if (TSDB_CODE_SUCCESS != code) {
2,083,313✔
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)) {
18,362,816✔
1144
      code = hbGetUserAuthInfo(connKey, hbParam, req);
739,770✔
1145
      if (TSDB_CODE_SUCCESS != code) {
739,770✔
1146
        tscWarn("hbGetUserAuthInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
238✔
1147
        return code;
238✔
1148
      }
1149
      atomic_store_8(&hbParam->pAppHbMgr->connHbFlag, 1);
739,532✔
1150
    }
1151

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

1158
    code = hbGetExpiredStbInfo(connKey, pCatalog, req);
18,362,578✔
1159
    if (TSDB_CODE_SUCCESS != code) {
18,362,578✔
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);
18,362,578✔
1166
    if (TSDB_CODE_SUCCESS != code) {
18,362,578✔
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);
18,362,578✔
1172
    if (TSDB_CODE_SUCCESS != code) {
18,362,578✔
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);
4,142,148✔
1178
    if (TSDB_CODE_SUCCESS != code) {
4,142,148✔
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
22,504,726✔
1185

1186
  return TSDB_CODE_SUCCESS;
22,504,726✔
1187
}
1188

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

1194
  clientHbMgr.rspHandle[CONN_TYPE__QUERY] = hbQueryHbRspHandle;
1,272,943✔
1195
  clientHbMgr.rspHandle[CONN_TYPE__TMQ] = hbMqHbRspHandle;
1,272,943✔
1196
}
1,272,943✔
1197

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

1210
  int64_t  maxIpWhiteVer = 0;
20,710,079✔
1211
  void    *pIter = NULL;
20,710,079✔
1212
  SHbParam param = {0};
20,710,079✔
1213
  while ((pIter = taosHashIterate(pAppHbMgr->activeInfo, pIter))) {
46,588,327✔
1214
    SClientHbReq *pOneReq = pIter;
25,878,248✔
1215
    SClientHbKey *connKey = &pOneReq->connKey;
25,878,248✔
1216
    STscObj      *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
25,878,248✔
1217

1218
    if (!pTscObj || atomic_load_8(&pTscObj->dropped) == 1) {
25,878,248✔
1219
      if (pTscObj) releaseTscObj(connKey->tscRid);
1,452,672✔
1220
      continue;
1,452,672✔
1221
    }
1222

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

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

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

1256
    int64_t ver = atomic_load_64(&pTscObj->whiteListInfo.ver);
24,425,576✔
1257
    maxIpWhiteVer = TMAX(maxIpWhiteVer, ver);
24,425,576✔
1258
    releaseTscObj(connKey->tscRid);
24,425,576✔
1259
  }
1260
  (*pBatchReq)->ipWhiteListVer = maxIpWhiteVer;
20,710,079✔
1261

1262
  return TSDB_CODE_SUCCESS;
20,710,079✔
1263
}
1264

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

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

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

1289
  taosHashClear(clientHbMgr.appSummary);
20,629,570✔
1290

1291
  for (int32_t i = 0; i < sz; ++i) {
44,465,529✔
1292
    SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
23,835,959✔
1293
    if (pAppHbMgr == NULL) continue;
23,835,959✔
1294

1295
    int64_t    clusterId = pAppHbMgr->pAppInstInfo->clusterId;
23,835,959✔
1296
    SAppHbReq *pApp = taosHashGet(clientHbMgr.appSummary, &clusterId, sizeof(clusterId));
23,835,959✔
1297
    if (NULL == pApp) {
23,835,959✔
1298
      (void)memcpy(&req.summary, &pAppHbMgr->pAppInstInfo->summary, sizeof(req.summary));
21,108,727✔
1299
      req.startTime = pAppHbMgr->startTime;
21,108,727✔
1300
      TSC_ERR_RET(taosHashPut(clientHbMgr.appSummary, &clusterId, sizeof(clusterId), &req, sizeof(req)));
21,108,727✔
1301
    } else {
1302
      if (pAppHbMgr->startTime < pApp->startTime) {
2,727,232✔
1303
        pApp->startTime = pAppHbMgr->startTime;
×
1304
      }
1305

1306
      hbMergeSummary(&pApp->summary, &pAppHbMgr->pAppInstInfo->summary);
2,727,232✔
1307
    }
1308
  }
1309

1310
  return TSDB_CODE_SUCCESS;
20,629,570✔
1311
}
1312

1313
static void *hbThreadFunc(void *param) {
1,272,943✔
1314
  setThreadName("hb");
1,272,943✔
1315
#ifdef WINDOWS
1316
  if (taosCheckCurrentInDll()) {
1317
    atexit(hbThreadFuncUnexpectedStopped);
1318
  }
1319
#endif
1320
  while (1) {
19,884,085✔
1321
    if (1 == clientHbMgr.threadStop) {
21,157,028✔
1322
      break;
521,670✔
1323
    }
1324

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

1330
    int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
20,635,358✔
1331
    if (sz > 0) {
20,635,358✔
1332
      if (TSDB_CODE_SUCCESS != hbGatherAppInfo()) {
20,629,570✔
1333
        tscError("hbGatherAppInfo failed");
×
1334
        return NULL;
×
1335
      }
1336
      if (sz > 1 && !clientHbMgr.appHbHash) {
20,629,570✔
1337
        clientHbMgr.appHbHash = taosHashInit(0, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), true, HASH_NO_LOCK);
39,550✔
1338
        if (NULL == clientHbMgr.appHbHash) {
39,550✔
1339
          tscError("taosHashInit failed");
×
1340
          return NULL;
×
1341
        }
1342
      }
1343
      taosHashClear(clientHbMgr.appHbHash);
20,629,570✔
1344
    }
1345

1346
    for (int i = 0; i < sz; i++) {
44,471,317✔
1347
      SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
23,835,959✔
1348
      if (pAppHbMgr == NULL) {
23,835,959✔
1349
        continue;
1,055,535✔
1350
      }
1351

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

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

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

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

1415
    if (TSDB_CODE_SUCCESS != taosThreadMutexUnlock(&clientHbMgr.lock)) {
20,635,358✔
1416
      tscError("taosThreadMutexLock failed");
×
1417
      return NULL;
×
1418
    }
1419
    taosMsleep(HEARTBEAT_INTERVAL);
20,635,358✔
1420
  }
1421
  taosHashCleanup(clientHbMgr.appHbHash);
521,670✔
1422
  return NULL;
521,670✔
1423
}
1424

1425
static int32_t hbCreateThread() {
1,272,943✔
1426
  int32_t      code = TSDB_CODE_SUCCESS;
1,272,943✔
1427
  TdThreadAttr thAttr;
1,160,656✔
1428
  TSC_ERR_JRET(taosThreadAttrInit(&thAttr));
1,272,943✔
1429
  TSC_ERR_JRET(taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE));
1,272,943✔
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) {
1,272,943✔
1435
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
1436
    TSC_ERR_RET(terrno);
×
1437
  }
1438
  (void)taosThreadAttrDestroy(&thAttr);
1,272,943✔
1439
_return:
1,272,943✔
1440

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

1446
  return code;
1,272,943✔
1447
}
1448

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

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

1472
  tscDebug("hb thread stopped");
1,272,943✔
1473
}
1474

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

1493
  // init app info
1494
  (*pAppHbMgr)->pAppInstInfo = pAppInstInfo;
1,338,944✔
1495

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

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

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

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

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

1520
void hbFreeAppHbMgr(SAppHbMgr *pTarget) {
1,338,944✔
1521
  void *pIter = taosHashIterate(pTarget->activeInfo, NULL);
1,338,944✔
1522
  while (pIter != NULL) {
1,568,905✔
1523
    SClientHbReq *pOneReq = pIter;
229,961✔
1524
    tFreeClientHbReq(pOneReq);
1525
    pIter = taosHashIterate(pTarget->activeInfo, pIter);
229,961✔
1526
  }
1527
  taosHashCleanup(pTarget->activeInfo);
1,338,944✔
1528
  pTarget->activeInfo = NULL;
1,338,944✔
1529

1530
  taosMemoryFree(pTarget->key);
1,338,944✔
1531
  taosMemoryFree(pTarget);
1,338,944✔
1532
}
1,338,944✔
1533

1534
void hbRemoveAppHbMrg(SAppHbMgr **pAppHbMgr) {
1,338,944✔
1535
  int32_t code = TSDB_CODE_SUCCESS;
1,338,944✔
1536
  code = taosThreadMutexLock(&clientHbMgr.lock);
1,338,944✔
1537
  if (TSDB_CODE_SUCCESS != code) {
1,338,944✔
1538
    tscError("failed to lock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1539
  }
1540
  int32_t mgrSize = taosArrayGetSize(clientHbMgr.appHbMgrs);
1,338,944✔
1541
  for (int32_t i = 0; i < mgrSize; ++i) {
1,338,944✔
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);
1,338,944✔
1551
  if (TSDB_CODE_SUCCESS != code) {
1,338,944✔
1552
    tscError("failed to unlock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1553
  }
1554
}
1,338,944✔
1555

1556
void appHbMgrCleanup(void) {
1,272,943✔
1557
  int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
1,272,943✔
1558
  for (int i = 0; i < sz; i++) {
2,611,887✔
1559
    SAppHbMgr *pTarget = taosArrayGetP(clientHbMgr.appHbMgrs, i);
1,338,944✔
1560
    if (pTarget == NULL) continue;
1,338,944✔
1561
    hbFreeAppHbMgr(pTarget);
1,338,944✔
1562
  }
1563
}
1,272,943✔
1564

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

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

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

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

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

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

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

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

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

1612
  return 0;
1,272,943✔
1613
}
1614

1615
void hbMgrCleanUp() {
1,274,873✔
1616
  hbStopThread();
1,274,873✔
1617

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

1622
  int32_t code = taosThreadMutexLock(&clientHbMgr.lock);
1,272,943✔
1623
  if (TSDB_CODE_SUCCESS != code) {
1,272,943✔
1624
    tscError("failed to lock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1625
  }
1626
  appHbMgrCleanup();
1,272,943✔
1627
  taosArrayDestroy(clientHbMgr.appHbMgrs);
1,272,943✔
1628
  clientHbMgr.appHbMgrs = NULL;
1,272,943✔
1629
  code = taosThreadMutexUnlock(&clientHbMgr.lock);
1,272,943✔
1630
  if (TSDB_CODE_SUCCESS != code) {
1,272,943✔
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) {
3,032,657✔
1636
  // init hash in activeinfo
1637
  void *data = taosHashGet(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
3,032,657✔
1638
  if (data != NULL) {
3,032,657✔
1639
    return 0;
×
1640
  }
1641
  SClientHbReq hbReq = {0};
3,032,657✔
1642
  hbReq.connKey = connKey;
3,032,657✔
1643
  hbReq.clusterId = clusterId;
3,032,657✔
1644
  // hbReq.info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
1645

1646
  TSC_ERR_RET(taosHashPut(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey), &hbReq, sizeof(SClientHbReq)));
3,032,657✔
1647

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

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

1658
  switch (connType) {
3,111,091✔
1659
    case CONN_TYPE__QUERY: {
3,032,657✔
1660
      return hbRegisterConnImpl(pAppHbMgr, connKey, clusterId);
3,032,657✔
1661
    }
1662
    case CONN_TYPE__TMQ: {
78,434✔
1663
      return 0;
78,434✔
1664
    }
1665
    default:
×
1666
      return 0;
×
1667
  }
1668
}
1669

1670
void hbDeregisterConn(STscObj *pTscObj, SClientHbKey connKey) {
2,901,079✔
1671
  int32_t code = taosThreadMutexLock(&clientHbMgr.lock);
2,901,079✔
1672
  if (TSDB_CODE_SUCCESS != code) {
2,901,658✔
1673
    tscError("failed to lock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1674
  }
1675
  SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, pTscObj->appHbMgrIdx);
2,901,658✔
1676
  if (pAppHbMgr) {
2,901,658✔
1677
    SClientHbReq *pReq = taosHashAcquire(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
2,901,658✔
1678
    if (pReq) {
2,901,658✔
1679
      tFreeClientHbReq(pReq);
1680
      code = taosHashRemove(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
2,802,696✔
1681
      if (TSDB_CODE_SUCCESS != code) {
2,802,696✔
1682
        tscError("hbDeregisterConn taosHashRemove error, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1683
      }
1684
      taosHashRelease(pAppHbMgr->activeInfo, pReq);
2,802,696✔
1685
      (void)atomic_sub_fetch_32(&pAppHbMgr->connKeyCnt, 1);
2,802,696✔
1686
    }
1687
  }
1688
  code = taosThreadMutexUnlock(&clientHbMgr.lock);
2,901,658✔
1689
  if (TSDB_CODE_SUCCESS != code) {
2,901,658✔
1690
    tscError("failed to unlock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1691
  }
1692
}
2,901,658✔
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; }
767,844✔
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