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

taosdata / TDengine / #3549

06 Dec 2024 09:44AM UTC coverage: 59.948% (+0.1%) from 59.846%
#3549

push

travis-ci

web-flow
Merge pull request #29057 from taosdata/docs/TD-33031-3.0

docs: description of user privileges

118833 of 254191 branches covered (46.75%)

Branch coverage included in aggregate %.

199893 of 277480 relevant lines covered (72.04%)

19006119.35 hits per line

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

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

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

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

63
    TSC_ERR_JRET(catalogUpdateUserAuthInfo(pCatalog, rsp));
2,416!
64
  }
65

66
  if (numOfBatchs > 0) {
2,273!
67
    TSC_ERR_JRET(hbUpdateUserAuthInfo(pAppHbMgr, &batchRsp));
2,273!
68
  }
69

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

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

77
static int32_t hbUpdateUserAuthInfo(SAppHbMgr *pAppHbMgr, SUserAuthBatchRsp *batchRsp) {
2,273✔
78
  int64_t clusterId = pAppHbMgr->pAppInstInfo->clusterId;
2,273✔
79
  for (int i = 0; i < TARRAY_SIZE(clientHbMgr.appHbMgrs); ++i) {
5,232✔
80
    SAppHbMgr *hbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
2,959✔
81
    if (!hbMgr || hbMgr->pAppInstInfo->clusterId != clusterId) {
2,959!
82
      continue;
5✔
83
    }
84

85
    SClientHbReq    *pReq = NULL;
2,954✔
86
    SGetUserAuthRsp *pRsp = NULL;
2,954✔
87
    while ((pReq = taosHashIterate(hbMgr->activeInfo, pReq))) {
9,125✔
88
      STscObj *pTscObj = (STscObj *)acquireTscObj(pReq->connKey.tscRid);
6,247✔
89
      if (!pTscObj) {
6,247✔
90
        continue;
663✔
91
      }
92

93
      if (!pRsp) {
5,584✔
94
        for (int32_t j = 0; j < TARRAY_SIZE(batchRsp->pArray); ++j) {
2,722✔
95
          SGetUserAuthRsp *rsp = TARRAY_GET_ELEM(batchRsp->pArray, j);
2,646✔
96
          if (0 == strncmp(rsp->user, pTscObj->user, TSDB_USER_LEN)) {
2,646✔
97
            pRsp = rsp;
2,508✔
98
            break;
2,508✔
99
          }
100
        }
101
        if (!pRsp) {
2,584✔
102
          releaseTscObj(pReq->connKey.tscRid);
76✔
103
          taosHashCancelIterate(hbMgr->activeInfo, pReq);
76✔
104
          break;
76✔
105
        }
106
      }
107

108
      if (pRsp->dropped == 1) {
5,508✔
109
        if (atomic_val_compare_exchange_8(&pTscObj->dropped, 0, 1) == 0) {
28✔
110
          if (pTscObj->userDroppedInfo.fp) {
3!
111
            SPassInfo *dropInfo = &pTscObj->userDroppedInfo;
×
112
            if (dropInfo->fp) {
×
113
              (*dropInfo->fp)(dropInfo->param, NULL, TAOS_NOTIFY_USER_DROPPED);
×
114
            }
115
          }
116
        }
117
        releaseTscObj(pReq->connKey.tscRid);
28✔
118
        continue;
28✔
119
      }
120

121
      pTscObj->authVer = pRsp->version;
5,480✔
122

123
      if (pTscObj->sysInfo != pRsp->sysInfo) {
5,480✔
124
        tscDebug("update sysInfo of user %s from %" PRIi8 " to %" PRIi8 ", tscRid:%" PRIi64, pRsp->user,
6!
125
                 pTscObj->sysInfo, pRsp->sysInfo, pTscObj->id);
126
        pTscObj->sysInfo = pRsp->sysInfo;
6✔
127
      }
128

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

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

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

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

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

196
_return:
3,048✔
197
  if (code) {
3,048!
198
    taosHashCleanup(vgInfo->vgHash);
×
199
    taosMemoryFreeClear(vgInfo);
×
200
  }
201

202
  *pInfo = vgInfo;
3,048✔
203
  return code;
3,048✔
204
}
205

206
static int32_t hbProcessDBInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
8,879✔
207
  int32_t code = 0;
8,879✔
208

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

216
  int32_t numOfBatchs = taosArrayGetSize(batchRsp.pArray);
8,879✔
217
  for (int32_t i = 0; i < numOfBatchs; ++i) {
12,349✔
218
    SDbHbRsp *rsp = taosArrayGet(batchRsp.pArray, i);
3,470✔
219
    if (NULL == rsp) {
3,470!
220
      code = terrno;
×
221
      goto _return;
×
222
    }
223
    if (rsp->useDbRsp) {
3,470✔
224
      tscDebug("hb use db rsp, db:%s, vgVersion:%d, stateTs:%" PRId64 ", uid:%" PRIx64, rsp->useDbRsp->db,
2,686✔
225
               rsp->useDbRsp->vgVersion, rsp->useDbRsp->stateTs, rsp->useDbRsp->uid);
226

227
      if (rsp->useDbRsp->vgVersion < 0) {
2,686✔
228
        tscDebug("hb to remove db, db:%s", rsp->useDbRsp->db);
143✔
229
        code = catalogRemoveDB(pCatalog, rsp->useDbRsp->db, rsp->useDbRsp->uid);
143✔
230
      } else {
231
        SDBVgInfo *vgInfo = NULL;
2,543✔
232
        code = hbGenerateVgInfoFromRsp(&vgInfo, rsp->useDbRsp);
2,543✔
233
        if (TSDB_CODE_SUCCESS != code) {
2,543!
234
          goto _return;
×
235
        }
236

237
        tscDebug("hb to update db vgInfo, db:%s", rsp->useDbRsp->db);
2,543✔
238

239
        TSC_ERR_JRET(catalogUpdateDBVgInfo(pCatalog, rsp->useDbRsp->db, rsp->useDbRsp->uid, vgInfo));
2,543!
240

241
        if (IS_SYS_DBNAME(rsp->useDbRsp->db)) {
2,543!
242
          code = hbGenerateVgInfoFromRsp(&vgInfo, rsp->useDbRsp);
505✔
243
          if (TSDB_CODE_SUCCESS != code) {
505!
244
            goto _return;
×
245
          }
246

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

254
    if (rsp->cfgRsp) {
3,470✔
255
      tscDebug("hb db cfg rsp, db:%s, cfgVersion:%d", rsp->cfgRsp->db, rsp->cfgRsp->cfgVersion);
427✔
256
      code = catalogUpdateDbCfg(pCatalog, rsp->cfgRsp->db, rsp->cfgRsp->dbId, rsp->cfgRsp);
427✔
257
      rsp->cfgRsp = NULL;
427✔
258
    }
259
    if (rsp->pTsmaRsp) {
3,470✔
260
      if (rsp->pTsmaRsp->pTsmas) {
1,976✔
261
        for (int32_t i = 0; i < rsp->pTsmaRsp->pTsmas->size; ++i) {
402✔
262
          STableTSMAInfo *pTsma = taosArrayGetP(rsp->pTsmaRsp->pTsmas, i);
251✔
263
          if (NULL == pTsma) {
251!
264
            TSC_ERR_JRET(TSDB_CODE_OUT_OF_RANGE);
×
265
          }
266
          TSC_ERR_JRET(catalogAsyncUpdateTSMA(pCatalog, &pTsma, rsp->dbTsmaVersion));
251!
267
        }
268
        taosArrayClear(rsp->pTsmaRsp->pTsmas);
151✔
269
      } else {
270
        TSC_ERR_JRET(catalogAsyncUpdateDbTsmaVersion(pCatalog, rsp->dbTsmaVersion, rsp->db, rsp->dbId));
1,825!
271
      }
272
    }
273
  }
274

275
_return:
8,879✔
276

277
  tFreeSDbHbBatchRsp(&batchRsp);
8,879✔
278
  return code;
8,879✔
279
}
280

281
static int32_t hbProcessStbInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
6,113✔
282
  int32_t code = TSDB_CODE_SUCCESS;
6,113✔
283

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

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

308
      TSC_ERR_JRET(catalogAsyncUpdateTableMeta(pCatalog, rsp));
16!
309
    }
310
  }
311

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

322
_return:
6,113✔
323
  taosArrayDestroy(hbRsp.pIndexRsp);
6,113✔
324
  hbRsp.pIndexRsp = NULL;
6,113✔
325

326
  tFreeSSTbHbRsp(&hbRsp);
6,113✔
327
  return code;
6,113✔
328
}
329

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

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

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

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

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

372
_return:
1✔
373
  taosArrayDestroy(hbRsp.pViewRsp);
1✔
374
  return code;
1✔
375
}
376

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

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

386
  int32_t numOfTsma = taosArrayGetSize(hbRsp.pTsmas);
348✔
387
  for (int32_t i = 0; i < numOfTsma; ++i) {
350✔
388
    STableTSMAInfo *pTsmaInfo = taosArrayGetP(hbRsp.pTsmas, i);
2✔
389

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

402
_return:
348✔
403
  taosArrayDestroy(hbRsp.pTsmas);
348✔
404
  return code;
348✔
405
}
406

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

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

496
  if (pRsp->query) {
111,998✔
497
    STscObj *pTscObj = (STscObj *)acquireTscObj(pRsp->connKey.tscRid);
107,825✔
498
    if (NULL == pTscObj) {
107,825✔
499
      tscDebug("tscObj rid %" PRIx64 " not exist", pRsp->connKey.tscRid);
3✔
500
    } else {
501
      if (pRsp->query->totalDnodes > 1 && !isEpsetEqual(&pTscObj->pAppInfo->mgmtEp.epSet, &pRsp->query->epSet)) {
107,822✔
502
        SEpSet *pOrig = &pTscObj->pAppInfo->mgmtEp.epSet;
125✔
503
        SEp    *pOrigEp = &pOrig->eps[pOrig->inUse];
125✔
504
        SEp    *pNewEp = &pRsp->query->epSet.eps[pRsp->query->epSet.inUse];
125✔
505
        tscDebug("mnode epset updated from %d/%d=>%s:%d to %d/%d=>%s:%d in hb", pOrig->inUse, pOrig->numOfEps,
125!
506
                 pOrigEp->fqdn, pOrigEp->port, pRsp->query->epSet.inUse, pRsp->query->epSet.numOfEps, pNewEp->fqdn,
507
                 pNewEp->port);
508

509
        updateEpSet_s(&pTscObj->pAppInfo->mgmtEp, &pRsp->query->epSet);
125✔
510
      }
511

512
      pTscObj->pAppInfo->totalDnodes = pRsp->query->totalDnodes;
107,822✔
513
      pTscObj->pAppInfo->onlineDnodes = pRsp->query->onlineDnodes;
107,822✔
514
      pTscObj->connId = pRsp->query->connId;
107,822✔
515
      tscTrace("conn %u hb rsp, dnodes %d/%d", pTscObj->connId, pTscObj->pAppInfo->onlineDnodes,
107,822✔
516
               pTscObj->pAppInfo->totalDnodes);
517

518
      if (pRsp->query->killRid) {
107,822!
519
        tscDebug("request rid %" PRIx64 " need to be killed now", pRsp->query->killRid);
×
520
        SRequestObj *pRequest = acquireRequest(pRsp->query->killRid);
×
521
        if (NULL == pRequest) {
×
522
          tscDebug("request 0x%" PRIx64 " not exist to kill", pRsp->query->killRid);
×
523
        } else {
524
          taos_stop_query((TAOS_RES *)pRequest);
×
525
          (void)releaseRequest(pRsp->query->killRid);
×
526
        }
527
      }
528

529
      if (pRsp->query->killConnection) {
107,822!
530
        taos_close_internal(pTscObj);
×
531
      }
532

533
      if (pRsp->query->pQnodeList) {
107,822✔
534
        if (TSDB_CODE_SUCCESS != updateQnodeList(pTscObj->pAppInfo, pRsp->query->pQnodeList)) {
76,637!
535
          tscWarn("update qnode list failed");
×
536
        }
537
      }
538

539
      releaseTscObj(pRsp->connKey.tscRid);
107,822✔
540
    }
541
  }
542

543
  int32_t kvNum = pRsp->info ? taosArrayGetSize(pRsp->info) : 0;
111,998!
544

545
  tscDebug("hb got %d rsp kv", kvNum);
111,998✔
546

547
  if (kvNum > 0) {
111,998✔
548
    struct SCatalog *pCatalog = NULL;
14,322✔
549
    int32_t          code = catalogGetHandle(pReq->clusterId, &pCatalog);
14,322✔
550
    if (code != TSDB_CODE_SUCCESS) {
14,322!
551
      tscWarn("catalogGetHandle failed, clusterId:%" PRIx64 ", error:%s", pReq->clusterId, tstrerror(code));
×
552
    } else {
553
      hbProcessQueryRspKvs(kvNum, pRsp->info, pCatalog, pAppHbMgr);
14,322✔
554
    }
555
  }
556

557
  taosHashRelease(pAppHbMgr->activeInfo, pReq);
111,998✔
558

559
  return TSDB_CODE_SUCCESS;
111,998✔
560
}
561

562
static int32_t hbAsyncCallBack(void *param, SDataBuf *pMsg, int32_t code) {
36,154✔
563
  if (0 == atomic_load_8(&clientHbMgr.inited)) {
36,154!
564
    goto _return;
×
565
  }
566

567
  static int32_t    emptyRspNum = 0;
568
  int32_t           idx = *(int32_t *)param;
36,154✔
569
  SClientHbBatchRsp pRsp = {0};
36,154✔
570
  if (TSDB_CODE_SUCCESS == code) {
36,154✔
571
    code = tDeserializeSClientHbBatchRsp(pMsg->pData, pMsg->len, &pRsp);
35,527✔
572
    if (TSDB_CODE_SUCCESS != code) {
35,527✔
573
      tscError("deserialize hb rsp failed");
3!
574
    }
575
    int32_t now = taosGetTimestampSec();
35,527✔
576
    int32_t delta = abs(now - pRsp.svrTimestamp);
35,527✔
577
    if (delta > timestampDeltaLimit) {
35,527✔
578
      code = TSDB_CODE_TIME_UNSYNCED;
3✔
579
      tscError("time diff: %ds is too big", delta);
3!
580
    }
581
  }
582

583
  int32_t rspNum = taosArrayGetSize(pRsp.rsps);
36,154✔
584

585
  (void)taosThreadMutexLock(&clientHbMgr.lock);
36,153✔
586

587
  SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, idx);
36,154✔
588
  if (pAppHbMgr == NULL) {
36,154!
589
    (void)taosThreadMutexUnlock(&clientHbMgr.lock);
×
590
    tscError("appHbMgr not exist, idx:%d", idx);
×
591
    taosMemoryFree(pMsg->pData);
×
592
    taosMemoryFree(pMsg->pEpSet);
×
593
    tFreeClientHbBatchRsp(&pRsp);
594
    return TSDB_CODE_OUT_OF_RANGE;
×
595
  }
596

597
  SAppInstInfo *pInst = pAppHbMgr->pAppInstInfo;
36,154✔
598
  if (code != 0) {
36,154✔
599
    pInst->onlineDnodes = pInst->totalDnodes ? 0 : -1;
630✔
600
    tscDebug("hb rsp error %s, update server status %d/%d", tstrerror(code), pInst->onlineDnodes, pInst->totalDnodes);
630!
601
    (void)taosThreadMutexUnlock(&clientHbMgr.lock);
630✔
602
    taosMemoryFree(pMsg->pData);
630✔
603
    taosMemoryFree(pMsg->pEpSet);
630✔
604
    tFreeClientHbBatchRsp(&pRsp);
605
    return code;
630✔
606
  }
607

608
  pInst->serverCfg.monitorParas = pRsp.monitorParas;
35,524✔
609
  pInst->serverCfg.enableAuditDelete = pRsp.enableAuditDelete;
35,524✔
610
  tscDebug("[monitor] paras from hb, clusterId:%" PRIx64 " monitorParas threshold:%d scope:%d", pInst->clusterId,
35,524✔
611
           pRsp.monitorParas.tsSlowLogThreshold, pRsp.monitorParas.tsSlowLogScope);
612

613
  if (rspNum) {
35,524✔
614
    tscDebug("hb got %d rsp, %d empty rsp received before", rspNum,
35,298✔
615
             atomic_val_compare_exchange_32(&emptyRspNum, emptyRspNum, 0));
616
  } else {
617
    (void)atomic_add_fetch_32(&emptyRspNum, 1);
226✔
618
  }
619

620
  for (int32_t i = 0; i < rspNum; ++i) {
147,622✔
621
    SClientHbRsp *rsp = taosArrayGet(pRsp.rsps, i);
112,098✔
622
    code = (*clientHbMgr.rspHandle[rsp->connKey.connType])(pAppHbMgr, rsp);
112,098✔
623
    if (code) {
112,098!
624
      break;
×
625
    }
626
  }
627

628
  (void)taosThreadMutexUnlock(&clientHbMgr.lock);
35,524✔
629

630
  tFreeClientHbBatchRsp(&pRsp);
631

632
_return:
35,524✔
633
  taosMemoryFree(pMsg->pData);
35,524✔
634
  taosMemoryFree(pMsg->pEpSet);
35,524✔
635
  return code;
35,524✔
636
}
637

638
int32_t hbBuildQueryDesc(SQueryHbReqBasic *hbBasic, STscObj *pObj) {
44,445✔
639
  int64_t    now = taosGetTimestampUs();
44,445✔
640
  SQueryDesc desc = {0};
44,445✔
641
  int32_t    code = 0;
44,445✔
642

643
  void *pIter = taosHashIterate(pObj->pRequests, NULL);
44,445✔
644
  while (pIter != NULL) {
90,951✔
645
    int64_t     *rid = pIter;
46,506✔
646
    SRequestObj *pRequest = acquireRequest(*rid);
46,506✔
647
    if (NULL == pRequest) {
46,506✔
648
      pIter = taosHashIterate(pObj->pRequests, pIter);
103✔
649
      continue;
103✔
650
    }
651

652
    if (pRequest->killed || 0 == pRequest->body.queryJob) {
46,403✔
653
      (void)releaseRequest(*rid);
16,451✔
654
      pIter = taosHashIterate(pObj->pRequests, pIter);
16,451✔
655
      continue;
16,451✔
656
    }
657

658
    tstrncpy(desc.sql, pRequest->sqlstr, sizeof(desc.sql));
29,952✔
659
    desc.stime = pRequest->metric.start / 1000;
29,952✔
660
    desc.queryId = pRequest->requestId;
29,952✔
661
    desc.useconds = now - pRequest->metric.start;
29,952✔
662
    desc.reqRid = pRequest->self;
29,952✔
663
    desc.stableQuery = pRequest->stableQuery;
29,952✔
664
    desc.isSubQuery = pRequest->isSubReq;
29,952✔
665
    code = taosGetFqdn(desc.fqdn);
29,952✔
666
    if (TSDB_CODE_SUCCESS != code) {
29,952!
667
      (void)releaseRequest(*rid);
×
668
      tscError("get fqdn failed");
×
669
      return TSDB_CODE_FAILED;
×
670
    }
671
    desc.subPlanNum = pRequest->body.subplanNum;
29,952✔
672

673
    if (desc.subPlanNum) {
29,952!
674
      desc.subDesc = taosArrayInit(desc.subPlanNum, sizeof(SQuerySubDesc));
29,952✔
675
      if (NULL == desc.subDesc) {
29,952!
676
        (void)releaseRequest(*rid);
×
677
        return terrno;
×
678
      }
679

680
      code = schedulerGetTasksStatus(pRequest->body.queryJob, desc.subDesc);
29,952✔
681
      if (code) {
29,952✔
682
        taosArrayDestroy(desc.subDesc);
5,824✔
683
        desc.subDesc = NULL;
5,824✔
684
      }
685
      desc.subPlanNum = taosArrayGetSize(desc.subDesc);
29,952✔
686
    } else {
687
      desc.subDesc = NULL;
×
688
    }
689

690
    (void)releaseRequest(*rid);
29,952✔
691
    if (NULL == taosArrayPush(hbBasic->queryDesc, &desc)) {
59,904!
692
      taosArrayDestroy(desc.subDesc);
×
693
      return terrno;
×
694
    }
695

696
    pIter = taosHashIterate(pObj->pRequests, pIter);
29,952✔
697
  }
698

699
  return code;
44,445✔
700
}
701

702
int32_t hbGetQueryBasicInfo(SClientHbKey *connKey, SClientHbReq *req) {
114,113✔
703
  STscObj *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
114,113✔
704
  if (NULL == pTscObj) {
114,113!
705
    tscWarn("tscObj rid %" PRIx64 " not exist", connKey->tscRid);
×
706
    return terrno;
×
707
  }
708

709
  SQueryHbReqBasic *hbBasic = (SQueryHbReqBasic *)taosMemoryCalloc(1, sizeof(SQueryHbReqBasic));
114,113✔
710
  if (NULL == hbBasic) {
114,113!
711
    tscError("calloc %d failed", (int32_t)sizeof(SQueryHbReqBasic));
×
712
    releaseTscObj(connKey->tscRid);
×
713
    return terrno;
×
714
  }
715

716
  hbBasic->connId = pTscObj->connId;
114,113✔
717

718
  int32_t numOfQueries = pTscObj->pRequests ? taosHashGetSize(pTscObj->pRequests) : 0;
114,113!
719
  if (numOfQueries <= 0) {
114,113✔
720
    req->query = hbBasic;
69,668✔
721
    releaseTscObj(connKey->tscRid);
69,668✔
722
    tscDebug("no queries on connection");
69,668✔
723
    return TSDB_CODE_SUCCESS;
69,668✔
724
  }
725

726
  hbBasic->queryDesc = taosArrayInit(numOfQueries, sizeof(SQueryDesc));
44,445✔
727
  if (NULL == hbBasic->queryDesc) {
44,445!
728
    tscWarn("taosArrayInit %d queryDesc failed", numOfQueries);
×
729
    releaseTscObj(connKey->tscRid);
×
730
    taosMemoryFree(hbBasic);
×
731
    return terrno;
×
732
  }
733

734
  int32_t code = hbBuildQueryDesc(hbBasic, pTscObj);
44,445✔
735
  if (code) {
44,445✔
736
    releaseTscObj(connKey->tscRid);
4,182✔
737
    if (hbBasic->queryDesc) {
4,182!
738
      taosArrayDestroyEx(hbBasic->queryDesc, tFreeClientHbQueryDesc);
4,182✔
739
    }
740
    taosMemoryFree(hbBasic);
4,182✔
741
    return code;
4,182✔
742
  }
743

744
  req->query = hbBasic;
40,263✔
745
  releaseTscObj(connKey->tscRid);
40,263✔
746

747
  return TSDB_CODE_SUCCESS;
40,263✔
748
}
749

750
static int32_t hbGetUserAuthInfo(SClientHbKey *connKey, SHbParam *param, SClientHbReq *req) {
2,337✔
751
  STscObj *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
2,337✔
752
  if (!pTscObj) {
2,337!
753
    tscWarn("tscObj rid %" PRIx64 " not exist", connKey->tscRid);
×
754
    return terrno;
×
755
  }
756

757
  int32_t code = 0;
2,337✔
758

759
  SKv  kv = {.key = HEARTBEAT_KEY_USER_AUTHINFO};
2,337✔
760
  SKv *pKv = NULL;
2,337✔
761
  if ((pKv = taosHashGet(req->info, &kv.key, sizeof(kv.key)))) {
2,337✔
762
    int32_t           userNum = pKv->valueLen / sizeof(SUserAuthVersion);
1,577✔
763
    SUserAuthVersion *userAuths = (SUserAuthVersion *)pKv->value;
1,577✔
764
    for (int32_t i = 0; i < userNum; ++i) {
1,643✔
765
      SUserAuthVersion *pUserAuth = userAuths + i;
1,638✔
766
      // both key and user exist, update version
767
      if (strncmp(pUserAuth->user, pTscObj->user, TSDB_USER_LEN) == 0) {
1,638✔
768
        pUserAuth->version = htonl(-1);  // force get userAuthInfo
1,572✔
769
        goto _return;
1,572✔
770
      }
771
    }
772
    // key exists, user not exist, append user
773
    SUserAuthVersion *qUserAuth =
774
        (SUserAuthVersion *)taosMemoryRealloc(pKv->value, (userNum + 1) * sizeof(SUserAuthVersion));
5✔
775
    if (qUserAuth) {
5!
776
      tstrncpy((qUserAuth + userNum)->user, pTscObj->user, TSDB_USER_LEN);
5✔
777
      (qUserAuth + userNum)->version = htonl(-1);  // force get userAuthInfo
5✔
778
      pKv->value = qUserAuth;
5✔
779
      pKv->valueLen += sizeof(SUserAuthVersion);
5✔
780
    } else {
781
      code = terrno;
×
782
    }
783
    goto _return;
5✔
784
  }
785

786
  // key/user not exist, add user
787
  SUserAuthVersion *user = taosMemoryMalloc(sizeof(SUserAuthVersion));
760✔
788
  if (!user) {
760!
789
    code = terrno;
×
790
    goto _return;
×
791
  }
792
  tstrncpy(user->user, pTscObj->user, TSDB_USER_LEN);
760✔
793
  user->version = htonl(-1);  // force get userAuthInfo
760✔
794
  kv.valueLen = sizeof(SUserAuthVersion);
760✔
795
  kv.value = user;
760✔
796

797
  tscDebug("hb got user auth info, valueLen:%d, user:%s, authVer:%d, tscRid:%" PRIi64, kv.valueLen, user->user,
760✔
798
           pTscObj->authVer, connKey->tscRid);
799

800
  if (!req->info) {
760!
801
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
760✔
802
    if (NULL == req->info) {
760!
803
      code = terrno;
×
804
      goto _return;
×
805
    }
806
  }
807

808
  if (taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)) != 0) {
760!
809
    taosMemoryFree(user);
×
810
    code = terrno ? terrno : TSDB_CODE_APP_ERROR;
×
811
    goto _return;
×
812
  }
813

814
_return:
760✔
815
  releaseTscObj(connKey->tscRid);
2,337✔
816
  if (code) {
2,337!
817
    tscError("hb got user auth info failed since %s", tstrerror(code));
×
818
  }
819

820
  return code;
2,337✔
821
}
822

823
int32_t hbGetExpiredUserInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
30,085✔
824
  SUserAuthVersion *users = NULL;
30,085✔
825
  uint32_t          userNum = 0;
30,085✔
826
  int32_t           code = 0;
30,085✔
827

828
  code = catalogGetExpiredUsers(pCatalog, &users, &userNum);
30,085✔
829
  if (TSDB_CODE_SUCCESS != code) {
30,085!
830
    return code;
×
831
  }
832

833
  if (userNum <= 0) {
30,085✔
834
    taosMemoryFree(users);
693✔
835
    return TSDB_CODE_SUCCESS;
693✔
836
  }
837

838
  for (int32_t i = 0; i < userNum; ++i) {
58,968✔
839
    SUserAuthVersion *user = &users[i];
29,576✔
840
    user->version = htonl(user->version);
29,576✔
841
  }
842

843
  SKv kv = {
29,392✔
844
      .key = HEARTBEAT_KEY_USER_AUTHINFO,
845
      .valueLen = sizeof(SUserAuthVersion) * userNum,
29,392✔
846
      .value = users,
847
  };
848

849
  tscDebug("hb got %d expired users, valueLen:%d", userNum, kv.valueLen);
29,392✔
850

851
  if (NULL == req->info) {
29,392!
852
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
29,392✔
853
    if (NULL == req->info) {
29,392!
854
      taosMemoryFree(users);
×
855
      return terrno;
×
856
    }
857
  }
858

859
  code = taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
29,392✔
860
  if (TSDB_CODE_SUCCESS != code) {
29,392!
861
    taosMemoryFree(users);
×
862
    return code;
×
863
  }
864

865
  return TSDB_CODE_SUCCESS;
29,392✔
866
}
867

868
int32_t hbGetExpiredDBInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
33,799✔
869
  SDbCacheInfo *dbs = NULL;
33,799✔
870
  uint32_t      dbNum = 0;
33,799✔
871
  int32_t       code = 0;
33,799✔
872

873
  code = catalogGetExpiredDBs(pCatalog, &dbs, &dbNum);
33,799✔
874
  if (TSDB_CODE_SUCCESS != code) {
33,799!
875
    return code;
×
876
  }
877

878
  if (dbNum <= 0) {
33,799✔
879
    taosMemoryFree(dbs);
24,388✔
880
    return TSDB_CODE_SUCCESS;
24,388✔
881
  }
882

883
  for (int32_t i = 0; i < dbNum; ++i) {
20,261✔
884
    SDbCacheInfo *db = &dbs[i];
10,850✔
885
    tscDebug("the %dth expired dbFName:%s, dbId:%" PRId64
10,850✔
886
             ", vgVersion:%d, cfgVersion:%d, numOfTable:%d, startTs:%" PRId64,
887
             i, db->dbFName, db->dbId, db->vgVersion, db->cfgVersion, db->numOfTable, db->stateTs);
888

889
    db->dbId = htobe64(db->dbId);
10,850✔
890
    db->vgVersion = htonl(db->vgVersion);
10,850✔
891
    db->cfgVersion = htonl(db->cfgVersion);
10,850✔
892
    db->numOfTable = htonl(db->numOfTable);
10,850✔
893
    db->stateTs = htobe64(db->stateTs);
10,850✔
894
    db->tsmaVersion = htonl(db->tsmaVersion);
10,850✔
895
  }
896

897
  SKv kv = {
9,411✔
898
      .key = HEARTBEAT_KEY_DBINFO,
899
      .valueLen = sizeof(SDbCacheInfo) * dbNum,
9,411✔
900
      .value = dbs,
901
  };
902

903
  tscDebug("hb got %d expired db, valueLen:%d", dbNum, kv.valueLen);
9,411✔
904

905
  if (NULL == req->info) {
9,411✔
906
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
17✔
907
    if (NULL == req->info) {
17!
908
      taosMemoryFree(dbs);
×
909
      return terrno;
×
910
    }
911
  }
912

913
  code = taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
9,411✔
914
  if (TSDB_CODE_SUCCESS != code) {
9,411!
915
    taosMemoryFree(dbs);
×
916
    return code;
×
917
  }
918

919
  return TSDB_CODE_SUCCESS;
9,411✔
920
}
921

922
int32_t hbGetExpiredStbInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
33,799✔
923
  SSTableVersion *stbs = NULL;
33,799✔
924
  uint32_t        stbNum = 0;
33,799✔
925
  int32_t         code = 0;
33,799✔
926

927
  code = catalogGetExpiredSTables(pCatalog, &stbs, &stbNum);
33,799✔
928
  if (TSDB_CODE_SUCCESS != code) {
33,799!
929
    return code;
×
930
  }
931

932
  if (stbNum <= 0) {
33,799✔
933
    taosMemoryFree(stbs);
27,259✔
934
    return TSDB_CODE_SUCCESS;
27,259✔
935
  }
936

937
  for (int32_t i = 0; i < stbNum; ++i) {
14,911✔
938
    SSTableVersion *stb = &stbs[i];
8,371✔
939
    stb->suid = htobe64(stb->suid);
8,371✔
940
    stb->sversion = htonl(stb->sversion);
8,371✔
941
    stb->tversion = htonl(stb->tversion);
8,371✔
942
    stb->smaVer = htonl(stb->smaVer);
8,371✔
943
  }
944

945
  SKv kv = {
6,540✔
946
      .key = HEARTBEAT_KEY_STBINFO,
947
      .valueLen = sizeof(SSTableVersion) * stbNum,
6,540✔
948
      .value = stbs,
949
  };
950

951
  tscDebug("hb got %d expired stb, valueLen:%d", stbNum, kv.valueLen);
6,540✔
952

953
  if (NULL == req->info) {
6,540!
954
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
×
955
    if (NULL == req->info) {
×
956
      taosMemoryFree(stbs);
×
957
      return terrno;
×
958
    }
959
  }
960

961
  code = taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
6,540✔
962
  if (TSDB_CODE_SUCCESS != code) {
6,540!
963
    taosMemoryFree(stbs);
×
964
    return code;
×
965
  }
966

967
  return TSDB_CODE_SUCCESS;
6,540✔
968
}
969

970
int32_t hbGetExpiredViewInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
33,799✔
971
  SViewVersion    *views = NULL;
33,799✔
972
  uint32_t         viewNum = 0;
33,799✔
973
  int32_t          code = 0;
33,799✔
974
  SDynViewVersion *pDynViewVer = NULL;
33,799✔
975

976
  TSC_ERR_JRET(catalogGetExpiredViews(pCatalog, &views, &viewNum, &pDynViewVer));
33,799!
977

978
  if (viewNum <= 0) {
33,799✔
979
    taosMemoryFree(views);
33,797✔
980
    taosMemoryFree(pDynViewVer);
33,797✔
981
    return TSDB_CODE_SUCCESS;
33,797✔
982
  }
983

984
  for (int32_t i = 0; i < viewNum; ++i) {
4✔
985
    SViewVersion *view = &views[i];
2✔
986
    view->dbId = htobe64(view->dbId);
2✔
987
    view->viewId = htobe64(view->viewId);
2✔
988
    view->version = htonl(view->version);
2✔
989
  }
990

991
  tscDebug("hb got %u expired view, valueLen:%lu", viewNum, sizeof(SViewVersion) * viewNum);
2!
992

993
  if (NULL == req->info) {
2!
994
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
×
995
    if (NULL == req->info) {
×
996
      TSC_ERR_JRET(terrno);
×
997
    }
998
  }
999

1000
  SKv kv = {
2✔
1001
      .key = HEARTBEAT_KEY_DYN_VIEW,
1002
      .valueLen = sizeof(SDynViewVersion),
1003
      .value = pDynViewVer,
1004
  };
1005

1006
  TSC_ERR_JRET(taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)));
2!
1007

1008
  kv.key = HEARTBEAT_KEY_VIEWINFO;
2✔
1009
  kv.valueLen = sizeof(SViewVersion) * viewNum;
2✔
1010
  kv.value = views;
2✔
1011

1012
  TSC_ERR_JRET(taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)));
2!
1013
  return TSDB_CODE_SUCCESS;
2✔
1014
_return:
×
1015
  taosMemoryFree(views);
×
1016
  taosMemoryFree(pDynViewVer);
×
1017
  return code;
×
1018
}
1019

1020
int32_t hbGetExpiredTSMAInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *pReq) {
33,799✔
1021
  int32_t       code = 0;
33,799✔
1022
  uint32_t      tsmaNum = 0;
33,799✔
1023
  STSMAVersion *tsmas = NULL;
33,799✔
1024

1025
  code = catalogGetExpiredTsmas(pCatalog, &tsmas, &tsmaNum);
33,799✔
1026
  if (code) {
33,799!
1027
    taosMemoryFree(tsmas);
×
1028
    return code;
×
1029
  }
1030

1031
  if (tsmaNum <= 0) {
33,799✔
1032
    taosMemoryFree(tsmas);
33,451✔
1033
    return TSDB_CODE_SUCCESS;
33,451✔
1034
  }
1035

1036
  for (int32_t i = 0; i < tsmaNum; ++i) {
710✔
1037
    STSMAVersion *tsma = &tsmas[i];
362✔
1038
    tsma->dbId = htobe64(tsma->dbId);
362✔
1039
    tsma->tsmaId = htobe64(tsma->tsmaId);
362✔
1040
    tsma->version = htonl(tsma->version);
362✔
1041
  }
1042

1043
  tscDebug("hb got %d expred tsmas, valueLen: %lu", tsmaNum, sizeof(STSMAVersion) * tsmaNum);
348!
1044

1045
  if (!pReq->info) {
348!
1046
    pReq->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
×
1047
    if (!pReq->info) {
×
1048
      taosMemoryFree(tsmas);
×
1049
      return terrno;
×
1050
    }
1051
  }
1052

1053
  SKv kv = {.key = HEARTBEAT_KEY_TSMA, .valueLen = sizeof(STSMAVersion) * tsmaNum, .value = tsmas};
348✔
1054
  code = taosHashPut(pReq->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
348✔
1055
  if (TSDB_CODE_SUCCESS != code) {
348!
1056
    taosMemoryFree(tsmas);
×
1057
    return code;
×
1058
  }
1059
  return TSDB_CODE_SUCCESS;
348✔
1060
}
1061

1062
int32_t hbGetAppInfo(int64_t clusterId, SClientHbReq *req) {
109,931✔
1063
  SAppHbReq *pApp = taosHashGet(clientHbMgr.appSummary, &clusterId, sizeof(clusterId));
109,931✔
1064
  if (NULL != pApp) {
109,931!
1065
    (void)memcpy(&req->app, pApp, sizeof(*pApp));
109,931✔
1066
  } else {
1067
    (void)memset(&req->app.summary, 0, sizeof(req->app.summary));
×
1068
    req->app.pid = taosGetPId();
×
1069
    req->app.appId = clientHbMgr.appId;
×
1070
    TSC_ERR_RET(taosGetAppName(req->app.name, NULL));
×
1071
  }
1072

1073
  return TSDB_CODE_SUCCESS;
109,931✔
1074
}
1075

1076
int32_t hbQueryHbReqHandle(SClientHbKey *connKey, void *param, SClientHbReq *req) {
114,113✔
1077
  int32_t   code = 0;
114,113✔
1078
  SHbParam *hbParam = (SHbParam *)param;
114,113✔
1079
  SCatalog *pCatalog = NULL;
114,113✔
1080

1081
  code = hbGetQueryBasicInfo(connKey, req);
114,113✔
1082
  if (code != TSDB_CODE_SUCCESS) {
114,113✔
1083
    tscWarn("hbGetQueryBasicInfo failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
4,182!
1084
    return code;
4,182✔
1085
  }
1086

1087
  if (hbParam->reqCnt == 0) {
109,931✔
1088
    code = catalogGetHandle(hbParam->clusterId, &pCatalog);
33,799✔
1089
    if (code != TSDB_CODE_SUCCESS) {
33,799!
1090
      tscWarn("catalogGetHandle failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1091
      return code;
×
1092
    }
1093

1094
    code = hbGetAppInfo(hbParam->clusterId, req);
33,799✔
1095
    if (TSDB_CODE_SUCCESS != code) {
33,799!
1096
      tscWarn("getAppInfo failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1097
      return code;
×
1098
    }
1099

1100
    if (!taosHashGet(clientHbMgr.appHbHash, &hbParam->clusterId, sizeof(hbParam->clusterId))) {
33,799✔
1101
      code = hbGetExpiredUserInfo(connKey, pCatalog, req);
30,085✔
1102
      if (TSDB_CODE_SUCCESS != code) {
30,085!
1103
        tscWarn("hbGetExpiredUserInfo failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1104
        return code;
×
1105
      }
1106
      if (clientHbMgr.appHbHash) {
30,085✔
1107
        code = taosHashPut(clientHbMgr.appHbHash, &hbParam->clusterId, sizeof(uint64_t), NULL, 0);
6,303✔
1108
        if (TSDB_CODE_SUCCESS != code) {
6,303!
1109
          tscWarn("hbQueryHbReqHandle put clusterId failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId,
×
1110
                  tstrerror(code));
1111
          return code;
×
1112
        }
1113
      }
1114
    }
1115

1116
    // invoke after hbGetExpiredUserInfo
1117
    if (2 != atomic_load_8(&hbParam->pAppHbMgr->connHbFlag)) {
33,799✔
1118
      code = hbGetUserAuthInfo(connKey, hbParam, req);
2,337✔
1119
      if (TSDB_CODE_SUCCESS != code) {
2,337!
1120
        tscWarn("hbGetUserAuthInfo failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1121
        return code;
×
1122
      }
1123
      atomic_store_8(&hbParam->pAppHbMgr->connHbFlag, 1);
2,337✔
1124
    }
1125

1126
    code = hbGetExpiredDBInfo(connKey, pCatalog, req);
33,799✔
1127
    if (TSDB_CODE_SUCCESS != code) {
33,799!
1128
      tscWarn("hbGetExpiredDBInfo failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1129
      return code;
×
1130
    }
1131

1132
    code = hbGetExpiredStbInfo(connKey, pCatalog, req);
33,799✔
1133
    if (TSDB_CODE_SUCCESS != code) {
33,799!
1134
      tscWarn("hbGetExpiredStbInfo failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1135
      return code;
×
1136
    }
1137

1138
#ifdef TD_ENTERPRISE
1139
    code = hbGetExpiredViewInfo(connKey, pCatalog, req);
33,799✔
1140
    if (TSDB_CODE_SUCCESS != code) {
33,799!
1141
      tscWarn("hbGetExpiredViewInfo failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1142
      return code;
×
1143
    }
1144
#endif
1145
    code = hbGetExpiredTSMAInfo(connKey, pCatalog, req);
33,799✔
1146
    if (TSDB_CODE_SUCCESS != code) {
33,799!
1147
      tscWarn("hbGetExpiredTSMAInfo failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1148
      return code;
×
1149
    }
1150
  } else {
1151
    code = hbGetAppInfo(hbParam->clusterId, req);
76,132✔
1152
    if (TSDB_CODE_SUCCESS != code) {
76,132!
1153
      tscWarn("hbGetAppInfo failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1154
      return code;
×
1155
    }
1156
  }
1157

1158
  ++hbParam->reqCnt;  // success to get catalog info
109,931✔
1159

1160
  return TSDB_CODE_SUCCESS;
109,931✔
1161
}
1162

1163
static FORCE_INLINE void hbMgrInitHandle() {
1164
  // init all handle
1165
  clientHbMgr.reqHandle[CONN_TYPE__QUERY] = hbQueryHbReqHandle;
3,968✔
1166
  clientHbMgr.reqHandle[CONN_TYPE__TMQ] = hbMqHbReqHandle;
3,968✔
1167

1168
  clientHbMgr.rspHandle[CONN_TYPE__QUERY] = hbQueryHbRspHandle;
3,968✔
1169
  clientHbMgr.rspHandle[CONN_TYPE__TMQ] = hbMqHbRspHandle;
3,968✔
1170
}
3,968✔
1171

1172
int32_t hbGatherAllInfo(SAppHbMgr *pAppHbMgr, SClientHbBatchReq **pBatchReq) {
37,481✔
1173
  *pBatchReq = taosMemoryCalloc(1, sizeof(SClientHbBatchReq));
37,481✔
1174
  if (pBatchReq == NULL) {
37,481!
1175
    return terrno;
×
1176
  }
1177
  int32_t connKeyCnt = atomic_load_32(&pAppHbMgr->connKeyCnt);
37,481✔
1178
  (*pBatchReq)->reqs = taosArrayInit(connKeyCnt, sizeof(SClientHbReq));
37,481✔
1179
  if (!(*pBatchReq)->reqs) {
37,481!
1180
    tFreeClientHbBatchReq(*pBatchReq);
×
1181
    return terrno;
×
1182
  }
1183

1184
  int64_t  maxIpWhiteVer = 0;
37,481✔
1185
  void    *pIter = NULL;
37,481✔
1186
  SHbParam param = {0};
37,481✔
1187
  while ((pIter = taosHashIterate(pAppHbMgr->activeInfo, pIter))) {
154,526✔
1188
    SClientHbReq *pOneReq = pIter;
117,045✔
1189
    SClientHbKey *connKey = &pOneReq->connKey;
117,045✔
1190
    STscObj      *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
117,045✔
1191

1192
    if (!pTscObj || atomic_load_8(&pTscObj->dropped) == 1) {
117,045✔
1193
      if (pTscObj) releaseTscObj(connKey->tscRid);
2,932✔
1194
      continue;
2,932✔
1195
    }
1196

1197
    pOneReq = taosArrayPush((*pBatchReq)->reqs, pOneReq);
114,113✔
1198
    if (NULL == pOneReq) {
114,113!
1199
      releaseTscObj(connKey->tscRid);
×
1200
      continue;
×
1201
    }
1202

1203
    switch (connKey->connType) {
114,113!
1204
      case CONN_TYPE__QUERY: {
114,113✔
1205
        if (param.clusterId == 0) {
114,113✔
1206
          // init
1207
          param.clusterId = pOneReq->clusterId;
37,252✔
1208
          param.pAppHbMgr = pAppHbMgr;
37,252✔
1209
          param.connHbFlag = atomic_load_8(&pAppHbMgr->connHbFlag);
37,252✔
1210
        }
1211
        break;
114,113✔
1212
      }
1213
      default:
×
1214
        break;
×
1215
    }
1216
    if (clientHbMgr.reqHandle[connKey->connType]) {
114,113!
1217
      int32_t code = (*clientHbMgr.reqHandle[connKey->connType])(connKey, &param, pOneReq);
114,113✔
1218
      if (code) {
114,113✔
1219
        tscWarn("hbGatherAllInfo failed since %s, tscRid:%" PRIi64 ", connType:%" PRIi8, tstrerror(code),
4,182!
1220
                connKey->tscRid, connKey->connType);
1221
      }
1222
    }
1223

1224
    int64_t ver = atomic_load_64(&pTscObj->whiteListInfo.ver);
114,113✔
1225
    maxIpWhiteVer = TMAX(maxIpWhiteVer, ver);
114,113✔
1226
    releaseTscObj(connKey->tscRid);
114,113✔
1227
  }
1228
  (*pBatchReq)->ipWhiteList = maxIpWhiteVer;
37,481✔
1229

1230
  return TSDB_CODE_SUCCESS;
37,481✔
1231
}
1232

1233
void hbThreadFuncUnexpectedStopped(void) { atomic_store_8(&clientHbMgr.threadStop, 2); }
×
1234

1235
void hbMergeSummary(SAppClusterSummary *dst, SAppClusterSummary *src) {
7,474✔
1236
  dst->numOfInsertsReq += src->numOfInsertsReq;
7,474✔
1237
  dst->numOfInsertRows += src->numOfInsertRows;
7,474✔
1238
  dst->insertElapsedTime += src->insertElapsedTime;
7,474✔
1239
  dst->insertBytes += src->insertBytes;
7,474✔
1240
  dst->fetchBytes += src->fetchBytes;
7,474✔
1241
  dst->queryElapsedTime += src->queryElapsedTime;
7,474✔
1242
  dst->numOfSlowQueries += src->numOfSlowQueries;
7,474✔
1243
  dst->totalRequests += src->totalRequests;
7,474✔
1244
  dst->currentRequests += src->currentRequests;
7,474✔
1245
}
7,474✔
1246

1247
int32_t hbGatherAppInfo(void) {
40,411✔
1248
  SAppHbReq req = {0};
40,411✔
1249
  int32_t   code = TSDB_CODE_SUCCESS;
40,411✔
1250
  int       sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
40,411✔
1251
  if (sz > 0) {
40,411!
1252
    req.pid = taosGetPId();
40,411✔
1253
    req.appId = clientHbMgr.appId;
40,411✔
1254
    TSC_ERR_RET(taosGetAppName(req.name, NULL));
40,411!
1255
  }
1256

1257
  taosHashClear(clientHbMgr.appSummary);
40,411✔
1258

1259
  for (int32_t i = 0; i < sz; ++i) {
88,326✔
1260
    SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
47,915✔
1261
    if (pAppHbMgr == NULL) continue;
47,915!
1262

1263
    int64_t    clusterId = pAppHbMgr->pAppInstInfo->clusterId;
47,915✔
1264
    SAppHbReq *pApp = taosHashGet(clientHbMgr.appSummary, &clusterId, sizeof(clusterId));
47,915✔
1265
    if (NULL == pApp) {
47,915✔
1266
      (void)memcpy(&req.summary, &pAppHbMgr->pAppInstInfo->summary, sizeof(req.summary));
40,441✔
1267
      req.startTime = pAppHbMgr->startTime;
40,441✔
1268
      TSC_ERR_RET(taosHashPut(clientHbMgr.appSummary, &clusterId, sizeof(clusterId), &req, sizeof(req)));
40,441!
1269
    } else {
1270
      if (pAppHbMgr->startTime < pApp->startTime) {
7,474!
1271
        pApp->startTime = pAppHbMgr->startTime;
×
1272
      }
1273

1274
      hbMergeSummary(&pApp->summary, &pAppHbMgr->pAppInstInfo->summary);
7,474✔
1275
    }
1276
  }
1277

1278
  return TSDB_CODE_SUCCESS;
40,411✔
1279
}
1280

1281
static void *hbThreadFunc(void *param) {
3,968✔
1282
  setThreadName("hb");
3,968✔
1283
#ifdef WINDOWS
1284
  if (taosCheckCurrentInDll()) {
1285
    atexit(hbThreadFuncUnexpectedStopped);
1286
  }
1287
#endif
1288
  while (1) {
38,185✔
1289
    if (1 == clientHbMgr.threadStop) {
42,153✔
1290
      break;
1,722✔
1291
    }
1292

1293
    if (TSDB_CODE_SUCCESS != taosThreadMutexLock(&clientHbMgr.lock)) {
40,431!
1294
      tscError("taosThreadMutexLock failed");
×
1295
      return NULL;
×
1296
    }
1297

1298
    int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
40,431✔
1299
    if (sz > 0) {
40,431✔
1300
      if (TSDB_CODE_SUCCESS != hbGatherAppInfo()) {
40,411!
1301
        tscError("hbGatherAppInfo failed");
×
1302
        return NULL;
×
1303
      }
1304
      if (sz > 1 && !clientHbMgr.appHbHash) {
40,411✔
1305
        clientHbMgr.appHbHash = taosHashInit(0, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), true, HASH_NO_LOCK);
175✔
1306
        if (NULL == clientHbMgr.appHbHash) {
175!
1307
          tscError("taosHashInit failed");
×
1308
          return NULL;
×
1309
        }
1310
      }
1311
      taosHashClear(clientHbMgr.appHbHash);
40,411✔
1312
    }
1313

1314
    for (int i = 0; i < sz; i++) {
88,346✔
1315
      SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
47,915✔
1316
      if (pAppHbMgr == NULL) {
47,915!
1317
        continue;
10,434✔
1318
      }
1319

1320
      int32_t connCnt = atomic_load_32(&pAppHbMgr->connKeyCnt);
47,915✔
1321
      if (connCnt == 0) {
47,915✔
1322
        continue;
10,434✔
1323
      }
1324
      SClientHbBatchReq *pReq = NULL;
37,481✔
1325
      int32_t            code = hbGatherAllInfo(pAppHbMgr, &pReq);
37,481✔
1326
      if (TSDB_CODE_SUCCESS != code || taosArrayGetP(clientHbMgr.appHbMgrs, i) == NULL) {
37,481!
1327
        terrno = code ? code : TSDB_CODE_OUT_OF_RANGE;
×
1328
        tFreeClientHbBatchReq(pReq);
×
1329
        continue;
×
1330
      }
1331
      int tlen = tSerializeSClientHbBatchReq(NULL, 0, pReq);
37,481✔
1332
      if (tlen == -1) {
37,481!
1333
        tFreeClientHbBatchReq(pReq);
×
1334
        break;
×
1335
      }
1336
      void *buf = taosMemoryMalloc(tlen);
37,481✔
1337
      if (buf == NULL) {
37,481!
1338
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1339
        tFreeClientHbBatchReq(pReq);
×
1340
        // hbClearReqInfo(pAppHbMgr);
1341
        break;
×
1342
      }
1343

1344
      if (tSerializeSClientHbBatchReq(buf, tlen, pReq) == -1) {
37,481!
1345
        tFreeClientHbBatchReq(pReq);
×
1346
        taosMemoryFree(buf);
×
1347
        break;
×
1348
      }
1349
      SMsgSendInfo *pInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
37,481✔
1350

1351
      if (pInfo == NULL) {
37,481!
1352
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1353
        tFreeClientHbBatchReq(pReq);
×
1354
        // hbClearReqInfo(pAppHbMgr);
1355
        taosMemoryFree(buf);
×
1356
        break;
×
1357
      }
1358
      pInfo->fp = hbAsyncCallBack;
37,481✔
1359
      pInfo->msgInfo.pData = buf;
37,481✔
1360
      pInfo->msgInfo.len = tlen;
37,481✔
1361
      pInfo->msgType = TDMT_MND_HEARTBEAT;
37,481✔
1362
      pInfo->param = taosMemoryMalloc(sizeof(int32_t));
37,481✔
1363
      if (pInfo->param  == NULL) {
37,481!
1364
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1365
        tFreeClientHbBatchReq(pReq);
×
1366
        // hbClearReqInfo(pAppHbMgr);
1367
        taosMemoryFree(buf);
×
1368
        taosMemoryFree(pInfo);
×
1369
        break;
×
1370
      }
1371
      *(int32_t *)pInfo->param = i;
37,481✔
1372
      pInfo->paramFreeFp = taosMemoryFree;
37,481✔
1373
      pInfo->requestId = generateRequestId();
37,481✔
1374
      pInfo->requestObjRefId = 0;
37,481✔
1375

1376
      SAppInstInfo *pAppInstInfo = pAppHbMgr->pAppInstInfo;
37,481✔
1377
      SEpSet        epSet = getEpSet_s(&pAppInstInfo->mgmtEp);
37,481✔
1378
      if (TSDB_CODE_SUCCESS != asyncSendMsgToServer(pAppInstInfo->pTransporter, &epSet, NULL, pInfo)) {
37,481!
1379
        tscWarn("failed to async send msg to server");
×
1380
      }
1381
      tFreeClientHbBatchReq(pReq);
37,481!
1382
      // hbClearReqInfo(pAppHbMgr);
1383
      (void)atomic_add_fetch_32(&pAppHbMgr->reportCnt, 1);
37,481✔
1384
    }
1385

1386
    if (TSDB_CODE_SUCCESS != taosThreadMutexUnlock(&clientHbMgr.lock)) {
40,431!
1387
      tscError("taosThreadMutexLock failed");
×
1388
      return NULL;
×
1389
    }
1390
    taosMsleep(HEARTBEAT_INTERVAL);
40,431✔
1391
  }
1392
  taosHashCleanup(clientHbMgr.appHbHash);
1,722✔
1393
  return NULL;
1,722✔
1394
}
1395

1396
static int32_t hbCreateThread() {
3,968✔
1397
  int32_t      code = TSDB_CODE_SUCCESS;
3,968✔
1398
  TdThreadAttr thAttr;
1399
  TSC_ERR_JRET(taosThreadAttrInit(&thAttr));
3,968!
1400
  TSC_ERR_JRET(taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE));
3,968!
1401

1402
  if (taosThreadCreate(&clientHbMgr.thread, &thAttr, hbThreadFunc, NULL) != 0) {
3,968!
1403
    terrno = TAOS_SYSTEM_ERROR(errno);
×
1404
    TSC_ERR_RET(terrno);
×
1405
  }
1406
  (void)taosThreadAttrDestroy(&thAttr);
3,968✔
1407
_return:
3,968✔
1408

1409
  if (code) {
3,968!
1410
    terrno = TAOS_SYSTEM_ERROR(errno);
×
1411
    TSC_ERR_RET(terrno);
×
1412
  }
1413

1414
  return code;
3,968✔
1415
}
1416

1417
static void hbStopThread() {
3,975✔
1418
  if (0 == atomic_load_8(&clientHbMgr.inited)) {
3,975✔
1419
    return;
7✔
1420
  }
1421
  if (atomic_val_compare_exchange_8(&clientHbMgr.threadStop, 0, 1)) {
3,968!
1422
    tscDebug("hb thread already stopped");
×
1423
    return;
×
1424
  }
1425

1426
  int32_t code = TSDB_CODE_SUCCESS;
3,968✔
1427
  // thread quit mode kill or inner exit from self-thread
1428
  if (clientHbMgr.quitByKill) {
3,968✔
1429
    code = taosThreadKill(clientHbMgr.thread, 0);
2,246✔
1430
    if (TSDB_CODE_SUCCESS != code) {
2,246!
1431
      tscError("taosThreadKill failed since %s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1432
    }
1433
  } else {
1434
    code = taosThreadJoin(clientHbMgr.thread, NULL);
1,722✔
1435
    if (TSDB_CODE_SUCCESS != code) {
1,722!
1436
      tscError("taosThreadJoin failed since %s", tstrerror(TAOS_SYSTEM_ERROR(errno)));
×
1437
    }
1438
  }
1439

1440
  tscDebug("hb thread stopped");
3,968✔
1441
}
1442

1443
int32_t appHbMgrInit(SAppInstInfo *pAppInstInfo, char *key, SAppHbMgr **pAppHbMgr) {
4,200✔
1444
  int32_t code = TSDB_CODE_SUCCESS;
4,200✔
1445
  TSC_ERR_RET(hbMgrInit());
4,200!
1446
  *pAppHbMgr = taosMemoryMalloc(sizeof(SAppHbMgr));
4,200✔
1447
  if (*pAppHbMgr == NULL) {
4,200!
1448
    TSC_ERR_JRET(terrno);
×
1449
  }
1450
  // init stat
1451
  (*pAppHbMgr)->startTime = taosGetTimestampMs();
4,200✔
1452
  (*pAppHbMgr)->connKeyCnt = 0;
4,200✔
1453
  (*pAppHbMgr)->connHbFlag = 0;
4,200✔
1454
  (*pAppHbMgr)->reportCnt = 0;
4,200✔
1455
  (*pAppHbMgr)->reportBytes = 0;
4,200✔
1456
  (*pAppHbMgr)->key = taosStrdup(key);
4,200✔
1457
  if ((*pAppHbMgr)->key == NULL) {
4,200!
1458
    TSC_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
×
1459
  }
1460

1461
  // init app info
1462
  (*pAppHbMgr)->pAppInstInfo = pAppInstInfo;
4,200✔
1463

1464
  // init hash info
1465
  (*pAppHbMgr)->activeInfo = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
4,200✔
1466

1467
  if ((*pAppHbMgr)->activeInfo == NULL) {
4,200!
1468
    TSC_ERR_JRET(terrno);
×
1469
  }
1470

1471
  // taosHashSetFreeFp(pAppHbMgr->activeInfo, tFreeClientHbReq);
1472

1473
  TSC_ERR_JRET(taosThreadMutexLock(&clientHbMgr.lock));
4,200!
1474
  if (taosArrayPush(clientHbMgr.appHbMgrs, &(*pAppHbMgr)) == NULL) {
8,400!
1475
    code = terrno;
×
1476
    (void)taosThreadMutexUnlock(&clientHbMgr.lock);
×
1477
    goto _return;
×
1478
  }
1479
  (*pAppHbMgr)->idx = taosArrayGetSize(clientHbMgr.appHbMgrs) - 1;
4,200✔
1480
  TSC_ERR_JRET(taosThreadMutexUnlock(&clientHbMgr.lock));
4,200!
1481

1482
  return TSDB_CODE_SUCCESS;
4,200✔
1483
_return:
×
1484
  taosMemoryFree(*pAppHbMgr);
×
1485
  return code;
×
1486
}
1487

1488
void hbFreeAppHbMgr(SAppHbMgr *pTarget) {
4,200✔
1489
  void *pIter = taosHashIterate(pTarget->activeInfo, NULL);
4,200✔
1490
  while (pIter != NULL) {
4,865✔
1491
    SClientHbReq *pOneReq = pIter;
665!
1492
    tFreeClientHbReq(pOneReq);
1493
    pIter = taosHashIterate(pTarget->activeInfo, pIter);
665✔
1494
  }
1495
  taosHashCleanup(pTarget->activeInfo);
4,200✔
1496
  pTarget->activeInfo = NULL;
4,200✔
1497

1498
  taosMemoryFree(pTarget->key);
4,200✔
1499
  taosMemoryFree(pTarget);
4,200✔
1500
}
4,200✔
1501

1502
void hbRemoveAppHbMrg(SAppHbMgr **pAppHbMgr) {
×
1503
  int32_t code = TSDB_CODE_SUCCESS;
×
1504
  code = taosThreadMutexLock(&clientHbMgr.lock);
×
1505
  if (TSDB_CODE_SUCCESS != code) {
×
1506
    tscError("failed to lock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1507
  }
1508
  int32_t mgrSize = taosArrayGetSize(clientHbMgr.appHbMgrs);
×
1509
  for (int32_t i = 0; i < mgrSize; ++i) {
×
1510
    SAppHbMgr *pItem = taosArrayGetP(clientHbMgr.appHbMgrs, i);
×
1511
    if (pItem == *pAppHbMgr) {
×
1512
      hbFreeAppHbMgr(*pAppHbMgr);
×
1513
      *pAppHbMgr = NULL;
×
1514
      taosArraySet(clientHbMgr.appHbMgrs, i, pAppHbMgr);
×
1515
      break;
×
1516
    }
1517
  }
1518
  code = taosThreadMutexUnlock(&clientHbMgr.lock);
×
1519
  if (TSDB_CODE_SUCCESS != code) {
×
1520
    tscError("failed to unlock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1521
  }
1522
}
×
1523

1524
void appHbMgrCleanup(void) {
3,968✔
1525
  int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
3,968✔
1526
  for (int i = 0; i < sz; i++) {
8,168✔
1527
    SAppHbMgr *pTarget = taosArrayGetP(clientHbMgr.appHbMgrs, i);
4,200✔
1528
    if (pTarget == NULL) continue;
4,200!
1529
    hbFreeAppHbMgr(pTarget);
4,200✔
1530
  }
1531
}
3,968✔
1532

1533
int32_t hbMgrInit() {
4,200✔
1534
  // init once
1535
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 0, 1);
4,200✔
1536
  if (old == 1) return 0;
4,200✔
1537

1538
  clientHbMgr.appId = tGenIdPI64();
3,968✔
1539
  tscDebug("app %" PRIx64 " initialized", clientHbMgr.appId);
3,968✔
1540

1541
  clientHbMgr.appSummary = taosHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
3,968✔
1542
  if (NULL == clientHbMgr.appSummary) {
3,968!
1543
    uError("hbMgrInit:taosHashInit error") return terrno;
×
1544
  }
1545
  clientHbMgr.appHbMgrs = taosArrayInit(0, sizeof(void *));
3,968✔
1546
  if (NULL == clientHbMgr.appHbMgrs) {
3,968!
1547
    uError("hbMgrInit:taosArrayInit error") return terrno;
×
1548
  }
1549
  TdThreadMutexAttr attr = {0};
3,968✔
1550

1551
  int ret = taosThreadMutexAttrInit(&attr);
3,968✔
1552
  if (ret != 0) {
3,968!
1553
    uError("hbMgrInit:taosThreadMutexAttrInit error") return ret;
×
1554
  }
1555

1556
  ret = taosThreadMutexAttrSetType(&attr, PTHREAD_MUTEX_RECURSIVE);
3,968✔
1557
  if (ret != 0) {
3,968!
1558
    uError("hbMgrInit:taosThreadMutexAttrSetType error") return ret;
×
1559
  }
1560

1561
  ret = taosThreadMutexInit(&clientHbMgr.lock, &attr);
3,968✔
1562
  if (ret != 0) {
3,968!
1563
    uError("hbMgrInit:taosThreadMutexInit error") return ret;
×
1564
  }
1565

1566
  ret = taosThreadMutexAttrDestroy(&attr);
3,968✔
1567
  if (ret != 0) {
3,968!
1568
    uError("hbMgrInit:taosThreadMutexAttrDestroy error") return ret;
×
1569
  }
1570

1571
  // init handle funcs
1572
  hbMgrInitHandle();
1573

1574
  // init backgroud thread
1575
  ret = hbCreateThread();
3,968✔
1576
  if (ret != 0) {
3,968!
1577
    uError("hbMgrInit:hbCreateThread error") return ret;
×
1578
  }
1579

1580
  return 0;
3,968✔
1581
}
1582

1583
void hbMgrCleanUp() {
3,975✔
1584
  hbStopThread();
3,975✔
1585

1586
  // destroy all appHbMgr
1587
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 1, 0);
3,975✔
1588
  if (old == 0) return;
3,975✔
1589

1590
  int32_t code = taosThreadMutexLock(&clientHbMgr.lock);
3,968✔
1591
  if (TSDB_CODE_SUCCESS != code) {
3,968!
1592
    tscError("failed to lock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1593
  }
1594
  appHbMgrCleanup();
3,968✔
1595
  taosArrayDestroy(clientHbMgr.appHbMgrs);
3,968✔
1596
  clientHbMgr.appHbMgrs = NULL;
3,968✔
1597
  code = taosThreadMutexUnlock(&clientHbMgr.lock);
3,968✔
1598
  if (TSDB_CODE_SUCCESS != code) {
3,968!
1599
    tscError("failed to unlock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1600
  }
1601
}
1602

1603
int32_t hbRegisterConnImpl(SAppHbMgr *pAppHbMgr, SClientHbKey connKey, int64_t clusterId) {
11,851✔
1604
  // init hash in activeinfo
1605
  void *data = taosHashGet(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
11,851✔
1606
  if (data != NULL) {
11,851!
1607
    return 0;
×
1608
  }
1609
  SClientHbReq hbReq = {0};
11,851✔
1610
  hbReq.connKey = connKey;
11,851✔
1611
  hbReq.clusterId = clusterId;
11,851✔
1612
  // hbReq.info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
1613

1614
  TSC_ERR_RET(taosHashPut(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey), &hbReq, sizeof(SClientHbReq)));
11,851!
1615

1616
  (void)atomic_add_fetch_32(&pAppHbMgr->connKeyCnt, 1);
11,851✔
1617
  return 0;
11,851✔
1618
}
1619

1620
int32_t hbRegisterConn(SAppHbMgr *pAppHbMgr, int64_t tscRefId, int64_t clusterId, int8_t connType) {
12,263✔
1621
  SClientHbKey connKey = {
12,263✔
1622
      .tscRid = tscRefId,
1623
      .connType = connType,
1624
  };
1625

1626
  switch (connType) {
12,263!
1627
    case CONN_TYPE__QUERY: {
11,851✔
1628
      return hbRegisterConnImpl(pAppHbMgr, connKey, clusterId);
11,851✔
1629
    }
1630
    case CONN_TYPE__TMQ: {
412✔
1631
      return 0;
412✔
1632
    }
1633
    default:
×
1634
      return 0;
×
1635
  }
1636
}
1637

1638
void hbDeregisterConn(STscObj *pTscObj, SClientHbKey connKey) {
11,695✔
1639
  int32_t code = taosThreadMutexLock(&clientHbMgr.lock);
11,695✔
1640
  if (TSDB_CODE_SUCCESS != code) {
11,695!
1641
    tscError("failed to lock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1642
  }
1643
  SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, pTscObj->appHbMgrIdx);
11,695✔
1644
  if (pAppHbMgr) {
11,695!
1645
    SClientHbReq *pReq = taosHashAcquire(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
11,695✔
1646
    if (pReq) {
11,695✔
1647
      tFreeClientHbReq(pReq);
1648
      code = taosHashRemove(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
11,186✔
1649
      if (TSDB_CODE_SUCCESS != code) {
11,186!
1650
        tscError("hbDeregisterConn taosHashRemove error, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1651
      }
1652
      taosHashRelease(pAppHbMgr->activeInfo, pReq);
11,186✔
1653
      (void)atomic_sub_fetch_32(&pAppHbMgr->connKeyCnt, 1);
11,186✔
1654
    }
1655
  }
1656
  code = taosThreadMutexUnlock(&clientHbMgr.lock);
11,695✔
1657
  if (TSDB_CODE_SUCCESS != code) {
11,695!
1658
    tscError("failed to unlock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1659
  }
1660
}
11,695✔
1661

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