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

taosdata / TDengine / #3562

20 Dec 2024 09:57AM UTC coverage: 26.655% (-32.2%) from 58.812%
#3562

push

travis-ci

web-flow
Merge pull request #29229 from taosdata/enh/TS-5749-3.0

enh: seperate tsdb async tasks to different thread pools

21498 of 109421 branches covered (19.65%)

Branch coverage included in aggregate %.

66 of 96 new or added lines in 7 files covered. (68.75%)

39441 existing lines in 157 files now uncovered.

35007 of 102566 relevant lines covered (34.13%)

53922.97 hits per line

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

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

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

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

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

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

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

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

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

85
    SClientHbReq    *pReq = NULL;
25✔
86
    SGetUserAuthRsp *pRsp = NULL;
25✔
87
    while ((pReq = taosHashIterate(hbMgr->activeInfo, pReq))) {
130✔
88
      STscObj *pTscObj = (STscObj *)acquireTscObj(pReq->connKey.tscRid);
105✔
89
      if (!pTscObj) {
105✔
90
        continue;
1✔
91
      }
92

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

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

121
      pTscObj->authVer = pRsp->version;
104✔
122

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

129
      if (pTscObj->passInfo.fp) {
104!
UNCOV
130
        SPassInfo *passInfo = &pTscObj->passInfo;
×
UNCOV
131
        int32_t    oldVer = atomic_load_32(&passInfo->ver);
×
UNCOV
132
        if (oldVer < pRsp->passVer) {
×
UNCOV
133
          atomic_store_32(&passInfo->ver, pRsp->passVer);
×
UNCOV
134
          if (passInfo->fp) {
×
UNCOV
135
            (*passInfo->fp)(passInfo->param, &pRsp->passVer, TAOS_NOTIFY_PASSVER);
×
136
          }
UNCOV
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) {
104!
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;
104✔
157
        int64_t         oldVer = atomic_load_64(&whiteListInfo->ver);
104✔
158
        atomic_store_64(&whiteListInfo->ver, pRsp->whiteListVer);
104✔
159
        tscDebug("update whitelist version of user %s from %" PRId64 " to %" PRId64 ", tscRid:%" PRIi64, pRsp->user,
104✔
160
                 oldVer, atomic_load_64(&whiteListInfo->ver), pTscObj->id);
161
      }
162
      releaseTscObj(pReq->connKey.tscRid);
104✔
163
    }
164
  }
165
  return 0;
24✔
166
}
167

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

175
  vgInfo->vgVersion = rsp->vgVersion;
327✔
176
  vgInfo->stateTs = rsp->stateTs;
327✔
177
  vgInfo->hashMethod = rsp->hashMethod;
327✔
178
  vgInfo->hashPrefix = rsp->hashPrefix;
327✔
179
  vgInfo->hashSuffix = rsp->hashSuffix;
327✔
180
  vgInfo->vgHash = taosHashInit(rsp->vgNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
327✔
181
  if (NULL == vgInfo->vgHash) {
327!
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) {
740✔
188
    SVgroupInfo *pInfo = taosArrayGet(rsp->pVgroupInfos, j);
413✔
189
    if (taosHashPut(vgInfo->vgHash, &pInfo->vgId, sizeof(int32_t), pInfo, sizeof(SVgroupInfo)) != 0) {
413!
190
      tscError("hash push failed, errno:%d", errno);
×
191
      code = terrno;
×
192
      goto _return;
×
193
    }
194
  }
195

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

202
  *pInfo = vgInfo;
327✔
203
  return code;
327✔
204
}
205

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

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

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

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

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

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

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

247
          TSC_ERR_JRET(catalogUpdateDBVgInfo(
96!
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) {
236✔
255
      tscDebug("hb db cfg rsp, db:%s, cfgVersion:%d", rsp->cfgRsp->db, rsp->cfgRsp->cfgVersion);
2!
256
      code = catalogUpdateDbCfg(pCatalog, rsp->cfgRsp->db, rsp->cfgRsp->dbId, rsp->cfgRsp);
2✔
257
      rsp->cfgRsp = NULL;
2✔
258
    }
259
    if (rsp->pTsmaRsp) {
236✔
260
      if (rsp->pTsmaRsp->pTsmas) {
14!
UNCOV
261
        for (int32_t i = 0; i < rsp->pTsmaRsp->pTsmas->size; ++i) {
×
UNCOV
262
          STableTSMAInfo *pTsma = taosArrayGetP(rsp->pTsmaRsp->pTsmas, i);
×
UNCOV
263
          if (NULL == pTsma) {
×
264
            TSC_ERR_JRET(TSDB_CODE_OUT_OF_RANGE);
×
265
          }
UNCOV
266
          TSC_ERR_JRET(catalogAsyncUpdateTSMA(pCatalog, &pTsma, rsp->dbTsmaVersion));
×
267
        }
UNCOV
268
        taosArrayClear(rsp->pTsmaRsp->pTsmas);
×
269
      } else {
270
        TSC_ERR_JRET(catalogAsyncUpdateDbTsmaVersion(pCatalog, rsp->dbTsmaVersion, rsp->db, rsp->dbId));
14!
271
      }
272
    }
273
  }
274

275
_return:
253✔
276

277
  tFreeSDbHbBatchRsp(&batchRsp);
253✔
278
  return code;
253✔
279
}
280

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

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

290
  int32_t numOfMeta = taosArrayGetSize(hbRsp.pMetaRsp);
110✔
291
  for (int32_t i = 0; i < numOfMeta; ++i) {
110!
UNCOV
292
    STableMetaRsp *rsp = taosArrayGet(hbRsp.pMetaRsp, i);
×
UNCOV
293
    if (NULL == rsp) {
×
294
      code = terrno;
×
295
      goto _return;
×
296
    }
UNCOV
297
    if (rsp->numOfColumns < 0) {
×
UNCOV
298
      tscDebug("hb to remove stb, db:%s, stb:%s", rsp->dbFName, rsp->stbName);
×
UNCOV
299
      TSC_ERR_JRET(catalogRemoveStbMeta(pCatalog, rsp->dbFName, rsp->dbId, rsp->stbName, rsp->suid));
×
300
    } else {
UNCOV
301
      tscDebug("hb to update stb, db:%s, stb:%s", rsp->dbFName, rsp->stbName);
×
UNCOV
302
      if (rsp->pSchemas[0].colId != PRIMARYKEY_TIMESTAMP_COL_ID) {
×
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

UNCOV
308
      TSC_ERR_JRET(catalogAsyncUpdateTableMeta(pCatalog, rsp));
×
309
    }
310
  }
311

312
  int32_t numOfIndex = taosArrayGetSize(hbRsp.pIndexRsp);
110✔
313
  for (int32_t i = 0; i < numOfIndex; ++i) {
110!
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:
110✔
323
  taosArrayDestroy(hbRsp.pIndexRsp);
110✔
324
  hbRsp.pIndexRsp = NULL;
110✔
325

326
  tFreeSSTbHbRsp(&hbRsp);
110✔
327
  return code;
110✔
328
}
329

UNCOV
330
static int32_t hbProcessDynViewRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
×
UNCOV
331
  return catalogUpdateDynViewVer(pCatalog, (SDynViewVersion *)value);
×
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

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

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

UNCOV
353
  int32_t numOfMeta = taosArrayGetSize(hbRsp.pViewRsp);
×
UNCOV
354
  for (int32_t i = 0; i < numOfMeta; ++i) {
×
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

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

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

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

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

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

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

407
static void hbProcessQueryRspKvs(int32_t kvNum, SArray *pKvs, struct SCatalog *pCatalog, SAppHbMgr *pAppHbMgr) {
346✔
408
  for (int32_t i = 0; i < kvNum; ++i) {
733✔
409
    SKv *kv = taosArrayGet(pKvs, i);
387✔
410
    if (NULL == kv) {
387!
411
      tscError("invalid hb kv, idx:%d", i);
×
412
      continue;
×
413
    }
414
    switch (kv->key) {
387!
415
      case HEARTBEAT_KEY_USER_AUTHINFO: {
24✔
416
        if (kv->valueLen <= 0 || NULL == kv->value) {
24!
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)) {
24!
421
          tscError("process user auth info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
422
          break;
×
423
        }
424
        break;
24✔
425
      }
426
      case HEARTBEAT_KEY_DBINFO: {
253✔
427
        if (kv->valueLen <= 0 || NULL == kv->value) {
253!
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)) {
253!
432
          tscError("process db info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
433
          break;
×
434
        }
435
        break;
253✔
436
      }
437
      case HEARTBEAT_KEY_STBINFO: {
110✔
438
        if (kv->valueLen <= 0 || NULL == kv->value) {
110!
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)) {
110!
443
          tscError("process stb info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
444
          break;
×
445
        }
446
        break;
110✔
447
      }
448
#ifdef TD_ENTERPRISE
UNCOV
449
      case HEARTBEAT_KEY_DYN_VIEW: {
×
UNCOV
450
        if (kv->valueLen <= 0 || NULL == kv->value) {
×
451
          tscError("invalid dyn view info, len:%d, value:%p", kv->valueLen, kv->value);
×
452
          break;
×
453
        }
UNCOV
454
        if (TSDB_CODE_SUCCESS != hbProcessDynViewRsp(kv->value, kv->valueLen, pCatalog)) {
×
455
          tscError("Process dyn view response failed, len: %d, value: %p", kv->valueLen, kv->value);
×
456
          break;
×
457
        }
UNCOV
458
        break;
×
459
      }
UNCOV
460
      case HEARTBEAT_KEY_VIEWINFO: {
×
UNCOV
461
        if (kv->valueLen <= 0 || NULL == kv->value) {
×
462
          tscError("invalid view info, len:%d, value:%p", kv->valueLen, kv->value);
×
463
          break;
×
464
        }
UNCOV
465
        if (TSDB_CODE_SUCCESS != hbProcessViewInfoRsp(kv->value, kv->valueLen, pCatalog)) {
×
466
          tscError("Process view info response failed, len: %d, value: %p", kv->valueLen, kv->value);
×
467
          break;
×
468
        }
UNCOV
469
        break;
×
470
      }
471
#endif
UNCOV
472
      case HEARTBEAT_KEY_TSMA: {
×
UNCOV
473
        if (kv->valueLen <= 0 || !kv->value) {
×
474
          tscError("Invalid tsma info, len: %d, value: %p", kv->valueLen, kv->value);
×
475
        }
UNCOV
476
        if (TSDB_CODE_SUCCESS != hbprocessTSMARsp(kv->value, kv->valueLen, pCatalog)) {
×
477
          tscError("Process tsma info response failed, len: %d, value: %p", kv->valueLen, kv->value);
×
478
        }
UNCOV
479
        break;
×
480
      }
481
      default:
×
482
        tscError("invalid hb key type:%d", kv->key);
×
483
        break;
×
484
    }
485
  }
486
}
346✔
487

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

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

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

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

518
      if (pRsp->query->killRid) {
3,285!
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) {
3,285!
530
        taos_close_internal(pTscObj);
×
531
      }
532

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

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

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

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

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

557
  taosHashRelease(pAppHbMgr->activeInfo, pReq);
3,293✔
558

559
  return TSDB_CODE_SUCCESS;
3,293✔
560
}
561

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

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

583
  int32_t rspNum = taosArrayGetSize(pRsp.rsps);
1,570✔
584

585
  (void)taosThreadMutexLock(&clientHbMgr.lock);
1,570✔
586

587
  SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, idx);
1,570✔
588
  if (pAppHbMgr == NULL) {
1,570!
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;
1,570✔
598

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

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

614
  if (rspNum) {
843!
615
    tscDebug("hb got %d rsp, %d empty rsp received before", rspNum,
843✔
616
             atomic_val_compare_exchange_32(&emptyRspNum, emptyRspNum, 0));
617
  } else {
UNCOV
618
    (void)atomic_add_fetch_32(&emptyRspNum, 1);
×
619
  }
620

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

629
  (void)taosThreadMutexUnlock(&clientHbMgr.lock);
843✔
630

631
  tFreeClientHbBatchRsp(&pRsp);
632

633
_return:
843✔
634
  taosMemoryFree(pMsg->pData);
843!
635
  taosMemoryFree(pMsg->pEpSet);
843!
636
  return code;
843✔
637
}
638

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

644
  void *pIter = taosHashIterate(pObj->pRequests, NULL);
1,639✔
645
  while (pIter != NULL) {
3,278✔
646
    int64_t     *rid = pIter;
1,639✔
647
    SRequestObj *pRequest = acquireRequest(*rid);
1,639✔
648
    if (NULL == pRequest) {
1,639✔
649
      pIter = taosHashIterate(pObj->pRequests, pIter);
1✔
650
      continue;
1✔
651
    }
652

653
    if (pRequest->killed || 0 == pRequest->body.queryJob) {
1,638✔
654
      (void)releaseRequest(*rid);
1,454✔
655
      pIter = taosHashIterate(pObj->pRequests, pIter);
1,454✔
656
      continue;
1,454✔
657
    }
658

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

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

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

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

697
    pIter = taosHashIterate(pObj->pRequests, pIter);
184✔
698
  }
699

700
  return code;
1,639✔
701
}
702

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

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

717
  hbBasic->connId = pTscObj->connId;
4,106✔
718

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

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

735
  int32_t code = hbBuildQueryDesc(hbBasic, pTscObj);
1,639✔
736
  if (code) {
1,639✔
737
    releaseTscObj(connKey->tscRid);
8✔
738
    if (hbBasic->queryDesc) {
8!
739
      taosArrayDestroyEx(hbBasic->queryDesc, tFreeClientHbQueryDesc);
8✔
740
    }
741
    taosMemoryFree(hbBasic);
8!
742
    return code;
8✔
743
  }
744

745
  req->query = hbBasic;
1,631✔
746
  releaseTscObj(connKey->tscRid);
1,631✔
747

748
  return TSDB_CODE_SUCCESS;
1,631✔
749
}
750

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

758
  int32_t code = 0;
644✔
759

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

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

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

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

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

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

821
  return code;
644✔
822
}
823

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

829
  code = catalogGetExpiredUsers(pCatalog, &users, &userNum);
1,648✔
830
  if (TSDB_CODE_SUCCESS != code) {
1,648!
831
    return code;
×
832
  }
833

834
  if (userNum <= 0) {
1,648✔
835
    taosMemoryFree(users);
635!
836
    return TSDB_CODE_SUCCESS;
635✔
837
  }
838

839
  for (int32_t i = 0; i < userNum; ++i) {
2,026✔
840
    SUserAuthVersion *user = &users[i];
1,013✔
841
    user->version = htonl(user->version);
1,013✔
842
  }
843

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

850
  tscDebug("hb got %d expired users, valueLen:%d", userNum, kv.valueLen);
1,013✔
851

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

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

866
  return TSDB_CODE_SUCCESS;
1,013✔
867
}
868

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

874
  code = catalogGetExpiredDBs(pCatalog, &dbs, &dbNum);
1,648✔
875
  if (TSDB_CODE_SUCCESS != code) {
1,648!
876
    return code;
×
877
  }
878

879
  if (dbNum <= 0) {
1,648✔
880
    taosMemoryFree(dbs);
1,358!
881
    return TSDB_CODE_SUCCESS;
1,358✔
882
  }
883

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

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

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

904
  tscDebug("hb got %d expired db, valueLen:%d", dbNum, kv.valueLen);
290✔
905

906
  if (NULL == req->info) {
290!
UNCOV
907
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
×
UNCOV
908
    if (NULL == req->info) {
×
909
      taosMemoryFree(dbs);
×
910
      return terrno;
×
911
    }
912
  }
913

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

920
  return TSDB_CODE_SUCCESS;
290✔
921
}
922

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

928
  code = catalogGetExpiredSTables(pCatalog, &stbs, &stbNum);
1,648✔
929
  if (TSDB_CODE_SUCCESS != code) {
1,648!
930
    return code;
×
931
  }
932

933
  if (stbNum <= 0) {
1,648✔
934
    taosMemoryFree(stbs);
1,520!
935
    return TSDB_CODE_SUCCESS;
1,520✔
936
  }
937

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

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

952
  tscDebug("hb got %d expired stb, valueLen:%d", stbNum, kv.valueLen);
128✔
953

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

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

968
  return TSDB_CODE_SUCCESS;
128✔
969
}
970

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

977
  TSC_ERR_JRET(catalogGetExpiredViews(pCatalog, &views, &viewNum, &pDynViewVer));
1,648!
978

979
  if (viewNum <= 0) {
1,648!
980
    taosMemoryFree(views);
1,648!
981
    taosMemoryFree(pDynViewVer);
1,648!
982
    return TSDB_CODE_SUCCESS;
1,648✔
983
  }
984

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

UNCOV
992
  tscDebug("hb got %u expired view, valueLen:%lu", viewNum, sizeof(SViewVersion) * viewNum);
×
993

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

UNCOV
1001
  SKv kv = {
×
1002
      .key = HEARTBEAT_KEY_DYN_VIEW,
1003
      .valueLen = sizeof(SDynViewVersion),
1004
      .value = pDynViewVer,
1005
  };
1006

UNCOV
1007
  TSC_ERR_JRET(taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)));
×
1008

UNCOV
1009
  kv.key = HEARTBEAT_KEY_VIEWINFO;
×
UNCOV
1010
  kv.valueLen = sizeof(SViewVersion) * viewNum;
×
UNCOV
1011
  kv.value = views;
×
1012

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

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

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

1032
  if (tsmaNum <= 0) {
1,648!
1033
    taosMemoryFree(tsmas);
1,648!
1034
    return TSDB_CODE_SUCCESS;
1,648✔
1035
  }
1036

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

UNCOV
1044
  tscDebug("hb got %d expred tsmas, valueLen: %lu", tsmaNum, sizeof(STSMAVersion) * tsmaNum);
×
1045

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

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

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

1074
  return TSDB_CODE_SUCCESS;
4,098✔
1075
}
1076

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

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

1088
  if (hbParam->reqCnt == 0) {
4,098✔
1089
    code = catalogGetHandle(hbParam->clusterId, &pCatalog);
1,648✔
1090
    if (code != TSDB_CODE_SUCCESS) {
1,648!
1091
      tscWarn("catalogGetHandle failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1092
      return code;
×
1093
    }
1094

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

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

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

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

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

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

1159
  ++hbParam->reqCnt;  // success to get catalog info
4,098✔
1160

1161
  return TSDB_CODE_SUCCESS;
4,098✔
1162
}
1163

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

1169
  clientHbMgr.rspHandle[CONN_TYPE__QUERY] = hbQueryHbRspHandle;
216✔
1170
  clientHbMgr.rspHandle[CONN_TYPE__TMQ] = hbMqHbRspHandle;
216✔
1171
}
216✔
1172

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

1185
  int64_t  maxIpWhiteVer = 0;
1,656✔
1186
  void    *pIter = NULL;
1,656✔
1187
  SHbParam param = {0};
1,656✔
1188
  while ((pIter = taosHashIterate(pAppHbMgr->activeInfo, pIter))) {
5,793✔
1189
    SClientHbReq *pOneReq = pIter;
4,137✔
1190
    SClientHbKey *connKey = &pOneReq->connKey;
4,137✔
1191
    STscObj      *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
4,137✔
1192

1193
    if (!pTscObj || atomic_load_8(&pTscObj->dropped) == 1) {
4,137!
1194
      if (pTscObj) releaseTscObj(connKey->tscRid);
31!
1195
      continue;
31✔
1196
    }
1197

1198
    tstrncpy(pOneReq->userApp, pTscObj->optionInfo.userApp, sizeof(pOneReq->userApp));
4,106✔
1199
    pOneReq->userIp = pTscObj->optionInfo.userIp;
4,106✔
1200

1201
    pOneReq = taosArrayPush((*pBatchReq)->reqs, pOneReq);
4,106✔
1202
    if (NULL == pOneReq) {
4,106!
1203
      releaseTscObj(connKey->tscRid);
×
1204
      continue;
×
1205
    }
1206

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

1228
    int64_t ver = atomic_load_64(&pTscObj->whiteListInfo.ver);
4,106✔
1229
    maxIpWhiteVer = TMAX(maxIpWhiteVer, ver);
4,106✔
1230
    releaseTscObj(connKey->tscRid);
4,106✔
1231
  }
1232
  (*pBatchReq)->ipWhiteList = maxIpWhiteVer;
1,656✔
1233

1234
  return TSDB_CODE_SUCCESS;
1,656✔
1235
}
1236

1237
void hbThreadFuncUnexpectedStopped(void) { atomic_store_8(&clientHbMgr.threadStop, 2); }
×
1238

1239
void hbMergeSummary(SAppClusterSummary *dst, SAppClusterSummary *src) {
1✔
1240
  dst->numOfInsertsReq += src->numOfInsertsReq;
1✔
1241
  dst->numOfInsertRows += src->numOfInsertRows;
1✔
1242
  dst->insertElapsedTime += src->insertElapsedTime;
1✔
1243
  dst->insertBytes += src->insertBytes;
1✔
1244
  dst->fetchBytes += src->fetchBytes;
1✔
1245
  dst->queryElapsedTime += src->queryElapsedTime;
1✔
1246
  dst->numOfSlowQueries += src->numOfSlowQueries;
1✔
1247
  dst->totalRequests += src->totalRequests;
1✔
1248
  dst->currentRequests += src->currentRequests;
1✔
1249
}
1✔
1250

1251
int32_t hbGatherAppInfo(void) {
1,972✔
1252
  SAppHbReq req = {0};
1,972✔
1253
  int32_t   code = TSDB_CODE_SUCCESS;
1,972✔
1254
  int       sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
1,972✔
1255
  if (sz > 0) {
1,972!
1256
    req.pid = taosGetPId();
1,972✔
1257
    req.appId = clientHbMgr.appId;
1,972✔
1258
    TSC_ERR_RET(taosGetAppName(req.name, NULL));
1,972!
1259
  }
1260

1261
  taosHashClear(clientHbMgr.appSummary);
1,972✔
1262

1263
  for (int32_t i = 0; i < sz; ++i) {
3,957✔
1264
    SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
1,985✔
1265
    if (pAppHbMgr == NULL) continue;
1,985!
1266

1267
    int64_t    clusterId = pAppHbMgr->pAppInstInfo->clusterId;
1,985✔
1268
    SAppHbReq *pApp = taosHashGet(clientHbMgr.appSummary, &clusterId, sizeof(clusterId));
1,985✔
1269
    if (NULL == pApp) {
1,985✔
1270
      (void)memcpy(&req.summary, &pAppHbMgr->pAppInstInfo->summary, sizeof(req.summary));
1,984✔
1271
      req.startTime = pAppHbMgr->startTime;
1,984✔
1272
      TSC_ERR_RET(taosHashPut(clientHbMgr.appSummary, &clusterId, sizeof(clusterId), &req, sizeof(req)));
1,984!
1273
    } else {
1274
      if (pAppHbMgr->startTime < pApp->startTime) {
1!
1275
        pApp->startTime = pAppHbMgr->startTime;
×
1276
      }
1277

1278
      hbMergeSummary(&pApp->summary, &pAppHbMgr->pAppInstInfo->summary);
1✔
1279
    }
1280
  }
1281

1282
  return TSDB_CODE_SUCCESS;
1,972✔
1283
}
1284

1285
static void *hbThreadFunc(void *param) {
216✔
1286
  setThreadName("hb");
216✔
1287
#ifdef WINDOWS
1288
  if (taosCheckCurrentInDll()) {
1289
    atexit(hbThreadFuncUnexpectedStopped);
1290
  }
1291
#endif
1292
  while (1) {
1,812✔
1293
    if (1 == clientHbMgr.threadStop) {
2,028✔
1294
      break;
54✔
1295
    }
1296

1297
    if (TSDB_CODE_SUCCESS != taosThreadMutexLock(&clientHbMgr.lock)) {
1,974!
1298
      tscError("taosThreadMutexLock failed");
×
1299
      return NULL;
×
1300
    }
1301

1302
    int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
1,974✔
1303
    if (sz > 0) {
1,974✔
1304
      if (TSDB_CODE_SUCCESS != hbGatherAppInfo()) {
1,972!
1305
        tscError("hbGatherAppInfo failed");
×
1306
        return NULL;
×
1307
      }
1308
      if (sz > 1 && !clientHbMgr.appHbHash) {
1,972✔
1309
        clientHbMgr.appHbHash = taosHashInit(0, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), true, HASH_NO_LOCK);
1✔
1310
        if (NULL == clientHbMgr.appHbHash) {
1!
1311
          tscError("taosHashInit failed");
×
1312
          return NULL;
×
1313
        }
1314
      }
1315
      taosHashClear(clientHbMgr.appHbHash);
1,972✔
1316
    }
1317

1318
    for (int i = 0; i < sz; i++) {
3,959✔
1319
      SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
1,985✔
1320
      if (pAppHbMgr == NULL) {
1,985!
1321
        continue;
329✔
1322
      }
1323

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

1347
      if (tSerializeSClientHbBatchReq(buf, tlen, pReq) == -1) {
1,656!
1348
        tFreeClientHbBatchReq(pReq);
×
1349
        taosMemoryFree(buf);
×
1350
        break;
×
1351
      }
1352
      SMsgSendInfo *pInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
1,656!
1353

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

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

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

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

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

1410
  if (code) {
216!
1411
    terrno = TAOS_SYSTEM_ERROR(errno);
×
1412
    TSC_ERR_RET(terrno);
×
1413
  }
1414

1415
  return code;
216✔
1416
}
1417

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

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

1441
  tscDebug("hb thread stopped");
216✔
1442
}
1443

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

1462
  // init app info
1463
  (*pAppHbMgr)->pAppInstInfo = pAppInstInfo;
218✔
1464

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

1468
  if ((*pAppHbMgr)->activeInfo == NULL) {
218!
1469
    TSC_ERR_JRET(terrno);
×
1470
  }
1471

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

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

1483
  return TSDB_CODE_SUCCESS;
218✔
1484
_return:
×
1485
  taosMemoryFree(*pAppHbMgr);
×
1486
  return code;
×
1487
}
1488

1489
void hbFreeAppHbMgr(SAppHbMgr *pTarget) {
218✔
1490
  void *pIter = taosHashIterate(pTarget->activeInfo, NULL);
218✔
1491
  while (pIter != NULL) {
285✔
1492
    SClientHbReq *pOneReq = pIter;
67!
1493
    tFreeClientHbReq(pOneReq);
1494
    pIter = taosHashIterate(pTarget->activeInfo, pIter);
67✔
1495
  }
1496
  taosHashCleanup(pTarget->activeInfo);
218✔
1497
  pTarget->activeInfo = NULL;
218✔
1498

1499
  taosMemoryFree(pTarget->key);
218!
1500
  taosMemoryFree(pTarget);
218!
1501
}
218✔
1502

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

1525
void appHbMgrCleanup(void) {
216✔
1526
  int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
216✔
1527
  for (int i = 0; i < sz; i++) {
434✔
1528
    SAppHbMgr *pTarget = taosArrayGetP(clientHbMgr.appHbMgrs, i);
218✔
1529
    if (pTarget == NULL) continue;
218!
1530
    hbFreeAppHbMgr(pTarget);
218✔
1531
  }
1532
}
216✔
1533

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

1539
  clientHbMgr.appId = tGenIdPI64();
216✔
1540
  tscDebug("app %" PRIx64 " initialized", clientHbMgr.appId);
216✔
1541

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

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

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

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

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

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

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

1581
  return 0;
216✔
1582
}
1583

1584
void hbMgrCleanUp() {
217✔
1585
  hbStopThread();
217✔
1586

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

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

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

1615
  TSC_ERR_RET(taosHashPut(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey), &hbReq, sizeof(SClientHbReq)));
803!
1616

1617
  (void)atomic_add_fetch_32(&pAppHbMgr->connKeyCnt, 1);
803✔
1618
  return 0;
803✔
1619
}
1620

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

1627
  switch (connType) {
808!
1628
    case CONN_TYPE__QUERY: {
803✔
1629
      return hbRegisterConnImpl(pAppHbMgr, connKey, clusterId);
803✔
1630
    }
1631
    case CONN_TYPE__TMQ: {
5✔
1632
      return 0;
5✔
1633
    }
1634
    default:
×
1635
      return 0;
×
1636
  }
1637
}
1638

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

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