ctgCache.c 74.4 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 35
int32_t ctgRLockVgInfo(SCatalog *pCtg, SCtgDBCache *dbCache, bool *inCache) {
  CTG_LOCK(CTG_READ, &dbCache->vgCache.vgLock);
dengyihao's avatar
dengyihao 已提交
36

D
dapan1121 已提交
37
  if (dbCache->deleted) {
D
dapan1121 已提交
38
    CTG_UNLOCK(CTG_READ, &dbCache->vgCache.vgLock);
D
dapan1121 已提交
39

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

D
dapan1121 已提交
42 43 44 45
    *inCache = false;
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
46 47
  if (NULL == dbCache->vgCache.vgInfo) {
    CTG_UNLOCK(CTG_READ, &dbCache->vgCache.vgLock);
D
dapan1121 已提交
48 49

    *inCache = false;
dengyihao's avatar
dengyihao 已提交
50
    ctgDebug("db vgInfo is empty, dbId:0x%" PRIx64, dbCache->dbId);
D
dapan1121 已提交
51 52 53 54
    return TSDB_CODE_SUCCESS;
  }

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

D
dapan1121 已提交
56 57 58
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
59 60
int32_t ctgWLockVgInfo(SCatalog *pCtg, SCtgDBCache *dbCache) {
  CTG_LOCK(CTG_WRITE, &dbCache->vgCache.vgLock);
D
dapan1121 已提交
61 62

  if (dbCache->deleted) {
dengyihao's avatar
dengyihao 已提交
63
    ctgDebug("db is dropping, dbId:0x%" PRIx64, dbCache->dbId);
D
dapan1121 已提交
64
    CTG_UNLOCK(CTG_WRITE, &dbCache->vgCache.vgLock);
D
dapan1121 已提交
65 66 67 68 69 70
    CTG_ERR_RET(TSDB_CODE_CTG_DB_DROPPED);
  }

  return TSDB_CODE_SUCCESS;
}

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

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

dengyihao's avatar
dengyihao 已提交
75 76
void ctgReleaseDBCache(SCatalog *pCtg, SCtgDBCache *dbCache) {
  CTG_UNLOCK(CTG_READ, &dbCache->dbLock);
D
dapan1121 已提交
77 78
  taosHashRelease(pCtg->dbCache, dbCache);
}
D
dapan1121 已提交
79

dengyihao's avatar
dengyihao 已提交
80
int32_t ctgAcquireDBCacheImpl(SCatalog *pCtg, const char *dbFName, SCtgDBCache **pCache, bool acquire) {
D
dapan1121 已提交
81
  char *p = strchr(dbFName, '.');
D
dapan1121 已提交
82
  if (p && IS_SYS_DBNAME(p + 1)) {
D
dapan1121 已提交
83 84 85
    dbFName = p + 1;
  }

D
dapan1121 已提交
86 87 88 89 90 91 92
  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 已提交
93

D
dapan1121 已提交
94 95 96 97 98 99
  if (NULL == dbCache) {
    *pCache = NULL;
    ctgDebug("db not in cache, dbFName:%s", dbFName);
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
100 101 102 103
  if (acquire) {
    CTG_LOCK(CTG_READ, &dbCache->dbLock);
  }

D
dapan1121 已提交
104 105 106
  if (dbCache->deleted) {
    if (acquire) {
      ctgReleaseDBCache(pCtg, dbCache);
dengyihao's avatar
dengyihao 已提交
107 108
    }

D
dapan1121 已提交
109 110 111 112 113 114
    *pCache = NULL;
    ctgDebug("db is removing from cache, dbFName:%s", dbFName);
    return TSDB_CODE_SUCCESS;
  }

  *pCache = dbCache;
dengyihao's avatar
dengyihao 已提交
115

D
dapan1121 已提交
116 117 118
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
119
int32_t ctgAcquireDBCache(SCatalog *pCtg, const char *dbFName, SCtgDBCache **pCache) {
D
dapan1121 已提交
120 121 122
  CTG_RET(ctgAcquireDBCacheImpl(pCtg, dbFName, pCache, true));
}

dengyihao's avatar
dengyihao 已提交
123
int32_t ctgGetDBCache(SCatalog *pCtg, const char *dbFName, SCtgDBCache **pCache) {
D
dapan1121 已提交
124 125 126
  CTG_RET(ctgAcquireDBCacheImpl(pCtg, dbFName, pCache, false));
}

dengyihao's avatar
dengyihao 已提交
127
void ctgReleaseVgInfoToCache(SCatalog *pCtg, SCtgDBCache *dbCache) {
D
dapan1121 已提交
128 129 130
  ctgRUnlockVgInfo(dbCache);
  ctgReleaseDBCache(pCtg, dbCache);
}
D
dapan1121 已提交
131

dengyihao's avatar
dengyihao 已提交
132
void ctgReleaseTbMetaToCache(SCatalog *pCtg, SCtgDBCache *dbCache, SCtgTbCache *pCache) {
D
dapan1121 已提交
133
  if (pCache && dbCache) {
D
dapan1121 已提交
134
    CTG_UNLOCK(CTG_READ, &pCache->metaLock);
dengyihao's avatar
dengyihao 已提交
135
    taosHashRelease(dbCache->tbCache, pCache);
D
dapan1121 已提交
136
  }
D
dapan1121 已提交
137

D
dapan1121 已提交
138 139
  if (dbCache) {
    ctgReleaseDBCache(pCtg, dbCache);
D
dapan1121 已提交
140
  }
D
dapan1121 已提交
141
}
D
dapan1121 已提交
142

dengyihao's avatar
dengyihao 已提交
143
void ctgReleaseTbIndexToCache(SCatalog *pCtg, SCtgDBCache *dbCache, SCtgTbCache *pCache) {
D
dapan1121 已提交
144 145
  if (pCache) {
    CTG_UNLOCK(CTG_READ, &pCache->indexLock);
dengyihao's avatar
dengyihao 已提交
146
    taosHashRelease(dbCache->tbCache, pCache);
D
dapan1121 已提交
147 148 149 150 151 152 153
  }

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

154
void ctgReleaseVgMetaToCache(SCatalog *pCtg, SCtgDBCache *dbCache, SCtgTbCache *pCache) {
D
dapan1121 已提交
155
  if (pCache && dbCache) {
156 157 158 159
    CTG_UNLOCK(CTG_READ, &pCache->metaLock);
    taosHashRelease(dbCache->tbCache, pCache);
  }

D
dapan1121 已提交
160 161 162 163
  if (dbCache) {
    ctgRUnlockVgInfo(dbCache);
    ctgReleaseDBCache(pCtg, dbCache);
  }
164 165
}

dengyihao's avatar
dengyihao 已提交
166
int32_t ctgAcquireVgInfoFromCache(SCatalog *pCtg, const char *dbFName, SCtgDBCache **pCache) {
D
dapan1121 已提交
167
  SCtgDBCache *dbCache = NULL;
D
dapan1121 已提交
168
  ctgAcquireDBCache(pCtg, dbFName, &dbCache);
dengyihao's avatar
dengyihao 已提交
169
  if (NULL == dbCache) {
D
dapan1121 已提交
170 171 172 173 174
    ctgDebug("db %s not in cache", dbFName);
    goto _return;
  }

  bool inCache = false;
D
dapan1121 已提交
175
  ctgRLockVgInfo(pCtg, dbCache, &inCache);
D
dapan1121 已提交
176 177 178 179 180 181 182
  if (!inCache) {
    ctgDebug("vgInfo of db %s not in cache", dbFName);
    goto _return;
  }

  *pCache = dbCache;

D
dapan1121 已提交
183
  CTG_CACHE_STAT_INC(numOfVgHit, 1);
D
dapan1121 已提交
184 185

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

D
dapan1121 已提交
187 188 189 190 191 192 193 194 195 196
  return TSDB_CODE_SUCCESS;

_return:

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

  *pCache = NULL;

D
dapan1121 已提交
197
  CTG_CACHE_STAT_INC(numOfVgMiss, 1);
dengyihao's avatar
dengyihao 已提交
198

D
dapan1121 已提交
199 200 201
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
202
int32_t ctgAcquireTbMetaFromCache(SCatalog *pCtg, char *dbFName, char *tbName, SCtgDBCache **pDb, SCtgTbCache **pTb) {
D
dapan1121 已提交
203
  SCtgDBCache *dbCache = NULL;
dengyihao's avatar
dengyihao 已提交
204
  SCtgTbCache *pCache = NULL;
D
dapan1121 已提交
205 206 207 208 209
  ctgAcquireDBCache(pCtg, dbFName, &dbCache);
  if (NULL == dbCache) {
    ctgDebug("db %s not in cache", dbFName);
    goto _return;
  }
dengyihao's avatar
dengyihao 已提交
210

D
dapan1121 已提交
211
  pCache = taosHashAcquire(dbCache->tbCache, tbName, strlen(tbName));
D
dapan1121 已提交
212 213 214
  if (NULL == pCache) {
    ctgDebug("tb %s not in cache, dbFName:%s", tbName, dbFName);
    goto _return;
D
dapan1121 已提交
215 216
  }

D
dapan1121 已提交
217 218 219 220 221 222 223 224 225 226
  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 已提交
227

D
dapan1121 已提交
228
  CTG_CACHE_STAT_INC(numOfMetaHit, 1);
D
dapan1121 已提交
229 230 231 232 233 234 235

  return TSDB_CODE_SUCCESS;

_return:

  ctgReleaseTbMetaToCache(pCtg, dbCache, pCache);

D
dapan1121 已提交
236
  CTG_CACHE_STAT_INC(numOfMetaMiss, 1);
dengyihao's avatar
dengyihao 已提交
237

D
dapan1121 已提交
238 239 240
  return TSDB_CODE_SUCCESS;
}

241 242 243
int32_t ctgAcquireVgMetaFromCache(SCatalog *pCtg, const char *dbFName, const char *tbName, SCtgDBCache **pDb, SCtgTbCache **pTb) {
  SCtgDBCache *dbCache = NULL;
  SCtgTbCache *tbCache = NULL;
D
dapan1121 已提交
244
  bool vgInCache = false;
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309

  ctgAcquireDBCache(pCtg, dbFName, &dbCache);
  if (NULL == dbCache) {
    ctgDebug("db %s not in cache", dbFName);
    CTG_CACHE_STAT_INC(numOfVgMiss, 1);
    goto _return;
  }

  ctgRLockVgInfo(pCtg, dbCache, &vgInCache);
  if (!vgInCache) {
    ctgDebug("vgInfo of db %s not in cache", dbFName);
    CTG_CACHE_STAT_INC(numOfVgMiss, 1);
    goto _return;
  }

  *pDb = dbCache;

  CTG_CACHE_STAT_INC(numOfVgHit, 1);

  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);
    CTG_CACHE_STAT_INC(numOfMetaMiss, 1);
    goto _return;
  }

  CTG_LOCK(CTG_READ, &tbCache->metaLock);
  if (NULL == tbCache->pMeta) {
    ctgDebug("tb %s meta not in cache, dbFName:%s", tbName, dbFName);
    CTG_CACHE_STAT_INC(numOfMetaMiss, 1);
    goto _return;
  }

  *pTb = tbCache;

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

  CTG_CACHE_STAT_INC(numOfMetaHit, 1);

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


310
/*
dengyihao's avatar
dengyihao 已提交
311 312 313
int32_t ctgAcquireStbMetaFromCache(SCatalog *pCtg, char *dbFName, uint64_t suid, SCtgDBCache **pDb, SCtgTbCache **pTb) {
  SCtgDBCache *dbCache = NULL;
  SCtgTbCache *pCache = NULL;
D
dapan1121 已提交
314 315 316 317 318
  ctgAcquireDBCache(pCtg, dbFName, &dbCache);
  if (NULL == dbCache) {
    ctgDebug("db %s not in cache", dbFName);
    goto _return;
  }
dengyihao's avatar
dengyihao 已提交
319 320

  char *stName = taosHashAcquire(dbCache->stbCache, &suid, sizeof(suid));
D
dapan1121 已提交
321
  if (NULL == stName) {
D
dapan1121 已提交
322
    ctgDebug("stb 0x%" PRIx64 " not in cache, dbFName:%s", suid, dbFName);
D
dapan1121 已提交
323 324 325 326 327
    goto _return;
  }

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

D
dapan1121 已提交
333 334
  taosHashRelease(dbCache->stbCache, stName);

D
dapan1121 已提交
335 336
  CTG_LOCK(CTG_READ, &pCache->metaLock);
  if (NULL == pCache->pMeta) {
D
dapan1121 已提交
337
    ctgDebug("stb 0x%" PRIx64 " meta not in cache, dbFName:%s", suid, dbFName);
D
dapan1121 已提交
338 339 340 341 342 343
    goto _return;
  }

  *pDb = dbCache;
  *pTb = pCache;

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

D
dapan1121 已提交
346
  CTG_CACHE_STAT_INC(numOfMetaHit, 1);
D
dapan1121 已提交
347 348 349 350 351 352 353

  return TSDB_CODE_SUCCESS;

_return:

  ctgReleaseTbMetaToCache(pCtg, dbCache, pCache);

D
dapan1121 已提交
354
  CTG_CACHE_STAT_INC(numOfMetaMiss, 1);
D
dapan1121 已提交
355 356 357

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

D
dapan1121 已提交
359 360
  return TSDB_CODE_SUCCESS;
}
361
*/
D
dapan1121 已提交
362

363
int32_t ctgAcquireStbMetaFromCache(SCtgDBCache *dbCache, SCatalog *pCtg, char *dbFName, uint64_t suid, SCtgTbCache **pTb) {
D
dapan1121 已提交
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
  SCtgTbCache *pCache = NULL;
  char *stName = taosHashAcquire(dbCache->stbCache, &suid, sizeof(suid));
  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);

  CTG_CACHE_STAT_INC(numOfMetaHit, 1);

  return TSDB_CODE_SUCCESS;

_return:

  ctgReleaseTbMetaToCache(pCtg, dbCache, pCache);

  CTG_CACHE_STAT_INC(numOfMetaMiss, 1);

  *pTb = NULL;

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
405
int32_t ctgAcquireTbIndexFromCache(SCatalog *pCtg, char *dbFName, char *tbName, SCtgDBCache **pDb, SCtgTbCache **pTb) {
D
dapan1121 已提交
406
  SCtgDBCache *dbCache = NULL;
dengyihao's avatar
dengyihao 已提交
407
  SCtgTbCache *pCache = NULL;
D
dapan1121 已提交
408 409
  ctgAcquireDBCache(pCtg, dbFName, &dbCache);
  if (NULL == dbCache) {
D
dapan1121 已提交
410 411 412
    ctgDebug("db %s not in cache", dbFName);
    goto _return;
  }
dengyihao's avatar
dengyihao 已提交
413

D
dapan1121 已提交
414
  int32_t sz = 0;
D
dapan1121 已提交
415
  pCache = taosHashAcquire(dbCache->tbCache, tbName, strlen(tbName));
D
dapan1121 已提交
416 417 418 419 420 421 422 423 424
  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 已提交
425 426
  }

D
dapan1121 已提交
427 428 429 430
  *pDb = dbCache;
  *pTb = pCache;

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

D
dapan1121 已提交
432
  CTG_CACHE_STAT_INC(numOfIndexHit, 1);
D
dapan1121 已提交
433 434 435 436 437 438 439

  return TSDB_CODE_SUCCESS;

_return:

  ctgReleaseTbIndexToCache(pCtg, dbCache, pCache);

D
dapan1121 已提交
440
  CTG_CACHE_STAT_INC(numOfIndexMiss, 1);
dengyihao's avatar
dengyihao 已提交
441

D
dapan1121 已提交
442 443 444
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
445
int32_t ctgTbMetaExistInCache(SCatalog *pCtg, char *dbFName, char *tbName, int32_t *exist) {
D
dapan1121 已提交
446
  SCtgDBCache *dbCache = NULL;
D
dapan1121 已提交
447
  SCtgTbCache *tbCache = NULL;
D
dapan1121 已提交
448 449 450
  ctgAcquireTbMetaFromCache(pCtg, dbFName, tbName, &dbCache, &tbCache);
  if (NULL == tbCache) {
    ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
dengyihao's avatar
dengyihao 已提交
451

D
dapan1121 已提交
452 453 454 455 456
    *exist = 0;
    return TSDB_CODE_SUCCESS;
  }

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

D
dapan1121 已提交
459 460 461
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
462 463
int32_t ctgCopyTbMeta(SCatalog *pCtg, SCtgTbMetaCtx *ctx, SCtgDBCache **pDb, SCtgTbCache **pTb, STableMeta **pTableMeta, char* dbFName) {
  SCtgDBCache *dbCache = *pDb;
464
  SCtgTbCache *tbCache = *pTb;
dengyihao's avatar
dengyihao 已提交
465
  STableMeta *tbMeta = tbCache->pMeta;
D
dapan1121 已提交
466 467 468 469
  ctx->tbInfo.inCache = true;
  ctx->tbInfo.dbId = dbCache->dbId;
  ctx->tbInfo.suid = tbMeta->suid;
  ctx->tbInfo.tbType = tbMeta->tableType;
dengyihao's avatar
dengyihao 已提交
470

D
dapan1121 已提交
471
  if (tbMeta->tableType != TSDB_CHILD_TABLE) {
D
dapan1121 已提交
472 473 474
    int32_t metaSize = CTG_META_SIZE(tbMeta);
    *pTableMeta = taosMemoryCalloc(1, metaSize);
    if (NULL == *pTableMeta) {
475
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
476 477 478
    }

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

D
dapan1121 已提交
480
    ctgDebug("Got tb %s meta from cache, type:%d, dbFName:%s", ctx->pName->tname, tbMeta->tableType, dbFName);
D
dapan1121 已提交
481 482
    return TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
483 484

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

D
dapan1121 已提交
486 487 488
  int32_t metaSize = sizeof(SCTableMeta);
  *pTableMeta = taosMemoryCalloc(1, metaSize);
  if (NULL == *pTableMeta) {
489
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
490 491
  }

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

D
dapan1121 已提交
494 495
  //ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);

496 497 498
  CTG_UNLOCK(CTG_READ, &tbCache->metaLock);
  taosHashRelease(dbCache->tbCache, tbCache);
  *pTb = NULL;
D
dapan1121 已提交
499
  
dengyihao's avatar
dengyihao 已提交
500 501
  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 已提交
502

503
  ctgAcquireStbMetaFromCache(dbCache, pCtg, dbFName, ctx->tbInfo.suid, &tbCache);
D
dapan1121 已提交
504 505
  if (NULL == tbCache) {
    taosMemoryFreeClear(*pTableMeta);
D
dapan1121 已提交
506
    *pDb = NULL;
D
dapan1121 已提交
507 508 509
    ctgDebug("stb 0x%" PRIx64 " meta not in cache", ctx->tbInfo.suid);
    return TSDB_CODE_SUCCESS;
  }
dengyihao's avatar
dengyihao 已提交
510

511 512
  *pTb = tbCache;

dengyihao's avatar
dengyihao 已提交
513 514 515
  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);
516 517
    taosMemoryFreeClear(*pTableMeta);
    CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
D
dapan1121 已提交
518 519
  }

D
dapan1121 已提交
520
  metaSize = CTG_META_SIZE(stbMeta);
D
dapan1121 已提交
521
  *pTableMeta = taosMemoryRealloc(*pTableMeta, metaSize);
dengyihao's avatar
dengyihao 已提交
522
  if (NULL == *pTableMeta) {
523
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
524 525
  }

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

528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
  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 已提交
551
  CTG_ERR_JRET(ctgCopyTbMeta(pCtg, ctx, &dbCache, &tbCache, pTableMeta, dbFName));
552

D
dapan1121 已提交
553
  ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
D
dapan1121 已提交
554

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

D
dapan1121 已提交
557 558 559 560
  return TSDB_CODE_SUCCESS;

_return:

D
dapan1121 已提交
561
  ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
D
dapan1121 已提交
562
  taosMemoryFreeClear(*pTableMeta);
dengyihao's avatar
dengyihao 已提交
563
  *pTableMeta = NULL;
dengyihao's avatar
dengyihao 已提交
564

D
dapan1121 已提交
565 566 567
  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
568 569
int32_t ctgReadTbVerFromCache(SCatalog *pCtg, SName *pTableName, int32_t *sver, int32_t *tver, int32_t *tbType,
                              uint64_t *suid, char *stbName) {
D
dapan1121 已提交
570
  *sver = -1;
D
dapan1121 已提交
571
  *tver = -1;
D
dapan1121 已提交
572 573

  SCtgDBCache *dbCache = NULL;
dengyihao's avatar
dengyihao 已提交
574
  SCtgTbCache *tbCache = NULL;
D
dapan1121 已提交
575
  char         dbFName[TSDB_DB_FNAME_LEN] = {0};
D
dapan1121 已提交
576 577
  tNameGetFullDbName(pTableName, dbFName);

D
dapan1121 已提交
578 579 580
  ctgAcquireTbMetaFromCache(pCtg, dbFName, pTableName->tname, &dbCache, &tbCache);
  if (NULL == tbCache) {
    ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
D
dapan1121 已提交
581 582 583
    return TSDB_CODE_SUCCESS;
  }

dengyihao's avatar
dengyihao 已提交
584
  STableMeta *tbMeta = tbCache->pMeta;
D
dapan1121 已提交
585 586
  *tbType = tbMeta->tableType;
  *suid = tbMeta->suid;
D
dapan1121 已提交
587

D
dapan1121 已提交
588
  if (*tbType != TSDB_CHILD_TABLE) {
D
dapan1121 已提交
589 590 591
    *sver = tbMeta->sversion;
    *tver = tbMeta->tversion;

dengyihao's avatar
dengyihao 已提交
592 593
    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 已提交
594

D
dapan1121 已提交
595
    ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
D
dapan1121 已提交
596 597 598
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
599
  // PROCESS FOR CHILD TABLE
dengyihao's avatar
dengyihao 已提交
600

601 602 603 604 605 606
  //ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
  if (tbCache) {
    CTG_UNLOCK(CTG_READ, &tbCache->metaLock);
    taosHashRelease(dbCache->tbCache, tbCache);
  }
  
D
dapan1121 已提交
607
  ctgDebug("Got ctb %s ver from cache, will continue to get its stb ver, dbFName:%s", pTableName->tname, dbFName);
dengyihao's avatar
dengyihao 已提交
608

609
  ctgAcquireStbMetaFromCache(dbCache, pCtg, dbFName, *suid, &tbCache);
D
dapan1121 已提交
610
  if (NULL == tbCache) {
D
dapan1121 已提交
611
    //ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
D
dapan1121 已提交
612
    ctgDebug("stb 0x%" PRIx64 " meta not in cache", *suid);
D
dapan1121 已提交
613 614
    return TSDB_CODE_SUCCESS;
  }
dengyihao's avatar
dengyihao 已提交
615 616

  STableMeta *stbMeta = tbCache->pMeta;
D
dapan1121 已提交
617
  if (stbMeta->suid != *suid) {
D
dapan1121 已提交
618
    ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
dengyihao's avatar
dengyihao 已提交
619
    ctgError("stb suid 0x%" PRIx64 " in stbCache mis-match, expected suid:0x%" PRIx64, stbMeta->suid, *suid);
D
dapan1121 已提交
620 621 622
    CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
  }

D
dapan1121 已提交
623
  size_t nameLen = 0;
D
dapan1121 已提交
624
  char  *name = taosHashGetKey(tbCache, &nameLen);
D
dapan1121 已提交
625 626 627 628

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

D
dapan1121 已提交
629 630
  *sver = stbMeta->sversion;
  *tver = stbMeta->tversion;
D
dapan1121 已提交
631

D
dapan1121 已提交
632
  ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
D
dapan1121 已提交
633

dengyihao's avatar
dengyihao 已提交
634 635
  ctgDebug("Got tb %s sver %d tver %d from cache, type:%d, dbFName:%s", pTableName->tname, *sver, *tver, *tbType,
           dbFName);
D
dapan1121 已提交
636 637 638 639

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
640
int32_t ctgReadTbTypeFromCache(SCatalog *pCtg, char *dbFName, char *tbName, int32_t *tbType) {
D
dapan1121 已提交
641
  SCtgDBCache *dbCache = NULL;
dengyihao's avatar
dengyihao 已提交
642
  SCtgTbCache *tbCache = NULL;
D
dapan1121 已提交
643
  CTG_ERR_RET(ctgAcquireTbMetaFromCache(pCtg, dbFName, tbName, &dbCache, &tbCache));
D
dapan1121 已提交
644 645
  if (NULL == tbCache) {
    ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);
D
dapan1121 已提交
646 647
    return TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
648 649 650 651

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

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

D
dapan1121 已提交
654 655 656
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
657 658
int32_t ctgReadTbIndexFromCache(SCatalog *pCtg, SName *pTableName, SArray **pRes) {
  int32_t      code = 0;
D
dapan1121 已提交
659
  SCtgDBCache *dbCache = NULL;
dengyihao's avatar
dengyihao 已提交
660
  SCtgTbCache *tbCache = NULL;
D
dapan1121 已提交
661 662
  char         dbFName[TSDB_DB_FNAME_LEN] = {0};
  tNameGetFullDbName(pTableName, dbFName);
D
dapan1121 已提交
663

D
dapan1121 已提交
664
  *pRes = NULL;
D
dapan1121 已提交
665

D
dapan1121 已提交
666 667 668
  ctgAcquireTbIndexFromCache(pCtg, dbFName, pTableName->tname, &dbCache, &tbCache);
  if (NULL == tbCache) {
    ctgReleaseTbIndexToCache(pCtg, dbCache, tbCache);
D
dapan1121 已提交
669 670 671
    return TSDB_CODE_SUCCESS;
  }

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

D
dapan1121 已提交
674
_return:
D
dapan1121 已提交
675

D
dapan1121 已提交
676
  ctgReleaseTbIndexToCache(pCtg, dbCache, tbCache);
D
dapan1121 已提交
677

D
dapan1121 已提交
678
  CTG_RET(code);
D
dapan1121 已提交
679 680
}

dengyihao's avatar
dengyihao 已提交
681
int32_t ctgChkAuthFromCache(SCatalog *pCtg, char *user, char *dbFName, AUTH_TYPE type, bool *inCache, bool *pass) {
682 683 684 685 686 687
  char *p = strchr(dbFName, '.');
  if (p) {
    ++p;
  } else {
    p = dbFName;
  }
dengyihao's avatar
dengyihao 已提交
688

689 690 691 692 693 694 695
  if (IS_SYS_DBNAME(p)) {
    *inCache = true;
    *pass = true;
    ctgDebug("sysdb %s, pass", dbFName);
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
696 697 698 699 700 701 702 703 704
  SCtgUserAuth *pUser = (SCtgUserAuth *)taosHashGet(pCtg->userCache, user, strlen(user));
  if (NULL == pUser) {
    ctgDebug("user not in cache, user:%s", user);
    goto _return;
  }

  *inCache = true;

  ctgDebug("Got user from cache, user:%s", user);
D
dapan1121 已提交
705
  CTG_CACHE_STAT_INC(numOfUserHit, 1);
dengyihao's avatar
dengyihao 已提交
706

D
dapan1121 已提交
707 708 709 710 711 712 713 714 715 716 717
  if (pUser->superUser) {
    *pass = true;
    return TSDB_CODE_SUCCESS;
  }

  CTG_LOCK(CTG_READ, &pUser->lock);
  if (pUser->createdDbs && taosHashGet(pUser->createdDbs, dbFName, strlen(dbFName))) {
    *pass = true;
    CTG_UNLOCK(CTG_READ, &pUser->lock);
    return TSDB_CODE_SUCCESS;
  }
dengyihao's avatar
dengyihao 已提交
718

D
dapan1121 已提交
719
  if (pUser->readDbs && taosHashGet(pUser->readDbs, dbFName, strlen(dbFName)) && CTG_AUTH_READ(type)) {
D
dapan1121 已提交
720 721
    *pass = true;
  }
dengyihao's avatar
dengyihao 已提交
722

D
dapan1121 已提交
723
  if (pUser->writeDbs && taosHashGet(pUser->writeDbs, dbFName, strlen(dbFName)) && CTG_AUTH_WRITE(type)) {
D
dapan1121 已提交
724 725 726 727
    *pass = true;
  }

  CTG_UNLOCK(CTG_READ, &pUser->lock);
dengyihao's avatar
dengyihao 已提交
728

D
dapan1121 已提交
729 730 731 732 733
  return TSDB_CODE_SUCCESS;

_return:

  *inCache = false;
D
dapan1121 已提交
734
  CTG_CACHE_STAT_INC(numOfUserMiss, 1);
dengyihao's avatar
dengyihao 已提交
735

D
dapan1121 已提交
736 737 738
  return TSDB_CODE_SUCCESS;
}

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

D
dapan1121 已提交
742 743 744
  SCtgQNode *node = gCtgMgmt.queue.head->next;
  gCtgMgmt.queue.head = gCtgMgmt.queue.head->next;

D
dapan1121 已提交
745
  CTG_QUEUE_DEC();
dengyihao's avatar
dengyihao 已提交
746

D
dapan1121 已提交
747 748
  taosMemoryFreeClear(orig);

D
dapan1121 已提交
749
  *op = node->op;
D
dapan1121 已提交
750 751
}

dengyihao's avatar
dengyihao 已提交
752
int32_t ctgEnqueue(SCatalog *pCtg, SCtgCacheOperation *operation) {
D
dapan1121 已提交
753 754 755
  SCtgQNode *node = taosMemoryCalloc(1, sizeof(SCtgQNode));
  if (NULL == node) {
    qError("calloc %d failed", (int32_t)sizeof(SCtgQNode));
D
dapan1121 已提交
756 757
    taosMemoryFree(operation->data);
    taosMemoryFree(operation);
D
dapan1121 已提交
758
    CTG_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
759 760
  }

dengyihao's avatar
dengyihao 已提交
761 762
  bool  syncOp = operation->syncOp;
  char *opName = gCtgCacheOperation[operation->opId].name;
D
dapan1121 已提交
763 764 765
  if (operation->syncOp) {
    tsem_init(&operation->rspSem, 0, 0);
  }
dengyihao's avatar
dengyihao 已提交
766

D
dapan1121 已提交
767
  node->op = operation;
D
dapan1121 已提交
768 769

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

D
dapan1121 已提交
771
  if (gCtgMgmt.queue.stopQueue) {
772 773 774 775
    ctgFreeQNode(node);
    CTG_UNLOCK(CTG_WRITE, &gCtgMgmt.queue.qlock);
    CTG_RET(TSDB_CODE_CTG_EXIT);
  }
776

D
dapan1121 已提交
777 778
  gCtgMgmt.queue.tail->next = node;
  gCtgMgmt.queue.tail = node;
779 780 781

  gCtgMgmt.queue.stopQueue = operation->stopQueue;

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

D
dapan1121 已提交
784 785
  ctgDebug("action [%s] added into queue", opName);

D
dapan1121 已提交
786
  CTG_QUEUE_INC();
D
dapan1121 已提交
787
  CTG_RT_STAT_INC(numOfOpEnqueue, 1);
D
dapan1121 已提交
788 789 790

  tsem_post(&gCtgMgmt.queue.reqSem);

D
dapan1121 已提交
791
  if (syncOp) {
792 793 794
    if (!operation->unLocked) {
      CTG_UNLOCK(CTG_READ, &gCtgMgmt.lock);
    }
D
dapan1121 已提交
795
    tsem_wait(&operation->rspSem);
796 797 798
    if (!operation->unLocked) {
      CTG_LOCK(CTG_READ, &gCtgMgmt.lock);
    }
D
dapan1121 已提交
799
    taosMemoryFree(operation);
D
dapan1121 已提交
800 801 802 803 804
  }

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
805 806
int32_t ctgDropDbCacheEnqueue(SCatalog *pCtg, const char *dbFName, int64_t dbId) {
  int32_t             code = 0;
D
dapan1121 已提交
807 808
  SCtgCacheOperation *op = taosMemoryCalloc(1, sizeof(SCtgCacheOperation));
  op->opId = CTG_OP_DROP_DB_CACHE;
809
  op->syncOp = true;
dengyihao's avatar
dengyihao 已提交
810

D
dapan1121 已提交
811
  SCtgDropDBMsg *msg = taosMemoryMalloc(sizeof(SCtgDropDBMsg));
D
dapan1121 已提交
812
  if (NULL == msg) {
D
dapan1121 已提交
813
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgDropDBMsg));
D
dapan1121 已提交
814
    taosMemoryFree(op);
D
dapan1121 已提交
815
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
816 817 818
  }

  char *p = strchr(dbFName, '.');
D
dapan1121 已提交
819
  if (p && IS_SYS_DBNAME(p + 1)) {
D
dapan1121 已提交
820 821 822 823
    dbFName = p + 1;
  }

  msg->pCtg = pCtg;
D
dapan1121 已提交
824
  tstrncpy(msg->dbFName, dbFName, sizeof(msg->dbFName));
D
dapan1121 已提交
825 826
  msg->dbId = dbId;

D
dapan1121 已提交
827
  op->data = msg;
D
dapan1121 已提交
828

D
dapan1121 已提交
829
  CTG_ERR_JRET(ctgEnqueue(pCtg, op));
D
dapan1121 已提交
830 831 832 833 834 835 836 837

  return TSDB_CODE_SUCCESS;

_return:

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
838 839
int32_t ctgDropDbVgroupEnqueue(SCatalog *pCtg, const char *dbFName, bool syncOp) {
  int32_t             code = 0;
D
dapan1121 已提交
840 841 842
  SCtgCacheOperation *op = taosMemoryCalloc(1, sizeof(SCtgCacheOperation));
  op->opId = CTG_OP_DROP_DB_VGROUP;
  op->syncOp = syncOp;
dengyihao's avatar
dengyihao 已提交
843

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

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

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

D
dapan1121 已提交
859
  op->data = msg;
D
dapan1121 已提交
860

D
dapan1121 已提交
861
  CTG_ERR_JRET(ctgEnqueue(pCtg, op));
D
dapan1121 已提交
862 863 864 865 866 867 868 869

  return TSDB_CODE_SUCCESS;

_return:

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
870 871 872
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 已提交
873 874 875
  SCtgCacheOperation *op = taosMemoryCalloc(1, sizeof(SCtgCacheOperation));
  op->opId = CTG_OP_DROP_STB_META;
  op->syncOp = syncOp;
dengyihao's avatar
dengyihao 已提交
876

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

  msg->pCtg = pCtg;
D
dapan1121 已提交
885 886
  tstrncpy(msg->dbFName, dbFName, sizeof(msg->dbFName));
  tstrncpy(msg->stbName, stbName, sizeof(msg->stbName));
D
dapan1121 已提交
887 888 889
  msg->dbId = dbId;
  msg->suid = suid;

D
dapan1121 已提交
890
  op->data = msg;
D
dapan1121 已提交
891

D
dapan1121 已提交
892
  CTG_ERR_JRET(ctgEnqueue(pCtg, op));
D
dapan1121 已提交
893 894 895 896 897 898 899 900

  return TSDB_CODE_SUCCESS;

_return:

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
901 902
int32_t ctgDropTbMetaEnqueue(SCatalog *pCtg, const char *dbFName, int64_t dbId, const char *tbName, bool syncOp) {
  int32_t             code = 0;
D
dapan1121 已提交
903 904 905
  SCtgCacheOperation *op = taosMemoryCalloc(1, sizeof(SCtgCacheOperation));
  op->opId = CTG_OP_DROP_TB_META;
  op->syncOp = syncOp;
dengyihao's avatar
dengyihao 已提交
906

D
dapan1121 已提交
907
  SCtgDropTblMetaMsg *msg = taosMemoryMalloc(sizeof(SCtgDropTblMetaMsg));
D
dapan1121 已提交
908
  if (NULL == msg) {
D
dapan1121 已提交
909
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgDropTblMetaMsg));
D
dapan1121 已提交
910
    taosMemoryFree(op);
D
dapan1121 已提交
911
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
912 913 914
  }

  msg->pCtg = pCtg;
D
dapan1121 已提交
915 916
  tstrncpy(msg->dbFName, dbFName, sizeof(msg->dbFName));
  tstrncpy(msg->tbName, tbName, sizeof(msg->tbName));
D
dapan1121 已提交
917 918
  msg->dbId = dbId;

D
dapan1121 已提交
919
  op->data = msg;
D
dapan1121 已提交
920

D
dapan1121 已提交
921
  CTG_ERR_JRET(ctgEnqueue(pCtg, op));
D
dapan1121 已提交
922 923 924 925 926 927 928 929

  return TSDB_CODE_SUCCESS;

_return:

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
930 931
int32_t ctgUpdateVgroupEnqueue(SCatalog *pCtg, const char *dbFName, int64_t dbId, SDBVgInfo *dbInfo, bool syncOp) {
  int32_t             code = 0;
D
dapan1121 已提交
932 933 934
  SCtgCacheOperation *op = taosMemoryCalloc(1, sizeof(SCtgCacheOperation));
  op->opId = CTG_OP_UPDATE_VGROUP;
  op->syncOp = syncOp;
dengyihao's avatar
dengyihao 已提交
935

D
dapan1121 已提交
936 937 938
  SCtgUpdateVgMsg *msg = taosMemoryMalloc(sizeof(SCtgUpdateVgMsg));
  if (NULL == msg) {
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgUpdateVgMsg));
D
dapan1121 已提交
939
    taosMemoryFree(op);
940
    freeVgInfo(dbInfo);
D
dapan1121 已提交
941
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
942 943 944
  }

  char *p = strchr(dbFName, '.');
D
dapan1121 已提交
945
  if (p && IS_SYS_DBNAME(p + 1)) {
D
dapan1121 已提交
946 947 948
    dbFName = p + 1;
  }

949 950 951 952 953 954
  code = ctgMakeVgArray(dbInfo);
  if (code) {
    taosMemoryFree(op);
    taosMemoryFree(msg);
    freeVgInfo(dbInfo);
    CTG_ERR_RET(code);
955 956
  }

D
dapan1121 已提交
957
  tstrncpy(msg->dbFName, dbFName, sizeof(msg->dbFName));
D
dapan1121 已提交
958 959 960 961
  msg->pCtg = pCtg;
  msg->dbId = dbId;
  msg->dbInfo = dbInfo;

D
dapan1121 已提交
962
  op->data = msg;
D
dapan1121 已提交
963

D
dapan1121 已提交
964
  CTG_ERR_JRET(ctgEnqueue(pCtg, op));
D
dapan1121 已提交
965 966 967 968 969

  return TSDB_CODE_SUCCESS;

_return:

970
  freeVgInfo(dbInfo);
D
dapan1121 已提交
971 972 973
  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
974 975
int32_t ctgUpdateTbMetaEnqueue(SCatalog *pCtg, STableMetaOutput *output, bool syncOp) {
  int32_t             code = 0;
D
dapan1121 已提交
976 977 978
  SCtgCacheOperation *op = taosMemoryCalloc(1, sizeof(SCtgCacheOperation));
  op->opId = CTG_OP_UPDATE_TB_META;
  op->syncOp = syncOp;
dengyihao's avatar
dengyihao 已提交
979

D
dapan1121 已提交
980
  SCtgUpdateTbMetaMsg *msg = taosMemoryMalloc(sizeof(SCtgUpdateTbMetaMsg));
D
dapan1121 已提交
981
  if (NULL == msg) {
D
dapan1121 已提交
982
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgUpdateTbMetaMsg));
D
dapan1121 已提交
983
    taosMemoryFree(op);
D
dapan1121 已提交
984
    CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
985 986 987
  }

  char *p = strchr(output->dbFName, '.');
D
dapan1121 已提交
988
  if (p && IS_SYS_DBNAME(p + 1)) {
D
dapan1121 已提交
989 990
    int32_t len = strlen(p + 1);
    memmove(output->dbFName, p + 1, len >= TSDB_DB_FNAME_LEN ? TSDB_DB_FNAME_LEN - 1 : len);
D
dapan1121 已提交
991 992 993
  }

  msg->pCtg = pCtg;
D
dapan1121 已提交
994
  msg->pMeta = output;
D
dapan1121 已提交
995

D
dapan1121 已提交
996
  op->data = msg;
D
dapan1121 已提交
997

D
dapan1121 已提交
998
  CTG_ERR_JRET(ctgEnqueue(pCtg, op));
D
dapan1121 已提交
999 1000

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

D
dapan1121 已提交
1002 1003
_return:

D
dapan1121 已提交
1004 1005 1006 1007 1008
  if (output) {
    taosMemoryFree(output->tbMeta);
    taosMemoryFree(output);
  }

D
dapan1121 已提交
1009 1010 1011
  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1012 1013
int32_t ctgUpdateVgEpsetEnqueue(SCatalog *pCtg, char *dbFName, int32_t vgId, SEpSet *pEpSet) {
  int32_t             code = 0;
D
dapan1121 已提交
1014 1015
  SCtgCacheOperation *op = taosMemoryCalloc(1, sizeof(SCtgCacheOperation));
  op->opId = CTG_OP_UPDATE_VG_EPSET;
dengyihao's avatar
dengyihao 已提交
1016

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

  msg->pCtg = pCtg;
D
dapan1121 已提交
1025
  tstrncpy(msg->dbFName, dbFName, sizeof(msg->dbFName));
D
dapan1121 已提交
1026 1027 1028
  msg->vgId = vgId;
  msg->epSet = *pEpSet;

D
dapan1121 已提交
1029
  op->data = msg;
D
dapan1121 已提交
1030

D
dapan1121 已提交
1031
  CTG_ERR_JRET(ctgEnqueue(pCtg, op));
D
dapan1121 已提交
1032 1033

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

D
dapan1121 已提交
1035 1036 1037 1038 1039
_return:

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1040 1041
int32_t ctgUpdateUserEnqueue(SCatalog *pCtg, SGetUserAuthRsp *pAuth, bool syncOp) {
  int32_t             code = 0;
D
dapan1121 已提交
1042 1043 1044
  SCtgCacheOperation *op = taosMemoryCalloc(1, sizeof(SCtgCacheOperation));
  op->opId = CTG_OP_UPDATE_USER;
  op->syncOp = syncOp;
dengyihao's avatar
dengyihao 已提交
1045

D
dapan1121 已提交
1046 1047 1048
  SCtgUpdateUserMsg *msg = taosMemoryMalloc(sizeof(SCtgUpdateUserMsg));
  if (NULL == msg) {
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgUpdateUserMsg));
D
dapan1121 已提交
1049
    taosMemoryFree(op);
D
dapan1121 已提交
1050
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1051 1052 1053 1054 1055
  }

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

D
dapan1121 已提交
1056
  op->data = msg;
D
dapan1121 已提交
1057

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

D
dapan1121 已提交
1060
  return TSDB_CODE_SUCCESS;
dengyihao's avatar
dengyihao 已提交
1061

D
dapan1121 已提交
1062 1063 1064
_return:

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

D
dapan1121 已提交
1066 1067 1068
  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1069 1070
int32_t ctgUpdateTbIndexEnqueue(SCatalog *pCtg, STableIndex **pIndex, bool syncOp) {
  int32_t             code = 0;
D
dapan1121 已提交
1071 1072 1073
  SCtgCacheOperation *op = taosMemoryCalloc(1, sizeof(SCtgCacheOperation));
  op->opId = CTG_OP_UPDATE_TB_INDEX;
  op->syncOp = syncOp;
dengyihao's avatar
dengyihao 已提交
1074

D
dapan1121 已提交
1075 1076 1077
  SCtgUpdateTbIndexMsg *msg = taosMemoryMalloc(sizeof(SCtgUpdateTbIndexMsg));
  if (NULL == msg) {
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgUpdateTbIndexMsg));
D
dapan1121 已提交
1078
    taosMemoryFree(op);
D
dapan1121 已提交
1079
    CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1080 1081 1082
  }

  msg->pCtg = pCtg;
D
dapan1121 已提交
1083
  msg->pIndex = *pIndex;
D
dapan1121 已提交
1084 1085 1086 1087

  op->data = msg;

  CTG_ERR_JRET(ctgEnqueue(pCtg, op));
D
dapan1121 已提交
1088 1089

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

D
dapan1121 已提交
1092 1093
_return:

D
dapan1121 已提交
1094
  taosArrayDestroyEx((*pIndex)->pIndex, tFreeSTableIndexInfo);
D
dapan1121 已提交
1095
  taosMemoryFreeClear(*pIndex);
dengyihao's avatar
dengyihao 已提交
1096

D
dapan1121 已提交
1097 1098 1099
  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1100 1101
int32_t ctgDropTbIndexEnqueue(SCatalog *pCtg, SName *pName, bool syncOp) {
  int32_t             code = 0;
D
dapan1121 已提交
1102 1103 1104
  SCtgCacheOperation *op = taosMemoryCalloc(1, sizeof(SCtgCacheOperation));
  op->opId = CTG_OP_DROP_TB_INDEX;
  op->syncOp = syncOp;
dengyihao's avatar
dengyihao 已提交
1105

D
dapan1121 已提交
1106 1107 1108
  SCtgDropTbIndexMsg *msg = taosMemoryMalloc(sizeof(SCtgDropTbIndexMsg));
  if (NULL == msg) {
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgDropTbIndexMsg));
D
dapan1121 已提交
1109
    taosMemoryFree(op);
D
dapan1121 已提交
1110
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1111 1112 1113 1114 1115 1116 1117 1118 1119
  }

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

D
dapan1121 已提交
1121
  return TSDB_CODE_SUCCESS;
dengyihao's avatar
dengyihao 已提交
1122

D
dapan1121 已提交
1123 1124 1125 1126 1127
_return:

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1128 1129
int32_t ctgClearCacheEnqueue(SCatalog *pCtg, bool freeCtg, bool stopQueue, bool syncOp) {
  int32_t             code = 0;
D
dapan1121 已提交
1130 1131 1132
  SCtgCacheOperation *op = taosMemoryCalloc(1, sizeof(SCtgCacheOperation));
  op->opId = CTG_OP_CLEAR_CACHE;
  op->syncOp = syncOp;
D
dapan1121 已提交
1133
  op->stopQueue = stopQueue;
1134
  op->unLocked = true;
dengyihao's avatar
dengyihao 已提交
1135

D
dapan1121 已提交
1136 1137 1138
  SCtgClearCacheMsg *msg = taosMemoryMalloc(sizeof(SCtgClearCacheMsg));
  if (NULL == msg) {
    ctgError("malloc %d failed", (int32_t)sizeof(SCtgClearCacheMsg));
D
dapan1121 已提交
1139
    taosMemoryFree(op);
D
dapan1121 已提交
1140
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1141 1142 1143
  }

  msg->pCtg = pCtg;
D
dapan1121 已提交
1144
  msg->freeCtg = freeCtg;
D
dapan1121 已提交
1145 1146 1147
  op->data = msg;

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

D
dapan1121 已提交
1149
  return TSDB_CODE_SUCCESS;
dengyihao's avatar
dengyihao 已提交
1150

D
dapan1121 已提交
1151 1152 1153 1154 1155
_return:

  CTG_RET(code);
}

D
dapan1121 已提交
1156 1157 1158 1159 1160 1161
int32_t ctgMetaRentInit(SCtgRentMgmt *mgmt, uint32_t rentSec, int8_t type) {
  mgmt->slotRIdx = 0;
  mgmt->slotNum = rentSec / CTG_RENT_SLOT_SECOND;
  mgmt->type = type;

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

D
dapan1121 已提交
1163 1164 1165
  mgmt->slots = taosMemoryCalloc(1, msgSize);
  if (NULL == mgmt->slots) {
    qError("calloc %d failed", (int32_t)msgSize);
D
dapan1121 已提交
1166
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1167 1168 1169
  }

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

D
dapan1121 已提交
1171 1172 1173 1174 1175 1176 1177
  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 已提交
1178 1179
  int32_t       code = 0;

D
dapan1121 已提交
1180 1181 1182 1183
  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 已提交
1184 1185
      qError("taosArrayInit %d failed, id:0x%" PRIx64 ", slot idx:%d, type:%d", CTG_DEFAULT_RENT_SLOT_SIZE, id, widx,
             mgmt->type);
D
dapan1121 已提交
1186
      CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1187 1188 1189 1190
    }
  }

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

  slot->needSort = true;

dengyihao's avatar
dengyihao 已提交
1197
  qDebug("add meta to rent, id:0x%" PRIx64 ", slot idx:%d, type:%d", id, widx, mgmt->type);
D
dapan1121 已提交
1198 1199 1200 1201 1202 1203 1204

_return:

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

dengyihao's avatar
dengyihao 已提交
1205 1206
int32_t ctgMetaRentUpdate(SCtgRentMgmt *mgmt, void *meta, int64_t id, int32_t size, __compar_fn_t sortCompare,
                          __compar_fn_t searchCompare) {
D
dapan1121 已提交
1207 1208 1209
  int16_t widx = abs((int)(id % mgmt->slotNum));

  SCtgRentSlot *slot = &mgmt->slots[widx];
dengyihao's avatar
dengyihao 已提交
1210
  int32_t       code = 0;
D
dapan1121 已提交
1211 1212 1213

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

  if (slot->needSort) {
dengyihao's avatar
dengyihao 已提交
1219 1220
    qDebug("meta slot before sorte, slot idx:%d, type:%d, size:%d", widx, mgmt->type,
           (int32_t)taosArrayGetSize(slot->meta));
D
dapan1121 已提交
1221 1222 1223 1224 1225 1226 1227
    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 已提交
1228 1229
    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 已提交
1230 1231 1232 1233 1234
    CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
  }

  memcpy(orig, meta, size);

dengyihao's avatar
dengyihao 已提交
1235
  qDebug("meta in rent updated, id:0x%" PRIx64 ", slot idx:%d, type:%d", id, widx, mgmt->type);
D
dapan1121 已提交
1236 1237 1238 1239 1240 1241

_return:

  CTG_UNLOCK(CTG_WRITE, &slot->lock);

  if (code) {
dengyihao's avatar
dengyihao 已提交
1242 1243
    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 已提交
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
    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 已提交
1254 1255
  int32_t       code = 0;

D
dapan1121 已提交
1256 1257
  CTG_LOCK(CTG_WRITE, &slot->lock);
  if (NULL == slot->meta) {
dengyihao's avatar
dengyihao 已提交
1258
    qError("empty meta slot, id:0x%" PRIx64 ", slot idx:%d, type:%d", id, widx, mgmt->type);
D
dapan1121 已提交
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
    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 已提交
1270
    qError("meta not found in slot, id:0x%" PRIx64 ", slot idx:%d, type:%d", id, widx, mgmt->type);
D
dapan1121 已提交
1271 1272 1273 1274 1275
    CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
  }

  taosArrayRemove(slot->meta, idx);

dengyihao's avatar
dengyihao 已提交
1276
  qDebug("meta in rent removed, id:0x%" PRIx64 ", slot idx:%d, type:%d", id, widx, mgmt->type);
D
dapan1121 已提交
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292

_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 已提交
1293 1294
  int32_t       code = 0;

D
dapan1121 已提交
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
  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 已提交
1313
    CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
  }

  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 已提交
1360 1361
  newDBCache.tbCache = taosHashInit(gCtgMgmt.cfg.maxTblCacheNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY),
                                    true, HASH_ENTRY_LOCK);
D
dapan1121 已提交
1362
  if (NULL == newDBCache.tbCache) {
D
dapan1121 已提交
1363
    ctgError("taosHashInit %d metaCache failed", gCtgMgmt.cfg.maxTblCacheNum);
D
dapan1121 已提交
1364
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1365 1366
  }

dengyihao's avatar
dengyihao 已提交
1367 1368
  newDBCache.stbCache = taosHashInit(gCtgMgmt.cfg.maxTblCacheNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT),
                                     true, HASH_ENTRY_LOCK);
D
dapan1121 已提交
1369
  if (NULL == newDBCache.stbCache) {
D
dapan1121 已提交
1370
    ctgError("taosHashInit %d stbCache failed", gCtgMgmt.cfg.maxTblCacheNum);
D
dapan1121 已提交
1371
    CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1372 1373 1374 1375 1376 1377 1378 1379
  }

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

D
dapan1121 已提交
1381
    ctgError("taosHashPut db to cache failed, dbFName:%s", dbFName);
D
dapan1121 已提交
1382
    CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1383 1384
  }

D
dapan1121 已提交
1385
  CTG_CACHE_STAT_INC(numOfDb, 1);
dengyihao's avatar
dengyihao 已提交
1386

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

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

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

D
dapan1121 已提交
1395 1396
    ctgDebug("db added to rent, dbFName:%s, vgVersion:%d, dbId:0x%" PRIx64, dbFName, vgVersion.vgVersion, dbId);
  }
D
dapan1121 已提交
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406

  return TSDB_CODE_SUCCESS;

_return:

  ctgFreeDbCache(&newDBCache);

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1407
void ctgRemoveStbRent(SCatalog *pCtg, SCtgDBCache *dbCache) {
D
dapan1121 已提交
1408 1409 1410
  if (NULL == dbCache->stbCache) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
1411

D
dapan1121 已提交
1412 1413 1414 1415
  void *pIter = taosHashIterate(dbCache->stbCache, NULL);
  while (pIter) {
    uint64_t *suid = NULL;
    suid = taosHashGetKey(pIter, NULL);
D
dapan1121 已提交
1416

dengyihao's avatar
dengyihao 已提交
1417 1418 1419
    if (TSDB_CODE_SUCCESS ==
        ctgMetaRentRemove(&pCtg->stbRent, *suid, ctgStbVersionSortCompare, ctgStbVersionSearchCompare)) {
      ctgDebug("stb removed from rent, suid:0x%" PRIx64, *suid);
D
dapan1121 已提交
1420
    }
dengyihao's avatar
dengyihao 已提交
1421

D
dapan1121 已提交
1422
    pIter = taosHashIterate(dbCache->stbCache, pIter);
D
dapan1121 已提交
1423 1424 1425
  }
}

dengyihao's avatar
dengyihao 已提交
1426
int32_t ctgRemoveDBFromCache(SCatalog *pCtg, SCtgDBCache *dbCache, const char *dbFName) {
D
dapan1121 已提交
1427
  uint64_t dbId = dbCache->dbId;
dengyihao's avatar
dengyihao 已提交
1428 1429

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

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

D
dapan1121 已提交
1433
  atomic_store_8(&dbCache->deleted, 1);
D
dapan1121 已提交
1434
  ctgRemoveStbRent(pCtg, dbCache);
D
dapan1121 已提交
1435 1436
  ctgFreeDbCache(dbCache);

D
dapan1121 已提交
1437 1438 1439
  CTG_UNLOCK(CTG_WRITE, &dbCache->dbLock);

  CTG_ERR_RET(ctgMetaRentRemove(&pCtg->dbRent, dbId, ctgDbVgVersionSortCompare, ctgDbVgVersionSearchCompare));
dengyihao's avatar
dengyihao 已提交
1440
  ctgDebug("db removed from rent, dbFName:%s, dbId:0x%" PRIx64, dbFName, dbId);
D
dapan1121 已提交
1441 1442 1443 1444 1445 1446

  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 已提交
1447
  CTG_CACHE_STAT_DEC(numOfDb, 1);
dengyihao's avatar
dengyihao 已提交
1448 1449
  ctgInfo("db removed from cache, dbFName:%s, dbId:0x%" PRIx64, dbFName, dbId);

D
dapan1121 已提交
1450 1451 1452
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
1453 1454
int32_t ctgGetAddDBCache(SCatalog *pCtg, const char *dbFName, uint64_t dbId, SCtgDBCache **pCache) {
  int32_t      code = 0;
D
dapan1121 已提交
1455 1456
  SCtgDBCache *dbCache = NULL;
  ctgGetDBCache(pCtg, dbFName, &dbCache);
dengyihao's avatar
dengyihao 已提交
1457

D
dapan1121 已提交
1458
  if (dbCache) {
dengyihao's avatar
dengyihao 已提交
1459
    // TODO OPEN IT
D
dapan1121 已提交
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
#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 已提交
1476

D
dapan1121 已提交
1477 1478 1479 1480 1481 1482 1483
    if (dbCache->dbId == dbId) {
      *pCache = dbCache;
      return TSDB_CODE_SUCCESS;
    }
#endif
    CTG_ERR_RET(ctgRemoveDBFromCache(pCtg, dbCache, dbFName));
  }
dengyihao's avatar
dengyihao 已提交
1484

D
dapan1121 已提交
1485 1486 1487 1488 1489 1490 1491 1492 1493
  CTG_ERR_RET(ctgAddNewDBCache(pCtg, dbFName, dbId));

  ctgGetDBCache(pCtg, dbFName, &dbCache);

  *pCache = dbCache;

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
1494 1495
int32_t ctgUpdateRentStbVersion(SCatalog *pCtg, char *dbFName, char *tbName, uint64_t dbId, uint64_t suid,
                                SCtgTbCache *pCache) {
D
dapan1121 已提交
1496 1497 1498 1499 1500 1501 1502 1503 1504
  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 已提交
1505

D
dapan1121 已提交
1506 1507
  tstrncpy(metaRent.dbFName, dbFName, sizeof(metaRent.dbFName));
  tstrncpy(metaRent.stbName, tbName, sizeof(metaRent.stbName));
D
dapan1121 已提交
1508

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

dengyihao's avatar
dengyihao 已提交
1512 1513
  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 已提交
1514

dengyihao's avatar
dengyihao 已提交
1515 1516
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
1517

dengyihao's avatar
dengyihao 已提交
1518 1519
int32_t ctgWriteTbMetaToCache(SCatalog *pCtg, SCtgDBCache *dbCache, char *dbFName, uint64_t dbId, char *tbName,
                              STableMeta *meta, int32_t metaSize) {
D
dapan1121 已提交
1520 1521
  if (NULL == dbCache->tbCache || NULL == dbCache->stbCache) {
    taosMemoryFree(meta);
dengyihao's avatar
dengyihao 已提交
1522
    ctgError("db is dropping, dbId:0x%" PRIx64, dbCache->dbId);
D
dapan1121 已提交
1523 1524 1525
    CTG_ERR_RET(TSDB_CODE_CTG_DB_DROPPED);
  }

dengyihao's avatar
dengyihao 已提交
1526 1527 1528 1529 1530
  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 已提交
1531 1532 1533
  if (orig) {
    origType = orig->tableType;

dengyihao's avatar
dengyihao 已提交
1534 1535
    if (origType == meta->tableType && orig->uid == meta->uid &&
        (origType == TSDB_CHILD_TABLE || (orig->sversion >= meta->sversion && orig->tversion >= meta->tversion))) {
D
dapan1121 已提交
1536 1537
      taosMemoryFree(meta);
      ctgDebug("ignore table %s meta update", tbName);
D
dapan1121 已提交
1538 1539
      return TSDB_CODE_SUCCESS;
    }
dengyihao's avatar
dengyihao 已提交
1540

D
dapan1121 已提交
1541
    if (origType == TSDB_SUPER_TABLE) {
D
dapan1121 已提交
1542
      if (taosHashRemove(dbCache->stbCache, &orig->suid, sizeof(orig->suid))) {
dengyihao's avatar
dengyihao 已提交
1543
        ctgError("stb not exist in stbCache, dbFName:%s, stb:%s, suid:0x%" PRIx64, dbFName, tbName, orig->suid);
D
dapan1121 已提交
1544
      } else {
D
dapan1121 已提交
1545
        CTG_CACHE_STAT_DEC(numOfStb, 1);
dengyihao's avatar
dengyihao 已提交
1546
        ctgDebug("stb removed from stbCache, dbFName:%s, stb:%s, suid:0x%" PRIx64, dbFName, tbName, orig->suid);
D
dapan1121 已提交
1547 1548 1549 1550
      }
    }
  }

D
dapan1121 已提交
1551 1552 1553 1554 1555
  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);
1556
      taosMemoryFree(meta);
D
dapan1121 已提交
1557
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1558
    }
dengyihao's avatar
dengyihao 已提交
1559

D
dapan1121 已提交
1560 1561
    pCache = taosHashGet(dbCache->tbCache, tbName, strlen(tbName));
  } else {
1562
    CTG_LOCK(CTG_WRITE, &pCache->metaLock);
D
dapan1121 已提交
1563 1564
    taosMemoryFree(pCache->pMeta);
    pCache->pMeta = meta;
1565
    CTG_UNLOCK(CTG_WRITE, &pCache->metaLock);
D
dapan1121 已提交
1566 1567 1568
  }

  if (NULL == orig) {
D
dapan1121 已提交
1569
    CTG_CACHE_STAT_INC(numOfTbl, 1);
D
dapan1121 已提交
1570 1571 1572 1573 1574 1575 1576 1577 1578
  }

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

D
dapan1121 已提交
1584
  CTG_CACHE_STAT_INC(numOfStb, 1);
D
dapan1121 已提交
1585

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

D
dapan1121 已提交
1589 1590 1591
  if (pCache) {
    CTG_ERR_RET(ctgUpdateRentStbVersion(pCtg, dbFName, tbName, dbId, meta->suid, pCache));
  }
dengyihao's avatar
dengyihao 已提交
1592

D
dapan1121 已提交
1593 1594 1595
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
1596
int32_t ctgWriteTbIndexToCache(SCatalog *pCtg, SCtgDBCache *dbCache, char *dbFName, char *tbName, STableIndex **index) {
D
dapan1121 已提交
1597
  if (NULL == dbCache->tbCache) {
D
dapan1121 已提交
1598
    ctgFreeSTableIndex(*index);
D
dapan1121 已提交
1599
    taosMemoryFreeClear(*index);
dengyihao's avatar
dengyihao 已提交
1600
    ctgError("db is dropping, dbId:0x%" PRIx64, dbCache->dbId);
D
dapan1121 已提交
1601 1602 1603
    CTG_ERR_RET(TSDB_CODE_CTG_DB_DROPPED);
  }

dengyihao's avatar
dengyihao 已提交
1604 1605 1606
  STableIndex *pIndex = *index;
  uint64_t     suid = pIndex->suid;
  SCtgTbCache *pCache = taosHashGet(dbCache->tbCache, tbName, strlen(tbName));
D
dapan1121 已提交
1607 1608 1609
  if (NULL == pCache) {
    SCtgTbCache cache = {0};
    cache.pIndex = pIndex;
dengyihao's avatar
dengyihao 已提交
1610

D
dapan1121 已提交
1611 1612
    if (taosHashPut(dbCache->tbCache, tbName, strlen(tbName), &cache, sizeof(cache)) != 0) {
      ctgFreeSTableIndex(*index);
D
dapan1121 已提交
1613 1614
      taosMemoryFreeClear(*index);
      ctgError("taosHashPut new tbCache failed, tbName:%s", tbName);
D
dapan1121 已提交
1615
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1616 1617 1618
    }

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

D
dapan1121 已提交
1622
    if (suid) {
D
dapan1121 已提交
1623
      CTG_ERR_RET(ctgUpdateRentStbVersion(pCtg, dbFName, tbName, dbCache->dbId, pIndex->suid, &cache));
D
dapan1121 已提交
1624
    }
dengyihao's avatar
dengyihao 已提交
1625

D
dapan1121 已提交
1626 1627 1628
    return TSDB_CODE_SUCCESS;
  }

1629 1630
  CTG_LOCK(CTG_WRITE, &pCache->indexLock);

D
dapan1121 已提交
1631
  if (pCache->pIndex) {
D
dapan1121 已提交
1632 1633 1634
    if (0 == suid) {
      suid = pCache->pIndex->suid;
    }
D
dapan1121 已提交
1635 1636 1637 1638 1639
    taosArrayDestroyEx(pCache->pIndex->pIndex, tFreeSTableIndexInfo);
    taosMemoryFreeClear(pCache->pIndex);
  }

  pCache->pIndex = pIndex;
1640 1641
  CTG_UNLOCK(CTG_WRITE, &pCache->indexLock);

D
dapan1121 已提交
1642 1643
  *index = NULL;

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

D
dapan1121 已提交
1647 1648 1649
  if (suid) {
    CTG_ERR_RET(ctgUpdateRentStbVersion(pCtg, dbFName, tbName, dbCache->dbId, suid, pCache));
  }
dengyihao's avatar
dengyihao 已提交
1650

D
dapan1121 已提交
1651 1652 1653
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
1654 1655 1656 1657
int32_t ctgUpdateTbMetaToCache(SCatalog *pCtg, STableMetaOutput *pOut, bool syncReq) {
  STableMetaOutput *pOutput = NULL;
  int32_t           code = 0;

D
dapan1121 已提交
1658
  CTG_ERR_RET(ctgCloneMetaOutput(pOut, &pOutput));
D
dapan1121 已提交
1659 1660 1661
  code = ctgUpdateTbMetaEnqueue(pCtg, pOutput, syncReq);
  pOutput = NULL;
  CTG_ERR_JRET(code);
D
dapan1121 已提交
1662 1663

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

D
dapan1121 已提交
1665 1666 1667 1668 1669 1670
_return:

  ctgFreeSTableMetaOutput(pOutput);
  CTG_RET(code);
}

D
dapan1121 已提交
1671
void ctgClearAllInstance(void) {
dengyihao's avatar
dengyihao 已提交
1672
  SCatalog *pCtg = NULL;
1673

dengyihao's avatar
dengyihao 已提交
1674
  void *pIter = taosHashIterate(gCtgMgmt.pCluster, NULL);
1675
  while (pIter) {
dengyihao's avatar
dengyihao 已提交
1676
    pCtg = *(SCatalog **)pIter;
1677 1678

    if (pCtg) {
D
dapan1121 已提交
1679 1680 1681 1682 1683 1684 1685 1686
      ctgClearHandle(pCtg);
    }

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

void ctgFreeAllInstance(void) {
dengyihao's avatar
dengyihao 已提交
1687
  SCatalog *pCtg = NULL;
D
dapan1121 已提交
1688

dengyihao's avatar
dengyihao 已提交
1689
  void *pIter = taosHashIterate(gCtgMgmt.pCluster, NULL);
D
dapan1121 已提交
1690
  while (pIter) {
dengyihao's avatar
dengyihao 已提交
1691
    pCtg = *(SCatalog **)pIter;
D
dapan1121 已提交
1692 1693 1694

    if (pCtg) {
      ctgFreeHandle(pCtg);
1695 1696 1697 1698 1699 1700 1701 1702
    }

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

  taosHashClear(gCtgMgmt.pCluster);
}

1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716
int32_t ctgVgInfoIdComp(void const* lp, void const* rp) {
  int32_t*    key = (int32_t*)lp;
  SVgroupInfo* pVg = (SVgroupInfo*)rp;

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

  return 0;
}


D
dapan1121 已提交
1717
int32_t ctgOpUpdateVgroup(SCtgCacheOperation *operation) {
dengyihao's avatar
dengyihao 已提交
1718
  int32_t          code = 0;
D
dapan1121 已提交
1719
  SCtgUpdateVgMsg *msg = operation->data;
dengyihao's avatar
dengyihao 已提交
1720 1721 1722 1723
  SDBVgInfo       *dbInfo = msg->dbInfo;
  char            *dbFName = msg->dbFName;
  SCatalog        *pCtg = msg->pCtg;

D
dapan1121 已提交
1724
  if (pCtg->stopUpdate || NULL == dbInfo->vgHash) {
D
dapan1121 已提交
1725
    goto _return;
D
dapan1121 已提交
1726
  }
dengyihao's avatar
dengyihao 已提交
1727

D
dapan1121 已提交
1728
  if (dbInfo->vgVersion < 0 || taosHashGetSize(dbInfo->vgHash) <= 0) {
D
dapan1121 已提交
1729
    ctgDebug("invalid db vgInfo, dbFName:%s, vgHash:%p, vgVersion:%d, vgHashSize:%d", dbFName, dbInfo->vgHash,
dengyihao's avatar
dengyihao 已提交
1730
             dbInfo->vgVersion, taosHashGetSize(dbInfo->vgHash));
D
dapan1121 已提交
1731
    CTG_ERR_JRET(TSDB_CODE_APP_ERROR);
D
dapan1121 已提交
1732 1733
  }

dengyihao's avatar
dengyihao 已提交
1734
  bool         newAdded = false;
dengyihao's avatar
dengyihao 已提交
1735 1736
  SDbVgVersion vgVersion = {
      .dbId = msg->dbId, .vgVersion = dbInfo->vgVersion, .numOfTable = dbInfo->numOfTable, .stateTs = dbInfo->stateTs};
D
dapan1121 已提交
1737 1738

  SCtgDBCache *dbCache = NULL;
D
dapan1121 已提交
1739
  CTG_ERR_JRET(ctgGetAddDBCache(msg->pCtg, dbFName, msg->dbId, &dbCache));
D
dapan1121 已提交
1740
  if (NULL == dbCache) {
dengyihao's avatar
dengyihao 已提交
1741
    ctgInfo("conflict db update, ignore this update, dbFName:%s, dbId:0x%" PRIx64, dbFName, msg->dbId);
D
dapan1121 已提交
1742
    CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
D
dapan1121 已提交
1743 1744 1745
  }

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

D
dapan1121 已提交
1748 1749
  if (vgCache->vgInfo) {
    SDBVgInfo *vgInfo = vgCache->vgInfo;
dengyihao's avatar
dengyihao 已提交
1750

D
dapan1121 已提交
1751
    if (dbInfo->vgVersion < vgInfo->vgVersion) {
dengyihao's avatar
dengyihao 已提交
1752 1753
      ctgDebug("db updateVgroup is ignored, dbFName:%s, vgVer:%d, curVer:%d", dbFName, dbInfo->vgVersion,
               vgInfo->vgVersion);
D
dapan1121 已提交
1754
      ctgWUnlockVgInfo(dbCache);
dengyihao's avatar
dengyihao 已提交
1755

D
dapan1121 已提交
1756
      goto _return;
D
dapan1121 已提交
1757 1758
    }

dengyihao's avatar
dengyihao 已提交
1759 1760 1761 1762
    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 已提交
1763
      ctgWUnlockVgInfo(dbCache);
dengyihao's avatar
dengyihao 已提交
1764

D
dapan1121 已提交
1765
      goto _return;
D
dapan1121 已提交
1766 1767
    }

1768
    freeVgInfo(vgInfo);
D
dapan1121 已提交
1769 1770 1771 1772 1773
  }

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

dengyihao's avatar
dengyihao 已提交
1774 1775
  ctgDebug("db vgInfo updated, dbFName:%s, vgVer:%d, stateTs:%" PRId64 ", dbId:0x%" PRIx64, dbFName,
           vgVersion.vgVersion, vgVersion.stateTs, vgVersion.dbId);
D
dapan1121 已提交
1776 1777 1778 1779 1780

  ctgWUnlockVgInfo(dbCache);

  dbCache = NULL;

1781
  //if (!IS_SYS_DBNAME(dbFName)) {
D
dapan1121 已提交
1782 1783 1784
    tstrncpy(vgVersion.dbFName, dbFName, sizeof(vgVersion.dbFName));
    CTG_ERR_JRET(ctgMetaRentUpdate(&msg->pCtg->dbRent, &vgVersion, vgVersion.dbId, sizeof(SDbVgVersion),
                                   ctgDbVgVersionSortCompare, ctgDbVgVersionSearchCompare));
1785
  //}
D
dapan1121 已提交
1786 1787 1788

_return:

1789
  freeVgInfo(msg->dbInfo);
D
dapan1121 已提交
1790
  taosMemoryFreeClear(msg);
dengyihao's avatar
dengyihao 已提交
1791

D
dapan1121 已提交
1792 1793 1794
  CTG_RET(code);
}

D
dapan1121 已提交
1795
int32_t ctgOpDropDbCache(SCtgCacheOperation *operation) {
dengyihao's avatar
dengyihao 已提交
1796
  int32_t        code = 0;
D
dapan1121 已提交
1797
  SCtgDropDBMsg *msg = operation->data;
dengyihao's avatar
dengyihao 已提交
1798
  SCatalog      *pCtg = msg->pCtg;
D
dapan1121 已提交
1799

D
dapan1121 已提交
1800 1801 1802 1803
  if (pCtg->stopUpdate) {
    goto _return;
  }

D
dapan1121 已提交
1804 1805 1806 1807 1808
  SCtgDBCache *dbCache = NULL;
  ctgGetDBCache(msg->pCtg, msg->dbFName, &dbCache);
  if (NULL == dbCache) {
    goto _return;
  }
dengyihao's avatar
dengyihao 已提交
1809

1810
  if (msg->dbId && dbCache->dbId != msg->dbId) {
dengyihao's avatar
dengyihao 已提交
1811 1812
    ctgInfo("dbId already updated, dbFName:%s, dbId:0x%" PRIx64 ", targetId:0x%" PRIx64, msg->dbFName, dbCache->dbId,
            msg->dbId);
D
dapan1121 已提交
1813 1814
    goto _return;
  }
dengyihao's avatar
dengyihao 已提交
1815

D
dapan1121 已提交
1816 1817 1818 1819 1820
  CTG_ERR_JRET(ctgRemoveDBFromCache(pCtg, dbCache, msg->dbFName));

_return:

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

D
dapan1121 已提交
1822 1823 1824
  CTG_RET(code);
}

D
dapan1121 已提交
1825
int32_t ctgOpDropDbVgroup(SCtgCacheOperation *operation) {
dengyihao's avatar
dengyihao 已提交
1826
  int32_t              code = 0;
D
dapan1121 已提交
1827
  SCtgDropDbVgroupMsg *msg = operation->data;
dengyihao's avatar
dengyihao 已提交
1828
  SCatalog            *pCtg = msg->pCtg;
D
dapan1121 已提交
1829

D
dapan1121 已提交
1830 1831 1832 1833
  if (pCtg->stopUpdate) {
    goto _return;
  }

D
dapan1121 已提交
1834 1835 1836 1837 1838
  SCtgDBCache *dbCache = NULL;
  ctgGetDBCache(msg->pCtg, msg->dbFName, &dbCache);
  if (NULL == dbCache) {
    goto _return;
  }
dengyihao's avatar
dengyihao 已提交
1839

dengyihao's avatar
dengyihao 已提交
1840
  CTG_ERR_JRET(ctgWLockVgInfo(pCtg, dbCache));
dengyihao's avatar
dengyihao 已提交
1841

1842
  freeVgInfo(dbCache->vgCache.vgInfo);
D
dapan1121 已提交
1843
  dbCache->vgCache.vgInfo = NULL;
D
dapan1121 已提交
1844 1845 1846

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

D
dapan1121 已提交
1847
  ctgWUnlockVgInfo(dbCache);
D
dapan1121 已提交
1848 1849 1850 1851

_return:

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

D
dapan1121 已提交
1853 1854 1855
  CTG_RET(code);
}

D
dapan1121 已提交
1856
int32_t ctgOpUpdateTbMeta(SCtgCacheOperation *operation) {
dengyihao's avatar
dengyihao 已提交
1857
  int32_t              code = 0;
D
dapan1121 已提交
1858
  SCtgUpdateTbMetaMsg *msg = operation->data;
dengyihao's avatar
dengyihao 已提交
1859 1860 1861
  SCatalog            *pCtg = msg->pCtg;
  STableMetaOutput    *pMeta = msg->pMeta;
  SCtgDBCache         *dbCache = NULL;
D
dapan1121 已提交
1862

D
dapan1121 已提交
1863 1864 1865
  if (pCtg->stopUpdate) {
    goto _return;
  }
dengyihao's avatar
dengyihao 已提交
1866

D
dapan1121 已提交
1867 1868
  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 已提交
1869 1870 1871
    CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
  }

D
dapan1121 已提交
1872 1873
  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 已提交
1874
    CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
dengyihao's avatar
dengyihao 已提交
1875 1876
  }

D
dapan1121 已提交
1877
  CTG_ERR_JRET(ctgGetAddDBCache(pCtg, pMeta->dbFName, pMeta->dbId, &dbCache));
D
dapan1121 已提交
1878
  if (NULL == dbCache) {
D
dapan1121 已提交
1879
    ctgInfo("conflict db update, ignore this update, dbFName:%s, dbId:0x%" PRIx64, pMeta->dbFName, pMeta->dbId);
D
dapan1121 已提交
1880 1881 1882
    CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
  }

D
dapan1121 已提交
1883 1884
  if (CTG_IS_META_TABLE(pMeta->metaType) || CTG_IS_META_BOTH(pMeta->metaType)) {
    int32_t metaSize = CTG_META_SIZE(pMeta->tbMeta);
D
dapan1121 已提交
1885
    code = ctgWriteTbMetaToCache(pCtg, dbCache, pMeta->dbFName, pMeta->dbId, pMeta->tbName, pMeta->tbMeta, metaSize);
D
dapan1121 已提交
1886
    pMeta->tbMeta = NULL;
D
dapan1121 已提交
1887
    CTG_ERR_JRET(code);
D
dapan1121 已提交
1888 1889
  }

D
dapan1121 已提交
1890
  if (CTG_IS_META_CTABLE(pMeta->metaType) || CTG_IS_META_BOTH(pMeta->metaType)) {
dengyihao's avatar
dengyihao 已提交
1891
    SCTableMeta *ctbMeta = taosMemoryMalloc(sizeof(SCTableMeta));
D
dapan1121 已提交
1892 1893 1894 1895
    if (NULL == ctbMeta) {
      CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
    }
    memcpy(ctbMeta, &pMeta->ctbMeta, sizeof(SCTableMeta));
dengyihao's avatar
dengyihao 已提交
1896 1897
    CTG_ERR_JRET(ctgWriteTbMetaToCache(pCtg, dbCache, pMeta->dbFName, pMeta->dbId, pMeta->ctbName,
                                       (STableMeta *)ctbMeta, sizeof(SCTableMeta)));
D
dapan1121 已提交
1898 1899 1900 1901
  }

_return:

D
dapan1121 已提交
1902 1903
  taosMemoryFreeClear(pMeta->tbMeta);
  taosMemoryFreeClear(pMeta);
dengyihao's avatar
dengyihao 已提交
1904

D
dapan1121 已提交
1905
  taosMemoryFreeClear(msg);
dengyihao's avatar
dengyihao 已提交
1906

D
dapan1121 已提交
1907 1908 1909
  CTG_RET(code);
}

D
dapan1121 已提交
1910
int32_t ctgOpDropStbMeta(SCtgCacheOperation *operation) {
dengyihao's avatar
dengyihao 已提交
1911
  int32_t             code = 0;
D
dapan1121 已提交
1912
  SCtgDropStbMetaMsg *msg = operation->data;
dengyihao's avatar
dengyihao 已提交
1913
  SCatalog           *pCtg = msg->pCtg;
D
dapan1121 已提交
1914

D
dapan1121 已提交
1915 1916 1917 1918
  if (pCtg->stopUpdate) {
    goto _return;
  }

D
dapan1121 已提交
1919 1920 1921
  SCtgDBCache *dbCache = NULL;
  ctgGetDBCache(pCtg, msg->dbFName, &dbCache);
  if (NULL == dbCache) {
dengyihao's avatar
dengyihao 已提交
1922
    goto _return;
D
dapan1121 已提交
1923 1924 1925
  }

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

D
dapan1121 已提交
1931
  if (taosHashRemove(dbCache->stbCache, &msg->suid, sizeof(msg->suid))) {
dengyihao's avatar
dengyihao 已提交
1932 1933
    ctgDebug("stb not exist in stbCache, may be removed, dbFName:%s, stb:%s, suid:0x%" PRIx64, msg->dbFName,
             msg->stbName, msg->suid);
D
dapan1121 已提交
1934
  } else {
D
dapan1121 已提交
1935
    CTG_CACHE_STAT_DEC(numOfStb, 1);
D
dapan1121 已提交
1936 1937
  }

dengyihao's avatar
dengyihao 已提交
1938
  SCtgTbCache *pTbCache = taosHashGet(dbCache->tbCache, msg->stbName, strlen(msg->stbName));
D
dapan1121 已提交
1939 1940 1941 1942 1943 1944 1945 1946 1947
  if (NULL == pTbCache) {
    ctgDebug("stb %s already not in cache", msg->stbName);
    goto _return;
  }

  CTG_LOCK(CTG_WRITE, &pTbCache->metaLock);
  ctgFreeTbCacheImpl(pTbCache);
  CTG_UNLOCK(CTG_WRITE, &pTbCache->metaLock);

dengyihao's avatar
dengyihao 已提交
1948 1949
  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 已提交
1950
  } else {
D
dapan1121 已提交
1951
    CTG_CACHE_STAT_DEC(numOfTbl, 1);
D
dapan1121 已提交
1952
  }
dengyihao's avatar
dengyihao 已提交
1953 1954

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

  CTG_ERR_JRET(ctgMetaRentRemove(&msg->pCtg->stbRent, msg->suid, ctgStbVersionSortCompare, ctgStbVersionSearchCompare));
dengyihao's avatar
dengyihao 已提交
1957 1958 1959

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

D
dapan1121 已提交
1960 1961 1962
_return:

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

D
dapan1121 已提交
1964 1965 1966
  CTG_RET(code);
}

D
dapan1121 已提交
1967
int32_t ctgOpDropTbMeta(SCtgCacheOperation *operation) {
dengyihao's avatar
dengyihao 已提交
1968
  int32_t             code = 0;
D
dapan1121 已提交
1969
  SCtgDropTblMetaMsg *msg = operation->data;
dengyihao's avatar
dengyihao 已提交
1970
  SCatalog           *pCtg = msg->pCtg;
D
dapan1121 已提交
1971

D
dapan1121 已提交
1972 1973 1974 1975
  if (pCtg->stopUpdate) {
    goto _return;
  }

D
dapan1121 已提交
1976 1977 1978
  SCtgDBCache *dbCache = NULL;
  ctgGetDBCache(pCtg, msg->dbFName, &dbCache);
  if (NULL == dbCache) {
D
dapan1121 已提交
1979
    goto _return;
D
dapan1121 已提交
1980 1981 1982
  }

  if (dbCache->dbId != msg->dbId) {
dengyihao's avatar
dengyihao 已提交
1983 1984
    ctgDebug("dbId 0x%" PRIx64 " not match with curId 0x%" PRIx64 ", dbFName:%s, tbName:%s", msg->dbId, dbCache->dbId,
             msg->dbFName, msg->tbName);
D
dapan1121 已提交
1985
    goto _return;
D
dapan1121 已提交
1986 1987
  }

dengyihao's avatar
dengyihao 已提交
1988
  SCtgTbCache *pTbCache = taosHashGet(dbCache->tbCache, msg->tbName, strlen(msg->tbName));
D
dapan1121 已提交
1989 1990 1991 1992 1993 1994 1995 1996
  if (NULL == pTbCache) {
    ctgDebug("tb %s already not in cache", msg->tbName);
    goto _return;
  }

  CTG_LOCK(CTG_WRITE, &pTbCache->metaLock);
  ctgFreeTbCacheImpl(pTbCache);
  CTG_UNLOCK(CTG_WRITE, &pTbCache->metaLock);
dengyihao's avatar
dengyihao 已提交
1997

D
dapan1121 已提交
1998 1999 2000
  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 已提交
2001
  } else {
D
dapan1121 已提交
2002
    CTG_CACHE_STAT_DEC(numOfTbl, 1);
D
dapan1121 已提交
2003 2004
  }

D
dapan1121 已提交
2005
  ctgDebug("table %s removed from cache, dbFName:%s", msg->tbName, msg->dbFName);
D
dapan1121 已提交
2006 2007 2008 2009 2010 2011 2012 2013

_return:

  taosMemoryFreeClear(msg);

  CTG_RET(code);
}

D
dapan1121 已提交
2014
int32_t ctgOpUpdateUser(SCtgCacheOperation *operation) {
dengyihao's avatar
dengyihao 已提交
2015
  int32_t            code = 0;
D
dapan1121 已提交
2016
  SCtgUpdateUserMsg *msg = operation->data;
dengyihao's avatar
dengyihao 已提交
2017 2018
  SCatalog          *pCtg = msg->pCtg;

D
dapan1121 已提交
2019 2020 2021 2022
  if (pCtg->stopUpdate) {
    goto _return;
  }

D
dapan1121 已提交
2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065
  SCtgUserAuth *pUser = (SCtgUserAuth *)taosHashGet(pCtg->userCache, msg->userAuth.user, strlen(msg->userAuth.user));
  if (NULL == pUser) {
    SCtgUserAuth userAuth = {0};

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

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

    taosMemoryFreeClear(msg);

    return TSDB_CODE_SUCCESS;
  }

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

  CTG_LOCK(CTG_WRITE, &pUser->lock);

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

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

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

  CTG_UNLOCK(CTG_WRITE, &pUser->lock);

_return:

  taosHashCleanup(msg->userAuth.createdDbs);
  taosHashCleanup(msg->userAuth.readDbs);
  taosHashCleanup(msg->userAuth.writeDbs);
dengyihao's avatar
dengyihao 已提交
2066

D
dapan1121 已提交
2067
  taosMemoryFreeClear(msg);
dengyihao's avatar
dengyihao 已提交
2068

D
dapan1121 已提交
2069 2070 2071
  CTG_RET(code);
}

D
dapan1121 已提交
2072
int32_t ctgOpUpdateEpset(SCtgCacheOperation *operation) {
dengyihao's avatar
dengyihao 已提交
2073
  int32_t             code = 0;
D
dapan1121 已提交
2074
  SCtgUpdateEpsetMsg *msg = operation->data;
dengyihao's avatar
dengyihao 已提交
2075
  SCatalog           *pCtg = msg->pCtg;
dengyihao's avatar
dengyihao 已提交
2076
  SCtgDBCache        *dbCache = NULL;
D
dapan1121 已提交
2077 2078 2079 2080 2081

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

D
dapan1121 已提交
2082
  CTG_ERR_JRET(ctgGetDBCache(pCtg, msg->dbFName, &dbCache));
D
dapan1121 已提交
2083 2084 2085 2086 2087
  if (NULL == dbCache) {
    ctgDebug("db %s not exist, ignore epset update", msg->dbFName);
    goto _return;
  }

D
dapan1121 已提交
2088 2089
  CTG_ERR_JRET(ctgWLockVgInfo(pCtg, dbCache));

dengyihao's avatar
dengyihao 已提交
2090
  SDBVgInfo *vgInfo = dbCache->vgCache.vgInfo;
D
dapan1121 已提交
2091
  if (NULL == vgInfo) {
D
dapan1121 已提交
2092 2093 2094
    ctgDebug("vgroup in db %s not cached, ignore epset update", msg->dbFName);
    goto _return;
  }
dengyihao's avatar
dengyihao 已提交
2095 2096

  SVgroupInfo *pInfo = taosHashGet(vgInfo->vgHash, &msg->vgId, sizeof(msg->vgId));
D
dapan1121 已提交
2097
  if (NULL == pInfo) {
2098 2099 2100 2101 2102 2103 2104
    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 已提交
2105 2106 2107
    goto _return;
  }

dengyihao's avatar
dengyihao 已提交
2108 2109 2110 2111 2112
  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 已提交
2113

D
dapan1121 已提交
2114
  pInfo->epSet = msg->epSet;
2115
  pInfo2->epSet = msg->epSet;
D
dapan1121 已提交
2116 2117 2118

_return:

D
dapan1121 已提交
2119
  if (code == TSDB_CODE_SUCCESS && dbCache) {
D
dapan1121 已提交
2120
    ctgWUnlockVgInfo(dbCache);
D
dapan1121 已提交
2121 2122 2123
  }

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

D
dapan1121 已提交
2125 2126 2127
  CTG_RET(code);
}

D
dapan1121 已提交
2128
int32_t ctgOpUpdateTbIndex(SCtgCacheOperation *operation) {
dengyihao's avatar
dengyihao 已提交
2129
  int32_t               code = 0;
D
dapan1121 已提交
2130
  SCtgUpdateTbIndexMsg *msg = operation->data;
dengyihao's avatar
dengyihao 已提交
2131 2132 2133 2134
  SCatalog             *pCtg = msg->pCtg;
  STableIndex          *pIndex = msg->pIndex;
  SCtgDBCache          *dbCache = NULL;

D
dapan1121 已提交
2135 2136 2137 2138
  if (pCtg->stopUpdate) {
    goto _return;
  }

D
dapan1121 已提交
2139
  CTG_ERR_JRET(ctgGetAddDBCache(pCtg, pIndex->dbFName, 0, &dbCache));
D
dapan1121 已提交
2140 2141 2142 2143 2144 2145 2146 2147 2148

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

_return:

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

D
dapan1121 已提交
2150
  taosMemoryFreeClear(msg);
dengyihao's avatar
dengyihao 已提交
2151

D
dapan1121 已提交
2152 2153 2154 2155
  CTG_RET(code);
}

int32_t ctgOpDropTbIndex(SCtgCacheOperation *operation) {
dengyihao's avatar
dengyihao 已提交
2156
  int32_t             code = 0;
D
dapan1121 已提交
2157
  SCtgDropTbIndexMsg *msg = operation->data;
dengyihao's avatar
dengyihao 已提交
2158 2159 2160
  SCatalog           *pCtg = msg->pCtg;
  SCtgDBCache        *dbCache = NULL;

D
dapan1121 已提交
2161 2162 2163 2164
  if (pCtg->stopUpdate) {
    goto _return;
  }

D
dapan1121 已提交
2165
  CTG_ERR_JRET(ctgGetDBCache(pCtg, msg->dbFName, &dbCache));
D
dapan1121 已提交
2166
  if (NULL == dbCache) {
D
dapan1121 已提交
2167 2168 2169
    return TSDB_CODE_SUCCESS;
  }

dengyihao's avatar
dengyihao 已提交
2170
  STableIndex *pIndex = taosMemoryCalloc(1, sizeof(STableIndex));
D
dapan1121 已提交
2171 2172
  if (NULL == pIndex) {
    CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
2173
  }
D
dapan1121 已提交
2174 2175 2176
  strcpy(pIndex->tbName, msg->tbName);
  strcpy(pIndex->dbFName, msg->dbFName);
  pIndex->version = -1;
D
dapan1121 已提交
2177

D
dapan1121 已提交
2178
  CTG_ERR_JRET(ctgWriteTbIndexToCache(pCtg, dbCache, pIndex->dbFName, pIndex->tbName, &pIndex));
D
dapan1121 已提交
2179 2180 2181 2182 2183 2184 2185

_return:

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

D
dapan1121 已提交
2187
  taosMemoryFreeClear(msg);
dengyihao's avatar
dengyihao 已提交
2188

D
dapan1121 已提交
2189 2190 2191
  CTG_RET(code);
}

D
dapan1121 已提交
2192
int32_t ctgOpClearCache(SCtgCacheOperation *operation) {
dengyihao's avatar
dengyihao 已提交
2193
  int32_t            code = 0;
D
dapan1121 已提交
2194
  SCtgClearCacheMsg *msg = operation->data;
dengyihao's avatar
dengyihao 已提交
2195
  SCatalog          *pCtg = msg->pCtg;
D
dapan1121 已提交
2196

D
dapan1121 已提交
2197 2198
  CTG_LOCK(CTG_WRITE, &gCtgMgmt.lock);

D
dapan1121 已提交
2199
  if (pCtg) {
D
dapan1121 已提交
2200 2201 2202 2203 2204
    if (msg->freeCtg) {
      ctgFreeHandle(pCtg);
    } else {
      ctgClearHandle(pCtg);
    }
dengyihao's avatar
dengyihao 已提交
2205

D
dapan1121 已提交
2206 2207
    goto _return;
  }
D
dapan1121 已提交
2208 2209 2210 2211 2212 2213

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

_return:
D
dapan1121 已提交
2216 2217

  CTG_UNLOCK(CTG_WRITE, &gCtgMgmt.lock);
dengyihao's avatar
dengyihao 已提交
2218

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

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

2224 2225 2226 2227 2228 2229 2230 2231
void ctgFreeCacheOperationData(SCtgCacheOperation *op) {
  if (NULL == op || NULL == op->data) {
    return;
  }

  switch (op->opId) {
    case CTG_OP_UPDATE_VGROUP: {
      SCtgUpdateVgMsg *msg = op->data;
2232
      freeVgInfo(msg->dbInfo);
2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276
      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);
      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 已提交
2277
void ctgCleanupCacheQueue(void) {
dengyihao's avatar
dengyihao 已提交
2278 2279
  SCtgQNode          *node = NULL;
  SCtgQNode          *nodeNext = NULL;
D
dapan1121 已提交
2280
  SCtgCacheOperation *op = NULL;
dengyihao's avatar
dengyihao 已提交
2281
  bool                stopQueue = false;
D
dapan1121 已提交
2282 2283 2284 2285 2286

  while (true) {
    node = gCtgMgmt.queue.head->next;
    while (node) {
      if (node->op) {
D
dapan1121 已提交
2287 2288 2289 2290 2291 2292
        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;
dengyihao's avatar
dengyihao 已提交
2293
          CTG_RT_STAT_INC(numOfOpDequeue, 1);
D
dapan1121 已提交
2294
        } else {
2295
          ctgFreeCacheOperationData(op);
dengyihao's avatar
dengyihao 已提交
2296
          CTG_RT_STAT_INC(numOfOpAbort, 1);
D
dapan1121 已提交
2297
        }
dengyihao's avatar
dengyihao 已提交
2298

D
dapan1121 已提交
2299 2300
        if (op->syncOp) {
          tsem_post(&op->rspSem);
D
dapan1121 已提交
2301
        } else {
D
dapan1121 已提交
2302
          taosMemoryFree(op);
D
dapan1121 已提交
2303
        }
D
dapan1121 已提交
2304
      }
D
dapan1121 已提交
2305 2306 2307

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

D
dapan1121 已提交
2309
      node = nodeNext;
D
dapan1121 已提交
2310 2311
    }

D
dapan1121 已提交
2312
    if (!stopQueue) {
D
dapan1121 已提交
2313 2314 2315 2316
      taosUsleep(1);
    } else {
      break;
    }
D
dapan1121 已提交
2317 2318 2319 2320 2321 2322
  }

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

dengyihao's avatar
dengyihao 已提交
2323
void *ctgUpdateThreadFunc(void *param) {
D
dapan1121 已提交
2324
  setThreadName("catalog");
2325

D
dapan1121 已提交
2326 2327 2328 2329 2330 2331
  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 已提交
2332

2333
    if (atomic_load_8((int8_t *)&gCtgMgmt.queue.stopQueue)) {
D
dapan1121 已提交
2334
      ctgCleanupCacheQueue();
D
dapan1121 已提交
2335 2336 2337
      break;
    }

D
dapan1121 已提交
2338 2339 2340
    SCtgCacheOperation *operation = NULL;
    ctgDequeue(&operation);
    SCatalog *pCtg = ((SCtgUpdateMsgHeader *)operation->data)->pCtg;
D
dapan1121 已提交
2341

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

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

D
dapan1121 已提交
2346
    if (operation->syncOp) {
D
dapan1121 已提交
2347
      tsem_post(&operation->rspSem);
D
dapan1121 已提交
2348 2349
    } else {
      taosMemoryFreeClear(operation);
D
dapan1121 已提交
2350 2351
    }

dengyihao's avatar
dengyihao 已提交
2352
    CTG_RT_STAT_INC(numOfOpDequeue, 1);
D
dapan1121 已提交
2353

D
dapan1121 已提交
2354
    ctgdShowCacheInfo();
D
dapan1121 已提交
2355 2356 2357
  }

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

D
dapan1121 已提交
2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370
  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 已提交
2371

D
dapan1121 已提交
2372 2373 2374 2375
  taosThreadAttrDestroy(&thAttr);
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
2376
int32_t ctgGetTbMetaFromCache(SCatalog *pCtg, SCtgTbMetaCtx *ctx, STableMeta **pTableMeta) {
D
dapan1121 已提交
2377
  if (IS_SYS_DBNAME(ctx->pName->dbname)) {
D
dapan1121 已提交
2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389
    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 已提交
2390
    *pTableMeta = NULL;
D
dapan1121 已提交
2391 2392 2393 2394 2395 2396 2397 2398 2399
  }

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

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
2400
#if 0
2401
int32_t ctgGetTbMetaBFromCache(SCatalog* pCtg, SRequestConnInfo *pConn, SCtgTbMetasCtx* ctx, SArray** pResList) {
D
dapan1121 已提交
2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438
  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};
2439
      fetch.tbIdx = i;
D
dapan1121 已提交
2440 2441 2442 2443 2444 2445
      fetch.fetchIdx = fIdx++;
      fetch.flag = nctx.flag;

      taosArrayPush(ctx->pFetchs, &fetch);
    }
    
2446
    taosArrayPush(ctx->pResList, &res);
D
dapan1121 已提交
2447 2448 2449
  }

  if (NULL == ctx->pFetchs) {
2450
    TSWAP(*pResList, ctx->pResList);
D
dapan1121 已提交
2451 2452 2453 2454
  }

  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
2455 2456
#endif

dengyihao's avatar
dengyihao 已提交
2457 2458 2459 2460 2461 2462 2463 2464 2465
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 已提交
2466 2467 2468 2469 2470 2471 2472 2473
  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 已提交
2474
  SCtgTbCache *pCache = NULL;
D
dapan1121 已提交
2475
  ctgAcquireDBCache(pCtg, dbFName, &dbCache);
dengyihao's avatar
dengyihao 已提交
2476

D
dapan1121 已提交
2477 2478 2479
  if (NULL == dbCache) {
    ctgDebug("db %s not in cache", dbFName);
    for (int32_t i = 0; i < tbNum; ++i) {
2480
      ctgAddFetch(&ctx->pFetchs, dbIdx, i, fetchIdx, baseResIdx + i, flag);
H
Haojun Liao 已提交
2481
      taosArrayPush(ctx->pResList, &(SMetaData){0});
D
dapan1121 已提交
2482 2483 2484 2485 2486 2487
    }

    return TSDB_CODE_SUCCESS;
  }

  for (int32_t i = 0; i < tbNum; ++i) {
H
Haojun Liao 已提交
2488
    pName = taosArrayGet(pList, i);
D
dapan1121 已提交
2489 2490 2491 2492

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

D
dapan1121 已提交
2496 2497 2498 2499 2500
      continue;
    }

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

D
dapan1121 已提交
2506 2507 2508
      continue;
    }

dengyihao's avatar
dengyihao 已提交
2509
    STableMeta *tbMeta = pCache->pMeta;
D
dapan1121 已提交
2510 2511 2512 2513 2514 2515 2516 2517

    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 已提交
2518 2519
    SMetaRes    res = {0};
    STableMeta *pTableMeta = NULL;
D
dapan1121 已提交
2520 2521 2522 2523
    if (tbMeta->tableType != TSDB_CHILD_TABLE) {
      int32_t metaSize = CTG_META_SIZE(tbMeta);
      pTableMeta = taosMemoryCalloc(1, metaSize);
      if (NULL == pTableMeta) {
2524
        ctgReleaseTbMetaToCache(pCtg, dbCache, pCache);
D
dapan1121 已提交
2525 2526
        CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
      }
dengyihao's avatar
dengyihao 已提交
2527

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

2530
      CTG_UNLOCK(CTG_READ, &pCache->metaLock);
dengyihao's avatar
dengyihao 已提交
2531 2532
      taosHashRelease(dbCache->tbCache, pCache);

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

D
dapan1121 已提交
2535
      res.pRes = pTableMeta;
2536
      taosArrayPush(ctx->pResList, &res);
D
dapan1121 已提交
2537 2538 2539

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

D
dapan1121 已提交
2541 2542 2543 2544
    // PROCESS FOR CHILD TABLE

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

2547
      CTG_UNLOCK(CTG_READ, &pCache->metaLock);
dengyihao's avatar
dengyihao 已提交
2548 2549
      taosHashRelease(dbCache->tbCache, pCache);

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

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

D
dapan1121 已提交
2555 2556
      continue;
    }
dengyihao's avatar
dengyihao 已提交
2557

D
dapan1121 已提交
2558 2559 2560
    int32_t metaSize = sizeof(SCTableMeta);
    pTableMeta = taosMemoryCalloc(1, metaSize);
    if (NULL == pTableMeta) {
dengyihao's avatar
dengyihao 已提交
2561
      ctgReleaseTbMetaToCache(pCtg, dbCache, pCache);
D
dapan1121 已提交
2562 2563
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
dengyihao's avatar
dengyihao 已提交
2564

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

2567
    CTG_UNLOCK(CTG_READ, &pCache->metaLock);
dengyihao's avatar
dengyihao 已提交
2568 2569 2570 2571 2572 2573
    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 已提交
2574 2575
    if (NULL == stName) {
      ctgDebug("stb 0x%" PRIx64 " not in cache, dbFName:%s", pTableMeta->suid, dbFName);
2576
      ctgAddFetch(&ctx->pFetchs, dbIdx, i, fetchIdx, baseResIdx + i, flag);
H
Haojun Liao 已提交
2577
      taosArrayPush(ctx->pResList, &(SMetaRes){0});
D
dapan1121 已提交
2578 2579 2580 2581 2582 2583 2584 2585 2586

      taosMemoryFreeClear(pTableMeta);
      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 已提交
2587

2588
      ctgAddFetch(&ctx->pFetchs, dbIdx, i, fetchIdx, baseResIdx + i, flag);
H
Haojun Liao 已提交
2589
      taosArrayPush(ctx->pResList, &(SMetaRes){0});
D
dapan1121 已提交
2590

dengyihao's avatar
dengyihao 已提交
2591
      taosMemoryFreeClear(pTableMeta);
D
dapan1121 已提交
2592 2593 2594 2595 2596 2597 2598 2599
      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);
2600
      CTG_UNLOCK(CTG_READ, &pCache->metaLock);
dengyihao's avatar
dengyihao 已提交
2601 2602
      taosHashRelease(dbCache->tbCache, pCache);

2603
      ctgAddFetch(&ctx->pFetchs, dbIdx, i, fetchIdx, baseResIdx + i, flag);
H
Haojun Liao 已提交
2604
      taosArrayPush(ctx->pResList, &(SMetaRes){0});
D
dapan1121 已提交
2605 2606 2607 2608 2609

      taosMemoryFreeClear(pTableMeta);

      continue;
    }
dengyihao's avatar
dengyihao 已提交
2610 2611 2612

    STableMeta *stbMeta = pCache->pMeta;
    if (stbMeta->suid != nctx.tbInfo.suid) {
2613
      CTG_UNLOCK(CTG_READ, &pCache->metaLock);
dengyihao's avatar
dengyihao 已提交
2614 2615 2616 2617 2618
      taosHashRelease(dbCache->tbCache, pCache);

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

2619
      ctgAddFetch(&ctx->pFetchs, dbIdx, i, fetchIdx, baseResIdx + i, flag);
H
Haojun Liao 已提交
2620
      taosArrayPush(ctx->pResList, &(SMetaRes){0});
D
dapan1121 已提交
2621 2622 2623 2624 2625

      taosMemoryFreeClear(pTableMeta);

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

D
dapan1121 已提交
2627 2628
    metaSize = CTG_META_SIZE(stbMeta);
    pTableMeta = taosMemoryRealloc(pTableMeta, metaSize);
dengyihao's avatar
dengyihao 已提交
2629
    if (NULL == pTableMeta) {
2630
      ctgReleaseTbMetaToCache(pCtg, dbCache, pCache);
D
dapan1121 已提交
2631 2632
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
dengyihao's avatar
dengyihao 已提交
2633

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

2636
    CTG_UNLOCK(CTG_READ, &pCache->metaLock);
dengyihao's avatar
dengyihao 已提交
2637 2638
    taosHashRelease(dbCache->tbCache, pCache);

D
dapan1121 已提交
2639
    res.pRes = pTableMeta;
2640
    taosArrayPush(ctx->pResList, &res);
D
dapan1121 已提交
2641 2642 2643 2644 2645

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

2646
  ctgReleaseDBCache(pCtg, dbCache);
dengyihao's avatar
dengyihao 已提交
2647

D
dapan1121 已提交
2648 2649 2650
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
2651
int32_t ctgRemoveTbMetaFromCache(SCatalog *pCtg, SName *pTableName, bool syncReq) {
D
dapan1121 已提交
2652
  int32_t       code = 0;
dengyihao's avatar
dengyihao 已提交
2653
  STableMeta   *tblMeta = NULL;
D
dapan1121 已提交
2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681
  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 已提交
2682
  if (IS_SYS_DBNAME(pTableName->dbname)) {
D
dapan1121 已提交
2683 2684 2685 2686
    ctgError("no valid vgInfo for db, dbname:%s", pTableName->dbname);
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
  }

dengyihao's avatar
dengyihao 已提交
2687
  SCtgDBCache *dbCache = NULL;
D
dapan1121 已提交
2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713
  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);
}