ctgCache.c 81.2 KB
Newer Older
D
dapan1121 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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 "catalogInt.h"
dengyihao's avatar
dengyihao 已提交
17
#include "query.h"
D
dapan1121 已提交
18
#include "systable.h"
dengyihao's avatar
dengyihao 已提交
19 20
#include "tname.h"
#include "trpc.h"
D
dapan1121 已提交
21

dengyihao's avatar
dengyihao 已提交
22 23 24 25 26 27 28 29 30 31 32
SCtgOperation gCtgCacheOperation[CTG_OP_MAX] = {{CTG_OP_UPDATE_VGROUP, "update vgInfo", ctgOpUpdateVgroup},
                                                {CTG_OP_UPDATE_TB_META, "update tbMeta", ctgOpUpdateTbMeta},
                                                {CTG_OP_DROP_DB_CACHE, "drop DB", ctgOpDropDbCache},
                                                {CTG_OP_DROP_DB_VGROUP, "drop DBVgroup", ctgOpDropDbVgroup},
                                                {CTG_OP_DROP_STB_META, "drop stbMeta", ctgOpDropStbMeta},
                                                {CTG_OP_DROP_TB_META, "drop tbMeta", ctgOpDropTbMeta},
                                                {CTG_OP_UPDATE_USER, "update user", ctgOpUpdateUser},
                                                {CTG_OP_UPDATE_VG_EPSET, "update epset", ctgOpUpdateEpset},
                                                {CTG_OP_UPDATE_TB_INDEX, "update tbIndex", ctgOpUpdateTbIndex},
                                                {CTG_OP_DROP_TB_INDEX, "drop tbIndex", ctgOpDropTbIndex},
                                                {CTG_OP_CLEAR_CACHE, "clear cache", ctgOpClearCache}};
D
dapan1121 已提交
33

D
dapan1121 已提交
34
SCtgCacheItemInfo gCtgStatItem[CTG_CI_MAX_VALUE] = {
D
dapan1121 已提交
35 36 37 38 39 40 41 42
    {"Cluster   ", CTG_CI_FLAG_LEVEL_GLOBAL},  //CTG_CI_CLUSTER
    {"Dnode     ", CTG_CI_FLAG_LEVEL_CLUSTER}, //CTG_CI_DNODE,
    {"Qnode     ", CTG_CI_FLAG_LEVEL_CLUSTER}, //CTG_CI_QNODE,
    {"DB        ", CTG_CI_FLAG_LEVEL_CLUSTER}, //CTG_CI_DB,
    {"DbVgroup  ", CTG_CI_FLAG_LEVEL_DB},      //CTG_CI_DB_VGROUP,
    {"DbCfg     ", CTG_CI_FLAG_LEVEL_DB},      //CTG_CI_DB_CFG,
    {"DbInfo    ", CTG_CI_FLAG_LEVEL_DB},      //CTG_CI_DB_INFO,
    {"StbMeta   ", CTG_CI_FLAG_LEVEL_DB},      //CTG_CI_STABLE_META,
D
dapan1121 已提交
43 44 45 46 47 48
    {"NtbMeta   ", CTG_CI_FLAG_LEVEL_DB},      //CTG_CI_NTABLE_META,
    {"CtbMeta   ", CTG_CI_FLAG_LEVEL_DB},      //CTG_CI_CTABLE_META,
    {"SysTblMeta", CTG_CI_FLAG_LEVEL_DB},      //CTG_CI_SYSTABLE_META,
    {"OthTblMeta", CTG_CI_FLAG_LEVEL_DB},      //CTG_CI_OTHERTABLE_META,
    {"TblSMA    ", CTG_CI_FLAG_LEVEL_DB},      //CTG_CI_TBL_SMA,
    {"TblCfg    ", CTG_CI_FLAG_LEVEL_DB},      //CTG_CI_TBL_CFG,
D
dapan1121 已提交
49
    {"TblTag    ", CTG_CI_FLAG_LEVEL_DB},      //CTG_CI_TBL_TAG,
D
dapan1121 已提交
50 51 52 53 54 55
    {"IndexInfo ", CTG_CI_FLAG_LEVEL_DB},      //CTG_CI_INDEX_INFO,
    {"User      ", CTG_CI_FLAG_LEVEL_CLUSTER}, //CTG_CI_USER,
    {"UDF       ", CTG_CI_FLAG_LEVEL_CLUSTER}, //CTG_CI_UDF,
    {"SvrVer    ", CTG_CI_FLAG_LEVEL_CLUSTER}  //CTG_CI_SVR_VER,
};

D
dapan1121 已提交
56 57
int32_t ctgRLockVgInfo(SCatalog *pCtg, SCtgDBCache *dbCache, bool *inCache) {
  CTG_LOCK(CTG_READ, &dbCache->vgCache.vgLock);
dengyihao's avatar
dengyihao 已提交
58

D
dapan1121 已提交
59
  if (dbCache->deleted) {
D
dapan1121 已提交
60
    CTG_UNLOCK(CTG_READ, &dbCache->vgCache.vgLock);
D
dapan1121 已提交
61

dengyihao's avatar
dengyihao 已提交
62 63
    ctgDebug("db is dropping, dbId:0x%" PRIx64, dbCache->dbId);

D
dapan1121 已提交
64 65 66 67
    *inCache = false;
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
68 69
  if (NULL == dbCache->vgCache.vgInfo) {
    CTG_UNLOCK(CTG_READ, &dbCache->vgCache.vgLock);
D
dapan1121 已提交
70 71

    *inCache = false;
dengyihao's avatar
dengyihao 已提交
72
    ctgDebug("db vgInfo is empty, dbId:0x%" PRIx64, dbCache->dbId);
D
dapan1121 已提交
73 74 75 76
    return TSDB_CODE_SUCCESS;
  }

  *inCache = true;
dengyihao's avatar
dengyihao 已提交
77

D
dapan1121 已提交
78 79 80
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
81 82
int32_t ctgWLockVgInfo(SCatalog *pCtg, SCtgDBCache *dbCache) {
  CTG_LOCK(CTG_WRITE, &dbCache->vgCache.vgLock);
D
dapan1121 已提交
83 84

  if (dbCache->deleted) {
dengyihao's avatar
dengyihao 已提交
85
    ctgDebug("db is dropping, dbId:0x%" PRIx64, dbCache->dbId);
D
dapan1121 已提交
86
    CTG_UNLOCK(CTG_WRITE, &dbCache->vgCache.vgLock);
D
dapan1121 已提交
87 88 89 90 91 92
    CTG_ERR_RET(TSDB_CODE_CTG_DB_DROPPED);
  }

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
93
void ctgRUnlockVgInfo(SCtgDBCache *dbCache) { CTG_UNLOCK(CTG_READ, &dbCache->vgCache.vgLock); }
D
dapan1121 已提交
94

dengyihao's avatar
dengyihao 已提交
95
void ctgWUnlockVgInfo(SCtgDBCache *dbCache) { CTG_UNLOCK(CTG_WRITE, &dbCache->vgCache.vgLock); }
D
dapan1121 已提交
96

dengyihao's avatar
dengyihao 已提交
97 98
void ctgReleaseDBCache(SCatalog *pCtg, SCtgDBCache *dbCache) {
  CTG_UNLOCK(CTG_READ, &dbCache->dbLock);
D
dapan1121 已提交
99 100
  taosHashRelease(pCtg->dbCache, dbCache);
}
D
dapan1121 已提交
101

dengyihao's avatar
dengyihao 已提交
102
int32_t ctgAcquireDBCacheImpl(SCatalog *pCtg, const char *dbFName, SCtgDBCache **pCache, bool acquire) {
D
dapan1121 已提交
103
  char *p = strchr(dbFName, '.');
D
dapan1121 已提交
104
  if (p && IS_SYS_DBNAME(p + 1)) {
D
dapan1121 已提交
105 106 107
    dbFName = p + 1;
  }

D
dapan1121 已提交
108 109 110 111 112 113 114
  SCtgDBCache *dbCache = NULL;

  if (acquire) {
    dbCache = (SCtgDBCache *)taosHashAcquire(pCtg->dbCache, dbFName, strlen(dbFName));
  } else {
    dbCache = (SCtgDBCache *)taosHashGet(pCtg->dbCache, dbFName, strlen(dbFName));
  }
dengyihao's avatar
dengyihao 已提交
115

D
dapan1121 已提交
116 117
  if (NULL == dbCache) {
    *pCache = NULL;
D
dapan1121 已提交
118
    CTG_CACHE_NHIT_INC(CTG_CI_DB, 1);
D
dapan1121 已提交
119 120 121 122
    ctgDebug("db not in cache, dbFName:%s", dbFName);
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
123 124 125 126
  if (acquire) {
    CTG_LOCK(CTG_READ, &dbCache->dbLock);
  }

D
dapan1121 已提交
127 128 129
  if (dbCache->deleted) {
    if (acquire) {
      ctgReleaseDBCache(pCtg, dbCache);
dengyihao's avatar
dengyihao 已提交
130 131
    }

D
dapan1121 已提交
132
    *pCache = NULL;
D
dapan1121 已提交
133
    CTG_CACHE_NHIT_INC(CTG_CI_DB, 1);
D
dapan1121 已提交
134 135 136 137 138
    ctgDebug("db is removing from cache, dbFName:%s", dbFName);
    return TSDB_CODE_SUCCESS;
  }

  *pCache = dbCache;
D
dapan1121 已提交
139
  CTG_CACHE_HIT_INC(CTG_CI_DB, 1);
dengyihao's avatar
dengyihao 已提交
140

D
dapan1121 已提交
141 142 143
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
144
int32_t ctgAcquireDBCache(SCatalog *pCtg, const char *dbFName, SCtgDBCache **pCache) {
D
dapan1121 已提交
145 146 147
  CTG_RET(ctgAcquireDBCacheImpl(pCtg, dbFName, pCache, true));
}

dengyihao's avatar
dengyihao 已提交
148
int32_t ctgGetDBCache(SCatalog *pCtg, const char *dbFName, SCtgDBCache **pCache) {
D
dapan1121 已提交
149 150 151
  CTG_RET(ctgAcquireDBCacheImpl(pCtg, dbFName, pCache, false));
}

dengyihao's avatar
dengyihao 已提交
152
void ctgReleaseVgInfoToCache(SCatalog *pCtg, SCtgDBCache *dbCache) {
D
dapan1121 已提交
153 154 155
  ctgRUnlockVgInfo(dbCache);
  ctgReleaseDBCache(pCtg, dbCache);
}
D
dapan1121 已提交
156

dengyihao's avatar
dengyihao 已提交
157
void ctgReleaseTbMetaToCache(SCatalog *pCtg, SCtgDBCache *dbCache, SCtgTbCache *pCache) {
D
dapan1121 已提交
158
  if (pCache && dbCache) {
D
dapan1121 已提交
159
    CTG_UNLOCK(CTG_READ, &pCache->metaLock);
dengyihao's avatar
dengyihao 已提交
160
    taosHashRelease(dbCache->tbCache, pCache);
D
dapan1121 已提交
161
  }
D
dapan1121 已提交
162

D
dapan1121 已提交
163 164
  if (dbCache) {
    ctgReleaseDBCache(pCtg, dbCache);
D
dapan1121 已提交
165
  }
D
dapan1121 已提交
166
}
D
dapan1121 已提交
167

dengyihao's avatar
dengyihao 已提交
168
void ctgReleaseTbIndexToCache(SCatalog *pCtg, SCtgDBCache *dbCache, SCtgTbCache *pCache) {
D
dapan1121 已提交
169 170
  if (pCache) {
    CTG_UNLOCK(CTG_READ, &pCache->indexLock);
dengyihao's avatar
dengyihao 已提交
171
    taosHashRelease(dbCache->tbCache, pCache);
D
dapan1121 已提交
172 173 174 175 176 177 178
  }

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

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

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

dengyihao's avatar
dengyihao 已提交
191
int32_t ctgAcquireVgInfoFromCache(SCatalog *pCtg, const char *dbFName, SCtgDBCache **pCache) {
D
dapan1121 已提交
192
  SCtgDBCache *dbCache = NULL;
D
dapan1121 已提交
193
  ctgAcquireDBCache(pCtg, dbFName, &dbCache);
dengyihao's avatar
dengyihao 已提交
194
  if (NULL == dbCache) {
D
dapan1121 已提交
195 196 197 198 199
    ctgDebug("db %s not in cache", dbFName);
    goto _return;
  }

  bool inCache = false;
D
dapan1121 已提交
200
  ctgRLockVgInfo(pCtg, dbCache, &inCache);
D
dapan1121 已提交
201 202 203 204 205 206 207
  if (!inCache) {
    ctgDebug("vgInfo of db %s not in cache", dbFName);
    goto _return;
  }

  *pCache = dbCache;

D
dapan1121 已提交
208
  CTG_CACHE_HIT_INC(CTG_CI_DB_VGROUP, 1);
D
dapan1121 已提交
209 210

  ctgDebug("Got db vgInfo from cache, dbFName:%s", dbFName);
dengyihao's avatar
dengyihao 已提交
211

D
dapan1121 已提交
212 213 214 215 216 217 218 219 220 221
  return TSDB_CODE_SUCCESS;

_return:

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

  *pCache = NULL;

D
dapan1121 已提交
222
  CTG_CACHE_NHIT_INC(CTG_CI_DB_VGROUP, 1);
dengyihao's avatar
dengyihao 已提交
223

D
dapan1121 已提交
224 225 226
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
227
int32_t ctgAcquireTbMetaFromCache(SCatalog *pCtg, char *dbFName, char *tbName, SCtgDBCache **pDb, SCtgTbCache **pTb) {
D
dapan1121 已提交
228
  SCtgDBCache *dbCache = NULL;
dengyihao's avatar
dengyihao 已提交
229
  SCtgTbCache *pCache = NULL;
D
dapan1121 已提交
230 231 232 233 234
  ctgAcquireDBCache(pCtg, dbFName, &dbCache);
  if (NULL == dbCache) {
    ctgDebug("db %s not in cache", dbFName);
    goto _return;
  }
dengyihao's avatar
dengyihao 已提交
235

D
dapan1121 已提交
236
  pCache = taosHashAcquire(dbCache->tbCache, tbName, strlen(tbName));
D
dapan1121 已提交
237 238 239
  if (NULL == pCache) {
    ctgDebug("tb %s not in cache, dbFName:%s", tbName, dbFName);
    goto _return;
D
dapan1121 已提交
240 241
  }

D
dapan1121 已提交
242 243 244 245 246 247 248 249 250 251
  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);
dengyihao's avatar
dengyihao 已提交
252

D
dapan1121 已提交
253
  CTG_META_HIT_INC(pCache->pMeta->tableType);
D
dapan1121 已提交
254 255 256 257 258 259 260

  return TSDB_CODE_SUCCESS;

_return:

  ctgReleaseTbMetaToCache(pCtg, dbCache, pCache);

D
dapan1121 已提交
261
  CTG_META_NHIT_INC();
dengyihao's avatar
dengyihao 已提交
262

D
dapan1121 已提交
263 264 265
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
266 267
int32_t ctgAcquireVgMetaFromCache(SCatalog *pCtg, const char *dbFName, const char *tbName, SCtgDBCache **pDb,
                                  SCtgTbCache **pTb) {
268 269
  SCtgDBCache *dbCache = NULL;
  SCtgTbCache *tbCache = NULL;
X
Xiaoyu Wang 已提交
270
  bool         vgInCache = false;
271 272 273 274

  ctgAcquireDBCache(pCtg, dbFName, &dbCache);
  if (NULL == dbCache) {
    ctgDebug("db %s not in cache", dbFName);
D
dapan1121 已提交
275
    CTG_CACHE_NHIT_INC(CTG_CI_DB_VGROUP, 1);
276 277 278 279 280 281
    goto _return;
  }

  ctgRLockVgInfo(pCtg, dbCache, &vgInCache);
  if (!vgInCache) {
    ctgDebug("vgInfo of db %s not in cache", dbFName);
D
dapan1121 已提交
282
    CTG_CACHE_NHIT_INC(CTG_CI_DB_VGROUP, 1);
283 284 285 286 287
    goto _return;
  }

  *pDb = dbCache;

D
dapan1121 已提交
288
  CTG_CACHE_HIT_INC(CTG_CI_DB_VGROUP, 1);
289 290 291 292 293 294

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

  tbCache = taosHashAcquire(dbCache->tbCache, tbName, strlen(tbName));
  if (NULL == tbCache) {
    ctgDebug("tb %s not in cache, dbFName:%s", tbName, dbFName);
D
dapan1121 已提交
295
    CTG_META_NHIT_INC();
296 297 298 299 300 301
    goto _return;
  }

  CTG_LOCK(CTG_READ, &tbCache->metaLock);
  if (NULL == tbCache->pMeta) {
    ctgDebug("tb %s meta not in cache, dbFName:%s", tbName, dbFName);
D
dapan1121 已提交
302
    CTG_META_NHIT_INC();
303 304 305 306 307 308 309
    goto _return;
  }

  *pTb = tbCache;

  ctgDebug("tb %s meta got in cache, dbFName:%s", tbName, dbFName);

D
dapan1121 已提交
310
  CTG_META_HIT_INC(tbCache->pMeta->tableType);
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334

  return TSDB_CODE_SUCCESS;

_return:

  if (tbCache) {
    CTG_UNLOCK(CTG_READ, &tbCache->metaLock);
    taosHashRelease(dbCache->tbCache, tbCache);
  }

  if (vgInCache) {
    ctgRUnlockVgInfo(dbCache);
  }

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

  *pDb = NULL;
  *pTb = NULL;

  return TSDB_CODE_SUCCESS;
}

335
/*
dengyihao's avatar
dengyihao 已提交
336 337 338
int32_t ctgAcquireStbMetaFromCache(SCatalog *pCtg, char *dbFName, uint64_t suid, SCtgDBCache **pDb, SCtgTbCache **pTb) {
  SCtgDBCache *dbCache = NULL;
  SCtgTbCache *pCache = NULL;
D
dapan1121 已提交
339 340 341 342 343
  ctgAcquireDBCache(pCtg, dbFName, &dbCache);
  if (NULL == dbCache) {
    ctgDebug("db %s not in cache", dbFName);
    goto _return;
  }
dengyihao's avatar
dengyihao 已提交
344 345

  char *stName = taosHashAcquire(dbCache->stbCache, &suid, sizeof(suid));
D
dapan1121 已提交
346
  if (NULL == stName) {
D
dapan1121 已提交
347
    ctgDebug("stb 0x%" PRIx64 " not in cache, dbFName:%s", suid, dbFName);
D
dapan1121 已提交
348 349 350 351 352
    goto _return;
  }

  pCache = taosHashAcquire(dbCache->tbCache, stName, strlen(stName));
  if (NULL == pCache) {
D
dapan1121 已提交
353
    ctgDebug("stb 0x%" PRIx64 " name %s not in cache, dbFName:%s", suid, stName, dbFName);
D
dapan1121 已提交
354 355 356 357
    taosHashRelease(dbCache->stbCache, stName);
    goto _return;
  }

D
dapan1121 已提交
358 359
  taosHashRelease(dbCache->stbCache, stName);

D
dapan1121 已提交
360 361
  CTG_LOCK(CTG_READ, &pCache->metaLock);
  if (NULL == pCache->pMeta) {
D
dapan1121 已提交
362
    ctgDebug("stb 0x%" PRIx64 " meta not in cache, dbFName:%s", suid, dbFName);
D
dapan1121 已提交
363 364 365 366 367 368
    goto _return;
  }

  *pDb = dbCache;
  *pTb = pCache;

D
dapan1121 已提交
369
  ctgDebug("stb 0x%" PRIx64 " meta got in cache, dbFName:%s", suid, dbFName);
dengyihao's avatar
dengyihao 已提交
370

D
dapan1121 已提交
371
  CTG_META_HIT_INC(pCache->pMeta->tableType, 1);
D
dapan1121 已提交
372 373 374 375 376 377 378

  return TSDB_CODE_SUCCESS;

_return:

  ctgReleaseTbMetaToCache(pCtg, dbCache, pCache);

D
dapan1121 已提交
379
  CTG_META_NHIT_INC(1);
D
dapan1121 已提交
380 381 382

  *pDb = NULL;
  *pTb = NULL;
dengyihao's avatar
dengyihao 已提交
383

D
dapan1121 已提交
384 385
  return TSDB_CODE_SUCCESS;
}
386
*/
D
dapan1121 已提交
387

X
Xiaoyu Wang 已提交
388 389
int32_t ctgAcquireStbMetaFromCache(SCtgDBCache *dbCache, SCatalog *pCtg, char *dbFName, uint64_t suid,
                                   SCtgTbCache **pTb) {
D
dapan1121 已提交
390
  SCtgTbCache *pCache = NULL;
X
Xiaoyu Wang 已提交
391
  char        *stName = taosHashAcquire(dbCache->stbCache, &suid, sizeof(suid));
D
dapan1121 已提交
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
  if (NULL == stName) {
    ctgDebug("stb 0x%" PRIx64 " not in cache, dbFName:%s", suid, dbFName);
    goto _return;
  }

  pCache = taosHashAcquire(dbCache->tbCache, stName, strlen(stName));
  if (NULL == pCache) {
    ctgDebug("stb 0x%" PRIx64 " name %s not in cache, dbFName:%s", suid, stName, dbFName);
    taosHashRelease(dbCache->stbCache, stName);
    goto _return;
  }

  taosHashRelease(dbCache->stbCache, stName);

  CTG_LOCK(CTG_READ, &pCache->metaLock);
  if (NULL == pCache->pMeta) {
    ctgDebug("stb 0x%" PRIx64 " meta not in cache, dbFName:%s", suid, dbFName);
    goto _return;
  }

  *pTb = pCache;

  ctgDebug("stb 0x%" PRIx64 " meta got in cache, dbFName:%s", suid, dbFName);

D
dapan1121 已提交
416
  CTG_META_HIT_INC(pCache->pMeta->tableType);
D
dapan1121 已提交
417 418 419 420 421 422 423

  return TSDB_CODE_SUCCESS;

_return:

  ctgReleaseTbMetaToCache(pCtg, dbCache, pCache);

D
dapan1121 已提交
424
  CTG_META_NHIT_INC();
D
dapan1121 已提交
425 426 427 428 429 430

  *pTb = NULL;

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
431
int32_t ctgAcquireTbIndexFromCache(SCatalog *pCtg, char *dbFName, char *tbName, SCtgDBCache **pDb, SCtgTbCache **pTb) {
D
dapan1121 已提交
432
  SCtgDBCache *dbCache = NULL;
dengyihao's avatar
dengyihao 已提交
433
  SCtgTbCache *pCache = NULL;
D
dapan1121 已提交
434 435
  ctgAcquireDBCache(pCtg, dbFName, &dbCache);
  if (NULL == dbCache) {
D
dapan1121 已提交
436 437 438
    ctgDebug("db %s not in cache", dbFName);
    goto _return;
  }
dengyihao's avatar
dengyihao 已提交
439

D
dapan1121 已提交
440
  int32_t sz = 0;
D
dapan1121 已提交
441
  pCache = taosHashAcquire(dbCache->tbCache, tbName, strlen(tbName));
D
dapan1121 已提交
442 443 444 445 446 447 448 449 450
  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 已提交
451 452
  }

D
dapan1121 已提交
453 454 455 456
  *pDb = dbCache;
  *pTb = pCache;

  ctgDebug("tb %s index got in cache, dbFName:%s", tbName, dbFName);
dengyihao's avatar
dengyihao 已提交
457

D
dapan1121 已提交
458
  CTG_CACHE_HIT_INC(CTG_CI_TBL_SMA, 1);
D
dapan1121 已提交
459 460 461 462 463 464 465

  return TSDB_CODE_SUCCESS;

_return:

  ctgReleaseTbIndexToCache(pCtg, dbCache, pCache);

D
dapan1121 已提交
466
  CTG_CACHE_NHIT_INC(CTG_CI_TBL_SMA, 1);
dengyihao's avatar
dengyihao 已提交
467

D
dapan1121 已提交
468 469 470
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
471
int32_t ctgTbMetaExistInCache(SCatalog *pCtg, char *dbFName, char *tbName, int32_t *exist) {
D
dapan1121 已提交
472
  SCtgDBCache *dbCache = NULL;
D
dapan1121 已提交
473
  SCtgTbCache *tbCache = NULL;
D
dapan1121 已提交
474 475 476
  ctgAcquireTbMetaFromCache(pCtg, dbFName, tbName, &dbCache, &tbCache);
  if (NULL == tbCache) {
    ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
dengyihao's avatar
dengyihao 已提交
477

D
dapan1121 已提交
478 479 480 481 482
    *exist = 0;
    return TSDB_CODE_SUCCESS;
  }

  *exist = 1;
D
dapan1121 已提交
483
  ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
dengyihao's avatar
dengyihao 已提交
484

D
dapan1121 已提交
485 486 487
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
488 489
int32_t ctgCopyTbMeta(SCatalog *pCtg, SCtgTbMetaCtx *ctx, SCtgDBCache **pDb, SCtgTbCache **pTb, STableMeta **pTableMeta,
                      char *dbFName) {
D
dapan1121 已提交
490
  SCtgDBCache *dbCache = *pDb;
491
  SCtgTbCache *tbCache = *pTb;
X
Xiaoyu Wang 已提交
492
  STableMeta  *tbMeta = tbCache->pMeta;
D
dapan1121 已提交
493 494 495 496
  ctx->tbInfo.inCache = true;
  ctx->tbInfo.dbId = dbCache->dbId;
  ctx->tbInfo.suid = tbMeta->suid;
  ctx->tbInfo.tbType = tbMeta->tableType;
dengyihao's avatar
dengyihao 已提交
497

D
dapan1121 已提交
498
  if (tbMeta->tableType != TSDB_CHILD_TABLE) {
D
dapan1121 已提交
499 500 501
    int32_t metaSize = CTG_META_SIZE(tbMeta);
    *pTableMeta = taosMemoryCalloc(1, metaSize);
    if (NULL == *pTableMeta) {
502
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
503 504 505
    }

    memcpy(*pTableMeta, tbMeta, metaSize);
dengyihao's avatar
dengyihao 已提交
506

D
dapan1121 已提交
507
    ctgDebug("Got tb %s meta from cache, type:%d, dbFName:%s", ctx->pName->tname, tbMeta->tableType, dbFName);
D
dapan1121 已提交
508 509
    return TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
510 511

  // PROCESS FOR CHILD TABLE
dengyihao's avatar
dengyihao 已提交
512

D
dapan1121 已提交
513 514 515
  int32_t metaSize = sizeof(SCTableMeta);
  *pTableMeta = taosMemoryCalloc(1, metaSize);
  if (NULL == *pTableMeta) {
516
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
517 518
  }

D
dapan1121 已提交
519
  memcpy(*pTableMeta, tbMeta, metaSize);
dengyihao's avatar
dengyihao 已提交
520

X
Xiaoyu Wang 已提交
521
  // ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
D
dapan1121 已提交
522

523 524 525
  CTG_UNLOCK(CTG_READ, &tbCache->metaLock);
  taosHashRelease(dbCache->tbCache, tbCache);
  *pTb = NULL;
X
Xiaoyu Wang 已提交
526

dengyihao's avatar
dengyihao 已提交
527 528
  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);
D
dapan1121 已提交
529

530
  ctgAcquireStbMetaFromCache(dbCache, pCtg, dbFName, ctx->tbInfo.suid, &tbCache);
D
dapan1121 已提交
531 532
  if (NULL == tbCache) {
    taosMemoryFreeClear(*pTableMeta);
D
dapan1121 已提交
533
    *pDb = NULL;
D
dapan1121 已提交
534 535 536
    ctgDebug("stb 0x%" PRIx64 " meta not in cache", ctx->tbInfo.suid);
    return TSDB_CODE_SUCCESS;
  }
dengyihao's avatar
dengyihao 已提交
537

538 539
  *pTb = tbCache;

dengyihao's avatar
dengyihao 已提交
540 541 542
  STableMeta *stbMeta = tbCache->pMeta;
  if (stbMeta->suid != ctx->tbInfo.suid) {
    ctgError("stb suid 0x%" PRIx64 " in stbCache mis-match, expected suid 0x%" PRIx64, stbMeta->suid, ctx->tbInfo.suid);
543 544
    taosMemoryFreeClear(*pTableMeta);
    CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
D
dapan1121 已提交
545 546
  }

D
dapan1121 已提交
547
  metaSize = CTG_META_SIZE(stbMeta);
D
dapan1121 已提交
548
  *pTableMeta = taosMemoryRealloc(*pTableMeta, metaSize);
dengyihao's avatar
dengyihao 已提交
549
  if (NULL == *pTableMeta) {
550
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
551 552
  }

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

555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
  return TSDB_CODE_SUCCESS;
}

int32_t ctgReadTbMetaFromCache(SCatalog *pCtg, SCtgTbMetaCtx *ctx, STableMeta **pTableMeta) {
  int32_t      code = 0;
  SCtgDBCache *dbCache = NULL;
  SCtgTbCache *tbCache = NULL;
  *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);
  }

  ctgAcquireTbMetaFromCache(pCtg, dbFName, ctx->pName->tname, &dbCache, &tbCache);
  if (NULL == tbCache) {
    ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
577
  CTG_ERR_JRET(ctgCopyTbMeta(pCtg, ctx, &dbCache, &tbCache, pTableMeta, dbFName));
578

D
dapan1121 已提交
579
  ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
D
dapan1121 已提交
580

D
dapan1121 已提交
581
  ctgDebug("Got tb %s meta from cache, dbFName:%s", ctx->pName->tname, dbFName);
dengyihao's avatar
dengyihao 已提交
582

D
dapan1121 已提交
583 584 585 586
  return TSDB_CODE_SUCCESS;

_return:

D
dapan1121 已提交
587
  ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
D
dapan1121 已提交
588
  taosMemoryFreeClear(*pTableMeta);
dengyihao's avatar
dengyihao 已提交
589
  *pTableMeta = NULL;
dengyihao's avatar
dengyihao 已提交
590

D
dapan1121 已提交
591 592 593
  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
594 595
int32_t ctgReadTbVerFromCache(SCatalog *pCtg, SName *pTableName, int32_t *sver, int32_t *tver, int32_t *tbType,
                              uint64_t *suid, char *stbName) {
D
dapan1121 已提交
596
  *sver = -1;
D
dapan1121 已提交
597
  *tver = -1;
D
dapan1121 已提交
598 599

  SCtgDBCache *dbCache = NULL;
dengyihao's avatar
dengyihao 已提交
600
  SCtgTbCache *tbCache = NULL;
D
dapan1121 已提交
601
  char         dbFName[TSDB_DB_FNAME_LEN] = {0};
D
dapan1121 已提交
602 603
  tNameGetFullDbName(pTableName, dbFName);

D
dapan1121 已提交
604 605 606
  ctgAcquireTbMetaFromCache(pCtg, dbFName, pTableName->tname, &dbCache, &tbCache);
  if (NULL == tbCache) {
    ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
D
dapan1121 已提交
607 608 609
    return TSDB_CODE_SUCCESS;
  }

dengyihao's avatar
dengyihao 已提交
610
  STableMeta *tbMeta = tbCache->pMeta;
D
dapan1121 已提交
611 612
  *tbType = tbMeta->tableType;
  *suid = tbMeta->suid;
D
dapan1121 已提交
613

D
dapan1121 已提交
614
  if (*tbType != TSDB_CHILD_TABLE) {
D
dapan1121 已提交
615 616 617
    *sver = tbMeta->sversion;
    *tver = tbMeta->tversion;

dengyihao's avatar
dengyihao 已提交
618 619
    ctgDebug("Got tb %s ver from cache, dbFName:%s, tbType:%d, sver:%d, tver:%d, suid:0x%" PRIx64, pTableName->tname,
             dbFName, *tbType, *sver, *tver, *suid);
D
dapan1121 已提交
620

D
dapan1121 已提交
621
    ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
D
dapan1121 已提交
622 623 624
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
625
  // PROCESS FOR CHILD TABLE
dengyihao's avatar
dengyihao 已提交
626

X
Xiaoyu Wang 已提交
627
  // ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
628 629 630 631
  if (tbCache) {
    CTG_UNLOCK(CTG_READ, &tbCache->metaLock);
    taosHashRelease(dbCache->tbCache, tbCache);
  }
X
Xiaoyu Wang 已提交
632

D
dapan1121 已提交
633
  ctgDebug("Got ctb %s ver from cache, will continue to get its stb ver, dbFName:%s", pTableName->tname, dbFName);
dengyihao's avatar
dengyihao 已提交
634

635
  ctgAcquireStbMetaFromCache(dbCache, pCtg, dbFName, *suid, &tbCache);
D
dapan1121 已提交
636
  if (NULL == tbCache) {
X
Xiaoyu Wang 已提交
637
    // ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
D
dapan1121 已提交
638
    ctgDebug("stb 0x%" PRIx64 " meta not in cache", *suid);
D
dapan1121 已提交
639 640
    return TSDB_CODE_SUCCESS;
  }
dengyihao's avatar
dengyihao 已提交
641 642

  STableMeta *stbMeta = tbCache->pMeta;
D
dapan1121 已提交
643
  if (stbMeta->suid != *suid) {
D
dapan1121 已提交
644
    ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
dengyihao's avatar
dengyihao 已提交
645
    ctgError("stb suid 0x%" PRIx64 " in stbCache mis-match, expected suid:0x%" PRIx64, stbMeta->suid, *suid);
D
dapan1121 已提交
646 647 648
    CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
  }

D
dapan1121 已提交
649
  size_t nameLen = 0;
D
dapan1121 已提交
650
  char  *name = taosHashGetKey(tbCache, &nameLen);
D
dapan1121 已提交
651 652 653 654

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

D
dapan1121 已提交
655 656
  *sver = stbMeta->sversion;
  *tver = stbMeta->tversion;
D
dapan1121 已提交
657

D
dapan1121 已提交
658
  ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
D
dapan1121 已提交
659

dengyihao's avatar
dengyihao 已提交
660 661
  ctgDebug("Got tb %s sver %d tver %d from cache, type:%d, dbFName:%s", pTableName->tname, *sver, *tver, *tbType,
           dbFName);
D
dapan1121 已提交
662 663 664 665

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
666
int32_t ctgReadTbTypeFromCache(SCatalog *pCtg, char *dbFName, char *tbName, int32_t *tbType) {
D
dapan1121 已提交
667
  SCtgDBCache *dbCache = NULL;
dengyihao's avatar
dengyihao 已提交
668
  SCtgTbCache *tbCache = NULL;
D
dapan1121 已提交
669
  CTG_ERR_RET(ctgAcquireTbMetaFromCache(pCtg, dbFName, tbName, &dbCache, &tbCache));
D
dapan1121 已提交
670 671
  if (NULL == tbCache) {
    ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
D
dapan1121 已提交
672 673
    return TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
674 675 676 677

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

dengyihao's avatar
dengyihao 已提交
678 679
  ctgDebug("Got tb %s tbType %d from cache, dbFName:%s", tbName, *tbType, dbFName);

D
dapan1121 已提交
680 681 682
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
683 684
int32_t ctgReadTbIndexFromCache(SCatalog *pCtg, SName *pTableName, SArray **pRes) {
  int32_t      code = 0;
D
dapan1121 已提交
685
  SCtgDBCache *dbCache = NULL;
dengyihao's avatar
dengyihao 已提交
686
  SCtgTbCache *tbCache = NULL;
D
dapan1121 已提交
687 688
  char         dbFName[TSDB_DB_FNAME_LEN] = {0};
  tNameGetFullDbName(pTableName, dbFName);
D
dapan1121 已提交
689

D
dapan1121 已提交
690
  *pRes = NULL;
D
dapan1121 已提交
691

D
dapan1121 已提交
692 693 694
  ctgAcquireTbIndexFromCache(pCtg, dbFName, pTableName->tname, &dbCache, &tbCache);
  if (NULL == tbCache) {
    ctgReleaseTbIndexToCache(pCtg, dbCache, tbCache);
D
dapan1121 已提交
695 696 697
    return TSDB_CODE_SUCCESS;
  }

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

D
dapan1121 已提交
700
_return:
D
dapan1121 已提交
701

D
dapan1121 已提交
702
  ctgReleaseTbIndexToCache(pCtg, dbCache, tbCache);
D
dapan1121 已提交
703

D
dapan1121 已提交
704
  CTG_RET(code);
D
dapan1121 已提交
705 706
}

707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
int32_t ctgGetCachedStbNameFromSuid(SCatalog* pCtg, char* dbFName, uint64_t suid, char **stbName) {
  *stbName = NULL;

  SCtgDBCache *dbCache = NULL;
  ctgAcquireDBCache(pCtg, dbFName, &dbCache);
  if (NULL == dbCache) {
    ctgDebug("db %s not in cache", dbFName);
    return TSDB_CODE_SUCCESS;
  }  
  
  char *stb = taosHashAcquire(dbCache->stbCache, &suid, sizeof(suid));
  if (NULL == stb) {
    ctgDebug("stb 0x%" PRIx64 " not in cache, dbFName:%s", suid, dbFName);
    return TSDB_CODE_SUCCESS;
  }

  *stbName = taosStrdup(stb);

  taosHashRelease(dbCache->stbCache, stb);

  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
730
int32_t ctgChkAuthFromCache(SCatalog *pCtg, SUserAuthInfo *pReq, bool *inCache, SCtgAuthRsp *pRes) {
D
dapan1121 已提交
731
  int32_t code = 0;
D
dapan1121 已提交
732
  if (IS_SYS_DBNAME(pReq->tbName.dbname)) {
733
    *inCache = true;
D
dapan1121 已提交
734 735
    pRes->pRawRes->pass = true;
    ctgDebug("sysdb %s, pass", pReq->tbName.dbname);
736 737 738
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
739
  SCtgUserAuth *pUser = (SCtgUserAuth *)taosHashGet(pCtg->userCache, pReq->user, strlen(pReq->user));
D
dapan1121 已提交
740
  if (NULL == pUser) {
D
dapan1121 已提交
741
    ctgDebug("user not in cache, user:%s", pReq->user);
D
dapan1121 已提交
742 743 744 745 746
    goto _return;
  }

  *inCache = true;

D
dapan1121 已提交
747
  ctgDebug("Got user from cache, user:%s", pReq->user);
D
dapan1121 已提交
748
  CTG_CACHE_HIT_INC(CTG_CI_USER, 1);
dengyihao's avatar
dengyihao 已提交
749

D
dapan1121 已提交
750 751 752
  SCtgAuthReq req = {0};
  req.pRawReq = pReq;
  req.onlyCache = true;
D
dapan1121 已提交
753 754

  CTG_LOCK(CTG_READ, &pUser->lock);
D
dapan1121 已提交
755
  memcpy(&req.authInfo, &pUser->userAuth, sizeof(pUser->userAuth));
D
dapan1121 已提交
756
  code = ctgChkSetAuthRes(pCtg, &req, pRes);
D
dapan1121 已提交
757
  CTG_UNLOCK(CTG_READ, &pUser->lock);
D
dapan1121 已提交
758
  CTG_ERR_JRET(code);
dengyihao's avatar
dengyihao 已提交
759

D
dapan1121 已提交
760 761
  if (pRes->metaNotExists) {
    goto _return;
D
dapan1121 已提交
762 763
  }

D
dapan1121 已提交
764
  CTG_RET(code);
D
dapan1121 已提交
765 766 767 768

_return:

  *inCache = false;
D
dapan1121 已提交
769
  CTG_CACHE_NHIT_INC(CTG_CI_USER, 1);
D
dapan1121 已提交
770
  ctgDebug("Get user from cache failed, user:%s, metaNotExists:%d, code:%d", pReq->user, pRes->metaNotExists, code);
dengyihao's avatar
dengyihao 已提交
771

D
dapan1121 已提交
772
  return code;
D
dapan1121 已提交
773 774
}

D
dapan1121 已提交
775
void ctgDequeue(SCtgCacheOperation **op) {
D
dapan1121 已提交
776
  SCtgQNode *orig = gCtgMgmt.queue.head;
dengyihao's avatar
dengyihao 已提交
777

D
dapan1121 已提交
778 779 780
  SCtgQNode *node = gCtgMgmt.queue.head->next;
  gCtgMgmt.queue.head = gCtgMgmt.queue.head->next;

D
dapan1121 已提交
781
  CTG_QUEUE_DEC();
dengyihao's avatar
dengyihao 已提交
782

D
dapan1121 已提交
783 784
  taosMemoryFreeClear(orig);

D
dapan1121 已提交
785
  *op = node->op;
D
dapan1121 已提交
786 787
}

dengyihao's avatar
dengyihao 已提交
788
int32_t ctgEnqueue(SCatalog *pCtg, SCtgCacheOperation *operation) {
D
dapan1121 已提交
789 790 791
  SCtgQNode *node = taosMemoryCalloc(1, sizeof(SCtgQNode));
  if (NULL == node) {
    qError("calloc %d failed", (int32_t)sizeof(SCtgQNode));
D
dapan1121 已提交
792 793
    taosMemoryFree(operation->data);
    taosMemoryFree(operation);
D
dapan1121 已提交
794
    CTG_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
795 796
  }

dengyihao's avatar
dengyihao 已提交
797 798
  bool  syncOp = operation->syncOp;
  char *opName = gCtgCacheOperation[operation->opId].name;
D
dapan1121 已提交
799 800 801
  if (operation->syncOp) {
    tsem_init(&operation->rspSem, 0, 0);
  }
dengyihao's avatar
dengyihao 已提交
802

D
dapan1121 已提交
803
  node->op = operation;
D
dapan1121 已提交
804 805

  CTG_LOCK(CTG_WRITE, &gCtgMgmt.queue.qlock);
806

D
dapan1121 已提交
807
  if (gCtgMgmt.queue.stopQueue) {
808 809 810 811
    ctgFreeQNode(node);
    CTG_UNLOCK(CTG_WRITE, &gCtgMgmt.queue.qlock);
    CTG_RET(TSDB_CODE_CTG_EXIT);
  }
812

D
dapan1121 已提交
813 814
  gCtgMgmt.queue.tail->next = node;
  gCtgMgmt.queue.tail = node;
815 816 817

  gCtgMgmt.queue.stopQueue = operation->stopQueue;

D
dapan1121 已提交
818 819
  CTG_UNLOCK(CTG_WRITE, &gCtgMgmt.queue.qlock);

D
dapan1121 已提交
820
  ctgDebug("%sync action [%s] added into queue", syncOp ? "S": "As", opName);
D
dapan1121 已提交
821

D
dapan1121 已提交
822
  CTG_QUEUE_INC();
D
dapan1121 已提交
823
  CTG_STAT_RT_INC(numOfOpEnqueue, 1);
D
dapan1121 已提交
824 825 826

  tsem_post(&gCtgMgmt.queue.reqSem);

D
dapan1121 已提交
827
  if (syncOp) {
828 829 830
    if (!operation->unLocked) {
      CTG_UNLOCK(CTG_READ, &gCtgMgmt.lock);
    }
D
dapan1121 已提交
831
    tsem_wait(&operation->rspSem);
832 833 834
    if (!operation->unLocked) {
      CTG_LOCK(CTG_READ, &gCtgMgmt.lock);
    }
D
dapan1121 已提交
835
    taosMemoryFree(operation);
D
dapan1121 已提交
836 837 838 839 840
  }

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
841 842
int32_t ctgDropDbCacheEnqueue(SCatalog *pCtg, const char *dbFName, int64_t dbId) {
  int32_t             code = 0;
D
dapan1121 已提交
843 844
  SCtgCacheOperation *op = taosMemoryCalloc(1, sizeof(SCtgCacheOperation));
  op->opId = CTG_OP_DROP_DB_CACHE;
845
  op->syncOp = true;
dengyihao's avatar
dengyihao 已提交
846

D
dapan1121 已提交
847
  SCtgDropDBMsg *msg = taosMemoryMalloc(sizeof(SCtgDropDBMsg));
D
dapan1121 已提交
848
  if (NULL == msg) {
D
dapan1121 已提交
849
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgDropDBMsg));
D
dapan1121 已提交
850
    taosMemoryFree(op);
D
dapan1121 已提交
851
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
852 853 854
  }

  char *p = strchr(dbFName, '.');
D
dapan1121 已提交
855
  if (p && IS_SYS_DBNAME(p + 1)) {
D
dapan1121 已提交
856 857 858 859
    dbFName = p + 1;
  }

  msg->pCtg = pCtg;
D
dapan1121 已提交
860
  tstrncpy(msg->dbFName, dbFName, sizeof(msg->dbFName));
D
dapan1121 已提交
861 862
  msg->dbId = dbId;

D
dapan1121 已提交
863
  op->data = msg;
D
dapan1121 已提交
864

D
dapan1121 已提交
865
  CTG_ERR_JRET(ctgEnqueue(pCtg, op));
D
dapan1121 已提交
866 867 868 869 870 871 872 873

  return TSDB_CODE_SUCCESS;

_return:

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
874 875
int32_t ctgDropDbVgroupEnqueue(SCatalog *pCtg, const char *dbFName, bool syncOp) {
  int32_t             code = 0;
D
dapan1121 已提交
876 877 878
  SCtgCacheOperation *op = taosMemoryCalloc(1, sizeof(SCtgCacheOperation));
  op->opId = CTG_OP_DROP_DB_VGROUP;
  op->syncOp = syncOp;
dengyihao's avatar
dengyihao 已提交
879

D
dapan1121 已提交
880 881 882
  SCtgDropDbVgroupMsg *msg = taosMemoryMalloc(sizeof(SCtgDropDbVgroupMsg));
  if (NULL == msg) {
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgDropDbVgroupMsg));
D
dapan1121 已提交
883
    taosMemoryFree(op);
D
dapan1121 已提交
884
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
885 886 887
  }

  char *p = strchr(dbFName, '.');
D
dapan1121 已提交
888
  if (p && IS_SYS_DBNAME(p + 1)) {
D
dapan1121 已提交
889 890 891 892
    dbFName = p + 1;
  }

  msg->pCtg = pCtg;
D
dapan1121 已提交
893
  tstrncpy(msg->dbFName, dbFName, sizeof(msg->dbFName));
D
dapan1121 已提交
894

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

  return TSDB_CODE_SUCCESS;

_return:

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
906 907 908
int32_t ctgDropStbMetaEnqueue(SCatalog *pCtg, const char *dbFName, int64_t dbId, const char *stbName, uint64_t suid,
                              bool syncOp) {
  int32_t             code = 0;
D
dapan1121 已提交
909 910 911
  SCtgCacheOperation *op = taosMemoryCalloc(1, sizeof(SCtgCacheOperation));
  op->opId = CTG_OP_DROP_STB_META;
  op->syncOp = syncOp;
dengyihao's avatar
dengyihao 已提交
912

D
dapan1121 已提交
913
  SCtgDropStbMetaMsg *msg = taosMemoryMalloc(sizeof(SCtgDropStbMetaMsg));
D
dapan1121 已提交
914
  if (NULL == msg) {
D
dapan1121 已提交
915
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgDropStbMetaMsg));
D
dapan1121 已提交
916
    taosMemoryFree(op);
D
dapan1121 已提交
917
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
918 919 920
  }

  msg->pCtg = pCtg;
D
dapan1121 已提交
921 922
  tstrncpy(msg->dbFName, dbFName, sizeof(msg->dbFName));
  tstrncpy(msg->stbName, stbName, sizeof(msg->stbName));
D
dapan1121 已提交
923 924 925
  msg->dbId = dbId;
  msg->suid = suid;

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

D
dapan1121 已提交
928
  CTG_ERR_JRET(ctgEnqueue(pCtg, op));
D
dapan1121 已提交
929 930 931 932 933 934 935 936

  return TSDB_CODE_SUCCESS;

_return:

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
937 938
int32_t ctgDropTbMetaEnqueue(SCatalog *pCtg, const char *dbFName, int64_t dbId, const char *tbName, bool syncOp) {
  int32_t             code = 0;
D
dapan1121 已提交
939 940 941
  SCtgCacheOperation *op = taosMemoryCalloc(1, sizeof(SCtgCacheOperation));
  op->opId = CTG_OP_DROP_TB_META;
  op->syncOp = syncOp;
dengyihao's avatar
dengyihao 已提交
942

D
dapan1121 已提交
943
  SCtgDropTblMetaMsg *msg = taosMemoryMalloc(sizeof(SCtgDropTblMetaMsg));
D
dapan1121 已提交
944
  if (NULL == msg) {
D
dapan1121 已提交
945
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgDropTblMetaMsg));
D
dapan1121 已提交
946
    taosMemoryFree(op);
D
dapan1121 已提交
947
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
948 949 950
  }

  msg->pCtg = pCtg;
D
dapan1121 已提交
951 952
  tstrncpy(msg->dbFName, dbFName, sizeof(msg->dbFName));
  tstrncpy(msg->tbName, tbName, sizeof(msg->tbName));
D
dapan1121 已提交
953 954
  msg->dbId = dbId;

D
dapan1121 已提交
955
  op->data = msg;
D
dapan1121 已提交
956

D
dapan1121 已提交
957
  CTG_ERR_JRET(ctgEnqueue(pCtg, op));
D
dapan1121 已提交
958 959 960 961 962 963 964 965

  return TSDB_CODE_SUCCESS;

_return:

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
966 967
int32_t ctgUpdateVgroupEnqueue(SCatalog *pCtg, const char *dbFName, int64_t dbId, SDBVgInfo *dbInfo, bool syncOp) {
  int32_t             code = 0;
D
dapan1121 已提交
968 969 970
  SCtgCacheOperation *op = taosMemoryCalloc(1, sizeof(SCtgCacheOperation));
  op->opId = CTG_OP_UPDATE_VGROUP;
  op->syncOp = syncOp;
dengyihao's avatar
dengyihao 已提交
971

D
dapan1121 已提交
972 973 974
  SCtgUpdateVgMsg *msg = taosMemoryMalloc(sizeof(SCtgUpdateVgMsg));
  if (NULL == msg) {
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgUpdateVgMsg));
D
dapan1121 已提交
975
    taosMemoryFree(op);
976
    freeVgInfo(dbInfo);
D
dapan1121 已提交
977
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
978 979 980
  }

  char *p = strchr(dbFName, '.');
D
dapan1121 已提交
981
  if (p && IS_SYS_DBNAME(p + 1)) {
D
dapan1121 已提交
982 983 984
    dbFName = p + 1;
  }

985 986 987 988 989 990
  code = ctgMakeVgArray(dbInfo);
  if (code) {
    taosMemoryFree(op);
    taosMemoryFree(msg);
    freeVgInfo(dbInfo);
    CTG_ERR_RET(code);
991 992
  }

D
dapan1121 已提交
993
  tstrncpy(msg->dbFName, dbFName, sizeof(msg->dbFName));
D
dapan1121 已提交
994 995 996 997
  msg->pCtg = pCtg;
  msg->dbId = dbId;
  msg->dbInfo = dbInfo;

D
dapan1121 已提交
998
  op->data = msg;
D
dapan1121 已提交
999

D
dapan1121 已提交
1000
  CTG_ERR_JRET(ctgEnqueue(pCtg, op));
D
dapan1121 已提交
1001 1002 1003 1004 1005

  return TSDB_CODE_SUCCESS;

_return:

1006
  freeVgInfo(dbInfo);
D
dapan1121 已提交
1007 1008 1009
  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1010 1011
int32_t ctgUpdateTbMetaEnqueue(SCatalog *pCtg, STableMetaOutput *output, bool syncOp) {
  int32_t             code = 0;
D
dapan1121 已提交
1012 1013 1014
  SCtgCacheOperation *op = taosMemoryCalloc(1, sizeof(SCtgCacheOperation));
  op->opId = CTG_OP_UPDATE_TB_META;
  op->syncOp = syncOp;
dengyihao's avatar
dengyihao 已提交
1015

D
dapan1121 已提交
1016
  SCtgUpdateTbMetaMsg *msg = taosMemoryMalloc(sizeof(SCtgUpdateTbMetaMsg));
D
dapan1121 已提交
1017
  if (NULL == msg) {
D
dapan1121 已提交
1018
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgUpdateTbMetaMsg));
D
dapan1121 已提交
1019
    taosMemoryFree(op);
D
dapan1121 已提交
1020
    CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1021 1022 1023
  }

  char *p = strchr(output->dbFName, '.');
D
dapan1121 已提交
1024
  if (p && IS_SYS_DBNAME(p + 1)) {
D
dapan1121 已提交
1025 1026
    int32_t len = strlen(p + 1);
    memmove(output->dbFName, p + 1, len >= TSDB_DB_FNAME_LEN ? TSDB_DB_FNAME_LEN - 1 : len);
D
dapan1121 已提交
1027 1028 1029
  }

  msg->pCtg = pCtg;
D
dapan1121 已提交
1030
  msg->pMeta = output;
D
dapan1121 已提交
1031

D
dapan1121 已提交
1032
  op->data = msg;
D
dapan1121 已提交
1033

D
dapan1121 已提交
1034
  CTG_ERR_JRET(ctgEnqueue(pCtg, op));
D
dapan1121 已提交
1035 1036

  return TSDB_CODE_SUCCESS;
dengyihao's avatar
dengyihao 已提交
1037

D
dapan1121 已提交
1038 1039
_return:

D
dapan1121 已提交
1040 1041 1042 1043 1044
  if (output) {
    taosMemoryFree(output->tbMeta);
    taosMemoryFree(output);
  }

D
dapan1121 已提交
1045 1046 1047
  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1048 1049
int32_t ctgUpdateVgEpsetEnqueue(SCatalog *pCtg, char *dbFName, int32_t vgId, SEpSet *pEpSet) {
  int32_t             code = 0;
D
dapan1121 已提交
1050 1051
  SCtgCacheOperation *op = taosMemoryCalloc(1, sizeof(SCtgCacheOperation));
  op->opId = CTG_OP_UPDATE_VG_EPSET;
dengyihao's avatar
dengyihao 已提交
1052

D
dapan1121 已提交
1053 1054 1055
  SCtgUpdateEpsetMsg *msg = taosMemoryMalloc(sizeof(SCtgUpdateEpsetMsg));
  if (NULL == msg) {
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgUpdateEpsetMsg));
D
dapan1121 已提交
1056
    taosMemoryFree(op);
D
dapan1121 已提交
1057
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1058 1059 1060
  }

  msg->pCtg = pCtg;
D
dapan1121 已提交
1061
  tstrncpy(msg->dbFName, dbFName, sizeof(msg->dbFName));
D
dapan1121 已提交
1062 1063 1064
  msg->vgId = vgId;
  msg->epSet = *pEpSet;

D
dapan1121 已提交
1065
  op->data = msg;
D
dapan1121 已提交
1066

D
dapan1121 已提交
1067
  CTG_ERR_JRET(ctgEnqueue(pCtg, op));
D
dapan1121 已提交
1068 1069

  return TSDB_CODE_SUCCESS;
dengyihao's avatar
dengyihao 已提交
1070

D
dapan1121 已提交
1071 1072 1073 1074 1075
_return:

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1076 1077
int32_t ctgUpdateUserEnqueue(SCatalog *pCtg, SGetUserAuthRsp *pAuth, bool syncOp) {
  int32_t             code = 0;
D
dapan1121 已提交
1078 1079 1080
  SCtgCacheOperation *op = taosMemoryCalloc(1, sizeof(SCtgCacheOperation));
  op->opId = CTG_OP_UPDATE_USER;
  op->syncOp = syncOp;
dengyihao's avatar
dengyihao 已提交
1081

D
dapan1121 已提交
1082 1083 1084
  SCtgUpdateUserMsg *msg = taosMemoryMalloc(sizeof(SCtgUpdateUserMsg));
  if (NULL == msg) {
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgUpdateUserMsg));
D
dapan1121 已提交
1085
    taosMemoryFree(op);
D
dapan1121 已提交
1086
    CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1087 1088 1089 1090 1091
  }

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

D
dapan1121 已提交
1092
  op->data = msg;
D
dapan1121 已提交
1093

D
dapan1121 已提交
1094
  CTG_ERR_JRET(ctgEnqueue(pCtg, op));
dengyihao's avatar
dengyihao 已提交
1095

D
dapan1121 已提交
1096
  return TSDB_CODE_SUCCESS;
dengyihao's avatar
dengyihao 已提交
1097

D
dapan1121 已提交
1098 1099 1100
_return:

  tFreeSGetUserAuthRsp(pAuth);
dengyihao's avatar
dengyihao 已提交
1101

D
dapan1121 已提交
1102 1103 1104
  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1105 1106
int32_t ctgUpdateTbIndexEnqueue(SCatalog *pCtg, STableIndex **pIndex, bool syncOp) {
  int32_t             code = 0;
D
dapan1121 已提交
1107 1108 1109
  SCtgCacheOperation *op = taosMemoryCalloc(1, sizeof(SCtgCacheOperation));
  op->opId = CTG_OP_UPDATE_TB_INDEX;
  op->syncOp = syncOp;
dengyihao's avatar
dengyihao 已提交
1110

D
dapan1121 已提交
1111 1112 1113
  SCtgUpdateTbIndexMsg *msg = taosMemoryMalloc(sizeof(SCtgUpdateTbIndexMsg));
  if (NULL == msg) {
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgUpdateTbIndexMsg));
D
dapan1121 已提交
1114
    taosMemoryFree(op);
D
dapan1121 已提交
1115
    CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1116 1117 1118
  }

  msg->pCtg = pCtg;
D
dapan1121 已提交
1119
  msg->pIndex = *pIndex;
D
dapan1121 已提交
1120 1121 1122 1123

  op->data = msg;

  CTG_ERR_JRET(ctgEnqueue(pCtg, op));
D
dapan1121 已提交
1124 1125

  *pIndex = NULL;
D
dapan1121 已提交
1126
  return TSDB_CODE_SUCCESS;
dengyihao's avatar
dengyihao 已提交
1127

D
dapan1121 已提交
1128 1129
_return:

D
dapan1121 已提交
1130
  taosArrayDestroyEx((*pIndex)->pIndex, tFreeSTableIndexInfo);
D
dapan1121 已提交
1131
  taosMemoryFreeClear(*pIndex);
dengyihao's avatar
dengyihao 已提交
1132

D
dapan1121 已提交
1133 1134 1135
  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1136 1137
int32_t ctgDropTbIndexEnqueue(SCatalog *pCtg, SName *pName, bool syncOp) {
  int32_t             code = 0;
D
dapan1121 已提交
1138 1139 1140
  SCtgCacheOperation *op = taosMemoryCalloc(1, sizeof(SCtgCacheOperation));
  op->opId = CTG_OP_DROP_TB_INDEX;
  op->syncOp = syncOp;
dengyihao's avatar
dengyihao 已提交
1141

D
dapan1121 已提交
1142 1143 1144
  SCtgDropTbIndexMsg *msg = taosMemoryMalloc(sizeof(SCtgDropTbIndexMsg));
  if (NULL == msg) {
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgDropTbIndexMsg));
D
dapan1121 已提交
1145
    taosMemoryFree(op);
D
dapan1121 已提交
1146
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1147 1148 1149 1150 1151 1152 1153 1154 1155
  }

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

  op->data = msg;

  CTG_ERR_JRET(ctgEnqueue(pCtg, op));
dengyihao's avatar
dengyihao 已提交
1156

D
dapan1121 已提交
1157
  return TSDB_CODE_SUCCESS;
dengyihao's avatar
dengyihao 已提交
1158

D
dapan1121 已提交
1159 1160 1161 1162 1163
_return:

  CTG_RET(code);
}

D
dapan1121 已提交
1164
int32_t ctgClearCacheEnqueue(SCatalog *pCtg, bool clearMeta, bool freeCtg, bool stopQueue, bool syncOp) {
dengyihao's avatar
dengyihao 已提交
1165
  int32_t             code = 0;
D
dapan1121 已提交
1166 1167 1168
  SCtgCacheOperation *op = taosMemoryCalloc(1, sizeof(SCtgCacheOperation));
  op->opId = CTG_OP_CLEAR_CACHE;
  op->syncOp = syncOp;
D
dapan1121 已提交
1169
  op->stopQueue = stopQueue;
1170
  op->unLocked = true;
dengyihao's avatar
dengyihao 已提交
1171

D
dapan1121 已提交
1172 1173 1174
  SCtgClearCacheMsg *msg = taosMemoryMalloc(sizeof(SCtgClearCacheMsg));
  if (NULL == msg) {
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgClearCacheMsg));
D
dapan1121 已提交
1175
    taosMemoryFree(op);
D
dapan1121 已提交
1176
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1177 1178 1179
  }

  msg->pCtg = pCtg;
D
dapan1121 已提交
1180
  msg->clearMeta = clearMeta;
D
dapan1121 已提交
1181
  msg->freeCtg = freeCtg;
D
dapan1121 已提交
1182 1183 1184
  op->data = msg;

  CTG_ERR_JRET(ctgEnqueue(pCtg, op));
dengyihao's avatar
dengyihao 已提交
1185

D
dapan1121 已提交
1186
  return TSDB_CODE_SUCCESS;
dengyihao's avatar
dengyihao 已提交
1187

D
dapan1121 已提交
1188 1189 1190 1191 1192
_return:

  CTG_RET(code);
}

D
dapan1121 已提交
1193
int32_t ctgMetaRentInit(SCtgRentMgmt *mgmt, uint32_t rentSec, int8_t type, int32_t size) {
D
dapan1121 已提交
1194 1195 1196
  mgmt->slotRIdx = 0;
  mgmt->slotNum = rentSec / CTG_RENT_SLOT_SECOND;
  mgmt->type = type;
D
dapan1121 已提交
1197
  mgmt->metaSize = size;
D
dapan1121 已提交
1198 1199

  size_t msgSize = sizeof(SCtgRentSlot) * mgmt->slotNum;
dengyihao's avatar
dengyihao 已提交
1200

D
dapan1121 已提交
1201 1202 1203
  mgmt->slots = taosMemoryCalloc(1, msgSize);
  if (NULL == mgmt->slots) {
    qError("calloc %d failed", (int32_t)msgSize);
D
dapan1121 已提交
1204
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1205 1206
  }

D
dapan1121 已提交
1207 1208
  mgmt->rentCacheSize = msgSize;

D
dapan1121 已提交
1209
  qDebug("meta rent initialized, type:%d, slotNum:%d", type, mgmt->slotNum);
dengyihao's avatar
dengyihao 已提交
1210

D
dapan1121 已提交
1211 1212 1213 1214 1215 1216 1217
  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];
dengyihao's avatar
dengyihao 已提交
1218 1219
  int32_t       code = 0;

D
dapan1121 已提交
1220 1221 1222 1223
  CTG_LOCK(CTG_WRITE, &slot->lock);
  if (NULL == slot->meta) {
    slot->meta = taosArrayInit(CTG_DEFAULT_RENT_SLOT_SIZE, size);
    if (NULL == slot->meta) {
dengyihao's avatar
dengyihao 已提交
1224 1225
      qError("taosArrayInit %d failed, id:0x%" PRIx64 ", slot idx:%d, type:%d", CTG_DEFAULT_RENT_SLOT_SIZE, id, widx,
             mgmt->type);
D
dapan1121 已提交
1226
      CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1227 1228 1229 1230
    }
  }

  if (NULL == taosArrayPush(slot->meta, meta)) {
dengyihao's avatar
dengyihao 已提交
1231
    qError("taosArrayPush meta to rent failed, id:0x%" PRIx64 ", slot idx:%d, type:%d", id, widx, mgmt->type);
D
dapan1121 已提交
1232
    CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1233 1234
  }

D
dapan1121 已提交
1235
  mgmt->rentCacheSize += size;
D
dapan1121 已提交
1236 1237
  slot->needSort = true;

dengyihao's avatar
dengyihao 已提交
1238
  qDebug("add meta to rent, id:0x%" PRIx64 ", slot idx:%d, type:%d", id, widx, mgmt->type);
D
dapan1121 已提交
1239 1240 1241 1242 1243 1244 1245

_return:

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

dengyihao's avatar
dengyihao 已提交
1246 1247
int32_t ctgMetaRentUpdate(SCtgRentMgmt *mgmt, void *meta, int64_t id, int32_t size, __compar_fn_t sortCompare,
                          __compar_fn_t searchCompare) {
D
dapan1121 已提交
1248 1249 1250
  int16_t widx = abs((int)(id % mgmt->slotNum));

  SCtgRentSlot *slot = &mgmt->slots[widx];
dengyihao's avatar
dengyihao 已提交
1251
  int32_t       code = 0;
D
dapan1121 已提交
1252 1253 1254

  CTG_LOCK(CTG_WRITE, &slot->lock);
  if (NULL == slot->meta) {
dengyihao's avatar
dengyihao 已提交
1255
    qDebug("empty meta slot, id:0x%" PRIx64 ", slot idx:%d, type:%d", id, widx, mgmt->type);
D
dapan1121 已提交
1256 1257 1258 1259
    CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
  }

  if (slot->needSort) {
dengyihao's avatar
dengyihao 已提交
1260 1261
    qDebug("meta slot before sorte, slot idx:%d, type:%d, size:%d", widx, mgmt->type,
           (int32_t)taosArrayGetSize(slot->meta));
D
dapan1121 已提交
1262 1263 1264 1265 1266 1267 1268
    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) {
dengyihao's avatar
dengyihao 已提交
1269 1270
    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 已提交
1271 1272 1273 1274 1275
    CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
  }

  memcpy(orig, meta, size);

dengyihao's avatar
dengyihao 已提交
1276
  qDebug("meta in rent updated, id:0x%" PRIx64 ", slot idx:%d, type:%d", id, widx, mgmt->type);
D
dapan1121 已提交
1277 1278 1279 1280 1281 1282

_return:

  CTG_UNLOCK(CTG_WRITE, &slot->lock);

  if (code) {
dengyihao's avatar
dengyihao 已提交
1283 1284
    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 已提交
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
    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];
dengyihao's avatar
dengyihao 已提交
1295 1296
  int32_t       code = 0;

D
dapan1121 已提交
1297 1298
  CTG_LOCK(CTG_WRITE, &slot->lock);
  if (NULL == slot->meta) {
dengyihao's avatar
dengyihao 已提交
1299
    qError("empty meta slot, id:0x%" PRIx64 ", slot idx:%d, type:%d", id, widx, mgmt->type);
D
dapan1121 已提交
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
    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) {
dengyihao's avatar
dengyihao 已提交
1311
    qError("meta not found in slot, id:0x%" PRIx64 ", slot idx:%d, type:%d", id, widx, mgmt->type);
D
dapan1121 已提交
1312 1313 1314 1315
    CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
  }

  taosArrayRemove(slot->meta, idx);
D
dapan1121 已提交
1316
  mgmt->rentCacheSize -= mgmt->metaSize;
D
dapan1121 已提交
1317

dengyihao's avatar
dengyihao 已提交
1318
  qDebug("meta in rent removed, id:0x%" PRIx64 ", slot idx:%d, type:%d", id, widx, mgmt->type);
D
dapan1121 已提交
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334

_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];
dengyihao's avatar
dengyihao 已提交
1335 1336
  int32_t       code = 0;

D
dapan1121 已提交
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
  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 已提交
1355
    CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
  }

  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;

dengyihao's avatar
dengyihao 已提交
1402 1403
  newDBCache.tbCache = taosHashInit(gCtgMgmt.cfg.maxTblCacheNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY),
                                    true, HASH_ENTRY_LOCK);
D
dapan1121 已提交
1404
  if (NULL == newDBCache.tbCache) {
D
dapan1121 已提交
1405
    ctgError("taosHashInit %d metaCache failed", gCtgMgmt.cfg.maxTblCacheNum);
D
dapan1121 已提交
1406
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1407 1408
  }

dengyihao's avatar
dengyihao 已提交
1409 1410
  newDBCache.stbCache = taosHashInit(gCtgMgmt.cfg.maxTblCacheNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT),
                                     true, HASH_ENTRY_LOCK);
D
dapan1121 已提交
1411
  if (NULL == newDBCache.stbCache) {
D
dapan1121 已提交
1412
    ctgError("taosHashInit %d stbCache failed", gCtgMgmt.cfg.maxTblCacheNum);
D
dapan1121 已提交
1413
    CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1414 1415 1416 1417 1418 1419 1420 1421
  }

  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;
    }
dengyihao's avatar
dengyihao 已提交
1422

D
dapan1121 已提交
1423
    ctgError("taosHashPut db to cache failed, dbFName:%s", dbFName);
D
dapan1121 已提交
1424
    CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1425 1426
  }

D
dapan1121 已提交
1427
  CTG_CACHE_NUM_INC(CTG_CI_DB, 1);
dengyihao's avatar
dengyihao 已提交
1428

D
dapan1121 已提交
1429
  SDbVgVersion vgVersion = {.dbId = newDBCache.dbId, .vgVersion = -1, .stateTs = 0};
D
dapan1121 已提交
1430
  tstrncpy(vgVersion.dbFName, dbFName, sizeof(vgVersion.dbFName));
D
dapan1121 已提交
1431

dengyihao's avatar
dengyihao 已提交
1432
  ctgDebug("db added to cache, dbFName:%s, dbId:0x%" PRIx64, dbFName, dbId);
D
dapan1121 已提交
1433

D
dapan1121 已提交
1434 1435
  if (!IS_SYS_DBNAME(dbFName)) {
    CTG_ERR_RET(ctgMetaRentAdd(&pCtg->dbRent, &vgVersion, dbId, sizeof(SDbVgVersion)));
D
dapan1121 已提交
1436

D
dapan1121 已提交
1437 1438
    ctgDebug("db added to rent, dbFName:%s, vgVersion:%d, dbId:0x%" PRIx64, dbFName, vgVersion.vgVersion, dbId);
  }
D
dapan1121 已提交
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448

  return TSDB_CODE_SUCCESS;

_return:

  ctgFreeDbCache(&newDBCache);

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1449
void ctgRemoveStbRent(SCatalog *pCtg, SCtgDBCache *dbCache) {
D
dapan1121 已提交
1450 1451 1452
  if (NULL == dbCache->stbCache) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
1453

D
dapan1121 已提交
1454 1455 1456 1457
  void *pIter = taosHashIterate(dbCache->stbCache, NULL);
  while (pIter) {
    uint64_t *suid = NULL;
    suid = taosHashGetKey(pIter, NULL);
D
dapan1121 已提交
1458

dengyihao's avatar
dengyihao 已提交
1459 1460 1461
    if (TSDB_CODE_SUCCESS ==
        ctgMetaRentRemove(&pCtg->stbRent, *suid, ctgStbVersionSortCompare, ctgStbVersionSearchCompare)) {
      ctgDebug("stb removed from rent, suid:0x%" PRIx64, *suid);
D
dapan1121 已提交
1462
    }
dengyihao's avatar
dengyihao 已提交
1463

D
dapan1121 已提交
1464
    pIter = taosHashIterate(dbCache->stbCache, pIter);
D
dapan1121 已提交
1465 1466 1467
  }
}

dengyihao's avatar
dengyihao 已提交
1468
int32_t ctgRemoveDBFromCache(SCatalog *pCtg, SCtgDBCache *dbCache, const char *dbFName) {
D
dapan1121 已提交
1469
  uint64_t dbId = dbCache->dbId;
dengyihao's avatar
dengyihao 已提交
1470 1471

  ctgInfo("start to remove db from cache, dbFName:%s, dbId:0x%" PRIx64, dbFName, dbCache->dbId);
D
dapan1121 已提交
1472

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

D
dapan1121 已提交
1475
  atomic_store_8(&dbCache->deleted, 1);
D
dapan1121 已提交
1476
  ctgRemoveStbRent(pCtg, dbCache);
D
dapan1121 已提交
1477 1478
  ctgFreeDbCache(dbCache);

D
dapan1121 已提交
1479 1480 1481
  CTG_UNLOCK(CTG_WRITE, &dbCache->dbLock);

  CTG_ERR_RET(ctgMetaRentRemove(&pCtg->dbRent, dbId, ctgDbVgVersionSortCompare, ctgDbVgVersionSearchCompare));
dengyihao's avatar
dengyihao 已提交
1482
  ctgDebug("db removed from rent, dbFName:%s, dbId:0x%" PRIx64, dbFName, dbId);
D
dapan1121 已提交
1483 1484 1485 1486 1487 1488

  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 已提交
1489
  CTG_CACHE_NUM_DEC(CTG_CI_DB, 1);
dengyihao's avatar
dengyihao 已提交
1490 1491
  ctgInfo("db removed from cache, dbFName:%s, dbId:0x%" PRIx64, dbFName, dbId);

D
dapan1121 已提交
1492 1493 1494
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
1495 1496
int32_t ctgGetAddDBCache(SCatalog *pCtg, const char *dbFName, uint64_t dbId, SCtgDBCache **pCache) {
  int32_t      code = 0;
D
dapan1121 已提交
1497 1498
  SCtgDBCache *dbCache = NULL;
  ctgGetDBCache(pCtg, dbFName, &dbCache);
dengyihao's avatar
dengyihao 已提交
1499

D
dapan1121 已提交
1500
  if (dbCache) {
dengyihao's avatar
dengyihao 已提交
1501
    // TODO OPEN IT
D
dapan1121 已提交
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
#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;
    }
dengyihao's avatar
dengyihao 已提交
1518

D
dapan1121 已提交
1519 1520 1521 1522 1523 1524 1525
    if (dbCache->dbId == dbId) {
      *pCache = dbCache;
      return TSDB_CODE_SUCCESS;
    }
#endif
    CTG_ERR_RET(ctgRemoveDBFromCache(pCtg, dbCache, dbFName));
  }
dengyihao's avatar
dengyihao 已提交
1526

D
dapan1121 已提交
1527 1528 1529 1530 1531 1532 1533 1534 1535
  CTG_ERR_RET(ctgAddNewDBCache(pCtg, dbFName, dbId));

  ctgGetDBCache(pCtg, dbFName, &dbCache);

  *pCache = dbCache;

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
1536 1537
int32_t ctgUpdateRentStbVersion(SCatalog *pCtg, char *dbFName, char *tbName, uint64_t dbId, uint64_t suid,
                                SCtgTbCache *pCache) {
D
dapan1121 已提交
1538 1539 1540 1541 1542 1543 1544 1545 1546
  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;
  }
dengyihao's avatar
dengyihao 已提交
1547

D
dapan1121 已提交
1548 1549
  tstrncpy(metaRent.dbFName, dbFName, sizeof(metaRent.dbFName));
  tstrncpy(metaRent.stbName, tbName, sizeof(metaRent.stbName));
D
dapan1121 已提交
1550

dengyihao's avatar
dengyihao 已提交
1551 1552
  CTG_ERR_RET(ctgMetaRentUpdate(&pCtg->stbRent, &metaRent, metaRent.suid, sizeof(SSTableVersion),
                                ctgStbVersionSortCompare, ctgStbVersionSearchCompare));
D
dapan1121 已提交
1553

dengyihao's avatar
dengyihao 已提交
1554 1555
  ctgDebug("db %s,0x%" PRIx64 " stb %s,0x%" PRIx64 " sver %d tver %d smaVer %d updated to stbRent", dbFName, dbId,
           tbName, suid, metaRent.sversion, metaRent.tversion, metaRent.smaVer);
D
dapan1121 已提交
1556

dengyihao's avatar
dengyihao 已提交
1557 1558
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
1559

dengyihao's avatar
dengyihao 已提交
1560 1561
int32_t ctgWriteTbMetaToCache(SCatalog *pCtg, SCtgDBCache *dbCache, char *dbFName, uint64_t dbId, char *tbName,
                              STableMeta *meta, int32_t metaSize) {
D
dapan1121 已提交
1562 1563
  if (NULL == dbCache->tbCache || NULL == dbCache->stbCache) {
    taosMemoryFree(meta);
dengyihao's avatar
dengyihao 已提交
1564
    ctgError("db is dropping, dbId:0x%" PRIx64, dbCache->dbId);
D
dapan1121 已提交
1565 1566 1567
    CTG_ERR_RET(TSDB_CODE_CTG_DB_DROPPED);
  }

dengyihao's avatar
dengyihao 已提交
1568 1569 1570 1571 1572
  bool         isStb = meta->tableType == TSDB_SUPER_TABLE;
  SCtgTbCache *pCache = taosHashGet(dbCache->tbCache, tbName, strlen(tbName));
  STableMeta  *orig = (pCache ? pCache->pMeta : NULL);
  int8_t       origType = 0;

D
dapan1121 已提交
1573 1574 1575
  if (orig) {
    origType = orig->tableType;

dengyihao's avatar
dengyihao 已提交
1576 1577
    if (origType == meta->tableType && orig->uid == meta->uid &&
        (origType == TSDB_CHILD_TABLE || (orig->sversion >= meta->sversion && orig->tversion >= meta->tversion))) {
D
dapan1121 已提交
1578 1579
      taosMemoryFree(meta);
      ctgDebug("ignore table %s meta update", tbName);
D
dapan1121 已提交
1580 1581
      return TSDB_CODE_SUCCESS;
    }
dengyihao's avatar
dengyihao 已提交
1582

D
dapan1121 已提交
1583
    if (origType == TSDB_SUPER_TABLE) {
D
dapan1121 已提交
1584 1585 1586 1587 1588 1589 1590 1591 1592
      char *stbName = taosHashGet(dbCache->stbCache, &orig->suid, sizeof(orig->suid));
      if (stbName) {
        uint64_t metaSize = strlen(stbName) + 1 + sizeof(orig->suid);
        if (taosHashRemove(dbCache->stbCache, &orig->suid, sizeof(orig->suid))) {
          ctgError("stb not exist in stbCache, dbFName:%s, stb:%s, suid:0x%" PRIx64, dbFName, tbName, orig->suid);
        } else {
          ctgDebug("stb removed from stbCache, dbFName:%s, stb:%s, suid:0x%" PRIx64, dbFName, tbName, orig->suid);
          atomic_sub_fetch_64(&dbCache->dbCacheSize, metaSize);
        }
D
dapan1121 已提交
1593 1594 1595 1596
      }
    }
  }

D
dapan1121 已提交
1597 1598 1599 1600 1601
  if (NULL == pCache) {
    SCtgTbCache cache = {0};
    cache.pMeta = meta;
    if (taosHashPut(dbCache->tbCache, tbName, strlen(tbName), &cache, sizeof(SCtgTbCache)) != 0) {
      ctgError("taosHashPut new tbCache failed, dbFName:%s, tbName:%s, tbType:%d", dbFName, tbName, meta->tableType);
1602
      taosMemoryFree(meta);
D
dapan1121 已提交
1603
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1604
    }
dengyihao's avatar
dengyihao 已提交
1605

D
dapan1121 已提交
1606 1607
    atomic_add_fetch_64(&dbCache->dbCacheSize, strlen(tbName) + sizeof(SCtgTbCache) + ctgGetTbMetaCacheSize(meta));

D
dapan1121 已提交
1608 1609
    pCache = taosHashGet(dbCache->tbCache, tbName, strlen(tbName));
  } else {
1610
    CTG_LOCK(CTG_WRITE, &pCache->metaLock);
D
dapan1121 已提交
1611 1612 1613
    if (orig) {
      CTG_META_NUM_DEC(origType);
    }
D
dapan1121 已提交
1614 1615 1616

    atomic_add_fetch_64(&dbCache->dbCacheSize, ctgGetTbMetaCacheSize(meta) - ctgGetTbMetaCacheSize(pCache->pMeta));

D
dapan1121 已提交
1617 1618
    taosMemoryFree(pCache->pMeta);
    pCache->pMeta = meta;
D
dapan1121 已提交
1619
    
1620
    CTG_UNLOCK(CTG_WRITE, &pCache->metaLock);
D
dapan1121 已提交
1621 1622
  }

D
dapan1121 已提交
1623
  CTG_META_NUM_INC(pCache->pMeta->tableType);
D
dapan1121 已提交
1624 1625 1626 1627 1628 1629 1630 1631

  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 已提交
1632
  if (taosHashPut(dbCache->stbCache, &meta->suid, sizeof(meta->suid), tbName, strlen(tbName) + 1) != 0) {
dengyihao's avatar
dengyihao 已提交
1633
    ctgError("taosHashPut to stable cache failed, suid:0x%" PRIx64, meta->suid);
D
dapan1121 已提交
1634
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1635 1636
  }

D
dapan1121 已提交
1637 1638
  atomic_add_fetch_64(&dbCache->dbCacheSize, sizeof(meta->suid) + strlen(tbName) + 1);

dengyihao's avatar
dengyihao 已提交
1639 1640
  ctgDebug("stb 0x%" PRIx64 " updated to cache, dbFName:%s, tbName:%s, tbType:%d", meta->suid, dbFName, tbName,
           meta->tableType);
D
dapan1121 已提交
1641

D
dapan1121 已提交
1642 1643 1644
  if (pCache) {
    CTG_ERR_RET(ctgUpdateRentStbVersion(pCtg, dbFName, tbName, dbId, meta->suid, pCache));
  }
dengyihao's avatar
dengyihao 已提交
1645

D
dapan1121 已提交
1646 1647 1648
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
1649
int32_t ctgWriteTbIndexToCache(SCatalog *pCtg, SCtgDBCache *dbCache, char *dbFName, char *tbName, STableIndex **index) {
D
dapan1121 已提交
1650
  if (NULL == dbCache->tbCache) {
D
dapan1121 已提交
1651
    ctgFreeSTableIndex(*index);
D
dapan1121 已提交
1652
    taosMemoryFreeClear(*index);
dengyihao's avatar
dengyihao 已提交
1653
    ctgError("db is dropping, dbId:0x%" PRIx64, dbCache->dbId);
D
dapan1121 已提交
1654 1655 1656
    CTG_ERR_RET(TSDB_CODE_CTG_DB_DROPPED);
  }

dengyihao's avatar
dengyihao 已提交
1657 1658 1659
  STableIndex *pIndex = *index;
  uint64_t     suid = pIndex->suid;
  SCtgTbCache *pCache = taosHashGet(dbCache->tbCache, tbName, strlen(tbName));
D
dapan1121 已提交
1660 1661 1662
  if (NULL == pCache) {
    SCtgTbCache cache = {0};
    cache.pIndex = pIndex;
dengyihao's avatar
dengyihao 已提交
1663

D
dapan1121 已提交
1664 1665
    if (taosHashPut(dbCache->tbCache, tbName, strlen(tbName), &cache, sizeof(cache)) != 0) {
      ctgFreeSTableIndex(*index);
D
dapan1121 已提交
1666 1667
      taosMemoryFreeClear(*index);
      ctgError("taosHashPut new tbCache failed, tbName:%s", tbName);
D
dapan1121 已提交
1668
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1669 1670
    }

D
dapan1121 已提交
1671 1672
    atomic_add_fetch_64(&dbCache->dbCacheSize, strlen(tbName) + sizeof(SCtgTbCache) + ctgGetTbIndexCacheSize(pIndex));

D
dapan1121 已提交
1673
    CTG_DB_NUM_INC(CTG_CI_TBL_SMA);
X
Xiaoyu Wang 已提交
1674

D
dapan1121 已提交
1675
    *index = NULL;
dengyihao's avatar
dengyihao 已提交
1676 1677
    ctgDebug("table %s index updated to cache, ver:%d, num:%d", tbName, pIndex->version,
             (int32_t)taosArrayGetSize(pIndex->pIndex));
D
dapan1121 已提交
1678

D
dapan1121 已提交
1679
    if (suid) {
D
dapan1121 已提交
1680
      CTG_ERR_RET(ctgUpdateRentStbVersion(pCtg, dbFName, tbName, dbCache->dbId, pIndex->suid, &cache));
D
dapan1121 已提交
1681
    }
dengyihao's avatar
dengyihao 已提交
1682

D
dapan1121 已提交
1683 1684 1685
    return TSDB_CODE_SUCCESS;
  }

1686 1687
  CTG_LOCK(CTG_WRITE, &pCache->indexLock);

D
dapan1121 已提交
1688
  if (pCache->pIndex) {
D
dapan1121 已提交
1689
    atomic_sub_fetch_64(&dbCache->dbCacheSize, ctgGetTbIndexCacheSize(pCache->pIndex));
D
dapan1121 已提交
1690 1691 1692
    if (0 == suid) {
      suid = pCache->pIndex->suid;
    }
D
dapan1121 已提交
1693 1694 1695 1696 1697
    taosArrayDestroyEx(pCache->pIndex->pIndex, tFreeSTableIndexInfo);
    taosMemoryFreeClear(pCache->pIndex);
  }

  pCache->pIndex = pIndex;
1698 1699
  CTG_UNLOCK(CTG_WRITE, &pCache->indexLock);

D
dapan1121 已提交
1700 1701
  atomic_add_fetch_64(&dbCache->dbCacheSize, ctgGetTbIndexCacheSize(pIndex));

D
dapan1121 已提交
1702 1703
  *index = NULL;

dengyihao's avatar
dengyihao 已提交
1704 1705
  ctgDebug("table %s index updated to cache, ver:%d, num:%d", tbName, pIndex->version,
           (int32_t)taosArrayGetSize(pIndex->pIndex));
D
dapan1121 已提交
1706

D
dapan1121 已提交
1707 1708 1709
  if (suid) {
    CTG_ERR_RET(ctgUpdateRentStbVersion(pCtg, dbFName, tbName, dbCache->dbId, suid, pCache));
  }
dengyihao's avatar
dengyihao 已提交
1710

D
dapan1121 已提交
1711 1712 1713
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
1714 1715 1716 1717
int32_t ctgUpdateTbMetaToCache(SCatalog *pCtg, STableMetaOutput *pOut, bool syncReq) {
  STableMetaOutput *pOutput = NULL;
  int32_t           code = 0;

D
dapan1121 已提交
1718
  CTG_ERR_RET(ctgCloneMetaOutput(pOut, &pOutput));
D
dapan1121 已提交
1719 1720 1721
  code = ctgUpdateTbMetaEnqueue(pCtg, pOutput, syncReq);
  pOutput = NULL;
  CTG_ERR_JRET(code);
D
dapan1121 已提交
1722 1723

  return TSDB_CODE_SUCCESS;
dengyihao's avatar
dengyihao 已提交
1724

D
dapan1121 已提交
1725 1726 1727 1728 1729 1730
_return:

  ctgFreeSTableMetaOutput(pOutput);
  CTG_RET(code);
}

D
dapan1121 已提交
1731
void ctgClearAllHandles(void) {
dengyihao's avatar
dengyihao 已提交
1732
  SCatalog *pCtg = NULL;
1733

dengyihao's avatar
dengyihao 已提交
1734
  void *pIter = taosHashIterate(gCtgMgmt.pCluster, NULL);
1735
  while (pIter) {
dengyihao's avatar
dengyihao 已提交
1736
    pCtg = *(SCatalog **)pIter;
1737 1738

    if (pCtg) {
D
dapan1121 已提交
1739 1740 1741 1742 1743 1744 1745
      ctgClearHandle(pCtg);
    }

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

D
dapan1121 已提交
1746
void ctgFreeAllHandles(void) {
dengyihao's avatar
dengyihao 已提交
1747
  SCatalog *pCtg = NULL;
D
dapan1121 已提交
1748

dengyihao's avatar
dengyihao 已提交
1749
  void *pIter = taosHashIterate(gCtgMgmt.pCluster, NULL);
D
dapan1121 已提交
1750
  while (pIter) {
dengyihao's avatar
dengyihao 已提交
1751
    pCtg = *(SCatalog **)pIter;
D
dapan1121 已提交
1752 1753 1754

    if (pCtg) {
      ctgFreeHandle(pCtg);
1755 1756 1757 1758 1759 1760 1761 1762
    }

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

  taosHashClear(gCtgMgmt.pCluster);
}

X
Xiaoyu Wang 已提交
1763 1764 1765
int32_t ctgVgInfoIdComp(void const *lp, void const *rp) {
  int32_t     *key = (int32_t *)lp;
  SVgroupInfo *pVg = (SVgroupInfo *)rp;
1766 1767 1768 1769 1770 1771 1772 1773 1774 1775

  if (*key < pVg->vgId) {
    return -1;
  } else if (*key > pVg->vgId) {
    return 1;
  }

  return 0;
}

D
dapan1121 已提交
1776
int32_t ctgOpUpdateVgroup(SCtgCacheOperation *operation) {
dengyihao's avatar
dengyihao 已提交
1777
  int32_t          code = 0;
D
dapan1121 已提交
1778
  SCtgUpdateVgMsg *msg = operation->data;
dengyihao's avatar
dengyihao 已提交
1779 1780 1781 1782
  SDBVgInfo       *dbInfo = msg->dbInfo;
  char            *dbFName = msg->dbFName;
  SCatalog        *pCtg = msg->pCtg;

D
dapan1121 已提交
1783
  if (pCtg->stopUpdate || NULL == dbInfo->vgHash) {
D
dapan1121 已提交
1784
    goto _return;
D
dapan1121 已提交
1785
  }
dengyihao's avatar
dengyihao 已提交
1786

D
dapan1121 已提交
1787
  if (dbInfo->vgVersion < 0 || (taosHashGetSize(dbInfo->vgHash) <= 0 && !IS_SYS_DBNAME(dbFName))) {
D
dapan1121 已提交
1788
    ctgDebug("invalid db vgInfo, dbFName:%s, vgHash:%p, vgVersion:%d, vgHashSize:%d", dbFName, dbInfo->vgHash,
dengyihao's avatar
dengyihao 已提交
1789
             dbInfo->vgVersion, taosHashGetSize(dbInfo->vgHash));
D
dapan1121 已提交
1790
    CTG_ERR_JRET(TSDB_CODE_APP_ERROR);
D
dapan1121 已提交
1791 1792
  }

dengyihao's avatar
dengyihao 已提交
1793
  bool         newAdded = false;
dengyihao's avatar
dengyihao 已提交
1794 1795
  SDbVgVersion vgVersion = {
      .dbId = msg->dbId, .vgVersion = dbInfo->vgVersion, .numOfTable = dbInfo->numOfTable, .stateTs = dbInfo->stateTs};
D
dapan1121 已提交
1796 1797

  SCtgDBCache *dbCache = NULL;
D
dapan1121 已提交
1798
  CTG_ERR_JRET(ctgGetAddDBCache(msg->pCtg, dbFName, msg->dbId, &dbCache));
D
dapan1121 已提交
1799
  if (NULL == dbCache) {
dengyihao's avatar
dengyihao 已提交
1800
    ctgInfo("conflict db update, ignore this update, dbFName:%s, dbId:0x%" PRIx64, dbFName, msg->dbId);
D
dapan1121 已提交
1801
    CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
D
dapan1121 已提交
1802 1803 1804
  }

  SCtgVgCache *vgCache = &dbCache->vgCache;
D
dapan1121 已提交
1805
  
D
dapan1121 已提交
1806
  CTG_ERR_JRET(ctgWLockVgInfo(msg->pCtg, dbCache));
dengyihao's avatar
dengyihao 已提交
1807

D
dapan1121 已提交
1808 1809
  if (vgCache->vgInfo) {
    SDBVgInfo *vgInfo = vgCache->vgInfo;
dengyihao's avatar
dengyihao 已提交
1810

D
dapan1121 已提交
1811
    if (dbInfo->vgVersion < vgInfo->vgVersion) {
dengyihao's avatar
dengyihao 已提交
1812 1813
      ctgDebug("db updateVgroup is ignored, dbFName:%s, vgVer:%d, curVer:%d", dbFName, dbInfo->vgVersion,
               vgInfo->vgVersion);
D
dapan1121 已提交
1814
      ctgWUnlockVgInfo(dbCache);
dengyihao's avatar
dengyihao 已提交
1815

D
dapan1121 已提交
1816
      goto _return;
D
dapan1121 已提交
1817 1818
    }

dengyihao's avatar
dengyihao 已提交
1819 1820 1821 1822
    if (dbInfo->vgVersion == vgInfo->vgVersion && dbInfo->numOfTable == vgInfo->numOfTable &&
        dbInfo->stateTs == vgInfo->stateTs) {
      ctgDebug("no new db vgroup update info, dbFName:%s, vgVer:%d, numOfTable:%d, stateTs:%" PRId64, dbFName,
               dbInfo->vgVersion, dbInfo->numOfTable, dbInfo->stateTs);
D
dapan1121 已提交
1823
      ctgWUnlockVgInfo(dbCache);
dengyihao's avatar
dengyihao 已提交
1824

D
dapan1121 已提交
1825
      goto _return;
D
dapan1121 已提交
1826 1827
    }

D
dapan1121 已提交
1828 1829 1830 1831
    uint64_t groupCacheSize = ctgGetDbVgroupCacheSize(vgCache->vgInfo);
    ctgDebug("sub dbGroupCacheSize %" PRIu64 " from db, dbFName:%s", groupCacheSize, dbFName);

    atomic_sub_fetch_64(&dbCache->dbCacheSize, groupCacheSize);
D
dapan1121 已提交
1832
    
1833
    freeVgInfo(vgInfo);
D
dapan1121 已提交
1834
    CTG_DB_NUM_RESET(CTG_CI_DB_VGROUP);
D
dapan1121 已提交
1835 1836 1837 1838
  }

  vgCache->vgInfo = dbInfo;
  msg->dbInfo = NULL;
D
dapan1121 已提交
1839
  CTG_DB_NUM_SET(CTG_CI_DB_VGROUP);
D
dapan1121 已提交
1840

dengyihao's avatar
dengyihao 已提交
1841 1842
  ctgDebug("db vgInfo updated, dbFName:%s, vgVer:%d, stateTs:%" PRId64 ", dbId:0x%" PRIx64, dbFName,
           vgVersion.vgVersion, vgVersion.stateTs, vgVersion.dbId);
D
dapan1121 已提交
1843 1844 1845

  ctgWUnlockVgInfo(dbCache);

D
dapan1121 已提交
1846 1847 1848
  uint64_t groupCacheSize = ctgGetDbVgroupCacheSize(vgCache->vgInfo);
  atomic_add_fetch_64(&dbCache->dbCacheSize, groupCacheSize);
  ctgDebug("add dbGroupCacheSize %" PRIu64 " from db, dbFName:%s", groupCacheSize, dbFName);
D
dapan1121 已提交
1849

D
dapan1121 已提交
1850 1851
  dbCache = NULL;

X
Xiaoyu Wang 已提交
1852 1853 1854 1855
  // if (!IS_SYS_DBNAME(dbFName)) {
  tstrncpy(vgVersion.dbFName, dbFName, sizeof(vgVersion.dbFName));
  CTG_ERR_JRET(ctgMetaRentUpdate(&msg->pCtg->dbRent, &vgVersion, vgVersion.dbId, sizeof(SDbVgVersion),
                                 ctgDbVgVersionSortCompare, ctgDbVgVersionSearchCompare));
1856
  //}
D
dapan1121 已提交
1857 1858 1859

_return:

1860
  freeVgInfo(msg->dbInfo);
D
dapan1121 已提交
1861
  taosMemoryFreeClear(msg);
dengyihao's avatar
dengyihao 已提交
1862

D
dapan1121 已提交
1863 1864 1865
  CTG_RET(code);
}

D
dapan1121 已提交
1866
int32_t ctgOpDropDbCache(SCtgCacheOperation *operation) {
dengyihao's avatar
dengyihao 已提交
1867
  int32_t        code = 0;
D
dapan1121 已提交
1868
  SCtgDropDBMsg *msg = operation->data;
dengyihao's avatar
dengyihao 已提交
1869
  SCatalog      *pCtg = msg->pCtg;
D
dapan1121 已提交
1870

D
dapan1121 已提交
1871 1872 1873 1874
  if (pCtg->stopUpdate) {
    goto _return;
  }

D
dapan1121 已提交
1875 1876 1877 1878 1879
  SCtgDBCache *dbCache = NULL;
  ctgGetDBCache(msg->pCtg, msg->dbFName, &dbCache);
  if (NULL == dbCache) {
    goto _return;
  }
dengyihao's avatar
dengyihao 已提交
1880

1881
  if (msg->dbId && dbCache->dbId != msg->dbId) {
dengyihao's avatar
dengyihao 已提交
1882 1883
    ctgInfo("dbId already updated, dbFName:%s, dbId:0x%" PRIx64 ", targetId:0x%" PRIx64, msg->dbFName, dbCache->dbId,
            msg->dbId);
D
dapan1121 已提交
1884 1885
    goto _return;
  }
dengyihao's avatar
dengyihao 已提交
1886

D
dapan1121 已提交
1887 1888 1889 1890 1891
  CTG_ERR_JRET(ctgRemoveDBFromCache(pCtg, dbCache, msg->dbFName));

_return:

  taosMemoryFreeClear(msg);
dengyihao's avatar
dengyihao 已提交
1892

D
dapan1121 已提交
1893 1894 1895
  CTG_RET(code);
}

D
dapan1121 已提交
1896
int32_t ctgOpDropDbVgroup(SCtgCacheOperation *operation) {
dengyihao's avatar
dengyihao 已提交
1897
  int32_t              code = 0;
D
dapan1121 已提交
1898
  SCtgDropDbVgroupMsg *msg = operation->data;
dengyihao's avatar
dengyihao 已提交
1899
  SCatalog            *pCtg = msg->pCtg;
D
dapan1121 已提交
1900

D
dapan1121 已提交
1901 1902 1903 1904
  if (pCtg->stopUpdate) {
    goto _return;
  }

D
dapan1121 已提交
1905 1906 1907 1908 1909
  SCtgDBCache *dbCache = NULL;
  ctgGetDBCache(msg->pCtg, msg->dbFName, &dbCache);
  if (NULL == dbCache) {
    goto _return;
  }
dengyihao's avatar
dengyihao 已提交
1910

dengyihao's avatar
dengyihao 已提交
1911
  CTG_ERR_JRET(ctgWLockVgInfo(pCtg, dbCache));
dengyihao's avatar
dengyihao 已提交
1912

D
dapan1121 已提交
1913 1914
  atomic_sub_fetch_64(&dbCache->dbCacheSize, ctgGetDbVgroupCacheSize(dbCache->vgCache.vgInfo));

1915
  freeVgInfo(dbCache->vgCache.vgInfo);
D
dapan1121 已提交
1916
  dbCache->vgCache.vgInfo = NULL;
D
dapan1121 已提交
1917

D
dapan1121 已提交
1918
  CTG_DB_NUM_RESET(CTG_CI_DB_VGROUP);
D
dapan1121 已提交
1919 1920
  ctgDebug("db vgInfo removed, dbFName:%s", msg->dbFName);

D
dapan1121 已提交
1921
  ctgWUnlockVgInfo(dbCache);
D
dapan1121 已提交
1922 1923 1924 1925

_return:

  taosMemoryFreeClear(msg);
dengyihao's avatar
dengyihao 已提交
1926

D
dapan1121 已提交
1927 1928 1929
  CTG_RET(code);
}

D
dapan1121 已提交
1930
int32_t ctgOpUpdateTbMeta(SCtgCacheOperation *operation) {
dengyihao's avatar
dengyihao 已提交
1931
  int32_t              code = 0;
D
dapan1121 已提交
1932
  SCtgUpdateTbMetaMsg *msg = operation->data;
dengyihao's avatar
dengyihao 已提交
1933 1934 1935
  SCatalog            *pCtg = msg->pCtg;
  STableMetaOutput    *pMeta = msg->pMeta;
  SCtgDBCache         *dbCache = NULL;
D
dapan1121 已提交
1936

D
dapan1121 已提交
1937 1938 1939
  if (pCtg->stopUpdate) {
    goto _return;
  }
dengyihao's avatar
dengyihao 已提交
1940

D
dapan1121 已提交
1941 1942
  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 已提交
1943 1944 1945
    CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
  }

D
dapan1121 已提交
1946 1947
  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 已提交
1948
    CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
dengyihao's avatar
dengyihao 已提交
1949 1950
  }

D
dapan1121 已提交
1951
  CTG_ERR_JRET(ctgGetAddDBCache(pCtg, pMeta->dbFName, pMeta->dbId, &dbCache));
D
dapan1121 已提交
1952
  if (NULL == dbCache) {
D
dapan1121 已提交
1953
    ctgInfo("conflict db update, ignore this update, dbFName:%s, dbId:0x%" PRIx64, pMeta->dbFName, pMeta->dbId);
D
dapan1121 已提交
1954 1955 1956
    CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
  }

D
dapan1121 已提交
1957 1958
  if (CTG_IS_META_TABLE(pMeta->metaType) || CTG_IS_META_BOTH(pMeta->metaType)) {
    int32_t metaSize = CTG_META_SIZE(pMeta->tbMeta);
D
dapan1121 已提交
1959
    code = ctgWriteTbMetaToCache(pCtg, dbCache, pMeta->dbFName, pMeta->dbId, pMeta->tbName, pMeta->tbMeta, metaSize);
D
dapan1121 已提交
1960
    pMeta->tbMeta = NULL;
D
dapan1121 已提交
1961
    CTG_ERR_JRET(code);
D
dapan1121 已提交
1962 1963
  }

D
dapan1121 已提交
1964
  if (CTG_IS_META_CTABLE(pMeta->metaType) || CTG_IS_META_BOTH(pMeta->metaType)) {
dengyihao's avatar
dengyihao 已提交
1965
    SCTableMeta *ctbMeta = taosMemoryMalloc(sizeof(SCTableMeta));
D
dapan1121 已提交
1966 1967 1968 1969
    if (NULL == ctbMeta) {
      CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
    }
    memcpy(ctbMeta, &pMeta->ctbMeta, sizeof(SCTableMeta));
dengyihao's avatar
dengyihao 已提交
1970 1971
    CTG_ERR_JRET(ctgWriteTbMetaToCache(pCtg, dbCache, pMeta->dbFName, pMeta->dbId, pMeta->ctbName,
                                       (STableMeta *)ctbMeta, sizeof(SCTableMeta)));
D
dapan1121 已提交
1972 1973 1974 1975
  }

_return:

D
dapan1121 已提交
1976 1977
  taosMemoryFreeClear(pMeta->tbMeta);
  taosMemoryFreeClear(pMeta);
dengyihao's avatar
dengyihao 已提交
1978

D
dapan1121 已提交
1979
  taosMemoryFreeClear(msg);
dengyihao's avatar
dengyihao 已提交
1980

D
dapan1121 已提交
1981 1982 1983
  CTG_RET(code);
}

D
dapan1121 已提交
1984
int32_t ctgOpDropStbMeta(SCtgCacheOperation *operation) {
dengyihao's avatar
dengyihao 已提交
1985
  int32_t             code = 0;
D
dapan1121 已提交
1986
  SCtgDropStbMetaMsg *msg = operation->data;
dengyihao's avatar
dengyihao 已提交
1987
  SCatalog           *pCtg = msg->pCtg;
D
dapan1121 已提交
1988
  int32_t             tblType = 0;
D
dapan1121 已提交
1989

D
dapan1121 已提交
1990 1991 1992 1993
  if (pCtg->stopUpdate) {
    goto _return;
  }

D
dapan1121 已提交
1994 1995 1996
  SCtgDBCache *dbCache = NULL;
  ctgGetDBCache(pCtg, msg->dbFName, &dbCache);
  if (NULL == dbCache) {
dengyihao's avatar
dengyihao 已提交
1997
    goto _return;
D
dapan1121 已提交
1998 1999 2000
  }

  if (msg->dbId && (dbCache->dbId != msg->dbId)) {
dengyihao's avatar
dengyihao 已提交
2001
    ctgDebug("dbId already modified, dbFName:%s, current:0x%" PRIx64 ", dbId:0x%" PRIx64 ", stb:%s, suid:0x%" PRIx64,
D
dapan1121 已提交
2002
             msg->dbFName, dbCache->dbId, msg->dbId, msg->stbName, msg->suid);
dengyihao's avatar
dengyihao 已提交
2003
    goto _return;
D
dapan1121 已提交
2004
  }
dengyihao's avatar
dengyihao 已提交
2005

D
dapan1121 已提交
2006 2007 2008 2009 2010 2011 2012 2013 2014
  char *stbName = taosHashGet(dbCache->stbCache, &msg->suid, sizeof(msg->suid));
  if (stbName) {
    uint64_t metaSize = strlen(stbName) + 1 + sizeof(msg->suid);
    if (taosHashRemove(dbCache->stbCache, &msg->suid, sizeof(msg->suid))) {
      ctgDebug("stb not exist in stbCache, may be removed, dbFName:%s, stb:%s, suid:0x%" PRIx64, msg->dbFName,
               msg->stbName, msg->suid);
    } else {
      atomic_sub_fetch_64(&dbCache->dbCacheSize, metaSize);
    }
D
dapan1121 已提交
2015
  }
D
dapan1121 已提交
2016
  
dengyihao's avatar
dengyihao 已提交
2017
  SCtgTbCache *pTbCache = taosHashGet(dbCache->tbCache, msg->stbName, strlen(msg->stbName));
D
dapan1121 已提交
2018 2019 2020 2021 2022
  if (NULL == pTbCache) {
    ctgDebug("stb %s already not in cache", msg->stbName);
    goto _return;
  }

D
dapan1121 已提交
2023
  tblType = pTbCache->pMeta->tableType;
D
dapan1121 已提交
2024 2025
  atomic_sub_fetch_64(&dbCache->dbCacheSize, ctgGetTbMetaCacheSize(pTbCache->pMeta) + ctgGetTbIndexCacheSize(pTbCache->pIndex));
  ctgFreeTbCacheImpl(pTbCache, true);
D
dapan1121 已提交
2026

dengyihao's avatar
dengyihao 已提交
2027 2028
  if (taosHashRemove(dbCache->tbCache, msg->stbName, strlen(msg->stbName))) {
    ctgError("stb not exist in cache, dbFName:%s, stb:%s, suid:0x%" PRIx64, msg->dbFName, msg->stbName, msg->suid);
D
dapan1121 已提交
2029
  } else {
D
dapan1121 已提交
2030
    CTG_META_NUM_DEC(tblType);
D
dapan1121 已提交
2031
    atomic_sub_fetch_64(&dbCache->dbCacheSize, sizeof(*pTbCache) + strlen(msg->stbName));
D
dapan1121 已提交
2032
  }
dengyihao's avatar
dengyihao 已提交
2033 2034

  ctgInfo("stb removed from cache, dbFName:%s, stbName:%s, suid:0x%" PRIx64, msg->dbFName, msg->stbName, msg->suid);
D
dapan1121 已提交
2035 2036

  CTG_ERR_JRET(ctgMetaRentRemove(&msg->pCtg->stbRent, msg->suid, ctgStbVersionSortCompare, ctgStbVersionSearchCompare));
dengyihao's avatar
dengyihao 已提交
2037 2038 2039

  ctgDebug("stb removed from rent, dbFName:%s, stbName:%s, suid:0x%" PRIx64, msg->dbFName, msg->stbName, msg->suid);

D
dapan1121 已提交
2040 2041 2042
_return:

  taosMemoryFreeClear(msg);
dengyihao's avatar
dengyihao 已提交
2043

D
dapan1121 已提交
2044 2045 2046
  CTG_RET(code);
}

D
dapan1121 已提交
2047
int32_t ctgOpDropTbMeta(SCtgCacheOperation *operation) {
dengyihao's avatar
dengyihao 已提交
2048
  int32_t             code = 0;
D
dapan1121 已提交
2049
  SCtgDropTblMetaMsg *msg = operation->data;
dengyihao's avatar
dengyihao 已提交
2050
  SCatalog           *pCtg = msg->pCtg;
D
dapan1121 已提交
2051
  int32_t             tblType = 0;
D
dapan1121 已提交
2052

D
dapan1121 已提交
2053 2054 2055 2056
  if (pCtg->stopUpdate) {
    goto _return;
  }

D
dapan1121 已提交
2057 2058 2059
  SCtgDBCache *dbCache = NULL;
  ctgGetDBCache(pCtg, msg->dbFName, &dbCache);
  if (NULL == dbCache) {
D
dapan1121 已提交
2060
    goto _return;
D
dapan1121 已提交
2061 2062 2063
  }

  if (dbCache->dbId != msg->dbId) {
dengyihao's avatar
dengyihao 已提交
2064 2065
    ctgDebug("dbId 0x%" PRIx64 " not match with curId 0x%" PRIx64 ", dbFName:%s, tbName:%s", msg->dbId, dbCache->dbId,
             msg->dbFName, msg->tbName);
D
dapan1121 已提交
2066
    goto _return;
D
dapan1121 已提交
2067 2068
  }

dengyihao's avatar
dengyihao 已提交
2069
  SCtgTbCache *pTbCache = taosHashGet(dbCache->tbCache, msg->tbName, strlen(msg->tbName));
D
dapan1121 已提交
2070 2071 2072 2073 2074
  if (NULL == pTbCache) {
    ctgDebug("tb %s already not in cache", msg->tbName);
    goto _return;
  }

D
dapan1121 已提交
2075
  tblType = pTbCache->pMeta->tableType;
D
dapan1121 已提交
2076 2077
  atomic_sub_fetch_64(&dbCache->dbCacheSize, ctgGetTbMetaCacheSize(pTbCache->pMeta) + ctgGetTbIndexCacheSize(pTbCache->pIndex));
  ctgFreeTbCacheImpl(pTbCache, true);
dengyihao's avatar
dengyihao 已提交
2078

D
dapan1121 已提交
2079 2080 2081
  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 已提交
2082
  } else {
D
dapan1121 已提交
2083
    atomic_sub_fetch_64(&dbCache->dbCacheSize, sizeof(*pTbCache) + strlen(msg->tbName));
D
dapan1121 已提交
2084
    CTG_META_NUM_DEC(tblType);
D
dapan1121 已提交
2085 2086
  }

D
dapan1121 已提交
2087
  ctgDebug("table %s removed from cache, dbFName:%s", msg->tbName, msg->dbFName);
D
dapan1121 已提交
2088 2089 2090 2091 2092 2093 2094 2095

_return:

  taosMemoryFreeClear(msg);

  CTG_RET(code);
}

D
dapan1121 已提交
2096
int32_t ctgOpUpdateUser(SCtgCacheOperation *operation) {
dengyihao's avatar
dengyihao 已提交
2097
  int32_t            code = 0;
D
dapan1121 已提交
2098
  SCtgUpdateUserMsg *msg = operation->data;
dengyihao's avatar
dengyihao 已提交
2099 2100
  SCatalog          *pCtg = msg->pCtg;

D
dapan1121 已提交
2101 2102 2103 2104
  if (pCtg->stopUpdate) {
    goto _return;
  }

D
dapan1121 已提交
2105 2106 2107 2108
  SCtgUserAuth *pUser = (SCtgUserAuth *)taosHashGet(pCtg->userCache, msg->userAuth.user, strlen(msg->userAuth.user));
  if (NULL == pUser) {
    SCtgUserAuth userAuth = {0};

D
dapan1121 已提交
2109
    memcpy(&userAuth.userAuth, &msg->userAuth, sizeof(msg->userAuth));
D
dapan1121 已提交
2110 2111
    userAuth.userCacheSize = ctgGetUserCacheSize(&userAuth.userAuth);
    
D
dapan1121 已提交
2112 2113 2114 2115 2116 2117 2118
    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);

D
dapan1121 已提交
2119 2120
    CTG_CACHE_NUM_INC(CTG_CI_USER, 1);

D
dapan1121 已提交
2121 2122 2123 2124 2125
    return TSDB_CODE_SUCCESS;
  }

  CTG_LOCK(CTG_WRITE, &pUser->lock);

D
dapan1121 已提交
2126 2127
  taosHashCleanup(pUser->userAuth.createdDbs);
  pUser->userAuth.createdDbs = msg->userAuth.createdDbs;
D
dapan1121 已提交
2128 2129
  msg->userAuth.createdDbs = NULL;

D
dapan1121 已提交
2130 2131
  taosHashCleanup(pUser->userAuth.readDbs);
  pUser->userAuth.readDbs = msg->userAuth.readDbs;
D
dapan1121 已提交
2132 2133
  msg->userAuth.readDbs = NULL;

D
dapan1121 已提交
2134 2135
  taosHashCleanup(pUser->userAuth.writeDbs);
  pUser->userAuth.writeDbs = msg->userAuth.writeDbs;
D
dapan1121 已提交
2136 2137
  msg->userAuth.writeDbs = NULL;

X
Xiaoyu Wang 已提交
2138 2139 2140 2141 2142 2143 2144 2145
  taosHashCleanup(pUser->userAuth.readTbs);
  pUser->userAuth.readTbs = msg->userAuth.readTbs;
  msg->userAuth.readTbs = NULL;

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

X
Xiaoyu Wang 已提交
2146 2147 2148 2149
  taosHashCleanup(pUser->userAuth.useDbs);
  pUser->userAuth.useDbs = msg->userAuth.useDbs;
  msg->userAuth.useDbs = NULL;

D
dapan1121 已提交
2150
  CTG_UNLOCK(CTG_WRITE, &pUser->lock);
D
dapan1121 已提交
2151 2152
  
  atomic_store_64(&pUser->userCacheSize, ctgGetUserCacheSize(&pUser->userAuth));
D
dapan1121 已提交
2153 2154 2155 2156 2157 2158

_return:

  taosHashCleanup(msg->userAuth.createdDbs);
  taosHashCleanup(msg->userAuth.readDbs);
  taosHashCleanup(msg->userAuth.writeDbs);
X
Xiaoyu Wang 已提交
2159 2160
  taosHashCleanup(msg->userAuth.readTbs);
  taosHashCleanup(msg->userAuth.writeTbs);
X
Xiaoyu Wang 已提交
2161
  taosHashCleanup(msg->userAuth.useDbs);
dengyihao's avatar
dengyihao 已提交
2162

D
dapan1121 已提交
2163
  taosMemoryFreeClear(msg);
dengyihao's avatar
dengyihao 已提交
2164

D
dapan1121 已提交
2165 2166 2167
  CTG_RET(code);
}

D
dapan1121 已提交
2168
int32_t ctgOpUpdateEpset(SCtgCacheOperation *operation) {
dengyihao's avatar
dengyihao 已提交
2169
  int32_t             code = 0;
D
dapan1121 已提交
2170
  SCtgUpdateEpsetMsg *msg = operation->data;
dengyihao's avatar
dengyihao 已提交
2171
  SCatalog           *pCtg = msg->pCtg;
dengyihao's avatar
dengyihao 已提交
2172
  SCtgDBCache        *dbCache = NULL;
D
dapan1121 已提交
2173 2174 2175 2176 2177

  if (pCtg->stopUpdate) {
    goto _return;
  }

D
dapan1121 已提交
2178
  CTG_ERR_JRET(ctgGetDBCache(pCtg, msg->dbFName, &dbCache));
D
dapan1121 已提交
2179 2180 2181 2182 2183
  if (NULL == dbCache) {
    ctgDebug("db %s not exist, ignore epset update", msg->dbFName);
    goto _return;
  }

D
dapan1121 已提交
2184 2185
  CTG_ERR_JRET(ctgWLockVgInfo(pCtg, dbCache));

dengyihao's avatar
dengyihao 已提交
2186
  SDBVgInfo *vgInfo = dbCache->vgCache.vgInfo;
D
dapan1121 已提交
2187
  if (NULL == vgInfo) {
D
dapan1121 已提交
2188 2189 2190
    ctgDebug("vgroup in db %s not cached, ignore epset update", msg->dbFName);
    goto _return;
  }
dengyihao's avatar
dengyihao 已提交
2191 2192

  SVgroupInfo *pInfo = taosHashGet(vgInfo->vgHash, &msg->vgId, sizeof(msg->vgId));
D
dapan1121 已提交
2193
  if (NULL == pInfo) {
2194 2195 2196 2197 2198 2199 2200
    ctgDebug("no vgroup %d in db %s vgHash, ignore epset update", msg->vgId, msg->dbFName);
    goto _return;
  }

  SVgroupInfo *pInfo2 = taosArraySearch(vgInfo->vgArray, &msg->vgId, ctgVgInfoIdComp, TD_EQ);
  if (NULL == pInfo2) {
    ctgDebug("no vgroup %d in db %s vgArray, ignore epset update", msg->vgId, msg->dbFName);
D
dapan1121 已提交
2201 2202 2203
    goto _return;
  }

dengyihao's avatar
dengyihao 已提交
2204 2205 2206 2207 2208
  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 已提交
2209

D
dapan1121 已提交
2210
  pInfo->epSet = msg->epSet;
2211
  pInfo2->epSet = msg->epSet;
D
dapan1121 已提交
2212 2213 2214

_return:

D
dapan1121 已提交
2215
  if (code == TSDB_CODE_SUCCESS && dbCache) {
D
dapan1121 已提交
2216
    ctgWUnlockVgInfo(dbCache);
D
dapan1121 已提交
2217 2218 2219
  }

  taosMemoryFreeClear(msg);
dengyihao's avatar
dengyihao 已提交
2220

D
dapan1121 已提交
2221 2222 2223
  CTG_RET(code);
}

D
dapan1121 已提交
2224
int32_t ctgOpUpdateTbIndex(SCtgCacheOperation *operation) {
dengyihao's avatar
dengyihao 已提交
2225
  int32_t               code = 0;
D
dapan1121 已提交
2226
  SCtgUpdateTbIndexMsg *msg = operation->data;
dengyihao's avatar
dengyihao 已提交
2227 2228 2229 2230
  SCatalog             *pCtg = msg->pCtg;
  STableIndex          *pIndex = msg->pIndex;
  SCtgDBCache          *dbCache = NULL;

D
dapan1121 已提交
2231 2232 2233 2234
  if (pCtg->stopUpdate) {
    goto _return;
  }

D
dapan1121 已提交
2235
  CTG_ERR_JRET(ctgGetAddDBCache(pCtg, pIndex->dbFName, 0, &dbCache));
D
dapan1121 已提交
2236 2237 2238 2239 2240 2241 2242 2243 2244

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

_return:

  if (pIndex) {
    taosArrayDestroyEx(pIndex->pIndex, tFreeSTableIndexInfo);
    taosMemoryFreeClear(pIndex);
  }
dengyihao's avatar
dengyihao 已提交
2245

D
dapan1121 已提交
2246
  taosMemoryFreeClear(msg);
dengyihao's avatar
dengyihao 已提交
2247

D
dapan1121 已提交
2248 2249 2250 2251
  CTG_RET(code);
}

int32_t ctgOpDropTbIndex(SCtgCacheOperation *operation) {
dengyihao's avatar
dengyihao 已提交
2252
  int32_t             code = 0;
D
dapan1121 已提交
2253
  SCtgDropTbIndexMsg *msg = operation->data;
dengyihao's avatar
dengyihao 已提交
2254 2255 2256
  SCatalog           *pCtg = msg->pCtg;
  SCtgDBCache        *dbCache = NULL;

D
dapan1121 已提交
2257 2258 2259 2260
  if (pCtg->stopUpdate) {
    goto _return;
  }

D
dapan1121 已提交
2261
  CTG_ERR_JRET(ctgGetDBCache(pCtg, msg->dbFName, &dbCache));
D
dapan1121 已提交
2262
  if (NULL == dbCache) {
D
dapan1121 已提交
2263 2264 2265
    return TSDB_CODE_SUCCESS;
  }

dengyihao's avatar
dengyihao 已提交
2266
  STableIndex *pIndex = taosMemoryCalloc(1, sizeof(STableIndex));
D
dapan1121 已提交
2267 2268
  if (NULL == pIndex) {
    CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
2269
  }
D
dapan1121 已提交
2270 2271 2272
  strcpy(pIndex->tbName, msg->tbName);
  strcpy(pIndex->dbFName, msg->dbFName);
  pIndex->version = -1;
D
dapan1121 已提交
2273

D
dapan1121 已提交
2274
  CTG_ERR_JRET(ctgWriteTbIndexToCache(pCtg, dbCache, pIndex->dbFName, pIndex->tbName, &pIndex));
D
dapan1121 已提交
2275 2276 2277 2278 2279 2280 2281

_return:

  if (pIndex) {
    taosArrayDestroyEx(pIndex->pIndex, tFreeSTableIndexInfo);
    taosMemoryFreeClear(pIndex);
  }
dengyihao's avatar
dengyihao 已提交
2282

D
dapan1121 已提交
2283
  taosMemoryFreeClear(msg);
dengyihao's avatar
dengyihao 已提交
2284

D
dapan1121 已提交
2285 2286 2287
  CTG_RET(code);
}

D
dapan1121 已提交
2288
void ctgClearFreeCache(SCtgCacheOperation *operation) {
D
dapan1121 已提交
2289
  SCtgClearCacheMsg *msg = operation->data;
dengyihao's avatar
dengyihao 已提交
2290
  SCatalog          *pCtg = msg->pCtg;
D
dapan1121 已提交
2291

D
dapan1121 已提交
2292
  CTG_LOCK(CTG_WRITE, &gCtgMgmt.lock);
D
dapan1121 已提交
2293
  
D
dapan1121 已提交
2294
  if (pCtg) {
D
dapan1121 已提交
2295 2296 2297 2298 2299
    if (msg->freeCtg) {
      ctgFreeHandle(pCtg);
    } else {
      ctgClearHandle(pCtg);
    }
D
dapan1121 已提交
2300 2301 2302 2303 2304 2305 2306 2307
  } else if (msg->freeCtg) {
    ctgFreeAllHandles();
  } else {
    ctgClearAllHandles();
  }
  
  CTG_UNLOCK(CTG_WRITE, &gCtgMgmt.lock);
}
dengyihao's avatar
dengyihao 已提交
2308

D
dapan1121 已提交
2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319
void ctgClearMetaCache(SCtgCacheOperation *operation) {
  SCtgClearCacheMsg *msg = operation->data;
  SCatalog          *pCtg = msg->pCtg;
  int64_t            clearedSize = 0;
  int64_t            clearedNum = 0;
  int64_t            remainSize = 0;
  bool               roundDone = false;

  if (pCtg) {
    ctgClearHandleMeta(pCtg, &clearedSize, &clearedNum, &roundDone);
  } else {
D
dapan1121 已提交
2320
    ctgClearAllHandleMeta(&clearedSize, &clearedNum, &roundDone);
D
dapan1121 已提交
2321
  }
D
dapan1121 已提交
2322

D
dapan1121 已提交
2323
  qDebug("catalog finish one round meta clear, clearedSize:%" PRId64 ", clearedNum:%" PRId64 ", done:%d", clearedSize, clearedNum, roundDone);
D
dapan1121 已提交
2324 2325 2326 2327

  ctgGetGlobalCacheSize(&remainSize);
  int32_t cacheMaxSize = atomic_load_32(&tsMetaCacheMaxSize);
  
D
dapan1121 已提交
2328
  if (CTG_CACHE_LOW(remainSize, cacheMaxSize)) {
D
dapan1121 已提交
2329 2330 2331 2332 2333 2334
    qDebug("catalog finish meta clear, remainSize:%" PRId64 ", cacheMaxSize:%dMB", remainSize, cacheMaxSize);
    taosTmrReset(ctgProcessTimerEvent, CTG_DEFAULT_CACHE_MON_MSEC, NULL, gCtgMgmt.timer, &gCtgMgmt.cacheTimer);
    return;
  }

  if (!roundDone) {
D
dapan1121 已提交
2335
    qDebug("catalog all meta cleared, remainSize:%" PRId64 ", cacheMaxSize:%dMB, to clear handle", remainSize, cacheMaxSize);
D
dapan1121 已提交
2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353
    ctgClearFreeCache(operation);
    taosTmrReset(ctgProcessTimerEvent, CTG_DEFAULT_CACHE_MON_MSEC, NULL, gCtgMgmt.timer, &gCtgMgmt.cacheTimer);
    return;
  }
  
  int32_t code = ctgClearCacheEnqueue(NULL, true, false, false, false);
  if (code) {
    qError("clear cache enqueue failed, error:%s", tstrerror(code));
    taosTmrReset(ctgProcessTimerEvent, CTG_DEFAULT_CACHE_MON_MSEC, NULL, gCtgMgmt.timer, &gCtgMgmt.cacheTimer);
  }
}

int32_t ctgOpClearCache(SCtgCacheOperation *operation) {
  int32_t            code = 0;
  SCtgClearCacheMsg *msg = operation->data;

  if (msg->clearMeta) {
    ctgClearMetaCache(operation);
D
dapan1121 已提交
2354
  } else {
D
dapan1121 已提交
2355
    ctgClearFreeCache(operation);
D
dapan1121 已提交
2356
  }
D
dapan1121 已提交
2357 2358

_return:
D
dapan1121 已提交
2359

D
dapan1121 已提交
2360
  taosMemoryFreeClear(msg);
dengyihao's avatar
dengyihao 已提交
2361

D
dapan1121 已提交
2362 2363 2364
  CTG_RET(code);
}

2365 2366 2367 2368 2369 2370 2371 2372
void ctgFreeCacheOperationData(SCtgCacheOperation *op) {
  if (NULL == op || NULL == op->data) {
    return;
  }

  switch (op->opId) {
    case CTG_OP_UPDATE_VGROUP: {
      SCtgUpdateVgMsg *msg = op->data;
2373
      freeVgInfo(msg->dbInfo);
2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398
      taosMemoryFreeClear(op->data);
      break;
    }
    case CTG_OP_UPDATE_TB_META: {
      SCtgUpdateTbMetaMsg *msg = op->data;
      taosMemoryFreeClear(msg->pMeta->tbMeta);
      taosMemoryFreeClear(msg->pMeta);
      taosMemoryFreeClear(op->data);
      break;
    }
    case CTG_OP_DROP_DB_CACHE:
    case CTG_OP_DROP_DB_VGROUP:
    case CTG_OP_DROP_STB_META:
    case CTG_OP_DROP_TB_META:
    case CTG_OP_UPDATE_VG_EPSET:
    case CTG_OP_DROP_TB_INDEX:
    case CTG_OP_CLEAR_CACHE: {
      taosMemoryFreeClear(op->data);
      break;
    }
    case CTG_OP_UPDATE_USER: {
      SCtgUpdateUserMsg *msg = op->data;
      taosHashCleanup(msg->userAuth.createdDbs);
      taosHashCleanup(msg->userAuth.readDbs);
      taosHashCleanup(msg->userAuth.writeDbs);
X
Xiaoyu Wang 已提交
2399 2400
      taosHashCleanup(msg->userAuth.readTbs);
      taosHashCleanup(msg->userAuth.writeTbs);
X
Xiaoyu Wang 已提交
2401
      taosHashCleanup(msg->userAuth.useDbs);
2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420
      taosMemoryFreeClear(op->data);
      break;
    }
    case CTG_OP_UPDATE_TB_INDEX: {
      SCtgUpdateTbIndexMsg *msg = op->data;
      if (msg->pIndex) {
        taosArrayDestroyEx(msg->pIndex->pIndex, tFreeSTableIndexInfo);
        taosMemoryFreeClear(msg->pIndex);
      }
      taosMemoryFreeClear(op->data);
      break;
    }
    default: {
      qError("invalid cache op id:%d", op->opId);
      break;
    }
  }
}

D
dapan1121 已提交
2421
void ctgCleanupCacheQueue(void) {
dengyihao's avatar
dengyihao 已提交
2422 2423
  SCtgQNode          *node = NULL;
  SCtgQNode          *nodeNext = NULL;
D
dapan1121 已提交
2424
  SCtgCacheOperation *op = NULL;
dengyihao's avatar
dengyihao 已提交
2425
  bool                stopQueue = false;
D
dapan1121 已提交
2426 2427 2428 2429 2430

  while (true) {
    node = gCtgMgmt.queue.head->next;
    while (node) {
      if (node->op) {
D
dapan1121 已提交
2431 2432 2433 2434 2435 2436
        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;
D
dapan1121 已提交
2437
          CTG_STAT_RT_INC(numOfOpDequeue, 1);
D
dapan1121 已提交
2438
        } else {
2439
          ctgFreeCacheOperationData(op);
D
dapan1121 已提交
2440
          CTG_STAT_RT_INC(numOfOpAbort, 1);
D
dapan1121 已提交
2441
        }
dengyihao's avatar
dengyihao 已提交
2442

D
dapan1121 已提交
2443 2444
        if (op->syncOp) {
          tsem_post(&op->rspSem);
D
dapan1121 已提交
2445
        } else {
D
dapan1121 已提交
2446
          taosMemoryFree(op);
D
dapan1121 已提交
2447
        }
D
dapan1121 已提交
2448
      }
D
dapan1121 已提交
2449 2450 2451

      nodeNext = node->next;
      taosMemoryFree(node);
dengyihao's avatar
dengyihao 已提交
2452

D
dapan1121 已提交
2453
      node = nodeNext;
D
dapan1121 已提交
2454 2455
    }

D
dapan1121 已提交
2456
    if (!stopQueue) {
D
dapan1121 已提交
2457 2458 2459 2460
      taosUsleep(1);
    } else {
      break;
    }
D
dapan1121 已提交
2461 2462 2463 2464 2465 2466
  }

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

dengyihao's avatar
dengyihao 已提交
2467
void *ctgUpdateThreadFunc(void *param) {
D
dapan1121 已提交
2468
  setThreadName("catalog");
2469

D
dapan1121 已提交
2470 2471 2472 2473 2474 2475
  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)));
    }
dengyihao's avatar
dengyihao 已提交
2476

2477
    if (atomic_load_8((int8_t *)&gCtgMgmt.queue.stopQueue)) {
D
dapan1121 已提交
2478
      ctgCleanupCacheQueue();
D
dapan1121 已提交
2479 2480 2481
      break;
    }

D
dapan1121 已提交
2482 2483 2484
    SCtgCacheOperation *operation = NULL;
    ctgDequeue(&operation);
    SCatalog *pCtg = ((SCtgUpdateMsgHeader *)operation->data)->pCtg;
D
dapan1121 已提交
2485

D
dapan1121 已提交
2486
    ctgDebug("process [%s] operation", gCtgCacheOperation[operation->opId].name);
dengyihao's avatar
dengyihao 已提交
2487

D
dapan1121 已提交
2488
    (*gCtgCacheOperation[operation->opId].func)(operation);
D
dapan1121 已提交
2489

D
dapan1121 已提交
2490
    if (operation->syncOp) {
D
dapan1121 已提交
2491
      tsem_post(&operation->rspSem);
D
dapan1121 已提交
2492 2493
    } else {
      taosMemoryFreeClear(operation);
D
dapan1121 已提交
2494 2495
    }

D
dapan1121 已提交
2496
    CTG_STAT_RT_INC(numOfOpDequeue, 1);
D
dapan1121 已提交
2497

D
dapan1121 已提交
2498
    ctgdShowCacheInfo();
D
dapan1121 已提交
2499
    ctgdShowStatInfo();
D
dapan1121 已提交
2500 2501 2502
  }

  qInfo("catalog update thread stopped");
dengyihao's avatar
dengyihao 已提交
2503

D
dapan1121 已提交
2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515
  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);
  }
dengyihao's avatar
dengyihao 已提交
2516

D
dapan1121 已提交
2517 2518 2519 2520
  taosThreadAttrDestroy(&thAttr);
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
2521
int32_t ctgGetTbMetaFromCache(SCatalog *pCtg, SCtgTbMetaCtx *ctx, STableMeta **pTableMeta) {
D
dapan1121 已提交
2522
  if (IS_SYS_DBNAME(ctx->pName->dbname)) {
D
dapan1121 已提交
2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534
    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);
dengyihao's avatar
dengyihao 已提交
2535
    *pTableMeta = NULL;
D
dapan1121 已提交
2536 2537 2538 2539 2540 2541 2542 2543 2544
  }

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

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
2545
#if 0
2546
int32_t ctgGetTbMetaBFromCache(SCatalog* pCtg, SRequestConnInfo *pConn, SCtgTbMetasCtx* ctx, SArray** pResList) {
D
dapan1121 已提交
2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583
  int32_t tbNum = taosArrayGetSize(ctx->pNames);
  SName* fName = taosArrayGet(ctx->pNames, 0);
  int32_t fIdx = 0;
  
  for (int32_t i = 0; i < tbNum; ++i) {
    SName* pName = taosArrayGet(ctx->pNames, i);
    SCtgTbMetaCtx nctx = {0};
    nctx.flag = CTG_FLAG_UNKNOWN_STB;
    nctx.pName = pName;
    
    if (IS_SYS_DBNAME(pName->dbname)) {
      CTG_FLAG_SET_SYS_DB(nctx.flag);
    }

    STableMeta *pTableMeta = NULL;
    CTG_ERR_RET(ctgReadTbMetaFromCache(pCtg, &nctx, &pTableMeta));
    SMetaRes res = {0};
    
    if (pTableMeta) {
      if (CTG_FLAG_MATCH_STB(nctx.flag, pTableMeta->tableType) &&
          ((!CTG_FLAG_IS_FORCE_UPDATE(nctx.flag)) || (CTG_FLAG_IS_SYS_DB(nctx.flag)))) {
        res.pRes = pTableMeta;
      } else {
        taosMemoryFreeClear(pTableMeta);
      }
    }

    if (NULL == res.pRes) {
      if (NULL == ctx->pFetchs) {
        ctx->pFetchs = taosArrayInit(tbNum, sizeof(SCtgFetch));
      }
      
      if (CTG_FLAG_IS_UNKNOWN_STB(nctx.flag)) {
        CTG_FLAG_SET_STB(nctx.flag, nctx.tbInfo.tbType);
      }

      SCtgFetch fetch = {0};
2584
      fetch.tbIdx = i;
D
dapan1121 已提交
2585 2586 2587 2588 2589 2590
      fetch.fetchIdx = fIdx++;
      fetch.flag = nctx.flag;

      taosArrayPush(ctx->pFetchs, &fetch);
    }
    
2591
    taosArrayPush(ctx->pResList, &res);
D
dapan1121 已提交
2592 2593 2594
  }

  if (NULL == ctx->pFetchs) {
2595
    TSWAP(*pResList, ctx->pResList);
D
dapan1121 已提交
2596 2597 2598 2599
  }

  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
2600 2601
#endif

dengyihao's avatar
dengyihao 已提交
2602 2603 2604 2605 2606 2607 2608 2609 2610
int32_t ctgGetTbMetasFromCache(SCatalog *pCtg, SRequestConnInfo *pConn, SCtgTbMetasCtx *ctx, int32_t dbIdx,
                               int32_t *fetchIdx, int32_t baseResIdx, SArray *pList) {
  int32_t     tbNum = taosArrayGetSize(pList);
  SName      *pName = taosArrayGet(pList, 0);
  char        dbFName[TSDB_DB_FNAME_LEN] = {0};
  int32_t     flag = CTG_FLAG_UNKNOWN_STB;
  uint64_t    lastSuid = 0;
  STableMeta *lastTableMeta = NULL;

D
dapan1121 已提交
2611 2612 2613 2614 2615 2616 2617 2618
  if (IS_SYS_DBNAME(pName->dbname)) {
    CTG_FLAG_SET_SYS_DB(flag);
    strcpy(dbFName, pName->dbname);
  } else {
    tNameGetFullDbName(pName, dbFName);
  }

  SCtgDBCache *dbCache = NULL;
dengyihao's avatar
dengyihao 已提交
2619
  SCtgTbCache *pCache = NULL;
D
dapan1121 已提交
2620
  ctgAcquireDBCache(pCtg, dbFName, &dbCache);
dengyihao's avatar
dengyihao 已提交
2621

D
dapan1121 已提交
2622 2623 2624
  if (NULL == dbCache) {
    ctgDebug("db %s not in cache", dbFName);
    for (int32_t i = 0; i < tbNum; ++i) {
2625
      ctgAddFetch(&ctx->pFetchs, dbIdx, i, fetchIdx, baseResIdx + i, flag);
H
Haojun Liao 已提交
2626
      taosArrayPush(ctx->pResList, &(SMetaData){0});
D
dapan1121 已提交
2627 2628 2629 2630 2631 2632
    }

    return TSDB_CODE_SUCCESS;
  }

  for (int32_t i = 0; i < tbNum; ++i) {
H
Haojun Liao 已提交
2633
    pName = taosArrayGet(pList, i);
D
dapan1121 已提交
2634 2635 2636 2637

    pCache = taosHashAcquire(dbCache->tbCache, pName->tname, strlen(pName->tname));
    if (NULL == pCache) {
      ctgDebug("tb %s not in cache, dbFName:%s", pName->tname, dbFName);
2638
      ctgAddFetch(&ctx->pFetchs, dbIdx, i, fetchIdx, baseResIdx + i, flag);
H
Haojun Liao 已提交
2639
      taosArrayPush(ctx->pResList, &(SMetaRes){0});
D
dapan1121 已提交
2640
      CTG_META_NHIT_INC();
dengyihao's avatar
dengyihao 已提交
2641

D
dapan1121 已提交
2642 2643 2644 2645 2646
      continue;
    }

    CTG_LOCK(CTG_READ, &pCache->metaLock);
    if (NULL == pCache->pMeta) {
K
kailixu 已提交
2647
      CTG_UNLOCK(CTG_READ, &pCache->metaLock);
D
dapan1121 已提交
2648
      ctgDebug("tb %s meta not in cache, dbFName:%s", pName->tname, dbFName);
2649
      ctgAddFetch(&ctx->pFetchs, dbIdx, i, fetchIdx, baseResIdx + i, flag);
H
Haojun Liao 已提交
2650
      taosArrayPush(ctx->pResList, &(SMetaRes){0});
D
dapan1121 已提交
2651
      CTG_META_NHIT_INC();
dengyihao's avatar
dengyihao 已提交
2652

D
dapan1121 已提交
2653 2654 2655
      continue;
    }

dengyihao's avatar
dengyihao 已提交
2656
    STableMeta *tbMeta = pCache->pMeta;
D
dapan1121 已提交
2657

D
dapan1121 已提交
2658
    CTG_META_HIT_INC(tbMeta->tableType);
X
Xiaoyu Wang 已提交
2659

D
dapan1121 已提交
2660 2661 2662 2663 2664 2665 2666
    SCtgTbMetaCtx nctx = {0};
    nctx.flag = flag;
    nctx.tbInfo.inCache = true;
    nctx.tbInfo.dbId = dbCache->dbId;
    nctx.tbInfo.suid = tbMeta->suid;
    nctx.tbInfo.tbType = tbMeta->tableType;

dengyihao's avatar
dengyihao 已提交
2667 2668
    SMetaRes    res = {0};
    STableMeta *pTableMeta = NULL;
D
dapan1121 已提交
2669 2670 2671 2672
    if (tbMeta->tableType != TSDB_CHILD_TABLE) {
      int32_t metaSize = CTG_META_SIZE(tbMeta);
      pTableMeta = taosMemoryCalloc(1, metaSize);
      if (NULL == pTableMeta) {
2673
        ctgReleaseTbMetaToCache(pCtg, dbCache, pCache);
D
dapan1121 已提交
2674 2675
        CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
      }
dengyihao's avatar
dengyihao 已提交
2676

D
dapan1121 已提交
2677
      memcpy(pTableMeta, tbMeta, metaSize);
dengyihao's avatar
dengyihao 已提交
2678

2679
      CTG_UNLOCK(CTG_READ, &pCache->metaLock);
dengyihao's avatar
dengyihao 已提交
2680 2681
      taosHashRelease(dbCache->tbCache, pCache);

D
dapan1121 已提交
2682
      ctgDebug("Got tb %s meta from cache, type:%d, dbFName:%s", pName->tname, tbMeta->tableType, dbFName);
dengyihao's avatar
dengyihao 已提交
2683

D
dapan1121 已提交
2684
      res.pRes = pTableMeta;
2685
      taosArrayPush(ctx->pResList, &res);
D
dapan1121 已提交
2686 2687 2688

      continue;
    }
dengyihao's avatar
dengyihao 已提交
2689

D
dapan1121 已提交
2690 2691 2692 2693
    // PROCESS FOR CHILD TABLE

    if (lastSuid && tbMeta->suid == lastSuid && lastTableMeta) {
      cloneTableMeta(lastTableMeta, &pTableMeta);
2694
      memcpy(pTableMeta, tbMeta, sizeof(SCTableMeta));
D
dapan1121 已提交
2695

2696
      CTG_UNLOCK(CTG_READ, &pCache->metaLock);
dengyihao's avatar
dengyihao 已提交
2697 2698
      taosHashRelease(dbCache->tbCache, pCache);

D
dapan1121 已提交
2699
      ctgDebug("Got tb %s meta from cache, type:%d, dbFName:%s", pName->tname, tbMeta->tableType, dbFName);
dengyihao's avatar
dengyihao 已提交
2700

D
dapan1121 已提交
2701
      res.pRes = pTableMeta;
2702
      taosArrayPush(ctx->pResList, &res);
dengyihao's avatar
dengyihao 已提交
2703

D
dapan1121 已提交
2704 2705
      continue;
    }
dengyihao's avatar
dengyihao 已提交
2706

D
dapan1121 已提交
2707 2708 2709
    int32_t metaSize = sizeof(SCTableMeta);
    pTableMeta = taosMemoryCalloc(1, metaSize);
    if (NULL == pTableMeta) {
dengyihao's avatar
dengyihao 已提交
2710
      ctgReleaseTbMetaToCache(pCtg, dbCache, pCache);
D
dapan1121 已提交
2711 2712
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
dengyihao's avatar
dengyihao 已提交
2713

D
dapan1121 已提交
2714
    memcpy(pTableMeta, tbMeta, metaSize);
dengyihao's avatar
dengyihao 已提交
2715

2716
    CTG_UNLOCK(CTG_READ, &pCache->metaLock);
dengyihao's avatar
dengyihao 已提交
2717 2718 2719 2720 2721 2722
    taosHashRelease(dbCache->tbCache, pCache);

    ctgDebug("Got ctb %s meta from cache, will continue to get its stb meta, type:%d, dbFName:%s", pName->tname,
             nctx.tbInfo.tbType, dbFName);

    char *stName = taosHashAcquire(dbCache->stbCache, &pTableMeta->suid, sizeof(pTableMeta->suid));
D
dapan1121 已提交
2723 2724
    if (NULL == stName) {
      ctgDebug("stb 0x%" PRIx64 " not in cache, dbFName:%s", pTableMeta->suid, dbFName);
2725
      ctgAddFetch(&ctx->pFetchs, dbIdx, i, fetchIdx, baseResIdx + i, flag);
H
Haojun Liao 已提交
2726
      taosArrayPush(ctx->pResList, &(SMetaRes){0});
D
dapan1121 已提交
2727
      taosMemoryFreeClear(pTableMeta);
D
dapan1121 已提交
2728 2729

      CTG_META_NHIT_INC();
D
dapan1121 已提交
2730 2731 2732 2733 2734 2735 2736
      continue;
    }

    pCache = taosHashAcquire(dbCache->tbCache, stName, strlen(stName));
    if (NULL == pCache) {
      ctgDebug("stb 0x%" PRIx64 " name %s not in cache, dbFName:%s", pTableMeta->suid, stName, dbFName);
      taosHashRelease(dbCache->stbCache, stName);
dengyihao's avatar
dengyihao 已提交
2737

2738
      ctgAddFetch(&ctx->pFetchs, dbIdx, i, fetchIdx, baseResIdx + i, flag);
H
Haojun Liao 已提交
2739
      taosArrayPush(ctx->pResList, &(SMetaRes){0});
dengyihao's avatar
dengyihao 已提交
2740
      taosMemoryFreeClear(pTableMeta);
X
Xiaoyu Wang 已提交
2741

D
dapan1121 已提交
2742
      CTG_META_NHIT_INC();
D
dapan1121 已提交
2743 2744 2745 2746 2747 2748 2749 2750
      continue;
    }

    taosHashRelease(dbCache->stbCache, stName);

    CTG_LOCK(CTG_READ, &pCache->metaLock);
    if (NULL == pCache->pMeta) {
      ctgDebug("stb 0x%" PRIx64 " meta not in cache, dbFName:%s", pTableMeta->suid, dbFName);
2751
      CTG_UNLOCK(CTG_READ, &pCache->metaLock);
dengyihao's avatar
dengyihao 已提交
2752 2753
      taosHashRelease(dbCache->tbCache, pCache);

2754
      ctgAddFetch(&ctx->pFetchs, dbIdx, i, fetchIdx, baseResIdx + i, flag);
H
Haojun Liao 已提交
2755
      taosArrayPush(ctx->pResList, &(SMetaRes){0});
D
dapan1121 已提交
2756 2757
      taosMemoryFreeClear(pTableMeta);

D
dapan1121 已提交
2758
      CTG_META_NHIT_INC();
D
dapan1121 已提交
2759 2760
      continue;
    }
dengyihao's avatar
dengyihao 已提交
2761 2762 2763

    STableMeta *stbMeta = pCache->pMeta;
    if (stbMeta->suid != nctx.tbInfo.suid) {
2764
      CTG_UNLOCK(CTG_READ, &pCache->metaLock);
dengyihao's avatar
dengyihao 已提交
2765 2766 2767 2768 2769
      taosHashRelease(dbCache->tbCache, pCache);

      ctgError("stb suid 0x%" PRIx64 " in stbCache mis-match, expected suid 0x%" PRIx64, stbMeta->suid,
               nctx.tbInfo.suid);

2770
      ctgAddFetch(&ctx->pFetchs, dbIdx, i, fetchIdx, baseResIdx + i, flag);
H
Haojun Liao 已提交
2771
      taosArrayPush(ctx->pResList, &(SMetaRes){0});
D
dapan1121 已提交
2772 2773
      taosMemoryFreeClear(pTableMeta);

D
dapan1121 已提交
2774
      CTG_META_NHIT_INC();
D
dapan1121 已提交
2775 2776
      continue;
    }
dengyihao's avatar
dengyihao 已提交
2777

D
dapan1121 已提交
2778 2779
    metaSize = CTG_META_SIZE(stbMeta);
    pTableMeta = taosMemoryRealloc(pTableMeta, metaSize);
dengyihao's avatar
dengyihao 已提交
2780
    if (NULL == pTableMeta) {
2781
      ctgReleaseTbMetaToCache(pCtg, dbCache, pCache);
D
dapan1121 已提交
2782 2783
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
dengyihao's avatar
dengyihao 已提交
2784

D
dapan1121 已提交
2785
    memcpy(&pTableMeta->sversion, &stbMeta->sversion, metaSize - sizeof(SCTableMeta));
dengyihao's avatar
dengyihao 已提交
2786

2787
    CTG_UNLOCK(CTG_READ, &pCache->metaLock);
dengyihao's avatar
dengyihao 已提交
2788 2789
    taosHashRelease(dbCache->tbCache, pCache);

D
dapan1121 已提交
2790 2791
    CTG_META_HIT_INC(pTableMeta->tableType);

D
dapan1121 已提交
2792
    res.pRes = pTableMeta;
2793
    taosArrayPush(ctx->pResList, &res);
D
dapan1121 已提交
2794 2795 2796 2797 2798

    lastSuid = pTableMeta->suid;
    lastTableMeta = pTableMeta;
  }

2799
  ctgReleaseDBCache(pCtg, dbCache);
dengyihao's avatar
dengyihao 已提交
2800

D
dapan1121 已提交
2801 2802 2803
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
2804
int32_t ctgRemoveTbMetaFromCache(SCatalog *pCtg, SName *pTableName, bool syncReq) {
D
dapan1121 已提交
2805
  int32_t       code = 0;
dengyihao's avatar
dengyihao 已提交
2806
  STableMeta   *tblMeta = NULL;
D
dapan1121 已提交
2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834
  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) {
D
dapan1121 已提交
2835
  if (IS_SYS_DBNAME(pTableName->dbname)) {
D
dapan1121 已提交
2836 2837 2838 2839
    ctgError("no valid vgInfo for db, dbname:%s", pTableName->dbname);
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
  }

dengyihao's avatar
dengyihao 已提交
2840
  SCtgDBCache *dbCache = NULL;
D
dapan1121 已提交
2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866
  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);
}