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

#include "trpc.h"
#include "query.h"
#include "tname.h"
#include "catalogInt.h"
#include "systable.h"

D
dapan1121 已提交
22
SCtgOperation gCtgCacheOperation[CTG_OP_MAX] = {
D
dapan1121 已提交
23
  {
D
dapan1121 已提交
24
    CTG_OP_UPDATE_VGROUP,
D
dapan1121 已提交
25
    "update vgInfo",
D
dapan1121 已提交
26
    ctgOpUpdateVgroup
D
dapan1121 已提交
27 28
  },
  {
D
dapan1121 已提交
29
    CTG_OP_UPDATE_TB_META,
D
dapan1121 已提交
30
    "update tbMeta",
D
dapan1121 已提交
31
    ctgOpUpdateTbMeta
D
dapan1121 已提交
32 33
  },
  {
D
dapan1121 已提交
34 35 36
    CTG_OP_DROP_DB_CACHE,
    "drop DB",
    ctgOpDropDbCache
D
dapan1121 已提交
37
  },
D
dapan1121 已提交
38 39 40 41 42
  {
    CTG_OP_DROP_DB_VGROUP,
    "drop DBVgroup",
    ctgOpDropDbVgroup
  },
D
dapan1121 已提交
43
  {
D
dapan1121 已提交
44 45 46
    CTG_OP_DROP_STB_META,
    "drop stbMeta",
    ctgOpDropStbMeta
D
dapan1121 已提交
47 48
  },
  {
D
dapan1121 已提交
49 50 51
    CTG_OP_DROP_TB_META,
    "drop tbMeta",
    ctgOpDropTbMeta
D
dapan1121 已提交
52 53
  },
  {
D
dapan1121 已提交
54
    CTG_OP_UPDATE_USER,
D
dapan1121 已提交
55
    "update user",
D
dapan1121 已提交
56 57 58 59 60 61
    ctgOpUpdateUser
  },
  {
    CTG_OP_UPDATE_VG_EPSET,
    "update epset",
    ctgOpUpdateEpset
D
dapan1121 已提交
62 63 64 65 66
  },
  {
    CTG_OP_UPDATE_TB_INDEX,
    "update tbIndex",
    ctgOpUpdateTbIndex
D
dapan1121 已提交
67 68 69 70 71
  },
  {
    CTG_OP_DROP_TB_INDEX,
    "drop tbIndex",
    ctgOpDropTbIndex
D
dapan1121 已提交
72 73 74 75 76
  },
  {
    CTG_OP_CLEAR_CACHE,
    "clear cache",
    ctgOpClearCache
D
dapan1121 已提交
77
  }  
D
dapan1121 已提交
78 79 80 81 82
};




D
dapan1121 已提交
83 84
int32_t ctgRLockVgInfo(SCatalog *pCtg, SCtgDBCache *dbCache, bool *inCache) {
  CTG_LOCK(CTG_READ, &dbCache->vgCache.vgLock);
D
dapan1121 已提交
85 86
  
  if (dbCache->deleted) {
D
dapan1121 已提交
87
    CTG_UNLOCK(CTG_READ, &dbCache->vgCache.vgLock);
D
dapan1121 已提交
88

D
dapan1121 已提交
89
    ctgDebug("db is dropping, dbId:0x%"PRIx64, dbCache->dbId);
D
dapan1121 已提交
90 91 92 93 94 95
    
    *inCache = false;
    return TSDB_CODE_SUCCESS;
  }

  
D
dapan1121 已提交
96 97
  if (NULL == dbCache->vgCache.vgInfo) {
    CTG_UNLOCK(CTG_READ, &dbCache->vgCache.vgLock);
D
dapan1121 已提交
98 99

    *inCache = false;
D
dapan1121 已提交
100
    ctgDebug("db vgInfo is empty, dbId:0x%"PRIx64, dbCache->dbId);
D
dapan1121 已提交
101 102 103 104 105 106 107 108
    return TSDB_CODE_SUCCESS;
  }

  *inCache = true;
  
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
109 110
int32_t ctgWLockVgInfo(SCatalog *pCtg, SCtgDBCache *dbCache) {
  CTG_LOCK(CTG_WRITE, &dbCache->vgCache.vgLock);
D
dapan1121 已提交
111 112

  if (dbCache->deleted) {
D
dapan1121 已提交
113
    ctgDebug("db is dropping, dbId:0x%"PRIx64, dbCache->dbId);
D
dapan1121 已提交
114
    CTG_UNLOCK(CTG_WRITE, &dbCache->vgCache.vgLock);
D
dapan1121 已提交
115 116 117 118 119 120
    CTG_ERR_RET(TSDB_CODE_CTG_DB_DROPPED);
  }

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
121 122
void ctgRUnlockVgInfo(SCtgDBCache *dbCache) {
  CTG_UNLOCK(CTG_READ, &dbCache->vgCache.vgLock);
D
dapan1121 已提交
123 124
}

D
dapan1121 已提交
125 126
void ctgWUnlockVgInfo(SCtgDBCache *dbCache) {
  CTG_UNLOCK(CTG_WRITE, &dbCache->vgCache.vgLock);
D
dapan1121 已提交
127 128
}

D
dapan1121 已提交
129 130
void ctgReleaseDBCache(SCatalog *pCtg, SCtgDBCache *dbCache) {
  CTG_UNLOCK(CTG_READ, &dbCache->dbLock);
D
dapan1121 已提交
131 132 133 134 135 136 137 138
}

int32_t ctgAcquireDBCacheImpl(SCatalog* pCtg, const char *dbFName, SCtgDBCache **pCache, bool acquire) {
  char *p = strchr(dbFName, '.');
  if (p && CTG_IS_SYS_DBNAME(p + 1)) {
    dbFName = p + 1;
  }

D
dapan1121 已提交
139
  SCtgDBCache *dbCache = (SCtgDBCache *)taosHashGet(pCtg->dbCache, dbFName, strlen(dbFName));
D
dapan1121 已提交
140 141 142 143 144 145
  if (NULL == dbCache) {
    *pCache = NULL;
    ctgDebug("db not in cache, dbFName:%s", dbFName);
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
146 147 148 149
  if (acquire) {
    CTG_LOCK(CTG_READ, &dbCache->dbLock);
  }

D
dapan1121 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
  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 已提交
173 174 175 176
void ctgReleaseVgInfoToCache(SCatalog* pCtg, SCtgDBCache *dbCache) {
  ctgRUnlockVgInfo(dbCache);
  ctgReleaseDBCache(pCtg, dbCache);
}
D
dapan1121 已提交
177

D
dapan1121 已提交
178 179 180 181 182
void ctgReleaseTbMetaToCache(SCatalog* pCtg, SCtgDBCache *dbCache, SCtgTbCache* pCache) {
  if (pCache) {
    CTG_UNLOCK(CTG_READ, &pCache->metaLock);
    taosHashRelease(dbCache->tbCache, pCache); 
  }
D
dapan1121 已提交
183

D
dapan1121 已提交
184 185
  if (dbCache) {
    ctgReleaseDBCache(pCtg, dbCache);
D
dapan1121 已提交
186
  }
D
dapan1121 已提交
187
}
D
dapan1121 已提交
188

D
dapan1121 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201
void ctgReleaseTbIndexToCache(SCatalog* pCtg, SCtgDBCache *dbCache, SCtgTbCache* pCache) {
  if (pCache) {
    CTG_UNLOCK(CTG_READ, &pCache->indexLock);
    taosHashRelease(dbCache->tbCache, pCache); 
  }

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

int32_t ctgAcquireVgInfoFromCache(SCatalog* pCtg, const char *dbFName, SCtgDBCache **pCache) {
  SCtgDBCache *dbCache = NULL;
D
dapan1121 已提交
202 203 204 205 206 207 208
  ctgAcquireDBCache(pCtg, dbFName, &dbCache);
  if (NULL == dbCache) {  
    ctgDebug("db %s not in cache", dbFName);
    goto _return;
  }

  bool inCache = false;
D
dapan1121 已提交
209
  ctgRLockVgInfo(pCtg, dbCache, &inCache);
D
dapan1121 已提交
210 211 212 213 214 215 216
  if (!inCache) {
    ctgDebug("vgInfo of db %s not in cache", dbFName);
    goto _return;
  }

  *pCache = dbCache;

D
dapan1121 已提交
217
  CTG_CACHE_STAT_INC(numOfVgHit, 1);
D
dapan1121 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230

  ctgDebug("Got db vgInfo from cache, dbFName:%s", dbFName);
  
  return TSDB_CODE_SUCCESS;

_return:

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

  *pCache = NULL;

D
dapan1121 已提交
231
  CTG_CACHE_STAT_INC(numOfVgMiss, 1);
D
dapan1121 已提交
232 233 234 235
  
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
236 237
int32_t ctgAcquireTbMetaFromCache(SCatalog* pCtg, char *dbFName, char* tbName, SCtgDBCache **pDb, SCtgTbCache** pTb) {
  SCtgDBCache *dbCache = NULL;
D
dapan1121 已提交
238
  SCtgTbCache* pCache = NULL;
D
dapan1121 已提交
239 240 241 242 243 244 245
  ctgAcquireDBCache(pCtg, dbFName, &dbCache);
  if (NULL == dbCache) {
    ctgDebug("db %s not in cache", dbFName);
    goto _return;
  }
  
  int32_t sz = 0;
D
dapan1121 已提交
246
  pCache = taosHashAcquire(dbCache->tbCache, tbName, strlen(tbName));
D
dapan1121 已提交
247 248 249
  if (NULL == pCache) {
    ctgDebug("tb %s not in cache, dbFName:%s", tbName, dbFName);
    goto _return;
D
dapan1121 已提交
250 251
  }

D
dapan1121 已提交
252 253 254 255 256 257 258 259 260 261 262
  CTG_LOCK(CTG_READ, &pCache->metaLock);
  if (NULL == pCache->pMeta) {
    ctgDebug("tb %s meta not in cache, dbFName:%s", tbName, dbFName);
    goto _return;
  }

  *pDb = dbCache;
  *pTb = pCache;

  ctgDebug("tb %s meta got in cache, dbFName:%s", tbName, dbFName);
  
D
dapan1121 已提交
263
  CTG_CACHE_STAT_INC(numOfMetaHit, 1);
D
dapan1121 已提交
264 265 266 267 268 269 270

  return TSDB_CODE_SUCCESS;

_return:

  ctgReleaseTbMetaToCache(pCtg, dbCache, pCache);

D
dapan1121 已提交
271
  CTG_CACHE_STAT_INC(numOfMetaMiss, 1);
D
dapan1121 已提交
272 273 274 275
  
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
276 277 278 279 280 281 282 283 284 285 286 287
int32_t ctgAcquireStbMetaFromCache(SCatalog* pCtg, char *dbFName, uint64_t suid, SCtgDBCache **pDb, SCtgTbCache** pTb) {
  SCtgDBCache* dbCache = NULL;
  SCtgTbCache* pCache = NULL;
  ctgAcquireDBCache(pCtg, dbFName, &dbCache);
  if (NULL == dbCache) {
    ctgDebug("db %s not in cache", dbFName);
    goto _return;
  }
  
  int32_t sz = 0;
  char* stName = taosHashAcquire(dbCache->stbCache, &suid, sizeof(suid));
  if (NULL == stName) {
D
dapan1121 已提交
288
    ctgDebug("stb 0x%" PRIx64 " not in cache, dbFName:%s", suid, dbFName);
D
dapan1121 已提交
289 290 291 292 293
    goto _return;
  }

  pCache = taosHashAcquire(dbCache->tbCache, stName, strlen(stName));
  if (NULL == pCache) {
D
dapan1121 已提交
294
    ctgDebug("stb 0x%" PRIx64 " name %s not in cache, dbFName:%s", suid, stName, dbFName);
D
dapan1121 已提交
295 296 297 298 299 300
    taosHashRelease(dbCache->stbCache, stName);
    goto _return;
  }

  CTG_LOCK(CTG_READ, &pCache->metaLock);
  if (NULL == pCache->pMeta) {
D
dapan1121 已提交
301
    ctgDebug("stb 0x%" PRIx64 " meta not in cache, dbFName:%s", suid, dbFName);
D
dapan1121 已提交
302 303 304 305 306 307
    goto _return;
  }

  *pDb = dbCache;
  *pTb = pCache;

D
dapan1121 已提交
308
  ctgDebug("stb 0x%" PRIx64 " meta got in cache, dbFName:%s", suid, dbFName);
D
dapan1121 已提交
309
  
D
dapan1121 已提交
310
  CTG_CACHE_STAT_INC(numOfMetaHit, 1);
D
dapan1121 已提交
311 312 313 314 315 316 317

  return TSDB_CODE_SUCCESS;

_return:

  ctgReleaseTbMetaToCache(pCtg, dbCache, pCache);

D
dapan1121 已提交
318
  CTG_CACHE_STAT_INC(numOfMetaMiss, 1);
D
dapan1121 已提交
319 320 321

  *pDb = NULL;
  *pTb = NULL;
D
dapan1121 已提交
322 323 324 325 326
  
  return TSDB_CODE_SUCCESS;
}


D
dapan1121 已提交
327
int32_t ctgAcquireTbIndexFromCache(SCatalog* pCtg, char *dbFName, char* tbName, SCtgDBCache **pDb, SCtgTbCache** pTb) {
D
dapan1121 已提交
328
  SCtgDBCache *dbCache = NULL;
D
dapan1121 已提交
329
  SCtgTbCache* pCache = NULL;
D
dapan1121 已提交
330 331
  ctgAcquireDBCache(pCtg, dbFName, &dbCache);
  if (NULL == dbCache) {
D
dapan1121 已提交
332 333 334 335 336
    ctgDebug("db %s not in cache", dbFName);
    goto _return;
  }
  
  int32_t sz = 0;
D
dapan1121 已提交
337
  pCache = taosHashAcquire(dbCache->tbCache, tbName, strlen(tbName));
D
dapan1121 已提交
338 339 340 341 342 343 344 345 346
  if (NULL == pCache) {
    ctgDebug("tb %s not in cache, dbFName:%s", tbName, dbFName);
    goto _return;
  }

  CTG_LOCK(CTG_READ, &pCache->indexLock);
  if (NULL == pCache->pIndex) {
    ctgDebug("tb %s index not in cache, dbFName:%s", tbName, dbFName);
    goto _return;
D
dapan1121 已提交
347 348
  }

D
dapan1121 已提交
349 350 351 352
  *pDb = dbCache;
  *pTb = pCache;

  ctgDebug("tb %s index got in cache, dbFName:%s", tbName, dbFName);
D
dapan1121 已提交
353
  
D
dapan1121 已提交
354
  CTG_CACHE_STAT_INC(numOfIndexHit, 1);
D
dapan1121 已提交
355 356 357 358 359 360 361

  return TSDB_CODE_SUCCESS;

_return:

  ctgReleaseTbIndexToCache(pCtg, dbCache, pCache);

D
dapan1121 已提交
362
  CTG_CACHE_STAT_INC(numOfIndexMiss, 1);
D
dapan1121 已提交
363 364 365 366 367 368 369
  
  return TSDB_CODE_SUCCESS;
}


int32_t ctgTbMetaExistInCache(SCatalog* pCtg, char *dbFName, char* tbName, int32_t *exist) {
  SCtgDBCache *dbCache = NULL;
D
dapan1121 已提交
370
  SCtgTbCache *tbCache = NULL;
D
dapan1121 已提交
371 372 373 374
  ctgAcquireTbMetaFromCache(pCtg, dbFName, tbName, &dbCache, &tbCache);
  if (NULL == tbCache) {
    ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
   
D
dapan1121 已提交
375 376 377 378 379
    *exist = 0;
    return TSDB_CODE_SUCCESS;
  }

  *exist = 1;
D
dapan1121 已提交
380
  ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
D
dapan1121 已提交
381 382 383 384 385 386 387
  
  return TSDB_CODE_SUCCESS;
}

int32_t ctgReadTbMetaFromCache(SCatalog* pCtg, SCtgTbMetaCtx* ctx, STableMeta** pTableMeta) {
  int32_t code = 0;
  SCtgDBCache *dbCache = NULL;
D
dapan1121 已提交
388
  SCtgTbCache *tbCache = NULL;  
D
dapan1121 已提交
389 390 391 392 393 394 395 396 397
  *pTableMeta = NULL;

  char dbFName[TSDB_DB_FNAME_LEN] = {0};
  if (CTG_FLAG_IS_SYS_DB(ctx->flag)) {
    strcpy(dbFName, ctx->pName->dbname);
  } else {
    tNameGetFullDbName(ctx->pName, dbFName);
  }

D
dapan1121 已提交
398 399 400
  ctgAcquireTbMetaFromCache(pCtg, dbFName, ctx->pName->tname, &dbCache, &tbCache);
  if (NULL == tbCache) {
    ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
D
dapan1121 已提交
401 402 403
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
404
  STableMeta* tbMeta = tbCache->pMeta;
D
dapan1121 已提交
405 406 407 408
  ctx->tbInfo.inCache = true;
  ctx->tbInfo.dbId = dbCache->dbId;
  ctx->tbInfo.suid = tbMeta->suid;
  ctx->tbInfo.tbType = tbMeta->tableType;
D
dapan1121 已提交
409
 
D
dapan1121 已提交
410
  if (tbMeta->tableType != TSDB_CHILD_TABLE) {
D
dapan1121 已提交
411 412 413 414 415 416 417 418 419
    int32_t metaSize = CTG_META_SIZE(tbMeta);
    *pTableMeta = taosMemoryCalloc(1, metaSize);
    if (NULL == *pTableMeta) {
      ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
      CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
    }

    memcpy(*pTableMeta, tbMeta, metaSize);
    
D
dapan1121 已提交
420 421
    ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
    ctgDebug("Got tb %s meta from cache, type:%d, dbFName:%s", ctx->pName->tname, tbMeta->tableType, dbFName);
D
dapan1121 已提交
422 423
    return TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
424 425

  // PROCESS FOR CHILD TABLE
D
dapan1121 已提交
426
  
D
dapan1121 已提交
427 428 429 430
  int32_t metaSize = sizeof(SCTableMeta);
  *pTableMeta = taosMemoryCalloc(1, metaSize);
  if (NULL == *pTableMeta) {
    CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
431 432
  }

D
dapan1121 已提交
433 434 435 436 437 438 439 440 441 442
  memcpy(*pTableMeta, tbMeta, metaSize);
  
  ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
  ctgDebug("Got ctb %s meta from cache, will continue to get its stb meta, type:%d, dbFName:%s", 
           ctx->pName->tname, ctx->tbInfo.tbType, dbFName);

  ctgAcquireStbMetaFromCache(pCtg, dbFName, ctx->tbInfo.suid, &dbCache, &tbCache);
  if (NULL == tbCache) {
    ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
    taosMemoryFreeClear(*pTableMeta);
D
dapan1121 已提交
443
    ctgDebug("stb 0x%" PRIx64 " meta not in cache", ctx->tbInfo.suid);
D
dapan1121 已提交
444 445 446 447 448 449
    return TSDB_CODE_SUCCESS;
  }
  
  STableMeta* stbMeta = tbCache->pMeta;
  if (stbMeta->suid != ctx->tbInfo.suid) {    
    ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
D
dapan1121 已提交
450
    ctgError("stb suid 0x%" PRIx64 " in stbCache mis-match, expected suid 0x%"PRIx64 , stbMeta->suid, ctx->tbInfo.suid);
D
dapan1121 已提交
451 452 453
    CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
  }

D
dapan1121 已提交
454
  metaSize = CTG_META_SIZE(stbMeta);
D
dapan1121 已提交
455 456
  *pTableMeta = taosMemoryRealloc(*pTableMeta, metaSize);
  if (NULL == *pTableMeta) {    
D
dapan1121 已提交
457
    ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
D
dapan1121 已提交
458
    CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
459 460
  }

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

D
dapan1121 已提交
463
  ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
D
dapan1121 已提交
464

D
dapan1121 已提交
465
  ctgDebug("Got tb %s meta from cache, dbFName:%s", ctx->pName->tname, dbFName);
D
dapan1121 已提交
466 467 468 469 470
  
  return TSDB_CODE_SUCCESS;

_return:

D
dapan1121 已提交
471
  ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
D
dapan1121 已提交
472 473 474 475 476
  taosMemoryFreeClear(*pTableMeta);
  
  CTG_RET(code);
}

D
dapan1121 已提交
477
int32_t ctgReadTbVerFromCache(SCatalog *pCtg, SName *pTableName, int32_t *sver, int32_t *tver, int32_t *tbType, uint64_t *suid,
D
dapan1121 已提交
478
                              char *stbName) {
D
dapan1121 已提交
479
  *sver = -1;
D
dapan1121 已提交
480
  *tver = -1;
D
dapan1121 已提交
481 482

  SCtgDBCache *dbCache = NULL;
D
dapan1121 已提交
483
  SCtgTbCache *tbCache = NULL;  
D
dapan1121 已提交
484
  char         dbFName[TSDB_DB_FNAME_LEN] = {0};
D
dapan1121 已提交
485 486
  tNameGetFullDbName(pTableName, dbFName);

D
dapan1121 已提交
487 488 489
  ctgAcquireTbMetaFromCache(pCtg, dbFName, pTableName->tname, &dbCache, &tbCache);
  if (NULL == tbCache) {
    ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
D
dapan1121 已提交
490 491 492
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
493 494 495
  STableMeta* tbMeta = tbCache->pMeta;
  *tbType = tbMeta->tableType;
  *suid = tbMeta->suid;
D
dapan1121 已提交
496

D
dapan1121 已提交
497
  if (*tbType != TSDB_CHILD_TABLE) {
D
dapan1121 已提交
498 499 500
    *sver = tbMeta->sversion;
    *tver = tbMeta->tversion;

D
dapan1121 已提交
501
    ctgDebug("Got tb %s ver from cache, dbFName:%s, tbType:%d, sver:%d, tver:%d, suid:0x%" PRIx64, 
D
dapan1121 已提交
502
             pTableName->tname, dbFName, *tbType, *sver, *tver, *suid);
D
dapan1121 已提交
503

D
dapan1121 已提交
504
    ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
D
dapan1121 已提交
505 506 507
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
508 509 510 511 512 513 514
  // PROCESS FOR CHILD TABLE
  
  ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
  ctgDebug("Got ctb %s ver from cache, will continue to get its stb ver, dbFName:%s", pTableName->tname, dbFName);
    
  ctgAcquireStbMetaFromCache(pCtg, dbFName, *suid, &dbCache, &tbCache);
  if (NULL == tbCache) {
D
dapan1121 已提交
515
    ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
D
dapan1121 已提交
516
    ctgDebug("stb 0x%" PRIx64 " meta not in cache", *suid);
D
dapan1121 已提交
517 518
    return TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
519 520 521
  
  STableMeta* stbMeta = tbCache->pMeta;
  if (stbMeta->suid != *suid) {
D
dapan1121 已提交
522
    ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
D
dapan1121 已提交
523
    ctgError("stb suid 0x%" PRIx64 " in stbCache mis-match, expected suid:0x%" PRIx64 , stbMeta->suid, *suid);
D
dapan1121 已提交
524 525 526
    CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
  }

D
dapan1121 已提交
527
  size_t nameLen = 0;
D
dapan1121 已提交
528
  char  *name = taosHashGetKey(tbCache, &nameLen);
D
dapan1121 已提交
529 530 531 532

  strncpy(stbName, name, nameLen);
  stbName[nameLen] = 0;

D
dapan1121 已提交
533 534
  *sver = stbMeta->sversion;
  *tver = stbMeta->tversion;
D
dapan1121 已提交
535

D
dapan1121 已提交
536
  ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
D
dapan1121 已提交
537

D
dapan1121 已提交
538
  ctgDebug("Got tb %s sver %d tver %d from cache, type:%d, dbFName:%s", pTableName->tname, *sver, *tver, *tbType, dbFName);
D
dapan1121 已提交
539 540 541 542

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
543

D
dapan1121 已提交
544
int32_t ctgReadTbTypeFromCache(SCatalog* pCtg, char* dbFName, char *tbName, int32_t *tbType) {
D
dapan1121 已提交
545 546
  SCtgDBCache *dbCache = NULL;
  SCtgTbCache *tbCache = NULL;  
D
dapan1121 已提交
547
  CTG_ERR_RET(ctgAcquireTbMetaFromCache(pCtg, dbFName, tbName, &dbCache, &tbCache));
D
dapan1121 已提交
548 549
  if (NULL == tbCache) {
    ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
D
dapan1121 已提交
550 551
    return TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
552 553 554 555

  *tbType = tbCache->pMeta->tableType;
  ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);

D
dapan1121 已提交
556
  ctgDebug("Got tb %s tbType %d from cache, dbFName:%s", tbName, *tbType, dbFName);  
D
dapan1121 已提交
557
  
D
dapan1121 已提交
558 559 560
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
561
int32_t ctgReadTbIndexFromCache(SCatalog* pCtg, SName* pTableName, SArray** pRes) {
D
dapan1121 已提交
562
  int32_t code = 0;
D
dapan1121 已提交
563
  SCtgDBCache *dbCache = NULL;
D
dapan1121 已提交
564 565 566
  SCtgTbCache *tbCache = NULL;  
  char         dbFName[TSDB_DB_FNAME_LEN] = {0};
  tNameGetFullDbName(pTableName, dbFName);
D
dapan1121 已提交
567

D
dapan1121 已提交
568
  *pRes = NULL;
D
dapan1121 已提交
569

D
dapan1121 已提交
570 571 572
  ctgAcquireTbIndexFromCache(pCtg, dbFName, pTableName->tname, &dbCache, &tbCache);
  if (NULL == tbCache) {
    ctgReleaseTbIndexToCache(pCtg, dbCache, tbCache);
D
dapan1121 已提交
573 574 575
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
576
  CTG_ERR_JRET(ctgCloneTableIndex(tbCache->pIndex->pIndex, pRes));
D
dapan1121 已提交
577

D
dapan1121 已提交
578
_return:
D
dapan1121 已提交
579

D
dapan1121 已提交
580
  ctgReleaseTbIndexToCache(pCtg, dbCache, tbCache);
D
dapan1121 已提交
581

D
dapan1121 已提交
582
  CTG_RET(code);
D
dapan1121 已提交
583 584
}

D
dapan1121 已提交
585
int32_t ctgChkAuthFromCache(SCatalog* pCtg, char* user, char* dbFName, AUTH_TYPE type, bool *inCache, bool *pass) {
D
dapan1121 已提交
586 587 588 589 590 591 592 593 594
  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);
D
dapan1121 已提交
595
  CTG_CACHE_STAT_INC(numOfUserHit, 1);
D
dapan1121 已提交
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
  
  if (pUser->superUser) {
    *pass = true;
    return TSDB_CODE_SUCCESS;
  }

  CTG_LOCK(CTG_READ, &pUser->lock);
  if (pUser->createdDbs && taosHashGet(pUser->createdDbs, dbFName, strlen(dbFName))) {
    *pass = true;
    CTG_UNLOCK(CTG_READ, &pUser->lock);
    return TSDB_CODE_SUCCESS;
  }
  
  if (pUser->readDbs && taosHashGet(pUser->readDbs, dbFName, strlen(dbFName)) && type == AUTH_TYPE_READ) {
    *pass = true;
  }
  
  if (pUser->writeDbs && taosHashGet(pUser->writeDbs, dbFName, strlen(dbFName)) && type == AUTH_TYPE_WRITE) {
    *pass = true;
  }

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

_return:

  *inCache = false;
D
dapan1121 已提交
624
  CTG_CACHE_STAT_INC(numOfUserMiss, 1);
D
dapan1121 已提交
625 626 627 628
  
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
629
void ctgDequeue(SCtgCacheOperation **op) {
D
dapan1121 已提交
630 631 632 633 634
  SCtgQNode *orig = gCtgMgmt.queue.head;
  
  SCtgQNode *node = gCtgMgmt.queue.head->next;
  gCtgMgmt.queue.head = gCtgMgmt.queue.head->next;

D
dapan1121 已提交
635
  CTG_QUEUE_DEC();
D
dapan1121 已提交
636 637 638
  
  taosMemoryFreeClear(orig);

D
dapan1121 已提交
639
  *op = node->op;
D
dapan1121 已提交
640 641 642
}


D
dapan1121 已提交
643
int32_t ctgEnqueue(SCatalog* pCtg, SCtgCacheOperation *operation) {
D
dapan1121 已提交
644 645 646
  SCtgQNode *node = taosMemoryCalloc(1, sizeof(SCtgQNode));
  if (NULL == node) {
    qError("calloc %d failed", (int32_t)sizeof(SCtgQNode));
D
dapan1121 已提交
647
    CTG_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
648 649
  }

D
dapan1121 已提交
650 651 652
  if (operation->syncOp) {
    tsem_init(&operation->rspSem, 0, 0);
  }
D
dapan1121 已提交
653
  
D
dapan1121 已提交
654
  node->op = operation;
D
dapan1121 已提交
655 656

  CTG_LOCK(CTG_WRITE, &gCtgMgmt.queue.qlock);
D
dapan1121 已提交
657
  if (gCtgMgmt.queue.stopQueue) {
658 659 660 661
    ctgFreeQNode(node);
    CTG_UNLOCK(CTG_WRITE, &gCtgMgmt.queue.qlock);
    CTG_RET(TSDB_CODE_CTG_EXIT);
  }
D
dapan1121 已提交
662
  gCtgMgmt.queue.stopQueue = operation->stopQueue;
D
dapan1121 已提交
663 664 665 666
  gCtgMgmt.queue.tail->next = node;
  gCtgMgmt.queue.tail = node;
  CTG_UNLOCK(CTG_WRITE, &gCtgMgmt.queue.qlock);

D
dapan1121 已提交
667
  CTG_QUEUE_INC();
D
dapan1121 已提交
668
  CTG_RT_STAT_INC(numOfOpEnqueue, 1);
D
dapan1121 已提交
669 670 671

  tsem_post(&gCtgMgmt.queue.reqSem);

D
dapan1121 已提交
672
  ctgDebug("action [%s] added into queue", gCtgCacheOperation[operation->opId].name);
D
dapan1121 已提交
673

D
dapan1121 已提交
674
  if (operation->syncOp) {
D
dapan1121 已提交
675
    tsem_wait(&operation->rspSem);
D
dapan1121 已提交
676
    taosMemoryFree(operation);
D
dapan1121 已提交
677 678 679 680 681 682
  }

  return TSDB_CODE_SUCCESS;
}


D
dapan1121 已提交
683
int32_t ctgDropDbCacheEnqueue(SCatalog* pCtg, const char *dbFName, int64_t dbId) {
D
dapan1121 已提交
684
  int32_t code = 0;
D
dapan1121 已提交
685 686 687
  SCtgCacheOperation *op = taosMemoryCalloc(1, sizeof(SCtgCacheOperation));
  op->opId = CTG_OP_DROP_DB_CACHE;
  
D
dapan1121 已提交
688
  SCtgDropDBMsg *msg = taosMemoryMalloc(sizeof(SCtgDropDBMsg));
D
dapan1121 已提交
689
  if (NULL == msg) {
D
dapan1121 已提交
690
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgDropDBMsg));
D
dapan1121 已提交
691
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
692 693 694 695 696 697 698 699 700 701 702
  }

  char *p = strchr(dbFName, '.');
  if (p && CTG_IS_SYS_DBNAME(p + 1)) {
    dbFName = p + 1;
  }

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

D
dapan1121 已提交
703
  op->data = msg;
D
dapan1121 已提交
704

D
dapan1121 已提交
705
  CTG_ERR_JRET(ctgEnqueue(pCtg, op));
D
dapan1121 已提交
706 707 708 709 710

  return TSDB_CODE_SUCCESS;

_return:

D
dapan1121 已提交
711
  taosMemoryFreeClear(op->data);
D
dapan1121 已提交
712 713 714
  CTG_RET(code);
}

D
dapan1121 已提交
715 716
int32_t ctgDropDbVgroupEnqueue(SCatalog* pCtg, const char *dbFName, bool syncOp) {
  int32_t code = 0;
D
dapan1121 已提交
717 718 719 720
  SCtgCacheOperation *op = taosMemoryCalloc(1, sizeof(SCtgCacheOperation));
  op->opId = CTG_OP_DROP_DB_VGROUP;
  op->syncOp = syncOp;
  
D
dapan1121 已提交
721 722 723
  SCtgDropDbVgroupMsg *msg = taosMemoryMalloc(sizeof(SCtgDropDbVgroupMsg));
  if (NULL == msg) {
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgDropDbVgroupMsg));
D
dapan1121 已提交
724
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
725 726 727 728 729 730 731 732 733 734
  }

  char *p = strchr(dbFName, '.');
  if (p && CTG_IS_SYS_DBNAME(p + 1)) {
    dbFName = p + 1;
  }

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

D
dapan1121 已提交
735
  op->data = msg;
D
dapan1121 已提交
736

D
dapan1121 已提交
737
  CTG_ERR_JRET(ctgEnqueue(pCtg, op));
D
dapan1121 已提交
738 739 740 741 742

  return TSDB_CODE_SUCCESS;

_return:

D
dapan1121 已提交
743
  taosMemoryFreeClear(op->data);
D
dapan1121 已提交
744 745 746 747
  CTG_RET(code);
}


D
dapan1121 已提交
748

D
dapan1121 已提交
749
int32_t ctgDropStbMetaEnqueue(SCatalog* pCtg, const char *dbFName, int64_t dbId, const char *stbName, uint64_t suid, bool syncOp) {
D
dapan1121 已提交
750
  int32_t code = 0;
D
dapan1121 已提交
751 752 753 754
  SCtgCacheOperation *op = taosMemoryCalloc(1, sizeof(SCtgCacheOperation));
  op->opId = CTG_OP_DROP_STB_META;
  op->syncOp = syncOp;
  
D
dapan1121 已提交
755
  SCtgDropStbMetaMsg *msg = taosMemoryMalloc(sizeof(SCtgDropStbMetaMsg));
D
dapan1121 已提交
756
  if (NULL == msg) {
D
dapan1121 已提交
757
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgDropStbMetaMsg));
D
dapan1121 已提交
758
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
759 760 761 762 763 764 765 766
  }

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

D
dapan1121 已提交
767
  op->data = msg;
D
dapan1121 已提交
768

D
dapan1121 已提交
769
  CTG_ERR_JRET(ctgEnqueue(pCtg, op));
D
dapan1121 已提交
770 771 772 773 774

  return TSDB_CODE_SUCCESS;

_return:

D
dapan1121 已提交
775
  taosMemoryFreeClear(op->data);
D
dapan1121 已提交
776 777 778 779 780
  CTG_RET(code);
}



D
dapan1121 已提交
781
int32_t ctgDropTbMetaEnqueue(SCatalog* pCtg, const char *dbFName, int64_t dbId, const char *tbName, bool syncOp) {
D
dapan1121 已提交
782
  int32_t code = 0;
D
dapan1121 已提交
783 784 785 786
  SCtgCacheOperation *op = taosMemoryCalloc(1, sizeof(SCtgCacheOperation));
  op->opId = CTG_OP_DROP_TB_META;
  op->syncOp = syncOp;
  
D
dapan1121 已提交
787
  SCtgDropTblMetaMsg *msg = taosMemoryMalloc(sizeof(SCtgDropTblMetaMsg));
D
dapan1121 已提交
788
  if (NULL == msg) {
D
dapan1121 已提交
789
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgDropTblMetaMsg));
D
dapan1121 已提交
790
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
791 792 793 794 795 796 797
  }

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

D
dapan1121 已提交
798
  op->data = msg;
D
dapan1121 已提交
799

D
dapan1121 已提交
800
  CTG_ERR_JRET(ctgEnqueue(pCtg, op));
D
dapan1121 已提交
801 802 803 804 805

  return TSDB_CODE_SUCCESS;

_return:

D
dapan1121 已提交
806
  taosMemoryFreeClear(op->data);
D
dapan1121 已提交
807 808 809
  CTG_RET(code);
}

D
dapan1121 已提交
810
int32_t ctgUpdateVgroupEnqueue(SCatalog* pCtg, const char *dbFName, int64_t dbId, SDBVgInfo* dbInfo, bool syncOp) {
D
dapan1121 已提交
811
  int32_t code = 0;
D
dapan1121 已提交
812 813 814 815
  SCtgCacheOperation *op = taosMemoryCalloc(1, sizeof(SCtgCacheOperation));
  op->opId = CTG_OP_UPDATE_VGROUP;
  op->syncOp = syncOp;
  
D
dapan1121 已提交
816 817 818 819
  SCtgUpdateVgMsg *msg = taosMemoryMalloc(sizeof(SCtgUpdateVgMsg));
  if (NULL == msg) {
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgUpdateVgMsg));
    ctgFreeVgInfo(dbInfo);
D
dapan1121 已提交
820
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
821 822 823 824 825 826 827 828 829 830 831 832
  }

  char *p = strchr(dbFName, '.');
  if (p && CTG_IS_SYS_DBNAME(p + 1)) {
    dbFName = p + 1;
  }

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

D
dapan1121 已提交
833
  op->data = msg;
D
dapan1121 已提交
834

D
dapan1121 已提交
835
  CTG_ERR_JRET(ctgEnqueue(pCtg, op));
D
dapan1121 已提交
836 837 838 839 840 841

  return TSDB_CODE_SUCCESS;

_return:

  ctgFreeVgInfo(dbInfo);
D
dapan1121 已提交
842
  taosMemoryFreeClear(op->data);
D
dapan1121 已提交
843 844 845
  CTG_RET(code);
}

D
dapan1121 已提交
846
int32_t ctgUpdateTbMetaEnqueue(SCatalog* pCtg, STableMetaOutput *output, bool syncOp) {
D
dapan1121 已提交
847
  int32_t code = 0;
D
dapan1121 已提交
848 849 850 851
  SCtgCacheOperation *op = taosMemoryCalloc(1, sizeof(SCtgCacheOperation));
  op->opId = CTG_OP_UPDATE_TB_META;
  op->syncOp = syncOp;
  
D
dapan1121 已提交
852
  SCtgUpdateTbMetaMsg *msg = taosMemoryMalloc(sizeof(SCtgUpdateTbMetaMsg));
D
dapan1121 已提交
853
  if (NULL == msg) {
D
dapan1121 已提交
854
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgUpdateTbMetaMsg));
D
dapan1121 已提交
855
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
856 857 858 859 860 861 862 863
  }

  char *p = strchr(output->dbFName, '.');
  if (p && CTG_IS_SYS_DBNAME(p + 1)) {
    memmove(output->dbFName, p + 1, strlen(p + 1));
  }

  msg->pCtg = pCtg;
D
dapan1121 已提交
864
  msg->pMeta = output;
D
dapan1121 已提交
865

D
dapan1121 已提交
866
  op->data = msg;
D
dapan1121 已提交
867

D
dapan1121 已提交
868
  CTG_ERR_JRET(ctgEnqueue(pCtg, op));
D
dapan1121 已提交
869 870 871 872 873 874 875 876 877 878 879 880

  return TSDB_CODE_SUCCESS;
  
_return:

  taosMemoryFreeClear(msg);
  
  CTG_RET(code);
}

int32_t ctgUpdateVgEpsetEnqueue(SCatalog* pCtg, char *dbFName, int32_t vgId, SEpSet* pEpSet) {
  int32_t code = 0;
D
dapan1121 已提交
881 882 883
  SCtgCacheOperation *op = taosMemoryCalloc(1, sizeof(SCtgCacheOperation));
  op->opId = CTG_OP_UPDATE_VG_EPSET;
  
D
dapan1121 已提交
884 885 886
  SCtgUpdateEpsetMsg *msg = taosMemoryMalloc(sizeof(SCtgUpdateEpsetMsg));
  if (NULL == msg) {
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgUpdateEpsetMsg));
D
dapan1121 已提交
887
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
888 889 890 891 892 893 894
  }

  msg->pCtg = pCtg;
  strcpy(msg->dbFName, dbFName);
  msg->vgId = vgId;
  msg->epSet = *pEpSet;

D
dapan1121 已提交
895
  op->data = msg;
D
dapan1121 已提交
896

D
dapan1121 已提交
897
  CTG_ERR_JRET(ctgEnqueue(pCtg, op));
D
dapan1121 已提交
898 899 900 901 902 903 904 905 906 907

  return TSDB_CODE_SUCCESS;
  
_return:

  taosMemoryFreeClear(msg);
  
  CTG_RET(code);
}

D
dapan1121 已提交
908 909


D
dapan1121 已提交
910
int32_t ctgUpdateUserEnqueue(SCatalog* pCtg, SGetUserAuthRsp *pAuth, bool syncOp) {
D
dapan1121 已提交
911
  int32_t code = 0;
D
dapan1121 已提交
912 913 914 915
  SCtgCacheOperation *op = taosMemoryCalloc(1, sizeof(SCtgCacheOperation));
  op->opId = CTG_OP_UPDATE_USER;
  op->syncOp = syncOp;
  
D
dapan1121 已提交
916 917 918
  SCtgUpdateUserMsg *msg = taosMemoryMalloc(sizeof(SCtgUpdateUserMsg));
  if (NULL == msg) {
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgUpdateUserMsg));
D
dapan1121 已提交
919
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
920 921 922 923 924
  }

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

D
dapan1121 已提交
925
  op->data = msg;
D
dapan1121 已提交
926

D
dapan1121 已提交
927
  CTG_ERR_JRET(ctgEnqueue(pCtg, op));
D
dapan1121 已提交
928
  
D
dapan1121 已提交
929 930 931 932 933 934 935 936 937 938
  return TSDB_CODE_SUCCESS;
  
_return:

  tFreeSGetUserAuthRsp(pAuth);
  taosMemoryFreeClear(msg);
  
  CTG_RET(code);
}

D
dapan1121 已提交
939
int32_t ctgUpdateTbIndexEnqueue(SCatalog* pCtg, STableIndex **pIndex, bool syncOp) {
D
dapan1121 已提交
940 941 942 943 944 945 946 947
  int32_t code = 0;
  SCtgCacheOperation *op = taosMemoryCalloc(1, sizeof(SCtgCacheOperation));
  op->opId = CTG_OP_UPDATE_TB_INDEX;
  op->syncOp = syncOp;
  
  SCtgUpdateTbIndexMsg *msg = taosMemoryMalloc(sizeof(SCtgUpdateTbIndexMsg));
  if (NULL == msg) {
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgUpdateTbIndexMsg));
D
dapan1121 已提交
948
    CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
949 950 951
  }

  msg->pCtg = pCtg;
D
dapan1121 已提交
952
  msg->pIndex = *pIndex;
D
dapan1121 已提交
953 954 955 956

  op->data = msg;

  CTG_ERR_JRET(ctgEnqueue(pCtg, op));
D
dapan1121 已提交
957 958

  *pIndex = NULL;
D
dapan1121 已提交
959 960 961 962
  return TSDB_CODE_SUCCESS;
  
_return:

D
dapan1121 已提交
963
  taosArrayDestroyEx((*pIndex)->pIndex, tFreeSTableIndexInfo);
D
dapan1121 已提交
964
  taosMemoryFreeClear(*pIndex);
D
dapan1121 已提交
965 966 967 968 969 970 971 972 973 974 975 976 977 978
  taosMemoryFreeClear(msg);
  
  CTG_RET(code);
}

int32_t ctgDropTbIndexEnqueue(SCatalog* pCtg, SName* pName, bool syncOp) {
  int32_t code = 0;
  SCtgCacheOperation *op = taosMemoryCalloc(1, sizeof(SCtgCacheOperation));
  op->opId = CTG_OP_DROP_TB_INDEX;
  op->syncOp = syncOp;
  
  SCtgDropTbIndexMsg *msg = taosMemoryMalloc(sizeof(SCtgDropTbIndexMsg));
  if (NULL == msg) {
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgDropTbIndexMsg));
D
dapan1121 已提交
979
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
  }

  msg->pCtg = pCtg;
  tNameGetFullDbName(pName, msg->dbFName);
  strcpy(msg->tbName, pName->tname);

  op->data = msg;

  CTG_ERR_JRET(ctgEnqueue(pCtg, op));
  
  return TSDB_CODE_SUCCESS;
  
_return:

  taosMemoryFreeClear(msg);
  
  CTG_RET(code);
}


D
dapan1121 已提交
1000
int32_t ctgClearCacheEnqueue(SCatalog* pCtg, bool freeCtg, bool stopQueue, bool syncOp) {
D
dapan1121 已提交
1001 1002 1003 1004
  int32_t code = 0;
  SCtgCacheOperation *op = taosMemoryCalloc(1, sizeof(SCtgCacheOperation));
  op->opId = CTG_OP_CLEAR_CACHE;
  op->syncOp = syncOp;
D
dapan1121 已提交
1005
  op->stopQueue = stopQueue;
D
dapan1121 已提交
1006 1007 1008 1009
  
  SCtgClearCacheMsg *msg = taosMemoryMalloc(sizeof(SCtgClearCacheMsg));
  if (NULL == msg) {
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgClearCacheMsg));
D
dapan1121 已提交
1010
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1011 1012 1013
  }

  msg->pCtg = pCtg;
D
dapan1121 已提交
1014
  msg->freeCtg = freeCtg;
D
dapan1121 已提交
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
  op->data = msg;

  CTG_ERR_JRET(ctgEnqueue(pCtg, op));
  
  return TSDB_CODE_SUCCESS;
  
_return:

  taosMemoryFreeClear(msg);
  
  CTG_RET(code);
}


D
dapan1121 已提交
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
int32_t ctgMetaRentInit(SCtgRentMgmt *mgmt, uint32_t rentSec, int8_t type) {
  mgmt->slotRIdx = 0;
  mgmt->slotNum = rentSec / CTG_RENT_SLOT_SECOND;
  mgmt->type = type;

  size_t msgSize = sizeof(SCtgRentSlot) * mgmt->slotNum;
  
  mgmt->slots = taosMemoryCalloc(1, msgSize);
  if (NULL == mgmt->slots) {
    qError("calloc %d failed", (int32_t)msgSize);
D
dapan1121 已提交
1039
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
  }

  qDebug("meta rent initialized, type:%d, slotNum:%d", type, mgmt->slotNum);
  
  return TSDB_CODE_SUCCESS;
}


int32_t ctgMetaRentAdd(SCtgRentMgmt *mgmt, void *meta, int64_t id, int32_t size) {
  int16_t widx = abs((int)(id % mgmt->slotNum));

  SCtgRentSlot *slot = &mgmt->slots[widx];
  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) {
D
dapan1121 已提交
1058
      qError("taosArrayInit %d failed, id:0x%"PRIx64", slot idx:%d, type:%d", CTG_DEFAULT_RENT_SLOT_SIZE, id, widx, mgmt->type);
D
dapan1121 已提交
1059
      CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1060 1061 1062 1063
    }
  }

  if (NULL == taosArrayPush(slot->meta, meta)) {
D
dapan1121 已提交
1064
    qError("taosArrayPush meta to rent failed, id:0x%"PRIx64", slot idx:%d, type:%d", id, widx, mgmt->type);
D
dapan1121 已提交
1065
    CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1066 1067 1068 1069
  }

  slot->needSort = true;

D
dapan1121 已提交
1070
  qDebug("add meta to rent, id:0x%"PRIx64", slot idx:%d, type:%d", id, widx, mgmt->type);
D
dapan1121 已提交
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085

_return:

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

int32_t ctgMetaRentUpdate(SCtgRentMgmt *mgmt, void *meta, int64_t id, int32_t size, __compar_fn_t sortCompare, __compar_fn_t searchCompare) {
  int16_t widx = abs((int)(id % mgmt->slotNum));

  SCtgRentSlot *slot = &mgmt->slots[widx];
  int32_t code = 0;

  CTG_LOCK(CTG_WRITE, &slot->lock);
  if (NULL == slot->meta) {
D
dapan1121 已提交
1086
    qError("empty meta slot, id:0x%"PRIx64", slot idx:%d, type:%d", id, widx, mgmt->type);
D
dapan1121 已提交
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
    CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
  }

  if (slot->needSort) {
    qDebug("meta slot before sorte, slot idx:%d, type:%d, size:%d", widx, mgmt->type, (int32_t)taosArrayGetSize(slot->meta));
    taosArraySort(slot->meta, sortCompare);
    slot->needSort = false;
    qDebug("meta slot sorted, slot idx:%d, type:%d, size:%d", widx, mgmt->type, (int32_t)taosArrayGetSize(slot->meta));
  }

  void *orig = taosArraySearch(slot->meta, &id, searchCompare, TD_EQ);
  if (NULL == orig) {
D
dapan1121 已提交
1099
    qDebug("meta not found in slot, id:0x%"PRIx64", slot idx:%d, type:%d, size:%d", id, widx, mgmt->type, (int32_t)taosArrayGetSize(slot->meta));
D
dapan1121 已提交
1100 1101 1102 1103 1104
    CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
  }

  memcpy(orig, meta, size);

D
dapan1121 已提交
1105
  qDebug("meta in rent updated, id:0x%"PRIx64", slot idx:%d, type:%d", id, widx, mgmt->type);
D
dapan1121 已提交
1106 1107 1108 1109 1110 1111

_return:

  CTG_UNLOCK(CTG_WRITE, &slot->lock);

  if (code) {
D
dapan1121 已提交
1112
    qDebug("meta in rent update failed, will try to add it, code:%x, id:0x%"PRIx64", slot idx:%d, type:%d", code, id, widx, mgmt->type);
D
dapan1121 已提交
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
    CTG_RET(ctgMetaRentAdd(mgmt, meta, id, size));
  }

  CTG_RET(code);
}

int32_t ctgMetaRentRemove(SCtgRentMgmt *mgmt, int64_t id, __compar_fn_t sortCompare, __compar_fn_t searchCompare) {
  int16_t widx = abs((int)(id % mgmt->slotNum));

  SCtgRentSlot *slot = &mgmt->slots[widx];
  int32_t code = 0;
  
  CTG_LOCK(CTG_WRITE, &slot->lock);
  if (NULL == slot->meta) {
D
dapan1121 已提交
1127
    qError("empty meta slot, id:0x%"PRIx64", slot idx:%d, type:%d", id, widx, mgmt->type);
D
dapan1121 已提交
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
    CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
  }

  if (slot->needSort) {
    taosArraySort(slot->meta, sortCompare);
    slot->needSort = false;
    qDebug("meta slot sorted, slot idx:%d, type:%d", widx, mgmt->type);
  }

  int32_t idx = taosArraySearchIdx(slot->meta, &id, searchCompare, TD_EQ);
  if (idx < 0) {
D
dapan1121 已提交
1139
    qError("meta not found in slot, id:0x%"PRIx64", slot idx:%d, type:%d", id, widx, mgmt->type);
D
dapan1121 已提交
1140 1141 1142 1143 1144
    CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
  }

  taosArrayRemove(slot->meta, idx);

D
dapan1121 已提交
1145
  qDebug("meta in rent removed, id:0x%"PRIx64", slot idx:%d, type:%d", id, widx, mgmt->type);
D
dapan1121 已提交
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182

_return:

  CTG_UNLOCK(CTG_WRITE, &slot->lock);

  CTG_RET(code);
}


int32_t ctgMetaRentGetImpl(SCtgRentMgmt *mgmt, void **res, uint32_t *num, int32_t size) {
  int16_t ridx = atomic_add_fetch_16(&mgmt->slotRIdx, 1);
  if (ridx >= mgmt->slotNum) {
    ridx %= mgmt->slotNum;
    atomic_store_16(&mgmt->slotRIdx, ridx);
  }

  SCtgRentSlot *slot = &mgmt->slots[ridx];
  int32_t code = 0;
  
  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 = taosMemoryMalloc(msize);
  if (NULL == *res) {
    qError("malloc %d failed", (int32_t)msize);
D
dapan1121 已提交
1183
    CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
  }

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

int32_t ctgMetaRentGet(SCtgRentMgmt *mgmt, void **res, uint32_t *num, int32_t size) {
  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));

  return TSDB_CODE_SUCCESS;
}

int32_t ctgAddNewDBCache(SCatalog *pCtg, const char *dbFName, uint64_t dbId) {
  int32_t code = 0;

  SCtgDBCache newDBCache = {0};
  newDBCache.dbId = dbId;

D
dapan1121 已提交
1230 1231
  newDBCache.tbCache = taosHashInit(gCtgMgmt.cfg.maxTblCacheNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
  if (NULL == newDBCache.tbCache) {
D
dapan1121 已提交
1232
    ctgError("taosHashInit %d metaCache failed", gCtgMgmt.cfg.maxTblCacheNum);
D
dapan1121 已提交
1233
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1234 1235
  }

D
dapan1121 已提交
1236 1237
  newDBCache.stbCache = taosHashInit(gCtgMgmt.cfg.maxTblCacheNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), true, HASH_ENTRY_LOCK);
  if (NULL == newDBCache.stbCache) {
D
dapan1121 已提交
1238
    ctgError("taosHashInit %d stbCache failed", gCtgMgmt.cfg.maxTblCacheNum);
D
dapan1121 已提交
1239
    CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
  }

  code = taosHashPut(pCtg->dbCache, dbFName, strlen(dbFName), &newDBCache, sizeof(SCtgDBCache));
  if (code) {
    if (HASH_NODE_EXIST(code)) {
      ctgDebug("db already in cache, dbFName:%s", dbFName);
      goto _return;
    }
    
    ctgError("taosHashPut db to cache failed, dbFName:%s", dbFName);
D
dapan1121 已提交
1250
    CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1251 1252
  }

D
dapan1121 已提交
1253
  CTG_CACHE_STAT_INC(numOfDb, 1);
D
dapan1121 已提交
1254 1255 1256 1257
 
  SDbVgVersion vgVersion = {.dbId = newDBCache.dbId, .vgVersion = -1};
  strncpy(vgVersion.dbFName, dbFName, sizeof(vgVersion.dbFName));

D
dapan1121 已提交
1258
  ctgDebug("db added to cache, dbFName:%s, dbId:0x%"PRIx64, dbFName, dbId);
D
dapan1121 已提交
1259 1260 1261

  CTG_ERR_RET(ctgMetaRentAdd(&pCtg->dbRent, &vgVersion, dbId, sizeof(SDbVgVersion)));

D
dapan1121 已提交
1262
  ctgDebug("db added to rent, dbFName:%s, vgVersion:%d, dbId:0x%"PRIx64, dbFName, vgVersion.vgVersion, dbId);
D
dapan1121 已提交
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273

  return TSDB_CODE_SUCCESS;

_return:

  ctgFreeDbCache(&newDBCache);

  CTG_RET(code);
}


D
dapan1121 已提交
1274 1275 1276 1277 1278 1279 1280 1281 1282
void ctgRemoveStbRent(SCatalog* pCtg, SCtgDBCache *dbCache) {
  if (NULL == dbCache->stbCache) {
    return;
  }
  
  void *pIter = taosHashIterate(dbCache->stbCache, NULL);
  while (pIter) {
    uint64_t *suid = NULL;
    suid = taosHashGetKey(pIter, NULL);
D
dapan1121 已提交
1283

D
dapan1121 已提交
1284
    if (TSDB_CODE_SUCCESS == ctgMetaRentRemove(&pCtg->stbRent, *suid, ctgStbVersionSortCompare, ctgStbVersionSearchCompare)) {
D
dapan1121 已提交
1285
      ctgDebug("stb removed from rent, suid:0x%"PRIx64, *suid);
D
dapan1121 已提交
1286
    }
D
dapan1121 已提交
1287 1288
        
    pIter = taosHashIterate(dbCache->stbCache, pIter);
D
dapan1121 已提交
1289 1290 1291 1292 1293 1294 1295
  }
}


int32_t ctgRemoveDBFromCache(SCatalog* pCtg, SCtgDBCache *dbCache, const char* dbFName) {
  uint64_t dbId = dbCache->dbId;
  
D
dapan1121 已提交
1296
  ctgInfo("start to remove db from cache, dbFName:%s, dbId:0x%"PRIx64, dbFName, dbCache->dbId);
D
dapan1121 已提交
1297

D
dapan1121 已提交
1298
  CTG_LOCK(CTG_WRITE, &dbCache->dbLock);
D
dapan1121 已提交
1299

D
dapan1121 已提交
1300
  atomic_store_8(&dbCache->deleted, 1);
D
dapan1121 已提交
1301
  ctgRemoveStbRent(pCtg, dbCache);
D
dapan1121 已提交
1302 1303
  ctgFreeDbCache(dbCache);

D
dapan1121 已提交
1304 1305 1306
  CTG_UNLOCK(CTG_WRITE, &dbCache->dbLock);

  CTG_ERR_RET(ctgMetaRentRemove(&pCtg->dbRent, dbId, ctgDbVgVersionSortCompare, ctgDbVgVersionSearchCompare));
D
dapan1121 已提交
1307
  ctgDebug("db removed from rent, dbFName:%s, dbId:0x%"PRIx64, dbFName, dbId);
D
dapan1121 已提交
1308 1309 1310 1311 1312 1313

  if (taosHashRemove(pCtg->dbCache, dbFName, strlen(dbFName))) {
    ctgInfo("taosHashRemove from dbCache failed, may be removed, dbFName:%s", dbFName);
    CTG_ERR_RET(TSDB_CODE_CTG_DB_DROPPED);
  }

D
dapan1121 已提交
1314
  CTG_CACHE_STAT_DEC(numOfDb, 1);
D
dapan1121 已提交
1315
  ctgInfo("db removed from cache, dbFName:%s, dbId:0x%"PRIx64, dbFName, dbId);
D
dapan1121 已提交
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
  
  return TSDB_CODE_SUCCESS;
}


int32_t ctgGetAddDBCache(SCatalog* pCtg, const char *dbFName, uint64_t dbId, SCtgDBCache **pCache) {
  int32_t code = 0;
  SCtgDBCache *dbCache = NULL;
  ctgGetDBCache(pCtg, dbFName, &dbCache);
  
  if (dbCache) {
  // TODO OPEN IT
#if 0    
    if (dbCache->dbId == dbId) {
      *pCache = dbCache;
      return TSDB_CODE_SUCCESS;
    }
#else
    if (0 == dbId) {
      *pCache = dbCache;
      return TSDB_CODE_SUCCESS;
    }

    if (dbId && (dbCache->dbId == 0)) {
      dbCache->dbId = dbId;
      *pCache = dbCache;
      return TSDB_CODE_SUCCESS;
    }
    
    if (dbCache->dbId == dbId) {
      *pCache = dbCache;
      return TSDB_CODE_SUCCESS;
    }
#endif
    CTG_ERR_RET(ctgRemoveDBFromCache(pCtg, dbCache, dbFName));
  }
  
  CTG_ERR_RET(ctgAddNewDBCache(pCtg, dbFName, dbId));

  ctgGetDBCache(pCtg, dbFName, &dbCache);

  *pCache = dbCache;

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
int32_t ctgUpdateRentStbVersion(SCatalog *pCtg, char* dbFName, char* tbName, uint64_t dbId, uint64_t suid, SCtgTbCache* pCache) {
  SSTableVersion metaRent = {.dbId = dbId, .suid = suid};
  if (pCache->pMeta) {
    metaRent.sversion = pCache->pMeta->sversion;
    metaRent.tversion = pCache->pMeta->tversion;
  }

  if (pCache->pIndex) {
    metaRent.smaVer = pCache->pIndex->version;
  }
  
  strcpy(metaRent.dbFName, dbFName);
  strcpy(metaRent.stbName, tbName);
  
  CTG_ERR_RET(ctgMetaRentUpdate(&pCtg->stbRent, &metaRent, metaRent.suid, sizeof(SSTableVersion), ctgStbVersionSortCompare, ctgStbVersionSearchCompare));

D
dapan1121 已提交
1378
  ctgDebug("db %s,0x%" PRIx64 " stb %s,0x%" PRIx64 " sver %d tver %d smaVer %d updated to stbRent", 
D
dapan1121 已提交
1379
           dbFName, dbId, tbName, suid, metaRent.sversion, metaRent.tversion, metaRent.smaVer);
D
dapan1121 已提交
1380 1381

  return TSDB_CODE_SUCCESS;         
D
dapan1121 已提交
1382 1383 1384
}


D
dapan1121 已提交
1385
int32_t ctgWriteTbMetaToCache(SCatalog *pCtg, SCtgDBCache *dbCache, char *dbFName, uint64_t dbId, char *tbName, STableMeta *meta, int32_t metaSize) {
D
dapan1121 已提交
1386 1387
  if (NULL == dbCache->tbCache || NULL == dbCache->stbCache) {
    taosMemoryFree(meta);
D
dapan1121 已提交
1388
    ctgError("db is dropping, dbId:0x%"PRIx64, dbCache->dbId);
D
dapan1121 已提交
1389 1390 1391
    CTG_ERR_RET(TSDB_CODE_CTG_DB_DROPPED);
  }

D
dapan1121 已提交
1392 1393 1394
  bool isStb = meta->tableType == TSDB_SUPER_TABLE;
  SCtgTbCache* pCache = taosHashGet(dbCache->tbCache, tbName, strlen(tbName));
  STableMeta *orig = (pCache ? pCache->pMeta : NULL);
D
dapan1121 已提交
1395 1396
  int8_t origType = 0;
  uint64_t origSuid = 0;
D
dapan1121 已提交
1397
  
D
dapan1121 已提交
1398 1399 1400
  if (orig) {
    origType = orig->tableType;

D
dapan1121 已提交
1401
    if (origType == meta->tableType && orig->uid == meta->uid && (origType == TSDB_CHILD_TABLE || (orig->sversion >= meta->sversion && orig->tversion >= meta->tversion))) {
D
dapan1121 已提交
1402 1403
      taosMemoryFree(meta);
      ctgDebug("ignore table %s meta update", tbName);
D
dapan1121 已提交
1404 1405 1406 1407
      return TSDB_CODE_SUCCESS;
    }
    
    if (origType == TSDB_SUPER_TABLE) {
D
dapan1121 已提交
1408
      if (taosHashRemove(dbCache->stbCache, &orig->suid, sizeof(orig->suid))) {
D
dapan1121 已提交
1409
        ctgError("stb not exist in stbCache, dbFName:%s, stb:%s, suid:0x%"PRIx64, dbFName, tbName, orig->suid);
D
dapan1121 已提交
1410
      } else {
D
dapan1121 已提交
1411
        CTG_CACHE_STAT_DEC(numOfStb, 1);
D
dapan1121 已提交
1412
        ctgDebug("stb removed from stbCache, dbFName:%s, stb:%s, suid:0x%"PRIx64, dbFName, tbName, orig->suid);
D
dapan1121 已提交
1413 1414 1415 1416 1417 1418
      }
      
      origSuid = orig->suid;
    }
  }

D
dapan1121 已提交
1419 1420 1421 1422 1423 1424
  if (NULL == pCache) {
    SCtgTbCache cache = {0};
    cache.pMeta = meta;
    if (taosHashPut(dbCache->tbCache, tbName, strlen(tbName), &cache, sizeof(SCtgTbCache)) != 0) {
      taosMemoryFree(meta);
      ctgError("taosHashPut new tbCache failed, dbFName:%s, tbName:%s, tbType:%d", dbFName, tbName, meta->tableType);
D
dapan1121 已提交
1425
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1426 1427
    }
    
D
dapan1121 已提交
1428 1429 1430 1431
    pCache = taosHashGet(dbCache->tbCache, tbName, strlen(tbName));
  } else {
    taosMemoryFree(pCache->pMeta);
    pCache->pMeta = meta;
D
dapan1121 已提交
1432 1433 1434
  }

  if (NULL == orig) {
D
dapan1121 已提交
1435
    CTG_CACHE_STAT_INC(numOfTbl, 1);
D
dapan1121 已提交
1436 1437 1438 1439 1440 1441 1442 1443 1444
  }

  ctgDebug("tbmeta updated to cache, dbFName:%s, tbName:%s, tbType:%d", dbFName, tbName, meta->tableType);
  ctgdShowTableMeta(pCtg, tbName, meta);

  if (!isStb) {
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
1445
  if (origSuid != meta->suid && taosHashPut(dbCache->stbCache, &meta->suid, sizeof(meta->suid), tbName, strlen(tbName) + 1) != 0) {
D
dapan1121 已提交
1446
    ctgError("taosHashPut to stable cache failed, suid:0x%"PRIx64, meta->suid);
D
dapan1121 已提交
1447
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1448 1449
  }

D
dapan1121 已提交
1450
  CTG_CACHE_STAT_INC(numOfStb, 1);
D
dapan1121 已提交
1451

D
dapan1121 已提交
1452
  ctgDebug("stb 0x%" PRIx64 " updated to cache, dbFName:%s, tbName:%s, tbType:%d", meta->suid, dbFName, tbName, meta->tableType);
D
dapan1121 已提交
1453

D
dapan1121 已提交
1454
  CTG_ERR_RET(ctgUpdateRentStbVersion(pCtg, dbFName, tbName, dbId, meta->suid, pCache));
D
dapan1121 已提交
1455 1456 1457 1458
  
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1459
int32_t ctgWriteTbIndexToCache(SCatalog *pCtg, SCtgDBCache *dbCache, char* dbFName, char *tbName, STableIndex **index) {
D
dapan1121 已提交
1460
  if (NULL == dbCache->tbCache) {
D
dapan1121 已提交
1461
    ctgFreeSTableIndex(*index);
D
dapan1121 已提交
1462
    taosMemoryFreeClear(*index);
D
dapan1121 已提交
1463
    ctgError("db is dropping, dbId:0x%"PRIx64, dbCache->dbId);
D
dapan1121 已提交
1464 1465 1466 1467
    CTG_ERR_RET(TSDB_CODE_CTG_DB_DROPPED);
  }

  STableIndex* pIndex = *index;
D
dapan1121 已提交
1468
  uint64_t suid = pIndex->suid;
D
dapan1121 已提交
1469 1470 1471 1472 1473
  SCtgTbCache* pCache = taosHashGet(dbCache->tbCache, tbName, strlen(tbName));
  if (NULL == pCache) {
    SCtgTbCache cache = {0};
    cache.pIndex = pIndex;
    
D
dapan1121 已提交
1474 1475
    if (taosHashPut(dbCache->tbCache, tbName, strlen(tbName), &cache, sizeof(cache)) != 0) {
      ctgFreeSTableIndex(*index);
D
dapan1121 已提交
1476 1477
      taosMemoryFreeClear(*index);
      ctgError("taosHashPut new tbCache failed, tbName:%s", tbName);
D
dapan1121 已提交
1478
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1479 1480 1481
    }

    *index = NULL;
D
dapan1121 已提交
1482
    ctgDebug("table %s index updated to cache, ver:%d, num:%d", tbName, pIndex->version, (int32_t)taosArrayGetSize(pIndex->pIndex));
D
dapan1121 已提交
1483

D
dapan1121 已提交
1484
    if (suid) {
D
dapan1121 已提交
1485
      CTG_ERR_RET(ctgUpdateRentStbVersion(pCtg, dbFName, tbName, dbCache->dbId, pIndex->suid, &cache));
D
dapan1121 已提交
1486 1487
    }
    
D
dapan1121 已提交
1488 1489 1490 1491
    return TSDB_CODE_SUCCESS;
  }

  if (pCache->pIndex) {
D
dapan1121 已提交
1492 1493 1494
    if (0 == suid) {
      suid = pCache->pIndex->suid;
    }
D
dapan1121 已提交
1495 1496 1497 1498 1499 1500 1501
    taosArrayDestroyEx(pCache->pIndex->pIndex, tFreeSTableIndexInfo);
    taosMemoryFreeClear(pCache->pIndex);
  }

  pCache->pIndex = pIndex;
  *index = NULL;

D
dapan1121 已提交
1502
  ctgDebug("table %s index updated to cache, ver:%d, num:%d", tbName, pIndex->version, (int32_t)taosArrayGetSize(pIndex->pIndex));
D
dapan1121 已提交
1503

D
dapan1121 已提交
1504 1505 1506
  if (suid) {
    CTG_ERR_RET(ctgUpdateRentStbVersion(pCtg, dbFName, tbName, dbCache->dbId, suid, pCache));
  }
D
dapan1121 已提交
1507 1508 1509 1510
  
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1511 1512 1513 1514 1515
int32_t ctgUpdateTbMetaToCache(SCatalog* pCtg, STableMetaOutput* pOut, bool syncReq) {
  STableMetaOutput* pOutput = NULL;
  int32_t code = 0;
  
  CTG_ERR_RET(ctgCloneMetaOutput(pOut, &pOutput));
D
dapan1121 已提交
1516
  CTG_ERR_JRET(ctgUpdateTbMetaEnqueue(pCtg, pOutput, syncReq));
D
dapan1121 已提交
1517 1518 1519 1520 1521 1522 1523 1524 1525

  return TSDB_CODE_SUCCESS;
  
_return:

  ctgFreeSTableMetaOutput(pOutput);
  CTG_RET(code);
}

D
dapan1121 已提交
1526
void ctgClearAllInstance(void) {
1527 1528 1529 1530 1531 1532 1533
  SCatalog* pCtg = NULL;

  void* pIter = taosHashIterate(gCtgMgmt.pCluster, NULL);
  while (pIter) {
    pCtg = *(SCatalog**)pIter;

    if (pCtg) {
D
dapan1121 已提交
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551
      ctgClearHandle(pCtg);
    }

    pIter = taosHashIterate(gCtgMgmt.pCluster, pIter);
  }

  taosHashClear(gCtgMgmt.pCluster);
}

void ctgFreeAllInstance(void) {
  SCatalog* pCtg = NULL;

  void* pIter = taosHashIterate(gCtgMgmt.pCluster, NULL);
  while (pIter) {
    pCtg = *(SCatalog**)pIter;

    if (pCtg) {
      ctgFreeHandle(pCtg);
1552 1553 1554 1555 1556 1557 1558 1559 1560
    }

    pIter = taosHashIterate(gCtgMgmt.pCluster, pIter);
  }

  taosHashClear(gCtgMgmt.pCluster);
}


D
dapan1121 已提交
1561
int32_t ctgOpUpdateVgroup(SCtgCacheOperation *operation) {
D
dapan1121 已提交
1562
  int32_t code = 0;
D
dapan1121 已提交
1563
  SCtgUpdateVgMsg *msg = operation->data;
D
dapan1121 已提交
1564 1565
  SDBVgInfo* dbInfo = msg->dbInfo;
  char* dbFName = msg->dbFName;
D
dapan1121 已提交
1566
  SCatalog* pCtg = msg->pCtg;
D
dapan1121 已提交
1567
  
D
dapan1121 已提交
1568 1569 1570 1571 1572 1573 1574
  if (NULL == dbInfo->vgHash) {
    return TSDB_CODE_SUCCESS;
  }
  
  if (dbInfo->vgVersion < 0 || taosHashGetSize(dbInfo->vgHash) <= 0) {
    ctgError("invalid db vgInfo, dbFName:%s, vgHash:%p, vgVersion:%d, vgHashSize:%d", 
             dbFName, dbInfo->vgHash, dbInfo->vgVersion, taosHashGetSize(dbInfo->vgHash));
D
dapan1121 已提交
1575
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1576 1577 1578 1579 1580 1581 1582 1583
  }

  bool newAdded = false;
  SDbVgVersion vgVersion = {.dbId = msg->dbId, .vgVersion = dbInfo->vgVersion, .numOfTable = dbInfo->numOfTable};

  SCtgDBCache *dbCache = NULL;
  CTG_ERR_RET(ctgGetAddDBCache(msg->pCtg, dbFName, msg->dbId, &dbCache));
  if (NULL == dbCache) {
D
dapan1121 已提交
1584
    ctgInfo("conflict db update, ignore this update, dbFName:%s, dbId:0x%"PRIx64, dbFName, msg->dbId);
D
dapan1121 已提交
1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
    CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
  }

  SCtgVgCache *vgCache = &dbCache->vgCache;
  CTG_ERR_RET(ctgWLockVgInfo(msg->pCtg, dbCache));
  
  if (vgCache->vgInfo) {
    SDBVgInfo *vgInfo = vgCache->vgInfo;
    
    if (dbInfo->vgVersion < vgInfo->vgVersion) {
      ctgDebug("db vgVer is old, dbFName:%s, vgVer:%d, curVer:%d", dbFName, dbInfo->vgVersion, vgInfo->vgVersion);
      ctgWUnlockVgInfo(dbCache);
      
      return TSDB_CODE_SUCCESS;
    }

    if (dbInfo->vgVersion == vgInfo->vgVersion && dbInfo->numOfTable == vgInfo->numOfTable) {
      ctgDebug("no new db vgVer or numOfTable, dbFName:%s, vgVer:%d, numOfTable:%d", dbFName, dbInfo->vgVersion, dbInfo->numOfTable);
      ctgWUnlockVgInfo(dbCache);
      
      return TSDB_CODE_SUCCESS;
    }

    ctgFreeVgInfo(vgInfo);
  }

  vgCache->vgInfo = dbInfo;
  msg->dbInfo = NULL;

D
dapan1121 已提交
1614
  ctgDebug("db vgInfo updated, dbFName:%s, vgVer:%d, dbId:0x%"PRIx64, dbFName, vgVersion.vgVersion, vgVersion.dbId);
D
dapan1121 已提交
1615 1616 1617 1618 1619 1620 1621

  ctgWUnlockVgInfo(dbCache);

  dbCache = NULL;

  strncpy(vgVersion.dbFName, dbFName, sizeof(vgVersion.dbFName));
  CTG_ERR_RET(ctgMetaRentUpdate(&msg->pCtg->dbRent, &vgVersion, vgVersion.dbId, sizeof(SDbVgVersion), ctgDbVgVersionSortCompare, ctgDbVgVersionSearchCompare));
D
dapan1121 已提交
1622 1623 1624 1625 1626 1627 1628 1629 1630

_return:

  ctgFreeVgInfo(msg->dbInfo);
  taosMemoryFreeClear(msg);
  
  CTG_RET(code);
}

D
dapan1121 已提交
1631
int32_t ctgOpDropDbCache(SCtgCacheOperation *operation) {
D
dapan1121 已提交
1632
  int32_t code = 0;
D
dapan1121 已提交
1633
  SCtgDropDBMsg *msg = operation->data;
D
dapan1121 已提交
1634 1635 1636 1637 1638 1639 1640 1641 1642
  SCatalog* pCtg = msg->pCtg;

  SCtgDBCache *dbCache = NULL;
  ctgGetDBCache(msg->pCtg, msg->dbFName, &dbCache);
  if (NULL == dbCache) {
    goto _return;
  }
  
  if (dbCache->dbId != msg->dbId) {
D
dapan1121 已提交
1643
    ctgInfo("dbId already updated, dbFName:%s, dbId:0x%"PRIx64 ", targetId:0x%"PRIx64, msg->dbFName, dbCache->dbId, msg->dbId);
D
dapan1121 已提交
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655
    goto _return;
  }
  
  CTG_ERR_JRET(ctgRemoveDBFromCache(pCtg, dbCache, msg->dbFName));

_return:

  taosMemoryFreeClear(msg);
  
  CTG_RET(code);
}

D
dapan1121 已提交
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666
int32_t ctgOpDropDbVgroup(SCtgCacheOperation *operation) {
  int32_t code = 0;
  SCtgDropDbVgroupMsg *msg = operation->data;
  SCatalog* pCtg = msg->pCtg;

  SCtgDBCache *dbCache = NULL;
  ctgGetDBCache(msg->pCtg, msg->dbFName, &dbCache);
  if (NULL == dbCache) {
    goto _return;
  }
  
D
dapan1121 已提交
1667
  CTG_ERR_RET(ctgWLockVgInfo(pCtg, dbCache));
D
dapan1121 已提交
1668
  
D
dapan1121 已提交
1669 1670
  ctgFreeVgInfo(dbCache->vgCache.vgInfo);
  dbCache->vgCache.vgInfo = NULL;
D
dapan1121 已提交
1671 1672 1673

  ctgDebug("db vgInfo removed, dbFName:%s", msg->dbFName);

D
dapan1121 已提交
1674
  ctgWUnlockVgInfo(dbCache);
D
dapan1121 已提交
1675 1676 1677 1678 1679 1680 1681 1682

_return:

  taosMemoryFreeClear(msg);
  
  CTG_RET(code);
}

D
dapan1121 已提交
1683

D
dapan1121 已提交
1684
int32_t ctgOpUpdateTbMeta(SCtgCacheOperation *operation) {
D
dapan1121 已提交
1685
  int32_t code = 0;
D
dapan1121 已提交
1686
  SCtgUpdateTbMetaMsg *msg = operation->data;
D
dapan1121 已提交
1687
  SCatalog* pCtg = msg->pCtg;
D
dapan1121 已提交
1688
  STableMetaOutput* pMeta = msg->pMeta;
D
dapan1121 已提交
1689 1690
  SCtgDBCache *dbCache = NULL;

D
dapan1121 已提交
1691 1692
  if ((!CTG_IS_META_CTABLE(pMeta->metaType)) && NULL == pMeta->tbMeta) {
    ctgError("no valid tbmeta got from meta rsp, dbFName:%s, tbName:%s", pMeta->dbFName, pMeta->tbName);
D
dapan1121 已提交
1693 1694 1695
    CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
  }

D
dapan1121 已提交
1696 1697
  if (CTG_IS_META_BOTH(pMeta->metaType) && TSDB_SUPER_TABLE != pMeta->tbMeta->tableType) {
    ctgError("table type error, expected:%d, actual:%d", TSDB_SUPER_TABLE, pMeta->tbMeta->tableType);
D
dapan1121 已提交
1698 1699 1700
    CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
  }    
  
D
dapan1121 已提交
1701
  CTG_ERR_JRET(ctgGetAddDBCache(pCtg, pMeta->dbFName, pMeta->dbId, &dbCache));
D
dapan1121 已提交
1702
  if (NULL == dbCache) {
D
dapan1121 已提交
1703
    ctgInfo("conflict db update, ignore this update, dbFName:%s, dbId:0x%" PRIx64, pMeta->dbFName, pMeta->dbId);
D
dapan1121 已提交
1704 1705 1706
    CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
  }

D
dapan1121 已提交
1707 1708 1709 1710
  if (CTG_IS_META_TABLE(pMeta->metaType) || CTG_IS_META_BOTH(pMeta->metaType)) {
    int32_t metaSize = CTG_META_SIZE(pMeta->tbMeta);
    CTG_ERR_JRET(ctgWriteTbMetaToCache(pCtg, dbCache, pMeta->dbFName, pMeta->dbId, pMeta->tbName, pMeta->tbMeta, metaSize));
    pMeta->tbMeta = NULL;
D
dapan1121 已提交
1711 1712
  }

D
dapan1121 已提交
1713 1714 1715 1716 1717 1718 1719
  if (CTG_IS_META_CTABLE(pMeta->metaType) || CTG_IS_META_BOTH(pMeta->metaType)) {
    SCTableMeta* ctbMeta = taosMemoryMalloc(sizeof(SCTableMeta));
    if (NULL == ctbMeta) {
      CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
    }
    memcpy(ctbMeta, &pMeta->ctbMeta, sizeof(SCTableMeta));
    CTG_ERR_JRET(ctgWriteTbMetaToCache(pCtg, dbCache, pMeta->dbFName, pMeta->dbId, pMeta->ctbName, (STableMeta *)ctbMeta, sizeof(SCTableMeta)));
D
dapan1121 已提交
1720 1721 1722 1723
  }

_return:

D
dapan1121 已提交
1724 1725 1726
  if (pMeta) {
    taosMemoryFreeClear(pMeta->tbMeta);
    taosMemoryFreeClear(pMeta);
D
dapan1121 已提交
1727 1728 1729 1730 1731 1732 1733 1734
  }
  
  taosMemoryFreeClear(msg);
  
  CTG_RET(code);
}


D
dapan1121 已提交
1735
int32_t ctgOpDropStbMeta(SCtgCacheOperation *operation) {
D
dapan1121 已提交
1736
  int32_t code = 0;
D
dapan1121 已提交
1737
  SCtgDropStbMetaMsg *msg = operation->data;
D
dapan1121 已提交
1738 1739 1740 1741 1742 1743 1744 1745 1746
  SCatalog* pCtg = msg->pCtg;

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

  if (msg->dbId && (dbCache->dbId != msg->dbId)) {
D
dapan1121 已提交
1747 1748
    ctgDebug("dbId already modified, dbFName:%s, current:0x%"PRIx64", dbId:0x%"PRIx64", stb:%s, suid:0x%"PRIx64, 
             msg->dbFName, dbCache->dbId, msg->dbId, msg->stbName, msg->suid);
D
dapan1121 已提交
1749 1750 1751
    return TSDB_CODE_SUCCESS;
  }
  
D
dapan1121 已提交
1752
  if (taosHashRemove(dbCache->stbCache, &msg->suid, sizeof(msg->suid))) {
D
dapan1121 已提交
1753
    ctgDebug("stb not exist in stbCache, may be removed, dbFName:%s, stb:%s, suid:0x%"PRIx64, msg->dbFName, msg->stbName, msg->suid);
D
dapan1121 已提交
1754
  } else {
D
dapan1121 已提交
1755
    CTG_CACHE_STAT_DEC(numOfStb, 1);
D
dapan1121 已提交
1756 1757
  }

D
dapan1121 已提交
1758
  if (taosHashRemove(dbCache->tbCache, msg->stbName, strlen(msg->stbName))) {  
D
dapan1121 已提交
1759
    ctgError("stb not exist in cache, dbFName:%s, stb:%s, suid:0x%"PRIx64, msg->dbFName, msg->stbName, msg->suid);
D
dapan1121 已提交
1760
  } else {
D
dapan1121 已提交
1761
    CTG_CACHE_STAT_DEC(numOfTbl, 1);
D
dapan1121 已提交
1762 1763
  }
  
D
dapan1121 已提交
1764
  ctgInfo("stb removed from cache, dbFName:%s, stbName:%s, suid:0x%"PRIx64, msg->dbFName, msg->stbName, msg->suid);
D
dapan1121 已提交
1765 1766 1767

  CTG_ERR_JRET(ctgMetaRentRemove(&msg->pCtg->stbRent, msg->suid, ctgStbVersionSortCompare, ctgStbVersionSearchCompare));
  
D
dapan1121 已提交
1768
  ctgDebug("stb removed from rent, dbFName:%s, stbName:%s, suid:0x%"PRIx64, msg->dbFName, msg->stbName, msg->suid);
D
dapan1121 已提交
1769 1770 1771 1772 1773 1774 1775 1776
  
_return:

  taosMemoryFreeClear(msg);
  
  CTG_RET(code);
}

D
dapan1121 已提交
1777
int32_t ctgOpDropTbMeta(SCtgCacheOperation *operation) {
D
dapan1121 已提交
1778
  int32_t code = 0;
D
dapan1121 已提交
1779
  SCtgDropTblMetaMsg *msg = operation->data;
D
dapan1121 已提交
1780 1781 1782 1783 1784 1785 1786 1787 1788
  SCatalog* pCtg = msg->pCtg;

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

  if (dbCache->dbId != msg->dbId) {
D
dapan1121 已提交
1789
    ctgDebug("dbId 0x%" PRIx64 " not match with curId 0x%"PRIx64", dbFName:%s, tbName:%s", msg->dbId, dbCache->dbId, msg->dbFName, msg->tbName);
D
dapan1121 已提交
1790 1791 1792
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
1793 1794 1795
  if (taosHashRemove(dbCache->tbCache, msg->tbName, strlen(msg->tbName))) {
    ctgError("tb %s not exist in cache, dbFName:%s", msg->tbName, msg->dbFName);
    CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
D
dapan1121 已提交
1796
  } else {
D
dapan1121 已提交
1797
    CTG_CACHE_STAT_DEC(numOfTbl, 1);
D
dapan1121 已提交
1798 1799
  }

D
dapan1121 已提交
1800
  ctgDebug("table %s removed from cache, dbFName:%s", msg->tbName, msg->dbFName);
D
dapan1121 已提交
1801 1802 1803 1804 1805 1806 1807 1808

_return:

  taosMemoryFreeClear(msg);

  CTG_RET(code);
}

D
dapan1121 已提交
1809
int32_t ctgOpUpdateUser(SCtgCacheOperation *operation) {
D
dapan1121 已提交
1810
  int32_t code = 0;
D
dapan1121 已提交
1811
  SCtgUpdateUserMsg *msg = operation->data;
D
dapan1121 已提交
1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862
  SCatalog* pCtg = msg->pCtg;
  
  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);
    }

    taosMemoryFreeClear(msg);

    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 已提交
1863 1864 1865 1866 1867 1868
int32_t ctgOpUpdateEpset(SCtgCacheOperation *operation) {
  int32_t code = 0;
  SCtgUpdateEpsetMsg *msg = operation->data;
  SCatalog* pCtg = msg->pCtg;
  
  SCtgDBCache *dbCache = NULL;
D
dapan1121 已提交
1869
  CTG_ERR_JRET(ctgGetDBCache(pCtg, msg->dbFName, &dbCache));
D
dapan1121 已提交
1870 1871 1872 1873 1874
  if (NULL == dbCache) {
    ctgDebug("db %s not exist, ignore epset update", msg->dbFName);
    goto _return;
  }

D
dapan1121 已提交
1875 1876 1877 1878
  CTG_ERR_JRET(ctgWLockVgInfo(pCtg, dbCache));

  SDBVgInfo *vgInfo = dbCache->vgCache.vgInfo;  
  if (NULL == vgInfo) {
D
dapan1121 已提交
1879 1880 1881 1882
    ctgDebug("vgroup in db %s not cached, ignore epset update", msg->dbFName);
    goto _return;
  }
  
D
dapan1121 已提交
1883
  SVgroupInfo* pInfo = taosHashGet(vgInfo->vgHash, &msg->vgId, sizeof(msg->vgId));
D
dapan1121 已提交
1884 1885 1886 1887 1888
  if (NULL == pInfo) {
    ctgDebug("no vgroup %d in db %s, ignore epset update", msg->vgId, msg->dbFName);
    goto _return;
  }

D
dapan1121 已提交
1889 1890 1891 1892 1893
  SEp* pOrigEp = &pInfo->epSet.eps[pInfo->epSet.inUse];
  SEp* pNewEp = &msg->epSet.eps[msg->epSet.inUse];
  ctgDebug("vgroup %d epset updated from %d/%d=>%s:%d to %d/%d=>%s:%d, dbFName:%s in ctg", 
          pInfo->vgId, pInfo->epSet.inUse, pInfo->epSet.numOfEps, pOrigEp->fqdn, pOrigEp->port, 
          msg->epSet.inUse, msg->epSet.numOfEps, pNewEp->fqdn, pNewEp->port, msg->dbFName);
D
dapan1121 已提交
1894

D
dapan1121 已提交
1895
  pInfo->epSet = msg->epSet;
D
dapan1121 已提交
1896 1897 1898 1899

_return:

  if (dbCache) {
D
dapan1121 已提交
1900
    ctgWUnlockVgInfo(dbCache);
D
dapan1121 已提交
1901 1902 1903 1904 1905 1906 1907
  }

  taosMemoryFreeClear(msg);
  
  CTG_RET(code);
}

D
dapan1121 已提交
1908 1909 1910 1911 1912 1913 1914
int32_t ctgOpUpdateTbIndex(SCtgCacheOperation *operation) {
  int32_t code = 0;
  SCtgUpdateTbIndexMsg *msg = operation->data;
  SCatalog* pCtg = msg->pCtg;
  STableIndex* pIndex = msg->pIndex;
  SCtgDBCache *dbCache = NULL;
   
D
dapan1121 已提交
1915
  CTG_ERR_JRET(ctgGetAddDBCache(pCtg, pIndex->dbFName, 0, &dbCache));
D
dapan1121 已提交
1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937

  CTG_ERR_JRET(ctgWriteTbIndexToCache(pCtg, dbCache, pIndex->dbFName, pIndex->tbName, &pIndex));

_return:

  if (pIndex) {
    taosArrayDestroyEx(pIndex->pIndex, tFreeSTableIndexInfo);
    taosMemoryFreeClear(pIndex);
  }
  
  taosMemoryFreeClear(msg);
  
  CTG_RET(code);
}

int32_t ctgOpDropTbIndex(SCtgCacheOperation *operation) {
  int32_t code = 0;
  SCtgDropTbIndexMsg *msg = operation->data;
  SCatalog* pCtg = msg->pCtg;
  SCtgDBCache *dbCache = NULL;
   
  CTG_ERR_JRET(ctgGetDBCache(pCtg, msg->dbFName, &dbCache));
D
dapan1121 已提交
1938
  if (NULL == dbCache) {
D
dapan1121 已提交
1939 1940 1941 1942 1943 1944
    return TSDB_CODE_SUCCESS;
  }

  STableIndex* pIndex = taosMemoryCalloc(1, sizeof(STableIndex));
  if (NULL == pIndex) {
    CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1945
  }
D
dapan1121 已提交
1946 1947 1948
  strcpy(pIndex->tbName, msg->tbName);
  strcpy(pIndex->dbFName, msg->dbFName);
  pIndex->version = -1;
D
dapan1121 已提交
1949

D
dapan1121 已提交
1950
  CTG_ERR_JRET(ctgWriteTbIndexToCache(pCtg, dbCache, pIndex->dbFName, pIndex->tbName, &pIndex));
D
dapan1121 已提交
1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963

_return:

  if (pIndex) {
    taosArrayDestroyEx(pIndex->pIndex, tFreeSTableIndexInfo);
    taosMemoryFreeClear(pIndex);
  }
  
  taosMemoryFreeClear(msg);
  
  CTG_RET(code);
}

D
dapan1121 已提交
1964

D
dapan1121 已提交
1965 1966 1967 1968 1969
int32_t ctgOpClearCache(SCtgCacheOperation *operation) {
  int32_t code = 0;
  SCtgClearCacheMsg *msg = operation->data;
  SCatalog* pCtg = msg->pCtg;

D
dapan1121 已提交
1970 1971
  CTG_LOCK(CTG_WRITE, &gCtgMgmt.lock);

D
dapan1121 已提交
1972
  if (pCtg) {
D
dapan1121 已提交
1973 1974 1975 1976 1977 1978
    if (msg->freeCtg) {
      ctgFreeHandle(pCtg);
    } else {
      ctgClearHandle(pCtg);
    }
    
D
dapan1121 已提交
1979 1980
    goto _return;
  }
D
dapan1121 已提交
1981 1982 1983 1984 1985 1986

  if (msg->freeCtg) {
    ctgFreeAllInstance();
  } else {
    ctgClearAllInstance();
  }
D
dapan1121 已提交
1987 1988

_return:
D
dapan1121 已提交
1989 1990

  CTG_UNLOCK(CTG_WRITE, &gCtgMgmt.lock);
D
dapan1121 已提交
1991 1992 1993 1994 1995 1996
 
  taosMemoryFreeClear(msg);
  
  CTG_RET(code);
}

D
dapan1121 已提交
1997
void ctgCleanupCacheQueue(void) {
D
dapan1121 已提交
1998
  SCtgQNode *node = NULL;
D
dapan1121 已提交
1999
  SCtgQNode *nodeNext = NULL;
D
dapan1121 已提交
2000 2001
  SCtgCacheOperation *op = NULL;
  bool stopQueue = false;
D
dapan1121 已提交
2002 2003 2004 2005 2006

  while (true) {
    node = gCtgMgmt.queue.head->next;
    while (node) {
      if (node->op) {
D
dapan1121 已提交
2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020
        op = node->op;
        if (op->stopQueue) {
          SCatalog *pCtg = ((SCtgUpdateMsgHeader *)op->data)->pCtg;
          ctgDebug("process [%s] operation", gCtgCacheOperation[op->opId].name);
          (*gCtgCacheOperation[op->opId].func)(op);
          stopQueue = true;
          CTG_RT_STAT_INC(numOfOpDequeue, 1); 
        } else {
          taosMemoryFree(op->data);
          CTG_RT_STAT_INC(numOfOpAbort, 1); 
        }
        
        if (op->syncOp) {
          tsem_post(&op->rspSem);
D
dapan1121 已提交
2021
        } else {
D
dapan1121 已提交
2022
          taosMemoryFree(op);
D
dapan1121 已提交
2023
        }
D
dapan1121 已提交
2024
      }
D
dapan1121 已提交
2025 2026 2027 2028 2029

      nodeNext = node->next;
      taosMemoryFree(node);
      
      node = nodeNext;
D
dapan1121 已提交
2030 2031
    }

D
dapan1121 已提交
2032
    if (!stopQueue) {
D
dapan1121 已提交
2033 2034 2035 2036
      taosUsleep(1);
    } else {
      break;
    }
D
dapan1121 已提交
2037 2038 2039 2040 2041 2042
  }

  taosMemoryFreeClear(gCtgMgmt.queue.head);
  gCtgMgmt.queue.tail = NULL;
}

D
dapan1121 已提交
2043 2044
void* ctgUpdateThreadFunc(void* param) {
  setThreadName("catalog");
2045

D
dapan1121 已提交
2046 2047 2048 2049 2050 2051 2052 2053
  qInfo("catalog update thread started");

  while (true) {
    if (tsem_wait(&gCtgMgmt.queue.reqSem)) {
      qError("ctg tsem_wait failed, error:%s", tstrerror(TAOS_SYSTEM_ERROR(errno)));
    }
    
    if (atomic_load_8((int8_t*)&gCtgMgmt.exit)) {
D
dapan1121 已提交
2054
      ctgCleanupCacheQueue();
D
dapan1121 已提交
2055 2056 2057
      break;
    }

D
dapan1121 已提交
2058 2059 2060
    SCtgCacheOperation *operation = NULL;
    ctgDequeue(&operation);
    SCatalog *pCtg = ((SCtgUpdateMsgHeader *)operation->data)->pCtg;
D
dapan1121 已提交
2061

D
dapan1121 已提交
2062
    ctgDebug("process [%s] operation", gCtgCacheOperation[operation->opId].name);
D
dapan1121 已提交
2063
    
D
dapan1121 已提交
2064
    (*gCtgCacheOperation[operation->opId].func)(operation);
D
dapan1121 已提交
2065

D
dapan1121 已提交
2066
    if (operation->syncOp) {
D
dapan1121 已提交
2067
      tsem_post(&operation->rspSem);
D
dapan1121 已提交
2068 2069
    }

D
dapan1121 已提交
2070
    CTG_RT_STAT_INC(numOfOpDequeue, 1); 
D
dapan1121 已提交
2071

D
dapan1121 已提交
2072
    ctgdShowCacheInfo();
D
dapan1121 已提交
2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096
    ctgdShowClusterCache(pCtg);
  }

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


int32_t ctgStartUpdateThread() {
  TdThreadAttr thAttr;
  taosThreadAttrInit(&thAttr);
  taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);

  if (taosThreadCreate(&gCtgMgmt.updateThread, &thAttr, ctgUpdateThreadFunc, NULL) != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    CTG_ERR_RET(terrno);
  }
  
  taosThreadAttrDestroy(&thAttr);
  return TSDB_CODE_SUCCESS;
}


D
dapan1121 已提交
2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184
int32_t ctgGetTbMetaFromCache(SCatalog* pCtg, SRequestConnInfo *pConn, SCtgTbMetaCtx* ctx, STableMeta** pTableMeta) {
  if (CTG_IS_SYS_DBNAME(ctx->pName->dbname)) {
    CTG_FLAG_SET_SYS_DB(ctx->flag);
  }

  CTG_ERR_RET(ctgReadTbMetaFromCache(pCtg, ctx, pTableMeta));

  if (*pTableMeta) {
    if (CTG_FLAG_MATCH_STB(ctx->flag, (*pTableMeta)->tableType) &&
        ((!CTG_FLAG_IS_FORCE_UPDATE(ctx->flag)) || (CTG_FLAG_IS_SYS_DB(ctx->flag)))) {
      return TSDB_CODE_SUCCESS;
    }

    taosMemoryFreeClear(*pTableMeta);
  }

  if (CTG_FLAG_IS_UNKNOWN_STB(ctx->flag)) {
    CTG_FLAG_SET_STB(ctx->flag, ctx->tbInfo.tbType);
  }

  return TSDB_CODE_SUCCESS;
}


int32_t ctgRemoveTbMetaFromCache(SCatalog* pCtg, SName* pTableName, bool syncReq) {
  int32_t       code = 0;
  STableMeta*   tblMeta = NULL;
  SCtgTbMetaCtx tbCtx = {0};
  tbCtx.flag = CTG_FLAG_UNKNOWN_STB;
  tbCtx.pName = pTableName;

  CTG_ERR_JRET(ctgReadTbMetaFromCache(pCtg, &tbCtx, &tblMeta));

  if (NULL == tblMeta) {
    ctgDebug("table already not in cache, db:%s, tblName:%s", pTableName->dbname, pTableName->tname);
    return TSDB_CODE_SUCCESS;
  }

  char dbFName[TSDB_DB_FNAME_LEN];
  tNameGetFullDbName(pTableName, dbFName);

  if (TSDB_SUPER_TABLE == tblMeta->tableType) {
    CTG_ERR_JRET(ctgDropStbMetaEnqueue(pCtg, dbFName, tbCtx.tbInfo.dbId, pTableName->tname, tblMeta->suid, syncReq));
  } else {
    CTG_ERR_JRET(ctgDropTbMetaEnqueue(pCtg, dbFName, tbCtx.tbInfo.dbId, pTableName->tname, syncReq));
  }

_return:

  taosMemoryFreeClear(tblMeta);

  CTG_RET(code);
}

int32_t ctgGetTbHashVgroupFromCache(SCatalog *pCtg, const SName *pTableName, SVgroupInfo **pVgroup) {
  if (CTG_IS_SYS_DBNAME(pTableName->dbname)) {
    ctgError("no valid vgInfo for db, dbname:%s", pTableName->dbname);
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
  }

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

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

  if (NULL == dbCache) {
    *pVgroup = NULL;
    return TSDB_CODE_SUCCESS;
  }

  *pVgroup = taosMemoryCalloc(1, sizeof(SVgroupInfo));
  CTG_ERR_JRET(ctgGetVgInfoFromHashValue(pCtg, dbCache->vgCache.vgInfo, pTableName, *pVgroup));

_return:

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

  if (code) {
    taosMemoryFreeClear(*pVgroup);
  }

  CTG_RET(code);
}

D
dapan1121 已提交
2185