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

taosdata / TDengine / #4867

26 Nov 2025 05:46AM UTC coverage: 64.473% (-0.03%) from 64.504%
#4867

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

765 of 945 new or added lines in 33 files covered. (80.95%)

3147 existing lines in 126 files now uncovered.

158042 of 245129 relevant lines covered (64.47%)

111495961.54 hits per line

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

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

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

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

35
SClientHbMgr clientHbMgr = {0};
36

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

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

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

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

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

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

63
    TSC_ERR_JRET(catalogUpdateUserAuthInfo(pCatalog, rsp));
933,911✔
64
  }
65

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

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

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

77
static int32_t hbUpdateUserAuthInfo(SAppHbMgr *pAppHbMgr, SUserAuthBatchRsp *batchRsp) {
915,454✔
78
  int64_t clusterId = pAppHbMgr->pAppInstInfo->clusterId;
915,454✔
79
  for (int i = 0; i < TARRAY_SIZE(clientHbMgr.appHbMgrs); ++i) {
2,460,728✔
80
    SAppHbMgr *hbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
1,545,274✔
81
    if (!hbMgr || hbMgr->pAppInstInfo->clusterId != clusterId) {
1,545,274✔
82
      continue;
35,215✔
83
    }
84

85
    SClientHbReq    *pReq = NULL;
1,510,059✔
86
    SGetUserAuthRsp *pRsp = NULL;
1,510,059✔
87
    while ((pReq = taosHashIterate(hbMgr->activeInfo, pReq))) {
2,809,039✔
88
      STscObj *pTscObj = (STscObj *)acquireTscObj(pReq->connKey.tscRid);
1,530,970✔
89
      if (!pTscObj) {
1,530,970✔
90
        continue;
119,430✔
91
      }
92

93
      if (!pRsp) {
1,411,540✔
94
        for (int32_t j = 0; j < TARRAY_SIZE(batchRsp->pArray); ++j) {
1,448,958✔
95
          SGetUserAuthRsp *rsp = TARRAY_GET_ELEM(batchRsp->pArray, j);
1,216,968✔
96
          if (0 == strncmp(rsp->user, pTscObj->user, TSDB_USER_LEN)) {
1,216,968✔
97
            pRsp = rsp;
952,244✔
98
            break;
952,244✔
99
          }
100
        }
101
        if (!pRsp) {
1,184,234✔
102
          releaseTscObj(pReq->connKey.tscRid);
231,990✔
103
          taosHashCancelIterate(hbMgr->activeInfo, pReq);
231,990✔
104
          break;
231,990✔
105
        }
106
      }
107

108
      if (pRsp->dropped == 1) {
1,179,550✔
109
        if (atomic_val_compare_exchange_8(&pTscObj->dropped, 0, 1) == 0) {
16,804✔
110
          if (pTscObj->userDroppedInfo.fp) {
14,027✔
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);
16,804✔
118
        continue;
16,804✔
119
      }
120

121
      pTscObj->authVer = pRsp->version;
1,162,746✔
122

123
      if (pTscObj->sysInfo != pRsp->sysInfo) {
1,162,746✔
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,162,746✔
130
        SPassInfo *passInfo = &pTscObj->passInfo;
6,186✔
131
        int32_t    oldVer = atomic_load_32(&passInfo->ver);
6,186✔
132
        if (oldVer < pRsp->passVer) {
6,186✔
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,162,746✔
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,162,746✔
157
        int64_t         oldVer = atomic_load_64(&whiteListInfo->ver);
1,162,746✔
158
        atomic_store_64(&whiteListInfo->ver, pRsp->whiteListVer);
1,162,746✔
159
        tscDebug("update whitelist version of user %s from %" PRId64 " to %" PRId64 ", conn:%" PRIi64, pRsp->user,
1,162,746✔
160
                 oldVer, atomic_load_64(&whiteListInfo->ver), pTscObj->id);
161
      }
162
      releaseTscObj(pReq->connKey.tscRid);
1,162,746✔
163
    }
164
  }
165
  return 0;
915,454✔
166
}
167

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

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

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

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

203
  *pInfo = vgInfo;
1,440,987✔
204
  return code;
1,440,987✔
205
}
206

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

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

217
  int32_t numOfBatchs = taosArrayGetSize(batchRsp.pArray);
5,628,036✔
218
  for (int32_t i = 0; i < numOfBatchs; ++i) {
7,247,538✔
219
    SDbHbRsp *rsp = taosArrayGet(batchRsp.pArray, i);
1,619,502✔
220
    if (NULL == rsp) {
1,619,502✔
221
      code = terrno;
×
222
      goto _return;
×
223
    }
224
    if (rsp->useDbRsp) {
1,619,502✔
225
      tscDebug("hb use db rsp, db:%s, vgVersion:%d, stateTs:%" PRId64 ", uid:%" PRIx64, rsp->useDbRsp->db,
1,214,788✔
226
               rsp->useDbRsp->vgVersion, rsp->useDbRsp->stateTs, rsp->useDbRsp->uid);
227

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

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

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

242
        if (IS_SYS_DBNAME(rsp->useDbRsp->db)) {
1,176,505✔
243
          code = hbGenerateVgInfoFromRsp(&vgInfo, rsp->useDbRsp);
264,482✔
244
          if (TSDB_CODE_SUCCESS != code) {
264,482✔
245
            goto _return;
×
246
          }
247

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

255
    if (rsp->cfgRsp) {
1,619,502✔
256
      tscDebug("hb db cfg rsp, db:%s, cfgVersion:%d", rsp->cfgRsp->db, rsp->cfgRsp->cfgVersion);
320,484✔
257
      code = catalogUpdateDbCfg(pCatalog, rsp->cfgRsp->db, rsp->cfgRsp->dbId, rsp->cfgRsp);
320,484✔
258
      rsp->cfgRsp = NULL;
320,484✔
259
    }
260
    if (rsp->pTsmaRsp) {
1,619,502✔
261
      if (rsp->pTsmaRsp->pTsmas) {
839,559✔
262
        for (int32_t i = 0; i < rsp->pTsmaRsp->pTsmas->size; ++i) {
2,860✔
263
          STableTSMAInfo *pTsma = taosArrayGetP(rsp->pTsmaRsp->pTsmas, i);
1,430✔
264
          if (NULL == pTsma) {
1,430✔
265
            TSC_ERR_JRET(TSDB_CODE_OUT_OF_RANGE);
×
266
          }
267
          TSC_ERR_JRET(catalogAsyncUpdateTSMA(pCatalog, &pTsma, rsp->dbTsmaVersion));
1,430✔
268
        }
269
        taosArrayClear(rsp->pTsmaRsp->pTsmas);
1,430✔
270
      } else {
271
        TSC_ERR_JRET(catalogAsyncUpdateDbTsmaVersion(pCatalog, rsp->dbTsmaVersion, rsp->db, rsp->dbId));
838,129✔
272
      }
273
    }
274
  }
275

276
_return:
5,628,036✔
277

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

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

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

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

309
      TSC_ERR_JRET(catalogAsyncUpdateTableMeta(pCatalog, rsp));
30,134✔
310
    }
311
  }
312

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

497
  if (pRsp->query) {
26,764,296✔
498
    STscObj *pTscObj = (STscObj *)acquireTscObj(pRsp->connKey.tscRid);
23,328,227✔
499
    if (NULL == pTscObj) {
23,328,227✔
500
      tscDebug("tscObj rid %" PRIx64 " not exist", pRsp->connKey.tscRid);
1,231✔
501
    } else {
502
      if (pRsp->query->totalDnodes > 1) {
23,326,996✔
503
        SEpSet  originEpset = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
6,838,778✔
504
        if (!isEpsetEqual(&originEpset, &pRsp->query->epSet)) {
6,838,778✔
505
          SEpSet *pOrig = &originEpset;
57,007✔
506
          SEp    *pOrigEp = &pOrig->eps[pOrig->inUse];
57,007✔
507
          SEp    *pNewEp = &pRsp->query->epSet.eps[pRsp->query->epSet.inUse];
57,007✔
508
          tscDebug("mnode epset updated from %d/%d=>%s:%d to %d/%d=>%s:%d in hb", pOrig->inUse, pOrig->numOfEps,
57,007✔
509
                   pOrigEp->fqdn, pOrigEp->port, pRsp->query->epSet.inUse, pRsp->query->epSet.numOfEps, pNewEp->fqdn,
510
                   pNewEp->port);
511

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

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

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

533
      if (pRsp->query->killConnection) {
23,326,996✔
534
        taos_close_internal(pTscObj);
×
535
      }
536

537
      if (pRsp->query->pQnodeList) {
23,326,996✔
538
        if (TSDB_CODE_SUCCESS != updateQnodeList(pTscObj->pAppInfo, pRsp->query->pQnodeList)) {
88,614✔
539
          tscWarn("update qnode list failed");
×
540
        }
541
      }
542

543
      releaseTscObj(pRsp->connKey.tscRid);
23,326,996✔
544
    }
545
  }
546

547
  int32_t kvNum = pRsp->info ? taosArrayGetSize(pRsp->info) : 0;
26,764,296✔
548

549
  tscDebug("hb got %d rsp kv", kvNum);
26,764,296✔
550

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

561
  taosHashRelease(pAppHbMgr->activeInfo, pReq);
26,764,296✔
562

563
  return TSDB_CODE_SUCCESS;
26,764,296✔
564
}
565

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

571
  static int32_t    emptyRspNum = 0;
572
  int32_t           idx = *(int32_t *)param;
23,772,289✔
573
  SClientHbBatchRsp pRsp = {0};
23,772,289✔
574
  if (TSDB_CODE_SUCCESS == code) {
23,772,289✔
575
    code = tDeserializeSClientHbBatchRsp(pMsg->pData, pMsg->len, &pRsp);
23,174,064✔
576
    if (TSDB_CODE_SUCCESS != code) {
23,172,965✔
577
      tscError("deserialize hb rsp failed");
960✔
578
    }
579
    int32_t now = taosGetTimestampSec();
23,172,965✔
580
    int32_t delta = abs(now - pRsp.svrTimestamp);
23,171,544✔
581
    if (delta > timestampDeltaLimit) {
23,171,544✔
582
      code = TSDB_CODE_TIME_UNSYNCED;
960✔
583
      tscError("time diff:%ds is too big", delta);
960✔
584
    }
585
  }
586

587
  int32_t rspNum = taosArrayGetSize(pRsp.rsps);
23,769,769✔
588

589
  (void)taosThreadMutexLock(&clientHbMgr.lock);
23,771,756✔
590

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

601
  SAppInstInfo *pInst = pAppHbMgr->pAppInstInfo;
23,772,407✔
602

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

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

620
  if (rspNum) {
23,173,218✔
621
    tscDebug("hb got %d rsp, %d empty rsp received before", rspNum,
22,614,803✔
622
             atomic_val_compare_exchange_32(&emptyRspNum, emptyRspNum, 0));
623
  } else {
624
    (void)atomic_add_fetch_32(&emptyRspNum, 1);
558,415✔
625
  }
626

627
  for (int32_t i = 0; i < rspNum; ++i) {
49,951,482✔
628
    SClientHbRsp *rsp = taosArrayGet(pRsp.rsps, i);
26,778,264✔
629
    code = (*clientHbMgr.rspHandle[rsp->connKey.connType])(pAppHbMgr, rsp);
26,778,264✔
630
    if (code) {
26,778,264✔
631
      break;
×
632
    }
633
  }
634

635
  (void)taosThreadMutexUnlock(&clientHbMgr.lock);
23,173,218✔
636

637
  tFreeClientHbBatchRsp(&pRsp);
638

639
_return:
23,173,218✔
640
  taosMemoryFree(pMsg->pData);
23,173,218✔
641
  taosMemoryFree(pMsg->pEpSet);
23,173,218✔
642
  return code;
23,173,218✔
643
}
644

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

650
  void *pIter = taosHashIterate(pObj->pRequests, NULL);
25,448,017✔
651
  while (pIter != NULL) {
51,443,441✔
652
    int64_t     *rid = pIter;
25,995,424✔
653
    SRequestObj *pRequest = acquireRequest(*rid);
25,995,424✔
654
    if (NULL == pRequest) {
25,995,424✔
655
      pIter = taosHashIterate(pObj->pRequests, pIter);
13,135✔
656
      continue;
13,135✔
657
    }
658

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

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

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

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

697
    (void)releaseRequest(*rid);
15,786,963✔
698
    if (NULL == taosArrayPush(hbBasic->queryDesc, &desc)) {
31,573,926✔
699
      taosArrayDestroy(desc.subDesc);
×
700
      return terrno;
×
701
    }
702

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

706
  return code;
25,448,017✔
707
}
708

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

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

723
  hbBasic->connId = pTscObj->connId;
27,390,081✔
724

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

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

741
  int32_t code = hbBuildQueryDesc(hbBasic, pTscObj);
25,448,017✔
742
  if (code) {
25,448,017✔
743
    releaseTscObj(connKey->tscRid);
3,473,738✔
744
    if (hbBasic->queryDesc) {
3,473,738✔
745
      taosArrayDestroyEx(hbBasic->queryDesc, tFreeClientHbQueryDesc);
3,473,738✔
746
    }
747
    taosMemoryFree(hbBasic);
3,473,738✔
748
    return code;
3,473,738✔
749
  }
750

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

754
  return TSDB_CODE_SUCCESS;
21,974,279✔
755
}
756

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

764
  int32_t code = 0;
864,066✔
765

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

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

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

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

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

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

827
  return code;
864,066✔
828
}
829

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

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

840
  if (userNum <= 0) {
18,896,533✔
841
    taosMemoryFree(users);
710,675✔
842
    return TSDB_CODE_SUCCESS;
710,675✔
843
  }
844

845
  for (int32_t i = 0; i < userNum; ++i) {
36,769,203✔
846
    SUserAuthVersion *user = &users[i];
18,583,345✔
847
    user->version = htonl(user->version);
18,583,345✔
848
  }
849

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

856
  tscDebug("hb got %d expired users, valueLen:%d", userNum, kv.valueLen);
18,185,858✔
857

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

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

872
  return TSDB_CODE_SUCCESS;
18,185,858✔
873
}
874

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

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

885
  if (dbNum <= 0) {
19,853,964✔
886
    taosMemoryFree(dbs);
14,199,444✔
887
    return TSDB_CODE_SUCCESS;
14,199,444✔
888
  }
889

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

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

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

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

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

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

926
  return TSDB_CODE_SUCCESS;
5,654,520✔
927
}
928

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

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

939
  if (stbNum <= 0) {
19,853,964✔
940
    taosMemoryFree(stbs);
16,075,052✔
941
    return TSDB_CODE_SUCCESS;
16,075,052✔
942
  }
943

944
  for (int32_t i = 0; i < stbNum; ++i) {
13,015,864✔
945
    SSTableVersion *stb = &stbs[i];
9,236,952✔
946
    stb->suid = htobe64(stb->suid);
9,236,952✔
947
    stb->sversion = htonl(stb->sversion);
9,236,952✔
948
    stb->tversion = htonl(stb->tversion);
9,236,952✔
949
    stb->smaVer = htonl(stb->smaVer);
9,236,952✔
950
  }
951

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

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

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

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

974
  return TSDB_CODE_SUCCESS;
3,778,912✔
975
}
976

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

983
  TSC_ERR_JRET(catalogGetExpiredViews(pCatalog, &views, &viewNum, &pDynViewVer));
19,853,964✔
984

985
  if (viewNum <= 0) {
19,853,964✔
986
    taosMemoryFree(views);
19,819,723✔
987
    taosMemoryFree(pDynViewVer);
19,819,723✔
988
    return TSDB_CODE_SUCCESS;
19,819,723✔
989
  }
990

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

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

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

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

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

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

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

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

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

1038
  if (tsmaNum <= 0) {
19,853,964✔
1039
    taosMemoryFree(tsmas);
19,761,014✔
1040
    return TSDB_CODE_SUCCESS;
19,761,014✔
1041
  }
1042

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

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

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

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

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

1080
  return TSDB_CODE_SUCCESS;
23,916,343✔
1081
}
1082

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

1088
  code = hbGetQueryBasicInfo(connKey, req);
27,390,081✔
1089
  if (code != TSDB_CODE_SUCCESS) {
27,390,081✔
1090
    tscWarn("hbGetQueryBasicInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
3,473,738✔
1091
    return code;
3,473,738✔
1092
  }
1093

1094
  if (hbParam->reqCnt == 0) {
23,916,343✔
1095
    code = catalogGetHandle(hbParam->clusterId, &pCatalog);
19,854,213✔
1096
    if (code != TSDB_CODE_SUCCESS) {
19,854,213✔
1097
      tscWarn("catalogGetHandle failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1098
      return code;
×
1099
    }
1100

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

1107
    if (!taosHashGet(clientHbMgr.appHbHash, &hbParam->clusterId, sizeof(hbParam->clusterId))) {
19,854,213✔
1108
      code = hbGetExpiredUserInfo(connKey, pCatalog, req);
18,896,533✔
1109
      if (TSDB_CODE_SUCCESS != code) {
18,896,533✔
1110
        tscWarn("hbGetExpiredUserInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1111
        return code;
×
1112
      }
1113
      if (clientHbMgr.appHbHash) {
18,896,533✔
1114
        code = taosHashPut(clientHbMgr.appHbHash, &hbParam->clusterId, sizeof(uint64_t), NULL, 0);
2,043,293✔
1115
        if (TSDB_CODE_SUCCESS != code) {
2,043,293✔
1116
          tscWarn("hbQueryHbReqHandle put clusterId failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId,
×
1117
                  tstrerror(code));
1118
          return code;
×
1119
        }
1120
      }
1121
    }
1122

1123
    // invoke after hbGetExpiredUserInfo
1124
    if (2 != atomic_load_8(&hbParam->pAppHbMgr->connHbFlag)) {
19,854,213✔
1125
      code = hbGetUserAuthInfo(connKey, hbParam, req);
864,315✔
1126
      if (TSDB_CODE_SUCCESS != code) {
864,315✔
1127
        tscWarn("hbGetUserAuthInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
249✔
1128
        return code;
249✔
1129
      }
1130
      atomic_store_8(&hbParam->pAppHbMgr->connHbFlag, 1);
864,066✔
1131
    }
1132

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

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

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

1165
  ++hbParam->reqCnt;  // success to get catalog info
23,916,094✔
1166

1167
  return TSDB_CODE_SUCCESS;
23,916,094✔
1168
}
1169

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

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

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

1191
  int64_t  maxIpWhiteVer = 0;
23,787,970✔
1192
  void    *pIter = NULL;
23,787,970✔
1193
  SHbParam param = {0};
23,787,970✔
1194
  while ((pIter = taosHashIterate(pAppHbMgr->activeInfo, pIter))) {
52,634,585✔
1195
    SClientHbReq *pOneReq = pIter;
28,846,615✔
1196
    SClientHbKey *connKey = &pOneReq->connKey;
28,846,615✔
1197
    STscObj      *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
28,846,615✔
1198

1199
    if (!pTscObj || atomic_load_8(&pTscObj->dropped) == 1) {
28,846,615✔
1200
      if (pTscObj) releaseTscObj(connKey->tscRid);
1,456,534✔
1201
      continue;
1,456,534✔
1202
    }
1203

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

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

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

1237
    int64_t ver = atomic_load_64(&pTscObj->whiteListInfo.ver);
27,390,081✔
1238
    maxIpWhiteVer = TMAX(maxIpWhiteVer, ver);
27,390,081✔
1239
    releaseTscObj(connKey->tscRid);
27,390,081✔
1240
  }
1241
  (*pBatchReq)->ipWhiteListVer = maxIpWhiteVer;
23,787,970✔
1242

1243
  return TSDB_CODE_SUCCESS;
23,787,970✔
1244
}
1245

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

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

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

1270
  taosHashClear(clientHbMgr.appSummary);
23,632,107✔
1271

1272
  for (int32_t i = 0; i < sz; ++i) {
50,486,169✔
1273
    SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
26,854,062✔
1274
    if (pAppHbMgr == NULL) continue;
26,854,062✔
1275

1276
    int64_t    clusterId = pAppHbMgr->pAppInstInfo->clusterId;
26,854,062✔
1277
    SAppHbReq *pApp = taosHashGet(clientHbMgr.appSummary, &clusterId, sizeof(clusterId));
26,854,062✔
1278
    if (NULL == pApp) {
26,854,062✔
1279
      (void)memcpy(&req.summary, &pAppHbMgr->pAppInstInfo->summary, sizeof(req.summary));
24,014,201✔
1280
      req.startTime = pAppHbMgr->startTime;
24,014,201✔
1281
      TSC_ERR_RET(taosHashPut(clientHbMgr.appSummary, &clusterId, sizeof(clusterId), &req, sizeof(req)));
24,014,201✔
1282
    } else {
1283
      if (pAppHbMgr->startTime < pApp->startTime) {
2,839,861✔
1284
        pApp->startTime = pAppHbMgr->startTime;
×
1285
      }
1286

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

1291
  return TSDB_CODE_SUCCESS;
23,632,107✔
1292
}
1293

1294
static void *hbThreadFunc(void *param) {
1,288,140✔
1295
  setThreadName("hb");
1,288,140✔
1296
#ifdef WINDOWS
1297
  if (taosCheckCurrentInDll()) {
1298
    atexit(hbThreadFuncUnexpectedStopped);
1299
  }
1300
#endif
1301
  while (1) {
22,876,903✔
1302
    if (1 == clientHbMgr.threadStop) {
24,165,043✔
1303
      break;
522,316✔
1304
    }
1305

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

1311
    int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
23,642,727✔
1312
    if (sz > 0) {
23,642,727✔
1313
      if (TSDB_CODE_SUCCESS != hbGatherAppInfo()) {
23,632,107✔
1314
        tscError("hbGatherAppInfo failed");
×
1315
        return NULL;
×
1316
      }
1317
      if (sz > 1 && !clientHbMgr.appHbHash) {
23,632,107✔
1318
        clientHbMgr.appHbHash = taosHashInit(0, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), true, HASH_NO_LOCK);
39,728✔
1319
        if (NULL == clientHbMgr.appHbHash) {
39,728✔
1320
          tscError("taosHashInit failed");
×
1321
          return NULL;
×
1322
        }
1323
      }
1324
      taosHashClear(clientHbMgr.appHbHash);
23,632,107✔
1325
    }
1326

1327
    for (int i = 0; i < sz; i++) {
50,496,789✔
1328
      SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
26,854,062✔
1329
      if (pAppHbMgr == NULL) {
26,854,062✔
1330
        continue;
1,036,997✔
1331
      }
1332

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

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

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

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

1396
    if (TSDB_CODE_SUCCESS != taosThreadMutexUnlock(&clientHbMgr.lock)) {
23,642,727✔
1397
      tscError("taosThreadMutexLock failed");
×
1398
      return NULL;
×
1399
    }
1400
    taosMsleep(HEARTBEAT_INTERVAL);
23,642,727✔
1401
  }
1402
  taosHashCleanup(clientHbMgr.appHbHash);
522,316✔
1403
  return NULL;
522,316✔
1404
}
1405

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1501
void hbFreeAppHbMgr(SAppHbMgr *pTarget) {
1,355,114✔
1502
  void *pIter = taosHashIterate(pTarget->activeInfo, NULL);
1,355,114✔
1503
  while (pIter != NULL) {
1,583,304✔
1504
    SClientHbReq *pOneReq = pIter;
228,190✔
1505
    tFreeClientHbReq(pOneReq);
1506
    pIter = taosHashIterate(pTarget->activeInfo, pIter);
228,190✔
1507
  }
1508
  taosHashCleanup(pTarget->activeInfo);
1,355,114✔
1509
  pTarget->activeInfo = NULL;
1,355,114✔
1510

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

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

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

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

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

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

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

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

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

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

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

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

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

1596
void hbMgrCleanUp() {
1,290,023✔
1597
  hbStopThread();
1,290,023✔
1598

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

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

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

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

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

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

1639
  switch (connType) {
3,143,187✔
1640
    case CONN_TYPE__QUERY: {
3,064,002✔
1641
      return hbRegisterConnImpl(pAppHbMgr, connKey, clusterId);
3,064,002✔
1642
    }
1643
    case CONN_TYPE__TMQ: {
79,185✔
1644
      return 0;
79,185✔
1645
    }
1646
    default:
×
1647
      return 0;
×
1648
  }
1649
}
1650

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

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