catalog.c 79.5 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
dapan1121 已提交
27
extern SCtgDebug gCTGDebug;
D
dapan 已提交
28 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
SCatalogMgmt gCtgMgmt = {0};
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
                          }
};

D
dapan1121 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68
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;
    }
  }

wafwerar's avatar
wafwerar 已提交
69
  taosMemoryFreeClear(mgmt->slots);
D
dapan1121 已提交
70 71 72 73 74 75
}


void ctgFreeTableMetaCache(SCtgTbMetaCache *cache) {
  CTG_LOCK(CTG_WRITE, &cache->stbLock);
  if (cache->stbCache) {
D
dapan1121 已提交
76
    int32_t stblNum = taosHashGetSize(cache->stbCache);  
D
dapan1121 已提交
77 78
    taosHashCleanup(cache->stbCache);
    cache->stbCache = NULL;
D
dapan1121 已提交
79
    CTG_CACHE_STAT_SUB(stblNum, stblNum);
D
dapan1121 已提交
80 81 82 83 84
  }
  CTG_UNLOCK(CTG_WRITE, &cache->stbLock);

  CTG_LOCK(CTG_WRITE, &cache->metaLock);
  if (cache->metaCache) {
D
dapan1121 已提交
85
    int32_t tblNum = taosHashGetSize(cache->metaCache);
D
dapan1121 已提交
86 87
    taosHashCleanup(cache->metaCache);
    cache->metaCache = NULL;
D
dapan1121 已提交
88
    CTG_CACHE_STAT_SUB(tblNum, tblNum);
D
dapan1121 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102
  }
  CTG_UNLOCK(CTG_WRITE, &cache->metaLock);
}

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

  if (vgInfo->vgHash) {
    taosHashCleanup(vgInfo->vgHash);
    vgInfo->vgHash = NULL;
  }
  
wafwerar's avatar
wafwerar 已提交
103
  taosMemoryFreeClear(vgInfo);
D
dapan1121 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
}

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) {
D
dapan1121 已提交
124 125
    int32_t dbNum = taosHashGetSize(pCtg->dbCache);
    
D
dapan1121 已提交
126 127 128 129 130 131 132 133 134 135 136 137
    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);
D
dapan1121 已提交
138 139
    
    CTG_CACHE_STAT_SUB(dbNum, dbNum);
D
dapan1121 已提交
140 141
  }
  
wafwerar's avatar
wafwerar 已提交
142
  taosMemoryFree(pCtg);
D
dapan1121 已提交
143 144 145 146 147 148 149 150
}



void ctgWaitAction(SCtgMetaAction *action) {
  while (true) {
    tsem_wait(&gCtgMgmt.queue.rspSem);
    
X
Xiaoyu Wang 已提交
151
    if (atomic_load_8((int8_t*)&gCtgMgmt.exit)) {
D
dapan1121 已提交
152 153 154 155 156 157 158 159 160 161 162 163
      tsem_post(&gCtgMgmt.queue.rspSem);
      break;
    }

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

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

void ctgPopAction(SCtgMetaAction **action) {
D
dapan1121 已提交
166
  SCtgQNode *orig = gCtgMgmt.queue.head;
D
dapan 已提交
167
  
D
dapan1121 已提交
168 169
  SCtgQNode *node = gCtgMgmt.queue.head->next;
  gCtgMgmt.queue.head = gCtgMgmt.queue.head->next;
D
dapan 已提交
170 171 172

  CTG_QUEUE_SUB();
  
wafwerar's avatar
wafwerar 已提交
173
  taosMemoryFreeClear(orig);
D
dapan 已提交
174 175 176 177 178

  *action = &node->action;
}


D
dapan1121 已提交
179
int32_t ctgPushAction(SCatalog* pCtg, SCtgMetaAction *action) {
wafwerar's avatar
wafwerar 已提交
180
  SCtgQNode *node = taosMemoryCalloc(1, sizeof(SCtgQNode));
D
dapan 已提交
181 182 183 184
  if (NULL == node) {
    qError("calloc %d failed", (int32_t)sizeof(SCtgQNode));
    CTG_RET(TSDB_CODE_CTG_MEM_ERROR);
  }
D
dapan1121 已提交
185 186

  action->seqId = atomic_add_fetch_64(&gCtgMgmt.queue.seqId, 1);
D
dapan 已提交
187 188 189
  
  node->action = *action;

D
dapan1121 已提交
190 191 192 193
  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 已提交
194 195

  CTG_QUEUE_ADD();
D
dapan1121 已提交
196
  CTG_RUNTIME_STAT_ADD(qNum, 1);
D
dapan 已提交
197

D
dapan1121 已提交
198
  tsem_post(&gCtgMgmt.queue.reqSem);
D
dapan 已提交
199

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

D
dapan1121 已提交
202 203
  if (action->syncReq) {
    ctgWaitAction(action);
D
dapan1121 已提交
204 205
  }

D
dapan1121 已提交
206
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
207 208 209
}


D
dapan1121 已提交
210 211 212
int32_t ctgPushRmDBMsgInQueue(SCatalog* pCtg, const char *dbFName, int64_t dbId) {
  int32_t code = 0;
  SCtgMetaAction action= {.act = CTG_ACT_REMOVE_DB};
wafwerar's avatar
wafwerar 已提交
213
  SCtgRemoveDBMsg *msg = taosMemoryMalloc(sizeof(SCtgRemoveDBMsg));
D
dapan1121 已提交
214 215 216 217 218
  if (NULL == msg) {
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgRemoveDBMsg));
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
  }

D
dapan1121 已提交
219 220 221 222 223
  char *p = strchr(dbFName, '.');
  if (p && CTG_IS_INF_DBNAME(p + 1)) {
    dbFName = p + 1;
  }

D
dapan1121 已提交
224 225 226 227 228 229
  msg->pCtg = pCtg;
  strncpy(msg->dbFName, dbFName, sizeof(msg->dbFName));
  msg->dbId = dbId;

  action.data = msg;

D
dapan1121 已提交
230
  CTG_ERR_JRET(ctgPushAction(pCtg, &action));
D
dapan1121 已提交
231 232 233 234

  return TSDB_CODE_SUCCESS;

_return:
H
Haojun Liao 已提交
235

wafwerar's avatar
wafwerar 已提交
236
  taosMemoryFreeClear(action.data);
H
Haojun Liao 已提交
237
  CTG_RET(code);
D
dapan1121 已提交
238 239 240
}


D
dapan1121 已提交
241
int32_t ctgPushRmStbMsgInQueue(SCatalog* pCtg, const char *dbFName, int64_t dbId, const char *stbName, uint64_t suid, bool syncReq) {
D
dapan1121 已提交
242
  int32_t code = 0;
D
dapan1121 已提交
243
  SCtgMetaAction action= {.act = CTG_ACT_REMOVE_STB, .syncReq = syncReq};
wafwerar's avatar
wafwerar 已提交
244
  SCtgRemoveStbMsg *msg = taosMemoryMalloc(sizeof(SCtgRemoveStbMsg));
D
dapan1121 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257
  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 已提交
258
  CTG_ERR_JRET(ctgPushAction(pCtg, &action));
D
dapan1121 已提交
259 260 261 262

  return TSDB_CODE_SUCCESS;

_return:
H
Haojun Liao 已提交
263

wafwerar's avatar
wafwerar 已提交
264
  taosMemoryFreeClear(action.data);
H
Haojun Liao 已提交
265
  CTG_RET(code);
D
dapan1121 已提交
266 267 268 269
}



D
dapan1121 已提交
270
int32_t ctgPushRmTblMsgInQueue(SCatalog* pCtg, const char *dbFName, int64_t dbId, const char *tbName, bool syncReq) {
D
dapan1121 已提交
271
  int32_t code = 0;
D
dapan1121 已提交
272
  SCtgMetaAction action= {.act = CTG_ACT_REMOVE_TBL, .syncReq = syncReq};
wafwerar's avatar
wafwerar 已提交
273
  SCtgRemoveTblMsg *msg = taosMemoryMalloc(sizeof(SCtgRemoveTblMsg));
D
dapan1121 已提交
274 275 276 277 278 279 280 281 282 283 284 285
  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 已提交
286
  CTG_ERR_JRET(ctgPushAction(pCtg, &action));
D
dapan1121 已提交
287 288 289 290

  return TSDB_CODE_SUCCESS;

_return:
H
Haojun Liao 已提交
291

wafwerar's avatar
wafwerar 已提交
292
  taosMemoryFreeClear(action.data);
H
Haojun Liao 已提交
293
  CTG_RET(code);
D
dapan1121 已提交
294 295
}

D
dapan1121 已提交
296 297 298
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};
wafwerar's avatar
wafwerar 已提交
299
  SCtgUpdateVgMsg *msg = taosMemoryMalloc(sizeof(SCtgUpdateVgMsg));
D
dapan1121 已提交
300 301 302 303
  if (NULL == msg) {
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgUpdateVgMsg));
    ctgFreeVgInfo(dbInfo);
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
D
dapan1121 已提交
304 305
  }

D
dapan1121 已提交
306 307 308 309 310
  char *p = strchr(dbFName, '.');
  if (p && CTG_IS_INF_DBNAME(p + 1)) {
    dbFName = p + 1;
  }

D
dapan1121 已提交
311 312 313 314
  strncpy(msg->dbFName, dbFName, sizeof(msg->dbFName));
  msg->pCtg = pCtg;
  msg->dbId = dbId;
  msg->dbInfo = dbInfo;
D
dapan1121 已提交
315

D
dapan1121 已提交
316
  action.data = msg;
D
dapan1121 已提交
317

D
dapan1121 已提交
318
  CTG_ERR_JRET(ctgPushAction(pCtg, &action));
D
dapan1121 已提交
319

D
dapan1121 已提交
320
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
321

D
dapan1121 已提交
322
_return:
D
dapan1121 已提交
323

D
dapan1121 已提交
324
  ctgFreeVgInfo(dbInfo);
wafwerar's avatar
wafwerar 已提交
325
  taosMemoryFreeClear(action.data);
D
dapan1121 已提交
326
  CTG_RET(code);
D
dapan1121 已提交
327 328
}

D
dapan1121 已提交
329 330
int32_t ctgPushUpdateTblMsgInQueue(SCatalog* pCtg, STableMetaOutput *output, bool syncReq) {
  int32_t code = 0;
D
dapan1121 已提交
331
  SCtgMetaAction action= {.act = CTG_ACT_UPDATE_TBL, .syncReq = syncReq};
wafwerar's avatar
wafwerar 已提交
332
  SCtgUpdateTblMsg *msg = taosMemoryMalloc(sizeof(SCtgUpdateTblMsg));
D
dapan1121 已提交
333 334 335 336 337
  if (NULL == msg) {
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgUpdateTblMsg));
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
  }

D
dapan1121 已提交
338 339 340 341 342
  char *p = strchr(output->dbFName, '.');
  if (p && CTG_IS_INF_DBNAME(p + 1)) {
    memmove(output->dbFName, p + 1, strlen(p + 1));
  }

D
dapan1121 已提交
343 344 345 346 347 348 349 350 351 352 353
  msg->pCtg = pCtg;
  msg->output = output;

  action.data = msg;

  CTG_ERR_JRET(ctgPushAction(pCtg, &action));

  return TSDB_CODE_SUCCESS;
  
_return:

wafwerar's avatar
wafwerar 已提交
354
  taosMemoryFreeClear(msg);
D
dapan1121 已提交
355 356 357 358
  
  CTG_RET(code);
}

D
dapan1121 已提交
359

D
dapan 已提交
360
int32_t ctgAcquireVgInfo(SCatalog *pCtg, SCtgDBCache *dbCache, bool *inCache) {
D
dapan1121 已提交
361 362
  CTG_LOCK(CTG_READ, &dbCache->vgLock);
  
D
dapan 已提交
363 364 365 366 367 368 369 370 371 372
  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 已提交
373 374 375
  if (NULL == dbCache->vgInfo) {
    CTG_UNLOCK(CTG_READ, &dbCache->vgLock);

D
dapan 已提交
376
    *inCache = false;
D
dapan1121 已提交
377
    ctgDebug("db vgInfo is empty, dbId:%"PRIx64, dbCache->dbId);
D
dapan1121 已提交
378 379 380
    return TSDB_CODE_SUCCESS;
  }

D
dapan 已提交
381 382
  *inCache = true;
  
D
dapan1121 已提交
383 384 385 386 387 388 389 390 391 392 393 394 395 396
  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 已提交
397

D
dapan1121 已提交
398 399 400
void ctgReleaseDBCache(SCatalog *pCtg, SCtgDBCache *dbCache) {
  taosHashRelease(pCtg->dbCache, dbCache);
}
D
dapan1121 已提交
401

D
dapan1121 已提交
402 403 404
void ctgReleaseVgInfo(SCtgDBCache *dbCache) {
  CTG_UNLOCK(CTG_READ, &dbCache->vgLock);
}
D
dapan1121 已提交
405

D
dapan1121 已提交
406 407 408
void ctgWReleaseVgInfo(SCtgDBCache *dbCache) {
  CTG_UNLOCK(CTG_WRITE, &dbCache->vgLock);
}
D
dapan1121 已提交
409

D
dapan1121 已提交
410

D
dapan 已提交
411
int32_t ctgAcquireDBCacheImpl(SCatalog* pCtg, const char *dbFName, SCtgDBCache **pCache, bool acquire) {
D
dapan1121 已提交
412 413 414 415 416
  char *p = strchr(dbFName, '.');
  if (p && CTG_IS_INF_DBNAME(p + 1)) {
    dbFName = p + 1;
  }

D
dapan 已提交
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
  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 已提交
454
int32_t ctgAcquireVgInfoFromCache(SCatalog* pCtg, const char *dbFName, SCtgDBCache **pCache, bool *inCache) {
D
dapan1121 已提交
455 456
  SCtgDBCache *dbCache = NULL;

D
dapan1121 已提交
457
  if (NULL == pCtg->dbCache) {
D
dapan1121 已提交
458 459
    ctgDebug("empty db cache, dbFName:%s", dbFName);
    goto _return;
D
dapan1121 已提交
460 461 462 463
  }

  ctgAcquireDBCache(pCtg, dbFName, &dbCache);
  if (NULL == dbCache) {  
D
dapan1121 已提交
464 465
    ctgDebug("db %s not in cache", dbFName);
    goto _return;
D
dapan1121 已提交
466 467
  }
  
D
dapan 已提交
468 469
  ctgAcquireVgInfo(pCtg, dbCache, inCache);
  if (!(*inCache)) {
D
dapan1121 已提交
470 471
    ctgDebug("vgInfo of db %s not in cache", dbFName);
    goto _return;
D
dapan1121 已提交
472
  }
D
dapan1121 已提交
473

D
dapan1121 已提交
474
  *pCache = dbCache;
D
dapan1121 已提交
475
  *inCache = true;
D
dapan1121 已提交
476

D
dapan1121 已提交
477 478
  CTG_CACHE_STAT_ADD(vgHitNum, 1);

D
dapan1121 已提交
479
  ctgDebug("Got db vgInfo from cache, dbFName:%s", dbFName);
D
dapan1121 已提交
480
  
D
dapan1121 已提交
481 482 483 484 485 486 487 488 489 490 491 492 493
  return TSDB_CODE_SUCCESS;

_return:

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

  *pCache = NULL;
  *inCache = false;

  CTG_CACHE_STAT_ADD(vgMissNum, 1);
  
D
dapan1121 已提交
494 495 496
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
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 已提交
519
    ctgError("error rsp for qnode list, error:%s", tstrerror(rpcRsp.code));
D
dapan1121 已提交
520 521 522 523 524 525 526 527 528
    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 已提交
529
  ctgDebug("Got qnode list from mnode, listNum:%d", (int32_t)taosArrayGetSize(*out));
D
dapan1121 已提交
530 531 532

  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
533 534


D
dapan1121 已提交
535
int32_t ctgGetDBVgInfoFromMnode(SCatalog* pCtg, void *pRpc, const SEpSet* pMgmtEps, SBuildUseDBInput *input, SUseDbOutput *out) {
D
dapan1121 已提交
536 537 538
  char *msg = NULL;
  int32_t msgLen = 0;

D
dapan 已提交
539
  ctgDebug("try to get db vgInfo from mnode, dbFName:%s", input->db);
D
dapan1121 已提交
540 541 542 543 544 545

  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 已提交
546
  
D
dapan1121 已提交
547
  SRpcMsg rpcMsg = {
H
Hongze Cheng 已提交
548
      .msgType = TDMT_MND_USE_DB,
D
catalog  
dapan1121 已提交
549
      .pCont   = msg,
D
dapan1121 已提交
550 551 552 553 554 555
      .contLen = msgLen,
  };

  SRpcMsg rpcRsp = {0};

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

D
dapan1121 已提交
561 562 563 564 565
  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 已提交
566

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

D
dapan1121 已提交
569 570
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
571

D
dapan1121 已提交
572 573 574 575 576 577 578 579 580 581 582 583 584 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
int32_t ctgGetDBCfgFromMnode(SCatalog* pCtg, void *pRpc, const SEpSet* pMgmtEps, const char *dbFName, SDbCfgInfo *out) {
  char *msg = NULL;
  int32_t msgLen = 0;

  ctgDebug("try to get db cfg from mnode, dbFName:%s", dbFName);

  int32_t code = queryBuildMsg[TMSG_INDEX(TDMT_MND_GET_DB_CFG)]((void *)dbFName, &msg, 0, &msgLen);
  if (code) {
    ctgError("Build get db cfg msg failed, code:%x, db:%s", code, dbFName);
    CTG_ERR_RET(code);
  }
  
  SRpcMsg rpcMsg = {
      .msgType = TDMT_MND_GET_DB_CFG,
      .pCont   = msg,
      .contLen = msgLen,
  };

  SRpcMsg rpcRsp = {0};

  rpcSendRecv(pRpc, (SEpSet*)pMgmtEps, &rpcMsg, &rpcRsp);
  if (TSDB_CODE_SUCCESS != rpcRsp.code) {
    ctgError("error rsp for get db cfg, error:%s, db:%s", tstrerror(rpcRsp.code), dbFName);
    CTG_ERR_RET(rpcRsp.code);
  }

  code = queryProcessMsgRsp[TMSG_INDEX(TDMT_MND_GET_DB_CFG)](out, rpcRsp.pCont, rpcRsp.contLen);
  if (code) {
    ctgError("Process get db cfg rsp failed, code:%x, db:%s", code, dbFName);
    CTG_ERR_RET(code);
  }

  ctgDebug("Got db cfg from mnode, dbFName:%s", dbFName);

  return TSDB_CODE_SUCCESS;
}


D
dapan1121 已提交
610 611
int32_t ctgIsTableMetaExistInCache(SCatalog* pCtg, char *dbFName, char* tbName, int32_t *exist) {
  if (NULL == pCtg->dbCache) {
D
dapan1121 已提交
612
    *exist = 0;
D
dapan1121 已提交
613 614 615 616
    ctgWarn("empty db cache, dbFName:%s, tbName:%s", dbFName, tbName);
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
617 618
  SCtgDBCache *dbCache = NULL;
  ctgAcquireDBCache(pCtg, dbFName, &dbCache);
D
dapan1121 已提交
619 620
  if (NULL == dbCache) {
    *exist = 0;
D
dapan1121 已提交
621 622 623
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
624
  size_t sz = 0;
D
dapan1121 已提交
625 626 627 628
  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 已提交
629
  if (NULL == tbMeta) {
D
dapan 已提交
630
    ctgReleaseDBCache(pCtg, dbCache);
D
dapan1121 已提交
631
    
D
dapan1121 已提交
632
    *exist = 0;
D
dapan1121 已提交
633
    ctgDebug("tbmeta not in cache, dbFName:%s, tbName:%s", dbFName, tbName);
D
dapan1121 已提交
634 635 636 637
    return TSDB_CODE_SUCCESS;
  }

  *exist = 1;
D
dapan1121 已提交
638

D
dapan 已提交
639
  ctgReleaseDBCache(pCtg, dbCache);
D
dapan1121 已提交
640
  
D
dapan1121 已提交
641
  ctgDebug("tbmeta is in cache, dbFName:%s, tbName:%s", dbFName, tbName);
D
dapan1121 已提交
642 643 644 645
  
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
646

D
dapan1121 已提交
647
int32_t ctgGetTableMetaFromCache(SCatalog* pCtg, const SName* pTableName, STableMeta** pTableMeta, bool *inCache, int32_t flag, uint64_t *dbId) {
D
dapan1121 已提交
648
  if (NULL == pCtg->dbCache) {
D
dapan1121 已提交
649 650
    ctgDebug("empty tbmeta cache, tbName:%s", pTableName->tname);
    goto _return;
D
dapan1121 已提交
651 652
  }

D
dapan1121 已提交
653
  char dbFName[TSDB_DB_FNAME_LEN] = {0};
D
dapan1121 已提交
654
  if (CTG_FLAG_IS_INF_DB(flag)) {
D
dapan1121 已提交
655 656 657 658
    strcpy(dbFName, pTableName->dbname);
  } else {
    tNameGetFullDbName(pTableName, dbFName);
  }
D
dapan1121 已提交
659

D
dapan1121 已提交
660 661
  *pTableMeta = NULL;

D
dapan1121 已提交
662 663
  SCtgDBCache *dbCache = NULL;
  ctgAcquireDBCache(pCtg, dbFName, &dbCache);
D
dapan1121 已提交
664
  if (NULL == dbCache) {
D
dapan1121 已提交
665 666
    ctgDebug("db %s not in cache", pTableName->tname);
    goto _return;
D
dapan1121 已提交
667 668
  }
  
H
Haojun Liao 已提交
669
  int32_t sz = 0;
D
dapan1121 已提交
670
  CTG_LOCK(CTG_READ, &dbCache->tbCache.metaLock);
H
Haojun Liao 已提交
671
  int32_t code = taosHashGetDup_m(dbCache->tbCache.metaCache, pTableName->tname, strlen(pTableName->tname), (void **)pTableMeta, &sz);
D
dapan1121 已提交
672 673
  CTG_UNLOCK(CTG_READ, &dbCache->tbCache.metaLock);

D
dapan1121 已提交
674
  if (NULL == *pTableMeta) {
D
dapan1121 已提交
675 676
    ctgReleaseDBCache(pCtg, dbCache);
    ctgDebug("tbl not in cache, dbFName:%s, tbName:%s", dbFName, pTableName->tname);
D
dapan1121 已提交
677
    goto _return;
D
dapan1121 已提交
678 679
  }

D
dapan1121 已提交
680 681 682
  if (dbId) {
    *dbId = dbCache->dbId;
  }
H
Haojun Liao 已提交
683

H
Haojun Liao 已提交
684
  STableMeta* tbMeta = *pTableMeta;
D
dapan1121 已提交
685

D
dapan1121 已提交
686
  if (tbMeta->tableType != TSDB_CHILD_TABLE) {
D
dapan1121 已提交
687
    ctgReleaseDBCache(pCtg, dbCache);
D
dapan1121 已提交
688
    ctgDebug("Got meta from cache, type:%d, dbFName:%s, tbName:%s", tbMeta->tableType, dbFName, pTableName->tname);
D
dapan1121 已提交
689 690 691 692

    *inCache = true;
    CTG_CACHE_STAT_ADD(tblHitNum, 1);
    
D
dapan1121 已提交
693 694 695
    return TSDB_CODE_SUCCESS;
  }
  
D
dapan1121 已提交
696
  CTG_LOCK(CTG_READ, &dbCache->tbCache.stbLock);
D
dapan1121 已提交
697
  
D
dapan1121 已提交
698
  STableMeta **stbMeta = taosHashGet(dbCache->tbCache.stbCache, &tbMeta->suid, sizeof(tbMeta->suid));
D
dapan1121 已提交
699
  if (NULL == stbMeta || NULL == *stbMeta) {
D
dapan1121 已提交
700
    CTG_UNLOCK(CTG_READ, &dbCache->tbCache.stbLock);
D
dapan1121 已提交
701 702
    ctgReleaseDBCache(pCtg, dbCache);
    ctgError("stb not in stbCache, suid:%"PRIx64, tbMeta->suid);
wafwerar's avatar
wafwerar 已提交
703
    taosMemoryFreeClear(*pTableMeta);
D
dapan1121 已提交
704
    goto _return;
D
dapan1121 已提交
705
  }
D
dapan1121 已提交
706

D
dapan1121 已提交
707
  if ((*stbMeta)->suid != tbMeta->suid) {    
D
dapan1121 已提交
708
    CTG_UNLOCK(CTG_READ, &dbCache->tbCache.stbLock);
D
dapan1121 已提交
709
    ctgReleaseDBCache(pCtg, dbCache);
wafwerar's avatar
wafwerar 已提交
710
    taosMemoryFreeClear(*pTableMeta);
D
dapan1121 已提交
711
    ctgError("stable suid in stbCache mis-match, expected suid:%"PRIx64 ",actual suid:%"PRIx64, tbMeta->suid, (*stbMeta)->suid);
D
dapan1121 已提交
712 713
    CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
  }
D
dapan1121 已提交
714

D
dapan1121 已提交
715
  int32_t metaSize = CTG_META_SIZE(*stbMeta);
wafwerar's avatar
wafwerar 已提交
716
  *pTableMeta = taosMemoryRealloc(*pTableMeta, metaSize);
D
dapan1121 已提交
717
  if (NULL == *pTableMeta) {    
D
dapan1121 已提交
718
    CTG_UNLOCK(CTG_READ, &dbCache->tbCache.stbLock);
D
dapan1121 已提交
719
    ctgReleaseDBCache(pCtg, dbCache);
D
dapan1121 已提交
720
    ctgError("realloc size[%d] failed", metaSize);
D
dapan1121 已提交
721
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
D
dapan1121 已提交
722 723
  }

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

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

D
dapan1121 已提交
728
  ctgReleaseDBCache(pCtg, dbCache);
D
dapan 已提交
729

D
dapan1121 已提交
730 731 732
  *inCache = true;
  CTG_CACHE_STAT_ADD(tblHitNum, 1);

D
dapan1121 已提交
733
  ctgDebug("Got tbmeta from cache, dbFName:%s, tbName:%s", dbFName, pTableName->tname);
D
dapan1121 已提交
734
  
D
dapan1121 已提交
735 736 737 738 739 740 741
  return TSDB_CODE_SUCCESS;

_return:

  *inCache = false;
  CTG_CACHE_STAT_ADD(tblMissNum, 1);
  
D
dapan1121 已提交
742 743 744
  return TSDB_CODE_SUCCESS;
}

D
dapan 已提交
745
int32_t ctgGetTableTypeFromCache(SCatalog* pCtg, const char* dbFName, const char *tableName, int32_t *tbType) {
D
dapan1121 已提交
746
  if (NULL == pCtg->dbCache) {
D
dapan 已提交
747
    ctgWarn("empty db cache, dbFName:%s, tbName:%s", dbFName, tableName);  
D
dapan1121 已提交
748 749
    return TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
750
  
D
dapan 已提交
751 752
  SCtgDBCache *dbCache = NULL;
  ctgAcquireDBCache(pCtg, dbFName, &dbCache);
D
dapan1121 已提交
753 754 755
  if (NULL == dbCache) {
    return TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
756

D
dapan1121 已提交
757
  CTG_LOCK(CTG_READ, &dbCache->tbCache.metaLock);
D
dapan 已提交
758
  STableMeta *pTableMeta = (STableMeta *)taosHashAcquire(dbCache->tbCache.metaCache, tableName, strlen(tableName));
D
dapan1121 已提交
759

D
dapan1121 已提交
760
  if (NULL == pTableMeta) {
D
dapan1121 已提交
761
    CTG_UNLOCK(CTG_READ, &dbCache->tbCache.metaLock);
D
dapan 已提交
762
    ctgWarn("tbl not in cache, dbFName:%s, tbName:%s", dbFName, tableName);  
D
dapan 已提交
763
    ctgReleaseDBCache(pCtg, dbCache);
D
dapan1121 已提交
764
    
D
dapan1121 已提交
765 766 767
    return TSDB_CODE_SUCCESS;
  }

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

D
dapan1121 已提交
770 771 772 773
  taosHashRelease(dbCache->tbCache.metaCache, pTableMeta);

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

D
dapan 已提交
774
  ctgReleaseDBCache(pCtg, dbCache);
D
dapan1121 已提交
775

D
dapan 已提交
776
  ctgDebug("Got tbtype from cache, dbFName:%s, tbName:%s, type:%d", dbFName, tableName, *tbType);  
D
dapan1121 已提交
777 778 779 780
  
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
781
int32_t ctgGetTableMetaFromMnodeImpl(SCatalog* pCtg, void *pTrans, const SEpSet* pMgmtEps, char *dbFName, char* tbName, STableMetaOutput* output) {
D
dapan1121 已提交
782
  SBuildTableMetaInput bInput = {.vgId = 0, .dbFName = dbFName, .tbName = tbName};
D
dapan1121 已提交
783 784 785 786
  char *msg = NULL;
  SEpSet *pVnodeEpSet = NULL;
  int32_t msgLen = 0;

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

D
dapan1121 已提交
789
  int32_t code = queryBuildMsg[TMSG_INDEX(TDMT_MND_TABLE_META)](&bInput, &msg, 0, &msgLen);
D
dapan1121 已提交
790 791 792 793
  if (code) {
    ctgError("Build mnode stablemeta msg failed, code:%x", code);
    CTG_ERR_RET(code);
  }
D
dapan1121 已提交
794 795

  SRpcMsg rpcMsg = {
D
dapan1121 已提交
796
      .msgType = TDMT_MND_TABLE_META,
D
dapan1121 已提交
797 798 799
      .pCont   = msg,
      .contLen = msgLen,
  };
D
dapan1121 已提交
800

D
dapan1121 已提交
801 802
  SRpcMsg rpcRsp = {0};

D
dapan1121 已提交
803
  rpcSendRecv(pTrans, (SEpSet*)pMgmtEps, &rpcMsg, &rpcRsp);
D
dapan1121 已提交
804 805
  
  if (TSDB_CODE_SUCCESS != rpcRsp.code) {
D
dapan1121 已提交
806
    if (CTG_TABLE_NOT_EXIST(rpcRsp.code)) {
D
dapan1121 已提交
807
      SET_META_TYPE_NULL(output->metaType);
D
dapan1121 已提交
808
      ctgDebug("stablemeta not exist in mnode, dbFName:%s, tbName:%s", dbFName, tbName);
D
dapan1121 已提交
809 810 811
      return TSDB_CODE_SUCCESS;
    }
    
D
dapan1121 已提交
812
    ctgError("error rsp for stablemeta from mnode, code:%s, dbFName:%s, tbName:%s", tstrerror(rpcRsp.code), dbFName, tbName);
D
dapan1121 已提交
813 814 815
    CTG_ERR_RET(rpcRsp.code);
  }

D
dapan1121 已提交
816
  code = queryProcessMsgRsp[TMSG_INDEX(TDMT_MND_TABLE_META)](output, rpcRsp.pCont, rpcRsp.contLen);
D
dapan1121 已提交
817
  if (code) {
D
dapan1121 已提交
818
    ctgError("Process mnode stablemeta rsp failed, code:%x, dbFName:%s, tbName:%s", code, dbFName, tbName);
D
dapan1121 已提交
819 820 821
    CTG_ERR_RET(code);
  }

D
dapan1121 已提交
822
  ctgDebug("Got table meta from mnode, dbFName:%s, tbName:%s", dbFName, tbName);
D
dapan1121 已提交
823 824 825 826

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
827
int32_t ctgGetTableMetaFromMnode(SCatalog* pCtg, void *pTrans, const SEpSet* pMgmtEps, const SName* pTableName, STableMetaOutput* output) {
D
dapan1121 已提交
828 829
  char dbFName[TSDB_DB_FNAME_LEN];
  tNameGetFullDbName(pTableName, dbFName);
D
dapan1121 已提交
830

D
dapan1121 已提交
831
  return ctgGetTableMetaFromMnodeImpl(pCtg, pTrans, pMgmtEps, dbFName, (char *)pTableName->tname, output);
D
dapan1121 已提交
832
}
D
dapan1121 已提交
833

D
dapan1121 已提交
834
int32_t ctgGetTableMetaFromVnodeImpl(SCatalog* pCtg, void *pTrans, const SEpSet* pMgmtEps, const SName* pTableName, SVgroupInfo *vgroupInfo, STableMetaOutput* output) {
D
dapan1121 已提交
835
  if (NULL == pCtg || NULL == pTrans || NULL == pMgmtEps || NULL == pTableName || NULL == vgroupInfo || NULL == output) {
D
dapan1121 已提交
836
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
D
dapan1121 已提交
837 838
  }

D
dapan1121 已提交
839 840
  char dbFName[TSDB_DB_FNAME_LEN];
  tNameGetFullDbName(pTableName, dbFName);
D
dapan1121 已提交
841

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

D
dapan1121 已提交
844
  SBuildTableMetaInput bInput = {.vgId = vgroupInfo->vgId, .dbFName = dbFName, .tbName = (char *)tNameGetTableName(pTableName)};
D
dapan1121 已提交
845 846 847
  char *msg = NULL;
  int32_t msgLen = 0;

D
dapan1121 已提交
848 849
  int32_t code = queryBuildMsg[TMSG_INDEX(TDMT_VND_TABLE_META)](&bInput, &msg, 0, &msgLen);
  if (code) {
D
dapan1121 已提交
850
    ctgError("Build vnode tablemeta msg failed, code:%x, dbFName:%s, tbName:%s", code, dbFName, tNameGetTableName(pTableName));
D
dapan1121 已提交
851 852
    CTG_ERR_RET(code);
  }
D
dapan1121 已提交
853 854

  SRpcMsg rpcMsg = {
H
Hongze Cheng 已提交
855
      .msgType = TDMT_VND_TABLE_META,
D
dapan1121 已提交
856 857 858 859 860
      .pCont   = msg,
      .contLen = msgLen,
  };

  SRpcMsg rpcRsp = {0};
L
Liu Jicong 已提交
861
  rpcSendRecv(pTrans, &vgroupInfo->epSet, &rpcMsg, &rpcRsp);
D
dapan1121 已提交
862
  
D
dapan1121 已提交
863
  if (TSDB_CODE_SUCCESS != rpcRsp.code) {
D
dapan1121 已提交
864
    if (CTG_TABLE_NOT_EXIST(rpcRsp.code)) {
D
dapan1121 已提交
865
      SET_META_TYPE_NULL(output->metaType);
D
dapan1121 已提交
866
      ctgDebug("tablemeta not exist in vnode, dbFName:%s, tbName:%s", dbFName, tNameGetTableName(pTableName));
D
dapan1121 已提交
867 868 869
      return TSDB_CODE_SUCCESS;
    }
  
D
dapan1121 已提交
870
    ctgError("error rsp for table meta from vnode, code:%s, dbFName:%s, tbName:%s", tstrerror(rpcRsp.code), dbFName, tNameGetTableName(pTableName));
D
dapan1121 已提交
871
    CTG_ERR_RET(rpcRsp.code);
D
dapan1121 已提交
872 873
  }

D
dapan1121 已提交
874 875
  code = queryProcessMsgRsp[TMSG_INDEX(TDMT_VND_TABLE_META)](output, rpcRsp.pCont, rpcRsp.contLen);
  if (code) {
D
dapan1121 已提交
876
    ctgError("Process vnode tablemeta rsp failed, code:%s, dbFName:%s, tbName:%s", tstrerror(code), dbFName, tNameGetTableName(pTableName));
D
dapan1121 已提交
877 878 879
    CTG_ERR_RET(code);
  }

D
dapan1121 已提交
880
  ctgDebug("Got table meta from vnode, dbFName:%s, tbName:%s", dbFName, tNameGetTableName(pTableName));
D
dapan1121 已提交
881 882 883
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
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 已提交
910

911 912
int32_t ctgGetHashFunction(int8_t hashMethod, tableNameHashFp *fp) {
  switch (hashMethod) {
D
dapan1121 已提交
913 914 915 916 917 918 919 920
    default:
      *fp = MurmurHash3_32;
      break;
  }

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
921
int32_t ctgGenerateVgList(SCatalog *pCtg, SHashObj *vgHash, SArray** pList) {
D
dapan1121 已提交
922
  SHashObj *vgroupHash = NULL;
923
  SVgroupInfo *vgInfo = NULL;
D
dapan1121 已提交
924 925
  SArray *vgList = NULL;
  int32_t code = 0;
D
dapan1121 已提交
926
  int32_t vgNum = taosHashGetSize(vgHash);
927

D
dapan1121 已提交
928
  vgList = taosArrayInit(vgNum, sizeof(SVgroupInfo));
D
dapan1121 已提交
929
  if (NULL == vgList) {
D
dapan1121 已提交
930
    ctgError("taosArrayInit failed, num:%d", vgNum);
D
dapan 已提交
931 932 933
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);    
  }

D
dapan1121 已提交
934
  void *pIter = taosHashIterate(vgHash, NULL);
935 936
  while (pIter) {
    vgInfo = pIter;
D
dapan1121 已提交
937

D
dapan1121 已提交
938
    if (NULL == taosArrayPush(vgList, vgInfo)) {
D
dapan1121 已提交
939
      ctgError("taosArrayPush failed, vgId:%d", vgInfo->vgId);
D
dapan1121 已提交
940
      taosHashCancelIterate(vgHash, pIter);      
D
dapan1121 已提交
941
      CTG_ERR_JRET(TSDB_CODE_CTG_MEM_ERROR);
942 943
    }
    
D
dapan1121 已提交
944
    pIter = taosHashIterate(vgHash, pIter);
945
    vgInfo = NULL;
D
dapan1121 已提交
946 947
  }

D
dapan1121 已提交
948
  *pList = vgList;
D
dapan1121 已提交
949

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

D
dapan1121 已提交
952
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
953 954 955 956 957 958 959 960

_return:

  if (vgList) {
    taosArrayDestroy(vgList);
  }

  CTG_RET(code);
D
dapan1121 已提交
961 962
}

D
dapan1121 已提交
963
int32_t ctgGetVgInfoFromHashValue(SCatalog *pCtg, SDBVgInfo *dbInfo, const SName *pTableName, SVgroupInfo *pVgroup) {
D
dapan1121 已提交
964 965
  int32_t code = 0;
  
D
dapan1121 已提交
966
  int32_t vgNum = taosHashGetSize(dbInfo->vgHash);
H
Haojun Liao 已提交
967 968 969
  char db[TSDB_DB_FNAME_LEN] = {0};
  tNameGetFullDbName(pTableName, db);

970
  if (vgNum <= 0) {
D
dapan1121 已提交
971
    ctgError("db vgroup cache invalid, db:%s, vgroup number:%d", db, vgNum);
D
dapan1121 已提交
972
    CTG_ERR_RET(TSDB_CODE_TSC_DB_NOT_SELECTED);
D
dapan1121 已提交
973 974
  }

975 976
  tableNameHashFp fp = NULL;
  SVgroupInfo *vgInfo = NULL;
D
dapan1121 已提交
977

D
dapan1121 已提交
978
  CTG_ERR_RET(ctgGetHashFunction(dbInfo->hashMethod, &fp));
979 980

  char tbFullName[TSDB_TABLE_FNAME_LEN];
H
Haojun Liao 已提交
981
  tNameExtractFullName(pTableName, tbFullName);
982 983 984

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

D
dapan1121 已提交
985
  void *pIter = taosHashIterate(dbInfo->vgHash, NULL);
986 987 988
  while (pIter) {
    vgInfo = pIter;
    if (hashValue >= vgInfo->hashBegin && hashValue <= vgInfo->hashEnd) {
D
dapan1121 已提交
989
      taosHashCancelIterate(dbInfo->vgHash, pIter);
990
      break;
D
dapan1121 已提交
991
    }
992
    
D
dapan1121 已提交
993
    pIter = taosHashIterate(dbInfo->vgHash, pIter);
994
    vgInfo = NULL;
D
dapan1121 已提交
995 996
  }

997
  if (NULL == vgInfo) {
D
dapan1121 已提交
998 999
    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);
1000 1001 1002 1003
  }

  *pVgroup = *vgInfo;

1004
  CTG_RET(code);
D
dapan1121 已提交
1005 1006
}

D
dapan1121 已提交
1007
int32_t ctgStbVersionSearchCompare(const void* key1, const void* key2) {
D
dapan 已提交
1008
  if (*(uint64_t *)key1 < ((SSTableMetaVersion*)key2)->suid) {
D
dapan1121 已提交
1009
    return -1;
D
dapan 已提交
1010
  } else if (*(uint64_t *)key1 > ((SSTableMetaVersion*)key2)->suid) {
D
dapan1121 已提交
1011 1012 1013 1014 1015 1016
    return 1;
  } else {
    return 0;
  }
}

D
dapan1121 已提交
1017
int32_t ctgDbVgVersionSearchCompare(const void* key1, const void* key2) {
D
dapan1121 已提交
1018
  if (*(int64_t *)key1 < ((SDbVgVersion*)key2)->dbId) {
D
dapan1121 已提交
1019
    return -1;
D
dapan1121 已提交
1020
  } else if (*(int64_t *)key1 > ((SDbVgVersion*)key2)->dbId) {
D
dapan1121 已提交
1021 1022 1023
    return 1;
  } else {
    return 0;
D
dapan1121 已提交
1024
  }
D
dapan1121 已提交
1025 1026
}

D
dapan1121 已提交
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
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 已提交
1048
int32_t ctgMetaRentInit(SCtgRentMgmt *mgmt, uint32_t rentSec, int8_t type) {
D
dapan1121 已提交
1049 1050 1051 1052
  mgmt->slotRIdx = 0;
  mgmt->slotNum = rentSec / CTG_RENT_SLOT_SECOND;
  mgmt->type = type;

D
dapan1121 已提交
1053
  size_t msgSize = sizeof(SCtgRentSlot) * mgmt->slotNum;
D
dapan1121 已提交
1054
  
wafwerar's avatar
wafwerar 已提交
1055
  mgmt->slots = taosMemoryCalloc(1, msgSize);
D
dapan1121 已提交
1056 1057
  if (NULL == mgmt->slots) {
    qError("calloc %d failed", (int32_t)msgSize);
D
dapan 已提交
1058
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
D
dapan1121 已提交
1059
  }
D
dapan1121 已提交
1060

D
dapan1121 已提交
1061 1062 1063 1064
  qDebug("meta rent initialized, type:%d, slotNum:%d", type, mgmt->slotNum);
  
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
1065

D
dapan1121 已提交
1066

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

D
dapan1121 已提交
1070
  SCtgRentSlot *slot = &mgmt->slots[widx];
D
dapan1121 已提交
1071 1072 1073 1074 1075 1076 1077 1078
  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 已提交
1079
    }
D
dapan1121 已提交
1080
  }
D
dapan1121 已提交
1081

D
dapan1121 已提交
1082 1083 1084
  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 已提交
1085 1086
  }

D
dapan1121 已提交
1087
  slot->needSort = true;
D
dapan1121 已提交
1088

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

D
dapan1121 已提交
1091 1092 1093 1094 1095 1096
_return:

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

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

D
dapan1121 已提交
1100
  SCtgRentSlot *slot = &mgmt->slots[widx];
D
dapan1121 已提交
1101
  int32_t code = 0;
1102

D
dapan1121 已提交
1103 1104
  CTG_LOCK(CTG_WRITE, &slot->lock);
  if (NULL == slot->meta) {
D
dapan1121 已提交
1105 1106
    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 已提交
1107 1108 1109
  }

  if (slot->needSort) {
D
dapan1121 已提交
1110
    qDebug("meta slot before sorte, slot idx:%d, type:%d, size:%d", widx, mgmt->type, (int32_t)taosArrayGetSize(slot->meta));
D
dapan1121 已提交
1111
    taosArraySort(slot->meta, sortCompare);
D
dapan1121 已提交
1112
    slot->needSort = false;
D
dapan1121 已提交
1113
    qDebug("meta slot sorted, slot idx:%d, type:%d, size:%d", widx, mgmt->type, (int32_t)taosArrayGetSize(slot->meta));
D
dapan1121 已提交
1114 1115
  }

D
dapan1121 已提交
1116
  void *orig = taosArraySearch(slot->meta, &id, searchCompare, TD_EQ);
D
dapan1121 已提交
1117
  if (NULL == orig) {
D
dapan1121 已提交
1118
    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 已提交
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
    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 已提交
1138
int32_t ctgMetaRentRemove(SCtgRentMgmt *mgmt, int64_t id, __compar_fn_t sortCompare, __compar_fn_t searchCompare) {
1139
  int16_t widx = abs((int)(id % mgmt->slotNum));
D
dapan1121 已提交
1140

D
dapan1121 已提交
1141
  SCtgRentSlot *slot = &mgmt->slots[widx];
D
dapan1121 已提交
1142 1143 1144 1145 1146 1147 1148 1149 1150
  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 已提交
1151
    taosArraySort(slot->meta, sortCompare);
D
dapan1121 已提交
1152 1153 1154 1155
    slot->needSort = false;
    qDebug("meta slot sorted, slot idx:%d, type:%d", widx, mgmt->type);
  }

D
dapan1121 已提交
1156
  int32_t idx = taosArraySearchIdx(slot->meta, &id, searchCompare, TD_EQ);
D
dapan1121 已提交
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
  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 已提交
1174
int32_t ctgMetaRentGetImpl(SCtgRentMgmt *mgmt, void **res, uint32_t *num, int32_t size) {
D
dapan1121 已提交
1175 1176 1177 1178
  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 已提交
1179
  }
D
dapan1121 已提交
1180

D
dapan1121 已提交
1181
  SCtgRentSlot *slot = &mgmt->slots[ridx];
D
dapan1121 已提交
1182
  int32_t code = 0;
D
dapan1121 已提交
1183
  
D
dapan1121 已提交
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
  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;
wafwerar's avatar
wafwerar 已提交
1199
  *res = taosMemoryMalloc(msize);
D
dapan1121 已提交
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
  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 已提交
1220
int32_t ctgMetaRentGet(SCtgRentMgmt *mgmt, void **res, uint32_t *num, int32_t size) {
D
dapan1121 已提交
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
  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 已提交
1240 1241 1242
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1243
int32_t ctgAddNewDBCache(SCatalog *pCtg, const char *dbFName, uint64_t dbId) {
D
dapan1121 已提交
1244
  int32_t code = 0;
D
dapan1121 已提交
1245

D
dapan1121 已提交
1246 1247 1248
  SCtgDBCache newDBCache = {0};
  newDBCache.dbId = dbId;

D
dapan 已提交
1249
  newDBCache.tbCache.metaCache = taosHashInit(gCtgMgmt.cfg.maxTblCacheNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
D
dapan1121 已提交
1250
  if (NULL == newDBCache.tbCache.metaCache) {
D
dapan 已提交
1251
    ctgError("taosHashInit %d metaCache failed", gCtgMgmt.cfg.maxTblCacheNum);
D
dapan1121 已提交
1252 1253 1254
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
  }

D
dapan 已提交
1255
  newDBCache.tbCache.stbCache = taosHashInit(gCtgMgmt.cfg.maxTblCacheNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), true, HASH_ENTRY_LOCK);
D
dapan1121 已提交
1256
  if (NULL == newDBCache.tbCache.stbCache) {
D
dapan 已提交
1257
    ctgError("taosHashInit %d stbCache failed", gCtgMgmt.cfg.maxTblCacheNum);
D
dapan1121 已提交
1258 1259 1260 1261
    CTG_ERR_JRET(TSDB_CODE_CTG_MEM_ERROR);
  }

  code = taosHashPut(pCtg->dbCache, dbFName, strlen(dbFName), &newDBCache, sizeof(SCtgDBCache));
D
dapan1121 已提交
1262 1263 1264
  if (code) {
    if (HASH_NODE_EXIST(code)) {
      ctgDebug("db already in cache, dbFName:%s", dbFName);
D
dapan1121 已提交
1265
      goto _return;
D
dapan1121 已提交
1266 1267 1268
    }
    
    ctgError("taosHashPut db to cache failed, dbFName:%s", dbFName);
D
dapan1121 已提交
1269 1270
    CTG_ERR_JRET(TSDB_CODE_CTG_MEM_ERROR);
  }
D
dapan1121 已提交
1271 1272

  CTG_CACHE_STAT_ADD(dbNum, 1);
D
dapan1121 已提交
1273
 
D
dapan1121 已提交
1274
  SDbVgVersion vgVersion = {.dbId = newDBCache.dbId, .vgVersion = -1};
D
dapan1121 已提交
1275 1276
  strncpy(vgVersion.dbFName, dbFName, sizeof(vgVersion.dbFName));

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

D
dapan1121 已提交
1279 1280 1281
  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 已提交
1282

D
dapan1121 已提交
1283
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1284

D
dapan1121 已提交
1285
_return:
D
dapan1121 已提交
1286

D
dapan1121 已提交
1287
  ctgFreeDbCache(&newDBCache);
D
dapan1121 已提交
1288

D
dapan1121 已提交
1289 1290
  CTG_RET(code);
}
D
dapan1121 已提交
1291

D
dapan1121 已提交
1292

D
dapan1121 已提交
1293
void ctgRemoveStbRent(SCatalog* pCtg, SCtgTbMetaCache *cache) {
D
dapan1121 已提交
1294 1295 1296 1297 1298
  CTG_LOCK(CTG_WRITE, &cache->stbLock);
  if (cache->stbCache) {
    void *pIter = taosHashIterate(cache->stbCache, NULL);
    while (pIter) {
      uint64_t *suid = NULL;
H
Haojun Liao 已提交
1299
      suid = taosHashGetKey(pIter, NULL);
D
dapan1121 已提交
1300

D
dapan1121 已提交
1301
      if (TSDB_CODE_SUCCESS == ctgMetaRentRemove(&pCtg->stbRent, *suid, ctgStbVersionSortCompare, ctgStbVersionSearchCompare)) {
D
dapan1121 已提交
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
        ctgDebug("stb removed from rent, suid:%"PRIx64, *suid);
      }
          
      pIter = taosHashIterate(cache->stbCache, pIter);
    }
  }
  CTG_UNLOCK(CTG_WRITE, &cache->stbLock);
}


D
dapan1121 已提交
1312
int32_t ctgRemoveDB(SCatalog* pCtg, SCtgDBCache *dbCache, const char* dbFName) {
D
dapan 已提交
1313 1314 1315 1316
  uint64_t dbId = dbCache->dbId;
  
  ctgInfo("start to remove db from cache, dbFName:%s, dbId:%"PRIx64, dbFName, dbCache->dbId);

D
dapan1121 已提交
1317 1318 1319 1320 1321 1322
  atomic_store_8(&dbCache->deleted, 1);

  ctgRemoveStbRent(pCtg, &dbCache->tbCache);

  ctgFreeDbCache(dbCache);

D
dapan1121 已提交
1323
  CTG_ERR_RET(ctgMetaRentRemove(&pCtg->dbRent, dbCache->dbId, ctgDbVgVersionSortCompare, ctgDbVgVersionSearchCompare));
D
dapan1121 已提交
1324 1325 1326 1327
  
  ctgDebug("db removed from rent, dbFName:%s, dbId:%"PRIx64, dbFName, dbCache->dbId);

  if (taosHashRemove(pCtg->dbCache, dbFName, strlen(dbFName))) {
D
dapan1121 已提交
1328 1329
    ctgInfo("taosHashRemove from dbCache failed, may be removed, dbFName:%s", dbFName);
    CTG_ERR_RET(TSDB_CODE_CTG_DB_DROPPED);
D
dapan1121 已提交
1330
  }
D
dapan 已提交
1331

D
dapan1121 已提交
1332 1333
  CTG_CACHE_STAT_SUB(dbNum, 1);

D
dapan 已提交
1334
  ctgInfo("db removed from cache, dbFName:%s, dbId:%"PRIx64, dbFName, dbId);
D
dapan1121 已提交
1335 1336 1337
  
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
1338 1339


D
dapan1121 已提交
1340
int32_t ctgGetAddDBCache(SCatalog* pCtg, const char *dbFName, uint64_t dbId, SCtgDBCache **pCache) {
D
dapan1121 已提交
1341 1342
  int32_t code = 0;
  SCtgDBCache *dbCache = NULL;
D
dapan1121 已提交
1343
  ctgGetDBCache(pCtg, dbFName, &dbCache);
D
dapan1121 已提交
1344
  
D
dapan1121 已提交
1345 1346
  if (dbCache) {
  // TODO OPEN IT
D
dapan1121 已提交
1347
#if 0    
D
dapan1121 已提交
1348 1349 1350 1351
    if (dbCache->dbId == dbId) {
      *pCache = dbCache;
      return TSDB_CODE_SUCCESS;
    }
D
dapan1121 已提交
1352
#else
D
dapan1121 已提交
1353 1354 1355
    if (0 == dbId) {
      *pCache = dbCache;
      return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1356 1357
    }

D
dapan1121 已提交
1358 1359 1360 1361 1362
    if (dbId && (dbCache->dbId == 0)) {
      dbCache->dbId = dbId;
      *pCache = dbCache;
      return TSDB_CODE_SUCCESS;
    }
D
dapan1121 已提交
1363
    
D
dapan1121 已提交
1364 1365 1366 1367 1368 1369
    if (dbCache->dbId == dbId) {
      *pCache = dbCache;
      return TSDB_CODE_SUCCESS;
    }
#endif
    CTG_ERR_RET(ctgRemoveDB(pCtg, dbCache, dbFName));
D
dapan1121 已提交
1370
  }
D
dapan1121 已提交
1371 1372
  
  CTG_ERR_RET(ctgAddNewDBCache(pCtg, dbFName, dbId));
D
dapan1121 已提交
1373

D
dapan1121 已提交
1374
  ctgGetDBCache(pCtg, dbFName, &dbCache);
D
dapan1121 已提交
1375

D
dapan1121 已提交
1376
  *pCache = dbCache;
D
dapan1121 已提交
1377

D
dapan1121 已提交
1378
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1379 1380 1381
}


D
dapan1121 已提交
1382 1383 1384
int32_t ctgUpdateDBVgInfo(SCatalog* pCtg, const char* dbFName, uint64_t dbId, SDBVgInfo** pDbInfo) {
  int32_t code = 0;
  SDBVgInfo* dbInfo = *pDbInfo;
D
dapan1121 已提交
1385 1386 1387 1388

  if (NULL == dbInfo->vgHash) {
    return TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
1389
  
D
dapan1121 已提交
1390
  if (dbInfo->vgVersion < 0 || taosHashGetSize(dbInfo->vgHash) <= 0) {
D
dapan1121 已提交
1391 1392
    ctgError("invalid db vgInfo, dbFName:%s, vgHash:%p, vgVersion:%d, vgHashSize:%d", 
      dbFName, dbInfo->vgHash, dbInfo->vgVersion, taosHashGetSize(dbInfo->vgHash));
D
dapan1121 已提交
1393
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
D
dapan1121 已提交
1394 1395
  }

D
dapan1121 已提交
1396
  bool newAdded = false;
D
dapan 已提交
1397
  SDbVgVersion vgVersion = {.dbId = dbId, .vgVersion = dbInfo->vgVersion, .numOfTable = dbInfo->numOfTable};
D
dapan1121 已提交
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407

  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 已提交
1408
  
D
dapan1121 已提交
1409
  if (dbCache->vgInfo) {
D
dapan 已提交
1410 1411 1412 1413 1414 1415 1416 1417 1418
    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 已提交
1419
      ctgWReleaseVgInfo(dbCache);
D
dapan1121 已提交
1420
      
D
dapan1121 已提交
1421
      return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1422
    }
D
dapan1121 已提交
1423 1424

    ctgFreeVgInfo(dbCache->vgInfo);
D
dapan1121 已提交
1425 1426
  }

D
dapan1121 已提交
1427
  dbCache->vgInfo = dbInfo;
D
dapan1121 已提交
1428

D
dapan1121 已提交
1429
  *pDbInfo = NULL;
D
dapan1121 已提交
1430

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

D
dapan1121 已提交
1433
  ctgWReleaseVgInfo(dbCache);
D
dapan1121 已提交
1434

D
dapan1121 已提交
1435
  dbCache = NULL;
D
dapan1121 已提交
1436

D
dapan1121 已提交
1437
  strncpy(vgVersion.dbFName, dbFName, sizeof(vgVersion.dbFName));
D
dapan1121 已提交
1438
  CTG_ERR_RET(ctgMetaRentUpdate(&pCtg->dbRent, &vgVersion, vgVersion.dbId, sizeof(SDbVgVersion), ctgDbVgVersionSortCompare, ctgDbVgVersionSearchCompare));
D
dapan1121 已提交
1439
  
D
dapan1121 已提交
1440 1441 1442 1443
  CTG_RET(code);
}


D
dapan1121 已提交
1444 1445
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 已提交
1446

D
dapan1121 已提交
1447 1448 1449 1450 1451
  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 已提交
1452 1453
  }

D
dapan1121 已提交
1454 1455 1456 1457 1458 1459 1460
  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 已提交
1461 1462 1463 1464 1465
    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);
D
dapan1121 已提交
1466 1467
        } else {
          CTG_CACHE_STAT_SUB(stblNum, 1);
D
dapan1121 已提交
1468 1469 1470 1471 1472
        }
        CTG_UNLOCK(CTG_WRITE, &tbCache->stbLock);

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

D
dapan1121 已提交
1476
      origSuid = orig->suid;
D
dapan1121 已提交
1477
    }
D
dapan1121 已提交
1478
  }
D
dapan1121 已提交
1479

D
dapan1121 已提交
1480 1481
  if (isStb) {
    CTG_LOCK(CTG_WRITE, &tbCache->stbLock);
D
dapan1121 已提交
1482
  }
D
dapan1121 已提交
1483
  
D
dapan1121 已提交
1484 1485 1486 1487 1488 1489 1490 1491 1492
  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 已提交
1493

D
dapan1121 已提交
1494 1495 1496 1497
  if (NULL == orig) {
    CTG_CACHE_STAT_ADD(tblNum, 1);
  }

D
dapan 已提交
1498
  ctgDebug("tbmeta updated to cache, dbFName:%s, tbName:%s, tbType:%d", dbFName, tbName, meta->tableType);
D
dapan1121 已提交
1499
  ctgdShowTableMeta(pCtg, tbName, meta);
D
dapan 已提交
1500

D
dapan1121 已提交
1501 1502 1503
  if (!isStb) {
    CTG_UNLOCK(CTG_READ, &tbCache->metaLock);  
    return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1504
  }
D
dapan1121 已提交
1505

D
dapan1121 已提交
1506
  if (origType == TSDB_SUPER_TABLE && origSuid == meta->suid) {
D
dapan1121 已提交
1507 1508 1509 1510
    CTG_UNLOCK(CTG_WRITE, &tbCache->stbLock);
    CTG_UNLOCK(CTG_READ, &tbCache->metaLock);  
    return TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
1511

D
dapan1121 已提交
1512 1513 1514 1515
  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 已提交
1516
    ctgError("taosHashPut stable to stable cache failed, suid:%"PRIx64, meta->suid);
D
dapan1121 已提交
1517
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
D
dapan1121 已提交
1518
  }
D
dapan1121 已提交
1519 1520

  CTG_CACHE_STAT_ADD(stblNum, 1);
D
dapan1121 已提交
1521 1522
  
  CTG_UNLOCK(CTG_WRITE, &tbCache->stbLock);
D
dapan1121 已提交
1523

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

D
dapan 已提交
1526
  ctgDebug("stb updated to stbCache, dbFName:%s, tbName:%s, tbType:%d", dbFName, tbName, meta->tableType);
D
dapan1121 已提交
1527 1528 1529 1530 1531 1532 1533

  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 已提交
1534 1535
}

D
dapan 已提交
1536
int32_t ctgCloneVgInfo(SDBVgInfo *src, SDBVgInfo **dst) {
wafwerar's avatar
wafwerar 已提交
1537
  *dst = taosMemoryMalloc(sizeof(SDBVgInfo));
D
dapan 已提交
1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
  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);
wafwerar's avatar
wafwerar 已提交
1549
    taosMemoryFreeClear(*dst);
D
dapan 已提交
1550 1551 1552 1553 1554 1555
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
  }

  int32_t *vgId = NULL;
  void *pIter = taosHashIterate(src->vgHash, NULL);
  while (pIter) {
H
Haojun Liao 已提交
1556
    vgId = taosHashGetKey(pIter, NULL);
D
dapan 已提交
1557 1558 1559 1560 1561

    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);
wafwerar's avatar
wafwerar 已提交
1562
      taosMemoryFreeClear(*dst);
D
dapan 已提交
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
      CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
    }
    
    pIter = taosHashIterate(src->vgHash, pIter);
  }


  return TSDB_CODE_SUCCESS;
}



D
dapan1121 已提交
1575
int32_t ctgGetDBVgInfo(SCatalog* pCtg, void *pRpc, const SEpSet* pMgmtEps, const char* dbFName, SCtgDBCache** dbCache, SDBVgInfo **pInfo) {
D
dapan1121 已提交
1576
  bool inCache = false;
D
dapan1121 已提交
1577
  int32_t code = 0;
D
dapan1121 已提交
1578 1579 1580

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

D
dapan1121 已提交
1581
  if (inCache) {
D
dapan1121 已提交
1582
    return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1583 1584 1585 1586 1587
  }

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

D
dapan1121 已提交
1588
  tstrncpy(input.db, dbFName, tListLen(input.db));
D
dapan1121 已提交
1589
  input.vgVersion = CTG_DEFAULT_INVALID_VERSION;
H
Haojun Liao 已提交
1590

D
dapan1121 已提交
1591 1592 1593 1594 1595 1596 1597 1598 1599
  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 已提交
1600

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

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

D
dapan1121 已提交
1605
  return TSDB_CODE_SUCCESS;
D
dapan 已提交
1606 1607 1608

_return:

wafwerar's avatar
wafwerar 已提交
1609
  taosMemoryFreeClear(*pInfo);
D
dapan 已提交
1610 1611 1612
  *pInfo = DbOut.dbVgroup;
  
  CTG_RET(code);
D
dapan1121 已提交
1613 1614
}

D
dapan1121 已提交
1615 1616 1617
int32_t ctgRefreshDBVgInfo(SCatalog* pCtg, void *pRpc, const SEpSet* pMgmtEps, const char* dbFName) {
  bool inCache = false;
  int32_t code = 0;
D
dapan1121 已提交
1618
  SCtgDBCache* dbCache = NULL;
D
dapan1121 已提交
1619

D
dapan1121 已提交
1620
  CTG_ERR_RET(ctgAcquireVgInfoFromCache(pCtg, dbFName, &dbCache, &inCache));
D
dapan1121 已提交
1621 1622 1623 1624 1625 1626

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

  if (inCache) {
D
dapan1121 已提交
1627
    input.dbId = dbCache->dbId;
D
dapan1121 已提交
1628

D
dapan1121 已提交
1629 1630
    ctgReleaseVgInfo(dbCache);
    ctgReleaseDBCache(pCtg, dbCache);
D
dapan1121 已提交
1631
  }
D
dapan1121 已提交
1632 1633 1634
  
  input.vgVersion = CTG_DEFAULT_INVALID_VERSION;
  input.numOfTable = 0;
D
dapan1121 已提交
1635 1636 1637

  code = ctgGetDBVgInfoFromMnode(pCtg, pRpc, pMgmtEps, &input, &DbOut);
  if (code) {
D
dapan1121 已提交
1638
    if (CTG_DB_NOT_EXIST(code) && inCache) {
D
dapan1121 已提交
1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651
      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 已提交
1652

D
dapan1121 已提交
1653
int32_t ctgCloneMetaOutput(STableMetaOutput *output, STableMetaOutput **pOutput) {
wafwerar's avatar
wafwerar 已提交
1654
  *pOutput = taosMemoryMalloc(sizeof(STableMetaOutput));
D
dapan1121 已提交
1655 1656 1657
  if (NULL == *pOutput) {
    qError("malloc %d failed", (int32_t)sizeof(STableMetaOutput));
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
D
dapan 已提交
1658 1659
  }

D
dapan1121 已提交
1660 1661 1662 1663
  memcpy(*pOutput, output, sizeof(STableMetaOutput));

  if (output->tbMeta) {
    int32_t metaSize = CTG_META_SIZE(output->tbMeta);
wafwerar's avatar
wafwerar 已提交
1664
    (*pOutput)->tbMeta = taosMemoryMalloc(metaSize);
D
dapan1121 已提交
1665
    if (NULL == (*pOutput)->tbMeta) {
D
dapan 已提交
1666
      qError("malloc %d failed", (int32_t)sizeof(STableMetaOutput));
wafwerar's avatar
wafwerar 已提交
1667
      taosMemoryFreeClear(*pOutput);
D
dapan1121 已提交
1668 1669 1670 1671
      CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
    }

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

D
dapan1121 已提交
1674 1675 1676
  return TSDB_CODE_SUCCESS;
}

D
dapan 已提交
1677 1678


D
dapan1121 已提交
1679
int32_t ctgRefreshTblMeta(SCatalog* pCtg, void *pTrans, const SEpSet* pMgmtEps, const SName* pTableName, int32_t flag, STableMetaOutput **pOutput, bool syncReq) {
D
dapan1121 已提交
1680
  if (NULL == pCtg || NULL == pTrans || NULL == pMgmtEps || NULL == pTableName) {
D
dapan1121 已提交
1681 1682 1683 1684 1685 1686
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
  }

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

D
dapan1121 已提交
1687
  if (!CTG_FLAG_IS_INF_DB(flag)) {
D
dapan1121 已提交
1688 1689
    CTG_ERR_RET(catalogGetTableHashVgroup(pCtg, pTrans, pMgmtEps, pTableName, &vgroupInfo));
  }
D
dapan1121 已提交
1690

D
dapan1121 已提交
1691
  STableMetaOutput  moutput = {0};
wafwerar's avatar
wafwerar 已提交
1692
  STableMetaOutput *output = taosMemoryCalloc(1, sizeof(STableMetaOutput));
D
dapan1121 已提交
1693 1694 1695 1696
  if (NULL == output) {
    ctgError("malloc %d failed", (int32_t)sizeof(STableMetaOutput));
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
  }
D
dapan1121 已提交
1697

D
dapan1121 已提交
1698
  if (CTG_FLAG_IS_INF_DB(flag)) {
D
dapan1121 已提交
1699 1700 1701
    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 已提交
1702
  } else if (CTG_FLAG_IS_STB(flag)) {
D
dapan 已提交
1703
    ctgDebug("will refresh tbmeta, supposed to be stb, tbName:%s", tNameGetTableName(pTableName));
D
dapan1121 已提交
1704 1705

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

D
dapan1121 已提交
1708
    if (CTG_IS_META_NULL(output->metaType)) {
D
dapan1121 已提交
1709
      CTG_ERR_JRET(ctgGetTableMetaFromVnode(pCtg, pTrans, pMgmtEps, pTableName, &vgroupInfo, output));
D
dapan1121 已提交
1710 1711
    }
  } else {
D
dapan1121 已提交
1712
    ctgDebug("will refresh tbmeta, not supposed to be stb, tbName:%s, flag:%d", tNameGetTableName(pTableName), flag);
D
dapan1121 已提交
1713 1714

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

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

wafwerar's avatar
wafwerar 已提交
1720
      taosMemoryFreeClear(output->tbMeta);
D
dapan1121 已提交
1721
      
D
dapan1121 已提交
1722
      CTG_ERR_JRET(ctgGetTableMetaFromMnodeImpl(pCtg, pTrans, pMgmtEps, output->dbFName, output->tbName, output));
D
dapan1121 已提交
1723
    } else if (CTG_IS_META_BOTH(output->metaType)) {
D
dapan1121 已提交
1724
      int32_t exist = 0;
D
dapan1121 已提交
1725 1726 1727
      if (!CTG_FLAG_IS_FORCE_UPDATE(flag)) {
        CTG_ERR_JRET(ctgIsTableMetaExistInCache(pCtg, output->dbFName, output->tbName, &exist));
      }
H
Haojun Liao 已提交
1728

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

D
dapan1121 已提交
1732
        if (CTG_IS_META_NULL(moutput.metaType)) {
D
dapan1121 已提交
1733
          SET_META_TYPE_NULL(output->metaType);
D
dapan1121 已提交
1734 1735
        }
        
wafwerar's avatar
wafwerar 已提交
1736
        taosMemoryFreeClear(output->tbMeta);
D
dapan1121 已提交
1737
        output->tbMeta = moutput.tbMeta;
D
dapan1121 已提交
1738 1739
        moutput.tbMeta = NULL;
      } else {
wafwerar's avatar
wafwerar 已提交
1740
        taosMemoryFreeClear(output->tbMeta);
D
dapan1121 已提交
1741
        
D
dapan1121 已提交
1742
        SET_META_TYPE_CTABLE(output->metaType); 
D
dapan1121 已提交
1743
      }
D
dapan1121 已提交
1744 1745 1746
    }
  }

D
dapan1121 已提交
1747
  if (CTG_IS_META_NULL(output->metaType)) {
D
dapan 已提交
1748
    ctgError("no tbmeta got, tbNmae:%s", tNameGetTableName(pTableName));
D
dapan1121 已提交
1749
    catalogRemoveTableMeta(pCtg, pTableName);
D
dapan1121 已提交
1750 1751 1752
    CTG_ERR_JRET(CTG_ERR_CODE_TABLE_NOT_EXIST);
  }

D
dapan 已提交
1753 1754 1755 1756 1757 1758
  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 已提交
1759 1760 1761 1762
  if (pOutput) {
    CTG_ERR_JRET(ctgCloneMetaOutput(output, pOutput));
  }

D
dapan1121 已提交
1763
  CTG_ERR_JRET(ctgPushUpdateTblMsgInQueue(pCtg, output, syncReq));
D
dapan 已提交
1764

D
dapan1121 已提交
1765
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1766 1767 1768

_return:

wafwerar's avatar
wafwerar 已提交
1769 1770
  taosMemoryFreeClear(output->tbMeta);
  taosMemoryFreeClear(output);
D
dapan1121 已提交
1771 1772 1773 1774
  
  CTG_RET(code);
}

D
dapan1121 已提交
1775
int32_t ctgGetTableMeta(SCatalog* pCtg, void *pRpc, const SEpSet* pMgmtEps, const SName* pTableName, STableMeta** pTableMeta, int32_t flag) {
D
dapan1121 已提交
1776
  if (NULL == pCtg || NULL == pRpc || NULL == pMgmtEps || NULL == pTableName || NULL == pTableMeta) {
D
dapan1121 已提交
1777 1778 1779
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
  }
  
D
dapan1121 已提交
1780
  bool inCache = false;
D
dapan1121 已提交
1781
  int32_t code = 0;
D
dapan1121 已提交
1782 1783 1784
  uint64_t dbId = 0;
  uint64_t suid = 0;
  STableMetaOutput *output = NULL;
D
dapan1121 已提交
1785

D
dapan1121 已提交
1786
  if (CTG_IS_INF_DBNAME(pTableName->dbname)) {
D
dapan1121 已提交
1787
    CTG_FLAG_SET_INF_DB(flag);
D
dapan1121 已提交
1788 1789
  }

D
dapan1121 已提交
1790
  CTG_ERR_RET(ctgGetTableMetaFromCache(pCtg, pTableName, pTableMeta, &inCache, flag, &dbId));
D
dapan1121 已提交
1791

D
dapan1121 已提交
1792 1793
  int32_t tbType = 0;

D
dapan1121 已提交
1794
  if (inCache) {
D
dapan1121 已提交
1795 1796
    if (CTG_FLAG_MATCH_STB(flag, (*pTableMeta)->tableType) && ((!CTG_FLAG_IS_FORCE_UPDATE(flag)) || (CTG_FLAG_IS_INF_DB(flag)))) {
      goto _return;
D
dapan1121 已提交
1797
    }
D
dapan1121 已提交
1798

D
dapan1121 已提交
1799 1800
    tbType = (*pTableMeta)->tableType;
    suid = (*pTableMeta)->suid;
D
dapan1121 已提交
1801

wafwerar's avatar
wafwerar 已提交
1802
    taosMemoryFreeClear(*pTableMeta);
D
dapan1121 已提交
1803
  }
D
dapan1121 已提交
1804

D
dapan1121 已提交
1805 1806
  if (CTG_FLAG_IS_UNKNOWN_STB(flag)) {
    CTG_FLAG_SET_STB(flag, tbType);
D
dapan1121 已提交
1807 1808
  }

D
dapan1121 已提交
1809

D
dapan 已提交
1810
  while (true) {
D
dapan1121 已提交
1811
    CTG_ERR_JRET(ctgRefreshTblMeta(pCtg, pRpc, pMgmtEps, pTableName, flag, &output, false));
D
dapan1121 已提交
1812

D
dapan 已提交
1813 1814 1815 1816
    if (CTG_IS_META_TABLE(output->metaType)) {
      *pTableMeta = output->tbMeta;
      goto _return;
    }
D
dapan1121 已提交
1817

D
dapan 已提交
1818 1819 1820 1821 1822 1823
    if (CTG_IS_META_BOTH(output->metaType)) {
      memcpy(output->tbMeta, &output->ctbMeta, sizeof(output->ctbMeta));
      
      *pTableMeta = output->tbMeta;
      goto _return;
    }
D
dapan1121 已提交
1824

D
dapan 已提交
1825 1826
    if ((!CTG_IS_META_CTABLE(output->metaType)) || output->tbMeta) {
      ctgError("invalid metaType:%d", output->metaType);
wafwerar's avatar
wafwerar 已提交
1827
      taosMemoryFreeClear(output->tbMeta);
D
dapan 已提交
1828 1829
      CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
    }
D
dapan1121 已提交
1830

D
dapan 已提交
1831
    // HANDLE ONLY CHILD TABLE META
D
dapan1121 已提交
1832

D
dapan 已提交
1833 1834 1835
    SName stbName = *pTableName;
    strcpy(stbName.tname, output->tbName);
    
D
dapan1121 已提交
1836 1837
    CTG_ERR_JRET(ctgGetTableMetaFromCache(pCtg, &stbName, pTableMeta, &inCache, flag, NULL));
    if (!inCache) {
D
dapan 已提交
1838 1839 1840
      ctgDebug("stb no longer exist, dbFName:%s, tbName:%s", output->dbFName, pTableName->tname);
      continue;
    }
D
dapan1121 已提交
1841

D
dapan 已提交
1842 1843 1844 1845
    memcpy(*pTableMeta, &output->ctbMeta, sizeof(output->ctbMeta));

    break;
  }
D
dapan1121 已提交
1846 1847 1848

_return:

D
dapan1121 已提交
1849
  if (CTG_TABLE_NOT_EXIST(code) && inCache) {
D
dapan1121 已提交
1850 1851 1852 1853 1854 1855 1856 1857
    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) {
D
dapan1121 已提交
1858
      ctgPushRmStbMsgInQueue(pCtg, dbFName, dbId, pTableName->tname, suid, false);
D
dapan1121 已提交
1859
    } else {
D
dapan1121 已提交
1860
      ctgPushRmTblMsgInQueue(pCtg, dbFName, dbId, pTableName->tname, false);
D
dapan1121 已提交
1861 1862 1863
    }
  }

wafwerar's avatar
wafwerar 已提交
1864
  taosMemoryFreeClear(output);
D
dapan1121 已提交
1865

D
dapan 已提交
1866 1867
  if (*pTableMeta) {
    ctgDebug("tbmeta returned, tbName:%s, tbType:%d", pTableName->tname, (*pTableMeta)->tableType);
D
dapan1121 已提交
1868
    ctgdShowTableMeta(pCtg, pTableName->tname, *pTableMeta);
D
dapan 已提交
1869 1870
  }

D
dapan1121 已提交
1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883
  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 已提交
1884
  ctgFreeVgInfo(msg->dbInfo);
wafwerar's avatar
wafwerar 已提交
1885
  taosMemoryFreeClear(msg);
D
dapan1121 已提交
1886 1887 1888 1889 1890 1891 1892 1893 1894 1895
  
  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 已提交
1896
  ctgGetDBCache(msg->pCtg, msg->dbFName, &dbCache);
D
dapan1121 已提交
1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909
  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:

wafwerar's avatar
wafwerar 已提交
1910
  taosMemoryFreeClear(msg);
D
dapan1121 已提交
1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924
  
  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 已提交
1925
    CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
D
dapan1121 已提交
1926 1927 1928 1929 1930 1931
  }

  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 已提交
1932
  
D
dapan1121 已提交
1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950
  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 已提交
1951
  if (output) {
wafwerar's avatar
wafwerar 已提交
1952 1953
    taosMemoryFreeClear(output->tbMeta);
    taosMemoryFreeClear(output);
D
dapan1121 已提交
1954
  }
D
dapan 已提交
1955
  
wafwerar's avatar
wafwerar 已提交
1956
  taosMemoryFreeClear(msg);
D
dapan1121 已提交
1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972
  
  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;
  }

D
dapan 已提交
1973
  if (msg->dbId && (dbCache->dbId != msg->dbId)) {
D
dapan1121 已提交
1974 1975 1976 1977 1978 1979 1980
    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))) {
    ctgDebug("stb not exist in stbCache, may be removed, dbFName:%s, stb:%s, suid:%"PRIx64, msg->dbFName, msg->stbName, msg->suid);
D
dapan1121 已提交
1981 1982
  } else {
    CTG_CACHE_STAT_SUB(stblNum, 1);
D
dapan1121 已提交
1983 1984 1985 1986 1987
  }

  CTG_LOCK(CTG_READ, &dbCache->tbCache.metaLock);
  if (taosHashRemove(dbCache->tbCache.metaCache, msg->stbName, strlen(msg->stbName))) {  
    ctgError("stb not exist in cache, dbFName:%s, stb:%s, suid:%"PRIx64, msg->dbFName, msg->stbName, msg->suid);
D
dapan1121 已提交
1988 1989 1990
  } else {
    CTG_CACHE_STAT_SUB(tblNum, 1);
  }
D
dapan1121 已提交
1991 1992 1993 1994 1995 1996
  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 已提交
1997
  CTG_ERR_JRET(ctgMetaRentRemove(&msg->pCtg->stbRent, msg->suid, ctgStbVersionSortCompare, ctgStbVersionSearchCompare));
D
dapan1121 已提交
1998 1999 2000 2001 2002
  
  ctgDebug("stb removed from rent, dbFName:%s, stbName:%s, suid:%"PRIx64, msg->dbFName, msg->stbName, msg->suid);
  
_return:

wafwerar's avatar
wafwerar 已提交
2003
  taosMemoryFreeClear(msg);
D
dapan1121 已提交
2004 2005 2006 2007 2008
  
  CTG_RET(code);
}

int32_t ctgActRemoveTbl(SCtgMetaAction *action) {
D
dapan1121 已提交
2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022
  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 已提交
2023

D
dapan1121 已提交
2024
  CTG_LOCK(CTG_READ, &dbCache->tbCache.metaLock);
H
Haojun Liao 已提交
2025
  if (taosHashRemove(dbCache->tbCache.metaCache, msg->tbName, strlen(msg->tbName))) {
D
dapan1121 已提交
2026 2027 2028
    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);
D
dapan1121 已提交
2029 2030
  } else {
    CTG_CACHE_STAT_SUB(tblNum, 1);
H
Haojun Liao 已提交
2031
  }
D
dapan1121 已提交
2032
  CTG_UNLOCK(CTG_READ, &dbCache->tbCache.metaLock);
D
dapan1121 已提交
2033

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

D
dapan1121 已提交
2036
_return:
D
dapan1121 已提交
2037

wafwerar's avatar
wafwerar 已提交
2038
  taosMemoryFreeClear(msg);
H
Haojun Liao 已提交
2039

D
dapan1121 已提交
2040
  CTG_RET(code);
D
dapan1121 已提交
2041 2042 2043 2044 2045 2046 2047 2048
}


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

  qInfo("catalog update thread started");

D
dapan 已提交
2049
  CTG_LOCK(CTG_READ, &gCtgMgmt.lock);
D
dapan1121 已提交
2050 2051
  
  while (true) {
D
dapan1121 已提交
2052 2053 2054
    if (tsem_wait(&gCtgMgmt.queue.reqSem)) {
      qError("ctg tsem_wait failed, error:%s", tstrerror(TAOS_SYSTEM_ERROR(errno)));
    }
D
dapan1121 已提交
2055
    
wafwerar's avatar
wafwerar 已提交
2056
    if (atomic_load_8((int8_t*)&gCtgMgmt.exit)) {
D
dapan1121 已提交
2057
      tsem_post(&gCtgMgmt.queue.rspSem);
D
dapan1121 已提交
2058 2059 2060 2061 2062
      break;
    }

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

D
dapan1121 已提交
2065
    ctgDebug("process [%s] action", gCtgAction[action->act].name);
D
dapan 已提交
2066 2067 2068
    
    (*gCtgAction[action->act].func)(action);

D
dapan1121 已提交
2069 2070 2071 2072 2073 2074
    gCtgMgmt.queue.seqDone = action->seqId;

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

D
dapan1121 已提交
2075
    CTG_RUNTIME_STAT_ADD(qDoneNum, 1); 
D
dapan1121 已提交
2076

D
dapan1121 已提交
2077
    ctgdShowClusterCache(pCtg);
D
dapan1121 已提交
2078 2079
  }

D
dapan 已提交
2080
  CTG_UNLOCK(CTG_READ, &gCtgMgmt.lock);
D
dapan1121 已提交
2081 2082 2083 2084 2085 2086 2087 2088

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


int32_t ctgStartUpdateThread() {
wafwerar's avatar
wafwerar 已提交
2089 2090 2091
  TdThreadAttr thAttr;
  taosThreadAttrInit(&thAttr);
  taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
D
dapan1121 已提交
2092

wafwerar's avatar
wafwerar 已提交
2093
  if (taosThreadCreate(&gCtgMgmt.updateThread, &thAttr, ctgUpdateThreadFunc, NULL) != 0) {
D
dapan1121 已提交
2094 2095
    terrno = TAOS_SYSTEM_ERROR(errno);
    CTG_ERR_RET(terrno);
D
dapan1121 已提交
2096 2097
  }
  
wafwerar's avatar
wafwerar 已提交
2098
  taosThreadAttrDestroy(&thAttr);
D
dapan1121 已提交
2099 2100 2101
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117
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 已提交
2118
  CTG_ERR_JRET(ctgGetDBVgInfo(pCtg, pRpc, pMgmtEps, db, &dbCache, &vgInfo));
D
dapan1121 已提交
2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162

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

wafwerar's avatar
wafwerar 已提交
2163
  taosMemoryFreeClear(tbMeta);
D
dapan1121 已提交
2164 2165 2166

  if (vgInfo) {
    taosHashCleanup(vgInfo->vgHash);
wafwerar's avatar
wafwerar 已提交
2167
    taosMemoryFreeClear(vgInfo);
D
dapan1121 已提交
2168 2169 2170 2171 2172 2173 2174 2175 2176 2177
  }

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

  CTG_RET(code);
}

D
dapan1121 已提交
2178
int32_t catalogInit(SCatalogCfg *cfg) {
D
dapan 已提交
2179
  if (gCtgMgmt.pCluster) {
D
dapan 已提交
2180
    qError("catalog already initialized");
D
dapan1121 已提交
2181
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
D
dapan1121 已提交
2182 2183
  }

wafwerar's avatar
wafwerar 已提交
2184
  atomic_store_8((int8_t*)&gCtgMgmt.exit, false);
D
dapan1121 已提交
2185

D
dapan1121 已提交
2186
  if (cfg) {
D
dapan 已提交
2187
    memcpy(&gCtgMgmt.cfg, cfg, sizeof(*cfg));
H
Haojun Liao 已提交
2188

D
dapan 已提交
2189 2190
    if (gCtgMgmt.cfg.maxDBCacheNum == 0) {
      gCtgMgmt.cfg.maxDBCacheNum = CTG_DEFAULT_CACHE_DB_NUMBER;
D
dapan1121 已提交
2191 2192
    }

D
dapan 已提交
2193 2194
    if (gCtgMgmt.cfg.maxTblCacheNum == 0) {
      gCtgMgmt.cfg.maxTblCacheNum = CTG_DEFAULT_CACHE_TBLMETA_NUMBER;
D
dapan1121 已提交
2195
    }
D
dapan1121 已提交
2196

D
dapan 已提交
2197 2198
    if (gCtgMgmt.cfg.dbRentSec == 0) {
      gCtgMgmt.cfg.dbRentSec = CTG_DEFAULT_RENT_SECOND;
D
dapan1121 已提交
2199 2200
    }

D
dapan 已提交
2201 2202
    if (gCtgMgmt.cfg.stbRentSec == 0) {
      gCtgMgmt.cfg.stbRentSec = CTG_DEFAULT_RENT_SECOND;
D
dapan1121 已提交
2203
    }
D
dapan1121 已提交
2204
  } else {
D
dapan 已提交
2205 2206 2207 2208
    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 已提交
2209 2210
  }

D
dapan 已提交
2211 2212
  gCtgMgmt.pCluster = taosHashInit(CTG_DEFAULT_CACHE_CLUSTER_NUMBER, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
  if (NULL == gCtgMgmt.pCluster) {
D
dapan1121 已提交
2213 2214
    qError("taosHashInit %d cluster cache failed", CTG_DEFAULT_CACHE_CLUSTER_NUMBER);
    CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
D
dapan1121 已提交
2215 2216
  }

D
dapan1121 已提交
2217 2218 2219 2220 2221 2222 2223 2224 2225
  if (tsem_init(&gCtgMgmt.queue.reqSem, 0, 0)) {
    qError("tsem_init failed, error:%s", tstrerror(TAOS_SYSTEM_ERROR(errno)));
    CTG_ERR_RET(TSDB_CODE_CTG_SYS_ERROR);
  }
  
  if (tsem_init(&gCtgMgmt.queue.rspSem, 0, 0)) {
    qError("tsem_init failed, error:%s", tstrerror(TAOS_SYSTEM_ERROR(errno)));
    CTG_ERR_RET(TSDB_CODE_CTG_SYS_ERROR);
  }
D
dapan1121 已提交
2226

wafwerar's avatar
wafwerar 已提交
2227
  gCtgMgmt.queue.head = taosMemoryCalloc(1, sizeof(SCtgQNode));
D
dapan1121 已提交
2228
  if (NULL == gCtgMgmt.queue.head) {
D
dapan1121 已提交
2229 2230 2231
    qError("calloc %d failed", (int32_t)sizeof(SCtgQNode));
    CTG_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }
D
dapan1121 已提交
2232
  gCtgMgmt.queue.tail = gCtgMgmt.queue.head;
D
dapan1121 已提交
2233

D
dapan1121 已提交
2234 2235
  CTG_ERR_RET(ctgStartUpdateThread());

D
dapan 已提交
2236
  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 已提交
2237

D
dapan 已提交
2238
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
2239 2240
}

D
dapan1121 已提交
2241
int32_t catalogGetHandle(uint64_t clusterId, SCatalog** catalogHandle) {
2242
  if (NULL == catalogHandle) {
D
dapan1121 已提交
2243
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
D
dapan 已提交
2244 2245
  }

D
dapan 已提交
2246
  if (NULL == gCtgMgmt.pCluster) {
D
dapan 已提交
2247
    qError("catalog cluster cache are not ready, clusterId:%"PRIx64, clusterId);
D
dapan1121 已提交
2248
    CTG_ERR_RET(TSDB_CODE_CTG_NOT_READY);
D
dapan 已提交
2249 2250
  }

D
dapan1121 已提交
2251 2252
  int32_t code = 0;
  SCatalog *clusterCtg = NULL;
D
dapan 已提交
2253

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

D
dapan1121 已提交
2257 2258 2259 2260 2261
    if (ctg && (*ctg)) {
      *catalogHandle = *ctg;
      qDebug("got catalog handle from cache, clusterId:%"PRIx64", CTG:%p", clusterId, *ctg);
      return TSDB_CODE_SUCCESS;
    }
D
dapan 已提交
2262

wafwerar's avatar
wafwerar 已提交
2263
    clusterCtg = taosMemoryCalloc(1, sizeof(SCatalog));
D
dapan1121 已提交
2264 2265 2266 2267 2268
    if (NULL == clusterCtg) {
      qError("calloc %d failed", (int32_t)sizeof(SCatalog));
      CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
    }

D
dapan1121 已提交
2269 2270
    clusterCtg->clusterId = clusterId;

D
dapan 已提交
2271 2272
    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 已提交
2273

D
dapan 已提交
2274
    clusterCtg->dbCache = taosHashInit(gCtgMgmt.cfg.maxDBCacheNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
D
dapan1121 已提交
2275 2276 2277 2278 2279
    if (NULL == clusterCtg->dbCache) {
      qError("taosHashInit %d dbCache failed", CTG_DEFAULT_CACHE_DB_NUMBER);
      CTG_ERR_JRET(TSDB_CODE_CTG_MEM_ERROR);
    }

D
dapan 已提交
2280
    SHashObj *metaCache = taosHashInit(gCtgMgmt.cfg.maxTblCacheNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
D
dapan1121 已提交
2281
    if (NULL == metaCache) {
D
dapan 已提交
2282
      qError("taosHashInit failed, num:%d", gCtgMgmt.cfg.maxTblCacheNum);
D
dapan1121 已提交
2283 2284 2285
      CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
    }
    
D
dapan 已提交
2286
    code = taosHashPut(gCtgMgmt.pCluster, &clusterId, sizeof(clusterId), &clusterCtg, POINTER_BYTES);
D
dapan1121 已提交
2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299
    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 已提交
2300
  }
D
dapan1121 已提交
2301 2302

  *catalogHandle = clusterCtg;
D
dapan1121 已提交
2303 2304

  CTG_CACHE_STAT_ADD(clusterNum, 1);
D
dapan 已提交
2305
  
D
dapan1121 已提交
2306
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
2307 2308 2309 2310 2311 2312 2313 2314

_return:

  ctgFreeHandle(clusterCtg);
  
  CTG_RET(code);
}

D
dapan1121 已提交
2315 2316
void catalogFreeHandle(SCatalog* pCtg) {
  if (NULL == pCtg) {
D
dapan1121 已提交
2317 2318
    return;
  }
D
dapan1121 已提交
2319

D
dapan 已提交
2320
  if (taosHashRemove(gCtgMgmt.pCluster, &pCtg->clusterId, sizeof(pCtg->clusterId))) {
D
dapan1121 已提交
2321
    ctgWarn("taosHashRemove from cluster failed, may already be freed, clusterId:%"PRIx64, pCtg->clusterId);
D
dapan1121 已提交
2322 2323 2324
    return;
  }

D
dapan1121 已提交
2325 2326
  CTG_CACHE_STAT_SUB(clusterNum, 1);

D
dapan1121 已提交
2327
  uint64_t clusterId = pCtg->clusterId;
D
dapan1121 已提交
2328
  
D
dapan1121 已提交
2329
  ctgFreeHandle(pCtg);
D
dapan1121 已提交
2330
  
D
dapan1121 已提交
2331
  ctgInfo("handle freed, culsterId:%"PRIx64, clusterId);
D
dapan 已提交
2332 2333
}

D
dapan1121 已提交
2334
int32_t catalogGetDBVgVersion(SCatalog* pCtg, const char* dbFName, int32_t* version, int64_t* dbId, int32_t *tableNum) {
D
dapan1121 已提交
2335 2336
  CTG_API_ENTER();

D
dapan1121 已提交
2337
  if (NULL == pCtg || NULL == dbFName || NULL == version || NULL == dbId) {
D
dapan1121 已提交
2338 2339 2340 2341
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

  SCtgDBCache *dbCache = NULL;
D
dapan 已提交
2342
  bool inCache = false;
D
dapan1121 已提交
2343
  int32_t code = 0;
D
dapan1121 已提交
2344

D
dapan1121 已提交
2345 2346
  CTG_ERR_JRET(ctgAcquireVgInfoFromCache(pCtg, dbFName, &dbCache, &inCache));
  if (!inCache) {
D
dapan1121 已提交
2347
    *version = CTG_DEFAULT_INVALID_VERSION;
D
dapan1121 已提交
2348
    CTG_API_LEAVE(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
2349 2350
  }

D
dapan1121 已提交
2351
  *version = dbCache->vgInfo->vgVersion;
D
dapan1121 已提交
2352
  *dbId = dbCache->dbId;
D
dapan1121 已提交
2353
  *tableNum = dbCache->vgInfo->numOfTable;
D
dapan1121 已提交
2354 2355 2356

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

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

D
dapan1121 已提交
2360
  CTG_API_LEAVE(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
2361 2362 2363 2364

_return:

  CTG_API_LEAVE(code);
D
dapan1121 已提交
2365 2366
}

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

D
dapan1121 已提交
2370 2371 2372
  if (NULL == pCtg || NULL == dbFName || NULL == pRpc || NULL == pMgmtEps || NULL == vgroupList) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }
2373

D
dapan1121 已提交
2374
  SCtgDBCache* dbCache = NULL;
2375
  int32_t code = 0;
D
dapan1121 已提交
2376
  SArray *vgList = NULL;
D
dapan1121 已提交
2377 2378
  SHashObj *vgHash = NULL;
  SDBVgInfo *vgInfo = NULL;
D
dapan1121 已提交
2379
  CTG_ERR_JRET(ctgGetDBVgInfo(pCtg, pRpc, pMgmtEps, dbFName, &dbCache, &vgInfo));
D
dapan1121 已提交
2380 2381 2382 2383
  if (dbCache) {
    vgHash = dbCache->vgInfo->vgHash;
  } else {
    vgHash = vgInfo->vgHash;
D
dapan1121 已提交
2384 2385
  }

D
dapan1121 已提交
2386
  CTG_ERR_JRET(ctgGenerateVgList(pCtg, vgHash, &vgList));
D
dapan1121 已提交
2387 2388 2389 2390 2391

  *vgroupList = vgList;
  vgList = NULL;

_return:
D
dapan1121 已提交
2392 2393

  if (dbCache) {
D
dapan1121 已提交
2394 2395
    ctgReleaseVgInfo(dbCache);
    ctgReleaseDBCache(pCtg, dbCache);
D
dapan1121 已提交
2396 2397
  }

D
dapan1121 已提交
2398 2399
  if (vgInfo) {
    taosHashCleanup(vgInfo->vgHash);
wafwerar's avatar
wafwerar 已提交
2400
    taosMemoryFreeClear(vgInfo);
D
dapan1121 已提交
2401 2402
  }

D
dapan1121 已提交
2403
  CTG_API_LEAVE(code);  
D
dapan1121 已提交
2404 2405 2406
}


D
dapan1121 已提交
2407
int32_t catalogUpdateDBVgInfo(SCatalog* pCtg, const char* dbFName, uint64_t dbId, SDBVgInfo* dbInfo) {
D
dapan1121 已提交
2408
  CTG_API_ENTER();
D
dapan1121 已提交
2409 2410

  int32_t code = 0;
D
dapan1121 已提交
2411
  
D
dapan1121 已提交
2412
  if (NULL == pCtg || NULL == dbFName || NULL == dbInfo) {
D
dapan1121 已提交
2413
    ctgFreeVgInfo(dbInfo);
D
dapan1121 已提交
2414 2415 2416
    CTG_ERR_JRET(TSDB_CODE_CTG_INVALID_INPUT);
  }

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

D
dapan1121 已提交
2419 2420
_return:

D
dapan1121 已提交
2421
  CTG_API_LEAVE(code);
D
dapan1121 已提交
2422 2423 2424
}


D
dapan1121 已提交
2425 2426 2427
int32_t catalogRemoveDB(SCatalog* pCtg, const char* dbFName, uint64_t dbId) {
  CTG_API_ENTER();

D
dapan1121 已提交
2428 2429
  int32_t code = 0;
  
D
dapan1121 已提交
2430 2431
  if (NULL == pCtg || NULL == dbFName) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
D
dapan1121 已提交
2432 2433
  }

D
dapan1121 已提交
2434
  if (NULL == pCtg->dbCache) {
D
dapan1121 已提交
2435
    CTG_API_LEAVE(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
2436
  }
D
dapan1121 已提交
2437

D
dapan1121 已提交
2438
  CTG_ERR_JRET(ctgPushRmDBMsgInQueue(pCtg, dbFName, dbId));
D
dapan 已提交
2439

D
dapan1121 已提交
2440
  CTG_API_LEAVE(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
2441
  
D
dapan1121 已提交
2442 2443
_return:

D
dapan1121 已提交
2444
  CTG_API_LEAVE(code);
D
dapan1121 已提交
2445 2446
}

D
dapan1121 已提交
2447 2448 2449
int32_t catalogUpdateVgEpSet(SCatalog* pCtg, const char* dbFName, int32_t vgId, SEpSet *epSet) {

}
D
dapan1121 已提交
2450

D
dapan1121 已提交
2451
int32_t catalogRemoveTableMeta(SCatalog* pCtg, const SName* pTableName) {
D
dapan 已提交
2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464
  CTG_API_ENTER();

  int32_t code = 0;
  
  if (NULL == pCtg || NULL == pTableName) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

  if (NULL == pCtg->dbCache) {
    CTG_API_LEAVE(TSDB_CODE_SUCCESS);
  }

  STableMeta *tblMeta = NULL;
D
dapan1121 已提交
2465
  bool inCache = false;
D
dapan 已提交
2466
  uint64_t dbId = 0;
D
dapan1121 已提交
2467
  CTG_ERR_JRET(ctgGetTableMetaFromCache(pCtg, pTableName, &tblMeta, &inCache, 0, &dbId));
D
dapan 已提交
2468

D
dapan1121 已提交
2469
  if (!inCache) {
D
dapan 已提交
2470 2471 2472 2473 2474 2475 2476 2477
    ctgDebug("table already not in cache, db:%s, tblName:%s", pTableName->dbname, pTableName->tname);
    goto _return;
  }

  char dbFName[TSDB_DB_FNAME_LEN];
  tNameGetFullDbName(pTableName, dbFName);
  
  if (TSDB_SUPER_TABLE == tblMeta->tableType) {
D
dapan1121 已提交
2478
    CTG_ERR_JRET(ctgPushRmStbMsgInQueue(pCtg, dbFName, dbId, pTableName->tname, tblMeta->suid, true));
D
dapan 已提交
2479
  } else {
D
dapan1121 已提交
2480
    CTG_ERR_JRET(ctgPushRmTblMsgInQueue(pCtg, dbFName, dbId, pTableName->tname, true));
D
dapan 已提交
2481 2482 2483 2484 2485
  }

 
_return:

wafwerar's avatar
wafwerar 已提交
2486
  taosMemoryFreeClear(tblMeta);
D
dapan 已提交
2487 2488 2489 2490 2491

  CTG_API_LEAVE(code);
}


D
dapan1121 已提交
2492 2493 2494
int32_t catalogRemoveStbMeta(SCatalog* pCtg, const char* dbFName, uint64_t dbId, const char* stbName, uint64_t suid) {
  CTG_API_ENTER();

D
dapan 已提交
2495 2496
  int32_t code = 0;
  
D
dapan1121 已提交
2497 2498
  if (NULL == pCtg || NULL == dbFName || NULL == stbName) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
D
dapan 已提交
2499 2500
  }

D
dapan1121 已提交
2501
  if (NULL == pCtg->dbCache) {
D
dapan1121 已提交
2502
    CTG_API_LEAVE(TSDB_CODE_SUCCESS);
D
dapan 已提交
2503
  }
D
dapan1121 已提交
2504

D
dapan1121 已提交
2505
  CTG_ERR_JRET(ctgPushRmStbMsgInQueue(pCtg, dbFName, dbId, stbName, suid, true));
D
dapan 已提交
2506

D
dapan1121 已提交
2507
  CTG_API_LEAVE(TSDB_CODE_SUCCESS);
D
dapan 已提交
2508
  
D
dapan1121 已提交
2509 2510
_return:

D
dapan1121 已提交
2511
  CTG_API_LEAVE(code);
D
dapan 已提交
2512 2513
}

D
dapan1121 已提交
2514 2515 2516
int32_t catalogGetIndexMeta(SCatalog* pCtg, void *pTrans, const SEpSet* pMgmtEps, const SName* pTableName, const char *pIndexName, SIndexMeta** pIndexMeta) {

}
D
dapan1121 已提交
2517

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

D
dapan1121 已提交
2521
  CTG_API_LEAVE(ctgGetTableMeta(pCtg, pTrans, pMgmtEps, pTableName, pTableMeta, CTG_FLAG_UNKNOWN_STB));
D
dapan1121 已提交
2522
}
D
dapan1121 已提交
2523

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

D
dapan1121 已提交
2527
  CTG_API_LEAVE(ctgGetTableMeta(pCtg, pTrans, pMgmtEps, pTableName, pTableMeta, CTG_FLAG_STB));
D
dapan1121 已提交
2528 2529
}

D
dapan1121 已提交
2530 2531 2532 2533 2534 2535 2536
int32_t catalogUpdateSTableMeta(SCatalog* pCtg, STableMetaRsp *rspMsg) {
  CTG_API_ENTER();

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

wafwerar's avatar
wafwerar 已提交
2537
  STableMetaOutput *output = taosMemoryCalloc(1, sizeof(STableMetaOutput));
D
dapan1121 已提交
2538 2539 2540 2541 2542
  if (NULL == output) {
    ctgError("malloc %d failed", (int32_t)sizeof(STableMetaOutput));
    CTG_API_LEAVE(TSDB_CODE_CTG_MEM_ERROR);
  }
  
D
dapan1121 已提交
2543 2544
  int32_t code = 0;

D
dapan1121 已提交
2545 2546
  strcpy(output->dbFName, rspMsg->dbFName);
  strcpy(output->tbName, rspMsg->tbName);
D
dapan1121 已提交
2547

D
dapan1121 已提交
2548
  output->dbId = rspMsg->dbId;
D
dapan1121 已提交
2549
  
D
dapan1121 已提交
2550
  SET_META_TYPE_TABLE(output->metaType);
D
dapan1121 已提交
2551
  
D
dapan1121 已提交
2552
  CTG_ERR_JRET(queryCreateTableMetaFromMsg(rspMsg, true, &output->tbMeta));
D
dapan1121 已提交
2553

D
dapan1121 已提交
2554
  CTG_ERR_JRET(ctgPushUpdateTblMsgInQueue(pCtg, output, false));
D
dapan 已提交
2555

D
dapan1121 已提交
2556 2557
  CTG_API_LEAVE(code);
  
D
dapan1121 已提交
2558 2559
_return:

wafwerar's avatar
wafwerar 已提交
2560 2561
  taosMemoryFreeClear(output->tbMeta);
  taosMemoryFreeClear(output);
D
dapan1121 已提交
2562
  
D
dapan1121 已提交
2563
  CTG_API_LEAVE(code);
D
dapan1121 已提交
2564 2565
}

D
dapan1121 已提交
2566 2567 2568 2569 2570 2571 2572 2573 2574
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 已提交
2575

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

D
dapan1121 已提交
2579
  if (NULL == pCtg || NULL == pTrans || NULL == pMgmtEps || NULL == pTableName) {
D
dapan1121 已提交
2580 2581 2582
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

D
dapan1121 已提交
2583
  CTG_API_LEAVE(ctgRefreshTblMeta(pCtg, pTrans, pMgmtEps, pTableName, CTG_FLAG_FORCE_UPDATE | CTG_FLAG_MAKE_STB(isSTable), NULL, true));
2584
}
2585

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

D
dapan1121 已提交
2589
  CTG_API_LEAVE(ctgGetTableMeta(pCtg, pTrans, pMgmtEps, pTableName, pTableMeta, CTG_FLAG_FORCE_UPDATE | CTG_FLAG_MAKE_STB(isSTable)));
D
dapan1121 已提交
2590 2591
}

D
dapan1121 已提交
2592
int32_t catalogGetTableDistVgInfo(SCatalog* pCtg, void *pRpc, const SEpSet* pMgmtEps, const SName* pTableName, SArray** pVgList) {
D
dapan1121 已提交
2593
  CTG_API_ENTER();
D
dapan1121 已提交
2594 2595 2596 2597

  if (NULL == pCtg || NULL == pRpc || NULL == pMgmtEps || NULL == pTableName || NULL == pVgList) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }
D
dapan1121 已提交
2598 2599 2600 2601 2602

  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 已提交
2603

D
dapan1121 已提交
2604
  int32_t code = 0;
D
dapan1121 已提交
2605

D
dapan1121 已提交
2606 2607 2608 2609 2610
  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 已提交
2611

D
dapan1121 已提交
2612 2613 2614 2615 2616 2617
        char dbFName[TSDB_DB_FNAME_LEN] = {0};
        tNameGetFullDbName(pTableName, dbFName);        
        CTG_ERR_JRET(ctgRefreshDBVgInfo(pCtg, pRpc, pMgmtEps, dbFName));
        
        continue;
      }
D
dapan1121 已提交
2618
    }
D
dapan 已提交
2619

D
dapan1121 已提交
2620
    break;
D
dapan1121 已提交
2621
  }
D
dapan 已提交
2622

D
dapan1121 已提交
2623
_return:
D
dapan 已提交
2624

D
dapan1121 已提交
2625
  CTG_API_LEAVE(code);
D
dapan1121 已提交
2626 2627 2628
}


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

D
dapan1121 已提交
2632 2633 2634 2635 2636
  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 已提交
2637 2638
  SCtgDBCache* dbCache = NULL;
  int32_t code = 0;
H
Haojun Liao 已提交
2639 2640
  char db[TSDB_DB_FNAME_LEN] = {0};
  tNameGetFullDbName(pTableName, db);
D
dapan1121 已提交
2641

D
dapan1121 已提交
2642
  SDBVgInfo *vgInfo = NULL;
D
dapan1121 已提交
2643
  CTG_ERR_JRET(ctgGetDBVgInfo(pCtg, pTrans, pMgmtEps, db, &dbCache, &vgInfo));
D
dapan1121 已提交
2644

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

D
dapan1121 已提交
2647
_return:
D
dapan1121 已提交
2648

D
dapan1121 已提交
2649
  if (dbCache) {
D
dapan1121 已提交
2650 2651 2652 2653 2654 2655
    ctgReleaseVgInfo(dbCache);
    ctgReleaseDBCache(pCtg, dbCache);
  }

  if (vgInfo) {
    taosHashCleanup(vgInfo->vgHash);
wafwerar's avatar
wafwerar 已提交
2656
    taosMemoryFreeClear(vgInfo);
D
dapan1121 已提交
2657
  }
D
dapan1121 已提交
2658

D
dapan1121 已提交
2659
  CTG_API_LEAVE(code);
D
dapan1121 已提交
2660 2661 2662
}


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

D
dapan1121 已提交
2666
  if (NULL == pCtg || NULL == pTrans || NULL == pMgmtEps || NULL == pReq || NULL == pRsp) {
D
dapan1121 已提交
2667 2668 2669
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

D
dapan1121 已提交
2670
  int32_t code = 0;
D
dapan1121 已提交
2671
  pRsp->pTableMeta = NULL;
D
dapan1121 已提交
2672 2673 2674

  if (pReq->pTableName) {
    int32_t tbNum = (int32_t)taosArrayGetSize(pReq->pTableName);
D
dapan1121 已提交
2675
    if (tbNum <= 0) {
D
dapan1121 已提交
2676
      ctgError("empty table name list, tbNum:%d", tbNum);
D
dapan1121 已提交
2677
      CTG_ERR_JRET(TSDB_CODE_CTG_INVALID_INPUT);
D
dapan1121 已提交
2678
    }
H
Haojun Liao 已提交
2679

D
dapan1121 已提交
2680 2681
    pRsp->pTableMeta = taosArrayInit(tbNum, POINTER_BYTES);
    if (NULL == pRsp->pTableMeta) {
D
dapan1121 已提交
2682
      ctgError("taosArrayInit %d failed", tbNum);
D
dapan1121 已提交
2683
      CTG_ERR_JRET(TSDB_CODE_CTG_MEM_ERROR);
D
dapan1121 已提交
2684 2685 2686 2687 2688 2689
    }
    
    for (int32_t i = 0; i < tbNum; ++i) {
      SName *name = taosArrayGet(pReq->pTableName, i);
      STableMeta *pTableMeta = NULL;
      
D
dapan1121 已提交
2690
      CTG_ERR_JRET(ctgGetTableMeta(pCtg, pTrans, pMgmtEps, name, &pTableMeta, CTG_FLAG_UNKNOWN_STB));
D
dapan1121 已提交
2691 2692 2693

      if (NULL == taosArrayPush(pRsp->pTableMeta, &pTableMeta)) {
        ctgError("taosArrayPush failed, idx:%d", i);
wafwerar's avatar
wafwerar 已提交
2694
        taosMemoryFreeClear(pTableMeta);
D
dapan1121 已提交
2695 2696 2697 2698 2699
        CTG_ERR_JRET(TSDB_CODE_CTG_MEM_ERROR);
      }
    }
  }

D
dapan1121 已提交
2700 2701 2702 2703
  if (pReq->qNodeRequired) {
    CTG_ERR_JRET(ctgGetQnodeListFromMnode(pCtg, pTrans, pMgmtEps, &pRsp->pEpSetList));
  }

D
dapan1121 已提交
2704
  CTG_API_LEAVE(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
2705 2706

_return:  
D
dapan1121 已提交
2707

D
dapan1121 已提交
2708 2709 2710 2711
  if (pRsp->pTableMeta) {
    int32_t aSize = taosArrayGetSize(pRsp->pTableMeta);
    for (int32_t i = 0; i < aSize; ++i) {
      STableMeta *pMeta = taosArrayGetP(pRsp->pTableMeta, i);
wafwerar's avatar
wafwerar 已提交
2712
      taosMemoryFreeClear(pMeta);
D
dapan1121 已提交
2713 2714 2715
    }
    
    taosArrayDestroy(pRsp->pTableMeta);
D
dapan1121 已提交
2716
    pRsp->pTableMeta = NULL;
D
dapan1121 已提交
2717
  }
D
dapan 已提交
2718
  
D
dapan1121 已提交
2719
  CTG_API_LEAVE(code);
2720
}
D
dapan 已提交
2721

D
dapan1121 已提交
2722
int32_t catalogGetQnodeList(SCatalog* pCtg, void *pRpc, const SEpSet* pMgmtEps, SArray* pQnodeList) {
D
dapan1121 已提交
2723
  CTG_API_ENTER();
D
dapan1121 已提交
2724 2725
  
  int32_t code = 0;
D
dapan1121 已提交
2726 2727 2728 2729
  if (NULL == pCtg || NULL == pRpc  || NULL == pMgmtEps || NULL == pQnodeList) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

D
dapan1121 已提交
2730 2731 2732
  CTG_ERR_JRET(ctgGetQnodeListFromMnode(pCtg, pRpc, pMgmtEps, &pQnodeList));

_return:
D
dapan 已提交
2733

D
dapan1121 已提交
2734
  CTG_API_LEAVE(TSDB_CODE_SUCCESS);
D
dapan 已提交
2735 2736
}

D
dapan1121 已提交
2737
int32_t catalogGetExpiredSTables(SCatalog* pCtg, SSTableMetaVersion **stables, uint32_t *num) {
D
dapan1121 已提交
2738 2739
  CTG_API_ENTER();

D
dapan1121 已提交
2740 2741
  if (NULL == pCtg || NULL == stables || NULL == num) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
D
dapan1121 已提交
2742 2743
  }

D
dapan1121 已提交
2744 2745 2746 2747
  CTG_API_LEAVE(ctgMetaRentGet(&pCtg->stbRent, (void **)stables, num, sizeof(SSTableMetaVersion)));
}

int32_t catalogGetExpiredDBs(SCatalog* pCtg, SDbVgVersion **dbs, uint32_t *num) {
D
dapan1121 已提交
2748
  CTG_API_ENTER();
D
dapan1121 已提交
2749 2750 2751 2752
  
  if (NULL == pCtg || NULL == dbs || NULL == num) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }
D
dapan1121 已提交
2753

D
dapan1121 已提交
2754
  CTG_API_LEAVE(ctgMetaRentGet(&pCtg->dbRent, (void **)dbs, num, sizeof(SDbVgVersion)));
D
dapan1121 已提交
2755 2756
}

D
dapan1121 已提交
2757 2758 2759 2760 2761 2762 2763 2764 2765
int32_t catalogGetDBCfg(SCatalog* pCtg, void *pRpc, const SEpSet* pMgmtEps, const char* dbFName, SDbCfgInfo* pDbCfg) {
  CTG_API_ENTER();
  
  if (NULL == pCtg || NULL == pRpc || NULL == pMgmtEps || NULL == dbFName || NULL == pDbCfg) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

  CTG_API_LEAVE(ctgGetDBCfgFromMnode(pCtg, pRpc, pMgmtEps, dbFName, pDbCfg));
}
D
dapan 已提交
2766

D
dapan 已提交
2767
void catalogDestroy(void) {
D
dapan1121 已提交
2768 2769
  qInfo("start to destroy catalog");
  
wafwerar's avatar
wafwerar 已提交
2770
  if (NULL == gCtgMgmt.pCluster || atomic_load_8((int8_t*)&gCtgMgmt.exit)) {
D
dapan1121 已提交
2771 2772 2773
    return;
  }

wafwerar's avatar
wafwerar 已提交
2774
  atomic_store_8((int8_t*)&gCtgMgmt.exit, true);
D
dapan 已提交
2775

D
dapan1121 已提交
2776 2777 2778 2779 2780 2781 2782
  if (tsem_post(&gCtgMgmt.queue.reqSem)) {
    qError("tsem_post failed, error:%s", tstrerror(TAOS_SYSTEM_ERROR(errno)));
  }
  
  if (tsem_post(&gCtgMgmt.queue.rspSem)) {
    qError("tsem_post failed, error:%s", tstrerror(TAOS_SYSTEM_ERROR(errno)));
  }
D
dapan1121 已提交
2783

D
dapan1121 已提交
2784
  while (CTG_IS_LOCKED(&gCtgMgmt.lock)) {
wafwerar's avatar
wafwerar 已提交
2785
    taosUsleep(1);
D
dapan1121 已提交
2786 2787
  }
  
D
dapan 已提交
2788
  CTG_LOCK(CTG_WRITE, &gCtgMgmt.lock);
D
dapan1121 已提交
2789

D
dapan1121 已提交
2790
  SCatalog *pCtg = NULL;
D
dapan 已提交
2791
  void *pIter = taosHashIterate(gCtgMgmt.pCluster, NULL);
D
dapan1121 已提交
2792
  while (pIter) {
D
dapan1121 已提交
2793
    pCtg = *(SCatalog **)pIter;
D
dapan1121 已提交
2794

D
dapan1121 已提交
2795 2796
    if (pCtg) {
      catalogFreeHandle(pCtg);
D
dapan1121 已提交
2797 2798
    }
    
D
dapan 已提交
2799
    pIter = taosHashIterate(gCtgMgmt.pCluster, pIter);
D
dapan 已提交
2800
  }
D
dapan1121 已提交
2801
  
D
dapan 已提交
2802 2803
  taosHashCleanup(gCtgMgmt.pCluster);
  gCtgMgmt.pCluster = NULL;
D
dapan1121 已提交
2804

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

D
dapan1121 已提交
2807
  qInfo("catalog destroyed");
D
dapan 已提交
2808 2809 2810 2811
}