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

taosdata / TDengine / #4829

30 Oct 2025 09:25AM UTC coverage: 49.734% (-11.3%) from 61.071%
#4829

push

travis-ci

web-flow
Merge pull request #33435 from taosdata/3.0

merge 3.0

123072 of 323930 branches covered (37.99%)

Branch coverage included in aggregate %.

7 of 25 new or added lines in 3 files covered. (28.0%)

35232 existing lines in 327 files now uncovered.

172062 of 269495 relevant lines covered (63.85%)

70709785.06 hits per line

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

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

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

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

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

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

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

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

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

85
    SClientHbReq    *pReq = NULL;
449,108✔
86
    SGetUserAuthRsp *pRsp = NULL;
449,108✔
87
    while ((pReq = taosHashIterate(hbMgr->activeInfo, pReq))) {
922,023✔
88
      STscObj *pTscObj = (STscObj *)acquireTscObj(pReq->connKey.tscRid);
520,174✔
89
      if (!pTscObj) {
520,174✔
90
        continue;
45,597✔
91
      }
92

93
      if (!pRsp) {
474,577✔
94
        for (int32_t j = 0; j < TARRAY_SIZE(batchRsp->pArray); ++j) {
461,962✔
95
          SGetUserAuthRsp *rsp = TARRAY_GET_ELEM(batchRsp->pArray, j);
414,703✔
96
          if (0 == strncmp(rsp->user, pTscObj->user, TSDB_USER_LEN)) {
414,703!
97
            pRsp = rsp;
359,064✔
98
            break;
359,064✔
99
          }
100
        }
101
        if (!pRsp) {
406,323✔
102
          releaseTscObj(pReq->connKey.tscRid);
47,259✔
103
          taosHashCancelIterate(hbMgr->activeInfo, pReq);
47,259✔
104
          break;
47,259✔
105
        }
106
      }
107

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

121
      pTscObj->authVer = pRsp->version;
425,367✔
122

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

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

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

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

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

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

203
  *pInfo = vgInfo;
135,440✔
204
  return code;
135,440✔
205
}
206

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

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

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

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

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

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

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

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

276
_return:
1,452,150✔
277

278
  tFreeSDbHbBatchRsp(&batchRsp);
1,452,150✔
279
  return code;
1,452,150✔
280
}
281

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

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

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

313
  int32_t numOfIndex = taosArrayGetSize(hbRsp.pIndexRsp);
1,041,382✔
314
  for (int32_t i = 0; i < numOfIndex; ++i) {
1,041,382!
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:
1,041,382✔
324
  taosArrayDestroy(hbRsp.pIndexRsp);
1,041,382✔
325
  hbRsp.pIndexRsp = NULL;
1,041,382✔
326

327
  tFreeSSTbHbRsp(&hbRsp);
1,041,382✔
328
  return code;
1,041,382✔
329
}
330

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

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

354
  int32_t numOfMeta = taosArrayGetSize(hbRsp.pViewRsp);
286✔
355
  for (int32_t i = 0; i < numOfMeta; ++i) {
286!
UNCOV
356
    SViewMetaRsp *rsp = taosArrayGetP(hbRsp.pViewRsp, i);
×
UNCOV
357
    if (NULL == rsp) {
×
358
      code = terrno;
×
359
      goto _return;
×
360
    }
UNCOV
361
    if (rsp->numOfCols < 0) {
×
UNCOV
362
      tscDebug("hb to remove view, db:%s, view:%s", rsp->dbFName, rsp->name);
×
UNCOV
363
      code = catalogRemoveViewMeta(pCatalog, rsp->dbFName, rsp->dbId, rsp->name, rsp->viewId);
×
UNCOV
364
      tFreeSViewMetaRsp(rsp);
×
UNCOV
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
    }
UNCOV
370
    TSC_ERR_JRET(code);
×
371
  }
372

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

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

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

UNCOV
387
  int32_t numOfTsma = taosArrayGetSize(hbRsp.pTsmas);
×
UNCOV
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

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

408
static void hbProcessQueryRspKvs(int32_t kvNum, SArray *pKvs, struct SCatalog *pCatalog, SAppHbMgr *pAppHbMgr) {
2,394,621✔
409
  for (int32_t i = 0; i < kvNum; ++i) {
5,236,871✔
410
    SKv *kv = taosArrayGet(pKvs, i);
2,842,250✔
411
    if (NULL == kv) {
2,842,250!
412
      tscError("invalid hb kv, idx:%d", i);
×
413
      continue;
×
414
    }
415
    switch (kv->key) {
2,842,250!
416
      case HEARTBEAT_KEY_USER_AUTHINFO: {
348,146✔
417
        if (kv->valueLen <= 0 || NULL == kv->value) {
348,146!
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)) {
348,146!
422
          tscError("process user auth info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
423
          break;
×
424
        }
425
        break;
348,146✔
426
      }
427
      case HEARTBEAT_KEY_DBINFO: {
1,452,150✔
428
        if (kv->valueLen <= 0 || NULL == kv->value) {
1,452,150!
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)) {
1,452,150!
433
          tscError("process db info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
434
          break;
×
435
        }
436
        break;
1,452,150✔
437
      }
438
      case HEARTBEAT_KEY_STBINFO: {
1,041,382✔
439
        if (kv->valueLen <= 0 || NULL == kv->value) {
1,041,382!
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)) {
1,041,382!
444
          tscError("process stb info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
445
          break;
×
446
        }
447
        break;
1,041,382✔
448
      }
449
#ifdef TD_ENTERPRISE
450
      case HEARTBEAT_KEY_DYN_VIEW: {
286✔
451
        if (kv->valueLen <= 0 || NULL == kv->value) {
286!
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)) {
286!
456
          tscError("Process dyn view response failed, len:%d, value:%p", kv->valueLen, kv->value);
×
457
          break;
×
458
        }
459
        break;
286✔
460
      }
461
      case HEARTBEAT_KEY_VIEWINFO: {
286✔
462
        if (kv->valueLen <= 0 || NULL == kv->value) {
286!
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)) {
286!
467
          tscError("Process view info response failed, len:%d, value:%p", kv->valueLen, kv->value);
×
468
          break;
×
469
        }
470
        break;
286✔
471
      }
472
#endif
UNCOV
473
      case HEARTBEAT_KEY_TSMA: {
×
UNCOV
474
        if (kv->valueLen <= 0 || !kv->value) {
×
475
          tscError("Invalid tsma info, len:%d, value:%p", kv->valueLen, kv->value);
×
476
        }
UNCOV
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
        }
UNCOV
480
        break;
×
481
      }
482
      default:
×
483
        tscError("invalid hb key type:%d", kv->key);
×
484
        break;
×
485
    }
486
  }
487
}
2,394,621✔
488

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

497
  if (pRsp->query) {
8,005,043✔
498
    STscObj *pTscObj = (STscObj *)acquireTscObj(pRsp->connKey.tscRid);
6,597,622✔
499
    if (NULL == pTscObj) {
6,597,622✔
500
      tscDebug("tscObj rid %" PRIx64 " not exist", pRsp->connKey.tscRid);
805✔
501
    } else {
502
      if (pRsp->query->totalDnodes > 1) {
6,596,817✔
503
        SEpSet  originEpset = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
613,617✔
504
        if (!isEpsetEqual(&originEpset, &pRsp->query->epSet)) {
613,617✔
505
          SEpSet *pOrig = &originEpset;
16,685✔
506
          SEp    *pOrigEp = &pOrig->eps[pOrig->inUse];
16,685✔
507
          SEp    *pNewEp = &pRsp->query->epSet.eps[pRsp->query->epSet.inUse];
16,685✔
508
          tscDebug("mnode epset updated from %d/%d=>%s:%d to %d/%d=>%s:%d in hb", pOrig->inUse, pOrig->numOfEps,
16,685!
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);
16,685✔
513
        }
514
      }
515

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

522
      if (pRsp->query->killRid) {
6,596,817!
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) {
6,596,817!
534
        taos_close_internal(pTscObj);
×
535
      }
536

537
      if (pRsp->query->pQnodeList) {
6,596,817✔
538
        if (TSDB_CODE_SUCCESS != updateQnodeList(pTscObj->pAppInfo, pRsp->query->pQnodeList)) {
961,387!
539
          tscWarn("update qnode list failed");
×
540
        }
541
      }
542

543
      releaseTscObj(pRsp->connKey.tscRid);
6,596,817✔
544
    }
545
  }
546

547
  int32_t kvNum = pRsp->info ? taosArrayGetSize(pRsp->info) : 0;
8,005,043!
548

549
  tscDebug("hb got %d rsp kv", kvNum);
8,005,043✔
550

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

561
  taosHashRelease(pAppHbMgr->activeInfo, pReq);
8,005,043✔
562

563
  return TSDB_CODE_SUCCESS;
8,005,043✔
564
}
565

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

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

587
  int32_t rspNum = taosArrayGetSize(pRsp.rsps);
6,309,947✔
588

589
  (void)taosThreadMutexLock(&clientHbMgr.lock);
6,310,365✔
590

591
  SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, idx);
6,310,532✔
592
  if (pAppHbMgr == NULL) {
6,310,532!
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;
6,310,532✔
602

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

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

620
  if (rspNum) {
6,288,215✔
621
    tscDebug("hb got %d rsp, %d empty rsp received before", rspNum,
6,263,151✔
622
             atomic_val_compare_exchange_32(&emptyRspNum, emptyRspNum, 0));
623
  } else {
624
    (void)atomic_add_fetch_32(&emptyRspNum, 1);
25,064✔
625
  }
626

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

635
  (void)taosThreadMutexUnlock(&clientHbMgr.lock);
6,288,215✔
636

637
  tFreeClientHbBatchRsp(&pRsp);
638

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

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

650
  void *pIter = taosHashIterate(pObj->pRequests, NULL);
7,239,050✔
651
  while (pIter != NULL) {
14,582,737✔
652
    int64_t     *rid = pIter;
7,343,687✔
653
    SRequestObj *pRequest = acquireRequest(*rid);
7,343,687✔
654
    if (NULL == pRequest) {
7,343,687✔
655
      pIter = taosHashIterate(pObj->pRequests, pIter);
5,409✔
656
      continue;
5,409✔
657
    }
658

659
    if (pRequest->killed || 0 == pRequest->body.queryJob) {
7,338,278✔
660
      (void)releaseRequest(*rid);
1,933,584✔
661
      pIter = taosHashIterate(pObj->pRequests, pIter);
1,933,584✔
662
      continue;
1,933,584✔
663
    }
664

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

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

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

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

703
    pIter = taosHashIterate(pObj->pRequests, pIter);
5,404,694✔
704
  }
705

706
  return code;
7,239,050✔
707
}
708

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

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

723
  hbBasic->connId = pTscObj->connId;
8,033,497✔
724

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

733
  hbBasic->queryDesc = taosArrayInit(numOfQueries, sizeof(SQueryDesc));
7,239,050✔
734
  if (NULL == hbBasic->queryDesc) {
7,239,050!
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);
7,239,050✔
742
  if (code) {
7,239,050✔
743
    releaseTscObj(connKey->tscRid);
1,419,433✔
744
    if (hbBasic->queryDesc) {
1,419,433!
745
      taosArrayDestroyEx(hbBasic->queryDesc, tFreeClientHbQueryDesc);
1,419,433✔
746
    }
747
    taosMemoryFree(hbBasic);
1,419,433!
748
    return code;
1,419,433✔
749
  }
750

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

754
  return TSDB_CODE_SUCCESS;
5,819,617✔
755
}
756

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

764
  int32_t code = 0;
341,705✔
765

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

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

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

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

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

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

827
  return code;
341,705✔
828
}
829

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

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

840
  if (userNum <= 0) {
4,396,469✔
841
    taosMemoryFree(users);
226,947!
842
    return TSDB_CODE_SUCCESS;
226,947✔
843
  }
844

845
  for (int32_t i = 0; i < userNum; ++i) {
8,492,754✔
846
    SUserAuthVersion *user = &users[i];
4,323,232✔
847
    user->version = htonl(user->version);
4,323,232✔
848
  }
849

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

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

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

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

872
  return TSDB_CODE_SUCCESS;
4,169,522✔
873
}
874

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

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

885
  if (dbNum <= 0) {
4,894,223✔
886
    taosMemoryFree(dbs);
3,437,255!
887
    return TSDB_CODE_SUCCESS;
3,437,255✔
888
  }
889

890
  for (int32_t i = 0; i < dbNum; ++i) {
3,141,168✔
891
    SDbCacheInfo *db = &dbs[i];
1,684,200✔
892
    tscDebug("the %dth expired db:%s, dbId:%" PRId64
1,684,200✔
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);
1,684,200✔
897
    db->vgVersion = htonl(db->vgVersion);
1,684,200✔
898
    db->cfgVersion = htonl(db->cfgVersion);
1,684,200✔
899
    db->numOfTable = htonl(db->numOfTable);
1,684,200✔
900
    db->stateTs = htobe64(db->stateTs);
1,684,200✔
901
    db->tsmaVersion = htonl(db->tsmaVersion);
1,684,200✔
902
  }
903

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

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

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

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

926
  return TSDB_CODE_SUCCESS;
1,456,968✔
927
}
928

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

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

939
  if (stbNum <= 0) {
4,894,223✔
940
    taosMemoryFree(stbs);
3,850,831!
941
    return TSDB_CODE_SUCCESS;
3,850,831✔
942
  }
943

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

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

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

960
  if (NULL == req->info) {
1,043,392!
UNCOV
961
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
×
UNCOV
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));
1,043,392✔
969
  if (TSDB_CODE_SUCCESS != code) {
1,043,392!
970
    taosMemoryFree(stbs);
×
971
    return code;
×
972
  }
973

974
  return TSDB_CODE_SUCCESS;
1,043,392✔
975
}
976

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

983
  TSC_ERR_JRET(catalogGetExpiredViews(pCatalog, &views, &viewNum, &pDynViewVer));
4,894,223!
984

985
  if (viewNum <= 0) {
4,894,223✔
986
    taosMemoryFree(views);
4,893,937!
987
    taosMemoryFree(pDynViewVer);
4,893,937!
988
    return TSDB_CODE_SUCCESS;
4,893,937✔
989
  }
990

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

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

1000
  if (NULL == req->info) {
286!
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 = {
286✔
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)));
286!
1014

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

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

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

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

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

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

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

UNCOV
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

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

1069
int32_t hbGetAppInfo(int64_t clusterId, SClientHbReq *req) {
6,614,064✔
1070
  SAppHbReq *pApp = taosHashGet(clientHbMgr.appSummary, &clusterId, sizeof(clusterId));
6,614,064✔
1071
  if (NULL != pApp) {
6,614,064!
1072
    (void)memcpy(&req->app, pApp, sizeof(*pApp));
6,614,064!
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;
6,614,064✔
1081
}
1082

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

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

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

1101
    code = hbGetAppInfo(hbParam->clusterId, req);
4,894,777✔
1102
    if (TSDB_CODE_SUCCESS != code) {
4,894,777!
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))) {
4,894,777✔
1108
      code = hbGetExpiredUserInfo(connKey, pCatalog, req);
4,396,469✔
1109
      if (TSDB_CODE_SUCCESS != code) {
4,396,469!
1110
        tscWarn("hbGetExpiredUserInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1111
        return code;
×
1112
      }
1113
      if (clientHbMgr.appHbHash) {
4,396,469✔
1114
        code = taosHashPut(clientHbMgr.appHbHash, &hbParam->clusterId, sizeof(uint64_t), NULL, 0);
593,293✔
1115
        if (TSDB_CODE_SUCCESS != code) {
593,293!
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)) {
4,894,777✔
1125
      code = hbGetUserAuthInfo(connKey, hbParam, req);
342,259✔
1126
      if (TSDB_CODE_SUCCESS != code) {
342,259✔
1127
        tscWarn("hbGetUserAuthInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
554!
1128
        return code;
554✔
1129
      }
1130
      atomic_store_8(&hbParam->pAppHbMgr->connHbFlag, 1);
341,705✔
1131
    }
1132

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

1139
    code = hbGetExpiredStbInfo(connKey, pCatalog, req);
4,894,223✔
1140
    if (TSDB_CODE_SUCCESS != code) {
4,894,223!
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);
4,894,223✔
1147
    if (TSDB_CODE_SUCCESS != code) {
4,894,223!
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);
4,894,223✔
1153
    if (TSDB_CODE_SUCCESS != code) {
4,894,223!
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);
1,719,287✔
1159
    if (TSDB_CODE_SUCCESS != code) {
1,719,287!
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
6,613,510✔
1166

1167
  return TSDB_CODE_SUCCESS;
6,613,510✔
1168
}
1169

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

1175
  clientHbMgr.rspHandle[CONN_TYPE__QUERY] = hbQueryHbRspHandle;
1,355,852✔
1176
  clientHbMgr.rspHandle[CONN_TYPE__TMQ] = hbMqHbRspHandle;
1,355,852✔
1177
}
1,355,852✔
1178

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

1191
  int64_t  maxIpWhiteVer = 0;
6,311,879✔
1192
  void    *pIter = NULL;
6,311,879✔
1193
  SHbParam param = {0};
6,311,879✔
1194
  while ((pIter = taosHashIterate(pAppHbMgr->activeInfo, pIter))) {
14,522,109✔
1195
    SClientHbReq *pOneReq = pIter;
8,210,230✔
1196
    SClientHbKey *connKey = &pOneReq->connKey;
8,210,230✔
1197
    STscObj      *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
8,210,230✔
1198

1199
    if (!pTscObj || atomic_load_8(&pTscObj->dropped) == 1) {
8,210,230✔
1200
      if (pTscObj) releaseTscObj(connKey->tscRid);
176,733✔
1201
      continue;
176,733✔
1202
    }
1203

1204
    tstrncpy(pOneReq->userApp, pTscObj->optionInfo.userApp, sizeof(pOneReq->userApp));
8,033,497!
1205
    tstrncpy(pOneReq->cInfo, pTscObj->optionInfo.cInfo, sizeof(pOneReq->cInfo));
8,033,497!
1206
    pOneReq->userIp = pTscObj->optionInfo.userIp;
8,033,497✔
1207
    pOneReq->userDualIp = pTscObj->optionInfo.userDualIp;
8,033,497✔
1208
    tstrncpy(pOneReq->sVer, td_version, TSDB_VERSION_LEN);
8,033,497!
1209

1210
    pOneReq = taosArrayPush((*pBatchReq)->reqs, pOneReq);
8,033,497✔
1211
    if (NULL == pOneReq) {
8,033,497!
1212
      releaseTscObj(connKey->tscRid);
×
1213
      continue;
×
1214
    }
1215

1216
    switch (connKey->connType) {
8,033,497!
1217
      case CONN_TYPE__QUERY: {
8,033,497✔
1218
        if (param.clusterId == 0) {
8,033,497✔
1219
          // init
1220
          param.clusterId = pOneReq->clusterId;
6,286,815✔
1221
          param.pAppHbMgr = pAppHbMgr;
6,286,815✔
1222
          param.connHbFlag = atomic_load_8(&pAppHbMgr->connHbFlag);
6,286,815✔
1223
        }
1224
        break;
8,033,497✔
1225
      }
1226
      default:
×
1227
        break;
×
1228
    }
1229
    if (clientHbMgr.reqHandle[connKey->connType]) {
8,033,497!
1230
      int32_t code = (*clientHbMgr.reqHandle[connKey->connType])(connKey, &param, pOneReq);
8,033,497✔
1231
      if (code) {
8,033,497✔
1232
        tscWarn("hbGatherAllInfo failed since %s, tscRid:%" PRIi64 ", connType:%" PRIi8, tstrerror(code),
1,419,987!
1233
                connKey->tscRid, connKey->connType);
1234
      }
1235
    }
1236

1237
    int64_t ver = atomic_load_64(&pTscObj->whiteListInfo.ver);
8,033,497✔
1238
    maxIpWhiteVer = TMAX(maxIpWhiteVer, ver);
8,033,497✔
1239
    releaseTscObj(connKey->tscRid);
8,033,497✔
1240
  }
1241
  (*pBatchReq)->ipWhiteListVer = maxIpWhiteVer;
6,311,879✔
1242

1243
  return TSDB_CODE_SUCCESS;
6,311,879✔
1244
}
1245

1246
void hbThreadFuncUnexpectedStopped(void) { atomic_store_8(&clientHbMgr.threadStop, 2); }
×
1247

1248
void hbMergeSummary(SAppClusterSummary *dst, SAppClusterSummary *src) {
813,730✔
1249
  dst->numOfInsertsReq += src->numOfInsertsReq;
813,730✔
1250
  dst->numOfInsertRows += src->numOfInsertRows;
813,730✔
1251
  dst->insertElapsedTime += src->insertElapsedTime;
813,730✔
1252
  dst->insertBytes += src->insertBytes;
813,730✔
1253
  dst->fetchBytes += src->fetchBytes;
813,730✔
1254
  dst->queryElapsedTime += src->queryElapsedTime;
813,730✔
1255
  dst->numOfSlowQueries += src->numOfSlowQueries;
813,730✔
1256
  dst->totalRequests += src->totalRequests;
813,730✔
1257
  dst->currentRequests += src->currentRequests;
813,730✔
1258
}
813,730✔
1259

1260
int32_t hbGatherAppInfo(void) {
6,941,975✔
1261
  SAppHbReq req = {0};
6,941,975✔
1262
  int32_t   code = TSDB_CODE_SUCCESS;
6,941,975✔
1263
  int       sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
6,941,975✔
1264
  if (sz > 0) {
6,941,975!
1265
    req.pid = taosGetPId();
6,941,975✔
1266
    req.appId = clientHbMgr.appId;
6,941,975✔
1267
    TSC_ERR_RET(taosGetAppName(req.name, NULL));
6,941,975!
1268
  }
1269

1270
  taosHashClear(clientHbMgr.appSummary);
6,941,975✔
1271

1272
  for (int32_t i = 0; i < sz; ++i) {
14,697,877✔
1273
    SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
7,755,902✔
1274
    if (pAppHbMgr == NULL) continue;
7,755,902!
1275

1276
    int64_t    clusterId = pAppHbMgr->pAppInstInfo->clusterId;
7,755,902✔
1277
    SAppHbReq *pApp = taosHashGet(clientHbMgr.appSummary, &clusterId, sizeof(clusterId));
7,755,902✔
1278
    if (NULL == pApp) {
7,755,902✔
1279
      (void)memcpy(&req.summary, &pAppHbMgr->pAppInstInfo->summary, sizeof(req.summary));
6,942,172✔
1280
      req.startTime = pAppHbMgr->startTime;
6,942,172✔
1281
      TSC_ERR_RET(taosHashPut(clientHbMgr.appSummary, &clusterId, sizeof(clusterId), &req, sizeof(req)));
6,942,172!
1282
    } else {
1283
      if (pAppHbMgr->startTime < pApp->startTime) {
813,730!
1284
        pApp->startTime = pAppHbMgr->startTime;
×
1285
      }
1286

1287
      hbMergeSummary(&pApp->summary, &pAppHbMgr->pAppInstInfo->summary);
813,730✔
1288
    }
1289
  }
1290

1291
  return TSDB_CODE_SUCCESS;
6,941,975✔
1292
}
1293

1294
static void *hbThreadFunc(void *param) {
1,355,852✔
1295
  setThreadName("hb");
1,355,852✔
1296
#ifdef WINDOWS
1297
  if (taosCheckCurrentInDll()) {
1298
    atexit(hbThreadFuncUnexpectedStopped);
1299
  }
1300
#endif
1301
  while (1) {
5,738,355✔
1302
    if (1 == clientHbMgr.threadStop) {
7,094,207✔
1303
      break;
145,769✔
1304
    }
1305

1306
    if (TSDB_CODE_SUCCESS != taosThreadMutexLock(&clientHbMgr.lock)) {
6,948,438!
1307
      tscError("taosThreadMutexLock failed");
×
1308
      return NULL;
×
1309
    }
1310

1311
    int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
6,948,438✔
1312
    if (sz > 0) {
6,948,438✔
1313
      if (TSDB_CODE_SUCCESS != hbGatherAppInfo()) {
6,941,975!
1314
        tscError("hbGatherAppInfo failed");
×
1315
        return NULL;
×
1316
      }
1317
      if (sz > 1 && !clientHbMgr.appHbHash) {
6,941,975✔
1318
        clientHbMgr.appHbHash = taosHashInit(0, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), true, HASH_NO_LOCK);
10,907✔
1319
        if (NULL == clientHbMgr.appHbHash) {
10,907!
1320
          tscError("taosHashInit failed");
×
1321
          return NULL;
×
1322
        }
1323
      }
1324
      taosHashClear(clientHbMgr.appHbHash);
6,941,975✔
1325
    }
1326

1327
    for (int i = 0; i < sz; i++) {
14,704,340✔
1328
      SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
7,755,902✔
1329
      if (pAppHbMgr == NULL) {
7,755,902!
1330
        continue;
13,428✔
1331
      }
1332

1333
      int32_t connCnt = atomic_load_32(&pAppHbMgr->connKeyCnt);
7,755,902✔
1334
      if (connCnt == 0) {
7,755,902✔
1335
        continue;
1,444,023✔
1336
      }
1337
      SClientHbBatchReq *pReq = NULL;
6,311,879✔
1338
      int32_t            code = hbGatherAllInfo(pAppHbMgr, &pReq);
6,311,879✔
1339
      if (TSDB_CODE_SUCCESS != code || taosArrayGetP(clientHbMgr.appHbMgrs, i) == NULL) {
6,311,879!
1340
        terrno = code ? code : TSDB_CODE_OUT_OF_RANGE;
×
1341
        tFreeClientHbBatchReq(pReq);
×
1342
        continue;
×
1343
      }
1344
      int tlen = tSerializeSClientHbBatchReq(NULL, 0, pReq);
6,311,879✔
1345
      if (tlen == -1) {
6,311,879!
1346
        tFreeClientHbBatchReq(pReq);
×
1347
        break;
×
1348
      }
1349
      void *buf = taosMemoryMalloc(tlen);
6,311,879!
1350
      if (buf == NULL) {
6,311,879!
1351
        tFreeClientHbBatchReq(pReq);
×
1352
        // hbClearReqInfo(pAppHbMgr);
1353
        break;
×
1354
      }
1355

1356
      if (tSerializeSClientHbBatchReq(buf, tlen, pReq) == -1) {
6,311,879!
1357
        tFreeClientHbBatchReq(pReq);
×
1358
        taosMemoryFree(buf);
×
1359
        break;
×
1360
      }
1361
      SMsgSendInfo *pInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
6,311,879!
1362

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

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

1396
    if (TSDB_CODE_SUCCESS != taosThreadMutexUnlock(&clientHbMgr.lock)) {
6,948,438!
1397
      tscError("taosThreadMutexLock failed");
×
1398
      return NULL;
×
1399
    }
1400
    taosMsleep(HEARTBEAT_INTERVAL);
6,948,438✔
1401
  }
1402
  taosHashCleanup(clientHbMgr.appHbHash);
145,769✔
1403
  return NULL;
145,769✔
1404
}
1405

1406
static int32_t hbCreateThread() {
1,355,852✔
1407
  int32_t      code = TSDB_CODE_SUCCESS;
1,355,852✔
1408
  TdThreadAttr thAttr;
1,350,761✔
1409
  TSC_ERR_JRET(taosThreadAttrInit(&thAttr));
1,355,852!
1410
  TSC_ERR_JRET(taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE));
1,355,852!
1411
#ifdef TD_COMPACT_OS
1412
  TSC_ERR_JRET(taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL));
1413
#endif
1414

1415
  if (taosThreadCreate(&clientHbMgr.thread, &thAttr, hbThreadFunc, NULL) != 0) {
1,355,852!
1416
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
1417
    TSC_ERR_RET(terrno);
×
1418
  }
1419
  (void)taosThreadAttrDestroy(&thAttr);
1,355,852✔
1420
_return:
1,355,852✔
1421

1422
  if (code) {
1,355,852!
1423
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
1424
    TSC_ERR_RET(terrno);
×
1425
  }
1426

1427
  return code;
1,355,852✔
1428
}
1429

1430
static void hbStopThread() {
1,356,558✔
1431
  if (0 == atomic_load_8(&clientHbMgr.inited)) {
1,356,558✔
1432
    return;
706✔
1433
  }
1434
  if (atomic_val_compare_exchange_8(&clientHbMgr.threadStop, 0, 1)) {
1,355,852!
1435
    tscDebug("hb thread already stopped");
×
1436
    return;
×
1437
  }
1438

1439
  int32_t code = TSDB_CODE_SUCCESS;
1,355,852✔
1440
  // thread quit mode kill or inner exit from self-thread
1441
  if (clientHbMgr.quitByKill) {
1,355,852✔
1442
    code = taosThreadKill(clientHbMgr.thread, 0);
1,243,048✔
1443
    if (TSDB_CODE_SUCCESS != code) {
1,243,048!
1444
      tscError("taosThreadKill failed since %s", tstrerror(code));
×
1445
    }
1446
  } else {
1447
    code = taosThreadJoin(clientHbMgr.thread, NULL);
112,804✔
1448
    if (TSDB_CODE_SUCCESS != code) {
112,804!
1449
      tscError("taosThreadJoin failed since %s", tstrerror(code));
×
1450
    }
1451
  }
1452

1453
  tscDebug("hb thread stopped");
1,355,852✔
1454
}
1455

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

1474
  // init app info
1475
  (*pAppHbMgr)->pAppInstInfo = pAppInstInfo;
1,373,200✔
1476

1477
  // init hash info
1478
  (*pAppHbMgr)->activeInfo = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
1,373,200✔
1479

1480
  if ((*pAppHbMgr)->activeInfo == NULL) {
1,373,200!
1481
    TSC_ERR_JRET(terrno);
×
1482
  }
1483

1484
  // taosHashSetFreeFp(pAppHbMgr->activeInfo, tFreeClientHbReq);
1485

1486
  TSC_ERR_JRET(taosThreadMutexLock(&clientHbMgr.lock));
1,373,200!
1487
  if (taosArrayPush(clientHbMgr.appHbMgrs, &(*pAppHbMgr)) == NULL) {
2,746,400!
1488
    code = terrno;
×
1489
    (void)taosThreadMutexUnlock(&clientHbMgr.lock);
×
1490
    goto _return;
×
1491
  }
1492
  (*pAppHbMgr)->idx = taosArrayGetSize(clientHbMgr.appHbMgrs) - 1;
1,373,200✔
1493
  TSC_ERR_JRET(taosThreadMutexUnlock(&clientHbMgr.lock));
1,373,200!
1494

1495
  return TSDB_CODE_SUCCESS;
1,373,200✔
1496
_return:
×
1497
  taosMemoryFree(*pAppHbMgr);
×
1498
  return code;
×
1499
}
1500

1501
void hbFreeAppHbMgr(SAppHbMgr *pTarget) {
1,373,200✔
1502
  void *pIter = taosHashIterate(pTarget->activeInfo, NULL);
1,373,200✔
1503
  while (pIter != NULL) {
1,421,161✔
1504
    SClientHbReq *pOneReq = pIter;
47,961!
1505
    tFreeClientHbReq(pOneReq);
1506
    pIter = taosHashIterate(pTarget->activeInfo, pIter);
47,961✔
1507
  }
1508
  taosHashCleanup(pTarget->activeInfo);
1,373,200✔
1509
  pTarget->activeInfo = NULL;
1,373,200✔
1510

1511
  taosMemoryFree(pTarget->key);
1,373,200!
1512
  taosMemoryFree(pTarget);
1,373,200!
1513
}
1,373,200✔
1514

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

1537
void appHbMgrCleanup(void) {
1,355,852✔
1538
  int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
1,355,852✔
1539
  for (int i = 0; i < sz; i++) {
2,729,052✔
1540
    SAppHbMgr *pTarget = taosArrayGetP(clientHbMgr.appHbMgrs, i);
1,373,200✔
1541
    if (pTarget == NULL) continue;
1,373,200!
1542
    hbFreeAppHbMgr(pTarget);
1,373,200✔
1543
  }
1544
}
1,355,852✔
1545

1546
int32_t hbMgrInit() {
1,373,200✔
1547
  // init once
1548
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 0, 1);
1,373,200✔
1549
  if (old == 1) return 0;
1,373,200✔
1550

1551
  clientHbMgr.appId = tGenIdPI64();
1,355,852✔
1552
  tscInfo("app initialized, appId:0x%" PRIx64, clientHbMgr.appId);
1,355,852!
1553

1554
  clientHbMgr.appSummary = taosHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
1,355,852✔
1555
  if (NULL == clientHbMgr.appSummary) {
1,355,852!
1556
    uError("hbMgrInit:taosHashInit error") return terrno;
×
1557
  }
1558
  clientHbMgr.appHbMgrs = taosArrayInit(0, sizeof(void *));
1,355,852✔
1559
  if (NULL == clientHbMgr.appHbMgrs) {
1,355,852!
1560
    uError("hbMgrInit:taosArrayInit error") return terrno;
×
1561
  }
1562
  TdThreadMutexAttr attr = {0};
1,355,852✔
1563

1564
  int ret = taosThreadMutexAttrInit(&attr);
1,355,852✔
1565
  if (ret != 0) {
1,355,852!
1566
    uError("hbMgrInit:taosThreadMutexAttrInit error") return ret;
×
1567
  }
1568

1569
  ret = taosThreadMutexAttrSetType(&attr, PTHREAD_MUTEX_RECURSIVE);
1,355,852✔
1570
  if (ret != 0) {
1,355,852!
1571
    uError("hbMgrInit:taosThreadMutexAttrSetType error") return ret;
×
1572
  }
1573

1574
  ret = taosThreadMutexInit(&clientHbMgr.lock, &attr);
1,355,852✔
1575
  if (ret != 0) {
1,355,852!
1576
    uError("hbMgrInit:taosThreadMutexInit error") return ret;
×
1577
  }
1578

1579
  ret = taosThreadMutexAttrDestroy(&attr);
1,355,852✔
1580
  if (ret != 0) {
1,355,852!
1581
    uError("hbMgrInit:taosThreadMutexAttrDestroy error") return ret;
×
1582
  }
1583

1584
  // init handle funcs
1585
  hbMgrInitHandle();
1586

1587
  // init backgroud thread
1588
  ret = hbCreateThread();
1,355,852✔
1589
  if (ret != 0) {
1,355,852!
1590
    uError("hbMgrInit:hbCreateThread error") return ret;
×
1591
  }
1592

1593
  return 0;
1,355,852✔
1594
}
1595

1596
void hbMgrCleanUp() {
1,356,558✔
1597
  hbStopThread();
1,356,558✔
1598

1599
  // destroy all appHbMgr
1600
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 1, 0);
1,356,558✔
1601
  if (old == 0) return;
1,356,558✔
1602

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

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

1627
  TSC_ERR_RET(taosHashPut(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey), &hbReq, sizeof(SClientHbReq)));
1,756,246!
1628

1629
  (void)atomic_add_fetch_32(&pAppHbMgr->connKeyCnt, 1);
1,756,246✔
1630
  return 0;
1,756,246✔
1631
}
1632

1633
int32_t hbRegisterConn(SAppHbMgr *pAppHbMgr, int64_t tscRefId, int64_t clusterId, int8_t connType) {
1,769,707✔
1634
  SClientHbKey connKey = {
1,769,707✔
1635
      .tscRid = tscRefId,
1636
      .connType = connType,
1637
  };
1638

1639
  switch (connType) {
1,769,707!
1640
    case CONN_TYPE__QUERY: {
1,756,246✔
1641
      return hbRegisterConnImpl(pAppHbMgr, connKey, clusterId);
1,756,246✔
1642
    }
1643
    case CONN_TYPE__TMQ: {
13,461✔
1644
      return 0;
13,461✔
1645
    }
1646
    default:
×
1647
      return 0;
×
1648
  }
1649
}
1650

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

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