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

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

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

D
dapan1121 已提交
29
extern SCtgDebug gCTGDebug;
D
dapan 已提交
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
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
dapan 已提交
55 56 57 58 59
                          },
                          {
                            CTG_ACT_UPDATE_USER,
                            "update user",
                            ctgActUpdateUser
D
dapan 已提交
60 61 62
                          }
};

D
dapan1121 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75
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 已提交
76
  taosMemoryFreeClear(mgmt->slots);
D
dapan1121 已提交
77 78 79 80 81 82
}


void ctgFreeTableMetaCache(SCtgTbMetaCache *cache) {
  CTG_LOCK(CTG_WRITE, &cache->stbLock);
  if (cache->stbCache) {
D
dapan1121 已提交
83
    int32_t stblNum = taosHashGetSize(cache->stbCache);  
D
dapan1121 已提交
84 85
    taosHashCleanup(cache->stbCache);
    cache->stbCache = NULL;
D
dapan1121 已提交
86
    CTG_CACHE_STAT_SUB(stblNum, stblNum);
D
dapan1121 已提交
87 88 89 90 91
  }
  CTG_UNLOCK(CTG_WRITE, &cache->stbLock);

  CTG_LOCK(CTG_WRITE, &cache->metaLock);
  if (cache->metaCache) {
D
dapan1121 已提交
92
    int32_t tblNum = taosHashGetSize(cache->metaCache);
D
dapan1121 已提交
93 94
    taosHashCleanup(cache->metaCache);
    cache->metaCache = NULL;
D
dapan1121 已提交
95
    CTG_CACHE_STAT_SUB(tblNum, tblNum);
D
dapan1121 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109
  }
  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 已提交
110
  taosMemoryFreeClear(vgInfo);
D
dapan1121 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124
}

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

D
dapan1121 已提交
125 126 127 128 129
void ctgFreeSCtgUserAuth(SCtgUserAuth *userCache) {
  taosHashCleanup(userCache->createdDbs);
  taosHashCleanup(userCache->readDbs);
  taosHashCleanup(userCache->writeDbs);
}
D
dapan1121 已提交
130 131 132 133 134 135

void ctgFreeHandle(SCatalog* pCtg) {
  ctgFreeMetaRent(&pCtg->dbRent);
  ctgFreeMetaRent(&pCtg->stbRent);
  
  if (pCtg->dbCache) {
D
dapan1121 已提交
136 137
    int32_t dbNum = taosHashGetSize(pCtg->dbCache);
    
D
dapan1121 已提交
138 139 140 141 142 143 144 145 146 147 148 149
    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 已提交
150 151
    
    CTG_CACHE_STAT_SUB(dbNum, dbNum);
D
dapan1121 已提交
152
  }
D
dapan1121 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

  if (pCtg->userCache) {
    int32_t userNum = taosHashGetSize(pCtg->userCache);
    
    void *pIter = taosHashIterate(pCtg->userCache, NULL);
    while (pIter) {
      SCtgUserAuth *userCache = pIter;

      ctgFreeSCtgUserAuth(userCache);
            
      pIter = taosHashIterate(pCtg->userCache, pIter);
    }  

    taosHashCleanup(pCtg->userCache);
    
    CTG_CACHE_STAT_SUB(userNum, userNum);
  }
    
wafwerar's avatar
wafwerar 已提交
171
  taosMemoryFree(pCtg);
D
dapan1121 已提交
172 173 174 175 176 177 178 179
}



void ctgWaitAction(SCtgMetaAction *action) {
  while (true) {
    tsem_wait(&gCtgMgmt.queue.rspSem);
    
X
Xiaoyu Wang 已提交
180
    if (atomic_load_8((int8_t*)&gCtgMgmt.exit)) {
D
dapan1121 已提交
181 182 183 184 185 186 187 188 189 190 191 192
      tsem_post(&gCtgMgmt.queue.rspSem);
      break;
    }

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

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

void ctgPopAction(SCtgMetaAction **action) {
D
dapan1121 已提交
195
  SCtgQNode *orig = gCtgMgmt.queue.head;
D
dapan 已提交
196
  
D
dapan1121 已提交
197 198
  SCtgQNode *node = gCtgMgmt.queue.head->next;
  gCtgMgmt.queue.head = gCtgMgmt.queue.head->next;
D
dapan 已提交
199 200 201

  CTG_QUEUE_SUB();
  
wafwerar's avatar
wafwerar 已提交
202
  taosMemoryFreeClear(orig);
D
dapan 已提交
203 204 205 206 207

  *action = &node->action;
}


D
dapan1121 已提交
208
int32_t ctgPushAction(SCatalog* pCtg, SCtgMetaAction *action) {
wafwerar's avatar
wafwerar 已提交
209
  SCtgQNode *node = taosMemoryCalloc(1, sizeof(SCtgQNode));
D
dapan 已提交
210 211 212 213
  if (NULL == node) {
    qError("calloc %d failed", (int32_t)sizeof(SCtgQNode));
    CTG_RET(TSDB_CODE_CTG_MEM_ERROR);
  }
D
dapan1121 已提交
214 215

  action->seqId = atomic_add_fetch_64(&gCtgMgmt.queue.seqId, 1);
D
dapan 已提交
216 217 218
  
  node->action = *action;

D
dapan1121 已提交
219 220 221 222
  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 已提交
223 224

  CTG_QUEUE_ADD();
D
dapan1121 已提交
225
  CTG_RUNTIME_STAT_ADD(qNum, 1);
D
dapan 已提交
226

D
dapan1121 已提交
227
  tsem_post(&gCtgMgmt.queue.reqSem);
D
dapan 已提交
228

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

D
dapan1121 已提交
231 232
  if (action->syncReq) {
    ctgWaitAction(action);
D
dapan1121 已提交
233 234
  }

D
dapan1121 已提交
235
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
236 237 238
}


D
dapan1121 已提交
239 240 241
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 已提交
242
  SCtgRemoveDBMsg *msg = taosMemoryMalloc(sizeof(SCtgRemoveDBMsg));
D
dapan1121 已提交
243 244 245 246 247
  if (NULL == msg) {
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgRemoveDBMsg));
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
  }

D
dapan1121 已提交
248
  char *p = strchr(dbFName, '.');
D
dapan1121 已提交
249
  if (p && CTG_IS_SYS_DBNAME(p + 1)) {
D
dapan1121 已提交
250 251 252
    dbFName = p + 1;
  }

D
dapan1121 已提交
253 254 255 256 257 258
  msg->pCtg = pCtg;
  strncpy(msg->dbFName, dbFName, sizeof(msg->dbFName));
  msg->dbId = dbId;

  action.data = msg;

D
dapan1121 已提交
259
  CTG_ERR_JRET(ctgPushAction(pCtg, &action));
D
dapan1121 已提交
260 261 262 263

  return TSDB_CODE_SUCCESS;

_return:
H
Haojun Liao 已提交
264

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


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

  return TSDB_CODE_SUCCESS;

_return:
H
Haojun Liao 已提交
292

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



D
dapan1121 已提交
299
int32_t ctgPushRmTblMsgInQueue(SCatalog* pCtg, const char *dbFName, int64_t dbId, const char *tbName, bool syncReq) {
D
dapan1121 已提交
300
  int32_t code = 0;
D
dapan1121 已提交
301
  SCtgMetaAction action= {.act = CTG_ACT_REMOVE_TBL, .syncReq = syncReq};
wafwerar's avatar
wafwerar 已提交
302
  SCtgRemoveTblMsg *msg = taosMemoryMalloc(sizeof(SCtgRemoveTblMsg));
D
dapan1121 已提交
303 304 305 306 307 308 309 310 311 312 313 314
  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 已提交
315
  CTG_ERR_JRET(ctgPushAction(pCtg, &action));
D
dapan1121 已提交
316 317 318 319

  return TSDB_CODE_SUCCESS;

_return:
H
Haojun Liao 已提交
320

wafwerar's avatar
wafwerar 已提交
321
  taosMemoryFreeClear(action.data);
H
Haojun Liao 已提交
322
  CTG_RET(code);
D
dapan1121 已提交
323 324
}

D
dapan1121 已提交
325 326 327
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 已提交
328
  SCtgUpdateVgMsg *msg = taosMemoryMalloc(sizeof(SCtgUpdateVgMsg));
D
dapan1121 已提交
329 330 331 332
  if (NULL == msg) {
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgUpdateVgMsg));
    ctgFreeVgInfo(dbInfo);
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
D
dapan1121 已提交
333 334
  }

D
dapan1121 已提交
335
  char *p = strchr(dbFName, '.');
D
dapan1121 已提交
336
  if (p && CTG_IS_SYS_DBNAME(p + 1)) {
D
dapan1121 已提交
337 338 339
    dbFName = p + 1;
  }

D
dapan1121 已提交
340 341 342 343
  strncpy(msg->dbFName, dbFName, sizeof(msg->dbFName));
  msg->pCtg = pCtg;
  msg->dbId = dbId;
  msg->dbInfo = dbInfo;
D
dapan1121 已提交
344

D
dapan1121 已提交
345
  action.data = msg;
D
dapan1121 已提交
346

D
dapan1121 已提交
347
  CTG_ERR_JRET(ctgPushAction(pCtg, &action));
D
dapan1121 已提交
348

D
dapan1121 已提交
349
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
350

D
dapan1121 已提交
351
_return:
D
dapan1121 已提交
352

D
dapan1121 已提交
353
  ctgFreeVgInfo(dbInfo);
wafwerar's avatar
wafwerar 已提交
354
  taosMemoryFreeClear(action.data);
D
dapan1121 已提交
355
  CTG_RET(code);
D
dapan1121 已提交
356 357
}

D
dapan1121 已提交
358 359
int32_t ctgPushUpdateTblMsgInQueue(SCatalog* pCtg, STableMetaOutput *output, bool syncReq) {
  int32_t code = 0;
D
dapan1121 已提交
360
  SCtgMetaAction action= {.act = CTG_ACT_UPDATE_TBL, .syncReq = syncReq};
wafwerar's avatar
wafwerar 已提交
361
  SCtgUpdateTblMsg *msg = taosMemoryMalloc(sizeof(SCtgUpdateTblMsg));
D
dapan1121 已提交
362 363 364 365 366
  if (NULL == msg) {
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgUpdateTblMsg));
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
  }

D
dapan1121 已提交
367
  char *p = strchr(output->dbFName, '.');
D
dapan1121 已提交
368
  if (p && CTG_IS_SYS_DBNAME(p + 1)) {
D
dapan1121 已提交
369 370 371
    memmove(output->dbFName, p + 1, strlen(p + 1));
  }

D
dapan1121 已提交
372 373 374 375 376 377 378 379 380 381 382
  msg->pCtg = pCtg;
  msg->output = output;

  action.data = msg;

  CTG_ERR_JRET(ctgPushAction(pCtg, &action));

  return TSDB_CODE_SUCCESS;
  
_return:

wafwerar's avatar
wafwerar 已提交
383
  taosMemoryFreeClear(msg);
D
dapan1121 已提交
384 385 386 387
  
  CTG_RET(code);
}

D
dapan 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
int32_t ctgPushUpdateUserMsgInQueue(SCatalog* pCtg, SGetUserAuthRsp *pAuth, bool syncReq) {
  int32_t code = 0;
  SCtgMetaAction action= {.act = CTG_ACT_UPDATE_USER, .syncReq = syncReq};
  SCtgUpdateUserMsg *msg = taosMemoryMalloc(sizeof(SCtgUpdateUserMsg));
  if (NULL == msg) {
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgUpdateUserMsg));
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
  }

  msg->pCtg = pCtg;
  msg->userAuth = *pAuth;

  action.data = msg;

  CTG_ERR_JRET(ctgPushAction(pCtg, &action));

  return TSDB_CODE_SUCCESS;
  
_return:

D
dapan 已提交
408
  tFreeSGetUserAuthRsp(pAuth);
D
dapan 已提交
409 410 411 412
  taosMemoryFreeClear(msg);
  
  CTG_RET(code);
}
D
dapan1121 已提交
413

D
dapan 已提交
414
int32_t ctgAcquireVgInfo(SCatalog *pCtg, SCtgDBCache *dbCache, bool *inCache) {
D
dapan1121 已提交
415 416
  CTG_LOCK(CTG_READ, &dbCache->vgLock);
  
D
dapan 已提交
417 418 419 420 421 422 423 424 425 426
  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 已提交
427 428 429
  if (NULL == dbCache->vgInfo) {
    CTG_UNLOCK(CTG_READ, &dbCache->vgLock);

D
dapan 已提交
430
    *inCache = false;
D
dapan1121 已提交
431
    ctgDebug("db vgInfo is empty, dbId:%"PRIx64, dbCache->dbId);
D
dapan1121 已提交
432 433 434
    return TSDB_CODE_SUCCESS;
  }

D
dapan 已提交
435 436
  *inCache = true;
  
D
dapan1121 已提交
437 438 439 440 441 442 443 444 445 446 447 448 449 450
  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 已提交
451

D
dapan1121 已提交
452 453 454
void ctgReleaseDBCache(SCatalog *pCtg, SCtgDBCache *dbCache) {
  taosHashRelease(pCtg->dbCache, dbCache);
}
D
dapan1121 已提交
455

D
dapan1121 已提交
456 457 458
void ctgReleaseVgInfo(SCtgDBCache *dbCache) {
  CTG_UNLOCK(CTG_READ, &dbCache->vgLock);
}
D
dapan1121 已提交
459

D
dapan1121 已提交
460 461 462
void ctgWReleaseVgInfo(SCtgDBCache *dbCache) {
  CTG_UNLOCK(CTG_WRITE, &dbCache->vgLock);
}
D
dapan1121 已提交
463

D
dapan1121 已提交
464

D
dapan 已提交
465
int32_t ctgAcquireDBCacheImpl(SCatalog* pCtg, const char *dbFName, SCtgDBCache **pCache, bool acquire) {
D
dapan1121 已提交
466
  char *p = strchr(dbFName, '.');
D
dapan1121 已提交
467
  if (p && CTG_IS_SYS_DBNAME(p + 1)) {
D
dapan1121 已提交
468 469 470
    dbFName = p + 1;
  }

D
dapan 已提交
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
  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 已提交
508
int32_t ctgAcquireVgInfoFromCache(SCatalog* pCtg, const char *dbFName, SCtgDBCache **pCache, bool *inCache) {
D
dapan1121 已提交
509 510
  SCtgDBCache *dbCache = NULL;

D
dapan1121 已提交
511
  if (NULL == pCtg->dbCache) {
D
dapan1121 已提交
512 513
    ctgDebug("empty db cache, dbFName:%s", dbFName);
    goto _return;
D
dapan1121 已提交
514 515 516 517
  }

  ctgAcquireDBCache(pCtg, dbFName, &dbCache);
  if (NULL == dbCache) {  
D
dapan1121 已提交
518 519
    ctgDebug("db %s not in cache", dbFName);
    goto _return;
D
dapan1121 已提交
520 521
  }
  
D
dapan 已提交
522 523
  ctgAcquireVgInfo(pCtg, dbCache, inCache);
  if (!(*inCache)) {
D
dapan1121 已提交
524 525
    ctgDebug("vgInfo of db %s not in cache", dbFName);
    goto _return;
D
dapan1121 已提交
526
  }
D
dapan1121 已提交
527

D
dapan1121 已提交
528
  *pCache = dbCache;
D
dapan1121 已提交
529
  *inCache = true;
D
dapan1121 已提交
530

D
dapan1121 已提交
531 532
  CTG_CACHE_STAT_ADD(vgHitNum, 1);

D
dapan1121 已提交
533
  ctgDebug("Got db vgInfo from cache, dbFName:%s", dbFName);
D
dapan1121 已提交
534
  
D
dapan1121 已提交
535 536 537 538 539 540 541 542 543 544 545 546 547
  return TSDB_CODE_SUCCESS;

_return:

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

  *pCache = NULL;
  *inCache = false;

  CTG_CACHE_STAT_ADD(vgMissNum, 1);
  
D
dapan1121 已提交
548 549 550
  return TSDB_CODE_SUCCESS;
}

D
dapan 已提交
551
int32_t ctgGetQnodeListFromMnode(SCatalog* pCtg, void *pRpc, const SEpSet* pMgmtEps, SArray *out) {
D
dapan1121 已提交
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
  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 已提交
573
    ctgError("error rsp for qnode list, error:%s", tstrerror(rpcRsp.code));
D
dapan1121 已提交
574 575 576 577 578 579 580 581 582
    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
dapan 已提交
583
  ctgDebug("Got qnode list from mnode, listNum:%d", (int32_t)taosArrayGetSize(out));
D
dapan1121 已提交
584 585 586

  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
587 588


D
dapan1121 已提交
589
int32_t ctgGetDBVgInfoFromMnode(SCatalog* pCtg, void *pRpc, const SEpSet* pMgmtEps, SBuildUseDBInput *input, SUseDbOutput *out) {
D
dapan1121 已提交
590 591 592
  char *msg = NULL;
  int32_t msgLen = 0;

D
dapan 已提交
593
  ctgDebug("try to get db vgInfo from mnode, dbFName:%s", input->db);
D
dapan1121 已提交
594 595 596 597 598 599

  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 已提交
600
  
D
dapan1121 已提交
601
  SRpcMsg rpcMsg = {
H
Hongze Cheng 已提交
602
      .msgType = TDMT_MND_USE_DB,
D
catalog  
dapan1121 已提交
603
      .pCont   = msg,
D
dapan1121 已提交
604 605 606 607 608 609
      .contLen = msgLen,
  };

  SRpcMsg rpcRsp = {0};

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

D
dapan1121 已提交
615 616 617 618 619
  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 已提交
620

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

D
dapan1121 已提交
623 624
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
625

D
dapan1121 已提交
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
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 已提交
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
int32_t ctgGetIndexInfoFromMnode(SCatalog* pCtg, void *pRpc, const SEpSet* pMgmtEps, const char *indexName, SIndexInfo *out) {
  char *msg = NULL;
  int32_t msgLen = 0;

  ctgDebug("try to get index from mnode, indexName:%s", indexName);

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

  SRpcMsg rpcRsp = {0};

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

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

  ctgDebug("Got index from mnode, indexName:%s", indexName);

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
700
int32_t ctgGetUdfInfoFromMnode(SCatalog* pCtg, void *pRpc, const SEpSet* pMgmtEps, const char *funcName, SFuncInfo **out) {
D
dapan1121 已提交
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
  char *msg = NULL;
  int32_t msgLen = 0;

  ctgDebug("try to get udf info from mnode, funcName:%s", funcName);

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

  SRpcMsg rpcRsp = {0};

  rpcSendRecv(pRpc, (SEpSet*)pMgmtEps, &rpcMsg, &rpcRsp);
  if (TSDB_CODE_SUCCESS != rpcRsp.code) {
D
dapan1121 已提交
722 723 724 725 726 727
    if (TSDB_CODE_MND_FUNC_NOT_EXIST == rpcRsp.code) {
      ctgDebug("funcName %s not exist in mnode", funcName);
      taosMemoryFreeClear(*out);
      CTG_RET(TSDB_CODE_SUCCESS);
    }
    
D
dapan1121 已提交
728 729 730 731
    ctgError("error rsp for get udf, error:%s, funcName:%s", tstrerror(rpcRsp.code), funcName);
    CTG_ERR_RET(rpcRsp.code);
  }

D
dapan1121 已提交
732
  code = queryProcessMsgRsp[TMSG_INDEX(TDMT_MND_RETRIEVE_FUNC)](*out, rpcRsp.pCont, rpcRsp.contLen);
D
dapan1121 已提交
733 734 735 736 737 738 739 740 741 742
  if (code) {
    ctgError("Process get udf rsp failed, code:%x, funcName:%s", code, funcName);
    CTG_ERR_RET(code);
  }

  ctgDebug("Got udf from mnode, funcName:%s", funcName);

  return TSDB_CODE_SUCCESS;
}

D
dapan 已提交
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
int32_t ctgGetUserDbAuthFromMnode(SCatalog* pCtg, void *pRpc, const SEpSet* pMgmtEps, const char *user, SGetUserAuthRsp *authRsp) {
  char *msg = NULL;
  int32_t msgLen = 0;

  ctgDebug("try to get user auth from mnode, user:%s", user);

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

  SRpcMsg rpcRsp = {0};

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

  code = queryProcessMsgRsp[TMSG_INDEX(TDMT_MND_GET_USER_AUTH)](authRsp, rpcRsp.pCont, rpcRsp.contLen);
  if (code) {
    ctgError("Process get user auth rsp failed, code:%x, user:%s", code, user);
    CTG_ERR_RET(code);
  }

  ctgDebug("Got user auth from mnode, user:%s", user);

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
780

D
dapan1121 已提交
781 782
int32_t ctgIsTableMetaExistInCache(SCatalog* pCtg, char *dbFName, char* tbName, int32_t *exist) {
  if (NULL == pCtg->dbCache) {
D
dapan1121 已提交
783
    *exist = 0;
D
dapan1121 已提交
784 785 786 787
    ctgWarn("empty db cache, dbFName:%s, tbName:%s", dbFName, tbName);
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
788 789
  SCtgDBCache *dbCache = NULL;
  ctgAcquireDBCache(pCtg, dbFName, &dbCache);
D
dapan1121 已提交
790 791
  if (NULL == dbCache) {
    *exist = 0;
D
dapan1121 已提交
792 793 794
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
795
  size_t sz = 0;
D
dapan1121 已提交
796 797 798 799
  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 已提交
800
  if (NULL == tbMeta) {
D
dapan 已提交
801
    ctgReleaseDBCache(pCtg, dbCache);
D
dapan1121 已提交
802
    
D
dapan1121 已提交
803
    *exist = 0;
D
dapan1121 已提交
804
    ctgDebug("tbmeta not in cache, dbFName:%s, tbName:%s", dbFName, tbName);
D
dapan1121 已提交
805 806 807 808
    return TSDB_CODE_SUCCESS;
  }

  *exist = 1;
D
dapan1121 已提交
809

D
dapan 已提交
810
  ctgReleaseDBCache(pCtg, dbCache);
D
dapan1121 已提交
811
  
D
dapan1121 已提交
812
  ctgDebug("tbmeta is in cache, dbFName:%s, tbName:%s", dbFName, tbName);
D
dapan1121 已提交
813 814 815 816
  
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
817

D
dapan1121 已提交
818
int32_t ctgGetTableMetaFromCache(SCatalog* pCtg, const SName* pTableName, STableMeta** pTableMeta, bool *inCache, int32_t flag, uint64_t *dbId) {
D
dapan1121 已提交
819
  if (NULL == pCtg->dbCache) {
D
dapan1121 已提交
820 821
    ctgDebug("empty tbmeta cache, tbName:%s", pTableName->tname);
    goto _return;
D
dapan1121 已提交
822 823
  }

D
dapan1121 已提交
824
  char dbFName[TSDB_DB_FNAME_LEN] = {0};
D
dapan1121 已提交
825
  if (CTG_FLAG_IS_SYS_DB(flag)) {
D
dapan1121 已提交
826 827 828 829
    strcpy(dbFName, pTableName->dbname);
  } else {
    tNameGetFullDbName(pTableName, dbFName);
  }
D
dapan1121 已提交
830

D
dapan1121 已提交
831 832
  *pTableMeta = NULL;

D
dapan1121 已提交
833 834
  SCtgDBCache *dbCache = NULL;
  ctgAcquireDBCache(pCtg, dbFName, &dbCache);
D
dapan1121 已提交
835
  if (NULL == dbCache) {
D
dapan1121 已提交
836 837
    ctgDebug("db %s not in cache", pTableName->tname);
    goto _return;
D
dapan1121 已提交
838 839
  }
  
H
Haojun Liao 已提交
840
  int32_t sz = 0;
D
dapan1121 已提交
841
  CTG_LOCK(CTG_READ, &dbCache->tbCache.metaLock);
H
Haojun Liao 已提交
842
  int32_t code = taosHashGetDup_m(dbCache->tbCache.metaCache, pTableName->tname, strlen(pTableName->tname), (void **)pTableMeta, &sz);
D
dapan1121 已提交
843 844
  CTG_UNLOCK(CTG_READ, &dbCache->tbCache.metaLock);

D
dapan1121 已提交
845
  if (NULL == *pTableMeta) {
D
dapan1121 已提交
846 847
    ctgReleaseDBCache(pCtg, dbCache);
    ctgDebug("tbl not in cache, dbFName:%s, tbName:%s", dbFName, pTableName->tname);
D
dapan1121 已提交
848
    goto _return;
D
dapan1121 已提交
849 850
  }

D
dapan1121 已提交
851 852 853
  if (dbId) {
    *dbId = dbCache->dbId;
  }
H
Haojun Liao 已提交
854

H
Haojun Liao 已提交
855
  STableMeta* tbMeta = *pTableMeta;
D
dapan1121 已提交
856

D
dapan1121 已提交
857
  if (tbMeta->tableType != TSDB_CHILD_TABLE) {
D
dapan1121 已提交
858
    ctgReleaseDBCache(pCtg, dbCache);
D
dapan1121 已提交
859
    ctgDebug("Got meta from cache, type:%d, dbFName:%s, tbName:%s", tbMeta->tableType, dbFName, pTableName->tname);
D
dapan1121 已提交
860 861 862 863

    *inCache = true;
    CTG_CACHE_STAT_ADD(tblHitNum, 1);
    
D
dapan1121 已提交
864 865
    return TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
866 867

  ctgDebug("Got subtable meta from cache, type:%d, dbFName:%s, tbName:%s, suid:%" PRIx64, tbMeta->tableType, dbFName, pTableName->tname, tbMeta->suid);
D
dapan1121 已提交
868
  
D
dapan1121 已提交
869
  CTG_LOCK(CTG_READ, &dbCache->tbCache.stbLock);
D
dapan1121 已提交
870
  
D
dapan1121 已提交
871
  STableMeta **stbMeta = taosHashGet(dbCache->tbCache.stbCache, &tbMeta->suid, sizeof(tbMeta->suid));
D
dapan1121 已提交
872
  if (NULL == stbMeta || NULL == *stbMeta) {
D
dapan1121 已提交
873
    CTG_UNLOCK(CTG_READ, &dbCache->tbCache.stbLock);
D
dapan1121 已提交
874 875
    ctgReleaseDBCache(pCtg, dbCache);
    ctgError("stb not in stbCache, suid:%"PRIx64, tbMeta->suid);
wafwerar's avatar
wafwerar 已提交
876
    taosMemoryFreeClear(*pTableMeta);
D
dapan1121 已提交
877
    goto _return;
D
dapan1121 已提交
878
  }
D
dapan1121 已提交
879

D
dapan1121 已提交
880
  if ((*stbMeta)->suid != tbMeta->suid) {    
D
dapan1121 已提交
881
    CTG_UNLOCK(CTG_READ, &dbCache->tbCache.stbLock);
D
dapan1121 已提交
882
    ctgReleaseDBCache(pCtg, dbCache);
wafwerar's avatar
wafwerar 已提交
883
    taosMemoryFreeClear(*pTableMeta);
D
dapan1121 已提交
884
    ctgError("stable suid in stbCache mis-match, expected suid:%"PRIx64 ",actual suid:%"PRIx64, tbMeta->suid, (*stbMeta)->suid);
D
dapan1121 已提交
885 886
    CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
  }
D
dapan1121 已提交
887

D
dapan1121 已提交
888
  int32_t metaSize = CTG_META_SIZE(*stbMeta);
wafwerar's avatar
wafwerar 已提交
889
  *pTableMeta = taosMemoryRealloc(*pTableMeta, metaSize);
D
dapan1121 已提交
890
  if (NULL == *pTableMeta) {    
D
dapan1121 已提交
891
    CTG_UNLOCK(CTG_READ, &dbCache->tbCache.stbLock);
D
dapan1121 已提交
892
    ctgReleaseDBCache(pCtg, dbCache);
D
dapan1121 已提交
893
    ctgError("realloc size[%d] failed", metaSize);
D
dapan1121 已提交
894
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
D
dapan1121 已提交
895 896
  }

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

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

D
dapan1121 已提交
901
  ctgReleaseDBCache(pCtg, dbCache);
D
dapan 已提交
902

D
dapan1121 已提交
903 904 905
  *inCache = true;
  CTG_CACHE_STAT_ADD(tblHitNum, 1);

D
dapan1121 已提交
906
  ctgDebug("Got tbmeta from cache, dbFName:%s, tbName:%s", dbFName, pTableName->tname);
D
dapan1121 已提交
907
  
D
dapan1121 已提交
908 909 910 911 912 913 914
  return TSDB_CODE_SUCCESS;

_return:

  *inCache = false;
  CTG_CACHE_STAT_ADD(tblMissNum, 1);
  
D
dapan1121 已提交
915 916 917
  return TSDB_CODE_SUCCESS;
}

D
dapan 已提交
918
int32_t ctgGetTableTypeFromCache(SCatalog* pCtg, const char* dbFName, const char *tableName, int32_t *tbType) {
D
dapan1121 已提交
919
  if (NULL == pCtg->dbCache) {
D
dapan 已提交
920
    ctgWarn("empty db cache, dbFName:%s, tbName:%s", dbFName, tableName);  
D
dapan1121 已提交
921 922
    return TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
923
  
D
dapan 已提交
924 925
  SCtgDBCache *dbCache = NULL;
  ctgAcquireDBCache(pCtg, dbFName, &dbCache);
D
dapan1121 已提交
926 927 928
  if (NULL == dbCache) {
    return TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
929

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

D
dapan1121 已提交
933
  if (NULL == pTableMeta) {
D
dapan1121 已提交
934
    CTG_UNLOCK(CTG_READ, &dbCache->tbCache.metaLock);
D
dapan 已提交
935
    ctgWarn("tbl not in cache, dbFName:%s, tbName:%s", dbFName, tableName);  
D
dapan 已提交
936
    ctgReleaseDBCache(pCtg, dbCache);
D
dapan1121 已提交
937
    
D
dapan1121 已提交
938 939 940
    return TSDB_CODE_SUCCESS;
  }

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

D
dapan1121 已提交
943 944 945 946
  taosHashRelease(dbCache->tbCache.metaCache, pTableMeta);

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

D
dapan 已提交
947
  ctgReleaseDBCache(pCtg, dbCache);
D
dapan1121 已提交
948

D
dapan 已提交
949
  ctgDebug("Got tbtype from cache, dbFName:%s, tbName:%s, type:%d", dbFName, tableName, *tbType);  
D
dapan1121 已提交
950 951 952 953
  
  return TSDB_CODE_SUCCESS;
}

D
dapan 已提交
954
int32_t ctgChkAuthFromCache(SCatalog* pCtg, const char* user, const char* dbFName, AUTH_TYPE type, bool *inCache, bool *pass) {
D
dapan 已提交
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
  if (NULL == pCtg->userCache) {
    ctgDebug("empty user auth cache, user:%s", user);
    goto _return;
  }
  
  SCtgUserAuth *pUser = (SCtgUserAuth *)taosHashGet(pCtg->userCache, user, strlen(user));
  if (NULL == pUser) {
    ctgDebug("user not in cache, user:%s", user);
    goto _return;
  }

  *inCache = true;

  ctgDebug("Got user from cache, user:%s", user);
  CTG_CACHE_STAT_ADD(userHitNum, 1);
  
  if (pUser->superUser) {
D
dapan 已提交
972
    *pass = true;
D
dapan 已提交
973 974 975 976 977
    return TSDB_CODE_SUCCESS;
  }

  CTG_LOCK(CTG_READ, &pUser->lock);
  if (pUser->createdDbs && taosHashGet(pUser->createdDbs, dbFName, strlen(dbFName))) {
D
dapan 已提交
978
    *pass = true;
D
dapan 已提交
979 980 981 982
    CTG_UNLOCK(CTG_READ, &pUser->lock);
    return TSDB_CODE_SUCCESS;
  }
  
D
dapan 已提交
983 984
  if (pUser->readDbs && taosHashGet(pUser->readDbs, dbFName, strlen(dbFName)) && type == AUTH_TYPE_READ) {
    *pass = true;
D
dapan 已提交
985 986
  }
  
D
dapan 已提交
987 988
  if (pUser->writeDbs && taosHashGet(pUser->writeDbs, dbFName, strlen(dbFName)) && type == AUTH_TYPE_WRITE) {
    *pass = true;
D
dapan 已提交
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
  }

  CTG_UNLOCK(CTG_READ, &pUser->lock);
  
  return TSDB_CODE_SUCCESS;

_return:

  *inCache = false;
  CTG_CACHE_STAT_ADD(userMissNum, 1);
  
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1003
int32_t ctgGetTableMetaFromMnodeImpl(SCatalog* pCtg, void *pTrans, const SEpSet* pMgmtEps, char *dbFName, char* tbName, STableMetaOutput* output) {
D
dapan1121 已提交
1004
  SBuildTableMetaInput bInput = {.vgId = 0, .dbFName = dbFName, .tbName = tbName};
D
dapan1121 已提交
1005 1006 1007 1008
  char *msg = NULL;
  SEpSet *pVnodeEpSet = NULL;
  int32_t msgLen = 0;

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

D
dapan1121 已提交
1011
  int32_t code = queryBuildMsg[TMSG_INDEX(TDMT_MND_TABLE_META)](&bInput, &msg, 0, &msgLen);
D
dapan1121 已提交
1012 1013 1014 1015
  if (code) {
    ctgError("Build mnode stablemeta msg failed, code:%x", code);
    CTG_ERR_RET(code);
  }
D
dapan1121 已提交
1016 1017

  SRpcMsg rpcMsg = {
D
dapan1121 已提交
1018
      .msgType = TDMT_MND_TABLE_META,
D
dapan1121 已提交
1019 1020 1021
      .pCont   = msg,
      .contLen = msgLen,
  };
D
dapan1121 已提交
1022

D
dapan1121 已提交
1023 1024
  SRpcMsg rpcRsp = {0};

D
dapan1121 已提交
1025
  rpcSendRecv(pTrans, (SEpSet*)pMgmtEps, &rpcMsg, &rpcRsp);
D
dapan1121 已提交
1026 1027
  
  if (TSDB_CODE_SUCCESS != rpcRsp.code) {
D
dapan1121 已提交
1028
    if (CTG_TABLE_NOT_EXIST(rpcRsp.code)) {
D
dapan1121 已提交
1029
      SET_META_TYPE_NULL(output->metaType);
D
dapan1121 已提交
1030
      ctgDebug("stablemeta not exist in mnode, dbFName:%s, tbName:%s", dbFName, tbName);
D
dapan1121 已提交
1031 1032 1033
      return TSDB_CODE_SUCCESS;
    }
    
D
dapan1121 已提交
1034
    ctgError("error rsp for stablemeta from mnode, code:%s, dbFName:%s, tbName:%s", tstrerror(rpcRsp.code), dbFName, tbName);
D
dapan1121 已提交
1035 1036 1037
    CTG_ERR_RET(rpcRsp.code);
  }

D
dapan1121 已提交
1038
  code = queryProcessMsgRsp[TMSG_INDEX(TDMT_MND_TABLE_META)](output, rpcRsp.pCont, rpcRsp.contLen);
D
dapan1121 已提交
1039
  if (code) {
D
dapan1121 已提交
1040
    ctgError("Process mnode stablemeta rsp failed, code:%x, dbFName:%s, tbName:%s", code, dbFName, tbName);
D
dapan1121 已提交
1041 1042 1043
    CTG_ERR_RET(code);
  }

D
dapan1121 已提交
1044
  ctgDebug("Got table meta from mnode, dbFName:%s, tbName:%s", dbFName, tbName);
D
dapan1121 已提交
1045 1046 1047 1048

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1049
int32_t ctgGetTableMetaFromMnode(SCatalog* pCtg, void *pTrans, const SEpSet* pMgmtEps, const SName* pTableName, STableMetaOutput* output) {
D
dapan1121 已提交
1050 1051
  char dbFName[TSDB_DB_FNAME_LEN];
  tNameGetFullDbName(pTableName, dbFName);
D
dapan1121 已提交
1052

D
dapan1121 已提交
1053
  return ctgGetTableMetaFromMnodeImpl(pCtg, pTrans, pMgmtEps, dbFName, (char *)pTableName->tname, output);
D
dapan1121 已提交
1054
}
D
dapan1121 已提交
1055

D
dapan1121 已提交
1056
int32_t ctgGetTableMetaFromVnodeImpl(SCatalog* pCtg, void *pTrans, const SEpSet* pMgmtEps, const SName* pTableName, SVgroupInfo *vgroupInfo, STableMetaOutput* output) {
D
dapan1121 已提交
1057
  if (NULL == pCtg || NULL == pTrans || NULL == pMgmtEps || NULL == pTableName || NULL == vgroupInfo || NULL == output) {
D
dapan1121 已提交
1058
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
D
dapan1121 已提交
1059 1060
  }

D
dapan1121 已提交
1061 1062
  char dbFName[TSDB_DB_FNAME_LEN];
  tNameGetFullDbName(pTableName, dbFName);
D
dapan1121 已提交
1063

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

D
dapan1121 已提交
1066
  SBuildTableMetaInput bInput = {.vgId = vgroupInfo->vgId, .dbFName = dbFName, .tbName = (char *)tNameGetTableName(pTableName)};
D
dapan1121 已提交
1067 1068 1069
  char *msg = NULL;
  int32_t msgLen = 0;

D
dapan1121 已提交
1070 1071
  int32_t code = queryBuildMsg[TMSG_INDEX(TDMT_VND_TABLE_META)](&bInput, &msg, 0, &msgLen);
  if (code) {
D
dapan1121 已提交
1072
    ctgError("Build vnode tablemeta msg failed, code:%x, dbFName:%s, tbName:%s", code, dbFName, tNameGetTableName(pTableName));
D
dapan1121 已提交
1073 1074
    CTG_ERR_RET(code);
  }
D
dapan1121 已提交
1075 1076

  SRpcMsg rpcMsg = {
H
Hongze Cheng 已提交
1077
      .msgType = TDMT_VND_TABLE_META,
D
dapan1121 已提交
1078 1079 1080 1081 1082
      .pCont   = msg,
      .contLen = msgLen,
  };

  SRpcMsg rpcRsp = {0};
L
Liu Jicong 已提交
1083
  rpcSendRecv(pTrans, &vgroupInfo->epSet, &rpcMsg, &rpcRsp);
D
dapan1121 已提交
1084
  
D
dapan1121 已提交
1085
  if (TSDB_CODE_SUCCESS != rpcRsp.code) {
D
dapan1121 已提交
1086
    if (CTG_TABLE_NOT_EXIST(rpcRsp.code)) {
D
dapan1121 已提交
1087
      SET_META_TYPE_NULL(output->metaType);
D
dapan1121 已提交
1088
      ctgDebug("tablemeta not exist in vnode, dbFName:%s, tbName:%s", dbFName, tNameGetTableName(pTableName));
D
dapan1121 已提交
1089 1090 1091
      return TSDB_CODE_SUCCESS;
    }
  
D
dapan1121 已提交
1092
    ctgError("error rsp for table meta from vnode, code:%s, dbFName:%s, tbName:%s", tstrerror(rpcRsp.code), dbFName, tNameGetTableName(pTableName));
D
dapan1121 已提交
1093
    CTG_ERR_RET(rpcRsp.code);
D
dapan1121 已提交
1094 1095
  }

D
dapan1121 已提交
1096 1097
  code = queryProcessMsgRsp[TMSG_INDEX(TDMT_VND_TABLE_META)](output, rpcRsp.pCont, rpcRsp.contLen);
  if (code) {
D
dapan1121 已提交
1098
    ctgError("Process vnode tablemeta rsp failed, code:%s, dbFName:%s, tbName:%s", tstrerror(code), dbFName, tNameGetTableName(pTableName));
D
dapan1121 已提交
1099 1100 1101
    CTG_ERR_RET(code);
  }

D
dapan1121 已提交
1102
  ctgDebug("Got table meta from vnode, dbFName:%s, tbName:%s", dbFName, tNameGetTableName(pTableName));
D
dapan1121 已提交
1103 1104 1105
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
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 已提交
1132

1133 1134
int32_t ctgGetHashFunction(int8_t hashMethod, tableNameHashFp *fp) {
  switch (hashMethod) {
D
dapan1121 已提交
1135 1136 1137 1138 1139 1140 1141 1142
    default:
      *fp = MurmurHash3_32;
      break;
  }

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1143
int32_t ctgGenerateVgList(SCatalog *pCtg, SHashObj *vgHash, SArray** pList) {
D
dapan1121 已提交
1144
  SHashObj *vgroupHash = NULL;
1145
  SVgroupInfo *vgInfo = NULL;
D
dapan1121 已提交
1146 1147
  SArray *vgList = NULL;
  int32_t code = 0;
D
dapan1121 已提交
1148
  int32_t vgNum = taosHashGetSize(vgHash);
1149

D
dapan1121 已提交
1150
  vgList = taosArrayInit(vgNum, sizeof(SVgroupInfo));
D
dapan1121 已提交
1151
  if (NULL == vgList) {
D
dapan1121 已提交
1152
    ctgError("taosArrayInit failed, num:%d", vgNum);
D
dapan 已提交
1153 1154 1155
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);    
  }

D
dapan1121 已提交
1156
  void *pIter = taosHashIterate(vgHash, NULL);
1157 1158
  while (pIter) {
    vgInfo = pIter;
D
dapan1121 已提交
1159

D
dapan1121 已提交
1160
    if (NULL == taosArrayPush(vgList, vgInfo)) {
D
dapan1121 已提交
1161
      ctgError("taosArrayPush failed, vgId:%d", vgInfo->vgId);
D
dapan1121 已提交
1162
      taosHashCancelIterate(vgHash, pIter);      
D
dapan1121 已提交
1163
      CTG_ERR_JRET(TSDB_CODE_CTG_MEM_ERROR);
1164 1165
    }
    
D
dapan1121 已提交
1166
    pIter = taosHashIterate(vgHash, pIter);
1167
    vgInfo = NULL;
D
dapan1121 已提交
1168 1169
  }

D
dapan1121 已提交
1170
  *pList = vgList;
D
dapan1121 已提交
1171

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

D
dapan1121 已提交
1174
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1175 1176 1177 1178 1179 1180 1181 1182

_return:

  if (vgList) {
    taosArrayDestroy(vgList);
  }

  CTG_RET(code);
D
dapan1121 已提交
1183 1184
}

D
dapan1121 已提交
1185
int32_t ctgGetVgInfoFromHashValue(SCatalog *pCtg, SDBVgInfo *dbInfo, const SName *pTableName, SVgroupInfo *pVgroup) {
D
dapan1121 已提交
1186 1187
  int32_t code = 0;
  
D
dapan1121 已提交
1188
  int32_t vgNum = taosHashGetSize(dbInfo->vgHash);
H
Haojun Liao 已提交
1189 1190 1191
  char db[TSDB_DB_FNAME_LEN] = {0};
  tNameGetFullDbName(pTableName, db);

1192
  if (vgNum <= 0) {
D
dapan1121 已提交
1193
    ctgError("db vgroup cache invalid, db:%s, vgroup number:%d", db, vgNum);
D
dapan1121 已提交
1194
    CTG_ERR_RET(TSDB_CODE_TSC_DB_NOT_SELECTED);
D
dapan1121 已提交
1195 1196
  }

1197 1198
  tableNameHashFp fp = NULL;
  SVgroupInfo *vgInfo = NULL;
D
dapan1121 已提交
1199

D
dapan1121 已提交
1200
  CTG_ERR_RET(ctgGetHashFunction(dbInfo->hashMethod, &fp));
1201 1202

  char tbFullName[TSDB_TABLE_FNAME_LEN];
H
Haojun Liao 已提交
1203
  tNameExtractFullName(pTableName, tbFullName);
1204 1205 1206

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

D
dapan1121 已提交
1207
  void *pIter = taosHashIterate(dbInfo->vgHash, NULL);
1208 1209 1210
  while (pIter) {
    vgInfo = pIter;
    if (hashValue >= vgInfo->hashBegin && hashValue <= vgInfo->hashEnd) {
D
dapan1121 已提交
1211
      taosHashCancelIterate(dbInfo->vgHash, pIter);
1212
      break;
D
dapan1121 已提交
1213
    }
1214
    
D
dapan1121 已提交
1215
    pIter = taosHashIterate(dbInfo->vgHash, pIter);
1216
    vgInfo = NULL;
D
dapan1121 已提交
1217 1218
  }

1219
  if (NULL == vgInfo) {
D
dapan1121 已提交
1220 1221
    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);
1222 1223 1224 1225
  }

  *pVgroup = *vgInfo;

1226
  CTG_RET(code);
D
dapan1121 已提交
1227 1228
}

D
dapan1121 已提交
1229
int32_t ctgStbVersionSearchCompare(const void* key1, const void* key2) {
D
dapan 已提交
1230
  if (*(uint64_t *)key1 < ((SSTableMetaVersion*)key2)->suid) {
D
dapan1121 已提交
1231
    return -1;
D
dapan 已提交
1232
  } else if (*(uint64_t *)key1 > ((SSTableMetaVersion*)key2)->suid) {
D
dapan1121 已提交
1233 1234 1235 1236 1237 1238
    return 1;
  } else {
    return 0;
  }
}

D
dapan1121 已提交
1239
int32_t ctgDbVgVersionSearchCompare(const void* key1, const void* key2) {
D
dapan1121 已提交
1240
  if (*(int64_t *)key1 < ((SDbVgVersion*)key2)->dbId) {
D
dapan1121 已提交
1241
    return -1;
D
dapan1121 已提交
1242
  } else if (*(int64_t *)key1 > ((SDbVgVersion*)key2)->dbId) {
D
dapan1121 已提交
1243 1244 1245
    return 1;
  } else {
    return 0;
D
dapan1121 已提交
1246
  }
D
dapan1121 已提交
1247 1248
}

D
dapan1121 已提交
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
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 已提交
1270
int32_t ctgMetaRentInit(SCtgRentMgmt *mgmt, uint32_t rentSec, int8_t type) {
D
dapan1121 已提交
1271 1272 1273 1274
  mgmt->slotRIdx = 0;
  mgmt->slotNum = rentSec / CTG_RENT_SLOT_SECOND;
  mgmt->type = type;

D
dapan1121 已提交
1275
  size_t msgSize = sizeof(SCtgRentSlot) * mgmt->slotNum;
D
dapan1121 已提交
1276
  
wafwerar's avatar
wafwerar 已提交
1277
  mgmt->slots = taosMemoryCalloc(1, msgSize);
D
dapan1121 已提交
1278 1279
  if (NULL == mgmt->slots) {
    qError("calloc %d failed", (int32_t)msgSize);
D
dapan 已提交
1280
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
D
dapan1121 已提交
1281
  }
D
dapan1121 已提交
1282

D
dapan1121 已提交
1283 1284 1285 1286
  qDebug("meta rent initialized, type:%d, slotNum:%d", type, mgmt->slotNum);
  
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
1287

D
dapan1121 已提交
1288

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

D
dapan1121 已提交
1292
  SCtgRentSlot *slot = &mgmt->slots[widx];
D
dapan1121 已提交
1293 1294 1295 1296 1297 1298 1299 1300
  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 已提交
1301
    }
D
dapan1121 已提交
1302
  }
D
dapan1121 已提交
1303

D
dapan1121 已提交
1304 1305 1306
  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 已提交
1307 1308
  }

D
dapan1121 已提交
1309
  slot->needSort = true;
D
dapan1121 已提交
1310

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

D
dapan1121 已提交
1313 1314 1315 1316 1317 1318
_return:

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

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

D
dapan1121 已提交
1322
  SCtgRentSlot *slot = &mgmt->slots[widx];
D
dapan1121 已提交
1323
  int32_t code = 0;
1324

D
dapan1121 已提交
1325 1326
  CTG_LOCK(CTG_WRITE, &slot->lock);
  if (NULL == slot->meta) {
D
dapan1121 已提交
1327 1328
    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 已提交
1329 1330 1331
  }

  if (slot->needSort) {
D
dapan1121 已提交
1332
    qDebug("meta slot before sorte, slot idx:%d, type:%d, size:%d", widx, mgmt->type, (int32_t)taosArrayGetSize(slot->meta));
D
dapan1121 已提交
1333
    taosArraySort(slot->meta, sortCompare);
D
dapan1121 已提交
1334
    slot->needSort = false;
D
dapan1121 已提交
1335
    qDebug("meta slot sorted, slot idx:%d, type:%d, size:%d", widx, mgmt->type, (int32_t)taosArrayGetSize(slot->meta));
D
dapan1121 已提交
1336 1337
  }

D
dapan1121 已提交
1338
  void *orig = taosArraySearch(slot->meta, &id, searchCompare, TD_EQ);
D
dapan1121 已提交
1339
  if (NULL == orig) {
D
dapan1121 已提交
1340
    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 已提交
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
    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 已提交
1360
int32_t ctgMetaRentRemove(SCtgRentMgmt *mgmt, int64_t id, __compar_fn_t sortCompare, __compar_fn_t searchCompare) {
1361
  int16_t widx = abs((int)(id % mgmt->slotNum));
D
dapan1121 已提交
1362

D
dapan1121 已提交
1363
  SCtgRentSlot *slot = &mgmt->slots[widx];
D
dapan1121 已提交
1364 1365 1366 1367 1368 1369 1370 1371 1372
  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 已提交
1373
    taosArraySort(slot->meta, sortCompare);
D
dapan1121 已提交
1374 1375 1376 1377
    slot->needSort = false;
    qDebug("meta slot sorted, slot idx:%d, type:%d", widx, mgmt->type);
  }

D
dapan1121 已提交
1378
  int32_t idx = taosArraySearchIdx(slot->meta, &id, searchCompare, TD_EQ);
D
dapan1121 已提交
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
  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 已提交
1396
int32_t ctgMetaRentGetImpl(SCtgRentMgmt *mgmt, void **res, uint32_t *num, int32_t size) {
D
dapan1121 已提交
1397 1398 1399 1400
  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 已提交
1401
  }
D
dapan1121 已提交
1402

D
dapan1121 已提交
1403
  SCtgRentSlot *slot = &mgmt->slots[ridx];
D
dapan1121 已提交
1404
  int32_t code = 0;
D
dapan1121 已提交
1405
  
D
dapan1121 已提交
1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
  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 已提交
1421
  *res = taosMemoryMalloc(msize);
D
dapan1121 已提交
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
  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 已提交
1442
int32_t ctgMetaRentGet(SCtgRentMgmt *mgmt, void **res, uint32_t *num, int32_t size) {
D
dapan1121 已提交
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461
  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 已提交
1462 1463 1464
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1465
int32_t ctgAddNewDBCache(SCatalog *pCtg, const char *dbFName, uint64_t dbId) {
D
dapan1121 已提交
1466
  int32_t code = 0;
D
dapan1121 已提交
1467

D
dapan1121 已提交
1468 1469 1470
  SCtgDBCache newDBCache = {0};
  newDBCache.dbId = dbId;

D
dapan 已提交
1471
  newDBCache.tbCache.metaCache = taosHashInit(gCtgMgmt.cfg.maxTblCacheNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
D
dapan1121 已提交
1472
  if (NULL == newDBCache.tbCache.metaCache) {
D
dapan 已提交
1473
    ctgError("taosHashInit %d metaCache failed", gCtgMgmt.cfg.maxTblCacheNum);
D
dapan1121 已提交
1474 1475 1476
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
  }

D
dapan 已提交
1477
  newDBCache.tbCache.stbCache = taosHashInit(gCtgMgmt.cfg.maxTblCacheNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), true, HASH_ENTRY_LOCK);
D
dapan1121 已提交
1478
  if (NULL == newDBCache.tbCache.stbCache) {
D
dapan 已提交
1479
    ctgError("taosHashInit %d stbCache failed", gCtgMgmt.cfg.maxTblCacheNum);
D
dapan1121 已提交
1480 1481 1482 1483
    CTG_ERR_JRET(TSDB_CODE_CTG_MEM_ERROR);
  }

  code = taosHashPut(pCtg->dbCache, dbFName, strlen(dbFName), &newDBCache, sizeof(SCtgDBCache));
D
dapan1121 已提交
1484 1485 1486
  if (code) {
    if (HASH_NODE_EXIST(code)) {
      ctgDebug("db already in cache, dbFName:%s", dbFName);
D
dapan1121 已提交
1487
      goto _return;
D
dapan1121 已提交
1488 1489 1490
    }
    
    ctgError("taosHashPut db to cache failed, dbFName:%s", dbFName);
D
dapan1121 已提交
1491 1492
    CTG_ERR_JRET(TSDB_CODE_CTG_MEM_ERROR);
  }
D
dapan1121 已提交
1493 1494

  CTG_CACHE_STAT_ADD(dbNum, 1);
D
dapan1121 已提交
1495
 
D
dapan1121 已提交
1496
  SDbVgVersion vgVersion = {.dbId = newDBCache.dbId, .vgVersion = -1};
D
dapan1121 已提交
1497 1498
  strncpy(vgVersion.dbFName, dbFName, sizeof(vgVersion.dbFName));

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

D
dapan1121 已提交
1501 1502 1503
  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 已提交
1504

D
dapan1121 已提交
1505
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1506

D
dapan1121 已提交
1507
_return:
D
dapan1121 已提交
1508

D
dapan1121 已提交
1509
  ctgFreeDbCache(&newDBCache);
D
dapan1121 已提交
1510

D
dapan1121 已提交
1511 1512
  CTG_RET(code);
}
D
dapan1121 已提交
1513

D
dapan1121 已提交
1514

D
dapan1121 已提交
1515
void ctgRemoveStbRent(SCatalog* pCtg, SCtgTbMetaCache *cache) {
D
dapan1121 已提交
1516 1517 1518 1519 1520
  CTG_LOCK(CTG_WRITE, &cache->stbLock);
  if (cache->stbCache) {
    void *pIter = taosHashIterate(cache->stbCache, NULL);
    while (pIter) {
      uint64_t *suid = NULL;
H
Haojun Liao 已提交
1521
      suid = taosHashGetKey(pIter, NULL);
D
dapan1121 已提交
1522

D
dapan1121 已提交
1523
      if (TSDB_CODE_SUCCESS == ctgMetaRentRemove(&pCtg->stbRent, *suid, ctgStbVersionSortCompare, ctgStbVersionSearchCompare)) {
D
dapan1121 已提交
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533
        ctgDebug("stb removed from rent, suid:%"PRIx64, *suid);
      }
          
      pIter = taosHashIterate(cache->stbCache, pIter);
    }
  }
  CTG_UNLOCK(CTG_WRITE, &cache->stbLock);
}


D
dapan1121 已提交
1534
int32_t ctgRemoveDB(SCatalog* pCtg, SCtgDBCache *dbCache, const char* dbFName) {
D
dapan 已提交
1535 1536 1537 1538
  uint64_t dbId = dbCache->dbId;
  
  ctgInfo("start to remove db from cache, dbFName:%s, dbId:%"PRIx64, dbFName, dbCache->dbId);

D
dapan1121 已提交
1539 1540 1541 1542 1543 1544
  atomic_store_8(&dbCache->deleted, 1);

  ctgRemoveStbRent(pCtg, &dbCache->tbCache);

  ctgFreeDbCache(dbCache);

D
dapan1121 已提交
1545
  CTG_ERR_RET(ctgMetaRentRemove(&pCtg->dbRent, dbCache->dbId, ctgDbVgVersionSortCompare, ctgDbVgVersionSearchCompare));
D
dapan1121 已提交
1546 1547 1548 1549
  
  ctgDebug("db removed from rent, dbFName:%s, dbId:%"PRIx64, dbFName, dbCache->dbId);

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

D
dapan1121 已提交
1554 1555
  CTG_CACHE_STAT_SUB(dbNum, 1);

D
dapan 已提交
1556
  ctgInfo("db removed from cache, dbFName:%s, dbId:%"PRIx64, dbFName, dbId);
D
dapan1121 已提交
1557 1558 1559
  
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
1560 1561


D
dapan1121 已提交
1562
int32_t ctgGetAddDBCache(SCatalog* pCtg, const char *dbFName, uint64_t dbId, SCtgDBCache **pCache) {
D
dapan1121 已提交
1563 1564
  int32_t code = 0;
  SCtgDBCache *dbCache = NULL;
D
dapan1121 已提交
1565
  ctgGetDBCache(pCtg, dbFName, &dbCache);
D
dapan1121 已提交
1566
  
D
dapan1121 已提交
1567 1568
  if (dbCache) {
  // TODO OPEN IT
D
dapan1121 已提交
1569
#if 0    
D
dapan1121 已提交
1570 1571 1572 1573
    if (dbCache->dbId == dbId) {
      *pCache = dbCache;
      return TSDB_CODE_SUCCESS;
    }
D
dapan1121 已提交
1574
#else
D
dapan1121 已提交
1575 1576 1577
    if (0 == dbId) {
      *pCache = dbCache;
      return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1578 1579
    }

D
dapan1121 已提交
1580 1581 1582 1583 1584
    if (dbId && (dbCache->dbId == 0)) {
      dbCache->dbId = dbId;
      *pCache = dbCache;
      return TSDB_CODE_SUCCESS;
    }
D
dapan1121 已提交
1585
    
D
dapan1121 已提交
1586 1587 1588 1589 1590 1591
    if (dbCache->dbId == dbId) {
      *pCache = dbCache;
      return TSDB_CODE_SUCCESS;
    }
#endif
    CTG_ERR_RET(ctgRemoveDB(pCtg, dbCache, dbFName));
D
dapan1121 已提交
1592
  }
D
dapan1121 已提交
1593 1594
  
  CTG_ERR_RET(ctgAddNewDBCache(pCtg, dbFName, dbId));
D
dapan1121 已提交
1595

D
dapan1121 已提交
1596
  ctgGetDBCache(pCtg, dbFName, &dbCache);
D
dapan1121 已提交
1597

D
dapan1121 已提交
1598
  *pCache = dbCache;
D
dapan1121 已提交
1599

D
dapan1121 已提交
1600
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1601 1602 1603
}


D
dapan1121 已提交
1604 1605 1606
int32_t ctgUpdateDBVgInfo(SCatalog* pCtg, const char* dbFName, uint64_t dbId, SDBVgInfo** pDbInfo) {
  int32_t code = 0;
  SDBVgInfo* dbInfo = *pDbInfo;
D
dapan1121 已提交
1607 1608 1609 1610

  if (NULL == dbInfo->vgHash) {
    return TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
1611
  
D
dapan1121 已提交
1612
  if (dbInfo->vgVersion < 0 || taosHashGetSize(dbInfo->vgHash) <= 0) {
D
dapan1121 已提交
1613 1614
    ctgError("invalid db vgInfo, dbFName:%s, vgHash:%p, vgVersion:%d, vgHashSize:%d", 
      dbFName, dbInfo->vgHash, dbInfo->vgVersion, taosHashGetSize(dbInfo->vgHash));
D
dapan1121 已提交
1615
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
D
dapan1121 已提交
1616 1617
  }

D
dapan1121 已提交
1618
  bool newAdded = false;
D
dapan 已提交
1619
  SDbVgVersion vgVersion = {.dbId = dbId, .vgVersion = dbInfo->vgVersion, .numOfTable = dbInfo->numOfTable};
D
dapan1121 已提交
1620 1621 1622 1623 1624 1625 1626 1627 1628 1629

  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 已提交
1630
  
D
dapan1121 已提交
1631
  if (dbCache->vgInfo) {
D
dapan 已提交
1632 1633 1634 1635 1636 1637 1638 1639 1640
    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 已提交
1641
      ctgWReleaseVgInfo(dbCache);
D
dapan1121 已提交
1642
      
D
dapan1121 已提交
1643
      return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1644
    }
D
dapan1121 已提交
1645 1646

    ctgFreeVgInfo(dbCache->vgInfo);
D
dapan1121 已提交
1647 1648
  }

D
dapan1121 已提交
1649
  dbCache->vgInfo = dbInfo;
D
dapan1121 已提交
1650

D
dapan1121 已提交
1651
  *pDbInfo = NULL;
D
dapan1121 已提交
1652

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

D
dapan1121 已提交
1655
  ctgWReleaseVgInfo(dbCache);
D
dapan1121 已提交
1656

D
dapan1121 已提交
1657
  dbCache = NULL;
D
dapan1121 已提交
1658

D
dapan1121 已提交
1659
  strncpy(vgVersion.dbFName, dbFName, sizeof(vgVersion.dbFName));
D
dapan1121 已提交
1660
  CTG_ERR_RET(ctgMetaRentUpdate(&pCtg->dbRent, &vgVersion, vgVersion.dbId, sizeof(SDbVgVersion), ctgDbVgVersionSortCompare, ctgDbVgVersionSearchCompare));
D
dapan1121 已提交
1661
  
D
dapan1121 已提交
1662 1663 1664 1665
  CTG_RET(code);
}


D
dapan1121 已提交
1666 1667
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 已提交
1668

D
dapan1121 已提交
1669 1670 1671 1672 1673
  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 已提交
1674 1675
  }

D
dapan1121 已提交
1676 1677 1678 1679 1680 1681
  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 已提交
1682 1683 1684 1685 1686

    if (origType == meta->tableType && orig->uid == meta->uid && orig->sversion >= meta->sversion && orig->tversion >= meta->tversion) {
      CTG_UNLOCK(CTG_READ, &tbCache->metaLock);  
      return TSDB_CODE_SUCCESS;
    }
D
dapan1121 已提交
1687
    
D
dapan1121 已提交
1688 1689 1690 1691 1692
    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 已提交
1693 1694
        } else {
          CTG_CACHE_STAT_SUB(stblNum, 1);
D
dapan1121 已提交
1695 1696 1697 1698 1699
        }
        CTG_UNLOCK(CTG_WRITE, &tbCache->stbLock);

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

D
dapan1121 已提交
1703
      origSuid = orig->suid;
D
dapan1121 已提交
1704
    }
D
dapan1121 已提交
1705
  }
D
dapan1121 已提交
1706

D
dapan1121 已提交
1707 1708
  if (isStb) {
    CTG_LOCK(CTG_WRITE, &tbCache->stbLock);
D
dapan1121 已提交
1709
  }
D
dapan1121 已提交
1710
  
D
dapan1121 已提交
1711 1712 1713 1714 1715 1716 1717 1718 1719
  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 已提交
1720

D
dapan1121 已提交
1721 1722 1723 1724
  if (NULL == orig) {
    CTG_CACHE_STAT_ADD(tblNum, 1);
  }

D
dapan1121 已提交
1725
  ctgDebug("tbmeta updated to cache, dbFName:%s, tbName:%s, tbType:%d, suid:%" PRIx64, dbFName, tbName, meta->tableType, meta->suid);
D
dapan1121 已提交
1726
  ctgdShowTableMeta(pCtg, tbName, meta);
D
dapan 已提交
1727

D
dapan1121 已提交
1728 1729 1730
  if (!isStb) {
    CTG_UNLOCK(CTG_READ, &tbCache->metaLock);  
    return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1731
  }
D
dapan1121 已提交
1732

D
dapan1121 已提交
1733 1734 1735 1736
  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 已提交
1737
    ctgError("taosHashPut stable to stable cache failed, suid:%"PRIx64, meta->suid);
D
dapan1121 已提交
1738
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
D
dapan1121 已提交
1739
  }
D
dapan1121 已提交
1740 1741

  CTG_CACHE_STAT_ADD(stblNum, 1);
D
dapan1121 已提交
1742 1743
  
  CTG_UNLOCK(CTG_WRITE, &tbCache->stbLock);
D
dapan1121 已提交
1744

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

D
dapan1121 已提交
1747
  ctgDebug("stb updated to stbCache, dbFName:%s, tbName:%s, tbType:%d, suid:%" PRIx64 ",ma:%p", dbFName, tbName, meta->tableType, meta->suid, tbMeta);
D
dapan1121 已提交
1748 1749 1750 1751 1752 1753 1754

  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 已提交
1755 1756
}

D
dapan 已提交
1757
int32_t ctgCloneVgInfo(SDBVgInfo *src, SDBVgInfo **dst) {
wafwerar's avatar
wafwerar 已提交
1758
  *dst = taosMemoryMalloc(sizeof(SDBVgInfo));
D
dapan 已提交
1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769
  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 已提交
1770
    taosMemoryFreeClear(*dst);
D
dapan 已提交
1771 1772 1773 1774 1775 1776
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
  }

  int32_t *vgId = NULL;
  void *pIter = taosHashIterate(src->vgHash, NULL);
  while (pIter) {
H
Haojun Liao 已提交
1777
    vgId = taosHashGetKey(pIter, NULL);
D
dapan 已提交
1778 1779 1780 1781 1782

    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 已提交
1783
      taosMemoryFreeClear(*dst);
D
dapan 已提交
1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
      CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
    }
    
    pIter = taosHashIterate(src->vgHash, pIter);
  }


  return TSDB_CODE_SUCCESS;
}



D
dapan1121 已提交
1796
int32_t ctgGetDBVgInfo(SCatalog* pCtg, void *pRpc, const SEpSet* pMgmtEps, const char* dbFName, SCtgDBCache** dbCache, SDBVgInfo **pInfo) {
D
dapan1121 已提交
1797
  bool inCache = false;
D
dapan1121 已提交
1798
  int32_t code = 0;
D
dapan1121 已提交
1799 1800 1801

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

D
dapan1121 已提交
1802
  if (inCache) {
D
dapan1121 已提交
1803
    return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1804 1805 1806 1807 1808
  }

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

D
dapan1121 已提交
1809
  tstrncpy(input.db, dbFName, tListLen(input.db));
D
dapan1121 已提交
1810
  input.vgVersion = CTG_DEFAULT_INVALID_VERSION;
H
Haojun Liao 已提交
1811

D
dapan1121 已提交
1812 1813 1814 1815 1816 1817 1818 1819 1820
  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 已提交
1821

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

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

D
dapan1121 已提交
1826
  return TSDB_CODE_SUCCESS;
D
dapan 已提交
1827 1828 1829

_return:

wafwerar's avatar
wafwerar 已提交
1830
  taosMemoryFreeClear(*pInfo);
D
dapan 已提交
1831 1832 1833
  *pInfo = DbOut.dbVgroup;
  
  CTG_RET(code);
D
dapan1121 已提交
1834 1835
}

D
dapan1121 已提交
1836 1837 1838
int32_t ctgRefreshDBVgInfo(SCatalog* pCtg, void *pRpc, const SEpSet* pMgmtEps, const char* dbFName) {
  bool inCache = false;
  int32_t code = 0;
D
dapan1121 已提交
1839
  SCtgDBCache* dbCache = NULL;
D
dapan1121 已提交
1840

D
dapan1121 已提交
1841
  CTG_ERR_RET(ctgAcquireVgInfoFromCache(pCtg, dbFName, &dbCache, &inCache));
D
dapan1121 已提交
1842 1843 1844 1845 1846 1847

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

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

D
dapan1121 已提交
1850 1851
    ctgReleaseVgInfo(dbCache);
    ctgReleaseDBCache(pCtg, dbCache);
D
dapan1121 已提交
1852
  }
D
dapan1121 已提交
1853 1854 1855
  
  input.vgVersion = CTG_DEFAULT_INVALID_VERSION;
  input.numOfTable = 0;
D
dapan1121 已提交
1856 1857 1858

  code = ctgGetDBVgInfoFromMnode(pCtg, pRpc, pMgmtEps, &input, &DbOut);
  if (code) {
D
dapan1121 已提交
1859
    if (CTG_DB_NOT_EXIST(code) && inCache) {
D
dapan1121 已提交
1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872
      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 已提交
1873

D
dapan1121 已提交
1874
int32_t ctgCloneMetaOutput(STableMetaOutput *output, STableMetaOutput **pOutput) {
wafwerar's avatar
wafwerar 已提交
1875
  *pOutput = taosMemoryMalloc(sizeof(STableMetaOutput));
D
dapan1121 已提交
1876 1877 1878
  if (NULL == *pOutput) {
    qError("malloc %d failed", (int32_t)sizeof(STableMetaOutput));
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
D
dapan 已提交
1879 1880
  }

D
dapan1121 已提交
1881 1882 1883 1884
  memcpy(*pOutput, output, sizeof(STableMetaOutput));

  if (output->tbMeta) {
    int32_t metaSize = CTG_META_SIZE(output->tbMeta);
wafwerar's avatar
wafwerar 已提交
1885
    (*pOutput)->tbMeta = taosMemoryMalloc(metaSize);
D
dapan1121 已提交
1886
    if (NULL == (*pOutput)->tbMeta) {
D
dapan 已提交
1887
      qError("malloc %d failed", (int32_t)sizeof(STableMetaOutput));
wafwerar's avatar
wafwerar 已提交
1888
      taosMemoryFreeClear(*pOutput);
D
dapan1121 已提交
1889 1890 1891 1892
      CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
    }

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

D
dapan1121 已提交
1895 1896 1897
  return TSDB_CODE_SUCCESS;
}

D
dapan 已提交
1898 1899


D
dapan1121 已提交
1900
int32_t ctgRefreshTblMeta(SCatalog* pCtg, void *pTrans, const SEpSet* pMgmtEps, const SName* pTableName, int32_t flag, STableMetaOutput **pOutput, bool syncReq) {
D
dapan1121 已提交
1901
  if (NULL == pCtg || NULL == pTrans || NULL == pMgmtEps || NULL == pTableName) {
D
dapan1121 已提交
1902 1903 1904 1905 1906 1907
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
  }

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

D
dapan1121 已提交
1908
  if (!CTG_FLAG_IS_SYS_DB(flag)) {
D
dapan1121 已提交
1909 1910
    CTG_ERR_RET(catalogGetTableHashVgroup(pCtg, pTrans, pMgmtEps, pTableName, &vgroupInfo));
  }
D
dapan1121 已提交
1911

D
dapan1121 已提交
1912
  STableMetaOutput  moutput = {0};
wafwerar's avatar
wafwerar 已提交
1913
  STableMetaOutput *output = taosMemoryCalloc(1, sizeof(STableMetaOutput));
D
dapan1121 已提交
1914 1915 1916 1917
  if (NULL == output) {
    ctgError("malloc %d failed", (int32_t)sizeof(STableMetaOutput));
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
  }
D
dapan1121 已提交
1918

D
dapan1121 已提交
1919
  if (CTG_FLAG_IS_SYS_DB(flag)) {
D
dapan1121 已提交
1920 1921 1922
    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 已提交
1923
  } else if (CTG_FLAG_IS_STB(flag)) {
D
dapan 已提交
1924
    ctgDebug("will refresh tbmeta, supposed to be stb, tbName:%s", tNameGetTableName(pTableName));
D
dapan1121 已提交
1925 1926

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

D
dapan1121 已提交
1929
    if (CTG_IS_META_NULL(output->metaType)) {
D
dapan1121 已提交
1930
      CTG_ERR_JRET(ctgGetTableMetaFromVnode(pCtg, pTrans, pMgmtEps, pTableName, &vgroupInfo, output));
D
dapan1121 已提交
1931 1932
    }
  } else {
D
dapan1121 已提交
1933
    ctgDebug("will refresh tbmeta, not supposed to be stb, tbName:%s, flag:%d", tNameGetTableName(pTableName), flag);
D
dapan1121 已提交
1934 1935

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

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

wafwerar's avatar
wafwerar 已提交
1941
      taosMemoryFreeClear(output->tbMeta);
D
dapan1121 已提交
1942
      
D
dapan1121 已提交
1943
      CTG_ERR_JRET(ctgGetTableMetaFromMnodeImpl(pCtg, pTrans, pMgmtEps, output->dbFName, output->tbName, output));
D
dapan1121 已提交
1944
    } else if (CTG_IS_META_BOTH(output->metaType)) {
D
dapan1121 已提交
1945
      int32_t exist = 0;
D
dapan1121 已提交
1946 1947 1948
      if (!CTG_FLAG_IS_FORCE_UPDATE(flag)) {
        CTG_ERR_JRET(ctgIsTableMetaExistInCache(pCtg, output->dbFName, output->tbName, &exist));
      }
H
Haojun Liao 已提交
1949

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

D
dapan1121 已提交
1953
        if (CTG_IS_META_NULL(moutput.metaType)) {
D
dapan1121 已提交
1954
          SET_META_TYPE_NULL(output->metaType);
D
dapan1121 已提交
1955 1956
        }
        
wafwerar's avatar
wafwerar 已提交
1957
        taosMemoryFreeClear(output->tbMeta);
D
dapan1121 已提交
1958
        output->tbMeta = moutput.tbMeta;
D
dapan1121 已提交
1959 1960
        moutput.tbMeta = NULL;
      } else {
wafwerar's avatar
wafwerar 已提交
1961
        taosMemoryFreeClear(output->tbMeta);
D
dapan1121 已提交
1962
        
D
dapan1121 已提交
1963
        SET_META_TYPE_CTABLE(output->metaType); 
D
dapan1121 已提交
1964
      }
D
dapan1121 已提交
1965 1966 1967
    }
  }

D
dapan1121 已提交
1968
  if (CTG_IS_META_NULL(output->metaType)) {
D
dapan 已提交
1969
    ctgError("no tbmeta got, tbNmae:%s", tNameGetTableName(pTableName));
D
dapan1121 已提交
1970
    catalogRemoveTableMeta(pCtg, pTableName);
D
dapan1121 已提交
1971 1972 1973
    CTG_ERR_JRET(CTG_ERR_CODE_TABLE_NOT_EXIST);
  }

D
dapan 已提交
1974 1975 1976 1977 1978 1979
  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 已提交
1980 1981 1982 1983
  if (pOutput) {
    CTG_ERR_JRET(ctgCloneMetaOutput(output, pOutput));
  }

D
dapan1121 已提交
1984
  CTG_ERR_JRET(ctgPushUpdateTblMsgInQueue(pCtg, output, syncReq));
D
dapan 已提交
1985

D
dapan1121 已提交
1986
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1987 1988 1989

_return:

wafwerar's avatar
wafwerar 已提交
1990 1991
  taosMemoryFreeClear(output->tbMeta);
  taosMemoryFreeClear(output);
D
dapan1121 已提交
1992 1993 1994 1995
  
  CTG_RET(code);
}

D
dapan1121 已提交
1996
int32_t ctgGetTableMeta(SCatalog* pCtg, void *pRpc, const SEpSet* pMgmtEps, const SName* pTableName, STableMeta** pTableMeta, int32_t flag) {
D
dapan1121 已提交
1997
  if (NULL == pCtg || NULL == pRpc || NULL == pMgmtEps || NULL == pTableName || NULL == pTableMeta) {
D
dapan1121 已提交
1998 1999
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
  }
D
dapan1121 已提交
2000

D
dapan1121 已提交
2001
  bool inCache = false;
D
dapan1121 已提交
2002
  int32_t code = 0;
D
dapan1121 已提交
2003 2004 2005
  uint64_t dbId = 0;
  uint64_t suid = 0;
  STableMetaOutput *output = NULL;
D
dapan1121 已提交
2006

D
dapan1121 已提交
2007 2008
  if (CTG_IS_SYS_DBNAME(pTableName->dbname)) {
    CTG_FLAG_SET_SYS_DB(flag);
D
dapan1121 已提交
2009 2010
  }

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

D
dapan1121 已提交
2013 2014
  int32_t tbType = 0;

D
dapan1121 已提交
2015
  if (inCache) {
D
dapan1121 已提交
2016
    if (CTG_FLAG_MATCH_STB(flag, (*pTableMeta)->tableType) && ((!CTG_FLAG_IS_FORCE_UPDATE(flag)) || (CTG_FLAG_IS_SYS_DB(flag)))) {
D
dapan1121 已提交
2017
      goto _return;
D
dapan1121 已提交
2018
    }
D
dapan1121 已提交
2019

D
dapan1121 已提交
2020 2021
    tbType = (*pTableMeta)->tableType;
    suid = (*pTableMeta)->suid;
D
dapan1121 已提交
2022

wafwerar's avatar
wafwerar 已提交
2023
    taosMemoryFreeClear(*pTableMeta);
D
dapan1121 已提交
2024
  }
D
dapan1121 已提交
2025

D
dapan1121 已提交
2026 2027
  if (CTG_FLAG_IS_UNKNOWN_STB(flag)) {
    CTG_FLAG_SET_STB(flag, tbType);
D
dapan1121 已提交
2028 2029
  }

D
dapan1121 已提交
2030

D
dapan 已提交
2031
  while (true) {
D
dapan1121 已提交
2032
    CTG_ERR_JRET(ctgRefreshTblMeta(pCtg, pRpc, pMgmtEps, pTableName, flag, &output, false));
D
dapan1121 已提交
2033

D
dapan 已提交
2034 2035 2036 2037
    if (CTG_IS_META_TABLE(output->metaType)) {
      *pTableMeta = output->tbMeta;
      goto _return;
    }
D
dapan1121 已提交
2038

D
dapan 已提交
2039 2040 2041 2042 2043 2044
    if (CTG_IS_META_BOTH(output->metaType)) {
      memcpy(output->tbMeta, &output->ctbMeta, sizeof(output->ctbMeta));
      
      *pTableMeta = output->tbMeta;
      goto _return;
    }
D
dapan1121 已提交
2045

D
dapan 已提交
2046 2047
    if ((!CTG_IS_META_CTABLE(output->metaType)) || output->tbMeta) {
      ctgError("invalid metaType:%d", output->metaType);
wafwerar's avatar
wafwerar 已提交
2048
      taosMemoryFreeClear(output->tbMeta);
D
dapan 已提交
2049 2050
      CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
    }
D
dapan1121 已提交
2051

D
dapan 已提交
2052
    // HANDLE ONLY CHILD TABLE META
D
dapan1121 已提交
2053

D
dapan 已提交
2054 2055
    SName stbName = *pTableName;
    strcpy(stbName.tname, output->tbName);
D
dapan1121 已提交
2056 2057

    taosMemoryFreeClear(output->tbMeta);
D
dapan 已提交
2058
    
D
dapan1121 已提交
2059 2060
    CTG_ERR_JRET(ctgGetTableMetaFromCache(pCtg, &stbName, pTableMeta, &inCache, flag, NULL));
    if (!inCache) {
D
dapan 已提交
2061
      ctgDebug("stb no longer exist, dbFName:%s, tbName:%s", output->dbFName, pTableName->tname);
D
dapan1121 已提交
2062
      
D
dapan 已提交
2063 2064
      continue;
    }
D
dapan1121 已提交
2065

D
dapan 已提交
2066 2067 2068 2069
    memcpy(*pTableMeta, &output->ctbMeta, sizeof(output->ctbMeta));

    break;
  }
D
dapan1121 已提交
2070 2071 2072

_return:

D
dapan1121 已提交
2073
  if (CTG_TABLE_NOT_EXIST(code) && inCache) {
D
dapan1121 已提交
2074
    char dbFName[TSDB_DB_FNAME_LEN] = {0};
D
dapan1121 已提交
2075
    if (CTG_FLAG_IS_SYS_DB(flag)) {
D
dapan1121 已提交
2076 2077 2078 2079 2080 2081
      strcpy(dbFName, pTableName->dbname);
    } else {
      tNameGetFullDbName(pTableName, dbFName);
    }

    if (TSDB_SUPER_TABLE == tbType) {
D
dapan1121 已提交
2082
      ctgPushRmStbMsgInQueue(pCtg, dbFName, dbId, pTableName->tname, suid, false);
D
dapan1121 已提交
2083
    } else {
D
dapan1121 已提交
2084
      ctgPushRmTblMsgInQueue(pCtg, dbFName, dbId, pTableName->tname, false);
D
dapan1121 已提交
2085 2086 2087
    }
  }

wafwerar's avatar
wafwerar 已提交
2088
  taosMemoryFreeClear(output);
D
dapan1121 已提交
2089

D
dapan 已提交
2090 2091
  if (*pTableMeta) {
    ctgDebug("tbmeta returned, tbName:%s, tbType:%d", pTableName->tname, (*pTableMeta)->tableType);
D
dapan1121 已提交
2092
    ctgdShowTableMeta(pCtg, pTableName->tname, *pTableMeta);
D
dapan 已提交
2093 2094
  }

D
dapan1121 已提交
2095 2096 2097
  CTG_RET(code);
}

D
dapan 已提交
2098
int32_t ctgChkAuth(SCatalog* pCtg, void *pRpc, const SEpSet* pMgmtEps, const char* user, const char* dbFName, AUTH_TYPE type, bool *pass) {
D
dapan 已提交
2099 2100 2101
  bool inCache = false;
  int32_t code = 0;
  
D
dapan 已提交
2102 2103 2104
  *pass = false;
  
  CTG_ERR_RET(ctgChkAuthFromCache(pCtg, user, dbFName, type, &inCache, pass));
D
dapan 已提交
2105 2106 2107 2108 2109 2110 2111 2112 2113

  if (inCache) {
    return TSDB_CODE_SUCCESS;
  }

  SGetUserAuthRsp authRsp = {0};
  CTG_ERR_RET(ctgGetUserDbAuthFromMnode(pCtg, pRpc, pMgmtEps, user, &authRsp));
  
  if (authRsp.superAuth) {
D
dapan 已提交
2114
    *pass = true;
D
dapan 已提交
2115 2116 2117 2118
    goto _return;
  }

  if (authRsp.createdDbs && taosHashGet(authRsp.createdDbs, dbFName, strlen(dbFName))) {
D
dapan 已提交
2119
    *pass = true;
D
dapan 已提交
2120 2121 2122
    goto _return;
  }

D
dapan 已提交
2123 2124
  if (authRsp.readDbs && taosHashGet(authRsp.readDbs, dbFName, strlen(dbFName)) && type == AUTH_TYPE_READ) {
    *pass = true;
D
dapan 已提交
2125 2126
  }

D
dapan 已提交
2127 2128
  if (authRsp.writeDbs && taosHashGet(authRsp.writeDbs, dbFName, strlen(dbFName)) && type == AUTH_TYPE_WRITE) {
    *pass = true;
D
dapan 已提交
2129 2130 2131 2132 2133 2134 2135 2136
  }

_return:

  ctgPushUpdateUserMsgInQueue(pCtg, &authRsp, false);

  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
2137 2138 2139 2140 2141 2142 2143 2144 2145 2146


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 已提交
2147
  ctgFreeVgInfo(msg->dbInfo);
wafwerar's avatar
wafwerar 已提交
2148
  taosMemoryFreeClear(msg);
D
dapan1121 已提交
2149 2150 2151 2152 2153 2154 2155 2156 2157 2158
  
  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 已提交
2159
  ctgGetDBCache(msg->pCtg, msg->dbFName, &dbCache);
D
dapan1121 已提交
2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172
  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 已提交
2173
  taosMemoryFreeClear(msg);
D
dapan1121 已提交
2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187
  
  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 已提交
2188
    CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
D
dapan1121 已提交
2189 2190 2191 2192 2193 2194
  }

  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 已提交
2195
  
D
dapan1121 已提交
2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213
  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 已提交
2214
  if (output) {
wafwerar's avatar
wafwerar 已提交
2215 2216
    taosMemoryFreeClear(output->tbMeta);
    taosMemoryFreeClear(output);
D
dapan1121 已提交
2217
  }
D
dapan 已提交
2218
  
wafwerar's avatar
wafwerar 已提交
2219
  taosMemoryFreeClear(msg);
D
dapan1121 已提交
2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235
  
  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 已提交
2236
  if (msg->dbId && (dbCache->dbId != msg->dbId)) {
D
dapan1121 已提交
2237 2238 2239 2240 2241 2242 2243
    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 已提交
2244 2245
  } else {
    CTG_CACHE_STAT_SUB(stblNum, 1);
D
dapan1121 已提交
2246 2247 2248 2249 2250
  }

  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 已提交
2251 2252 2253
  } else {
    CTG_CACHE_STAT_SUB(tblNum, 1);
  }
D
dapan1121 已提交
2254 2255 2256 2257 2258 2259
  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 已提交
2260
  CTG_ERR_JRET(ctgMetaRentRemove(&msg->pCtg->stbRent, msg->suid, ctgStbVersionSortCompare, ctgStbVersionSearchCompare));
D
dapan1121 已提交
2261 2262 2263 2264 2265
  
  ctgDebug("stb removed from rent, dbFName:%s, stbName:%s, suid:%"PRIx64, msg->dbFName, msg->stbName, msg->suid);
  
_return:

wafwerar's avatar
wafwerar 已提交
2266
  taosMemoryFreeClear(msg);
D
dapan1121 已提交
2267 2268 2269 2270 2271
  
  CTG_RET(code);
}

int32_t ctgActRemoveTbl(SCtgMetaAction *action) {
D
dapan1121 已提交
2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285
  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 已提交
2286

D
dapan1121 已提交
2287
  CTG_LOCK(CTG_READ, &dbCache->tbCache.metaLock);
H
Haojun Liao 已提交
2288
  if (taosHashRemove(dbCache->tbCache.metaCache, msg->tbName, strlen(msg->tbName))) {
D
dapan1121 已提交
2289 2290 2291
    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 已提交
2292 2293
  } else {
    CTG_CACHE_STAT_SUB(tblNum, 1);
H
Haojun Liao 已提交
2294
  }
D
dapan1121 已提交
2295
  CTG_UNLOCK(CTG_READ, &dbCache->tbCache.metaLock);
D
dapan1121 已提交
2296

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

D
dapan1121 已提交
2299
_return:
D
dapan1121 已提交
2300

wafwerar's avatar
wafwerar 已提交
2301
  taosMemoryFreeClear(msg);
H
Haojun Liao 已提交
2302

D
dapan1121 已提交
2303
  CTG_RET(code);
D
dapan1121 已提交
2304 2305
}

D
dapan 已提交
2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333
int32_t ctgActUpdateUser(SCtgMetaAction *action) {
  int32_t code = 0;
  SCtgUpdateUserMsg *msg = action->data;
  SCatalog* pCtg = msg->pCtg;

  if (NULL == pCtg->userCache) {
    pCtg->userCache = taosHashInit(gCtgMgmt.cfg.maxUserCacheNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
    if (NULL == pCtg->userCache) {
      ctgError("taosHashInit %d user cache failed", gCtgMgmt.cfg.maxUserCacheNum);
      CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }
  
  SCtgUserAuth *pUser = (SCtgUserAuth *)taosHashGet(pCtg->userCache, msg->userAuth.user, strlen(msg->userAuth.user));
  if (NULL == pUser) {
    SCtgUserAuth userAuth = {0};

    userAuth.version = msg->userAuth.version;
    userAuth.superUser = msg->userAuth.superAuth;
    userAuth.createdDbs = msg->userAuth.createdDbs;
    userAuth.readDbs = msg->userAuth.readDbs;
    userAuth.writeDbs = msg->userAuth.writeDbs;

    if (taosHashPut(pCtg->userCache, msg->userAuth.user, strlen(msg->userAuth.user), &userAuth, sizeof(userAuth))) {
      ctgError("taosHashPut user %s to cache failed", msg->userAuth.user);
      CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
    }

D
dapan1121 已提交
2334 2335
    taosMemoryFreeClear(msg);

D
dapan 已提交
2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368
    return TSDB_CODE_SUCCESS;
  }

  pUser->version = msg->userAuth.version;

  CTG_LOCK(CTG_WRITE, &pUser->lock);

  taosHashCleanup(pUser->createdDbs);
  pUser->createdDbs = msg->userAuth.createdDbs;
  msg->userAuth.createdDbs = NULL;

  taosHashCleanup(pUser->readDbs);
  pUser->readDbs = msg->userAuth.readDbs;
  msg->userAuth.readDbs = NULL;

  taosHashCleanup(pUser->writeDbs);
  pUser->writeDbs = msg->userAuth.writeDbs;
  msg->userAuth.writeDbs = NULL;

  CTG_UNLOCK(CTG_WRITE, &pUser->lock);

_return:


  taosHashCleanup(msg->userAuth.createdDbs);
  taosHashCleanup(msg->userAuth.readDbs);
  taosHashCleanup(msg->userAuth.writeDbs);
 
  taosMemoryFreeClear(msg);
  
  CTG_RET(code);
}

D
dapan1121 已提交
2369 2370 2371 2372 2373 2374

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

  qInfo("catalog update thread started");

D
dapan 已提交
2375
  CTG_LOCK(CTG_READ, &gCtgMgmt.lock);
D
dapan1121 已提交
2376 2377
  
  while (true) {
D
dapan1121 已提交
2378 2379 2380
    if (tsem_wait(&gCtgMgmt.queue.reqSem)) {
      qError("ctg tsem_wait failed, error:%s", tstrerror(TAOS_SYSTEM_ERROR(errno)));
    }
D
dapan1121 已提交
2381
    
wafwerar's avatar
wafwerar 已提交
2382
    if (atomic_load_8((int8_t*)&gCtgMgmt.exit)) {
D
dapan1121 已提交
2383
      tsem_post(&gCtgMgmt.queue.rspSem);
D
dapan1121 已提交
2384 2385 2386 2387 2388
      break;
    }

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

D
dapan1121 已提交
2391
    ctgDebug("process [%s] action", gCtgAction[action->act].name);
D
dapan 已提交
2392 2393 2394
    
    (*gCtgAction[action->act].func)(action);

D
dapan1121 已提交
2395 2396 2397 2398 2399 2400
    gCtgMgmt.queue.seqDone = action->seqId;

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

D
dapan1121 已提交
2401
    CTG_RUNTIME_STAT_ADD(qDoneNum, 1); 
D
dapan1121 已提交
2402

D
dapan1121 已提交
2403
    ctgdShowClusterCache(pCtg);
D
dapan1121 已提交
2404 2405
  }

D
dapan 已提交
2406
  CTG_UNLOCK(CTG_READ, &gCtgMgmt.lock);
D
dapan1121 已提交
2407 2408 2409 2410 2411 2412 2413 2414

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


int32_t ctgStartUpdateThread() {
wafwerar's avatar
wafwerar 已提交
2415 2416 2417
  TdThreadAttr thAttr;
  taosThreadAttrInit(&thAttr);
  taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
D
dapan1121 已提交
2418

wafwerar's avatar
wafwerar 已提交
2419
  if (taosThreadCreate(&gCtgMgmt.updateThread, &thAttr, ctgUpdateThreadFunc, NULL) != 0) {
D
dapan1121 已提交
2420 2421
    terrno = TAOS_SYSTEM_ERROR(errno);
    CTG_ERR_RET(terrno);
D
dapan1121 已提交
2422 2423
  }
  
wafwerar's avatar
wafwerar 已提交
2424
  taosThreadAttrDestroy(&thAttr);
D
dapan1121 已提交
2425 2426 2427
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443
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 已提交
2444
  CTG_ERR_JRET(ctgGetDBVgInfo(pCtg, pRpc, pMgmtEps, db, &dbCache, &vgInfo));
D
dapan1121 已提交
2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488

  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 已提交
2489
  taosMemoryFreeClear(tbMeta);
D
dapan1121 已提交
2490 2491 2492

  if (vgInfo) {
    taosHashCleanup(vgInfo->vgHash);
wafwerar's avatar
wafwerar 已提交
2493
    taosMemoryFreeClear(vgInfo);
D
dapan1121 已提交
2494 2495 2496 2497 2498 2499 2500 2501 2502 2503
  }

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

  CTG_RET(code);
}

D
dapan1121 已提交
2504
int32_t catalogInit(SCatalogCfg *cfg) {
D
dapan 已提交
2505
  if (gCtgMgmt.pCluster) {
D
dapan 已提交
2506
    qError("catalog already initialized");
D
dapan1121 已提交
2507
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
D
dapan1121 已提交
2508 2509
  }

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

D
dapan1121 已提交
2512
  if (cfg) {
D
dapan 已提交
2513
    memcpy(&gCtgMgmt.cfg, cfg, sizeof(*cfg));
H
Haojun Liao 已提交
2514

D
dapan 已提交
2515 2516
    if (gCtgMgmt.cfg.maxDBCacheNum == 0) {
      gCtgMgmt.cfg.maxDBCacheNum = CTG_DEFAULT_CACHE_DB_NUMBER;
D
dapan1121 已提交
2517 2518
    }

D
dapan 已提交
2519 2520
    if (gCtgMgmt.cfg.maxTblCacheNum == 0) {
      gCtgMgmt.cfg.maxTblCacheNum = CTG_DEFAULT_CACHE_TBLMETA_NUMBER;
D
dapan1121 已提交
2521
    }
D
dapan1121 已提交
2522

D
dapan 已提交
2523 2524
    if (gCtgMgmt.cfg.dbRentSec == 0) {
      gCtgMgmt.cfg.dbRentSec = CTG_DEFAULT_RENT_SECOND;
D
dapan1121 已提交
2525 2526
    }

D
dapan 已提交
2527 2528
    if (gCtgMgmt.cfg.stbRentSec == 0) {
      gCtgMgmt.cfg.stbRentSec = CTG_DEFAULT_RENT_SECOND;
D
dapan1121 已提交
2529
    }
D
dapan1121 已提交
2530
  } else {
D
dapan 已提交
2531 2532 2533 2534
    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 已提交
2535 2536
  }

D
dapan 已提交
2537 2538
  gCtgMgmt.pCluster = taosHashInit(CTG_DEFAULT_CACHE_CLUSTER_NUMBER, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
  if (NULL == gCtgMgmt.pCluster) {
D
dapan1121 已提交
2539 2540
    qError("taosHashInit %d cluster cache failed", CTG_DEFAULT_CACHE_CLUSTER_NUMBER);
    CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
D
dapan1121 已提交
2541 2542
  }

D
dapan1121 已提交
2543 2544 2545 2546 2547 2548 2549 2550 2551
  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 已提交
2552

wafwerar's avatar
wafwerar 已提交
2553
  gCtgMgmt.queue.head = taosMemoryCalloc(1, sizeof(SCtgQNode));
D
dapan1121 已提交
2554
  if (NULL == gCtgMgmt.queue.head) {
D
dapan1121 已提交
2555 2556 2557
    qError("calloc %d failed", (int32_t)sizeof(SCtgQNode));
    CTG_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }
D
dapan1121 已提交
2558
  gCtgMgmt.queue.tail = gCtgMgmt.queue.head;
D
dapan1121 已提交
2559

D
dapan1121 已提交
2560 2561
  CTG_ERR_RET(ctgStartUpdateThread());

D
dapan 已提交
2562
  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 已提交
2563

D
dapan 已提交
2564
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
2565 2566
}

D
dapan1121 已提交
2567
int32_t catalogGetHandle(uint64_t clusterId, SCatalog** catalogHandle) {
2568
  if (NULL == catalogHandle) {
D
dapan1121 已提交
2569
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
D
dapan 已提交
2570 2571
  }

D
dapan 已提交
2572
  if (NULL == gCtgMgmt.pCluster) {
D
dapan 已提交
2573
    qError("catalog cluster cache are not ready, clusterId:%"PRIx64, clusterId);
D
dapan1121 已提交
2574
    CTG_ERR_RET(TSDB_CODE_CTG_NOT_READY);
D
dapan 已提交
2575 2576
  }

D
dapan1121 已提交
2577 2578
  int32_t code = 0;
  SCatalog *clusterCtg = NULL;
D
dapan 已提交
2579

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

D
dapan1121 已提交
2583 2584 2585 2586 2587
    if (ctg && (*ctg)) {
      *catalogHandle = *ctg;
      qDebug("got catalog handle from cache, clusterId:%"PRIx64", CTG:%p", clusterId, *ctg);
      return TSDB_CODE_SUCCESS;
    }
D
dapan 已提交
2588

wafwerar's avatar
wafwerar 已提交
2589
    clusterCtg = taosMemoryCalloc(1, sizeof(SCatalog));
D
dapan1121 已提交
2590 2591 2592 2593 2594
    if (NULL == clusterCtg) {
      qError("calloc %d failed", (int32_t)sizeof(SCatalog));
      CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
    }

D
dapan1121 已提交
2595 2596
    clusterCtg->clusterId = clusterId;

D
dapan 已提交
2597 2598
    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 已提交
2599

D
dapan 已提交
2600
    clusterCtg->dbCache = taosHashInit(gCtgMgmt.cfg.maxDBCacheNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
D
dapan1121 已提交
2601 2602 2603 2604 2605
    if (NULL == clusterCtg->dbCache) {
      qError("taosHashInit %d dbCache failed", CTG_DEFAULT_CACHE_DB_NUMBER);
      CTG_ERR_JRET(TSDB_CODE_CTG_MEM_ERROR);
    }

D
dapan 已提交
2606
    code = taosHashPut(gCtgMgmt.pCluster, &clusterId, sizeof(clusterId), &clusterCtg, POINTER_BYTES);
D
dapan1121 已提交
2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619
    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 已提交
2620
  }
D
dapan1121 已提交
2621 2622

  *catalogHandle = clusterCtg;
D
dapan1121 已提交
2623 2624

  CTG_CACHE_STAT_ADD(clusterNum, 1);
D
dapan 已提交
2625
  
D
dapan1121 已提交
2626
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
2627 2628 2629 2630 2631 2632 2633 2634

_return:

  ctgFreeHandle(clusterCtg);
  
  CTG_RET(code);
}

D
dapan1121 已提交
2635 2636
void catalogFreeHandle(SCatalog* pCtg) {
  if (NULL == pCtg) {
D
dapan1121 已提交
2637 2638
    return;
  }
D
dapan1121 已提交
2639

D
dapan 已提交
2640
  if (taosHashRemove(gCtgMgmt.pCluster, &pCtg->clusterId, sizeof(pCtg->clusterId))) {
D
dapan1121 已提交
2641
    ctgWarn("taosHashRemove from cluster failed, may already be freed, clusterId:%"PRIx64, pCtg->clusterId);
D
dapan1121 已提交
2642 2643 2644
    return;
  }

D
dapan1121 已提交
2645 2646
  CTG_CACHE_STAT_SUB(clusterNum, 1);

D
dapan1121 已提交
2647
  uint64_t clusterId = pCtg->clusterId;
D
dapan1121 已提交
2648
  
D
dapan1121 已提交
2649
  ctgFreeHandle(pCtg);
D
dapan1121 已提交
2650
  
D
dapan1121 已提交
2651
  ctgInfo("handle freed, culsterId:%"PRIx64, clusterId);
D
dapan 已提交
2652 2653
}

D
dapan1121 已提交
2654
int32_t catalogGetDBVgVersion(SCatalog* pCtg, const char* dbFName, int32_t* version, int64_t* dbId, int32_t *tableNum) {
D
dapan1121 已提交
2655 2656
  CTG_API_ENTER();

D
dapan1121 已提交
2657
  if (NULL == pCtg || NULL == dbFName || NULL == version || NULL == dbId) {
D
dapan1121 已提交
2658 2659 2660 2661
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

  SCtgDBCache *dbCache = NULL;
D
dapan 已提交
2662
  bool inCache = false;
D
dapan1121 已提交
2663
  int32_t code = 0;
D
dapan1121 已提交
2664

D
dapan1121 已提交
2665 2666
  CTG_ERR_JRET(ctgAcquireVgInfoFromCache(pCtg, dbFName, &dbCache, &inCache));
  if (!inCache) {
D
dapan1121 已提交
2667
    *version = CTG_DEFAULT_INVALID_VERSION;
D
dapan1121 已提交
2668
    CTG_API_LEAVE(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
2669 2670
  }

D
dapan1121 已提交
2671
  *version = dbCache->vgInfo->vgVersion;
D
dapan1121 已提交
2672
  *dbId = dbCache->dbId;
D
dapan1121 已提交
2673
  *tableNum = dbCache->vgInfo->numOfTable;
D
dapan1121 已提交
2674 2675 2676

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

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

D
dapan1121 已提交
2680
  CTG_API_LEAVE(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
2681 2682 2683 2684

_return:

  CTG_API_LEAVE(code);
D
dapan1121 已提交
2685 2686
}

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

D
dapan1121 已提交
2690 2691 2692
  if (NULL == pCtg || NULL == dbFName || NULL == pRpc || NULL == pMgmtEps || NULL == vgroupList) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }
2693

D
dapan1121 已提交
2694
  SCtgDBCache* dbCache = NULL;
2695
  int32_t code = 0;
D
dapan1121 已提交
2696
  SArray *vgList = NULL;
D
dapan1121 已提交
2697 2698
  SHashObj *vgHash = NULL;
  SDBVgInfo *vgInfo = NULL;
D
dapan1121 已提交
2699
  CTG_ERR_JRET(ctgGetDBVgInfo(pCtg, pRpc, pMgmtEps, dbFName, &dbCache, &vgInfo));
D
dapan1121 已提交
2700 2701 2702 2703
  if (dbCache) {
    vgHash = dbCache->vgInfo->vgHash;
  } else {
    vgHash = vgInfo->vgHash;
D
dapan1121 已提交
2704 2705
  }

D
dapan1121 已提交
2706
  CTG_ERR_JRET(ctgGenerateVgList(pCtg, vgHash, &vgList));
D
dapan1121 已提交
2707 2708 2709 2710 2711

  *vgroupList = vgList;
  vgList = NULL;

_return:
D
dapan1121 已提交
2712 2713

  if (dbCache) {
D
dapan1121 已提交
2714 2715
    ctgReleaseVgInfo(dbCache);
    ctgReleaseDBCache(pCtg, dbCache);
D
dapan1121 已提交
2716 2717
  }

D
dapan1121 已提交
2718 2719
  if (vgInfo) {
    taosHashCleanup(vgInfo->vgHash);
wafwerar's avatar
wafwerar 已提交
2720
    taosMemoryFreeClear(vgInfo);
D
dapan1121 已提交
2721 2722
  }

D
dapan1121 已提交
2723
  CTG_API_LEAVE(code);  
D
dapan1121 已提交
2724 2725 2726
}


D
dapan1121 已提交
2727
int32_t catalogUpdateDBVgInfo(SCatalog* pCtg, const char* dbFName, uint64_t dbId, SDBVgInfo* dbInfo) {
D
dapan1121 已提交
2728
  CTG_API_ENTER();
D
dapan1121 已提交
2729 2730

  int32_t code = 0;
D
dapan1121 已提交
2731
  
D
dapan1121 已提交
2732
  if (NULL == pCtg || NULL == dbFName || NULL == dbInfo) {
D
dapan1121 已提交
2733
    ctgFreeVgInfo(dbInfo);
D
dapan1121 已提交
2734 2735 2736
    CTG_ERR_JRET(TSDB_CODE_CTG_INVALID_INPUT);
  }

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

D
dapan1121 已提交
2739 2740
_return:

D
dapan1121 已提交
2741
  CTG_API_LEAVE(code);
D
dapan1121 已提交
2742 2743 2744
}


D
dapan1121 已提交
2745 2746 2747
int32_t catalogRemoveDB(SCatalog* pCtg, const char* dbFName, uint64_t dbId) {
  CTG_API_ENTER();

D
dapan1121 已提交
2748 2749
  int32_t code = 0;
  
D
dapan1121 已提交
2750 2751
  if (NULL == pCtg || NULL == dbFName) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
D
dapan1121 已提交
2752 2753
  }

D
dapan1121 已提交
2754
  if (NULL == pCtg->dbCache) {
D
dapan1121 已提交
2755
    CTG_API_LEAVE(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
2756
  }
D
dapan1121 已提交
2757

D
dapan1121 已提交
2758
  CTG_ERR_JRET(ctgPushRmDBMsgInQueue(pCtg, dbFName, dbId));
D
dapan 已提交
2759

D
dapan1121 已提交
2760
  CTG_API_LEAVE(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
2761
  
D
dapan1121 已提交
2762 2763
_return:

D
dapan1121 已提交
2764
  CTG_API_LEAVE(code);
D
dapan1121 已提交
2765 2766
}

D
dapan1121 已提交
2767
int32_t catalogUpdateVgEpSet(SCatalog* pCtg, const char* dbFName, int32_t vgId, SEpSet *epSet) {
2768
  return 0;
D
dapan1121 已提交
2769
}
D
dapan1121 已提交
2770

D
dapan1121 已提交
2771
int32_t catalogRemoveTableMeta(SCatalog* pCtg, const SName* pTableName) {
D
dapan 已提交
2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784
  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 已提交
2785
  bool inCache = false;
D
dapan 已提交
2786
  uint64_t dbId = 0;
D
dapan1121 已提交
2787
  CTG_ERR_JRET(ctgGetTableMetaFromCache(pCtg, pTableName, &tblMeta, &inCache, 0, &dbId));
D
dapan 已提交
2788

D
dapan1121 已提交
2789
  if (!inCache) {
D
dapan 已提交
2790 2791 2792 2793 2794 2795 2796 2797
    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 已提交
2798
    CTG_ERR_JRET(ctgPushRmStbMsgInQueue(pCtg, dbFName, dbId, pTableName->tname, tblMeta->suid, true));
D
dapan 已提交
2799
  } else {
D
dapan1121 已提交
2800
    CTG_ERR_JRET(ctgPushRmTblMsgInQueue(pCtg, dbFName, dbId, pTableName->tname, true));
D
dapan 已提交
2801 2802 2803 2804 2805
  }

 
_return:

wafwerar's avatar
wafwerar 已提交
2806
  taosMemoryFreeClear(tblMeta);
D
dapan 已提交
2807 2808 2809 2810 2811

  CTG_API_LEAVE(code);
}


D
dapan1121 已提交
2812 2813 2814
int32_t catalogRemoveStbMeta(SCatalog* pCtg, const char* dbFName, uint64_t dbId, const char* stbName, uint64_t suid) {
  CTG_API_ENTER();

D
dapan 已提交
2815 2816
  int32_t code = 0;
  
D
dapan1121 已提交
2817 2818
  if (NULL == pCtg || NULL == dbFName || NULL == stbName) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
D
dapan 已提交
2819 2820
  }

D
dapan1121 已提交
2821
  if (NULL == pCtg->dbCache) {
D
dapan1121 已提交
2822
    CTG_API_LEAVE(TSDB_CODE_SUCCESS);
D
dapan 已提交
2823
  }
D
dapan1121 已提交
2824

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

D
dapan1121 已提交
2827
  CTG_API_LEAVE(TSDB_CODE_SUCCESS);
D
dapan 已提交
2828
  
D
dapan1121 已提交
2829 2830
_return:

D
dapan1121 已提交
2831
  CTG_API_LEAVE(code);
D
dapan 已提交
2832 2833
}

D
dapan1121 已提交
2834
int32_t catalogGetIndexMeta(SCatalog* pCtg, void *pTrans, const SEpSet* pMgmtEps, const SName* pTableName, const char *pIndexName, SIndexMeta** pIndexMeta) {
2835
  return 0;
D
dapan1121 已提交
2836
}
D
dapan1121 已提交
2837

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

D
dapan1121 已提交
2841
  CTG_API_LEAVE(ctgGetTableMeta(pCtg, pTrans, pMgmtEps, pTableName, pTableMeta, CTG_FLAG_UNKNOWN_STB));
D
dapan1121 已提交
2842
}
D
dapan1121 已提交
2843

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

D
dapan1121 已提交
2847
  CTG_API_LEAVE(ctgGetTableMeta(pCtg, pTrans, pMgmtEps, pTableName, pTableMeta, CTG_FLAG_STB));
D
dapan1121 已提交
2848 2849
}

D
dapan1121 已提交
2850 2851 2852 2853 2854 2855 2856
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 已提交
2857
  STableMetaOutput *output = taosMemoryCalloc(1, sizeof(STableMetaOutput));
D
dapan1121 已提交
2858 2859 2860 2861 2862
  if (NULL == output) {
    ctgError("malloc %d failed", (int32_t)sizeof(STableMetaOutput));
    CTG_API_LEAVE(TSDB_CODE_CTG_MEM_ERROR);
  }
  
D
dapan1121 已提交
2863 2864
  int32_t code = 0;

D
dapan1121 已提交
2865 2866
  strcpy(output->dbFName, rspMsg->dbFName);
  strcpy(output->tbName, rspMsg->tbName);
D
dapan1121 已提交
2867

D
dapan1121 已提交
2868
  output->dbId = rspMsg->dbId;
D
dapan1121 已提交
2869
  
D
dapan1121 已提交
2870
  SET_META_TYPE_TABLE(output->metaType);
D
dapan1121 已提交
2871
  
D
dapan1121 已提交
2872
  CTG_ERR_JRET(queryCreateTableMetaFromMsg(rspMsg, true, &output->tbMeta));
D
dapan1121 已提交
2873

D
dapan1121 已提交
2874
  CTG_ERR_JRET(ctgPushUpdateTblMsgInQueue(pCtg, output, false));
D
dapan 已提交
2875

D
dapan1121 已提交
2876 2877
  CTG_API_LEAVE(code);
  
D
dapan1121 已提交
2878 2879
_return:

wafwerar's avatar
wafwerar 已提交
2880 2881
  taosMemoryFreeClear(output->tbMeta);
  taosMemoryFreeClear(output);
D
dapan1121 已提交
2882
  
D
dapan1121 已提交
2883
  CTG_API_LEAVE(code);
D
dapan1121 已提交
2884 2885
}

D
dapan1121 已提交
2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961


int32_t ctgGetTbSverFromCache(SCatalog* pCtg, const SName* pTableName, int32_t* sver) {
  *sver = -1;
  
  if (NULL == pCtg->dbCache) {
    ctgDebug("empty tbmeta cache, tbName:%s", pTableName->tname);
    return TSDB_CODE_SUCCESS;
  }

  SCtgDBCache *dbCache = NULL;
  char dbFName[TSDB_DB_FNAME_LEN] = {0};
  tNameGetFullDbName(pTableName, dbFName);

  ctgAcquireDBCache(pCtg, dbFName, &dbCache);
  if (NULL == dbCache) {
    ctgDebug("db %s not in cache", pTableName->tname);
    return TSDB_CODE_SUCCESS;
  }
  
  int32_t tbType = 0;
  uint64_t suid = 0;
  CTG_LOCK(CTG_READ, &dbCache->tbCache.metaLock);
  STableMeta* tbMeta = taosHashGet(dbCache->tbCache.metaCache, pTableName->tname, strlen(pTableName->tname));
  if (tbMeta) {
    tbType = tbMeta->tableType;
    suid = tbMeta->suid;
    if (tbType != TSDB_CHILD_TABLE) {
      *sver = tbMeta->sversion;
    }
  }
  CTG_UNLOCK(CTG_READ, &dbCache->tbCache.metaLock);

  if (NULL == tbMeta) {
    ctgReleaseDBCache(pCtg, dbCache);
    return TSDB_CODE_SUCCESS;
  }

  if (tbType != TSDB_CHILD_TABLE) {
    ctgReleaseDBCache(pCtg, dbCache);
    ctgDebug("Got sver %d from cache, type:%d, dbFName:%s, tbName:%s", *sver, tbType, dbFName, pTableName->tname);
    
    return TSDB_CODE_SUCCESS;
  }

  ctgDebug("Got subtable meta from cache, dbFName:%s, tbName:%s, suid:%" PRIx64, dbFName, pTableName->tname, suid);
  
  CTG_LOCK(CTG_READ, &dbCache->tbCache.stbLock);
  
  STableMeta **stbMeta = taosHashGet(dbCache->tbCache.stbCache, &suid, sizeof(suid));
  if (NULL == stbMeta || NULL == *stbMeta) {
    CTG_UNLOCK(CTG_READ, &dbCache->tbCache.stbLock);
    ctgReleaseDBCache(pCtg, dbCache);
    ctgDebug("stb not in stbCache, suid:%"PRIx64, suid);
    return TSDB_CODE_SUCCESS;
  }

  if ((*stbMeta)->suid != suid) {    
    CTG_UNLOCK(CTG_READ, &dbCache->tbCache.stbLock);
    ctgReleaseDBCache(pCtg, dbCache);
    ctgError("stable suid in stbCache mis-match, expected suid:%"PRIx64 ",actual suid:%"PRIx64, suid, (*stbMeta)->suid);
    CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
  }

  *sver = (*stbMeta)->sversion;

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

  ctgReleaseDBCache(pCtg, dbCache);

  ctgDebug("Got sver %d from cache, type:%d, dbFName:%s, tbName:%s", *sver, tbType, dbFName, pTableName->tname);
  
  return TSDB_CODE_SUCCESS;
}


D
dapan1121 已提交
2962
int32_t catalogChkTbMetaVersion(SCatalog* pCtg, void *pTrans, const SEpSet* pMgmtEps, SArray* pTables) {
D
dapan1121 已提交
2963 2964 2965 2966 2967
  CTG_API_ENTER();

  if (NULL == pCtg || NULL == pTrans || NULL == pMgmtEps || NULL == pTables) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }
D
dapan1121 已提交
2968

D
dapan1121 已提交
2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986
  SName name;
  int32_t sver = 0;
  int32_t tbNum = taosArrayGetSize(pTables);
  for (int32_t i = 0; i < tbNum; ++i) {
    STbSVersion* pTb = (STbSVersion*)taosArrayGet(pTables, i);
    tNameFromString(&name, pTb->tbFName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
    
    if (CTG_IS_SYS_DBNAME(name.dbname)) {
      continue;
    }

    ctgGetTbSverFromCache(pCtg, &name, &sver);
    if (sver >= 0 && sver < pTb->sver) {
      catalogRemoveTableMeta(pCtg, &name); //TODO REMOVE STB FROM CACHE
    }
  }

  CTG_API_LEAVE(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
2987 2988 2989
}


D
dapan1121 已提交
2990 2991 2992 2993 2994 2995 2996 2997 2998
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 已提交
2999

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

D
dapan1121 已提交
3003
  if (NULL == pCtg || NULL == pTrans || NULL == pMgmtEps || NULL == pTableName) {
D
dapan1121 已提交
3004 3005 3006
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

D
dapan1121 已提交
3007
  CTG_API_LEAVE(ctgRefreshTblMeta(pCtg, pTrans, pMgmtEps, pTableName, CTG_FLAG_FORCE_UPDATE | CTG_FLAG_MAKE_STB(isSTable), NULL, true));
3008
}
3009

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

D
dapan1121 已提交
3013
  CTG_API_LEAVE(ctgGetTableMeta(pCtg, pTrans, pMgmtEps, pTableName, pTableMeta, CTG_FLAG_FORCE_UPDATE | CTG_FLAG_MAKE_STB(isSTable)));
D
dapan1121 已提交
3014 3015
}

D
dapan1121 已提交
3016
int32_t catalogGetTableDistVgInfo(SCatalog* pCtg, void *pRpc, const SEpSet* pMgmtEps, const SName* pTableName, SArray** pVgList) {
D
dapan1121 已提交
3017
  CTG_API_ENTER();
D
dapan1121 已提交
3018 3019 3020 3021

  if (NULL == pCtg || NULL == pRpc || NULL == pMgmtEps || NULL == pTableName || NULL == pVgList) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }
D
dapan1121 已提交
3022

D
dapan1121 已提交
3023
  if (CTG_IS_SYS_DBNAME(pTableName->dbname)) {
D
dapan1121 已提交
3024 3025 3026
    ctgError("no valid vgInfo for db, dbname:%s", pTableName->dbname);
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }
D
dapan1121 已提交
3027

D
dapan1121 已提交
3028
  int32_t code = 0;
D
dapan1121 已提交
3029

D
dapan1121 已提交
3030 3031 3032 3033 3034
  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 已提交
3035

D
dapan1121 已提交
3036 3037 3038 3039 3040 3041
        char dbFName[TSDB_DB_FNAME_LEN] = {0};
        tNameGetFullDbName(pTableName, dbFName);        
        CTG_ERR_JRET(ctgRefreshDBVgInfo(pCtg, pRpc, pMgmtEps, dbFName));
        
        continue;
      }
D
dapan1121 已提交
3042
    }
D
dapan 已提交
3043

D
dapan1121 已提交
3044
    break;
D
dapan1121 已提交
3045
  }
D
dapan 已提交
3046

D
dapan1121 已提交
3047
_return:
D
dapan 已提交
3048

D
dapan1121 已提交
3049
  CTG_API_LEAVE(code);
D
dapan1121 已提交
3050 3051 3052
}


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

D
dapan1121 已提交
3056
  if (CTG_IS_SYS_DBNAME(pTableName->dbname)) {
D
dapan1121 已提交
3057 3058 3059 3060
    ctgError("no valid vgInfo for db, dbname:%s", pTableName->dbname);
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

D
dapan1121 已提交
3061 3062
  SCtgDBCache* dbCache = NULL;
  int32_t code = 0;
H
Haojun Liao 已提交
3063 3064
  char db[TSDB_DB_FNAME_LEN] = {0};
  tNameGetFullDbName(pTableName, db);
D
dapan1121 已提交
3065

D
dapan1121 已提交
3066
  SDBVgInfo *vgInfo = NULL;
D
dapan1121 已提交
3067
  CTG_ERR_JRET(ctgGetDBVgInfo(pCtg, pTrans, pMgmtEps, db, &dbCache, &vgInfo));
D
dapan1121 已提交
3068

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

D
dapan1121 已提交
3071
_return:
D
dapan1121 已提交
3072

D
dapan1121 已提交
3073
  if (dbCache) {
D
dapan1121 已提交
3074 3075 3076 3077 3078 3079
    ctgReleaseVgInfo(dbCache);
    ctgReleaseDBCache(pCtg, dbCache);
  }

  if (vgInfo) {
    taosHashCleanup(vgInfo->vgHash);
wafwerar's avatar
wafwerar 已提交
3080
    taosMemoryFreeClear(vgInfo);
D
dapan1121 已提交
3081
  }
D
dapan1121 已提交
3082

D
dapan1121 已提交
3083
  CTG_API_LEAVE(code);
D
dapan1121 已提交
3084 3085 3086
}


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

D
dapan1121 已提交
3090
  if (NULL == pCtg || NULL == pTrans || NULL == pMgmtEps || NULL == pReq || NULL == pRsp) {
D
dapan1121 已提交
3091 3092 3093
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

D
dapan1121 已提交
3094
  int32_t code = 0;
D
dapan1121 已提交
3095
  pRsp->pTableMeta = NULL;
D
dapan1121 已提交
3096 3097 3098

  if (pReq->pTableName) {
    int32_t tbNum = (int32_t)taosArrayGetSize(pReq->pTableName);
D
dapan1121 已提交
3099
    if (tbNum <= 0) {
D
dapan1121 已提交
3100
      ctgError("empty table name list, tbNum:%d", tbNum);
D
dapan1121 已提交
3101
      CTG_ERR_JRET(TSDB_CODE_CTG_INVALID_INPUT);
D
dapan1121 已提交
3102
    }
H
Haojun Liao 已提交
3103

D
dapan1121 已提交
3104 3105
    pRsp->pTableMeta = taosArrayInit(tbNum, POINTER_BYTES);
    if (NULL == pRsp->pTableMeta) {
D
dapan1121 已提交
3106
      ctgError("taosArrayInit %d failed", tbNum);
D
dapan1121 已提交
3107
      CTG_ERR_JRET(TSDB_CODE_CTG_MEM_ERROR);
D
dapan1121 已提交
3108 3109 3110 3111 3112 3113
    }
    
    for (int32_t i = 0; i < tbNum; ++i) {
      SName *name = taosArrayGet(pReq->pTableName, i);
      STableMeta *pTableMeta = NULL;
      
D
dapan1121 已提交
3114
      CTG_ERR_JRET(ctgGetTableMeta(pCtg, pTrans, pMgmtEps, name, &pTableMeta, CTG_FLAG_UNKNOWN_STB));
D
dapan1121 已提交
3115 3116 3117

      if (NULL == taosArrayPush(pRsp->pTableMeta, &pTableMeta)) {
        ctgError("taosArrayPush failed, idx:%d", i);
wafwerar's avatar
wafwerar 已提交
3118
        taosMemoryFreeClear(pTableMeta);
D
dapan1121 已提交
3119 3120 3121 3122 3123
        CTG_ERR_JRET(TSDB_CODE_CTG_MEM_ERROR);
      }
    }
  }

D
dapan1121 已提交
3124
  if (pReq->qNodeRequired) {
D
dapan 已提交
3125 3126
    pRsp->pQnodeList = taosArrayInit(10, sizeof(SQueryNodeAddr));
    CTG_ERR_JRET(ctgGetQnodeListFromMnode(pCtg, pTrans, pMgmtEps, pRsp->pQnodeList));
D
dapan1121 已提交
3127 3128
  }

D
dapan1121 已提交
3129
  CTG_API_LEAVE(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
3130 3131

_return:  
D
dapan1121 已提交
3132

D
dapan1121 已提交
3133 3134 3135 3136
  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 已提交
3137
      taosMemoryFreeClear(pMeta);
D
dapan1121 已提交
3138 3139 3140
    }
    
    taosArrayDestroy(pRsp->pTableMeta);
D
dapan1121 已提交
3141
    pRsp->pTableMeta = NULL;
D
dapan1121 已提交
3142
  }
D
dapan 已提交
3143
  
D
dapan1121 已提交
3144
  CTG_API_LEAVE(code);
3145
}
D
dapan 已提交
3146

D
dapan1121 已提交
3147
int32_t catalogGetQnodeList(SCatalog* pCtg, void *pRpc, const SEpSet* pMgmtEps, SArray* pQnodeList) {
D
dapan1121 已提交
3148
  CTG_API_ENTER();
D
dapan1121 已提交
3149 3150
  
  int32_t code = 0;
D
dapan1121 已提交
3151 3152 3153 3154
  if (NULL == pCtg || NULL == pRpc  || NULL == pMgmtEps || NULL == pQnodeList) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

D
dapan 已提交
3155
  CTG_ERR_JRET(ctgGetQnodeListFromMnode(pCtg, pRpc, pMgmtEps, pQnodeList));
D
dapan1121 已提交
3156 3157

_return:
D
dapan 已提交
3158

D
dapan1121 已提交
3159
  CTG_API_LEAVE(TSDB_CODE_SUCCESS);
D
dapan 已提交
3160 3161
}

D
dapan1121 已提交
3162
int32_t catalogGetExpiredSTables(SCatalog* pCtg, SSTableMetaVersion **stables, uint32_t *num) {
D
dapan1121 已提交
3163 3164
  CTG_API_ENTER();

D
dapan1121 已提交
3165 3166
  if (NULL == pCtg || NULL == stables || NULL == num) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
D
dapan1121 已提交
3167 3168
  }

D
dapan1121 已提交
3169 3170 3171 3172
  CTG_API_LEAVE(ctgMetaRentGet(&pCtg->stbRent, (void **)stables, num, sizeof(SSTableMetaVersion)));
}

int32_t catalogGetExpiredDBs(SCatalog* pCtg, SDbVgVersion **dbs, uint32_t *num) {
D
dapan1121 已提交
3173
  CTG_API_ENTER();
D
dapan1121 已提交
3174 3175 3176 3177
  
  if (NULL == pCtg || NULL == dbs || NULL == num) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }
D
dapan1121 已提交
3178

D
dapan1121 已提交
3179
  CTG_API_LEAVE(ctgMetaRentGet(&pCtg->dbRent, (void **)dbs, num, sizeof(SDbVgVersion)));
D
dapan1121 已提交
3180 3181
}

D
dapan 已提交
3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210
int32_t catalogGetExpiredUsers(SCatalog* pCtg, SUserAuthVersion **users, uint32_t *num) {
  CTG_API_ENTER();
  
  if (NULL == pCtg || NULL == users || NULL == num) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

  *num = taosHashGetSize(pCtg->userCache);
  if (*num > 0) {
    *users = taosMemoryCalloc(*num, sizeof(SUserAuthVersion));
    if (NULL == *users) {
      ctgError("calloc %d userAuthVersion failed", *num);
      CTG_API_LEAVE(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

  uint32_t i = 0;
  SCtgUserAuth *pAuth = taosHashIterate(pCtg->userCache, NULL);
  while (pAuth != NULL) {
    void *key = taosHashGetKey(pAuth, NULL);
    strncpy((*users)[i].user, key, sizeof((*users)[i].user));
    (*users)[i].version = pAuth->version;
    pAuth = taosHashIterate(pCtg->userCache, pAuth);
  }

  CTG_API_LEAVE(TSDB_CODE_SUCCESS);
}


D
dapan1121 已提交
3211 3212 3213 3214 3215 3216 3217 3218 3219
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 已提交
3220

D
dapan1121 已提交
3221 3222 3223 3224 3225 3226 3227 3228 3229 3230
int32_t catalogGetIndexInfo(SCatalog* pCtg, void *pRpc, const SEpSet* pMgmtEps, const char* indexName, SIndexInfo* pInfo) {
  CTG_API_ENTER();
  
  if (NULL == pCtg || NULL == pRpc || NULL == pMgmtEps || NULL == indexName || NULL == pInfo) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

  CTG_API_LEAVE(ctgGetIndexInfoFromMnode(pCtg, pRpc, pMgmtEps, indexName, pInfo));
}

D
dapan1121 已提交
3231
int32_t catalogGetUdfInfo(SCatalog* pCtg, void *pRpc, const SEpSet* pMgmtEps, const char* funcName, SFuncInfo** pInfo) {
D
dapan1121 已提交
3232 3233 3234 3235 3236 3237
  CTG_API_ENTER();
  
  if (NULL == pCtg || NULL == pRpc || NULL == pMgmtEps || NULL == funcName || NULL == pInfo) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

D
dapan1121 已提交
3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252
  int32_t code = 0;
  *pInfo = taosMemoryMalloc(sizeof(SFuncInfo));
  if (NULL == *pInfo) {
    CTG_API_LEAVE(TSDB_CODE_OUT_OF_MEMORY);
  }

  CTG_ERR_JRET(ctgGetUdfInfoFromMnode(pCtg, pRpc, pMgmtEps, funcName, pInfo));
  
_return:

  if (code) {
    taosMemoryFreeClear(*pInfo);    
  }
  
  CTG_API_LEAVE(code);
D
dapan1121 已提交
3253 3254
}

D
dapan 已提交
3255
int32_t catalogChkAuth(SCatalog* pCtg, void *pRpc, const SEpSet* pMgmtEps, const char* user, const char* dbFName, AUTH_TYPE type, bool *pass) {
D
dapan 已提交
3256 3257
  CTG_API_ENTER();
  
D
dapan 已提交
3258
  if (NULL == pCtg || NULL == pRpc || NULL == pMgmtEps || NULL == user || NULL == dbFName || NULL == pass) {
D
dapan 已提交
3259 3260 3261 3262
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

  int32_t code = 0;
D
dapan 已提交
3263
  CTG_ERR_JRET(ctgChkAuth(pCtg, pRpc, pMgmtEps, user, dbFName, type, pass));
D
dapan 已提交
3264 3265 3266 3267 3268 3269
  
_return:

  CTG_API_LEAVE(code);
}

D
dapan 已提交
3270 3271 3272 3273 3274 3275 3276 3277 3278 3279
int32_t catalogUpdateUserAuthInfo(SCatalog* pCtg, SGetUserAuthRsp* pAuth) {
  CTG_API_ENTER();

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

  CTG_API_LEAVE(ctgPushUpdateUserMsgInQueue(pCtg, pAuth, false));
}

D
dapan1121 已提交
3280

D
dapan 已提交
3281
void catalogDestroy(void) {
D
dapan1121 已提交
3282 3283
  qInfo("start to destroy catalog");
  
wafwerar's avatar
wafwerar 已提交
3284
  if (NULL == gCtgMgmt.pCluster || atomic_load_8((int8_t*)&gCtgMgmt.exit)) {
D
dapan1121 已提交
3285 3286 3287
    return;
  }

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

D
dapan1121 已提交
3290 3291 3292 3293 3294 3295 3296
  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 已提交
3297

D
dapan1121 已提交
3298
  while (CTG_IS_LOCKED(&gCtgMgmt.lock)) {
wafwerar's avatar
wafwerar 已提交
3299
    taosUsleep(1);
D
dapan1121 已提交
3300 3301
  }
  
D
dapan 已提交
3302
  CTG_LOCK(CTG_WRITE, &gCtgMgmt.lock);
D
dapan1121 已提交
3303

D
dapan1121 已提交
3304
  SCatalog *pCtg = NULL;
D
dapan 已提交
3305
  void *pIter = taosHashIterate(gCtgMgmt.pCluster, NULL);
D
dapan1121 已提交
3306
  while (pIter) {
D
dapan1121 已提交
3307
    pCtg = *(SCatalog **)pIter;
D
dapan1121 已提交
3308

D
dapan1121 已提交
3309 3310
    if (pCtg) {
      catalogFreeHandle(pCtg);
D
dapan1121 已提交
3311 3312
    }
    
D
dapan 已提交
3313
    pIter = taosHashIterate(gCtgMgmt.pCluster, pIter);
D
dapan 已提交
3314
  }
D
dapan1121 已提交
3315
  
D
dapan 已提交
3316 3317
  taosHashCleanup(gCtgMgmt.pCluster);
  gCtgMgmt.pCluster = NULL;
D
dapan1121 已提交
3318

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

D
dapan1121 已提交
3321
  qInfo("catalog destroyed");
D
dapan 已提交
3322 3323 3324 3325
}