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

taosdata / TDengine / #3558

17 Dec 2024 06:05AM UTC coverage: 59.778% (+1.6%) from 58.204%
#3558

push

travis-ci

web-flow
Merge pull request #29179 from taosdata/merge/mainto3.0

merge: form main to 3.0 branch

132787 of 287595 branches covered (46.17%)

Branch coverage included in aggregate %.

104 of 191 new or added lines in 5 files covered. (54.45%)

6085 existing lines in 168 files now uncovered.

209348 of 284746 relevant lines covered (73.52%)

8164844.48 hits per line

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

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

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

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

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

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

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

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

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

85
    SClientHbReq    *pReq = NULL;
3,770✔
86
    SGetUserAuthRsp *pRsp = NULL;
3,770✔
87
    while ((pReq = taosHashIterate(hbMgr->activeInfo, pReq))) {
9,145✔
88
      STscObj *pTscObj = (STscObj *)acquireTscObj(pReq->connKey.tscRid);
6,037✔
89
      if (!pTscObj) {
6,037✔
90
        continue;
404✔
91
      }
92

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

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

121
      pTscObj->authVer = pRsp->version;
4,899✔
122

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

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

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

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

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

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

202
  *pInfo = vgInfo;
2,937✔
203
  return code;
2,937✔
204
}
205

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

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

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

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

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

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

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

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

275
_return:
8,226✔
276

277
  tFreeSDbHbBatchRsp(&batchRsp);
8,226✔
278
  return code;
8,226✔
279
}
280

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

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

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

312
  int32_t numOfIndex = taosArrayGetSize(hbRsp.pIndexRsp);
5,644✔
313
  for (int32_t i = 0; i < numOfIndex; ++i) {
5,644!
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:
5,644✔
323
  taosArrayDestroy(hbRsp.pIndexRsp);
5,644✔
324
  hbRsp.pIndexRsp = NULL;
5,644✔
325

326
  tFreeSSTbHbRsp(&hbRsp);
5,644✔
327
  return code;
5,644✔
328
}
329

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

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

353
  int32_t numOfMeta = taosArrayGetSize(hbRsp.pViewRsp);
3✔
354
  for (int32_t i = 0; i < numOfMeta; ++i) {
3!
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:
3✔
373
  taosArrayDestroy(hbRsp.pViewRsp);
3✔
374
  return code;
3✔
375
}
376

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

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

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

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

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

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

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

496
  if (pRsp->query) {
125,108✔
497
    STscObj *pTscObj = (STscObj *)acquireTscObj(pRsp->connKey.tscRid);
121,174✔
498
    if (NULL == pTscObj) {
121,174!
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)) {
121,174✔
502
        SEpSet *pOrig = &pTscObj->pAppInfo->mgmtEp.epSet;
91✔
503
        SEp    *pOrigEp = &pOrig->eps[pOrig->inUse];
91✔
504
        SEp    *pNewEp = &pRsp->query->epSet.eps[pRsp->query->epSet.inUse];
91✔
505
        tscDebug("mnode epset updated from %d/%d=>%s:%d to %d/%d=>%s:%d in hb", pOrig->inUse, pOrig->numOfEps,
91!
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);
91✔
510
      }
511

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

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

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

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

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

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

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

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

559
  return TSDB_CODE_SUCCESS;
125,108✔
560
}
561

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

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

583
  int32_t rspNum = taosArrayGetSize(pRsp.rsps);
33,512✔
584

585
  (void)taosThreadMutexLock(&clientHbMgr.lock);
33,511✔
586

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

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

609
  pInst->serverCfg.monitorParas = pRsp.monitorParas;
32,907✔
610
  pInst->serverCfg.enableAuditDelete = pRsp.enableAuditDelete;
32,907✔
611
  tscDebug("[monitor] paras from hb, clusterId:%" PRIx64 " monitorParas threshold:%d scope:%d", pInst->clusterId,
32,907✔
612
           pRsp.monitorParas.tsSlowLogThreshold, pRsp.monitorParas.tsSlowLogScope);
613

614
  if (rspNum) {
32,907✔
615
    tscDebug("hb got %d rsp, %d empty rsp received before", rspNum,
32,752✔
616
             atomic_val_compare_exchange_32(&emptyRspNum, emptyRspNum, 0));
617
  } else {
618
    (void)atomic_add_fetch_32(&emptyRspNum, 1);
155✔
619
  }
620

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

629
  (void)taosThreadMutexUnlock(&clientHbMgr.lock);
32,907✔
630

631
  tFreeClientHbBatchRsp(&pRsp);
632

633
_return:
32,907✔
634
  taosMemoryFree(pMsg->pData);
32,907!
635
  taosMemoryFree(pMsg->pEpSet);
32,907!
636
  return code;
32,907✔
637
}
638

639
int32_t hbBuildQueryDesc(SQueryHbReqBasic *hbBasic, STscObj *pObj) {
41,235✔
640
  int64_t    now = taosGetTimestampUs();
41,235✔
641
  SQueryDesc desc = {0};
41,235✔
642
  int32_t    code = 0;
41,235✔
643

644
  void *pIter = taosHashIterate(pObj->pRequests, NULL);
41,235✔
645
  while (pIter != NULL) {
84,389✔
646
    int64_t     *rid = pIter;
43,154✔
647
    SRequestObj *pRequest = acquireRequest(*rid);
43,154✔
648
    if (NULL == pRequest) {
43,154✔
649
      pIter = taosHashIterate(pObj->pRequests, pIter);
178✔
650
      continue;
178✔
651
    }
652

653
    if (pRequest->killed || 0 == pRequest->body.queryJob) {
42,976✔
654
      (void)releaseRequest(*rid);
14,505✔
655
      pIter = taosHashIterate(pObj->pRequests, pIter);
14,505✔
656
      continue;
14,505✔
657
    }
658

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

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

681
      code = schedulerGetTasksStatus(pRequest->body.queryJob, desc.subDesc);
28,471✔
682
      if (code) {
28,471✔
683
        taosArrayDestroy(desc.subDesc);
5,348✔
684
        desc.subDesc = NULL;
5,348✔
685
      }
686
      desc.subPlanNum = taosArrayGetSize(desc.subDesc);
28,471✔
687
    } else {
688
      desc.subDesc = NULL;
×
689
    }
690

691
    (void)releaseRequest(*rid);
28,471✔
692
    if (NULL == taosArrayPush(hbBasic->queryDesc, &desc)) {
56,942!
693
      taosArrayDestroy(desc.subDesc);
×
694
      return terrno;
×
695
    }
696

697
    pIter = taosHashIterate(pObj->pRequests, pIter);
28,471✔
698
  }
699

700
  return code;
41,235✔
701
}
702

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

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

717
  hbBasic->connId = pTscObj->connId;
127,169✔
718

719
  int32_t numOfQueries = pTscObj->pRequests ? taosHashGetSize(pTscObj->pRequests) : 0;
127,169!
720
  if (numOfQueries <= 0) {
127,169✔
721
    req->query = hbBasic;
85,934✔
722
    releaseTscObj(connKey->tscRid);
85,934✔
723
    tscDebug("no queries on connection");
85,934✔
724
    return TSDB_CODE_SUCCESS;
85,934✔
725
  }
726

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

735
  int32_t code = hbBuildQueryDesc(hbBasic, pTscObj);
41,235✔
736
  if (code) {
41,235✔
737
    releaseTscObj(connKey->tscRid);
3,941✔
738
    if (hbBasic->queryDesc) {
3,941!
739
      taosArrayDestroyEx(hbBasic->queryDesc, tFreeClientHbQueryDesc);
3,941✔
740
    }
741
    taosMemoryFree(hbBasic);
3,941!
742
    return code;
3,941✔
743
  }
744

745
  req->query = hbBasic;
37,294✔
746
  releaseTscObj(connKey->tscRid);
37,294✔
747

748
  return TSDB_CODE_SUCCESS;
37,294✔
749
}
750

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

758
  int32_t code = 0;
2,121✔
759

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

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

798
  tscDebug("hb got user auth info, valueLen:%d, user:%s, authVer:%d, tscRid:%" PRIi64, kv.valueLen, user->user,
660✔
799
           pTscObj->authVer, connKey->tscRid);
800

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

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

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

821
  return code;
2,121✔
822
}
823

824
int32_t hbGetExpiredUserInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
27,908✔
825
  SUserAuthVersion *users = NULL;
27,908✔
826
  uint32_t          userNum = 0;
27,908✔
827
  int32_t           code = 0;
27,908✔
828

829
  code = catalogGetExpiredUsers(pCatalog, &users, &userNum);
27,908✔
830
  if (TSDB_CODE_SUCCESS != code) {
27,908!
831
    return code;
×
832
  }
833

834
  if (userNum <= 0) {
27,908✔
835
    taosMemoryFree(users);
578!
836
    return TSDB_CODE_SUCCESS;
578✔
837
  }
838

839
  for (int32_t i = 0; i < userNum; ++i) {
54,913✔
840
    SUserAuthVersion *user = &users[i];
27,583✔
841
    user->version = htonl(user->version);
27,583✔
842
  }
843

844
  SKv kv = {
27,330✔
845
      .key = HEARTBEAT_KEY_USER_AUTHINFO,
846
      .valueLen = sizeof(SUserAuthVersion) * userNum,
27,330✔
847
      .value = users,
848
  };
849

850
  tscDebug("hb got %d expired users, valueLen:%d", userNum, kv.valueLen);
27,330✔
851

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

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

866
  return TSDB_CODE_SUCCESS;
27,330✔
867
}
868

869
int32_t hbGetExpiredDBInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
31,524✔
870
  SDbCacheInfo *dbs = NULL;
31,524✔
871
  uint32_t      dbNum = 0;
31,524✔
872
  int32_t       code = 0;
31,524✔
873

874
  code = catalogGetExpiredDBs(pCatalog, &dbs, &dbNum);
31,524✔
875
  if (TSDB_CODE_SUCCESS != code) {
31,524!
876
    return code;
×
877
  }
878

879
  if (dbNum <= 0) {
31,524✔
880
    taosMemoryFree(dbs);
22,834!
881
    return TSDB_CODE_SUCCESS;
22,834✔
882
  }
883

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

890
    db->dbId = htobe64(db->dbId);
10,247✔
891
    db->vgVersion = htonl(db->vgVersion);
10,247✔
892
    db->cfgVersion = htonl(db->cfgVersion);
10,247✔
893
    db->numOfTable = htonl(db->numOfTable);
10,247✔
894
    db->stateTs = htobe64(db->stateTs);
10,247✔
895
    db->tsmaVersion = htonl(db->tsmaVersion);
10,247✔
896
  }
897

898
  SKv kv = {
8,690✔
899
      .key = HEARTBEAT_KEY_DBINFO,
900
      .valueLen = sizeof(SDbCacheInfo) * dbNum,
8,690✔
901
      .value = dbs,
902
  };
903

904
  tscDebug("hb got %d expired db, valueLen:%d", dbNum, kv.valueLen);
8,690✔
905

906
  if (NULL == req->info) {
8,690✔
907
    req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
15✔
908
    if (NULL == req->info) {
15!
909
      taosMemoryFree(dbs);
×
910
      return terrno;
×
911
    }
912
  }
913

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

920
  return TSDB_CODE_SUCCESS;
8,690✔
921
}
922

923
int32_t hbGetExpiredStbInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *req) {
31,524✔
924
  SSTableVersion *stbs = NULL;
31,524✔
925
  uint32_t        stbNum = 0;
31,524✔
926
  int32_t         code = 0;
31,524✔
927

928
  code = catalogGetExpiredSTables(pCatalog, &stbs, &stbNum);
31,524✔
929
  if (TSDB_CODE_SUCCESS != code) {
31,524!
930
    return code;
×
931
  }
932

933
  if (stbNum <= 0) {
31,524✔
934
    taosMemoryFree(stbs);
25,395!
935
    return TSDB_CODE_SUCCESS;
25,395✔
936
  }
937

938
  for (int32_t i = 0; i < stbNum; ++i) {
14,771✔
939
    SSTableVersion *stb = &stbs[i];
8,642✔
940
    stb->suid = htobe64(stb->suid);
8,642✔
941
    stb->sversion = htonl(stb->sversion);
8,642✔
942
    stb->tversion = htonl(stb->tversion);
8,642✔
943
    stb->smaVer = htonl(stb->smaVer);
8,642✔
944
  }
945

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

952
  tscDebug("hb got %d expired stb, valueLen:%d", stbNum, kv.valueLen);
6,129✔
953

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

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

968
  return TSDB_CODE_SUCCESS;
6,129✔
969
}
970

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

977
  TSC_ERR_JRET(catalogGetExpiredViews(pCatalog, &views, &viewNum, &pDynViewVer));
31,524!
978

979
  if (viewNum <= 0) {
31,524✔
980
    taosMemoryFree(views);
31,521!
981
    taosMemoryFree(pDynViewVer);
31,521!
982
    return TSDB_CODE_SUCCESS;
31,521✔
983
  }
984

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

992
  tscDebug("hb got %u expired view, valueLen:%lu", viewNum, sizeof(SViewVersion) * viewNum);
3!
993

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

1001
  SKv kv = {
3✔
1002
      .key = HEARTBEAT_KEY_DYN_VIEW,
1003
      .valueLen = sizeof(SDynViewVersion),
1004
      .value = pDynViewVer,
1005
  };
1006

1007
  TSC_ERR_JRET(taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)));
3!
1008

1009
  kv.key = HEARTBEAT_KEY_VIEWINFO;
3✔
1010
  kv.valueLen = sizeof(SViewVersion) * viewNum;
3✔
1011
  kv.value = views;
3✔
1012

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

1021
int32_t hbGetExpiredTSMAInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *pReq) {
31,524✔
1022
  int32_t       code = 0;
31,524✔
1023
  uint32_t      tsmaNum = 0;
31,524✔
1024
  STSMAVersion *tsmas = NULL;
31,524✔
1025

1026
  code = catalogGetExpiredTsmas(pCatalog, &tsmas, &tsmaNum);
31,524✔
1027
  if (code) {
31,524!
1028
    taosMemoryFree(tsmas);
×
1029
    return code;
×
1030
  }
1031

1032
  if (tsmaNum <= 0) {
31,524✔
1033
    taosMemoryFree(tsmas);
31,183!
1034
    return TSDB_CODE_SUCCESS;
31,183✔
1035
  }
1036

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

1044
  tscDebug("hb got %d expred tsmas, valueLen: %lu", tsmaNum, sizeof(STSMAVersion) * tsmaNum);
341!
1045

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

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

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

1074
  return TSDB_CODE_SUCCESS;
123,228✔
1075
}
1076

1077
int32_t hbQueryHbReqHandle(SClientHbKey *connKey, void *param, SClientHbReq *req) {
127,169✔
1078
  int32_t   code = 0;
127,169✔
1079
  SHbParam *hbParam = (SHbParam *)param;
127,169✔
1080
  SCatalog *pCatalog = NULL;
127,169✔
1081

1082
  code = hbGetQueryBasicInfo(connKey, req);
127,169✔
1083
  if (code != TSDB_CODE_SUCCESS) {
127,169✔
1084
    tscWarn("hbGetQueryBasicInfo failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
3,941!
1085
    return code;
3,941✔
1086
  }
1087

1088
  if (hbParam->reqCnt == 0) {
123,228✔
1089
    code = catalogGetHandle(hbParam->clusterId, &pCatalog);
31,524✔
1090
    if (code != TSDB_CODE_SUCCESS) {
31,524!
1091
      tscWarn("catalogGetHandle failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1092
      return code;
×
1093
    }
1094

1095
    code = hbGetAppInfo(hbParam->clusterId, req);
31,524✔
1096
    if (TSDB_CODE_SUCCESS != code) {
31,524!
1097
      tscWarn("getAppInfo failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1098
      return code;
×
1099
    }
1100

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

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

1127
    code = hbGetExpiredDBInfo(connKey, pCatalog, req);
31,524✔
1128
    if (TSDB_CODE_SUCCESS != code) {
31,524!
1129
      tscWarn("hbGetExpiredDBInfo failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code));
×
1130
      return code;
×
1131
    }
1132

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

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

1159
  ++hbParam->reqCnt;  // success to get catalog info
123,228✔
1160

1161
  return TSDB_CODE_SUCCESS;
123,228✔
1162
}
1163

1164
static FORCE_INLINE void hbMgrInitHandle() {
1165
  // init all handle
1166
  clientHbMgr.reqHandle[CONN_TYPE__QUERY] = hbQueryHbReqHandle;
1,965✔
1167
  clientHbMgr.reqHandle[CONN_TYPE__TMQ] = hbMqHbReqHandle;
1,965✔
1168

1169
  clientHbMgr.rspHandle[CONN_TYPE__QUERY] = hbQueryHbRspHandle;
1,965✔
1170
  clientHbMgr.rspHandle[CONN_TYPE__TMQ] = hbMqHbRspHandle;
1,965✔
1171
}
1,965✔
1172

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

1185
  int64_t  maxIpWhiteVer = 0;
34,844✔
1186
  void    *pIter = NULL;
34,844✔
1187
  SHbParam param = {0};
34,844✔
1188
  while ((pIter = taosHashIterate(pAppHbMgr->activeInfo, pIter))) {
163,733✔
1189
    SClientHbReq *pOneReq = pIter;
128,889✔
1190
    SClientHbKey *connKey = &pOneReq->connKey;
128,889✔
1191
    STscObj      *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid);
128,889✔
1192

1193
    if (!pTscObj || atomic_load_8(&pTscObj->dropped) == 1) {
128,889✔
1194
      if (pTscObj) releaseTscObj(connKey->tscRid);
1,720✔
1195
      continue;
1,720✔
1196
    }
1197

1198
    tstrncpy(pOneReq->userApp, pTscObj->optionInfo.userApp, sizeof(pOneReq->userApp));
127,169✔
1199
    pOneReq->userIp = pTscObj->optionInfo.userIp;
127,169✔
1200

1201
    pOneReq = taosArrayPush((*pBatchReq)->reqs, pOneReq);
127,169✔
1202
    if (NULL == pOneReq) {
127,169!
1203
      releaseTscObj(connKey->tscRid);
×
1204
      continue;
×
1205
    }
1206

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

1228
    int64_t ver = atomic_load_64(&pTscObj->whiteListInfo.ver);
127,169✔
1229
    maxIpWhiteVer = TMAX(maxIpWhiteVer, ver);
127,169✔
1230
    releaseTscObj(connKey->tscRid);
127,169✔
1231
  }
1232
  (*pBatchReq)->ipWhiteList = maxIpWhiteVer;
34,844✔
1233

1234
  return TSDB_CODE_SUCCESS;
34,844✔
1235
}
1236

1237
void hbThreadFuncUnexpectedStopped(void) { atomic_store_8(&clientHbMgr.threadStop, 2); }
×
1238

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

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

1261
  taosHashClear(clientHbMgr.appSummary);
35,732✔
1262

1263
  for (int32_t i = 0; i < sz; ++i) {
78,637✔
1264
    SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
42,905✔
1265
    if (pAppHbMgr == NULL) continue;
42,905!
1266

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

1278
      hbMergeSummary(&pApp->summary, &pAppHbMgr->pAppInstInfo->summary);
7,171✔
1279
    }
1280
  }
1281

1282
  return TSDB_CODE_SUCCESS;
35,732✔
1283
}
1284

1285
static void *hbThreadFunc(void *param) {
1,965✔
1286
  setThreadName("hb");
1,965✔
1287
#ifdef WINDOWS
1288
  if (taosCheckCurrentInDll()) {
1289
    atexit(hbThreadFuncUnexpectedStopped);
1290
  }
1291
#endif
1292
  while (1) {
35,351✔
1293
    if (1 == clientHbMgr.threadStop) {
37,316✔
1294
      break;
1,567✔
1295
    }
1296

1297
    if (TSDB_CODE_SUCCESS != taosThreadMutexLock(&clientHbMgr.lock)) {
35,749!
1298
      tscError("taosThreadMutexLock failed");
×
1299
      return NULL;
×
1300
    }
1301

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

1318
    for (int i = 0; i < sz; i++) {
78,654✔
1319
      SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, i);
42,905✔
1320
      if (pAppHbMgr == NULL) {
42,905!
1321
        continue;
8,061✔
1322
      }
1323

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

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

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

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

1387
    if (TSDB_CODE_SUCCESS != taosThreadMutexUnlock(&clientHbMgr.lock)) {
35,749!
1388
      tscError("taosThreadMutexLock failed");
×
1389
      return NULL;
×
1390
    }
1391
    taosMsleep(HEARTBEAT_INTERVAL);
35,749✔
1392
  }
1393
  taosHashCleanup(clientHbMgr.appHbHash);
1,567✔
1394
  return NULL;
1,567✔
1395
}
1396

1397
static int32_t hbCreateThread() {
1,965✔
1398
  int32_t      code = TSDB_CODE_SUCCESS;
1,965✔
1399
  TdThreadAttr thAttr;
1400
  TSC_ERR_JRET(taosThreadAttrInit(&thAttr));
1,965!
1401
  TSC_ERR_JRET(taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE));
1,965!
1402

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

1410
  if (code) {
1,965!
1411
    terrno = TAOS_SYSTEM_ERROR(errno);
×
1412
    TSC_ERR_RET(terrno);
×
1413
  }
1414

1415
  return code;
1,965✔
1416
}
1417

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

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

1441
  tscDebug("hb thread stopped");
1,965✔
1442
}
1443

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

1462
  // init app info
1463
  (*pAppHbMgr)->pAppInstInfo = pAppInstInfo;
2,178✔
1464

1465
  // init hash info
1466
  (*pAppHbMgr)->activeInfo = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK);
2,178✔
1467

1468
  if ((*pAppHbMgr)->activeInfo == NULL) {
2,178!
1469
    TSC_ERR_JRET(terrno);
×
1470
  }
1471

1472
  // taosHashSetFreeFp(pAppHbMgr->activeInfo, tFreeClientHbReq);
1473

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

1483
  return TSDB_CODE_SUCCESS;
2,178✔
1484
_return:
×
1485
  taosMemoryFree(*pAppHbMgr);
×
1486
  return code;
×
1487
}
1488

1489
void hbFreeAppHbMgr(SAppHbMgr *pTarget) {
2,178✔
1490
  void *pIter = taosHashIterate(pTarget->activeInfo, NULL);
2,178✔
1491
  while (pIter != NULL) {
2,716✔
1492
    SClientHbReq *pOneReq = pIter;
538!
1493
    tFreeClientHbReq(pOneReq);
1494
    pIter = taosHashIterate(pTarget->activeInfo, pIter);
538✔
1495
  }
1496
  taosHashCleanup(pTarget->activeInfo);
2,178✔
1497
  pTarget->activeInfo = NULL;
2,178✔
1498

1499
  taosMemoryFree(pTarget->key);
2,178!
1500
  taosMemoryFree(pTarget);
2,178!
1501
}
2,178✔
1502

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

1525
void appHbMgrCleanup(void) {
1,965✔
1526
  int sz = taosArrayGetSize(clientHbMgr.appHbMgrs);
1,965✔
1527
  for (int i = 0; i < sz; i++) {
4,143✔
1528
    SAppHbMgr *pTarget = taosArrayGetP(clientHbMgr.appHbMgrs, i);
2,178✔
1529
    if (pTarget == NULL) continue;
2,178!
1530
    hbFreeAppHbMgr(pTarget);
2,178✔
1531
  }
1532
}
1,965✔
1533

1534
int32_t hbMgrInit() {
2,178✔
1535
  // init once
1536
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 0, 1);
2,178✔
1537
  if (old == 1) return 0;
2,178✔
1538

1539
  clientHbMgr.appId = tGenIdPI64();
1,965✔
1540
  tscDebug("app %" PRIx64 " initialized", clientHbMgr.appId);
1,965✔
1541

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

1552
  int ret = taosThreadMutexAttrInit(&attr);
1,965✔
1553
  if (ret != 0) {
1,965!
1554
    uError("hbMgrInit:taosThreadMutexAttrInit error") return ret;
×
1555
  }
1556

1557
  ret = taosThreadMutexAttrSetType(&attr, PTHREAD_MUTEX_RECURSIVE);
1,965✔
1558
  if (ret != 0) {
1,965!
1559
    uError("hbMgrInit:taosThreadMutexAttrSetType error") return ret;
×
1560
  }
1561

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

1567
  ret = taosThreadMutexAttrDestroy(&attr);
1,965✔
1568
  if (ret != 0) {
1,965!
1569
    uError("hbMgrInit:taosThreadMutexAttrDestroy error") return ret;
×
1570
  }
1571

1572
  // init handle funcs
1573
  hbMgrInitHandle();
1574

1575
  // init backgroud thread
1576
  ret = hbCreateThread();
1,965✔
1577
  if (ret != 0) {
1,965!
1578
    uError("hbMgrInit:hbCreateThread error") return ret;
×
1579
  }
1580

1581
  return 0;
1,965✔
1582
}
1583

1584
void hbMgrCleanUp() {
1,975✔
1585
  hbStopThread();
1,975✔
1586

1587
  // destroy all appHbMgr
1588
  int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 1, 0);
1,975✔
1589
  if (old == 0) return;
1,975✔
1590

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

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

1615
  TSC_ERR_RET(taosHashPut(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey), &hbReq, sizeof(SClientHbReq)));
9,199!
1616

1617
  (void)atomic_add_fetch_32(&pAppHbMgr->connKeyCnt, 1);
9,199✔
1618
  return 0;
9,199✔
1619
}
1620

1621
int32_t hbRegisterConn(SAppHbMgr *pAppHbMgr, int64_t tscRefId, int64_t clusterId, int8_t connType) {
9,610✔
1622
  SClientHbKey connKey = {
9,610✔
1623
      .tscRid = tscRefId,
1624
      .connType = connType,
1625
  };
1626

1627
  switch (connType) {
9,610!
1628
    case CONN_TYPE__QUERY: {
9,199✔
1629
      return hbRegisterConnImpl(pAppHbMgr, connKey, clusterId);
9,199✔
1630
    }
1631
    case CONN_TYPE__TMQ: {
411✔
1632
      return 0;
411✔
1633
    }
1634
    default:
×
1635
      return 0;
×
1636
  }
1637
}
1638

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

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