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

taosdata / TDengine / #3561

19 Dec 2024 03:15AM UTC coverage: 58.812% (-1.3%) from 60.124%
#3561

push

travis-ci

web-flow
Merge pull request #29213 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

130770 of 287658 branches covered (45.46%)

Branch coverage included in aggregate %.

32 of 78 new or added lines in 4 files covered. (41.03%)

7347 existing lines in 166 files now uncovered.

205356 of 283866 relevant lines covered (72.34%)

7187865.64 hits per line

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

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

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

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

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

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

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

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

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

85
    SClientHbReq    *pReq = NULL;
3,488✔
86
    SGetUserAuthRsp *pRsp = NULL;
3,488✔
87
    while ((pReq = taosHashIterate(hbMgr->activeInfo, pReq))) {
7,791✔
88
      STscObj *pTscObj = (STscObj *)acquireTscObj(pReq->connKey.tscRid);
4,940✔
89
      if (!pTscObj) {
4,940✔
90
        continue;
321✔
91
      }
92

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

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

121
      pTscObj->authVer = pRsp->version;
3,920✔
122

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

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

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

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

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

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

202
  *pInfo = vgInfo;
2,787✔
203
  return code;
2,787✔
204
}
205

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

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

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

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

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

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

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

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

275
_return:
8,194✔
276

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

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

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

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

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

326
  tFreeSSTbHbRsp(&hbRsp);
5,725✔
327
  return code;
5,725✔
328
}
329

330
static int32_t hbProcessDynViewRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
3✔
331
  return catalogUpdateDynViewVer(pCatalog, (SDynViewVersion *)value);
3✔
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) {
3✔
344
  int32_t code = TSDB_CODE_SUCCESS;
3✔
345

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

353
  int32_t numOfMeta = taosArrayGetSize(hbRsp.pViewRsp);
3✔
354
  for (int32_t i = 0; i < numOfMeta; ++i) {
3!
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:
3✔
373
  taosArrayDestroy(hbRsp.pViewRsp);
3✔
374
  return code;
3✔
375
}
376

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

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

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

390
    if (!pTsmaInfo->pFuncs) {
3!
391
      tscDebug("hb to remove tsma: %s.%s", pTsmaInfo->dbFName, pTsmaInfo->name);
3!
392
      code = catalogRemoveTSMA(pCatalog, pTsmaInfo);
3✔
393
      tFreeAndClearTableTSMAInfo(pTsmaInfo);
3✔
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);
3!
400
  }
401

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

407
static void hbProcessQueryRspKvs(int32_t kvNum, SArray *pKvs, struct SCatalog *pCatalog, SAppHbMgr *pAppHbMgr) {
13,571✔
408
  for (int32_t i = 0; i < kvNum; ++i) {
29,800✔
409
    SKv *kv = taosArrayGet(pKvs, i);
16,229✔
410
    if (NULL == kv) {
16,229!
411
      tscError("invalid hb kv, idx:%d", i);
×
412
      continue;
×
413
    }
414
    switch (kv->key) {
16,229!
415
      case HEARTBEAT_KEY_USER_AUTHINFO: {
1,972✔
416
        if (kv->valueLen <= 0 || NULL == kv->value) {
1,972!
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)) {
1,972!
421
          tscError("process user auth info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
422
          break;
×
423
        }
424
        break;
1,972✔
425
      }
426
      case HEARTBEAT_KEY_DBINFO: {
8,194✔
427
        if (kv->valueLen <= 0 || NULL == kv->value) {
8,194!
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,194!
432
          tscError("process db info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
433
          break;
×
434
        }
435
        break;
8,194✔
436
      }
437
      case HEARTBEAT_KEY_STBINFO: {
5,725✔
438
        if (kv->valueLen <= 0 || NULL == kv->value) {
5,725!
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)) {
5,725!
443
          tscError("process stb info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
444
          break;
×
445
        }
446
        break;
5,725✔
447
      }
448
#ifdef TD_ENTERPRISE
449
      case HEARTBEAT_KEY_DYN_VIEW: {
3✔
450
        if (kv->valueLen <= 0 || NULL == kv->value) {
3!
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)) {
3!
455
          tscError("Process dyn view response failed, len: %d, value: %p", kv->valueLen, kv->value);
×
456
          break;
×
457
        }
458
        break;
3✔
459
      }
460
      case HEARTBEAT_KEY_VIEWINFO: {
3✔
461
        if (kv->valueLen <= 0 || NULL == kv->value) {
3!
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)) {
3!
466
          tscError("Process view info response failed, len: %d, value: %p", kv->valueLen, kv->value);
×
467
          break;
×
468
        }
469
        break;
3✔
470
      }
471
#endif
472
      case HEARTBEAT_KEY_TSMA: {
332✔
473
        if (kv->valueLen <= 0 || !kv->value) {
332!
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)) {
332!
477
          tscError("Process tsma info response failed, len: %d, value: %p", kv->valueLen, kv->value);
×
478
        }
479
        break;
332✔
480
      }
481
      default:
×
482
        tscError("invalid hb key type:%d", kv->key);
×
483
        break;
×
484
    }
485
  }
486
}
13,571✔
487

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

496
  if (pRsp->query) {
112,149✔
497
    STscObj *pTscObj = (STscObj *)acquireTscObj(pRsp->connKey.tscRid);
107,977✔
498
    if (NULL == pTscObj) {
107,977!
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)) {
107,977✔
502
        SEpSet *pOrig = &pTscObj->pAppInfo->mgmtEp.epSet;
41✔
503
        SEp    *pOrigEp = &pOrig->eps[pOrig->inUse];
41✔
504
        SEp    *pNewEp = &pRsp->query->epSet.eps[pRsp->query->epSet.inUse];
41✔
505
        tscDebug("mnode epset updated from %d/%d=>%s:%d to %d/%d=>%s:%d in hb", pOrig->inUse, pOrig->numOfEps,
41!
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);
41✔
510
      }
511

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

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

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

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

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

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

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

557
  taosHashRelease(pAppHbMgr->activeInfo, pReq);
112,149✔
558

559
  return TSDB_CODE_SUCCESS;
112,149✔
560
}
561

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

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

583
  int32_t rspNum = taosArrayGetSize(pRsp.rsps);
32,992✔
584

585
  (void)taosThreadMutexLock(&clientHbMgr.lock);
32,992✔
586

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

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

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

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

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

629
  (void)taosThreadMutexUnlock(&clientHbMgr.lock);
32,385✔
630

631
  tFreeClientHbBatchRsp(&pRsp);
632

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

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

644
  void *pIter = taosHashIterate(pObj->pRequests, NULL);
41,025✔
645
  while (pIter != NULL) {
84,235✔
646
    int64_t     *rid = pIter;
43,210✔
647
    SRequestObj *pRequest = acquireRequest(*rid);
43,210✔
648
    if (NULL == pRequest) {
43,210✔
649
      pIter = taosHashIterate(pObj->pRequests, pIter);
174✔
650
      continue;
174✔
651
    }
652

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

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

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

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

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

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

700
  return code;
41,025✔
701
}
702

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

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

717
  hbBasic->connId = pTscObj->connId;
114,216✔
718

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

727
  hbBasic->queryDesc = taosArrayInit(numOfQueries, sizeof(SQueryDesc));
41,025✔
728
  if (NULL == hbBasic->queryDesc) {
41,025!
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);
41,025✔
736
  if (code) {
41,025✔
737
    releaseTscObj(connKey->tscRid);
4,173✔
738
    if (hbBasic->queryDesc) {
4,173!
739
      taosArrayDestroyEx(hbBasic->queryDesc, tFreeClientHbQueryDesc);
4,173✔
740
    }
741
    taosMemoryFree(hbBasic);
4,173!
742
    return code;
4,173✔
743
  }
744

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

748
  return TSDB_CODE_SUCCESS;
36,852✔
749
}
750

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

758
  int32_t code = 0;
2,009✔
759

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

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

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

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

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

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

821
  return code;
2,009✔
822
}
823

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

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

834
  if (userNum <= 0) {
27,474✔
835
    taosMemoryFree(users);
541!
836
    return TSDB_CODE_SUCCESS;
541✔
837
  }
838

839
  for (int32_t i = 0; i < userNum; ++i) {
54,105✔
840
    SUserAuthVersion *user = &users[i];
27,172✔
841
    user->version = htonl(user->version);
27,172✔
842
  }
843

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

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

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

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

866
  return TSDB_CODE_SUCCESS;
26,933✔
867
}
868

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

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

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

884
  for (int32_t i = 0; i < dbNum; ++i) {
19,051✔
885
    SDbCacheInfo *db = &dbs[i];
10,304✔
886
    tscDebug("the %dth expired dbFName:%s, dbId:%" PRId64
10,304✔
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);
10,304✔
891
    db->vgVersion = htonl(db->vgVersion);
10,304✔
892
    db->cfgVersion = htonl(db->cfgVersion);
10,304✔
893
    db->numOfTable = htonl(db->numOfTable);
10,304✔
894
    db->stateTs = htobe64(db->stateTs);
10,304✔
895
    db->tsmaVersion = htonl(db->tsmaVersion);
10,304✔
896
  }
897

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

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

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

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

920
  return TSDB_CODE_SUCCESS;
8,747✔
921
}
922

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

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

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

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

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

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

954
  if (NULL == req->info) {
6,100!
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));
6,100✔
963
  if (TSDB_CODE_SUCCESS != code) {
6,100!
964
    taosMemoryFree(stbs);
×
965
    return code;
×
966
  }
967

968
  return TSDB_CODE_SUCCESS;
6,100✔
969
}
970

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1063
int32_t hbGetAppInfo(int64_t clusterId, SClientHbReq *req) {
110,043✔
1064
  SAppHbReq *pApp = taosHashGet(clientHbMgr.appSummary, &clusterId, sizeof(clusterId));
110,043✔
1065
  if (NULL != pApp) {
110,043!
1066
    (void)memcpy(&req->app, pApp, sizeof(*pApp));
110,043✔
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;
110,043✔
1075
}
1076

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

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

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

1095
    code = hbGetAppInfo(hbParam->clusterId, req);
30,648✔
1096
    if (TSDB_CODE_SUCCESS != code) {
30,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))) {
30,648✔
1102
      code = hbGetExpiredUserInfo(connKey, pCatalog, req);
27,474✔
1103
      if (TSDB_CODE_SUCCESS != code) {
27,474!
1104
        tscWarn("hbGetExpiredUserInfo failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1105
        return code;
×
1106
      }
1107
      if (clientHbMgr.appHbHash) {
27,474✔
1108
        code = taosHashPut(clientHbMgr.appHbHash, &hbParam->clusterId, sizeof(uint64_t), NULL, 0);
5,543✔
1109
        if (TSDB_CODE_SUCCESS != code) {
5,543!
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)) {
30,648✔
1119
      code = hbGetUserAuthInfo(connKey, hbParam, req);
2,009✔
1120
      if (TSDB_CODE_SUCCESS != code) {
2,009!
1121
        tscWarn("hbGetUserAuthInfo failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1122
        return code;
×
1123
      }
1124
      atomic_store_8(&hbParam->pAppHbMgr->connHbFlag, 1);
2,009✔
1125
    }
1126

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

1133
    code = hbGetExpiredStbInfo(connKey, pCatalog, req);
30,648✔
1134
    if (TSDB_CODE_SUCCESS != code) {
30,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);
30,648✔
1141
    if (TSDB_CODE_SUCCESS != code) {
30,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);
30,648✔
1147
    if (TSDB_CODE_SUCCESS != code) {
30,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);
79,395✔
1153
    if (TSDB_CODE_SUCCESS != code) {
79,395!
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
110,043✔
1160

1161
  return TSDB_CODE_SUCCESS;
110,043✔
1162
}
1163

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

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

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

1185
  int64_t  maxIpWhiteVer = 0;
34,337✔
1186
  void    *pIter = NULL;
34,337✔
1187
  SHbParam param = {0};
34,337✔
1188
  while ((pIter = taosHashIterate(pAppHbMgr->activeInfo, pIter))) {
150,110✔
1189
    SClientHbReq *pOneReq = pIter;
115,773✔
1190
    SClientHbKey *connKey = &pOneReq->connKey;
115,773✔
1191
    STscObj      *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
115,773✔
1192

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

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

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

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

1228
    int64_t ver = atomic_load_64(&pTscObj->whiteListInfo.ver);
114,216✔
1229
    maxIpWhiteVer = TMAX(maxIpWhiteVer, ver);
114,216✔
1230
    releaseTscObj(connKey->tscRid);
114,216✔
1231
  }
1232
  (*pBatchReq)->ipWhiteList = maxIpWhiteVer;
34,337✔
1233

1234
  return TSDB_CODE_SUCCESS;
34,337✔
1235
}
1236

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

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

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

1261
  taosHashClear(clientHbMgr.appSummary);
35,475✔
1262

1263
  for (int32_t i = 0; i < sz; ++i) {
77,383✔
1264
    SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
41,908✔
1265
    if (pAppHbMgr == NULL) continue;
41,908!
1266

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

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

1282
  return TSDB_CODE_SUCCESS;
35,475✔
1283
}
1284

1285
static void *hbThreadFunc(void *param) {
1,967✔
1286
  setThreadName("hb");
1,967✔
1287
#ifdef WINDOWS
1288
  if (taosCheckCurrentInDll()) {
1289
    atexit(hbThreadFuncUnexpectedStopped);
1290
  }
1291
#endif
1292
  while (1) {
35,033✔
1293
    if (1 == clientHbMgr.threadStop) {
37,000✔
1294
      break;
1,493✔
1295
    }
1296

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

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

1318
    for (int i = 0; i < sz; i++) {
77,415✔
1319
      SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
41,908✔
1320
      if (pAppHbMgr == NULL) {
41,908!
1321
        continue;
7,571✔
1322
      }
1323

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

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

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

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

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

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

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

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

1415
  return code;
1,967✔
1416
}
1417

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

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

1441
  tscDebug("hb thread stopped");
1,967✔
1442
}
1443

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

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

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

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

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

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

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

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

1499
  taosMemoryFree(pTarget->key);
2,142!
1500
  taosMemoryFree(pTarget);
2,142!
1501
}
2,142✔
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) {
1,967✔
1526
  int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
1,967✔
1527
  for (int i = 0; i < sz; i++) {
4,109✔
1528
    SAppHbMgr *pTarget = taosArrayGetP(clientHbMgr.appHbMgrs, i);
2,142✔
1529
    if (pTarget == NULL) continue;
2,142!
1530
    hbFreeAppHbMgr(pTarget);
2,142✔
1531
  }
1532
}
1,967✔
1533

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

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

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

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

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

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

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

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

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

1581
  return 0;
1,967✔
1582
}
1583

1584
void hbMgrCleanUp() {
1,976✔
1585
  hbStopThread();
1,976✔
1586

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

1591
  int32_t code = taosThreadMutexLock(&clientHbMgr.lock);
1,967✔
1592
  if (TSDB_CODE_SUCCESS != code) {
1,967!
1593
    tscError("failed to lock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1594
  }
1595
  appHbMgrCleanup();
1,967✔
1596
  taosArrayDestroy(clientHbMgr.appHbMgrs);
1,967✔
1597
  clientHbMgr.appHbMgrs = NULL;
1,967✔
1598
  code = taosThreadMutexUnlock(&clientHbMgr.lock);
1,967✔
1599
  if (TSDB_CODE_SUCCESS != code) {
1,967!
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) {
7,478✔
1605
  // init hash in activeinfo
1606
  void *data = taosHashGet(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
7,478✔
1607
  if (data != NULL) {
7,478!
1608
    return 0;
×
1609
  }
1610
  SClientHbReq hbReq = {0};
7,478✔
1611
  hbReq.connKey = connKey;
7,478✔
1612
  hbReq.clusterId = clusterId;
7,478✔
1613
  // hbReq.info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
1614

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

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

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

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

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