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

taosdata / TDengine / #4143

24 May 2025 03:30AM UTC coverage: 32.868% (-29.4%) from 62.238%
#4143

push

travis-ci

web-flow
test: migrate stream cases (#31164)

76401 of 312956 branches covered (24.41%)

Branch coverage included in aggregate %.

128686 of 311012 relevant lines covered (41.38%)

579734.08 hits per line

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

275
_return:
52✔
276

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

529
      if (pRsp->query->killConnection) {
171!
530
        taos_close_internal(pTscObj);
×
531
      }
532

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

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

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

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

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

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

559
  return TSDB_CODE_SUCCESS;
172✔
560
}
561

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

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

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

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

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

597
  SAppInstInfo *pInst = pAppHbMgr->pAppInstInfo;
125✔
598

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

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

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

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

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

633
  tFreeClientHbBatchRsp(&pRsp);
634

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

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

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

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

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

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

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

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

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

702
  return code;
65✔
703
}
704

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

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

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

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

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

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

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

750
  return TSDB_CODE_SUCCESS;
64✔
751
}
752

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

760
  int32_t code = 0;
33✔
761

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

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

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

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

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

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

823
  return code;
33✔
824
}
825

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

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

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

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

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

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

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

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

868
  return TSDB_CODE_SUCCESS;
140✔
869
}
870

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

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

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

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

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

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

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

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

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

922
  return TSDB_CODE_SUCCESS;
60✔
923
}
924

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

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

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

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

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

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

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

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

970
  return TSDB_CODE_SUCCESS;
70✔
971
}
972

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1076
  return TSDB_CODE_SUCCESS;
190✔
1077
}
1078

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

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

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

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

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

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

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

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

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

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

1163
  return TSDB_CODE_SUCCESS;
190✔
1164
}
1165

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

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

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

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

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

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

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

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

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

1236
  return TSDB_CODE_SUCCESS;
142✔
1237
}
1238

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

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

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

1263
  taosHashClear(clientHbMgr.appSummary);
157✔
1264

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

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

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

1284
  return TSDB_CODE_SUCCESS;
157✔
1285
}
1286

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

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

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

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

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

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

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

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

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

1399
static int32_t hbCreateThread() {
17✔
1400
  int32_t      code = TSDB_CODE_SUCCESS;
17✔
1401
  TdThreadAttr thAttr;
1402
  TSC_ERR_JRET(taosThreadAttrInit(&thAttr));
17!
1403
  TSC_ERR_JRET(taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE));
17!
1404
#ifdef TD_COMPACT_OS
1405
  TSC_ERR_JRET(taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL));
1406
#endif
1407

1408
  if (taosThreadCreate(&clientHbMgr.thread, &thAttr, hbThreadFunc, NULL) != 0) {
17!
1409
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
1410
    TSC_ERR_RET(terrno);
×
1411
  }
1412
  (void)taosThreadAttrDestroy(&thAttr);
17✔
1413
_return:
17✔
1414

1415
  if (code) {
17!
1416
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
1417
    TSC_ERR_RET(terrno);
×
1418
  }
1419

1420
  return code;
17✔
1421
}
1422

1423
static void hbStopThread() {
19✔
1424
  if (0 == atomic_load_8(&clientHbMgr.inited)) {
19✔
1425
    return;
2✔
1426
  }
1427
  if (atomic_val_compare_exchange_8(&clientHbMgr.threadStop, 0, 1)) {
17!
1428
    tscDebug("hb thread already stopped");
×
1429
    return;
×
1430
  }
1431

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

1446
  tscDebug("hb thread stopped");
17✔
1447
}
1448

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

1467
  // init app info
1468
  (*pAppHbMgr)->pAppInstInfo = pAppInstInfo;
19✔
1469

1470
  // init hash info
1471
  (*pAppHbMgr)->activeInfo = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
19✔
1472

1473
  if ((*pAppHbMgr)->activeInfo == NULL) {
19!
1474
    TSC_ERR_JRET(terrno);
×
1475
  }
1476

1477
  // taosHashSetFreeFp(pAppHbMgr->activeInfo, tFreeClientHbReq);
1478

1479
  TSC_ERR_JRET(taosThreadMutexLock(&clientHbMgr.lock));
19!
1480
  if (taosArrayPush(clientHbMgr.appHbMgrs, &(*pAppHbMgr)) == NULL) {
38!
1481
    code = terrno;
×
1482
    (void)taosThreadMutexUnlock(&clientHbMgr.lock);
×
1483
    goto _return;
×
1484
  }
1485
  (*pAppHbMgr)->idx = taosArrayGetSize(clientHbMgr.appHbMgrs) - 1;
19✔
1486
  TSC_ERR_JRET(taosThreadMutexUnlock(&clientHbMgr.lock));
19!
1487

1488
  return TSDB_CODE_SUCCESS;
19✔
1489
_return:
×
1490
  taosMemoryFree(*pAppHbMgr);
×
1491
  return code;
×
1492
}
1493

1494
void hbFreeAppHbMgr(SAppHbMgr *pTarget) {
19✔
1495
  void *pIter = taosHashIterate(pTarget->activeInfo, NULL);
19✔
1496
  while (pIter != NULL) {
35✔
1497
    SClientHbReq *pOneReq = pIter;
16!
1498
    tFreeClientHbReq(pOneReq);
1499
    pIter = taosHashIterate(pTarget->activeInfo, pIter);
16✔
1500
  }
1501
  taosHashCleanup(pTarget->activeInfo);
19✔
1502
  pTarget->activeInfo = NULL;
19✔
1503

1504
  taosMemoryFree(pTarget->key);
19!
1505
  taosMemoryFree(pTarget);
19!
1506
}
19✔
1507

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

1530
void appHbMgrCleanup(void) {
17✔
1531
  int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
17✔
1532
  for (int i = 0; i < sz; i++) {
36✔
1533
    SAppHbMgr *pTarget = taosArrayGetP(clientHbMgr.appHbMgrs, i);
19✔
1534
    if (pTarget == NULL) continue;
19!
1535
    hbFreeAppHbMgr(pTarget);
19✔
1536
  }
1537
}
17✔
1538

1539
int32_t hbMgrInit() {
19✔
1540
  // init once
1541
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 0, 1);
19✔
1542
  if (old == 1) return 0;
19✔
1543

1544
  clientHbMgr.appId = tGenIdPI64();
17✔
1545
  tscInfo("app initialized, appId:0x%" PRIx64, clientHbMgr.appId);
17!
1546

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

1557
  int ret = taosThreadMutexAttrInit(&attr);
17✔
1558
  if (ret != 0) {
17!
1559
    uError("hbMgrInit:taosThreadMutexAttrInit error") return ret;
×
1560
  }
1561

1562
  ret = taosThreadMutexAttrSetType(&attr, PTHREAD_MUTEX_RECURSIVE);
17✔
1563
  if (ret != 0) {
17!
1564
    uError("hbMgrInit:taosThreadMutexAttrSetType error") return ret;
×
1565
  }
1566

1567
  ret = taosThreadMutexInit(&clientHbMgr.lock, &attr);
17✔
1568
  if (ret != 0) {
17!
1569
    uError("hbMgrInit:taosThreadMutexInit error") return ret;
×
1570
  }
1571

1572
  ret = taosThreadMutexAttrDestroy(&attr);
17✔
1573
  if (ret != 0) {
17!
1574
    uError("hbMgrInit:taosThreadMutexAttrDestroy error") return ret;
×
1575
  }
1576

1577
  // init handle funcs
1578
  hbMgrInitHandle();
1579

1580
  // init backgroud thread
1581
  ret = hbCreateThread();
17✔
1582
  if (ret != 0) {
17!
1583
    uError("hbMgrInit:hbCreateThread error") return ret;
×
1584
  }
1585

1586
  return 0;
17✔
1587
}
1588

1589
void hbMgrCleanUp() {
19✔
1590
  hbStopThread();
19✔
1591

1592
  // destroy all appHbMgr
1593
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 1, 0);
19✔
1594
  if (old == 0) return;
19✔
1595

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

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

1620
  TSC_ERR_RET(taosHashPut(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey), &hbReq, sizeof(SClientHbReq)));
108!
1621

1622
  (void)atomic_add_fetch_32(&pAppHbMgr->connKeyCnt, 1);
108✔
1623
  return 0;
108✔
1624
}
1625

1626
int32_t hbRegisterConn(SAppHbMgr *pAppHbMgr, int64_t tscRefId, int64_t clusterId, int8_t connType) {
119✔
1627
  SClientHbKey connKey = {
119✔
1628
      .tscRid = tscRefId,
1629
      .connType = connType,
1630
  };
1631

1632
  switch (connType) {
119!
1633
    case CONN_TYPE__QUERY: {
108✔
1634
      return hbRegisterConnImpl(pAppHbMgr, connKey, clusterId);
108✔
1635
    }
1636
    case CONN_TYPE__TMQ: {
11✔
1637
      return 0;
11✔
1638
    }
1639
    default:
×
1640
      return 0;
×
1641
  }
1642
}
1643

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

1668
// set heart beat thread quit mode , if quicByKill 1 then kill thread else quit from inner
1669
void taos_set_hb_quit(int8_t quitByKill) { clientHbMgr.quitByKill = quitByKill; }
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc