ctgUtil.c 30.1 KB
Newer Older
D
dapan1121 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * 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 "trpc.h"
#include "query.h"
#include "tname.h"
#include "catalogInt.h"
#include "systable.h"

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);
}

D
dapan1121 已提交
42 43 44 45 46
void ctgFreeBatch(SCtgBatch *pBatch) {
  if (NULL == pBatch) {
    return;
  }
  
D
dapan1121 已提交
47
  taosArrayDestroyEx(pBatch->pMsgs, ctgFreeBatchMsg);
D
dapan1121 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
  taosArrayDestroy(pBatch->pTaskIds);
}

void ctgFreeBatchs(SHashObj *pBatchs) {
  void* p = taosHashIterate(pBatchs, NULL);
  while (NULL != p) {
    SCtgBatch* pBatch = (SCtgBatch*)p;

    ctgFreeBatch(pBatch);

    p = taosHashIterate(pBatchs, p);
  }

  taosHashCleanup(pBatchs);
}

D
dapan1121 已提交
64 65 66 67
char *ctgTaskTypeStr(CTG_TASK_TYPE type) {
  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 82 83
    case CTG_TASK_GET_TB_INDEX:
      return "[get table index]";
    case CTG_TASK_GET_TB_CFG:
      return "[get table cfg]";
D
dapan1121 已提交
84 85 86 87 88 89
    case CTG_TASK_GET_INDEX:
      return "[get index]";
    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";
  }
}

101
void ctgFreeQNode(SCtgQNode *node) {
102 103 104 105 106 107 108 109 110 111
  if (NULL == node) {
    return;
  }

  if (node->op) {
    taosMemoryFree(node->op->data);
    taosMemoryFree(node->op);
  }
  
  taosMemoryFree(node);
112 113
}

D
dapan1121 已提交
114 115 116 117 118 119 120 121 122 123
void ctgFreeSTableIndex(void *info) {
  if (NULL == info) {
    return;
  }

  STableIndex *pInfo = (STableIndex *)info;

  taosArrayDestroyEx(pInfo->pIndex, tFreeSTableIndexInfo);
}

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

/*  
D
dapan1121 已提交
129 130 131 132
  for (int32_t i = 0; i < taosArrayGetSize(pData->pDbVgroup); ++i) {
    SArray** pArray = taosArrayGet(pData->pDbVgroup, i);
    taosArrayDestroy(*pArray);
  }
D
dapan1121 已提交
133
*/
D
dapan1121 已提交
134 135 136 137 138
  taosArrayDestroy(pData->pDbVgroup);
  pData->pDbVgroup = NULL;
  
  taosArrayDestroy(pData->pTableHash);
  pData->pTableHash = NULL;
D
dapan1121 已提交
139 140 141

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

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

  taosArrayDestroy(pData->pDbInfo);
  pData->pDbInfo = NULL;
D
dapan1121 已提交
157 158 159 160 161 162 163 164 165
  
  taosArrayDestroy(pData->pIndex);
  pData->pIndex = NULL;
  
  taosArrayDestroy(pData->pUser);
  pData->pUser = NULL;
  
  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 173

  taosMemoryFreeClear(pData->pSvrVer);
D
dapan1121 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
}

void ctgFreeSCtgUserAuth(SCtgUserAuth *userCache) {
  taosHashCleanup(userCache->createdDbs);
  taosHashCleanup(userCache->readDbs);
  taosHashCleanup(userCache->writeDbs);
}

void ctgFreeMetaRent(SCtgRentMgmt *mgmt) {
  if (NULL == mgmt->slots) {
    return;
  }

  for (int32_t i = 0; i < mgmt->slotNum; ++i) {
    SCtgRentSlot *slot = &mgmt->slots[i];
    if (slot->meta) {
      taosArrayDestroy(slot->meta);
      slot->meta = NULL;
    }
  }

  taosMemoryFreeClear(mgmt->slots);
}

D
dapan1121 已提交
198 199 200 201 202
void ctgFreeStbMetaCache(SCtgDBCache *dbCache) {
  if (NULL == dbCache->stbCache) {
    return;
  }

D
dapan1121 已提交
203
  int32_t stbNum = taosHashGetSize(dbCache->stbCache);  
D
dapan1121 已提交
204 205
  taosHashCleanup(dbCache->stbCache);
  dbCache->stbCache = NULL;
D
dapan1121 已提交
206
  CTG_CACHE_STAT_DEC(numOfStb, stbNum);
D
dapan1121 已提交
207 208 209
}

void ctgFreeTbCacheImpl(SCtgTbCache *pCache) {
D
dapan1121 已提交
210
  qDebug("tbMeta freed, p:%p", pCache->pMeta);
D
dapan1121 已提交
211 212 213 214 215 216
  taosMemoryFreeClear(pCache->pMeta);
  if (pCache->pIndex) {
    taosArrayDestroyEx(pCache->pIndex->pIndex, tFreeSTableIndexInfo);
    taosMemoryFreeClear(pCache->pIndex);
  }
}
D
dapan1121 已提交
217

D
dapan1121 已提交
218 219 220
void ctgFreeTbCache(SCtgDBCache *dbCache) {
  if (NULL == dbCache->tbCache) {
    return;
D
dapan1121 已提交
221 222
  }

D
dapan1121 已提交
223 224 225 226 227
  int32_t tblNum = taosHashGetSize(dbCache->tbCache);
  SCtgTbCache *pCache = taosHashIterate(dbCache->tbCache, NULL);
  while (NULL != pCache) {
    ctgFreeTbCacheImpl(pCache);
    pCache = taosHashIterate(dbCache->tbCache, pCache);
D
dapan1121 已提交
228
  }
D
dapan1121 已提交
229 230
  taosHashCleanup(dbCache->tbCache);
  dbCache->tbCache = NULL;
D
dapan1121 已提交
231
  CTG_CACHE_STAT_DEC(numOfTbl, tblNum);
D
dapan1121 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
}

void ctgFreeVgInfo(SDBVgInfo *vgInfo) {
  if (NULL == vgInfo) {
    return;
  }

  if (vgInfo->vgHash) {
    taosHashCleanup(vgInfo->vgHash);
    vgInfo->vgHash = NULL;
  }
  
  taosMemoryFreeClear(vgInfo);
}

D
dapan1121 已提交
247 248 249 250
void ctgFreeVgInfoCache(SCtgDBCache *dbCache) {
  ctgFreeVgInfo(dbCache->vgCache.vgInfo);
}

D
dapan1121 已提交
251 252 253 254 255
void ctgFreeDbCache(SCtgDBCache *dbCache) {
  if (NULL == dbCache) {
    return;
  }

D
dapan1121 已提交
256 257 258
  ctgFreeVgInfoCache(dbCache);
  ctgFreeStbMetaCache(dbCache);
  ctgFreeTbCache(dbCache);
D
dapan1121 已提交
259 260
}

D
dapan1121 已提交
261 262 263 264
void ctgFreeInstDbCache(SHashObj* pDbCache) {
  if (NULL == pDbCache) {
    return;
  }
D
dapan1121 已提交
265
  
D
dapan1121 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
  int32_t dbNum = taosHashGetSize(pDbCache);
  
  void *pIter = taosHashIterate(pDbCache, NULL);
  while (pIter) {
    SCtgDBCache *dbCache = pIter;
    atomic_store_8(&dbCache->deleted, 1);
    ctgFreeDbCache(dbCache);
          
    pIter = taosHashIterate(pDbCache, pIter);
  }  

  taosHashCleanup(pDbCache);
  
  CTG_CACHE_STAT_DEC(numOfDb, dbNum);
}
D
dapan1121 已提交
281

D
dapan1121 已提交
282 283 284
void ctgFreeInstUserCache(SHashObj* pUserCache) {
  if (NULL == pUserCache) {
    return;
D
dapan1121 已提交
285
  }
D
dapan1121 已提交
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
  
  int32_t userNum = taosHashGetSize(pUserCache);
  
  void *pIter = taosHashIterate(pUserCache, NULL);
  while (pIter) {
    SCtgUserAuth *userCache = pIter;
    ctgFreeSCtgUserAuth(userCache);
  
    pIter = taosHashIterate(pUserCache, pIter);
  }  
  
  taosHashCleanup(pUserCache);
  
  CTG_CACHE_STAT_DEC(numOfUser, userNum);
}
D
dapan1121 已提交
301

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

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

  taosMemoryFree(pCtg);
}


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

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

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

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

  CTG_CACHE_STAT_DEC(numOfCluster, 1);
D
dapan1121 已提交
327

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

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

D
dapan1121 已提交
333 334 335 336
void ctgClearHandle(SCatalog* pCtg) {
  if (NULL == pCtg) {
    return;
  }
D
dapan1121 已提交
337

D
dapan1121 已提交
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
  uint64_t clusterId = pCtg->clusterId;

  ctgFreeMetaRent(&pCtg->dbRent);
  ctgFreeMetaRent(&pCtg->stbRent);

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

  ctgMetaRentInit(&pCtg->dbRent, gCtgMgmt.cfg.dbRentSec, CTG_RENT_DB);
  ctgMetaRentInit(&pCtg->stbRent, gCtgMgmt.cfg.stbRentSec, CTG_RENT_STABLE);
  
  pCtg->dbCache = taosHashInit(gCtgMgmt.cfg.maxDBCacheNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
  if (NULL == pCtg->dbCache) {
    qError("taosHashInit %d dbCache failed", CTG_DEFAULT_CACHE_DB_NUMBER);
  }
  
  pCtg->userCache = taosHashInit(gCtgMgmt.cfg.maxUserCacheNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
  if (NULL == pCtg->userCache) {
    ctgError("taosHashInit %d user cache failed", gCtgMgmt.cfg.maxUserCacheNum);
  }

  CTG_CACHE_STAT_INC(numOfClear, 1);

C
Cary Xu 已提交
361
  ctgInfo("handle cleared, clusterId:0x%" PRIx64, clusterId);
D
dapan1121 已提交
362
}
D
dapan1121 已提交
363

D
dapan1121 已提交
364
void ctgFreeSUseDbOutput(SUseDbOutput* pOutput) {
D
dapan1121 已提交
365
  if (NULL == pOutput) {
D
dapan1121 已提交
366 367 368
    return;
  }

D
dapan1121 已提交
369 370 371 372 373
  if (pOutput->dbVgroup) {
    taosHashCleanup(pOutput->dbVgroup->vgHash);
    taosMemoryFreeClear(pOutput->dbVgroup);
  }
  
D
dapan1121 已提交
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
  taosMemoryFree(pOutput);
}

void ctgFreeMsgCtx(SCtgMsgCtx* pCtx) {
  taosMemoryFreeClear(pCtx->target);
  if (NULL == pCtx->out) {
    return;
  }
  
  switch (pCtx->reqType) {
    case TDMT_MND_GET_DB_CFG: {
      SDbCfgInfo* pOut = (SDbCfgInfo*)pCtx->out;
      taosArrayDestroy(pOut->pRetensions);
      taosMemoryFreeClear(pCtx->out);
      break;
    }
    case TDMT_MND_USE_DB:{
      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 已提交
414
    case TDMT_MND_GET_TABLE_INDEX: {
D
dapan1121 已提交
415
      STableIndex* pOut = (STableIndex*)pCtx->out;
D
dapan1121 已提交
416
      if (pOut) {
D
dapan1121 已提交
417
        taosArrayDestroyEx(pOut->pIndex, tFreeSTableIndexInfo);
D
dapan1121 已提交
418 419 420 421
        taosMemoryFreeClear(pCtx->out);
      }
      break;
    }
D
dapan1121 已提交
422 423 424 425 426 427 428
    case TDMT_VND_TABLE_CFG:
    case TDMT_MND_TABLE_CFG: {
      STableCfgRsp* pOut = (STableCfgRsp*)pCtx->out;
      tFreeSTableCfgRsp(pOut);
      taosMemoryFreeClear(pCtx->out);
      break;
    }
D
dapan1121 已提交
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
    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);
      taosMemoryFreeClear(pCtx->out);
      break;
    }
    default:
      qError("invalid reqType %d", pCtx->reqType);
      break;
  }
}

D
dapan1121 已提交
450 451 452 453 454 455 456 457
void ctgFreeTbMetasMsgCtx(SCtgMsgCtx* pCtx) {
  ctgFreeMsgCtx(pCtx);
  if (pCtx->lastOut) {
    ctgFreeSTableMetaOutput((STableMetaOutput*)pCtx->lastOut);
    pCtx->lastOut = NULL;
  }
}

D
dapan1121 已提交
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
void ctgFreeSTableMetaOutput(STableMetaOutput* pOutput) {
  if (NULL == pOutput) {
    return;
  }
  
  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;
  
  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 已提交
485 486 487 488 489 490 491 492 493
void ctgFreeBatchMeta(void* meta) {
  if (NULL == meta) {
    return;
  }
  
  SMetaRes* pRes = (SMetaRes*)meta;
  taosMemoryFreeClear(pRes->pRes);
}

494 495 496 497 498 499 500 501 502 503
void ctgFreeBatchHash(void* hash) {
  if (NULL == hash) {
    return;
  }
  
  SMetaRes* pRes = (SMetaRes*)hash;
  taosMemoryFreeClear(pRes->pRes);
}


D
dapan1121 已提交
504 505
void ctgFreeTaskRes(CTG_TASK_TYPE type, void **pRes) {
  switch (type) {
D
dapan1121 已提交
506 507
    case CTG_TASK_GET_QNODE:
    case CTG_TASK_GET_DNODE:
D
dapan1121 已提交
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
    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;
    }
    case CTG_TASK_GET_TB_INDEX: {
      taosArrayDestroyEx(*pRes, tFreeSTableIndexInfo);
      *pRes = NULL;
      break;
    }
D
dapan1121 已提交
526 527 528 529 530 531 532 533
    case CTG_TASK_GET_TB_CFG: {
      if (*pRes) {
        STableCfg* pInfo = (STableCfg*)*pRes;
        tFreeSTableCfgRsp(pInfo);
        taosMemoryFreeClear(*pRes);
      }
      break;
    }
D
dapan1121 已提交
534 535 536 537 538 539 540
    case CTG_TASK_GET_TB_HASH:
    case CTG_TASK_GET_DB_INFO:
    case CTG_TASK_GET_INDEX:
    case CTG_TASK_GET_UDF: 
    case CTG_TASK_GET_USER: 
    case CTG_TASK_GET_SVR_VER:
    case CTG_TASK_GET_TB_META: {
D
dapan1121 已提交
541 542 543
      taosMemoryFreeClear(*pRes);
      break;
    }
D
dapan1121 已提交
544 545 546 547 548 549 550 551 552
    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));
      }
      *pRes = NULL; // no need to free it
      break;
    }
553 554 555 556 557 558 559 560 561
    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));
      }
      *pRes = NULL; // no need to free it
      break;
    }    
D
dapan1121 已提交
562 563 564 565 566 567 568 569 570
    default:
      qError("invalid task type %d", type);
      break;
  }
}


void ctgFreeSubTaskRes(CTG_TASK_TYPE type, void **pRes) {
  switch (type) {
D
dapan1121 已提交
571
    case CTG_TASK_GET_QNODE:
D
dapan1121 已提交
572 573 574 575 576
    case CTG_TASK_GET_DNODE: {
      taosArrayDestroy((SArray*)*pRes);
      *pRes = NULL;
      break;
    }
D
dapan1121 已提交
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
    case CTG_TASK_GET_DB_VGROUP: {
      if (*pRes) {
        SDBVgInfo* pInfo = (SDBVgInfo*)*pRes;
        taosHashCleanup(pInfo->vgHash);
        taosMemoryFreeClear(*pRes);
      }
      break;
    }
    case CTG_TASK_GET_DB_CFG: {
      if (*pRes) {
        SDbCfgInfo* pInfo = (SDbCfgInfo*)*pRes;
        taosArrayDestroy(pInfo->pRetensions);
        taosMemoryFreeClear(*pRes);
      }
      break;
    }
    case CTG_TASK_GET_TB_INDEX: {
      taosArrayDestroyEx(*pRes, tFreeSTableIndexInfo);
      *pRes = NULL;
      break;
    }
D
dapan1121 已提交
598 599 600 601 602 603 604 605
    case CTG_TASK_GET_TB_CFG: {
      if (*pRes) {
        STableCfg* pInfo = (STableCfg*)*pRes;
        tFreeSTableCfgRsp(pInfo);
        taosMemoryFreeClear(*pRes);
      }
      break;
    }
D
dapan1121 已提交
606 607 608 609 610 611
    case CTG_TASK_GET_TB_META:    
    case CTG_TASK_GET_DB_INFO:
    case CTG_TASK_GET_TB_HASH:
    case CTG_TASK_GET_INDEX: 
    case CTG_TASK_GET_UDF: 
    case CTG_TASK_GET_SVR_VER: 
D
dapan1121 已提交
612 613 614 615
    case CTG_TASK_GET_USER: {
      taosMemoryFreeClear(*pRes);
      break;
    }
D
dapan1121 已提交
616 617 618 619 620
    case CTG_TASK_GET_TB_META_BATCH: {
      taosArrayDestroyEx(*pRes, ctgFreeBatchMeta);
      *pRes = NULL;
      break;
    }
621 622 623 624 625
    case CTG_TASK_GET_TB_HASH_BATCH: {
      taosArrayDestroyEx(*pRes, ctgFreeBatchHash);
      *pRes = NULL;
      break;
    }
D
dapan1121 已提交
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
    default:
      qError("invalid task type %d", type);
      break;
  }
}


void ctgClearSubTaskRes(SCtgSubRes *pRes) {
  pRes->code = 0;

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

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

void ctgFreeTaskCtx(SCtgTask* pTask) {
D
dapan1121 已提交
644 645 646 647 648 649 650 651
  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 已提交
652
      taosMemoryFreeClear(pTask->taskCtx);
D
dapan1121 已提交
653 654
      break;
    }
D
dapan1121 已提交
655
    case CTG_TASK_GET_TB_META_BATCH: {
656
      SCtgTbMetasCtx* taskCtx = (SCtgTbMetasCtx*)pTask->taskCtx;
657
      taosArrayDestroyEx(taskCtx->pResList, ctgFreeBatchMeta);
D
dapan1121 已提交
658 659 660
      taosArrayDestroy(taskCtx->pFetchs);
      // NO NEED TO FREE pNames

D
dapan1121 已提交
661
      taosArrayDestroyEx(pTask->msgCtxs, (FDelete)ctgFreeTbMetasMsgCtx);
D
dapan1121 已提交
662 663 664 665 666 667 668 669
      
      if (pTask->msgCtx.lastOut) {
        ctgFreeSTableMetaOutput((STableMetaOutput*)pTask->msgCtx.lastOut);
        pTask->msgCtx.lastOut = NULL;
      }
      taosMemoryFreeClear(pTask->taskCtx);
      break;
    }
D
dapan1121 已提交
670 671 672
    case CTG_TASK_GET_TB_HASH: {
      SCtgTbHashCtx* taskCtx = (SCtgTbHashCtx*)pTask->taskCtx;
      taosMemoryFreeClear(taskCtx->pName);
D
dapan1121 已提交
673
      taosMemoryFreeClear(pTask->taskCtx);      
D
dapan1121 已提交
674 675
      break;
    }
676
    case CTG_TASK_GET_TB_HASH_BATCH: {
677
      SCtgTbHashsCtx* taskCtx = (SCtgTbHashsCtx*)pTask->taskCtx;
678 679 680 681 682 683 684 685 686
      taosArrayDestroyEx(taskCtx->pResList, ctgFreeBatchHash);
      taosArrayDestroy(taskCtx->pFetchs);
      // NO NEED TO FREE pNames

      taosArrayDestroyEx(pTask->msgCtxs, (FDelete)ctgFreeMsgCtx);
      
      taosMemoryFreeClear(pTask->taskCtx);
      break;
    }    
D
dapan1121 已提交
687 688 689 690
    case CTG_TASK_GET_TB_INDEX: {
      SCtgTbIndexCtx* taskCtx = (SCtgTbIndexCtx*)pTask->taskCtx;
      taosMemoryFreeClear(taskCtx->pName);
      taosMemoryFreeClear(pTask->taskCtx);
D
dapan1121 已提交
691 692 693 694 695 696 697
      break;
    }
    case CTG_TASK_GET_TB_CFG: {
      SCtgTbCfgCtx* taskCtx = (SCtgTbCfgCtx*)pTask->taskCtx;
      taosMemoryFreeClear(taskCtx->pName);
      taosMemoryFreeClear(taskCtx->pVgInfo);
      taosMemoryFreeClear(pTask->taskCtx);
D
dapan1121 已提交
698 699
      break;
    }
D
dapan1121 已提交
700 701 702 703 704 705
    case CTG_TASK_GET_DB_VGROUP:
    case CTG_TASK_GET_DB_CFG:
    case CTG_TASK_GET_DB_INFO:    
    case CTG_TASK_GET_INDEX:
    case CTG_TASK_GET_UDF:
    case CTG_TASK_GET_QNODE:    
D
dapan1121 已提交
706 707 708 709 710 711 712 713 714 715
    case CTG_TASK_GET_USER: {
      taosMemoryFreeClear(pTask->taskCtx);
      break;
    }
    default:
      qError("invalid task type %d", pTask->type);
      break;
  }
}

D
dapan1121 已提交
716 717 718 719 720 721 722 723 724 725

void ctgFreeTask(SCtgTask* pTask) {
  ctgFreeMsgCtx(&pTask->msgCtx);
  ctgFreeTaskRes(pTask->type, &pTask->res);
  ctgFreeTaskCtx(pTask);

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

D
dapan1121 已提交
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
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;
  }
  
  SCtgJob* pJob = (SCtgJob*)job;

  int64_t rid = pJob->refId;
  uint64_t qid = pJob->queryId;

  ctgFreeTasks(pJob->pTasks);
D
dapan1121 已提交
751
  ctgFreeBatchs(pJob->pBatchs);
D
dapan1121 已提交
752 753 754 755 756

  ctgFreeSMetaData(&pJob->jobRes);

  taosMemoryFree(job);

D
dapan1121 已提交
757
  qDebug("QID:0x%" PRIx64 ", ctg job 0x%" PRIx64 " freed", qid, rid);
D
dapan1121 已提交
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
}

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

  pCtx->reqType = reqType;
  pCtx->out = out;
  if (target) {
    pCtx->target = strdup(target);
    if (NULL == pCtx->target) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  } else {
    pCtx->target = NULL;
  }

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
int32_t ctgAddMsgCtx(SArray* pCtxs, int32_t reqType, void* out, char* target) {
  SCtgMsgCtx ctx = {0};
  
  ctx.reqType = reqType;
  ctx.out = out;
  if (target) {
    ctx.target = strdup(target);
    if (NULL == ctx.target) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

  taosArrayPush(pCtxs, &ctx);

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814

int32_t ctgGetHashFunction(int8_t hashMethod, tableNameHashFp *fp) {
  switch (hashMethod) {
    default:
      *fp = MurmurHash3_32;
      break;
  }

  return TSDB_CODE_SUCCESS;
}

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);

  vgList = taosArrayInit(vgNum, sizeof(SVgroupInfo));
  if (NULL == vgList) {
    ctgError("taosArrayInit failed, num:%d", vgNum);
D
dapan1121 已提交
815
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);    
D
dapan1121 已提交
816 817 818 819 820 821 822 823 824
  }

  void *pIter = taosHashIterate(vgHash, NULL);
  while (pIter) {
    vgInfo = pIter;

    if (NULL == taosArrayPush(vgList, vgInfo)) {
      ctgError("taosArrayPush failed, vgId:%d", vgInfo->vgId);
      taosHashCancelIterate(vgHash, pIter);      
D
dapan1121 已提交
825
      CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
    }
    
    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);
}


int32_t ctgGetVgInfoFromHashValue(SCatalog *pCtg, SDBVgInfo *dbInfo, const SName *pTableName, SVgroupInfo *pVgroup) {
  int32_t code = 0;
  
  int32_t vgNum = taosHashGetSize(dbInfo->vgHash);
  char db[TSDB_DB_FNAME_LEN] = {0};
  tNameGetFullDbName(pTableName, db);

  if (vgNum <= 0) {
    ctgError("db vgroup cache invalid, db:%s, vgroup number:%d", db, vgNum);
    CTG_ERR_RET(TSDB_CODE_TSC_DB_NOT_SELECTED);
  }

  SVgroupInfo *vgInfo = NULL;
  char tbFullName[TSDB_TABLE_FNAME_LEN];
  tNameExtractFullName(pTableName, tbFullName);

864
  uint32_t hashValue = taosGetTbHashVal(tbFullName, (uint32_t)strlen(tbFullName), dbInfo->hashMethod, dbInfo->hashPrefix, dbInfo->hashSuffix);
D
dapan1121 已提交
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884

  void *pIter = taosHashIterate(dbInfo->vgHash, NULL);
  while (pIter) {
    vgInfo = pIter;
    if (hashValue >= vgInfo->hashBegin && hashValue <= vgInfo->hashEnd) {
      taosHashCancelIterate(dbInfo->vgHash, pIter);
      break;
    }
    
    pIter = taosHashIterate(dbInfo->vgHash, pIter);
    vgInfo = NULL;
  }

  if (NULL == vgInfo) {
    ctgError("no hash range found for hash value [%u], db:%s, numOfVgId:%d", hashValue, db, taosHashGetSize(dbInfo->vgHash));
    CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
  }

  *pVgroup = *vgInfo;

S
Shengliang Guan 已提交
885
  ctgDebug("Got tb %s hash vgroup, vgId:%d, epNum %d, current %s port %d", tbFullName, vgInfo->vgId, vgInfo->epSet.numOfEps,
D
dapan1121 已提交
886 887
    vgInfo->epSet.eps[vgInfo->epSet.inUse].fqdn, vgInfo->epSet.eps[vgInfo->epSet.inUse].port);

D
dapan1121 已提交
888 889 890
  CTG_RET(code);
}

891 892 893 894 895 896 897 898 899 900 901 902 903
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 已提交
904 905 906 907 908 909 910 911 912 913 914 915
int ctgVgInfoComp(const void* lp, const void* rp) {
  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;
}

916

D
dapan1121 已提交
917
int32_t ctgGetVgInfosFromHashValue(SCatalog *pCtg, SCtgTaskReq* tReq, SDBVgInfo *dbInfo, SCtgTbHashsCtx *pCtx, char* dbFName, SArray* pNames, bool update) {
918
  int32_t code = 0;
D
dapan1121 已提交
919
  SCtgTask* pTask = tReq->pTask;
920 921 922 923
  SMetaRes res = {0};
  int32_t vgNum = taosHashGetSize(dbInfo->vgHash);
  if (vgNum <= 0) {
    ctgError("db vgroup cache invalid, db:%s, vgroup number:%d", dbFName, vgNum);
D
dapan1121 已提交
924
    CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
925 926 927
  }

  SVgroupInfo *vgInfo = NULL;
928
  int32_t tbNum = taosArrayGetSize(pNames);
929 930 931 932 933 934

  if (1 == vgNum) {
    void *pIter = taosHashIterate(dbInfo->vgHash, NULL);
    for (int32_t i = 0; i < tbNum; ++i) {
      vgInfo = taosMemoryMalloc(sizeof(SVgroupInfo));
      if (NULL == vgInfo) {
D
dapan1121 已提交
935
        taosHashCancelIterate(dbInfo->vgHash, pIter);
936 937 938 939 940 941 942 943 944
        CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
      }

      *vgInfo = *(SVgroupInfo*)pIter;

      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) {
D
dapan1121 已提交
945
        SCtgFetch* pFetch = taosArrayGet(pCtx->pFetchs, tReq->msgIdx);
946 947 948 949 950 951 952 953
        SMetaRes *pRes = taosArrayGet(pCtx->pResList, pFetch->resIdx + i);
        pRes->pRes = vgInfo;
      } else {
        res.pRes = vgInfo;
        taosArrayPush(pCtx->pResList, &res);
      }
    }

954
    taosHashCancelIterate(dbInfo->vgHash, pIter);
955 956 957
    return TSDB_CODE_SUCCESS;
  }

958 959 960 961 962 963 964
  SArray* pVgList = taosArrayInit(vgNum, POINTER_BYTES);
  void *pIter = taosHashIterate(dbInfo->vgHash, NULL);
  while (pIter) {
    taosArrayPush(pVgList, &pIter);
    pIter = taosHashIterate(dbInfo->vgHash, pIter);
  }

D
dapan1121 已提交
965 966
  taosArraySort(pVgList, ctgVgInfoComp);

967 968 969 970 971 972 973
  char tbFullName[TSDB_TABLE_FNAME_LEN];
  sprintf(tbFullName, "%s.", dbFName);
  int32_t offset = strlen(tbFullName);
  SName* pName = NULL;
  int32_t tbNameLen = 0;
  
  for (int32_t i = 0; i < tbNum; ++i) {
974
    pName = taosArrayGet(pNames, i);
975 976 977 978

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

979
    uint32_t hashValue = taosGetTbHashVal(tbFullName, (uint32_t)strlen(tbFullName), dbInfo->hashMethod, dbInfo->hashPrefix, dbInfo->hashSuffix);
980

981
    SVgroupInfo **p = taosArraySearch(pVgList, &hashValue, ctgHashValueComp, TD_EQ);
982

983
    if (NULL == p) {
984
      ctgError("no hash range found for hash value [%u], db:%s, numOfVgId:%d", hashValue, dbFName, taosHashGetSize(dbInfo->vgHash));
985
      taosArrayDestroy(pVgList);
986 987 988
      CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
    }

989 990
    vgInfo = *p;

991 992
    SVgroupInfo* pNewVg = taosMemoryMalloc(sizeof(SVgroupInfo));
    if (NULL == pNewVg) {
993
      taosArrayDestroy(pVgList);
994 995 996 997 998 999 1000 1001 1002
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }

    *pNewVg = *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);

    if (update) {
D
dapan1121 已提交
1003
      SCtgFetch* pFetch = taosArrayGet(pCtx->pFetchs, tReq->msgIdx);
1004 1005 1006 1007 1008 1009 1010 1011
      SMetaRes *pRes = taosArrayGet(pCtx->pResList, pFetch->resIdx + i);
      pRes->pRes = pNewVg;
    } else {
      res.pRes = pNewVg;
      taosArrayPush(pCtx->pResList, &res);
    }    
  }

1012 1013
  taosArrayDestroy(pVgList);

1014 1015 1016 1017
  CTG_RET(code);
}


D
dapan1121 已提交
1018
int32_t ctgStbVersionSearchCompare(const void* key1, const void* key2) {
D
dapan1121 已提交
1019
  if (*(uint64_t *)key1 < ((SSTableVersion*)key2)->suid) {
D
dapan1121 已提交
1020
    return -1;
D
dapan1121 已提交
1021
  } else if (*(uint64_t *)key1 > ((SSTableVersion*)key2)->suid) {
D
dapan1121 已提交
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
    return 1;
  } else {
    return 0;
  }
}

int32_t ctgDbVgVersionSearchCompare(const void* key1, const void* key2) {
  if (*(int64_t *)key1 < ((SDbVgVersion*)key2)->dbId) {
    return -1;
  } else if (*(int64_t *)key1 > ((SDbVgVersion*)key2)->dbId) {
    return 1;
  } else {
    return 0;
  }
}

int32_t ctgStbVersionSortCompare(const void* key1, const void* key2) {
D
dapan1121 已提交
1039
  if (((SSTableVersion*)key1)->suid < ((SSTableVersion*)key2)->suid) {
D
dapan1121 已提交
1040
    return -1;
D
dapan1121 已提交
1041
  } else if (((SSTableVersion*)key1)->suid > ((SSTableVersion*)key2)->suid) {
D
dapan1121 已提交
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
    return 1;
  } else {
    return 0;
  }
}

int32_t ctgDbVgVersionSortCompare(const void* key1, const void* key2) {
  if (((SDbVgVersion*)key1)->dbId < ((SDbVgVersion*)key2)->dbId) {
    return -1;
  } else if (((SDbVgVersion*)key1)->dbId > ((SDbVgVersion*)key2)->dbId) {
    return 1;
  } else {
    return 0;
  }
}




int32_t ctgCloneVgInfo(SDBVgInfo *src, SDBVgInfo **dst) {
  *dst = taosMemoryMalloc(sizeof(SDBVgInfo));
  if (NULL == *dst) {
    qError("malloc %d failed", (int32_t)sizeof(SDBVgInfo));
D
dapan1121 已提交
1065
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1066 1067 1068 1069 1070 1071 1072 1073 1074
  }

  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 已提交
1075
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
  }

  int32_t *vgId = NULL;
  void *pIter = taosHashIterate(src->vgHash, NULL);
  while (pIter) {
    vgId = taosHashGetKey(pIter, NULL);

    if (taosHashPut((*dst)->vgHash, (void *)vgId, sizeof(int32_t), pIter, sizeof(SVgroupInfo))) {
      qError("taosHashPut failed, hashSize:%d", (int32_t)hashSize);
      taosHashCancelIterate(src->vgHash, pIter);
      taosHashCleanup((*dst)->vgHash);
      taosMemoryFreeClear(*dst);
D
dapan1121 已提交
1088
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
    }
    
    pIter = taosHashIterate(src->vgHash, pIter);
  }


  return TSDB_CODE_SUCCESS;
}



int32_t ctgCloneMetaOutput(STableMetaOutput *output, STableMetaOutput **pOutput) {
  *pOutput = taosMemoryMalloc(sizeof(STableMetaOutput));
  if (NULL == *pOutput) {
    qError("malloc %d failed", (int32_t)sizeof(STableMetaOutput));
D
dapan1121 已提交
1104
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1105 1106 1107 1108 1109 1110 1111
  }

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

  if (output->tbMeta) {
    int32_t metaSize = CTG_META_SIZE(output->tbMeta);
    (*pOutput)->tbMeta = taosMemoryMalloc(metaSize);
D
dapan1121 已提交
1112
    qDebug("tbMeta cloned, size:%d, p:%p", metaSize, (*pOutput)->tbMeta);
D
dapan1121 已提交
1113 1114 1115
    if (NULL == (*pOutput)->tbMeta) {
      qError("malloc %d failed", (int32_t)sizeof(STableMetaOutput));
      taosMemoryFreeClear(*pOutput);
D
dapan1121 已提交
1116
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1117 1118 1119 1120 1121 1122 1123 1124
    }

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

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
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) {
    STableIndexInfo *pInfo = taosArrayGet(pIndex, i);
D
dapan1121 已提交
1139 1140
    pInfo = taosArrayPush(*pRes, pInfo);
    pInfo->expr = strdup(pInfo->expr);
D
dapan1121 已提交
1141 1142 1143 1144 1145 1146
  }

  return TSDB_CODE_SUCCESS;
}


D
dapan1121 已提交
1147 1148
int32_t ctgUpdateSendTargetInfo(SMsgSendInfo *pMsgSendInfo, int32_t msgType, char* dbFName, int32_t vgId) {
  if (msgType == TDMT_VND_TABLE_META || msgType == TDMT_VND_TABLE_CFG || msgType == TDMT_VND_BATCH_META) {
D
dapan1121 已提交
1149
    pMsgSendInfo->target.type = TARGET_TYPE_VNODE;
D
dapan1121 已提交
1150
    pMsgSendInfo->target.vgId = vgId;
D
dapan1121 已提交
1151 1152 1153 1154 1155 1156 1157
    pMsgSendInfo->target.dbFName = strdup(dbFName);
  } else {
    pMsgSendInfo->target.type = TARGET_TYPE_MNODE;
  }

  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
1158

1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
int32_t ctgGetTablesReqNum(SArray *pList) {
  if (NULL == pList) {
    return 0;
  }

  int32_t total = 0;
  int32_t n = taosArrayGetSize(pList);
  for (int32_t i = 0; i < n; ++i) {
    STablesReq *pReq = taosArrayGet(pList, i);
    total += taosArrayGetSize(pReq->pTables);
  }

  return total;
}

int32_t ctgAddFetch(SArray** pFetchs, int32_t dbIdx, int32_t tbIdx, int32_t *fetchIdx, int32_t resIdx, int32_t flag) {
  if (NULL == (*pFetchs)) {
    *pFetchs = taosArrayInit(CTG_DEFAULT_FETCH_NUM, sizeof(SCtgFetch));
  }
  
  SCtgFetch fetch = {0};
  fetch.dbIdx = dbIdx;
  fetch.tbIdx = tbIdx;
  fetch.fetchIdx = (*fetchIdx)++;
  fetch.resIdx = resIdx;
  fetch.flag = flag;
  
  taosArrayPush(*pFetchs, &fetch);

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1191 1192 1193 1194
SName* ctgGetFetchName(SArray* pNames, SCtgFetch* pFetch) {
  STablesReq* pReq = (STablesReq*)taosArrayGet(pNames, pFetch->dbIdx);
  return (SName*)taosArrayGet(pReq->pTables, pFetch->tbIdx);
}
1195

D
dapan1121 已提交
1196