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

taosdata / TDengine / #4875

09 Dec 2025 01:22AM UTC coverage: 64.472% (-0.2%) from 64.623%
#4875

push

travis-ci

guanshengliang
fix: temporarily disable memory leak detection for UDF tests (#33856)

162014 of 251293 relevant lines covered (64.47%)

104318075.66 hits per line

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

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

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

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

63
    TSC_ERR_JRET(catalogUpdateUserAuthInfo(pCatalog, rsp));
852,124✔
64
  }
65

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

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

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

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

85
    SClientHbReq    *pReq = NULL;
1,378,075✔
86
    SGetUserAuthRsp *pRsp = NULL;
1,378,075✔
87
    while ((pReq = taosHashIterate(hbMgr->activeInfo, pReq))) {
2,570,221✔
88
      STscObj *pTscObj = (STscObj *)acquireTscObj(pReq->connKey.tscRid);
1,407,136✔
89
      if (!pTscObj) {
1,407,136✔
90
        continue;
100,301✔
91
      }
92

93
      if (!pRsp) {
1,306,835✔
94
        for (int32_t j = 0; j < TARRAY_SIZE(batchRsp->pArray); ++j) {
1,346,484✔
95
          SGetUserAuthRsp *rsp = TARRAY_GET_ELEM(batchRsp->pArray, j);
1,131,494✔
96
          if (0 == strncmp(rsp->user, pTscObj->user, TSDB_USER_LEN)) {
1,131,494✔
97
            pRsp = rsp;
882,486✔
98
            break;
882,486✔
99
          }
100
        }
101
        if (!pRsp) {
1,097,476✔
102
          releaseTscObj(pReq->connKey.tscRid);
214,990✔
103
          taosHashCancelIterate(hbMgr->activeInfo, pReq);
214,990✔
104
          break;
214,990✔
105
        }
106
      }
107

108
      if (pRsp->dropped == 1) {
1,091,845✔
109
        if (atomic_val_compare_exchange_8(&pTscObj->dropped, 0, 1) == 0) {
17,013✔
110
          if (pTscObj->userDroppedInfo.fp) {
13,924✔
111
            SPassInfo *dropInfo = &pTscObj->userDroppedInfo;
12,350✔
112
            if (dropInfo->fp) {
12,350✔
113
              (*dropInfo->fp)(dropInfo->param, NULL, TAOS_NOTIFY_USER_DROPPED);
12,350✔
114
            }
115
          }
116
        }
117
        releaseTscObj(pReq->connKey.tscRid);
17,013✔
118
        continue;
17,013✔
119
      }
120

121
      pTscObj->authVer = pRsp->version;
1,074,832✔
122

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

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

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

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

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

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

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

222
  *pInfo = vgInfo;
1,289,624✔
223
  return code;
1,289,624✔
224
}
225

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

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

236
  int32_t numOfBatchs = taosArrayGetSize(batchRsp.pArray);
4,495,446✔
237
  for (int32_t i = 0; i < numOfBatchs; ++i) {
5,862,869✔
238
    SDbHbRsp *rsp = taosArrayGet(batchRsp.pArray, i);
1,367,423✔
239
    if (NULL == rsp) {
1,367,423✔
240
      code = terrno;
×
241
      goto _return;
×
242
    }
243
    if (rsp->useDbRsp) {
1,367,423✔
244
      tscDebug("hb use db rsp, db:%s, vgVersion:%d, stateTs:%" PRId64 ", uid:%" PRIx64, rsp->useDbRsp->db,
1,086,394✔
245
               rsp->useDbRsp->vgVersion, rsp->useDbRsp->stateTs, rsp->useDbRsp->uid);
246

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

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

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

261
        if (IS_SYS_DBNAME(rsp->useDbRsp->db)) {
1,055,755✔
262
          code = hbGenerateVgInfoFromRsp(&vgInfo, rsp->useDbRsp);
233,869✔
263
          if (TSDB_CODE_SUCCESS != code) {
233,869✔
264
            goto _return;
×
265
          }
266

267
          TSC_ERR_JRET(catalogUpdateDBVgInfo(
233,869✔
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,367,423✔
275
      tscDebug("hb db cfg rsp, db:%s, cfgVersion:%d", rsp->cfgRsp->db, rsp->cfgRsp->cfgVersion);
206,353✔
276
      code = catalogUpdateDbCfg(pCatalog, rsp->cfgRsp->db, rsp->cfgRsp->dbId, rsp->cfgRsp);
206,353✔
277
      rsp->cfgRsp = NULL;
206,353✔
278
    }
279
    if (rsp->pTsmaRsp) {
1,367,423✔
280
      if (rsp->pTsmaRsp->pTsmas) {
689,756✔
281
        for (int32_t i = 0; i < rsp->pTsmaRsp->pTsmas->size; ++i) {
2,832✔
282
          STableTSMAInfo *pTsma = taosArrayGetP(rsp->pTsmaRsp->pTsmas, i);
1,416✔
283
          if (NULL == pTsma) {
1,416✔
284
            TSC_ERR_JRET(TSDB_CODE_OUT_OF_RANGE);
×
285
          }
286
          TSC_ERR_JRET(catalogAsyncUpdateTSMA(pCatalog, &pTsma, rsp->dbTsmaVersion));
1,416✔
287
        }
288
        taosArrayClear(rsp->pTsmaRsp->pTsmas);
1,416✔
289
      } else {
290
        TSC_ERR_JRET(catalogAsyncUpdateDbTsmaVersion(pCatalog, rsp->dbTsmaVersion, rsp->db, rsp->dbId));
688,340✔
291
      }
292
    }
293
  }
294

295
_return:
4,495,446✔
296

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

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

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

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

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

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

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

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

373
  int32_t numOfMeta = taosArrayGetSize(hbRsp.pViewRsp);
4,145✔
374
  for (int32_t i = 0; i < numOfMeta; ++i) {
4,145✔
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:
4,145✔
393
  taosArrayDestroy(hbRsp.pViewRsp);
4,145✔
394
  return code;
4,145✔
395
}
396

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

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

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

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

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

516
  if (pRsp->query) {
21,814,539✔
517
    STscObj *pTscObj = (STscObj *)acquireTscObj(pRsp->connKey.tscRid);
19,885,025✔
518
    if (NULL == pTscObj) {
19,885,025✔
519
      tscDebug("tscObj rid %" PRIx64 " not exist", pRsp->connKey.tscRid);
897✔
520
    } else {
521
      if (pRsp->query->totalDnodes > 1) {
19,884,128✔
522
        SEpSet  originEpset = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
6,126,167✔
523
        if (!isEpsetEqual(&originEpset, &pRsp->query->epSet)) {
6,126,167✔
524
          SEpSet *pOrig = &originEpset;
57,621✔
525
          SEp    *pOrigEp = &pOrig->eps[pOrig->inUse];
57,621✔
526
          SEp    *pNewEp = &pRsp->query->epSet.eps[pRsp->query->epSet.inUse];
57,621✔
527
          tscDebug("mnode epset updated from %d/%d=>%s:%d to %d/%d=>%s:%d in hb", pOrig->inUse, pOrig->numOfEps,
57,621✔
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);
57,621✔
532
        }
533
      }
534

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

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

556
      if (pRsp->query->pQnodeList) {
19,884,128✔
557
        if (TSDB_CODE_SUCCESS != updateQnodeList(pTscObj->pAppInfo, pRsp->query->pQnodeList)) {
90,794✔
558
          tscWarn("update qnode list failed");
×
559
        }
560
      }
561

562
      releaseTscObj(pRsp->connKey.tscRid);
19,884,128✔
563
    }
564
  }
565

566
  int32_t kvNum = pRsp->info ? taosArrayGetSize(pRsp->info) : 0;
21,814,539✔
567

568
  tscDebug("hb got %d rsp kv", kvNum);
21,814,539✔
569

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

580
  taosHashRelease(pAppHbMgr->activeInfo, pReq);
21,814,539✔
581

582
  return TSDB_CODE_SUCCESS;
21,814,539✔
583
}
584

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

590
  static int32_t    emptyRspNum = 0;
591
  int32_t           idx = *(int32_t *)param;
18,584,451✔
592
  SClientHbBatchRsp pRsp = {0};
18,584,451✔
593
  if (TSDB_CODE_SUCCESS == code) {
18,584,449✔
594
    code = tDeserializeSClientHbBatchRsp(pMsg->pData, pMsg->len, &pRsp);
18,117,388✔
595
    if (TSDB_CODE_SUCCESS != code) {
18,115,918✔
596
      tscError("deserialize hb rsp failed");
1,220✔
597
    }
598
    int32_t now = taosGetTimestampSec();
18,115,918✔
599
    int32_t delta = abs(now - pRsp.svrTimestamp);
18,115,192✔
600
    if (delta > timestampDeltaLimit) {
18,115,192✔
601
      code = TSDB_CODE_TIME_UNSYNCED;
1,220✔
602
      tscError("time diff:%ds is too big", delta);
1,220✔
603
    }
604
  }
605

606
  int32_t rspNum = taosArrayGetSize(pRsp.rsps);
18,582,253✔
607

608
  (void)taosThreadMutexLock(&clientHbMgr.lock);
18,581,831✔
609

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

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

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

639
  if (rspNum) {
18,116,233✔
640
    tscDebug("hb got %d rsp, %d empty rsp received before", rspNum,
17,584,948✔
641
             atomic_val_compare_exchange_32(&emptyRspNum, emptyRspNum, 0));
642
  } else {
643
    (void)atomic_add_fetch_32(&emptyRspNum, 1);
531,285✔
644
  }
645

646
  for (int32_t i = 0; i < rspNum; ++i) {
39,942,058✔
647
    SClientHbRsp *rsp = taosArrayGet(pRsp.rsps, i);
21,825,825✔
648
    code = (*clientHbMgr.rspHandle[rsp->connKey.connType])(pAppHbMgr, rsp);
21,825,825✔
649
    if (code) {
21,825,825✔
650
      break;
×
651
    }
652
  }
653

654
  (void)taosThreadMutexUnlock(&clientHbMgr.lock);
18,116,233✔
655

656
  tFreeClientHbBatchRsp(&pRsp);
657

658
_return:
18,116,233✔
659
  taosMemoryFree(pMsg->pData);
18,116,233✔
660
  taosMemoryFree(pMsg->pEpSet);
18,116,233✔
661
  return code;
18,116,233✔
662
}
663

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

669
  void *pIter = taosHashIterate(pObj->pRequests, NULL);
20,409,413✔
670
  while (pIter != NULL) {
41,336,655✔
671
    int64_t     *rid = pIter;
20,927,242✔
672
    SRequestObj *pRequest = acquireRequest(*rid);
20,927,242✔
673
    if (NULL == pRequest) {
20,927,242✔
674
      pIter = taosHashIterate(pObj->pRequests, pIter);
9,941✔
675
      continue;
9,941✔
676
    }
677

678
    if (pRequest->killed || 0 == pRequest->body.queryJob) {
20,917,301✔
679
      (void)releaseRequest(*rid);
9,461,803✔
680
      pIter = taosHashIterate(pObj->pRequests, pIter);
9,461,803✔
681
      continue;
9,461,803✔
682
    }
683

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

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

706
      code = schedulerGetTasksStatus(pRequest->body.queryJob, desc.subDesc);
11,455,498✔
707
      if (code) {
11,455,498✔
708
        taosArrayDestroy(desc.subDesc);
2,043,002✔
709
        desc.subDesc = NULL;
2,043,002✔
710
      }
711
      desc.subPlanNum = taosArrayGetSize(desc.subDesc);
11,455,498✔
712
    } else {
713
      desc.subDesc = NULL;
×
714
    }
715

716
    (void)releaseRequest(*rid);
11,455,498✔
717
    if (NULL == taosArrayPush(hbBasic->queryDesc, &desc)) {
22,910,996✔
718
      taosArrayDestroy(desc.subDesc);
×
719
      return terrno;
×
720
    }
721

722
    pIter = taosHashIterate(pObj->pRequests, pIter);
11,455,498✔
723
  }
724

725
  return code;
20,409,413✔
726
}
727

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

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

742
  hbBasic->connId = pTscObj->connId;
22,303,203✔
743

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

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

770
  req->query = hbBasic;
18,445,052✔
771
  releaseTscObj(connKey->tscRid);
18,445,052✔
772

773
  return TSDB_CODE_SUCCESS;
18,445,052✔
774
}
775

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

783
  int32_t code = 0;
805,199✔
784

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

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

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

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

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

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

846
  return code;
805,199✔
847
}
848

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

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

859
  if (userNum <= 0) {
15,223,693✔
860
    taosMemoryFree(users);
642,497✔
861
    return TSDB_CODE_SUCCESS;
642,497✔
862
  }
863

864
  for (int32_t i = 0; i < userNum; ++i) {
29,527,747✔
865
    SUserAuthVersion *user = &users[i];
14,946,551✔
866
    user->version = htonl(user->version);
14,946,551✔
867
  }
868

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

875
  tscDebug("hb got %d expired users, valueLen:%d", userNum, kv.valueLen);
14,581,196✔
876

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

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

891
  return TSDB_CODE_SUCCESS;
14,581,196✔
892
}
893

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

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

904
  if (dbNum <= 0) {
16,197,268✔
905
    taosMemoryFree(dbs);
11,677,286✔
906
    return TSDB_CODE_SUCCESS;
11,677,286✔
907
  }
908

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

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

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

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

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

945
  return TSDB_CODE_SUCCESS;
4,519,982✔
946
}
947

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

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

958
  if (stbNum <= 0) {
16,197,268✔
959
    taosMemoryFree(stbs);
13,162,600✔
960
    return TSDB_CODE_SUCCESS;
13,162,600✔
961
  }
962

963
  for (int32_t i = 0; i < stbNum; ++i) {
8,664,262✔
964
    SSTableVersion *stb = &stbs[i];
5,629,594✔
965
    stb->suid = htobe64(stb->suid);
5,629,594✔
966
    stb->sversion = htonl(stb->sversion);
5,629,594✔
967
    stb->tversion = htonl(stb->tversion);
5,629,594✔
968
    stb->smaVer = htonl(stb->smaVer);
5,629,594✔
969
  }
970

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

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

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

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

993
  return TSDB_CODE_SUCCESS;
3,034,668✔
994
}
995

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

1002
  TSC_ERR_JRET(catalogGetExpiredViews(pCatalog, &views, &viewNum, &pDynViewVer));
16,197,268✔
1003

1004
  if (viewNum <= 0) {
16,197,268✔
1005
    taosMemoryFree(views);
16,186,775✔
1006
    taosMemoryFree(pDynViewVer);
16,186,775✔
1007
    return TSDB_CODE_SUCCESS;
16,186,775✔
1008
  }
1009

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

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

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

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

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

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

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

1057
  if (tsmaNum <= 0) {
16,197,268✔
1058
    taosMemoryFree(tsmas);
16,105,936✔
1059
    return TSDB_CODE_SUCCESS;
16,105,936✔
1060
  }
1061

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

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

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

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

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

1107
  code = hbGetQueryBasicInfo(connKey, req);
22,303,203✔
1108
  if (code != TSDB_CODE_SUCCESS) {
22,303,203✔
1109
    tscWarn("hbGetQueryBasicInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
1,964,361✔
1110
    return code;
1,964,361✔
1111
  }
1112

1113
  if (hbParam->reqCnt == 0) {
20,338,842✔
1114
    code = catalogGetHandle(hbParam->clusterId, &pCatalog);
16,197,462✔
1115
    if (code != TSDB_CODE_SUCCESS) {
16,197,462✔
1116
      tscWarn("catalogGetHandle failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1117
      return code;
×
1118
    }
1119

1120
    code = hbGetAppInfo(hbParam->clusterId, req);
16,197,462✔
1121
    if (TSDB_CODE_SUCCESS != code) {
16,197,462✔
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))) {
16,197,462✔
1127
      code = hbGetExpiredUserInfo(connKey, pCatalog, req);
15,223,693✔
1128
      if (TSDB_CODE_SUCCESS != code) {
15,223,693✔
1129
        tscWarn("hbGetExpiredUserInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1130
        return code;
×
1131
      }
1132
      if (clientHbMgr.appHbHash) {
15,223,693✔
1133
        code = taosHashPut(clientHbMgr.appHbHash, &hbParam->clusterId, sizeof(uint64_t), NULL, 0);
1,913,300✔
1134
        if (TSDB_CODE_SUCCESS != code) {
1,913,300✔
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)) {
16,197,462✔
1144
      code = hbGetUserAuthInfo(connKey, hbParam, req);
805,393✔
1145
      if (TSDB_CODE_SUCCESS != code) {
805,393✔
1146
        tscWarn("hbGetUserAuthInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
194✔
1147
        return code;
194✔
1148
      }
1149
      atomic_store_8(&hbParam->pAppHbMgr->connHbFlag, 1);
805,199✔
1150
    }
1151

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

1158
    code = hbGetExpiredStbInfo(connKey, pCatalog, req);
16,197,268✔
1159
    if (TSDB_CODE_SUCCESS != code) {
16,197,268✔
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);
16,197,268✔
1166
    if (TSDB_CODE_SUCCESS != code) {
16,197,268✔
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);
16,197,268✔
1172
    if (TSDB_CODE_SUCCESS != code) {
16,197,268✔
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,141,380✔
1178
    if (TSDB_CODE_SUCCESS != code) {
4,141,380✔
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
20,338,648✔
1185

1186
  return TSDB_CODE_SUCCESS;
20,338,648✔
1187
}
1188

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

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

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

1210
  int64_t  maxIpWhiteVer = 0;
18,593,981✔
1211
  void    *pIter = NULL;
18,593,981✔
1212
  SHbParam param = {0};
18,593,981✔
1213
  while ((pIter = taosHashIterate(pAppHbMgr->activeInfo, pIter))) {
42,324,553✔
1214
    SClientHbReq *pOneReq = pIter;
23,730,572✔
1215
    SClientHbKey *connKey = &pOneReq->connKey;
23,730,572✔
1216
    STscObj      *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
23,730,572✔
1217

1218
    if (!pTscObj || atomic_load_8(&pTscObj->dropped) == 1) {
23,730,572✔
1219
      if (pTscObj) releaseTscObj(connKey->tscRid);
1,427,369✔
1220
      continue;
1,427,369✔
1221
    }
1222

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

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

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

1256
    int64_t ver = atomic_load_64(&pTscObj->whiteListInfo.ver);
22,303,203✔
1257
    maxIpWhiteVer = TMAX(maxIpWhiteVer, ver);
22,303,203✔
1258
    releaseTscObj(connKey->tscRid);
22,303,203✔
1259
  }
1260
  (*pBatchReq)->ipWhiteListVer = maxIpWhiteVer;
18,593,981✔
1261

1262
  return TSDB_CODE_SUCCESS;
18,593,981✔
1263
}
1264

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

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

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

1289
  taosHashClear(clientHbMgr.appSummary);
18,464,535✔
1290

1291
  for (int32_t i = 0; i < sz; ++i) {
39,904,061✔
1292
    SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
21,439,526✔
1293
    if (pAppHbMgr == NULL) continue;
21,439,526✔
1294

1295
    int64_t    clusterId = pAppHbMgr->pAppInstInfo->clusterId;
21,439,526✔
1296
    SAppHbReq *pApp = taosHashGet(clientHbMgr.appSummary, &clusterId, sizeof(clusterId));
21,439,526✔
1297
    if (NULL == pApp) {
21,439,526✔
1298
      (void)memcpy(&req.summary, &pAppHbMgr->pAppInstInfo->summary, sizeof(req.summary));
18,755,061✔
1299
      req.startTime = pAppHbMgr->startTime;
18,755,061✔
1300
      TSC_ERR_RET(taosHashPut(clientHbMgr.appSummary, &clusterId, sizeof(clusterId), &req, sizeof(req)));
18,755,061✔
1301
    } else {
1302
      if (pAppHbMgr->startTime < pApp->startTime) {
2,684,465✔
1303
        pApp->startTime = pAppHbMgr->startTime;
×
1304
      }
1305

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

1310
  return TSDB_CODE_SUCCESS;
18,464,535✔
1311
}
1312

1313
static void *hbThreadFunc(void *param) {
1,272,380✔
1314
  setThreadName("hb");
1,272,380✔
1315
#ifdef WINDOWS
1316
  if (taosCheckCurrentInDll()) {
1317
    atexit(hbThreadFuncUnexpectedStopped);
1318
  }
1319
#endif
1320
  while (1) {
17,720,067✔
1321
    if (1 == clientHbMgr.threadStop) {
18,992,447✔
1322
      break;
518,804✔
1323
    }
1324

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

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

1346
    for (int i = 0; i < sz; i++) {
39,913,169✔
1347
      SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
21,439,526✔
1348
      if (pAppHbMgr == NULL) {
21,439,526✔
1349
        continue;
875,296✔
1350
      }
1351

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

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

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

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

1415
    if (TSDB_CODE_SUCCESS != taosThreadMutexUnlock(&clientHbMgr.lock)) {
18,473,643✔
1416
      tscError("taosThreadMutexLock failed");
×
1417
      return NULL;
×
1418
    }
1419
    taosMsleep(HEARTBEAT_INTERVAL);
18,473,643✔
1420
  }
1421
  taosHashCleanup(clientHbMgr.appHbHash);
518,804✔
1422
  return NULL;
518,804✔
1423
}
1424

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

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

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

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

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

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

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

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

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

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

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

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

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

1520
void hbFreeAppHbMgr(SAppHbMgr *pTarget) {
1,338,853✔
1521
  void *pIter = taosHashIterate(pTarget->activeInfo, NULL);
1,338,853✔
1522
  while (pIter != NULL) {
1,571,748✔
1523
    SClientHbReq *pOneReq = pIter;
232,895✔
1524
    tFreeClientHbReq(pOneReq);
1525
    pIter = taosHashIterate(pTarget->activeInfo, pIter);
232,895✔
1526
  }
1527
  taosHashCleanup(pTarget->activeInfo);
1,338,853✔
1528
  pTarget->activeInfo = NULL;
1,338,853✔
1529

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

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

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

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

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

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

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

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

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

1598
  ret = taosThreadMutexAttrDestroy(&attr);
1,272,380✔
1599
  if (ret != 0) {
1,272,380✔
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,380✔
1608
  if (ret != 0) {
1,272,380✔
1609
    uError("hbMgrInit:hbCreateThread error") return ret;
×
1610
  }
1611

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

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

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

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

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

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

1658
  switch (connType) {
3,130,097✔
1659
    case CONN_TYPE__QUERY: {
3,049,428✔
1660
      return hbRegisterConnImpl(pAppHbMgr, connKey, clusterId);
3,049,428✔
1661
    }
1662
    case CONN_TYPE__TMQ: {
80,669✔
1663
      return 0;
80,669✔
1664
    }
1665
    default:
×
1666
      return 0;
×
1667
  }
1668
}
1669

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