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

taosdata / TDengine / #4888

17 Dec 2025 07:43AM UTC coverage: 65.281% (-0.008%) from 65.289%
#4888

push

travis-ci

web-flow
test: update ci env (#33959)

178960 of 274136 relevant lines covered (65.28%)

101703773.12 hits per line

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

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

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

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

63
    TSC_ERR_JRET(catalogUpdateUserAuthInfo(pCatalog, rsp));
653,837✔
64
  }
65

66
  if (numOfBatchs > 0) {
636,733✔
67
    TSC_ERR_JRET(hbUpdateUserAuthInfo(pAppHbMgr, &batchRsp));
636,733✔
68
  }
69

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

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

77
static int32_t updateUserSessMetric(const char *user, SUserSessCfg *pCfg) {
861,827✔
78
  int32_t code = 0;
861,827✔
79
  int32_t lino = 0;
861,827✔
80
  if (user == NULL || pCfg == NULL) {
861,827✔
81
    return code;
×
82
  }
83

84
  SUserSessCfg cfg = {0, 0, 0, 0, 0};
861,827✔
85

86
  if (memcmp(pCfg, &cfg, sizeof(SUserSessCfg)) == 0) {
861,827✔
87
    return TSDB_CODE_SUCCESS;
861,827✔
88
  }
89
  tscInfo(
×
90
      "update session metric for user:%s, sessPerUser:%d, sessConnTime:%d, sessConnIdleTime:%d, sessMaxConcurrency:%d, "
91
      "sessMaxCallVnodeNum:%d",
92
      user, pCfg->sessPerUser, pCfg->sessConnTime, pCfg->sessConnIdleTime, pCfg->sessMaxConcurrency,
93
      pCfg->sessMaxCallVnodeNum);
94

95
  if (pCfg->sessPerUser != 0) {
×
96
    code = sessMgtUpdataLimit((char *)user, SESSION_PER_USER, pCfg->sessPerUser);
×
97
    TAOS_CHECK_GOTO(code, &lino, _error);
×
98
  }
99

100
  if (pCfg->sessConnTime != 0) {
×
101
    code = sessMgtUpdataLimit((char *)user, SESSION_CONN_TIME, pCfg->sessConnTime);
×
102
    TAOS_CHECK_GOTO(code, &lino, _error);
×
103
  }
104

105
  if (pCfg->sessConnIdleTime != 0) {
×
106
    code = sessMgtUpdataLimit((char *)user, SESSION_CONN_IDLE_TIME, pCfg->sessConnIdleTime);
×
107
    TAOS_CHECK_GOTO(code, &lino, _error);
×
108
  }
109

110
  if (pCfg->sessMaxConcurrency != 0) {
×
111
    code = sessMgtUpdataLimit((char *)user, SESSION_MAX_CONCURRENCY, pCfg->sessMaxConcurrency);
×
112
    TAOS_CHECK_GOTO(code, &lino, _error);
×
113
  }
114

115
  if (pCfg->sessMaxCallVnodeNum != 0) {
×
116
    code = sessMgtUpdataLimit((char *)user, SESSION_MAX_CALL_VNODE_NUM, pCfg->sessMaxCallVnodeNum);
×
117
    TAOS_CHECK_GOTO(code, &lino, _error);
×
118
  }
119
_error:
×
120
  return code;
×
121
}
122
static int32_t hbUpdateUserAuthInfo(SAppHbMgr *pAppHbMgr, SUserAuthBatchRsp *batchRsp) {
636,733✔
123
  int32_t code = 0;
636,733✔
124
  int64_t clusterId = pAppHbMgr->pAppInstInfo->clusterId;
636,733✔
125
  for (int i = 0; i < TARRAY_SIZE(clientHbMgr.appHbMgrs); ++i) {
1,588,277✔
126
    SAppHbMgr *hbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
951,544✔
127
    if (!hbMgr || hbMgr->pAppInstInfo->clusterId != clusterId) {
951,544✔
128
      continue;
14,700✔
129
    }
130

131
    SClientHbReq    *pReq = NULL;
936,844✔
132
    SGetUserAuthRsp *pRsp = NULL;
936,844✔
133
    while ((pReq = taosHashIterate(hbMgr->activeInfo, pReq))) {
1,905,767✔
134
      STscObj *pTscObj = (STscObj *)acquireTscObj(pReq->connKey.tscRid);
1,080,328✔
135
      if (!pTscObj) {
1,080,328✔
136
        continue;
102,861✔
137
      }
138

139
      if (!pRsp) {
977,467✔
140
        for (int32_t j = 0; j < TARRAY_SIZE(batchRsp->pArray); ++j) {
910,951✔
141
          SGetUserAuthRsp *rsp = TARRAY_GET_ELEM(batchRsp->pArray, j);
799,546✔
142
          if (0 == strncmp(rsp->user, pTscObj->user, TSDB_USER_LEN)) {
799,546✔
143
            pRsp = rsp;
670,397✔
144
            break;
670,397✔
145
          }
146
        }
147
        if (!pRsp) {
781,802✔
148
          releaseTscObj(pReq->connKey.tscRid);
111,405✔
149
          taosHashCancelIterate(hbMgr->activeInfo, pReq);
111,405✔
150
          break;
111,405✔
151
        }
152
      }
153

154
      if (pRsp->dropped == 1) {
866,062✔
155
        if (atomic_val_compare_exchange_8(&pTscObj->dropped, 0, 1) == 0) {
4,235✔
156
          if (pTscObj->userDroppedInfo.fp) {
3,432✔
157
            SPassInfo *dropInfo = &pTscObj->userDroppedInfo;
1,850✔
158
            if (dropInfo->fp) {
1,850✔
159
              (*dropInfo->fp)(dropInfo->param, NULL, TAOS_NOTIFY_USER_DROPPED);
1,850✔
160
            }
161
          }
162
        }
163
        code = sessMgtRemoveUser(pTscObj->user);
4,235✔
164
        if (code != 0) {
4,235✔
165
          tscError("failed to remove user session metric, user:%s, code:%d", pTscObj->user, code);  
×
166
        }
167
        releaseTscObj(pReq->connKey.tscRid);
4,235✔
168
        continue;
4,235✔
169
      }
170

171
      pTscObj->authVer = pRsp->version;
861,827✔
172
      if (updateUserSessMetric(pTscObj->user, &pRsp->sessCfg) != 0) {
861,827✔
173
        tscError("failed to update user session metric, user:%s", pTscObj->user);
×
174
      }
175

176
      if (pTscObj->sysInfo != pRsp->sysInfo) {
861,827✔
177
        tscDebug("update sysInfo of user %s from %" PRIi8 " to %" PRIi8 ", conn:%" PRIi64, pRsp->user,
3,700✔
178
                 pTscObj->sysInfo, pRsp->sysInfo, pTscObj->id);
179
        pTscObj->sysInfo = pRsp->sysInfo;
3,700✔
180
      }
181

182
      if (pTscObj->passInfo.fp) {
861,827✔
183
        SPassInfo *passInfo = &pTscObj->passInfo;
1,110✔
184
        int32_t    oldVer = atomic_load_32(&passInfo->ver);
1,110✔
185
        if (oldVer < pRsp->passVer) {
1,110✔
186
          atomic_store_32(&passInfo->ver, pRsp->passVer);
740✔
187
          if (passInfo->fp) {
740✔
188
            (*passInfo->fp)(passInfo->param, &pRsp->passVer, TAOS_NOTIFY_PASSVER);
740✔
189
          }
190
          tscDebug("update passVer of user %s from %d to %d, conn:%" PRIi64, pRsp->user, oldVer,
740✔
191
                   atomic_load_32(&passInfo->ver), pTscObj->id);
192
        }
193
      }
194

195
      if (pTscObj->whiteListInfo.fp) {
861,827✔
196
        SWhiteListInfo *whiteListInfo = &pTscObj->whiteListInfo;
×
197
        int64_t         oldVer = atomic_load_64(&whiteListInfo->ver);
×
198
        if (oldVer != pRsp->whiteListVer) {
×
199
          atomic_store_64(&whiteListInfo->ver, pRsp->whiteListVer);
×
200
          if (whiteListInfo->fp) {
×
201
            (*whiteListInfo->fp)(whiteListInfo->param, &pRsp->whiteListVer, TAOS_NOTIFY_WHITELIST_VER);
×
202
          }
203
          tscDebug("update whitelist version of user %s from %" PRId64 " to %" PRId64 ", conn:%" PRIi64, pRsp->user,
×
204
                   oldVer, atomic_load_64(&whiteListInfo->ver), pTscObj->id);
205
        }
206
      } else {
207
        // Need to update version information to prevent frequent fetching of authentication
208
        // information.
209
        SWhiteListInfo *whiteListInfo = &pTscObj->whiteListInfo;
861,827✔
210
        int64_t         oldVer = atomic_load_64(&whiteListInfo->ver);
861,827✔
211
        atomic_store_64(&whiteListInfo->ver, pRsp->whiteListVer);
861,827✔
212
        tscDebug("update whitelist version of user %s from %" PRId64 " to %" PRId64 ", conn:%" PRIi64, pRsp->user,
861,827✔
213
                 oldVer, atomic_load_64(&whiteListInfo->ver), pTscObj->id);
214
      }
215

216
      if (pTscObj->dateTimeWhiteListInfo.fp) {
861,827✔
217
        SWhiteListInfo *whiteListInfo = &pTscObj->dateTimeWhiteListInfo;
×
218
        int64_t         oldVer = atomic_load_64(&whiteListInfo->ver);
×
219
        if (oldVer != pRsp->timeWhiteListVer) {
×
220
          atomic_store_64(&whiteListInfo->ver, pRsp->timeWhiteListVer);
×
221
          if (whiteListInfo->fp) {
×
222
            (*whiteListInfo->fp)(whiteListInfo->param, &pRsp->timeWhiteListVer, TAOS_NOTIFY_DATETIME_WHITELIST_VER);
×
223
          }
224
          tscDebug("update date time whitelist version of user %s from %" PRId64 " to %" PRId64 ", conn:%" PRIi64, pRsp->user,
×
225
                   oldVer, atomic_load_64(&whiteListInfo->ver), pTscObj->id);
226
        }
227
      } else {
228
        SWhiteListInfo *whiteListInfo = &pTscObj->dateTimeWhiteListInfo;
861,827✔
229
        int64_t         oldVer = atomic_load_64(&whiteListInfo->ver);
861,827✔
230
        atomic_store_64(&whiteListInfo->ver, pRsp->timeWhiteListVer);
861,827✔
231
        tscDebug("update date time whitelist version of user %s from %" PRId64 " to %" PRId64 ", conn:%" PRIi64, pRsp->user,
861,827✔
232
                 oldVer, atomic_load_64(&whiteListInfo->ver), pTscObj->id);
233
      }
234
      releaseTscObj(pReq->connKey.tscRid);
861,827✔
235
    }
236
  }
237
  return 0;
636,733✔
238
}
239

240
static int32_t hbGenerateVgInfoFromRsp(SDBVgInfo **pInfo, SUseDbRsp *rsp) {
1,261,277✔
241
  int32_t    code = 0;
1,261,277✔
242
  SDBVgInfo *vgInfo = taosMemoryCalloc(1, sizeof(SDBVgInfo));
1,261,277✔
243
  if (NULL == vgInfo) {
1,261,277✔
244
    return terrno;
×
245
  }
246

247
  vgInfo->vgVersion = rsp->vgVersion;
1,261,277✔
248
  vgInfo->stateTs = rsp->stateTs;
1,261,277✔
249
  vgInfo->flags = rsp->flags;
1,261,277✔
250
  vgInfo->hashMethod = rsp->hashMethod;
1,261,277✔
251
  vgInfo->hashPrefix = rsp->hashPrefix;
1,261,277✔
252
  vgInfo->hashSuffix = rsp->hashSuffix;
1,261,277✔
253
  vgInfo->vgHash = taosHashInit(rsp->vgNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
1,261,277✔
254
  if (NULL == vgInfo->vgHash) {
1,261,277✔
255
    tscError("hash init[%d] failed", rsp->vgNum);
×
256
    code = terrno;
×
257
    goto _return;
×
258
  }
259

260
  for (int32_t j = 0; j < rsp->vgNum; ++j) {
4,697,735✔
261
    SVgroupInfo *pInfo = taosArrayGet(rsp->pVgroupInfos, j);
3,436,458✔
262
    if (taosHashPut(vgInfo->vgHash, &pInfo->vgId, sizeof(int32_t), pInfo, sizeof(SVgroupInfo)) != 0) {
3,436,458✔
263
      tscError("hash push failed, terrno:%d", terrno);
×
264
      code = terrno;
×
265
      goto _return;
×
266
    }
267
  }
268

269
_return:
1,261,277✔
270
  if (code) {
1,261,277✔
271
    taosHashCleanup(vgInfo->vgHash);
×
272
    taosMemoryFreeClear(vgInfo);
×
273
  }
274

275
  *pInfo = vgInfo;
1,261,277✔
276
  return code;
1,261,277✔
277
}
278

279
static int32_t hbProcessDBInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
4,338,888✔
280
  int32_t code = 0;
4,338,888✔
281

282
  SDbHbBatchRsp batchRsp = {0};
4,338,888✔
283
  if (tDeserializeSDbHbBatchRsp(value, valueLen, &batchRsp) != 0) {
4,338,888✔
284
    terrno = TSDB_CODE_INVALID_MSG;
×
285
    code = terrno;
×
286
    goto _return;
×
287
  }
288

289
  int32_t numOfBatchs = taosArrayGetSize(batchRsp.pArray);
4,338,888✔
290
  for (int32_t i = 0; i < numOfBatchs; ++i) {
5,598,980✔
291
    SDbHbRsp *rsp = taosArrayGet(batchRsp.pArray, i);
1,260,092✔
292
    if (NULL == rsp) {
1,260,092✔
293
      code = terrno;
×
294
      goto _return;
×
295
    }
296
    if (rsp->useDbRsp) {
1,260,092✔
297
      tscDebug("hb use db rsp, db:%s, vgVersion:%d, stateTs:%" PRId64 ", uid:%" PRIx64, rsp->useDbRsp->db,
1,046,853✔
298
               rsp->useDbRsp->vgVersion, rsp->useDbRsp->stateTs, rsp->useDbRsp->uid);
299

300
      if (rsp->useDbRsp->vgVersion < 0) {
1,046,853✔
301
        tscDebug("hb to remove db, db:%s", rsp->useDbRsp->db);
31,790✔
302
        code = catalogRemoveDB(pCatalog, rsp->useDbRsp->db, rsp->useDbRsp->uid);
31,790✔
303
      } else {
304
        SDBVgInfo *vgInfo = NULL;
1,015,063✔
305
        code = hbGenerateVgInfoFromRsp(&vgInfo, rsp->useDbRsp);
1,015,063✔
306
        if (TSDB_CODE_SUCCESS != code) {
1,015,063✔
307
          goto _return;
×
308
        }
309

310
        tscDebug("hb to update db vgInfo, db:%s", rsp->useDbRsp->db);
1,015,063✔
311

312
        TSC_ERR_JRET(catalogUpdateDBVgInfo(pCatalog, rsp->useDbRsp->db, rsp->useDbRsp->uid, vgInfo));
1,015,063✔
313

314
        if (IS_SYS_DBNAME(rsp->useDbRsp->db)) {
1,015,063✔
315
          code = hbGenerateVgInfoFromRsp(&vgInfo, rsp->useDbRsp);
246,214✔
316
          if (TSDB_CODE_SUCCESS != code) {
246,214✔
317
            goto _return;
×
318
          }
319

320
          TSC_ERR_JRET(catalogUpdateDBVgInfo(
246,214✔
321
              pCatalog, (rsp->useDbRsp->db[0] == 'i') ? TSDB_PERFORMANCE_SCHEMA_DB : TSDB_INFORMATION_SCHEMA_DB,
322
              rsp->useDbRsp->uid, vgInfo));
323
        }
324
      }
325
    }
326

327
    if (rsp->cfgRsp) {
1,260,092✔
328
      tscDebug("hb db cfg rsp, db:%s, cfgVersion:%d", rsp->cfgRsp->db, rsp->cfgRsp->cfgVersion);
156,983✔
329
      code = catalogUpdateDbCfg(pCatalog, rsp->cfgRsp->db, rsp->cfgRsp->dbId, rsp->cfgRsp);
156,983✔
330
      rsp->cfgRsp = NULL;
156,983✔
331
    }
332
    if (rsp->pTsmaRsp) {
1,260,092✔
333
      if (rsp->pTsmaRsp->pTsmas) {
595,981✔
334
        for (int32_t i = 0; i < rsp->pTsmaRsp->pTsmas->size; ++i) {
1,418✔
335
          STableTSMAInfo *pTsma = taosArrayGetP(rsp->pTsmaRsp->pTsmas, i);
709✔
336
          if (NULL == pTsma) {
709✔
337
            TSC_ERR_JRET(TSDB_CODE_OUT_OF_RANGE);
×
338
          }
339
          TSC_ERR_JRET(catalogAsyncUpdateTSMA(pCatalog, &pTsma, rsp->dbTsmaVersion));
709✔
340
        }
341
        taosArrayClear(rsp->pTsmaRsp->pTsmas);
709✔
342
      } else {
343
        TSC_ERR_JRET(catalogAsyncUpdateDbTsmaVersion(pCatalog, rsp->dbTsmaVersion, rsp->db, rsp->dbId));
595,272✔
344
      }
345
    }
346
  }
347

348
_return:
4,338,888✔
349

350
  tFreeSDbHbBatchRsp(&batchRsp);
4,338,888✔
351
  return code;
4,338,888✔
352
}
353

354
static int32_t hbProcessStbInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
3,132,547✔
355
  int32_t code = TSDB_CODE_SUCCESS;
3,132,547✔
356

357
  SSTbHbRsp hbRsp = {0};
3,132,547✔
358
  if (tDeserializeSSTbHbRsp(value, valueLen, &hbRsp) != 0) {
3,132,547✔
359
    terrno = TSDB_CODE_INVALID_MSG;
×
360
    return -1;
×
361
  }
362

363
  int32_t numOfMeta = taosArrayGetSize(hbRsp.pMetaRsp);
3,132,547✔
364
  for (int32_t i = 0; i < numOfMeta; ++i) {
3,187,184✔
365
    STableMetaRsp *rsp = taosArrayGet(hbRsp.pMetaRsp, i);
54,637✔
366
    if (NULL == rsp) {
54,637✔
367
      code = terrno;
×
368
      goto _return;
×
369
    }
370
    if (rsp->numOfColumns < 0) {
54,637✔
371
      tscDebug("hb to remove stb, db:%s, stb:%s", rsp->dbFName, rsp->stbName);
35,882✔
372
      TSC_ERR_JRET(catalogRemoveStbMeta(pCatalog, rsp->dbFName, rsp->dbId, rsp->stbName, rsp->suid));
35,882✔
373
    } else {
374
      tscDebug("hb to update stb, db:%s, stb:%s", rsp->dbFName, rsp->stbName);
18,755✔
375
      if (rsp->pSchemas[0].colId != PRIMARYKEY_TIMESTAMP_COL_ID) {
18,755✔
376
        tscError("invalid colId[%" PRIi16 "] for the first column in table meta rsp msg", rsp->pSchemas[0].colId);
×
377
        tFreeSSTbHbRsp(&hbRsp);
×
378
        return TSDB_CODE_TSC_INVALID_VALUE;
×
379
      }
380

381
      TSC_ERR_JRET(catalogAsyncUpdateTableMeta(pCatalog, rsp));
18,755✔
382
    }
383
  }
384

385
  int32_t numOfIndex = taosArrayGetSize(hbRsp.pIndexRsp);
3,132,547✔
386
  for (int32_t i = 0; i < numOfIndex; ++i) {
3,132,547✔
387
    STableIndexRsp *rsp = taosArrayGet(hbRsp.pIndexRsp, i);
×
388
    if (NULL == rsp) {
×
389
      code = terrno;
×
390
      goto _return;
×
391
    }
392
    TSC_ERR_JRET(catalogUpdateTableIndex(pCatalog, rsp));
×
393
  }
394

395
_return:
3,132,547✔
396
  taosArrayDestroy(hbRsp.pIndexRsp);
3,132,547✔
397
  hbRsp.pIndexRsp = NULL;
3,132,547✔
398

399
  tFreeSSTbHbRsp(&hbRsp);
3,132,547✔
400
  return code;
3,132,547✔
401
}
402

403
static int32_t hbProcessDynViewRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
4,010✔
404
  return catalogUpdateDynViewVer(pCatalog, (SDynViewVersion *)value);
4,010✔
405
}
406

407
static void hbFreeSViewMetaInRsp(void *p) {
×
408
  if (NULL == p || NULL == *(void **)p) {
×
409
    return;
×
410
  }
411
  SViewMetaRsp *pRsp = *(SViewMetaRsp **)p;
×
412
  tFreeSViewMetaRsp(pRsp);
×
413
  taosMemoryFreeClear(pRsp);
×
414
}
415

416
static int32_t hbProcessViewInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
4,010✔
417
  int32_t code = TSDB_CODE_SUCCESS;
4,010✔
418

419
  SViewHbRsp hbRsp = {0};
4,010✔
420
  if (tDeserializeSViewHbRsp(value, valueLen, &hbRsp) != 0) {
4,010✔
421
    taosArrayDestroyEx(hbRsp.pViewRsp, hbFreeSViewMetaInRsp);
×
422
    terrno = TSDB_CODE_INVALID_MSG;
×
423
    return -1;
×
424
  }
425

426
  int32_t numOfMeta = taosArrayGetSize(hbRsp.pViewRsp);
4,010✔
427
  for (int32_t i = 0; i < numOfMeta; ++i) {
4,464✔
428
    SViewMetaRsp *rsp = taosArrayGetP(hbRsp.pViewRsp, i);
454✔
429
    if (NULL == rsp) {
454✔
430
      code = terrno;
×
431
      goto _return;
×
432
    }
433
    if (rsp->numOfCols < 0) {
454✔
434
      tscDebug("hb to remove view, db:%s, view:%s", rsp->dbFName, rsp->name);
454✔
435
      code = catalogRemoveViewMeta(pCatalog, rsp->dbFName, rsp->dbId, rsp->name, rsp->viewId);
454✔
436
      tFreeSViewMetaRsp(rsp);
454✔
437
      taosMemoryFreeClear(rsp);
454✔
438
    } else {
439
      tscDebug("hb to update view, db:%s, view:%s", rsp->dbFName, rsp->name);
×
440
      code = catalogUpdateViewMeta(pCatalog, rsp);
×
441
    }
442
    TSC_ERR_JRET(code);
454✔
443
  }
444

445
_return:
4,010✔
446
  taosArrayDestroy(hbRsp.pViewRsp);
4,010✔
447
  return code;
4,010✔
448
}
449

450
static int32_t hbprocessTSMARsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) {
46,085✔
451
  int32_t code = 0;
46,085✔
452

453
  STSMAHbRsp hbRsp = {0};
46,085✔
454
  if (tDeserializeTSMAHbRsp(value, valueLen, &hbRsp)) {
46,085✔
455
    terrno = TSDB_CODE_INVALID_MSG;
×
456
    return -1;
×
457
  }
458

459
  int32_t numOfTsma = taosArrayGetSize(hbRsp.pTsmas);
46,085✔
460
  for (int32_t i = 0; i < numOfTsma; ++i) {
46,085✔
461
    STableTSMAInfo *pTsmaInfo = taosArrayGetP(hbRsp.pTsmas, i);
×
462

463
    if (!pTsmaInfo->pFuncs) {
×
464
      tscDebug("hb to remove tsma:%s.%s", pTsmaInfo->dbFName, pTsmaInfo->name);
×
465
      code = catalogRemoveTSMA(pCatalog, pTsmaInfo);
×
466
      tFreeAndClearTableTSMAInfo(pTsmaInfo);
×
467
    } else {
468
      tscDebug("hb to update tsma:%s.%s", pTsmaInfo->dbFName, pTsmaInfo->name);
×
469
      code = catalogUpdateTSMA(pCatalog, &pTsmaInfo);
×
470
      tFreeAndClearTableTSMAInfo(pTsmaInfo);
×
471
    }
472
    TSC_ERR_JRET(code);
×
473
  }
474

475
_return:
46,085✔
476
  taosArrayDestroy(hbRsp.pTsmas);
46,085✔
477
  return code;
46,085✔
478
}
479

480
static void hbProcessQueryRspKvs(int32_t kvNum, SArray *pKvs, struct SCatalog *pCatalog, SAppHbMgr *pAppHbMgr) {
6,780,663✔
481
  for (int32_t i = 0; i < kvNum; ++i) {
14,942,936✔
482
    SKv *kv = taosArrayGet(pKvs, i);
8,162,273✔
483
    if (NULL == kv) {
8,162,273✔
484
      tscError("invalid hb kv, idx:%d", i);
×
485
      continue;
×
486
    }
487
    switch (kv->key) {
8,162,273✔
488
      case HEARTBEAT_KEY_USER_AUTHINFO: {
636,733✔
489
        if (kv->valueLen <= 0 || NULL == kv->value) {
636,733✔
490
          tscError("invalid hb user auth info, len:%d, value:%p", kv->valueLen, kv->value);
×
491
          break;
×
492
        }
493
        if (TSDB_CODE_SUCCESS != hbProcessUserAuthInfoRsp(kv->value, kv->valueLen, pCatalog, pAppHbMgr)) {
636,733✔
494
          tscError("process user auth info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
495
          break;
×
496
        }
497
        break;
636,733✔
498
      }
499
      case HEARTBEAT_KEY_DBINFO: {
4,338,888✔
500
        if (kv->valueLen <= 0 || NULL == kv->value) {
4,338,888✔
501
          tscError("invalid hb db info, len:%d, value:%p", kv->valueLen, kv->value);
×
502
          break;
×
503
        }
504
        if (TSDB_CODE_SUCCESS != hbProcessDBInfoRsp(kv->value, kv->valueLen, pCatalog)) {
4,338,888✔
505
          tscError("process db info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
506
          break;
×
507
        }
508
        break;
4,338,888✔
509
      }
510
      case HEARTBEAT_KEY_STBINFO: {
3,132,547✔
511
        if (kv->valueLen <= 0 || NULL == kv->value) {
3,132,547✔
512
          tscError("invalid hb stb info, len:%d, value:%p", kv->valueLen, kv->value);
×
513
          break;
×
514
        }
515
        if (TSDB_CODE_SUCCESS != hbProcessStbInfoRsp(kv->value, kv->valueLen, pCatalog)) {
3,132,547✔
516
          tscError("process stb info response faild, len:%d, value:%p", kv->valueLen, kv->value);
×
517
          break;
×
518
        }
519
        break;
3,132,547✔
520
      }
521
#ifdef TD_ENTERPRISE
522
      case HEARTBEAT_KEY_DYN_VIEW: {
4,010✔
523
        if (kv->valueLen <= 0 || NULL == kv->value) {
4,010✔
524
          tscError("invalid dyn view info, len:%d, value:%p", kv->valueLen, kv->value);
×
525
          break;
×
526
        }
527
        if (TSDB_CODE_SUCCESS != hbProcessDynViewRsp(kv->value, kv->valueLen, pCatalog)) {
4,010✔
528
          tscError("Process dyn view response failed, len:%d, value:%p", kv->valueLen, kv->value);
×
529
          break;
×
530
        }
531
        break;
4,010✔
532
      }
533
      case HEARTBEAT_KEY_VIEWINFO: {
4,010✔
534
        if (kv->valueLen <= 0 || NULL == kv->value) {
4,010✔
535
          tscError("invalid view info, len:%d, value:%p", kv->valueLen, kv->value);
×
536
          break;
×
537
        }
538
        if (TSDB_CODE_SUCCESS != hbProcessViewInfoRsp(kv->value, kv->valueLen, pCatalog)) {
4,010✔
539
          tscError("Process view info response failed, len:%d, value:%p", kv->valueLen, kv->value);
×
540
          break;
×
541
        }
542
        break;
4,010✔
543
      }
544
#endif
545
      case HEARTBEAT_KEY_TSMA: {
46,085✔
546
        if (kv->valueLen <= 0 || !kv->value) {
46,085✔
547
          tscError("Invalid tsma info, len:%d, value:%p", kv->valueLen, kv->value);
×
548
        }
549
        if (TSDB_CODE_SUCCESS != hbprocessTSMARsp(kv->value, kv->valueLen, pCatalog)) {
46,085✔
550
          tscError("Process tsma info response failed, len:%d, value:%p", kv->valueLen, kv->value);
×
551
        }
552
        break;
46,085✔
553
      }
554
      default:
×
555
        tscError("invalid hb key type:%d", kv->key);
×
556
        break;
×
557
    }
558
  }
559
}
6,780,663✔
560

561
static int32_t hbQueryHbRspHandle(SAppHbMgr *pAppHbMgr, SClientHbRsp *pRsp) {
22,662,660✔
562
  SClientHbReq *pReq = taosHashAcquire(pAppHbMgr->activeInfo, &pRsp->connKey, sizeof(SClientHbKey));
22,662,660✔
563
  if (NULL == pReq) {
22,662,660✔
564
    tscWarn("pReq to get activeInfo, may be dropped, refId:%" PRIx64 ", type:%d", pRsp->connKey.tscRid,
9,250✔
565
            pRsp->connKey.connType);
566
    return TSDB_CODE_SUCCESS;
9,250✔
567
  }
568

569
  if (pRsp->query) {
22,653,410✔
570
    STscObj *pTscObj = (STscObj *)acquireTscObj(pRsp->connKey.tscRid);
20,632,841✔
571
    if (NULL == pTscObj) {
20,632,841✔
572
      tscDebug("tscObj rid %" PRIx64 " not exist", pRsp->connKey.tscRid);
1,031✔
573
    } else {
574
      if (pRsp->query->totalDnodes > 1) {
20,631,810✔
575
        SEpSet  originEpset = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
6,138,823✔
576
        if (!isEpsetEqual(&originEpset, &pRsp->query->epSet)) {
6,138,823✔
577
          SEpSet *pOrig = &originEpset;
61,666✔
578
          SEp    *pOrigEp = &pOrig->eps[pOrig->inUse];
61,666✔
579
          SEp    *pNewEp = &pRsp->query->epSet.eps[pRsp->query->epSet.inUse];
61,666✔
580
          tscDebug("mnode epset updated from %d/%d=>%s:%d to %d/%d=>%s:%d in hb", pOrig->inUse, pOrig->numOfEps,
61,666✔
581
                   pOrigEp->fqdn, pOrigEp->port, pRsp->query->epSet.inUse, pRsp->query->epSet.numOfEps, pNewEp->fqdn,
582
                   pNewEp->port);
583

584
          updateEpSet_s(&pTscObj->pAppInfo->mgmtEp, &pRsp->query->epSet);
61,666✔
585
        }
586
      }
587

588
      pTscObj->pAppInfo->totalDnodes = pRsp->query->totalDnodes;
20,631,810✔
589
      pTscObj->pAppInfo->onlineDnodes = pRsp->query->onlineDnodes;
20,631,810✔
590
      pTscObj->connId = pRsp->query->connId;
20,631,810✔
591
      tscTrace("connId:%u, hb rsp, dnodes %d/%d", pTscObj->connId, pTscObj->pAppInfo->onlineDnodes,
20,631,810✔
592
               pTscObj->pAppInfo->totalDnodes);
593

594
      if (pRsp->query->killRid) {
20,631,810✔
595
        tscDebug("QID:0x%" PRIx64 ", need to be killed now", pRsp->query->killRid);
×
596
        SRequestObj *pRequest = acquireRequest(pRsp->query->killRid);
×
597
        if (NULL == pRequest) {
×
598
          tscDebug("QID:0x%" PRIx64 ", not exist to kill", pRsp->query->killRid);
×
599
        } else {
600
          taos_stop_query((TAOS_RES *)pRequest);
×
601
          (void)releaseRequest(pRsp->query->killRid);
×
602
        }
603
      }
604

605
      if (pRsp->query->killConnection) {
20,631,810✔
606
        taos_close_internal(pTscObj);
×
607
      }
608

609
      if (pRsp->query->pQnodeList) {
20,631,810✔
610
        if (TSDB_CODE_SUCCESS != updateQnodeList(pTscObj->pAppInfo, pRsp->query->pQnodeList)) {
62,283✔
611
          tscWarn("update qnode list failed");
×
612
        }
613
      }
614

615
      releaseTscObj(pRsp->connKey.tscRid);
20,631,810✔
616
    }
617
  }
618

619
  int32_t kvNum = pRsp->info ? taosArrayGetSize(pRsp->info) : 0;
22,653,410✔
620

621
  tscDebug("hb got %d rsp kv", kvNum);
22,653,410✔
622

623
  if (kvNum > 0) {
22,653,410✔
624
    struct SCatalog *pCatalog = NULL;
6,780,663✔
625
    int32_t          code = catalogGetHandle(pReq->clusterId, &pCatalog);
6,780,663✔
626
    if (code != TSDB_CODE_SUCCESS) {
6,780,663✔
627
      tscWarn("catalogGetHandle failed, clusterId:0x%" PRIx64 ", error:%s", pReq->clusterId, tstrerror(code));
×
628
    } else {
629
      hbProcessQueryRspKvs(kvNum, pRsp->info, pCatalog, pAppHbMgr);
6,780,663✔
630
    }
631
  }
632

633
  taosHashRelease(pAppHbMgr->activeInfo, pReq);
22,653,410✔
634

635
  return TSDB_CODE_SUCCESS;
22,653,410✔
636
}
637

638
static int32_t hbAsyncCallBack(void *param, SDataBuf *pMsg, int32_t code) {
18,522,607✔
639
  if (0 == atomic_load_8(&clientHbMgr.inited)) {
18,522,607✔
640
    goto _return;
×
641
  }
642

643
  static int32_t    emptyRspNum = 0;
644
  int32_t           idx = *(int32_t *)param;
18,522,276✔
645
  SClientHbBatchRsp pRsp = {0};
18,522,570✔
646
  if (TSDB_CODE_SUCCESS == code) {
18,522,570✔
647
    code = tDeserializeSClientHbBatchRsp(pMsg->pData, pMsg->len, &pRsp);
18,350,885✔
648
    if (TSDB_CODE_SUCCESS != code) {
18,350,164✔
649
      tscError("deserialize hb rsp failed");
170✔
650
    }
651
    int32_t now = taosGetTimestampSec();
18,350,164✔
652
    int32_t delta = abs(now - pRsp.svrTimestamp);
18,349,194✔
653
    if (delta > tsTimestampDeltaLimit) {
18,349,194✔
654
      code = TSDB_CODE_TIME_UNSYNCED;
170✔
655
      tscError("time diff:%ds is too big", delta);
170✔
656
    }
657
  }
658

659
  int32_t rspNum = taosArrayGetSize(pRsp.rsps);
18,520,879✔
660

661
  (void)taosThreadMutexLock(&clientHbMgr.lock);
18,521,431✔
662

663
  SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, idx);
18,522,607✔
664
  if (pAppHbMgr == NULL) {
18,522,607✔
665
    (void)taosThreadMutexUnlock(&clientHbMgr.lock);
×
666
    tscError("appHbMgr not exist, idx:%d", idx);
×
667
    taosMemoryFree(pMsg->pData);
×
668
    taosMemoryFree(pMsg->pEpSet);
×
669
    tFreeClientHbBatchRsp(&pRsp);
670
    return TSDB_CODE_OUT_OF_RANGE;
×
671
  }
672

673
  SAppInstInfo *pInst = pAppHbMgr->pAppInstInfo;
18,522,607✔
674

675
  if (code != 0) {
18,522,607✔
676
    pInst->onlineDnodes = pInst->totalDnodes ? 0 : -1;
171,892✔
677
    tscDebug("hb rsp error %s, update server status %d/%d", tstrerror(code), pInst->onlineDnodes, pInst->totalDnodes);
171,892✔
678
    (void)taosThreadMutexUnlock(&clientHbMgr.lock);
171,892✔
679
    taosMemoryFree(pMsg->pData);
171,892✔
680
    taosMemoryFree(pMsg->pEpSet);
171,892✔
681
    tFreeClientHbBatchRsp(&pRsp);
682
    return code;
171,892✔
683
  }
684

685
  pInst->serverCfg.monitorParas = pRsp.monitorParas;
18,350,715✔
686
  pInst->serverCfg.enableAuditDelete = pRsp.enableAuditDelete;
18,350,715✔
687
  pInst->serverCfg.enableAuditSelect = pRsp.enableAuditSelect;
18,350,715✔
688
  pInst->serverCfg.enableAuditInsert = pRsp.enableAuditInsert;
18,350,715✔
689
  pInst->serverCfg.auditLevel = pRsp.auditLevel;
18,350,715✔
690
  pInst->serverCfg.enableStrongPass = pRsp.enableStrongPass;
18,350,715✔
691
  tsEnableStrongPassword = pInst->serverCfg.enableStrongPass;
18,350,715✔
692
  tscDebug("monitor paras from hb, clusterId:0x%" PRIx64 ", threshold:%d scope:%d", pInst->clusterId,
18,350,715✔
693
           pRsp.monitorParas.tsSlowLogThreshold, pRsp.monitorParas.tsSlowLogScope);
694

695
  if (rspNum) {
18,350,715✔
696
    tscDebug("hb got %d rsp, %d empty rsp received before", rspNum,
17,792,529✔
697
             atomic_val_compare_exchange_32(&emptyRspNum, emptyRspNum, 0));
698
  } else {
699
    (void)atomic_add_fetch_32(&emptyRspNum, 1);
558,186✔
700
  }
701

702
  for (int32_t i = 0; i < rspNum; ++i) {
41,013,375✔
703
    SClientHbRsp *rsp = taosArrayGet(pRsp.rsps, i);
22,662,660✔
704
    code = (*clientHbMgr.rspHandle[rsp->connKey.connType])(pAppHbMgr, rsp);
22,662,660✔
705
    if (code) {
22,662,660✔
706
      break;
×
707
    }
708
  }
709

710
  (void)taosThreadMutexUnlock(&clientHbMgr.lock);
18,350,715✔
711

712
  tFreeClientHbBatchRsp(&pRsp);
713

714
_return:
18,350,715✔
715
  taosMemoryFree(pMsg->pData);
18,350,715✔
716
  taosMemoryFree(pMsg->pEpSet);
18,350,715✔
717
  return code;
18,350,715✔
718
}
719

720
int32_t hbBuildQueryDesc(SQueryHbReqBasic *hbBasic, STscObj *pObj) {
21,032,362✔
721
  int64_t    now = taosGetTimestampUs();
21,032,362✔
722
  SQueryDesc desc = {0};
21,032,362✔
723
  int32_t    code = 0;
21,032,362✔
724

725
  void *pIter = taosHashIterate(pObj->pRequests, NULL);
21,032,362✔
726
  while (pIter != NULL) {
42,545,340✔
727
    int64_t     *rid = pIter;
21,512,978✔
728
    SRequestObj *pRequest = acquireRequest(*rid);
21,512,978✔
729
    if (NULL == pRequest) {
21,512,978✔
730
      pIter = taosHashIterate(pObj->pRequests, pIter);
13,118✔
731
      continue;
13,118✔
732
    }
733

734
    if (pRequest->killed || 0 == pRequest->body.queryJob) {
21,499,860✔
735
      (void)releaseRequest(*rid);
9,750,864✔
736
      pIter = taosHashIterate(pObj->pRequests, pIter);
9,750,864✔
737
      continue;
9,750,864✔
738
    }
739

740
    tstrncpy(desc.sql, pRequest->sqlstr, sizeof(desc.sql));
11,748,996✔
741
    desc.stime = pRequest->metric.start / 1000;
11,748,996✔
742
    desc.queryId = pRequest->requestId;
11,748,996✔
743
    desc.useconds = now - pRequest->metric.start;
11,748,996✔
744
    desc.reqRid = pRequest->self;
11,748,996✔
745
    desc.stableQuery = pRequest->stableQuery;
11,748,996✔
746
    desc.isSubQuery = pRequest->isSubReq;
11,748,996✔
747
    code = taosGetFqdn(desc.fqdn);
11,748,996✔
748
    if (TSDB_CODE_SUCCESS != code) {
11,748,996✔
749
      (void)releaseRequest(*rid);
×
750
      tscError("get fqdn failed");
×
751
      return TSDB_CODE_FAILED;
×
752
    }
753
    desc.subPlanNum = pRequest->body.subplanNum;
11,748,996✔
754

755
    if (desc.subPlanNum) {
11,748,996✔
756
      desc.subDesc = taosArrayInit(desc.subPlanNum, sizeof(SQuerySubDesc));
11,748,996✔
757
      if (NULL == desc.subDesc) {
11,748,996✔
758
        (void)releaseRequest(*rid);
×
759
        return terrno;
×
760
      }
761

762
      code = schedulerGetTasksStatus(pRequest->body.queryJob, desc.subDesc);
11,748,996✔
763
      if (code) {
11,748,996✔
764
        taosArrayDestroy(desc.subDesc);
2,107,139✔
765
        desc.subDesc = NULL;
2,107,139✔
766
      }
767
      desc.subPlanNum = taosArrayGetSize(desc.subDesc);
11,748,996✔
768
    } else {
769
      desc.subDesc = NULL;
×
770
    }
771

772
    (void)releaseRequest(*rid);
11,748,996✔
773
    if (NULL == taosArrayPush(hbBasic->queryDesc, &desc)) {
23,497,992✔
774
      taosArrayDestroy(desc.subDesc);
×
775
      return terrno;
×
776
    }
777

778
    pIter = taosHashIterate(pObj->pRequests, pIter);
11,748,996✔
779
  }
780

781
  return code;
21,032,362✔
782
}
783

784
int32_t hbGetQueryBasicInfo(SClientHbKey *connKey, SClientHbReq *req) {
22,843,371✔
785
  STscObj *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
22,843,371✔
786
  if (NULL == pTscObj) {
22,843,371✔
787
    tscWarn("tscObj rid 0x%" PRIx64 " not exist", connKey->tscRid);
×
788
    return terrno;
×
789
  }
790

791
  SQueryHbReqBasic *hbBasic = (SQueryHbReqBasic *)taosMemoryCalloc(1, sizeof(SQueryHbReqBasic));
22,843,371✔
792
  if (NULL == hbBasic) {
22,843,371✔
793
    tscError("calloc %d failed", (int32_t)sizeof(SQueryHbReqBasic));
×
794
    releaseTscObj(connKey->tscRid);
×
795
    return terrno;
×
796
  }
797

798
  hbBasic->connId = pTscObj->connId;
22,843,371✔
799

800
  int32_t numOfQueries = pTscObj->pRequests ? taosHashGetSize(pTscObj->pRequests) : 0;
22,843,371✔
801
  if (numOfQueries <= 0) {
22,843,371✔
802
    req->query = hbBasic;
1,811,009✔
803
    releaseTscObj(connKey->tscRid);
1,811,009✔
804
    tscDebug("no queries on connection");
1,811,009✔
805
    return TSDB_CODE_SUCCESS;
1,811,009✔
806
  }
807

808
  hbBasic->queryDesc = taosArrayInit(numOfQueries, sizeof(SQueryDesc));
21,032,362✔
809
  if (NULL == hbBasic->queryDesc) {
21,032,362✔
810
    tscWarn("taosArrayInit %d queryDesc failed", numOfQueries);
×
811
    releaseTscObj(connKey->tscRid);
×
812
    taosMemoryFree(hbBasic);
×
813
    return terrno;
×
814
  }
815

816
  int32_t code = hbBuildQueryDesc(hbBasic, pTscObj);
21,032,362✔
817
  if (code) {
21,032,362✔
818
    releaseTscObj(connKey->tscRid);
2,054,305✔
819
    if (hbBasic->queryDesc) {
2,054,305✔
820
      taosArrayDestroyEx(hbBasic->queryDesc, tFreeClientHbQueryDesc);
2,054,305✔
821
    }
822
    taosMemoryFree(hbBasic);
2,054,305✔
823
    return code;
2,054,305✔
824
  }
825

826
  req->query = hbBasic;
18,978,057✔
827
  releaseTscObj(connKey->tscRid);
18,978,057✔
828

829
  return TSDB_CODE_SUCCESS;
18,978,057✔
830
}
831

832
static int32_t hbGetUserAuthInfo(SClientHbKey *connKey, SHbParam *param, SClientHbReq *req) {
604,843✔
833
  STscObj *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
604,843✔
834
  if (!pTscObj) {
604,843✔
835
    tscWarn("tscObj rid 0x%" PRIx64 " not exist", connKey->tscRid);
×
836
    return terrno;
×
837
  }
838

839
  int32_t code = 0;
604,843✔
840

841
  SKv  kv = {.key = HEARTBEAT_KEY_USER_AUTHINFO};
604,843✔
842
  SKv *pKv = NULL;
604,843✔
843
  if ((pKv = taosHashGet(req->info, &kv.key, sizeof(kv.key)))) {
604,843✔
844
    int32_t           userNum = pKv->valueLen / sizeof(SUserAuthVersion);
237,507✔
845
    SUserAuthVersion *userAuths = (SUserAuthVersion *)pKv->value;
237,507✔
846
    for (int32_t i = 0; i < userNum; ++i) {
242,275✔
847
      SUserAuthVersion *pUserAuth = userAuths + i;
241,599✔
848
      // both key and user exist, update version
849
      if (strncmp(pUserAuth->user, pTscObj->user, TSDB_USER_LEN) == 0) {
241,599✔
850
        pUserAuth->version = htonl(-1);  // force get userAuthInfo
236,831✔
851
        goto _return;
236,831✔
852
      }
853
    }
854
    // key exists, user not exist, append user
855
    SUserAuthVersion *qUserAuth =
1,352✔
856
        (SUserAuthVersion *)taosMemoryRealloc(pKv->value, (userNum + 1) * sizeof(SUserAuthVersion));
676✔
857
    if (qUserAuth) {
676✔
858
      tstrncpy((qUserAuth + userNum)->user, pTscObj->user, TSDB_USER_LEN);
676✔
859
      (qUserAuth + userNum)->version = htonl(-1);  // force get userAuthInfo
676✔
860
      pKv->value = qUserAuth;
676✔
861
      pKv->valueLen += sizeof(SUserAuthVersion);
676✔
862
    } else {
863
      code = terrno;
×
864
    }
865
    goto _return;
676✔
866
  }
867

868
  // key/user not exist, add user
869
  SUserAuthVersion *user = taosMemoryMalloc(sizeof(SUserAuthVersion));
367,336✔
870
  if (!user) {
367,336✔
871
    code = terrno;
×
872
    goto _return;
×
873
  }
874
  tstrncpy(user->user, pTscObj->user, TSDB_USER_LEN);
367,336✔
875
  user->version = htonl(-1);  // force get userAuthInfo
367,336✔
876
  kv.valueLen = sizeof(SUserAuthVersion);
367,336✔
877
  kv.value = user;
367,336✔
878

879
  tscDebug("hb got user auth info, valueLen:%d, user:%s, authVer:%d, tscRid:%" PRIi64, kv.valueLen, user->user,
367,336✔
880
           pTscObj->authVer, connKey->tscRid);
881

882
  if (!req->info) {
367,336✔
883
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
367,336✔
884
    if (NULL == req->info) {
367,336✔
885
      code = terrno;
×
886
      goto _return;
×
887
    }
888
  }
889

890
  if (taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)) != 0) {
367,336✔
891
    taosMemoryFree(user);
×
892
    code = terrno ? terrno : TSDB_CODE_APP_ERROR;
×
893
    goto _return;
×
894
  }
895

896
_return:
596,967✔
897
  releaseTscObj(connKey->tscRid);
604,843✔
898
  if (code) {
604,843✔
899
    tscError("hb got user auth info failed since %s", tstrerror(code));
×
900
  }
901

902
  return code;
604,843✔
903
}
904

905
int32_t hbGetExpiredUserInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
15,132,265✔
906
  SUserAuthVersion *users = NULL;
15,132,265✔
907
  uint32_t          userNum = 0;
15,132,265✔
908
  int32_t           code = 0;
15,132,265✔
909

910
  code = catalogGetExpiredUsers(pCatalog, &users, &userNum);
15,132,265✔
911
  if (TSDB_CODE_SUCCESS != code) {
15,132,265✔
912
    return code;
×
913
  }
914

915
  if (userNum <= 0) {
15,132,265✔
916
    taosMemoryFree(users);
582,133✔
917
    return TSDB_CODE_SUCCESS;
582,133✔
918
  }
919

920
  for (int32_t i = 0; i < userNum; ++i) {
29,438,281✔
921
    SUserAuthVersion *user = &users[i];
14,888,149✔
922
    user->version = htonl(user->version);
14,888,149✔
923
  }
924

925
  SKv kv = {
14,550,132✔
926
      .key = HEARTBEAT_KEY_USER_AUTHINFO,
927
      .valueLen = sizeof(SUserAuthVersion) * userNum,
14,550,132✔
928
      .value = users,
929
  };
930

931
  tscDebug("hb got %d expired users, valueLen:%d", userNum, kv.valueLen);
14,550,132✔
932

933
  if (NULL == req->info) {
14,550,132✔
934
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
14,550,132✔
935
    if (NULL == req->info) {
14,550,132✔
936
      taosMemoryFree(users);
×
937
      return terrno;
×
938
    }
939
  }
940

941
  code = taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
14,550,132✔
942
  if (TSDB_CODE_SUCCESS != code) {
14,550,132✔
943
    taosMemoryFree(users);
×
944
    return code;
×
945
  }
946

947
  return TSDB_CODE_SUCCESS;
14,550,132✔
948
}
949

950
int32_t hbGetExpiredDBInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
16,018,168✔
951
  SDbCacheInfo *dbs = NULL;
16,018,168✔
952
  uint32_t      dbNum = 0;
16,018,168✔
953
  int32_t       code = 0;
16,018,168✔
954

955
  code = catalogGetExpiredDBs(pCatalog, &dbs, &dbNum);
16,018,168✔
956
  if (TSDB_CODE_SUCCESS != code) {
16,018,168✔
957
    return code;
×
958
  }
959

960
  if (dbNum <= 0) {
16,018,168✔
961
    taosMemoryFree(dbs);
11,662,056✔
962
    return TSDB_CODE_SUCCESS;
11,662,056✔
963
  }
964

965
  for (int32_t i = 0; i < dbNum; ++i) {
9,607,471✔
966
    SDbCacheInfo *db = &dbs[i];
5,251,359✔
967
    tscDebug("the %dth expired db:%s, dbId:%" PRId64
5,251,359✔
968
             ", vgVersion:%d, cfgVersion:%d, numOfTable:%d, startTs:%" PRId64,
969
             i, db->dbFName, db->dbId, db->vgVersion, db->cfgVersion, db->numOfTable, db->stateTs);
970

971
    db->dbId = htobe64(db->dbId);
5,251,359✔
972
    db->vgVersion = htonl(db->vgVersion);
5,251,359✔
973
    db->cfgVersion = htonl(db->cfgVersion);
5,251,359✔
974
    db->numOfTable = htonl(db->numOfTable);
5,251,359✔
975
    db->stateTs = htobe64(db->stateTs);
5,251,359✔
976
    db->tsmaVersion = htonl(db->tsmaVersion);
5,251,359✔
977
  }
978

979
  SKv kv = {
4,356,112✔
980
      .key = HEARTBEAT_KEY_DBINFO,
981
      .valueLen = sizeof(SDbCacheInfo) * dbNum,
4,356,112✔
982
      .value = dbs,
983
  };
984

985
  tscDebug("hb got %d expired db, valueLen:%d", dbNum, kv.valueLen);
4,356,112✔
986

987
  if (NULL == req->info) {
4,356,112✔
988
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
37,533✔
989
    if (NULL == req->info) {
37,533✔
990
      taosMemoryFree(dbs);
×
991
      return terrno;
×
992
    }
993
  }
994

995
  code = taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
4,356,112✔
996
  if (TSDB_CODE_SUCCESS != code) {
4,356,112✔
997
    taosMemoryFree(dbs);
×
998
    return code;
×
999
  }
1000

1001
  return TSDB_CODE_SUCCESS;
4,356,112✔
1002
}
1003

1004
int32_t hbGetExpiredStbInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
16,018,168✔
1005
  SSTableVersion *stbs = NULL;
16,018,168✔
1006
  uint32_t        stbNum = 0;
16,018,168✔
1007
  int32_t         code = 0;
16,018,168✔
1008

1009
  code = catalogGetExpiredSTables(pCatalog, &stbs, &stbNum);
16,018,168✔
1010
  if (TSDB_CODE_SUCCESS != code) {
16,018,168✔
1011
    return code;
×
1012
  }
1013

1014
  if (stbNum <= 0) {
16,018,168✔
1015
    taosMemoryFree(stbs);
12,873,865✔
1016
    return TSDB_CODE_SUCCESS;
12,873,865✔
1017
  }
1018

1019
  for (int32_t i = 0; i < stbNum; ++i) {
8,792,938✔
1020
    SSTableVersion *stb = &stbs[i];
5,648,635✔
1021
    stb->suid = htobe64(stb->suid);
5,648,635✔
1022
    stb->sversion = htonl(stb->sversion);
5,648,635✔
1023
    stb->tversion = htonl(stb->tversion);
5,648,635✔
1024
    stb->smaVer = htonl(stb->smaVer);
5,648,635✔
1025
  }
1026

1027
  SKv kv = {
3,144,303✔
1028
      .key = HEARTBEAT_KEY_STBINFO,
1029
      .valueLen = sizeof(SSTableVersion) * stbNum,
3,144,303✔
1030
      .value = stbs,
1031
  };
1032

1033
  tscDebug("hb got %d expired stb, valueLen:%d", stbNum, kv.valueLen);
3,144,303✔
1034

1035
  if (NULL == req->info) {
3,144,303✔
1036
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
1,228✔
1037
    if (NULL == req->info) {
1,228✔
1038
      taosMemoryFree(stbs);
×
1039
      return terrno;
×
1040
    }
1041
  }
1042

1043
  code = taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
3,144,303✔
1044
  if (TSDB_CODE_SUCCESS != code) {
3,144,303✔
1045
    taosMemoryFree(stbs);
×
1046
    return code;
×
1047
  }
1048

1049
  return TSDB_CODE_SUCCESS;
3,144,303✔
1050
}
1051

1052
int32_t hbGetExpiredViewInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
16,018,168✔
1053
  SViewVersion    *views = NULL;
16,018,168✔
1054
  uint32_t         viewNum = 0;
16,018,168✔
1055
  int32_t          code = 0;
16,018,168✔
1056
  SDynViewVersion *pDynViewVer = NULL;
16,018,168✔
1057

1058
  TSC_ERR_JRET(catalogGetExpiredViews(pCatalog, &views, &viewNum, &pDynViewVer));
16,018,168✔
1059

1060
  if (viewNum <= 0) {
16,018,168✔
1061
    taosMemoryFree(views);
16,012,148✔
1062
    taosMemoryFree(pDynViewVer);
16,012,148✔
1063
    return TSDB_CODE_SUCCESS;
16,012,148✔
1064
  }
1065

1066
  for (int32_t i = 0; i < viewNum; ++i) {
12,485✔
1067
    SViewVersion *view = &views[i];
6,465✔
1068
    view->dbId = htobe64(view->dbId);
6,465✔
1069
    view->viewId = htobe64(view->viewId);
6,465✔
1070
    view->version = htonl(view->version);
6,465✔
1071
  }
1072

1073
  tscDebug("hb got %u expired view, valueLen:%lu", viewNum, sizeof(SViewVersion) * viewNum);
6,020✔
1074

1075
  if (NULL == req->info) {
6,020✔
1076
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
×
1077
    if (NULL == req->info) {
×
1078
      TSC_ERR_JRET(terrno);
×
1079
    }
1080
  }
1081

1082
  SKv kv = {
6,020✔
1083
      .key = HEARTBEAT_KEY_DYN_VIEW,
1084
      .valueLen = sizeof(SDynViewVersion),
1085
      .value = pDynViewVer,
1086
  };
1087

1088
  TSC_ERR_JRET(taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)));
6,020✔
1089

1090
  kv.key = HEARTBEAT_KEY_VIEWINFO;
6,020✔
1091
  kv.valueLen = sizeof(SViewVersion) * viewNum;
6,020✔
1092
  kv.value = views;
6,020✔
1093

1094
  TSC_ERR_JRET(taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)));
6,020✔
1095
  return TSDB_CODE_SUCCESS;
6,020✔
1096
_return:
×
1097
  taosMemoryFree(views);
×
1098
  taosMemoryFree(pDynViewVer);
×
1099
  return code;
×
1100
}
1101

1102
int32_t hbGetExpiredTSMAInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *pReq) {
16,018,168✔
1103
  int32_t       code = 0;
16,018,168✔
1104
  uint32_t      tsmaNum = 0;
16,018,168✔
1105
  STSMAVersion *tsmas = NULL;
16,018,168✔
1106

1107
  code = catalogGetExpiredTsmas(pCatalog, &tsmas, &tsmaNum);
16,018,168✔
1108
  if (code) {
16,018,168✔
1109
    taosMemoryFree(tsmas);
×
1110
    return code;
×
1111
  }
1112

1113
  if (tsmaNum <= 0) {
16,018,168✔
1114
    taosMemoryFree(tsmas);
15,972,083✔
1115
    return TSDB_CODE_SUCCESS;
15,972,083✔
1116
  }
1117

1118
  for (int32_t i = 0; i < tsmaNum; ++i) {
92,170✔
1119
    STSMAVersion *tsma = &tsmas[i];
46,085✔
1120
    tsma->dbId = htobe64(tsma->dbId);
46,085✔
1121
    tsma->tsmaId = htobe64(tsma->tsmaId);
46,085✔
1122
    tsma->version = htonl(tsma->version);
46,085✔
1123
  }
1124

1125
  tscDebug("hb got %d expred tsmas, valueLen:%lu", tsmaNum, sizeof(STSMAVersion) * tsmaNum);
46,085✔
1126

1127
  if (!pReq->info) {
46,085✔
1128
    pReq->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
×
1129
    if (!pReq->info) {
×
1130
      taosMemoryFree(tsmas);
×
1131
      return terrno;
×
1132
    }
1133
  }
1134

1135
  SKv kv = {.key = HEARTBEAT_KEY_TSMA, .valueLen = sizeof(STSMAVersion) * tsmaNum, .value = tsmas};
46,085✔
1136
  code = taosHashPut(pReq->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv));
46,085✔
1137
  if (TSDB_CODE_SUCCESS != code) {
46,085✔
1138
    taosMemoryFree(tsmas);
×
1139
    return code;
×
1140
  }
1141
  return TSDB_CODE_SUCCESS;
46,085✔
1142
}
1143

1144
int32_t hbGetAppInfo(int64_t clusterId, SClientHbReq *req) {
20,789,066✔
1145
  SAppHbReq *pApp = taosHashGet(clientHbMgr.appSummary, &clusterId, sizeof(clusterId));
20,789,066✔
1146
  if (NULL != pApp) {
20,789,066✔
1147
    (void)memcpy(&req->app, pApp, sizeof(*pApp));
20,789,066✔
1148
  } else {
1149
    (void)memset(&req->app.summary, 0, sizeof(req->app.summary));
×
1150
    req->app.pid = taosGetPId();
×
1151
    req->app.appId = clientHbMgr.appId;
×
1152
    TSC_ERR_RET(taosGetAppName(req->app.name, NULL));
×
1153
  }
1154

1155
  return TSDB_CODE_SUCCESS;
20,789,066✔
1156
}
1157

1158
int32_t hbQueryHbReqHandle(SClientHbKey *connKey, void *param, SClientHbReq *req) {
22,843,371✔
1159
  int32_t   code = 0;
22,843,371✔
1160
  SHbParam *hbParam = (SHbParam *)param;
22,843,371✔
1161
  SCatalog *pCatalog = NULL;
22,843,371✔
1162

1163
  code = hbGetQueryBasicInfo(connKey, req);
22,843,371✔
1164
  if (code != TSDB_CODE_SUCCESS) {
22,843,371✔
1165
    tscWarn("hbGetQueryBasicInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
2,054,305✔
1166
    return code;
2,054,305✔
1167
  }
1168

1169
  if (hbParam->reqCnt == 0) {
20,789,066✔
1170
    code = catalogGetHandle(hbParam->clusterId, &pCatalog);
16,018,168✔
1171
    if (code != TSDB_CODE_SUCCESS) {
16,018,168✔
1172
      tscWarn("catalogGetHandle failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1173
      return code;
×
1174
    }
1175

1176
    code = hbGetAppInfo(hbParam->clusterId, req);
16,018,168✔
1177
    if (TSDB_CODE_SUCCESS != code) {
16,018,168✔
1178
      tscWarn("getAppInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1179
      return code;
×
1180
    }
1181

1182
    if (!taosHashGet(clientHbMgr.appHbHash, &hbParam->clusterId, sizeof(hbParam->clusterId))) {
16,018,168✔
1183
      code = hbGetExpiredUserInfo(connKey, pCatalog, req);
15,132,265✔
1184
      if (TSDB_CODE_SUCCESS != code) {
15,132,265✔
1185
        tscWarn("hbGetExpiredUserInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1186
        return code;
×
1187
      }
1188
      if (clientHbMgr.appHbHash) {
15,132,265✔
1189
        code = taosHashPut(clientHbMgr.appHbHash, &hbParam->clusterId, sizeof(uint64_t), NULL, 0);
1,514,058✔
1190
        if (TSDB_CODE_SUCCESS != code) {
1,514,058✔
1191
          tscWarn("hbQueryHbReqHandle put clusterId failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId,
×
1192
                  tstrerror(code));
1193
          return code;
×
1194
        }
1195
      }
1196
    }
1197

1198
    // invoke after hbGetExpiredUserInfo
1199
    if (2 != atomic_load_8(&hbParam->pAppHbMgr->connHbFlag)) {
16,018,168✔
1200
      code = hbGetUserAuthInfo(connKey, hbParam, req);
604,843✔
1201
      if (TSDB_CODE_SUCCESS != code) {
604,843✔
1202
        tscWarn("hbGetUserAuthInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1203
        return code;
×
1204
      }
1205
      atomic_store_8(&hbParam->pAppHbMgr->connHbFlag, 1);
604,843✔
1206
    }
1207

1208
    code = hbGetExpiredDBInfo(connKey, pCatalog, req);
16,018,168✔
1209
    if (TSDB_CODE_SUCCESS != code) {
16,018,168✔
1210
      tscWarn("hbGetExpiredDBInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1211
      return code;
×
1212
    }
1213

1214
    code = hbGetExpiredStbInfo(connKey, pCatalog, req);
16,018,168✔
1215
    if (TSDB_CODE_SUCCESS != code) {
16,018,168✔
1216
      tscWarn("hbGetExpiredStbInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1217
      return code;
×
1218
    }
1219

1220
#ifdef TD_ENTERPRISE
1221
    code = hbGetExpiredViewInfo(connKey, pCatalog, req);
16,018,168✔
1222
    if (TSDB_CODE_SUCCESS != code) {
16,018,168✔
1223
      tscWarn("hbGetExpiredViewInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1224
      return code;
×
1225
    }
1226
#endif
1227
    code = hbGetExpiredTSMAInfo(connKey, pCatalog, req);
16,018,168✔
1228
    if (TSDB_CODE_SUCCESS != code) {
16,018,168✔
1229
      tscWarn("hbGetExpiredTSMAInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1230
      return code;
×
1231
    }
1232
  } else {
1233
    code = hbGetAppInfo(hbParam->clusterId, req);
4,770,898✔
1234
    if (TSDB_CODE_SUCCESS != code) {
4,770,898✔
1235
      tscWarn("hbGetAppInfo failed, clusterId:0x%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1236
      return code;
×
1237
    }
1238
  }
1239

1240
  ++hbParam->reqCnt;  // success to get catalog info
20,789,066✔
1241

1242
  return TSDB_CODE_SUCCESS;
20,789,066✔
1243
}
1244

1245
static FORCE_INLINE void hbMgrInitHandle() {
1246
  // init all handle
1247
  clientHbMgr.reqHandle[CONN_TYPE__QUERY] = hbQueryHbReqHandle;
1,189,632✔
1248
  clientHbMgr.reqHandle[CONN_TYPE__TMQ] = hbMqHbReqHandle;
1,189,632✔
1249

1250
  clientHbMgr.rspHandle[CONN_TYPE__QUERY] = hbQueryHbRspHandle;
1,189,632✔
1251
  clientHbMgr.rspHandle[CONN_TYPE__TMQ] = hbMqHbRspHandle;
1,189,632✔
1252
}
1,189,632✔
1253

1254
int32_t hbGatherAllInfo(SAppHbMgr *pAppHbMgr, SClientHbBatchReq **pBatchReq) {
18,533,282✔
1255
  *pBatchReq = taosMemoryCalloc(1, sizeof(SClientHbBatchReq));
18,533,282✔
1256
  if (pBatchReq == NULL) {
18,533,282✔
1257
    return terrno;
×
1258
  }
1259
  int32_t connKeyCnt = atomic_load_32(&pAppHbMgr->connKeyCnt);
18,533,282✔
1260
  (*pBatchReq)->reqs = taosArrayInit(connKeyCnt, sizeof(SClientHbReq));
18,533,282✔
1261
  if (!(*pBatchReq)->reqs) {
18,533,282✔
1262
    tFreeClientHbBatchReq(*pBatchReq);
×
1263
    return terrno;
×
1264
  }
1265

1266
  int64_t  maxIpWhiteVer = 0;
18,533,282✔
1267
  void    *pIter = NULL;
18,533,282✔
1268
  SHbParam param = {0};
18,533,282✔
1269
  while ((pIter = taosHashIterate(pAppHbMgr->activeInfo, pIter))) {
42,828,429✔
1270
    SClientHbReq *pOneReq = pIter;
24,295,147✔
1271
    SClientHbKey *connKey = &pOneReq->connKey;
24,295,147✔
1272
    STscObj      *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
24,295,147✔
1273

1274
    if (!pTscObj || atomic_load_8(&pTscObj->dropped) == 1) {
24,295,147✔
1275
      if (pTscObj) releaseTscObj(connKey->tscRid);
1,451,776✔
1276
      continue;
1,451,776✔
1277
    }
1278

1279
    tstrncpy(pOneReq->userApp, pTscObj->optionInfo.userApp, sizeof(pOneReq->userApp));
22,843,371✔
1280
    tstrncpy(pOneReq->cInfo, pTscObj->optionInfo.cInfo, sizeof(pOneReq->cInfo));
22,843,371✔
1281
    pOneReq->userIp = pTscObj->optionInfo.userIp;
22,843,371✔
1282
    pOneReq->userDualIp = pTscObj->optionInfo.userDualIp;
22,843,371✔
1283
    tstrncpy(pOneReq->sVer, td_version, TSDB_VERSION_LEN);
22,843,371✔
1284

1285
    pOneReq = taosArrayPush((*pBatchReq)->reqs, pOneReq);
22,843,371✔
1286
    if (NULL == pOneReq) {
22,843,371✔
1287
      releaseTscObj(connKey->tscRid);
×
1288
      continue;
×
1289
    }
1290

1291
    switch (connKey->connType) {
22,843,371✔
1292
      case CONN_TYPE__QUERY: {
22,843,371✔
1293
        if (param.clusterId == 0) {
22,843,371✔
1294
          // init
1295
          param.clusterId = pOneReq->clusterId;
17,973,240✔
1296
          param.pAppHbMgr = pAppHbMgr;
17,973,240✔
1297
          param.connHbFlag = atomic_load_8(&pAppHbMgr->connHbFlag);
17,973,240✔
1298
        }
1299
        break;
22,843,371✔
1300
      }
1301
      default:
×
1302
        break;
×
1303
    }
1304
    if (clientHbMgr.reqHandle[connKey->connType]) {
22,843,371✔
1305
      int32_t code = (*clientHbMgr.reqHandle[connKey->connType])(connKey, &param, pOneReq);
22,843,371✔
1306
      if (code) {
22,843,371✔
1307
        tscWarn("hbGatherAllInfo failed since %s, tscRid:%" PRIi64 ", connType:%" PRIi8, tstrerror(code),
2,054,305✔
1308
                connKey->tscRid, connKey->connType);
1309
      }
1310
    }
1311

1312
    int64_t ver = atomic_load_64(&pTscObj->whiteListInfo.ver);
22,843,371✔
1313
    maxIpWhiteVer = TMAX(maxIpWhiteVer, ver);
22,843,371✔
1314
    releaseTscObj(connKey->tscRid);
22,843,371✔
1315
  }
1316
  (*pBatchReq)->ipWhiteListVer = maxIpWhiteVer;
18,533,282✔
1317

1318
  return TSDB_CODE_SUCCESS;
18,533,282✔
1319
}
1320

1321
void hbThreadFuncUnexpectedStopped(void) { atomic_store_8(&clientHbMgr.threadStop, 2); }
×
1322

1323
void hbMergeSummary(SAppClusterSummary *dst, SAppClusterSummary *src) {
2,246,597✔
1324
  dst->numOfInsertsReq += src->numOfInsertsReq;
2,246,597✔
1325
  dst->numOfInsertRows += src->numOfInsertRows;
2,246,597✔
1326
  dst->insertElapsedTime += src->insertElapsedTime;
2,246,597✔
1327
  dst->insertBytes += src->insertBytes;
2,246,597✔
1328
  dst->fetchBytes += src->fetchBytes;
2,246,597✔
1329
  dst->queryElapsedTime += src->queryElapsedTime;
2,246,597✔
1330
  dst->numOfSlowQueries += src->numOfSlowQueries;
2,246,597✔
1331
  dst->totalRequests += src->totalRequests;
2,246,597✔
1332
  dst->currentRequests += src->currentRequests;
2,246,597✔
1333
}
2,246,597✔
1334

1335
int32_t hbGatherAppInfo(void) {
18,447,085✔
1336
  SAppHbReq req = {0};
18,447,085✔
1337
  int32_t   code = TSDB_CODE_SUCCESS;
18,447,085✔
1338
  int       sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
18,447,085✔
1339
  if (sz > 0) {
18,447,085✔
1340
    req.pid = taosGetPId();
18,447,085✔
1341
    req.appId = clientHbMgr.appId;
18,447,085✔
1342
    TSC_ERR_RET(taosGetAppName(req.name, NULL));
18,447,085✔
1343
  }
1344

1345
  taosHashClear(clientHbMgr.appSummary);
18,447,085✔
1346

1347
  for (int32_t i = 0; i < sz; ++i) {
39,162,093✔
1348
    SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
20,715,008✔
1349
    if (pAppHbMgr == NULL) continue;
20,715,008✔
1350

1351
    int64_t    clusterId = pAppHbMgr->pAppInstInfo->clusterId;
20,715,008✔
1352
    SAppHbReq *pApp = taosHashGet(clientHbMgr.appSummary, &clusterId, sizeof(clusterId));
20,715,008✔
1353
    if (NULL == pApp) {
20,715,008✔
1354
      (void)memcpy(&req.summary, &pAppHbMgr->pAppInstInfo->summary, sizeof(req.summary));
18,468,411✔
1355
      req.startTime = pAppHbMgr->startTime;
18,468,411✔
1356
      TSC_ERR_RET(taosHashPut(clientHbMgr.appSummary, &clusterId, sizeof(clusterId), &req, sizeof(req)));
18,468,411✔
1357
    } else {
1358
      if (pAppHbMgr->startTime < pApp->startTime) {
2,246,597✔
1359
        pApp->startTime = pAppHbMgr->startTime;
×
1360
      }
1361

1362
      hbMergeSummary(&pApp->summary, &pAppHbMgr->pAppInstInfo->summary);
2,246,597✔
1363
    }
1364
  }
1365

1366
  return TSDB_CODE_SUCCESS;
18,447,085✔
1367
}
1368

1369
static void *hbThreadFunc(void *param) {
1,189,632✔
1370
  setThreadName("hb");
1,189,632✔
1371
#ifdef WINDOWS
1372
  if (taosCheckCurrentInDll()) {
1373
    atexit(hbThreadFuncUnexpectedStopped);
1374
  }
1375
#endif
1376
  while (1) {
17,752,531✔
1377
    if (1 == clientHbMgr.threadStop) {
18,942,163✔
1378
      break;
490,363✔
1379
    }
1380

1381
    if (TSDB_CODE_SUCCESS != taosThreadMutexLock(&clientHbMgr.lock)) {
18,451,800✔
1382
      tscError("taosThreadMutexLock failed");
×
1383
      return NULL;
×
1384
    }
1385

1386
    int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
18,451,800✔
1387
    if (sz > 0) {
18,451,800✔
1388
      if (TSDB_CODE_SUCCESS != hbGatherAppInfo()) {
18,447,085✔
1389
        tscError("hbGatherAppInfo failed");
×
1390
        return NULL;
×
1391
      }
1392
      if (sz > 1 && !clientHbMgr.appHbHash) {
18,447,085✔
1393
        clientHbMgr.appHbHash = taosHashInit(0, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), true, HASH_NO_LOCK);
35,685✔
1394
        if (NULL == clientHbMgr.appHbHash) {
35,685✔
1395
          tscError("taosHashInit failed");
×
1396
          return NULL;
×
1397
        }
1398
      }
1399
      taosHashClear(clientHbMgr.appHbHash);
18,447,085✔
1400
    }
1401

1402
    for (int i = 0; i < sz; i++) {
39,166,808✔
1403
      SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
20,715,008✔
1404
      if (pAppHbMgr == NULL) {
20,715,008✔
1405
        continue;
31,315✔
1406
      }
1407

1408
      int32_t connCnt = atomic_load_32(&pAppHbMgr->connKeyCnt);
20,715,008✔
1409
      if (connCnt == 0) {
20,715,008✔
1410
        continue;
2,181,726✔
1411
      }
1412
      SClientHbBatchReq *pReq = NULL;
18,533,282✔
1413
      int32_t            code = hbGatherAllInfo(pAppHbMgr, &pReq);
18,533,282✔
1414
      if (TSDB_CODE_SUCCESS != code || taosArrayGetP(clientHbMgr.appHbMgrs, i) == NULL) {
18,533,282✔
1415
        terrno = code ? code : TSDB_CODE_OUT_OF_RANGE;
×
1416
        tFreeClientHbBatchReq(pReq);
×
1417
        continue;
×
1418
      }
1419
      int tlen = tSerializeSClientHbBatchReq(NULL, 0, pReq);
18,533,282✔
1420
      if (tlen == -1) {
18,533,282✔
1421
        tFreeClientHbBatchReq(pReq);
×
1422
        break;
×
1423
      }
1424
      void *buf = taosMemoryMalloc(tlen);
18,533,282✔
1425
      if (buf == NULL) {
18,533,282✔
1426
        tFreeClientHbBatchReq(pReq);
×
1427
        // hbClearReqInfo(pAppHbMgr);
1428
        break;
×
1429
      }
1430

1431
      if (tSerializeSClientHbBatchReq(buf, tlen, pReq) == -1) {
18,533,282✔
1432
        tFreeClientHbBatchReq(pReq);
×
1433
        taosMemoryFree(buf);
×
1434
        break;
×
1435
      }
1436
      SMsgSendInfo *pInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
18,533,282✔
1437

1438
      if (pInfo == NULL) {
18,533,282✔
1439
        tFreeClientHbBatchReq(pReq);
×
1440
        // hbClearReqInfo(pAppHbMgr);
1441
        taosMemoryFree(buf);
×
1442
        break;
×
1443
      }
1444
      pInfo->fp = hbAsyncCallBack;
18,533,282✔
1445
      pInfo->msgInfo.pData = buf;
18,533,282✔
1446
      pInfo->msgInfo.len = tlen;
18,533,282✔
1447
      pInfo->msgType = TDMT_MND_HEARTBEAT;
18,533,282✔
1448
      pInfo->param = taosMemoryMalloc(sizeof(int32_t));
18,533,282✔
1449
      if (pInfo->param  == NULL) {
18,533,282✔
1450
        tFreeClientHbBatchReq(pReq);
×
1451
        // hbClearReqInfo(pAppHbMgr);
1452
        taosMemoryFree(buf);
×
1453
        taosMemoryFree(pInfo);
×
1454
        break;
×
1455
      }
1456
      *(int32_t *)pInfo->param = i;
18,533,282✔
1457
      pInfo->paramFreeFp = taosAutoMemoryFree;
18,533,282✔
1458
      pInfo->requestId = generateRequestId();
18,533,282✔
1459
      pInfo->requestObjRefId = 0;
18,533,282✔
1460

1461
      SAppInstInfo *pAppInstInfo = pAppHbMgr->pAppInstInfo;
18,533,282✔
1462
      SEpSet        epSet = getEpSet_s(&pAppInstInfo->mgmtEp);
18,533,282✔
1463
      if (TSDB_CODE_SUCCESS != asyncSendMsgToServer(pAppInstInfo->pTransporter, &epSet, NULL, pInfo)) {
18,533,282✔
1464
        tscWarn("failed to async send msg to server");
×
1465
      }
1466
      tFreeClientHbBatchReq(pReq);
18,533,282✔
1467
      // hbClearReqInfo(pAppHbMgr);
1468
      (void)atomic_add_fetch_32(&pAppHbMgr->reportCnt, 1);
18,533,282✔
1469
    }
1470

1471
    if (TSDB_CODE_SUCCESS != taosThreadMutexUnlock(&clientHbMgr.lock)) {
18,451,800✔
1472
      tscError("taosThreadMutexLock failed");
×
1473
      return NULL;
×
1474
    }
1475
    taosMsleep(HEARTBEAT_INTERVAL);
18,451,800✔
1476
  }
1477
  taosHashCleanup(clientHbMgr.appHbHash);
490,363✔
1478
  return NULL;
490,363✔
1479
}
1480

1481
static int32_t hbCreateThread() {
1,189,632✔
1482
  int32_t      code = TSDB_CODE_SUCCESS;
1,189,632✔
1483
  TdThreadAttr thAttr;
1,174,553✔
1484
  TSC_ERR_JRET(taosThreadAttrInit(&thAttr));
1,189,632✔
1485
  TSC_ERR_JRET(taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE));
1,189,632✔
1486
#ifdef TD_COMPACT_OS
1487
  TSC_ERR_JRET(taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL));
1488
#endif
1489

1490
  if (taosThreadCreate(&clientHbMgr.thread, &thAttr, hbThreadFunc, NULL) != 0) {
1,189,632✔
1491
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
1492
    TSC_ERR_RET(terrno);
×
1493
  }
1494
  (void)taosThreadAttrDestroy(&thAttr);
1,189,632✔
1495
_return:
1,189,632✔
1496

1497
  if (code) {
1,189,632✔
1498
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
1499
    TSC_ERR_RET(terrno);
×
1500
  }
1501

1502
  return code;
1,189,632✔
1503
}
1504

1505
static void hbStopThread() {
1,191,147✔
1506
  if (0 == atomic_load_8(&clientHbMgr.inited)) {
1,191,147✔
1507
    return;
1,515✔
1508
  }
1509
  if (atomic_val_compare_exchange_8(&clientHbMgr.threadStop, 0, 1)) {
1,189,632✔
1510
    tscDebug("hb thread already stopped");
×
1511
    return;
×
1512
  }
1513

1514
  int32_t code = TSDB_CODE_SUCCESS;
1,189,632✔
1515
  // thread quit mode kill or inner exit from self-thread
1516
  if (clientHbMgr.quitByKill) {
1,189,632✔
1517
    code = taosThreadKill(clientHbMgr.thread, 0);
742,384✔
1518
    if (TSDB_CODE_SUCCESS != code) {
742,384✔
1519
      tscError("taosThreadKill failed since %s", tstrerror(code));
×
1520
    }
1521
  } else {
1522
    code = taosThreadJoin(clientHbMgr.thread, NULL);
447,248✔
1523
    if (TSDB_CODE_SUCCESS != code) {
447,248✔
1524
      tscError("taosThreadJoin failed since %s", tstrerror(code));
×
1525
    }
1526
  }
1527

1528
  tscDebug("hb thread stopped");
1,189,632✔
1529
}
1530

1531
int32_t appHbMgrInit(SAppInstInfo *pAppInstInfo, char *key, SAppHbMgr **pAppHbMgr) {
1,247,333✔
1532
  int32_t code = TSDB_CODE_SUCCESS;
1,247,333✔
1533
  TSC_ERR_RET(hbMgrInit());
1,247,333✔
1534
  *pAppHbMgr = taosMemoryMalloc(sizeof(SAppHbMgr));
1,247,333✔
1535
  if (*pAppHbMgr == NULL) {
1,247,333✔
1536
    TSC_ERR_JRET(terrno);
×
1537
  }
1538
  // init stat
1539
  (*pAppHbMgr)->startTime = taosGetTimestampMs();
2,478,237✔
1540
  (*pAppHbMgr)->connKeyCnt = 0;
1,247,333✔
1541
  (*pAppHbMgr)->connHbFlag = 0;
1,247,333✔
1542
  (*pAppHbMgr)->reportCnt = 0;
1,247,333✔
1543
  (*pAppHbMgr)->reportBytes = 0;
1,247,333✔
1544
  (*pAppHbMgr)->key = taosStrdup(key);
1,247,333✔
1545
  if ((*pAppHbMgr)->key == NULL) {
1,247,333✔
1546
    TSC_ERR_JRET(terrno);
×
1547
  }
1548

1549
  // init app info
1550
  (*pAppHbMgr)->pAppInstInfo = pAppInstInfo;
1,247,333✔
1551

1552
  // init hash info
1553
  (*pAppHbMgr)->activeInfo = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
1,247,333✔
1554

1555
  if ((*pAppHbMgr)->activeInfo == NULL) {
1,247,333✔
1556
    TSC_ERR_JRET(terrno);
×
1557
  }
1558

1559
  // taosHashSetFreeFp(pAppHbMgr->activeInfo, tFreeClientHbReq);
1560

1561
  TSC_ERR_JRET(taosThreadMutexLock(&clientHbMgr.lock));
1,247,333✔
1562
  if (taosArrayPush(clientHbMgr.appHbMgrs, &(*pAppHbMgr)) == NULL) {
2,494,666✔
1563
    code = terrno;
×
1564
    (void)taosThreadMutexUnlock(&clientHbMgr.lock);
×
1565
    goto _return;
×
1566
  }
1567
  (*pAppHbMgr)->idx = taosArrayGetSize(clientHbMgr.appHbMgrs) - 1;
1,247,333✔
1568
  TSC_ERR_JRET(taosThreadMutexUnlock(&clientHbMgr.lock));
1,247,333✔
1569

1570
  return TSDB_CODE_SUCCESS;
1,247,333✔
1571
_return:
×
1572
  taosMemoryFree(*pAppHbMgr);
×
1573
  return code;
×
1574
}
1575

1576
void hbFreeAppHbMgr(SAppHbMgr *pTarget) {
1,247,333✔
1577
  void *pIter = taosHashIterate(pTarget->activeInfo, NULL);
1,247,333✔
1578
  while (pIter != NULL) {
1,393,190✔
1579
    SClientHbReq *pOneReq = pIter;
145,857✔
1580
    tFreeClientHbReq(pOneReq);
1581
    pIter = taosHashIterate(pTarget->activeInfo, pIter);
145,857✔
1582
  }
1583
  taosHashCleanup(pTarget->activeInfo);
1,247,333✔
1584
  pTarget->activeInfo = NULL;
1,247,333✔
1585

1586
  taosMemoryFree(pTarget->key);
1,247,333✔
1587
  taosMemoryFree(pTarget);
1,247,333✔
1588
}
1,247,333✔
1589

1590
void hbRemoveAppHbMrg(SAppHbMgr **pAppHbMgr) {
1,247,333✔
1591
  int32_t code = TSDB_CODE_SUCCESS;
1,247,333✔
1592
  code = taosThreadMutexLock(&clientHbMgr.lock);
1,247,333✔
1593
  if (TSDB_CODE_SUCCESS != code) {
1,247,333✔
1594
    tscError("failed to lock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1595
  }
1596
  int32_t mgrSize = taosArrayGetSize(clientHbMgr.appHbMgrs);
1,247,333✔
1597
  for (int32_t i = 0; i < mgrSize; ++i) {
1,247,333✔
1598
    SAppHbMgr *pItem = taosArrayGetP(clientHbMgr.appHbMgrs, i);
×
1599
    if (pItem == *pAppHbMgr) {
×
1600
      hbFreeAppHbMgr(*pAppHbMgr);
×
1601
      *pAppHbMgr = NULL;
×
1602
      taosArraySet(clientHbMgr.appHbMgrs, i, pAppHbMgr);
×
1603
      break;
×
1604
    }
1605
  }
1606
  code = taosThreadMutexUnlock(&clientHbMgr.lock);
1,247,333✔
1607
  if (TSDB_CODE_SUCCESS != code) {
1,247,333✔
1608
    tscError("failed to unlock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1609
  }
1610
}
1,247,333✔
1611

1612
void appHbMgrCleanup(void) {
1,189,632✔
1613
  int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
1,189,632✔
1614
  for (int i = 0; i < sz; i++) {
2,436,965✔
1615
    SAppHbMgr *pTarget = taosArrayGetP(clientHbMgr.appHbMgrs, i);
1,247,333✔
1616
    if (pTarget == NULL) continue;
1,247,333✔
1617
    hbFreeAppHbMgr(pTarget);
1,247,333✔
1618
  }
1619
}
1,189,632✔
1620

1621
int32_t hbMgrInit() {
1,247,333✔
1622
  // init once
1623
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 0, 1);
1,247,333✔
1624
  if (old == 1) return 0;
1,247,333✔
1625

1626
  clientHbMgr.appId = tGenIdPI64();
1,189,632✔
1627
  tscInfo("app initialized, appId:0x%" PRIx64, clientHbMgr.appId);
1,189,632✔
1628

1629
  clientHbMgr.appSummary = taosHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
1,189,632✔
1630
  if (NULL == clientHbMgr.appSummary) {
1,189,632✔
1631
    uError("hbMgrInit:taosHashInit error") return terrno;
×
1632
  }
1633
  clientHbMgr.appHbMgrs = taosArrayInit(0, sizeof(void *));
1,189,632✔
1634
  if (NULL == clientHbMgr.appHbMgrs) {
1,189,632✔
1635
    uError("hbMgrInit:taosArrayInit error") return terrno;
×
1636
  }
1637
  TdThreadMutexAttr attr = {0};
1,189,632✔
1638

1639
  int ret = taosThreadMutexAttrInit(&attr);
1,189,632✔
1640
  if (ret != 0) {
1,189,632✔
1641
    uError("hbMgrInit:taosThreadMutexAttrInit error") return ret;
×
1642
  }
1643

1644
  ret = taosThreadMutexAttrSetType(&attr, PTHREAD_MUTEX_RECURSIVE);
1,189,632✔
1645
  if (ret != 0) {
1,189,632✔
1646
    uError("hbMgrInit:taosThreadMutexAttrSetType error") return ret;
×
1647
  }
1648

1649
  ret = taosThreadMutexInit(&clientHbMgr.lock, &attr);
1,189,632✔
1650
  if (ret != 0) {
1,189,632✔
1651
    uError("hbMgrInit:taosThreadMutexInit error") return ret;
×
1652
  }
1653

1654
  ret = taosThreadMutexAttrDestroy(&attr);
1,189,632✔
1655
  if (ret != 0) {
1,189,632✔
1656
    uError("hbMgrInit:taosThreadMutexAttrDestroy error") return ret;
×
1657
  }
1658

1659
  // init handle funcs
1660
  hbMgrInitHandle();
1661

1662
  // init backgroud thread
1663
  ret = hbCreateThread();
1,189,632✔
1664
  if (ret != 0) {
1,189,632✔
1665
    uError("hbMgrInit:hbCreateThread error") return ret;
×
1666
  }
1667

1668
  return 0;
1,189,632✔
1669
}
1670

1671
void hbMgrCleanUp() {
1,191,147✔
1672
  hbStopThread();
1,191,147✔
1673

1674
  // destroy all appHbMgr
1675
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 1, 0);
1,191,147✔
1676
  if (old == 0) return;
1,191,147✔
1677

1678
  int32_t code = taosThreadMutexLock(&clientHbMgr.lock);
1,189,632✔
1679
  if (TSDB_CODE_SUCCESS != code) {
1,189,632✔
1680
    tscError("failed to lock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1681
  }
1682
  appHbMgrCleanup();
1,189,632✔
1683
  taosArrayDestroy(clientHbMgr.appHbMgrs);
1,189,632✔
1684
  clientHbMgr.appHbMgrs = NULL;
1,189,632✔
1685
  code = taosThreadMutexUnlock(&clientHbMgr.lock);
1,189,632✔
1686
  if (TSDB_CODE_SUCCESS != code) {
1,189,632✔
1687
    tscError("failed to unlock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1688
  }
1689
}
1690

1691
int32_t hbRegisterConnImpl(SAppHbMgr *pAppHbMgr, SClientHbKey connKey, int64_t clusterId) {
2,638,717✔
1692
  // init hash in activeinfo
1693
  void *data = taosHashGet(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
2,638,717✔
1694
  if (data != NULL) {
2,638,717✔
1695
    return 0;
×
1696
  }
1697
  SClientHbReq hbReq = {0};
2,638,717✔
1698
  hbReq.connKey = connKey;
2,638,717✔
1699
  hbReq.clusterId = clusterId;
2,638,717✔
1700
  // hbReq.info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
1701

1702
  TSC_ERR_RET(taosHashPut(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey), &hbReq, sizeof(SClientHbReq)));
2,638,717✔
1703

1704
  (void)atomic_add_fetch_32(&pAppHbMgr->connKeyCnt, 1);
2,638,717✔
1705
  return 0;
2,638,717✔
1706
}
1707

1708
int32_t hbRegisterConn(SAppHbMgr *pAppHbMgr, int64_t tscRefId, int64_t clusterId, int8_t connType) {
2,688,872✔
1709
  SClientHbKey connKey = {
2,688,872✔
1710
      .tscRid = tscRefId,
1711
      .connType = connType,
1712
  };
1713

1714
  switch (connType) {
2,688,872✔
1715
    case CONN_TYPE__QUERY: {
2,638,717✔
1716
      return hbRegisterConnImpl(pAppHbMgr, connKey, clusterId);
2,638,717✔
1717
    }
1718
    case CONN_TYPE__TMQ: {
50,155✔
1719
      return 0;
50,155✔
1720
    }
1721
    default:
×
1722
      return 0;
×
1723
  }
1724
}
1725

1726
void hbDeregisterConn(STscObj *pTscObj, SClientHbKey connKey) {
2,556,154✔
1727
  int32_t code = taosThreadMutexLock(&clientHbMgr.lock);
2,556,154✔
1728
  if (TSDB_CODE_SUCCESS != code) {
2,556,154✔
1729
    tscError("failed to lock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1730
  }
1731
  SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, pTscObj->appHbMgrIdx);
2,556,154✔
1732
  if (pAppHbMgr) {
2,556,154✔
1733
    SClientHbReq *pReq = taosHashAcquire(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
2,556,154✔
1734
    if (pReq) {
2,556,154✔
1735
      tFreeClientHbReq(pReq);
1736
      code = taosHashRemove(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey));
2,492,860✔
1737
      if (TSDB_CODE_SUCCESS != code) {
2,492,860✔
1738
        tscError("hbDeregisterConn taosHashRemove error, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1739
      }
1740
      taosHashRelease(pAppHbMgr->activeInfo, pReq);
2,492,860✔
1741
      (void)atomic_sub_fetch_32(&pAppHbMgr->connKeyCnt, 1);
2,492,860✔
1742
    }
1743
  }
1744
  code = taosThreadMutexUnlock(&clientHbMgr.lock);
2,556,154✔
1745
  if (TSDB_CODE_SUCCESS != code) {
2,556,154✔
1746
    tscError("failed to unlock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
1747
  }
1748
}
2,556,154✔
1749

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