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

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

D
dapan 已提交
23
SCatalogMgmt gCtgMgmt = {0};
D
dapan1121 已提交
24

H
Hongze Cheng 已提交
25
int32_t ctgGetDBVgInfo(SCatalog* pCtg, SRequestConnInfo* pConn, const char* dbFName, SCtgDBCache** dbCache,
26
                       SDBVgInfo** pInfo, bool* exists) {
D
dapan1121 已提交
27
  int32_t code = 0;
D
dapan1121 已提交
28

D
dapan1121 已提交
29
  CTG_ERR_RET(ctgAcquireVgInfoFromCache(pCtg, dbFName, dbCache));
D
dapan1121 已提交
30

D
dapan1121 已提交
31
  if (*dbCache) {
32 33 34 35 36 37 38 39 40
    if (exists) {
      *exists = true;
    }
    
    return TSDB_CODE_SUCCESS;
  }

  if (exists) {
    *exists = false;
D
dapan1121 已提交
41
    return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
42 43
  }

X
Xiaoyu Wang 已提交
44
  SUseDbOutput     DbOut = {0};
D
dapan1121 已提交
45 46
  SBuildUseDBInput input = {0};

D
dapan1121 已提交
47
  tstrncpy(input.db, dbFName, tListLen(input.db));
D
dapan1121 已提交
48
  input.vgVersion = CTG_DEFAULT_INVALID_VERSION;
H
Haojun Liao 已提交
49

D
dapan1121 已提交
50
  CTG_ERR_RET(ctgGetDBVgInfoFromMnode(pCtg, pConn, &input, &DbOut, NULL));
D
dapan1121 已提交
51

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

D
dapan1121 已提交
54
  CTG_ERR_RET(ctgUpdateVgroupEnqueue(pCtg, dbFName, DbOut.dbId, DbOut.dbVgroup, false));
D
dapan 已提交
55

D
dapan1121 已提交
56
  return TSDB_CODE_SUCCESS;
D
dapan 已提交
57 58 59

_return:

wafwerar's avatar
wafwerar 已提交
60
  taosMemoryFreeClear(*pInfo);
D
dapan 已提交
61
  *pInfo = DbOut.dbVgroup;
X
Xiaoyu Wang 已提交
62

D
dapan 已提交
63
  CTG_RET(code);
D
dapan1121 已提交
64 65
}

H
Hongze Cheng 已提交
66 67
int32_t ctgRefreshDBVgInfo(SCatalog* pCtg, SRequestConnInfo* pConn, const char* dbFName) {
  int32_t      code = 0;
D
dapan1121 已提交
68
  SCtgDBCache* dbCache = NULL;
D
dapan1121 已提交
69

D
dapan1121 已提交
70
  CTG_ERR_RET(ctgAcquireVgInfoFromCache(pCtg, dbFName, &dbCache));
D
dapan1121 已提交
71

X
Xiaoyu Wang 已提交
72
  SUseDbOutput     DbOut = {0};
D
dapan1121 已提交
73 74 75
  SBuildUseDBInput input = {0};
  tstrncpy(input.db, dbFName, tListLen(input.db));

D
dapan1121 已提交
76
  if (NULL != dbCache) {
D
dapan1121 已提交
77
    input.dbId = dbCache->dbId;
D
dapan1121 已提交
78

D
dapan1121 已提交
79
    ctgReleaseVgInfoToCache(pCtg, dbCache);
D
dapan1121 已提交
80
  }
X
Xiaoyu Wang 已提交
81

D
dapan1121 已提交
82 83
  input.vgVersion = CTG_DEFAULT_INVALID_VERSION;
  input.numOfTable = 0;
D
dapan1121 已提交
84

D
dapan1121 已提交
85
  code = ctgGetDBVgInfoFromMnode(pCtg, pConn, &input, &DbOut, NULL);
D
dapan1121 已提交
86
  if (code) {
D
dapan1121 已提交
87
    if (CTG_DB_NOT_EXIST(code) && (NULL != dbCache)) {
D
dapan1121 已提交
88
      ctgDebug("db no longer exist, dbFName:%s, dbId:0x%" PRIx64, input.db, input.dbId);
D
dapan1121 已提交
89
      ctgDropDbCacheEnqueue(pCtg, input.db, input.dbId);
D
dapan1121 已提交
90 91 92 93 94
    }

    CTG_ERR_RET(code);
  }

D
dapan1121 已提交
95
  CTG_ERR_RET(ctgUpdateVgroupEnqueue(pCtg, dbFName, DbOut.dbId, DbOut.dbVgroup, true));
D
dapan 已提交
96

D
dapan1121 已提交
97 98 99
  return TSDB_CODE_SUCCESS;
}

H
Hongze Cheng 已提交
100 101
int32_t ctgRefreshTbMeta(SCatalog* pCtg, SRequestConnInfo* pConn, SCtgTbMetaCtx* ctx, STableMetaOutput** pOutput,
                         bool syncReq) {
D
dapan1121 已提交
102
  SVgroupInfo vgroupInfo = {0};
X
Xiaoyu Wang 已提交
103
  int32_t     code = 0;
D
dapan1121 已提交
104

D
dapan1121 已提交
105
  if (!CTG_FLAG_IS_SYS_DB(ctx->flag)) {
106
    CTG_ERR_RET(ctgGetTbHashVgroup(pCtg, pConn, ctx->pName, &vgroupInfo, NULL));
D
dapan1121 已提交
107
  }
D
dapan1121 已提交
108

D
dapan1121 已提交
109
  STableMetaOutput  moutput = {0};
X
Xiaoyu Wang 已提交
110
  STableMetaOutput* output = taosMemoryCalloc(1, sizeof(STableMetaOutput));
D
dapan1121 已提交
111 112
  if (NULL == output) {
    ctgError("malloc %d failed", (int32_t)sizeof(STableMetaOutput));
D
dapan1121 已提交
113
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
114
  }
D
dapan1121 已提交
115

D
dapan1121 已提交
116 117
  if (CTG_FLAG_IS_SYS_DB(ctx->flag)) {
    ctgDebug("will refresh tbmeta, supposed in information_schema, tbName:%s", tNameGetTableName(ctx->pName));
D
dapan1121 已提交
118

H
Hongze Cheng 已提交
119 120
    CTG_ERR_JRET(
        ctgGetTbMetaFromMnodeImpl(pCtg, pConn, (char*)ctx->pName->dbname, (char*)ctx->pName->tname, output, NULL));
D
dapan1121 已提交
121 122
  } else if (CTG_FLAG_IS_STB(ctx->flag)) {
    ctgDebug("will refresh tbmeta, supposed to be stb, tbName:%s", tNameGetTableName(ctx->pName));
D
dapan1121 已提交
123 124

    // if get from mnode failed, will not try vnode
D
dapan1121 已提交
125
    CTG_ERR_JRET(ctgGetTbMetaFromMnode(pCtg, pConn, ctx->pName, output, NULL));
D
dapan1121 已提交
126

D
dapan1121 已提交
127
    if (CTG_IS_META_NULL(output->metaType)) {
D
dapan1121 已提交
128
      CTG_ERR_JRET(ctgGetTbMetaFromVnode(pCtg, pConn, ctx->pName, &vgroupInfo, output, NULL));
D
dapan1121 已提交
129 130
    }
  } else {
X
Xiaoyu Wang 已提交
131 132
    ctgDebug("will refresh tbmeta, not supposed to be stb, tbName:%s, flag:%d", tNameGetTableName(ctx->pName),
             ctx->flag);
D
dapan1121 已提交
133 134

    // if get from vnode failed or no table meta, will not try mnode
D
dapan1121 已提交
135
    CTG_ERR_JRET(ctgGetTbMetaFromVnode(pCtg, pConn, ctx->pName, &vgroupInfo, output, NULL));
D
dapan1121 已提交
136

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

wafwerar's avatar
wafwerar 已提交
140
      taosMemoryFreeClear(output->tbMeta);
H
Hongze Cheng 已提交
141

D
dapan1121 已提交
142
      CTG_ERR_JRET(ctgGetTbMetaFromMnodeImpl(pCtg, pConn, output->dbFName, output->tbName, output, NULL));
D
dapan1121 已提交
143
    } else if (CTG_IS_META_BOTH(output->metaType)) {
D
dapan1121 已提交
144
      int32_t exist = 0;
D
dapan1121 已提交
145 146
      if (!CTG_FLAG_IS_FORCE_UPDATE(ctx->flag)) {
        CTG_ERR_JRET(ctgTbMetaExistInCache(pCtg, output->dbFName, output->tbName, &exist));
D
dapan1121 已提交
147
      }
H
Haojun Liao 已提交
148

D
dapan1121 已提交
149
      if (0 == exist) {
D
dapan1121 已提交
150
        CTG_ERR_JRET(ctgGetTbMetaFromMnodeImpl(pCtg, pConn, output->dbFName, output->tbName, &moutput, NULL));
D
dapan1121 已提交
151

D
dapan1121 已提交
152
        if (CTG_IS_META_NULL(moutput.metaType)) {
D
dapan1121 已提交
153
          SET_META_TYPE_NULL(output->metaType);
D
dapan1121 已提交
154
        }
X
Xiaoyu Wang 已提交
155

wafwerar's avatar
wafwerar 已提交
156
        taosMemoryFreeClear(output->tbMeta);
D
dapan1121 已提交
157
        output->tbMeta = moutput.tbMeta;
D
dapan1121 已提交
158 159
        moutput.tbMeta = NULL;
      } else {
wafwerar's avatar
wafwerar 已提交
160
        taosMemoryFreeClear(output->tbMeta);
X
Xiaoyu Wang 已提交
161 162

        SET_META_TYPE_CTABLE(output->metaType);
D
dapan1121 已提交
163
      }
D
dapan1121 已提交
164 165 166
    }
  }

D
dapan1121 已提交
167
  if (CTG_IS_META_NULL(output->metaType)) {
D
dapan1121 已提交
168 169
    ctgError("no tbmeta got, tbNmae:%s", tNameGetTableName(ctx->pName));
    ctgRemoveTbMetaFromCache(pCtg, ctx->pName, false);
D
dapan1121 已提交
170 171 172
    CTG_ERR_JRET(CTG_ERR_CODE_TABLE_NOT_EXIST);
  }

D
dapan 已提交
173
  if (CTG_IS_META_TABLE(output->metaType)) {
X
Xiaoyu Wang 已提交
174 175
    ctgDebug("tbmeta got, dbFName:%s, tbName:%s, tbType:%d", output->dbFName, output->tbName,
             output->tbMeta->tableType);
D
dapan 已提交
176
  } else {
X
Xiaoyu Wang 已提交
177 178
    ctgDebug("tbmeta got, dbFName:%s, tbName:%s, tbType:%d, stbMetaGot:%d", output->dbFName, output->ctbName,
             output->ctbMeta.tableType, CTG_IS_META_BOTH(output->metaType));
D
dapan 已提交
179 180
  }

D
dapan1121 已提交
181 182
  if (pOutput) {
    CTG_ERR_JRET(ctgCloneMetaOutput(output, pOutput));
D
dapan1121 已提交
183
  }
D
dapan1121 已提交
184

D
dapan1121 已提交
185 186 187
  code = ctgUpdateTbMetaEnqueue(pCtg, output, syncReq);
  output = NULL;
  CTG_ERR_JRET(code);
D
dapan1121 已提交
188 189 190

  return TSDB_CODE_SUCCESS;

D
dapan1121 已提交
191 192
_return:

D
dapan1121 已提交
193 194 195 196 197
  if (output) {
    taosMemoryFreeClear(output->tbMeta);
    taosMemoryFreeClear(output);
  }
  
D
dapan1121 已提交
198 199 200
  CTG_RET(code);
}

H
Hongze Cheng 已提交
201 202 203
int32_t ctgGetTbMeta(SCatalog* pCtg, SRequestConnInfo* pConn, SCtgTbMetaCtx* ctx, STableMeta** pTableMeta) {
  int32_t           code = 0;
  STableMetaOutput* output = NULL;
D
dapan1121 已提交
204

D
dapan1121 已提交
205
  CTG_ERR_RET(ctgGetTbMetaFromCache(pCtg, ctx, pTableMeta));
206
  if (*pTableMeta || (ctx->flag & CTG_FLAG_ONLY_CACHE)) {
D
dapan1121 已提交
207 208
    goto _return;
  }
H
Haojun Liao 已提交
209

D
dapan1121 已提交
210
  while (true) {
211
    CTG_ERR_JRET(ctgRefreshTbMeta(pCtg, pConn, ctx, &output, ctx->flag & CTG_FLAG_SYNC_OP));
D
dapan1121 已提交
212

D
dapan1121 已提交
213 214 215
    if (CTG_IS_META_TABLE(output->metaType)) {
      *pTableMeta = output->tbMeta;
      goto _return;
D
dapan 已提交
216
    }
D
dapan1121 已提交
217 218 219

    if (CTG_IS_META_BOTH(output->metaType)) {
      memcpy(output->tbMeta, &output->ctbMeta, sizeof(output->ctbMeta));
X
Xiaoyu Wang 已提交
220

D
dapan1121 已提交
221 222 223 224 225 226 227 228
      *pTableMeta = output->tbMeta;
      goto _return;
    }

    if ((!CTG_IS_META_CTABLE(output->metaType)) || output->tbMeta) {
      ctgError("invalid metaType:%d", output->metaType);
      taosMemoryFreeClear(output->tbMeta);
      CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
D
dapan 已提交
229 230
    }

D
dapan1121 已提交
231
    // HANDLE ONLY CHILD TABLE META
D
dapan1121 已提交
232

D
dapan1121 已提交
233
    taosMemoryFreeClear(output->tbMeta);
D
dapan 已提交
234

D
dapan1121 已提交
235 236 237 238 239
    SName stbName = *ctx->pName;
    strcpy(stbName.tname, output->tbName);
    SCtgTbMetaCtx stbCtx = {0};
    stbCtx.flag = ctx->flag;
    stbCtx.pName = &stbName;
X
Xiaoyu Wang 已提交
240

D
dapan1121 已提交
241 242 243 244 245
    CTG_ERR_JRET(ctgReadTbMetaFromCache(pCtg, &stbCtx, pTableMeta));
    if (NULL == *pTableMeta) {
      ctgDebug("stb no longer exist, dbFName:%s, tbName:%s", output->dbFName, ctx->pName->tname);
      continue;
    }
D
dapan 已提交
246

D
dapan1121 已提交
247
    memcpy(*pTableMeta, &output->ctbMeta, sizeof(output->ctbMeta));
D
dapan 已提交
248

D
dapan1121 已提交
249 250
    break;
  }
D
dapan 已提交
251

D
dapan1121 已提交
252
_return:
D
dapan 已提交
253

D
dapan1121 已提交
254 255 256 257 258 259 260
  if (CTG_TABLE_NOT_EXIST(code) && ctx->tbInfo.inCache) {
    char dbFName[TSDB_DB_FNAME_LEN] = {0};
    if (CTG_FLAG_IS_SYS_DB(ctx->flag)) {
      strcpy(dbFName, ctx->pName->dbname);
    } else {
      tNameGetFullDbName(ctx->pName, dbFName);
    }
D
dapan 已提交
261

D
dapan1121 已提交
262
    if (TSDB_SUPER_TABLE == ctx->tbInfo.tbType) {
D
dapan1121 已提交
263
      ctgDropStbMetaEnqueue(pCtg, dbFName, ctx->tbInfo.dbId, ctx->pName->tname, ctx->tbInfo.suid, false);
D
dapan1121 已提交
264
    } else {
D
dapan1121 已提交
265
      ctgDropTbMetaEnqueue(pCtg, dbFName, ctx->tbInfo.dbId, ctx->pName->tname, false);
D
dapan1121 已提交
266 267
    }
  }
D
dapan 已提交
268

D
dapan1121 已提交
269
  taosMemoryFreeClear(output);
D
dapan 已提交
270

D
dapan1121 已提交
271 272 273 274
  if (*pTableMeta) {
    ctgDebug("tbmeta returned, tbName:%s, tbType:%d", ctx->pName->tname, (*pTableMeta)->tableType);
    ctgdShowTableMeta(pCtg, ctx->pName->tname, *pTableMeta);
  }
D
dapan 已提交
275 276 277 278

  CTG_RET(code);
}

X
Xiaoyu Wang 已提交
279 280
int32_t ctgUpdateTbMeta(SCatalog* pCtg, STableMetaRsp* rspMsg, bool syncOp) {
  STableMetaOutput* output = taosMemoryCalloc(1, sizeof(STableMetaOutput));
D
dapan1121 已提交
281 282
  if (NULL == output) {
    ctgError("malloc %d failed", (int32_t)sizeof(STableMetaOutput));
D
dapan1121 已提交
283
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
284
  }
X
Xiaoyu Wang 已提交
285

D
dapan1121 已提交
286 287 288 289 290
  int32_t code = 0;

  strcpy(output->dbFName, rspMsg->dbFName);

  output->dbId = rspMsg->dbId;
X
Xiaoyu Wang 已提交
291

292 293
  if (TSDB_CHILD_TABLE == rspMsg->tableType && NULL == rspMsg->pSchemas) {
    strcpy(output->ctbName, rspMsg->tbName);
X
Xiaoyu Wang 已提交
294

295 296 297 298 299 300 301 302 303 304
    SET_META_TYPE_CTABLE(output->metaType);

    CTG_ERR_JRET(queryCreateCTableMetaFromMsg(rspMsg, &output->ctbMeta));
  } else {
    strcpy(output->tbName, rspMsg->tbName);

    SET_META_TYPE_TABLE(output->metaType);

    CTG_ERR_JRET(queryCreateTableMetaFromMsg(rspMsg, rspMsg->tableType == TSDB_SUPER_TABLE, &output->tbMeta));
  }
D
dapan1121 已提交
305

D
dapan1121 已提交
306 307 308
  code = ctgUpdateTbMetaEnqueue(pCtg, output, syncOp);
  output = NULL;
  CTG_ERR_JRET(code);
D
dapan1121 已提交
309 310

  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
311

D
dapan1121 已提交
312 313
_return:

D
dapan1121 已提交
314 315 316 317 318
  if (output) {
    taosMemoryFreeClear(output->tbMeta);
    taosMemoryFreeClear(output);
  }
  
D
dapan1121 已提交
319 320 321
  CTG_RET(code);
}

D
dapan1121 已提交
322
int32_t ctgChkAuth(SCatalog* pCtg, SRequestConnInfo* pConn, SUserAuthInfo *pReq, SUserAuthRes* pRes, bool* exists) {
H
Hongze Cheng 已提交
323
  bool    inCache = false;
D
dapan1121 已提交
324
  int32_t code = 0;
D
dapan1121 已提交
325 326
  SCtgAuthRsp rsp = {0};
  rsp.pRawRes = pRes;
X
Xiaoyu Wang 已提交
327

D
dapan1121 已提交
328
  CTG_ERR_RET(ctgChkAuthFromCache(pCtg, pReq, &inCache, &rsp));
D
dapan1121 已提交
329

D
dapan1121 已提交
330
  if (inCache) {
D
dapan1121 已提交
331 332 333 334 335 336 337
    if (exists) {
      *exists = true;
    }
    
    return TSDB_CODE_SUCCESS;
  } else if (exists) {
    *exists = false;
D
dapan1121 已提交
338 339
    return TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
340

D
dapan1121 已提交
341 342 343 344 345
  SCtgAuthReq req = {0};
  req.pRawReq = pReq;
  req.pConn = pConn;
  req.onlyCache = exists ? true : false;
  CTG_ERR_RET(ctgGetUserDbAuthFromMnode(pCtg, pConn, pReq->user, &req.authInfo, NULL));
D
dapan1121 已提交
346

D
dapan1121 已提交
347 348 349
  CTG_ERR_JRET(ctgChkSetAuthRes(pCtg, &req, &rsp));
  if (rsp.metaNotExists && exists) {
    *exists = false;
D
dapan1121 已提交
350
  }
D
dapan1121 已提交
351

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

D
dapan1121 已提交
354
  ctgUpdateUserEnqueue(pCtg, &req.authInfo, false);
D
dapan1121 已提交
355

D
dapan1121 已提交
356
  CTG_RET(code);
D
dapan1121 已提交
357 358
}

H
Hongze Cheng 已提交
359
int32_t ctgGetTbType(SCatalog* pCtg, SRequestConnInfo* pConn, SName* pTableName, int32_t* tbType) {
D
dapan1121 已提交
360 361 362 363 364 365 366
  char dbFName[TSDB_DB_FNAME_LEN];
  tNameGetFullDbName(pTableName, dbFName);
  CTG_ERR_RET(ctgReadTbTypeFromCache(pCtg, dbFName, pTableName->tname, tbType));
  if (*tbType > 0) {
    return TSDB_CODE_SUCCESS;
  }

H
Hongze Cheng 已提交
367
  STableMeta*   pMeta = NULL;
D
dapan1121 已提交
368 369 370 371
  SCtgTbMetaCtx ctx = {0};
  ctx.pName = (SName*)pTableName;
  ctx.flag = CTG_FLAG_UNKNOWN_STB;
  CTG_ERR_RET(ctgGetTbMeta(pCtg, pConn, &ctx, &pMeta));
D
dapan1121 已提交
372 373 374 375 376 377 378

  *tbType = pMeta->tableType;
  taosMemoryFree(pMeta);

  return TSDB_CODE_SUCCESS;
}

H
Hongze Cheng 已提交
379
int32_t ctgGetTbIndex(SCatalog* pCtg, SRequestConnInfo* pConn, SName* pTableName, SArray** pRes) {
D
dapan1121 已提交
380 381 382 383 384
  CTG_ERR_RET(ctgReadTbIndexFromCache(pCtg, pTableName, pRes));
  if (*pRes) {
    return TSDB_CODE_SUCCESS;
  }

H
Hongze Cheng 已提交
385
  STableIndex* pIndex = taosMemoryCalloc(1, sizeof(STableIndex));
D
dapan1121 已提交
386 387 388
  if (NULL == pIndex) {
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }
H
Hongze Cheng 已提交
389

D
dapan1121 已提交
390 391 392 393 394 395
  int32_t code = ctgGetTbIndexFromMnode(pCtg, pConn, (SName*)pTableName, pIndex, NULL);
  if (TSDB_CODE_MND_DB_INDEX_NOT_EXIST == code) {
    code = 0;
    goto _return;
  }
  CTG_ERR_JRET(code);
H
Hongze Cheng 已提交
396

D
dapan1121 已提交
397 398 399 400 401 402
  SArray* pInfo = NULL;
  CTG_ERR_JRET(ctgCloneTableIndex(pIndex->pIndex, &pInfo));

  *pRes = pInfo;

  CTG_ERR_JRET(ctgUpdateTbIndexEnqueue(pCtg, &pIndex, false));
D
dapan1121 已提交
403 404

  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
405 406 407 408 409 410 411 412 413 414

_return:

  tFreeSTableIndexRsp(pIndex);
  taosMemoryFree(pIndex);

  taosArrayDestroyEx(*pRes, tFreeSTableIndexInfo);
  *pRes = NULL;

  CTG_RET(code);
D
dapan1121 已提交
415 416
}

H
Hongze Cheng 已提交
417
int32_t ctgGetTbCfg(SCatalog* pCtg, SRequestConnInfo* pConn, SName* pTableName, STableCfg** pCfg) {
D
dapan1121 已提交
418 419 420 421 422 423 424
  int32_t tbType = 0;
  CTG_ERR_RET(ctgGetTbType(pCtg, pConn, pTableName, &tbType));

  if (TSDB_SUPER_TABLE == tbType) {
    CTG_ERR_RET(ctgGetTableCfgFromMnode(pCtg, pConn, pTableName, pCfg, NULL));
  } else {
    SVgroupInfo vgroupInfo = {0};
425
    CTG_ERR_RET(ctgGetTbHashVgroup(pCtg, pConn, pTableName, &vgroupInfo, NULL));
D
dapan1121 已提交
426 427 428 429 430
    CTG_ERR_RET(ctgGetTableCfgFromVnode(pCtg, pConn, pTableName, &vgroupInfo, pCfg, NULL));
  }

  CTG_RET(TSDB_CODE_SUCCESS);
}
D
dapan1121 已提交
431

D
dapan1121 已提交
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
int32_t ctgGetTbTag(SCatalog* pCtg, SRequestConnInfo* pConn, SName* pTableName, SArray** pRes) {
  SVgroupInfo vgroupInfo = {0};
  STableCfg* pCfg = NULL;
  int32_t code = 0;

  CTG_ERR_RET(ctgGetTbHashVgroup(pCtg, pConn, pTableName, &vgroupInfo, NULL));
  CTG_ERR_RET(ctgGetTableCfgFromVnode(pCtg, pConn, pTableName, &vgroupInfo, &pCfg, NULL));

  if (NULL == pCfg->pTags || pCfg->tagsLen <= 0) {
    ctgError("invalid tag in tbCfg rsp, pTags:%p, len:%d", pCfg->pTags, pCfg->tagsLen);
    CTG_ERR_JRET(TSDB_CODE_INVALID_MSG);
  }

  SArray* pTagVals = NULL;
  STag*   pTag = (STag*)pCfg->pTags;

  if (tTagIsJson(pTag)) {
    pTagVals = taosArrayInit(1, sizeof(STagVal));
    if (NULL == pTagVals) {
      CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
    }

    char* pJson = parseTagDatatoJson(pTag);
    STagVal tagVal;
    tagVal.cid = 0;
    tagVal.type = TSDB_DATA_TYPE_JSON;
    tagVal.pData = pJson;
    tagVal.nData = strlen(pJson);
    taosArrayPush(pTagVals, &tagVal);
  } else {
    CTG_ERR_JRET(tTagToValArray((const STag*)pCfg->pTags, &pTagVals));
  }

  *pRes = pTagVals;

_return:

  tFreeSTableCfgRsp((STableCfgRsp*)pCfg);
  
  CTG_RET(code);
}

H
Hongze Cheng 已提交
474 475 476 477 478 479 480
int32_t ctgGetTbDistVgInfo(SCatalog* pCtg, SRequestConnInfo* pConn, SName* pTableName, SArray** pVgList) {
  STableMeta*   tbMeta = NULL;
  int32_t       code = 0;
  SVgroupInfo   vgroupInfo = {0};
  SCtgDBCache*  dbCache = NULL;
  SArray*       vgList = NULL;
  SDBVgInfo*    vgInfo = NULL;
D
dapan1121 已提交
481 482 483
  SCtgTbMetaCtx ctx = {0};
  ctx.pName = pTableName;
  ctx.flag = CTG_FLAG_UNKNOWN_STB;
D
dapan1121 已提交
484 485

  *pVgList = NULL;
H
Hongze Cheng 已提交
486

D
dapan1121 已提交
487
  CTG_ERR_JRET(ctgGetTbMeta(pCtg, pConn, &ctx, &tbMeta));
D
dapan1121 已提交
488 489 490 491

  char db[TSDB_DB_FNAME_LEN] = {0};
  tNameGetFullDbName(pTableName, db);

H
Hongze Cheng 已提交
492
  SHashObj* vgHash = NULL;
493
  CTG_ERR_JRET(ctgGetDBVgInfo(pCtg, pConn, db, &dbCache, &vgInfo, NULL));
D
dapan1121 已提交
494 495

  if (dbCache) {
D
dapan1121 已提交
496
    vgHash = dbCache->vgCache.vgInfo->vgHash;
D
dapan1121 已提交
497 498 499 500 501 502 503 504 505 506
  } else {
    vgHash = vgInfo->vgHash;
  }

  if (tbMeta->tableType == TSDB_SUPER_TABLE) {
    CTG_ERR_JRET(ctgGenerateVgList(pCtg, vgHash, pVgList));
  } else {
    // USE HASH METHOD INSTEAD OF VGID IN TBMETA
    ctgError("invalid method to get none stb vgInfo, tbType:%d", tbMeta->tableType);
    CTG_ERR_JRET(TSDB_CODE_CTG_INVALID_INPUT);
X
Xiaoyu Wang 已提交
507

D
dapan1121 已提交
508 509 510 511 512 513 514 515 516 517
#if 0  
    int32_t vgId = tbMeta->vgId;
    if (taosHashGetDup(vgHash, &vgId, sizeof(vgId), &vgroupInfo) != 0) {
      ctgWarn("table's vgId not found in vgroup list, vgId:%d, tbName:%s", vgId, tNameGetTableName(pTableName));
      CTG_ERR_JRET(TSDB_CODE_CTG_VG_META_MISMATCH);
    }

    vgList = taosArrayInit(1, sizeof(SVgroupInfo));
    if (NULL == vgList) {
      ctgError("taosArrayInit %d failed", (int32_t)sizeof(SVgroupInfo));
D
dapan1121 已提交
518
      CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);    
D
dapan1121 已提交
519 520 521 522 523 524 525 526 527
    }

    if (NULL == taosArrayPush(vgList, &vgroupInfo)) {
      ctgError("taosArrayPush vgroupInfo to array failed, vgId:%d, tbName:%s", vgId, tNameGetTableName(pTableName));
      CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
    }

    *pVgList = vgList;
    vgList = NULL;
X
Xiaoyu Wang 已提交
528
#endif
D
dapan1121 已提交
529 530 531 532 533
  }

_return:

  if (dbCache) {
D
dapan1121 已提交
534
    ctgRUnlockVgInfo(dbCache);
D
dapan1121 已提交
535 536 537
    ctgReleaseDBCache(pCtg, dbCache);
  }

wafwerar's avatar
wafwerar 已提交
538
  taosMemoryFreeClear(tbMeta);
D
dapan1121 已提交
539 540

  if (vgInfo) {
541
    freeVgInfo(vgInfo);
D
dapan1121 已提交
542 543 544 545 546 547 548 549 550 551
  }

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

  CTG_RET(code);
}

552
int32_t ctgGetTbHashVgroup(SCatalog* pCtg, SRequestConnInfo* pConn, const SName* pTableName, SVgroupInfo* pVgroup, bool* exists) {
D
dapan1121 已提交
553 554 555 556 557 558 559 560 561 562
  if (IS_SYS_DBNAME(pTableName->dbname)) {
    ctgError("no valid vgInfo for db, dbname:%s", pTableName->dbname);
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
  }

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

H
Hongze Cheng 已提交
563
  SDBVgInfo* vgInfo = NULL;
564
  CTG_ERR_JRET(ctgGetDBVgInfo(pCtg, pConn, db, &dbCache, &vgInfo, exists));
D
dapan1121 已提交
565

566 567 568 569 570
  if (exists && false == *exists) {
    ctgDebug("db %s vgInfo not in cache", pTableName->dbname);
    return TSDB_CODE_SUCCESS;
  }
  
D
dapan1121 已提交
571
  CTG_ERR_JRET(ctgGetVgInfoFromHashValue(pCtg, pConn ? &pConn->mgmtEps : NULL, vgInfo ? vgInfo : dbCache->vgCache.vgInfo, pTableName, pVgroup));
D
dapan1121 已提交
572 573 574 575 576 577 578 579 580

_return:

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

  if (vgInfo) {
581
    freeVgInfo(vgInfo);
D
dapan1121 已提交
582 583 584 585 586
  }

  CTG_RET(code);
}

D
dapan1121 已提交
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
int32_t ctgGetTbsHashVgId(SCatalog* pCtg, SRequestConnInfo* pConn, int32_t acctId, const char* pDb, const char* pTbs[], int32_t tbNum, int32_t* vgId) {
  if (IS_SYS_DBNAME(pDb)) {
    ctgError("no valid vgInfo for db, dbname:%s", pDb);
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
  }

  SCtgDBCache* dbCache = NULL;
  int32_t      code = 0;
  char         dbFName[TSDB_DB_FNAME_LEN] = {0};
  snprintf(dbFName, TSDB_DB_FNAME_LEN, "%d.%s", acctId, pDb);

  SDBVgInfo* vgInfo = NULL;
  CTG_ERR_JRET(ctgGetDBVgInfo(pCtg, pConn, dbFName, &dbCache, &vgInfo, NULL));
  
  CTG_ERR_JRET(ctgGetVgIdsFromHashValue(pCtg, vgInfo ? vgInfo : dbCache->vgCache.vgInfo, dbFName, pTbs, tbNum, vgId));

_return:

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

  if (vgInfo) {
    freeVgInfo(vgInfo);
  }

  CTG_RET(code);
}


618 619 620 621 622 623 624 625 626 627 628 629 630 631
int32_t ctgGetCachedTbVgMeta(SCatalog* pCtg, const SName* pTableName, SVgroupInfo* pVgroup, STableMeta** pTableMeta) {
  int32_t      code = 0;
  char         db[TSDB_DB_FNAME_LEN] = {0};
  tNameGetFullDbName(pTableName, db);
  SCtgDBCache *dbCache = NULL;
  SCtgTbCache *tbCache = NULL;

  CTG_ERR_RET(ctgAcquireVgMetaFromCache(pCtg, db, pTableName->tname, &dbCache, &tbCache));

  if (NULL == dbCache || NULL == tbCache) {
    *pTableMeta = NULL;
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
632
  CTG_ERR_JRET(ctgGetVgInfoFromHashValue(pCtg, NULL, dbCache->vgCache.vgInfo, pTableName, pVgroup));
633

D
dapan1121 已提交
634 635
  ctgRUnlockVgInfo(dbCache);

636 637 638
  SCtgTbMetaCtx ctx = {0};
  ctx.pName = (SName*)pTableName;
  ctx.flag = CTG_FLAG_UNKNOWN_STB;
D
dapan1121 已提交
639 640 641 642 643
  code = ctgCopyTbMeta(pCtg, &ctx, &dbCache, &tbCache, pTableMeta, db);

  ctgReleaseTbMetaToCache(pCtg, dbCache, tbCache);

  CTG_RET(code);
644 645 646 647 648 649 650 651 652

_return:
  
  ctgReleaseVgMetaToCache(pCtg, dbCache, tbCache);

  CTG_RET(code);
}


D
dapan1121 已提交
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
int32_t ctgRemoveTbMeta(SCatalog* pCtg, SName* pTableName) {
  int32_t code = 0;

  if (NULL == pCtg || NULL == pTableName) {
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
  }

  if (NULL == pCtg->dbCache) {
    return TSDB_CODE_SUCCESS;
  }

  CTG_ERR_JRET(ctgRemoveTbMetaFromCache(pCtg, pTableName, true));

_return:

  CTG_RET(code);
}

D
dapan1121 已提交
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
void ctgProcessTimerEvent(void *param, void *tmrId) {
  CTG_API_NENTER();

  int32_t cacheMaxSize = atomic_load_32(&tsMetaCacheMaxSize);
  if (cacheMaxSize >= 0) {
    uint64_t cacheSize = 0;
    ctgGetGlobalCacheSize(&cacheSize);
    bool overflow = CTG_CACHE_OVERFLOW(cacheSize, cacheMaxSize);
    
    qDebug("catalog cache size: %" PRIu64"B, maxCaseSize:%dMB, %s", cacheSize, cacheMaxSize, overflow ? "overflow" : "NO overflow");

    if (overflow) {
      int32_t code = ctgClearCacheEnqueue(NULL, true, false, false, false);
      if (code) {
        qError("clear cache enqueue failed, error:%s", tstrerror(code));
        taosTmrReset(ctgProcessTimerEvent, CTG_DEFAULT_CACHE_MON_MSEC, NULL, gCtgMgmt.timer, &gCtgMgmt.cacheTimer);
      }

      goto _return;
    }
  }
D
dapan1121 已提交
692 693

  qTrace("reset catalog timer");
D
dapan1121 已提交
694 695 696 697 698 699 700
  taosTmrReset(ctgProcessTimerEvent, CTG_DEFAULT_CACHE_MON_MSEC, NULL, gCtgMgmt.timer, &gCtgMgmt.cacheTimer);

_return:

  CTG_API_NLEAVE();
}

D
dapan1121 已提交
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
int32_t ctgGetDBCfg(SCatalog* pCtg, SRequestConnInfo* pConn, const char* dbFName, SDbCfgInfo* pDbCfg) {
  CTG_ERR_RET(ctgReadDBCfgFromCache(pCtg, dbFName, pDbCfg));

  if (pDbCfg->cfgVersion < 0) {
    CTG_ERR_RET(ctgGetDBCfgFromMnode(pCtg, pConn, dbFName, pDbCfg, NULL));
    SDbCfgInfo *pCfg = ctgCloneDbCfgInfo(pDbCfg);    
    if (NULL == pCfg) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
    
    CTG_ERR_RET(ctgUpdateDbCfgEnqueue(pCtg, dbFName, pDbCfg->dbId, pCfg, false));
  }

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
717

X
Xiaoyu Wang 已提交
718
int32_t catalogInit(SCatalogCfg* cfg) {
D
dapan 已提交
719
  qDebug("catalogInit start");
D
dapan 已提交
720
  if (gCtgMgmt.pCluster) {
D
dapan 已提交
721
    qError("catalog already initialized");
D
dapan1121 已提交
722
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
D
dapan1121 已提交
723 724
  }

D
dapan1121 已提交
725
  memset(&gCtgMgmt, 0, sizeof(gCtgMgmt));
D
dapan1121 已提交
726

D
dapan1121 已提交
727
  if (cfg) {
D
dapan 已提交
728
    memcpy(&gCtgMgmt.cfg, cfg, sizeof(*cfg));
H
Haojun Liao 已提交
729

D
dapan 已提交
730 731
    if (gCtgMgmt.cfg.maxDBCacheNum == 0) {
      gCtgMgmt.cfg.maxDBCacheNum = CTG_DEFAULT_CACHE_DB_NUMBER;
D
dapan1121 已提交
732 733
    }

D
dapan 已提交
734 735
    if (gCtgMgmt.cfg.maxTblCacheNum == 0) {
      gCtgMgmt.cfg.maxTblCacheNum = CTG_DEFAULT_CACHE_TBLMETA_NUMBER;
D
dapan1121 已提交
736
    }
D
dapan1121 已提交
737

D
dapan 已提交
738 739
    if (gCtgMgmt.cfg.dbRentSec == 0) {
      gCtgMgmt.cfg.dbRentSec = CTG_DEFAULT_RENT_SECOND;
D
dapan1121 已提交
740 741
    }

D
dapan 已提交
742 743
    if (gCtgMgmt.cfg.stbRentSec == 0) {
      gCtgMgmt.cfg.stbRentSec = CTG_DEFAULT_RENT_SECOND;
D
dapan1121 已提交
744
    }
D
dapan1121 已提交
745
  } else {
D
dapan 已提交
746 747 748 749
    gCtgMgmt.cfg.maxDBCacheNum = CTG_DEFAULT_CACHE_DB_NUMBER;
    gCtgMgmt.cfg.maxTblCacheNum = CTG_DEFAULT_CACHE_TBLMETA_NUMBER;
    gCtgMgmt.cfg.dbRentSec = CTG_DEFAULT_RENT_SECOND;
    gCtgMgmt.cfg.stbRentSec = CTG_DEFAULT_RENT_SECOND;
D
dapan 已提交
750 751
  }

X
Xiaoyu Wang 已提交
752 753
  gCtgMgmt.pCluster = taosHashInit(CTG_DEFAULT_CACHE_CLUSTER_NUMBER, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT),
                                   false, HASH_ENTRY_LOCK);
D
dapan 已提交
754
  if (NULL == gCtgMgmt.pCluster) {
D
dapan1121 已提交
755 756
    qError("taosHashInit %d cluster cache failed", CTG_DEFAULT_CACHE_CLUSTER_NUMBER);
    CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR);
D
dapan1121 已提交
757 758
  }

D
dapan1121 已提交
759 760 761 762
  if (tsem_init(&gCtgMgmt.queue.reqSem, 0, 0)) {
    qError("tsem_init failed, error:%s", tstrerror(TAOS_SYSTEM_ERROR(errno)));
    CTG_ERR_RET(TSDB_CODE_CTG_SYS_ERROR);
  }
X
Xiaoyu Wang 已提交
763

wafwerar's avatar
wafwerar 已提交
764
  gCtgMgmt.queue.head = taosMemoryCalloc(1, sizeof(SCtgQNode));
D
dapan1121 已提交
765
  if (NULL == gCtgMgmt.queue.head) {
D
dapan1121 已提交
766
    qError("calloc %d failed", (int32_t)sizeof(SCtgQNode));
S
Shengliang Guan 已提交
767
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
768
  }
D
dapan1121 已提交
769
  gCtgMgmt.queue.tail = gCtgMgmt.queue.head;
D
dapan1121 已提交
770

D
dapan1121 已提交
771 772 773 774 775 776
  gCtgMgmt.jobPool = taosOpenRef(200, ctgFreeJob);
  if (gCtgMgmt.jobPool < 0) {
    qError("taosOpenRef failed, error:%s", tstrerror(terrno));
    CTG_ERR_RET(terrno);
  }

D
dapan1121 已提交
777 778 779 780 781 782 783 784 785 786 787 788
  gCtgMgmt.timer = taosTmrInit(0, 0, 0, "catalog");
  if (NULL == gCtgMgmt.timer) {
    qError("init timer failed, error:%s", tstrerror(terrno));
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }

  gCtgMgmt.cacheTimer = taosTmrStart(ctgProcessTimerEvent, CTG_DEFAULT_CACHE_MON_MSEC, NULL, gCtgMgmt.timer);
  if (NULL == gCtgMgmt.cacheTimer) {
    qError("start cache timer failed");
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }

D
dapan1121 已提交
789 790
  CTG_ERR_RET(ctgStartUpdateThread());

X
Xiaoyu Wang 已提交
791 792
  qDebug("catalog initialized, maxDb:%u, maxTbl:%u, dbRentSec:%u, stbRentSec:%u", gCtgMgmt.cfg.maxDBCacheNum,
         gCtgMgmt.cfg.maxTblCacheNum, gCtgMgmt.cfg.dbRentSec, gCtgMgmt.cfg.stbRentSec);
D
dapan1121 已提交
793

D
dapan 已提交
794
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
795 796
}

D
dapan1121 已提交
797
int32_t catalogGetHandle(uint64_t clusterId, SCatalog** catalogHandle) {
798
  if (NULL == catalogHandle) {
D
dapan1121 已提交
799
    CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);
D
dapan 已提交
800 801
  }

D
dapan1121 已提交
802 803
  CTG_API_ENTER();

D
dapan 已提交
804
  if (NULL == gCtgMgmt.pCluster) {
D
dapan1121 已提交
805
    qError("catalog cluster cache are not ready, clusterId:0x%" PRIx64, clusterId);
D
dapan1121 已提交
806
    CTG_API_LEAVE(TSDB_CODE_CTG_NOT_READY);
D
dapan 已提交
807 808
  }

X
Xiaoyu Wang 已提交
809 810
  int32_t   code = 0;
  SCatalog* clusterCtg = NULL;
D
dapan 已提交
811

D
dapan1121 已提交
812
  while (true) {
X
Xiaoyu Wang 已提交
813
    SCatalog** ctg = (SCatalog**)taosHashGet(gCtgMgmt.pCluster, (char*)&clusterId, sizeof(clusterId));
D
dapan 已提交
814

D
dapan1121 已提交
815 816
    if (ctg && (*ctg)) {
      *catalogHandle = *ctg;
D
dapan1121 已提交
817
      CTG_STAT_HIT_INC(CTG_CI_CLUSTER, 1);
D
dapan1121 已提交
818
      qDebug("got catalog handle from cache, clusterId:0x%" PRIx64 ", CTG:%p", clusterId, *ctg);
D
dapan1121 已提交
819
      CTG_API_LEAVE(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
820
    }
D
dapan 已提交
821

D
dapan1121 已提交
822
    CTG_STAT_NHIT_INC(CTG_CI_CLUSTER, 1);
D
dapan1121 已提交
823

wafwerar's avatar
wafwerar 已提交
824
    clusterCtg = taosMemoryCalloc(1, sizeof(SCatalog));
D
dapan1121 已提交
825 826
    if (NULL == clusterCtg) {
      qError("calloc %d failed", (int32_t)sizeof(SCatalog));
D
dapan1121 已提交
827
      CTG_API_LEAVE(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
828 829
    }

D
dapan1121 已提交
830 831
    clusterCtg->clusterId = clusterId;

D
dapan1121 已提交
832
    CTG_ERR_JRET(ctgMetaRentInit(&clusterCtg->dbRent, gCtgMgmt.cfg.dbRentSec, CTG_RENT_DB, sizeof(SDbCacheInfo)));
D
dapan1121 已提交
833
    CTG_ERR_JRET(ctgMetaRentInit(&clusterCtg->stbRent, gCtgMgmt.cfg.stbRentSec, CTG_RENT_STABLE, sizeof(SSTableVersion)));
D
dapan1121 已提交
834

X
Xiaoyu Wang 已提交
835 836
    clusterCtg->dbCache = taosHashInit(gCtgMgmt.cfg.maxDBCacheNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY),
                                       false, HASH_ENTRY_LOCK);
D
dapan1121 已提交
837 838
    if (NULL == clusterCtg->dbCache) {
      qError("taosHashInit %d dbCache failed", CTG_DEFAULT_CACHE_DB_NUMBER);
D
dapan1121 已提交
839 840 841
      CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
    }

H
Hongze Cheng 已提交
842 843
    clusterCtg->userCache = taosHashInit(gCtgMgmt.cfg.maxUserCacheNum,
                                         taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
D
dapan1121 已提交
844 845 846
    if (NULL == clusterCtg->userCache) {
      qError("taosHashInit %d user cache failed", gCtgMgmt.cfg.maxUserCacheNum);
      CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
847 848
    }

D
dapan 已提交
849
    code = taosHashPut(gCtgMgmt.pCluster, &clusterId, sizeof(clusterId), &clusterCtg, POINTER_BYTES);
D
dapan1121 已提交
850 851
    if (code) {
      if (HASH_NODE_EXIST(code)) {
D
dapan1121 已提交
852
        ctgFreeHandleImpl(clusterCtg);
D
dapan1121 已提交
853 854
        continue;
      }
X
Xiaoyu Wang 已提交
855

D
dapan1121 已提交
856
      qError("taosHashPut CTG to cache failed, clusterId:0x%" PRIx64, clusterId);
D
dapan1121 已提交
857 858 859
      CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
    }

D
dapan1121 已提交
860
    qDebug("add CTG to cache, clusterId:0x%" PRIx64 ", CTG:%p", clusterId, clusterCtg);
D
dapan1121 已提交
861 862

    break;
D
dapan 已提交
863
  }
D
dapan1121 已提交
864 865

  *catalogHandle = clusterCtg;
D
dapan1121 已提交
866

D
dapan1121 已提交
867
  CTG_STAT_NUM_INC(CTG_CI_CLUSTER, 1);
X
Xiaoyu Wang 已提交
868

D
dapan1121 已提交
869
  CTG_API_LEAVE(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
870 871 872

_return:

D
dapan1121 已提交
873
  ctgFreeHandleImpl(clusterCtg);
D
dapan1121 已提交
874

D
dapan1121 已提交
875
  CTG_API_LEAVE(code);
D
dapan 已提交
876 877
}

D
dapan1121 已提交
878
int32_t catalogGetDBVgVersion(SCatalog* pCtg, const char* dbFName, int32_t* version, int64_t* dbId, int32_t* tableNum, int64_t* pStateTs) {
D
dapan1121 已提交
879 880
  CTG_API_ENTER();

D
dapan1121 已提交
881
  if (NULL == pCtg || NULL == dbFName || NULL == version || NULL == dbId || NULL == tableNum || NULL == pStateTs) {
D
dapan1121 已提交
882 883 884
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

X
Xiaoyu Wang 已提交
885 886
  SCtgDBCache* dbCache = NULL;
  int32_t      code = 0;
D
dapan1121 已提交
887

D
dapan1121 已提交
888 889
  CTG_ERR_JRET(ctgAcquireVgInfoFromCache(pCtg, dbFName, &dbCache));
  if (NULL == dbCache) {
D
dapan1121 已提交
890
    *version = CTG_DEFAULT_INVALID_VERSION;
D
dapan1121 已提交
891
    CTG_API_LEAVE(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
892 893
  }

D
dapan1121 已提交
894
  *version = dbCache->vgCache.vgInfo->vgVersion;
D
dapan1121 已提交
895
  *dbId = dbCache->dbId;
D
dapan1121 已提交
896
  *tableNum = dbCache->vgCache.vgInfo->numOfTable;
D
dapan1121 已提交
897

D
dapan1121 已提交
898
  ctgReleaseVgInfoToCache(pCtg, dbCache);
D
dapan1121 已提交
899

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

D
dapan1121 已提交
902
  CTG_API_LEAVE(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
903 904 905 906

_return:

  CTG_API_LEAVE(code);
D
dapan1121 已提交
907 908
}

909
int32_t catalogGetDBVgList(SCatalog* pCtg, SRequestConnInfo* pConn, const char* dbFName, SArray** vgroupList) {
D
dapan1121 已提交
910 911
  CTG_API_ENTER();

D
dapan1121 已提交
912
  if (NULL == pCtg || NULL == dbFName || NULL == pConn || NULL == vgroupList) {
D
dapan1121 已提交
913 914
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }
915

D
dapan1121 已提交
916
  SCtgDBCache* dbCache = NULL;
H
Hongze Cheng 已提交
917 918 919 920
  int32_t      code = 0;
  SArray*      vgList = NULL;
  SHashObj*    vgHash = NULL;
  SDBVgInfo*   vgInfo = NULL;
921
  CTG_ERR_JRET(ctgGetDBVgInfo(pCtg, pConn, dbFName, &dbCache, &vgInfo, NULL));
D
dapan1121 已提交
922
  if (dbCache) {
D
dapan1121 已提交
923
    vgHash = dbCache->vgCache.vgInfo->vgHash;
D
dapan1121 已提交
924 925
  } else {
    vgHash = vgInfo->vgHash;
D
dapan1121 已提交
926 927
  }

D
dapan1121 已提交
928
  CTG_ERR_JRET(ctgGenerateVgList(pCtg, vgHash, &vgList));
D
dapan1121 已提交
929 930 931 932 933

  *vgroupList = vgList;
  vgList = NULL;

_return:
D
dapan1121 已提交
934 935

  if (dbCache) {
D
dapan1121 已提交
936
    ctgRUnlockVgInfo(dbCache);
D
dapan1121 已提交
937
    ctgReleaseDBCache(pCtg, dbCache);
D
dapan1121 已提交
938 939
  }

D
dapan1121 已提交
940
  if (vgInfo) {
941
    freeVgInfo(vgInfo);
D
dapan1121 已提交
942 943
  }

X
Xiaoyu Wang 已提交
944
  CTG_API_LEAVE(code);
D
dapan1121 已提交
945 946
}

947 948 949 950 951 952 953 954 955 956
int32_t catalogGetDBVgInfo(SCatalog* pCtg, SRequestConnInfo* pConn, const char* dbFName, TAOS_DB_ROUTE_INFO* pInfo) {
  CTG_API_ENTER();

  if (NULL == pCtg || NULL == dbFName || NULL == pConn || NULL == pInfo) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

  SCtgDBCache* dbCache = NULL;
  int32_t      code = 0;
  SDBVgInfo*   dbInfo = NULL;
D
dapan1121 已提交
957
  CTG_ERR_JRET(ctgGetDBVgInfo(pCtg, pConn, dbFName, &dbCache, &dbInfo, NULL));
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
  if (dbCache) {
    dbInfo = dbCache->vgCache.vgInfo;
  }

  pInfo->routeVersion = dbInfo->vgVersion;
  pInfo->hashPrefix = dbInfo->hashPrefix;
  pInfo->hashSuffix = dbInfo->hashSuffix;
  pInfo->hashMethod = dbInfo->hashMethod;
  pInfo->vgNum = taosHashGetSize(dbInfo->vgHash);
  if (pInfo->vgNum <= 0) {
    ctgError("invalid vgNum %d in db %s's vgHash", pInfo->vgNum, dbFName);
    CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR);
  }

  pInfo->vgHash = taosMemoryCalloc(pInfo->vgNum, sizeof(TAOS_VGROUP_HASH_INFO));
  if (NULL == pInfo->vgHash) {
    CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
  }

  SVgroupInfo* vgInfo = NULL;  
  int32_t i = 0;
  void* pIter = taosHashIterate(dbInfo->vgHash, NULL);
  while (pIter) {
    vgInfo = pIter;

    pInfo->vgHash[i].vgId = vgInfo->vgId;
    pInfo->vgHash[i].hashBegin = vgInfo->hashBegin;
    pInfo->vgHash[i].hashEnd = vgInfo->hashEnd;
    
    pIter = taosHashIterate(dbInfo->vgHash, pIter);
    vgInfo = NULL;
    ++i;
  }

_return:

  if (dbCache) {
    ctgRUnlockVgInfo(dbCache);
    ctgReleaseDBCache(pCtg, dbCache);
997
  } else if (dbInfo) {
998
    freeVgInfo(dbInfo);
999 1000 1001 1002 1003
  }

  CTG_API_LEAVE(code);
}

D
dapan1121 已提交
1004
int32_t catalogUpdateDBVgInfo(SCatalog* pCtg, const char* dbFName, uint64_t dbId, SDBVgInfo* dbInfo) {
D
dapan1121 已提交
1005
  CTG_API_ENTER();
D
dapan1121 已提交
1006 1007

  int32_t code = 0;
X
Xiaoyu Wang 已提交
1008

D
dapan1121 已提交
1009
  if (NULL == pCtg || NULL == dbFName || NULL == dbInfo) {
1010
    freeVgInfo(dbInfo);
D
dapan1121 已提交
1011 1012 1013
    CTG_ERR_JRET(TSDB_CODE_CTG_INVALID_INPUT);
  }

D
dapan1121 已提交
1014
  code = ctgUpdateVgroupEnqueue(pCtg, dbFName, dbId, dbInfo, false);
D
dapan1121 已提交
1015

D
dapan1121 已提交
1016 1017
_return:

D
dapan1121 已提交
1018
  CTG_API_LEAVE(code);
D
dapan1121 已提交
1019 1020
}

D
dapan1121 已提交
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
int32_t catalogUpdateDbCfg(SCatalog* pCtg, const char* dbFName, uint64_t dbId, SDbCfgInfo* cfgInfo) {
  CTG_API_ENTER();

  int32_t code = 0;

  if (NULL == pCtg || NULL == dbFName || NULL == cfgInfo) {
    freeDbCfgInfo(cfgInfo);
    CTG_ERR_JRET(TSDB_CODE_CTG_INVALID_INPUT);
  }

  code = ctgUpdateDbCfgEnqueue(pCtg, dbFName, dbId, cfgInfo, false);

_return:

  CTG_API_LEAVE(code);
}

D
dapan1121 已提交
1038 1039 1040
int32_t catalogRemoveDB(SCatalog* pCtg, const char* dbFName, uint64_t dbId) {
  CTG_API_ENTER();

D
dapan1121 已提交
1041
  int32_t code = 0;
X
Xiaoyu Wang 已提交
1042

D
dapan1121 已提交
1043 1044
  if (NULL == pCtg || NULL == dbFName) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
D
dapan1121 已提交
1045 1046
  }

D
dapan1121 已提交
1047
  if (NULL == pCtg->dbCache) {
D
dapan1121 已提交
1048
    CTG_API_LEAVE(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
1049
  }
D
dapan1121 已提交
1050

D
dapan1121 已提交
1051
  CTG_ERR_JRET(ctgDropDbCacheEnqueue(pCtg, dbFName, dbId));
D
dapan 已提交
1052

D
dapan1121 已提交
1053
  CTG_API_LEAVE(TSDB_CODE_SUCCESS);
X
Xiaoyu Wang 已提交
1054

D
dapan1121 已提交
1055 1056
_return:

D
dapan1121 已提交
1057
  CTG_API_LEAVE(code);
D
dapan1121 已提交
1058 1059
}

X
Xiaoyu Wang 已提交
1060
int32_t catalogUpdateVgEpSet(SCatalog* pCtg, const char* dbFName, int32_t vgId, SEpSet* epSet) {
D
dapan1121 已提交
1061 1062 1063
  CTG_API_ENTER();

  int32_t code = 0;
X
Xiaoyu Wang 已提交
1064

D
dapan1121 已提交
1065 1066 1067 1068 1069 1070 1071
  if (NULL == pCtg || NULL == dbFName || NULL == epSet) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

  CTG_ERR_JRET(ctgUpdateVgEpsetEnqueue(pCtg, (char*)dbFName, vgId, epSet));

_return:
X
Xiaoyu Wang 已提交
1072

D
dapan1121 已提交
1073
  CTG_API_LEAVE(code);
D
dapan1121 已提交
1074
}
D
dapan1121 已提交
1075

H
Hongze Cheng 已提交
1076
int32_t catalogUpdateTableIndex(SCatalog* pCtg, STableIndexRsp* pRsp) {
D
dapan1121 已提交
1077 1078 1079
  CTG_API_ENTER();

  int32_t code = 0;
H
Hongze Cheng 已提交
1080

D
dapan1121 已提交
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
  if (NULL == pCtg || NULL == pRsp) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

  STableIndex* pIndex = taosMemoryCalloc(1, sizeof(STableIndex));
  if (NULL == pIndex) {
    CTG_API_LEAVE(TSDB_CODE_OUT_OF_MEMORY);
  }

  memcpy(pIndex, pRsp, sizeof(STableIndex));

  CTG_ERR_JRET(ctgUpdateTbIndexEnqueue(pCtg, &pIndex, false));

_return:
H
Hongze Cheng 已提交
1095

D
dapan1121 已提交
1096 1097 1098
  CTG_API_LEAVE(code);
}

D
dapan1121 已提交
1099
int32_t catalogRemoveTableMeta(SCatalog* pCtg, SName* pTableName) {
D
dapan 已提交
1100 1101
  CTG_API_ENTER();

D
dapan1121 已提交
1102
  CTG_API_LEAVE(ctgRemoveTbMeta(pCtg, pTableName));
D
dapan 已提交
1103 1104
}

D
dapan1121 已提交
1105 1106 1107
int32_t catalogRemoveStbMeta(SCatalog* pCtg, const char* dbFName, uint64_t dbId, const char* stbName, uint64_t suid) {
  CTG_API_ENTER();

D
dapan 已提交
1108
  int32_t code = 0;
X
Xiaoyu Wang 已提交
1109

D
dapan1121 已提交
1110 1111
  if (NULL == pCtg || NULL == dbFName || NULL == stbName) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
D
dapan 已提交
1112 1113
  }

D
dapan1121 已提交
1114
  if (NULL == pCtg->dbCache) {
D
dapan1121 已提交
1115
    CTG_API_LEAVE(TSDB_CODE_SUCCESS);
D
dapan 已提交
1116
  }
D
dapan1121 已提交
1117

D
dapan1121 已提交
1118
  CTG_ERR_JRET(ctgDropStbMetaEnqueue(pCtg, dbFName, dbId, stbName, suid, true));
D
dapan 已提交
1119

D
dapan1121 已提交
1120
  CTG_API_LEAVE(TSDB_CODE_SUCCESS);
X
Xiaoyu Wang 已提交
1121

D
dapan1121 已提交
1122 1123
_return:

D
dapan1121 已提交
1124
  CTG_API_LEAVE(code);
D
dapan 已提交
1125 1126
}

H
Hongze Cheng 已提交
1127
int32_t catalogGetTableMeta(SCatalog* pCtg, SRequestConnInfo* pConn, const SName* pTableName, STableMeta** pTableMeta) {
D
dapan1121 已提交
1128 1129
  CTG_API_ENTER();

D
dapan1121 已提交
1130 1131 1132
  SCtgTbMetaCtx ctx = {0};
  ctx.pName = (SName*)pTableName;
  ctx.flag = CTG_FLAG_UNKNOWN_STB;
H
Hongze Cheng 已提交
1133

D
dapan1121 已提交
1134
  CTG_API_LEAVE(ctgGetTbMeta(pCtg, pConn, &ctx, pTableMeta));
D
dapan1121 已提交
1135
}
D
dapan1121 已提交
1136

D
dapan1121 已提交
1137
int32_t catalogGetCachedTableMeta(SCatalog* pCtg, const SName* pTableName, STableMeta** pTableMeta) {
1138 1139 1140 1141 1142 1143
  CTG_API_ENTER();

  SCtgTbMetaCtx ctx = {0};
  ctx.pName = (SName*)pTableName;
  ctx.flag = CTG_FLAG_UNKNOWN_STB | CTG_FLAG_ONLY_CACHE;

D
dapan1121 已提交
1144
  CTG_API_LEAVE(ctgGetTbMeta(pCtg, NULL, &ctx, pTableMeta));
1145 1146 1147
}


H
Hongze Cheng 已提交
1148 1149
int32_t catalogGetSTableMeta(SCatalog* pCtg, SRequestConnInfo* pConn, const SName* pTableName,
                             STableMeta** pTableMeta) {
D
dapan1121 已提交
1150 1151
  CTG_API_ENTER();

D
dapan1121 已提交
1152 1153 1154 1155
  SCtgTbMetaCtx ctx = {0};
  ctx.pName = (SName*)pTableName;
  ctx.flag = CTG_FLAG_STB;

D
dapan1121 已提交
1156
  CTG_API_LEAVE(ctgGetTbMeta(pCtg, pConn, &ctx, pTableMeta));
D
dapan1121 已提交
1157 1158
}

D
dapan1121 已提交
1159
int32_t catalogGetCachedSTableMeta(SCatalog* pCtg, const SName* pTableName,          STableMeta** pTableMeta) {
1160 1161 1162 1163 1164 1165
  CTG_API_ENTER();

  SCtgTbMetaCtx ctx = {0};
  ctx.pName = (SName*)pTableName;
  ctx.flag = CTG_FLAG_STB | CTG_FLAG_ONLY_CACHE;

D
dapan1121 已提交
1166
  CTG_API_LEAVE(ctgGetTbMeta(pCtg, NULL, &ctx, pTableMeta));
1167 1168 1169
}


X
Xiaoyu Wang 已提交
1170
int32_t catalogUpdateTableMeta(SCatalog* pCtg, STableMetaRsp* pMsg) {
D
dapan1121 已提交
1171 1172
  CTG_API_ENTER();

D
dapan1121 已提交
1173
  if (NULL == pCtg || NULL == pMsg) {
D
dapan1121 已提交
1174 1175 1176
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

D
dapan1121 已提交
1177
  int32_t code = 0;
D
dapan1121 已提交
1178
  CTG_ERR_JRET(ctgUpdateTbMeta(pCtg, pMsg, true));
X
Xiaoyu Wang 已提交
1179

D
dapan1121 已提交
1180
_return:
X
Xiaoyu Wang 已提交
1181

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

D
dapan1121 已提交
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
int32_t catalogAsyncUpdateTableMeta(SCatalog* pCtg, STableMetaRsp* pMsg) {
  CTG_API_ENTER();

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

  int32_t code = 0;
  CTG_ERR_JRET(ctgUpdateTbMeta(pCtg, pMsg, false));

_return:

  CTG_API_LEAVE(code);
}


H
Hongze Cheng 已提交
1201
int32_t catalogChkTbMetaVersion(SCatalog* pCtg, SRequestConnInfo* pConn, SArray* pTables) {
D
dapan1121 已提交
1202 1203
  CTG_API_ENTER();

D
dapan1121 已提交
1204
  if (NULL == pCtg || NULL == pConn || NULL == pTables) {
D
dapan1121 已提交
1205 1206
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }
D
dapan1121 已提交
1207

D
dapan1121 已提交
1208
  SName   name = {0};
D
dapan1121 已提交
1209
  int32_t sver = 0;
D
dapan1121 已提交
1210
  int32_t tver = 0;
D
dapan1121 已提交
1211 1212 1213
  int32_t tbNum = taosArrayGetSize(pTables);
  for (int32_t i = 0; i < tbNum; ++i) {
    STbSVersion* pTb = (STbSVersion*)taosArrayGet(pTables, i);
D
dapan1121 已提交
1214 1215 1216
    if (NULL == pTb->tbFName || 0 == pTb->tbFName[0]) {
      continue;
    }
X
Xiaoyu Wang 已提交
1217

D
dapan1121 已提交
1218
    tNameFromString(&name, pTb->tbFName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
D
dapan1121 已提交
1219

D
dapan1121 已提交
1220
    if (IS_SYS_DBNAME(name.dbname)) {
D
dapan1121 已提交
1221 1222 1223
      continue;
    }

L
Liu Jicong 已提交
1224
    int32_t  tbType = 0;
D
dapan1121 已提交
1225
    uint64_t suid = 0;
L
Liu Jicong 已提交
1226
    char     stbName[TSDB_TABLE_FNAME_LEN];
D
dapan1121 已提交
1227 1228
    ctgReadTbVerFromCache(pCtg, &name, &sver, &tver, &tbType, &suid, stbName);
    if ((sver >= 0 && sver < pTb->sver) || (tver >= 0 && tver < pTb->tver)) {
D
dapan1121 已提交
1229 1230 1231
      switch (tbType) {
        case TSDB_CHILD_TABLE: {
          SName stb = name;
D
dapan1121 已提交
1232
          tstrncpy(stb.tname, stbName, sizeof(stb.tname));
D
dapan1121 已提交
1233
          ctgRemoveTbMeta(pCtg, &stb);
D
dapan1121 已提交
1234 1235 1236 1237
          break;
        }
        case TSDB_SUPER_TABLE:
        case TSDB_NORMAL_TABLE:
D
dapan1121 已提交
1238
          ctgRemoveTbMeta(pCtg, &name);
D
dapan1121 已提交
1239 1240 1241 1242 1243
          break;
        default:
          ctgError("ignore table type %d", tbType);
          break;
      }
D
dapan1121 已提交
1244 1245 1246 1247
    }
  }

  CTG_API_LEAVE(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
1248 1249
}

H
Hongze Cheng 已提交
1250
int32_t catalogRefreshDBVgInfo(SCatalog* pCtg, SRequestConnInfo* pConn, const char* dbFName) {
D
dapan1121 已提交
1251 1252
  CTG_API_ENTER();

D
dapan1121 已提交
1253
  if (NULL == pCtg || NULL == pConn || NULL == dbFName) {
D
dapan1121 已提交
1254 1255 1256
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

D
dapan1121 已提交
1257
  CTG_API_LEAVE(ctgRefreshDBVgInfo(pCtg, pConn, dbFName));
D
dapan1121 已提交
1258
}
D
dapan1121 已提交
1259

H
Hongze Cheng 已提交
1260
int32_t catalogRefreshTableMeta(SCatalog* pCtg, SRequestConnInfo* pConn, const SName* pTableName, int32_t isSTable) {
D
dapan1121 已提交
1261 1262
  CTG_API_ENTER();

D
dapan1121 已提交
1263
  if (NULL == pCtg || NULL == pConn || NULL == pTableName) {
D
dapan1121 已提交
1264 1265 1266
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

D
dapan1121 已提交
1267 1268 1269
  SCtgTbMetaCtx ctx = {0};
  ctx.pName = (SName*)pTableName;
  ctx.flag = CTG_FLAG_FORCE_UPDATE | CTG_FLAG_MAKE_STB(isSTable);
1270 1271 1272
  if (IS_SYS_DBNAME(ctx.pName->dbname)) {
    CTG_FLAG_SET_SYS_DB(ctx.flag);
  }
D
dapan1121 已提交
1273

D
dapan1121 已提交
1274
  CTG_API_LEAVE(ctgRefreshTbMeta(pCtg, pConn, &ctx, NULL, true));
1275
}
1276

H
Hongze Cheng 已提交
1277 1278
int32_t catalogRefreshGetTableMeta(SCatalog* pCtg, SRequestConnInfo* pConn, const SName* pTableName,
                                   STableMeta** pTableMeta, int32_t isSTable) {
D
dapan1121 已提交
1279 1280
  CTG_API_ENTER();

D
dapan1121 已提交
1281 1282 1283 1284
  SCtgTbMetaCtx ctx = {0};
  ctx.pName = (SName*)pTableName;
  ctx.flag = CTG_FLAG_FORCE_UPDATE | CTG_FLAG_MAKE_STB(isSTable);

D
dapan1121 已提交
1285
  CTG_API_LEAVE(ctgGetTbMeta(pCtg, pConn, &ctx, pTableMeta));
D
dapan1121 已提交
1286 1287
}

H
Hongze Cheng 已提交
1288
int32_t catalogGetTableDistVgInfo(SCatalog* pCtg, SRequestConnInfo* pConn, const SName* pTableName, SArray** pVgList) {
D
dapan1121 已提交
1289
  CTG_API_ENTER();
D
dapan1121 已提交
1290

D
dapan1121 已提交
1291
  if (NULL == pCtg || NULL == pConn || NULL == pTableName || NULL == pVgList) {
D
dapan1121 已提交
1292 1293
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }
D
dapan1121 已提交
1294

D
dapan1121 已提交
1295
  if (IS_SYS_DBNAME(pTableName->dbname)) {
D
dapan1121 已提交
1296 1297 1298
    ctgError("no valid vgInfo for db, dbname:%s", pTableName->dbname);
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }
D
dapan1121 已提交
1299

D
dapan1121 已提交
1300
  CTG_API_LEAVE(ctgGetTbDistVgInfo(pCtg, pConn, (SName*)pTableName, pVgList));
D
dapan1121 已提交
1301 1302
}

H
Hongze Cheng 已提交
1303 1304
int32_t catalogGetTableHashVgroup(SCatalog* pCtg, SRequestConnInfo* pConn, const SName* pTableName,
                                  SVgroupInfo* pVgroup) {
D
dapan1121 已提交
1305 1306
  CTG_API_ENTER();

1307 1308 1309
  CTG_API_LEAVE(ctgGetTbHashVgroup(pCtg, pConn, pTableName, pVgroup, NULL));
}

D
dapan1121 已提交
1310 1311 1312 1313 1314 1315 1316
int32_t catalogGetTablesHashVgId(SCatalog* pCtg, SRequestConnInfo* pConn, int32_t acctId, const char* pDb, const char* pTableName[],
                                  int32_t tableNum, int32_t *vgId) {
  CTG_API_ENTER();

  CTG_API_LEAVE(ctgGetTbsHashVgId(pCtg, pConn, acctId, pDb, pTableName, tableNum, vgId));
}

D
dapan1121 已提交
1317
int32_t catalogGetCachedTableHashVgroup(SCatalog* pCtg, const SName* pTableName,           SVgroupInfo* pVgroup, bool* exists) {
1318 1319
  CTG_API_ENTER();

D
dapan1121 已提交
1320
  CTG_API_LEAVE(ctgGetTbHashVgroup(pCtg, NULL, pTableName, pVgroup, exists));
D
dapan1121 已提交
1321 1322
}

1323 1324 1325
int32_t catalogGetCachedTableVgMeta(SCatalog* pCtg, const SName* pTableName,          SVgroupInfo* pVgroup, STableMeta** pTableMeta) {
  CTG_API_ENTER();

1326
  CTG_API_LEAVE(ctgGetCachedTbVgMeta(pCtg, pTableName, pVgroup, pTableMeta));
1327 1328 1329
}


D
dapan1121 已提交
1330
#if 0
H
Hongze Cheng 已提交
1331
int32_t catalogGetAllMeta(SCatalog* pCtg, SRequestConnInfo* pConn, const SCatalogReq* pReq, SMetaData* pRsp) {
D
dapan1121 已提交
1332 1333
  CTG_API_ENTER();

D
dapan1121 已提交
1334
  if (NULL == pCtg || NULL == pConn || NULL == pReq || NULL == pRsp) {
D
dapan1121 已提交
1335 1336 1337
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

D
dapan1121 已提交
1338
  int32_t code = 0;
D
dapan1121 已提交
1339
  pRsp->pTableMeta = NULL;
D
dapan1121 已提交
1340

D
dapan1121 已提交
1341 1342
  if (pReq->pTableMeta) {
    int32_t tbNum = (int32_t)taosArrayGetSize(pReq->pTableMeta);
D
dapan1121 已提交
1343
    if (tbNum <= 0) {
D
dapan1121 已提交
1344
      ctgError("empty table name list, tbNum:%d", tbNum);
D
dapan1121 已提交
1345
      CTG_ERR_JRET(TSDB_CODE_CTG_INVALID_INPUT);
D
dapan1121 已提交
1346
    }
H
Haojun Liao 已提交
1347

D
dapan1121 已提交
1348 1349
    pRsp->pTableMeta = taosArrayInit(tbNum, POINTER_BYTES);
    if (NULL == pRsp->pTableMeta) {
D
dapan1121 已提交
1350
      ctgError("taosArrayInit %d failed", tbNum);
D
dapan1121 已提交
1351
      CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1352
    }
X
Xiaoyu Wang 已提交
1353

D
dapan1121 已提交
1354
    for (int32_t i = 0; i < tbNum; ++i) {
X
Xiaoyu Wang 已提交
1355 1356
      SName*        name = taosArrayGet(pReq->pTableMeta, i);
      STableMeta*   pTableMeta = NULL;
D
dapan1121 已提交
1357 1358 1359
      SCtgTbMetaCtx ctx = {0};
      ctx.pName = name;
      ctx.flag = CTG_FLAG_UNKNOWN_STB;
H
Hongze Cheng 已提交
1360

D
dapan1121 已提交
1361
      CTG_ERR_JRET(ctgGetTbMeta(pCtg, pConn, &ctx, &pTableMeta));
D
dapan1121 已提交
1362 1363 1364

      if (NULL == taosArrayPush(pRsp->pTableMeta, &pTableMeta)) {
        ctgError("taosArrayPush failed, idx:%d", i);
wafwerar's avatar
wafwerar 已提交
1365
        taosMemoryFreeClear(pTableMeta);
D
dapan1121 已提交
1366
        CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1367 1368 1369 1370
      }
    }
  }

D
dapan1121 已提交
1371
  if (pReq->qNodeRequired) {
D
dapan1121 已提交
1372
    pRsp->pQnodeList = taosArrayInit(10, sizeof(SQueryNodeLoad));
D
dapan1121 已提交
1373
    CTG_ERR_JRET(ctgGetQnodeListFromMnode(pCtg, pConn, pRsp->pQnodeList, NULL));
D
dapan1121 已提交
1374 1375
  }

D
dapan1121 已提交
1376
  CTG_API_LEAVE(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
1377

X
Xiaoyu Wang 已提交
1378
_return:
D
dapan1121 已提交
1379

D
dapan1121 已提交
1380 1381 1382
  if (pRsp->pTableMeta) {
    int32_t aSize = taosArrayGetSize(pRsp->pTableMeta);
    for (int32_t i = 0; i < aSize; ++i) {
X
Xiaoyu Wang 已提交
1383
      STableMeta* pMeta = taosArrayGetP(pRsp->pTableMeta, i);
wafwerar's avatar
wafwerar 已提交
1384
      taosMemoryFreeClear(pMeta);
D
dapan1121 已提交
1385
    }
X
Xiaoyu Wang 已提交
1386

D
dapan1121 已提交
1387
    taosArrayDestroy(pRsp->pTableMeta);
D
dapan1121 已提交
1388
    pRsp->pTableMeta = NULL;
D
dapan1121 已提交
1389
  }
X
Xiaoyu Wang 已提交
1390

D
dapan1121 已提交
1391
  CTG_API_LEAVE(code);
1392
}
D
dapan1121 已提交
1393
#endif
D
dapan 已提交
1394

H
Hongze Cheng 已提交
1395 1396
int32_t catalogAsyncGetAllMeta(SCatalog* pCtg, SRequestConnInfo* pConn, const SCatalogReq* pReq, catalogCallback fp,
                               void* param, int64_t* jobId) {
D
dapan1121 已提交
1397 1398
  CTG_API_ENTER();

D
dapan1121 已提交
1399
  if (NULL == pCtg || NULL == pConn || NULL == pReq || NULL == fp || NULL == param) {
D
dapan1121 已提交
1400 1401 1402
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

H
Hongze Cheng 已提交
1403 1404
  int32_t  code = 0;
  SCtgJob* pJob = NULL;
D
dapan1121 已提交
1405
  CTG_ERR_JRET(ctgInitJob(pCtg, pConn, &pJob, pReq, fp, param));
D
dapan1121 已提交
1406 1407 1408

  CTG_ERR_JRET(ctgLaunchJob(pJob));

1409
  // NOTE: here the assignment of jobId is invalid, may over-write the true scheduler created query job.
X
Xiaoyu Wang 已提交
1410 1411
  //  *jobId = pJob->refId;

D
dapan1121 已提交
1412
_return:
D
dapan1121 已提交
1413

D
dapan1121 已提交
1414 1415 1416 1417 1418 1419 1420
  if (pJob) {
    taosReleaseRef(gCtgMgmt.jobPool, pJob->refId);

    if (code) {
      taosRemoveRef(gCtgMgmt.jobPool, pJob->refId);
    }
  }
X
Xiaoyu Wang 已提交
1421

D
dapan1121 已提交
1422 1423 1424
  CTG_API_LEAVE(code);
}

H
Hongze Cheng 已提交
1425
int32_t catalogGetQnodeList(SCatalog* pCtg, SRequestConnInfo* pConn, SArray* pQnodeList) {
D
dapan1121 已提交
1426
  CTG_API_ENTER();
X
Xiaoyu Wang 已提交
1427

D
dapan1121 已提交
1428
  int32_t code = 0;
D
dapan1121 已提交
1429
  if (NULL == pCtg || NULL == pConn || NULL == pQnodeList) {
D
dapan1121 已提交
1430 1431 1432
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

D
dapan1121 已提交
1433
  CTG_CACHE_NHIT_INC(CTG_CI_QNODE, 1);
D
dapan1121 已提交
1434
  CTG_ERR_JRET(ctgGetQnodeListFromMnode(pCtg, pConn, pQnodeList, NULL));
D
dapan1121 已提交
1435 1436

_return:
D
dapan 已提交
1437

D
dapan1121 已提交
1438
  CTG_API_LEAVE(TSDB_CODE_SUCCESS);
D
dapan 已提交
1439 1440
}

D
dapan1121 已提交
1441 1442 1443 1444 1445 1446 1447 1448
int32_t catalogGetDnodeList(SCatalog* pCtg, SRequestConnInfo* pConn, SArray** pDnodeList) {
  CTG_API_ENTER();

  int32_t code = 0;
  if (NULL == pCtg || NULL == pConn || NULL == pDnodeList) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

D
dapan1121 已提交
1449
  CTG_CACHE_NHIT_INC(CTG_CI_DNODE, 1);
D
dapan1121 已提交
1450 1451 1452 1453 1454
  CTG_ERR_JRET(ctgGetDnodeListFromMnode(pCtg, pConn, pDnodeList, NULL));

_return:

  CTG_API_LEAVE(TSDB_CODE_SUCCESS);
1455 1456
}

H
Hongze Cheng 已提交
1457
int32_t catalogGetExpiredSTables(SCatalog* pCtg, SSTableVersion** stables, uint32_t* num) {
D
dapan1121 已提交
1458 1459
  CTG_API_ENTER();

D
dapan1121 已提交
1460 1461
  if (NULL == pCtg || NULL == stables || NULL == num) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
D
dapan1121 已提交
1462 1463
  }

H
Hongze Cheng 已提交
1464
  CTG_API_LEAVE(ctgMetaRentGet(&pCtg->stbRent, (void**)stables, num, sizeof(SSTableVersion)));
D
dapan1121 已提交
1465 1466
}

D
dapan1121 已提交
1467
int32_t catalogGetExpiredDBs(SCatalog* pCtg, SDbCacheInfo** dbs, uint32_t* num) {
D
dapan1121 已提交
1468
  CTG_API_ENTER();
X
Xiaoyu Wang 已提交
1469

D
dapan1121 已提交
1470 1471 1472
  if (NULL == pCtg || NULL == dbs || NULL == num) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }
D
dapan1121 已提交
1473

D
dapan1121 已提交
1474
  CTG_API_LEAVE(ctgMetaRentGet(&pCtg->dbRent, (void**)dbs, num, sizeof(SDbCacheInfo)));
D
dapan1121 已提交
1475 1476
}

X
Xiaoyu Wang 已提交
1477
int32_t catalogGetExpiredUsers(SCatalog* pCtg, SUserAuthVersion** users, uint32_t* num) {
D
dapan 已提交
1478
  CTG_API_ENTER();
X
Xiaoyu Wang 已提交
1479

D
dapan 已提交
1480 1481 1482 1483 1484
  if (NULL == pCtg || NULL == users || NULL == num) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

  *num = taosHashGetSize(pCtg->userCache);
D
dapan1121 已提交
1485 1486 1487
  if (*num <= 0) {
    CTG_API_LEAVE(TSDB_CODE_SUCCESS);
  }
H
Hongze Cheng 已提交
1488

D
dapan1121 已提交
1489 1490 1491 1492
  *users = taosMemoryCalloc(*num, sizeof(SUserAuthVersion));
  if (NULL == *users) {
    ctgError("calloc %d userAuthVersion failed", *num);
    CTG_API_LEAVE(TSDB_CODE_OUT_OF_MEMORY);
D
dapan 已提交
1493 1494
  }

X
Xiaoyu Wang 已提交
1495 1496
  uint32_t      i = 0;
  SCtgUserAuth* pAuth = taosHashIterate(pCtg->userCache, NULL);
D
dapan 已提交
1497
  while (pAuth != NULL) {
D
fix bug  
dapan1121 已提交
1498
    size_t len = 0;
X
Xiaoyu Wang 已提交
1499
    void*  key = taosHashGetKey(pAuth, &len);
D
fix bug  
dapan1121 已提交
1500 1501
    strncpy((*users)[i].user, key, len);
    (*users)[i].user[len] = 0;
D
dapan1121 已提交
1502
    (*users)[i].version = pAuth->userAuth.version;
D
fix bug  
dapan1121 已提交
1503
    ++i;
D
dapan1121 已提交
1504 1505 1506 1507
    if (i >= *num) {
      taosHashCancelIterate(pCtg->userCache, pAuth);
      break;
    }
H
Hongze Cheng 已提交
1508

D
dapan 已提交
1509 1510 1511 1512 1513 1514
    pAuth = taosHashIterate(pCtg->userCache, pAuth);
  }

  CTG_API_LEAVE(TSDB_CODE_SUCCESS);
}

H
Hongze Cheng 已提交
1515
int32_t catalogGetDBCfg(SCatalog* pCtg, SRequestConnInfo* pConn, const char* dbFName, SDbCfgInfo* pDbCfg) {
D
dapan1121 已提交
1516
  CTG_API_ENTER();
H
Hongze Cheng 已提交
1517

D
dapan1121 已提交
1518
  if (NULL == pCtg || NULL == pConn || NULL == dbFName || NULL == pDbCfg) {
D
dapan1121 已提交
1519 1520 1521
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

D
dapan1121 已提交
1522
  CTG_API_LEAVE(ctgGetDBCfg(pCtg, pConn, dbFName, pDbCfg));
D
dapan1121 已提交
1523
}
D
dapan 已提交
1524

H
Hongze Cheng 已提交
1525
int32_t catalogGetIndexMeta(SCatalog* pCtg, SRequestConnInfo* pConn, const char* indexName, SIndexInfo* pInfo) {
D
dapan1121 已提交
1526
  CTG_API_ENTER();
H
Hongze Cheng 已提交
1527

D
dapan1121 已提交
1528
  if (NULL == pCtg || NULL == pConn || NULL == indexName || NULL == pInfo) {
D
dapan1121 已提交
1529 1530 1531
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

D
dapan1121 已提交
1532
  CTG_API_LEAVE(ctgGetIndexInfoFromMnode(pCtg, pConn, indexName, pInfo, NULL));
D
dapan1121 已提交
1533 1534
}

H
Hongze Cheng 已提交
1535
int32_t catalogGetTableIndex(SCatalog* pCtg, SRequestConnInfo* pConn, const SName* pTableName, SArray** pRes) {
D
dapan1121 已提交
1536
  CTG_API_ENTER();
H
Hongze Cheng 已提交
1537

D
dapan1121 已提交
1538
  if (NULL == pCtg || NULL == pConn || NULL == pTableName || NULL == pRes) {
D
dapan1121 已提交
1539 1540 1541
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

D
dapan1121 已提交
1542
  int32_t code = 0;
D
dapan1121 已提交
1543
  CTG_ERR_JRET(ctgGetTbIndex(pCtg, pConn, (SName*)pTableName, pRes));
H
Hongze Cheng 已提交
1544

D
dapan1121 已提交
1545 1546 1547
_return:

  CTG_API_LEAVE(code);
D
dapan1121 已提交
1548 1549
}

D
dapan1121 已提交
1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564
int32_t catalogGetTableTag(SCatalog* pCtg, SRequestConnInfo* pConn, const SName* pTableName, SArray** pRes) {
  CTG_API_ENTER();

  if (NULL == pCtg || NULL == pConn || NULL == pTableName || NULL == pRes) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

  int32_t code = 0;
  CTG_ERR_JRET(ctgGetTbTag(pCtg, pConn, (SName*)pTableName, pRes));

_return:

  CTG_API_LEAVE(code);
}

H
Hongze Cheng 已提交
1565
int32_t catalogRefreshGetTableCfg(SCatalog* pCtg, SRequestConnInfo* pConn, const SName* pTableName, STableCfg** pCfg) {
D
dapan1121 已提交
1566
  CTG_API_ENTER();
H
Hongze Cheng 已提交
1567

D
dapan1121 已提交
1568 1569 1570 1571
  if (NULL == pCtg || NULL == pConn || NULL == pTableName || NULL == pCfg) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

D
dapan1121 已提交
1572
  int32_t code = 0;
D
dapan1121 已提交
1573
  CTG_ERR_JRET(ctgRemoveTbMeta(pCtg, (SName*)pTableName));
D
dapan1121 已提交
1574 1575 1576 1577 1578 1579

  CTG_ERR_JRET(ctgGetTbCfg(pCtg, pConn, (SName*)pTableName, pCfg));

_return:

  CTG_API_LEAVE(code);
D
dapan1121 已提交
1580 1581
}

H
Hongze Cheng 已提交
1582
int32_t catalogGetUdfInfo(SCatalog* pCtg, SRequestConnInfo* pConn, const char* funcName, SFuncInfo* pInfo) {
D
dapan1121 已提交
1583
  CTG_API_ENTER();
H
Hongze Cheng 已提交
1584

D
dapan1121 已提交
1585
  if (NULL == pCtg || NULL == pConn || NULL == funcName || NULL == pInfo) {
D
dapan1121 已提交
1586 1587 1588
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

D
dapan1121 已提交
1589 1590
  CTG_CACHE_NHIT_INC(CTG_CI_UDF, 1);

D
dapan1121 已提交
1591
  int32_t code = 0;
D
dapan1121 已提交
1592
  CTG_ERR_JRET(ctgGetUdfInfoFromMnode(pCtg, pConn, funcName, pInfo, NULL));
H
Hongze Cheng 已提交
1593

D
dapan1121 已提交
1594
_return:
X
Xiaoyu Wang 已提交
1595

D
dapan1121 已提交
1596
  CTG_API_LEAVE(code);
D
dapan1121 已提交
1597 1598
}

D
dapan1121 已提交
1599
int32_t catalogChkAuth(SCatalog* pCtg, SRequestConnInfo* pConn, SUserAuthInfo *pAuth, SUserAuthRes* pRes) {
D
dapan 已提交
1600
  CTG_API_ENTER();
H
Hongze Cheng 已提交
1601

D
dapan1121 已提交
1602
  if (NULL == pCtg || NULL == pConn || NULL == pAuth || NULL == pRes) {
D
dapan 已提交
1603 1604 1605 1606
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

  int32_t code = 0;
D
dapan1121 已提交
1607
  CTG_ERR_JRET(ctgChkAuth(pCtg, pConn, pAuth, pRes, NULL));
H
Hongze Cheng 已提交
1608

D
dapan 已提交
1609 1610 1611 1612 1613
_return:

  CTG_API_LEAVE(code);
}

D
dapan1121 已提交
1614
int32_t catalogChkAuthFromCache(SCatalog* pCtg, SUserAuthInfo *pAuth,        SUserAuthRes* pRes, bool* exists) {
D
dapan1121 已提交
1615 1616
  CTG_API_ENTER();

D
dapan1121 已提交
1617
  if (NULL == pCtg || NULL == pAuth || NULL == pRes || NULL == exists) {
D
dapan1121 已提交
1618 1619 1620 1621
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

  int32_t code = 0;
D
dapan1121 已提交
1622
  CTG_ERR_JRET(ctgChkAuth(pCtg, NULL, pAuth, pRes, exists));
D
dapan1121 已提交
1623 1624 1625 1626 1627 1628 1629

_return:

  CTG_API_LEAVE(code);
}


H
Hongze Cheng 已提交
1630
int32_t catalogGetServerVersion(SCatalog* pCtg, SRequestConnInfo* pConn, char** pVersion) {
D
dapan1121 已提交
1631
  CTG_API_ENTER();
H
Hongze Cheng 已提交
1632

D
dapan1121 已提交
1633 1634 1635 1636
  if (NULL == pCtg || NULL == pConn || NULL == pVersion) {
    CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT);
  }

D
dapan1121 已提交
1637 1638
  CTG_CACHE_NHIT_INC(CTG_CI_SVR_VER, 1);

D
dapan1121 已提交
1639 1640
  int32_t code = 0;
  CTG_ERR_JRET(ctgGetSvrVerFromMnode(pCtg, pConn, pVersion, NULL));
H
Hongze Cheng 已提交
1641

D
dapan1121 已提交
1642 1643 1644 1645 1646
_return:

  CTG_API_LEAVE(code);
}

D
dapan 已提交
1647 1648 1649 1650 1651 1652 1653
int32_t catalogUpdateUserAuthInfo(SCatalog* pCtg, SGetUserAuthRsp* pAuth) {
  CTG_API_ENTER();

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

D
dapan1121 已提交
1654
  CTG_API_LEAVE(ctgUpdateUserEnqueue(pCtg, pAuth, false));
D
dapan 已提交
1655 1656
}

D
dapan1121 已提交
1657
int32_t catalogClearCache(void) {
D
dapan1121 已提交
1658
  CTG_API_ENTER_NOLOCK();
D
dapan1121 已提交
1659 1660 1661

  qInfo("start to clear catalog cache");

D
dapan1121 已提交
1662
  if (NULL == gCtgMgmt.pCluster) {
D
dapan1121 已提交
1663
    CTG_API_LEAVE_NOLOCK(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
1664 1665
  }

D
dapan1121 已提交
1666
  int32_t code = ctgClearCacheEnqueue(NULL, false, false, false, true);
D
dapan1121 已提交
1667 1668

  qInfo("clear catalog cache end, code: %s", tstrerror(code));
H
Hongze Cheng 已提交
1669

D
dapan1121 已提交
1670
  CTG_API_LEAVE_NOLOCK(code);
D
dapan1121 已提交
1671 1672
}

D
dapan 已提交
1673
void catalogDestroy(void) {
D
dapan1121 已提交
1674
  qInfo("start to destroy catalog");
X
Xiaoyu Wang 已提交
1675

wafwerar's avatar
wafwerar 已提交
1676
  if (NULL == gCtgMgmt.pCluster || atomic_load_8((int8_t*)&gCtgMgmt.exit)) {
D
dapan1121 已提交
1677 1678 1679
    return;
  }

D
dapan1121 已提交
1680 1681 1682 1683 1684 1685 1686
  if (gCtgMgmt.cacheTimer) {
    taosTmrStop(gCtgMgmt.cacheTimer);
    gCtgMgmt.cacheTimer = NULL;
    taosTmrCleanUp(gCtgMgmt.timer);
    gCtgMgmt.timer = NULL;
  }

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

D
dapan1121 已提交
1689
  if (!taosCheckCurrentInDll()) {
D
dapan1121 已提交
1690
    ctgClearCacheEnqueue(NULL, false, true, true, true);
D
dapan1121 已提交
1691
    taosThreadJoin(gCtgMgmt.updateThread, NULL);
D
dapan1121 已提交
1692
  }
1693

D
dapan 已提交
1694 1695
  taosHashCleanup(gCtgMgmt.pCluster);
  gCtgMgmt.pCluster = NULL;
D
dapan1121 已提交
1696 1697

  qInfo("catalog destroyed");
D
dapan 已提交
1698
}