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

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

D
dapan1121 已提交
21 22
SCatalogMgmt ctgMgmt = {0};

D
dapan1121 已提交
23
SCtgDebug gCTGDebug = {0};
24 25


D
dapan1121 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
void ctgFreeMetaRent(SCtgRentMgmt *mgmt) {
  if (NULL == mgmt->slots) {
    return;
  }

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

  tfree(mgmt->slots);
}


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

  if (table->cache) {
    taosHashCleanup(table->cache);
    table->cache = NULL;
  }
}

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

  atomic_store_8(&dbCache->deleted, 1);

  SDBVgroupInfo *dbInfo = NULL;
  if (dbCache->vgInfo) {
    CTG_LOCK(CTG_WRITE, &dbCache->vgLock);

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

    tfree(dbCache->vgInfo);
    CTG_UNLOCK(CTG_WRITE, &dbCache->vgLock);
  }

  ctgFreeTableMetaCache(&dbCache->tbCache);
}


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

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

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


int32_t ctgGetDBVgroupFromCache(struct SCatalog* pCatalog, const char *dbName, SCtgDBCache **dbCache, bool *inCache) {
  if (NULL == pCatalog->dbCache) {
D
dapan1121 已提交
103
    *inCache = false;
D
dapan1121 已提交
104
    ctgWarn("empty db cache, dbName:%s", dbName);
D
dapan1121 已提交
105 106 107
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
108
  SCtgDBCache *cache = NULL;
D
dapan1121 已提交
109

D
dapan1121 已提交
110
  while (true) {
D
dapan1121 已提交
111
    cache = taosHashAcquire(pCatalog->dbCache, dbName, strlen(dbName));
D
dapan1121 已提交
112

D
dapan1121 已提交
113
    if (NULL == cache) {
D
dapan1121 已提交
114
      *inCache = false;
D
dapan1121 已提交
115
      ctgWarn("not in db vgroup cache, dbName:%s", dbName);
D
dapan1121 已提交
116 117 118
      return TSDB_CODE_SUCCESS;
    }

D
dapan1121 已提交
119 120 121 122
    CTG_LOCK(CTG_READ, &cache->vgLock);
    if (NULL == cache->vgInfo) {
      CTG_UNLOCK(CTG_READ, &cache->vgLock);
      taosHashRelease(pCatalog->dbCache, cache);
D
dapan1121 已提交
123 124 125 126 127 128
      ctgWarn("db cache vgInfo is NULL, dbName:%s", dbName);
      
      continue;
    }

    break;
D
dapan1121 已提交
129
  }
D
dapan1121 已提交
130

D
dapan1121 已提交
131
  *dbCache = cache;
D
dapan1121 已提交
132
  *inCache = true;
D
dapan1121 已提交
133 134

  ctgDebug("Got db vgroup from cache, dbName:%s", dbName);
D
dapan1121 已提交
135 136 137 138 139 140 141 142 143 144
  
  return TSDB_CODE_SUCCESS;
}



int32_t ctgGetDBVgroupFromMnode(struct SCatalog* pCatalog, void *pRpc, const SEpSet* pMgmtEps, SBuildUseDBInput *input, SUseDbOutput *out) {
  char *msg = NULL;
  int32_t msgLen = 0;

D
dapan1121 已提交
145 146 147 148 149 150 151
  ctgDebug("try to get db vgroup from mnode, db:%s", input->db);

  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 已提交
152
  
D
dapan1121 已提交
153
  SRpcMsg rpcMsg = {
H
Hongze Cheng 已提交
154
      .msgType = TDMT_MND_USE_DB,
D
catalog  
dapan1121 已提交
155
      .pCont   = msg,
D
dapan1121 已提交
156 157 158 159 160 161
      .contLen = msgLen,
  };

  SRpcMsg rpcRsp = {0};

  rpcSendRecv(pRpc, (SEpSet*)pMgmtEps, &rpcMsg, &rpcRsp);
D
dapan1121 已提交
162
  if (TSDB_CODE_SUCCESS != rpcRsp.code) {
H
Haojun Liao 已提交
163
    ctgError("error rsp for use db, code:%s, db:%s", tstrerror(rpcRsp.code), input->db);
D
dapan1121 已提交
164
    CTG_ERR_RET(rpcRsp.code);
D
dapan1121 已提交
165
  }
D
dapan1121 已提交
166

D
dapan1121 已提交
167 168 169 170 171
  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 已提交
172

D
dapan1121 已提交
173 174
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
175

D
dapan1121 已提交
176 177
int32_t ctgIsTableMetaExistInCache(struct SCatalog* pCatalog, char *dbFName, char* tbName, int32_t *exist) {
  if (NULL == pCatalog->dbCache) {
D
dapan1121 已提交
178
    *exist = 0;
D
dapan1121 已提交
179 180 181 182 183 184 185 186
    ctgWarn("empty db cache, dbFName:%s, tbName:%s", dbFName, tbName);
    return TSDB_CODE_SUCCESS;
  }

  SCtgDBCache *dbCache = taosHashAcquire(pCatalog->dbCache, dbFName, strlen(dbFName));
  if (NULL == dbCache) {
    *exist = 0;
    ctgWarn("db not exist in cache, dbFName:%s", dbFName);
D
dapan1121 已提交
187 188 189 190
    return TSDB_CODE_SUCCESS;
  }


D
dapan1121 已提交
191 192
  size_t sz = 0;
  STableMeta *tbMeta = taosHashGet(dbCache->tbCache.cache, tbName, strlen(tbName));
D
dapan1121 已提交
193
  if (NULL == tbMeta) {
D
dapan1121 已提交
194 195
    taosHashRelease(pCatalog->dbCache, dbCache);
    
D
dapan1121 已提交
196
    *exist = 0;
D
dapan1121 已提交
197
    ctgDebug("tbmeta not in cache, dbFName:%s, tbName:%s", dbFName, tbName);
D
dapan1121 已提交
198 199 200 201
    return TSDB_CODE_SUCCESS;
  }

  *exist = 1;
D
dapan1121 已提交
202 203

  taosHashRelease(pCatalog->dbCache, dbCache);
D
dapan1121 已提交
204
  
D
dapan1121 已提交
205
  ctgDebug("tbmeta is in cache, dbFName:%s, tbName:%s", dbFName, tbName);
D
dapan1121 已提交
206 207 208 209
  
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
210

H
Haojun Liao 已提交
211
int32_t ctgGetTableMetaFromCache(struct SCatalog* pCatalog, const SName* pTableName, STableMeta** pTableMeta, int32_t *exist) {
D
dapan1121 已提交
212
  if (NULL == pCatalog->dbCache) {
D
dapan1121 已提交
213
    *exist = 0;
D
dapan1121 已提交
214
    ctgWarn("empty tbmeta cache, tbName:%s", pTableName->tname);
D
dapan1121 已提交
215 216 217
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
218 219
  char db[TSDB_DB_FNAME_LEN] = {0};
  tNameGetFullDbName(pTableName, db);
D
dapan1121 已提交
220

D
dapan1121 已提交
221 222
  *pTableMeta = NULL;

D
dapan1121 已提交
223 224 225 226 227 228
  SCtgDBCache *dbCache = taosHashAcquire(pCatalog->dbCache, db, strlen(db));
  if (NULL == dbCache) {
    *exist = 0;
    ctgWarn("no db cache, dbFName:%s, tbName:%s", db, pTableName->tname);
    return TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
229

D
dapan1121 已提交
230 231 232 233 234 235 236 237 238
  if (NULL == dbCache->tbCache.cache) {
    *exist = 0;
    taosHashRelease(pCatalog->dbCache, dbCache);
    ctgWarn("empty tbmeta cache, dbFName:%s, tbName:%s", db, pTableName->tname);
    return TSDB_CODE_SUCCESS;
  }
  
  size_t sz = 0;
  STableMeta *tbMeta = taosHashGetCloneExt(dbCache->tbCache.cache, pTableName->tname, strlen(pTableName->tname), NULL, (void **)pTableMeta, &sz);
D
dapan1121 已提交
239
  if (NULL == *pTableMeta) {
D
dapan1121 已提交
240
    *exist = 0;
D
dapan1121 已提交
241 242
    taosHashRelease(pCatalog->dbCache, dbCache);
    ctgDebug("tbmeta not in cache, dbFName:%s, tbName:%s", db, pTableName->tname);
D
dapan1121 已提交
243 244 245
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
246
  *exist = 1;
D
dapan1121 已提交
247 248
  
  tbMeta = *pTableMeta;
D
dapan1121 已提交
249

D
dapan1121 已提交
250
  if (tbMeta->tableType != TSDB_CHILD_TABLE) {
D
dapan1121 已提交
251
    ctgDebug("Got tbmeta from cache, type:%d, dbFName:%s, tbName:%s", tbMeta->tableType, db, pTableName->tname);
D
dapan1121 已提交
252 253 254
    return TSDB_CODE_SUCCESS;
  }
  
D
dapan1121 已提交
255
  CTG_LOCK(CTG_READ, &dbCache->tbCache.stbLock);
D
dapan1121 已提交
256
  
D
dapan1121 已提交
257
  STableMeta **stbMeta = taosHashGet(dbCache->tbCache.stbCache, &tbMeta->suid, sizeof(tbMeta->suid));
D
dapan1121 已提交
258
  if (NULL == stbMeta || NULL == *stbMeta) {
D
dapan1121 已提交
259 260
    CTG_UNLOCK(CTG_READ, &dbCache->tbCache.stbLock);
    ctgError("stable not in stbCache, suid:%"PRIx64, tbMeta->suid);
D
dapan1121 已提交
261 262 263 264
    tfree(*pTableMeta);
    *exist = 0;
    return TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
265

D
dapan1121 已提交
266
  if ((*stbMeta)->suid != tbMeta->suid) {    
D
dapan1121 已提交
267
    CTG_UNLOCK(CTG_READ, &dbCache->tbCache.stbLock);
D
dapan1121 已提交
268
    tfree(*pTableMeta);
D
dapan1121 已提交
269
    ctgError("stable suid in stbCache mis-match, expected suid:%"PRIx64 ",actual suid:%"PRIx64, tbMeta->suid, (*stbMeta)->suid);
D
dapan1121 已提交
270 271
    CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
  }
D
dapan1121 已提交
272

D
dapan1121 已提交
273 274 275
  int32_t metaSize = sizeof(STableMeta) + ((*stbMeta)->tableInfo.numOfTags + (*stbMeta)->tableInfo.numOfColumns) * sizeof(SSchema);
  *pTableMeta = realloc(*pTableMeta, metaSize);
  if (NULL == *pTableMeta) {    
D
dapan1121 已提交
276
    CTG_UNLOCK(CTG_READ, &dbCache->tbCache.stbLock);
D
dapan1121 已提交
277
    ctgError("realloc size[%d] failed", metaSize);
D
dapan1121 已提交
278
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
D
dapan1121 已提交
279 280
  }

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

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

D
dapan1121 已提交
285
  ctgDebug("Got tbmeta from cache, dbFName:%s, tbName:%s", db, pTableName->tname);
D
dapan1121 已提交
286 287 288 289
  
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
290
int32_t ctgGetTableTypeFromCache(struct SCatalog* pCatalog, const SName* pTableName, int32_t *tbType) {
D
dapan1121 已提交
291 292
  if (NULL == pCatalog->dbCache) {
    ctgWarn("empty db cache, tbName:%s", pTableName->tname);  
D
dapan1121 已提交
293 294 295
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
296 297
  char dbName[TSDB_DB_FNAME_LEN] = {0};
  tNameGetFullDbName(pTableName, dbName);
D
dapan1121 已提交
298

D
dapan1121 已提交
299 300 301 302 303
  SCtgDBCache *dbCache = taosHashAcquire(pCatalog->dbCache, dbName, strlen(dbName));
  if (NULL == dbCache) {
    ctgInfo("db not in cache, dbFName:%s", dbName);
    return TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
304

D
dapan1121 已提交
305
  STableMeta *pTableMeta = (STableMeta *)taosHashAcquire(dbCache->tbCache.cache, pTableName->tname, strlen(pTableName->tname));
D
dapan1121 已提交
306
  if (NULL == pTableMeta) {
D
dapan1121 已提交
307 308 309
    ctgWarn("tbmeta not in cache, dbFName:%s, tbName:%s", dbName, pTableName->tname);  
    taosHashRelease(pCatalog->dbCache, dbCache);
    
D
dapan1121 已提交
310 311 312
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
313 314 315 316
  *tbType = atomic_load_8(&pTableMeta->tableType);

  taosHashRelease(dbCache->tbCache.cache, dbCache);
  taosHashRelease(pCatalog->dbCache, dbCache);
D
dapan1121 已提交
317

D
dapan1121 已提交
318
  ctgDebug("Got tbtype from cache, dbFName:%s, tbName:%s, type:%d", dbName, pTableName->tname, *tbType);  
D
dapan1121 已提交
319 320 321 322
  
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
323 324
int32_t ctgGetTableMetaFromMnodeImpl(struct SCatalog* pCatalog, void *pTransporter, const SEpSet* pMgmtEps, char *dbFName, char* tbName, STableMetaOutput* output) {
  SBuildTableMetaInput bInput = {.vgId = 0, .dbFName = dbFName, .tbName = tbName};
D
dapan1121 已提交
325 326 327 328
  char *msg = NULL;
  SEpSet *pVnodeEpSet = NULL;
  int32_t msgLen = 0;

D
dapan1121 已提交
329
  ctgDebug("try to get table meta from mnode, dbFName:%s, tbName:%s", dbFName, tbName);
D
dapan1121 已提交
330 331 332 333 334 335

  int32_t code = queryBuildMsg[TMSG_INDEX(TDMT_MND_STB_META)](&bInput, &msg, 0, &msgLen);
  if (code) {
    ctgError("Build mnode stablemeta msg failed, code:%x", code);
    CTG_ERR_RET(code);
  }
D
dapan1121 已提交
336 337 338 339 340 341

  SRpcMsg rpcMsg = {
      .msgType = TDMT_MND_STB_META,
      .pCont   = msg,
      .contLen = msgLen,
  };
D
dapan1121 已提交
342

D
dapan1121 已提交
343 344
  SRpcMsg rpcRsp = {0};

D
dapan1121 已提交
345
  rpcSendRecv(pTransporter, (SEpSet*)pMgmtEps, &rpcMsg, &rpcRsp);
D
dapan1121 已提交
346 347
  
  if (TSDB_CODE_SUCCESS != rpcRsp.code) {
D
dapan1121 已提交
348
    if (CTG_TABLE_NOT_EXIST(rpcRsp.code)) {
D
dapan1121 已提交
349
      SET_META_TYPE_NULL(output->metaType);
D
dapan1121 已提交
350
      ctgDebug("stablemeta not exist in mnode, dbFName:%s, tbName:%s", dbFName, tbName);
D
dapan1121 已提交
351 352 353
      return TSDB_CODE_SUCCESS;
    }
    
D
dapan1121 已提交
354
    ctgError("error rsp for stablemeta from mnode, code:%s, dbFName:%s, tbName:%s", tstrerror(rpcRsp.code), dbFName, tbName);
D
dapan1121 已提交
355 356 357
    CTG_ERR_RET(rpcRsp.code);
  }

D
dapan1121 已提交
358 359
  code = queryProcessMsgRsp[TMSG_INDEX(TDMT_MND_STB_META)](output, rpcRsp.pCont, rpcRsp.contLen);
  if (code) {
D
dapan1121 已提交
360
    ctgError("Process mnode stablemeta rsp failed, code:%x, dbFName:%s, tbName:%s", code, dbFName, tbName);
D
dapan1121 已提交
361 362 363
    CTG_ERR_RET(code);
  }

D
dapan1121 已提交
364
  ctgDebug("Got table meta from mnode, dbFName:%s, tbName:%s", dbFName, tbName);
D
dapan1121 已提交
365 366 367 368

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
369
int32_t ctgGetTableMetaFromMnode(struct SCatalog* pCatalog, void *pTransporter, const SEpSet* pMgmtEps, const SName* pTableName, STableMetaOutput* output) {
D
dapan1121 已提交
370 371
  char dbFName[TSDB_DB_FNAME_LEN];
  tNameGetFullDbName(pTableName, dbFName);
D
dapan1121 已提交
372

D
dapan1121 已提交
373
  return ctgGetTableMetaFromMnodeImpl(pCatalog, pTransporter, pMgmtEps, dbFName, (char *)pTableName->tname, output);
D
dapan1121 已提交
374
}
D
dapan1121 已提交
375

D
dapan1121 已提交
376 377
int32_t ctgGetTableMetaFromVnode(struct SCatalog* pCatalog, void *pTransporter, const SEpSet* pMgmtEps, const SName* pTableName, SVgroupInfo *vgroupInfo, STableMetaOutput* output) {
  if (NULL == pCatalog || NULL == pTransporter || NULL == pMgmtEps || NULL == pTableName || NULL == vgroupInfo || NULL == output) {
D
dapan1121 已提交
378
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
D
dapan1121 已提交
379 380
  }

D
dapan1121 已提交
381 382
  char dbFName[TSDB_DB_FNAME_LEN];
  tNameGetFullDbName(pTableName, dbFName);
D
dapan1121 已提交
383

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

D
dapan1121 已提交
386
  SBuildTableMetaInput bInput = {.vgId = vgroupInfo->vgId, .dbFName = dbFName, .tbName = (char *)tNameGetTableName(pTableName)};
D
dapan1121 已提交
387 388 389
  char *msg = NULL;
  int32_t msgLen = 0;

D
dapan1121 已提交
390 391
  int32_t code = queryBuildMsg[TMSG_INDEX(TDMT_VND_TABLE_META)](&bInput, &msg, 0, &msgLen);
  if (code) {
D
dapan1121 已提交
392
    ctgError("Build vnode tablemeta msg failed, code:%x, dbFName:%s, tbName:%s", code, dbFName, tNameGetTableName(pTableName));
D
dapan1121 已提交
393 394
    CTG_ERR_RET(code);
  }
D
dapan1121 已提交
395 396

  SRpcMsg rpcMsg = {
H
Hongze Cheng 已提交
397
      .msgType = TDMT_VND_TABLE_META,
D
dapan1121 已提交
398 399 400 401 402
      .pCont   = msg,
      .contLen = msgLen,
  };

  SRpcMsg rpcRsp = {0};
H
Haojun Liao 已提交
403
  rpcSendRecv(pTransporter, &vgroupInfo->epset, &rpcMsg, &rpcRsp);
D
dapan1121 已提交
404
  
D
dapan1121 已提交
405
  if (TSDB_CODE_SUCCESS != rpcRsp.code) {
D
dapan1121 已提交
406
    if (CTG_TABLE_NOT_EXIST(rpcRsp.code)) {
D
dapan1121 已提交
407
      SET_META_TYPE_NULL(output->metaType);
D
dapan1121 已提交
408
      ctgDebug("tablemeta not exist in vnode, dbFName:%s, tbName:%s", dbFName, tNameGetTableName(pTableName));
D
dapan1121 已提交
409 410 411
      return TSDB_CODE_SUCCESS;
    }
  
D
dapan1121 已提交
412
    ctgError("error rsp for table meta from vnode, code:%s, dbFName:%s, tbName:%s", tstrerror(rpcRsp.code), dbFName, tNameGetTableName(pTableName));
D
dapan1121 已提交
413
    CTG_ERR_RET(rpcRsp.code);
D
dapan1121 已提交
414 415
  }

D
dapan1121 已提交
416 417
  code = queryProcessMsgRsp[TMSG_INDEX(TDMT_VND_TABLE_META)](output, rpcRsp.pCont, rpcRsp.contLen);
  if (code) {
D
dapan1121 已提交
418
    ctgError("Process vnode tablemeta rsp failed, code:%s, dbFName:%s, tbName:%s", tstrerror(code), dbFName, tNameGetTableName(pTableName));
D
dapan1121 已提交
419 420 421
    CTG_ERR_RET(code);
  }

D
dapan1121 已提交
422
  ctgDebug("Got table meta from vnode, dbFName:%s, tbName:%s", dbFName, tNameGetTableName(pTableName));
D
dapan1121 已提交
423 424 425 426
  return TSDB_CODE_SUCCESS;
}


427 428
int32_t ctgGetHashFunction(int8_t hashMethod, tableNameHashFp *fp) {
  switch (hashMethod) {
D
dapan1121 已提交
429 430 431 432 433 434 435 436
    default:
      *fp = MurmurHash3_32;
      break;
  }

  return TSDB_CODE_SUCCESS;
}

D
dapan 已提交
437
int32_t ctgGetVgInfoFromDB(struct SCatalog *pCatalog, void *pRpc, const SEpSet *pMgmtEps, SDBVgroupInfo *dbInfo, SArray** vgroupList) {
D
dapan1121 已提交
438
  SHashObj *vgroupHash = NULL;
439
  SVgroupInfo *vgInfo = NULL;
D
dapan1121 已提交
440 441
  SArray *vgList = NULL;
  int32_t code = 0;
D
dapan1121 已提交
442
  int32_t vgNum = taosHashGetSize(dbInfo->vgHash);
443

D
dapan1121 已提交
444
  vgList = taosArrayInit(vgNum, sizeof(SVgroupInfo));
D
dapan1121 已提交
445
  if (NULL == vgList) {
D
dapan1121 已提交
446
    ctgError("taosArrayInit failed, num:%d", vgNum);
D
dapan 已提交
447 448 449
    CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);    
  }

D
dapan1121 已提交
450
  void *pIter = taosHashIterate(dbInfo->vgHash, NULL);
451 452
  while (pIter) {
    vgInfo = pIter;
D
dapan1121 已提交
453

D
dapan1121 已提交
454
    if (NULL == taosArrayPush(vgList, vgInfo)) {
D
dapan1121 已提交
455
      ctgError("taosArrayPush failed, vgId:%d", vgInfo->vgId);
D
dapan1121 已提交
456
      CTG_ERR_JRET(TSDB_CODE_CTG_MEM_ERROR);
457 458
    }
    
D
dapan1121 已提交
459
    pIter = taosHashIterate(dbInfo->vgHash, pIter);
460
    vgInfo = NULL;
D
dapan1121 已提交
461 462
  }

D
dapan1121 已提交
463 464 465
  *vgroupList = vgList;
  vgList = NULL;

D
dapan1121 已提交
466 467
  ctgDebug("Got vg list from DB, vgNum:%d", vgNum);

D
dapan1121 已提交
468
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
469 470 471 472 473 474 475 476

_return:

  if (vgList) {
    taosArrayDestroy(vgList);
  }

  CTG_RET(code);
D
dapan1121 已提交
477 478
}

D
dapan1121 已提交
479
int32_t ctgGetVgInfoFromHashValue(struct SCatalog *pCatalog, SDBVgroupInfo *dbInfo, const SName *pTableName, SVgroupInfo *pVgroup) {
D
dapan1121 已提交
480 481
  int32_t code = 0;
  
D
dapan1121 已提交
482
  int32_t vgNum = taosHashGetSize(dbInfo->vgHash);
H
Haojun Liao 已提交
483 484 485
  char db[TSDB_DB_FNAME_LEN] = {0};
  tNameGetFullDbName(pTableName, db);

486
  if (vgNum <= 0) {
D
dapan1121 已提交
487
    ctgError("db vgroup cache invalid, db:%s, vgroup number:%d", db, vgNum);
D
dapan1121 已提交
488
    CTG_ERR_RET(TSDB_CODE_TSC_DB_NOT_SELECTED);
D
dapan1121 已提交
489 490
  }

491 492
  tableNameHashFp fp = NULL;
  SVgroupInfo *vgInfo = NULL;
D
dapan1121 已提交
493

D
dapan1121 已提交
494
  CTG_ERR_RET(ctgGetHashFunction(dbInfo->hashMethod, &fp));
495 496

  char tbFullName[TSDB_TABLE_FNAME_LEN];
H
Haojun Liao 已提交
497
  tNameExtractFullName(pTableName, tbFullName);
498 499 500

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

D
dapan1121 已提交
501
  void *pIter = taosHashIterate(dbInfo->vgHash, NULL);
502 503 504
  while (pIter) {
    vgInfo = pIter;
    if (hashValue >= vgInfo->hashBegin && hashValue <= vgInfo->hashEnd) {
D
dapan1121 已提交
505
      taosHashCancelIterate(dbInfo->vgHash, pIter);
506
      break;
D
dapan1121 已提交
507
    }
508
    
D
dapan1121 已提交
509
    pIter = taosHashIterate(dbInfo->vgHash, pIter);
510
    vgInfo = NULL;
D
dapan1121 已提交
511 512
  }

513
  if (NULL == vgInfo) {
D
dapan1121 已提交
514 515
    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);
516 517 518 519
  }

  *pVgroup = *vgInfo;

520
  CTG_RET(code);
D
dapan1121 已提交
521 522
}

D
dapan1121 已提交
523 524 525 526 527 528 529 530 531 532 533
int32_t ctgSTableVersionCompare(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 ctgDbVgVersionCompare(const void* key1, const void* key2) {
D
dapan1121 已提交
534
  if (*(int64_t *)key1 < ((SDbVgVersion*)key2)->dbId) {
D
dapan1121 已提交
535
    return -1;
D
dapan1121 已提交
536
  } else if (*(int64_t *)key1 > ((SDbVgVersion*)key2)->dbId) {
D
dapan1121 已提交
537 538 539
    return 1;
  } else {
    return 0;
D
dapan1121 已提交
540
  }
D
dapan1121 已提交
541 542 543
}


D
dapan1121 已提交
544
int32_t ctgMetaRentInit(SCtgRentMgmt *mgmt, uint32_t rentSec, int8_t type) {
D
dapan1121 已提交
545 546 547 548
  mgmt->slotRIdx = 0;
  mgmt->slotNum = rentSec / CTG_RENT_SLOT_SECOND;
  mgmt->type = type;

D
dapan1121 已提交
549
  size_t msgSize = sizeof(SCtgRentSlot) * mgmt->slotNum;
D
dapan1121 已提交
550
  
D
dapan1121 已提交
551 552 553 554 555
  mgmt->slots = calloc(1, msgSize);
  if (NULL == mgmt->slots) {
    qError("calloc %d failed", (int32_t)msgSize);
    return TSDB_CODE_CTG_MEM_ERROR;
  }
D
dapan1121 已提交
556

D
dapan1121 已提交
557 558 559 560
  qDebug("meta rent initialized, type:%d, slotNum:%d", type, mgmt->slotNum);
  
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
561

D
dapan1121 已提交
562

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

D
dapan1121 已提交
566
  SCtgRentSlot *slot = &mgmt->slots[widx];
D
dapan1121 已提交
567 568 569 570 571 572 573 574
  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 已提交
575
    }
D
dapan1121 已提交
576
  }
D
dapan1121 已提交
577

D
dapan1121 已提交
578 579 580
  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 已提交
581 582
  }

D
dapan1121 已提交
583
  slot->needSort = true;
D
dapan1121 已提交
584

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

D
dapan1121 已提交
587 588 589 590 591 592
_return:

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

D
dapan1121 已提交
593
int32_t ctgMetaRentUpdate(SCtgRentMgmt *mgmt, void *meta, int64_t id, int32_t size, __compar_fn_t compare) {
D
dapan1121 已提交
594
  int16_t widx = abs(id % mgmt->slotNum);
D
dapan1121 已提交
595

D
dapan1121 已提交
596
  SCtgRentSlot *slot = &mgmt->slots[widx];
D
dapan1121 已提交
597 598 599 600
  int32_t code = 0;
  
  CTG_LOCK(CTG_WRITE, &slot->lock);
  if (NULL == slot->meta) {
D
dapan1121 已提交
601 602
    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 已提交
603 604 605 606 607
  }

  if (slot->needSort) {
    taosArraySort(slot->meta, compare);
    slot->needSort = false;
D
dapan1121 已提交
608
    qDebug("meta slot sorted, slot idx:%d, type:%d", widx, mgmt->type);
D
dapan1121 已提交
609 610 611
  }

  void *orig = taosArraySearch(slot->meta, &id, compare, TD_EQ);
D
dapan1121 已提交
612
  if (NULL == orig) {
D
dapan1121 已提交
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
    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);
  }

  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 已提交
633
int32_t ctgMetaRentRemove(SCtgRentMgmt *mgmt, int64_t id, __compar_fn_t compare) {
D
dapan1121 已提交
634 635
  int16_t widx = abs(id % mgmt->slotNum);

D
dapan1121 已提交
636
  SCtgRentSlot *slot = &mgmt->slots[widx];
D
dapan1121 已提交
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 663 664 665 666 667 668
  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) {
    taosArraySort(slot->meta, compare);
    slot->needSort = false;
    qDebug("meta slot sorted, slot idx:%d, type:%d", widx, mgmt->type);
  }

  int32_t idx = taosArraySearchIdx(slot->meta, &id, compare, TD_EQ);
  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 已提交
669
int32_t ctgMetaRentGetImpl(SCtgRentMgmt *mgmt, void **res, uint32_t *num, int32_t size) {
D
dapan1121 已提交
670 671 672 673
  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 已提交
674
  }
D
dapan1121 已提交
675

D
dapan1121 已提交
676
  SCtgRentSlot *slot = &mgmt->slots[ridx];
D
dapan1121 已提交
677
  int32_t code = 0;
D
dapan1121 已提交
678
  
D
dapan1121 已提交
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
  CTG_LOCK(CTG_READ, &slot->lock);
  if (NULL == slot->meta) {
    qDebug("empty meta in slot:%d, type:%d", ridx, mgmt->type);
    *num = 0;
    goto _return;
  }

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

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

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

  memcpy(*res, meta, msize);

  *num = (uint32_t)metaNum;

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

_return:

  CTG_UNLOCK(CTG_READ, &slot->lock);

  CTG_RET(code);
}

D
dapan1121 已提交
715
int32_t ctgMetaRentGet(SCtgRentMgmt *mgmt, void **res, uint32_t *num, int32_t size) {
D
dapan1121 已提交
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
  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 已提交
735 736 737 738
  return TSDB_CODE_SUCCESS;
}


D
dapan1121 已提交
739

D
dapan1121 已提交
740
int32_t ctgUpdateTableMetaCache(struct SCatalog *pCatalog, STableMetaOutput *output) {
D
dapan1121 已提交
741
  int32_t code = 0;
D
dapan1121 已提交
742
  SCtgDBCache *dbCache = NULL;
D
dapan1121 已提交
743

D
dapan1121 已提交
744
  if ((!CTG_IS_META_CTABLE(output->metaType)) && NULL == output->tbMeta) {
D
dapan1121 已提交
745
    ctgError("no valid tbmeta got from meta rsp, dbFName:%s, tbName:%s", output->dbFName, output->tbName);
D
dapan1121 已提交
746
    CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
D
dapan1121 已提交
747 748
  }

D
dapan1121 已提交
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
  if (NULL == pCatalog->dbCache) {
    SHashObj *cache = taosHashInit(ctgMgmt.cfg.maxDBCacheNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
    if (NULL == cache) {
      ctgError("taosHashInit %d failed", CTG_DEFAULT_CACHE_DB_NUMBER);
      CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
    }

    if (NULL != atomic_val_compare_exchange_ptr(&pCatalog->dbCache, NULL, cache)) {
      taosHashCleanup(cache);
    }
  }

  while (true) {
    dbCache = (SCtgDBCache *)taosHashAcquire(pCatalog->dbCache, output->dbFName, strlen(output->dbFName));
    if (dbCache) {
      break;
    }
    
    SCtgDBCache newDbCache = {0};

    if (taosHashPut(pCatalog->dbCache, output->dbFName, strlen(output->dbFName), &newDbCache, sizeof(newDbCache))) {
      ctgError("taosHashPut db to cache failed, db:%s", output->dbFName);
      CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
    }
  }

  if (NULL == dbCache->tbCache.cache) {
D
dapan1121 已提交
776 777
    SHashObj *cache = taosHashInit(ctgMgmt.cfg.maxTblCacheNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
    if (NULL == cache) {
D
dapan1121 已提交
778
      ctgError("taosHashInit failed, num:%d", ctgMgmt.cfg.maxTblCacheNum);
D
dapan1121 已提交
779
      CTG_ERR_JRET(TSDB_CODE_CTG_MEM_ERROR);
D
dapan1121 已提交
780
    }
D
dapan1121 已提交
781

D
dapan1121 已提交
782
    if (NULL != atomic_val_compare_exchange_ptr(&dbCache->tbCache.cache, NULL, cache)) {
D
dapan1121 已提交
783 784
      taosHashCleanup(cache);
    }
D
dapan1121 已提交
785
  }
D
dapan1121 已提交
786

D
dapan1121 已提交
787
  if (NULL == dbCache->tbCache.stbCache) {
D
dapan1121 已提交
788 789
    SHashObj *cache = taosHashInit(ctgMgmt.cfg.maxTblCacheNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), true, HASH_ENTRY_LOCK);
    if (NULL == cache) {
D
dapan1121 已提交
790
      ctgError("taosHashInit failed, num:%d", ctgMgmt.cfg.maxTblCacheNum);
D
dapan1121 已提交
791
      CTG_ERR_JRET(TSDB_CODE_CTG_MEM_ERROR);
D
dapan1121 已提交
792
    }
D
dapan1121 已提交
793

D
dapan1121 已提交
794
    if (NULL != atomic_val_compare_exchange_ptr(&dbCache->tbCache.stbCache, NULL, cache)) {
D
dapan1121 已提交
795 796
      taosHashCleanup(cache);
    }
D
dapan1121 已提交
797 798
  }

D
dapan1121 已提交
799
  if (CTG_IS_META_CTABLE(output->metaType) || CTG_IS_META_BOTH(output->metaType)) {
D
dapan1121 已提交
800 801 802
    if (taosHashPut(dbCache->tbCache.cache, output->ctbName, strlen(output->ctbName), &output->ctbMeta, sizeof(output->ctbMeta)) != 0) {
      ctgError("taosHashPut ctbmeta to cache failed, ctbName:%s", output->ctbName);
      CTG_ERR_JRET(TSDB_CODE_CTG_MEM_ERROR);
D
dapan1121 已提交
803 804
    }

D
dapan1121 已提交
805
    ctgDebug("ctbmeta updated to cache, ctbName:%s", output->ctbName);
D
dapan1121 已提交
806
  }
D
dapan1121 已提交
807

D
dapan1121 已提交
808
  if (CTG_IS_META_CTABLE(output->metaType)) {
D
dapan1121 已提交
809
    goto _return;
D
dapan1121 已提交
810
  }
D
dapan1121 已提交
811 812 813
  
  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);
D
dapan1121 已提交
814
    CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
D
dapan1121 已提交
815
  }    
D
dapan1121 已提交
816

D
dapan1121 已提交
817
  int32_t tbSize = sizeof(*output->tbMeta) + sizeof(SSchema) * (output->tbMeta->tableInfo.numOfColumns + output->tbMeta->tableInfo.numOfTags);
D
dapan1121 已提交
818 819

  if (TSDB_SUPER_TABLE == output->tbMeta->tableType) {
D
dapan1121 已提交
820 821 822
    bool newAdded = false;
    SSTableMetaVersion metaRent = {.suid = output->tbMeta->suid, .sversion = output->tbMeta->sversion, .tversion = output->tbMeta->tversion};
    
D
dapan1121 已提交
823 824 825 826 827
    CTG_LOCK(CTG_WRITE, &dbCache->tbCache.stbLock);
    if (taosHashPut(dbCache->tbCache.cache, output->tbName, strlen(output->tbName), output->tbMeta, tbSize) != 0) {
      CTG_UNLOCK(CTG_WRITE, &dbCache->tbCache.stbLock);
      ctgError("taosHashPut tablemeta to cache failed, dbFName:%s, tbName:%s", output->dbFName, output->tbName);
      CTG_ERR_JRET(TSDB_CODE_CTG_MEM_ERROR);
D
dapan1121 已提交
828 829
    }

D
dapan1121 已提交
830 831 832
    STableMeta *tbMeta = taosHashGet(dbCache->tbCache.cache, output->tbName, strlen(output->tbName));
    if (taosHashPutExt(dbCache->tbCache.stbCache, &output->tbMeta->suid, sizeof(output->tbMeta->suid), &tbMeta, POINTER_BYTES, &newAdded) != 0) {
      CTG_UNLOCK(CTG_WRITE, &dbCache->tbCache.stbLock);
D
dapan1121 已提交
833
      ctgError("taosHashPutExt stable to stable cache failed, suid:%"PRIx64, output->tbMeta->suid);
D
dapan1121 已提交
834
      CTG_ERR_JRET(TSDB_CODE_CTG_MEM_ERROR);
D
dapan1121 已提交
835
    }
D
dapan1121 已提交
836
    CTG_UNLOCK(CTG_WRITE, &dbCache->tbCache.stbLock);
D
dapan1121 已提交
837 838 839 840

    ctgDebug("update stable to cache, suid:%"PRIx64, output->tbMeta->suid);

    if (newAdded) {
D
dapan1121 已提交
841
      CTG_ERR_JRET(ctgMetaRentAdd(&pCatalog->stbRent, &metaRent, metaRent.suid, sizeof(SSTableMetaVersion)));
D
dapan1121 已提交
842
    } else {
D
dapan1121 已提交
843
      CTG_ERR_JRET(ctgMetaRentUpdate(&pCatalog->stbRent, &metaRent, metaRent.suid, sizeof(SSTableMetaVersion), ctgSTableVersionCompare));
D
dapan1121 已提交
844
    }
D
dapan1121 已提交
845
  } else {
D
dapan1121 已提交
846 847 848
    if (taosHashPut(dbCache->tbCache.cache, output->tbName, strlen(output->tbName), output->tbMeta, tbSize) != 0) {
      ctgError("taosHashPut tablemeta to cache failed, dbFName:%s, tbName:%s", output->dbFName, output->tbName);
      CTG_ERR_JRET(TSDB_CODE_CTG_MEM_ERROR);
D
dapan1121 已提交
849 850
    }
  }
D
dapan1121 已提交
851

D
dapan1121 已提交
852 853 854 855 856 857 858
  ctgDebug("update tablemeta to cache, dbFName:%s, tbName:%s", output->dbFName, output->tbName);

_return:

  if (dbCache) {
    taosHashRelease(pCatalog->dbCache, dbCache);
  }
D
dapan1121 已提交
859

D
dapan1121 已提交
860
  CTG_RET(code);
D
dapan1121 已提交
861 862
}

D
dapan1121 已提交
863
int32_t ctgGetDBVgroup(struct SCatalog* pCatalog, void *pRpc, const SEpSet* pMgmtEps, const char* dbName, bool forceUpdate, SCtgDBCache** dbCache) {
D
dapan1121 已提交
864
  bool inCache = false;
865
  if (!forceUpdate) {
D
dapan1121 已提交
866
    CTG_ERR_RET(ctgGetDBVgroupFromCache(pCatalog, dbName, dbCache, &inCache));
D
dapan1121 已提交
867
    if (inCache) {
D
dapan1121 已提交
868 869
      return TSDB_CODE_SUCCESS;
    }
870 871

    ctgDebug("failed to get DB vgroupInfo from cache, dbName:%s, load it from mnode, update:%d", dbName, forceUpdate);
D
dapan1121 已提交
872 873 874 875 876
  }

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

877
  tstrncpy(input.db, dbName, tListLen(input.db));
D
dapan1121 已提交
878
  input.vgVersion = CTG_DEFAULT_INVALID_VERSION;
H
Haojun Liao 已提交
879

D
dapan1121 已提交
880 881
  while (true) {
    CTG_ERR_RET(ctgGetDBVgroupFromMnode(pCatalog, pRpc, pMgmtEps, &input, &DbOut));
D
dapan1121 已提交
882 883
    CTG_ERR_RET(catalogUpdateDBVgroup(pCatalog, dbName, DbOut.dbVgroup));
    CTG_ERR_RET(ctgGetDBVgroupFromCache(pCatalog, dbName, dbCache, &inCache));
D
dapan1121 已提交
884 885

    if (!inCache) {
D
dapan1121 已提交
886
      ctgWarn("can't get db vgroup from cache, will retry, db:%s", dbName);
D
dapan1121 已提交
887 888 889 890 891 892 893 894 895 896
      continue;
    }

    break;
  }

  return TSDB_CODE_SUCCESS;
}


D
dapan1121 已提交
897
int32_t ctgValidateAndRemoveDb(struct SCatalog* pCatalog, const char* dbName, uint64_t dbId, bool *removed) {
D
dapan1121 已提交
898
  *removed = false;
D
dapan1121 已提交
899

D
dapan1121 已提交
900 901 902
  SCtgDBCache *dbCache = (SCtgDBCache *)taosHashAcquire(pCatalog->dbCache, dbName, strlen(dbName));
  if (NULL == dbCache) {
    ctgInfo("db not exist in dbCache, may be removed, db:%s", dbName);
D
dapan1121 已提交
903 904 905
    return TSDB_CODE_SUCCESS;
  }
  
D
dapan1121 已提交
906
  CTG_LOCK(CTG_WRITE, &dbCache->vgLock);
D
dapan1121 已提交
907
  
D
dapan1121 已提交
908 909 910 911 912 913 914 915 916 917 918
  if (NULL == dbCache->vgInfo) {
    ctgInfo("db vgInfo not in dbCache, may be removed, db:%s, dbId:%"PRIx64, dbName, dbId);
    CTG_UNLOCK(CTG_WRITE, &dbCache->vgLock);
    taosHashRelease(pCatalog->dbCache, dbCache);
    return TSDB_CODE_SUCCESS;
  }
  
  if (dbCache->vgInfo->dbId != dbId) {
    ctgInfo("db id already updated, db:%s, dbId:%"PRIx64 ", targetId:%"PRIx64, dbName, dbCache->vgInfo->dbId, dbId);
    CTG_UNLOCK(CTG_WRITE, &dbCache->vgLock);
    taosHashRelease(pCatalog->dbCache, dbCache);
D
dapan1121 已提交
919 920
    return TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
921
    
D
dapan1121 已提交
922 923 924 925
  if (dbCache->vgInfo->vgHash) {
    ctgInfo("cleanup db vgInfo, db:%s, dbId:%"PRIx64, dbName, dbId);
    taosHashCleanup(dbCache->vgInfo->vgHash);
    tfree(dbCache->vgInfo);
D
dapan1121 已提交
926 927
  }

D
dapan1121 已提交
928 929 930 931
  if (taosHashRemove(pCatalog->dbCache, dbName, strlen(dbName))) {
    ctgError("taosHashRemove from dbCache failed, db:%s", dbName);
    CTG_UNLOCK(CTG_WRITE, &dbCache->vgLock);      
    taosHashRelease(pCatalog->dbCache, dbCache);
D
dapan1121 已提交
932
    CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
D
dapan1121 已提交
933
  }
D
dapan1121 已提交
934 935

  dbCache->deleted = true;
D
dapan1121 已提交
936
  
D
dapan1121 已提交
937 938 939
  CTG_UNLOCK(CTG_WRITE, &dbCache->vgLock);

  ctgFreeTableMetaCache(&dbCache->tbCache);
D
dapan1121 已提交
940

D
dapan1121 已提交
941
  taosHashRelease(pCatalog->dbCache, dbCache);
D
dapan1121 已提交
942

D
dapan1121 已提交
943 944
  *removed = true;
  
D
dapan1121 已提交
945 946 947 948
  return TSDB_CODE_SUCCESS;
}


D
dapan1121 已提交
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
int32_t ctgRenewTableMetaImpl(struct SCatalog* pCatalog, void *pTransporter, const SEpSet* pMgmtEps, const SName* pTableName, int32_t isSTable) {
  if (NULL == pCatalog || NULL == pTransporter || NULL == pMgmtEps || NULL == pTableName) {
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
  }

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

  CTG_ERR_RET(catalogGetTableHashVgroup(pCatalog, pTransporter, pMgmtEps, pTableName, &vgroupInfo));

  STableMetaOutput voutput = {0};
  STableMetaOutput moutput = {0};
  STableMetaOutput *output = &voutput;

  if (CTG_IS_STABLE(isSTable)) {
D
dapan1121 已提交
964
    ctgDebug("will renew tbmeta, supposed to be stb, tbName:%s", tNameGetTableName(pTableName));
D
dapan1121 已提交
965 966

    // if get from mnode failed, will not try vnode
D
dapan1121 已提交
967 968
    CTG_ERR_JRET(ctgGetTableMetaFromMnode(pCatalog, pTransporter, pMgmtEps, pTableName, &moutput));

D
dapan1121 已提交
969
    if (CTG_IS_META_NULL(moutput.metaType)) {
D
dapan1121 已提交
970 971 972 973 974
      CTG_ERR_JRET(ctgGetTableMetaFromVnode(pCatalog, pTransporter, pMgmtEps, pTableName, &vgroupInfo, &voutput));
    } else {
      output = &moutput;
    }
  } else {
D
dapan1121 已提交
975
    ctgDebug("will renew tbmeta, not supposed to be stb, tbName:%s, isStable:%d", tNameGetTableName(pTableName), isSTable);
D
dapan1121 已提交
976 977

    // if get from vnode failed or no table meta, will not try mnode
D
dapan1121 已提交
978 979
    CTG_ERR_JRET(ctgGetTableMetaFromVnode(pCatalog, pTransporter, pMgmtEps, pTableName, &vgroupInfo, &voutput));

D
dapan1121 已提交
980
    if (CTG_IS_META_TABLE(voutput.metaType) && TSDB_SUPER_TABLE == voutput.tbMeta->tableType) {
D
dapan1121 已提交
981
      ctgDebug("will continue to renew tbmeta since got stb, tbName:%s, metaType:%d", tNameGetTableName(pTableName), voutput.metaType);
D
dapan1121 已提交
982
      
D
dapan1121 已提交
983
      CTG_ERR_JRET(ctgGetTableMetaFromMnodeImpl(pCatalog, pTransporter, pMgmtEps, voutput.dbFName, voutput.tbName, &moutput));
D
dapan1121 已提交
984

D
dapan1121 已提交
985 986
      voutput.metaType = moutput.metaType;
      
D
dapan1121 已提交
987 988 989
      tfree(voutput.tbMeta);
      voutput.tbMeta = moutput.tbMeta;
      moutput.tbMeta = NULL;
D
dapan1121 已提交
990 991
    } else if (CTG_IS_META_BOTH(voutput.metaType)) {
      int32_t exist = 0;
D
dapan1121 已提交
992
      CTG_ERR_JRET(ctgIsTableMetaExistInCache(pCatalog, voutput.dbFName, voutput.tbName, &exist));
D
dapan1121 已提交
993
      if (0 == exist) {
D
dapan1121 已提交
994
        CTG_ERR_JRET(ctgGetTableMetaFromMnodeImpl(pCatalog, pTransporter, pMgmtEps, voutput.dbFName, voutput.tbName, &moutput));
D
dapan1121 已提交
995

D
dapan1121 已提交
996 997
        if (CTG_IS_META_NULL(moutput.metaType)) {
          SET_META_TYPE_NULL(voutput.metaType);
D
dapan1121 已提交
998 999 1000 1001 1002 1003
        }
        
        tfree(voutput.tbMeta);
        voutput.tbMeta = moutput.tbMeta;
        moutput.tbMeta = NULL;
      } else {
D
dapan1121 已提交
1004 1005
        tfree(voutput.tbMeta);
        
D
dapan1121 已提交
1006 1007
        SET_META_TYPE_CTABLE(voutput.metaType); 
      }
D
dapan1121 已提交
1008 1009 1010
    }
  }

D
dapan1121 已提交
1011
  if (CTG_IS_META_NULL(output->metaType)) {
1012
    ctgError("no tablemeta got, tbNmae:%s", tNameGetTableName(pTableName));
D
dapan1121 已提交
1013 1014 1015
    CTG_ERR_JRET(CTG_ERR_CODE_TABLE_NOT_EXIST);
  }

D
dapan1121 已提交
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
  CTG_ERR_JRET(ctgUpdateTableMetaCache(pCatalog, output));

_return:

  tfree(voutput.tbMeta);
  tfree(moutput.tbMeta);
  
  CTG_RET(code);
}

D
dapan1121 已提交
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
int32_t ctgGetTableMeta(struct SCatalog* pCatalog, void *pRpc, const SEpSet* pMgmtEps, const SName* pTableName, bool forceUpdate, STableMeta** pTableMeta, int32_t isSTable) {
  if (NULL == pCatalog || NULL == pRpc || NULL == pMgmtEps || NULL == pTableName || NULL == pTableMeta) {
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
  }
  
  int32_t exist = 0;

  if (!forceUpdate) {
    CTG_ERR_RET(ctgGetTableMetaFromCache(pCatalog, pTableName, pTableMeta, &exist));

    if (exist && CTG_TBTYPE_MATCH(isSTable, (*pTableMeta)->tableType)) {
      return TSDB_CODE_SUCCESS;
    }
  } else if (CTG_IS_UNKNOWN_STABLE(isSTable)) {
    int32_t tbType = 0;
    
    CTG_ERR_RET(ctgGetTableTypeFromCache(pCatalog, pTableName, &tbType));

    CTG_SET_STABLE(isSTable, tbType);
  }

  CTG_ERR_RET(ctgRenewTableMetaImpl(pCatalog, pRpc, pMgmtEps, pTableName, isSTable));

  CTG_ERR_RET(ctgGetTableMetaFromCache(pCatalog, pTableName, pTableMeta, &exist));

  if (0 == exist) {
1052
    ctgError("renew tablemeta succeed but get from cache failed, may be deleted, tbName:%s", tNameGetTableName(pTableName));
D
dapan1121 已提交
1053 1054 1055 1056 1057 1058 1059
    CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
  }
  
  return TSDB_CODE_SUCCESS;
}


D
dapan1121 已提交
1060
int32_t catalogInit(SCatalogCfg *cfg) {
D
dapan1121 已提交
1061
  if (ctgMgmt.pCluster) {
D
dapan1121 已提交
1062
    qError("catalog already init");
D
dapan1121 已提交
1063
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
D
dapan1121 已提交
1064 1065 1066 1067
  }

  if (cfg) {
    memcpy(&ctgMgmt.cfg, cfg, sizeof(*cfg));
H
Haojun Liao 已提交
1068

D
dapan1121 已提交
1069 1070 1071 1072 1073 1074 1075
    if (ctgMgmt.cfg.maxDBCacheNum == 0) {
      ctgMgmt.cfg.maxDBCacheNum = CTG_DEFAULT_CACHE_DB_NUMBER;
    }

    if (ctgMgmt.cfg.maxTblCacheNum == 0) {
      ctgMgmt.cfg.maxTblCacheNum = CTG_DEFAULT_CACHE_TABLEMETA_NUMBER;
    }
D
dapan1121 已提交
1076 1077 1078 1079 1080

    if (ctgMgmt.cfg.dbRentSec == 0) {
      ctgMgmt.cfg.dbRentSec = CTG_DEFAULT_RENT_SECOND;
    }

D
dapan1121 已提交
1081 1082
    if (ctgMgmt.cfg.stbRentSec == 0) {
      ctgMgmt.cfg.stbRentSec = CTG_DEFAULT_RENT_SECOND;
D
dapan1121 已提交
1083
    }
D
dapan1121 已提交
1084 1085 1086
  } else {
    ctgMgmt.cfg.maxDBCacheNum = CTG_DEFAULT_CACHE_DB_NUMBER;
    ctgMgmt.cfg.maxTblCacheNum = CTG_DEFAULT_CACHE_TABLEMETA_NUMBER;
D
dapan1121 已提交
1087
    ctgMgmt.cfg.dbRentSec = CTG_DEFAULT_RENT_SECOND;
D
dapan1121 已提交
1088
    ctgMgmt.cfg.stbRentSec = CTG_DEFAULT_RENT_SECOND;
D
dapan 已提交
1089 1090
  }

D
dapan1121 已提交
1091
  ctgMgmt.pCluster = taosHashInit(CTG_DEFAULT_CACHE_CLUSTER_NUMBER, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
D
dapan1121 已提交
1092
  if (NULL == ctgMgmt.pCluster) {
D
dapan1121 已提交
1093 1094
    qError("taosHashInit %d cluster cache failed", CTG_DEFAULT_CACHE_CLUSTER_NUMBER);
    CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
D
dapan1121 已提交
1095 1096
  }

D
dapan1121 已提交
1097
  qDebug("catalog initialized, maxDb:%u, maxTbl:%u, dbRentSec:%u, stbRentSec:%u", ctgMgmt.cfg.maxDBCacheNum, ctgMgmt.cfg.maxTblCacheNum, ctgMgmt.cfg.dbRentSec, ctgMgmt.cfg.stbRentSec);
D
dapan1121 已提交
1098

D
dapan 已提交
1099
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1100 1101
}

1102 1103
int32_t catalogGetHandle(uint64_t clusterId, struct SCatalog** catalogHandle) {
  if (NULL == catalogHandle) {
D
dapan1121 已提交
1104
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
D
dapan 已提交
1105 1106 1107
  }

  if (NULL == ctgMgmt.pCluster) {
D
dapan1121 已提交
1108
    qError("cluster cache are not ready, clusterId:%"PRIx64, clusterId);
D
dapan1121 已提交
1109
    CTG_ERR_RET(TSDB_CODE_CTG_NOT_READY);
D
dapan 已提交
1110 1111
  }

D
dapan1121 已提交
1112 1113
  int32_t code = 0;
  SCatalog *clusterCtg = NULL;
D
dapan 已提交
1114

D
dapan1121 已提交
1115 1116
  while (true) {
    SCatalog **ctg = (SCatalog **)taosHashGet(ctgMgmt.pCluster, (char*)&clusterId, sizeof(clusterId));
D
dapan 已提交
1117

D
dapan1121 已提交
1118 1119 1120 1121 1122
    if (ctg && (*ctg)) {
      *catalogHandle = *ctg;
      qDebug("got catalog handle from cache, clusterId:%"PRIx64", CTG:%p", clusterId, *ctg);
      return TSDB_CODE_SUCCESS;
    }
D
dapan 已提交
1123

D
dapan1121 已提交
1124 1125 1126 1127 1128 1129
    clusterCtg = calloc(1, sizeof(SCatalog));
    if (NULL == clusterCtg) {
      qError("calloc %d failed", (int32_t)sizeof(SCatalog));
      CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
    }

D
dapan1121 已提交
1130 1131
    clusterCtg->clusterId = clusterId;

D
dapan1121 已提交
1132
    CTG_ERR_JRET(ctgMetaRentInit(&clusterCtg->dbRent, ctgMgmt.cfg.dbRentSec, CTG_RENT_DB));
D
dapan1121 已提交
1133
    CTG_ERR_JRET(ctgMetaRentInit(&clusterCtg->stbRent, ctgMgmt.cfg.stbRentSec, CTG_RENT_STABLE));
D
dapan1121 已提交
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148

    code = taosHashPut(ctgMgmt.pCluster, &clusterId, sizeof(clusterId), &clusterCtg, POINTER_BYTES);
    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 已提交
1149
  }
D
dapan1121 已提交
1150 1151

  *catalogHandle = clusterCtg;
D
dapan 已提交
1152
  
D
dapan1121 已提交
1153
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165

_return:

  ctgFreeHandle(clusterCtg);
  
  CTG_RET(code);
}

void catalogFreeHandle(struct SCatalog* pCatalog) {
  if (NULL == pCatalog) {
    return;
  }
D
dapan1121 已提交
1166 1167 1168 1169 1170 1171 1172

  if (taosHashRemove(ctgMgmt.pCluster, &pCatalog->clusterId, sizeof(pCatalog->clusterId))) {
    ctgWarn("taosHashRemove from cluster failed, may already be freed, clusterId:%"PRIx64, pCatalog->clusterId);
    return;
  }

  uint64_t clusterId = pCatalog->clusterId;
D
dapan1121 已提交
1173 1174
  
  ctgFreeHandle(pCatalog);
D
dapan1121 已提交
1175 1176

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

D
dapan1121 已提交
1179 1180
int32_t catalogGetDBVgroupVersion(struct SCatalog* pCatalog, const char* dbName, int32_t* version) {
  if (NULL == pCatalog || NULL == dbName || NULL == version) {
D
dapan1121 已提交
1181
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
D
dapan1121 已提交
1182 1183
  }

D
dapan1121 已提交
1184
  if (NULL == pCatalog->dbCache) {
D
dapan1121 已提交
1185
    *version = CTG_DEFAULT_INVALID_VERSION;
D
dapan1121 已提交
1186
    ctgInfo("empty db cache, dbName:%s", dbName);
D
dapan1121 已提交
1187 1188 1189
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
1190 1191
  SCtgDBCache *db = taosHashAcquire(pCatalog->dbCache, dbName, strlen(dbName));
  if (NULL == db) {
D
dapan1121 已提交
1192
    *version = CTG_DEFAULT_INVALID_VERSION;
D
dapan1121 已提交
1193
    ctgInfo("db not in cache, dbName:%s", dbName);
D
dapan1121 已提交
1194 1195 1196
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
  CTG_LOCK(CTG_READ, &db->vgLock);
  
  if (NULL == db->vgInfo) {
    CTG_UNLOCK(CTG_READ, &db->vgLock);

    *version = CTG_DEFAULT_INVALID_VERSION;
    ctgInfo("db not in cache, dbName:%s", dbName);
    return TSDB_CODE_SUCCESS;
  }

  *version = db->vgInfo->vgVersion;
  CTG_UNLOCK(CTG_READ, &db->vgLock);
  
  taosHashRelease(pCatalog->dbCache, db);
D
dapan1121 已提交
1211

D
dapan1121 已提交
1212 1213
  ctgDebug("Got db vgVersion from cache, dbName:%s, vgVersion:%d", dbName, *version);

D
dapan1121 已提交
1214 1215 1216
  return TSDB_CODE_SUCCESS;
}

1217
int32_t catalogGetDBVgroup(struct SCatalog* pCatalog, void *pRpc, const SEpSet* pMgmtEps, const char* dbName, bool forceUpdate, SArray** vgroupList) {
D
dapan1121 已提交
1218 1219 1220 1221
  if (NULL == pCatalog || NULL == dbName || NULL == pRpc || NULL == pMgmtEps || NULL == vgroupList) {
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
  }

D
dapan1121 已提交
1222
  SCtgDBCache* dbCache   = NULL;
D
dapan1121 已提交
1223
  SVgroupInfo *vgInfo = NULL;
1224 1225

  int32_t code = 0;
D
dapan1121 已提交
1226
  SArray *vgList = NULL;
D
dapan1121 已提交
1227
  CTG_ERR_JRET(ctgGetDBVgroup(pCatalog, pRpc, pMgmtEps, dbName, forceUpdate, &dbCache));
D
dapan1121 已提交
1228

D
dapan1121 已提交
1229 1230
  int32_t vgNum = (int32_t)taosHashGetSize(dbCache->vgInfo->vgHash);
  vgList = taosArrayInit(vgNum, sizeof(SVgroupInfo));
D
dapan1121 已提交
1231
  if (NULL == vgList) {
D
dapan1121 已提交
1232
    ctgError("taosArrayInit %d failed", vgNum);
D
dapan1121 已提交
1233 1234 1235
    CTG_ERR_JRET(TSDB_CODE_CTG_MEM_ERROR);    
  }

D
dapan1121 已提交
1236
  void *pIter = taosHashIterate(dbCache->vgInfo->vgHash, NULL);
D
dapan1121 已提交
1237 1238 1239 1240
  while (pIter) {
    vgInfo = pIter;

    if (NULL == taosArrayPush(vgList, vgInfo)) {
D
dapan1121 已提交
1241
      ctgError("taosArrayPush failed, vgId:%d", vgInfo->vgId);
D
dapan1121 已提交
1242
      taosHashCancelIterate(dbCache->vgInfo->vgHash, pIter);
D
dapan1121 已提交
1243 1244 1245
      CTG_ERR_JRET(TSDB_CODE_CTG_MEM_ERROR);
    }
    
D
dapan1121 已提交
1246
    pIter = taosHashIterate(dbCache->vgInfo->vgHash, pIter);
D
dapan1121 已提交
1247 1248 1249 1250 1251 1252 1253
    vgInfo = NULL;
  }

  *vgroupList = vgList;
  vgList = NULL;

_return:
D
dapan1121 已提交
1254 1255 1256 1257

  if (dbCache) {
    CTG_UNLOCK(CTG_READ, &dbCache->vgLock);
    taosHashRelease(pCatalog->dbCache, dbCache);
D
dapan1121 已提交
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
  }

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

  CTG_RET(code);  
}


D
dapan1121 已提交
1269
int32_t catalogUpdateDBVgroup(struct SCatalog* pCatalog, const char* dbName, SDBVgroupInfo* dbInfo) {
D
dapan1121 已提交
1270 1271
  int32_t code = 0;
  
D
dapan1121 已提交
1272
  if (NULL == pCatalog || NULL == dbName || NULL == dbInfo) {
D
dapan1121 已提交
1273 1274 1275
    CTG_ERR_JRET(TSDB_CODE_CTG_INVALID_INPUT);
  }

D
dapan1121 已提交
1276 1277
  if (NULL == dbInfo->vgHash || dbInfo->vgVersion < 0 || taosHashGetSize(dbInfo->vgHash) <= 0) {
    ctgError("invalid db vgInfo, dbName:%s, vgHash:%p, vgVersion:%d", dbName, dbInfo->vgHash, dbInfo->vgVersion);
D
dapan1121 已提交
1278
    CTG_ERR_JRET(TSDB_CODE_CTG_MEM_ERROR);
D
dapan1121 已提交
1279 1280
  }

D
dapan1121 已提交
1281
  if (NULL == pCatalog->dbCache) {
D
dapan1121 已提交
1282 1283
    SHashObj *cache = taosHashInit(ctgMgmt.cfg.maxDBCacheNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
    if (NULL == cache) {
D
dapan1121 已提交
1284
      ctgError("taosHashInit %d failed", CTG_DEFAULT_CACHE_DB_NUMBER);
D
dapan1121 已提交
1285
      CTG_ERR_JRET(TSDB_CODE_CTG_MEM_ERROR);
D
dapan1121 已提交
1286
    }
D
dapan1121 已提交
1287

D
dapan1121 已提交
1288
    if (NULL != atomic_val_compare_exchange_ptr(&pCatalog->dbCache, NULL, cache)) {
D
dapan1121 已提交
1289 1290
      taosHashCleanup(cache);
    }
D
dapan1121 已提交
1291 1292 1293
  }

  bool newAdded = false;
D
dapan1121 已提交
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
  SDbVgVersion vgVersion = {.dbId = dbInfo->dbId, .vgVersion = dbInfo->vgVersion};

  SCtgDBCache *dbCache = (SCtgDBCache *)taosHashAcquire(pCatalog->dbCache, dbName, strlen(dbName));
  if (dbCache) {
    CTG_LOCK(CTG_WRITE, &dbCache->vgLock);
    
    if (NULL == dbCache->vgInfo) {
      newAdded = true;

      dbCache->vgInfo = dbInfo;
    } else {
      if (dbCache->vgInfo->dbId != dbInfo->dbId) {
        ctgMetaRentRemove(&pCatalog->dbRent, dbCache->vgInfo->dbId, ctgDbVgVersionCompare);
        newAdded = true;
      } else if (dbInfo->vgVersion <= dbCache->vgInfo->vgVersion) {
        ctgInfo("db vgVersion is not new, db:%s, vgVersion:%d, current:%d", dbName, dbInfo->vgVersion, dbCache->vgInfo->vgVersion);
        CTG_UNLOCK(CTG_WRITE, &dbCache->vgLock);
        taosHashRelease(pCatalog->dbCache, dbCache);
        
        goto _return;
      }
      
      if (dbCache->vgInfo->vgHash) {
        ctgInfo("cleanup db vgHash, db:%s", dbName);
        taosHashCleanup(dbCache->vgInfo->vgHash);
        dbCache->vgInfo->vgHash = NULL;
      }
    }

    CTG_UNLOCK(CTG_WRITE, &dbCache->vgLock);
    taosHashRelease(pCatalog->dbCache, dbCache);
  } else {
    SCtgDBCache newDBCache = {0};
    newDBCache.vgInfo = dbInfo;
    
    if (taosHashPut(pCatalog->dbCache, dbName, strlen(dbName), &newDBCache, sizeof(newDBCache)) != 0) {
      ctgError("taosHashPut db & db vgroup to cache failed, db:%s", dbName);
      CTG_ERR_JRET(TSDB_CODE_CTG_MEM_ERROR);
    }

    newAdded = true;
D
dapan1121 已提交
1335 1336
  }

D
dapan1121 已提交
1337
  dbInfo = NULL;
D
dapan1121 已提交
1338

D
dapan1121 已提交
1339 1340
  strncpy(vgVersion.dbName, dbName, sizeof(vgVersion.dbName));
  
D
dapan1121 已提交
1341
  if (newAdded) {
D
dapan1121 已提交
1342
    CTG_ERR_JRET(ctgMetaRentAdd(&pCatalog->dbRent, &vgVersion, vgVersion.dbId, sizeof(SDbVgVersion)));
D
dapan1121 已提交
1343
  } else {
D
dapan1121 已提交
1344
    CTG_ERR_JRET(ctgMetaRentUpdate(&pCatalog->dbRent, &vgVersion, vgVersion.dbId, sizeof(SDbVgVersion), ctgDbVgVersionCompare));
D
dapan1121 已提交
1345 1346
  }
  
D
dapan1121 已提交
1347
  ctgDebug("dbName:%s vgroup updated, vgVersion:%d", dbName, vgVersion.vgVersion);
D
dapan1121 已提交
1348 1349 1350 1351


_return:

D
dapan1121 已提交
1352 1353 1354 1355
  if (dbInfo) {
    taosHashCleanup(dbInfo->vgHash);
    dbInfo->vgHash = NULL;
    tfree(dbInfo);
D
dapan1121 已提交
1356
  }
D
dapan1121 已提交
1357 1358 1359 1360 1361
  
  CTG_RET(code);
}


D
dapan1121 已提交
1362
int32_t catalogRemoveDB(struct SCatalog* pCatalog, const char* dbName, uint64_t dbId) {
D
dapan1121 已提交
1363
  int32_t code = 0;
D
dapan1121 已提交
1364
  bool removed = false;
D
dapan1121 已提交
1365
  
D
dapan1121 已提交
1366
  if (NULL == pCatalog || NULL == dbName) {
D
dapan1121 已提交
1367
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
D
dapan1121 已提交
1368 1369
  }

D
dapan1121 已提交
1370
  if (NULL == pCatalog->dbCache) {
D
dapan1121 已提交
1371
    return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1372 1373
  }
  
D
dapan1121 已提交
1374
  CTG_ERR_RET(ctgValidateAndRemoveDb(pCatalog, dbName, dbId, &removed));
D
dapan1121 已提交
1375 1376
  if (!removed) {
    return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1377 1378
  }
  
D
dapan1121 已提交
1379
  ctgInfo("db removed from cache, db:%s, uid:%"PRIx64, dbName, dbId);
D
dapan1121 已提交
1380

D
dapan1121 已提交
1381
  CTG_ERR_RET(ctgMetaRentRemove(&pCatalog->dbRent, dbId, ctgDbVgVersionCompare));
D
dapan1121 已提交
1382
  
D
dapan1121 已提交
1383
  ctgDebug("db removed from rent, db:%s, uid:%"PRIx64, dbName, dbId);
D
dapan1121 已提交
1384 1385
  
  CTG_RET(code);
D
dapan1121 已提交
1386 1387
}

D
dapan1121 已提交
1388

H
Haojun Liao 已提交
1389
int32_t catalogGetTableMeta(struct SCatalog* pCatalog, void *pTransporter, const SEpSet* pMgmtEps, const SName* pTableName, STableMeta** pTableMeta) {
D
dapan1121 已提交
1390
  return ctgGetTableMeta(pCatalog, pTransporter, pMgmtEps, pTableName, false, pTableMeta, -1);
D
dapan1121 已提交
1391
}
D
dapan1121 已提交
1392

D
dapan1121 已提交
1393
int32_t catalogGetSTableMeta(struct SCatalog* pCatalog, void * pTransporter, const SEpSet* pMgmtEps, const SName* pTableName, STableMeta** pTableMeta) {
D
dapan1121 已提交
1394
  return ctgGetTableMeta(pCatalog, pTransporter, pMgmtEps, pTableName, false, pTableMeta, 1);
D
dapan1121 已提交
1395 1396 1397
}

int32_t catalogRenewTableMeta(struct SCatalog* pCatalog, void *pTransporter, const SEpSet* pMgmtEps, const SName* pTableName, int32_t isSTable) {
1398
  if (NULL == pCatalog || NULL == pTransporter || NULL == pMgmtEps || NULL == pTableName) {
D
dapan1121 已提交
1399
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
D
dapan 已提交
1400 1401
  }

D
dapan1121 已提交
1402
  return ctgRenewTableMetaImpl(pCatalog, pTransporter, pMgmtEps, pTableName, isSTable);
1403
}
1404

D
dapan1121 已提交
1405
int32_t catalogRenewAndGetTableMeta(struct SCatalog* pCatalog, void *pTransporter, const SEpSet* pMgmtEps, const SName* pTableName, STableMeta** pTableMeta, int32_t isSTable) {
D
dapan1121 已提交
1406
  return ctgGetTableMeta(pCatalog, pTransporter, pMgmtEps, pTableName, true, pTableMeta, isSTable);
D
dapan1121 已提交
1407 1408
}

H
Haojun Liao 已提交
1409 1410
int32_t catalogGetTableDistVgroup(struct SCatalog* pCatalog, void *pRpc, const SEpSet* pMgmtEps, const SName* pTableName, SArray** pVgroupList) {
  if (NULL == pCatalog || NULL == pRpc || NULL == pMgmtEps || NULL == pTableName || NULL == pVgroupList) {
D
dapan1121 已提交
1411
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
D
dapan1121 已提交
1412 1413 1414 1415 1416
  }
  
  STableMeta *tbMeta = NULL;
  int32_t code = 0;
  SVgroupInfo vgroupInfo = {0};
D
dapan1121 已提交
1417
  SCtgDBCache* dbCache = NULL;
D
dapan1121 已提交
1418 1419 1420
  SArray *vgList = NULL;

  *pVgroupList = NULL;
D
dapan1121 已提交
1421
  
D
dapan1121 已提交
1422
  CTG_ERR_JRET(ctgGetTableMeta(pCatalog, pRpc, pMgmtEps, pTableName, false, &tbMeta, -1));
D
dapan1121 已提交
1423

H
Haojun Liao 已提交
1424 1425
  char db[TSDB_DB_FNAME_LEN] = {0};
  tNameGetFullDbName(pTableName, db);
D
dapan1121 已提交
1426
  CTG_ERR_JRET(ctgGetDBVgroup(pCatalog, pRpc, pMgmtEps, db, false, &dbCache));
D
dapan 已提交
1427

D
dapan 已提交
1428 1429 1430 1431 1432 1433 1434 1435 1436 1437
  // REMOEV THIS ....
  if (0 == tbMeta->vgId) {
    SVgroupInfo vgroup = {0};
    
    catalogGetTableHashVgroup(pCatalog, pRpc, pMgmtEps, pTableName, &vgroup);

    tbMeta->vgId = vgroup.vgId;
  }
  // REMOVE THIS ....

1438
  if (tbMeta->tableType == TSDB_SUPER_TABLE) {
D
dapan1121 已提交
1439
    CTG_ERR_JRET(ctgGetVgInfoFromDB(pCatalog, pRpc, pMgmtEps, dbCache->vgInfo, pVgroupList));
D
dapan1121 已提交
1440
  } else {
1441
    int32_t vgId = tbMeta->vgId;
D
dapan1121 已提交
1442
    if (NULL == taosHashGetClone(dbCache->vgInfo->vgHash, &vgId, sizeof(vgId), &vgroupInfo)) {
1443
      ctgError("table's vgId not found in vgroup list, vgId:%d, tbName:%s", vgId, tNameGetTableName(pTableName));
D
dapan 已提交
1444
      CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);    
1445
    }
D
dapan1121 已提交
1446

D
dapan1121 已提交
1447 1448
    vgList = taosArrayInit(1, sizeof(SVgroupInfo));
    if (NULL == vgList) {
D
dapan1121 已提交
1449
      ctgError("taosArrayInit %d failed", (int32_t)sizeof(SVgroupInfo));
D
dapan 已提交
1450 1451 1452
      CTG_ERR_JRET(TSDB_CODE_CTG_MEM_ERROR);    
    }

D
dapan1121 已提交
1453
    if (NULL == taosArrayPush(vgList, &vgroupInfo)) {
1454
      ctgError("taosArrayPush vgroupInfo to array failed, vgId:%d, tbName:%s", vgId, tNameGetTableName(pTableName));
D
dapan1121 已提交
1455 1456
      CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
    }
D
dapan 已提交
1457

D
dapan1121 已提交
1458 1459 1460
    *pVgroupList = vgList;
    vgList = NULL;
  }
D
dapan 已提交
1461

D
dapan1121 已提交
1462 1463
_return:
  tfree(tbMeta);
D
dapan 已提交
1464

D
dapan1121 已提交
1465 1466 1467
  if (dbCache) {
    CTG_UNLOCK(CTG_READ, &dbCache->vgLock);
    taosHashRelease(pCatalog->dbCache, dbCache);
D
dapan1121 已提交
1468 1469 1470 1471 1472 1473
  }

  if (vgList) {
    taosArrayDestroy(vgList);
    vgList = NULL;
  }
D
dapan1121 已提交
1474
  
D
dapan1121 已提交
1475
  CTG_RET(code);
D
dapan1121 已提交
1476 1477 1478
}


H
Haojun Liao 已提交
1479
int32_t catalogGetTableHashVgroup(struct SCatalog *pCatalog, void *pTransporter, const SEpSet *pMgmtEps, const SName *pTableName, SVgroupInfo *pVgroup) {
D
dapan1121 已提交
1480
  SCtgDBCache* dbCache = NULL;
D
dapan1121 已提交
1481 1482
  int32_t code = 0;

H
Haojun Liao 已提交
1483 1484
  char db[TSDB_DB_FNAME_LEN] = {0};
  tNameGetFullDbName(pTableName, db);
D
dapan1121 已提交
1485

D
dapan1121 已提交
1486
  CTG_ERR_RET(ctgGetDBVgroup(pCatalog, pTransporter, pMgmtEps, db, false, &dbCache));
D
dapan1121 已提交
1487

D
dapan1121 已提交
1488
  CTG_ERR_JRET(ctgGetVgInfoFromHashValue(pCatalog, dbCache->vgInfo, pTableName, pVgroup));
D
dapan1121 已提交
1489

D
dapan1121 已提交
1490
_return:
D
dapan1121 已提交
1491

D
dapan1121 已提交
1492 1493 1494
  if (dbCache) {
    CTG_UNLOCK(CTG_READ, &dbCache->vgLock);  
    taosHashRelease(pCatalog->dbCache, dbCache);
D
dapan1121 已提交
1495
  }
D
dapan1121 已提交
1496

D
dapan1121 已提交
1497
  CTG_RET(code);
D
dapan1121 已提交
1498 1499 1500
}


H
Haojun Liao 已提交
1501 1502
int32_t catalogGetAllMeta(struct SCatalog* pCatalog, void *pTransporter, const SEpSet* pMgmtEps, const SCatalogReq* pReq, SMetaData* pRsp) {
  if (NULL == pCatalog || NULL == pTransporter || NULL == pMgmtEps || NULL == pReq || NULL == pRsp) {
D
dapan1121 已提交
1503
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
D
dapan 已提交
1504
  }
D
dapan1121 已提交
1505 1506 1507 1508 1509

  int32_t code = 0;

  if (pReq->pTableName) {
    int32_t tbNum = (int32_t)taosArrayGetSize(pReq->pTableName);
D
dapan1121 已提交
1510
    if (tbNum <= 0) {
D
dapan1121 已提交
1511
      ctgError("empty table name list, tbNum:%d", tbNum);
D
dapan1121 已提交
1512 1513
      CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
    }
H
Haojun Liao 已提交
1514

D
dapan1121 已提交
1515 1516
    pRsp->pTableMeta = taosArrayInit(tbNum, POINTER_BYTES);
    if (NULL == pRsp->pTableMeta) {
D
dapan1121 已提交
1517
      ctgError("taosArrayInit %d failed", tbNum);
D
dapan1121 已提交
1518
      CTG_ERR_RET(TSDB_CODE_CTG_MEM_ERROR);
D
dapan1121 已提交
1519 1520 1521 1522 1523 1524
    }
    
    for (int32_t i = 0; i < tbNum; ++i) {
      SName *name = taosArrayGet(pReq->pTableName, i);
      STableMeta *pTableMeta = NULL;
      
H
Haojun Liao 已提交
1525
      CTG_ERR_JRET(ctgGetTableMeta(pCatalog, pTransporter, pMgmtEps, name, false, &pTableMeta, -1));
D
dapan1121 已提交
1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537

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

  return TSDB_CODE_SUCCESS;

_return:  
D
dapan1121 已提交
1538

D
dapan1121 已提交
1539 1540 1541 1542 1543 1544 1545 1546
  if (pRsp->pTableMeta) {
    int32_t aSize = taosArrayGetSize(pRsp->pTableMeta);
    for (int32_t i = 0; i < aSize; ++i) {
      STableMeta *pMeta = taosArrayGetP(pRsp->pTableMeta, i);
      tfree(pMeta);
    }
    
    taosArrayDestroy(pRsp->pTableMeta);
D
dapan1121 已提交
1547
    pRsp->pTableMeta = NULL;
D
dapan1121 已提交
1548
  }
D
dapan 已提交
1549
  
D
dapan1121 已提交
1550
  CTG_RET(code);
1551
}
D
dapan 已提交
1552

D
dapan1121 已提交
1553 1554
int32_t catalogGetQnodeList(struct SCatalog* pCatalog, void *pRpc, const SEpSet* pMgmtEps, SArray* pQnodeList) {
  if (NULL == pCatalog || NULL == pRpc  || NULL == pMgmtEps || NULL == pQnodeList) {
D
dapan 已提交
1555 1556 1557
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
  }

D
dapan1121 已提交
1558
  //TODO
D
dapan 已提交
1559 1560 1561 1562

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1563 1564 1565 1566 1567
int32_t catalogGetExpiredSTables(struct SCatalog* pCatalog, SSTableMetaVersion **stables, uint32_t *num) {
  if (NULL == pCatalog || NULL == stables || NULL == num) {
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
  }

D
dapan1121 已提交
1568
  CTG_RET(ctgMetaRentGet(&pCatalog->stbRent, (void **)stables, num, sizeof(SSTableMetaVersion)));
D
dapan1121 已提交
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578
}

int32_t catalogGetExpiredDBs(struct SCatalog* pCatalog, SDbVgVersion **dbs, uint32_t *num) {
  if (NULL == pCatalog || NULL == dbs || NULL == num) {
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
  }

  CTG_RET(ctgMetaRentGet(&pCatalog->dbRent, (void **)dbs, num, sizeof(SDbVgVersion)));
}

D
dapan 已提交
1579

D
dapan 已提交
1580
void catalogDestroy(void) {
D
dapan1121 已提交
1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594
  if (NULL == ctgMgmt.pCluster) {
    return;
  }

  SCatalog *pCatalog = NULL;
  void *pIter = taosHashIterate(ctgMgmt.pCluster, NULL);
  while (pIter) {
    pCatalog = *(SCatalog **)pIter;

    if (pCatalog) {
      catalogFreeHandle(pCatalog);
    }
    
    pIter = taosHashIterate(ctgMgmt.pCluster, pIter);
D
dapan 已提交
1595
  }
D
dapan1121 已提交
1596 1597 1598
  
  taosHashCleanup(ctgMgmt.pCluster);
  ctgMgmt.pCluster = NULL;
D
dapan1121 已提交
1599 1600

  qInfo("catalog destroyed");
D
dapan 已提交
1601 1602 1603 1604
}