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

taosdata / TDengine / #4473

08 Jul 2025 09:38AM UTC coverage: 62.922% (+0.7%) from 62.22%
#4473

push

travis-ci

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

merge: from main to 3.0 branch

158525 of 321496 branches covered (49.31%)

Branch coverage included in aggregate %.

56 of 60 new or added lines in 13 files covered. (93.33%)

1333 existing lines in 67 files now uncovered.

245526 of 320647 relevant lines covered (76.57%)

17689640.25 hits per line

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

56.85
/source/libs/catalog/src/ctgUtil.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 "catalogInt.h"
17
#include "query.h"
18
#include "systable.h"
19
#include "tname.h"
20
#include "trpc.h"
21

22
void ctgFreeSViewMeta(SViewMeta* pMeta) {
1,223✔
23
  if (NULL == pMeta) {
1,223✔
24
    return;
317✔
25
  }
26

27
  taosMemoryFree(pMeta->user);
906!
28
  taosMemoryFree(pMeta->querySql);
906!
29
  taosMemoryFree(pMeta->pSchema);
906!
30
}
31

32
void ctgFreeMsgSendParam(void* param) {
206,651✔
33
  if (NULL == param) {
206,651!
34
    return;
×
35
  }
36

37
  SCtgTaskCallbackParam* pParam = (SCtgTaskCallbackParam*)param;
206,651✔
38
  taosArrayDestroy(pParam->taskId);
206,651✔
39
  taosArrayDestroy(pParam->msgIdx);
206,652✔
40

41
  taosMemoryFree(param);
206,653!
42
}
43

44
void ctgFreeBatchMsg(void* msg) {
413,701✔
45
  if (NULL == msg) {
413,701!
46
    return;
×
47
  }
48
  SBatchMsg* pMsg = (SBatchMsg*)msg;
413,701✔
49
  taosMemoryFree(pMsg->msg);
413,701!
50
}
51

52
void ctgFreeBatch(SCtgBatch* pBatch) {
206,649✔
53
  if (NULL == pBatch) {
206,649!
54
    return;
×
55
  }
56

57
  taosArrayDestroyEx(pBatch->pMsgs, ctgFreeBatchMsg);
206,649✔
58
  taosArrayDestroy(pBatch->pTaskIds);
206,653✔
59
}
60

61
void ctgFreeBatchs(SHashObj* pBatchs) {
1,638,289✔
62
  void* p = taosHashIterate(pBatchs, NULL);
1,638,289✔
63
  while (NULL != p) {
1,845,031✔
64
    SCtgBatch* pBatch = (SCtgBatch*)p;
206,653✔
65

66
    ctgFreeBatch(pBatch);
206,653✔
67

68
    p = taosHashIterate(pBatchs, p);
206,652✔
69
  }
70

71
  taosHashCleanup(pBatchs);
1,638,378✔
72
}
1,638,452✔
73

74
char* ctgTaskTypeStr(CTG_TASK_TYPE type) {
4,657,421✔
75
  switch (type) {
4,657,421!
76
    case CTG_TASK_GET_QNODE:
12,927✔
77
      return "[get qnode list]";
12,927✔
78
    case CTG_TASK_GET_DNODE:
367✔
79
      return "[get dnode list]";
367✔
80
    case CTG_TASK_GET_DB_VGROUP:
789,301✔
81
      return "[get db vgroup]";
789,301✔
82
    case CTG_TASK_GET_DB_CFG:
110,831✔
83
      return "[get db cfg]";
110,831✔
84
    case CTG_TASK_GET_DB_INFO:
4,984✔
85
      return "[get db info]";
4,984✔
86
    case CTG_TASK_GET_TB_META:
20,819✔
87
      return "[get table meta]";
20,819✔
88
    case CTG_TASK_GET_TB_HASH:
×
89
      return "[get table hash]";
×
90
    case CTG_TASK_GET_TB_SMA_INDEX:
1,378✔
91
      return "[get table sma]";
1,378✔
92
    case CTG_TASK_GET_TB_CFG:
250✔
93
      return "[get table cfg]";
250✔
94
    case CTG_TASK_GET_INDEX_INFO:
×
95
      return "[get index info]";
×
96
    case CTG_TASK_GET_UDF:
96✔
97
      return "[get udf]";
96✔
98
    case CTG_TASK_GET_USER:
1,039,548✔
99
      return "[get user]";
1,039,548✔
100
    case CTG_TASK_GET_SVR_VER:
×
101
      return "[get svr ver]";
×
102
    case CTG_TASK_GET_TB_META_BATCH:
906,137✔
103
      return "[bget table meta]";
906,137✔
104
    case CTG_TASK_GET_TB_HASH_BATCH:
908,641✔
105
      return "[bget table hash]";
908,641✔
106
    case CTG_TASK_GET_TB_TAG:
3✔
107
      return "[get table tag]";
3✔
108
    case CTG_TASK_GET_VIEW:
862,162✔
109
      return "[get view]";
862,162✔
110
    case CTG_TASK_GET_TB_TSMA:
×
111
      return "[get table TSMA]";
×
112
    case CTG_TASK_GET_TSMA:
×
113
      return "[get TSMA]";
×
114
    case CTG_TASK_GET_TB_NAME:
×
115
      return "[bget table name]";
×
116
    default:
×
117
      return "unknown";
×
118
  }
119
}
120

121
void ctgFreeQNode(SCtgQNode* node) {
×
122
  if (NULL == node) {
×
123
    return;
×
124
  }
125

126
  if (node->op) {
×
127
    taosMemoryFree(node->op->data);
×
128
    taosMemoryFree(node->op);
×
129
  }
130

131
  taosMemoryFree(node);
×
132
}
133

134
void ctgFreeSTableIndex(void* info) {
×
135
  if (NULL == info) {
×
136
    return;
×
137
  }
138

139
  STableIndex* pInfo = (STableIndex*)info;
×
140

141
  taosArrayDestroyEx(pInfo->pIndex, tFreeSTableIndexInfo);
×
142
}
143

144
void ctgFreeSMetaData(SMetaData* pData) {
1,431,718✔
145
  taosArrayDestroy(pData->pTableMeta);
1,431,718✔
146
  pData->pTableMeta = NULL;
1,431,800✔
147

148
  /*
149
    for (int32_t i = 0; i < taosArrayGetSize(pData->pDbVgroup); ++i) {
150
      SArray** pArray = taosArrayGet(pData->pDbVgroup, i);
151
      taosArrayDestroy(*pArray);
152
    }
153
  */
154
  taosArrayDestroy(pData->pDbVgroup);
1,431,800✔
155
  pData->pDbVgroup = NULL;
1,431,795✔
156

157
  taosArrayDestroy(pData->pTableHash);
1,431,795✔
158
  pData->pTableHash = NULL;
1,431,785✔
159

160
  taosArrayDestroy(pData->pTableIndex);
1,431,785✔
161
  pData->pTableIndex = NULL;
1,431,761✔
162

163
  taosArrayDestroy(pData->pUdfList);
1,431,761✔
164
  pData->pUdfList = NULL;
1,431,770✔
165

166
  /*
167
    for (int32_t i = 0; i < taosArrayGetSize(pData->pDbCfg); ++i) {
168
      SDbCfgInfo* pInfo = taosArrayGet(pData->pDbCfg, i);
169
      taosArrayDestroy(pInfo->pRetensions);
170
    }
171
  */
172
  taosArrayDestroy(pData->pDbCfg);
1,431,770✔
173
  pData->pDbCfg = NULL;
1,431,789✔
174

175
  taosArrayDestroy(pData->pDbInfo);
1,431,789✔
176
  pData->pDbInfo = NULL;
1,431,850✔
177

178
  taosArrayDestroy(pData->pIndex);
1,431,850✔
179
  pData->pIndex = NULL;
1,431,827✔
180

181
  taosArrayDestroy(pData->pUser);
1,431,827✔
182
  pData->pUser = NULL;
1,431,828✔
183

184
  taosArrayDestroy(pData->pQnodeList);
1,431,828✔
185
  pData->pQnodeList = NULL;
1,431,812✔
186

187
  taosArrayDestroy(pData->pDnodeList);
1,431,812✔
188
  pData->pDnodeList = NULL;
1,431,802✔
189

190
  taosArrayDestroy(pData->pTableCfg);
1,431,802✔
191
  pData->pTableCfg = NULL;
1,431,780✔
192

193
  taosArrayDestroy(pData->pTableTag);
1,431,780✔
194
  pData->pTableTag = NULL;
1,431,774✔
195

196
  taosArrayDestroy(pData->pView);
1,431,774✔
197
  pData->pView = NULL;
1,431,796✔
198

199
  taosArrayDestroy(pData->pTableTsmas);
1,431,796✔
200
  pData->pTableTsmas = NULL;
1,431,803✔
201

202
  taosArrayDestroy(pData->pTsmas);
1,431,803✔
203
  pData->pTsmas = NULL;
1,431,797✔
204

205
  taosArrayDestroyEx(pData->pVSubTables, tDestroySVSubTablesRsp);
1,431,797✔
206
  pData->pVSubTables = NULL;
1,431,805✔
207

208
  taosArrayDestroyEx(pData->pVStbRefDbs, tDestroySVStbRefDbsRsp);
1,431,805✔
209
  pData->pVStbRefDbs = NULL;
1,431,818✔
210

211
  taosMemoryFreeClear(pData->pSvrVer);
1,431,818!
212
}
1,431,818✔
213

214
void ctgFreeSCtgUserAuth(SCtgUserAuth* userCache) {
23,135✔
215
  taosHashCleanup(userCache->userAuth.createdDbs);
23,135✔
216
  taosHashCleanup(userCache->userAuth.readDbs);
23,135✔
217
  taosHashCleanup(userCache->userAuth.writeDbs);
23,135✔
218
  taosHashCleanup(userCache->userAuth.readTbs);
23,135✔
219
  taosHashCleanup(userCache->userAuth.writeTbs);
23,135✔
220
  taosHashCleanup(userCache->userAuth.alterTbs);
23,135✔
221
  taosHashCleanup(userCache->userAuth.readViews);
23,135✔
222
  taosHashCleanup(userCache->userAuth.writeViews);
23,135✔
223
  taosHashCleanup(userCache->userAuth.alterViews);
23,135✔
224
  taosHashCleanup(userCache->userAuth.useDbs);
23,135✔
225
}
23,135✔
226

227
void ctgFreeMetaRent(SCtgRentMgmt* mgmt) {
101,751✔
228
  if (NULL == mgmt->slots) {
101,751!
229
    return;
×
230
  }
231

232
  for (int32_t i = 0; i < mgmt->slotNum; ++i) {
712,155✔
233
    SCtgRentSlot* slot = &mgmt->slots[i];
610,404✔
234
    if (slot->meta) {
610,404✔
235
      taosArrayDestroy(slot->meta);
36,492✔
236
      slot->meta = NULL;
36,492✔
237
    }
238
  }
239

240
  taosMemoryFreeClear(mgmt->slots);
101,751!
241
  mgmt->rentCacheSize = 0;
101,753✔
242
}
243

244
void ctgFreeStbMetaCache(SCtgDBCache* dbCache) {
36,788✔
245
  if (NULL == dbCache->stbCache) {
36,788!
246
    return;
×
247
  }
248

249
  int32_t stbNum = taosHashGetSize(dbCache->stbCache);
36,788✔
250
  taosHashCleanup(dbCache->stbCache);
36,788✔
251
  dbCache->stbCache = NULL;
36,788✔
252
}
253

254
void ctgFreeTbCacheImpl(SCtgTbCache* pCache, bool lock) {
283,498✔
255
  if (pCache->pMeta) {
283,498✔
256
    if (lock) {
283,497✔
257
      CTG_LOCK(CTG_WRITE, &pCache->metaLock);
27,900!
258
    }
259
    taosMemoryFreeClear(pCache->pMeta);
283,497!
260
    if (lock) {
283,497✔
261
      CTG_UNLOCK(CTG_WRITE, &pCache->metaLock);
27,900!
262
    }
263
  }
264

265
  if (pCache->pIndex) {
283,498✔
266
    if (lock) {
1!
267
      CTG_LOCK(CTG_WRITE, &pCache->indexLock);
×
268
    }
269
    taosArrayDestroyEx(pCache->pIndex->pIndex, tFreeSTableIndexInfo);
1✔
270
    taosMemoryFreeClear(pCache->pIndex);
1!
271
    if (lock) {
1!
272
      CTG_UNLOCK(CTG_WRITE, &pCache->indexLock);
×
273
    }
274
  }
275
}
283,498✔
276

277
void ctgFreeViewCacheImpl(SCtgViewCache* pCache, bool lock) {
317✔
278
  if (lock) {
317✔
279
    CTG_LOCK(CTG_WRITE, &pCache->viewLock);
243!
280
  }
281
  if (pCache->pMeta) {
317!
282
    ctgFreeSViewMeta(pCache->pMeta);
317✔
283
    taosMemoryFreeClear(pCache->pMeta);
317!
284
  }
285
  if (lock) {
317✔
286
    CTG_UNLOCK(CTG_WRITE, &pCache->viewLock);
243!
287
  }
288
}
317✔
289

290
void ctgFreeViewCache(SCtgDBCache* dbCache) {
36,788✔
291
  if (NULL == dbCache->viewCache) {
36,788!
292
    return;
×
293
  }
294

295
  SCtgViewCache* pCache = taosHashIterate(dbCache->viewCache, NULL);
36,788✔
296
  while (NULL != pCache) {
36,862✔
297
    ctgFreeViewCacheImpl(pCache, false);
74✔
298
    pCache = taosHashIterate(dbCache->viewCache, pCache);
74✔
299
  }
300
  taosHashCleanup(dbCache->viewCache);
36,788✔
301
  dbCache->viewCache = NULL;
36,788✔
302
}
303

304
void ctgFreeTbCache(SCtgDBCache* dbCache) {
36,788✔
305
  if (NULL == dbCache->tbCache) {
36,788!
306
    return;
×
307
  }
308

309
  SCtgTbCache* pCache = taosHashIterate(dbCache->tbCache, NULL);
36,788✔
310
  while (NULL != pCache) {
292,386✔
311
    ctgFreeTbCacheImpl(pCache, false);
255,598✔
312
    pCache = taosHashIterate(dbCache->tbCache, pCache);
255,598✔
313
  }
314
  taosHashCleanup(dbCache->tbCache);
36,788✔
315
  dbCache->tbCache = NULL;
36,788✔
316
}
317

318
void ctgFreeTSMACacheImpl(SCtgTSMACache* pCache, bool lock) {
34✔
319
  if (lock) {
34!
320
    CTG_LOCK(CTG_WRITE, &pCache->tsmaLock);
×
321
  }
322
  if (pCache->pTsmas) {
34✔
323
    taosArrayDestroyP(pCache->pTsmas, tFreeAndClearTableTSMAInfo);
18✔
324
    pCache->pTsmas = NULL;
18✔
325
  }
326
  if (lock) {
34!
327
    CTG_UNLOCK(CTG_WRITE, &pCache->tsmaLock);
×
328
  }
329
}
34✔
330

331
void ctgFreeTSMACache(SCtgDBCache* dbCache) {
36,788✔
332
  if (!dbCache) return;
36,788!
333

334
  SCtgTSMACache* pCache = taosHashIterate(dbCache->tsmaCache, NULL);
36,788✔
335
  while (pCache) {
36,822✔
336
    ctgFreeTSMACacheImpl(pCache, false);
34✔
337
    pCache = taosHashIterate(dbCache->tsmaCache, pCache);
34✔
338
  }
339
  taosHashCleanup(dbCache->tsmaCache);
36,788✔
340
  dbCache->tsmaCache = NULL;
36,788✔
341
}
342

343
void ctgFreeVgInfoCache(SCtgDBCache* dbCache) { freeVgInfo(dbCache->vgCache.vgInfo); }
36,788✔
344
void ctgFreeCfgInfoCache(SCtgDBCache* dbCache) { freeDbCfgInfo(dbCache->cfgCache.cfgInfo); }
36,788✔
345

346
void ctgFreeDbCache(SCtgDBCache* dbCache) {
36,788✔
347
  if (NULL == dbCache) {
36,788!
348
    return;
×
349
  }
350

351
  ctgFreeVgInfoCache(dbCache);
36,788✔
352
  ctgFreeCfgInfoCache(dbCache);
36,788✔
353
  ctgFreeStbMetaCache(dbCache);
36,788✔
354
  ctgFreeTbCache(dbCache);
36,788✔
355
  ctgFreeViewCache(dbCache);
36,788✔
356
  ctgFreeTSMACache(dbCache);
36,788✔
357
}
358

359
void ctgFreeInstDbCache(SHashObj* pDbCache) {
25,442✔
360
  if (NULL == pDbCache) {
25,442!
361
    return;
×
362
  }
363

364
  int32_t dbNum = taosHashGetSize(pDbCache);
25,442✔
365

366
  void* pIter = taosHashIterate(pDbCache, NULL);
25,441✔
367
  while (pIter) {
53,656✔
368
    SCtgDBCache* dbCache = pIter;
28,217✔
369
    atomic_store_8(&dbCache->deleted, 1);
28,217✔
370
    ctgFreeDbCache(dbCache);
28,217✔
371

372
    pIter = taosHashIterate(pDbCache, pIter);
28,217✔
373
  }
374

375
  taosHashCleanup(pDbCache);
25,439✔
376
}
377

378
void ctgFreeInstUserCache(SHashObj* pUserCache) {
25,445✔
379
  if (NULL == pUserCache) {
25,445!
380
    return;
×
381
  }
382

383
  int32_t userNum = taosHashGetSize(pUserCache);
25,445✔
384

385
  void* pIter = taosHashIterate(pUserCache, NULL);
25,444✔
386
  while (pIter) {
48,550✔
387
    SCtgUserAuth* userCache = pIter;
23,106✔
388
    ctgFreeSCtgUserAuth(userCache);
23,106✔
389

390
    pIter = taosHashIterate(pUserCache, pIter);
23,106✔
391
  }
392

393
  taosHashCleanup(pUserCache);
25,444✔
394
}
395

396
void ctgFreeHandleImpl(SCatalog* pCtg) {
42✔
397
  ctgFreeMetaRent(&pCtg->dbRent);
42✔
398
  ctgFreeMetaRent(&pCtg->stbRent);
57✔
399
  ctgFreeMetaRent(&pCtg->viewRent);
59✔
400
  ctgFreeMetaRent(&pCtg->tsmaRent);
58✔
401

402
  ctgFreeInstDbCache(pCtg->dbCache);
58✔
403
  ctgFreeInstUserCache(pCtg->userCache);
62✔
404

405
  taosMemoryFree(pCtg);
62!
406
}
62✔
407

408
int32_t ctgRemoveCacheUser(SCatalog* pCtg, SCtgUserAuth* pUser, const char* user) {
29✔
409
  CTG_LOCK(CTG_WRITE, &pUser->lock);
29!
410
  ctgFreeSCtgUserAuth(pUser);
29✔
411
  CTG_UNLOCK(CTG_WRITE, &pUser->lock);
29!
412

413
  if (taosHashRemove(pCtg->userCache, user, strlen(user)) == 0) {
29!
414
    return 0;  // user found and removed
29✔
415
  }
416

417
  return -1;
×
418
}
419

420
void ctgFreeHandle(SCatalog* pCtg) {
17,363✔
421
  if (NULL == pCtg) {
17,363!
422
    return;
×
423
  }
424

425
  int64_t clusterId = pCtg->clusterId;
17,363✔
426

427
  ctgFreeMetaRent(&pCtg->dbRent);
17,363✔
428
  ctgFreeMetaRent(&pCtg->stbRent);
17,363✔
429
  ctgFreeMetaRent(&pCtg->viewRent);
17,363✔
430
  ctgFreeMetaRent(&pCtg->tsmaRent);
17,363✔
431

432
  ctgFreeInstDbCache(pCtg->dbCache);
17,363✔
433
  ctgFreeInstUserCache(pCtg->userCache);
17,363✔
434

435
  CTG_STAT_NUM_DEC(CTG_CI_CLUSTER, 1);
17,363✔
436

437
  taosMemoryFree(pCtg);
17,363!
438

439
  ctgInfo("clusterId:0x%" PRIx64 ", handle freed", clusterId);
17,363!
440
}
441

442
void ctgClearHandleMeta(SCatalog* pCtg, int64_t* pClearedSize, int64_t* pCleardNum, bool* roundDone) {
×
443
  int64_t cacheSize = 0;
×
444
  int32_t code = 0;
×
445
  void*   pIter = taosHashIterate(pCtg->dbCache, NULL);
×
446
  while (pIter) {
×
447
    SCtgDBCache* dbCache = pIter;
×
448

449
    SCtgTbCache* pCache = taosHashIterate(dbCache->tbCache, NULL);
×
450
    while (NULL != pCache) {
×
451
      size_t len = 0;
×
452
      void*  key = taosHashGetKey(pCache, &len);
×
453

454
      if (pCache->pMeta && TSDB_SUPER_TABLE == pCache->pMeta->tableType) {
×
455
        pCache = taosHashIterate(dbCache->tbCache, pCache);
×
456
        continue;
×
457
      }
458

459
      code = taosHashRemove(dbCache->tbCache, key, len);
×
460
      if (code) {
×
461
        qError("taosHashRemove table cache failed, key:%s, len:%d, error:%s", (char*)key, (int32_t)len,
×
462
               tstrerror(code));
463
      }
464

465
      cacheSize =
×
466
          len + sizeof(SCtgTbCache) + ctgGetTbMetaCacheSize(pCache->pMeta) + ctgGetTbIndexCacheSize(pCache->pIndex);
×
467
      (void)atomic_sub_fetch_64(&dbCache->dbCacheSize, cacheSize);
×
468
      *pClearedSize += cacheSize;
×
469
      (*pCleardNum)++;
×
470

471
      if (pCache->pMeta) {
×
472
        CTG_META_NUM_DEC(pCache->pMeta->tableType);
×
473
      }
474

475
      ctgFreeTbCacheImpl(pCache, true);
×
476

477
      if (*pCleardNum >= CTG_CLEAR_CACHE_ROUND_TB_NUM) {
×
478
        taosHashCancelIterate(dbCache->tbCache, pCache);
×
479
        goto _return;
×
480
      }
481

482
      pCache = taosHashIterate(dbCache->tbCache, pCache);
×
483
    }
484

485
    pIter = taosHashIterate(pCtg->dbCache, pIter);
×
486
  }
487

488
_return:
×
489

490
  if (*pCleardNum >= CTG_CLEAR_CACHE_ROUND_TB_NUM) {
×
491
    *roundDone = true;
×
492
  }
493
}
×
494

495
void ctgClearAllHandleMeta(int64_t* clearedSize, int64_t* clearedNum, bool* roundDone) {
×
496
  SCatalog* pCtg = NULL;
×
497

498
  void* pIter = taosHashIterate(gCtgMgmt.pCluster, NULL);
×
499
  while (pIter) {
×
500
    pCtg = *(SCatalog**)pIter;
×
501

502
    if (pCtg) {
×
503
      ctgClearHandleMeta(pCtg, clearedSize, clearedNum, roundDone);
×
504
      if (*roundDone) {
×
505
        taosHashCancelIterate(gCtgMgmt.pCluster, pIter);
×
506
        break;
×
507
      }
508
    }
509

510
    pIter = taosHashIterate(gCtgMgmt.pCluster, pIter);
×
511
  }
512
}
×
513

514
void ctgClearHandle(SCatalog* pCtg) {
8,020✔
515
  if (NULL == pCtg) {
8,020!
516
    return;
×
517
  }
518

519
  int64_t clusterId = pCtg->clusterId;
8,020✔
520

521
  ctgFreeMetaRent(&pCtg->dbRent);
8,020✔
522
  ctgFreeMetaRent(&pCtg->stbRent);
8,020✔
523
  ctgFreeMetaRent(&pCtg->viewRent);
8,020✔
524
  ctgFreeMetaRent(&pCtg->tsmaRent);
8,020✔
525

526
  ctgFreeInstDbCache(pCtg->dbCache);
8,020✔
527
  ctgFreeInstUserCache(pCtg->userCache);
8,020✔
528

529
  (void)ctgMetaRentInit(&pCtg->dbRent, gCtgMgmt.cfg.dbRentSec, CTG_RENT_DB, sizeof(SDbCacheInfo));
8,020✔
530
  (void)ctgMetaRentInit(&pCtg->stbRent, gCtgMgmt.cfg.stbRentSec, CTG_RENT_STABLE, sizeof(SSTableVersion));
8,020✔
531
  (void)ctgMetaRentInit(&pCtg->viewRent, gCtgMgmt.cfg.viewRentSec, CTG_RENT_VIEW, sizeof(SViewVersion));
8,020✔
532
  (void)ctgMetaRentInit(&pCtg->tsmaRent, gCtgMgmt.cfg.tsmaRentSec, CTG_RENT_TSMA, sizeof(STSMAVersion));
8,020✔
533

534
  pCtg->dbCache = taosHashInit(gCtgMgmt.cfg.maxDBCacheNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false,
8,020✔
535
                               HASH_ENTRY_LOCK);
536
  if (NULL == pCtg->dbCache) {
8,020!
537
    qError("taosHashInit %d dbCache failed", CTG_DEFAULT_CACHE_DB_NUMBER);
×
538
  }
539

540
  pCtg->userCache = taosHashInit(gCtgMgmt.cfg.maxUserCacheNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false,
8,020✔
541
                                 HASH_ENTRY_LOCK);
542
  if (NULL == pCtg->userCache) {
8,020!
543
    ctgError("taosHashInit %d user cache failed", gCtgMgmt.cfg.maxUserCacheNum);
×
544
  }
545

546
  TAOS_MEMSET(pCtg->cacheStat.cacheNum, 0, sizeof(pCtg->cacheStat.cacheNum));
8,020✔
547

548
  CTG_STAT_RT_INC(numOfOpClearCache, 1);
8,020✔
549

550
  ctgInfo("clusterId:0x%" PRIx64 ", handle cleared", clusterId);
8,020!
551
}
552

553
void ctgFreeSUseDbOutput(SUseDbOutput* pOutput) {
129,809✔
554
  if (NULL == pOutput) {
129,809!
555
    return;
×
556
  }
557

558
  if (pOutput->dbVgroup) {
129,809✔
559
    freeVgInfo(pOutput->dbVgroup);
95,845✔
560
  }
561

562
  taosMemoryFree(pOutput);
129,810!
563
}
564

565
void ctgFreeMsgCtx(SCtgMsgCtx* pCtx) {
7,838,538✔
566
  taosMemoryFreeClear(pCtx->target);
7,838,538!
567
  if (NULL == pCtx->out) {
7,838,543✔
568
    return;
7,482,187✔
569
  }
570

571
  switch (pCtx->reqType) {
356,356!
572
    case TDMT_MND_GET_DB_CFG: {
844✔
573
      SDbCfgInfo* pOut = (SDbCfgInfo*)pCtx->out;
844✔
574
      taosArrayDestroy(pOut->pRetensions);
844✔
575
      taosMemoryFreeClear(pCtx->out);
844!
576
      break;
844✔
577
    }
578
    case TDMT_MND_USE_DB: {
129,810✔
579
      SUseDbOutput* pOut = (SUseDbOutput*)pCtx->out;
129,810✔
580
      ctgFreeSUseDbOutput(pOut);
129,810✔
581
      pCtx->out = NULL;
129,810✔
582
      break;
129,810✔
583
    }
584
    case TDMT_MND_GET_INDEX: {
×
585
      SIndexInfo* pOut = (SIndexInfo*)pCtx->out;
×
586
      taosMemoryFreeClear(pCtx->out);
×
587
      break;
×
588
    }
589
    case TDMT_MND_QNODE_LIST: {
×
590
      SArray* pOut = (SArray*)pCtx->out;
×
591
      taosArrayDestroy(pOut);
×
592
      pCtx->out = NULL;
×
593
      break;
×
594
    }
595
    case TDMT_VND_TABLE_META:
173,322✔
596
    case TDMT_MND_TABLE_META:
597
    case TDMT_VND_TABLE_NAME: {
598
      STableMetaOutput* pOut = (STableMetaOutput*)pCtx->out;
173,322✔
599
      taosMemoryFree(pOut->tbMeta);
173,322!
600
      taosMemoryFree(pOut->vctbMeta);
173,322!
601
      taosMemoryFreeClear(pCtx->out);
173,321!
602
      break;
173,323✔
603
    }
604
    case TDMT_MND_GET_TABLE_INDEX: {
1,378✔
605
      STableIndex* pOut = (STableIndex*)pCtx->out;
1,378✔
606
      if (pOut) {
1,378!
607
        taosArrayDestroyEx(pOut->pIndex, tFreeSTableIndexInfo);
1,378✔
608
        taosMemoryFreeClear(pCtx->out);
1,378!
609
      }
610
      break;
1,378✔
611
    }
612
    case TDMT_VND_TABLE_CFG:
3✔
613
    case TDMT_MND_TABLE_CFG: {
614
      STableCfgRsp* pOut = (STableCfgRsp*)pCtx->out;
3✔
615
      tFreeSTableCfgRsp(pOut);
3✔
616
      taosMemoryFreeClear(pCtx->out);
3!
617
      break;
3✔
618
    }
619
    case TDMT_MND_RETRIEVE_FUNC: {
10✔
620
      SFuncInfo* pOut = (SFuncInfo*)pCtx->out;
10✔
621
      taosMemoryFree(pOut->pCode);
10!
622
      taosMemoryFree(pOut->pComment);
10!
623
      taosMemoryFreeClear(pCtx->out);
10!
624
      break;
10✔
625
    }
626
    case TDMT_MND_GET_USER_AUTH: {
14✔
627
      SGetUserAuthRsp* pOut = (SGetUserAuthRsp*)pCtx->out;
14✔
628
      taosHashCleanup(pOut->createdDbs);
14✔
629
      taosHashCleanup(pOut->readDbs);
14✔
630
      taosHashCleanup(pOut->writeDbs);
14✔
631
      taosHashCleanup(pOut->readTbs);
14✔
632
      taosHashCleanup(pOut->writeTbs);
14✔
633
      taosHashCleanup(pOut->alterTbs);
14✔
634
      taosHashCleanup(pOut->readViews);
14✔
635
      taosHashCleanup(pOut->writeViews);
14✔
636
      taosHashCleanup(pOut->alterViews);
14✔
637
      taosHashCleanup(pOut->useDbs);
14✔
638
      taosMemoryFreeClear(pCtx->out);
14!
639
      break;
14✔
640
    }
641
    case TDMT_MND_VIEW_META: {
45,662✔
642
      if (NULL != pCtx->out) {
45,662!
643
        SViewMetaRsp* pOut = *(SViewMetaRsp**)pCtx->out;
45,662✔
644
        if (NULL != pOut) {
45,662!
645
          tFreeSViewMetaRsp(pOut);
×
646
          taosMemoryFree(pOut);
×
647
        }
648
        taosMemoryFreeClear(pCtx->out);
45,662!
649
      }
650
      break;
45,662✔
651
    }
652
    case TDMT_MND_GET_TSMA:
149✔
653
    case TDMT_MND_GET_TABLE_TSMA: {
654
      if (pCtx->out) {
149!
655
        tFreeTableTSMAInfoRsp(pCtx->out);
149✔
656
        taosMemoryFreeClear(pCtx->out);
149!
657
      }
658
      break;
149✔
659
    }
660
    case TDMT_VND_GET_STREAM_PROGRESS: {
5,464✔
661
      if (pCtx->out) {
5,464!
662
        taosMemoryFreeClear(pCtx->out);
5,464!
663
      }
664
      break;
5,464✔
665
    }
666
    case TDMT_VND_VSUBTABLES_META: {
×
667
      taosMemoryFreeClear(pCtx->target);
×
668
      break;
×
669
    }
670
    case TDMT_VND_VSTB_REF_DBS: {
×
671
      taosMemoryFreeClear(pCtx->target);
×
672
      break;
×
673
    }
674
    default:
×
675
      qError("invalid reqType %d", pCtx->reqType);
×
676
      break;
×
677
  }
678
}
679

680
void ctgFreeTbMetasMsgCtx(SCtgMsgCtx* pCtx) {
121,053✔
681
  ctgFreeMsgCtx(pCtx);
121,053✔
682
  if (pCtx->lastOut) {
121,054✔
683
    ctgFreeSTableMetaOutput((STableMetaOutput*)pCtx->lastOut);
2,058✔
684
    pCtx->lastOut = NULL;
2,058✔
685
  }
686
}
121,054✔
687

688
void ctgFreeSTableMetaOutput(STableMetaOutput* pOutput) {
3,990✔
689
  if (NULL == pOutput) {
3,990!
690
    return;
×
691
  }
692

693
  taosMemoryFree(pOutput->tbMeta);
3,990!
694
  taosMemoryFree(pOutput->vctbMeta);
3,990!
695
  taosMemoryFree(pOutput);
3,990!
696
}
697

698
void ctgResetTbMetaTask(SCtgTask* pTask) {
×
699
  SCtgTbMetaCtx* taskCtx = (SCtgTbMetaCtx*)pTask->taskCtx;
×
700
  TAOS_MEMSET(&taskCtx->tbInfo, 0, sizeof(taskCtx->tbInfo));
×
701
  taskCtx->flag = CTG_FLAG_UNKNOWN_STB;
×
702

703
  if (pTask->msgCtx.lastOut) {
×
704
    ctgFreeSTableMetaOutput((STableMetaOutput*)pTask->msgCtx.lastOut);
×
705
    pTask->msgCtx.lastOut = NULL;
×
706
  }
707
  if (pTask->msgCtx.out) {
×
708
    ctgFreeSTableMetaOutput((STableMetaOutput*)pTask->msgCtx.out);
×
709
    pTask->msgCtx.out = NULL;
×
710
  }
711
  taosMemoryFreeClear(pTask->msgCtx.target);
×
712
  taosMemoryFreeClear(pTask->res);
×
713
}
×
714

715
void ctgFreeBatchMeta(void* meta) {
1,448,873✔
716
  if (NULL == meta) {
1,448,873!
717
    return;
×
718
  }
719

720
  SMetaRes* pRes = (SMetaRes*)meta;
1,448,873✔
721
  taosMemoryFreeClear(pRes->pRes);
1,448,873!
722
}
723

724
void ctgFreeBatchHash(void* hash) {
1,442,970✔
725
  if (NULL == hash) {
1,442,970!
726
    return;
×
727
  }
728

729
  SMetaRes* pRes = (SMetaRes*)hash;
1,442,970✔
730
  taosMemoryFreeClear(pRes->pRes);
1,442,970!
731
}
732

733
void ctgFreeViewMetaRes(void* res) {
1,332,190✔
734
  if (NULL == res) {
1,332,190!
735
    return;
×
736
  }
737

738
  SMetaRes* pRes = (SMetaRes*)res;
1,332,190✔
739
  if (NULL != pRes->pRes) {
1,332,190✔
740
    SViewMeta* pMeta = (SViewMeta*)pRes->pRes;
589✔
741
    ctgFreeSViewMeta(pMeta);
589✔
742
    taosMemoryFreeClear(pRes->pRes);
589!
743
  }
744
}
745

746
void ctgFreeTbTSMARes(void* res) {
2,452✔
747
  if (!res) {
2,452!
748
    return;
×
749
  }
750

751
  SMetaRes* pRes = res;
2,452✔
752
  if (pRes->pRes) {
2,452✔
753
    STableTSMAInfoRsp* pTsmaRsp = pRes->pRes;
2,010✔
754
    tFreeTableTSMAInfoRsp(pTsmaRsp);
2,010✔
755
    taosMemoryFree(pTsmaRsp);
2,010!
756
    pRes->pRes = NULL;
2,010✔
757
  }
758
}
759

760
void ctgFreeJsonTagVal(void* val) {
×
761
  if (NULL == val) {
×
762
    return;
×
763
  }
764

765
  STagVal* pVal = (STagVal*)val;
×
766

767
  if (TSDB_DATA_TYPE_JSON == pVal->type) {
×
768
    taosMemoryFree(pVal->pData);
×
769
  }
770
}
771

772
void ctgFreeTaskRes(CTG_TASK_TYPE type, void** pRes) {
7,229,460✔
773
  switch (type) {
7,229,460!
774
    case CTG_TASK_GET_QNODE:
1,051,552✔
775
    case CTG_TASK_GET_DNODE:
776
    case CTG_TASK_GET_DB_VGROUP: {
777
      taosArrayDestroy((SArray*)*pRes);
1,051,552✔
778
      *pRes = NULL;
1,051,588✔
779
      break;
1,051,588✔
780
    }
781
    case CTG_TASK_GET_DB_CFG: {
186,388✔
782
      if (*pRes) {
186,388✔
783
        SDbCfgInfo* pInfo = (SDbCfgInfo*)*pRes;
185,552✔
784
        taosArrayDestroy(pInfo->pRetensions);
185,552✔
785
        taosMemoryFreeClear(*pRes);
185,597!
786
      }
787
      break;
186,455✔
788
    }
789
    case CTG_TASK_GET_TB_SMA_INDEX: {
1,378✔
790
      taosArrayDestroyEx(*pRes, tFreeSTableIndexInfo);
1,378✔
791
      *pRes = NULL;
1,378✔
792
      break;
1,378✔
793
    }
794
    case CTG_TASK_GET_TB_CFG: {
365✔
795
      if (*pRes) {
365✔
796
        STableCfg* pInfo = (STableCfg*)*pRes;
356✔
797
        tFreeSTableCfgRsp(pInfo);
356✔
798
        taosMemoryFreeClear(*pRes);
356!
799
      }
800
      break;
365✔
801
    }
802
    case CTG_TASK_GET_USER: {
1,417,240✔
803
      if (*pRes) {
1,417,240✔
804
        SUserAuthRes* pAuth = (SUserAuthRes*)*pRes;
1,417,220✔
805
        for (int32_t i = 0; i < AUTH_RES_MAX_VALUE; ++i) {
4,251,570✔
806
          nodesDestroyNode(pAuth->pCond[i]);
2,834,349✔
807
        }
808
        taosMemoryFreeClear(*pRes);
1,417,221!
809
      }
810
      break;
1,417,249✔
811
    }
812
    case CTG_TASK_GET_TB_HASH:
44,841✔
813
    case CTG_TASK_GET_DB_INFO:
814
    case CTG_TASK_GET_INDEX_INFO:
815
    case CTG_TASK_GET_UDF:
816
    case CTG_TASK_GET_SVR_VER:
817
    case CTG_TASK_GET_TB_META: {
818
      taosMemoryFreeClear(*pRes);
44,841!
819
      break;
44,855✔
820
    }
821
    case CTG_TASK_GET_TB_TAG: {
3✔
822
      if (1 == taosArrayGetSize(*pRes)) {
3!
823
        taosArrayDestroyEx(*pRes, ctgFreeJsonTagVal);
×
824
      } else {
825
        taosArrayDestroy(*pRes);
3✔
826
      }
827
      *pRes = NULL;
3✔
828
      break;
3✔
829
    }
830
    case CTG_TASK_GET_TB_META_BATCH: {
1,246,697✔
831
      SArray* pArray = (SArray*)*pRes;
1,246,697✔
832
      int32_t num = taosArrayGetSize(pArray);
1,246,697✔
833
      for (int32_t i = 0; i < num; ++i) {
2,694,143✔
834
        ctgFreeBatchMeta(taosArrayGet(pArray, i));
1,447,399✔
835
      }
836
      *pRes = NULL;  // no need to free it
1,246,744✔
837
      break;
1,246,744✔
838
    }
839
    case CTG_TASK_GET_TB_HASH_BATCH: {
1,249,091✔
840
      SArray* pArray = (SArray*)*pRes;
1,249,091✔
841
      int32_t num = taosArrayGetSize(pArray);
1,249,091✔
842
      for (int32_t i = 0; i < num; ++i) {
2,692,153✔
843
        ctgFreeBatchHash(taosArrayGet(pArray, i));
1,443,041✔
844
      }
845
      *pRes = NULL;  // no need to free it
1,249,112✔
846
      break;
1,249,112✔
847
    }
848
    case CTG_TASK_GET_VIEW: {
1,172,384✔
849
      SArray* pArray = (SArray*)*pRes;
1,172,384✔
850
      int32_t num = taosArrayGetSize(pArray);
1,172,384✔
851
      for (int32_t i = 0; i < num; ++i) {
2,504,593✔
852
        ctgFreeViewMetaRes(taosArrayGet(pArray, i));
1,332,224✔
853
      }
854
      *pRes = NULL;  // no need to free it
1,172,369✔
855
      break;
1,172,369✔
856
    }
857
    case CTG_TASK_GET_TSMA:
2,683✔
858
    case CTG_TASK_GET_TB_TSMA: {
859
      SArray* pArr = (SArray*)*pRes;
2,683✔
860
      int32_t num = taosArrayGetSize(pArr);
2,683✔
861
      for (int32_t i = 0; i < num; ++i) {
5,135✔
862
        ctgFreeTbTSMARes(taosArrayGet(pArr, i));
2,452✔
863
      }
864
      *pRes = NULL;
2,683✔
865
      break;
2,683✔
866
    }
867
    case CTG_TASK_GET_TB_NAME: {
675✔
868
      SArray* pArray = (SArray*)*pRes;
675✔
869
      int32_t num = taosArrayGetSize(pArray);
675✔
870
      for (int32_t i = 0; i < num; ++i) {
2,246✔
871
        ctgFreeBatchMeta(taosArrayGet(pArray, i));
1,571✔
872
      }
873
      *pRes = NULL;  // no need to free it
675✔
874
      break;
675✔
875
    }
876
    case CTG_TASK_GET_V_SUBTABLES:
856,899✔
877
    case CTG_TASK_GET_V_STBREFDBS: {
878
      break;
856,899✔
879
    }
880
    default:
×
881
      qError("invalid task type %d", type);
×
882
      break;
×
883
  }
884
}
7,230,375✔
885

886
void ctgFreeSubTaskRes(CTG_TASK_TYPE type, void** pRes) {
459✔
887
  switch (type) {
459!
888
    case CTG_TASK_GET_QNODE:
×
889
    case CTG_TASK_GET_DNODE: {
890
      taosArrayDestroy((SArray*)*pRes);
×
891
      *pRes = NULL;
×
892
      break;
×
893
    }
894
    case CTG_TASK_GET_DB_VGROUP: {
1✔
895
      if (*pRes) {
1!
896
        SDBVgInfo* pInfo = (SDBVgInfo*)*pRes;
1✔
897
        freeVgInfo(pInfo);
1✔
898
      }
899
      break;
1✔
900
    }
901
    case CTG_TASK_GET_DB_CFG: {
×
902
      if (*pRes) {
×
903
        SDbCfgInfo* pInfo = (SDbCfgInfo*)*pRes;
×
904
        taosArrayDestroy(pInfo->pRetensions);
×
905
        taosMemoryFreeClear(*pRes);
×
906
      }
907
      break;
×
908
    }
909
    case CTG_TASK_GET_TB_SMA_INDEX: {
×
910
      taosArrayDestroyEx(*pRes, tFreeSTableIndexInfo);
×
911
      *pRes = NULL;
×
912
      break;
×
913
    }
914
    case CTG_TASK_GET_TB_CFG: {
×
915
      if (*pRes) {
×
916
        STableCfg* pInfo = (STableCfg*)*pRes;
×
917
        tFreeSTableCfgRsp(pInfo);
×
918
        taosMemoryFreeClear(*pRes);
×
919
      }
920
      break;
×
921
    }
922
    case CTG_TASK_GET_TB_META:
458✔
923
    case CTG_TASK_GET_DB_INFO:
924
    case CTG_TASK_GET_TB_HASH:
925
    case CTG_TASK_GET_INDEX_INFO:
926
    case CTG_TASK_GET_UDF:
927
    case CTG_TASK_GET_SVR_VER:
928
    case CTG_TASK_GET_USER: {
929
      taosMemoryFreeClear(*pRes);
458!
930
      break;
458✔
931
    }
932
    case CTG_TASK_GET_TB_META_BATCH: {
×
933
      taosArrayDestroyEx(*pRes, ctgFreeBatchMeta);
×
934
      *pRes = NULL;
×
935
      break;
×
936
    }
937
    case CTG_TASK_GET_TB_HASH_BATCH: {
×
938
      taosArrayDestroyEx(*pRes, ctgFreeBatchHash);
×
939
      *pRes = NULL;
×
940
      break;
×
941
    }
942
    case CTG_TASK_GET_TB_NAME: {
×
943
      taosArrayDestroyEx(*pRes, ctgFreeBatchMeta);
×
944
      *pRes = NULL;
×
945
      break;
×
946
    }
947
    case CTG_TASK_GET_V_SUBTABLES:
×
948
    case CTG_TASK_GET_V_STBREFDBS: {
949

950
    }
951
    default:
952
      qError("invalid task type %d", type);
×
953
      break;
×
954
  }
955
}
459✔
956

957
void ctgClearSubTaskRes(SCtgSubRes* pRes) {
7,269,755✔
958
  pRes->code = 0;
7,269,755✔
959

960
  if (NULL == pRes->res) {
7,269,755✔
961
    return;
7,269,363✔
962
  }
963

964
  ctgFreeSubTaskRes(pRes->type, &pRes->res);
392✔
965
}
966

967
void ctgFreeTaskCtx(SCtgTask* pTask) {
7,233,433✔
968
  switch (pTask->type) {
7,233,433!
969
    case CTG_TASK_GET_TB_META: {
36,189✔
970
      SCtgTbMetaCtx* taskCtx = (SCtgTbMetaCtx*)pTask->taskCtx;
36,189✔
971
      taosMemoryFreeClear(taskCtx->pName);
36,189!
972
      if (pTask->msgCtx.lastOut) {
36,189✔
973
        ctgFreeSTableMetaOutput((STableMetaOutput*)pTask->msgCtx.lastOut);
1,932✔
974
        pTask->msgCtx.lastOut = NULL;
1,932✔
975
      }
976
      taosMemoryFreeClear(pTask->taskCtx);
36,189!
977
      break;
36,189✔
978
    }
979
    case CTG_TASK_GET_TB_META_BATCH: {
1,247,253✔
980
      SCtgTbMetasCtx* taskCtx = (SCtgTbMetasCtx*)pTask->taskCtx;
1,247,253✔
981
      taosArrayDestroyEx(taskCtx->pResList, ctgFreeBatchMeta);
1,247,253✔
982
      taosArrayDestroy(taskCtx->pFetchs);
1,247,231✔
983
      // NO NEED TO FREE pNames
984

985
      taosArrayDestroyEx(pTask->msgCtxs, (FDelete)ctgFreeTbMetasMsgCtx);
1,247,270✔
986

987
      if (pTask->msgCtx.lastOut) {
1,247,310!
988
        ctgFreeSTableMetaOutput((STableMetaOutput*)pTask->msgCtx.lastOut);
×
989
        pTask->msgCtx.lastOut = NULL;
×
990
      }
991
      taosMemoryFreeClear(pTask->taskCtx);
1,247,310!
992
      break;
1,247,306✔
993
    }
994
    case CTG_TASK_GET_TB_HASH: {
×
995
      SCtgTbHashCtx* taskCtx = (SCtgTbHashCtx*)pTask->taskCtx;
×
996
      taosMemoryFreeClear(taskCtx->pName);
×
997
      taosMemoryFreeClear(pTask->taskCtx);
×
998
      break;
×
999
    }
1000
    case CTG_TASK_GET_TB_HASH_BATCH: {
1,249,677✔
1001
      SCtgTbHashsCtx* taskCtx = (SCtgTbHashsCtx*)pTask->taskCtx;
1,249,677✔
1002
      taosArrayDestroyEx(taskCtx->pResList, ctgFreeBatchHash);
1,249,677✔
1003
      taosArrayDestroy(taskCtx->pFetchs);
1,249,682✔
1004
      // NO NEED TO FREE pNames
1005

1006
      taosArrayDestroyEx(pTask->msgCtxs, (FDelete)ctgFreeMsgCtx);
1,249,683✔
1007

1008
      taosMemoryFreeClear(pTask->taskCtx);
1,249,690!
1009
      break;
1,249,701✔
1010
    }
1011
    case CTG_TASK_GET_TB_SMA_INDEX: {
1,378✔
1012
      SCtgTbIndexCtx* taskCtx = (SCtgTbIndexCtx*)pTask->taskCtx;
1,378✔
1013
      taosMemoryFreeClear(taskCtx->pName);
1,378!
1014
      taosMemoryFreeClear(pTask->taskCtx);
1,378!
1015
      break;
1,378✔
1016
    }
1017
    case CTG_TASK_GET_TB_CFG: {
365✔
1018
      SCtgTbCfgCtx* taskCtx = (SCtgTbCfgCtx*)pTask->taskCtx;
365✔
1019
      taosMemoryFreeClear(taskCtx->pName);
365!
1020
      taosMemoryFreeClear(taskCtx->pVgInfo);
365!
1021
      taosMemoryFreeClear(pTask->taskCtx);
365!
1022
      break;
365✔
1023
    }
1024
    case CTG_TASK_GET_TB_TAG: {
3✔
1025
      SCtgTbTagCtx* taskCtx = (SCtgTbTagCtx*)pTask->taskCtx;
3✔
1026
      taosMemoryFreeClear(taskCtx->pName);
3!
1027
      taosMemoryFreeClear(taskCtx->pVgInfo);
3!
1028
      taosMemoryFreeClear(taskCtx);
3!
1029
      break;
3✔
1030
    }
1031
    case CTG_TASK_GET_DB_VGROUP:
2,664,854✔
1032
    case CTG_TASK_GET_DB_CFG:
1033
    case CTG_TASK_GET_DB_INFO:
1034
    case CTG_TASK_GET_INDEX_INFO:
1035
    case CTG_TASK_GET_UDF:
1036
    case CTG_TASK_GET_QNODE:
1037
    case CTG_TASK_GET_USER: {
1038
      taosMemoryFreeClear(pTask->taskCtx);
2,664,854!
1039
      break;
2,664,986✔
1040
    }
1041
    case CTG_TASK_GET_VIEW: {
1,172,951✔
1042
      SCtgViewsCtx* taskCtx = (SCtgViewsCtx*)pTask->taskCtx;
1,172,951✔
1043
      taosArrayDestroyEx(taskCtx->pResList, ctgFreeViewMetaRes);
1,172,951✔
1044
      taosArrayDestroy(taskCtx->pFetchs);
1,172,957✔
1045
      // NO NEED TO FREE pNames
1046

1047
      taosArrayDestroyEx(pTask->msgCtxs, (FDelete)ctgFreeMsgCtx);
1,172,959✔
1048

1049
      taosMemoryFreeClear(pTask->taskCtx);
1,172,965!
1050
      break;
1,172,978✔
1051
    }
1052
    case CTG_TASK_GET_TSMA:
2,683✔
1053
    case CTG_TASK_GET_TB_TSMA: {
1054
      SCtgTbTSMACtx* pTsmaCtx = pTask->taskCtx;
2,683✔
1055
      taosArrayDestroyEx(pTsmaCtx->pResList, ctgFreeTbTSMARes);
2,683✔
1056
      taosArrayDestroy(pTsmaCtx->pFetches);
2,683✔
1057
      taosArrayDestroyEx(pTask->msgCtxs, (FDelete)ctgFreeMsgCtx);
2,683✔
1058
      taosMemoryFreeClear(pTask->taskCtx);
2,683!
1059
      break;
2,683✔
1060
    }
1061
    case CTG_TASK_GET_TB_NAME: {
675✔
1062
      SCtgTbNamesCtx* taskCtx = (SCtgTbNamesCtx*)pTask->taskCtx;
675✔
1063
      taosArrayDestroyEx(taskCtx->pResList, ctgFreeBatchMeta);
675✔
1064
      taosArrayDestroy(taskCtx->pFetchs);
675✔
1065
      // NO NEED TO FREE pNames
1066

1067
      taosArrayDestroyEx(pTask->msgCtxs, (FDelete)ctgFreeTbMetasMsgCtx);
675✔
1068

1069
      if (pTask->msgCtx.lastOut) {
675!
1070
        ctgFreeSTableMetaOutput((STableMetaOutput*)pTask->msgCtx.lastOut);
×
1071
        pTask->msgCtx.lastOut = NULL;
×
1072
      }
1073
      taosMemoryFreeClear(pTask->taskCtx);
675!
1074
      break;
675✔
1075
    }
1076
    case CTG_TASK_GET_V_SUBTABLES: {
1,225✔
1077
      SCtgVSubTablesCtx* taskCtx = (SCtgVSubTablesCtx*)pTask->taskCtx;
1,225✔
1078
      if (taskCtx->clonedVgroups) {
1,225!
1079
        taosArrayDestroy(taskCtx->pVgroups);
×
1080
        taskCtx->pVgroups = NULL;
×
1081
      }
1082
      if (taskCtx->pResList) {
1,225✔
1083
        for (int32_t i = 0; i < taskCtx->vgNum; ++i) {
23✔
1084
          SVSubTablesRsp* pVg = taskCtx->pResList + i;
8✔
1085
          tDestroySVSubTablesRsp(pVg);
8✔
1086
        }
1087
        taosMemoryFreeClear(taskCtx->pResList);
15!
1088
      }
1089
      taosMemoryFreeClear(taskCtx->pMeta);
1,225!
1090
      taosMemoryFreeClear(pTask->taskCtx);
1,225!
1091
      break;
1,225✔
1092
    }
1093
    case CTG_TASK_GET_V_STBREFDBS: {
856,166✔
1094
      SCtgVStbRefDbsCtx* taskCtx = (SCtgVStbRefDbsCtx*)pTask->taskCtx;
856,166✔
1095
      if (taskCtx->clonedVgroups) {
856,166!
1096
        taosArrayDestroy(taskCtx->pVgroups);
×
1097
        taskCtx->pVgroups = NULL;
×
1098
      }
1099
      if (taskCtx->pResList) {
856,166✔
1100
        for (int32_t i = 0; i < taskCtx->vgNum; ++i) {
14,901✔
1101
          SVStbRefDbsRsp* pVg = taskCtx->pResList + i;
9,940✔
1102
          tDestroySVSubTablesRsp(pVg);
9,940✔
1103
        }
1104
        taosMemoryFreeClear(taskCtx->pResList);
4,961!
1105
      }
1106
      taosMemoryFreeClear(taskCtx->pMeta);
856,166!
1107
      taosMemoryFreeClear(pTask->taskCtx);
856,177!
1108
      break;
856,176✔
1109
    }
1110
    default:
14✔
1111
      qError("invalid task type %d", pTask->type);
14!
1112
      break;
367✔
1113
  }
1114
}
7,234,032✔
1115

1116
void ctgFreeTask(SCtgTask* pTask, bool freeRes) {
7,233,248✔
1117
  ctgFreeMsgCtx(&pTask->msgCtx);
7,233,248✔
1118
  if (freeRes || pTask->subTask) {
7,233,223✔
1119
    ctgFreeTaskRes(pTask->type, &pTask->res);
7,229,601✔
1120
  }
1121
  ctgFreeTaskCtx(pTask);
7,233,396✔
1122

1123
  taosArrayDestroy(pTask->pParents);
7,233,656✔
1124
  ctgClearSubTaskRes(&pTask->subRes);
7,233,573✔
1125
}
7,233,389✔
1126

1127
void ctgFreeTasks(SArray* pArray, bool freeRes) {
1,431,691✔
1128
  if (NULL == pArray) {
1,431,691!
1129
    return;
×
1130
  }
1131

1132
  int32_t num = taosArrayGetSize(pArray);
1,431,691✔
1133
  for (int32_t i = 0; i < num; ++i) {
8,665,249✔
1134
    SCtgTask* pTask = taosArrayGet(pArray, i);
7,233,501✔
1135
    ctgFreeTask(pTask, freeRes);
7,233,330✔
1136
  }
1137

1138
  taosArrayDestroy(pArray);
1,431,748✔
1139
}
1140

1141
void ctgFreeJob(void* job) {
1,431,635✔
1142
  if (NULL == job) {
1,431,635!
1143
    return;
×
1144
  }
1145

1146
  SCtgJob* pJob = (SCtgJob*)job;
1,431,635✔
1147

1148
  int64_t  rid = pJob->refId;
1,431,635✔
1149
  uint64_t qid = pJob->queryId;
1,431,635✔
1150

1151
  ctgFreeTasks(pJob->pTasks, pJob->jobRes.ctgFree);
1,431,635✔
1152
  ctgFreeBatchs(pJob->pBatchs);
1,431,771✔
1153

1154
  ctgFreeSMetaData(&pJob->jobRes);
1,431,806✔
1155

1156
  taosMemoryFree(job);
1,431,799!
1157

1158
  qTrace("QID:0x%" PRIx64 ", job:0x%" PRIx64 ", catalog job freed", qid, rid);
1,431,818✔
1159
}
1160

1161
int32_t ctgUpdateMsgCtx(SCtgMsgCtx* pCtx, int32_t reqType, void* out, char* target) {
403,744✔
1162
  ctgFreeMsgCtx(pCtx);
403,744✔
1163

1164
  pCtx->reqType = reqType;
403,744✔
1165
  pCtx->out = out;
403,744✔
1166
  if (target) {
403,744✔
1167
    pCtx->target = taosStrdup(target);
390,430!
1168
    if (NULL == pCtx->target) {
390,431!
1169
      CTG_ERR_RET(terrno);
×
1170
    }
1171
  } else {
1172
    pCtx->target = NULL;
13,314✔
1173
  }
1174

1175
  return TSDB_CODE_SUCCESS;
403,745✔
1176
}
1177

1178
int32_t ctgAddMsgCtx(SArray* pCtxs, int32_t reqType, void* out, char* target) {
×
1179
  SCtgMsgCtx ctx = {0};
×
1180

1181
  ctx.reqType = reqType;
×
1182
  ctx.out = out;
×
1183
  if (target) {
×
1184
    ctx.target = taosStrdup(target);
×
1185
    if (NULL == ctx.target) {
×
1186
      CTG_ERR_RET(terrno);
×
1187
    }
1188
  }
1189

1190
  if (NULL == taosArrayPush(pCtxs, &ctx)) {
×
1191
    ctgFreeMsgCtx(&ctx);
×
1192
    CTG_ERR_RET(terrno);
×
1193
  }
1194

1195
  return TSDB_CODE_SUCCESS;
×
1196
}
1197

1198
int32_t ctgGetHashFunction(int8_t hashMethod, tableNameHashFp* fp) {
×
1199
  switch (hashMethod) {
×
1200
    default:
1201
      *fp = MurmurHash3_32;
×
1202
      break;
×
1203
  }
1204

1205
  return TSDB_CODE_SUCCESS;
×
1206
}
1207

1208
int32_t ctgVgInfoIdComp(void const *lp, void const *rp) {
165,222✔
1209
  SVgroupInfo* pVg1 = (SVgroupInfo*)lp;
165,222✔
1210
  SVgroupInfo* pVg2 = (SVgroupInfo*)rp;
165,222✔
1211

1212
  if (pVg1->vgId < pVg2->vgId) {
165,222✔
1213
    return -1;
140,177✔
1214
  } else if (pVg1->vgId > pVg2->vgId) {
25,045!
1215
    return 1;
25,045✔
1216
  }
1217

1218
  return 0;
×
1219
}
1220

1221
int32_t ctgGenerateVgList(SCatalog* pCtg, SHashObj* vgHash, SArray** pList, const char* dbFName) {
1,104,313✔
1222
  SHashObj*    vgroupHash = NULL;
1,104,313✔
1223
  SVgroupInfo* vgInfo = NULL;
1,104,313✔
1224
  SArray*      vgList = NULL;
1,104,313✔
1225
  int32_t      code = 0;
1,104,313✔
1226
  int32_t      vgNum = taosHashGetSize(vgHash);
1,104,313✔
1227
  SName        name = {0};
1,104,351✔
1228
  code = tNameFromString(&name, dbFName, T_NAME_ACCT | T_NAME_DB);
1,104,351✔
1229
  CTG_ERR_RET(code);
1,104,445!
1230

1231
  vgList = taosArrayInit(vgNum, sizeof(SVgroupInfo));
1,104,445✔
1232
  if (NULL == vgList) {
1,104,405!
1233
    ctgError("taosArrayInit failed, num:%d", vgNum);
×
1234
    CTG_ERR_RET(terrno);
×
1235
  }
1236

1237
  void* pIter = taosHashIterate(vgHash, NULL);
1,104,405✔
1238
  while (pIter) {
5,017,327✔
1239
    vgInfo = pIter;
3,912,834✔
1240

1241
    if (NULL == taosArrayPush(vgList, vgInfo)) {
3,912,670!
1242
      ctgError("taosArrayPush failed, vgId:%d", vgInfo->vgId);
×
1243
      taosHashCancelIterate(vgHash, pIter);
×
1244
      CTG_ERR_JRET(terrno);
×
1245
    }
1246

1247
    pIter = taosHashIterate(vgHash, pIter);
3,912,670✔
1248
  }
1249

1250
  if (IS_SYS_DBNAME(name.dbname))
1,104,493✔
1251
    taosArraySort(vgList, ctgVgInfoIdComp);
16,652✔
1252
  else
1253
    taosArraySort(vgList, ctgVgInfoComp);
1,087,841✔
1254

1255
  *pList = vgList;
1,104,405✔
1256

1257
  ctgDebug("get vgList from cache, vgNum:%d", vgNum);
1,104,405✔
1258

1259
  return TSDB_CODE_SUCCESS;
1,104,340✔
1260

1261
_return:
×
1262

1263
  if (vgList) {
×
1264
    taosArrayDestroy(vgList);
×
1265
  }
1266

1267
  CTG_RET(code);
×
1268
}
1269

1270
int ctgVgInfoComp(const void* lp, const void* rp) {
5,371,011✔
1271
  SVgroupInfo* pLeft = (SVgroupInfo*)lp;
5,371,011✔
1272
  SVgroupInfo* pRight = (SVgroupInfo*)rp;
5,371,011✔
1273
  if (pLeft->hashBegin < pRight->hashBegin) {
5,371,011✔
1274
    return -1;
4,788,837✔
1275
  } else if (pLeft->hashBegin > pRight->hashBegin) {
582,174!
1276
    return 1;
582,617✔
1277
  }
1278

1279
  return 0;
×
1280
}
1281

1282
int32_t ctgHashValueComp(void const* lp, void const* rp) {
20,717,693✔
1283
  uint32_t*    key = (uint32_t*)lp;
20,717,693✔
1284
  SVgroupInfo* pVg = (SVgroupInfo*)rp;
20,717,693✔
1285

1286
  if (*key < pVg->hashBegin) {
20,717,693✔
1287
    return -1;
1,907,036✔
1288
  } else if (*key > pVg->hashEnd) {
18,810,657✔
1289
    return 1;
7,535,933✔
1290
  }
1291

1292
  return 0;
11,274,724✔
1293
}
1294

1295
int32_t ctgGetVgInfoFromHashValue(SCatalog* pCtg, SEpSet* pMgmtEps, SDBVgInfo* dbInfo, const SName* pTableName,
9,975,939✔
1296
                                  SVgroupInfo* pVgroup) {
1297
  int32_t code = 0;
9,975,939✔
1298
  CTG_ERR_RET(ctgMakeVgArray(dbInfo, pTableName->dbname, false));
9,975,939!
1299

1300
  int32_t vgNum = taosArrayGetSize(dbInfo->vgArray);
9,981,608✔
1301
  char    db[TSDB_DB_FNAME_LEN] = {0};
9,970,477✔
1302
  (void)tNameGetFullDbName(pTableName, db);
9,970,477✔
1303

1304
  if (IS_SYS_DBNAME(pTableName->dbname)) {
9,990,678!
1305
    pVgroup->vgId = MNODE_HANDLE;
×
1306
    if (pMgmtEps) {
×
1307
      TAOS_MEMCPY(&pVgroup->epSet, pMgmtEps, sizeof(pVgroup->epSet));
×
1308
    }
1309

1310
    return TSDB_CODE_SUCCESS;
×
1311
  }
1312

1313
  if (vgNum <= 0) {
9,990,678!
1314
    ctgError("db vgroup cache invalid, db:%s, vgroup number:%d", db, vgNum);
×
1315
    CTG_ERR_RET(TSDB_CODE_TSC_DB_NOT_SELECTED);
×
1316
  }
1317

1318
  SVgroupInfo* vgInfo = NULL;
9,990,678✔
1319
  char         tbFullName[TSDB_TABLE_FNAME_LEN];
1320
  code = tNameExtractFullName(pTableName, tbFullName);
9,990,678✔
1321
  if (code) {
9,974,046!
1322
    ctgError("tNameExtractFullName failed, error:%s, type:%d, dbName:%s, tname:%s", tstrerror(code), pTableName->type,
×
1323
             pTableName->dbname, pTableName->tname);
1324
    CTG_ERR_RET(code);
×
1325
  }
1326

1327
  uint32_t hashValue = taosGetTbHashVal(tbFullName, (uint32_t)strlen(tbFullName), dbInfo->hashMethod,
19,951,832✔
1328
                                        dbInfo->hashPrefix, dbInfo->hashSuffix);
9,974,046✔
1329

1330
  vgInfo = taosArraySearch(dbInfo->vgArray, &hashValue, ctgHashValueComp, TD_EQ);
9,977,786✔
1331

1332
  /*
1333
    void* pIter = taosHashIterate(dbInfo->vgHash, NULL);
1334
    while (pIter) {
1335
      vgInfo = pIter;
1336
      if (hashValue >= vgInfo->hashBegin && hashValue <= vgInfo->hashEnd) {
1337
        taosHashCancelIterate(dbInfo->vgHash, pIter);
1338
        break;
1339
      }
1340

1341
      pIter = taosHashIterate(dbInfo->vgHash, pIter);
1342
      vgInfo = NULL;
1343
    }
1344
  */
1345

1346
  if (NULL == vgInfo) {
9,962,546!
1347
    ctgError("tb:%s, no hash range found for hash value [%u], db:%s, numOfVgId:%d", tbFullName, hashValue, db,
×
1348
             (int32_t)taosArrayGetSize(dbInfo->vgArray));
1349
    CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
×
1350
  }
1351

1352
  *pVgroup = *vgInfo;
9,962,546✔
1353

1354
  ctgTrace("tb:%s, get hash vgroup, vgId:%d, epNum:%d, current ep:%s:%u", tbFullName, vgInfo->vgId,
9,962,546✔
1355
           vgInfo->epSet.numOfEps, vgInfo->epSet.eps[vgInfo->epSet.inUse].fqdn,
1356
           vgInfo->epSet.eps[vgInfo->epSet.inUse].port);
1357

1358
  CTG_RET(code);
9,977,830!
1359
}
1360

1361
int32_t ctgGetVgInfosFromHashValue(SCatalog* pCtg, SEpSet* pMgmgEpSet, SCtgTaskReq* tReq, SDBVgInfo* dbInfo,
1,251,831✔
1362
                                   SCtgTbHashsCtx* pCtx, char* dbFName, SArray* pNames, bool update) {
1363
  int32_t      code = 0;
1,251,831✔
1364
  SCtgTask*    pTask = tReq->pTask;
1,251,831✔
1365
  SMetaRes     res = {0};
1,251,831✔
1366
  SVgroupInfo* vgInfo = NULL;
1,251,831✔
1367

1368
  CTG_ERR_RET(ctgMakeVgArray(dbInfo, dbFName, true));
1,251,831!
1369

1370
  int32_t tbNum = taosArrayGetSize(pNames);
1,251,908✔
1371

1372
  char* pSep = strchr(dbFName, '.');
1,251,992✔
1373
  if (pSep && IS_SYS_DBNAME(pSep + 1)) {
1,251,992!
1374
    SVgroupInfo mgmtInfo = {0};
17,003✔
1375
    mgmtInfo.vgId = MNODE_HANDLE;
17,003✔
1376
    if (pMgmgEpSet) {
17,003!
1377
      TAOS_MEMCPY(&mgmtInfo.epSet, pMgmgEpSet, sizeof(mgmtInfo.epSet));
17,003✔
1378
    }
1379

1380
    for (int32_t i = 0; i < tbNum; ++i) {
34,089✔
1381
      vgInfo = taosMemoryMalloc(sizeof(SVgroupInfo));
17,086!
1382
      if (NULL == vgInfo) {
17,086!
1383
        CTG_ERR_RET(terrno);
×
1384
      }
1385

1386
      TAOS_MEMCPY(vgInfo, &mgmtInfo, sizeof(mgmtInfo));
17,086✔
1387

1388
      ctgDebug("get tb hash vgroup, vgId:%d, epNum %d, current %s port %d", vgInfo->vgId, vgInfo->epSet.numOfEps,
17,086✔
1389
               vgInfo->epSet.eps[vgInfo->epSet.inUse].fqdn, vgInfo->epSet.eps[vgInfo->epSet.inUse].port);
1390

1391
      if (update) {
17,086✔
1392
        SCtgFetch* pFetch = taosArrayGet(pCtx->pFetchs, tReq->msgIdx);
763✔
1393
        if (NULL == pFetch) {
763!
1394
          ctgError("fail to get the %dth SCtgFetch, total:%d", tReq->msgIdx, (int32_t)taosArrayGetSize(pCtx->pFetchs));
×
1395
          CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
×
1396
        }
1397
        SMetaRes* pRes = taosArrayGet(pCtx->pResList, pFetch->resIdx + i);
763✔
1398
        if (NULL == pFetch) {
763!
1399
          ctgError("fail to get the %dth SMetaRes, total:%d", pFetch->resIdx + i,
×
1400
                   (int32_t)taosArrayGetSize(pCtx->pResList));
1401
          CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
×
1402
        }
1403

1404
        pRes->pRes = vgInfo;
763✔
1405
      } else {
1406
        res.pRes = vgInfo;
16,323✔
1407
        if (NULL == taosArrayPush(pCtx->pResList, &res)) {
32,646!
1408
          CTG_ERR_RET(terrno);
×
1409
        }
1410
      }
1411
    }
1412

1413
    return TSDB_CODE_SUCCESS;
17,003✔
1414
  }
1415

1416
  int32_t vgNum = taosArrayGetSize(dbInfo->vgArray);
1,234,989✔
1417
  if (vgNum <= 0) {
1,234,839!
1418
    ctgError("db vgroup cache invalid, db:%s, vgroup number:%d", dbFName, vgNum);
×
1419
    CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
×
1420
  }
1421

1422
  if (1 == vgNum) {
1,234,800✔
1423
    for (int32_t i = 0; i < tbNum; ++i) {
175,680✔
1424
      vgInfo = taosMemoryMalloc(sizeof(SVgroupInfo));
88,197!
1425
      if (NULL == vgInfo) {
88,197!
1426
        CTG_ERR_RET(terrno);
×
1427
      }
1428

1429
      SVgroupInfo* pSrcVg = (SVgroupInfo*)taosArrayGet(dbInfo->vgArray, 0);
88,197✔
1430
      if (NULL == pSrcVg) {
88,197!
1431
        ctgError("fail to get the 0th SVgroupInfo, total:%d", (int32_t)taosArrayGetSize(dbInfo->vgArray));
×
1432
        CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
×
1433
      }
1434

1435
      TAOS_MEMCPY(vgInfo, pSrcVg, sizeof(*pSrcVg));
88,197✔
1436

1437
      ctgDebug("get tb hash vgroup, vgId:%d, epNum %d, current %s port %d", vgInfo->vgId, vgInfo->epSet.numOfEps,
88,197✔
1438
               vgInfo->epSet.eps[vgInfo->epSet.inUse].fqdn, vgInfo->epSet.eps[vgInfo->epSet.inUse].port);
1439

1440
      if (update) {
88,197✔
1441
        SCtgFetch* pFetch = taosArrayGet(pCtx->pFetchs, tReq->msgIdx);
1,554✔
1442
        if (NULL == pFetch) {
1,554!
1443
          ctgError("fail to get the %dth SCtgFetch, total:%d", tReq->msgIdx, (int32_t)taosArrayGetSize(pCtx->pFetchs));
×
1444
          CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
×
1445
        }
1446
        SMetaRes* pRes = taosArrayGet(pCtx->pResList, pFetch->resIdx + i);
1,554✔
1447
        if (NULL == pRes) {
1,554!
1448
          ctgError("fail to get the %dth SMetaRes, total:%d", pFetch->resIdx + i,
×
1449
                   (int32_t)taosArrayGetSize(pCtx->pResList));
1450
          CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
×
1451
        }
1452

1453
        pRes->pRes = vgInfo;
1,554✔
1454
      } else {
1455
        res.pRes = vgInfo;
86,643✔
1456
        if (NULL == taosArrayPush(pCtx->pResList, &res)) {
173,286!
1457
          CTG_ERR_RET(terrno);
×
1458
        }
1459
      }
1460
    }
1461

1462
    return TSDB_CODE_SUCCESS;
87,483✔
1463
  }
1464

1465
  char tbFullName[TSDB_TABLE_FNAME_LEN];
1466
  (void)snprintf(tbFullName, sizeof(tbFullName), "%s.", dbFName);
1,147,317✔
1467
  int32_t offset = strlen(tbFullName);
1,147,317✔
1468
  SName*  pName = NULL;
1,147,317✔
1469
  int32_t tbNameLen = 0;
1,147,317✔
1470

1471
  for (int32_t i = 0; i < tbNum; ++i) {
2,485,387✔
1472
    pName = taosArrayGet(pNames, i);
1,337,842✔
1473
    if (NULL == pName) {
1,337,848!
1474
      ctgError("fail to get the %dth SName, total:%d", i, (int32_t)taosArrayGetSize(pNames));
×
1475
      CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
×
1476
    }
1477

1478
    tbNameLen = offset + strlen(pName->tname);
1,337,848✔
1479
    TAOS_STRCPY(tbFullName + offset, pName->tname);
1,337,848✔
1480

1481
    uint32_t hashValue = taosGetTbHashVal(tbFullName, (uint32_t)strlen(tbFullName), dbInfo->hashMethod,
2,675,931✔
1482
                                          dbInfo->hashPrefix, dbInfo->hashSuffix);
1,337,848✔
1483

1484
    vgInfo = taosArraySearch(dbInfo->vgArray, &hashValue, ctgHashValueComp, TD_EQ);
1,338,083✔
1485
    if (NULL == vgInfo) {
1,337,917!
1486
      ctgError("no hash range found for hash value [%u], db:%s, numOfVgId:%d", hashValue, dbFName,
×
1487
               (int32_t)taosArrayGetSize(dbInfo->vgArray));
1488
      CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
×
1489
    }
1490

1491
    SVgroupInfo* pNewVg = taosMemoryMalloc(sizeof(SVgroupInfo));
1,337,917!
1492
    if (NULL == pNewVg) {
1,338,113!
1493
      CTG_ERR_RET(terrno);
×
1494
    }
1495

1496
    *pNewVg = *vgInfo;
1,338,113✔
1497

1498
    ctgTrace("tb:%s, get hash vgroup, vgId:%d, epNum:%d, current ep:%s:%u", tbFullName, vgInfo->vgId,
1,338,113✔
1499
             vgInfo->epSet.numOfEps, vgInfo->epSet.eps[vgInfo->epSet.inUse].fqdn,
1500
             vgInfo->epSet.eps[vgInfo->epSet.inUse].port);
1501

1502
    if (update) {
1,337,987✔
1503
      SCtgFetch* pFetch = taosArrayGet(pCtx->pFetchs, tReq->msgIdx);
31,963✔
1504
      if (NULL == pFetch) {
31,963!
1505
        ctgError("fail to get the %dth SCtgFetch, total:%d", tReq->msgIdx, (int32_t)taosArrayGetSize(pCtx->pFetchs));
×
1506
        CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
×
1507
      }
1508
      SMetaRes* pRes = taosArrayGet(pCtx->pResList, pFetch->resIdx + i);
31,963✔
1509
      if (NULL == pRes) {
31,963!
1510
        ctgError("fail to get the %dth SMetaRes, total:%d", pFetch->resIdx + i,
×
1511
                 (int32_t)taosArrayGetSize(pCtx->pResList));
1512
        CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
×
1513
      }
1514

1515
      pRes->pRes = pNewVg;
31,963✔
1516
    } else {
1517
      res.pRes = pNewVg;
1,306,024✔
1518
      if (NULL == taosArrayPush(pCtx->pResList, &res)) {
2,612,119!
1519
        CTG_ERR_RET(terrno);
×
1520
      }
1521
    }
1522
  }
1523

1524
  CTG_RET(code);
1,147,545!
1525
}
1526

1527
int32_t ctgGetVgIdsFromHashValue(SCatalog* pCtg, SDBVgInfo* dbInfo, char* dbFName, const char* pTbs[], int32_t tbNum,
×
1528
                                 int32_t* vgId) {
1529
  int32_t code = 0;
×
1530
  CTG_ERR_RET(ctgMakeVgArray(dbInfo, dbFName, true));
×
1531

1532
  int32_t vgNum = taosArrayGetSize(dbInfo->vgArray);
×
1533
  if (vgNum <= 0) {
×
1534
    ctgError("db vgroup cache invalid, db:%s, vgroup number:%d", dbFName, vgNum);
×
1535
    CTG_ERR_RET(TSDB_CODE_TSC_DB_NOT_SELECTED);
×
1536
  }
1537

1538
  SVgroupInfo* vgInfo = NULL;
×
1539
  char         tbFullName[TSDB_TABLE_FNAME_LEN];
1540
  (void)snprintf(tbFullName, sizeof(tbFullName), "%s.", dbFName);
×
1541
  int32_t offset = strlen(tbFullName);
×
1542

1543
  for (int32_t i = 0; i < tbNum; ++i) {
×
1544
    (void)snprintf(tbFullName + offset, sizeof(tbFullName) - offset, "%s", pTbs[i]);
×
1545
    uint32_t hashValue = taosGetTbHashVal(tbFullName, (uint32_t)strlen(tbFullName), dbInfo->hashMethod,
×
1546
                                          dbInfo->hashPrefix, dbInfo->hashSuffix);
×
1547

1548
    vgInfo = taosArraySearch(dbInfo->vgArray, &hashValue, ctgHashValueComp, TD_EQ);
×
1549
    if (NULL == vgInfo) {
×
1550
      ctgError("no hash range found for hash value [%u], db:%s, numOfVgId:%d", hashValue, dbFName,
×
1551
               (int32_t)taosArrayGetSize(dbInfo->vgArray));
1552
      CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
×
1553
    }
1554

1555
    vgId[i] = vgInfo->vgId;
×
1556

1557
    ctgDebug("tb:%s, get vgId:%d", tbFullName, vgInfo->vgId);
×
1558
  }
1559

1560
  CTG_RET(code);
×
1561
}
1562

1563
int32_t ctgStbVersionSearchCompare(const void* key1, const void* key2) {
19,796✔
1564
  if (*(uint64_t*)key1 < ((SSTableVersion*)key2)->suid) {
19,796✔
1565
    return -1;
7,003✔
1566
  } else if (*(uint64_t*)key1 > ((SSTableVersion*)key2)->suid) {
12,793✔
1567
    return 1;
1,782✔
1568
  } else {
1569
    return 0;
11,011✔
1570
  }
1571
}
1572

1573
int32_t ctgDbCacheInfoSearchCompare(const void* key1, const void* key2) {
88,858✔
1574
  if (*(int64_t*)key1 < ((SDbCacheInfo*)key2)->dbId) {
88,858✔
1575
    return -1;
804✔
1576
  } else if (*(int64_t*)key1 > ((SDbCacheInfo*)key2)->dbId) {
88,054✔
1577
    return 1;
3,718✔
1578
  } else {
1579
    return 0;
84,336✔
1580
  }
1581
}
1582

1583
int32_t ctgViewVersionSearchCompare(const void* key1, const void* key2) {
340✔
1584
  if (*(uint64_t*)key1 < ((SViewVersion*)key2)->viewId) {
340✔
1585
    return -1;
59✔
1586
  } else if (*(uint64_t*)key1 > ((SViewVersion*)key2)->viewId) {
281✔
1587
    return 1;
38✔
1588
  } else {
1589
    return 0;
243✔
1590
  }
1591
}
1592

1593
int32_t ctgTSMAVersionSearchCompare(const void* key1, const void* key2) {
1,811✔
1594
  if (*(uint64_t*)key1 < ((STSMAVersion*)key2)->tsmaId) {
1,811✔
1595
    return -1;
12✔
1596
  } else if (*(uint64_t*)key1 > ((STSMAVersion*)key2)->tsmaId) {
1,799✔
1597
    return 1;
116✔
1598
  } else {
1599
    return 0;
1,683✔
1600
  }
1601
}
1602

1603
int32_t ctgStbVersionSortCompare(const void* key1, const void* key2) {
36,178✔
1604
  if (((SSTableVersion*)key1)->suid < ((SSTableVersion*)key2)->suid) {
36,178✔
1605
    return -1;
29,242✔
1606
  } else if (((SSTableVersion*)key1)->suid > ((SSTableVersion*)key2)->suid) {
6,936!
1607
    return 1;
6,936✔
1608
  } else {
1609
    return 0;
×
1610
  }
1611
}
1612

1613
int32_t ctgDbCacheInfoSortCompare(const void* key1, const void* key2) {
1,790✔
1614
  if (((SDbCacheInfo*)key1)->dbId < ((SDbCacheInfo*)key2)->dbId) {
1,790✔
1615
    return -1;
1,064✔
1616
  } else if (((SDbCacheInfo*)key1)->dbId > ((SDbCacheInfo*)key2)->dbId) {
726!
1617
    return 1;
726✔
1618
  } else {
1619
    return 0;
×
1620
  }
1621
}
1622

1623
int32_t ctgViewVersionSortCompare(const void* key1, const void* key2) {
86✔
1624
  if (((SViewVersion*)key1)->viewId < ((SViewVersion*)key2)->viewId) {
86✔
1625
    return -1;
30✔
1626
  } else if (((SViewVersion*)key1)->viewId > ((SViewVersion*)key2)->viewId) {
56!
1627
    return 1;
56✔
1628
  } else {
1629
    return 0;
×
1630
  }
1631
}
1632

1633
int32_t ctgTSMAVersionSortCompare(const void* key1, const void* key2) {
17✔
1634
  if (((STSMAVersion*)key1)->tsmaId < ((STSMAVersion*)key2)->tsmaId) {
17✔
1635
    return -1;
5✔
1636
  } else if (((STSMAVersion*)key1)->tsmaId > ((STSMAVersion*)key2)->tsmaId) {
12!
1637
    return 1;
12✔
1638
  } else {
1639
    return 0;
×
1640
  }
1641
}
1642

1643
int32_t ctgMakeVgArray(SDBVgInfo* dbInfo, const char* dbName, bool isFullName) {
11,375,191✔
1644
  __compar_fn_t sortFunc = ctgVgInfoComp;
11,375,191✔
1645
  if (dbName) {
11,375,191!
1646
    const char*   realDbName = dbName;
11,377,846✔
1647
    SName         name = {0};
11,377,846✔
1648
    if (isFullName) {
11,377,846✔
1649
      int32_t code = tNameFromString(&name, dbName, T_NAME_ACCT | T_NAME_DB);
1,251,925✔
1650
      CTG_ERR_RET(code);
1,252,027!
1651
      realDbName = name.dbname;
1,252,058✔
1652
    }
1653
    if (IS_SYS_DBNAME(realDbName)) sortFunc = ctgVgInfoIdComp;
11,377,979✔
1654
  }
1655
  return ctgMakeVgArraySortBy(dbInfo, sortFunc);
11,375,324✔
1656
}
1657

1658
int32_t ctgMakeVgArraySortBy(SDBVgInfo* dbInfo, __compar_fn_t sort_func) {
11,377,474✔
1659
  if (NULL == dbInfo) {
11,377,474!
1660
    return TSDB_CODE_SUCCESS;
×
1661
  }
1662

1663
  if (dbInfo->vgHash && NULL == dbInfo->vgArray) {
11,377,474!
1664
    int32_t vgSize = taosHashGetSize(dbInfo->vgHash);
206,700✔
1665
    dbInfo->vgArray = taosArrayInit(vgSize, sizeof(SVgroupInfo));
206,693✔
1666
    if (NULL == dbInfo->vgArray) {
206,700!
1667
      CTG_ERR_RET(terrno);
×
1668
    }
1669

1670
    void* pIter = taosHashIterate(dbInfo->vgHash, NULL);
206,700✔
1671
    while (pIter) {
1,077,066✔
1672
      if (NULL == taosArrayPush(dbInfo->vgArray, pIter)) {
1,740,698!
1673
        taosHashCancelIterate(dbInfo->vgHash, pIter);
×
1674
        CTG_ERR_RET(terrno);
×
1675
      }
1676

1677
      pIter = taosHashIterate(dbInfo->vgHash, pIter);
870,335✔
1678
    }
1679

1680
    taosArraySort(dbInfo->vgArray, sort_func);
206,703✔
1681
  }
1682

1683
  return TSDB_CODE_SUCCESS;
11,381,456✔
1684
}
1685

1686
int32_t ctgCloneVgInfo(SDBVgInfo* src, SDBVgInfo** dst) {
2,778✔
1687
  CTG_ERR_RET(ctgMakeVgArray(src, NULL, false));
2,778!
1688

1689
  *dst = taosMemoryMalloc(sizeof(SDBVgInfo));
2,778!
1690
  if (NULL == *dst) {
2,778!
1691
    qError("malloc %d failed", (int32_t)sizeof(SDBVgInfo));
×
1692
    CTG_ERR_RET(terrno);
×
1693
  }
1694

1695
  TAOS_MEMCPY(*dst, src, sizeof(SDBVgInfo));
2,778✔
1696

1697
  size_t hashSize = taosHashGetSize(src->vgHash);
2,778✔
1698
  (*dst)->vgHash = taosHashInit(hashSize, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
2,778✔
1699
  if (NULL == (*dst)->vgHash) {
2,778!
1700
    qError("taosHashInit %d failed", (int32_t)hashSize);
×
1701
    taosMemoryFreeClear(*dst);
×
1702
    CTG_ERR_RET(terrno);
×
1703
  }
1704

1705
  int32_t* vgId = NULL;
2,778✔
1706
  void*    pIter = taosHashIterate(src->vgHash, NULL);
2,778✔
1707
  while (pIter) {
28,382✔
1708
    vgId = taosHashGetKey(pIter, NULL);
25,604✔
1709

1710
    if (taosHashPut((*dst)->vgHash, (void*)vgId, sizeof(int32_t), pIter, sizeof(SVgroupInfo))) {
25,604!
1711
      qError("taosHashPut failed, hashSize:%d", (int32_t)hashSize);
×
1712
      taosHashCancelIterate(src->vgHash, pIter);
×
1713
      taosHashCleanup((*dst)->vgHash);
×
1714
      taosMemoryFreeClear(*dst);
×
1715
      CTG_ERR_RET(terrno);
×
1716
    }
1717

1718
    pIter = taosHashIterate(src->vgHash, pIter);
25,604✔
1719
  }
1720

1721
  if (src->vgArray) {
2,778!
1722
    (*dst)->vgArray = taosArrayDup(src->vgArray, NULL);
2,778✔
1723
    if (NULL == (*dst)->vgArray) {
2,778!
1724
      taosHashCleanup((*dst)->vgHash);
×
1725
      taosMemoryFreeClear(*dst);
×
1726
      CTG_ERR_RET(terrno);
×
1727
    }
1728
  }
1729

1730
  return TSDB_CODE_SUCCESS;
2,778✔
1731
}
1732

1733
int32_t ctgCloneMetaOutput(STableMetaOutput* output, STableMetaOutput** pOutput) {
99,657✔
1734
  *pOutput = taosMemoryMalloc(sizeof(STableMetaOutput));
99,657!
1735
  if (NULL == *pOutput) {
99,659!
1736
    qError("malloc %d failed", (int32_t)sizeof(STableMetaOutput));
×
1737
    CTG_ERR_RET(terrno);
×
1738
  }
1739

1740
  TAOS_MEMCPY(*pOutput, output, sizeof(STableMetaOutput));
99,659✔
1741

1742
  if (output->vctbMeta) {
99,659✔
1743
    int32_t metaSize = sizeof(SVCTableMeta);
294✔
1744
    int32_t colRefSize = 0;
294✔
1745
    if (hasRefCol(output->vctbMeta->tableType) && (*pOutput)->vctbMeta->colRef) {
294!
1746
      colRefSize = output->vctbMeta->numOfColRefs * sizeof(SColRef);
294✔
1747
    }
1748
    (*pOutput)->vctbMeta = taosMemoryMalloc(metaSize + colRefSize);
294!
1749
    if (NULL == (*pOutput)->vctbMeta) {
294!
1750
      qError("malloc %d failed", (int32_t)sizeof(STableMetaOutput));
×
1751
      taosMemoryFreeClear(*pOutput);
×
1752
      CTG_ERR_RET(terrno);
×
1753
    }
1754

1755
    TAOS_MEMCPY((*pOutput)->vctbMeta, output->vctbMeta, metaSize);
294✔
1756
    if (hasRefCol(output->vctbMeta->tableType) && (*pOutput)->vctbMeta->colRef) {
294!
1757
      (*pOutput)->vctbMeta->colRef = (SColRef*)((char*)(*pOutput)->vctbMeta + metaSize);
294✔
1758
      TAOS_MEMCPY((*pOutput)->vctbMeta->colRef, output->vctbMeta->colRef, colRefSize);
294✔
1759
    } else {
1760
      (*pOutput)->vctbMeta->colRef = NULL;
×
1761
    }
1762
  }
1763

1764
  if (output->tbMeta) {
99,659✔
1765
    int32_t metaSize = CTG_META_SIZE(output->tbMeta);
99,556✔
1766
    int32_t schemaExtSize = 0;
99,556✔
1767
    int32_t colRefSize = 0;
99,556✔
1768
    if (withExtSchema(output->tbMeta->tableType) && (*pOutput)->tbMeta->schemaExt) {
99,556✔
1769
      schemaExtSize = output->tbMeta->tableInfo.numOfColumns * sizeof(SSchemaExt);
95,693✔
1770
    }
1771
    if (hasRefCol(output->tbMeta->tableType) && (*pOutput)->tbMeta->colRef) {
99,557!
1772
      colRefSize = output->tbMeta->tableInfo.numOfColumns * sizeof(SColRef);
122✔
1773
    }
1774

1775
    (*pOutput)->tbMeta = taosMemoryMalloc(metaSize + schemaExtSize + colRefSize);
99,558!
1776
    qTrace("tbmeta cloned, size:%d, p:%p", metaSize, (*pOutput)->tbMeta);
99,558✔
1777

1778
    if (NULL == (*pOutput)->tbMeta) {
99,558!
1779
      qError("malloc %d failed", (int32_t)sizeof(STableMetaOutput));
×
1780
      taosMemoryFreeClear(*pOutput);
×
1781
      CTG_ERR_RET(terrno);
×
1782
    }
1783

1784
    TAOS_MEMCPY((*pOutput)->tbMeta, output->tbMeta, metaSize);
99,558✔
1785
    if (withExtSchema(output->tbMeta->tableType) && (*pOutput)->tbMeta->schemaExt) {
99,558✔
1786
      (*pOutput)->tbMeta->schemaExt = (SSchemaExt*)((char*)(*pOutput)->tbMeta + metaSize);
95,692✔
1787
      TAOS_MEMCPY((*pOutput)->tbMeta->schemaExt, output->tbMeta->schemaExt, schemaExtSize);
95,692✔
1788
    } else {
1789
      (*pOutput)->tbMeta->schemaExt = NULL;
3,862✔
1790
    }
1791
    if (hasRefCol(output->tbMeta->tableType) && (*pOutput)->tbMeta->colRef) {
99,554!
1792
      (*pOutput)->tbMeta->colRef = (SColRef*)((char*)(*pOutput)->tbMeta + metaSize + schemaExtSize);
122✔
1793
      TAOS_MEMCPY((*pOutput)->tbMeta->colRef, output->tbMeta->colRef, colRefSize);
122✔
1794
    } else {
1795
      (*pOutput)->tbMeta->colRef = NULL;
99,434✔
1796
    }
1797
  }
1798

1799
  return TSDB_CODE_SUCCESS;
99,659✔
1800
}
1801

1802
int32_t ctgCloneTableIndex(SArray* pIndex, SArray** pRes) {
1✔
1803
  if (NULL == pIndex) {
1!
1804
    *pRes = NULL;
×
1805
    return TSDB_CODE_SUCCESS;
×
1806
  }
1807

1808
  int32_t num = taosArrayGetSize(pIndex);
1✔
1809
  *pRes = taosArrayInit(num, sizeof(STableIndexInfo));
1✔
1810
  if (NULL == *pRes) {
1!
1811
    CTG_ERR_RET(terrno);
×
1812
  }
1813

1814
  for (int32_t i = 0; i < num; ++i) {
4✔
1815
    STableIndexInfo* pInfo = taosArrayGet(pIndex, i);
3✔
1816
    if (NULL == pInfo) {
3!
1817
      qError("fail to get the %dth STableIndexInfo, total:%d", i, (int32_t)taosArrayGetSize(pIndex));
×
1818
      CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
×
1819
    }
1820
    pInfo = taosArrayPush(*pRes, pInfo);
3✔
1821
    if (NULL == pInfo) {
3!
1822
      CTG_ERR_RET(terrno);
×
1823
    }
1824
    pInfo->expr = taosStrdup(pInfo->expr);
3!
1825
    if (NULL == pInfo->expr) {
3!
1826
      CTG_ERR_RET(terrno);
×
1827
    }
1828
  }
1829

1830
  return TSDB_CODE_SUCCESS;
1✔
1831
}
1832

1833
int32_t ctgUpdateSendTargetInfo(SMsgSendInfo* pMsgSendInfo, int32_t msgType, char* dbFName, int32_t vgId) {
206,632✔
1834
  if (msgType == TDMT_VND_TABLE_META || msgType == TDMT_VND_TABLE_CFG || msgType == TDMT_VND_BATCH_META ||
206,632!
1835
      msgType == TDMT_VND_TABLE_NAME || msgType == TDMT_VND_VSUBTABLES_META ||
75,669!
1836
      msgType == TDMT_VND_GET_STREAM_PROGRESS || msgType == TDMT_VND_VSTB_REF_DBS) {
75,669!
1837
    pMsgSendInfo->target.type = TARGET_TYPE_VNODE;
130,963✔
1838
    pMsgSendInfo->target.vgId = vgId;
130,963✔
1839
    pMsgSendInfo->target.dbFName = taosStrdup(dbFName);
130,963!
1840
    if (NULL == pMsgSendInfo->target.dbFName) {
130,979!
1841
      CTG_ERR_RET(terrno);
×
1842
    }
1843
  } else {
1844
    pMsgSendInfo->target.type = TARGET_TYPE_MNODE;
75,669✔
1845
  }
1846

1847
  return TSDB_CODE_SUCCESS;
206,648✔
1848
}
1849

1850
int32_t ctgGetTablesReqNum(SArray* pList) {
5,725,610✔
1851
  if (NULL == pList) {
5,725,610✔
1852
    return 0;
2,056,107✔
1853
  }
1854

1855
  int32_t total = 0;
3,669,503✔
1856
  int32_t n = taosArrayGetSize(pList);
3,669,503✔
1857
  for (int32_t i = 0; i < n; ++i) {
7,347,813✔
1858
    STablesReq* pReq = taosArrayGet(pList, i);
3,677,552✔
1859
    if (NULL == pReq) {
3,677,532!
1860
      qError("fail to get the %dth STablesReq, total:%d", i, (int32_t)taosArrayGetSize(pList));
×
1861
      CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
×
1862
    }
1863

1864
    total += taosArrayGetSize(pReq->pTables);
3,677,532✔
1865
  }
1866

1867
  return total;
3,670,261✔
1868
}
1869

1870
int32_t ctgAddFetch(SArray** pFetchs, int32_t dbIdx, int32_t tbIdx, int32_t* fetchIdx, int32_t resIdx, int32_t flag) {
200,516✔
1871
  if (NULL == (*pFetchs)) {
200,516✔
1872
    *pFetchs = taosArrayInit(CTG_DEFAULT_FETCH_NUM, sizeof(SCtgFetch));
197,042✔
1873
    if (NULL == *pFetchs) {
197,041!
1874
      CTG_ERR_RET(terrno);
×
1875
    }
1876
  }
1877

1878
  SCtgFetch fetch = {0};
200,515✔
1879
  fetch.dbIdx = dbIdx;
200,515✔
1880
  fetch.tbIdx = tbIdx;
200,515✔
1881
  fetch.fetchIdx = (*fetchIdx)++;
200,515✔
1882
  fetch.resIdx = resIdx;
200,515✔
1883
  fetch.flag = flag;
200,515✔
1884

1885
  if (NULL == taosArrayPush(*pFetchs, &fetch)) {
401,039!
1886
    CTG_ERR_RET(terrno);
×
1887
  }
1888

1889
  return TSDB_CODE_SUCCESS;
200,525✔
1890
}
1891

1892
int32_t ctgGetFetchName(SArray* pNames, SCtgFetch* pFetch, SName** ppName) {
497,886✔
1893
  STablesReq* pReq = (STablesReq*)taosArrayGet(pNames, pFetch->dbIdx);
497,886✔
1894
  if (NULL == pReq) {
497,889!
1895
    qError("fail to get the %dth tb in pTables, tbNum:%d", pFetch->tbIdx, (int32_t)taosArrayGetSize(pReq->pTables));
×
1896
    return TSDB_CODE_CTG_INTERNAL_ERROR;
×
1897
  }
1898

1899
  *ppName = (SName*)taosArrayGet(pReq->pTables, pFetch->tbIdx);
497,889✔
1900
  if (NULL == *ppName) {
497,897✔
1901
    qError("fail to get the %dth tb in pTables, tbNum:%d", pFetch->tbIdx, (int32_t)taosArrayGetSize(pReq->pTables));
3!
1902
    return TSDB_CODE_CTG_INTERNAL_ERROR;
×
1903
  }
1904

1905
  return TSDB_CODE_SUCCESS;
497,894✔
1906
}
1907

1908
static int32_t ctgCloneDbVgroup(void* pSrc, void** ppDst) {
×
1909
#if 0
1910
  if (NULL == pSrc) {
1911
    *ppDst = NULL;
1912
    return TSDB_CODE_SUCCESS;
1913
  }
1914
  
1915
  *ppDst = taosArrayDup((const SArray*)pSrc, NULL); 
1916
  return (*ppDst) ? TSDB_CODE_SUCCESS : terrno;
1917
#else
1918
  return TSDB_CODE_CTG_INTERNAL_ERROR;
×
1919
#endif
1920
}
1921

1922
static void ctgFreeDbVgroup(void* p) { taosArrayDestroy((SArray*)((SMetaRes*)p)->pRes); }
611✔
1923

1924
int32_t ctgCloneDbCfgInfo(void* pSrc, SDbCfgInfo** ppDst) {
185,515✔
1925
  SDbCfgInfo* pDst = taosMemoryMalloc(sizeof(SDbCfgInfo));
185,515!
1926
  if (NULL == pDst) {
185,600!
1927
    return terrno;
×
1928
  }
1929

1930
  TAOS_MEMCPY(pDst, pSrc, sizeof(SDbCfgInfo));
185,600✔
1931
  if (((SDbCfgInfo*)pSrc)->pRetensions) {
185,600✔
1932
    pDst->pRetensions = taosArrayDup(((SDbCfgInfo*)pSrc)->pRetensions, NULL);
9✔
1933
    if (NULL == pDst->pRetensions) {
9!
1934
      taosMemoryFree(pDst);
115!
1935
      return terrno;
×
1936
    }
1937
  }
1938

1939
  *ppDst = pDst;
185,485✔
1940

1941
  return TSDB_CODE_SUCCESS;
185,485✔
1942
}
1943

1944
static void ctgFreeDbCfgInfo(void* p) {
12✔
1945
  SDbCfgInfo* pDst = (SDbCfgInfo*)((SMetaRes*)p)->pRes;
12✔
1946
  freeDbCfgInfo(pDst);
12✔
1947
}
12✔
1948

1949
static int32_t ctgCloneDbInfo(void* pSrc, void** ppDst) {
×
1950
#if 0
1951
  SDbInfo* pDst = taosMemoryMalloc(sizeof(SDbInfo));
1952
  if (NULL == pDst) {
1953
    return terrno;
1954
  }
1955
  
1956
  TAOS_MEMCPY(pDst, pSrc, sizeof(SDbInfo));
1957
  
1958
  return TSDB_CODE_SUCCESS;
1959
#else
1960
  return TSDB_CODE_CTG_INTERNAL_ERROR;
×
1961
#endif
1962
}
1963

1964
static void ctgFreeDbInfo(void* p) { taosMemoryFree(((SMetaRes*)p)->pRes); }
×
1965

1966
// static void* ctgCloneTableMeta(void* pSrc) {
1967
//   STableMeta* pMeta = pSrc;
1968
//   int32_t total = pMeta->tableInfo.numOfColumns + pMeta->tableInfo.numOfTags;
1969
//   STableMeta* pDst = taosMemoryMalloc(sizeof(STableMeta));
1970
//   if (NULL == pDst) {
1971
//     return NULL;
1972
//   }
1973
//   void* pSchema = taosMemoryMalloc(total * sizeof(SSchema));
1974
//   if (NULL == pSchema) {
1975
//     taosMemoryFree(pDst);
1976
//     return NULL;
1977
//   }
1978
//   void* pSchemaExt = taosMemoryMalloc(pMeta->tableInfo.numOfColumns * sizeof(SSchemaExt));
1979
//   if (NULL == pSchemaExt) {
1980
//     taosMemoryFree(pSchema);
1981
//     taosMemoryFree(pDst);
1982
//     return NULL;
1983
//   }
1984
//   memcpy(pDst, pSrc, sizeof(STableMeta));
1985
//   pDst->schema = pSchema;
1986
//   pDst->schemaExt = pSchemaExt;
1987
//   memcpy(pDst->schema, pMeta->schema, total * sizeof(SSchema));
1988
//   memcpy(pDst->schemaExt, pMeta->schemaExt, pMeta->tableInfo.numOfColumns * sizeof(SSchemaExt));
1989
//   return pDst;
1990
// }
1991

1992
static void ctgFreeTableMeta(void* p) { taosMemoryFree(((SMetaRes*)p)->pRes); }
666!
1993

1994
static int32_t ctgCloneVgroupInfo(void* pSrc, void** ppDst) {
×
1995
#if 0
1996
  SVgroupInfo* pDst = taosMemoryMalloc(sizeof(SVgroupInfo));
1997
  if (NULL == pDst) {
1998
    return NULL;
1999
  }
2000
  memcpy(pDst, pSrc, sizeof(SVgroupInfo));
2001
  return pDst;
2002
#else
2003
  return TSDB_CODE_CTG_INTERNAL_ERROR;
×
2004
#endif
2005
}
2006

2007
static void ctgFreeVgroupInfo(void* p) { taosMemoryFree(((SMetaRes*)p)->pRes); }
666!
2008

2009
static int32_t ctgCloneTableIndexs(void* pSrc, void** ppDst) {
×
2010
#if 0
2011
  return taosArrayDup((const SArray*)pSrc, NULL);
2012
#else
2013
  return TSDB_CODE_CTG_INTERNAL_ERROR;
×
2014
#endif
2015
}
2016

2017
static void ctgFreeTableIndexs(void* p) { taosArrayDestroy((SArray*)((SMetaRes*)p)->pRes); }
×
2018

2019
static int32_t ctgCloneFuncInfo(void* pSrc, void** ppDst) {
×
2020
#if 0
2021
  SFuncInfo* pDst = taosMemoryMalloc(sizeof(SFuncInfo));
2022
  if (NULL == pDst) {
2023
    return NULL;
2024
  }
2025
  memcpy(pDst, pSrc, sizeof(SFuncInfo));
2026
  return pDst;
2027
#else
2028
  return TSDB_CODE_CTG_INTERNAL_ERROR;
×
2029
#endif
2030
}
2031

2032
static void ctgFreeFuncInfo(void* p) { taosMemoryFree(((SMetaRes*)p)->pRes); }
×
2033

2034
static int32_t ctgCloneIndexInfo(void* pSrc) {
×
2035
#if 0
2036
  SIndexInfo* pDst = taosMemoryMalloc(sizeof(SIndexInfo));
2037
  if (NULL == pDst) {
2038
    return NULL;
2039
  }
2040
  memcpy(pDst, pSrc, sizeof(SIndexInfo));
2041
  return pDst;
2042
#else
2043
  return TSDB_CODE_CTG_INTERNAL_ERROR;
×
2044
#endif
2045
}
2046

2047
static void ctgFreeIndexInfo(void* p) { taosMemoryFree(((SMetaRes*)p)->pRes); }
×
2048

2049
static int32_t ctgCloneUserAuth(void* pSrc) {
×
2050
#if 0
2051
  bool* pDst = taosMemoryMalloc(sizeof(bool));
2052
  if (NULL == pDst) {
2053
    return NULL;
2054
  }
2055
  *pDst = *(bool*)pSrc;
2056
  return pDst;
2057
#else
2058
  return TSDB_CODE_CTG_INTERNAL_ERROR;
×
2059
#endif
2060
}
2061

2062
static void ctgFreeUserAuth(void* p) { taosMemoryFree(((SMetaRes*)p)->pRes); }
776!
2063

2064
static int32_t ctgCloneQnodeList(void* pSrc) {
×
2065
#if 0
2066
  return taosArrayDup((const SArray*)pSrc, NULL);
2067
#else
2068
  return TSDB_CODE_CTG_INTERNAL_ERROR;
×
2069
#endif
2070
}
2071

2072
static void ctgFreeQnodeList(void* p) { taosArrayDestroy((SArray*)((SMetaRes*)p)->pRes); }
×
2073

2074
// static void* ctgCloneTableCfg(void* pSrc) {
2075
//   STableCfg* pDst = taosMemoryMalloc(sizeof(STableCfg));
2076
//   if (NULL == pDst) {
2077
//     return NULL;
2078
//   }
2079
//   memcpy(pDst, pSrc, sizeof(STableCfg));
2080
//   return pDst;
2081
// }
2082

2083
static void ctgFreeTableCfg(void* p) { taosMemoryFree(((SMetaRes*)p)->pRes); }
×
2084

2085
static int32_t ctgCloneDnodeList(void* pSrc) {
×
2086
#if 0
2087
  return taosArrayDup((const SArray*)pSrc, NULL);
2088
#else
2089
  return TSDB_CODE_CTG_INTERNAL_ERROR;
×
2090
#endif
2091
}
2092

2093
static void ctgFreeDnodeList(void* p) { taosArrayDestroy((SArray*)((SMetaRes*)p)->pRes); }
×
2094

2095
static int32_t ctgCloneViewMeta(void* pSrc) {
×
2096
#if 0
2097
  SViewMeta* pSrcMeta = pSrc;
2098
  SViewMeta* pDst = taosMemoryMalloc(sizeof(SViewMeta));
2099
  if (NULL == pDst) {
2100
    return NULL;
2101
  }
2102
  pDst->user = tstrdup(pSrcMeta->user);
2103
  pDst->querySql = tstrdup(pSrcMeta->querySql);
2104
  pDst->pSchema = taosMemoryMalloc(pSrcMeta->numOfCols * sizeof(*pSrcMeta->pSchema));
2105
  if (NULL == pDst->pSchema) {
2106
    return pDst;
2107
  }
2108
  memcpy(pDst->pSchema, pSrcMeta->pSchema, pSrcMeta->numOfCols * sizeof(*pSrcMeta->pSchema));
2109
  return pDst;
2110
#else
2111
  return TSDB_CODE_CTG_INTERNAL_ERROR;
×
2112
#endif
2113
}
2114

2115
static void ctgFreeViewMeta(void* p) {
666✔
2116
  SViewMeta* pMeta = ((SMetaRes*)p)->pRes;
666✔
2117
  if (NULL == pMeta) {
666✔
2118
    return;
489✔
2119
  }
2120
  taosMemoryFree(pMeta->user);
177!
2121
  taosMemoryFree(pMeta->querySql);
177!
2122
  taosMemoryFree(pMeta->pSchema);
177!
2123
  taosMemoryFree(pMeta);
177!
2124
}
2125

2126
void ctgFreeTbTSMAInfo(void* p) {
×
2127
  tFreeTableTSMAInfoRsp(((SMetaRes*)p)->pRes);
×
2128
  taosMemoryFree(((SMetaRes*)p)->pRes);
×
2129
}
×
2130

2131
int32_t ctgChkSetTbAuthRes(SCatalog* pCtg, SCtgAuthReq* req, SCtgAuthRsp* res) {
1,041✔
2132
  int32_t          code = 0;
1,041✔
2133
  STableMeta*      pMeta = NULL;
1,041✔
2134
  SGetUserAuthRsp* pInfo = &req->authInfo;
1,041✔
2135
  SHashObj*        pTbs = (AUTH_TYPE_READ == req->singleType) ? pInfo->readTbs : pInfo->writeTbs;
1,041✔
2136
  char*            stbName = NULL;
1,041✔
2137

2138
  char tbFName[TSDB_TABLE_FNAME_LEN];
2139
  char dbFName[TSDB_DB_FNAME_LEN];
2140
  code = tNameExtractFullName(&req->pRawReq->tbName, tbFName);
1,041✔
2141
  if (code) {
1,041!
2142
    ctgError("tNameExtractFullName failed, error:%s, type:%d, dbName:%s, tname:%s", tstrerror(code),
×
2143
             req->pRawReq->tbName.type, req->pRawReq->tbName.dbname, req->pRawReq->tbName.tname);
2144
    CTG_ERR_RET(code);
×
2145
  }
2146

2147
  (void)tNameGetFullDbName(&req->pRawReq->tbName, dbFName);
1,041✔
2148

2149
  while (true) {
206✔
2150
    taosMemoryFreeClear(pMeta);
1,247!
2151

2152
    char* pCond = taosHashGet(pTbs, tbFName, strlen(tbFName) + 1);
1,247✔
2153
    if (pCond) {
1,247✔
2154
      if (strlen(pCond) > 1) {
584✔
2155
        CTG_ERR_JRET(nodesStringToNode(pCond, &res->pRawRes->pCond[AUTH_RES_BASIC]));
41!
2156
      }
2157

2158
      res->pRawRes->pass[AUTH_RES_BASIC] = true;
584✔
2159
      goto _return;
584✔
2160
    }
2161

2162
    if (stbName) {
663✔
2163
      res->pRawRes->pass[AUTH_RES_BASIC] = false;
56✔
2164
      goto _return;
56✔
2165
    }
2166

2167
    CTG_ERR_JRET(catalogGetCachedTableMeta(pCtg, &req->pRawReq->tbName, &pMeta));
607!
2168
    if (NULL == pMeta) {
607✔
2169
      if (req->onlyCache) {
164!
2170
        res->metaNotExists = true;
164✔
2171
        ctgDebug("db:%s, tb:%s meta not in cache for auth", req->pRawReq->tbName.dbname, req->pRawReq->tbName.tname);
164!
2172
        goto _return;
164✔
2173
      }
2174

2175
      SCtgTbMetaCtx ctx = {0};
×
2176
      ctx.pName = (SName*)&req->pRawReq->tbName;
×
2177
      ctx.flag = CTG_FLAG_UNKNOWN_STB | CTG_FLAG_SYNC_OP;
×
2178

2179
      CTG_ERR_JRET(ctgGetTbMeta(pCtg, req->pConn, &ctx, &pMeta));
×
2180
    }
2181

2182
    if (TSDB_SUPER_TABLE == pMeta->tableType || TSDB_NORMAL_TABLE == pMeta->tableType || TSDB_VIRTUAL_NORMAL_TABLE == pMeta->tableType) {
443✔
2183
      res->pRawRes->pass[AUTH_RES_BASIC] = false;
237✔
2184
      goto _return;
237✔
2185
    }
2186

2187
    if (TSDB_CHILD_TABLE == pMeta->tableType || TSDB_VIRTUAL_CHILD_TABLE == pMeta->tableType) {
206!
2188
      CTG_ERR_JRET(ctgGetCachedStbNameFromSuid(pCtg, dbFName, pMeta->suid, &stbName));
206!
2189
      if (NULL == stbName) {
206!
2190
        if (req->onlyCache) {
×
2191
          res->metaNotExists = true;
×
2192
          ctgDebug("suid:%" PRIu64 ", name not in cache for auth", pMeta->suid);
×
2193
          goto _return;
×
2194
        }
2195

2196
        continue;
×
2197
      }
2198

2199
      (void)snprintf(tbFName, sizeof(tbFName), "%s.%s", dbFName, stbName);
206✔
2200
      continue;
206✔
2201
    }
2202

2203
    ctgError("invalid table type %d for %s", pMeta->tableType, tbFName);
×
2204
    CTG_ERR_JRET(TSDB_CODE_INVALID_PARA);
×
2205
  }
2206

2207
_return:
1,041✔
2208

2209
  taosMemoryFree(pMeta);
1,041!
2210
  taosMemoryFree(stbName);
1,041!
2211

2212
  CTG_RET(code);
1,041!
2213
}
2214

2215
int32_t ctgChkSetBasicAuthRes(SCatalog* pCtg, SCtgAuthReq* req, SCtgAuthRsp* res) {
11,171,995✔
2216
  int32_t          code = 0;
11,171,995✔
2217
  SUserAuthInfo*   pReq = req->pRawReq;
11,171,995✔
2218
  SUserAuthRes*    pRes = res->pRawRes;
11,171,995✔
2219
  SGetUserAuthRsp* pInfo = &req->authInfo;
11,171,995✔
2220

2221
  pRes->pass[AUTH_RES_BASIC] = false;
11,171,995✔
2222
  pRes->pCond[AUTH_RES_BASIC] = NULL;
11,171,995✔
2223

2224
  if (!pInfo->enable) {
11,171,995!
2225
    return TSDB_CODE_SUCCESS;
×
2226
  }
2227

2228
  if (pInfo->superAuth) {
11,171,995!
2229
    pRes->pass[AUTH_RES_BASIC] = true;
11,174,364✔
2230
    return TSDB_CODE_SUCCESS;
11,174,364✔
2231
  }
2232

UNCOV
2233
  if (IS_SYS_DBNAME(pReq->tbName.dbname)) {
×
2234
    pRes->pass[AUTH_RES_BASIC] = true;
257✔
2235
    ctgDebug("sysdb %s, pass", pReq->tbName.dbname);
257✔
2236
    return TSDB_CODE_SUCCESS;
257✔
2237
  }
2238

UNCOV
2239
  if (req->tbNotExists) {
×
2240
    // pRes->pass[AUTH_RES_BASIC] = true;
2241
    // return TSDB_CODE_SUCCESS;
2242
    pReq->tbName.type = TSDB_DB_NAME_T;
42✔
2243
  }
2244

2245
  char dbFName[TSDB_DB_FNAME_LEN];
UNCOV
2246
  (void)tNameGetFullDbName(&pReq->tbName, dbFName);
×
2247

2248
  // since that we add read/write previliges when create db, there is no need to check createdDbs
2249
#if 0
2250
  if (pInfo->createdDbs && taosHashGet(pInfo->createdDbs, dbFName, strlen(dbFName))) {
2251
    pRes->pass = true;
2252
    return TSDB_CODE_SUCCESS;
2253
  }
2254
#endif
2255

2256
  switch (pReq->type) {
1,954!
2257
    case AUTH_TYPE_READ: {
889✔
2258
      if (pReq->tbName.type == TSDB_TABLE_NAME_T && pInfo->readTbs && taosHashGetSize(pInfo->readTbs) > 0) {
889!
2259
        req->singleType = AUTH_TYPE_READ;
426✔
2260
        CTG_ERR_RET(ctgChkSetTbAuthRes(pCtg, req, res));
426!
2261
        if (pRes->pass[AUTH_RES_BASIC] || res->metaNotExists) {
426✔
2262
          return TSDB_CODE_SUCCESS;
297✔
2263
        }
2264
      }
2265

2266
      if (pInfo->readDbs && taosHashGet(pInfo->readDbs, dbFName, strlen(dbFName) + 1)) {
592!
2267
        pRes->pass[AUTH_RES_BASIC] = true;
322✔
2268
        return TSDB_CODE_SUCCESS;
322✔
2269
      }
2270

2271
      break;
270✔
2272
    }
2273
    case AUTH_TYPE_WRITE: {
1,033✔
2274
      if (pReq->tbName.type == TSDB_TABLE_NAME_T && pInfo->writeTbs && taosHashGetSize(pInfo->writeTbs) > 0) {
1,033!
2275
        req->singleType = AUTH_TYPE_WRITE;
615✔
2276
        CTG_ERR_RET(ctgChkSetTbAuthRes(pCtg, req, res));
615!
2277
        if (pRes->pass[AUTH_RES_BASIC] || res->metaNotExists) {
615✔
2278
          return TSDB_CODE_SUCCESS;
451✔
2279
        }
2280
      }
2281

2282
      if (pInfo->writeDbs && taosHashGet(pInfo->writeDbs, dbFName, strlen(dbFName) + 1)) {
582!
2283
        pRes->pass[AUTH_RES_BASIC] = true;
384✔
2284
        return TSDB_CODE_SUCCESS;
384✔
2285
      }
2286

2287
      break;
198✔
2288
    }
2289
    case AUTH_TYPE_READ_OR_WRITE: {
32✔
2290
      if ((pInfo->readDbs && taosHashGet(pInfo->readDbs, dbFName, strlen(dbFName) + 1)) ||
32!
2291
          (pInfo->writeDbs && taosHashGet(pInfo->writeDbs, dbFName, strlen(dbFName) + 1)) ||
17!
2292
          (pInfo->useDbs && taosHashGet(pInfo->useDbs, dbFName, strlen(dbFName) + 1))) {
17!
2293
        pRes->pass[AUTH_RES_BASIC] = true;
18✔
2294
        return TSDB_CODE_SUCCESS;
18✔
2295
      }
2296

2297
      break;
14✔
2298
    }
2299
    default:
×
2300
      break;
×
2301
  }
2302

2303
  return TSDB_CODE_SUCCESS;
482✔
2304
}
2305

2306
int32_t ctgChkSetViewAuthRes(SCatalog* pCtg, SCtgAuthReq* req, SCtgAuthRsp* res) {
11,168,342✔
2307
  int32_t          code = 0;
11,168,342✔
2308
  SUserAuthInfo*   pReq = req->pRawReq;
11,168,342✔
2309
  SUserAuthRes*    pRes = res->pRawRes;
11,168,342✔
2310
  SGetUserAuthRsp* pInfo = &req->authInfo;
11,168,342✔
2311

2312
  pRes->pass[AUTH_RES_VIEW] = false;
11,168,342✔
2313
  pRes->pCond[AUTH_RES_VIEW] = NULL;
11,168,342✔
2314

2315
  if (!pInfo->enable) {
11,168,342!
2316
    return TSDB_CODE_SUCCESS;
×
2317
  }
2318

2319
  if (pInfo->superAuth) {
11,168,342!
2320
    pRes->pass[AUTH_RES_VIEW] = true;
11,169,961✔
2321
    return TSDB_CODE_SUCCESS;
11,169,961✔
2322
  }
2323

UNCOV
2324
  if (pReq->tbName.type != TSDB_TABLE_NAME_T) {
×
2325
    return TSDB_CODE_SUCCESS;
178✔
2326
  }
2327

2328
  char viewFName[TSDB_VIEW_FNAME_LEN];
UNCOV
2329
  if (IS_SYS_DBNAME(req->pRawReq->tbName.dbname)) {
×
2330
    (void)snprintf(viewFName, sizeof(viewFName), "%s.%s", req->pRawReq->tbName.dbname, req->pRawReq->tbName.tname);
257✔
2331
  } else {
UNCOV
2332
    code = tNameExtractFullName(&req->pRawReq->tbName, viewFName);
×
2333
    if (code) {
1,799!
2334
      ctgError("tNameExtractFullName failed, error:%s, type:%d, dbName:%s, tname:%s", tstrerror(code),
×
2335
               req->pRawReq->tbName.type, req->pRawReq->tbName.dbname, req->pRawReq->tbName.tname);
2336
      CTG_ERR_RET(code);
×
2337
    }
2338
  }
2339
  int32_t len = strlen(viewFName) + 1;
2,056✔
2340

2341
  switch (pReq->type) {
2,056!
2342
    case AUTH_TYPE_READ: {
1,149✔
2343
      char* value = taosHashGet(pInfo->readViews, viewFName, len);
1,149✔
2344
      if (NULL != value) {
1,149✔
2345
        pRes->pass[AUTH_RES_VIEW] = true;
57✔
2346
        return TSDB_CODE_SUCCESS;
57✔
2347
      }
2348
      break;
1,092✔
2349
    }
2350
    case AUTH_TYPE_WRITE: {
887✔
2351
      char* value = taosHashGet(pInfo->writeViews, viewFName, len);
887✔
2352
      if (NULL != value) {
887✔
2353
        pRes->pass[AUTH_RES_VIEW] = true;
12✔
2354
        return TSDB_CODE_SUCCESS;
12✔
2355
      }
2356
      break;
875✔
2357
    }
2358
    case AUTH_TYPE_ALTER: {
20✔
2359
      char* value = taosHashGet(pInfo->alterViews, viewFName, len);
20✔
2360
      if (NULL != value) {
20✔
2361
        pRes->pass[AUTH_RES_VIEW] = true;
7✔
2362
        return TSDB_CODE_SUCCESS;
7✔
2363
      }
2364
      break;
13✔
2365
    }
2366
    default:
×
2367
      break;
×
2368
  }
2369

2370
  return TSDB_CODE_SUCCESS;
1,980✔
2371
}
2372

2373
int32_t ctgChkSetAuthRes(SCatalog* pCtg, SCtgAuthReq* req, SCtgAuthRsp* res) {
11,170,081✔
2374
#ifdef TD_ENTERPRISE
2375
  CTG_ERR_RET(ctgChkSetViewAuthRes(pCtg, req, res));
11,170,081!
2376
  if (req->pRawReq->isView) {
11,173,996✔
2377
    return TSDB_CODE_SUCCESS;
490✔
2378
  }
2379
#endif
2380
  CTG_RET(ctgChkSetBasicAuthRes(pCtg, req, res));
11,173,506!
2381
}
2382

2383
#if 0
2384
static int32_t ctgCloneMetaDataArray(SArray* pSrc, __array_item_dup_fn_t copyFunc, SArray** pDst) {
2385
  if (NULL == pSrc) {
2386
    return TSDB_CODE_SUCCESS;
2387
  }
2388

2389
  int32_t size = taosArrayGetSize(pSrc);
2390
  *pDst = taosArrayInit(size, sizeof(SMetaRes));
2391
  if (NULL == *pDst) {
2392
    return terrno;
2393
  }
2394
  for (int32_t i = 0; i < size; ++i) {
2395
    SMetaRes* pRes = taosArrayGet(pSrc, i);
2396
    SMetaRes  res = {.code = pRes->code, .pRes = copyFunc(pRes->pRes)};
2397
    if (NULL == res.pRes) {
2398
      return terrno;
2399
    }
2400
    taosArrayPush(*pDst, &res);
2401
  }
2402

2403
  return TSDB_CODE_SUCCESS;
2404
}
2405

2406
SMetaData* catalogCloneMetaData(SMetaData* pData) {
2407
  SMetaData* pRes = taosMemoryCalloc(1, sizeof(SMetaData));
2408
  if (NULL == pRes) {
2409
    return NULL;
2410
  }
2411

2412
  int32_t code = ctgCloneMetaDataArray(pData->pDbVgroup, ctgCloneDbVgroup, &pRes->pDbVgroup);
2413
  if (TSDB_CODE_SUCCESS == code) {
2414
    code = ctgCloneMetaDataArray(pData->pDbCfg, ctgCloneDbCfgInfo, &pRes->pDbCfg);
2415
  }
2416
  if (TSDB_CODE_SUCCESS == code) {
2417
    code = ctgCloneMetaDataArray(pData->pDbInfo, ctgCloneDbInfo, &pRes->pDbInfo);
2418
  }
2419
  if (TSDB_CODE_SUCCESS == code) {
2420
    code = ctgCloneMetaDataArray(pData->pTableMeta, ctgCloneTableMeta, &pRes->pTableMeta);
2421
  }
2422
  if (TSDB_CODE_SUCCESS == code) {
2423
    code = ctgCloneMetaDataArray(pData->pTableHash, ctgCloneVgroupInfo, &pRes->pTableHash);
2424
  }
2425
  if (TSDB_CODE_SUCCESS == code) {
2426
    code = ctgCloneMetaDataArray(pData->pTableIndex, ctgCloneTableIndices, &pRes->pTableIndex);
2427
  }
2428
  if (TSDB_CODE_SUCCESS == code) {
2429
    code = ctgCloneMetaDataArray(pData->pUdfList, ctgCloneFuncInfo, &pRes->pUdfList);
2430
  }
2431
  if (TSDB_CODE_SUCCESS == code) {
2432
    code = ctgCloneMetaDataArray(pData->pIndex, ctgCloneIndexInfo, &pRes->pIndex);
2433
  }
2434
  if (TSDB_CODE_SUCCESS == code) {
2435
    code = ctgCloneMetaDataArray(pData->pUser, ctgCloneUserAuth, &pRes->pUser);
2436
  }
2437
  if (TSDB_CODE_SUCCESS == code) {
2438
    code = ctgCloneMetaDataArray(pData->pQnodeList, ctgCloneQnodeList, &pRes->pQnodeList);
2439
  }
2440
  if (TSDB_CODE_SUCCESS == code) {
2441
    code = ctgCloneMetaDataArray(pData->pTableCfg, ctgCloneTableCfg, &pRes->pTableCfg);
2442
  }
2443
  if (TSDB_CODE_SUCCESS == code) {
2444
    code = ctgCloneMetaDataArray(pData->pDnodeList, ctgCloneDnodeList, &pRes->pDnodeList);
2445
  }
2446

2447
  if (TSDB_CODE_SUCCESS != code) {
2448
    catalogFreeMetaData(pRes);
2449
    return NULL;
2450
  }
2451

2452
  return pRes;
2453
}
2454
#endif
2455

2456
void ctgDestroySMetaData(SMetaData* pData) {
638✔
2457
  if (NULL == pData) {
638!
2458
    return;
×
2459
  }
2460

2461
  taosArrayDestroyEx(pData->pDbVgroup, ctgFreeDbVgroup);
638✔
2462
  taosArrayDestroyEx(pData->pDbCfg, ctgFreeDbCfgInfo);
638✔
2463
  taosArrayDestroyEx(pData->pDbInfo, ctgFreeDbInfo);
638✔
2464
  taosArrayDestroyEx(pData->pTableMeta, ctgFreeTableMeta);
638✔
2465
  taosArrayDestroyEx(pData->pTableHash, ctgFreeVgroupInfo);
638✔
2466
  taosArrayDestroyEx(pData->pTableIndex, ctgFreeTableIndexs);
638✔
2467
  taosArrayDestroyEx(pData->pUdfList, ctgFreeFuncInfo);
638✔
2468
  taosArrayDestroyEx(pData->pIndex, ctgFreeIndexInfo);
638✔
2469
  taosArrayDestroyEx(pData->pUser, ctgFreeUserAuth);
638✔
2470
  taosArrayDestroyEx(pData->pQnodeList, ctgFreeQnodeList);
638✔
2471
  taosArrayDestroyEx(pData->pTableCfg, ctgFreeTableCfg);
638✔
2472
  taosArrayDestroyEx(pData->pDnodeList, ctgFreeDnodeList);
638✔
2473
  taosArrayDestroyEx(pData->pView, ctgFreeViewMeta);
638✔
2474
  taosArrayDestroyEx(pData->pTableTsmas, ctgFreeTbTSMAInfo);
638✔
2475
  taosArrayDestroyEx(pData->pTsmas, ctgFreeTbTSMAInfo);
638✔
2476
  taosMemoryFreeClear(pData->pSvrVer);
638!
2477
}
2478

2479
uint64_t ctgGetTbIndexCacheSize(STableIndex* pIndex) {
27,901✔
2480
  if (NULL == pIndex) {
27,901✔
2481
    return 0;
27,900✔
2482
  }
2483

2484
  return sizeof(*pIndex) + pIndex->indexSize;
1✔
2485
}
2486

2487
uint64_t ctgGetViewMetaCacheSize(SViewMeta* pMeta) {
560✔
2488
  if (NULL == pMeta) {
560!
2489
    return 0;
×
2490
  }
2491

2492
  return sizeof(*pMeta) + strlen(pMeta->querySql) + 1 + strlen(pMeta->user) + 1 + pMeta->numOfCols * sizeof(SSchema);
560✔
2493
}
2494

2495
FORCE_INLINE uint64_t ctgGetTbMetaCacheSize(STableMeta* pMeta) {
325,541✔
2496
  if (NULL == pMeta) {
325,541!
2497
    return 0;
×
2498
  }
2499

2500
  switch (pMeta->tableType) {
325,541!
2501
    case TSDB_SUPER_TABLE:
32,319✔
2502
      return sizeof(*pMeta) + (pMeta->tableInfo.numOfColumns + pMeta->tableInfo.numOfTags) * sizeof(SSchema);
32,319✔
2503
    case TSDB_CHILD_TABLE:
249,882✔
2504
      return sizeof(SCTableMeta);
249,882✔
2505
    case TSDB_VIRTUAL_CHILD_TABLE:
780✔
2506
      return sizeof(SVCTableMeta);
780✔
2507
    default:
42,560✔
2508
      return sizeof(*pMeta) + pMeta->tableInfo.numOfColumns * sizeof(SSchema);
42,560✔
2509
  }
2510

2511
  return 0;
2512
}
2513

2514
uint64_t ctgGetDbVgroupCacheSize(SDBVgInfo* pVg) {
101,571✔
2515
  if (NULL == pVg) {
101,571✔
2516
    return 0;
316✔
2517
  }
2518

2519
  return sizeof(*pVg) + taosHashGetSize(pVg->vgHash) * (sizeof(SVgroupInfo) + sizeof(int32_t)) +
101,255✔
2520
         taosArrayGetSize(pVg->vgArray) * sizeof(SVgroupInfo);
101,255✔
2521
}
2522

2523
uint64_t ctgGetUserCacheSize(SGetUserAuthRsp* pAuth) {
25,892✔
2524
  if (NULL == pAuth) {
25,892!
2525
    return 0;
×
2526
  }
2527

2528
  uint64_t cacheSize = 0;
25,892✔
2529
  char*    p = taosHashIterate(pAuth->createdDbs, NULL);
25,892✔
2530
  while (p != NULL) {
59,470✔
2531
    size_t len = 0;
33,578✔
2532
    void*  key = taosHashGetKey(p, &len);
33,578✔
2533
    cacheSize += len + strlen(p) + 1;
33,578✔
2534

2535
    p = taosHashIterate(pAuth->createdDbs, p);
33,578✔
2536
  }
2537

2538
  p = taosHashIterate(pAuth->readDbs, NULL);
25,892✔
2539
  while (p != NULL) {
26,155✔
2540
    size_t len = 0;
263✔
2541
    void*  key = taosHashGetKey(p, &len);
263✔
2542
    cacheSize += len + strlen(p) + 1;
263✔
2543

2544
    p = taosHashIterate(pAuth->readDbs, p);
263✔
2545
  }
2546

2547
  p = taosHashIterate(pAuth->writeDbs, NULL);
25,892✔
2548
  while (p != NULL) {
26,153✔
2549
    size_t len = 0;
261✔
2550
    void*  key = taosHashGetKey(p, &len);
261✔
2551
    cacheSize += len + strlen(p) + 1;
261✔
2552

2553
    p = taosHashIterate(pAuth->writeDbs, p);
261✔
2554
  }
2555

2556
  p = taosHashIterate(pAuth->readTbs, NULL);
25,892✔
2557
  while (p != NULL) {
26,203✔
2558
    size_t len = 0;
311✔
2559
    void*  key = taosHashGetKey(p, &len);
311✔
2560
    cacheSize += len + strlen(p) + 1;
311✔
2561

2562
    p = taosHashIterate(pAuth->readTbs, p);
311✔
2563
  }
2564

2565
  p = taosHashIterate(pAuth->writeTbs, NULL);
25,892✔
2566
  while (p != NULL) {
26,206✔
2567
    size_t len = 0;
314✔
2568
    void*  key = taosHashGetKey(p, &len);
314✔
2569
    cacheSize += len + strlen(p) + 1;
314✔
2570

2571
    p = taosHashIterate(pAuth->writeTbs, p);
314✔
2572
  }
2573

2574
  p = taosHashIterate(pAuth->alterTbs, NULL);
25,892✔
2575
  while (p != NULL) {
26,003✔
2576
    size_t len = 0;
111✔
2577
    void*  key = taosHashGetKey(p, &len);
111✔
2578
    cacheSize += len + strlen(p) + 1;
111✔
2579

2580
    p = taosHashIterate(pAuth->alterTbs, p);
111✔
2581
  }
2582

2583
  p = taosHashIterate(pAuth->readViews, NULL);
25,892✔
2584
  while (p != NULL) {
25,969✔
2585
    size_t len = 0;
77✔
2586
    void*  key = taosHashGetKey(p, &len);
77✔
2587
    cacheSize += len + strlen(p) + 1;
77✔
2588

2589
    p = taosHashIterate(pAuth->readViews, p);
77✔
2590
  }
2591

2592
  p = taosHashIterate(pAuth->writeViews, NULL);
25,892✔
2593
  while (p != NULL) {
25,953✔
2594
    size_t len = 0;
61✔
2595
    void*  key = taosHashGetKey(p, &len);
61✔
2596
    cacheSize += len + strlen(p) + 1;
61✔
2597

2598
    p = taosHashIterate(pAuth->writeViews, p);
61✔
2599
  }
2600

2601
  p = taosHashIterate(pAuth->alterViews, NULL);
25,892✔
2602
  while (p != NULL) {
25,956✔
2603
    size_t len = 0;
64✔
2604
    void*  key = taosHashGetKey(p, &len);
64✔
2605
    cacheSize += len + strlen(p) + 1;
64✔
2606

2607
    p = taosHashIterate(pAuth->alterViews, p);
64✔
2608
  }
2609

2610
  int32_t* ref = taosHashIterate(pAuth->useDbs, NULL);
25,892✔
2611
  while (ref != NULL) {
26,370✔
2612
    size_t len = 0;
478✔
2613
    void*  key = taosHashGetKey(ref, &len);
478✔
2614
    cacheSize += len + sizeof(*ref);
478✔
2615

2616
    ref = taosHashIterate(pAuth->useDbs, ref);
478✔
2617
  }
2618

2619
  return cacheSize;
25,892✔
2620
}
2621

2622
uint64_t ctgGetClusterCacheSize(SCatalog* pCtg) {
×
2623
  uint64_t cacheSize = sizeof(SCatalog);
×
2624

2625
  SCtgUserAuth* pAuth = taosHashIterate(pCtg->userCache, NULL);
×
2626
  while (pAuth != NULL) {
×
2627
    size_t len = 0;
×
2628
    void*  key = taosHashGetKey(pAuth, &len);
×
2629
    cacheSize += len + sizeof(SCtgUserAuth) + atomic_load_64(&pAuth->userCacheSize);
×
2630

2631
    pAuth = taosHashIterate(pCtg->userCache, pAuth);
×
2632
  }
2633

2634
  SCtgDBCache* pDb = taosHashIterate(pCtg->dbCache, NULL);
×
2635
  while (pDb != NULL) {
×
2636
    size_t len = 0;
×
2637
    void*  key = taosHashGetKey(pDb, &len);
×
2638
    cacheSize += len + sizeof(SCtgDBCache) + atomic_load_64(&pDb->dbCacheSize);
×
2639

2640
    pDb = taosHashIterate(pCtg->dbCache, pDb);
×
2641
  }
2642

2643
  cacheSize += pCtg->dbRent.rentCacheSize;
×
2644
  cacheSize += pCtg->stbRent.rentCacheSize;
×
2645
  cacheSize += pCtg->viewRent.rentCacheSize;
×
2646

2647
  return cacheSize;
×
2648
}
2649

2650
void ctgGetClusterCacheStat(SCatalog* pCtg) {
×
2651
  for (int32_t i = 0; i < CTG_CI_MAX_VALUE; ++i) {
×
2652
    if (0 == (gCtgStatItem[i].flag & CTG_CI_FLAG_LEVEL_DB)) {
×
2653
      continue;
×
2654
    }
2655

2656
    pCtg->cacheStat.cacheNum[i] = 0;
×
2657
  }
2658

2659
  SCtgDBCache* dbCache = NULL;
×
2660
  void*        pIter = taosHashIterate(pCtg->dbCache, NULL);
×
2661
  while (pIter) {
×
2662
    dbCache = (SCtgDBCache*)pIter;
×
2663

2664
    for (int32_t i = 0; i < CTG_CI_MAX_VALUE; ++i) {
×
2665
      if (0 == (gCtgStatItem[i].flag & CTG_CI_FLAG_LEVEL_DB)) {
×
2666
        continue;
×
2667
      }
2668

2669
      pCtg->cacheStat.cacheNum[i] += dbCache->dbCacheNum[i];
×
2670
    }
2671

2672
    pIter = taosHashIterate(pCtg->dbCache, pIter);
×
2673
  }
2674
}
×
2675

2676
void ctgSummaryClusterCacheStat(SCatalog* pCtg) {
×
2677
  for (int32_t i = 0; i < CTG_CI_MAX_VALUE; ++i) {
×
2678
    if (gCtgStatItem[i].flag & CTG_CI_FLAG_LEVEL_GLOBAL) {
×
2679
      continue;
×
2680
    }
2681

2682
    gCtgMgmt.statInfo.cache.cacheNum[i] += pCtg->cacheStat.cacheNum[i];
×
2683
    gCtgMgmt.statInfo.cache.cacheHit[i] += pCtg->cacheStat.cacheHit[i];
×
2684
    gCtgMgmt.statInfo.cache.cacheNHit[i] += pCtg->cacheStat.cacheNHit[i];
×
2685
  }
2686
}
×
2687

2688
void ctgGetGlobalCacheStat(SCtgCacheStat* pStat) {
×
2689
  for (int32_t i = 0; i < CTG_CI_MAX_VALUE; ++i) {
×
2690
    if (gCtgStatItem[i].flag & CTG_CI_FLAG_LEVEL_GLOBAL) {
×
2691
      continue;
×
2692
    }
2693

2694
    gCtgMgmt.statInfo.cache.cacheNum[i] = 0;
×
2695
    gCtgMgmt.statInfo.cache.cacheHit[i] = 0;
×
2696
    gCtgMgmt.statInfo.cache.cacheNHit[i] = 0;
×
2697
  }
2698

2699
  SCatalog* pCtg = NULL;
×
2700
  void*     pIter = taosHashIterate(gCtgMgmt.pCluster, NULL);
×
2701
  while (pIter) {
×
2702
    pCtg = *(SCatalog**)pIter;
×
2703

2704
    if (pCtg) {
×
2705
      ctgGetClusterCacheStat(pCtg);
×
2706
      ctgSummaryClusterCacheStat(pCtg);
×
2707
    }
2708

2709
    pIter = taosHashIterate(gCtgMgmt.pCluster, pIter);
×
2710
  }
2711

2712
  TAOS_MEMCPY(pStat, &gCtgMgmt.statInfo.cache, sizeof(gCtgMgmt.statInfo.cache));
×
2713
}
×
2714

2715
void ctgGetGlobalCacheSize(uint64_t* pSize) {
×
2716
  *pSize = 0;
×
2717

2718
  SCatalog* pCtg = NULL;
×
2719
  void*     pIter = taosHashIterate(gCtgMgmt.pCluster, NULL);
×
2720
  while (pIter) {
×
2721
    size_t len = 0;
×
2722
    void*  key = taosHashGetKey(pIter, &len);
×
2723
    *pSize += len + POINTER_BYTES;
×
2724

2725
    pCtg = *(SCatalog**)pIter;
×
2726
    if (pCtg) {
×
2727
      *pSize += ctgGetClusterCacheSize(pCtg);
×
2728
    }
2729

2730
    pIter = taosHashIterate(gCtgMgmt.pCluster, pIter);
×
2731
  }
2732
}
×
2733

2734
int32_t ctgBuildViewNullRes(SCtgTask* pTask, SCtgViewsCtx* pCtx) {
1,128,228✔
2735
  SCatalog* pCtg = pTask->pJob->pCtg;
1,128,228✔
2736
  int32_t   dbNum = taosArrayGetSize(pCtx->pNames);
1,128,228✔
2737
  for (int32_t i = 0; i < dbNum; ++i) {
2,258,964✔
2738
    STablesReq* pReq = taosArrayGet(pCtx->pNames, i);
1,130,316✔
2739
    if (NULL == pReq) {
1,130,434!
2740
      qError("fail to get the %dth STablesReq, total:%d", i, (int32_t)taosArrayGetSize(pCtx->pNames));
×
2741
      CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
×
2742
    }
2743

2744
    int32_t viewNum = taosArrayGetSize(pReq->pTables);
1,130,434✔
2745

2746
    ctgDebug("start to check views in db %s, viewNum %d", pReq->dbFName, viewNum);
1,130,497✔
2747

2748
    for (int32_t m = 0; m < viewNum; ++m) {
2,416,916✔
2749
      if (NULL == taosArrayPush(pCtx->pResList, &(SMetaData){0})) {
2,572,749!
2750
        CTG_ERR_RET(terrno);
×
2751
      }
2752
    }
2753
  }
2754

2755
  return TSDB_CODE_SUCCESS;
1,128,648✔
2756
}
2757

2758
int32_t dupViewMetaFromRsp(SViewMetaRsp* pRsp, SViewMeta* pViewMeta) {
634✔
2759
  pViewMeta->querySql = tstrdup(pRsp->querySql);
634✔
2760
  if (NULL == pViewMeta->querySql) {
634!
2761
    CTG_ERR_RET(terrno);
×
2762
  }
2763
  pViewMeta->user = tstrdup(pRsp->user);
634✔
2764
  if (NULL == pViewMeta->user) {
634!
2765
    CTG_ERR_RET(terrno);
×
2766
  }
2767
  pViewMeta->version = pRsp->version;
634✔
2768
  pViewMeta->viewId = pRsp->viewId;
634✔
2769
  pViewMeta->precision = pRsp->precision;
634✔
2770
  pViewMeta->type = pRsp->type;
634✔
2771
  pViewMeta->numOfCols = pRsp->numOfCols;
634✔
2772
  pViewMeta->pSchema = taosMemoryMalloc(pViewMeta->numOfCols * sizeof(SSchema));
634!
2773
  if (pViewMeta->pSchema == NULL) {
634!
2774
    CTG_ERR_RET(terrno);
×
2775
  }
2776

2777
  TAOS_MEMCPY(pViewMeta->pSchema, pRsp->pSchema, pViewMeta->numOfCols * sizeof(SSchema));
634✔
2778

2779
  return TSDB_CODE_SUCCESS;
634✔
2780
}
2781

2782
int32_t ctgBuildUseDbOutput(SUseDbOutput** ppOut, SDBVgInfo* vgInfo) {
×
2783
  *ppOut = taosMemoryCalloc(1, sizeof(SUseDbOutput));
×
2784
  if (NULL == *ppOut) {
×
2785
    CTG_ERR_RET(terrno);
×
2786
  }
2787

2788
  int32_t code = cloneDbVgInfo(vgInfo, &(*ppOut)->dbVgroup);
×
2789
  if (code) {
×
2790
    taosMemoryFreeClear(*ppOut);
×
2791
    CTG_RET(code);
×
2792
  }
2793

2794
  return TSDB_CODE_SUCCESS;
×
2795
}
2796

2797
uint64_t ctgGetTbTSMACacheSize(STableTSMAInfo* pTsmaInfo) {
3,380✔
2798
  if (!pTsmaInfo) {
3,380!
2799
    return 0;
×
2800
  }
2801
  uint64_t size = sizeof(STableTSMAInfo);
3,380✔
2802
  if (pTsmaInfo->pFuncs) {
3,380!
2803
    size += sizeof(STableTSMAFuncInfo) * pTsmaInfo->pFuncs->size;
3,380✔
2804
  }
2805
  if (pTsmaInfo->pTags) {
3,380!
2806
    size += sizeof(SSchema) * pTsmaInfo->pTags->size;
3,380✔
2807
  }
2808
  if (pTsmaInfo->pUsedCols) {
3,380!
2809
    size += sizeof(SSchema) * pTsmaInfo->pUsedCols->size;
3,380✔
2810
  }
2811

2812
  return size;
3,380✔
2813
}
2814

2815
bool hasOutOfDateTSMACache(SArray* pTsmas) {
1,452✔
2816
  if (!pTsmas || pTsmas->size == 0) {
1,452!
2817
    return false;
×
2818
  }
2819
  for (int32_t i = 0; i < pTsmas->size; ++i) {
3,907✔
2820
    STSMACache* pTsmaInfo = taosArrayGetP(pTsmas, i);
3,115✔
2821
    if (NULL == pTsmaInfo) {
3,115!
2822
      continue;
×
2823
    }
2824
    if (isCtgTSMACacheOutOfDate(pTsmaInfo)) {
3,115✔
2825
      return true;
660✔
2826
    }
2827
  }
2828

2829
  return false;
792✔
2830
}
2831

2832
bool isCtgTSMACacheOutOfDate(STSMACache* pTsmaCache) {
3,115✔
2833
  int64_t now = taosGetTimestampMs();
3,115✔
2834
  bool    ret = !pTsmaCache->fillHistoryFinished ||
5,570✔
2835
             (tsMaxTsmaCalcDelay * 1000 - pTsmaCache->delayDuration) < (now - pTsmaCache->reqTs);
2,455!
2836
  if (ret) {
3,115✔
2837
    qDebug("tsma:%s.%s in cache has been out of date, history finished:%d, remain valid after:%" PRId64
660!
2838
           " passed:%" PRId64,
2839
           pTsmaCache->dbFName, pTsmaCache->name, pTsmaCache->fillHistoryFinished,
2840
           tsMaxTsmaCalcDelay * 1000 - pTsmaCache->delayDuration, now - pTsmaCache->reqTs);
2841
  }
2842
  return ret;
3,115✔
2843
}
2844

2845
int32_t ctgAddTSMAFetch(SArray** pFetchs, int32_t dbIdx, int32_t tbIdx, int32_t* fetchIdx, int32_t resIdx, int32_t flag,
1,201✔
2846
                        CTG_TSMA_FETCH_TYPE fetchType, const SName* sourceTbName) {
2847
  if (NULL == (*pFetchs)) {
1,201!
2848
    *pFetchs = taosArrayInit(CTG_DEFAULT_FETCH_NUM, sizeof(SCtgTSMAFetch));
1,201✔
2849
    if (NULL == *pFetchs) {
1,201!
2850
      CTG_ERR_RET(terrno);
×
2851
    }
2852
  }
2853

2854
  SCtgTSMAFetch fetch = {0};
1,201✔
2855
  fetch.dbIdx = dbIdx;
1,201✔
2856
  fetch.tbIdx = tbIdx;
1,201✔
2857
  fetch.fetchIdx = (*fetchIdx)++;
1,201✔
2858
  fetch.resIdx = resIdx;
1,201✔
2859

2860
  fetch.flag = flag;
1,201✔
2861
  fetch.fetchType = fetchType;
1,201✔
2862
  if (sourceTbName) {
1,201✔
2863
    fetch.tsmaSourceTbName = *sourceTbName;
796✔
2864
  }
2865

2866
  if (NULL == taosArrayPush(*pFetchs, &fetch)) {
2,402!
2867
    CTG_ERR_RET(terrno);
×
2868
  }
2869

2870
  return TSDB_CODE_SUCCESS;
1,201✔
2871
}
2872

2873
int32_t ctgBuildNormalChildVtbList(SCtgVSubTablesCtx* pCtx) {
14✔
2874
  int32_t code = TSDB_CODE_SUCCESS, line = 0;
14✔
2875
  char tbFName[TSDB_TABLE_FNAME_LEN];
2876
  pCtx->pResList = taosMemoryCalloc(1, sizeof(*pCtx->pResList));
14!
2877
  QUERY_CHECK_NULL(pCtx->pResList, code, line, _return, terrno);
14!
2878

2879
  pCtx->pResList->vgId = pCtx->pMeta->vgId;
14✔
2880

2881
  pCtx->pResList->pTables = taosArrayInit(1, POINTER_BYTES);
14✔
2882
  QUERY_CHECK_NULL(pCtx->pResList->pTables, code, line, _return, terrno);
14!
2883

2884
  SSHashObj* pSrcTbls = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
14✔
2885
  QUERY_CHECK_NULL(pSrcTbls, code, line, _return, terrno);
14!
2886

2887
  int32_t refColsNum = 0;
14✔
2888
  for (int32_t i = 0; i < pCtx->pMeta->numOfColRefs; ++i) {
206✔
2889
    if (!pCtx->pMeta->colRef[i].hasRef) {
192✔
2890
      continue;
18✔
2891
    }
2892
    
2893
    refColsNum++;
174✔
2894
  }
2895
  
2896
  SVCTableRefCols* pTb = (SVCTableRefCols*)taosMemoryCalloc(1, refColsNum * sizeof(SRefColInfo) + sizeof(SVCTableRefCols));
14!
2897
  QUERY_CHECK_NULL(pTb, code, line, _return, terrno);
14!
2898
  pTb->uid = pCtx->pMeta->uid;
14✔
2899
  pTb->numOfColRefs = refColsNum;
14✔
2900
  pTb->refCols = (SRefColInfo*)(pTb + 1);
14✔
2901
  
2902
  refColsNum = 0;
14✔
2903
  for (int32_t j = 0; j < pCtx->pMeta->numOfColRefs; j++) {
206✔
2904
    if (!pCtx->pMeta->colRef[j].hasRef) {
192✔
2905
      continue;
18✔
2906
    }
2907

2908
    pTb->refCols[refColsNum].colId = pCtx->pMeta->colRef[j].id;
174✔
2909
    tstrncpy(pTb->refCols[refColsNum].refColName, pCtx->pMeta->colRef[j].refColName, TSDB_COL_NAME_LEN);
174✔
2910
    tstrncpy(pTb->refCols[refColsNum].refTableName, pCtx->pMeta->colRef[j].refTableName, TSDB_TABLE_NAME_LEN);
174✔
2911
    tstrncpy(pTb->refCols[refColsNum].refDbName,pCtx->pMeta->colRef[j].refDbName, TSDB_DB_NAME_LEN);
174✔
2912

2913
    snprintf(tbFName, sizeof(tbFName), "%s.%s", pTb->refCols[refColsNum].refDbName, pTb->refCols[refColsNum].refTableName);
174✔
2914

2915
    if (NULL == tSimpleHashGet(pSrcTbls, tbFName, strlen(tbFName))) {
174✔
2916
      QUERY_CHECK_CODE(tSimpleHashPut(pSrcTbls, tbFName, strlen(tbFName), &code, sizeof(code)), line, _return);
36!
2917
    }
2918
    
2919
    refColsNum++;
174✔
2920
  }
2921

2922
  pTb->numOfSrcTbls = tSimpleHashGetSize(pSrcTbls);
14✔
2923
  QUERY_CHECK_NULL(taosArrayPush(pCtx->pResList->pTables, &pTb), code, line, _return, terrno);
28!
2924
  pTb = NULL;
14✔
2925

2926
_return:
14✔
2927

2928
  tSimpleHashCleanup(pSrcTbls);
14✔
2929
  taosMemoryFree(pTb);
14!
2930
  
2931
  if (code) {
14!
2932
    qError("%s failed since %s", __func__, tstrerror(code));
×
2933
  }
2934

2935
  return code;
14✔
2936
}
2937

2938

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc