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

taosdata / TDengine / #3647

13 Mar 2025 05:26AM UTC coverage: 25.9% (-2.5%) from 28.375%
#3647

push

travis-ci

web-flow
Merge pull request #30158 from taosdata/docs/anchor-caps-30

docs: lowercase anchors for 3.0

53974 of 285572 branches covered (18.9%)

Branch coverage included in aggregate %.

92870 of 281392 relevant lines covered (33.0%)

617448.64 hits per line

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

275
_return:
18✔
276

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

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

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

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

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

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

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

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

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

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

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

353
  int32_t numOfMeta = taosArrayGetSize(hbRsp.pViewRsp);
×
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

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

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

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

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

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

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

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

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

496
  if (pRsp->query) {
81✔
497
    STscObj *pTscObj = (STscObj *)acquireTscObj(pRsp->connKey.tscRid);
74✔
498
    if (NULL == pTscObj) {
74!
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)) {
74!
502
        SEpSet *pOrig = &pTscObj->pAppInfo->mgmtEp.epSet;
×
503
        SEp    *pOrigEp = &pOrig->eps[pOrig->inUse];
×
504
        SEp    *pNewEp = &pRsp->query->epSet.eps[pRsp->query->epSet.inUse];
×
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

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

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

518
      if (pRsp->query->killRid) {
74!
519
        tscDebug("QID:%" PRIx64 ", need to be killed now", pRsp->query->killRid);
×
520
        SRequestObj *pRequest = acquireRequest(pRsp->query->killRid);
×
521
        if (NULL == pRequest) {
×
522
          tscDebug("QID: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) {
74!
530
        taos_close_internal(pTscObj);
×
531
      }
532

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

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

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

545
  tscDebug("hb got %d rsp kv", kvNum);
81!
546

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

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

559
  return TSDB_CODE_SUCCESS;
81✔
560
}
561

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

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

583
  int32_t rspNum = taosArrayGetSize(pRsp.rsps);
77✔
584

585
  (void)taosThreadMutexLock(&clientHbMgr.lock);
77✔
586

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

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

609
  pInst->serverCfg.monitorParas = pRsp.monitorParas;
77✔
610
  pInst->serverCfg.enableAuditDelete = pRsp.enableAuditDelete;
77✔
611
  pInst->serverCfg.enableStrongPass = pRsp.enableStrongPass;
77✔
612
  tsEnableStrongPassword = pInst->serverCfg.enableStrongPass;
77✔
613
  tscDebug("monitor paras from hb, clusterId:0x%" PRIx64 ", threshold:%d scope:%d", pInst->clusterId,
77!
614
           pRsp.monitorParas.tsSlowLogThreshold, pRsp.monitorParas.tsSlowLogScope);
615

616
  if (rspNum) {
77✔
617
    tscDebug("hb got %d rsp, %d empty rsp received before", rspNum,
74!
618
             atomic_val_compare_exchange_32(&emptyRspNum, emptyRspNum, 0));
619
  } else {
620
    (void)atomic_add_fetch_32(&emptyRspNum, 1);
3✔
621
  }
622

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

631
  (void)taosThreadMutexUnlock(&clientHbMgr.lock);
77✔
632

633
  tFreeClientHbBatchRsp(&pRsp);
634

635
_return:
77✔
636
  taosMemoryFree(pMsg->pData);
77!
637
  taosMemoryFree(pMsg->pEpSet);
77!
638
  return code;
77✔
639
}
640

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

646
  void *pIter = taosHashIterate(pObj->pRequests, NULL);
75✔
647
  while (pIter != NULL) {
162✔
648
    int64_t     *rid = pIter;
87✔
649
    SRequestObj *pRequest = acquireRequest(*rid);
87✔
650
    if (NULL == pRequest) {
87!
651
      pIter = taosHashIterate(pObj->pRequests, pIter);
×
652
      continue;
×
653
    }
654

655
    if (pRequest->killed || 0 == pRequest->body.queryJob) {
87!
656
      (void)releaseRequest(*rid);
78✔
657
      pIter = taosHashIterate(pObj->pRequests, pIter);
78✔
658
      continue;
78✔
659
    }
660

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

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

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

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

699
    pIter = taosHashIterate(pObj->pRequests, pIter);
9✔
700
  }
701

702
  return code;
75✔
703
}
704

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

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

719
  hbBasic->connId = pTscObj->connId;
81✔
720

721
  int32_t numOfQueries = pTscObj->pRequests ? taosHashGetSize(pTscObj->pRequests) : 0;
81!
722
  if (numOfQueries <= 0) {
81✔
723
    req->query = hbBasic;
6✔
724
    releaseTscObj(connKey->tscRid);
6✔
725
    tscDebug("no queries on connection");
6!
726
    return TSDB_CODE_SUCCESS;
6✔
727
  }
728

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

737
  int32_t code = hbBuildQueryDesc(hbBasic, pTscObj);
75✔
738
  if (code) {
75✔
739
    releaseTscObj(connKey->tscRid);
7✔
740
    if (hbBasic->queryDesc) {
7!
741
      taosArrayDestroyEx(hbBasic->queryDesc, tFreeClientHbQueryDesc);
7✔
742
    }
743
    taosMemoryFree(hbBasic);
7!
744
    return code;
7✔
745
  }
746

747
  req->query = hbBasic;
68✔
748
  releaseTscObj(connKey->tscRid);
68✔
749

750
  return TSDB_CODE_SUCCESS;
68✔
751
}
752

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

760
  int32_t code = 0;
27✔
761

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

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

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

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

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

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

823
  return code;
27✔
824
}
825

826
int32_t hbGetExpiredUserInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
73✔
827
  SUserAuthVersion *users = NULL;
73✔
828
  uint32_t          userNum = 0;
73✔
829
  int32_t           code = 0;
73✔
830

831
  code = catalogGetExpiredUsers(pCatalog, &users, &userNum);
73✔
832
  if (TSDB_CODE_SUCCESS != code) {
73!
833
    return code;
×
834
  }
835

836
  if (userNum <= 0) {
73✔
837
    taosMemoryFree(users);
2!
838
    return TSDB_CODE_SUCCESS;
2✔
839
  }
840

841
  for (int32_t i = 0; i < userNum; ++i) {
142✔
842
    SUserAuthVersion *user = &users[i];
71✔
843
    user->version = htonl(user->version);
71✔
844
  }
845

846
  SKv kv = {
71✔
847
      .key = HEARTBEAT_KEY_USER_AUTHINFO,
848
      .valueLen = sizeof(SUserAuthVersion) * userNum,
71✔
849
      .value = users,
850
  };
851

852
  tscDebug("hb got %d expired users, valueLen:%d", userNum, kv.valueLen);
71!
853

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

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

868
  return TSDB_CODE_SUCCESS;
71✔
869
}
870

871
int32_t hbGetExpiredDBInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
73✔
872
  SDbCacheInfo *dbs = NULL;
73✔
873
  uint32_t      dbNum = 0;
73✔
874
  int32_t       code = 0;
73✔
875

876
  code = catalogGetExpiredDBs(pCatalog, &dbs, &dbNum);
73✔
877
  if (TSDB_CODE_SUCCESS != code) {
73!
878
    return code;
×
879
  }
880

881
  if (dbNum <= 0) {
73✔
882
    taosMemoryFree(dbs);
55!
883
    return TSDB_CODE_SUCCESS;
55✔
884
  }
885

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

892
    db->dbId = htobe64(db->dbId);
21✔
893
    db->vgVersion = htonl(db->vgVersion);
21✔
894
    db->cfgVersion = htonl(db->cfgVersion);
21✔
895
    db->numOfTable = htonl(db->numOfTable);
21✔
896
    db->stateTs = htobe64(db->stateTs);
21✔
897
    db->tsmaVersion = htonl(db->tsmaVersion);
21✔
898
  }
899

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

906
  tscDebug("hb got %d expired db, valueLen:%d", dbNum, kv.valueLen);
18!
907

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

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

922
  return TSDB_CODE_SUCCESS;
18✔
923
}
924

925
int32_t hbGetExpiredStbInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
73✔
926
  SSTableVersion *stbs = NULL;
73✔
927
  uint32_t        stbNum = 0;
73✔
928
  int32_t         code = 0;
73✔
929

930
  code = catalogGetExpiredSTables(pCatalog, &stbs, &stbNum);
73✔
931
  if (TSDB_CODE_SUCCESS != code) {
73!
932
    return code;
×
933
  }
934

935
  if (stbNum <= 0) {
73✔
936
    taosMemoryFree(stbs);
67!
937
    return TSDB_CODE_SUCCESS;
67✔
938
  }
939

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

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

954
  tscDebug("hb got %d expired stb, valueLen:%d", stbNum, kv.valueLen);
6!
955

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

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

970
  return TSDB_CODE_SUCCESS;
6✔
971
}
972

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

979
  TSC_ERR_JRET(catalogGetExpiredViews(pCatalog, &views, &viewNum, &pDynViewVer));
73!
980

981
  if (viewNum <= 0) {
73!
982
    taosMemoryFree(views);
73!
983
    taosMemoryFree(pDynViewVer);
73!
984
    return TSDB_CODE_SUCCESS;
73✔
985
  }
986

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

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

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

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

1009
  TSC_ERR_JRET(taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)));
×
1010

1011
  kv.key = HEARTBEAT_KEY_VIEWINFO;
×
1012
  kv.valueLen = sizeof(SViewVersion) * viewNum;
×
1013
  kv.value = views;
×
1014

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

1023
int32_t hbGetExpiredTSMAInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *pReq) {
73✔
1024
  int32_t       code = 0;
73✔
1025
  uint32_t      tsmaNum = 0;
73✔
1026
  STSMAVersion *tsmas = NULL;
73✔
1027

1028
  code = catalogGetExpiredTsmas(pCatalog, &tsmas, &tsmaNum);
73✔
1029
  if (code) {
73!
1030
    taosMemoryFree(tsmas);
×
1031
    return code;
×
1032
  }
1033

1034
  if (tsmaNum <= 0) {
73!
1035
    taosMemoryFree(tsmas);
73!
1036
    return TSDB_CODE_SUCCESS;
73✔
1037
  }
1038

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

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

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

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

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

1076
  return TSDB_CODE_SUCCESS;
74✔
1077
}
1078

1079
int32_t hbQueryHbReqHandle(SClientHbKey *connKey, void *param, SClientHbReq *req) {
81✔
1080
  int32_t   code = 0;
81✔
1081
  SHbParam *hbParam = (SHbParam *)param;
81✔
1082
  SCatalog *pCatalog = NULL;
81✔
1083

1084
  code = hbGetQueryBasicInfo(connKey, req);
81✔
1085
  if (code != TSDB_CODE_SUCCESS) {
81✔
1086
    tscWarn("hbGetQueryBasicInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
7!
1087
    return code;
7✔
1088
  }
1089

1090
  if (hbParam->reqCnt == 0) {
74✔
1091
    code = catalogGetHandle(hbParam->clusterId, &pCatalog);
73✔
1092
    if (code != TSDB_CODE_SUCCESS) {
73!
1093
      tscWarn("catalogGetHandle failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1094
      return code;
×
1095
    }
1096

1097
    code = hbGetAppInfo(hbParam->clusterId, req);
73✔
1098
    if (TSDB_CODE_SUCCESS != code) {
73!
1099
      tscWarn("getAppInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1100
      return code;
×
1101
    }
1102

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

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

1129
    code = hbGetExpiredDBInfo(connKey, pCatalog, req);
73✔
1130
    if (TSDB_CODE_SUCCESS != code) {
73!
1131
      tscWarn("hbGetExpiredDBInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1132
      return code;
×
1133
    }
1134

1135
    code = hbGetExpiredStbInfo(connKey, pCatalog, req);
73✔
1136
    if (TSDB_CODE_SUCCESS != code) {
73!
1137
      tscWarn("hbGetExpiredStbInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1138
      return code;
×
1139
    }
1140

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

1161
  ++hbParam->reqCnt;  // success to get catalog info
74✔
1162

1163
  return TSDB_CODE_SUCCESS;
74✔
1164
}
1165

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

1171
  clientHbMgr.rspHandle[CONN_TYPE__QUERY] = hbQueryHbRspHandle;
5✔
1172
  clientHbMgr.rspHandle[CONN_TYPE__TMQ] = hbMqHbRspHandle;
5✔
1173
}
5✔
1174

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

1187
  int64_t  maxIpWhiteVer = 0;
77✔
1188
  void    *pIter = NULL;
77✔
1189
  SHbParam param = {0};
77✔
1190
  while ((pIter = taosHashIterate(pAppHbMgr->activeInfo, pIter))) {
258✔
1191
    SClientHbReq *pOneReq = pIter;
181✔
1192
    SClientHbKey *connKey = &pOneReq->connKey;
181✔
1193
    STscObj      *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
181✔
1194

1195
    if (!pTscObj || atomic_load_8(&pTscObj->dropped) == 1) {
181!
1196
      if (pTscObj) releaseTscObj(connKey->tscRid);
100!
1197
      continue;
100✔
1198
    }
1199

1200
    tstrncpy(pOneReq->userApp, pTscObj->optionInfo.userApp, sizeof(pOneReq->userApp));
81✔
1201
    pOneReq->userIp = pTscObj->optionInfo.userIp;
81✔
1202

1203
    pOneReq = taosArrayPush((*pBatchReq)->reqs, pOneReq);
81✔
1204
    if (NULL == pOneReq) {
81!
1205
      releaseTscObj(connKey->tscRid);
×
1206
      continue;
×
1207
    }
1208

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

1230
    int64_t ver = atomic_load_64(&pTscObj->whiteListInfo.ver);
81✔
1231
    maxIpWhiteVer = TMAX(maxIpWhiteVer, ver);
81✔
1232
    releaseTscObj(connKey->tscRid);
81✔
1233
  }
1234
  (*pBatchReq)->ipWhiteList = maxIpWhiteVer;
77✔
1235

1236
  return TSDB_CODE_SUCCESS;
77✔
1237
}
1238

1239
void hbThreadFuncUnexpectedStopped(void) { atomic_store_8(&clientHbMgr.threadStop, 2); }
×
1240

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

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

1263
  taosHashClear(clientHbMgr.appSummary);
93✔
1264

1265
  for (int32_t i = 0; i < sz; ++i) {
197✔
1266
    SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
104✔
1267
    if (pAppHbMgr == NULL) continue;
104!
1268

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

1280
      hbMergeSummary(&pApp->summary, &pAppHbMgr->pAppInstInfo->summary);
×
1281
    }
1282
  }
1283

1284
  return TSDB_CODE_SUCCESS;
93✔
1285
}
1286

1287
static void *hbThreadFunc(void *param) {
5✔
1288
  setThreadName("hb");
5✔
1289
#ifdef WINDOWS
1290
  if (taosCheckCurrentInDll()) {
1291
    atexit(hbThreadFuncUnexpectedStopped);
1292
  }
1293
#endif
1294
  while (1) {
93✔
1295
    if (1 == clientHbMgr.threadStop) {
98✔
1296
      break;
5✔
1297
    }
1298

1299
    if (TSDB_CODE_SUCCESS != taosThreadMutexLock(&clientHbMgr.lock)) {
93!
1300
      tscError("taosThreadMutexLock failed");
×
1301
      return NULL;
×
1302
    }
1303

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

1320
    for (int i = 0; i < sz; i++) {
197✔
1321
      SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
104✔
1322
      if (pAppHbMgr == NULL) {
104!
1323
        continue;
27✔
1324
      }
1325

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

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

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

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

1389
    if (TSDB_CODE_SUCCESS != taosThreadMutexUnlock(&clientHbMgr.lock)) {
93!
1390
      tscError("taosThreadMutexLock failed");
×
1391
      return NULL;
×
1392
    }
1393
    taosMsleep(HEARTBEAT_INTERVAL);
93✔
1394
  }
1395
  taosHashCleanup(clientHbMgr.appHbHash);
5✔
1396
  return NULL;
5✔
1397
}
1398

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

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

1412
  if (code) {
5!
1413
    terrno = TAOS_SYSTEM_ERROR(errno);
×
1414
    TSC_ERR_RET(terrno);
×
1415
  }
1416

1417
  return code;
5✔
1418
}
1419

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

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

1443
  tscDebug("hb thread stopped");
5!
1444
}
1445

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

1464
  // init app info
1465
  (*pAppHbMgr)->pAppInstInfo = pAppInstInfo;
7✔
1466

1467
  // init hash info
1468
  (*pAppHbMgr)->activeInfo = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
7✔
1469

1470
  if ((*pAppHbMgr)->activeInfo == NULL) {
7!
1471
    TSC_ERR_JRET(terrno);
×
1472
  }
1473

1474
  // taosHashSetFreeFp(pAppHbMgr->activeInfo, tFreeClientHbReq);
1475

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

1485
  return TSDB_CODE_SUCCESS;
7✔
1486
_return:
×
1487
  taosMemoryFree(*pAppHbMgr);
×
1488
  return code;
×
1489
}
1490

1491
void hbFreeAppHbMgr(SAppHbMgr *pTarget) {
7✔
1492
  void *pIter = taosHashIterate(pTarget->activeInfo, NULL);
7✔
1493
  while (pIter != NULL) {
17✔
1494
    SClientHbReq *pOneReq = pIter;
10!
1495
    tFreeClientHbReq(pOneReq);
1496
    pIter = taosHashIterate(pTarget->activeInfo, pIter);
10✔
1497
  }
1498
  taosHashCleanup(pTarget->activeInfo);
7✔
1499
  pTarget->activeInfo = NULL;
7✔
1500

1501
  taosMemoryFree(pTarget->key);
7!
1502
  taosMemoryFree(pTarget);
7!
1503
}
7✔
1504

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

1527
void appHbMgrCleanup(void) {
5✔
1528
  int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
5✔
1529
  for (int i = 0; i < sz; i++) {
12✔
1530
    SAppHbMgr *pTarget = taosArrayGetP(clientHbMgr.appHbMgrs, i);
7✔
1531
    if (pTarget == NULL) continue;
7!
1532
    hbFreeAppHbMgr(pTarget);
7✔
1533
  }
1534
}
5✔
1535

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

1541
  clientHbMgr.appId = tGenIdPI64();
5✔
1542
  tscInfo("app initialized, appId:0x%" PRIx64, clientHbMgr.appId);
5!
1543

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

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

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

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

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

1574
  // init handle funcs
1575
  hbMgrInitHandle();
1576

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

1583
  return 0;
5✔
1584
}
1585

1586
void hbMgrCleanUp() {
5✔
1587
  hbStopThread();
5✔
1588

1589
  // destroy all appHbMgr
1590
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 1, 0);
5✔
1591
  if (old == 0) return;
5!
1592

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

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

1617
  TSC_ERR_RET(taosHashPut(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey), &hbReq, sizeof(SClientHbReq)));
86!
1618

1619
  (void)atomic_add_fetch_32(&pAppHbMgr->connKeyCnt, 1);
86✔
1620
  return 0;
86✔
1621
}
1622

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

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

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

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