catalog.c 80.2 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * 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/>.
H
Haojun Liao 已提交
14 15
 */

D
dapan1121 已提交
16
#include "trpc.h"
D
dapan1121 已提交
17
#include "query.h"
D
dapan1121 已提交
18
#include "tname.h"
H
Haojun Liao 已提交
19
#include "catalogInt.h"
20

D
dapan 已提交
21 22 23 24 25 26
int32_t ctgActUpdateVg(SCtgMetaAction *action);
int32_t ctgActUpdateTbl(SCtgMetaAction *action);
int32_t ctgActRemoveDB(SCtgMetaAction *action);
int32_t ctgActRemoveStb(SCtgMetaAction *action);
int32_t ctgActRemoveTbl(SCtgMetaAction *action);

D
dapan 已提交
27
SCatalogMgmt gCtgMgmt = {0};
D
dapan1121 已提交
28
SCtgDebug gCTGDebug = {0};
D
dapan 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
SCtgAction gCtgAction[CTG_ACT_MAX] = {{
                            CTG_ACT_UPDATE_VG,
                            "update vgInfo",
                            ctgActUpdateVg
                          },
                          {
                            CTG_ACT_UPDATE_TBL,
                            "update tbMeta",
                            ctgActUpdateTbl
                          },
                          {
                            CTG_ACT_REMOVE_DB,
                            "remove DB",
                            ctgActRemoveDB
                          },
                          {
                            CTG_ACT_REMOVE_STB,
                            "remove stbMeta",
                            ctgActRemoveStb
                          },
                          {
                            CTG_ACT_REMOVE_TBL,
                            "remove tbMeta",
                            ctgActRemoveTbl
                          }
};

int32_t ctgDbgEnableDebug(char *option) {
  if (0 == strcasecmp(option, "lock")) {
    gCTGDebug.lockDebug = true;
    qDebug("lock debug enabled");
    return TSDB_CODE_SUCCESS;
  }

  if (0 == strcasecmp(option, "cache")) {
    gCTGDebug.cacheDebug = true;
    qDebug("cache debug enabled");
    return TSDB_CODE_SUCCESS;
  }

  if (0 == strcasecmp(option, "api")) {
    gCTGDebug.apiDebug = true;
    qDebug("api debug enabled");
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
75 76 77 78 79 80
  if (0 == strcasecmp(option, "meta")) {
    gCTGDebug.metaDebug = true;
    qDebug("api debug enabled");
    return TSDB_CODE_SUCCESS;
  }

D
dapan 已提交
81 82 83 84 85 86 87 88 89 90
  qError("invalid debug option:%s", option);
  
  return TSDB_CODE_CTG_INTERNAL_ERROR;
}

int32_t ctgDbgGetStatNum(char *option, void *res) {
  if (0 == strcasecmp(option, "runtime.qDoneNum")) {
    *(uint64_t *)res = atomic_load_64(&gCtgMgmt.stat.runtime.qDoneNum);
    return TSDB_CODE_SUCCESS;
  }
91

D
dapan 已提交
92 93 94 95
  qError("invalid stat option:%s", option);
  
  return TSDB_CODE_CTG_INTERNAL_ERROR;
}
D
dapan1121 已提交
96

D
dapan1121 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
int32_t ctgDbgGetTbMetaNum(SCtgDBCache *dbCache) {
  return dbCache->tbCache.metaCache ? (int32_t)taosHashGetSize(dbCache->tbCache.metaCache) : 0;
}

int32_t ctgDbgGetStbNum(SCtgDBCache *dbCache) {
  return dbCache->tbCache.stbCache ? (int32_t)taosHashGetSize(dbCache->tbCache.stbCache) : 0;
}

int32_t ctgDbgGetRentNum(SCtgRentMgmt *rent) {
  int32_t num = 0;
  for (uint16_t i = 0; i < rent->slotNum; ++i) {
    SCtgRentSlot *slot = &rent->slots[i];
    if (NULL == slot->meta) {
      continue;
    }

    num += taosArrayGetSize(slot->meta);
  }

  return num;
}

D
dapan1121 已提交
119 120
int32_t ctgDbgGetClusterCacheNum(SCatalog* pCtg, int32_t type) {
  if (NULL == pCtg || NULL == pCtg->dbCache) {
D
dapan1121 已提交
121 122 123 124 125
    return 0;
  }

  switch (type) {
    case CTG_DBG_DB_NUM:
D
dapan1121 已提交
126
      return (int32_t)taosHashGetSize(pCtg->dbCache);
D
dapan1121 已提交
127
    case CTG_DBG_DB_RENT_NUM:
D
dapan1121 已提交
128
      return ctgDbgGetRentNum(&pCtg->dbRent);
D
dapan1121 已提交
129
    case CTG_DBG_STB_RENT_NUM:
D
dapan1121 已提交
130
      return ctgDbgGetRentNum(&pCtg->stbRent);
D
dapan1121 已提交
131 132 133 134 135 136
    default:
      break;
  }

  SCtgDBCache *dbCache = NULL;
  int32_t num = 0;
D
dapan1121 已提交
137
  void *pIter = taosHashIterate(pCtg->dbCache, NULL);
D
dapan1121 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150
  while (pIter) {
    dbCache = (SCtgDBCache *)pIter;
    switch (type) {
      case CTG_DBG_META_NUM:
        num += ctgDbgGetTbMetaNum(dbCache);
        break;
      case CTG_DBG_STB_NUM:
        num += ctgDbgGetStbNum(dbCache);
        break;
      default:
        ctgError("invalid type:%d", type);
        break;
    }
D
dapan1121 已提交
151
    pIter = taosHashIterate(pCtg->dbCache, pIter);
D
dapan1121 已提交
152 153 154 155 156
  }

  return num;
}

D
dapan1121 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170
void ctgDbgShowTableMeta(SCatalog* pCtg, const char *tbName, STableMeta* p) {
  if (!gCTGDebug.metaDebug) {
    return;
  }

  STableComInfo *c = &p->tableInfo;

  if (TSDB_CHILD_TABLE == p->tableType) {
    ctgDebug("table [%s] meta: type:%d, vgId:%d, uid:%" PRIx64 ",suid:%" PRIx64, tbName, p->tableType, p->vgId, p->uid, p->suid);
    return;
  } else {
    ctgDebug("table [%s] meta: type:%d, vgId:%d, uid:%" PRIx64 ",suid:%" PRIx64 ",sv:%d, tv:%d, tagNum:%d, precision:%d, colNum:%d, rowSize:%d",
     tbName, p->tableType, p->vgId, p->uid, p->suid, p->sversion, p->tversion, c->numOfTags, c->precision, c->numOfColumns, c->rowSize);
  }
H
Haojun Liao 已提交
171

D
dapan1121 已提交
172 173 174 175 176 177
  int32_t colNum = c->numOfColumns + c->numOfTags;
  for (int32_t i = 0; i < colNum; ++i) {
    SSchema *s = &p->schema[i];
    ctgDebug("[%d] name:%s, type:%d, colId:%d, bytes:%d", i, s->name, s->type, s->colId, s->bytes);
  }
}
D
dapan1121 已提交
178

D
dapan1121 已提交
179 180
void ctgDbgShowDBCache(SCatalog* pCtg, SHashObj *dbHash) {
  if (NULL == dbHash || !gCTGDebug.cacheDebug) {
D
dapan1121 已提交
181 182 183 184 185 186 187 188 189 190 191 192
    return;
  }

  int32_t i = 0;
  SCtgDBCache *dbCache = NULL;
  void *pIter = taosHashIterate(dbHash, NULL);
  while (pIter) {
    char *dbFName = NULL;
    size_t len = 0;
    
    dbCache = (SCtgDBCache *)pIter;

D
dapan1121 已提交
193
    dbFName = taosHashGetKey(pIter, &len);
D
dapan1121 已提交
194

D
dapan1121 已提交
195 196 197 198 199 200
    int32_t metaNum = dbCache->tbCache.metaCache ? taosHashGetSize(dbCache->tbCache.metaCache) : 0;
    int32_t stbNum = dbCache->tbCache.stbCache ? taosHashGetSize(dbCache->tbCache.stbCache) : 0;
    int32_t vgVersion = CTG_DEFAULT_INVALID_VERSION;
    int32_t hashMethod = -1;
    int32_t vgNum = 0;

D
dapan1121 已提交
201
    if (dbCache->vgInfo) {
D
dapan1121 已提交
202 203
      vgVersion = dbCache->vgInfo->vgVersion;
      hashMethod = dbCache->vgInfo->hashMethod;
D
dapan1121 已提交
204
      if (dbCache->vgInfo->vgHash) {
D
dapan1121 已提交
205
        vgNum = taosHashGetSize(dbCache->vgInfo->vgHash);
D
dapan1121 已提交
206
      }
H
Haojun Liao 已提交
207
    }
D
dapan1121 已提交
208
    
H
Haojun Liao 已提交
209
    ctgDebug("[%d] db [%.*s][%"PRIx64"] %s: metaNum:%d, stbNum:%d, vgVersion:%d, hashMethod:%d, vgNum:%d",
D
dapan1121 已提交
210 211
      i, (int32_t)len, dbFName, dbCache->dbId, dbCache->deleted?"deleted":"", metaNum, stbNum, vgVersion, hashMethod, vgNum);

D
dapan1121 已提交
212 213 214 215
    pIter = taosHashIterate(dbHash, pIter);
  }
}

D
dapan1121 已提交
216 217 218



D
dapan1121 已提交
219
void ctgDbgShowClusterCache(SCatalog* pCtg) {
D
dapan1121 已提交
220
  if (!gCTGDebug.cacheDebug || NULL == pCtg) {
D
dapan1121 已提交
221 222 223
    return;
  }

D
dapan1121 已提交
224
  ctgDebug("## cluster %"PRIx64" %p cache Info ##", pCtg->clusterId, pCtg);
H
Haojun Liao 已提交
225
  ctgDebug("db:%d meta:%d stb:%d dbRent:%d stbRent:%d", ctgDbgGetClusterCacheNum(pCtg, CTG_DBG_DB_NUM), ctgDbgGetClusterCacheNum(pCtg, CTG_DBG_META_NUM),
D
dapan1121 已提交
226
    ctgDbgGetClusterCacheNum(pCtg, CTG_DBG_STB_NUM), ctgDbgGetClusterCacheNum(pCtg, CTG_DBG_DB_RENT_NUM), ctgDbgGetClusterCacheNum(pCtg, CTG_DBG_STB_RENT_NUM));    
D
dapan1121 已提交
227
  
D
dapan1121 已提交
228
  ctgDbgShowDBCache(pCtg, pCtg->dbCache);
D
dapan1121 已提交
229 230
}

D
dapan 已提交
231

D
dapan1121 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
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;
    }
  }

  tfree(mgmt->slots);
}


void ctgFreeTableMetaCache(SCtgTbMetaCache *cache) {
  CTG_LOCK(CTG_WRITE, &cache->stbLock);
  if (cache->stbCache) {
    taosHashCleanup(cache->stbCache);
    cache->stbCache = NULL;
  }
  CTG_UNLOCK(CTG_WRITE, &cache->stbLock);

  CTG_LOCK(CTG_WRITE, &cache->metaLock);
  if (cache->metaCache) {
    taosHashCleanup(cache->metaCache);
    cache->metaCache = NULL;
  }
  CTG_UNLOCK(CTG_WRITE, &cache->metaLock);
}

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

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

void ctgFreeDbCache(SCtgDBCache *dbCache) {
  if (NULL == dbCache) {
    return;
  }

  CTG_LOCK(CTG_WRITE, &dbCache->vgLock);
  ctgFreeVgInfo (dbCache->vgInfo);
  CTG_UNLOCK(CTG_WRITE, &dbCache->vgLock);

  ctgFreeTableMetaCache(&dbCache->tbCache);
}


void ctgFreeHandle(SCatalog* pCtg) {
  ctgFreeMetaRent(&pCtg->dbRent);
  ctgFreeMetaRent(&pCtg->stbRent);
  
  if (pCtg->dbCache) {
    void *pIter = taosHashIterate(pCtg->dbCache, NULL);
    while (pIter) {
      SCtgDBCache *dbCache = pIter;

      atomic_store_8(&dbCache->deleted, 1);

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

    taosHashCleanup(pCtg->dbCache);
  }
  
  free(pCtg);
}



void ctgWaitAction(SCtgMetaAction *action) {
  while (true) {
    tsem_wait(&gCtgMgmt.queue.rspSem);
    
    if (atomic_load_8(&gCtgMgmt.exit)) {
      tsem_post(&gCtgMgmt.queue.rspSem);
      break;
    }

    if (gCtgMgmt.queue.seqDone >= action->seqId) {
      break;
    }

    tsem_post(&gCtgMgmt.queue.rspSem);
    sched_yield();
  }
}
D
dapan 已提交
332 333

void ctgPopAction(SCtgMetaAction **action) {
D
dapan1121 已提交
334
  SCtgQNode *orig = gCtgMgmt.queue.head;
D
dapan 已提交
335
  
D
dapan1121 已提交
336 337
  SCtgQNode *node = gCtgMgmt.queue.head->next;
  gCtgMgmt.queue.head = gCtgMgmt.queue.head->next;
D
dapan 已提交
338 339 340 341 342 343 344 345 346

  CTG_QUEUE_SUB();
  
  tfree(orig);

  *action = &node->action;
}


D
dapan1121 已提交
347
int32_t ctgPushAction(SCatalog* pCtg, SCtgMetaAction *action) {
D
dapan 已提交
348 349 350 351 352
  SCtgQNode *node = calloc(1, sizeof(SCtgQNode));
  if (NULL == node) {
    qError("calloc %d failed", (int32_t)sizeof(SCtgQNode));
    CTG_RET(TSDB_CODE_CTG_MEM_ERROR);
  }
D
dapan1121 已提交
353 354

  action->seqId = atomic_add_fetch_64(&gCtgMgmt.queue.seqId, 1);
D
dapan 已提交
355 356 357
  
  node->action = *action;

D
dapan1121 已提交
358 359 360 361
  CTG_LOCK(CTG_WRITE, &gCtgMgmt.queue.qlock);
  gCtgMgmt.queue.tail->next = node;
  gCtgMgmt.queue.tail = node;
  CTG_UNLOCK(CTG_WRITE, &gCtgMgmt.queue.qlock);
D
dapan 已提交
362 363

  CTG_QUEUE_ADD();
D
dapan1121 已提交
364
  CTG_STAT_ADD(gCtgMgmt.stat.runtime.qNum);
D
dapan 已提交
365

D
dapan1121 已提交
366
  tsem_post(&gCtgMgmt.queue.reqSem);
D
dapan 已提交
367

D
dapan1121 已提交
368
  ctgDebug("action [%s] added into queue", gCtgAction[action->act].name);
D
dapan 已提交
369

D
dapan1121 已提交
370 371
  if (action->syncReq) {
    ctgWaitAction(action);
D
dapan1121 已提交
372 373
  }

D
dapan1121 已提交
374
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
375 376 377
}


D
dapan1121 已提交
378 379 380 381 382 383 384 385 386
int32_t ctgPushRmDBMsgInQueue(SCatalog* pCtg, const char *dbFName, int64_t dbId) {
  int32_t code = 0;
  SCtgMetaAction action= {.act = CTG_ACT_REMOVE_DB};
  SCtgRemoveDBMsg *msg = malloc(sizeof(SCtgRemoveDBMsg));
  if (NULL == msg) {
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgRemoveDBMsg));
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
  }

D
dapan1121 已提交
387 388 389 390 391
  char *p = strchr(dbFName, '.');
  if (p && CTG_IS_INF_DBNAME(p + 1)) {
    dbFName = p + 1;
  }

D
dapan1121 已提交
392 393 394 395 396 397
  msg->pCtg = pCtg;
  strncpy(msg->dbFName, dbFName, sizeof(msg->dbFName));
  msg->dbId = dbId;

  action.data = msg;

D
dapan1121 已提交
398
  CTG_ERR_JRET(ctgPushAction(pCtg, &action));
D
dapan1121 已提交
399 400 401 402

  return TSDB_CODE_SUCCESS;

_return:
H
Haojun Liao 已提交
403

D
dapan1121 已提交
404
  tfree(action.data);
H
Haojun Liao 已提交
405
  CTG_RET(code);
D
dapan1121 已提交
406 407 408
}


D
dapan1121 已提交
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
int32_t ctgPushRmStbMsgInQueue(SCatalog* pCtg, const char *dbFName, int64_t dbId, const char *stbName, uint64_t suid) {
  int32_t code = 0;
  SCtgMetaAction action= {.act = CTG_ACT_REMOVE_STB};
  SCtgRemoveStbMsg *msg = malloc(sizeof(SCtgRemoveStbMsg));
  if (NULL == msg) {
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgRemoveStbMsg));
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
  }

  msg->pCtg = pCtg;
  strncpy(msg->dbFName, dbFName, sizeof(msg->dbFName));
  strncpy(msg->stbName, stbName, sizeof(msg->stbName));
  msg->dbId = dbId;
  msg->suid = suid;

  action.data = msg;

D
dapan1121 已提交
426
  CTG_ERR_JRET(ctgPushAction(pCtg, &action));
D
dapan1121 已提交
427 428 429 430

  return TSDB_CODE_SUCCESS;

_return:
H
Haojun Liao 已提交
431

D
dapan1121 已提交
432
  tfree(action.data);
H
Haojun Liao 已提交
433
  CTG_RET(code);
D
dapan1121 已提交
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
}



int32_t ctgPushRmTblMsgInQueue(SCatalog* pCtg, const char *dbFName, int64_t dbId, const char *tbName) {
  int32_t code = 0;
  SCtgMetaAction action= {.act = CTG_ACT_REMOVE_TBL};
  SCtgRemoveTblMsg *msg = malloc(sizeof(SCtgRemoveTblMsg));
  if (NULL == msg) {
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgRemoveTblMsg));
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
  }

  msg->pCtg = pCtg;
  strncpy(msg->dbFName, dbFName, sizeof(msg->dbFName));
  strncpy(msg->tbName, tbName, sizeof(msg->tbName));
  msg->dbId = dbId;

  action.data = msg;

D
dapan1121 已提交
454
  CTG_ERR_JRET(ctgPushAction(pCtg, &action));
D
dapan1121 已提交
455 456 457 458

  return TSDB_CODE_SUCCESS;

_return:
H
Haojun Liao 已提交
459

D
dapan1121 已提交
460
  tfree(action.data);
H
Haojun Liao 已提交
461
  CTG_RET(code);
D
dapan1121 已提交
462 463
}

D
dapan1121 已提交
464 465 466 467 468 469 470 471
int32_t ctgPushUpdateVgMsgInQueue(SCatalog* pCtg, const char *dbFName, int64_t dbId, SDBVgInfo* dbInfo, bool syncReq) {
  int32_t code = 0;
  SCtgMetaAction action= {.act = CTG_ACT_UPDATE_VG, .syncReq = syncReq};
  SCtgUpdateVgMsg *msg = malloc(sizeof(SCtgUpdateVgMsg));
  if (NULL == msg) {
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgUpdateVgMsg));
    ctgFreeVgInfo(dbInfo);
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
D
dapan1121 已提交
472 473
  }

D
dapan1121 已提交
474 475 476 477 478
  char *p = strchr(dbFName, '.');
  if (p && CTG_IS_INF_DBNAME(p + 1)) {
    dbFName = p + 1;
  }

D
dapan1121 已提交
479 480 481 482
  strncpy(msg->dbFName, dbFName, sizeof(msg->dbFName));
  msg->pCtg = pCtg;
  msg->dbId = dbId;
  msg->dbInfo = dbInfo;
D
dapan1121 已提交
483

D
dapan1121 已提交
484
  action.data = msg;
D
dapan1121 已提交
485

D
dapan1121 已提交
486
  CTG_ERR_JRET(ctgPushAction(pCtg, &action));
D
dapan1121 已提交
487

D
dapan1121 已提交
488
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
489

D
dapan1121 已提交
490
_return:
D
dapan1121 已提交
491

D
dapan1121 已提交
492 493 494
  ctgFreeVgInfo(dbInfo);
  tfree(action.data);
  CTG_RET(code);
D
dapan1121 已提交
495 496
}

D
dapan1121 已提交
497 498 499 500 501 502 503 504 505
int32_t ctgPushUpdateTblMsgInQueue(SCatalog* pCtg, STableMetaOutput *output, bool syncReq) {
  int32_t code = 0;
  SCtgMetaAction action= {.act = CTG_ACT_UPDATE_TBL};
  SCtgUpdateTblMsg *msg = malloc(sizeof(SCtgUpdateTblMsg));
  if (NULL == msg) {
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgUpdateTblMsg));
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
  }

D
dapan1121 已提交
506 507 508 509 510
  char *p = strchr(output->dbFName, '.');
  if (p && CTG_IS_INF_DBNAME(p + 1)) {
    memmove(output->dbFName, p + 1, strlen(p + 1));
  }

D
dapan1121 已提交
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
  msg->pCtg = pCtg;
  msg->output = output;

  action.data = msg;

  CTG_ERR_JRET(ctgPushAction(pCtg, &action));

  return TSDB_CODE_SUCCESS;
  
_return:

  tfree(msg);
  
  CTG_RET(code);
}

D
dapan1121 已提交
527

D
dapan 已提交
528
int32_t ctgAcquireVgInfo(SCatalog *pCtg, SCtgDBCache *dbCache, bool *inCache) {
D
dapan1121 已提交
529 530
  CTG_LOCK(CTG_READ, &dbCache->vgLock);
  
D
dapan 已提交
531 532 533 534 535 536 537 538 539 540
  if (dbCache->deleted) {
    CTG_UNLOCK(CTG_READ, &dbCache->vgLock);

    ctgDebug("db is dropping, dbId:%"PRIx64, dbCache->dbId);
    
    *inCache = false;
    return TSDB_CODE_SUCCESS;
  }

  
D
dapan1121 已提交
541 542 543
  if (NULL == dbCache->vgInfo) {
    CTG_UNLOCK(CTG_READ, &dbCache->vgLock);

D
dapan 已提交
544
    *inCache = false;
D
dapan1121 已提交
545
    ctgDebug("db vgInfo is empty, dbId:%"PRIx64, dbCache->dbId);
D
dapan1121 已提交
546 547 548
    return TSDB_CODE_SUCCESS;
  }

D
dapan 已提交
549 550
  *inCache = true;
  
D
dapan1121 已提交
551 552 553 554 555 556 557 558 559 560 561 562 563 564
  return TSDB_CODE_SUCCESS;
}

int32_t ctgWAcquireVgInfo(SCatalog *pCtg, SCtgDBCache *dbCache) {
  CTG_LOCK(CTG_WRITE, &dbCache->vgLock);

  if (dbCache->deleted) {
    ctgDebug("db is dropping, dbId:%"PRIx64, dbCache->dbId);
    CTG_UNLOCK(CTG_WRITE, &dbCache->vgLock);
    CTG_ERR_RET(TSDB_CODE_CTG_DB_DROPPED);
  }

  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
565

D
dapan1121 已提交
566 567 568
void ctgReleaseDBCache(SCatalog *pCtg, SCtgDBCache *dbCache) {
  taosHashRelease(pCtg->dbCache, dbCache);
}
D
dapan1121 已提交
569

D
dapan1121 已提交
570 571 572
void ctgReleaseVgInfo(SCtgDBCache *dbCache) {
  CTG_UNLOCK(CTG_READ, &dbCache->vgLock);
}
D
dapan1121 已提交
573

D
dapan1121 已提交
574 575 576
void ctgWReleaseVgInfo(SCtgDBCache *dbCache) {
  CTG_UNLOCK(CTG_WRITE, &dbCache->vgLock);
}
D
dapan1121 已提交
577

D
dapan1121 已提交
578

D
dapan 已提交
579
int32_t ctgAcquireDBCacheImpl(SCatalog* pCtg, const char *dbFName, SCtgDBCache **pCache, bool acquire) {
D
dapan1121 已提交
580 581 582 583 584
  char *p = strchr(dbFName, '.');
  if (p && CTG_IS_INF_DBNAME(p + 1)) {
    dbFName = p + 1;
  }

D
dapan 已提交
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
  SCtgDBCache *dbCache = NULL;
  if (acquire) {
    dbCache = (SCtgDBCache *)taosHashAcquire(pCtg->dbCache, dbFName, strlen(dbFName));
  } else {
    dbCache = (SCtgDBCache *)taosHashGet(pCtg->dbCache, dbFName, strlen(dbFName));
  }
  
  if (NULL == dbCache) {
    *pCache = NULL;
    ctgDebug("db not in cache, dbFName:%s", dbFName);
    return TSDB_CODE_SUCCESS;
  }

  if (dbCache->deleted) {
    if (acquire) {
      ctgReleaseDBCache(pCtg, dbCache);
    }    
    
    *pCache = NULL;
    ctgDebug("db is removing from cache, dbFName:%s", dbFName);
    return TSDB_CODE_SUCCESS;
  }

  *pCache = dbCache;
    
  return TSDB_CODE_SUCCESS;
}

int32_t ctgAcquireDBCache(SCatalog* pCtg, const char *dbFName, SCtgDBCache **pCache) {
  CTG_RET(ctgAcquireDBCacheImpl(pCtg, dbFName, pCache, true));
}

int32_t ctgGetDBCache(SCatalog* pCtg, const char *dbFName, SCtgDBCache **pCache) {
  CTG_RET(ctgAcquireDBCacheImpl(pCtg, dbFName, pCache, false));
}


D
dapan1121 已提交
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
int32_t ctgAcquireVgInfoFromCache(SCatalog* pCtg, const char *dbFName, SCtgDBCache **pCache, bool *inCache) {
  if (NULL == pCtg->dbCache) {
    *pCache = NULL;
    *inCache = false;
    ctgWarn("empty db cache, dbFName:%s", dbFName);
    return TSDB_CODE_SUCCESS;
  }

  SCtgDBCache *dbCache = NULL;
  ctgAcquireDBCache(pCtg, dbFName, &dbCache);
  if (NULL == dbCache) {  
    *pCache = NULL;
    *inCache = false;
    return TSDB_CODE_SUCCESS;
  }
  
D
dapan 已提交
638 639
  ctgAcquireVgInfo(pCtg, dbCache, inCache);
  if (!(*inCache)) {
D
dapan1121 已提交
640 641 642 643
    ctgReleaseDBCache(pCtg, dbCache);
  
    *pCache = NULL;
    return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
644
  }
D
dapan1121 已提交
645

D
dapan1121 已提交
646
  *pCache = dbCache;
D
dapan1121 已提交
647
  *inCache = true;
D
dapan1121 已提交
648

D
dapan1121 已提交
649
  ctgDebug("Got db vgInfo from cache, dbFName:%s", dbFName);
D
dapan1121 已提交
650 651 652 653
  
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
int32_t ctgGetQnodeListFromMnode(SCatalog* pCtg, void *pRpc, const SEpSet* pMgmtEps, SArray **out) {
  char *msg = NULL;
  int32_t msgLen = 0;

  ctgDebug("try to get qnode list from mnode, mgmtEpInUse:%d", pMgmtEps->inUse);

  int32_t code = queryBuildMsg[TMSG_INDEX(TDMT_MND_QNODE_LIST)](NULL, &msg, 0, &msgLen);
  if (code) {
    ctgError("Build qnode list msg failed, error:%s", tstrerror(code));
    CTG_ERR_RET(code);
  }
  
  SRpcMsg rpcMsg = {
      .msgType = TDMT_MND_QNODE_LIST,
      .pCont   = msg,
      .contLen = msgLen,
  };

  SRpcMsg rpcRsp = {0};

  rpcSendRecv(pRpc, (SEpSet*)pMgmtEps, &rpcMsg, &rpcRsp);
  if (TSDB_CODE_SUCCESS != rpcRsp.code) {
D
dapan1121 已提交
676
    ctgError("error rsp for qnode list, error:%s", tstrerror(rpcRsp.code));
D
dapan1121 已提交
677 678 679 680 681 682 683 684 685
    CTG_ERR_RET(rpcRsp.code);
  }

  code = queryProcessMsgRsp[TMSG_INDEX(TDMT_MND_QNODE_LIST)](out, rpcRsp.pCont, rpcRsp.contLen);
  if (code) {
    ctgError("Process qnode list rsp failed, error:%s", tstrerror(rpcRsp.code));
    CTG_ERR_RET(code);
  }

D
dapan1121 已提交
686
  ctgDebug("Got qnode list from mnode, listNum:%d", (int32_t)taosArrayGetSize(*out));
D
dapan1121 已提交
687 688 689

  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
690 691


D
dapan1121 已提交
692
int32_t ctgGetDBVgInfoFromMnode(SCatalog* pCtg, void *pRpc, const SEpSet* pMgmtEps, SBuildUseDBInput *input, SUseDbOutput *out) {
D
dapan1121 已提交
693 694 695
  char *msg = NULL;
  int32_t msgLen = 0;

D
dapan 已提交
696
  ctgDebug("try to get db vgInfo from mnode, dbFName:%s", input->db);
D
dapan1121 已提交
697 698 699 700 701 702

  int32_t code = queryBuildMsg[TMSG_INDEX(TDMT_MND_USE_DB)](input, &msg, 0, &msgLen);
  if (code) {
    ctgError("Build use db msg failed, code:%x, db:%s", code, input->db);
    CTG_ERR_RET(code);
  }
D
ut test  
dapan1121 已提交
703
  
D
dapan1121 已提交
704
  SRpcMsg rpcMsg = {
H
Hongze Cheng 已提交
705
      .msgType = TDMT_MND_USE_DB,
D
catalog  
dapan1121 已提交
706
      .pCont   = msg,
D
dapan1121 已提交
707 708 709 710 711 712
      .contLen = msgLen,
  };

  SRpcMsg rpcRsp = {0};

  rpcSendRecv(pRpc, (SEpSet*)pMgmtEps, &rpcMsg, &rpcRsp);
D
dapan1121 已提交
713
  if (TSDB_CODE_SUCCESS != rpcRsp.code) {
D
dapan1121 已提交
714
    ctgError("error rsp for use db, error:%s, db:%s", tstrerror(rpcRsp.code), input->db);
D
dapan1121 已提交
715
    CTG_ERR_RET(rpcRsp.code);
D
dapan1121 已提交
716
  }
D
dapan1121 已提交
717

D
dapan1121 已提交
718 719 720 721 722
  code = queryProcessMsgRsp[TMSG_INDEX(TDMT_MND_USE_DB)](out, rpcRsp.pCont, rpcRsp.contLen);
  if (code) {
    ctgError("Process use db rsp failed, code:%x, db:%s", code, input->db);
    CTG_ERR_RET(code);
  }
D
dapan1121 已提交
723

D
dapan 已提交
724 725
  ctgDebug("Got db vgInfo from mnode, dbFName:%s", input->db);

D
dapan1121 已提交
726 727
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
728

D
dapan1121 已提交
729 730
int32_t ctgIsTableMetaExistInCache(SCatalog* pCtg, char *dbFName, char* tbName, int32_t *exist) {
  if (NULL == pCtg->dbCache) {
D
dapan1121 已提交
731
    *exist = 0;
D
dapan1121 已提交
732 733 734 735
    ctgWarn("empty db cache, dbFName:%s, tbName:%s", dbFName, tbName);
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
736 737
  SCtgDBCache *dbCache = NULL;
  ctgAcquireDBCache(pCtg, dbFName, &dbCache);
D
dapan1121 已提交
738 739
  if (NULL == dbCache) {
    *exist = 0;
D
dapan1121 已提交
740 741 742
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
743
  size_t sz = 0;
D
dapan1121 已提交
744 745 746 747
  CTG_LOCK(CTG_READ, &dbCache->tbCache.metaLock);  
  STableMeta *tbMeta = taosHashGet(dbCache->tbCache.metaCache, tbName, strlen(tbName));
  CTG_UNLOCK(CTG_READ, &dbCache->tbCache.metaLock);
  
D
dapan1121 已提交
748
  if (NULL == tbMeta) {
D
dapan 已提交
749
    ctgReleaseDBCache(pCtg, dbCache);
D
dapan1121 已提交
750
    
D
dapan1121 已提交
751
    *exist = 0;
D
dapan1121 已提交
752
    ctgDebug("tbmeta not in cache, dbFName:%s, tbName:%s", dbFName, tbName);
D
dapan1121 已提交
753 754 755 756
    return TSDB_CODE_SUCCESS;
  }

  *exist = 1;
D
dapan1121 已提交
757

D
dapan 已提交
758
  ctgReleaseDBCache(pCtg, dbCache);
D
dapan1121 已提交
759
  
D
dapan1121 已提交
760
  ctgDebug("tbmeta is in cache, dbFName:%s, tbName:%s", dbFName, tbName);
D
dapan1121 已提交
761 762 763 764
  
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
765

D
dapan1121 已提交
766
int32_t ctgGetTableMetaFromCache(SCatalog* pCtg, const SName* pTableName, STableMeta** pTableMeta, int32_t *exist, int32_t flag, uint64_t *dbId) {
D
dapan1121 已提交
767
  if (NULL == pCtg->dbCache) {
D
dapan1121 已提交
768
    *exist = 0;
D
dapan1121 已提交
769
    ctgWarn("empty tbmeta cache, tbName:%s", pTableName->tname);
D
dapan1121 已提交
770 771 772
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
773
  char dbFName[TSDB_DB_FNAME_LEN] = {0};
D
dapan1121 已提交
774
  if (CTG_FLAG_IS_INF_DB(flag)) {
D
dapan1121 已提交
775 776 777 778
    strcpy(dbFName, pTableName->dbname);
  } else {
    tNameGetFullDbName(pTableName, dbFName);
  }
D
dapan1121 已提交
779

D
dapan1121 已提交
780 781
  *pTableMeta = NULL;

D
dapan1121 已提交
782 783
  SCtgDBCache *dbCache = NULL;
  ctgAcquireDBCache(pCtg, dbFName, &dbCache);
D
dapan1121 已提交
784 785 786 787 788
  if (NULL == dbCache) {
    *exist = 0;
    return TSDB_CODE_SUCCESS;
  }
  
H
Haojun Liao 已提交
789
  int32_t sz = 0;
D
dapan1121 已提交
790
  CTG_LOCK(CTG_READ, &dbCache->tbCache.metaLock);
H
Haojun Liao 已提交
791
  int32_t code = taosHashGetDup_m(dbCache->tbCache.metaCache, pTableName->tname, strlen(pTableName->tname), (void **)pTableMeta, &sz);
D
dapan1121 已提交
792 793
  CTG_UNLOCK(CTG_READ, &dbCache->tbCache.metaLock);

D
dapan1121 已提交
794
  if (NULL == *pTableMeta) {
D
dapan1121 已提交
795
    *exist = 0;
D
dapan1121 已提交
796 797
    ctgReleaseDBCache(pCtg, dbCache);
    ctgDebug("tbl not in cache, dbFName:%s, tbName:%s", dbFName, pTableName->tname);
D
dapan1121 已提交
798 799 800
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
801
  *exist = 1;
D
dapan1121 已提交
802 803 804
  if (dbId) {
    *dbId = dbCache->dbId;
  }
H
Haojun Liao 已提交
805

H
Haojun Liao 已提交
806
  STableMeta* tbMeta = *pTableMeta;
D
dapan1121 已提交
807

D
dapan1121 已提交
808
  if (tbMeta->tableType != TSDB_CHILD_TABLE) {
D
dapan1121 已提交
809
    ctgReleaseDBCache(pCtg, dbCache);
D
dapan1121 已提交
810
    ctgDebug("Got meta from cache, type:%d, dbFName:%s, tbName:%s", tbMeta->tableType, dbFName, pTableName->tname);
D
dapan1121 已提交
811 812 813
    return TSDB_CODE_SUCCESS;
  }
  
D
dapan1121 已提交
814
  CTG_LOCK(CTG_READ, &dbCache->tbCache.stbLock);
D
dapan1121 已提交
815
  
D
dapan1121 已提交
816
  STableMeta **stbMeta = taosHashGet(dbCache->tbCache.stbCache, &tbMeta->suid, sizeof(tbMeta->suid));
D
dapan1121 已提交
817
  if (NULL == stbMeta || NULL == *stbMeta) {
D
dapan1121 已提交
818
    CTG_UNLOCK(CTG_READ, &dbCache->tbCache.stbLock);
D
dapan1121 已提交
819 820
    ctgReleaseDBCache(pCtg, dbCache);
    ctgError("stb not in stbCache, suid:%"PRIx64, tbMeta->suid);
D
dapan1121 已提交
821 822 823 824
    tfree(*pTableMeta);
    *exist = 0;
    return TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
825

D
dapan1121 已提交
826
  if ((*stbMeta)->suid != tbMeta->suid) {    
D
dapan1121 已提交
827
    CTG_UNLOCK(CTG_READ, &dbCache->tbCache.stbLock);
D
dapan1121 已提交
828
    ctgReleaseDBCache(pCtg, dbCache);
D
dapan1121 已提交
829
    tfree(*pTableMeta);
D
dapan1121 已提交
830
    ctgError("stable suid in stbCache mis-match, expected suid:%"PRIx64 ",actual suid:%"PRIx64, tbMeta->suid, (*stbMeta)->suid);
D
dapan1121 已提交
831 832
    CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
  }
D
dapan1121 已提交
833

D
dapan1121 已提交
834
  int32_t metaSize = CTG_META_SIZE(*stbMeta);
D
dapan1121 已提交
835 836
  *pTableMeta = realloc(*pTableMeta, metaSize);
  if (NULL == *pTableMeta) {    
D
dapan1121 已提交
837
    CTG_UNLOCK(CTG_READ, &dbCache->tbCache.stbLock);
D
dapan1121 已提交
838
    ctgReleaseDBCache(pCtg, dbCache);
D
dapan1121 已提交
839
    ctgError("realloc size[%d] failed", metaSize);
D
dapan1121 已提交
840
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
D
dapan1121 已提交
841 842
  }

D
dapan1121 已提交
843 844
  memcpy(&(*pTableMeta)->sversion, &(*stbMeta)->sversion, metaSize - sizeof(SCTableMeta));

D
dapan1121 已提交
845
  CTG_UNLOCK(CTG_READ, &dbCache->tbCache.stbLock);
D
dapan1121 已提交
846

D
dapan1121 已提交
847
  ctgReleaseDBCache(pCtg, dbCache);
D
dapan 已提交
848

D
dapan1121 已提交
849
  ctgDebug("Got tbmeta from cache, dbFName:%s, tbName:%s", dbFName, pTableName->tname);
D
dapan1121 已提交
850 851 852 853
  
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
854
int32_t ctgGetTableTypeFromCache(SCatalog* pCtg, const SName* pTableName, int32_t *tbType, int32_t flag) {
D
dapan1121 已提交
855
  if (NULL == pCtg->dbCache) {
D
dapan1121 已提交
856
    ctgWarn("empty db cache, tbName:%s", pTableName->tname);  
D
dapan1121 已提交
857 858 859
    return TSDB_CODE_SUCCESS;
  }

D
dapan 已提交
860
  char dbFName[TSDB_DB_FNAME_LEN] = {0};
D
dapan1121 已提交
861
  if (CTG_FLAG_IS_INF_DB(flag)) {
D
dapan1121 已提交
862 863 864 865 866
    strcpy(dbFName, pTableName->dbname);
  } else {
    tNameGetFullDbName(pTableName, dbFName);
  }
  
D
dapan 已提交
867 868
  SCtgDBCache *dbCache = NULL;
  ctgAcquireDBCache(pCtg, dbFName, &dbCache);
D
dapan1121 已提交
869 870 871
  if (NULL == dbCache) {
    return TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
872

D
dapan1121 已提交
873 874 875
  CTG_LOCK(CTG_READ, &dbCache->tbCache.metaLock);
  STableMeta *pTableMeta = (STableMeta *)taosHashAcquire(dbCache->tbCache.metaCache, pTableName->tname, strlen(pTableName->tname));

D
dapan1121 已提交
876
  if (NULL == pTableMeta) {
D
dapan1121 已提交
877
    CTG_UNLOCK(CTG_READ, &dbCache->tbCache.metaLock);
D
dapan 已提交
878 879
    ctgWarn("tbl not in cache, dbFName:%s, tbName:%s", dbFName, pTableName->tname);  
    ctgReleaseDBCache(pCtg, dbCache);
D
dapan1121 已提交
880
    
D
dapan1121 已提交
881 882 883
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
884 885
  *tbType = atomic_load_8(&pTableMeta->tableType);

D
dapan1121 已提交
886 887 888 889
  taosHashRelease(dbCache->tbCache.metaCache, pTableMeta);

  CTG_UNLOCK(CTG_READ, &dbCache->tbCache.metaLock);

D
dapan 已提交
890
  ctgReleaseDBCache(pCtg, dbCache);
D
dapan1121 已提交
891

D
dapan 已提交
892
  ctgDebug("Got tbtype from cache, dbFName:%s, tbName:%s, type:%d", dbFName, pTableName->tname, *tbType);  
D
dapan1121 已提交
893 894 895 896
  
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
897
int32_t ctgGetTableMetaFromMnodeImpl(SCatalog* pCtg, void *pTrans, const SEpSet* pMgmtEps, char *dbFName, char* tbName, STableMetaOutput* output) {
D
dapan1121 已提交
898
  SBuildTableMetaInput bInput = {.vgId = 0, .dbFName = dbFName, .tbName = tbName};
D
dapan1121 已提交
899 900 901 902
  char *msg = NULL;
  SEpSet *pVnodeEpSet = NULL;
  int32_t msgLen = 0;

D
dapan1121 已提交
903
  ctgDebug("try to get table meta from mnode, dbFName:%s, tbName:%s", dbFName, tbName);
D
dapan1121 已提交
904

D
dapan1121 已提交
905
  int32_t code = queryBuildMsg[TMSG_INDEX(TDMT_MND_TABLE_META)](&bInput, &msg, 0, &msgLen);
D
dapan1121 已提交
906 907 908 909
  if (code) {
    ctgError("Build mnode stablemeta msg failed, code:%x", code);
    CTG_ERR_RET(code);
  }
D
dapan1121 已提交
910 911

  SRpcMsg rpcMsg = {
D
dapan1121 已提交
912
      .msgType = TDMT_MND_TABLE_META,
D
dapan1121 已提交
913 914 915
      .pCont   = msg,
      .contLen = msgLen,
  };
D
dapan1121 已提交
916

D
dapan1121 已提交
917 918
  SRpcMsg rpcRsp = {0};

D
dapan1121 已提交
919
  rpcSendRecv(pTrans, (SEpSet*)pMgmtEps, &rpcMsg, &rpcRsp);
D
dapan1121 已提交
920 921
  
  if (TSDB_CODE_SUCCESS != rpcRsp.code) {
D
dapan1121 已提交
922
    if (CTG_TABLE_NOT_EXIST(rpcRsp.code)) {
D
dapan1121 已提交
923
      SET_META_TYPE_NULL(output->metaType);
D
dapan1121 已提交
924
      ctgDebug("stablemeta not exist in mnode, dbFName:%s, tbName:%s", dbFName, tbName);
D
dapan1121 已提交
925 926 927
      return TSDB_CODE_SUCCESS;
    }
    
D
dapan1121 已提交
928
    ctgError("error rsp for stablemeta from mnode, code:%s, dbFName:%s, tbName:%s", tstrerror(rpcRsp.code), dbFName, tbName);
D
dapan1121 已提交
929 930 931
    CTG_ERR_RET(rpcRsp.code);
  }

D
dapan1121 已提交
932
  code = queryProcessMsgRsp[TMSG_INDEX(TDMT_MND_TABLE_META)](output, rpcRsp.pCont, rpcRsp.contLen);
D
dapan1121 已提交
933
  if (code) {
D
dapan1121 已提交
934
    ctgError("Process mnode stablemeta rsp failed, code:%x, dbFName:%s, tbName:%s", code, dbFName, tbName);
D
dapan1121 已提交
935 936 937
    CTG_ERR_RET(code);
  }

D
dapan1121 已提交
938
  ctgDebug("Got table meta from mnode, dbFName:%s, tbName:%s", dbFName, tbName);
D
dapan1121 已提交
939 940 941 942

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
943
int32_t ctgGetTableMetaFromMnode(SCatalog* pCtg, void *pTrans, const SEpSet* pMgmtEps, const SName* pTableName, STableMetaOutput* output) {
D
dapan1121 已提交
944 945
  char dbFName[TSDB_DB_FNAME_LEN];
  tNameGetFullDbName(pTableName, dbFName);
D
dapan1121 已提交
946

D
dapan1121 已提交
947
  return ctgGetTableMetaFromMnodeImpl(pCtg, pTrans, pMgmtEps, dbFName, (char *)pTableName->tname, output);
D
dapan1121 已提交
948
}
D
dapan1121 已提交
949

D
dapan1121 已提交
950
int32_t ctgGetTableMetaFromVnodeImpl(SCatalog* pCtg, void *pTrans, const SEpSet* pMgmtEps, const SName* pTableName, SVgroupInfo *vgroupInfo, STableMetaOutput* output) {
D
dapan1121 已提交
951
  if (NULL == pCtg || NULL == pTrans || NULL == pMgmtEps || NULL == pTableName || NULL == vgroupInfo || NULL == output) {
D
dapan1121 已提交
952
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
D
dapan1121 已提交
953 954
  }

D
dapan1121 已提交
955 956
  char dbFName[TSDB_DB_FNAME_LEN];
  tNameGetFullDbName(pTableName, dbFName);
D
dapan1121 已提交
957

D
dapan1121 已提交
958
  ctgDebug("try to get table meta from vnode, dbFName:%s, tbName:%s", dbFName, tNameGetTableName(pTableName));
D
dapan1121 已提交
959

D
dapan1121 已提交
960
  SBuildTableMetaInput bInput = {.vgId = vgroupInfo->vgId, .dbFName = dbFName, .tbName = (char *)tNameGetTableName(pTableName)};
D
dapan1121 已提交
961 962 963
  char *msg = NULL;
  int32_t msgLen = 0;

D
dapan1121 已提交
964 965
  int32_t code = queryBuildMsg[TMSG_INDEX(TDMT_VND_TABLE_META)](&bInput, &msg, 0, &msgLen);
  if (code) {
D
dapan1121 已提交
966
    ctgError("Build vnode tablemeta msg failed, code:%x, dbFName:%s, tbName:%s", code, dbFName, tNameGetTableName(pTableName));
D
dapan1121 已提交
967 968
    CTG_ERR_RET(code);
  }
D
dapan1121 已提交
969 970

  SRpcMsg rpcMsg = {
H
Hongze Cheng 已提交
971
      .msgType = TDMT_VND_TABLE_META,
D
dapan1121 已提交
972 973 974 975 976
      .pCont   = msg,
      .contLen = msgLen,
  };

  SRpcMsg rpcRsp = {0};
L
Liu Jicong 已提交
977
  rpcSendRecv(pTrans, &vgroupInfo->epSet, &rpcMsg, &rpcRsp);
D
dapan1121 已提交
978
  
D
dapan1121 已提交
979
  if (TSDB_CODE_SUCCESS != rpcRsp.code) {
D
dapan1121 已提交
980
    if (CTG_TABLE_NOT_EXIST(rpcRsp.code)) {
D
dapan1121 已提交
981
      SET_META_TYPE_NULL(output->metaType);
D
dapan1121 已提交
982
      ctgDebug("tablemeta not exist in vnode, dbFName:%s, tbName:%s", dbFName, tNameGetTableName(pTableName));
D
dapan1121 已提交
983 984 985
      return TSDB_CODE_SUCCESS;
    }
  
D
dapan1121 已提交
986
    ctgError("error rsp for table meta from vnode, code:%s, dbFName:%s, tbName:%s", tstrerror(rpcRsp.code), dbFName, tNameGetTableName(pTableName));
D
dapan1121 已提交
987
    CTG_ERR_RET(rpcRsp.code);
D
dapan1121 已提交
988 989
  }

D
dapan1121 已提交
990 991
  code = queryProcessMsgRsp[TMSG_INDEX(TDMT_VND_TABLE_META)](output, rpcRsp.pCont, rpcRsp.contLen);
  if (code) {
D
dapan1121 已提交
992
    ctgError("Process vnode tablemeta rsp failed, code:%s, dbFName:%s, tbName:%s", tstrerror(code), dbFName, tNameGetTableName(pTableName));
D
dapan1121 已提交
993 994 995
    CTG_ERR_RET(code);
  }

D
dapan1121 已提交
996
  ctgDebug("Got table meta from vnode, dbFName:%s, tbName:%s", dbFName, tNameGetTableName(pTableName));
D
dapan1121 已提交
997 998 999
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
int32_t ctgGetTableMetaFromVnode(SCatalog* pCtg, void *pTrans, const SEpSet* pMgmtEps, const SName* pTableName, SVgroupInfo *vgroupInfo, STableMetaOutput* output) {
  int32_t code = 0;
  int32_t retryNum = 0;
  
  while (retryNum < CTG_DEFAULT_MAX_RETRY_TIMES) {
    code = ctgGetTableMetaFromVnodeImpl(pCtg, pTrans, pMgmtEps, pTableName, vgroupInfo, output);
    if (code) {
      if (TSDB_CODE_VND_HASH_MISMATCH == code) {
        char dbFName[TSDB_DB_FNAME_LEN];
        tNameGetFullDbName(pTableName, dbFName);
        
        code = catalogRefreshDBVgInfo(pCtg, pTrans, pMgmtEps, dbFName);
        if (code != TSDB_CODE_SUCCESS) {
          break;
        }

        ++retryNum;
        continue;
      }
    }

    break;
  }

  CTG_RET(code);
}
D
dapan1121 已提交
1026

1027 1028
int32_t ctgGetHashFunction(int8_t hashMethod, tableNameHashFp *fp) {
  switch (hashMethod) {
D
dapan1121 已提交
1029 1030 1031 1032 1033 1034 1035 1036
    default:
      *fp = MurmurHash3_32;
      break;
  }

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1037
int32_t ctgGenerateVgList(SCatalog *pCtg, SHashObj *vgHash, SArray** pList) {
D
dapan1121 已提交
1038
  SHashObj *vgroupHash = NULL;
1039
  SVgroupInfo *vgInfo = NULL;
D
dapan1121 已提交
1040 1041
  SArray *vgList = NULL;
  int32_t code = 0;
D
dapan1121 已提交
1042
  int32_t vgNum = taosHashGetSize(vgHash);
1043

D
dapan1121 已提交
1044
  vgList = taosArrayInit(vgNum, sizeof(SVgroupInfo));
D
dapan1121 已提交
1045
  if (NULL == vgList) {
D
dapan1121 已提交
1046
    ctgError("taosArrayInit failed, num:%d", vgNum);
D
dapan 已提交
1047 1048 1049
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);    
  }

D
dapan1121 已提交
1050
  void *pIter = taosHashIterate(vgHash, NULL);
1051 1052
  while (pIter) {
    vgInfo = pIter;
D
dapan1121 已提交
1053

D
dapan1121 已提交
1054
    if (NULL == taosArrayPush(vgList, vgInfo)) {
D
dapan1121 已提交
1055
      ctgError("taosArrayPush failed, vgId:%d", vgInfo->vgId);
D
dapan1121 已提交
1056
      taosHashCancelIterate(vgHash, pIter);      
D
dapan1121 已提交
1057
      CTG_ERR_JRET(TSDB_CODE_CTG_MEM_ERROR);
1058 1059
    }
    
D
dapan1121 已提交
1060
    pIter = taosHashIterate(vgHash, pIter);
1061
    vgInfo = NULL;
D
dapan1121 已提交
1062 1063
  }

D
dapan1121 已提交
1064
  *pList = vgList;
D
dapan1121 已提交
1065

D
dapan1121 已提交
1066
  ctgDebug("Got vgList from cache, vgNum:%d", vgNum);
D
dapan1121 已提交
1067

D
dapan1121 已提交
1068
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1069 1070 1071 1072 1073 1074 1075 1076

_return:

  if (vgList) {
    taosArrayDestroy(vgList);
  }

  CTG_RET(code);
D
dapan1121 已提交
1077 1078
}

D
dapan1121 已提交
1079
int32_t ctgGetVgInfoFromHashValue(SCatalog *pCtg, SDBVgInfo *dbInfo, const SName *pTableName, SVgroupInfo *pVgroup) {
D
dapan1121 已提交
1080 1081
  int32_t code = 0;
  
D
dapan1121 已提交
1082
  int32_t vgNum = taosHashGetSize(dbInfo->vgHash);
H
Haojun Liao 已提交
1083 1084 1085
  char db[TSDB_DB_FNAME_LEN] = {0};
  tNameGetFullDbName(pTableName, db);

1086
  if (vgNum <= 0) {
D
dapan1121 已提交
1087
    ctgError("db vgroup cache invalid, db:%s, vgroup number:%d", db, vgNum);
D
dapan1121 已提交
1088
    CTG_ERR_RET(TSDB_CODE_TSC_DB_NOT_SELECTED);
D
dapan1121 已提交
1089 1090
  }

1091 1092
  tableNameHashFp fp = NULL;
  SVgroupInfo *vgInfo = NULL;
D
dapan1121 已提交
1093

D
dapan1121 已提交
1094
  CTG_ERR_RET(ctgGetHashFunction(dbInfo->hashMethod, &fp));
1095 1096

  char tbFullName[TSDB_TABLE_FNAME_LEN];
H
Haojun Liao 已提交
1097
  tNameExtractFullName(pTableName, tbFullName);
1098 1099 1100

  uint32_t hashValue = (*fp)(tbFullName, (uint32_t)strlen(tbFullName));

D
dapan1121 已提交
1101
  void *pIter = taosHashIterate(dbInfo->vgHash, NULL);
1102 1103 1104
  while (pIter) {
    vgInfo = pIter;
    if (hashValue >= vgInfo->hashBegin && hashValue <= vgInfo->hashEnd) {
D
dapan1121 已提交
1105
      taosHashCancelIterate(dbInfo->vgHash, pIter);
1106
      break;
D
dapan1121 已提交
1107
    }
1108
    
D
dapan1121 已提交
1109
    pIter = taosHashIterate(dbInfo->vgHash, pIter);
1110
    vgInfo = NULL;
D
dapan1121 已提交
1111 1112
  }

1113
  if (NULL == vgInfo) {
D
dapan1121 已提交
1114 1115
    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);
1116 1117 1118 1119
  }

  *pVgroup = *vgInfo;

1120
  CTG_RET(code);
D
dapan1121 已提交
1121 1122
}

D
dapan1121 已提交
1123
int32_t ctgStbVersionSearchCompare(const void* key1, const void* key2) {
D
dapan 已提交
1124
  if (*(uint64_t *)key1 < ((SSTableMetaVersion*)key2)->suid) {
D
dapan1121 已提交
1125
    return -1;
D
dapan 已提交
1126
  } else if (*(uint64_t *)key1 > ((SSTableMetaVersion*)key2)->suid) {
D
dapan1121 已提交
1127 1128 1129 1130 1131 1132
    return 1;
  } else {
    return 0;
  }
}

D
dapan1121 已提交
1133
int32_t ctgDbVgVersionSearchCompare(const void* key1, const void* key2) {
D
dapan1121 已提交
1134
  if (*(int64_t *)key1 < ((SDbVgVersion*)key2)->dbId) {
D
dapan1121 已提交
1135
    return -1;
D
dapan1121 已提交
1136
  } else if (*(int64_t *)key1 > ((SDbVgVersion*)key2)->dbId) {
D
dapan1121 已提交
1137 1138 1139
    return 1;
  } else {
    return 0;
D
dapan1121 已提交
1140
  }
D
dapan1121 已提交
1141 1142
}

D
dapan1121 已提交
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
int32_t ctgStbVersionSortCompare(const void* key1, const void* key2) {
  if (((SSTableMetaVersion*)key1)->suid < ((SSTableMetaVersion*)key2)->suid) {
    return -1;
  } else if (((SSTableMetaVersion*)key1)->suid > ((SSTableMetaVersion*)key2)->suid) {
    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;
  }
}


D
dapan1121 已提交
1164
int32_t ctgMetaRentInit(SCtgRentMgmt *mgmt, uint32_t rentSec, int8_t type) {
D
dapan1121 已提交
1165 1166 1167 1168
  mgmt->slotRIdx = 0;
  mgmt->slotNum = rentSec / CTG_RENT_SLOT_SECOND;
  mgmt->type = type;

D
dapan1121 已提交
1169
  size_t msgSize = sizeof(SCtgRentSlot) * mgmt->slotNum;
D
dapan1121 已提交
1170
  
D
dapan1121 已提交
1171 1172 1173
  mgmt->slots = calloc(1, msgSize);
  if (NULL == mgmt->slots) {
    qError("calloc %d failed", (int32_t)msgSize);
D
dapan 已提交
1174
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
D
dapan1121 已提交
1175
  }
D
dapan1121 已提交
1176

D
dapan1121 已提交
1177 1178 1179 1180
  qDebug("meta rent initialized, type:%d, slotNum:%d", type, mgmt->slotNum);
  
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
1181

D
dapan1121 已提交
1182

D
dapan1121 已提交
1183
int32_t ctgMetaRentAdd(SCtgRentMgmt *mgmt, void *meta, int64_t id, int32_t size) {
D
dapan1121 已提交
1184
  int16_t widx = abs(id % mgmt->slotNum);
D
dapan1121 已提交
1185

D
dapan1121 已提交
1186
  SCtgRentSlot *slot = &mgmt->slots[widx];
D
dapan1121 已提交
1187 1188 1189 1190 1191 1192 1193 1194
  int32_t code = 0;
  
  CTG_LOCK(CTG_WRITE, &slot->lock);
  if (NULL == slot->meta) {
    slot->meta = taosArrayInit(CTG_DEFAULT_RENT_SLOT_SIZE, size);
    if (NULL == slot->meta) {
      qError("taosArrayInit %d failed, id:%"PRIx64", slot idx:%d, type:%d", CTG_DEFAULT_RENT_SLOT_SIZE, id, widx, mgmt->type);
      CTG_ERR_JRET(TSDB_CODE_CTG_MEM_ERROR);
D
dapan1121 已提交
1195
    }
D
dapan1121 已提交
1196
  }
D
dapan1121 已提交
1197

D
dapan1121 已提交
1198 1199 1200
  if (NULL == taosArrayPush(slot->meta, meta)) {
    qError("taosArrayPush meta to rent failed, id:%"PRIx64", slot idx:%d, type:%d", id, widx, mgmt->type);
    CTG_ERR_JRET(TSDB_CODE_CTG_MEM_ERROR);
D
dapan1121 已提交
1201 1202
  }

D
dapan1121 已提交
1203
  slot->needSort = true;
D
dapan1121 已提交
1204

D
dapan1121 已提交
1205
  qDebug("add meta to rent, id:%"PRIx64", slot idx:%d, type:%d", id, widx, mgmt->type);
D
dapan1121 已提交
1206

D
dapan1121 已提交
1207 1208 1209 1210 1211 1212
_return:

  CTG_UNLOCK(CTG_WRITE, &slot->lock);
  CTG_RET(code);
}

D
dapan1121 已提交
1213
int32_t ctgMetaRentUpdate(SCtgRentMgmt *mgmt, void *meta, int64_t id, int32_t size, __compar_fn_t sortCompare, __compar_fn_t searchCompare) {
D
dapan1121 已提交
1214
  int16_t widx = abs(id % mgmt->slotNum);
D
dapan1121 已提交
1215

D
dapan1121 已提交
1216
  SCtgRentSlot *slot = &mgmt->slots[widx];
D
dapan1121 已提交
1217 1218 1219 1220
  int32_t code = 0;
  
  CTG_LOCK(CTG_WRITE, &slot->lock);
  if (NULL == slot->meta) {
D
dapan1121 已提交
1221 1222
    qError("empty meta slot, id:%"PRIx64", slot idx:%d, type:%d", id, widx, mgmt->type);
    CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
D
dapan1121 已提交
1223 1224 1225
  }

  if (slot->needSort) {
D
dapan1121 已提交
1226
    qDebug("meta slot before sorte, slot idx:%d, type:%d, size:%d", widx, mgmt->type, (int32_t)taosArrayGetSize(slot->meta));
D
dapan1121 已提交
1227
    taosArraySort(slot->meta, sortCompare);
D
dapan1121 已提交
1228
    slot->needSort = false;
D
dapan1121 已提交
1229
    qDebug("meta slot sorted, slot idx:%d, type:%d, size:%d", widx, mgmt->type, (int32_t)taosArrayGetSize(slot->meta));
D
dapan1121 已提交
1230 1231
  }

D
dapan1121 已提交
1232
  void *orig = taosArraySearch(slot->meta, &id, searchCompare, TD_EQ);
D
dapan1121 已提交
1233
  if (NULL == orig) {
D
dapan1121 已提交
1234
    qError("meta not found in slot, id:%"PRIx64", slot idx:%d, type:%d, size:%d", id, widx, mgmt->type, (int32_t)taosArrayGetSize(slot->meta));
D
dapan1121 已提交
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
    CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
  }

  memcpy(orig, meta, size);

  qDebug("meta in rent updated, id:%"PRIx64", slot idx:%d, type:%d", id, widx, mgmt->type);

_return:

  CTG_UNLOCK(CTG_WRITE, &slot->lock);

  if (code) {
    qWarn("meta in rent update failed, will try to add it, code:%x, id:%"PRIx64", slot idx:%d, type:%d", code, id, widx, mgmt->type);
    CTG_RET(ctgMetaRentAdd(mgmt, meta, id, size));
  }

  CTG_RET(code);
}

D
dapan1121 已提交
1254
int32_t ctgMetaRentRemove(SCtgRentMgmt *mgmt, int64_t id, __compar_fn_t sortCompare, __compar_fn_t searchCompare) {
D
dapan1121 已提交
1255 1256
  int16_t widx = abs(id % mgmt->slotNum);

D
dapan1121 已提交
1257
  SCtgRentSlot *slot = &mgmt->slots[widx];
D
dapan1121 已提交
1258 1259 1260 1261 1262 1263 1264 1265 1266
  int32_t code = 0;
  
  CTG_LOCK(CTG_WRITE, &slot->lock);
  if (NULL == slot->meta) {
    qError("empty meta slot, id:%"PRIx64", slot idx:%d, type:%d", id, widx, mgmt->type);
    CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
  }

  if (slot->needSort) {
D
dapan1121 已提交
1267
    taosArraySort(slot->meta, sortCompare);
D
dapan1121 已提交
1268 1269 1270 1271
    slot->needSort = false;
    qDebug("meta slot sorted, slot idx:%d, type:%d", widx, mgmt->type);
  }

D
dapan1121 已提交
1272
  int32_t idx = taosArraySearchIdx(slot->meta, &id, searchCompare, TD_EQ);
D
dapan1121 已提交
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
  if (idx < 0) {
    qError("meta not found in slot, id:%"PRIx64", slot idx:%d, type:%d", id, widx, mgmt->type);
    CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
  }

  taosArrayRemove(slot->meta, idx);

  qDebug("meta in rent removed, id:%"PRIx64", slot idx:%d, type:%d", id, widx, mgmt->type);

_return:

  CTG_UNLOCK(CTG_WRITE, &slot->lock);

  CTG_RET(code);
}


D
dapan1121 已提交
1290
int32_t ctgMetaRentGetImpl(SCtgRentMgmt *mgmt, void **res, uint32_t *num, int32_t size) {
D
dapan1121 已提交
1291 1292 1293 1294
  int16_t ridx = atomic_add_fetch_16(&mgmt->slotRIdx, 1);
  if (ridx >= mgmt->slotNum) {
    ridx %= mgmt->slotNum;
    atomic_store_16(&mgmt->slotRIdx, ridx);
D
dapan1121 已提交
1295
  }
D
dapan1121 已提交
1296

D
dapan1121 已提交
1297
  SCtgRentSlot *slot = &mgmt->slots[ridx];
D
dapan1121 已提交
1298
  int32_t code = 0;
D
dapan1121 已提交
1299
  
D
dapan1121 已提交
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
  CTG_LOCK(CTG_READ, &slot->lock);
  if (NULL == slot->meta) {
    qDebug("empty meta in slot:%d, type:%d", ridx, mgmt->type);
    *num = 0;
    goto _return;
  }

  size_t metaNum = taosArrayGetSize(slot->meta);
  if (metaNum <= 0) {
    qDebug("no meta in slot:%d, type:%d", ridx, mgmt->type);
    *num = 0;
    goto _return;
  }

  size_t msize = metaNum * size;
  *res = malloc(msize);
  if (NULL == *res) {
    qError("malloc %d failed", (int32_t)msize);
    CTG_ERR_JRET(TSDB_CODE_CTG_MEM_ERROR);
  }

  void *meta = taosArrayGet(slot->meta, 0);

  memcpy(*res, meta, msize);

  *num = (uint32_t)metaNum;

  qDebug("Got %d meta from rent, type:%d", (int32_t)metaNum, mgmt->type);

_return:

  CTG_UNLOCK(CTG_READ, &slot->lock);

  CTG_RET(code);
}

D
dapan1121 已提交
1336
int32_t ctgMetaRentGet(SCtgRentMgmt *mgmt, void **res, uint32_t *num, int32_t size) {
D
dapan1121 已提交
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
  while (true) {
    int64_t msec = taosGetTimestampMs();
    int64_t lsec = atomic_load_64(&mgmt->lastReadMsec);
    if ((msec - lsec) < CTG_RENT_SLOT_SECOND * 1000) {
      *res = NULL;
      *num = 0;
      qDebug("too short time period to get expired meta, type:%d", mgmt->type);
      return TSDB_CODE_SUCCESS;
    }

    if (lsec != atomic_val_compare_exchange_64(&mgmt->lastReadMsec, lsec, msec)) {
      continue;
    }

    break;
  }

  CTG_ERR_RET(ctgMetaRentGetImpl(mgmt, res, num, size));

D
dapan1121 已提交
1356 1357 1358
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1359
int32_t ctgAddNewDBCache(SCatalog *pCtg, const char *dbFName, uint64_t dbId) {
D
dapan1121 已提交
1360
  int32_t code = 0;
D
dapan1121 已提交
1361

D
dapan1121 已提交
1362 1363 1364
  SCtgDBCache newDBCache = {0};
  newDBCache.dbId = dbId;

D
dapan 已提交
1365
  newDBCache.tbCache.metaCache = taosHashInit(gCtgMgmt.cfg.maxTblCacheNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
D
dapan1121 已提交
1366
  if (NULL == newDBCache.tbCache.metaCache) {
D
dapan 已提交
1367
    ctgError("taosHashInit %d metaCache failed", gCtgMgmt.cfg.maxTblCacheNum);
D
dapan1121 已提交
1368 1369 1370
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
  }

D
dapan 已提交
1371
  newDBCache.tbCache.stbCache = taosHashInit(gCtgMgmt.cfg.maxTblCacheNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), true, HASH_ENTRY_LOCK);
D
dapan1121 已提交
1372
  if (NULL == newDBCache.tbCache.stbCache) {
D
dapan 已提交
1373
    ctgError("taosHashInit %d stbCache failed", gCtgMgmt.cfg.maxTblCacheNum);
D
dapan1121 已提交
1374 1375 1376 1377
    CTG_ERR_JRET(TSDB_CODE_CTG_MEM_ERROR);
  }

  code = taosHashPut(pCtg->dbCache, dbFName, strlen(dbFName), &newDBCache, sizeof(SCtgDBCache));
D
dapan1121 已提交
1378 1379 1380
  if (code) {
    if (HASH_NODE_EXIST(code)) {
      ctgDebug("db already in cache, dbFName:%s", dbFName);
D
dapan1121 已提交
1381
      goto _return;
D
dapan1121 已提交
1382 1383 1384
    }
    
    ctgError("taosHashPut db to cache failed, dbFName:%s", dbFName);
D
dapan1121 已提交
1385 1386
    CTG_ERR_JRET(TSDB_CODE_CTG_MEM_ERROR);
  }
D
dapan1121 已提交
1387
 
D
dapan1121 已提交
1388
  SDbVgVersion vgVersion = {.dbId = newDBCache.dbId, .vgVersion = -1};
D
dapan1121 已提交
1389 1390
  strncpy(vgVersion.dbFName, dbFName, sizeof(vgVersion.dbFName));

D
dapan1121 已提交
1391
  ctgDebug("db added to cache, dbFName:%s, dbId:%"PRIx64, dbFName, dbId);
D
dapan1121 已提交
1392

D
dapan1121 已提交
1393 1394 1395
  CTG_ERR_RET(ctgMetaRentAdd(&pCtg->dbRent, &vgVersion, dbId, sizeof(SDbVgVersion)));

  ctgDebug("db added to rent, dbFName:%s, vgVersion:%d, dbId:%"PRIx64, dbFName, vgVersion.vgVersion, dbId);
D
dapan1121 已提交
1396

D
dapan1121 已提交
1397
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1398

D
dapan1121 已提交
1399
_return:
D
dapan1121 已提交
1400

D
dapan1121 已提交
1401
  ctgFreeDbCache(&newDBCache);
D
dapan1121 已提交
1402

D
dapan1121 已提交
1403 1404
  CTG_RET(code);
}
D
dapan1121 已提交
1405

D
dapan1121 已提交
1406

D
dapan1121 已提交
1407
void ctgRemoveStbRent(SCatalog* pCtg, SCtgTbMetaCache *cache) {
D
dapan1121 已提交
1408 1409 1410 1411 1412
  CTG_LOCK(CTG_WRITE, &cache->stbLock);
  if (cache->stbCache) {
    void *pIter = taosHashIterate(cache->stbCache, NULL);
    while (pIter) {
      uint64_t *suid = NULL;
H
Haojun Liao 已提交
1413
      suid = taosHashGetKey(pIter, NULL);
D
dapan1121 已提交
1414

D
dapan1121 已提交
1415
      if (TSDB_CODE_SUCCESS == ctgMetaRentRemove(&pCtg->stbRent, *suid, ctgStbVersionSortCompare, ctgStbVersionSearchCompare)) {
D
dapan1121 已提交
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
        ctgDebug("stb removed from rent, suid:%"PRIx64, *suid);
      }
          
      pIter = taosHashIterate(cache->stbCache, pIter);
    }
  }
  CTG_UNLOCK(CTG_WRITE, &cache->stbLock);
}


D
dapan1121 已提交
1426
int32_t ctgRemoveDB(SCatalog* pCtg, SCtgDBCache *dbCache, const char* dbFName) {
D
dapan 已提交
1427 1428 1429 1430
  uint64_t dbId = dbCache->dbId;
  
  ctgInfo("start to remove db from cache, dbFName:%s, dbId:%"PRIx64, dbFName, dbCache->dbId);

D
dapan1121 已提交
1431 1432 1433 1434 1435 1436
  atomic_store_8(&dbCache->deleted, 1);

  ctgRemoveStbRent(pCtg, &dbCache->tbCache);

  ctgFreeDbCache(dbCache);

D
dapan1121 已提交
1437
  CTG_ERR_RET(ctgMetaRentRemove(&pCtg->dbRent, dbCache->dbId, ctgDbVgVersionSortCompare, ctgDbVgVersionSearchCompare));
D
dapan1121 已提交
1438 1439 1440 1441
  
  ctgDebug("db removed from rent, dbFName:%s, dbId:%"PRIx64, dbFName, dbCache->dbId);

  if (taosHashRemove(pCtg->dbCache, dbFName, strlen(dbFName))) {
D
dapan1121 已提交
1442 1443
    ctgInfo("taosHashRemove from dbCache failed, may be removed, dbFName:%s", dbFName);
    CTG_ERR_RET(TSDB_CODE_CTG_DB_DROPPED);
D
dapan1121 已提交
1444
  }
D
dapan 已提交
1445 1446

  ctgInfo("db removed from cache, dbFName:%s, dbId:%"PRIx64, dbFName, dbId);
D
dapan1121 已提交
1447 1448 1449
  
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
1450 1451


D
dapan1121 已提交
1452
int32_t ctgGetAddDBCache(SCatalog* pCtg, const char *dbFName, uint64_t dbId, SCtgDBCache **pCache) {
D
dapan1121 已提交
1453 1454
  int32_t code = 0;
  SCtgDBCache *dbCache = NULL;
D
dapan1121 已提交
1455
  ctgGetDBCache(pCtg, dbFName, &dbCache);
D
dapan1121 已提交
1456
  
D
dapan1121 已提交
1457 1458
  if (dbCache) {
  // TODO OPEN IT
D
dapan1121 已提交
1459
#if 0    
D
dapan1121 已提交
1460 1461 1462 1463
    if (dbCache->dbId == dbId) {
      *pCache = dbCache;
      return TSDB_CODE_SUCCESS;
    }
D
dapan1121 已提交
1464
#else
D
dapan1121 已提交
1465 1466 1467
    if (0 == dbId) {
      *pCache = dbCache;
      return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1468 1469
    }

D
dapan1121 已提交
1470 1471 1472 1473 1474
    if (dbId && (dbCache->dbId == 0)) {
      dbCache->dbId = dbId;
      *pCache = dbCache;
      return TSDB_CODE_SUCCESS;
    }
D
dapan1121 已提交
1475
    
D
dapan1121 已提交
1476 1477 1478 1479 1480 1481
    if (dbCache->dbId == dbId) {
      *pCache = dbCache;
      return TSDB_CODE_SUCCESS;
    }
#endif
    CTG_ERR_RET(ctgRemoveDB(pCtg, dbCache, dbFName));
D
dapan1121 已提交
1482
  }
D
dapan1121 已提交
1483 1484
  
  CTG_ERR_RET(ctgAddNewDBCache(pCtg, dbFName, dbId));
D
dapan1121 已提交
1485

D
dapan1121 已提交
1486
  ctgGetDBCache(pCtg, dbFName, &dbCache);
D
dapan1121 已提交
1487

D
dapan1121 已提交
1488
  *pCache = dbCache;
D
dapan1121 已提交
1489

D
dapan1121 已提交
1490
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1491 1492 1493
}


D
dapan1121 已提交
1494 1495 1496
int32_t ctgUpdateDBVgInfo(SCatalog* pCtg, const char* dbFName, uint64_t dbId, SDBVgInfo** pDbInfo) {
  int32_t code = 0;
  SDBVgInfo* dbInfo = *pDbInfo;
D
dapan1121 已提交
1497 1498 1499 1500

  if (NULL == dbInfo->vgHash) {
    return TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
1501
  
D
dapan1121 已提交
1502
  if (dbInfo->vgVersion < 0 || taosHashGetSize(dbInfo->vgHash) <= 0) {
D
dapan1121 已提交
1503 1504
    ctgError("invalid db vgInfo, dbFName:%s, vgHash:%p, vgVersion:%d, vgHashSize:%d", 
      dbFName, dbInfo->vgHash, dbInfo->vgVersion, taosHashGetSize(dbInfo->vgHash));
D
dapan1121 已提交
1505
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
D
dapan1121 已提交
1506 1507
  }

D
dapan1121 已提交
1508
  bool newAdded = false;
D
dapan 已提交
1509
  SDbVgVersion vgVersion = {.dbId = dbId, .vgVersion = dbInfo->vgVersion, .numOfTable = dbInfo->numOfTable};
D
dapan1121 已提交
1510 1511 1512 1513 1514 1515 1516 1517 1518 1519

  SCtgDBCache *dbCache = NULL;
  CTG_ERR_RET(ctgGetAddDBCache(pCtg, dbFName, dbId, &dbCache));
  if (NULL == dbCache) {
    ctgInfo("conflict db update, ignore this update, dbFName:%s, dbId:%"PRIx64, dbFName, dbId);
    CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
  }

  SDBVgInfo *vgInfo = NULL;
  CTG_ERR_RET(ctgWAcquireVgInfo(pCtg, dbCache));
D
dapan1121 已提交
1520
  
D
dapan1121 已提交
1521
  if (dbCache->vgInfo) {
D
dapan 已提交
1522 1523 1524 1525 1526 1527 1528 1529 1530
    if (dbInfo->vgVersion < dbCache->vgInfo->vgVersion) {
      ctgDebug("db vgVersion is old, dbFName:%s, vgVersion:%d, currentVersion:%d", dbFName, dbInfo->vgVersion, dbCache->vgInfo->vgVersion);
      ctgWReleaseVgInfo(dbCache);
      
      return TSDB_CODE_SUCCESS;
    }

    if (dbInfo->vgVersion == dbCache->vgInfo->vgVersion && dbInfo->numOfTable == dbCache->vgInfo->numOfTable) {
      ctgDebug("no new db vgVersion or numOfTable, dbFName:%s, vgVersion:%d, numOfTable:%d", dbFName, dbInfo->vgVersion, dbInfo->numOfTable);
D
dapan1121 已提交
1531
      ctgWReleaseVgInfo(dbCache);
D
dapan1121 已提交
1532
      
D
dapan1121 已提交
1533
      return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1534
    }
D
dapan1121 已提交
1535 1536

    ctgFreeVgInfo(dbCache->vgInfo);
D
dapan1121 已提交
1537 1538
  }

D
dapan1121 已提交
1539
  dbCache->vgInfo = dbInfo;
D
dapan1121 已提交
1540

D
dapan1121 已提交
1541
  *pDbInfo = NULL;
D
dapan1121 已提交
1542

D
dapan1121 已提交
1543
  ctgDebug("db vgInfo updated, dbFName:%s, vgVersion:%d, dbId:%"PRIx64, dbFName, vgVersion.vgVersion, vgVersion.dbId);
D
dapan1121 已提交
1544

D
dapan1121 已提交
1545
  ctgWReleaseVgInfo(dbCache);
D
dapan1121 已提交
1546

D
dapan1121 已提交
1547
  dbCache = NULL;
D
dapan1121 已提交
1548

D
dapan1121 已提交
1549
  strncpy(vgVersion.dbFName, dbFName, sizeof(vgVersion.dbFName));
D
dapan1121 已提交
1550
  CTG_ERR_RET(ctgMetaRentUpdate(&pCtg->dbRent, &vgVersion, vgVersion.dbId, sizeof(SDbVgVersion), ctgDbVgVersionSortCompare, ctgDbVgVersionSearchCompare));
D
dapan1121 已提交
1551
  
D
dapan1121 已提交
1552 1553 1554 1555
  CTG_RET(code);
}


D
dapan1121 已提交
1556 1557
int32_t ctgUpdateTblMeta(SCatalog *pCtg, SCtgDBCache *dbCache, char *dbFName, uint64_t dbId, char *tbName, STableMeta *meta, int32_t metaSize) {
  SCtgTbMetaCache *tbCache = &dbCache->tbCache;
D
dapan1121 已提交
1558

D
dapan1121 已提交
1559 1560 1561 1562 1563
  CTG_LOCK(CTG_READ, &tbCache->metaLock);
  if (dbCache->deleted || NULL == tbCache->metaCache || NULL == tbCache->stbCache) {
    CTG_UNLOCK(CTG_READ, &tbCache->metaLock);    
    ctgError("db is dropping, dbId:%"PRIx64, dbCache->dbId);
    CTG_ERR_RET(TSDB_CODE_CTG_DB_DROPPED);
D
dapan1121 已提交
1564 1565
  }

D
dapan1121 已提交
1566 1567 1568 1569 1570 1571 1572
  int8_t origType = 0;
  uint64_t origSuid = 0;
  bool isStb = meta->tableType == TSDB_SUPER_TABLE;
  STableMeta *orig = taosHashGet(tbCache->metaCache, tbName, strlen(tbName));
  if (orig) {
    origType = orig->tableType;
    
D
dapan1121 已提交
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582
    if (origType == TSDB_SUPER_TABLE) {
      if ((!isStb) || orig->suid != meta->suid) {
        CTG_LOCK(CTG_WRITE, &tbCache->stbLock);
        if (taosHashRemove(tbCache->stbCache, &orig->suid, sizeof(orig->suid))) {
          ctgError("stb not exist in stbCache, dbFName:%s, stb:%s, suid:%"PRIx64, dbFName, tbName, orig->suid);
        }
        CTG_UNLOCK(CTG_WRITE, &tbCache->stbLock);

        ctgDebug("stb removed from stbCache, dbFName:%s, stb:%s, suid:%"PRIx64, dbFName, tbName, orig->suid);
        
D
dapan1121 已提交
1583
        ctgMetaRentRemove(&pCtg->stbRent, orig->suid, ctgStbVersionSortCompare, ctgStbVersionSearchCompare);
D
dapan1121 已提交
1584
      }
D
dapan1121 已提交
1585

D
dapan1121 已提交
1586
      origSuid = orig->suid;
D
dapan1121 已提交
1587
    }
D
dapan1121 已提交
1588
  }
D
dapan1121 已提交
1589

D
dapan1121 已提交
1590 1591
  if (isStb) {
    CTG_LOCK(CTG_WRITE, &tbCache->stbLock);
D
dapan1121 已提交
1592
  }
D
dapan1121 已提交
1593
  
D
dapan1121 已提交
1594 1595 1596 1597 1598 1599 1600 1601 1602
  if (taosHashPut(tbCache->metaCache, tbName, strlen(tbName), meta, metaSize) != 0) {
    if (isStb) {
      CTG_UNLOCK(CTG_WRITE, &tbCache->stbLock);
    }
    
    CTG_UNLOCK(CTG_READ, &tbCache->metaLock);  
    ctgError("taosHashPut tbmeta to cache failed, dbFName:%s, tbName:%s, tbType:%d", dbFName, tbName, meta->tableType);
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
  }
D
dapan1121 已提交
1603

D
dapan 已提交
1604
  ctgDebug("tbmeta updated to cache, dbFName:%s, tbName:%s, tbType:%d", dbFName, tbName, meta->tableType);
D
dapan1121 已提交
1605
  ctgDbgShowTableMeta(pCtg, tbName, meta);
D
dapan 已提交
1606

D
dapan1121 已提交
1607 1608 1609
  if (!isStb) {
    CTG_UNLOCK(CTG_READ, &tbCache->metaLock);  
    return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1610
  }
D
dapan1121 已提交
1611

D
dapan1121 已提交
1612
  if (origType == TSDB_SUPER_TABLE && origSuid == meta->suid) {
D
dapan1121 已提交
1613 1614 1615 1616
    CTG_UNLOCK(CTG_WRITE, &tbCache->stbLock);
    CTG_UNLOCK(CTG_READ, &tbCache->metaLock);  
    return TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
1617

D
dapan1121 已提交
1618 1619 1620 1621
  STableMeta *tbMeta = taosHashGet(tbCache->metaCache, tbName, strlen(tbName));
  if (taosHashPut(tbCache->stbCache, &meta->suid, sizeof(meta->suid), &tbMeta, POINTER_BYTES) != 0) {
    CTG_UNLOCK(CTG_WRITE, &tbCache->stbLock);
    CTG_UNLOCK(CTG_READ, &tbCache->metaLock);    
H
Haojun Liao 已提交
1622
    ctgError("taosHashPut stable to stable cache failed, suid:%"PRIx64, meta->suid);
D
dapan1121 已提交
1623
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
D
dapan1121 已提交
1624
  }
D
dapan1121 已提交
1625 1626
  
  CTG_UNLOCK(CTG_WRITE, &tbCache->stbLock);
D
dapan1121 已提交
1627

D
dapan1121 已提交
1628 1629
  CTG_UNLOCK(CTG_READ, &tbCache->metaLock);

D
dapan 已提交
1630
  ctgDebug("stb updated to stbCache, dbFName:%s, tbName:%s, tbType:%d", dbFName, tbName, meta->tableType);
D
dapan1121 已提交
1631 1632 1633 1634 1635 1636 1637

  SSTableMetaVersion metaRent = {.dbId = dbId, .suid = meta->suid, .sversion = meta->sversion, .tversion = meta->tversion};
  strcpy(metaRent.dbFName, dbFName);
  strcpy(metaRent.stbName, tbName);
  CTG_ERR_RET(ctgMetaRentAdd(&pCtg->stbRent, &metaRent, metaRent.suid, sizeof(SSTableMetaVersion)));
  
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1638 1639
}

D
dapan 已提交
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659
int32_t ctgCloneVgInfo(SDBVgInfo *src, SDBVgInfo **dst) {
  *dst = malloc(sizeof(SDBVgInfo));
  if (NULL == *dst) {
    qError("malloc %d failed", (int32_t)sizeof(SDBVgInfo));
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
  }

  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);
    tfree(*dst);
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
  }

  int32_t *vgId = NULL;
  void *pIter = taosHashIterate(src->vgHash, NULL);
  while (pIter) {
H
Haojun Liao 已提交
1660
    vgId = taosHashGetKey(pIter, NULL);
D
dapan 已提交
1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678

    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);
      tfree(*dst);
      CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
    }
    
    pIter = taosHashIterate(src->vgHash, pIter);
  }


  return TSDB_CODE_SUCCESS;
}



D
dapan1121 已提交
1679
int32_t ctgGetDBVgInfo(SCatalog* pCtg, void *pRpc, const SEpSet* pMgmtEps, const char* dbFName, SCtgDBCache** dbCache, SDBVgInfo **pInfo) {
D
dapan1121 已提交
1680
  bool inCache = false;
D
dapan1121 已提交
1681
  int32_t code = 0;
D
dapan1121 已提交
1682 1683 1684

  CTG_ERR_RET(ctgAcquireVgInfoFromCache(pCtg, dbFName, dbCache, &inCache));

D
dapan1121 已提交
1685
  if (inCache) {
D
dapan1121 已提交
1686
    return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1687 1688 1689 1690 1691
  }

  SUseDbOutput DbOut = {0};
  SBuildUseDBInput input = {0};

D
dapan1121 已提交
1692
  tstrncpy(input.db, dbFName, tListLen(input.db));
D
dapan1121 已提交
1693
  input.vgVersion = CTG_DEFAULT_INVALID_VERSION;
H
Haojun Liao 已提交
1694

D
dapan1121 已提交
1695 1696 1697 1698 1699 1700 1701 1702 1703
  code = ctgGetDBVgInfoFromMnode(pCtg, pRpc, pMgmtEps, &input, &DbOut);
  if (code) {
    if (CTG_DB_NOT_EXIST(code) && input.vgVersion > CTG_DEFAULT_INVALID_VERSION) {
      ctgDebug("db no longer exist, dbFName:%s, dbId:%" PRIx64, input.db, input.dbId);
      ctgPushRmDBMsgInQueue(pCtg, input.db, input.dbId);
    }

    CTG_ERR_RET(code);
  }
D
dapan1121 已提交
1704

D
dapan 已提交
1705
  CTG_ERR_JRET(ctgCloneVgInfo(DbOut.dbVgroup, pInfo));
D
dapan1121 已提交
1706

D
dapan1121 已提交
1707
  CTG_ERR_RET(ctgPushUpdateVgMsgInQueue(pCtg, dbFName, DbOut.dbId, DbOut.dbVgroup, false));
D
dapan 已提交
1708

D
dapan1121 已提交
1709
  return TSDB_CODE_SUCCESS;
D
dapan 已提交
1710 1711 1712 1713 1714 1715 1716

_return:

  tfree(*pInfo);
  *pInfo = DbOut.dbVgroup;
  
  CTG_RET(code);
D
dapan1121 已提交
1717 1718
}

D
dapan1121 已提交
1719 1720 1721
int32_t ctgRefreshDBVgInfo(SCatalog* pCtg, void *pRpc, const SEpSet* pMgmtEps, const char* dbFName) {
  bool inCache = false;
  int32_t code = 0;
D
dapan1121 已提交
1722
  SCtgDBCache* dbCache = NULL;
D
dapan1121 已提交
1723

D
dapan1121 已提交
1724
  CTG_ERR_RET(ctgAcquireVgInfoFromCache(pCtg, dbFName, &dbCache, &inCache));
D
dapan1121 已提交
1725 1726 1727 1728 1729 1730

  SUseDbOutput DbOut = {0};
  SBuildUseDBInput input = {0};
  tstrncpy(input.db, dbFName, tListLen(input.db));

  if (inCache) {
D
dapan1121 已提交
1731 1732 1733
    input.dbId = dbCache->dbId;
    input.vgVersion = dbCache->vgInfo->vgVersion;
    input.numOfTable = dbCache->vgInfo->numOfTable;
D
dapan1121 已提交
1734

D
dapan1121 已提交
1735 1736
    ctgReleaseVgInfo(dbCache);
    ctgReleaseDBCache(pCtg, dbCache);
D
dapan1121 已提交
1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
  } else {
    input.vgVersion = CTG_DEFAULT_INVALID_VERSION;
  }

  code = ctgGetDBVgInfoFromMnode(pCtg, pRpc, pMgmtEps, &input, &DbOut);
  if (code) {
    if (CTG_DB_NOT_EXIST(code) && input.vgVersion > CTG_DEFAULT_INVALID_VERSION) {
      ctgDebug("db no longer exist, dbFName:%s, dbId:%" PRIx64, input.db, input.dbId);
      ctgPushRmDBMsgInQueue(pCtg, input.db, input.dbId);
    }

    CTG_ERR_RET(code);
  }

  CTG_ERR_RET(ctgPushUpdateVgMsgInQueue(pCtg, dbFName, DbOut.dbId, DbOut.dbVgroup, true));

  return TSDB_CODE_SUCCESS;
}


D
dapan 已提交
1757

D
dapan1121 已提交
1758 1759 1760 1761 1762
int32_t ctgCloneMetaOutput(STableMetaOutput *output, STableMetaOutput **pOutput) {
  *pOutput = malloc(sizeof(STableMetaOutput));
  if (NULL == *pOutput) {
    qError("malloc %d failed", (int32_t)sizeof(STableMetaOutput));
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
D
dapan 已提交
1763 1764
  }

D
dapan1121 已提交
1765 1766 1767 1768 1769 1770
  memcpy(*pOutput, output, sizeof(STableMetaOutput));

  if (output->tbMeta) {
    int32_t metaSize = CTG_META_SIZE(output->tbMeta);
    (*pOutput)->tbMeta = malloc(metaSize);
    if (NULL == (*pOutput)->tbMeta) {
D
dapan 已提交
1771
      qError("malloc %d failed", (int32_t)sizeof(STableMetaOutput));
D
dapan1121 已提交
1772 1773 1774 1775 1776
      tfree(*pOutput);
      CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
    }

    memcpy((*pOutput)->tbMeta, output->tbMeta, metaSize);
D
dapan 已提交
1777 1778
  }

D
dapan1121 已提交
1779 1780 1781
  return TSDB_CODE_SUCCESS;
}

D
dapan 已提交
1782 1783


D
dapan1121 已提交
1784
int32_t ctgRefreshTblMeta(SCatalog* pCtg, void *pTrans, const SEpSet* pMgmtEps, const SName* pTableName, int32_t flag, STableMetaOutput **pOutput, bool syncReq) {
D
dapan1121 已提交
1785
  if (NULL == pCtg || NULL == pTrans || NULL == pMgmtEps || NULL == pTableName) {
D
dapan1121 已提交
1786 1787 1788 1789 1790 1791
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
  }

  SVgroupInfo vgroupInfo = {0};
  int32_t code = 0;

D
dapan1121 已提交
1792
  if (!CTG_FLAG_IS_INF_DB(flag)) {
D
dapan1121 已提交
1793 1794
    CTG_ERR_RET(catalogGetTableHashVgroup(pCtg, pTrans, pMgmtEps, pTableName, &vgroupInfo));
  }
D
dapan1121 已提交
1795

D
dapan1121 已提交
1796
  STableMetaOutput  moutput = {0};
S
Shengliang Guan 已提交
1797
  STableMetaOutput *output = calloc(1, sizeof(STableMetaOutput));
D
dapan1121 已提交
1798 1799 1800 1801
  if (NULL == output) {
    ctgError("malloc %d failed", (int32_t)sizeof(STableMetaOutput));
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
  }
D
dapan1121 已提交
1802

D
dapan1121 已提交
1803
  if (CTG_FLAG_IS_INF_DB(flag)) {
D
dapan1121 已提交
1804 1805 1806
    ctgDebug("will refresh tbmeta, supposed in information_schema, tbName:%s", tNameGetTableName(pTableName));

    CTG_ERR_JRET(ctgGetTableMetaFromMnodeImpl(pCtg, pTrans, pMgmtEps, (char *)pTableName->dbname, (char *)pTableName->tname, output));
D
dapan1121 已提交
1807
  } else if (CTG_FLAG_IS_STB(flag)) {
D
dapan 已提交
1808
    ctgDebug("will refresh tbmeta, supposed to be stb, tbName:%s", tNameGetTableName(pTableName));
D
dapan1121 已提交
1809 1810

    // if get from mnode failed, will not try vnode
D
dapan1121 已提交
1811
    CTG_ERR_JRET(ctgGetTableMetaFromMnode(pCtg, pTrans, pMgmtEps, pTableName, output));
D
dapan1121 已提交
1812

D
dapan1121 已提交
1813
    if (CTG_IS_META_NULL(output->metaType)) {
D
dapan1121 已提交
1814
      CTG_ERR_JRET(ctgGetTableMetaFromVnode(pCtg, pTrans, pMgmtEps, pTableName, &vgroupInfo, output));
D
dapan1121 已提交
1815 1816
    }
  } else {
D
dapan1121 已提交
1817
    ctgDebug("will refresh tbmeta, not supposed to be stb, tbName:%s, flag:%d", tNameGetTableName(pTableName), flag);
D
dapan1121 已提交
1818 1819

    // if get from vnode failed or no table meta, will not try mnode
D
dapan1121 已提交
1820
    CTG_ERR_JRET(ctgGetTableMetaFromVnode(pCtg, pTrans, pMgmtEps, pTableName, &vgroupInfo, output));
D
dapan1121 已提交
1821

D
dapan1121 已提交
1822
    if (CTG_IS_META_TABLE(output->metaType) && TSDB_SUPER_TABLE == output->tbMeta->tableType) {
D
dapan1121 已提交
1823
      ctgDebug("will continue to refresh tbmeta since got stb, tbName:%s", tNameGetTableName(pTableName));
D
dapan1121 已提交
1824

D
dapan1121 已提交
1825
      tfree(output->tbMeta);
D
dapan1121 已提交
1826
      
D
dapan1121 已提交
1827
      CTG_ERR_JRET(ctgGetTableMetaFromMnodeImpl(pCtg, pTrans, pMgmtEps, output->dbFName, output->tbName, output));
D
dapan1121 已提交
1828
    } else if (CTG_IS_META_BOTH(output->metaType)) {
D
dapan1121 已提交
1829
      int32_t exist = 0;
D
dapan1121 已提交
1830 1831 1832
      if (!CTG_FLAG_IS_FORCE_UPDATE(flag)) {
        CTG_ERR_JRET(ctgIsTableMetaExistInCache(pCtg, output->dbFName, output->tbName, &exist));
      }
H
Haojun Liao 已提交
1833

D
dapan1121 已提交
1834
      if (0 == exist) {
D
dapan1121 已提交
1835
        CTG_ERR_JRET(ctgGetTableMetaFromMnodeImpl(pCtg, pTrans, pMgmtEps, output->dbFName, output->tbName, &moutput));
D
dapan1121 已提交
1836

D
dapan1121 已提交
1837
        if (CTG_IS_META_NULL(moutput.metaType)) {
D
dapan1121 已提交
1838
          SET_META_TYPE_NULL(output->metaType);
D
dapan1121 已提交
1839 1840
        }
        
D
dapan1121 已提交
1841 1842
        tfree(output->tbMeta);
        output->tbMeta = moutput.tbMeta;
D
dapan1121 已提交
1843 1844
        moutput.tbMeta = NULL;
      } else {
D
dapan1121 已提交
1845
        tfree(output->tbMeta);
D
dapan1121 已提交
1846
        
D
dapan1121 已提交
1847
        SET_META_TYPE_CTABLE(output->metaType); 
D
dapan1121 已提交
1848
      }
D
dapan1121 已提交
1849 1850 1851
    }
  }

D
dapan1121 已提交
1852
  if (CTG_IS_META_NULL(output->metaType)) {
D
dapan 已提交
1853
    ctgError("no tbmeta got, tbNmae:%s", tNameGetTableName(pTableName));
D
dapan1121 已提交
1854 1855 1856
    CTG_ERR_JRET(CTG_ERR_CODE_TABLE_NOT_EXIST);
  }

D
dapan 已提交
1857 1858 1859 1860 1861 1862
  if (CTG_IS_META_TABLE(output->metaType)) {
    ctgDebug("tbmeta got, dbFName:%s, tbName:%s, tbType:%d", output->dbFName, output->tbName, output->tbMeta->tableType);
  } else {
    ctgDebug("tbmeta got, dbFName:%s, tbName:%s, tbType:%d, stbMetaGot:%d", output->dbFName, output->ctbName, output->ctbMeta.tableType, CTG_IS_META_BOTH(output->metaType));
  }

D
dapan1121 已提交
1863 1864 1865 1866
  if (pOutput) {
    CTG_ERR_JRET(ctgCloneMetaOutput(output, pOutput));
  }

D
dapan1121 已提交
1867
  CTG_ERR_JRET(ctgPushUpdateTblMsgInQueue(pCtg, output, syncReq));
D
dapan 已提交
1868

D
dapan1121 已提交
1869
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1870 1871 1872

_return:

D
dapan1121 已提交
1873
  tfree(output->tbMeta);
D
dapan 已提交
1874
  tfree(output);
D
dapan1121 已提交
1875 1876 1877 1878
  
  CTG_RET(code);
}

D
dapan1121 已提交
1879
int32_t ctgGetTableMeta(SCatalog* pCtg, void *pRpc, const SEpSet* pMgmtEps, const SName* pTableName, STableMeta** pTableMeta, int32_t flag) {
D
dapan1121 已提交
1880
  if (NULL == pCtg || NULL == pRpc || NULL == pMgmtEps || NULL == pTableName || NULL == pTableMeta) {
D
dapan1121 已提交
1881 1882 1883 1884
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
  }
  
  int32_t exist = 0;
D
dapan1121 已提交
1885
  int32_t code = 0;
D
dapan1121 已提交
1886 1887 1888
  uint64_t dbId = 0;
  uint64_t suid = 0;
  STableMetaOutput *output = NULL;
D
dapan1121 已提交
1889

D
dapan1121 已提交
1890
  if (CTG_IS_INF_DBNAME(pTableName->dbname)) {
D
dapan1121 已提交
1891
    CTG_FLAG_SET_INF_DB(flag);
D
dapan1121 已提交
1892 1893
  }

D
dapan1121 已提交
1894
  CTG_ERR_RET(ctgGetTableMetaFromCache(pCtg, pTableName, pTableMeta, &exist, flag, &dbId));
D
dapan1121 已提交
1895

D
dapan1121 已提交
1896 1897 1898 1899 1900
  int32_t tbType = 0;

  if (exist) {
    if (CTG_FLAG_MATCH_STB(flag, (*pTableMeta)->tableType) && ((!CTG_FLAG_IS_FORCE_UPDATE(flag)) || (CTG_FLAG_IS_INF_DB(flag)))) {
      goto _return;
D
dapan1121 已提交
1901
    }
D
dapan1121 已提交
1902

D
dapan1121 已提交
1903 1904
    tbType = (*pTableMeta)->tableType;
    suid = (*pTableMeta)->suid;
D
dapan1121 已提交
1905

D
dapan1121 已提交
1906
    tfree(*pTableMeta);
D
dapan1121 已提交
1907
  }
D
dapan1121 已提交
1908

D
dapan1121 已提交
1909 1910
  if (CTG_FLAG_IS_UNKNOWN_STB(flag)) {
    CTG_FLAG_SET_STB(flag, tbType);
D
dapan1121 已提交
1911 1912
  }

D
dapan1121 已提交
1913

D
dapan 已提交
1914
  while (true) {
D
dapan1121 已提交
1915
    CTG_ERR_JRET(ctgRefreshTblMeta(pCtg, pRpc, pMgmtEps, pTableName, flag, &output, false));
D
dapan1121 已提交
1916

D
dapan 已提交
1917 1918 1919 1920
    if (CTG_IS_META_TABLE(output->metaType)) {
      *pTableMeta = output->tbMeta;
      goto _return;
    }
D
dapan1121 已提交
1921

D
dapan 已提交
1922 1923 1924 1925 1926 1927
    if (CTG_IS_META_BOTH(output->metaType)) {
      memcpy(output->tbMeta, &output->ctbMeta, sizeof(output->ctbMeta));
      
      *pTableMeta = output->tbMeta;
      goto _return;
    }
D
dapan1121 已提交
1928

D
dapan 已提交
1929 1930 1931 1932 1933
    if ((!CTG_IS_META_CTABLE(output->metaType)) || output->tbMeta) {
      ctgError("invalid metaType:%d", output->metaType);
      tfree(output->tbMeta);
      CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
    }
D
dapan1121 已提交
1934

D
dapan 已提交
1935
    // HANDLE ONLY CHILD TABLE META
D
dapan1121 已提交
1936

D
dapan 已提交
1937 1938 1939
    SName stbName = *pTableName;
    strcpy(stbName.tname, output->tbName);
    
D
dapan1121 已提交
1940
    CTG_ERR_JRET(ctgGetTableMetaFromCache(pCtg, &stbName, pTableMeta, &exist, flag, NULL));
D
dapan 已提交
1941 1942 1943 1944
    if (0 == exist) {
      ctgDebug("stb no longer exist, dbFName:%s, tbName:%s", output->dbFName, pTableName->tname);
      continue;
    }
D
dapan1121 已提交
1945

D
dapan 已提交
1946 1947 1948 1949
    memcpy(*pTableMeta, &output->ctbMeta, sizeof(output->ctbMeta));

    break;
  }
D
dapan1121 已提交
1950 1951 1952

_return:

D
dapan1121 已提交
1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967
  if (CTG_TABLE_NOT_EXIST(code) && exist) {
    char dbFName[TSDB_DB_FNAME_LEN] = {0};
    if (CTG_FLAG_IS_INF_DB(flag)) {
      strcpy(dbFName, pTableName->dbname);
    } else {
      tNameGetFullDbName(pTableName, dbFName);
    }

    if (TSDB_SUPER_TABLE == tbType) {
      ctgPushRmStbMsgInQueue(pCtg, dbFName, dbId, pTableName->tname, suid);
    } else {
      ctgPushRmTblMsgInQueue(pCtg, dbFName, dbId, pTableName->tname);
    }
  }

D
dapan1121 已提交
1968 1969
  tfree(output);

D
dapan 已提交
1970 1971
  if (*pTableMeta) {
    ctgDebug("tbmeta returned, tbName:%s, tbType:%d", pTableName->tname, (*pTableMeta)->tableType);
D
dapan1121 已提交
1972
    ctgDbgShowTableMeta(pCtg, pTableName->tname, *pTableMeta);
D
dapan 已提交
1973 1974
  }

D
dapan1121 已提交
1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987
  CTG_RET(code);
}



int32_t ctgActUpdateVg(SCtgMetaAction *action) {
  int32_t code = 0;
  SCtgUpdateVgMsg *msg = action->data;
  
  CTG_ERR_JRET(ctgUpdateDBVgInfo(msg->pCtg, msg->dbFName, msg->dbId, &msg->dbInfo));

_return:

D
dapan1121 已提交
1988
  ctgFreeVgInfo(msg->dbInfo);
D
dapan1121 已提交
1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999
  tfree(msg);
  
  CTG_RET(code);
}

int32_t ctgActRemoveDB(SCtgMetaAction *action) {
  int32_t code = 0;
  SCtgRemoveDBMsg *msg = action->data;
  SCatalog* pCtg = msg->pCtg;

  SCtgDBCache *dbCache = NULL;
D
dapan 已提交
2000
  ctgGetDBCache(msg->pCtg, msg->dbFName, &dbCache);
D
dapan1121 已提交
2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028
  if (NULL == dbCache) {
    goto _return;
  }
  
  if (dbCache->dbId != msg->dbId) {
    ctgInfo("dbId already updated, dbFName:%s, dbId:%"PRIx64 ", targetId:%"PRIx64, msg->dbFName, dbCache->dbId, msg->dbId);
    goto _return;
  }
  
  CTG_ERR_JRET(ctgRemoveDB(pCtg, dbCache, msg->dbFName));

_return:

  tfree(msg);
  
  CTG_RET(code);
}


int32_t ctgActUpdateTbl(SCtgMetaAction *action) {
  int32_t code = 0;
  SCtgUpdateTblMsg *msg = action->data;
  SCatalog* pCtg = msg->pCtg;
  STableMetaOutput* output = msg->output;
  SCtgDBCache *dbCache = NULL;

  if ((!CTG_IS_META_CTABLE(output->metaType)) && NULL == output->tbMeta) {
    ctgError("no valid tbmeta got from meta rsp, dbFName:%s, tbName:%s", output->dbFName, output->tbName);
D
dapan 已提交
2029
    CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
D
dapan1121 已提交
2030 2031 2032 2033 2034 2035
  }

  if (CTG_IS_META_BOTH(output->metaType) && TSDB_SUPER_TABLE != output->tbMeta->tableType) {
    ctgError("table type error, expected:%d, actual:%d", TSDB_SUPER_TABLE, output->tbMeta->tableType);
    CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
  }    
D
dapan1121 已提交
2036
  
D
dapan1121 已提交
2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054
  CTG_ERR_JRET(ctgGetAddDBCache(pCtg, output->dbFName, output->dbId, &dbCache));
  if (NULL == dbCache) {
    ctgInfo("conflict db update, ignore this update, dbFName:%s, dbId:%"PRIx64, output->dbFName, output->dbId);
    CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
  }

  if (CTG_IS_META_TABLE(output->metaType) || CTG_IS_META_BOTH(output->metaType)) {
    int32_t metaSize = CTG_META_SIZE(output->tbMeta);
    
    CTG_ERR_JRET(ctgUpdateTblMeta(pCtg, dbCache, output->dbFName, output->dbId, output->tbName, output->tbMeta, metaSize));
  }

  if (CTG_IS_META_CTABLE(output->metaType) || CTG_IS_META_BOTH(output->metaType)) {
    CTG_ERR_JRET(ctgUpdateTblMeta(pCtg, dbCache, output->dbFName, output->dbId, output->ctbName, (STableMeta *)&output->ctbMeta, sizeof(output->ctbMeta)));
  }

_return:

D
dapan 已提交
2055 2056 2057
  if (output) {
    tfree(output->tbMeta);
    tfree(output);
D
dapan1121 已提交
2058
  }
D
dapan 已提交
2059
  
D
dapan1121 已提交
2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093
  tfree(msg);
  
  CTG_RET(code);
}


int32_t ctgActRemoveStb(SCtgMetaAction *action) {
  int32_t code = 0;
  SCtgRemoveStbMsg *msg = action->data;
  SCatalog* pCtg = msg->pCtg;

  SCtgDBCache *dbCache = NULL;
  ctgGetDBCache(pCtg, msg->dbFName, &dbCache);
  if (NULL == dbCache) {
    return TSDB_CODE_SUCCESS;
  }

  if (dbCache->dbId != msg->dbId) {
    ctgDebug("dbId already modified, dbFName:%s, current:%"PRIx64", dbId:%"PRIx64", stb:%s, suid:%"PRIx64, msg->dbFName, dbCache->dbId, msg->dbId, msg->stbName, msg->suid);
    return TSDB_CODE_SUCCESS;
  }
  
  CTG_LOCK(CTG_WRITE, &dbCache->tbCache.stbLock);
  if (taosHashRemove(dbCache->tbCache.stbCache, &msg->suid, sizeof(msg->suid))) {
    CTG_UNLOCK(CTG_WRITE, &dbCache->tbCache.stbLock);
    ctgDebug("stb not exist in stbCache, may be removed, dbFName:%s, stb:%s, suid:%"PRIx64, msg->dbFName, msg->stbName, msg->suid);
    return TSDB_CODE_SUCCESS;
  }

  CTG_LOCK(CTG_READ, &dbCache->tbCache.metaLock);
  if (taosHashRemove(dbCache->tbCache.metaCache, msg->stbName, strlen(msg->stbName))) {  
    CTG_UNLOCK(CTG_READ, &dbCache->tbCache.metaLock);
    CTG_UNLOCK(CTG_WRITE, &dbCache->tbCache.stbLock);
    ctgError("stb not exist in cache, dbFName:%s, stb:%s, suid:%"PRIx64, msg->dbFName, msg->stbName, msg->suid);
D
dapan1121 已提交
2094
    CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
D
dapan1121 已提交
2095 2096 2097 2098 2099 2100 2101
  }  
  CTG_UNLOCK(CTG_READ, &dbCache->tbCache.metaLock);
  
  CTG_UNLOCK(CTG_WRITE, &dbCache->tbCache.stbLock);
  
  ctgInfo("stb removed from cache, dbFName:%s, stbName:%s, suid:%"PRIx64, msg->dbFName, msg->stbName, msg->suid);

D
dapan1121 已提交
2102
  CTG_ERR_JRET(ctgMetaRentRemove(&msg->pCtg->stbRent, msg->suid, ctgStbVersionSortCompare, ctgStbVersionSearchCompare));
D
dapan1121 已提交
2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113
  
  ctgDebug("stb removed from rent, dbFName:%s, stbName:%s, suid:%"PRIx64, msg->dbFName, msg->stbName, msg->suid);
  
_return:

  tfree(msg);
  
  CTG_RET(code);
}

int32_t ctgActRemoveTbl(SCtgMetaAction *action) {
D
dapan1121 已提交
2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127
  int32_t code = 0;
  SCtgRemoveTblMsg *msg = action->data;
  SCatalog* pCtg = msg->pCtg;

  SCtgDBCache *dbCache = NULL;
  ctgGetDBCache(pCtg, msg->dbFName, &dbCache);
  if (NULL == dbCache) {
    return TSDB_CODE_SUCCESS;
  }

  if (dbCache->dbId != msg->dbId) {
    ctgDebug("dbId already modified, dbFName:%s, current:%"PRIx64", dbId:%"PRIx64", tbName:%s", msg->dbFName, dbCache->dbId, msg->dbId, msg->tbName);
    return TSDB_CODE_SUCCESS;
  }
H
Haojun Liao 已提交
2128

D
dapan1121 已提交
2129
  CTG_LOCK(CTG_READ, &dbCache->tbCache.metaLock);
H
Haojun Liao 已提交
2130
  if (taosHashRemove(dbCache->tbCache.metaCache, msg->tbName, strlen(msg->tbName))) {
D
dapan1121 已提交
2131 2132 2133
    CTG_UNLOCK(CTG_READ, &dbCache->tbCache.metaLock);
    ctgError("stb not exist in cache, dbFName:%s, tbName:%s", msg->dbFName, msg->tbName);
    CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
H
Haojun Liao 已提交
2134
  }
D
dapan1121 已提交
2135
  CTG_UNLOCK(CTG_READ, &dbCache->tbCache.metaLock);
D
dapan1121 已提交
2136

D
dapan1121 已提交
2137
  ctgInfo("table removed from cache, dbFName:%s, tbName:%s", msg->dbFName, msg->tbName);
H
Haojun Liao 已提交
2138

D
dapan1121 已提交
2139
_return:
D
dapan1121 已提交
2140

D
dapan1121 已提交
2141
  tfree(msg);
H
Haojun Liao 已提交
2142

D
dapan1121 已提交
2143
  CTG_RET(code);
D
dapan1121 已提交
2144 2145 2146 2147 2148 2149 2150 2151
}


void* ctgUpdateThreadFunc(void* param) {
  setThreadName("catalog");

  qInfo("catalog update thread started");

D
dapan 已提交
2152
  CTG_LOCK(CTG_READ, &gCtgMgmt.lock);
D
dapan1121 已提交
2153 2154
  
  while (true) {
D
dapan1121 已提交
2155
    tsem_wait(&gCtgMgmt.queue.reqSem);
D
dapan1121 已提交
2156
    
D
dapan 已提交
2157
    if (atomic_load_8(&gCtgMgmt.exit)) {
D
dapan1121 已提交
2158
      tsem_post(&gCtgMgmt.queue.rspSem);
D
dapan1121 已提交
2159 2160 2161 2162 2163
      break;
    }

    SCtgMetaAction *action = NULL;
    ctgPopAction(&action);
D
dapan1121 已提交
2164
    SCatalog *pCtg = ((SCtgUpdateMsgHeader *)action->data)->pCtg;
D
dapan1121 已提交
2165

D
dapan1121 已提交
2166
    ctgDebug("process [%s] action", gCtgAction[action->act].name);
D
dapan 已提交
2167 2168 2169
    
    (*gCtgAction[action->act].func)(action);

D
dapan1121 已提交
2170 2171 2172 2173 2174 2175
    gCtgMgmt.queue.seqDone = action->seqId;

    if (action->syncReq) {
      tsem_post(&gCtgMgmt.queue.rspSem);
    }

D
dapan 已提交
2176
    CTG_STAT_ADD(gCtgMgmt.stat.runtime.qDoneNum); 
D
dapan1121 已提交
2177 2178

    ctgDbgShowClusterCache(pCtg);
D
dapan1121 已提交
2179 2180
  }

D
dapan 已提交
2181
  CTG_UNLOCK(CTG_READ, &gCtgMgmt.lock);
D
dapan1121 已提交
2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193

  qInfo("catalog update thread stopped");
  
  return NULL;
}


int32_t ctgStartUpdateThread() {
  pthread_attr_t thAttr;
  pthread_attr_init(&thAttr);
  pthread_attr_setdetachstate(&thAttr, PTHREAD_CREATE_JOINABLE);

D
dapan 已提交
2194
  if (pthread_create(&gCtgMgmt.updateThread, &thAttr, ctgUpdateThreadFunc, NULL) != 0) {
D
dapan1121 已提交
2195 2196
    terrno = TAOS_SYSTEM_ERROR(errno);
    CTG_ERR_RET(terrno);
D
dapan1121 已提交
2197 2198
  }
  
D
dapan1121 已提交
2199
  pthread_attr_destroy(&thAttr);
D
dapan1121 已提交
2200 2201 2202
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218
int32_t ctgGetTableDistVgInfo(SCatalog* pCtg, void *pRpc, const SEpSet* pMgmtEps, const SName* pTableName, SArray** pVgList) {
  STableMeta *tbMeta = NULL;
  int32_t code = 0;
  SVgroupInfo vgroupInfo = {0};
  SCtgDBCache* dbCache = NULL;
  SArray *vgList = NULL;
  SDBVgInfo *vgInfo = NULL;

  *pVgList = NULL;
  
  CTG_ERR_JRET(ctgGetTableMeta(pCtg, pRpc, pMgmtEps, pTableName, &tbMeta, CTG_FLAG_UNKNOWN_STB));

  char db[TSDB_DB_FNAME_LEN] = {0};
  tNameGetFullDbName(pTableName, db);

  SHashObj *vgHash = NULL;  
D
dapan1121 已提交
2219
  CTG_ERR_JRET(ctgGetDBVgInfo(pCtg, pRpc, pMgmtEps, db, &dbCache, &vgInfo));
D
dapan1121 已提交
2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278

  if (dbCache) {
    vgHash = dbCache->vgInfo->vgHash;
  } else {
    vgHash = vgInfo->vgHash;
  }

  if (tbMeta->tableType == TSDB_SUPER_TABLE) {
    CTG_ERR_JRET(ctgGenerateVgList(pCtg, vgHash, pVgList));
  } else {
    // USE HASH METHOD INSTEAD OF VGID IN TBMETA
    ctgError("invalid method to get none stb vgInfo, tbType:%d", tbMeta->tableType);
    CTG_ERR_JRET(TSDB_CODE_CTG_INVALID_INPUT);
    
#if 0  
    int32_t vgId = tbMeta->vgId;
    if (taosHashGetDup(vgHash, &vgId, sizeof(vgId), &vgroupInfo) != 0) {
      ctgWarn("table's vgId not found in vgroup list, vgId:%d, tbName:%s", vgId, tNameGetTableName(pTableName));
      CTG_ERR_JRET(TSDB_CODE_CTG_VG_META_MISMATCH);
    }

    vgList = taosArrayInit(1, sizeof(SVgroupInfo));
    if (NULL == vgList) {
      ctgError("taosArrayInit %d failed", (int32_t)sizeof(SVgroupInfo));
      CTG_ERR_JRET(TSDB_CODE_CTG_MEM_ERROR);    
    }

    if (NULL == taosArrayPush(vgList, &vgroupInfo)) {
      ctgError("taosArrayPush vgroupInfo to array failed, vgId:%d, tbName:%s", vgId, tNameGetTableName(pTableName));
      CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
    }

    *pVgList = vgList;
    vgList = NULL;
#endif    
  }

_return:

  if (dbCache) {
    ctgReleaseVgInfo(dbCache);
    ctgReleaseDBCache(pCtg, dbCache);
  }

  tfree(tbMeta);

  if (vgInfo) {
    taosHashCleanup(vgInfo->vgHash);
    tfree(vgInfo);
  }

  if (vgList) {
    taosArrayDestroy(vgList);
    vgList = NULL;
  }

  CTG_RET(code);
}

D
dapan1121 已提交
2279

D
dapan1121 已提交
2280
int32_t catalogInit(SCatalogCfg *cfg) {
D
dapan 已提交
2281
  if (gCtgMgmt.pCluster) {
D
dapan 已提交
2282
    qError("catalog already initialized");
D
dapan1121 已提交
2283
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
D
dapan1121 已提交
2284 2285
  }

D
dapan 已提交
2286
  atomic_store_8(&gCtgMgmt.exit, false);
D
dapan1121 已提交
2287

D
dapan1121 已提交
2288
  if (cfg) {
D
dapan 已提交
2289
    memcpy(&gCtgMgmt.cfg, cfg, sizeof(*cfg));
H
Haojun Liao 已提交
2290

D
dapan 已提交
2291 2292
    if (gCtgMgmt.cfg.maxDBCacheNum == 0) {
      gCtgMgmt.cfg.maxDBCacheNum = CTG_DEFAULT_CACHE_DB_NUMBER;
D
dapan1121 已提交
2293 2294
    }

D
dapan 已提交
2295 2296
    if (gCtgMgmt.cfg.maxTblCacheNum == 0) {
      gCtgMgmt.cfg.maxTblCacheNum = CTG_DEFAULT_CACHE_TBLMETA_NUMBER;
D
dapan1121 已提交
2297
    }
D
dapan1121 已提交
2298

D
dapan 已提交
2299 2300
    if (gCtgMgmt.cfg.dbRentSec == 0) {
      gCtgMgmt.cfg.dbRentSec = CTG_DEFAULT_RENT_SECOND;
D
dapan1121 已提交
2301 2302
    }

D
dapan 已提交
2303 2304
    if (gCtgMgmt.cfg.stbRentSec == 0) {
      gCtgMgmt.cfg.stbRentSec = CTG_DEFAULT_RENT_SECOND;
D
dapan1121 已提交
2305
    }
D
dapan1121 已提交
2306
  } else {
D
dapan 已提交
2307 2308 2309 2310
    gCtgMgmt.cfg.maxDBCacheNum = CTG_DEFAULT_CACHE_DB_NUMBER;
    gCtgMgmt.cfg.maxTblCacheNum = CTG_DEFAULT_CACHE_TBLMETA_NUMBER;
    gCtgMgmt.cfg.dbRentSec = CTG_DEFAULT_RENT_SECOND;
    gCtgMgmt.cfg.stbRentSec = CTG_DEFAULT_RENT_SECOND;
D
dapan 已提交
2311 2312
  }

D
dapan 已提交
2313 2314
  gCtgMgmt.pCluster = taosHashInit(CTG_DEFAULT_CACHE_CLUSTER_NUMBER, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
  if (NULL == gCtgMgmt.pCluster) {
D
dapan1121 已提交
2315 2316
    qError("taosHashInit %d cluster cache failed", CTG_DEFAULT_CACHE_CLUSTER_NUMBER);
    CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
D
dapan1121 已提交
2317 2318
  }

D
dapan1121 已提交
2319 2320
  CTG_ERR_RET(ctgStartUpdateThread());

D
dapan1121 已提交
2321 2322
  tsem_init(&gCtgMgmt.queue.reqSem, 0, 0);
  tsem_init(&gCtgMgmt.queue.rspSem, 0, 0);
D
dapan1121 已提交
2323

D
dapan1121 已提交
2324 2325
  gCtgMgmt.queue.head = calloc(1, sizeof(SCtgQNode));
  if (NULL == gCtgMgmt.queue.head) {
D
dapan1121 已提交
2326 2327 2328
    qError("calloc %d failed", (int32_t)sizeof(SCtgQNode));
    CTG_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }
D
dapan1121 已提交
2329
  gCtgMgmt.queue.tail = gCtgMgmt.queue.head;
D
dapan1121 已提交
2330

D
dapan 已提交
2331
  qDebug("catalog initialized, maxDb:%u, maxTbl:%u, dbRentSec:%u, stbRentSec:%u", gCtgMgmt.cfg.maxDBCacheNum, gCtgMgmt.cfg.maxTblCacheNum, gCtgMgmt.cfg.dbRentSec, gCtgMgmt.cfg.stbRentSec);
D
dapan1121 已提交
2332

D
dapan 已提交
2333
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
2334 2335
}

D
dapan1121 已提交
2336
int32_t catalogGetHandle(uint64_t clusterId, SCatalog** catalogHandle) {
2337
  if (NULL == catalogHandle) {
D
dapan1121 已提交
2338
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
D
dapan 已提交
2339 2340
  }

D
dapan 已提交
2341
  if (NULL == gCtgMgmt.pCluster) {
D
dapan 已提交
2342
    qError("catalog cluster cache are not ready, clusterId:%"PRIx64, clusterId);
D
dapan1121 已提交
2343
    CTG_ERR_RET(TSDB_CODE_CTG_NOT_READY);
D
dapan 已提交
2344 2345
  }

D
dapan1121 已提交
2346 2347
  int32_t code = 0;
  SCatalog *clusterCtg = NULL;
D
dapan 已提交
2348

D
dapan1121 已提交
2349
  while (true) {
D
dapan 已提交
2350
    SCatalog **ctg = (SCatalog **)taosHashGet(gCtgMgmt.pCluster, (char*)&clusterId, sizeof(clusterId));
D
dapan 已提交
2351

D
dapan1121 已提交
2352 2353 2354 2355 2356
    if (ctg && (*ctg)) {
      *catalogHandle = *ctg;
      qDebug("got catalog handle from cache, clusterId:%"PRIx64", CTG:%p", clusterId, *ctg);
      return TSDB_CODE_SUCCESS;
    }
D
dapan 已提交
2357

D
dapan1121 已提交
2358 2359 2360 2361 2362 2363
    clusterCtg = calloc(1, sizeof(SCatalog));
    if (NULL == clusterCtg) {
      qError("calloc %d failed", (int32_t)sizeof(SCatalog));
      CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
    }

D
dapan1121 已提交
2364 2365
    clusterCtg->clusterId = clusterId;

D
dapan 已提交
2366 2367
    CTG_ERR_JRET(ctgMetaRentInit(&clusterCtg->dbRent, gCtgMgmt.cfg.dbRentSec, CTG_RENT_DB));
    CTG_ERR_JRET(ctgMetaRentInit(&clusterCtg->stbRent, gCtgMgmt.cfg.stbRentSec, CTG_RENT_STABLE));
D
dapan1121 已提交
2368

D
dapan 已提交
2369
    clusterCtg->dbCache = taosHashInit(gCtgMgmt.cfg.maxDBCacheNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
D
dapan1121 已提交
2370 2371 2372 2373 2374
    if (NULL == clusterCtg->dbCache) {
      qError("taosHashInit %d dbCache failed", CTG_DEFAULT_CACHE_DB_NUMBER);
      CTG_ERR_JRET(TSDB_CODE_CTG_MEM_ERROR);
    }

D
dapan 已提交
2375
    SHashObj *metaCache = taosHashInit(gCtgMgmt.cfg.maxTblCacheNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
D
dapan1121 已提交
2376
    if (NULL == metaCache) {
D
dapan 已提交
2377
      qError("taosHashInit failed, num:%d", gCtgMgmt.cfg.maxTblCacheNum);
D
dapan1121 已提交
2378 2379 2380
      CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
    }
    
D
dapan 已提交
2381
    code = taosHashPut(gCtgMgmt.pCluster, &clusterId, sizeof(clusterId), &clusterCtg, POINTER_BYTES);
D
dapan1121 已提交
2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394
    if (code) {
      if (HASH_NODE_EXIST(code)) {
        ctgFreeHandle(clusterCtg);
        continue;
      }
      
      qError("taosHashPut CTG to cache failed, clusterId:%"PRIx64, clusterId);
      CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
    }

    qDebug("add CTG to cache, clusterId:%"PRIx64", CTG:%p", clusterId, clusterCtg);

    break;
D
dapan 已提交
2395
  }
D
dapan1121 已提交
2396 2397

  *catalogHandle = clusterCtg;
D
dapan 已提交
2398
  
D
dapan1121 已提交
2399
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
2400 2401 2402 2403 2404 2405 2406 2407

_return:

  ctgFreeHandle(clusterCtg);
  
  CTG_RET(code);
}

D
dapan1121 已提交
2408 2409
void catalogFreeHandle(SCatalog* pCtg) {
  if (NULL == pCtg) {
D
dapan1121 已提交
2410 2411
    return;
  }
D
dapan1121 已提交
2412

D
dapan 已提交
2413
  if (taosHashRemove(gCtgMgmt.pCluster, &pCtg->clusterId, sizeof(pCtg->clusterId))) {
D
dapan1121 已提交
2414
    ctgWarn("taosHashRemove from cluster failed, may already be freed, clusterId:%"PRIx64, pCtg->clusterId);
D
dapan1121 已提交
2415 2416 2417
    return;
  }

D
dapan1121 已提交
2418
  uint64_t clusterId = pCtg->clusterId;
D
dapan1121 已提交
2419
  
D
dapan1121 已提交
2420
  ctgFreeHandle(pCtg);
D
dapan1121 已提交
2421 2422

  ctgInfo("handle freed, culsterId:%"PRIx64, clusterId);
D
dapan 已提交
2423 2424
}

D
dapan1121 已提交
2425
int32_t catalogGetDBVgVersion(SCatalog* pCtg, const char* dbFName, int32_t* version, int64_t* dbId, int32_t *tableNum) {
D
dapan1121 已提交
2426 2427
  CTG_API_ENTER();

D
dapan1121 已提交
2428
  if (NULL == pCtg || NULL == dbFName || NULL == version || NULL == dbId) {
D
dapan1121 已提交
2429 2430 2431 2432
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

  if (NULL == pCtg->dbCache) {
D
dapan1121 已提交
2433
    *version = CTG_DEFAULT_INVALID_VERSION;
D
dapan1121 已提交
2434
    ctgInfo("empty db cache, dbFName:%s", dbFName);
D
dapan1121 已提交
2435
    CTG_API_LEAVE(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
2436 2437
  }

D
dapan1121 已提交
2438 2439 2440
  SCtgDBCache *dbCache = NULL;
  ctgAcquireDBCache(pCtg, dbFName, &dbCache);
  if (NULL == dbCache) {
D
dapan1121 已提交
2441
    *version = CTG_DEFAULT_INVALID_VERSION;
D
dapan1121 已提交
2442
    CTG_API_LEAVE(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
2443 2444
  }

D
dapan 已提交
2445 2446 2447
  bool inCache = false;
  ctgAcquireVgInfo(pCtg, dbCache, &inCache);
  if (!inCache) {
D
dapan1121 已提交
2448
    ctgReleaseDBCache(pCtg, dbCache);
D
dapan1121 已提交
2449 2450

    *version = CTG_DEFAULT_INVALID_VERSION;
D
dapan1121 已提交
2451
    CTG_API_LEAVE(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
2452 2453
  }

D
dapan1121 已提交
2454
  *version = dbCache->vgInfo->vgVersion;
D
dapan1121 已提交
2455
  *dbId = dbCache->dbId;
D
dapan1121 已提交
2456
  *tableNum = dbCache->vgInfo->numOfTable;
D
dapan1121 已提交
2457 2458 2459

  ctgReleaseVgInfo(dbCache);
  ctgReleaseDBCache(pCtg, dbCache);
D
dapan1121 已提交
2460

D
dapan1121 已提交
2461
  ctgDebug("Got db vgVersion from cache, dbFName:%s, vgVersion:%d", dbFName, *version);
D
dapan1121 已提交
2462

D
dapan1121 已提交
2463
  CTG_API_LEAVE(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
2464 2465
}

D
dapan1121 已提交
2466
int32_t catalogGetDBVgInfo(SCatalog* pCtg, void *pRpc, const SEpSet* pMgmtEps, const char* dbFName, SArray** vgroupList) {
D
dapan1121 已提交
2467 2468
  CTG_API_ENTER();

D
dapan1121 已提交
2469 2470 2471
  if (NULL == pCtg || NULL == dbFName || NULL == pRpc || NULL == pMgmtEps || NULL == vgroupList) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }
2472

D
dapan1121 已提交
2473
  SCtgDBCache* dbCache = NULL;
2474
  int32_t code = 0;
D
dapan1121 已提交
2475
  SArray *vgList = NULL;
D
dapan1121 已提交
2476 2477
  SHashObj *vgHash = NULL;
  SDBVgInfo *vgInfo = NULL;
D
dapan1121 已提交
2478
  CTG_ERR_JRET(ctgGetDBVgInfo(pCtg, pRpc, pMgmtEps, dbFName, &dbCache, &vgInfo));
D
dapan1121 已提交
2479 2480 2481 2482
  if (dbCache) {
    vgHash = dbCache->vgInfo->vgHash;
  } else {
    vgHash = vgInfo->vgHash;
D
dapan1121 已提交
2483 2484
  }

D
dapan1121 已提交
2485
  CTG_ERR_JRET(ctgGenerateVgList(pCtg, vgHash, &vgList));
D
dapan1121 已提交
2486 2487 2488 2489 2490

  *vgroupList = vgList;
  vgList = NULL;

_return:
D
dapan1121 已提交
2491 2492

  if (dbCache) {
D
dapan1121 已提交
2493 2494
    ctgReleaseVgInfo(dbCache);
    ctgReleaseDBCache(pCtg, dbCache);
D
dapan1121 已提交
2495 2496
  }

D
dapan1121 已提交
2497 2498 2499
  if (vgInfo) {
    taosHashCleanup(vgInfo->vgHash);
    tfree(vgInfo);
D
dapan1121 已提交
2500 2501
  }

D
dapan1121 已提交
2502
  CTG_API_LEAVE(code);  
D
dapan1121 已提交
2503 2504 2505
}


D
dapan1121 已提交
2506
int32_t catalogUpdateDBVgInfo(SCatalog* pCtg, const char* dbFName, uint64_t dbId, SDBVgInfo* dbInfo) {
D
dapan1121 已提交
2507
  CTG_API_ENTER();
D
dapan1121 已提交
2508 2509

  int32_t code = 0;
D
dapan1121 已提交
2510
  
D
dapan1121 已提交
2511
  if (NULL == pCtg || NULL == dbFName || NULL == dbInfo) {
D
dapan1121 已提交
2512
    ctgFreeVgInfo(dbInfo);
D
dapan1121 已提交
2513 2514 2515
    CTG_ERR_JRET(TSDB_CODE_CTG_INVALID_INPUT);
  }

D
dapan1121 已提交
2516
  code = ctgPushUpdateVgMsgInQueue(pCtg, dbFName, dbId, dbInfo, false);
D
dapan1121 已提交
2517

D
dapan1121 已提交
2518 2519
_return:

D
dapan1121 已提交
2520
  CTG_API_LEAVE(code);
D
dapan1121 已提交
2521 2522 2523
}


D
dapan1121 已提交
2524 2525 2526
int32_t catalogRemoveDB(SCatalog* pCtg, const char* dbFName, uint64_t dbId) {
  CTG_API_ENTER();

D
dapan1121 已提交
2527 2528
  int32_t code = 0;
  
D
dapan1121 已提交
2529 2530
  if (NULL == pCtg || NULL == dbFName) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
D
dapan1121 已提交
2531 2532
  }

D
dapan1121 已提交
2533
  if (NULL == pCtg->dbCache) {
D
dapan1121 已提交
2534
    CTG_API_LEAVE(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
2535
  }
D
dapan1121 已提交
2536

D
dapan1121 已提交
2537
  CTG_ERR_JRET(ctgPushRmDBMsgInQueue(pCtg, dbFName, dbId));
D
dapan 已提交
2538

D
dapan1121 已提交
2539
  CTG_API_LEAVE(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
2540
  
D
dapan1121 已提交
2541 2542
_return:

D
dapan1121 已提交
2543
  CTG_API_LEAVE(code);
D
dapan1121 已提交
2544 2545
}

D
dapan1121 已提交
2546 2547 2548
int32_t catalogRemoveStbMeta(SCatalog* pCtg, const char* dbFName, uint64_t dbId, const char* stbName, uint64_t suid) {
  CTG_API_ENTER();

D
dapan 已提交
2549 2550
  int32_t code = 0;
  
D
dapan1121 已提交
2551 2552
  if (NULL == pCtg || NULL == dbFName || NULL == stbName) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
D
dapan 已提交
2553 2554
  }

D
dapan1121 已提交
2555
  if (NULL == pCtg->dbCache) {
D
dapan1121 已提交
2556
    CTG_API_LEAVE(TSDB_CODE_SUCCESS);
D
dapan 已提交
2557
  }
D
dapan1121 已提交
2558

D
dapan1121 已提交
2559
  CTG_ERR_JRET(ctgPushRmStbMsgInQueue(pCtg, dbFName, dbId, stbName, suid));
D
dapan 已提交
2560

D
dapan1121 已提交
2561
  CTG_API_LEAVE(TSDB_CODE_SUCCESS);
D
dapan 已提交
2562
  
D
dapan1121 已提交
2563 2564
_return:

D
dapan1121 已提交
2565
  CTG_API_LEAVE(code);
D
dapan 已提交
2566 2567
}

D
dapan1121 已提交
2568

D
dapan1121 已提交
2569
int32_t catalogGetTableMeta(SCatalog* pCtg, void *pTrans, const SEpSet* pMgmtEps, const SName* pTableName, STableMeta** pTableMeta) {
D
dapan1121 已提交
2570 2571
  CTG_API_ENTER();

D
dapan1121 已提交
2572
  CTG_API_LEAVE(ctgGetTableMeta(pCtg, pTrans, pMgmtEps, pTableName, pTableMeta, CTG_FLAG_UNKNOWN_STB));
D
dapan1121 已提交
2573
}
D
dapan1121 已提交
2574

D
dapan1121 已提交
2575
int32_t catalogGetSTableMeta(SCatalog* pCtg, void * pTrans, const SEpSet* pMgmtEps, const SName* pTableName, STableMeta** pTableMeta) {
D
dapan1121 已提交
2576 2577
  CTG_API_ENTER();

D
dapan1121 已提交
2578
  CTG_API_LEAVE(ctgGetTableMeta(pCtg, pTrans, pMgmtEps, pTableName, pTableMeta, CTG_FLAG_STB));
D
dapan1121 已提交
2579 2580
}

D
dapan1121 已提交
2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593
int32_t catalogUpdateSTableMeta(SCatalog* pCtg, STableMetaRsp *rspMsg) {
  CTG_API_ENTER();

  if (NULL == pCtg || NULL == rspMsg) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

  STableMetaOutput *output = calloc(1, sizeof(STableMetaOutput));
  if (NULL == output) {
    ctgError("malloc %d failed", (int32_t)sizeof(STableMetaOutput));
    CTG_API_LEAVE(TSDB_CODE_CTG_MEM_ERROR);
  }
  
D
dapan1121 已提交
2594 2595
  int32_t code = 0;

D
dapan1121 已提交
2596 2597
  strcpy(output->dbFName, rspMsg->dbFName);
  strcpy(output->tbName, rspMsg->tbName);
D
dapan1121 已提交
2598

D
dapan1121 已提交
2599
  output->dbId = rspMsg->dbId;
D
dapan1121 已提交
2600
  
D
dapan1121 已提交
2601
  SET_META_TYPE_TABLE(output->metaType);
D
dapan1121 已提交
2602
  
D
dapan1121 已提交
2603
  CTG_ERR_JRET(queryCreateTableMetaFromMsg(rspMsg, true, &output->tbMeta));
D
dapan1121 已提交
2604

D
dapan1121 已提交
2605
  CTG_ERR_JRET(ctgPushUpdateTblMsgInQueue(pCtg, output, false));
D
dapan 已提交
2606

D
dapan1121 已提交
2607 2608
  CTG_API_LEAVE(code);
  
D
dapan1121 已提交
2609 2610
_return:

D
dapan1121 已提交
2611 2612
  tfree(output->tbMeta);
  tfree(output);
D
dapan1121 已提交
2613
  
D
dapan1121 已提交
2614
  CTG_API_LEAVE(code);
D
dapan1121 已提交
2615 2616
}

D
dapan1121 已提交
2617 2618 2619 2620 2621 2622 2623 2624 2625
int32_t catalogRefreshDBVgInfo(SCatalog* pCtg, void *pTrans, const SEpSet* pMgmtEps, const char* dbFName) {
  CTG_API_ENTER();

  if (NULL == pCtg || NULL == pTrans || NULL == pMgmtEps || NULL == dbFName) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

  CTG_API_LEAVE(ctgRefreshDBVgInfo(pCtg, pTrans, pMgmtEps, dbFName));
}
D
dapan1121 已提交
2626

D
dapan1121 已提交
2627
int32_t catalogRefreshTableMeta(SCatalog* pCtg, void *pTrans, const SEpSet* pMgmtEps, const SName* pTableName, int32_t isSTable) {
D
dapan1121 已提交
2628 2629
  CTG_API_ENTER();

D
dapan1121 已提交
2630
  if (NULL == pCtg || NULL == pTrans || NULL == pMgmtEps || NULL == pTableName) {
D
dapan1121 已提交
2631 2632 2633
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

D
dapan1121 已提交
2634
  CTG_API_LEAVE(ctgRefreshTblMeta(pCtg, pTrans, pMgmtEps, pTableName, CTG_FLAG_FORCE_UPDATE | CTG_FLAG_MAKE_STB(isSTable), NULL, false));
2635
}
2636

D
dapan1121 已提交
2637
int32_t catalogRefreshGetTableMeta(SCatalog* pCtg, void *pTrans, const SEpSet* pMgmtEps, const SName* pTableName, STableMeta** pTableMeta, int32_t isSTable) {
D
dapan1121 已提交
2638 2639
  CTG_API_ENTER();

D
dapan1121 已提交
2640
  CTG_API_LEAVE(ctgGetTableMeta(pCtg, pTrans, pMgmtEps, pTableName, pTableMeta, CTG_FLAG_FORCE_UPDATE | CTG_FLAG_MAKE_STB(isSTable)));
D
dapan1121 已提交
2641 2642
}

D
dapan1121 已提交
2643
int32_t catalogGetTableDistVgInfo(SCatalog* pCtg, void *pRpc, const SEpSet* pMgmtEps, const SName* pTableName, SArray** pVgList) {
D
dapan1121 已提交
2644
  CTG_API_ENTER();
D
dapan1121 已提交
2645 2646 2647 2648

  if (NULL == pCtg || NULL == pRpc || NULL == pMgmtEps || NULL == pTableName || NULL == pVgList) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }
D
dapan1121 已提交
2649 2650 2651 2652 2653

  if (CTG_IS_INF_DBNAME(pTableName->dbname)) {
    ctgError("no valid vgInfo for db, dbname:%s", pTableName->dbname);
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }
D
dapan1121 已提交
2654

D
dapan1121 已提交
2655
  int32_t code = 0;
D
dapan1121 已提交
2656

D
dapan1121 已提交
2657 2658 2659 2660 2661
  while (true) {
    code = ctgGetTableDistVgInfo(pCtg, pRpc, pMgmtEps, pTableName, pVgList);
    if (code) {
      if (TSDB_CODE_CTG_VG_META_MISMATCH == code) {
        CTG_ERR_JRET(ctgRefreshTblMeta(pCtg, pRpc, pMgmtEps, pTableName, CTG_FLAG_FORCE_UPDATE | CTG_FLAG_MAKE_STB(CTG_FLAG_UNKNOWN_STB), NULL, true));
D
dapan 已提交
2662

D
dapan1121 已提交
2663 2664 2665 2666 2667 2668
        char dbFName[TSDB_DB_FNAME_LEN] = {0};
        tNameGetFullDbName(pTableName, dbFName);        
        CTG_ERR_JRET(ctgRefreshDBVgInfo(pCtg, pRpc, pMgmtEps, dbFName));
        
        continue;
      }
D
dapan1121 已提交
2669
    }
D
dapan 已提交
2670

D
dapan1121 已提交
2671
    break;
D
dapan1121 已提交
2672
  }
D
dapan 已提交
2673

D
dapan1121 已提交
2674
_return:
D
dapan 已提交
2675

D
dapan1121 已提交
2676
  CTG_API_LEAVE(code);
D
dapan1121 已提交
2677 2678 2679
}


D
dapan1121 已提交
2680
int32_t catalogGetTableHashVgroup(SCatalog *pCtg, void *pTrans, const SEpSet *pMgmtEps, const SName *pTableName, SVgroupInfo *pVgroup) {
D
dapan1121 已提交
2681 2682
  CTG_API_ENTER();

D
dapan1121 已提交
2683 2684 2685 2686 2687
  if (CTG_IS_INF_DBNAME(pTableName->dbname)) {
    ctgError("no valid vgInfo for db, dbname:%s", pTableName->dbname);
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

D
dapan1121 已提交
2688 2689
  SCtgDBCache* dbCache = NULL;
  int32_t code = 0;
H
Haojun Liao 已提交
2690 2691
  char db[TSDB_DB_FNAME_LEN] = {0};
  tNameGetFullDbName(pTableName, db);
D
dapan1121 已提交
2692

D
dapan1121 已提交
2693
  SDBVgInfo *vgInfo = NULL;
D
dapan1121 已提交
2694
  CTG_ERR_JRET(ctgGetDBVgInfo(pCtg, pTrans, pMgmtEps, db, &dbCache, &vgInfo));
D
dapan1121 已提交
2695

D
dapan1121 已提交
2696
  CTG_ERR_JRET(ctgGetVgInfoFromHashValue(pCtg, vgInfo ? vgInfo : dbCache->vgInfo, pTableName, pVgroup));
D
dapan1121 已提交
2697

D
dapan1121 已提交
2698
_return:
D
dapan1121 已提交
2699

D
dapan1121 已提交
2700
  if (dbCache) {
D
dapan1121 已提交
2701 2702 2703 2704 2705 2706 2707
    ctgReleaseVgInfo(dbCache);
    ctgReleaseDBCache(pCtg, dbCache);
  }

  if (vgInfo) {
    taosHashCleanup(vgInfo->vgHash);
    tfree(vgInfo);
D
dapan1121 已提交
2708
  }
D
dapan1121 已提交
2709

D
dapan1121 已提交
2710
  CTG_API_LEAVE(code);
D
dapan1121 已提交
2711 2712 2713
}


D
dapan1121 已提交
2714
int32_t catalogGetAllMeta(SCatalog* pCtg, void *pTrans, const SEpSet* pMgmtEps, const SCatalogReq* pReq, SMetaData* pRsp) {
D
dapan1121 已提交
2715 2716
  CTG_API_ENTER();

D
dapan1121 已提交
2717
  if (NULL == pCtg || NULL == pTrans || NULL == pMgmtEps || NULL == pReq || NULL == pRsp) {
D
dapan1121 已提交
2718 2719 2720
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

D
dapan1121 已提交
2721
  int32_t code = 0;
D
dapan1121 已提交
2722
  pRsp->pTableMeta = NULL;
D
dapan1121 已提交
2723 2724 2725

  if (pReq->pTableName) {
    int32_t tbNum = (int32_t)taosArrayGetSize(pReq->pTableName);
D
dapan1121 已提交
2726
    if (tbNum <= 0) {
D
dapan1121 已提交
2727
      ctgError("empty table name list, tbNum:%d", tbNum);
D
dapan1121 已提交
2728
      CTG_ERR_JRET(TSDB_CODE_CTG_INVALID_INPUT);
D
dapan1121 已提交
2729
    }
H
Haojun Liao 已提交
2730

D
dapan1121 已提交
2731 2732
    pRsp->pTableMeta = taosArrayInit(tbNum, POINTER_BYTES);
    if (NULL == pRsp->pTableMeta) {
D
dapan1121 已提交
2733
      ctgError("taosArrayInit %d failed", tbNum);
D
dapan1121 已提交
2734
      CTG_ERR_JRET(TSDB_CODE_CTG_MEM_ERROR);
D
dapan1121 已提交
2735 2736 2737 2738 2739 2740
    }
    
    for (int32_t i = 0; i < tbNum; ++i) {
      SName *name = taosArrayGet(pReq->pTableName, i);
      STableMeta *pTableMeta = NULL;
      
D
dapan1121 已提交
2741
      CTG_ERR_JRET(ctgGetTableMeta(pCtg, pTrans, pMgmtEps, name, &pTableMeta, CTG_FLAG_UNKNOWN_STB));
D
dapan1121 已提交
2742 2743 2744 2745 2746 2747 2748 2749 2750

      if (NULL == taosArrayPush(pRsp->pTableMeta, &pTableMeta)) {
        ctgError("taosArrayPush failed, idx:%d", i);
        tfree(pTableMeta);
        CTG_ERR_JRET(TSDB_CODE_CTG_MEM_ERROR);
      }
    }
  }

D
dapan1121 已提交
2751 2752 2753 2754
  if (pReq->qNodeRequired) {
    CTG_ERR_JRET(ctgGetQnodeListFromMnode(pCtg, pTrans, pMgmtEps, &pRsp->pEpSetList));
  }

D
dapan1121 已提交
2755
  CTG_API_LEAVE(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
2756 2757

_return:  
D
dapan1121 已提交
2758

D
dapan1121 已提交
2759 2760 2761 2762 2763 2764 2765 2766
  if (pRsp->pTableMeta) {
    int32_t aSize = taosArrayGetSize(pRsp->pTableMeta);
    for (int32_t i = 0; i < aSize; ++i) {
      STableMeta *pMeta = taosArrayGetP(pRsp->pTableMeta, i);
      tfree(pMeta);
    }
    
    taosArrayDestroy(pRsp->pTableMeta);
D
dapan1121 已提交
2767
    pRsp->pTableMeta = NULL;
D
dapan1121 已提交
2768
  }
D
dapan 已提交
2769
  
D
dapan1121 已提交
2770
  CTG_API_LEAVE(code);
2771
}
D
dapan 已提交
2772

D
dapan1121 已提交
2773
int32_t catalogGetQnodeList(SCatalog* pCtg, void *pRpc, const SEpSet* pMgmtEps, SArray* pQnodeList) {
D
dapan1121 已提交
2774 2775
  CTG_API_ENTER();

D
dapan1121 已提交
2776 2777 2778 2779
  if (NULL == pCtg || NULL == pRpc  || NULL == pMgmtEps || NULL == pQnodeList) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

D
dapan1121 已提交
2780
  //TODO
D
dapan 已提交
2781

D
dapan1121 已提交
2782
  CTG_API_LEAVE(TSDB_CODE_SUCCESS);
D
dapan 已提交
2783 2784
}

D
dapan1121 已提交
2785
int32_t catalogGetExpiredSTables(SCatalog* pCtg, SSTableMetaVersion **stables, uint32_t *num) {
D
dapan1121 已提交
2786 2787
  CTG_API_ENTER();

D
dapan1121 已提交
2788 2789
  if (NULL == pCtg || NULL == stables || NULL == num) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
D
dapan1121 已提交
2790 2791
  }

D
dapan1121 已提交
2792 2793 2794 2795
  CTG_API_LEAVE(ctgMetaRentGet(&pCtg->stbRent, (void **)stables, num, sizeof(SSTableMetaVersion)));
}

int32_t catalogGetExpiredDBs(SCatalog* pCtg, SDbVgVersion **dbs, uint32_t *num) {
D
dapan1121 已提交
2796
  CTG_API_ENTER();
D
dapan1121 已提交
2797 2798 2799 2800
  
  if (NULL == pCtg || NULL == dbs || NULL == num) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }
D
dapan1121 已提交
2801

D
dapan1121 已提交
2802
  CTG_API_LEAVE(ctgMetaRentGet(&pCtg->dbRent, (void **)dbs, num, sizeof(SDbVgVersion)));
D
dapan1121 已提交
2803 2804
}

D
dapan 已提交
2805

D
dapan 已提交
2806
void catalogDestroy(void) {
D
dapan1121 已提交
2807 2808
  qInfo("start to destroy catalog");
  
D
dapan 已提交
2809
  if (NULL == gCtgMgmt.pCluster || atomic_load_8(&gCtgMgmt.exit)) {
D
dapan1121 已提交
2810 2811 2812
    return;
  }

D
dapan 已提交
2813 2814
  atomic_store_8(&gCtgMgmt.exit, true);

D
dapan1121 已提交
2815 2816
  tsem_post(&gCtgMgmt.queue.reqSem);
  tsem_post(&gCtgMgmt.queue.rspSem);
D
dapan1121 已提交
2817

D
dapan1121 已提交
2818
  while (CTG_IS_LOCKED(&gCtgMgmt.lock)) {
wafwerar's avatar
wafwerar 已提交
2819
    taosUsleep(1);
D
dapan1121 已提交
2820 2821
  }
  
D
dapan 已提交
2822
  CTG_LOCK(CTG_WRITE, &gCtgMgmt.lock);
D
dapan1121 已提交
2823

D
dapan1121 已提交
2824
  SCatalog *pCtg = NULL;
D
dapan 已提交
2825
  void *pIter = taosHashIterate(gCtgMgmt.pCluster, NULL);
D
dapan1121 已提交
2826
  while (pIter) {
D
dapan1121 已提交
2827
    pCtg = *(SCatalog **)pIter;
D
dapan1121 已提交
2828

D
dapan1121 已提交
2829 2830
    if (pCtg) {
      catalogFreeHandle(pCtg);
D
dapan1121 已提交
2831 2832
    }
    
D
dapan 已提交
2833
    pIter = taosHashIterate(gCtgMgmt.pCluster, pIter);
D
dapan 已提交
2834
  }
D
dapan1121 已提交
2835
  
D
dapan 已提交
2836 2837
  taosHashCleanup(gCtgMgmt.pCluster);
  gCtgMgmt.pCluster = NULL;
D
dapan1121 已提交
2838

D
dapan 已提交
2839
  CTG_UNLOCK(CTG_WRITE, &gCtgMgmt.lock);
D
dapan1121 已提交
2840

D
dapan1121 已提交
2841
  qInfo("catalog destroyed");
D
dapan 已提交
2842 2843 2844 2845
}