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

taosdata / TDengine / #4720

08 Sep 2025 08:43AM UTC coverage: 58.139% (-0.6%) from 58.762%
#4720

push

travis-ci

web-flow
Merge pull request #32881 from taosdata/enh/add-new-windows-ci

fix(ci): update workflow reference to use new Windows CI YAML

133181 of 292179 branches covered (45.58%)

Branch coverage included in aggregate %.

201691 of 283811 relevant lines covered (71.07%)

5442780.71 hits per line

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

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

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

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

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

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

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

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

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

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

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

108
      if (pRsp->dropped == 1) {
645!
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;
645✔
122

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

129
      if (pTscObj->passInfo.fp) {
645!
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, conn:%" PRIi64, pRsp->user, oldVer,
×
138
                   atomic_load_32(&passInfo->ver), pTscObj->id);
139
        }
140
      }
141

142
      if (pTscObj->whiteListInfo.fp) {
645!
143
        SWhiteListInfo *whiteListInfo = &pTscObj->whiteListInfo;
×
144
        int64_t         oldVer = atomic_load_64(&whiteListInfo->ver);
×
145
        if (oldVer != pRsp->whiteListVer) {
×
146
          atomic_store_64(&whiteListInfo->ver, pRsp->whiteListVer);
×
147
          if (whiteListInfo->fp) {
×
148
            (*whiteListInfo->fp)(whiteListInfo->param, &pRsp->whiteListVer, TAOS_NOTIFY_WHITELIST_VER);
×
149
          }
150
          tscDebug("update whitelist version of user %s from %" PRId64 " to %" PRId64 ", conn:%" PRIi64, pRsp->user,
×
151
                   oldVer, atomic_load_64(&whiteListInfo->ver), pTscObj->id);
152
        }
153
      } else {
154
        // Need to update version information to prevent frequent fetching of authentication
155
        // information.
156
        SWhiteListInfo *whiteListInfo = &pTscObj->whiteListInfo;
645✔
157
        int64_t         oldVer = atomic_load_64(&whiteListInfo->ver);
645✔
158
        atomic_store_64(&whiteListInfo->ver, pRsp->whiteListVer);
645✔
159
        tscDebug("update whitelist version of user %s from %" PRId64 " to %" PRId64 ", conn:%" PRIi64, pRsp->user,
645✔
160
                 oldVer, atomic_load_64(&whiteListInfo->ver), pTscObj->id);
161
      }
162
      releaseTscObj(pReq->connKey.tscRid);
645✔
163
    }
164
  }
165
  return 0;
156✔
166
}
167

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

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

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

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

203
  *pInfo = vgInfo;
43✔
204
  return code;
43✔
205
}
206

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

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

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

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

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

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

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

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

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

276
_return:
301✔
277

278
  tFreeSDbHbBatchRsp(&batchRsp);
301✔
279
  return code;
301✔
280
}
281

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

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

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

309
      TSC_ERR_JRET(catalogAsyncUpdateTableMeta(pCatalog, rsp));
2!
310
    }
311
  }
312

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

323
_return:
160✔
324
  taosArrayDestroy(hbRsp.pIndexRsp);
160✔
325
  hbRsp.pIndexRsp = NULL;
160✔
326

327
  tFreeSSTbHbRsp(&hbRsp);
160✔
328
  return code;
160✔
329
}
330

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

533
      if (pRsp->query->killConnection) {
13,190!
534
        taos_close_internal(pTscObj);
×
535
      }
536

537
      if (pRsp->query->pQnodeList) {
13,190✔
538
        if (TSDB_CODE_SUCCESS != updateQnodeList(pTscObj->pAppInfo, pRsp->query->pQnodeList)) {
4,059!
539
          tscWarn("update qnode list failed");
×
540
        }
541
      }
542

543
      releaseTscObj(pRsp->connKey.tscRid);
13,190✔
544
    }
545
  }
546

547
  int32_t kvNum = pRsp->info ? taosArrayGetSize(pRsp->info) : 0;
13,205!
548

549
  tscDebug("hb got %d rsp kv", kvNum);
13,205✔
550

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

561
  taosHashRelease(pAppHbMgr->activeInfo, pReq);
13,205✔
562

563
  return TSDB_CODE_SUCCESS;
13,205✔
564
}
565

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

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

587
  int32_t rspNum = taosArrayGetSize(pRsp.rsps);
1,219✔
588

589
  (void)taosThreadMutexLock(&clientHbMgr.lock);
1,219✔
590

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

601
  SAppInstInfo *pInst = pAppHbMgr->pAppInstInfo;
1,219✔
602

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

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

620
  if (rspNum) {
1,219✔
621
    tscDebug("hb got %d rsp, %d empty rsp received before", rspNum,
996✔
622
             atomic_val_compare_exchange_32(&emptyRspNum, emptyRspNum, 0));
623
  } else {
624
    (void)atomic_add_fetch_32(&emptyRspNum, 1);
223✔
625
  }
626

627
  for (int32_t i = 0; i < rspNum; ++i) {
14,425✔
628
    SClientHbRsp *rsp = taosArrayGet(pRsp.rsps, i);
13,206✔
629
    code = (*clientHbMgr.rspHandle[rsp->connKey.connType])(pAppHbMgr, rsp);
13,206✔
630
    if (code) {
13,206!
631
      break;
×
632
    }
633
  }
634

635
  (void)taosThreadMutexUnlock(&clientHbMgr.lock);
1,219✔
636

637
  tFreeClientHbBatchRsp(&pRsp);
638

639
_return:
1,219✔
640
  taosMemoryFree(pMsg->pData);
1,219!
641
  taosMemoryFree(pMsg->pEpSet);
1,219!
642
  return code;
1,219✔
643
}
644

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

650
  void *pIter = taosHashIterate(pObj->pRequests, NULL);
5,804✔
651
  while (pIter != NULL) {
11,644✔
652
    int64_t     *rid = pIter;
5,840✔
653
    SRequestObj *pRequest = acquireRequest(*rid);
5,840✔
654
    if (NULL == pRequest) {
5,840✔
655
      pIter = taosHashIterate(pObj->pRequests, pIter);
2✔
656
      continue;
2✔
657
    }
658

659
    if (pRequest->killed || 0 == pRequest->body.queryJob) {
5,838✔
660
      (void)releaseRequest(*rid);
3,012✔
661
      pIter = taosHashIterate(pObj->pRequests, pIter);
3,012✔
662
      continue;
3,012✔
663
    }
664

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

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

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

697
    (void)releaseRequest(*rid);
2,826✔
698
    if (NULL == taosArrayPush(hbBasic->queryDesc, &desc)) {
5,652!
699
      taosArrayDestroy(desc.subDesc);
×
700
      return terrno;
×
701
    }
702

703
    pIter = taosHashIterate(pObj->pRequests, pIter);
2,826✔
704
  }
705

706
  return code;
5,804✔
707
}
708

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

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

723
  hbBasic->connId = pTscObj->connId;
13,206✔
724

725
  int32_t numOfQueries = pTscObj->pRequests ? taosHashGetSize(pTscObj->pRequests) : 0;
13,206!
726
  if (numOfQueries <= 0) {
13,206✔
727
    req->query = hbBasic;
7,402✔
728
    releaseTscObj(connKey->tscRid);
7,402✔
729
    tscDebug("no queries on connection");
7,402✔
730
    return TSDB_CODE_SUCCESS;
7,402✔
731
  }
732

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

741
  int32_t code = hbBuildQueryDesc(hbBasic, pTscObj);
5,804✔
742
  if (code) {
5,804✔
743
    releaseTscObj(connKey->tscRid);
15✔
744
    if (hbBasic->queryDesc) {
15!
745
      taosArrayDestroyEx(hbBasic->queryDesc, tFreeClientHbQueryDesc);
15✔
746
    }
747
    taosMemoryFree(hbBasic);
15!
748
    return code;
15✔
749
  }
750

751
  req->query = hbBasic;
5,789✔
752
  releaseTscObj(connKey->tscRid);
5,789✔
753

754
  return TSDB_CODE_SUCCESS;
5,789✔
755
}
756

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

764
  int32_t code = 0;
157✔
765

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

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

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

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

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

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

827
  return code;
157✔
828
}
829

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

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

840
  if (userNum <= 0) {
995✔
841
    taosMemoryFree(users);
13!
842
    return TSDB_CODE_SUCCESS;
13✔
843
  }
844

845
  for (int32_t i = 0; i < userNum; ++i) {
2,024✔
846
    SUserAuthVersion *user = &users[i];
1,042✔
847
    user->version = htonl(user->version);
1,042✔
848
  }
849

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

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

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

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

872
  return TSDB_CODE_SUCCESS;
982✔
873
}
874

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

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

885
  if (dbNum <= 0) {
996✔
886
    taosMemoryFree(dbs);
694!
887
    return TSDB_CODE_SUCCESS;
694✔
888
  }
889

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

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

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

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

912
  if (NULL == req->info) {
302!
913
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
×
914
    if (NULL == req->info) {
×
915
      taosMemoryFree(dbs);
×
916
      return terrno;
×
917
    }
918
  }
919

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

926
  return TSDB_CODE_SUCCESS;
302✔
927
}
928

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

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

939
  if (stbNum <= 0) {
996✔
940
    taosMemoryFree(stbs);
836!
941
    return TSDB_CODE_SUCCESS;
836✔
942
  }
943

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

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

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

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

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

974
  return TSDB_CODE_SUCCESS;
160✔
975
}
976

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

983
  TSC_ERR_JRET(catalogGetExpiredViews(pCatalog, &views, &viewNum, &pDynViewVer));
996!
984

985
  if (viewNum <= 0) {
996!
986
    taosMemoryFree(views);
996!
987
    taosMemoryFree(pDynViewVer);
996!
988
    return TSDB_CODE_SUCCESS;
996✔
989
  }
990

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

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

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

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

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

1015
  kv.key = HEARTBEAT_KEY_VIEWINFO;
×
1016
  kv.valueLen = sizeof(SViewVersion) * viewNum;
×
1017
  kv.value = views;
×
1018

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

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

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

1038
  if (tsmaNum <= 0) {
996!
1039
    taosMemoryFree(tsmas);
996!
1040
    return TSDB_CODE_SUCCESS;
996✔
1041
  }
1042

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

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

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

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

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

1080
  return TSDB_CODE_SUCCESS;
13,191✔
1081
}
1082

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

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

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

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

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

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

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

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

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

1165
  ++hbParam->reqCnt;  // success to get catalog info
13,191✔
1166

1167
  return TSDB_CODE_SUCCESS;
13,191✔
1168
}
1169

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

1175
  clientHbMgr.rspHandle[CONN_TYPE__QUERY] = hbQueryHbRspHandle;
2,020✔
1176
  clientHbMgr.rspHandle[CONN_TYPE__TMQ] = hbMqHbRspHandle;
2,020✔
1177
}
2,020✔
1178

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

1191
  int64_t  maxIpWhiteVer = 0;
1,219✔
1192
  void    *pIter = NULL;
1,219✔
1193
  SHbParam param = {0};
1,219✔
1194
  while ((pIter = taosHashIterate(pAppHbMgr->activeInfo, pIter))) {
15,741✔
1195
    SClientHbReq *pOneReq = pIter;
14,522✔
1196
    SClientHbKey *connKey = &pOneReq->connKey;
14,522✔
1197
    STscObj      *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
14,522✔
1198

1199
    if (!pTscObj || atomic_load_8(&pTscObj->dropped) == 1) {
14,522!
1200
      if (pTscObj) releaseTscObj(connKey->tscRid);
1,316!
1201
      continue;
1,316✔
1202
    }
1203

1204
    tstrncpy(pOneReq->userApp, pTscObj->optionInfo.userApp, sizeof(pOneReq->userApp));
13,206✔
1205
    pOneReq->userIp = pTscObj->optionInfo.userIp;
13,206✔
1206
    pOneReq->userDualIp = pTscObj->optionInfo.userDualIp;
13,206✔
1207
    pOneReq = taosArrayPush((*pBatchReq)->reqs, pOneReq);
13,206✔
1208
    if (NULL == pOneReq) {
13,206!
1209
      releaseTscObj(connKey->tscRid);
×
1210
      continue;
×
1211
    }
1212

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

1234
    int64_t ver = atomic_load_64(&pTscObj->whiteListInfo.ver);
13,206✔
1235
    maxIpWhiteVer = TMAX(maxIpWhiteVer, ver);
13,206✔
1236
    releaseTscObj(connKey->tscRid);
13,206✔
1237
  }
1238
  (*pBatchReq)->ipWhiteListVer = maxIpWhiteVer;
1,219✔
1239

1240
  return TSDB_CODE_SUCCESS;
1,219✔
1241
}
1242

1243
void hbThreadFuncUnexpectedStopped(void) { atomic_store_8(&clientHbMgr.threadStop, 2); }
×
1244

1245
void hbMergeSummary(SAppClusterSummary *dst, SAppClusterSummary *src) {
410✔
1246
  dst->numOfInsertsReq += src->numOfInsertsReq;
410✔
1247
  dst->numOfInsertRows += src->numOfInsertRows;
410✔
1248
  dst->insertElapsedTime += src->insertElapsedTime;
410✔
1249
  dst->insertBytes += src->insertBytes;
410✔
1250
  dst->fetchBytes += src->fetchBytes;
410✔
1251
  dst->queryElapsedTime += src->queryElapsedTime;
410✔
1252
  dst->numOfSlowQueries += src->numOfSlowQueries;
410✔
1253
  dst->totalRequests += src->totalRequests;
410✔
1254
  dst->currentRequests += src->currentRequests;
410✔
1255
}
410✔
1256

1257
int32_t hbGatherAppInfo(void) {
1,898✔
1258
  SAppHbReq req = {0};
1,898✔
1259
  int32_t   code = TSDB_CODE_SUCCESS;
1,898✔
1260
  int       sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
1,898✔
1261
  if (sz > 0) {
1,898!
1262
    req.pid = taosGetPId();
1,898✔
1263
    req.appId = clientHbMgr.appId;
1,898✔
1264
    TSC_ERR_RET(taosGetAppName(req.name, NULL));
1,898!
1265
  }
1266

1267
  taosHashClear(clientHbMgr.appSummary);
1,898✔
1268

1269
  for (int32_t i = 0; i < sz; ++i) {
4,223✔
1270
    SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
2,325✔
1271
    if (pAppHbMgr == NULL) continue;
2,325!
1272

1273
    int64_t    clusterId = pAppHbMgr->pAppInstInfo->clusterId;
2,325✔
1274
    SAppHbReq *pApp = taosHashGet(clientHbMgr.appSummary, &clusterId, sizeof(clusterId));
2,325✔
1275
    if (NULL == pApp) {
2,325✔
1276
      (void)memcpy(&req.summary, &pAppHbMgr->pAppInstInfo->summary, sizeof(req.summary));
1,915✔
1277
      req.startTime = pAppHbMgr->startTime;
1,915✔
1278
      TSC_ERR_RET(taosHashPut(clientHbMgr.appSummary, &clusterId, sizeof(clusterId), &req, sizeof(req)));
1,915!
1279
    } else {
1280
      if (pAppHbMgr->startTime < pApp->startTime) {
410!
1281
        pApp->startTime = pAppHbMgr->startTime;
×
1282
      }
1283

1284
      hbMergeSummary(&pApp->summary, &pAppHbMgr->pAppInstInfo->summary);
410✔
1285
    }
1286
  }
1287

1288
  return TSDB_CODE_SUCCESS;
1,898✔
1289
}
1290

1291
static void *hbThreadFunc(void *param) {
2,020✔
1292
  setThreadName("hb");
2,020✔
1293
#ifdef WINDOWS
1294
  if (taosCheckCurrentInDll()) {
1295
    atexit(hbThreadFuncUnexpectedStopped);
1296
  }
1297
#endif
1298
  while (1) {
2,248✔
1299
    if (1 == clientHbMgr.threadStop) {
4,268✔
1300
      break;
367✔
1301
    }
1302

1303
    if (TSDB_CODE_SUCCESS != taosThreadMutexLock(&clientHbMgr.lock)) {
3,901!
1304
      tscError("taosThreadMutexLock failed");
×
1305
      return NULL;
×
1306
    }
1307

1308
    int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
3,901✔
1309
    if (sz > 0) {
3,901✔
1310
      if (TSDB_CODE_SUCCESS != hbGatherAppInfo()) {
1,898!
1311
        tscError("hbGatherAppInfo failed");
×
1312
        return NULL;
×
1313
      }
1314
      if (sz > 1 && !clientHbMgr.appHbHash) {
1,898✔
1315
        clientHbMgr.appHbHash = taosHashInit(0, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), true, HASH_NO_LOCK);
26✔
1316
        if (NULL == clientHbMgr.appHbHash) {
26!
1317
          tscError("taosHashInit failed");
×
1318
          return NULL;
×
1319
        }
1320
      }
1321
      taosHashClear(clientHbMgr.appHbHash);
1,898✔
1322
    }
1323

1324
    for (int i = 0; i < sz; i++) {
6,226✔
1325
      SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
2,325✔
1326
      if (pAppHbMgr == NULL) {
2,325!
1327
        continue;
1,106✔
1328
      }
1329

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

1353
      if (tSerializeSClientHbBatchReq(buf, tlen, pReq) == -1) {
1,219!
1354
        tFreeClientHbBatchReq(pReq);
×
1355
        taosMemoryFree(buf);
×
1356
        break;
×
1357
      }
1358
      SMsgSendInfo *pInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
1,219!
1359

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

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

1393
    if (TSDB_CODE_SUCCESS != taosThreadMutexUnlock(&clientHbMgr.lock)) {
3,901!
1394
      tscError("taosThreadMutexLock failed");
×
1395
      return NULL;
×
1396
    }
1397
    taosMsleep(HEARTBEAT_INTERVAL);
3,901✔
1398
  }
1399
  taosHashCleanup(clientHbMgr.appHbHash);
367✔
1400
  return NULL;
367✔
1401
}
1402

1403
static int32_t hbCreateThread() {
2,020✔
1404
  int32_t      code = TSDB_CODE_SUCCESS;
2,020✔
1405
  TdThreadAttr thAttr;
1406
  TSC_ERR_JRET(taosThreadAttrInit(&thAttr));
2,020!
1407
  TSC_ERR_JRET(taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE));
2,020!
1408
#ifdef TD_COMPACT_OS
1409
  TSC_ERR_JRET(taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL));
1410
#endif
1411

1412
  if (taosThreadCreate(&clientHbMgr.thread, &thAttr, hbThreadFunc, NULL) != 0) {
2,020!
1413
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
1414
    TSC_ERR_RET(terrno);
×
1415
  }
1416
  (void)taosThreadAttrDestroy(&thAttr);
2,020✔
1417
_return:
2,020✔
1418

1419
  if (code) {
2,020!
1420
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
1421
    TSC_ERR_RET(terrno);
×
1422
  }
1423

1424
  return code;
2,020✔
1425
}
1426

1427
static void hbStopThread() {
2,037✔
1428
  if (0 == atomic_load_8(&clientHbMgr.inited)) {
2,037✔
1429
    return;
17✔
1430
  }
1431
  if (atomic_val_compare_exchange_8(&clientHbMgr.threadStop, 0, 1)) {
2,020!
1432
    tscDebug("hb thread already stopped");
×
1433
    return;
×
1434
  }
1435

1436
  int32_t code = TSDB_CODE_SUCCESS;
2,020✔
1437
  // thread quit mode kill or inner exit from self-thread
1438
  if (clientHbMgr.quitByKill) {
2,020✔
1439
    code = taosThreadKill(clientHbMgr.thread, 0);
1,656✔
1440
    if (TSDB_CODE_SUCCESS != code) {
1,656!
1441
      tscError("taosThreadKill failed since %s", tstrerror(code));
×
1442
    }
1443
  } else {
1444
    code = taosThreadJoin(clientHbMgr.thread, NULL);
364✔
1445
    if (TSDB_CODE_SUCCESS != code) {
364!
1446
      tscError("taosThreadJoin failed since %s", tstrerror(code));
×
1447
    }
1448
  }
1449

1450
  tscDebug("hb thread stopped");
2,020✔
1451
}
1452

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

1471
  // init app info
1472
  (*pAppHbMgr)->pAppInstInfo = pAppInstInfo;
2,072✔
1473

1474
  // init hash info
1475
  (*pAppHbMgr)->activeInfo = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
2,072✔
1476

1477
  if ((*pAppHbMgr)->activeInfo == NULL) {
2,072!
1478
    TSC_ERR_JRET(terrno);
×
1479
  }
1480

1481
  // taosHashSetFreeFp(pAppHbMgr->activeInfo, tFreeClientHbReq);
1482

1483
  TSC_ERR_JRET(taosThreadMutexLock(&clientHbMgr.lock));
2,072!
1484
  if (taosArrayPush(clientHbMgr.appHbMgrs, &(*pAppHbMgr)) == NULL) {
4,144!
1485
    code = terrno;
×
1486
    (void)taosThreadMutexUnlock(&clientHbMgr.lock);
×
1487
    goto _return;
×
1488
  }
1489
  (*pAppHbMgr)->idx = taosArrayGetSize(clientHbMgr.appHbMgrs) - 1;
2,072✔
1490
  TSC_ERR_JRET(taosThreadMutexUnlock(&clientHbMgr.lock));
2,072!
1491

1492
  return TSDB_CODE_SUCCESS;
2,072✔
1493
_return:
×
1494
  taosMemoryFree(*pAppHbMgr);
×
1495
  return code;
×
1496
}
1497

1498
void hbFreeAppHbMgr(SAppHbMgr *pTarget) {
2,072✔
1499
  void *pIter = taosHashIterate(pTarget->activeInfo, NULL);
2,072✔
1500
  while (pIter != NULL) {
2,829✔
1501
    SClientHbReq *pOneReq = pIter;
757!
1502
    tFreeClientHbReq(pOneReq);
1503
    pIter = taosHashIterate(pTarget->activeInfo, pIter);
757✔
1504
  }
1505
  taosHashCleanup(pTarget->activeInfo);
2,072✔
1506
  pTarget->activeInfo = NULL;
2,072✔
1507

1508
  taosMemoryFree(pTarget->key);
2,072!
1509
  taosMemoryFree(pTarget);
2,072!
1510
}
2,072✔
1511

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

1534
void appHbMgrCleanup(void) {
2,020✔
1535
  int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
2,020✔
1536
  for (int i = 0; i < sz; i++) {
4,092✔
1537
    SAppHbMgr *pTarget = taosArrayGetP(clientHbMgr.appHbMgrs, i);
2,072✔
1538
    if (pTarget == NULL) continue;
2,072!
1539
    hbFreeAppHbMgr(pTarget);
2,072✔
1540
  }
1541
}
2,020✔
1542

1543
int32_t hbMgrInit() {
2,072✔
1544
  // init once
1545
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 0, 1);
2,072✔
1546
  if (old == 1) return 0;
2,072✔
1547

1548
  clientHbMgr.appId = tGenIdPI64();
2,020✔
1549
  tscInfo("app initialized, appId:0x%" PRIx64, clientHbMgr.appId);
2,020!
1550

1551
  clientHbMgr.appSummary = taosHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
2,020✔
1552
  if (NULL == clientHbMgr.appSummary) {
2,020!
1553
    uError("hbMgrInit:taosHashInit error") return terrno;
×
1554
  }
1555
  clientHbMgr.appHbMgrs = taosArrayInit(0, sizeof(void *));
2,020✔
1556
  if (NULL == clientHbMgr.appHbMgrs) {
2,020!
1557
    uError("hbMgrInit:taosArrayInit error") return terrno;
×
1558
  }
1559
  TdThreadMutexAttr attr = {0};
2,020✔
1560

1561
  int ret = taosThreadMutexAttrInit(&attr);
2,020✔
1562
  if (ret != 0) {
2,020!
1563
    uError("hbMgrInit:taosThreadMutexAttrInit error") return ret;
×
1564
  }
1565

1566
  ret = taosThreadMutexAttrSetType(&attr, PTHREAD_MUTEX_RECURSIVE);
2,020✔
1567
  if (ret != 0) {
2,020!
1568
    uError("hbMgrInit:taosThreadMutexAttrSetType error") return ret;
×
1569
  }
1570

1571
  ret = taosThreadMutexInit(&clientHbMgr.lock, &attr);
2,020✔
1572
  if (ret != 0) {
2,020!
1573
    uError("hbMgrInit:taosThreadMutexInit error") return ret;
×
1574
  }
1575

1576
  ret = taosThreadMutexAttrDestroy(&attr);
2,020✔
1577
  if (ret != 0) {
2,020!
1578
    uError("hbMgrInit:taosThreadMutexAttrDestroy error") return ret;
×
1579
  }
1580

1581
  // init handle funcs
1582
  hbMgrInitHandle();
1583

1584
  // init backgroud thread
1585
  ret = hbCreateThread();
2,020✔
1586
  if (ret != 0) {
2,020!
1587
    uError("hbMgrInit:hbCreateThread error") return ret;
×
1588
  }
1589

1590
  return 0;
2,020✔
1591
}
1592

1593
void hbMgrCleanUp() {
2,037✔
1594
  hbStopThread();
2,037✔
1595

1596
  // destroy all appHbMgr
1597
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 1, 0);
2,037✔
1598
  if (old == 0) return;
2,037✔
1599

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

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

1624
  TSC_ERR_RET(taosHashPut(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey), &hbReq, sizeof(SClientHbReq)));
6,891!
1625

1626
  (void)atomic_add_fetch_32(&pAppHbMgr->connKeyCnt, 1);
6,891✔
1627
  return 0;
6,891✔
1628
}
1629

1630
int32_t hbRegisterConn(SAppHbMgr *pAppHbMgr, int64_t tscRefId, int64_t clusterId, int8_t connType) {
6,954✔
1631
  SClientHbKey connKey = {
6,954✔
1632
      .tscRid = tscRefId,
1633
      .connType = connType,
1634
  };
1635

1636
  switch (connType) {
6,954!
1637
    case CONN_TYPE__QUERY: {
6,891✔
1638
      return hbRegisterConnImpl(pAppHbMgr, connKey, clusterId);
6,891✔
1639
    }
1640
    case CONN_TYPE__TMQ: {
63✔
1641
      return 0;
63✔
1642
    }
1643
    default:
×
1644
      return 0;
×
1645
  }
1646
}
1647

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

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

© 2025 Coveralls, Inc