ctgUtil.c 52.1 KB
Newer Older
D
dapan1121 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "catalogInt.h"
H
Hongze Cheng 已提交
17
#include "query.h"
D
dapan1121 已提交
18
#include "systable.h"
H
Hongze Cheng 已提交
19 20
#include "tname.h"
#include "trpc.h"
D
dapan1121 已提交
21

22 23 24 25 26 27 28
void ctgFreeMsgSendParam(void* param) {
  if (NULL == param) {
    return;
  }

  SCtgTaskCallbackParam* pParam = (SCtgTaskCallbackParam*)param;
  taosArrayDestroy(pParam->taskId);
D
dapan1121 已提交
29
  taosArrayDestroy(pParam->msgIdx);
30 31 32

  taosMemoryFree(param);
}
D
dapan1121 已提交
33

D
dapan1121 已提交
34 35 36 37 38 39 40 41
void ctgFreeBatchMsg(void* msg) {
  if (NULL == msg) {
    return;
  }
  SBatchMsg* pMsg = (SBatchMsg*)msg;
  taosMemoryFree(pMsg->msg);
}

H
Hongze Cheng 已提交
42
void ctgFreeBatch(SCtgBatch* pBatch) {
D
dapan1121 已提交
43 44 45
  if (NULL == pBatch) {
    return;
  }
H
Hongze Cheng 已提交
46

D
dapan1121 已提交
47
  taosArrayDestroyEx(pBatch->pMsgs, ctgFreeBatchMsg);
D
dapan1121 已提交
48 49 50
  taosArrayDestroy(pBatch->pTaskIds);
}

H
Hongze Cheng 已提交
51
void ctgFreeBatchs(SHashObj* pBatchs) {
D
dapan1121 已提交
52 53 54 55 56 57 58 59 60 61 62 63
  void* p = taosHashIterate(pBatchs, NULL);
  while (NULL != p) {
    SCtgBatch* pBatch = (SCtgBatch*)p;

    ctgFreeBatch(pBatch);

    p = taosHashIterate(pBatchs, p);
  }

  taosHashCleanup(pBatchs);
}

H
Hongze Cheng 已提交
64
char* ctgTaskTypeStr(CTG_TASK_TYPE type) {
D
dapan1121 已提交
65 66 67
  switch (type) {
    case CTG_TASK_GET_QNODE:
      return "[get qnode list]";
D
dapan1121 已提交
68 69
    case CTG_TASK_GET_DNODE:
      return "[get dnode list]";
D
dapan1121 已提交
70 71 72 73 74 75 76 77 78 79
    case CTG_TASK_GET_DB_VGROUP:
      return "[get db vgroup]";
    case CTG_TASK_GET_DB_CFG:
      return "[get db cfg]";
    case CTG_TASK_GET_DB_INFO:
      return "[get db info]";
    case CTG_TASK_GET_TB_META:
      return "[get table meta]";
    case CTG_TASK_GET_TB_HASH:
      return "[get table hash]";
D
dapan1121 已提交
80 81
    case CTG_TASK_GET_TB_SMA_INDEX:
      return "[get table sma]";
D
dapan1121 已提交
82 83
    case CTG_TASK_GET_TB_CFG:
      return "[get table cfg]";
D
dapan1121 已提交
84 85
    case CTG_TASK_GET_INDEX_INFO:
      return "[get index info]";
D
dapan1121 已提交
86 87 88 89
    case CTG_TASK_GET_UDF:
      return "[get udf]";
    case CTG_TASK_GET_USER:
      return "[get user]";
D
dapan1121 已提交
90 91
    case CTG_TASK_GET_SVR_VER:
      return "[get svr ver]";
D
dapan1121 已提交
92 93
    case CTG_TASK_GET_TB_META_BATCH:
      return "[bget table meta]";
94 95
    case CTG_TASK_GET_TB_HASH_BATCH:
      return "[bget table hash]";
D
dapan1121 已提交
96 97 98 99 100
    default:
      return "unknown";
  }
}

H
Hongze Cheng 已提交
101
void ctgFreeQNode(SCtgQNode* node) {
102 103 104 105 106 107 108 109
  if (NULL == node) {
    return;
  }

  if (node->op) {
    taosMemoryFree(node->op->data);
    taosMemoryFree(node->op);
  }
H
Hongze Cheng 已提交
110

111
  taosMemoryFree(node);
112 113
}

H
Hongze Cheng 已提交
114
void ctgFreeSTableIndex(void* info) {
D
dapan1121 已提交
115 116 117 118
  if (NULL == info) {
    return;
  }

H
Hongze Cheng 已提交
119
  STableIndex* pInfo = (STableIndex*)info;
D
dapan1121 已提交
120 121 122 123

  taosArrayDestroyEx(pInfo->pIndex, tFreeSTableIndexInfo);
}

D
dapan1121 已提交
124 125 126
void ctgFreeSMetaData(SMetaData* pData) {
  taosArrayDestroy(pData->pTableMeta);
  pData->pTableMeta = NULL;
D
dapan1121 已提交
127

H
Hongze Cheng 已提交
128 129 130 131 132 133
  /*
    for (int32_t i = 0; i < taosArrayGetSize(pData->pDbVgroup); ++i) {
      SArray** pArray = taosArrayGet(pData->pDbVgroup, i);
      taosArrayDestroy(*pArray);
    }
  */
D
dapan1121 已提交
134 135
  taosArrayDestroy(pData->pDbVgroup);
  pData->pDbVgroup = NULL;
H
Hongze Cheng 已提交
136

D
dapan1121 已提交
137 138
  taosArrayDestroy(pData->pTableHash);
  pData->pTableHash = NULL;
D
dapan1121 已提交
139 140 141

  taosArrayDestroy(pData->pTableIndex);
  pData->pTableIndex = NULL;
H
Hongze Cheng 已提交
142

D
dapan1121 已提交
143 144
  taosArrayDestroy(pData->pUdfList);
  pData->pUdfList = NULL;
D
dapan1121 已提交
145

H
Hongze Cheng 已提交
146 147 148 149 150 151
  /*
    for (int32_t i = 0; i < taosArrayGetSize(pData->pDbCfg); ++i) {
      SDbCfgInfo* pInfo = taosArrayGet(pData->pDbCfg, i);
      taosArrayDestroy(pInfo->pRetensions);
    }
  */
D
dapan1121 已提交
152 153
  taosArrayDestroy(pData->pDbCfg);
  pData->pDbCfg = NULL;
D
dapan1121 已提交
154 155 156

  taosArrayDestroy(pData->pDbInfo);
  pData->pDbInfo = NULL;
H
Hongze Cheng 已提交
157

D
dapan1121 已提交
158 159
  taosArrayDestroy(pData->pIndex);
  pData->pIndex = NULL;
H
Hongze Cheng 已提交
160

D
dapan1121 已提交
161 162
  taosArrayDestroy(pData->pUser);
  pData->pUser = NULL;
H
Hongze Cheng 已提交
163

D
dapan1121 已提交
164 165
  taosArrayDestroy(pData->pQnodeList);
  pData->pQnodeList = NULL;
D
dapan1121 已提交
166

D
dapan1121 已提交
167 168 169
  taosArrayDestroy(pData->pDnodeList);
  pData->pDnodeList = NULL;

D
dapan1121 已提交
170 171
  taosArrayDestroy(pData->pTableCfg);
  pData->pTableCfg = NULL;
D
dapan1121 已提交
172

X
Xiaoyu Wang 已提交
173 174 175
  taosArrayDestroy(pData->pTableTag);
  pData->pTableTag = NULL;

D
dapan1121 已提交
176
  taosMemoryFreeClear(pData->pSvrVer);
D
dapan1121 已提交
177 178
}

H
Hongze Cheng 已提交
179
void ctgFreeSCtgUserAuth(SCtgUserAuth* userCache) {
D
dapan1121 已提交
180 181 182 183 184
  taosHashCleanup(userCache->userAuth.createdDbs);
  taosHashCleanup(userCache->userAuth.readDbs);
  taosHashCleanup(userCache->userAuth.writeDbs);
  taosHashCleanup(userCache->userAuth.readTbs);
  taosHashCleanup(userCache->userAuth.writeTbs);
X
Xiaoyu Wang 已提交
185
  taosHashCleanup(userCache->userAuth.useDbs);
D
dapan1121 已提交
186 187
}

H
Hongze Cheng 已提交
188
void ctgFreeMetaRent(SCtgRentMgmt* mgmt) {
D
dapan1121 已提交
189 190 191 192 193
  if (NULL == mgmt->slots) {
    return;
  }

  for (int32_t i = 0; i < mgmt->slotNum; ++i) {
H
Hongze Cheng 已提交
194
    SCtgRentSlot* slot = &mgmt->slots[i];
D
dapan1121 已提交
195 196 197 198 199 200 201
    if (slot->meta) {
      taosArrayDestroy(slot->meta);
      slot->meta = NULL;
    }
  }

  taosMemoryFreeClear(mgmt->slots);
D
dapan1121 已提交
202
  mgmt->rentCacheSize = 0;
D
dapan1121 已提交
203 204
}

H
Hongze Cheng 已提交
205
void ctgFreeStbMetaCache(SCtgDBCache* dbCache) {
D
dapan1121 已提交
206 207 208 209
  if (NULL == dbCache->stbCache) {
    return;
  }

H
Hongze Cheng 已提交
210
  int32_t stbNum = taosHashGetSize(dbCache->stbCache);
D
dapan1121 已提交
211 212 213 214
  taosHashCleanup(dbCache->stbCache);
  dbCache->stbCache = NULL;
}

D
dapan1121 已提交
215 216 217 218 219 220 221 222 223 224 225
void ctgFreeTbCacheImpl(SCtgTbCache* pCache, bool lock) {
  if (pCache->pMeta) {
    if (lock) { 
      CTG_LOCK(CTG_WRITE, &pCache->metaLock);
    }
    taosMemoryFreeClear(pCache->pMeta);
    if (lock) { 
      CTG_UNLOCK(CTG_WRITE, &pCache->metaLock);
    }
  }

D
dapan1121 已提交
226
  if (pCache->pIndex) {
D
dapan1121 已提交
227 228 229
    if (lock) { 
      CTG_LOCK(CTG_WRITE, &pCache->indexLock);
    }
D
dapan1121 已提交
230 231
    taosArrayDestroyEx(pCache->pIndex->pIndex, tFreeSTableIndexInfo);
    taosMemoryFreeClear(pCache->pIndex);
D
dapan1121 已提交
232 233 234
    if (lock) { 
      CTG_UNLOCK(CTG_WRITE, &pCache->indexLock);
    }
D
dapan1121 已提交
235 236
  }
}
D
dapan1121 已提交
237

H
Hongze Cheng 已提交
238
void ctgFreeTbCache(SCtgDBCache* dbCache) {
D
dapan1121 已提交
239 240
  if (NULL == dbCache->tbCache) {
    return;
D
dapan1121 已提交
241 242
  }

H
Hongze Cheng 已提交
243 244
  int32_t      tblNum = taosHashGetSize(dbCache->tbCache);
  SCtgTbCache* pCache = taosHashIterate(dbCache->tbCache, NULL);
D
dapan1121 已提交
245
  while (NULL != pCache) {
D
dapan1121 已提交
246
    ctgFreeTbCacheImpl(pCache, false);
D
dapan1121 已提交
247
    pCache = taosHashIterate(dbCache->tbCache, pCache);
D
dapan1121 已提交
248
  }
D
dapan1121 已提交
249 250
  taosHashCleanup(dbCache->tbCache);
  dbCache->tbCache = NULL;
D
dapan1121 已提交
251 252
}

253
void ctgFreeVgInfoCache(SCtgDBCache* dbCache) { freeVgInfo(dbCache->vgCache.vgInfo); }
254
void ctgFreeCfgInfoCache(SCtgDBCache* dbCache) { freeDbCfgInfo(dbCache->cfgCache.cfgInfo); }
D
dapan1121 已提交
255

H
Hongze Cheng 已提交
256
void ctgFreeDbCache(SCtgDBCache* dbCache) {
D
dapan1121 已提交
257 258 259 260
  if (NULL == dbCache) {
    return;
  }

D
dapan1121 已提交
261
  ctgFreeVgInfoCache(dbCache);
262
  ctgFreeCfgInfoCache(dbCache);
D
dapan1121 已提交
263 264
  ctgFreeStbMetaCache(dbCache);
  ctgFreeTbCache(dbCache);
D
dapan1121 已提交
265 266
}

D
dapan1121 已提交
267 268 269 270
void ctgFreeInstDbCache(SHashObj* pDbCache) {
  if (NULL == pDbCache) {
    return;
  }
H
Hongze Cheng 已提交
271

D
dapan1121 已提交
272
  int32_t dbNum = taosHashGetSize(pDbCache);
H
Hongze Cheng 已提交
273 274

  void* pIter = taosHashIterate(pDbCache, NULL);
D
dapan1121 已提交
275
  while (pIter) {
H
Hongze Cheng 已提交
276
    SCtgDBCache* dbCache = pIter;
D
dapan1121 已提交
277 278
    atomic_store_8(&dbCache->deleted, 1);
    ctgFreeDbCache(dbCache);
H
Hongze Cheng 已提交
279

D
dapan1121 已提交
280
    pIter = taosHashIterate(pDbCache, pIter);
H
Hongze Cheng 已提交
281
  }
D
dapan1121 已提交
282 283 284

  taosHashCleanup(pDbCache);
}
D
dapan1121 已提交
285

D
dapan1121 已提交
286 287 288
void ctgFreeInstUserCache(SHashObj* pUserCache) {
  if (NULL == pUserCache) {
    return;
D
dapan1121 已提交
289
  }
H
Hongze Cheng 已提交
290

D
dapan1121 已提交
291
  int32_t userNum = taosHashGetSize(pUserCache);
H
Hongze Cheng 已提交
292 293

  void* pIter = taosHashIterate(pUserCache, NULL);
D
dapan1121 已提交
294
  while (pIter) {
H
Hongze Cheng 已提交
295
    SCtgUserAuth* userCache = pIter;
D
dapan1121 已提交
296
    ctgFreeSCtgUserAuth(userCache);
H
Hongze Cheng 已提交
297

D
dapan1121 已提交
298
    pIter = taosHashIterate(pUserCache, pIter);
H
Hongze Cheng 已提交
299 300
  }

D
dapan1121 已提交
301 302
  taosHashCleanup(pUserCache);
}
D
dapan1121 已提交
303

D
dapan1121 已提交
304 305 306
void ctgFreeHandleImpl(SCatalog* pCtg) {
  ctgFreeMetaRent(&pCtg->dbRent);
  ctgFreeMetaRent(&pCtg->stbRent);
D
dapan1121 已提交
307

D
dapan1121 已提交
308 309
  ctgFreeInstDbCache(pCtg->dbCache);
  ctgFreeInstUserCache(pCtg->userCache);
D
dapan1121 已提交
310 311 312 313

  taosMemoryFree(pCtg);
}

D
dapan1121 已提交
314 315 316 317 318
void ctgFreeHandle(SCatalog* pCtg) {
  if (NULL == pCtg) {
    return;
  }

D
dapan1121 已提交
319
  uint64_t clusterId = pCtg->clusterId;
D
dapan1121 已提交
320

D
dapan1121 已提交
321 322
  ctgFreeMetaRent(&pCtg->dbRent);
  ctgFreeMetaRent(&pCtg->stbRent);
D
dapan1121 已提交
323

D
dapan1121 已提交
324 325 326
  ctgFreeInstDbCache(pCtg->dbCache);
  ctgFreeInstUserCache(pCtg->userCache);

D
dapan1121 已提交
327
  CTG_STAT_NUM_DEC(CTG_CI_CLUSTER, 1);
D
dapan1121 已提交
328

D
dapan1121 已提交
329
  taosMemoryFree(pCtg);
D
dapan1121 已提交
330

C
Cary Xu 已提交
331
  ctgInfo("handle freed, clusterId:0x%" PRIx64, clusterId);
D
dapan1121 已提交
332 333
}

D
dapan1121 已提交
334 335 336 337 338 339 340 341 342 343 344 345
void ctgClearHandleMeta(SCatalog* pCtg, int64_t *pClearedSize, int64_t *pCleardNum, bool *roundDone) {
  int64_t cacheSize = 0;
  void* pIter = taosHashIterate(pCtg->dbCache, NULL);
  while (pIter) {
    SCtgDBCache* dbCache = pIter;
    
    SCtgTbCache* pCache = taosHashIterate(dbCache->tbCache, NULL);
    while (NULL != pCache) {
      size_t len = 0;
      void*  key = taosHashGetKey(pCache, &len);

      if (pCache->pMeta && TSDB_SUPER_TABLE == pCache->pMeta->tableType) {
D
dapan1121 已提交
346
        pCache = taosHashIterate(dbCache->tbCache, pCache);
D
dapan1121 已提交
347 348 349 350 351 352 353 354
        continue;
      }
      
      taosHashRemove(dbCache->tbCache, key, len);
      cacheSize = len + sizeof(SCtgTbCache) + ctgGetTbMetaCacheSize(pCache->pMeta) + ctgGetTbIndexCacheSize(pCache->pIndex);
      atomic_sub_fetch_64(&dbCache->dbCacheSize, cacheSize);
      *pClearedSize += cacheSize;
      (*pCleardNum)++;
D
dapan1121 已提交
355 356 357 358

      if (pCache->pMeta) {
        CTG_META_NUM_DEC(pCache->pMeta->tableType);
      }
D
dapan1121 已提交
359 360
      
      ctgFreeTbCacheImpl(pCache, true);
D
dapan1121 已提交
361
      
D
dapan1121 已提交
362
      if (*pCleardNum >= CTG_CLEAR_CACHE_ROUND_TB_NUM) {
D
dapan1121 已提交
363
        taosHashCancelIterate(dbCache->tbCache, pCache);
D
dapan1121 已提交
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
        goto _return;
      }
            
      pCache = taosHashIterate(dbCache->tbCache, pCache);
    }

    pIter = taosHashIterate(pCtg->dbCache, pIter);
  }

_return:

  if (*pCleardNum >= CTG_CLEAR_CACHE_ROUND_TB_NUM) {
    *roundDone = true;
  }
}

D
dapan1121 已提交
380
void ctgClearAllHandleMeta(int64_t *clearedSize, int64_t *clearedNum, bool *roundDone) {
D
dapan1121 已提交
381 382 383 384 385 386 387
  SCatalog *pCtg = NULL;

  void *pIter = taosHashIterate(gCtgMgmt.pCluster, NULL);
  while (pIter) {
    pCtg = *(SCatalog **)pIter;

    if (pCtg) {
D
dapan1121 已提交
388
      ctgClearHandleMeta(pCtg, clearedSize, clearedNum, roundDone);
D
dapan1121 已提交
389 390 391 392 393 394 395 396 397 398
      if (*roundDone) {
        taosHashCancelIterate(gCtgMgmt.pCluster, pIter);
        break;
      }
    }

    pIter = taosHashIterate(gCtgMgmt.pCluster, pIter);
  }
}

D
dapan1121 已提交
399 400 401 402
void ctgClearHandle(SCatalog* pCtg) {
  if (NULL == pCtg) {
    return;
  }
D
dapan1121 已提交
403

D
dapan1121 已提交
404
  uint64_t clusterId = pCtg->clusterId;
D
dapan1121 已提交
405
  
D
dapan1121 已提交
406 407 408 409 410 411
  ctgFreeMetaRent(&pCtg->dbRent);
  ctgFreeMetaRent(&pCtg->stbRent);

  ctgFreeInstDbCache(pCtg->dbCache);
  ctgFreeInstUserCache(pCtg->userCache);

D
dapan1121 已提交
412
  ctgMetaRentInit(&pCtg->dbRent, gCtgMgmt.cfg.dbRentSec, CTG_RENT_DB, sizeof(SDbCacheInfo));
D
dapan1121 已提交
413
  ctgMetaRentInit(&pCtg->stbRent, gCtgMgmt.cfg.stbRentSec, CTG_RENT_STABLE, sizeof(SSTableVersion));
H
Hongze Cheng 已提交
414 415 416

  pCtg->dbCache = taosHashInit(gCtgMgmt.cfg.maxDBCacheNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false,
                               HASH_ENTRY_LOCK);
D
dapan1121 已提交
417 418 419
  if (NULL == pCtg->dbCache) {
    qError("taosHashInit %d dbCache failed", CTG_DEFAULT_CACHE_DB_NUMBER);
  }
H
Hongze Cheng 已提交
420 421 422

  pCtg->userCache = taosHashInit(gCtgMgmt.cfg.maxUserCacheNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false,
                                 HASH_ENTRY_LOCK);
D
dapan1121 已提交
423 424 425 426
  if (NULL == pCtg->userCache) {
    ctgError("taosHashInit %d user cache failed", gCtgMgmt.cfg.maxUserCacheNum);
  }

D
dapan1121 已提交
427 428 429
  memset(pCtg->cacheStat.cacheNum, 0, sizeof(pCtg->cacheStat.cacheNum));

  CTG_STAT_RT_INC(numOfOpClearCache, 1);
D
dapan1121 已提交
430

C
Cary Xu 已提交
431
  ctgInfo("handle cleared, clusterId:0x%" PRIx64, clusterId);
D
dapan1121 已提交
432
}
D
dapan1121 已提交
433

D
dapan1121 已提交
434
void ctgFreeSUseDbOutput(SUseDbOutput* pOutput) {
D
dapan1121 已提交
435
  if (NULL == pOutput) {
D
dapan1121 已提交
436 437 438
    return;
  }

D
dapan1121 已提交
439
  if (pOutput->dbVgroup) {
440
    freeVgInfo(pOutput->dbVgroup);
D
dapan1121 已提交
441
  }
H
Hongze Cheng 已提交
442

D
dapan1121 已提交
443 444 445 446 447 448 449 450
  taosMemoryFree(pOutput);
}

void ctgFreeMsgCtx(SCtgMsgCtx* pCtx) {
  taosMemoryFreeClear(pCtx->target);
  if (NULL == pCtx->out) {
    return;
  }
H
Hongze Cheng 已提交
451

D
dapan1121 已提交
452 453 454 455 456 457 458
  switch (pCtx->reqType) {
    case TDMT_MND_GET_DB_CFG: {
      SDbCfgInfo* pOut = (SDbCfgInfo*)pCtx->out;
      taosArrayDestroy(pOut->pRetensions);
      taosMemoryFreeClear(pCtx->out);
      break;
    }
H
Hongze Cheng 已提交
459
    case TDMT_MND_USE_DB: {
D
dapan1121 已提交
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
      SUseDbOutput* pOut = (SUseDbOutput*)pCtx->out;
      ctgFreeSUseDbOutput(pOut);
      pCtx->out = NULL;
      break;
    }
    case TDMT_MND_GET_INDEX: {
      SIndexInfo* pOut = (SIndexInfo*)pCtx->out;
      taosMemoryFreeClear(pCtx->out);
      break;
    }
    case TDMT_MND_QNODE_LIST: {
      SArray* pOut = (SArray*)pCtx->out;
      taosArrayDestroy(pOut);
      pCtx->out = NULL;
      break;
    }
    case TDMT_VND_TABLE_META:
    case TDMT_MND_TABLE_META: {
      STableMetaOutput* pOut = (STableMetaOutput*)pCtx->out;
      taosMemoryFree(pOut->tbMeta);
      taosMemoryFreeClear(pCtx->out);
      break;
    }
D
dapan1121 已提交
483
    case TDMT_MND_GET_TABLE_INDEX: {
D
dapan1121 已提交
484
      STableIndex* pOut = (STableIndex*)pCtx->out;
D
dapan1121 已提交
485
      if (pOut) {
D
dapan1121 已提交
486
        taosArrayDestroyEx(pOut->pIndex, tFreeSTableIndexInfo);
D
dapan1121 已提交
487 488 489 490
        taosMemoryFreeClear(pCtx->out);
      }
      break;
    }
D
dapan1121 已提交
491 492 493 494 495 496 497
    case TDMT_VND_TABLE_CFG:
    case TDMT_MND_TABLE_CFG: {
      STableCfgRsp* pOut = (STableCfgRsp*)pCtx->out;
      tFreeSTableCfgRsp(pOut);
      taosMemoryFreeClear(pCtx->out);
      break;
    }
D
dapan1121 已提交
498 499 500 501 502 503 504 505 506 507 508 509
    case TDMT_MND_RETRIEVE_FUNC: {
      SFuncInfo* pOut = (SFuncInfo*)pCtx->out;
      taosMemoryFree(pOut->pCode);
      taosMemoryFree(pOut->pComment);
      taosMemoryFreeClear(pCtx->out);
      break;
    }
    case TDMT_MND_GET_USER_AUTH: {
      SGetUserAuthRsp* pOut = (SGetUserAuthRsp*)pCtx->out;
      taosHashCleanup(pOut->createdDbs);
      taosHashCleanup(pOut->readDbs);
      taosHashCleanup(pOut->writeDbs);
X
Xiaoyu Wang 已提交
510 511
      taosHashCleanup(pOut->readTbs);
      taosHashCleanup(pOut->writeTbs);
X
Xiaoyu Wang 已提交
512
      taosHashCleanup(pOut->useDbs);
D
dapan1121 已提交
513 514 515 516 517 518 519 520 521
      taosMemoryFreeClear(pCtx->out);
      break;
    }
    default:
      qError("invalid reqType %d", pCtx->reqType);
      break;
  }
}

D
dapan1121 已提交
522 523 524 525 526 527 528 529
void ctgFreeTbMetasMsgCtx(SCtgMsgCtx* pCtx) {
  ctgFreeMsgCtx(pCtx);
  if (pCtx->lastOut) {
    ctgFreeSTableMetaOutput((STableMetaOutput*)pCtx->lastOut);
    pCtx->lastOut = NULL;
  }
}

D
dapan1121 已提交
530 531 532 533
void ctgFreeSTableMetaOutput(STableMetaOutput* pOutput) {
  if (NULL == pOutput) {
    return;
  }
H
Hongze Cheng 已提交
534

D
dapan1121 已提交
535 536 537 538 539 540 541 542
  taosMemoryFree(pOutput->tbMeta);
  taosMemoryFree(pOutput);
}

void ctgResetTbMetaTask(SCtgTask* pTask) {
  SCtgTbMetaCtx* taskCtx = (SCtgTbMetaCtx*)pTask->taskCtx;
  memset(&taskCtx->tbInfo, 0, sizeof(taskCtx->tbInfo));
  taskCtx->flag = CTG_FLAG_UNKNOWN_STB;
H
Hongze Cheng 已提交
543

D
dapan1121 已提交
544 545 546 547 548 549 550 551 552 553 554 555
  if (pTask->msgCtx.lastOut) {
    ctgFreeSTableMetaOutput((STableMetaOutput*)pTask->msgCtx.lastOut);
    pTask->msgCtx.lastOut = NULL;
  }
  if (pTask->msgCtx.out) {
    ctgFreeSTableMetaOutput((STableMetaOutput*)pTask->msgCtx.out);
    pTask->msgCtx.out = NULL;
  }
  taosMemoryFreeClear(pTask->msgCtx.target);
  taosMemoryFreeClear(pTask->res);
}

D
dapan1121 已提交
556 557 558 559
void ctgFreeBatchMeta(void* meta) {
  if (NULL == meta) {
    return;
  }
H
Hongze Cheng 已提交
560

D
dapan1121 已提交
561 562 563 564
  SMetaRes* pRes = (SMetaRes*)meta;
  taosMemoryFreeClear(pRes->pRes);
}

565 566 567 568
void ctgFreeBatchHash(void* hash) {
  if (NULL == hash) {
    return;
  }
H
Hongze Cheng 已提交
569

570 571 572 573
  SMetaRes* pRes = (SMetaRes*)hash;
  taosMemoryFreeClear(pRes->pRes);
}

X
Xiaoyu Wang 已提交
574
void ctgFreeJsonTagVal(void* val) {
575 576 577 578
  if (NULL == val) {
    return;
  }

X
Xiaoyu Wang 已提交
579
  STagVal* pVal = (STagVal*)val;
580 581 582 583 584 585

  if (TSDB_DATA_TYPE_JSON == pVal->type) {
    taosMemoryFree(pVal->pData);
  }
}

H
Hongze Cheng 已提交
586
void ctgFreeTaskRes(CTG_TASK_TYPE type, void** pRes) {
D
dapan1121 已提交
587
  switch (type) {
D
dapan1121 已提交
588 589
    case CTG_TASK_GET_QNODE:
    case CTG_TASK_GET_DNODE:
D
dapan1121 已提交
590 591 592 593 594 595 596 597 598 599 600 601 602
    case CTG_TASK_GET_DB_VGROUP: {
      taosArrayDestroy((SArray*)*pRes);
      *pRes = NULL;
      break;
    }
    case CTG_TASK_GET_DB_CFG: {
      if (*pRes) {
        SDbCfgInfo* pInfo = (SDbCfgInfo*)*pRes;
        taosArrayDestroy(pInfo->pRetensions);
        taosMemoryFreeClear(*pRes);
      }
      break;
    }
D
dapan1121 已提交
603
    case CTG_TASK_GET_TB_SMA_INDEX: {
D
dapan1121 已提交
604 605 606 607
      taosArrayDestroyEx(*pRes, tFreeSTableIndexInfo);
      *pRes = NULL;
      break;
    }
D
dapan1121 已提交
608 609 610 611 612 613 614 615
    case CTG_TASK_GET_TB_CFG: {
      if (*pRes) {
        STableCfg* pInfo = (STableCfg*)*pRes;
        tFreeSTableCfgRsp(pInfo);
        taosMemoryFreeClear(*pRes);
      }
      break;
    }
D
dapan1121 已提交
616 617
    case CTG_TASK_GET_USER: {
      if (*pRes) {
X
Xiaoyu Wang 已提交
618
        SUserAuthRes* pAuth = (SUserAuthRes*)*pRes;
D
dapan1121 已提交
619 620 621 622 623
        nodesDestroyNode(pAuth->pCond);
        taosMemoryFreeClear(*pRes);
      }
      break;
    }
D
dapan1121 已提交
624 625
    case CTG_TASK_GET_TB_HASH:
    case CTG_TASK_GET_DB_INFO:
D
dapan1121 已提交
626
    case CTG_TASK_GET_INDEX_INFO:
H
Hongze Cheng 已提交
627
    case CTG_TASK_GET_UDF:
D
dapan1121 已提交
628 629
    case CTG_TASK_GET_SVR_VER:
    case CTG_TASK_GET_TB_META: {
D
dapan1121 已提交
630 631 632
      taosMemoryFreeClear(*pRes);
      break;
    }
633 634 635 636 637 638 639
    case CTG_TASK_GET_TB_TAG: {
      if (1 == taosArrayGetSize(*pRes)) {
        taosArrayDestroyEx(*pRes, ctgFreeJsonTagVal);
      } else {
        taosArrayDestroy(*pRes);
      }
      *pRes = NULL;
X
Xiaoyu Wang 已提交
640
      break;
641
    }
D
dapan1121 已提交
642 643 644 645 646 647
    case CTG_TASK_GET_TB_META_BATCH: {
      SArray* pArray = (SArray*)*pRes;
      int32_t num = taosArrayGetSize(pArray);
      for (int32_t i = 0; i < num; ++i) {
        ctgFreeBatchMeta(taosArrayGet(pArray, i));
      }
H
Hongze Cheng 已提交
648
      *pRes = NULL;  // no need to free it
D
dapan1121 已提交
649 650
      break;
    }
651 652 653 654 655 656
    case CTG_TASK_GET_TB_HASH_BATCH: {
      SArray* pArray = (SArray*)*pRes;
      int32_t num = taosArrayGetSize(pArray);
      for (int32_t i = 0; i < num; ++i) {
        ctgFreeBatchHash(taosArrayGet(pArray, i));
      }
H
Hongze Cheng 已提交
657
      *pRes = NULL;  // no need to free it
658
      break;
H
Hongze Cheng 已提交
659
    }
D
dapan1121 已提交
660 661 662 663 664 665
    default:
      qError("invalid task type %d", type);
      break;
  }
}

H
Hongze Cheng 已提交
666
void ctgFreeSubTaskRes(CTG_TASK_TYPE type, void** pRes) {
D
dapan1121 已提交
667
  switch (type) {
D
dapan1121 已提交
668
    case CTG_TASK_GET_QNODE:
D
dapan1121 已提交
669 670 671 672 673
    case CTG_TASK_GET_DNODE: {
      taosArrayDestroy((SArray*)*pRes);
      *pRes = NULL;
      break;
    }
D
dapan1121 已提交
674 675 676
    case CTG_TASK_GET_DB_VGROUP: {
      if (*pRes) {
        SDBVgInfo* pInfo = (SDBVgInfo*)*pRes;
677
        freeVgInfo(pInfo);
D
dapan1121 已提交
678 679 680 681 682 683 684 685 686 687 688
      }
      break;
    }
    case CTG_TASK_GET_DB_CFG: {
      if (*pRes) {
        SDbCfgInfo* pInfo = (SDbCfgInfo*)*pRes;
        taosArrayDestroy(pInfo->pRetensions);
        taosMemoryFreeClear(*pRes);
      }
      break;
    }
D
dapan1121 已提交
689
    case CTG_TASK_GET_TB_SMA_INDEX: {
D
dapan1121 已提交
690 691 692 693
      taosArrayDestroyEx(*pRes, tFreeSTableIndexInfo);
      *pRes = NULL;
      break;
    }
D
dapan1121 已提交
694 695 696 697 698 699 700 701
    case CTG_TASK_GET_TB_CFG: {
      if (*pRes) {
        STableCfg* pInfo = (STableCfg*)*pRes;
        tFreeSTableCfgRsp(pInfo);
        taosMemoryFreeClear(*pRes);
      }
      break;
    }
H
Hongze Cheng 已提交
702
    case CTG_TASK_GET_TB_META:
D
dapan1121 已提交
703 704
    case CTG_TASK_GET_DB_INFO:
    case CTG_TASK_GET_TB_HASH:
D
dapan1121 已提交
705
    case CTG_TASK_GET_INDEX_INFO:
H
Hongze Cheng 已提交
706 707
    case CTG_TASK_GET_UDF:
    case CTG_TASK_GET_SVR_VER:
D
dapan1121 已提交
708 709 710 711
    case CTG_TASK_GET_USER: {
      taosMemoryFreeClear(*pRes);
      break;
    }
D
dapan1121 已提交
712 713 714 715 716
    case CTG_TASK_GET_TB_META_BATCH: {
      taosArrayDestroyEx(*pRes, ctgFreeBatchMeta);
      *pRes = NULL;
      break;
    }
717 718 719 720 721
    case CTG_TASK_GET_TB_HASH_BATCH: {
      taosArrayDestroyEx(*pRes, ctgFreeBatchHash);
      *pRes = NULL;
      break;
    }
D
dapan1121 已提交
722 723 724 725 726 727
    default:
      qError("invalid task type %d", type);
      break;
  }
}

H
Hongze Cheng 已提交
728
void ctgClearSubTaskRes(SCtgSubRes* pRes) {
D
dapan1121 已提交
729 730 731 732 733 734 735 736 737 738
  pRes->code = 0;

  if (NULL == pRes->res) {
    return;
  }

  ctgFreeSubTaskRes(pRes->type, &pRes->res);
}

void ctgFreeTaskCtx(SCtgTask* pTask) {
D
dapan1121 已提交
739 740 741 742 743 744 745 746
  switch (pTask->type) {
    case CTG_TASK_GET_TB_META: {
      SCtgTbMetaCtx* taskCtx = (SCtgTbMetaCtx*)pTask->taskCtx;
      taosMemoryFreeClear(taskCtx->pName);
      if (pTask->msgCtx.lastOut) {
        ctgFreeSTableMetaOutput((STableMetaOutput*)pTask->msgCtx.lastOut);
        pTask->msgCtx.lastOut = NULL;
      }
D
dapan1121 已提交
747
      taosMemoryFreeClear(pTask->taskCtx);
D
dapan1121 已提交
748 749
      break;
    }
D
dapan1121 已提交
750
    case CTG_TASK_GET_TB_META_BATCH: {
751
      SCtgTbMetasCtx* taskCtx = (SCtgTbMetasCtx*)pTask->taskCtx;
752
      taosArrayDestroyEx(taskCtx->pResList, ctgFreeBatchMeta);
D
dapan1121 已提交
753 754 755
      taosArrayDestroy(taskCtx->pFetchs);
      // NO NEED TO FREE pNames

D
dapan1121 已提交
756
      taosArrayDestroyEx(pTask->msgCtxs, (FDelete)ctgFreeTbMetasMsgCtx);
H
Hongze Cheng 已提交
757

D
dapan1121 已提交
758 759 760 761 762 763 764
      if (pTask->msgCtx.lastOut) {
        ctgFreeSTableMetaOutput((STableMetaOutput*)pTask->msgCtx.lastOut);
        pTask->msgCtx.lastOut = NULL;
      }
      taosMemoryFreeClear(pTask->taskCtx);
      break;
    }
D
dapan1121 已提交
765 766 767
    case CTG_TASK_GET_TB_HASH: {
      SCtgTbHashCtx* taskCtx = (SCtgTbHashCtx*)pTask->taskCtx;
      taosMemoryFreeClear(taskCtx->pName);
H
Hongze Cheng 已提交
768
      taosMemoryFreeClear(pTask->taskCtx);
D
dapan1121 已提交
769 770
      break;
    }
771
    case CTG_TASK_GET_TB_HASH_BATCH: {
772
      SCtgTbHashsCtx* taskCtx = (SCtgTbHashsCtx*)pTask->taskCtx;
773 774 775 776 777
      taosArrayDestroyEx(taskCtx->pResList, ctgFreeBatchHash);
      taosArrayDestroy(taskCtx->pFetchs);
      // NO NEED TO FREE pNames

      taosArrayDestroyEx(pTask->msgCtxs, (FDelete)ctgFreeMsgCtx);
H
Hongze Cheng 已提交
778

779 780
      taosMemoryFreeClear(pTask->taskCtx);
      break;
H
Hongze Cheng 已提交
781
    }
D
dapan1121 已提交
782
    case CTG_TASK_GET_TB_SMA_INDEX: {
D
dapan1121 已提交
783 784 785
      SCtgTbIndexCtx* taskCtx = (SCtgTbIndexCtx*)pTask->taskCtx;
      taosMemoryFreeClear(taskCtx->pName);
      taosMemoryFreeClear(pTask->taskCtx);
D
dapan1121 已提交
786 787 788 789 790 791 792
      break;
    }
    case CTG_TASK_GET_TB_CFG: {
      SCtgTbCfgCtx* taskCtx = (SCtgTbCfgCtx*)pTask->taskCtx;
      taosMemoryFreeClear(taskCtx->pName);
      taosMemoryFreeClear(taskCtx->pVgInfo);
      taosMemoryFreeClear(pTask->taskCtx);
D
dapan1121 已提交
793 794
      break;
    }
795 796 797 798
    case CTG_TASK_GET_TB_TAG: {
      SCtgTbTagCtx* taskCtx = (SCtgTbTagCtx*)pTask->taskCtx;
      taosMemoryFreeClear(taskCtx->pName);
      taosMemoryFreeClear(taskCtx->pVgInfo);
X
Xiaoyu Wang 已提交
799
      taosMemoryFreeClear(taskCtx);
800 801
      break;
    }
D
dapan1121 已提交
802 803
    case CTG_TASK_GET_DB_VGROUP:
    case CTG_TASK_GET_DB_CFG:
H
Hongze Cheng 已提交
804
    case CTG_TASK_GET_DB_INFO:
D
dapan1121 已提交
805
    case CTG_TASK_GET_INDEX_INFO:
D
dapan1121 已提交
806
    case CTG_TASK_GET_UDF:
H
Hongze Cheng 已提交
807
    case CTG_TASK_GET_QNODE:
D
dapan1121 已提交
808 809 810 811 812 813 814 815 816 817
    case CTG_TASK_GET_USER: {
      taosMemoryFreeClear(pTask->taskCtx);
      break;
    }
    default:
      qError("invalid task type %d", pTask->type);
      break;
  }
}

D
dapan1121 已提交
818 819 820 821 822 823 824 825 826
void ctgFreeTask(SCtgTask* pTask) {
  ctgFreeMsgCtx(&pTask->msgCtx);
  ctgFreeTaskRes(pTask->type, &pTask->res);
  ctgFreeTaskCtx(pTask);

  taosArrayDestroy(pTask->pParents);
  ctgClearSubTaskRes(&pTask->subRes);
}

D
dapan1121 已提交
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
void ctgFreeTasks(SArray* pArray) {
  if (NULL == pArray) {
    return;
  }

  int32_t num = taosArrayGetSize(pArray);
  for (int32_t i = 0; i < num; ++i) {
    SCtgTask* pTask = taosArrayGet(pArray, i);
    ctgFreeTask(pTask);
  }

  taosArrayDestroy(pArray);
}

void ctgFreeJob(void* job) {
  if (NULL == job) {
    return;
  }
H
Hongze Cheng 已提交
845

D
dapan1121 已提交
846 847
  SCtgJob* pJob = (SCtgJob*)job;

H
Hongze Cheng 已提交
848
  int64_t  rid = pJob->refId;
D
dapan1121 已提交
849 850 851
  uint64_t qid = pJob->queryId;

  ctgFreeTasks(pJob->pTasks);
D
dapan1121 已提交
852
  ctgFreeBatchs(pJob->pBatchs);
D
dapan1121 已提交
853 854 855 856 857

  ctgFreeSMetaData(&pJob->jobRes);

  taosMemoryFree(job);

D
dapan1121 已提交
858
  qDebug("QID:0x%" PRIx64 ", ctg job 0x%" PRIx64 " freed", qid, rid);
D
dapan1121 已提交
859 860 861 862 863 864 865 866
}

int32_t ctgUpdateMsgCtx(SCtgMsgCtx* pCtx, int32_t reqType, void* out, char* target) {
  ctgFreeMsgCtx(pCtx);

  pCtx->reqType = reqType;
  pCtx->out = out;
  if (target) {
867
    pCtx->target = taosStrdup(target);
D
dapan1121 已提交
868 869 870 871 872 873 874 875 876 877
    if (NULL == pCtx->target) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  } else {
    pCtx->target = NULL;
  }

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
878 879
int32_t ctgAddMsgCtx(SArray* pCtxs, int32_t reqType, void* out, char* target) {
  SCtgMsgCtx ctx = {0};
H
Hongze Cheng 已提交
880

D
dapan1121 已提交
881 882 883
  ctx.reqType = reqType;
  ctx.out = out;
  if (target) {
884
    ctx.target = taosStrdup(target);
D
dapan1121 已提交
885 886 887 888 889 890 891 892 893 894
    if (NULL == ctx.target) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

  taosArrayPush(pCtxs, &ctx);

  return TSDB_CODE_SUCCESS;
}

H
Hongze Cheng 已提交
895
int32_t ctgGetHashFunction(int8_t hashMethod, tableNameHashFp* fp) {
D
dapan1121 已提交
896 897 898 899 900 901 902 903 904
  switch (hashMethod) {
    default:
      *fp = MurmurHash3_32;
      break;
  }

  return TSDB_CODE_SUCCESS;
}

H
Hongze Cheng 已提交
905 906 907 908 909 910
int32_t ctgGenerateVgList(SCatalog* pCtg, SHashObj* vgHash, SArray** pList) {
  SHashObj*    vgroupHash = NULL;
  SVgroupInfo* vgInfo = NULL;
  SArray*      vgList = NULL;
  int32_t      code = 0;
  int32_t      vgNum = taosHashGetSize(vgHash);
D
dapan1121 已提交
911 912 913 914

  vgList = taosArrayInit(vgNum, sizeof(SVgroupInfo));
  if (NULL == vgList) {
    ctgError("taosArrayInit failed, num:%d", vgNum);
H
Hongze Cheng 已提交
915
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
916 917
  }

H
Hongze Cheng 已提交
918
  void* pIter = taosHashIterate(vgHash, NULL);
D
dapan1121 已提交
919 920 921 922 923
  while (pIter) {
    vgInfo = pIter;

    if (NULL == taosArrayPush(vgList, vgInfo)) {
      ctgError("taosArrayPush failed, vgId:%d", vgInfo->vgId);
H
Hongze Cheng 已提交
924
      taosHashCancelIterate(vgHash, pIter);
D
dapan1121 已提交
925
      CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
926
    }
H
Hongze Cheng 已提交
927

D
dapan1121 已提交
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
    pIter = taosHashIterate(vgHash, pIter);
    vgInfo = NULL;
  }

  *pList = vgList;

  ctgDebug("Got vgList from cache, vgNum:%d", vgNum);

  return TSDB_CODE_SUCCESS;

_return:

  if (vgList) {
    taosArrayDestroy(vgList);
  }

  CTG_RET(code);
}

947
int ctgVgInfoComp(const void* lp, const void* rp) {
D
dapan1121 已提交
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
  SVgroupInfo* pLeft = (SVgroupInfo*)lp;
  SVgroupInfo* pRight = (SVgroupInfo*)rp;
  if (pLeft->hashBegin < pRight->hashBegin) {
    return -1;
  } else if (pLeft->hashBegin > pRight->hashBegin) {
    return 1;
  }

  return 0;
}

int32_t ctgHashValueComp(void const* lp, void const* rp) {
  uint32_t*    key = (uint32_t*)lp;
  SVgroupInfo* pVg = (SVgroupInfo*)rp;

  if (*key < pVg->hashBegin) {
    return -1;
  } else if (*key > pVg->hashEnd) {
    return 1;
  }

  return 0;
}

D
dapan1121 已提交
972
int32_t ctgGetVgInfoFromHashValue(SCatalog* pCtg, SEpSet* pMgmtEps, SDBVgInfo* dbInfo, const SName* pTableName, SVgroupInfo* pVgroup) {
D
dapan1121 已提交
973
  int32_t code = 0;
D
dapan1121 已提交
974
  CTG_ERR_RET(ctgMakeVgArray(dbInfo));
H
Hongze Cheng 已提交
975

976
  int32_t vgNum = taosArrayGetSize(dbInfo->vgArray);
H
Hongze Cheng 已提交
977
  char    db[TSDB_DB_FNAME_LEN] = {0};
D
dapan1121 已提交
978 979
  tNameGetFullDbName(pTableName, db);

D
dapan1121 已提交
980 981 982 983 984 985 986 987
  if (IS_SYS_DBNAME(pTableName->dbname)) {
    pVgroup->vgId = MNODE_HANDLE;
    if (pMgmtEps) {
      memcpy(&pVgroup->epSet, pMgmtEps, sizeof(pVgroup->epSet));
    }
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
988 989 990 991 992
  if (vgNum <= 0) {
    ctgError("db vgroup cache invalid, db:%s, vgroup number:%d", db, vgNum);
    CTG_ERR_RET(TSDB_CODE_TSC_DB_NOT_SELECTED);
  }

H
Hongze Cheng 已提交
993 994
  SVgroupInfo* vgInfo = NULL;
  char         tbFullName[TSDB_TABLE_FNAME_LEN];
D
dapan1121 已提交
995 996
  tNameExtractFullName(pTableName, tbFullName);

H
Hongze Cheng 已提交
997 998
  uint32_t hashValue = taosGetTbHashVal(tbFullName, (uint32_t)strlen(tbFullName), dbInfo->hashMethod,
                                        dbInfo->hashPrefix, dbInfo->hashSuffix);
D
dapan1121 已提交
999

1000 1001
  vgInfo = taosArraySearch(dbInfo->vgArray, &hashValue, ctgHashValueComp, TD_EQ);

X
Xiaoyu Wang 已提交
1002 1003 1004 1005 1006 1007 1008 1009
  /*
    void* pIter = taosHashIterate(dbInfo->vgHash, NULL);
    while (pIter) {
      vgInfo = pIter;
      if (hashValue >= vgInfo->hashBegin && hashValue <= vgInfo->hashEnd) {
        taosHashCancelIterate(dbInfo->vgHash, pIter);
        break;
      }
H
Hongze Cheng 已提交
1010

X
Xiaoyu Wang 已提交
1011 1012 1013 1014
      pIter = taosHashIterate(dbInfo->vgHash, pIter);
      vgInfo = NULL;
    }
  */
D
dapan1121 已提交
1015 1016

  if (NULL == vgInfo) {
D
dapan1121 已提交
1017 1018
    ctgError("no hash range found for hash value [%u], db:%s, numOfVgId:%d", hashValue, db,
             (int32_t)taosArrayGetSize(dbInfo->vgArray));
D
dapan1121 已提交
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
    CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
  }

  *pVgroup = *vgInfo;

  ctgDebug("Got tb %s hash vgroup, vgId:%d, epNum %d, current %s port %d", tbFullName, vgInfo->vgId,
           vgInfo->epSet.numOfEps, vgInfo->epSet.eps[vgInfo->epSet.inUse].fqdn,
           vgInfo->epSet.eps[vgInfo->epSet.inUse].port);

  CTG_RET(code);
D
dapan1121 已提交
1029 1030
}

D
dapan1121 已提交
1031
int32_t ctgGetVgInfosFromHashValue(SCatalog* pCtg, SEpSet* pMgmgEpSet, SCtgTaskReq* tReq, SDBVgInfo* dbInfo, SCtgTbHashsCtx* pCtx,
H
Hongze Cheng 已提交
1032 1033
                                   char* dbFName, SArray* pNames, bool update) {
  int32_t   code = 0;
D
dapan1121 已提交
1034
  SCtgTask* pTask = tReq->pTask;
H
Hongze Cheng 已提交
1035
  SMetaRes  res = {0};
D
dapan1121 已提交
1036
  SVgroupInfo* vgInfo = NULL;
D
dapan1121 已提交
1037 1038 1039

  CTG_ERR_RET(ctgMakeVgArray(dbInfo));

D
dapan1121 已提交
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
  int32_t      tbNum = taosArrayGetSize(pNames);

  char* pSep = strchr(dbFName, '.');
  if (pSep && IS_SYS_DBNAME(pSep + 1)) {
    SVgroupInfo mgmtInfo = {0};
    mgmtInfo.vgId = MNODE_HANDLE;
    if (pMgmgEpSet) {
      memcpy(&mgmtInfo.epSet, pMgmgEpSet, sizeof(mgmtInfo.epSet));
    }
    for (int32_t i = 0; i < tbNum; ++i) {
      vgInfo = taosMemoryMalloc(sizeof(SVgroupInfo));
      if (NULL == vgInfo) {
        CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
      }

      memcpy(vgInfo, &mgmtInfo, sizeof(mgmtInfo));

      ctgDebug("Got tb hash vgroup, vgId:%d, epNum %d, current %s port %d", vgInfo->vgId, vgInfo->epSet.numOfEps,
               vgInfo->epSet.eps[vgInfo->epSet.inUse].fqdn, vgInfo->epSet.eps[vgInfo->epSet.inUse].port);

      if (update) {
        SCtgFetch* pFetch = taosArrayGet(pCtx->pFetchs, tReq->msgIdx);
        SMetaRes*  pRes = taosArrayGet(pCtx->pResList, pFetch->resIdx + i);
        pRes->pRes = vgInfo;
      } else {
        res.pRes = vgInfo;
        taosArrayPush(pCtx->pResList, &res);
      }
    }
    return TSDB_CODE_SUCCESS;
  }

X
Xiaoyu Wang 已提交
1072
  int32_t vgNum = taosArrayGetSize(dbInfo->vgArray);
1073 1074
  if (vgNum <= 0) {
    ctgError("db vgroup cache invalid, db:%s, vgroup number:%d", dbFName, vgNum);
D
dapan1121 已提交
1075
    CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
1076 1077 1078 1079 1080 1081 1082 1083 1084
  }

  if (1 == vgNum) {
    for (int32_t i = 0; i < tbNum; ++i) {
      vgInfo = taosMemoryMalloc(sizeof(SVgroupInfo));
      if (NULL == vgInfo) {
        CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
      }

1085
      *vgInfo = *(SVgroupInfo*)taosArrayGet(dbInfo->vgArray, 0);
1086 1087

      ctgDebug("Got tb hash vgroup, vgId:%d, epNum %d, current %s port %d", vgInfo->vgId, vgInfo->epSet.numOfEps,
H
Hongze Cheng 已提交
1088
               vgInfo->epSet.eps[vgInfo->epSet.inUse].fqdn, vgInfo->epSet.eps[vgInfo->epSet.inUse].port);
1089 1090

      if (update) {
D
dapan1121 已提交
1091
        SCtgFetch* pFetch = taosArrayGet(pCtx->pFetchs, tReq->msgIdx);
H
Hongze Cheng 已提交
1092
        SMetaRes*  pRes = taosArrayGet(pCtx->pResList, pFetch->resIdx + i);
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
        pRes->pRes = vgInfo;
      } else {
        res.pRes = vgInfo;
        taosArrayPush(pCtx->pResList, &res);
      }
    }

    return TSDB_CODE_SUCCESS;
  }

  char tbFullName[TSDB_TABLE_FNAME_LEN];
  sprintf(tbFullName, "%s.", dbFName);
  int32_t offset = strlen(tbFullName);
H
Hongze Cheng 已提交
1106
  SName*  pName = NULL;
1107
  int32_t tbNameLen = 0;
H
Hongze Cheng 已提交
1108

1109
  for (int32_t i = 0; i < tbNum; ++i) {
1110
    pName = taosArrayGet(pNames, i);
1111 1112 1113 1114

    tbNameLen = offset + strlen(pName->tname);
    strcpy(tbFullName + offset, pName->tname);

H
Hongze Cheng 已提交
1115 1116
    uint32_t hashValue = taosGetTbHashVal(tbFullName, (uint32_t)strlen(tbFullName), dbInfo->hashMethod,
                                          dbInfo->hashPrefix, dbInfo->hashSuffix);
1117

1118 1119
    vgInfo = taosArraySearch(dbInfo->vgArray, &hashValue, ctgHashValueComp, TD_EQ);
    if (NULL == vgInfo) {
D
dapan1121 已提交
1120 1121
      ctgError("no hash range found for hash value [%u], db:%s, numOfVgId:%d", hashValue, dbFName,
               (int32_t)taosArrayGetSize(dbInfo->vgArray));
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
      CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
    }

    SVgroupInfo* pNewVg = taosMemoryMalloc(sizeof(SVgroupInfo));
    if (NULL == pNewVg) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }

    *pNewVg = *vgInfo;

H
Hongze Cheng 已提交
1132 1133 1134
    ctgDebug("Got tb %s hash vgroup, vgId:%d, epNum %d, current %s port %d", tbFullName, vgInfo->vgId,
             vgInfo->epSet.numOfEps, vgInfo->epSet.eps[vgInfo->epSet.inUse].fqdn,
             vgInfo->epSet.eps[vgInfo->epSet.inUse].port);
1135 1136

    if (update) {
D
dapan1121 已提交
1137
      SCtgFetch* pFetch = taosArrayGet(pCtx->pFetchs, tReq->msgIdx);
H
Hongze Cheng 已提交
1138
      SMetaRes*  pRes = taosArrayGet(pCtx->pResList, pFetch->resIdx + i);
1139 1140 1141 1142
      pRes->pRes = pNewVg;
    } else {
      res.pRes = pNewVg;
      taosArrayPush(pCtx->pResList, &res);
H
Hongze Cheng 已提交
1143
    }
1144 1145 1146 1147 1148
  }

  CTG_RET(code);
}

X
Xiaoyu Wang 已提交
1149 1150 1151 1152 1153 1154
int32_t ctgGetVgIdsFromHashValue(SCatalog* pCtg, SDBVgInfo* dbInfo, char* dbFName, const char* pTbs[], int32_t tbNum,
                                 int32_t* vgId) {
  int32_t code = 0;
  CTG_ERR_RET(ctgMakeVgArray(dbInfo));

  int32_t vgNum = taosArrayGetSize(dbInfo->vgArray);
D
dapan1121 已提交
1155

X
Xiaoyu Wang 已提交
1156 1157 1158 1159
  if (vgNum <= 0) {
    ctgError("db vgroup cache invalid, db:%s, vgroup number:%d", dbFName, vgNum);
    CTG_ERR_RET(TSDB_CODE_TSC_DB_NOT_SELECTED);
  }
D
dapan1121 已提交
1160

X
Xiaoyu Wang 已提交
1161 1162 1163 1164
  SVgroupInfo* vgInfo = NULL;
  char         tbFullName[TSDB_TABLE_FNAME_LEN];
  snprintf(tbFullName, sizeof(tbFullName), "%s.", dbFName);
  int32_t offset = strlen(tbFullName);
D
dapan1121 已提交
1165

X
Xiaoyu Wang 已提交
1166 1167 1168 1169
  for (int32_t i = 0; i < tbNum; ++i) {
    snprintf(tbFullName + offset, sizeof(tbFullName) - offset, "%s", pTbs[i]);
    uint32_t hashValue = taosGetTbHashVal(tbFullName, (uint32_t)strlen(tbFullName), dbInfo->hashMethod,
                                          dbInfo->hashPrefix, dbInfo->hashSuffix);
D
dapan1121 已提交
1170

X
Xiaoyu Wang 已提交
1171 1172 1173 1174 1175 1176
    vgInfo = taosArraySearch(dbInfo->vgArray, &hashValue, ctgHashValueComp, TD_EQ);
    if (NULL == vgInfo) {
      ctgError("no hash range found for hash value [%u], db:%s, numOfVgId:%d", hashValue, dbFName,
               (int32_t)taosArrayGetSize(dbInfo->vgArray));
      CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
    }
D
dapan1121 已提交
1177

X
Xiaoyu Wang 已提交
1178
    vgId[i] = vgInfo->vgId;
D
dapan1121 已提交
1179

X
Xiaoyu Wang 已提交
1180 1181
    ctgDebug("Got tb %s vgId:%d", tbFullName, vgInfo->vgId);
  }
D
dapan1121 已提交
1182

X
Xiaoyu Wang 已提交
1183
  CTG_RET(code);
D
dapan1121 已提交
1184 1185
}

D
dapan1121 已提交
1186
int32_t ctgStbVersionSearchCompare(const void* key1, const void* key2) {
H
Hongze Cheng 已提交
1187
  if (*(uint64_t*)key1 < ((SSTableVersion*)key2)->suid) {
D
dapan1121 已提交
1188
    return -1;
H
Hongze Cheng 已提交
1189
  } else if (*(uint64_t*)key1 > ((SSTableVersion*)key2)->suid) {
D
dapan1121 已提交
1190 1191 1192 1193 1194 1195
    return 1;
  } else {
    return 0;
  }
}

D
dapan1121 已提交
1196 1197
int32_t ctgDbCacheInfoSearchCompare(const void* key1, const void* key2) {
  if (*(int64_t*)key1 < ((SDbCacheInfo*)key2)->dbId) {
D
dapan1121 已提交
1198
    return -1;
D
dapan1121 已提交
1199
  } else if (*(int64_t*)key1 > ((SDbCacheInfo*)key2)->dbId) {
D
dapan1121 已提交
1200 1201 1202 1203 1204 1205 1206
    return 1;
  } else {
    return 0;
  }
}

int32_t ctgStbVersionSortCompare(const void* key1, const void* key2) {
D
dapan1121 已提交
1207
  if (((SSTableVersion*)key1)->suid < ((SSTableVersion*)key2)->suid) {
D
dapan1121 已提交
1208
    return -1;
D
dapan1121 已提交
1209
  } else if (((SSTableVersion*)key1)->suid > ((SSTableVersion*)key2)->suid) {
D
dapan1121 已提交
1210 1211 1212 1213 1214 1215
    return 1;
  } else {
    return 0;
  }
}

D
dapan1121 已提交
1216 1217
int32_t ctgDbCacheInfoSortCompare(const void* key1, const void* key2) {
  if (((SDbCacheInfo*)key1)->dbId < ((SDbCacheInfo*)key2)->dbId) {
D
dapan1121 已提交
1218
    return -1;
D
dapan1121 已提交
1219
  } else if (((SDbCacheInfo*)key1)->dbId > ((SDbCacheInfo*)key2)->dbId) {
D
dapan1121 已提交
1220 1221 1222 1223 1224 1225
    return 1;
  } else {
    return 0;
  }
}

1226 1227 1228 1229
int32_t ctgMakeVgArray(SDBVgInfo* dbInfo) {
  if (NULL == dbInfo) {
    return TSDB_CODE_SUCCESS;
  }
X
Xiaoyu Wang 已提交
1230

1231 1232 1233 1234 1235
  if (dbInfo->vgHash && NULL == dbInfo->vgArray) {
    dbInfo->vgArray = taosArrayInit(100, sizeof(SVgroupInfo));
    if (NULL == dbInfo->vgArray) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
X
Xiaoyu Wang 已提交
1236 1237

    void* pIter = taosHashIterate(dbInfo->vgHash, NULL);
1238 1239 1240 1241
    while (pIter) {
      taosArrayPush(dbInfo->vgArray, pIter);
      pIter = taosHashIterate(dbInfo->vgHash, pIter);
    }
X
Xiaoyu Wang 已提交
1242

1243 1244 1245 1246 1247 1248
    taosArraySort(dbInfo->vgArray, ctgVgInfoComp);
  }

  return TSDB_CODE_SUCCESS;
}

H
Hongze Cheng 已提交
1249
int32_t ctgCloneVgInfo(SDBVgInfo* src, SDBVgInfo** dst) {
1250 1251
  CTG_ERR_RET(ctgMakeVgArray(src));

D
dapan1121 已提交
1252 1253 1254
  *dst = taosMemoryMalloc(sizeof(SDBVgInfo));
  if (NULL == *dst) {
    qError("malloc %d failed", (int32_t)sizeof(SDBVgInfo));
D
dapan1121 已提交
1255
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1256 1257 1258 1259 1260 1261 1262 1263 1264
  }

  memcpy(*dst, src, sizeof(SDBVgInfo));

  size_t hashSize = taosHashGetSize(src->vgHash);
  (*dst)->vgHash = taosHashInit(hashSize, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
  if (NULL == (*dst)->vgHash) {
    qError("taosHashInit %d failed", (int32_t)hashSize);
    taosMemoryFreeClear(*dst);
D
dapan1121 已提交
1265
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1266 1267
  }

H
Hongze Cheng 已提交
1268 1269
  int32_t* vgId = NULL;
  void*    pIter = taosHashIterate(src->vgHash, NULL);
D
dapan1121 已提交
1270 1271 1272
  while (pIter) {
    vgId = taosHashGetKey(pIter, NULL);

H
Hongze Cheng 已提交
1273
    if (taosHashPut((*dst)->vgHash, (void*)vgId, sizeof(int32_t), pIter, sizeof(SVgroupInfo))) {
D
dapan1121 已提交
1274 1275 1276 1277
      qError("taosHashPut failed, hashSize:%d", (int32_t)hashSize);
      taosHashCancelIterate(src->vgHash, pIter);
      taosHashCleanup((*dst)->vgHash);
      taosMemoryFreeClear(*dst);
D
dapan1121 已提交
1278
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1279
    }
H
Hongze Cheng 已提交
1280

D
dapan1121 已提交
1281 1282 1283
    pIter = taosHashIterate(src->vgHash, pIter);
  }

1284 1285 1286 1287
  if (src->vgArray) {
    (*dst)->vgArray = taosArrayDup(src->vgArray, NULL);
  }

D
dapan1121 已提交
1288 1289 1290
  return TSDB_CODE_SUCCESS;
}

H
Hongze Cheng 已提交
1291
int32_t ctgCloneMetaOutput(STableMetaOutput* output, STableMetaOutput** pOutput) {
D
dapan1121 已提交
1292 1293 1294
  *pOutput = taosMemoryMalloc(sizeof(STableMetaOutput));
  if (NULL == *pOutput) {
    qError("malloc %d failed", (int32_t)sizeof(STableMetaOutput));
D
dapan1121 已提交
1295
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1296 1297 1298 1299 1300 1301 1302
  }

  memcpy(*pOutput, output, sizeof(STableMetaOutput));

  if (output->tbMeta) {
    int32_t metaSize = CTG_META_SIZE(output->tbMeta);
    (*pOutput)->tbMeta = taosMemoryMalloc(metaSize);
D
dapan1121 已提交
1303
    qDebug("tbMeta cloned, size:%d, p:%p", metaSize, (*pOutput)->tbMeta);
D
dapan1121 已提交
1304 1305 1306
    if (NULL == (*pOutput)->tbMeta) {
      qError("malloc %d failed", (int32_t)sizeof(STableMetaOutput));
      taosMemoryFreeClear(*pOutput);
D
dapan1121 已提交
1307
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1308 1309 1310 1311 1312 1313 1314 1315
    }

    memcpy((*pOutput)->tbMeta, output->tbMeta, metaSize);
  }

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
int32_t ctgCloneTableIndex(SArray* pIndex, SArray** pRes) {
  if (NULL == pIndex) {
    *pRes = NULL;
    return TSDB_CODE_SUCCESS;
  }

  int32_t num = taosArrayGetSize(pIndex);
  *pRes = taosArrayInit(num, sizeof(STableIndexInfo));
  if (NULL == *pRes) {
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }

  for (int32_t i = 0; i < num; ++i) {
H
Hongze Cheng 已提交
1329
    STableIndexInfo* pInfo = taosArrayGet(pIndex, i);
D
dapan1121 已提交
1330
    pInfo = taosArrayPush(*pRes, pInfo);
1331
    pInfo->expr = taosStrdup(pInfo->expr);
D
dapan1121 已提交
1332 1333 1334 1335 1336
  }

  return TSDB_CODE_SUCCESS;
}

H
Hongze Cheng 已提交
1337
int32_t ctgUpdateSendTargetInfo(SMsgSendInfo* pMsgSendInfo, int32_t msgType, char* dbFName, int32_t vgId) {
D
dapan1121 已提交
1338
  if (msgType == TDMT_VND_TABLE_META || msgType == TDMT_VND_TABLE_CFG || msgType == TDMT_VND_BATCH_META) {
D
dapan1121 已提交
1339
    pMsgSendInfo->target.type = TARGET_TYPE_VNODE;
D
dapan1121 已提交
1340
    pMsgSendInfo->target.vgId = vgId;
1341
    pMsgSendInfo->target.dbFName = taosStrdup(dbFName);
D
dapan1121 已提交
1342 1343 1344 1345 1346 1347
  } else {
    pMsgSendInfo->target.type = TARGET_TYPE_MNODE;
  }

  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
1348

H
Hongze Cheng 已提交
1349
int32_t ctgGetTablesReqNum(SArray* pList) {
1350 1351 1352 1353 1354 1355 1356
  if (NULL == pList) {
    return 0;
  }

  int32_t total = 0;
  int32_t n = taosArrayGetSize(pList);
  for (int32_t i = 0; i < n; ++i) {
H
Hongze Cheng 已提交
1357
    STablesReq* pReq = taosArrayGet(pList, i);
1358 1359 1360 1361 1362 1363
    total += taosArrayGetSize(pReq->pTables);
  }

  return total;
}

H
Hongze Cheng 已提交
1364
int32_t ctgAddFetch(SArray** pFetchs, int32_t dbIdx, int32_t tbIdx, int32_t* fetchIdx, int32_t resIdx, int32_t flag) {
1365 1366 1367
  if (NULL == (*pFetchs)) {
    *pFetchs = taosArrayInit(CTG_DEFAULT_FETCH_NUM, sizeof(SCtgFetch));
  }
H
Hongze Cheng 已提交
1368

1369 1370 1371 1372 1373 1374
  SCtgFetch fetch = {0};
  fetch.dbIdx = dbIdx;
  fetch.tbIdx = tbIdx;
  fetch.fetchIdx = (*fetchIdx)++;
  fetch.resIdx = resIdx;
  fetch.flag = flag;
H
Hongze Cheng 已提交
1375

1376 1377 1378 1379 1380
  taosArrayPush(*pFetchs, &fetch);

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1381 1382 1383 1384
SName* ctgGetFetchName(SArray* pNames, SCtgFetch* pFetch) {
  STablesReq* pReq = (STablesReq*)taosArrayGet(pNames, pFetch->dbIdx);
  return (SName*)taosArrayGet(pReq->pTables, pFetch->tbIdx);
}
1385

H
Haojun Liao 已提交
1386
static void* ctgCloneDbVgroup(void* pSrc) { return taosArrayDup((const SArray*)pSrc, NULL); }
1387

H
Hongze Cheng 已提交
1388
static void ctgFreeDbVgroup(void* p) { taosArrayDestroy((SArray*)((SMetaRes*)p)->pRes); }
1389

D
dapan1121 已提交
1390
void* ctgCloneDbCfgInfo(void* pSrc) {
1391 1392 1393 1394 1395
  SDbCfgInfo* pDst = taosMemoryMalloc(sizeof(SDbCfgInfo));
  if (NULL == pDst) {
    return NULL;
  }
  memcpy(pDst, pSrc, sizeof(SDbCfgInfo));
D
dapan1121 已提交
1396
  pDst->pRetensions = taosArrayDup(((SDbCfgInfo *)pSrc)->pRetensions, NULL);
1397 1398 1399
  return pDst;
}

D
dapan1121 已提交
1400 1401 1402 1403
static void ctgFreeDbCfgInfo(void* p) { 
  SDbCfgInfo* pDst = (SDbCfgInfo *)((SMetaRes*)p)->pRes;
  freeDbCfgInfo(pDst);
}
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413

static void* ctgCloneDbInfo(void* pSrc) {
  SDbInfo* pDst = taosMemoryMalloc(sizeof(SDbInfo));
  if (NULL == pDst) {
    return NULL;
  }
  memcpy(pDst, pSrc, sizeof(SDbInfo));
  return pDst;
}

H
Hongze Cheng 已提交
1414
static void ctgFreeDbInfo(void* p) { taosMemoryFree(((SMetaRes*)p)->pRes); }
1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426

static void* ctgCloneTableMeta(void* pSrc) {
  STableMeta* pMeta = pSrc;
  int32_t size = sizeof(STableMeta) + (pMeta->tableInfo.numOfColumns + pMeta->tableInfo.numOfTags) * sizeof(SSchema);
  STableMeta* pDst = taosMemoryMalloc(size);
  if (NULL == pDst) {
    return NULL;
  }
  memcpy(pDst, pSrc, size);
  return pDst;
}

H
Hongze Cheng 已提交
1427
static void ctgFreeTableMeta(void* p) { taosMemoryFree(((SMetaRes*)p)->pRes); }
1428 1429 1430 1431 1432 1433 1434 1435 1436 1437

static void* ctgCloneVgroupInfo(void* pSrc) {
  SVgroupInfo* pDst = taosMemoryMalloc(sizeof(SVgroupInfo));
  if (NULL == pDst) {
    return NULL;
  }
  memcpy(pDst, pSrc, sizeof(SVgroupInfo));
  return pDst;
}

H
Hongze Cheng 已提交
1438
static void ctgFreeVgroupInfo(void* p) { taosMemoryFree(((SMetaRes*)p)->pRes); }
1439

H
Haojun Liao 已提交
1440
static void* ctgCloneTableIndices(void* pSrc) { return taosArrayDup((const SArray*)pSrc, NULL); }
1441

H
Hongze Cheng 已提交
1442
static void ctgFreeTableIndices(void* p) { taosArrayDestroy((SArray*)((SMetaRes*)p)->pRes); }
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452

static void* ctgCloneFuncInfo(void* pSrc) {
  SFuncInfo* pDst = taosMemoryMalloc(sizeof(SFuncInfo));
  if (NULL == pDst) {
    return NULL;
  }
  memcpy(pDst, pSrc, sizeof(SFuncInfo));
  return pDst;
}

H
Hongze Cheng 已提交
1453
static void ctgFreeFuncInfo(void* p) { taosMemoryFree(((SMetaRes*)p)->pRes); }
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463

static void* ctgCloneIndexInfo(void* pSrc) {
  SIndexInfo* pDst = taosMemoryMalloc(sizeof(SIndexInfo));
  if (NULL == pDst) {
    return NULL;
  }
  memcpy(pDst, pSrc, sizeof(SIndexInfo));
  return pDst;
}

H
Hongze Cheng 已提交
1464
static void ctgFreeIndexInfo(void* p) { taosMemoryFree(((SMetaRes*)p)->pRes); }
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474

static void* ctgCloneUserAuth(void* pSrc) {
  bool* pDst = taosMemoryMalloc(sizeof(bool));
  if (NULL == pDst) {
    return NULL;
  }
  *pDst = *(bool*)pSrc;
  return pDst;
}

H
Hongze Cheng 已提交
1475
static void ctgFreeUserAuth(void* p) { taosMemoryFree(((SMetaRes*)p)->pRes); }
1476

H
Haojun Liao 已提交
1477
static void* ctgCloneQnodeList(void* pSrc) { return taosArrayDup((const SArray*)pSrc, NULL); }
D
dapan1121 已提交
1478

H
Hongze Cheng 已提交
1479
static void ctgFreeQnodeList(void* p) { taosArrayDestroy((SArray*)((SMetaRes*)p)->pRes); }
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489

static void* ctgCloneTableCfg(void* pSrc) {
  STableCfg* pDst = taosMemoryMalloc(sizeof(STableCfg));
  if (NULL == pDst) {
    return NULL;
  }
  memcpy(pDst, pSrc, sizeof(STableCfg));
  return pDst;
}

H
Hongze Cheng 已提交
1490
static void ctgFreeTableCfg(void* p) { taosMemoryFree(((SMetaRes*)p)->pRes); }
1491

H
Haojun Liao 已提交
1492
static void* ctgCloneDnodeList(void* pSrc) { return taosArrayDup((const SArray*)pSrc, NULL); }
1493

H
Hongze Cheng 已提交
1494
static void ctgFreeDnodeList(void* p) { taosArrayDestroy((SArray*)((SMetaRes*)p)->pRes); }
1495

X
Xiaoyu Wang 已提交
1496 1497 1498 1499 1500
int32_t ctgChkSetTbAuthRes(SCatalog* pCtg, SCtgAuthReq* req, SCtgAuthRsp* res) {
  int32_t          code = 0;
  STableMeta*      pMeta = NULL;
  SGetUserAuthRsp* pInfo = &req->authInfo;
  SHashObj*        pTbs = (AUTH_TYPE_READ == req->singleType) ? pInfo->readTbs : pInfo->writeTbs;
1501
  char*            stbName = NULL;
X
Xiaoyu Wang 已提交
1502

1503 1504 1505 1506 1507 1508
  char tbFName[TSDB_TABLE_FNAME_LEN];
  char dbFName[TSDB_DB_FNAME_LEN];
  tNameExtractFullName(&req->pRawReq->tbName, tbFName);
  tNameGetFullDbName(&req->pRawReq->tbName, dbFName);

  while (true) {
D
dapan1121 已提交
1509 1510
    taosMemoryFreeClear(pMeta);

1511 1512 1513
    char* pCond = taosHashGet(pTbs, tbFName, strlen(tbFName));
    if (pCond) {
      if (strlen(pCond) > 1) {
D
dapan1121 已提交
1514
        CTG_ERR_JRET(nodesStringToNode(pCond, &res->pRawRes->pCond));
1515
      }
X
Xiaoyu Wang 已提交
1516

1517
      res->pRawRes->pass = true;
D
dapan1121 已提交
1518
      goto _return;
D
dapan1121 已提交
1519
    }
X
Xiaoyu Wang 已提交
1520

1521 1522 1523 1524
    if (stbName) {
      res->pRawRes->pass = false;
      goto _return;
    }
D
dapan1121 已提交
1525

D
dapan1121 已提交
1526
    CTG_ERR_JRET(catalogGetCachedTableMeta(pCtg, &req->pRawReq->tbName, &pMeta));
1527 1528 1529 1530
    if (NULL == pMeta) {
      if (req->onlyCache) {
        res->metaNotExists = true;
        ctgDebug("db %s tb %s meta not in cache for auth", req->pRawReq->tbName.dbname, req->pRawReq->tbName.tname);
D
dapan1121 已提交
1531
        goto _return;
1532
      }
D
dapan1121 已提交
1533

1534 1535 1536
      SCtgTbMetaCtx ctx = {0};
      ctx.pName = (SName*)&req->pRawReq->tbName;
      ctx.flag = CTG_FLAG_UNKNOWN_STB | CTG_FLAG_SYNC_OP;
X
Xiaoyu Wang 已提交
1537

D
dapan1121 已提交
1538
      CTG_ERR_JRET(ctgGetTbMeta(pCtg, req->pConn, &ctx, &pMeta));
1539
    }
D
dapan1121 已提交
1540

1541 1542 1543 1544
    if (TSDB_SUPER_TABLE == pMeta->tableType || TSDB_NORMAL_TABLE == pMeta->tableType) {
      res->pRawRes->pass = false;
      goto _return;
    }
D
dapan1121 已提交
1545

1546 1547 1548 1549 1550 1551
    if (TSDB_CHILD_TABLE == pMeta->tableType) {
      CTG_ERR_JRET(ctgGetCachedStbNameFromSuid(pCtg, dbFName, pMeta->suid, &stbName));
      if (NULL == stbName) {
        if (req->onlyCache) {
          res->metaNotExists = true;
          ctgDebug("suid %" PRIu64 " name not in cache for auth", pMeta->suid);
D
dapan1121 已提交
1552
          goto _return;
1553
        }
D
dapan1121 已提交
1554

1555 1556
        continue;
      }
X
Xiaoyu Wang 已提交
1557

1558 1559 1560
      sprintf(tbFName, "%s.%s", dbFName, stbName);
      continue;
    }
X
Xiaoyu Wang 已提交
1561

1562 1563 1564
    ctgError("Invalid table type %d for %s", pMeta->tableType, tbFName);
    CTG_ERR_JRET(TSDB_CODE_INVALID_PARA);
  }
D
dapan1121 已提交
1565 1566 1567 1568

_return:

  taosMemoryFree(pMeta);
D
dapan1121 已提交
1569
  taosMemoryFree(stbName);
X
Xiaoyu Wang 已提交
1570

D
dapan1121 已提交
1571 1572 1573
  CTG_RET(code);
}

X
Xiaoyu Wang 已提交
1574 1575 1576 1577 1578 1579
int32_t ctgChkSetAuthRes(SCatalog* pCtg, SCtgAuthReq* req, SCtgAuthRsp* res) {
  int32_t          code = 0;
  SUserAuthInfo*   pReq = req->pRawReq;
  SUserAuthRes*    pRes = res->pRawRes;
  SGetUserAuthRsp* pInfo = &req->authInfo;

D
dapan1121 已提交
1580
  pRes->pass = false;
X
Xiaoyu Wang 已提交
1581
  pRes->pCond = NULL;
D
dapan1121 已提交
1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602

  if (!pInfo->enable) {
    pRes->pass = false;
    return TSDB_CODE_SUCCESS;
  }

  if (pInfo->superAuth) {
    pRes->pass = true;
    return TSDB_CODE_SUCCESS;
  }

  char dbFName[TSDB_DB_FNAME_LEN];
  tNameGetFullDbName(&pReq->tbName, dbFName);

  if (pInfo->createdDbs && taosHashGet(pInfo->createdDbs, dbFName, strlen(dbFName))) {
    pRes->pass = true;
    return TSDB_CODE_SUCCESS;
  }

  switch (pReq->type) {
    case AUTH_TYPE_READ: {
1603
      if (pReq->tbName.type == TSDB_TABLE_NAME_T && pInfo->readTbs && taosHashGetSize(pInfo->readTbs) > 0) {
X
Xiaoyu Wang 已提交
1604 1605
        req->singleType = AUTH_TYPE_READ;
        CTG_ERR_RET(ctgChkSetTbAuthRes(pCtg, req, res));
1606
        if (pRes->pass || res->metaNotExists) {
X
Xiaoyu Wang 已提交
1607 1608 1609 1610
          return TSDB_CODE_SUCCESS;
        }
      }

D
dapan1121 已提交
1611 1612 1613 1614
      if (pInfo->readDbs && taosHashGet(pInfo->readDbs, dbFName, strlen(dbFName))) {
        pRes->pass = true;
        return TSDB_CODE_SUCCESS;
      }
X
Xiaoyu Wang 已提交
1615

D
dapan1121 已提交
1616 1617 1618
      break;
    }
    case AUTH_TYPE_WRITE: {
1619
      if (pReq->tbName.type == TSDB_TABLE_NAME_T && pInfo->writeTbs && taosHashGetSize(pInfo->writeTbs) > 0) {
X
Xiaoyu Wang 已提交
1620 1621
        req->singleType = AUTH_TYPE_WRITE;
        CTG_ERR_RET(ctgChkSetTbAuthRes(pCtg, req, res));
1622
        if (pRes->pass || res->metaNotExists) {
X
Xiaoyu Wang 已提交
1623 1624 1625 1626
          return TSDB_CODE_SUCCESS;
        }
      }

D
dapan1121 已提交
1627 1628 1629 1630
      if (pInfo->writeDbs && taosHashGet(pInfo->writeDbs, dbFName, strlen(dbFName))) {
        pRes->pass = true;
        return TSDB_CODE_SUCCESS;
      }
X
Xiaoyu Wang 已提交
1631

D
dapan1121 已提交
1632 1633 1634 1635
      break;
    }
    case AUTH_TYPE_READ_OR_WRITE: {
      if ((pInfo->readDbs && taosHashGet(pInfo->readDbs, dbFName, strlen(dbFName))) ||
X
Xiaoyu Wang 已提交
1636 1637
          (pInfo->writeDbs && taosHashGet(pInfo->writeDbs, dbFName, strlen(dbFName))) ||
          (pInfo->useDbs && taosHashGet(pInfo->useDbs, dbFName, strlen(dbFName)))) {
D
dapan1121 已提交
1638 1639 1640
        pRes->pass = true;
        return TSDB_CODE_SUCCESS;
      }
X
Xiaoyu Wang 已提交
1641

D
dapan1121 已提交
1642 1643 1644 1645 1646 1647 1648 1649 1650
      break;
    }
    default:
      break;
  }

  return TSDB_CODE_SUCCESS;
}

1651
#if 0
H
Haojun Liao 已提交
1652
static int32_t ctgCloneMetaDataArray(SArray* pSrc, __array_item_dup_fn_t copyFunc, SArray** pDst) {
1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663
  if (NULL == pSrc) {
    return TSDB_CODE_SUCCESS;
  }

  int32_t size = taosArrayGetSize(pSrc);
  *pDst = taosArrayInit(size, sizeof(SMetaRes));
  if (NULL == *pDst) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  for (int32_t i = 0; i < size; ++i) {
    SMetaRes* pRes = taosArrayGet(pSrc, i);
H
Hongze Cheng 已提交
1664
    SMetaRes  res = {.code = pRes->code, .pRes = copyFunc(pRes->pRes)};
1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732
    if (NULL == res.pRes) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
    taosArrayPush(*pDst, &res);
  }

  return TSDB_CODE_SUCCESS;
}

SMetaData* catalogCloneMetaData(SMetaData* pData) {
  SMetaData* pRes = taosMemoryCalloc(1, sizeof(SMetaData));
  if (NULL == pRes) {
    return NULL;
  }

  int32_t code = ctgCloneMetaDataArray(pData->pDbVgroup, ctgCloneDbVgroup, &pRes->pDbVgroup);
  if (TSDB_CODE_SUCCESS == code) {
    code = ctgCloneMetaDataArray(pData->pDbCfg, ctgCloneDbCfgInfo, &pRes->pDbCfg);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = ctgCloneMetaDataArray(pData->pDbInfo, ctgCloneDbInfo, &pRes->pDbInfo);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = ctgCloneMetaDataArray(pData->pTableMeta, ctgCloneTableMeta, &pRes->pTableMeta);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = ctgCloneMetaDataArray(pData->pTableHash, ctgCloneVgroupInfo, &pRes->pTableHash);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = ctgCloneMetaDataArray(pData->pTableIndex, ctgCloneTableIndices, &pRes->pTableIndex);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = ctgCloneMetaDataArray(pData->pUdfList, ctgCloneFuncInfo, &pRes->pUdfList);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = ctgCloneMetaDataArray(pData->pIndex, ctgCloneIndexInfo, &pRes->pIndex);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = ctgCloneMetaDataArray(pData->pUser, ctgCloneUserAuth, &pRes->pUser);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = ctgCloneMetaDataArray(pData->pQnodeList, ctgCloneQnodeList, &pRes->pQnodeList);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = ctgCloneMetaDataArray(pData->pTableCfg, ctgCloneTableCfg, &pRes->pTableCfg);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = ctgCloneMetaDataArray(pData->pDnodeList, ctgCloneDnodeList, &pRes->pDnodeList);
  }

  if (TSDB_CODE_SUCCESS != code) {
    catalogFreeMetaData(pRes);
    return NULL;
  }

  return pRes;
}

void catalogFreeMetaData(SMetaData* pData) {
  if (NULL == pData) {
    return;
  }

  taosArrayDestroyEx(pData->pDbVgroup, ctgFreeDbVgroup);
  taosArrayDestroyEx(pData->pDbCfg, ctgFreeDbCfgInfo);
  taosArrayDestroyEx(pData->pDbInfo, ctgFreeDbInfo);
  taosArrayDestroyEx(pData->pTableMeta, ctgFreeTableMeta);
  taosArrayDestroyEx(pData->pTableHash, ctgFreeVgroupInfo);
H
Hongze Cheng 已提交
1733 1734
  taosArrayDestroyEx(pData->pTableIndex, ctgFreeTableIndices);
  taosArrayDestroyEx(pData->pUdfList, ctgFreeFuncInfo);
1735 1736 1737 1738 1739 1740 1741 1742
  taosArrayDestroyEx(pData->pIndex, ctgFreeIndexInfo);
  taosArrayDestroyEx(pData->pUser, ctgFreeUserAuth);
  taosArrayDestroyEx(pData->pQnodeList, ctgFreeQnodeList);
  taosArrayDestroyEx(pData->pTableCfg, ctgFreeTableCfg);
  taosArrayDestroyEx(pData->pDnodeList, ctgFreeDnodeList);
  taosMemoryFreeClear(pData->pSvrVer);
  taosMemoryFree(pData);
}
1743
#endif
D
dapan1121 已提交
1744

D
dapan1121 已提交
1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868
uint64_t ctgGetTbIndexCacheSize(STableIndex *pIndex) {
  if (NULL == pIndex) {
    return 0;
  }

  return sizeof(*pIndex) + pIndex->indexSize;
}

FORCE_INLINE uint64_t ctgGetTbMetaCacheSize(STableMeta *pMeta) {
  if (NULL == pMeta) {
    return 0;
  }

  switch (pMeta->tableType) {
    case TSDB_SUPER_TABLE:
      return sizeof(*pMeta) + (pMeta->tableInfo.numOfColumns + pMeta->tableInfo.numOfTags) * sizeof(SSchema);
    case TSDB_CHILD_TABLE:
      return sizeof(SCTableMeta);
    default:
      return sizeof(*pMeta) + pMeta->tableInfo.numOfColumns * sizeof(SSchema);
  }

  return 0;
}

uint64_t ctgGetDbVgroupCacheSize(SDBVgInfo *pVg) {
  if (NULL == pVg) {
    return 0;
  }

  return sizeof(*pVg) + taosHashGetSize(pVg->vgHash) * (sizeof(SVgroupInfo) + sizeof(int32_t)) 
    + taosArrayGetSize(pVg->vgArray) * sizeof(SVgroupInfo);
}

uint64_t ctgGetUserCacheSize(SGetUserAuthRsp *pAuth) {
  if (NULL == pAuth) {
    return 0;
  }

  uint64_t cacheSize = 0;
  char* p = taosHashIterate(pAuth->createdDbs, NULL);
  while (p != NULL) {
    size_t len = 0;
    void*  key = taosHashGetKey(p, &len);
    cacheSize += len + strlen(p);

    p = taosHashIterate(pAuth->createdDbs, p);
  }

  p = taosHashIterate(pAuth->readDbs, NULL);
  while (p != NULL) {
    size_t len = 0;
    void*  key = taosHashGetKey(p, &len);
    cacheSize += len + strlen(p);

    p = taosHashIterate(pAuth->readDbs, p);
  }  

  p = taosHashIterate(pAuth->writeDbs, NULL);
  while (p != NULL) {
    size_t len = 0;
    void*  key = taosHashGetKey(p, &len);
    cacheSize += len + strlen(p);

    p = taosHashIterate(pAuth->writeDbs, p);
  } 

  p = taosHashIterate(pAuth->readTbs, NULL);
  while (p != NULL) {
    size_t len = 0;
    void*  key = taosHashGetKey(p, &len);
    cacheSize += len + strlen(p);

    p = taosHashIterate(pAuth->readTbs, p);
  } 

  p = taosHashIterate(pAuth->writeTbs, NULL);
  while (p != NULL) {
    size_t len = 0;
    void*  key = taosHashGetKey(p, &len);
    cacheSize += len + strlen(p);

    p = taosHashIterate(pAuth->writeTbs, p);
  } 

  int32_t *ref = taosHashIterate(pAuth->useDbs, NULL);
  while (ref != NULL) {
    size_t len = 0;
    void*  key = taosHashGetKey(ref, &len);
    cacheSize += len + sizeof(*ref);

    ref = taosHashIterate(pAuth->useDbs, ref);
  } 

  return cacheSize;
}

uint64_t ctgGetClusterCacheSize(SCatalog *pCtg) {
  uint64_t cacheSize = sizeof(SCatalog);
  
  SCtgUserAuth* pAuth = taosHashIterate(pCtg->userCache, NULL);
  while (pAuth != NULL) {
    size_t len = 0;
    void*  key = taosHashGetKey(pAuth, &len);
    cacheSize += len + sizeof(SCtgUserAuth) + atomic_load_64(&pAuth->userCacheSize);

    pAuth = taosHashIterate(pCtg->userCache, pAuth);
  }

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

    pDb = taosHashIterate(pCtg->dbCache, pDb);
  }

  cacheSize += pCtg->dbRent.rentCacheSize;
  cacheSize += pCtg->stbRent.rentCacheSize;

  return cacheSize;
}

D
dapan1121 已提交
1869 1870 1871 1872 1873
void ctgGetClusterCacheStat(SCatalog* pCtg) {
  for (int32_t i = 0; i < CTG_CI_MAX_VALUE; ++i) {
    if (0 == (gCtgStatItem[i].flag & CTG_CI_FLAG_LEVEL_DB)) {
      continue;
    }
X
Xiaoyu Wang 已提交
1874

D
dapan1121 已提交
1875 1876
    pCtg->cacheStat.cacheNum[i] = 0;
  }
X
Xiaoyu Wang 已提交
1877 1878 1879

  SCtgDBCache* dbCache = NULL;
  void*        pIter = taosHashIterate(pCtg->dbCache, NULL);
D
dapan1121 已提交
1880
  while (pIter) {
X
Xiaoyu Wang 已提交
1881 1882
    dbCache = (SCtgDBCache*)pIter;

D
dapan1121 已提交
1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894
    for (int32_t i = 0; i < CTG_CI_MAX_VALUE; ++i) {
      if (0 == (gCtgStatItem[i].flag & CTG_CI_FLAG_LEVEL_DB)) {
        continue;
      }

      pCtg->cacheStat.cacheNum[i] += dbCache->dbCacheNum[i];
    }

    pIter = taosHashIterate(pCtg->dbCache, pIter);
  }
}

X
Xiaoyu Wang 已提交
1895
void ctgSummaryClusterCacheStat(SCatalog* pCtg) {
D
dapan1121 已提交
1896 1897 1898 1899
  for (int32_t i = 0; i < CTG_CI_MAX_VALUE; ++i) {
    if (gCtgStatItem[i].flag & CTG_CI_FLAG_LEVEL_GLOBAL) {
      continue;
    }
X
Xiaoyu Wang 已提交
1900

D
dapan1121 已提交
1901 1902 1903 1904 1905 1906
    gCtgMgmt.statInfo.cache.cacheNum[i] += pCtg->cacheStat.cacheNum[i];
    gCtgMgmt.statInfo.cache.cacheHit[i] += pCtg->cacheStat.cacheHit[i];
    gCtgMgmt.statInfo.cache.cacheNHit[i] += pCtg->cacheStat.cacheNHit[i];
  }
}

X
Xiaoyu Wang 已提交
1907
void ctgGetGlobalCacheStat(SCtgCacheStat* pStat) {
D
dapan1121 已提交
1908 1909 1910 1911
  for (int32_t i = 0; i < CTG_CI_MAX_VALUE; ++i) {
    if (gCtgStatItem[i].flag & CTG_CI_FLAG_LEVEL_GLOBAL) {
      continue;
    }
X
Xiaoyu Wang 已提交
1912

D
dapan1121 已提交
1913 1914 1915 1916
    gCtgMgmt.statInfo.cache.cacheNum[i] = 0;
    gCtgMgmt.statInfo.cache.cacheHit[i] = 0;
    gCtgMgmt.statInfo.cache.cacheNHit[i] = 0;
  }
X
Xiaoyu Wang 已提交
1917 1918 1919

  SCatalog* pCtg = NULL;
  void*     pIter = taosHashIterate(gCtgMgmt.pCluster, NULL);
D
dapan1121 已提交
1920
  while (pIter) {
X
Xiaoyu Wang 已提交
1921
    pCtg = *(SCatalog**)pIter;
D
dapan1121 已提交
1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933

    if (pCtg) {
      ctgGetClusterCacheStat(pCtg);
      ctgSummaryClusterCacheStat(pCtg);
    }

    pIter = taosHashIterate(gCtgMgmt.pCluster, pIter);
  }

  memcpy(pStat, &gCtgMgmt.statInfo.cache, sizeof(gCtgMgmt.statInfo.cache));
}

D
dapan1121 已提交
1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950
void ctgGetGlobalCacheSize(uint64_t *pSize) {
  *pSize = 0;

  SCatalog* pCtg = NULL;
  void*     pIter = taosHashIterate(gCtgMgmt.pCluster, NULL);
  while (pIter) {
    size_t len = 0;
    void*  key = taosHashGetKey(pIter, &len);
    *pSize += len + POINTER_BYTES; 
    
    pCtg = *(SCatalog**)pIter;
    if (pCtg) {
      *pSize += ctgGetClusterCacheSize(pCtg);
    }

    pIter = taosHashIterate(gCtgMgmt.pCluster, pIter);
  }
D
dapan1121 已提交
1951 1952
}