catalogInt.h 39.1 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * 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/>.
 */

#ifndef _TD_CATALOG_INT_H_
#define _TD_CATALOG_INT_H_

#ifdef __cplusplus
extern "C" {
#endif

H
Haojun Liao 已提交
23
#include "catalog.h"
D
dapan1121 已提交
24
#include "query.h"
H
Hongze Cheng 已提交
25
#include "tcommon.h"
D
dapan1121 已提交
26 27
#include "ttimer.h"
#include "tglobal.h"
H
Haojun Liao 已提交
28

D
dapan1121 已提交
29
#define CTG_DEFAULT_CACHE_CLUSTER_NUMBER 6
H
Hongze Cheng 已提交
30 31
#define CTG_DEFAULT_CACHE_VGROUP_NUMBER  100
#define CTG_DEFAULT_CACHE_DB_NUMBER      20
D
dapan1121 已提交
32
#define CTG_DEFAULT_CACHE_TBLMETA_NUMBER 1000
H
Hongze Cheng 已提交
33 34 35 36 37
#define CTG_DEFAULT_RENT_SECOND          10
#define CTG_DEFAULT_RENT_SLOT_SIZE       10
#define CTG_DEFAULT_MAX_RETRY_TIMES      3
#define CTG_DEFAULT_BATCH_NUM            64
#define CTG_DEFAULT_FETCH_NUM            8
D
dapan1121 已提交
38
#define CTG_MAX_COMMAND_LEN              512
D
dapan1121 已提交
39 40
#define CTG_DEFAULT_CACHE_MON_MSEC       5000
#define CTG_CLEAR_CACHE_ROUND_TB_NUM     3000  
D
dapan1121 已提交
41

D
dapan1121 已提交
42
#define CTG_RENT_SLOT_SECOND 1.5
D
dapan1121 已提交
43 44

#define CTG_DEFAULT_INVALID_VERSION (-1)
D
dapan 已提交
45

D
dapan1121 已提交
46
#define CTG_ERR_CODE_TABLE_NOT_EXIST TSDB_CODE_PAR_TABLE_NOT_EXIST
D
dapan1121 已提交
47

D
dapan1121 已提交
48 49
#define CTG_BATCH_FETCH 1

D
dapan1121 已提交
50
typedef enum {
D
dapan1121 已提交
51
  CTG_CI_CLUSTER = 0,
D
dapan1121 已提交
52 53 54 55 56 57 58 59 60 61 62
  CTG_CI_DNODE,
  CTG_CI_QNODE,
  CTG_CI_DB,
  CTG_CI_DB_VGROUP,
  CTG_CI_DB_CFG,
  CTG_CI_DB_INFO,
  CTG_CI_STABLE_META,
  CTG_CI_NTABLE_META,
  CTG_CI_CTABLE_META,
  CTG_CI_SYSTABLE_META,
  CTG_CI_OTHERTABLE_META,
D
dapan1121 已提交
63
  CTG_CI_TBL_SMA,
D
dapan1121 已提交
64
  CTG_CI_TBL_CFG,
65
  CTG_CI_TBL_TAG,
D
dapan1121 已提交
66
  CTG_CI_INDEX_INFO,
D
dapan1121 已提交
67 68 69 70 71 72
  CTG_CI_USER,
  CTG_CI_UDF,
  CTG_CI_SVR_VER,
  CTG_CI_MAX_VALUE,
} CTG_CACHE_ITEM;

D
dapan1121 已提交
73
#define CTG_CI_FLAG_LEVEL_GLOBAL  (1)
X
Xiaoyu Wang 已提交
74 75
#define CTG_CI_FLAG_LEVEL_CLUSTER (1 << 1)
#define CTG_CI_FLAG_LEVEL_DB      (1 << 2)
D
dapan1121 已提交
76

D
dapan1121 已提交
77 78 79 80 81
enum {
  CTG_READ = 1,
  CTG_WRITE,
};

D
dapan1121 已提交
82 83 84 85 86
enum {
  CTG_RENT_DB = 1,
  CTG_RENT_STABLE,
};

D
dapan1121 已提交
87
enum {
D
dapan1121 已提交
88
  CTG_OP_UPDATE_VGROUP = 0,
D
dapan1121 已提交
89
  CTG_OP_UPDATE_DB_CFG,
D
dapan1121 已提交
90 91
  CTG_OP_UPDATE_TB_META,
  CTG_OP_DROP_DB_CACHE,
D
dapan1121 已提交
92
  CTG_OP_DROP_DB_VGROUP,
D
dapan1121 已提交
93 94 95 96
  CTG_OP_DROP_STB_META,
  CTG_OP_DROP_TB_META,
  CTG_OP_UPDATE_USER,
  CTG_OP_UPDATE_VG_EPSET,
D
dapan1121 已提交
97 98
  CTG_OP_UPDATE_TB_INDEX,
  CTG_OP_DROP_TB_INDEX,
D
dapan1121 已提交
99
  CTG_OP_CLEAR_CACHE,
D
dapan1121 已提交
100
  CTG_OP_MAX
D
dapan1121 已提交
101 102
};

D
dapan1121 已提交
103 104
typedef enum {
  CTG_TASK_GET_QNODE = 0,
D
dapan1121 已提交
105
  CTG_TASK_GET_DNODE,
D
dapan1121 已提交
106 107
  CTG_TASK_GET_DB_VGROUP,
  CTG_TASK_GET_DB_CFG,
D
dapan1121 已提交
108
  CTG_TASK_GET_DB_INFO,
D
dapan1121 已提交
109 110
  CTG_TASK_GET_TB_META,
  CTG_TASK_GET_TB_HASH,
D
dapan1121 已提交
111
  CTG_TASK_GET_TB_SMA_INDEX,
D
dapan1121 已提交
112
  CTG_TASK_GET_TB_CFG,
D
dapan1121 已提交
113
  CTG_TASK_GET_INDEX_INFO,
D
dapan1121 已提交
114 115
  CTG_TASK_GET_UDF,
  CTG_TASK_GET_USER,
D
dapan1121 已提交
116
  CTG_TASK_GET_SVR_VER,
D
dapan1121 已提交
117
  CTG_TASK_GET_TB_META_BATCH,
118
  CTG_TASK_GET_TB_HASH_BATCH,
119
  CTG_TASK_GET_TB_TAG,
D
dapan1121 已提交
120 121
} CTG_TASK_TYPE;

D
dapan1121 已提交
122 123 124 125 126
typedef enum {
  CTG_TASK_LAUNCHED = 1,
  CTG_TASK_DONE,
} CTG_TASK_STATUS;

D
dapan1121 已提交
127
typedef struct SCtgDebug {
D
dapan1121 已提交
128 129 130 131
  bool     lockEnable;
  bool     cacheEnable;
  bool     apiEnable;
  bool     metaEnable;
D
dapan1121 已提交
132
  bool     statEnable;
D
dapan1121 已提交
133
  uint32_t showCachePeriodSec;
D
dapan1121 已提交
134
} SCtgDebug;
135

D
dapan1121 已提交
136 137
typedef struct SCtgCacheStat {
  uint64_t cacheNum[CTG_CI_MAX_VALUE];
D
dapan1121 已提交
138
  uint64_t cacheSize[CTG_CI_MAX_VALUE];
D
dapan1121 已提交
139 140 141 142
  uint64_t cacheHit[CTG_CI_MAX_VALUE];
  uint64_t cacheNHit[CTG_CI_MAX_VALUE];
} SCtgCacheStat;

D
dapan1121 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155
typedef struct SCtgAuthReq {
  SRequestConnInfo* pConn;
  SUserAuthInfo*    pRawReq;
  SGetUserAuthRsp   authInfo;
  AUTH_TYPE         singleType;
  bool              onlyCache;
} SCtgAuthReq;

typedef struct SCtgAuthRsp {
  SUserAuthRes* pRawRes;
  bool          metaNotExists;
} SCtgAuthRsp;

D
dapan1121 已提交
156 157 158 159 160 161 162
typedef struct SCtgTbCacheInfo {
  bool     inCache;
  uint64_t dbId;
  uint64_t suid;
  int32_t  tbType;
} SCtgTbCacheInfo;

163 164 165 166 167
typedef struct SCtgTbMetaParam {
  SName*  pName;
  int32_t flag;
} SCtgTbMetaParam;

D
dapan1121 已提交
168 169
typedef struct SCtgTbMetaCtx {
  SCtgTbCacheInfo tbInfo;
H
Hongze Cheng 已提交
170 171 172
  int32_t         vgId;
  SName*          pName;
  int32_t         flag;
D
dapan1121 已提交
173 174
} SCtgTbMetaCtx;

D
dapan1121 已提交
175
typedef struct SCtgFetch {
H
Hongze Cheng 已提交
176 177 178 179 180
  int32_t         dbIdx;
  int32_t         tbIdx;
  int32_t         fetchIdx;
  int32_t         resIdx;
  int32_t         flag;
D
dapan1121 已提交
181
  SCtgTbCacheInfo tbInfo;
H
Hongze Cheng 已提交
182
  int32_t         vgId;
D
dapan1121 已提交
183 184
} SCtgFetch;

185
typedef struct SCtgTbMetasCtx {
D
dapan1121 已提交
186 187
  int32_t fetchNum;
  SArray* pNames;
188
  SArray* pResList;
D
dapan1121 已提交
189
  SArray* pFetchs;
190
} SCtgTbMetasCtx;
D
dapan1121 已提交
191

D
dapan1121 已提交
192 193 194 195
typedef struct SCtgTbIndexCtx {
  SName* pName;
} SCtgTbIndexCtx;

D
dapan1121 已提交
196 197 198 199 200 201
typedef struct SCtgTbCfgCtx {
  SName*       pName;
  int32_t      tbType;
  SVgroupInfo* pVgInfo;
} SCtgTbCfgCtx;

202 203 204 205 206
typedef struct SCtgTbTagCtx {
  SName*       pName;
  SVgroupInfo* pVgInfo;
} SCtgTbTagCtx;

D
dapan1121 已提交
207 208 209 210 211 212 213 214
typedef struct SCtgDbVgCtx {
  char dbFName[TSDB_DB_FNAME_LEN];
} SCtgDbVgCtx;

typedef struct SCtgDbCfgCtx {
  char dbFName[TSDB_DB_FNAME_LEN];
} SCtgDbCfgCtx;

D
dapan1121 已提交
215 216 217 218
typedef struct SCtgDbInfoCtx {
  char dbFName[TSDB_DB_FNAME_LEN];
} SCtgDbInfoCtx;

D
dapan1121 已提交
219
typedef struct SCtgTbHashCtx {
H
Hongze Cheng 已提交
220
  char   dbFName[TSDB_DB_FNAME_LEN];
D
dapan1121 已提交
221 222 223
  SName* pName;
} SCtgTbHashCtx;

224
typedef struct SCtgTbHashsCtx {
225 226 227 228
  int32_t fetchNum;
  SArray* pNames;
  SArray* pResList;
  SArray* pFetchs;
229
} SCtgTbHashsCtx;
230

D
dapan1121 已提交
231 232 233 234 235 236 237 238 239 240 241
typedef struct SCtgIndexCtx {
  char indexFName[TSDB_INDEX_FNAME_LEN];
} SCtgIndexCtx;

typedef struct SCtgUdfCtx {
  char udfName[TSDB_FUNC_NAME_LEN];
} SCtgUdfCtx;

typedef struct SCtgUserCtx {
  SUserAuthInfo user;
} SCtgUserCtx;
242

D
dapan1121 已提交
243
typedef STableIndexRsp STableIndex;
D
dapan1121 已提交
244

D
dapan1121 已提交
245 246 247
typedef struct SCtgTbCache {
  SRWLatch     metaLock;
  SRWLatch     indexLock;
D
dapan1121 已提交
248
  STableMeta*  pMeta;
H
Hongze Cheng 已提交
249
  STableIndex* pIndex;
D
dapan1121 已提交
250 251 252
} SCtgTbCache;

typedef struct SCtgVgCache {
H
Hongze Cheng 已提交
253 254
  SRWLatch   vgLock;
  SDBVgInfo* vgInfo;
D
dapan1121 已提交
255 256
} SCtgVgCache;

D
dapan1121 已提交
257 258 259 260 261
typedef struct SCtgCfgCache {
  SRWLatch    cfgLock;
  SDbCfgInfo* cfgInfo;
} SCtgCfgCache;

D
dapan1121 已提交
262
typedef struct SCtgDBCache {
D
dapan1121 已提交
263 264 265 266 267 268 269
  SRWLatch     dbLock;  // RC between destroy tbCache/stbCache and all reads
  uint64_t     dbId;
  int8_t       deleted;
  SCtgVgCache  vgCache;
  SCtgCfgCache cfgCache;
  SHashObj*    tbCache;   // key:tbname, value:SCtgTbCache
  SHashObj*    stbCache;  // key:suid, value:char*
270
  uint64_t     dbCacheNum[CTG_CI_MAX_VALUE];
271
  uint64_t     dbCacheSize;
D
dapan1121 已提交
272
} SCtgDBCache;
D
dapan1121 已提交
273

D
dapan1121 已提交
274
typedef struct SCtgRentSlot {
D
dapan1121 已提交
275 276
  SRWLatch lock;
  bool     needSort;
D
dapan1121 已提交
277
  SArray*  meta;  // element is SDbCacheInfo or SSTableVersion
D
dapan1121 已提交
278
} SCtgRentSlot;
D
dapan1121 已提交
279

D
dapan1121 已提交
280
typedef struct SCtgRentMgmt {
H
Hongze Cheng 已提交
281 282 283 284
  int8_t        type;
  uint16_t      slotNum;
  uint16_t      slotRIdx;
  int64_t       lastReadMsec;
D
dapan1121 已提交
285 286
  uint64_t      rentCacheSize;
  int32_t       metaSize;
H
Hongze Cheng 已提交
287
  SCtgRentSlot* slots;
D
dapan1121 已提交
288
} SCtgRentMgmt;
D
dapan1121 已提交
289

D
dapan 已提交
290
typedef struct SCtgUserAuth {
D
dapan1121 已提交
291 292
  SRWLatch        lock;
  SGetUserAuthRsp userAuth;
D
dapan1121 已提交
293
  uint64_t        userCacheSize;
D
dapan 已提交
294 295
} SCtgUserAuth;

D
dapan1121 已提交
296
typedef struct SCatalog {
D
dapan1121 已提交
297 298 299 300 301 302 303
  uint64_t      clusterId;
  bool          stopUpdate;
  SHashObj*     userCache;  // key:user, value:SCtgUserAuth
  SHashObj*     dbCache;    // key:dbname, value:SCtgDBCache
  SCtgRentMgmt  dbRent;
  SCtgRentMgmt  stbRent;
  SCtgCacheStat cacheStat;
H
Haojun Liao 已提交
304 305
} SCatalog;

D
dapan1121 已提交
306
typedef struct SCtgBatch {
H
Hongze Cheng 已提交
307 308 309
  int32_t          batchId;
  int32_t          msgType;
  SArray*          pMsgs;
310
  SRequestConnInfo conn;
H
Hongze Cheng 已提交
311 312 313
  char             dbFName[TSDB_DB_FNAME_LEN];
  SArray*          pTaskIds;
  SArray*          pMsgIdxs;
D
dapan1121 已提交
314 315
} SCtgBatch;

D
dapan1121 已提交
316
typedef struct SCtgJob {
H
Hongze Cheng 已提交
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
  int64_t   refId;
  int32_t   batchId;
  SHashObj* pBatchs;
  SArray*   pTasks;
  int32_t   subTaskNum;
  int32_t   taskDone;
  SMetaData jobRes;
  int32_t   jobResCode;
  int32_t   taskIdx;
  SRWLatch  taskLock;

  uint64_t         queryId;
  SCatalog*        pCtg;
  SRequestConnInfo conn;
  void*            userParam;
  catalogCallback  userFp;
  int32_t          tbMetaNum;
  int32_t          tbHashNum;
335
  int32_t          tbTagNum;
H
Hongze Cheng 已提交
336 337 338 339 340 341 342 343 344 345 346
  int32_t          dbVgNum;
  int32_t          udfNum;
  int32_t          qnodeNum;
  int32_t          dnodeNum;
  int32_t          dbCfgNum;
  int32_t          indexNum;
  int32_t          userNum;
  int32_t          dbInfoNum;
  int32_t          tbIndexNum;
  int32_t          tbCfgNum;
  int32_t          svrVerNum;
D
dapan1121 已提交
347 348 349
} SCtgJob;

typedef struct SCtgMsgCtx {
H
Hongze Cheng 已提交
350 351 352 353
  int32_t   reqType;
  void*     lastOut;
  void*     out;
  char*     target;
354
  SHashObj* pBatchs;
D
dapan1121 已提交
355 356
} SCtgMsgCtx;

357
typedef struct SCtgTaskCallbackParam {
H
Hongze Cheng 已提交
358 359 360 361 362 363
  uint64_t queryId;
  int64_t  refId;
  SArray*  taskId;
  int32_t  reqType;
  int32_t  batchId;
  SArray*  msgIdx;
364 365
} SCtgTaskCallbackParam;

D
dapan1121 已提交
366 367 368 369 370 371 372 373 374 375
typedef struct SCtgTask SCtgTask;
typedef int32_t (*ctgSubTaskCbFp)(SCtgTask*);

typedef struct SCtgSubRes {
  CTG_TASK_TYPE  type;
  int32_t        code;
  void*          res;
  ctgSubTaskCbFp fp;
} SCtgSubRes;

H
Haojun Liao 已提交
376
struct SCtgTask {
D
dapan1121 已提交
377
  CTG_TASK_TYPE   type;
D
dapan1121 已提交
378
  bool            subTask;
D
dapan1121 已提交
379 380 381
  int32_t         taskId;
  SCtgJob*        pJob;
  void*           taskCtx;
D
dapan1121 已提交
382
  SArray*         msgCtxs;
D
dapan1121 已提交
383 384 385 386 387 388 389
  SCtgMsgCtx      msgCtx;
  int32_t         code;
  void*           res;
  CTG_TASK_STATUS status;
  SRWLatch        lock;
  SArray*         pParents;
  SCtgSubRes      subRes;
H
Haojun Liao 已提交
390
};
D
dapan1121 已提交
391

D
dapan1121 已提交
392 393 394 395 396
typedef struct SCtgTaskReq {
  SCtgTask* pTask;
  int32_t   msgIdx;
} SCtgTaskReq;

D
dapan1121 已提交
397
typedef int32_t (*ctgInitTaskFp)(SCtgJob*, int32_t, void*);
D
dapan1121 已提交
398
typedef int32_t (*ctgLanchTaskFp)(SCtgTask*);
H
Hongze Cheng 已提交
399
typedef int32_t (*ctgHandleTaskMsgRspFp)(SCtgTaskReq*, int32_t, const SDataBuf*, int32_t);
D
dapan1121 已提交
400
typedef int32_t (*ctgDumpTaskResFp)(SCtgTask*);
D
dapan1121 已提交
401 402
typedef int32_t (*ctgCloneTaskResFp)(SCtgTask*, void**);
typedef int32_t (*ctgCompTaskFp)(SCtgTask*, void*, bool*);
D
dapan1121 已提交
403 404

typedef struct SCtgAsyncFps {
D
dapan1121 已提交
405 406
  ctgInitTaskFp         initFp;
  ctgLanchTaskFp        launchFp;
D
dapan1121 已提交
407
  ctgHandleTaskMsgRspFp handleRspFp;
D
dapan1121 已提交
408 409 410
  ctgDumpTaskResFp      dumpResFp;
  ctgCompTaskFp         compFp;
  ctgCloneTaskResFp     cloneFp;
D
dapan1121 已提交
411 412
} SCtgAsyncFps;

D
dapan1121 已提交
413
typedef struct SCtgApiStat {
wafwerar's avatar
wafwerar 已提交
414
#if defined(WINDOWS) || defined(_TD_DARWIN_64)
wafwerar's avatar
wafwerar 已提交
415 416 417
  size_t avoidCompilationErrors;
#endif

D
dapan1121 已提交
418 419
} SCtgApiStat;

D
dapan1121 已提交
420
typedef struct SCtgRuntimeStat {
D
dapan1121 已提交
421 422 423
  uint64_t numOfOpAbort;
  uint64_t numOfOpEnqueue;
  uint64_t numOfOpDequeue;
D
dapan1121 已提交
424
  uint64_t numOfOpClearMeta;
D
dapan1121 已提交
425
  uint64_t numOfOpClearCache;
D
dapan1121 已提交
426
} SCtgRuntimeStat;
D
dapan1121 已提交
427 428

typedef struct SCatalogStat {
H
Hongze Cheng 已提交
429 430 431
  SCtgApiStat     api;
  SCtgRuntimeStat runtime;
  SCtgCacheStat   cache;
D
dapan1121 已提交
432 433
} SCatalogStat;

D
dapan1121 已提交
434 435 436 437
typedef struct SCtgUpdateMsgHeader {
  SCatalog* pCtg;
} SCtgUpdateMsgHeader;

D
dapan1121 已提交
438
typedef struct SCtgUpdateVgMsg {
H
Hongze Cheng 已提交
439 440 441
  SCatalog*  pCtg;
  char       dbFName[TSDB_DB_FNAME_LEN];
  uint64_t   dbId;
D
dapan1121 已提交
442 443 444
  SDBVgInfo* dbInfo;
} SCtgUpdateVgMsg;

D
dapan1121 已提交
445 446 447 448 449 450 451
typedef struct SCtgUpdateDbCfgMsg {
  SCatalog*   pCtg;
  char        dbFName[TSDB_DB_FNAME_LEN];
  uint64_t    dbId;
  SDbCfgInfo* cfgInfo;
} SCtgUpdateDbCfgMsg;

D
dapan1121 已提交
452 453 454 455
typedef struct SCtgUpdateTbMetaMsg {
  SCatalog*         pCtg;
  STableMetaOutput* pMeta;
} SCtgUpdateTbMetaMsg;
D
dapan1121 已提交
456

D
dapan1121 已提交
457
typedef struct SCtgDropDBMsg {
D
dapan1121 已提交
458
  SCatalog* pCtg;
H
Hongze Cheng 已提交
459 460
  char      dbFName[TSDB_DB_FNAME_LEN];
  uint64_t  dbId;
D
dapan1121 已提交
461
} SCtgDropDBMsg;
D
dapan1121 已提交
462

D
dapan1121 已提交
463 464
typedef struct SCtgDropDbVgroupMsg {
  SCatalog* pCtg;
H
Hongze Cheng 已提交
465
  char      dbFName[TSDB_DB_FNAME_LEN];
D
dapan1121 已提交
466 467 468
} SCtgDropDbVgroupMsg;

typedef struct SCtgDropStbMetaMsg {
D
dapan1121 已提交
469
  SCatalog* pCtg;
H
Hongze Cheng 已提交
470 471 472 473
  char      dbFName[TSDB_DB_FNAME_LEN];
  char      stbName[TSDB_TABLE_NAME_LEN];
  uint64_t  dbId;
  uint64_t  suid;
D
dapan1121 已提交
474
} SCtgDropStbMetaMsg;
D
dapan1121 已提交
475

D
dapan1121 已提交
476
typedef struct SCtgDropTblMetaMsg {
D
dapan1121 已提交
477
  SCatalog* pCtg;
H
Hongze Cheng 已提交
478 479 480
  char      dbFName[TSDB_DB_FNAME_LEN];
  char      tbName[TSDB_TABLE_NAME_LEN];
  uint64_t  dbId;
D
dapan1121 已提交
481
} SCtgDropTblMetaMsg;
D
dapan1121 已提交
482

D
dapan 已提交
483
typedef struct SCtgUpdateUserMsg {
H
Hongze Cheng 已提交
484
  SCatalog*       pCtg;
D
dapan 已提交
485
  SGetUserAuthRsp userAuth;
D
dapan 已提交
486
} SCtgUpdateUserMsg;
D
dapan 已提交
487

D
dapan1121 已提交
488 489 490 491 492 493
typedef struct SCtgUpdateTbIndexMsg {
  SCatalog*    pCtg;
  STableIndex* pIndex;
} SCtgUpdateTbIndexMsg;

typedef struct SCtgDropTbIndexMsg {
H
Hongze Cheng 已提交
494 495 496
  SCatalog* pCtg;
  char      dbFName[TSDB_DB_FNAME_LEN];
  char      tbName[TSDB_TABLE_NAME_LEN];
D
dapan1121 已提交
497 498
} SCtgDropTbIndexMsg;

D
dapan1121 已提交
499
typedef struct SCtgClearCacheMsg {
H
Hongze Cheng 已提交
500
  SCatalog* pCtg;
D
dapan1121 已提交
501
  bool      clearMeta;
H
Hongze Cheng 已提交
502
  bool      freeCtg;
D
dapan1121 已提交
503 504
} SCtgClearCacheMsg;

D
dapan1121 已提交
505 506
typedef struct SCtgUpdateEpsetMsg {
  SCatalog* pCtg;
H
Hongze Cheng 已提交
507 508 509
  char      dbFName[TSDB_DB_FNAME_LEN];
  int32_t   vgId;
  SEpSet    epSet;
D
dapan1121 已提交
510
} SCtgUpdateEpsetMsg;
D
dapan1121 已提交
511

D
dapan1121 已提交
512
typedef struct SCtgCacheOperation {
H
Hongze Cheng 已提交
513 514 515 516 517 518
  int32_t opId;
  void*   data;
  bool    syncOp;
  tsem_t  rspSem;
  bool    stopQueue;
  bool    unLocked;
D
dapan1121 已提交
519
} SCtgCacheOperation;
D
dapan1121 已提交
520 521

typedef struct SCtgQNode {
H
Hongze Cheng 已提交
522 523
  SCtgCacheOperation* op;
  struct SCtgQNode*   next;
D
dapan1121 已提交
524 525
} SCtgQNode;

D
dapan1121 已提交
526
typedef struct SCtgQueue {
H
Hongze Cheng 已提交
527 528 529 530 531 532
  SRWLatch   qlock;
  bool       stopQueue;
  SCtgQNode* head;
  SCtgQNode* tail;
  tsem_t     reqSem;
  uint64_t   qRemainNum;
D
dapan1121 已提交
533 534 535
} SCtgQueue;

typedef struct SCatalogMgmt {
H
Hongze Cheng 已提交
536 537 538 539
  bool         exit;
  int32_t      jobPool;
  SRWLatch     lock;
  SCtgQueue    queue;
D
dapan1121 已提交
540 541
  void        *timer;
  tmr_h        cacheTimer;
H
Hongze Cheng 已提交
542 543
  TdThread     updateThread;
  SHashObj*    pCluster;  // key: clusterId, value: SCatalog*
D
dapan1121 已提交
544
  SCatalogStat statInfo;
H
Hongze Cheng 已提交
545
  SCatalogCfg  cfg;
D
dapan1121 已提交
546 547
} SCatalogMgmt;

H
Hongze Cheng 已提交
548 549
typedef uint32_t (*tableNameHashFp)(const char*, uint32_t);
typedef int32_t (*ctgOpFunc)(SCtgCacheOperation*);
D
dapan 已提交
550

D
dapan1121 已提交
551
typedef struct SCtgOperation {
H
Hongze Cheng 已提交
552 553
  int32_t   opId;
  char      name[32];
D
dapan1121 已提交
554 555
  ctgOpFunc func;
} SCtgOperation;
D
dapan 已提交
556

D
dapan1121 已提交
557
typedef struct SCtgCacheItemInfo {
D
dapan1121 已提交
558 559
  char*            name;
  int32_t          flag;
D
dapan1121 已提交
560 561
} SCtgCacheItemInfo;

X
Xiaoyu Wang 已提交
562
#define CTG_AUTH_READ(_t)  ((_t) == AUTH_TYPE_READ || (_t) == AUTH_TYPE_READ_OR_WRITE)
D
dapan1121 已提交
563 564
#define CTG_AUTH_WRITE(_t) ((_t) == AUTH_TYPE_WRITE || (_t) == AUTH_TYPE_READ_OR_WRITE)

D
dapan1121 已提交
565 566
#define CTG_QUEUE_INC() atomic_add_fetch_64(&gCtgMgmt.queue.qRemainNum, 1)
#define CTG_QUEUE_DEC() atomic_sub_fetch_64(&gCtgMgmt.queue.qRemainNum, 1)
D
dapan 已提交
567

D
dapan1121 已提交
568 569
#define CTG_STAT_INC(_item, _n) atomic_add_fetch_64(&(_item), _n)
#define CTG_STAT_DEC(_item, _n) atomic_sub_fetch_64(&(_item), _n)
H
Hongze Cheng 已提交
570
#define CTG_STAT_GET(_item)     atomic_load_64(&(_item))
D
dapan1121 已提交
571

X
Xiaoyu Wang 已提交
572 573 574 575 576 577
#define CTG_STAT_API_INC(item, n)  (CTG_STAT_INC(gCtgMgmt.statInfo.api.item, n))
#define CTG_STAT_RT_INC(item, n)   (CTG_STAT_INC(gCtgMgmt.statInfo.runtime.item, n))
#define CTG_STAT_NUM_INC(item, n)  (CTG_STAT_INC(gCtgMgmt.statInfo.cache.cacheNum[item], n))
#define CTG_STAT_NUM_DEC(item, n)  (CTG_STAT_DEC(gCtgMgmt.statInfo.cache.cacheNum[item], n))
#define CTG_STAT_HIT_INC(item, n)  (CTG_STAT_INC(gCtgMgmt.statInfo.cache.cacheHit[item], n))
#define CTG_STAT_HIT_DEC(item, n)  (CTG_STAT_DEC(gCtgMgmt.statInfo.cache.cacheHit[item], n))
D
dapan1121 已提交
578 579 580
#define CTG_STAT_NHIT_INC(item, n) (CTG_STAT_INC(gCtgMgmt.statInfo.cache.cacheNHit[item], n))
#define CTG_STAT_NHIT_DEC(item, n) (CTG_STAT_DEC(gCtgMgmt.statInfo.cache.cacheNHit[item], n))

X
Xiaoyu Wang 已提交
581 582 583
#define CTG_CACHE_NUM_INC(item, n)  (CTG_STAT_INC(pCtg->cacheStat.cacheNum[item], n))
#define CTG_CACHE_NUM_DEC(item, n)  (CTG_STAT_DEC(pCtg->cacheStat.cacheNum[item], n))
#define CTG_CACHE_HIT_INC(item, n)  (CTG_STAT_INC(pCtg->cacheStat.cacheHit[item], n))
D
dapan1121 已提交
584
#define CTG_CACHE_NHIT_INC(item, n) (CTG_STAT_INC(pCtg->cacheStat.cacheNHit[item], n))
D
dapan1121 已提交
585

D
dapan1121 已提交
586 587 588 589 590
#define CTG_DB_NUM_INC(_item)   dbCache->dbCacheNum[_item] += 1
#define CTG_DB_NUM_DEC(_item)   dbCache->dbCacheNum[_item] -= 1
#define CTG_DB_NUM_SET(_item)   dbCache->dbCacheNum[_item] = 1
#define CTG_DB_NUM_RESET(_item) dbCache->dbCacheNum[_item] = 0

X
Xiaoyu Wang 已提交
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
#define CTG_META_NUM_INC(type)                  \
  do {                                          \
    switch (type) {                             \
      case TSDB_SUPER_TABLE:                    \
        CTG_DB_NUM_INC(CTG_CI_STABLE_META);     \
        break;                                  \
      case TSDB_CHILD_TABLE:                    \
        CTG_DB_NUM_INC(CTG_CI_CTABLE_META);     \
        break;                                  \
      case TSDB_NORMAL_TABLE:                   \
        CTG_DB_NUM_INC(CTG_CI_NTABLE_META);     \
        break;                                  \
      case TSDB_SYSTEM_TABLE:                   \
        CTG_DB_NUM_INC(CTG_CI_SYSTABLE_META);   \
        break;                                  \
      default:                                  \
        CTG_DB_NUM_INC(CTG_CI_OTHERTABLE_META); \
        break;                                  \
    }                                           \
  } while (0)
D
dapan1121 已提交
611

X
Xiaoyu Wang 已提交
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
#define CTG_META_NUM_DEC(type)                  \
  do {                                          \
    switch (type) {                             \
      case TSDB_SUPER_TABLE:                    \
        CTG_DB_NUM_DEC(CTG_CI_STABLE_META);     \
        break;                                  \
      case TSDB_CHILD_TABLE:                    \
        CTG_DB_NUM_DEC(CTG_CI_CTABLE_META);     \
        break;                                  \
      case TSDB_NORMAL_TABLE:                   \
        CTG_DB_NUM_DEC(CTG_CI_NTABLE_META);     \
        break;                                  \
      case TSDB_SYSTEM_TABLE:                   \
        CTG_DB_NUM_DEC(CTG_CI_SYSTABLE_META);   \
        break;                                  \
      default:                                  \
        CTG_DB_NUM_DEC(CTG_CI_OTHERTABLE_META); \
        break;                                  \
    }                                           \
  } while (0)
D
dapan1121 已提交
632

X
Xiaoyu Wang 已提交
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
#define CTG_META_HIT_INC(type)                        \
  do {                                                \
    switch (type) {                                   \
      case TSDB_SUPER_TABLE:                          \
        CTG_CACHE_HIT_INC(CTG_CI_STABLE_META, 1);     \
        break;                                        \
      case TSDB_CHILD_TABLE:                          \
        CTG_CACHE_HIT_INC(CTG_CI_CTABLE_META, 1);     \
        break;                                        \
      case TSDB_NORMAL_TABLE:                         \
        CTG_CACHE_HIT_INC(CTG_CI_NTABLE_META, 1);     \
        break;                                        \
      case TSDB_SYSTEM_TABLE:                         \
        CTG_CACHE_HIT_INC(CTG_CI_SYSTABLE_META, 1);   \
        break;                                        \
      default:                                        \
        CTG_CACHE_HIT_INC(CTG_CI_OTHERTABLE_META, 1); \
        break;                                        \
    }                                                 \
  } while (0)

#define CTG_META_NHIT_INC() CTG_CACHE_NHIT_INC(CTG_CI_OTHERTABLE_META, 1)
D
dapan1121 已提交
655

H
Hongze Cheng 已提交
656
#define CTG_IS_META_NULL(type)   ((type) == META_TYPE_NULL_TABLE)
D
dapan1121 已提交
657
#define CTG_IS_META_CTABLE(type) ((type) == META_TYPE_CTABLE)
H
Hongze Cheng 已提交
658 659
#define CTG_IS_META_TABLE(type)  ((type) == META_TYPE_TABLE)
#define CTG_IS_META_BOTH(type)   ((type) == META_TYPE_BOTH_TABLE)
D
dapan1121 已提交
660

D
dapan1121 已提交
661 662 663
#define CTG_FLAG_STB          0x1
#define CTG_FLAG_NOT_STB      0x2
#define CTG_FLAG_UNKNOWN_STB  0x4
D
dapan1121 已提交
664
#define CTG_FLAG_SYS_DB       0x8
D
dapan1121 已提交
665
#define CTG_FLAG_FORCE_UPDATE 0x10
666
#define CTG_FLAG_ONLY_CACHE   0x20
667
#define CTG_FLAG_SYNC_OP      0x40
D
dapan1121 已提交
668

D
dapan 已提交
669 670
#define CTG_FLAG_SET(_flag, _v) ((_flag) |= (_v))

H
Hongze Cheng 已提交
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
#define CTG_FLAG_IS_STB(_flag)          ((_flag)&CTG_FLAG_STB)
#define CTG_FLAG_IS_NOT_STB(_flag)      ((_flag)&CTG_FLAG_NOT_STB)
#define CTG_FLAG_IS_UNKNOWN_STB(_flag)  ((_flag)&CTG_FLAG_UNKNOWN_STB)
#define CTG_FLAG_IS_SYS_DB(_flag)       ((_flag)&CTG_FLAG_SYS_DB)
#define CTG_FLAG_IS_FORCE_UPDATE(_flag) ((_flag)&CTG_FLAG_FORCE_UPDATE)
#define CTG_FLAG_SET_SYS_DB(_flag)      ((_flag) |= CTG_FLAG_SYS_DB)
#define CTG_FLAG_SET_STB(_flag, tbType)                                                       \
  do {                                                                                        \
    (_flag) |= ((tbType) == TSDB_SUPER_TABLE)                                                 \
                   ? CTG_FLAG_STB                                                             \
                   : ((tbType) > TSDB_SUPER_TABLE ? CTG_FLAG_NOT_STB : CTG_FLAG_UNKNOWN_STB); \
  } while (0)
#define CTG_FLAG_MAKE_STB(_isStb) \
  (((_isStb) == 1) ? CTG_FLAG_STB : ((_isStb) == 0 ? CTG_FLAG_NOT_STB : CTG_FLAG_UNKNOWN_STB))
#define CTG_FLAG_MATCH_STB(_flag, tbType)                                                        \
  (CTG_FLAG_IS_UNKNOWN_STB(_flag) || (CTG_FLAG_IS_STB(_flag) && (tbType) == TSDB_SUPER_TABLE) || \
   (CTG_FLAG_IS_NOT_STB(_flag) && (tbType) != TSDB_SUPER_TABLE))
D
dapan1121 已提交
688

H
Hongze Cheng 已提交
689 690 691 692
#define CTG_GET_TASK_MSGCTX(_task, _id)                                                             \
  (((CTG_TASK_GET_TB_META_BATCH == (_task)->type) || (CTG_TASK_GET_TB_HASH_BATCH == (_task)->type)) \
       ? taosArrayGet((_task)->msgCtxs, (_id))                                                      \
       : &(_task)->msgCtx)
D
dapan1121 已提交
693

H
Hongze Cheng 已提交
694 695
#define CTG_META_SIZE(pMeta) \
  (sizeof(STableMeta) + ((pMeta)->tableInfo.numOfTags + (pMeta)->tableInfo.numOfColumns) * sizeof(SSchema))
D
dapan1121 已提交
696

697
#define CTG_TABLE_NOT_EXIST(code) (code == CTG_ERR_CODE_TABLE_NOT_EXIST)
698 699
#define CTG_DB_NOT_EXIST(code) \
  (code == TSDB_CODE_MND_DB_NOT_EXIST || code == TSDB_CODE_MND_DB_IN_CREATING || code == TSDB_CODE_MND_DB_IN_DROPPING)
H
Hongze Cheng 已提交
700

D
dapan1121 已提交
701 702 703 704
#define CTG_CACHE_OVERFLOW(_csize, _maxsize) ((_maxsize >= 0) ? ((_csize) >= (_maxsize) * 1048576L * 0.9) : false)
#define CTG_CACHE_LOW(_csize, _maxsize) ((_maxsize >= 0) ? ((_csize) <= (_maxsize) * 1048576L * 0.75) : true)


H
Hongze Cheng 已提交
705 706 707 708 709 710 711
#define ctgFatal(param, ...) qFatal("CTG:%p " param, pCtg, __VA_ARGS__)
#define ctgError(param, ...) qError("CTG:%p " param, pCtg, __VA_ARGS__)
#define ctgWarn(param, ...)  qWarn("CTG:%p " param, pCtg, __VA_ARGS__)
#define ctgInfo(param, ...)  qInfo("CTG:%p " param, pCtg, __VA_ARGS__)
#define ctgDebug(param, ...) qDebug("CTG:%p " param, pCtg, __VA_ARGS__)
#define ctgTrace(param, ...) qTrace("CTG:%p " param, pCtg, __VA_ARGS__)

712 713 714 715 716 717 718
#define ctgTaskFatal(param, ...) qFatal("QID:%" PRIx64 " CTG:%p " param, pTask->pJob->queryId, pCtg, __VA_ARGS__)
#define ctgTaskError(param, ...) qError("QID:%" PRIx64 " CTG:%p " param, pTask->pJob->queryId, pCtg, __VA_ARGS__)
#define ctgTaskWarn(param, ...)  qWarn("QID:%" PRIx64 " CTG:%p " param, pTask->pJob->queryId, pCtg, __VA_ARGS__)
#define ctgTaskInfo(param, ...)  qInfo("QID:%" PRIx64 " CTG:%p " param, pTask->pJob->queryId, pCtg, __VA_ARGS__)
#define ctgTaskDebug(param, ...) qDebug("QID:%" PRIx64 " CTG:%p " param, pTask->pJob->queryId, pCtg, __VA_ARGS__)
#define ctgTaskTrace(param, ...) qTrace("QID:%" PRIx64 " CTG:%p " param, pTask->pJob->queryId, pCtg, __VA_ARGS__)

H
Hongze Cheng 已提交
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
#define CTG_LOCK_DEBUG(...)     \
  do {                          \
    if (gCTGDebug.lockEnable) { \
      qDebug(__VA_ARGS__);      \
    }                           \
  } while (0)
#define CTG_CACHE_DEBUG(...)     \
  do {                           \
    if (gCTGDebug.cacheEnable) { \
      qDebug(__VA_ARGS__);       \
    }                            \
  } while (0)
#define CTG_API_DEBUG(...)     \
  do {                         \
    if (gCTGDebug.apiEnable) { \
      qDebug(__VA_ARGS__);     \
    }                          \
  } while (0)
D
dapan1121 已提交
737

H
Hongze Cheng 已提交
738
#define TD_RWLATCH_WRITE_FLAG_COPY 0x40000000
D
dapan 已提交
739

X
Xiaoyu Wang 已提交
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
#define CTG_LOCK(type, _lock)                                                                                \
  do {                                                                                                       \
    if (CTG_READ == (type)) {                                                                                \
      ASSERTS(atomic_load_32((_lock)) >= 0, "invalid lock value before read lock");                          \
      CTG_LOCK_DEBUG("CTG RLOCK%p:%d, %s:%d B", (_lock), atomic_load_32(_lock), __FILE__, __LINE__);         \
      taosRLockLatch(_lock);                                                                                 \
      CTG_LOCK_DEBUG("CTG RLOCK%p:%d, %s:%d E", (_lock), atomic_load_32(_lock), __FILE__, __LINE__);         \
      ASSERTS(atomic_load_32((_lock)) > 0, "invalid lock value after read lock");                            \
    } else {                                                                                                 \
      ASSERTS(atomic_load_32((_lock)) >= 0, "invalid lock value before write lock");                         \
      CTG_LOCK_DEBUG("CTG WLOCK%p:%d, %s:%d B", (_lock), atomic_load_32(_lock), __FILE__, __LINE__);         \
      taosWLockLatch(_lock);                                                                                 \
      CTG_LOCK_DEBUG("CTG WLOCK%p:%d, %s:%d E", (_lock), atomic_load_32(_lock), __FILE__, __LINE__);         \
      ASSERTS(atomic_load_32((_lock)) == TD_RWLATCH_WRITE_FLAG_COPY, "invalid lock value after write lock"); \
    }                                                                                                        \
H
Hongze Cheng 已提交
755
  } while (0)
756

X
Xiaoyu Wang 已提交
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
#define CTG_UNLOCK(type, _lock)                                                                                 \
  do {                                                                                                          \
    if (CTG_READ == (type)) {                                                                                   \
      ASSERTS(atomic_load_32((_lock)) > 0, "invalid lock value before read unlock");                            \
      CTG_LOCK_DEBUG("CTG RULOCK%p:%d, %s:%d B", (_lock), atomic_load_32(_lock), __FILE__, __LINE__);           \
      taosRUnLockLatch(_lock);                                                                                  \
      CTG_LOCK_DEBUG("CTG RULOCK%p:%d, %s:%d E", (_lock), atomic_load_32(_lock), __FILE__, __LINE__);           \
      ASSERTS(atomic_load_32((_lock)) >= 0, "invalid lock value after read unlock");                            \
    } else {                                                                                                    \
      ASSERTS(atomic_load_32((_lock)) == TD_RWLATCH_WRITE_FLAG_COPY, "invalid lock value before write unlock"); \
      CTG_LOCK_DEBUG("CTG WULOCK%p:%d, %s:%d B", (_lock), atomic_load_32(_lock), __FILE__, __LINE__);           \
      taosWUnLockLatch(_lock);                                                                                  \
      CTG_LOCK_DEBUG("CTG WULOCK%p:%d, %s:%d E", (_lock), atomic_load_32(_lock), __FILE__, __LINE__);           \
      ASSERTS(atomic_load_32((_lock)) >= 0, "invalid lock value after write unlock");                           \
    }                                                                                                           \
H
Hongze Cheng 已提交
772
  } while (0)
D
dapan1121 已提交
773

H
Hongze Cheng 已提交
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
#define CTG_ERR_RET(c)                \
  do {                                \
    int32_t _code = c;                \
    if (_code != TSDB_CODE_SUCCESS) { \
      terrno = _code;                 \
      return _code;                   \
    }                                 \
  } while (0)
#define CTG_RET(c)                    \
  do {                                \
    int32_t _code = c;                \
    if (_code != TSDB_CODE_SUCCESS) { \
      terrno = _code;                 \
    }                                 \
    return _code;                     \
  } while (0)
#define CTG_ERR_JRET(c)              \
  do {                               \
    code = c;                        \
    if (code != TSDB_CODE_SUCCESS) { \
      terrno = code;                 \
      goto _return;                  \
    }                                \
D
dapan1121 已提交
797 798
  } while (0)

H
Hongze Cheng 已提交
799 800 801 802 803 804 805
#define CTG_API_LEAVE(c)                             \
  do {                                               \
    int32_t __code = c;                              \
    CTG_UNLOCK(CTG_READ, &gCtgMgmt.lock);            \
    CTG_API_DEBUG("CTG API leave %s", __FUNCTION__); \
    CTG_RET(__code);                                 \
  } while (0)
D
dapan1121 已提交
806

D
dapan1121 已提交
807 808 809 810 811 812
#define CTG_API_NLEAVE()                             \
  do {                                               \
    CTG_UNLOCK(CTG_READ, &gCtgMgmt.lock);            \
    CTG_API_DEBUG("CTG API leave %s", __FUNCTION__); \
  } while (0)  

H
Hongze Cheng 已提交
813 814 815 816 817 818 819 820 821
#define CTG_API_ENTER()                              \
  do {                                               \
    CTG_API_DEBUG("CTG API enter %s", __FUNCTION__); \
    CTG_LOCK(CTG_READ, &gCtgMgmt.lock);              \
    if (atomic_load_8((int8_t*)&gCtgMgmt.exit)) {    \
      CTG_API_LEAVE(TSDB_CODE_CTG_OUT_OF_SERVICE);   \
    }                                                \
  } while (0)

D
dapan1121 已提交
822 823 824 825 826 827 828 829 830
#define CTG_API_NENTER()                             \
  do {                                               \
    CTG_API_DEBUG("CTG API enter %s", __FUNCTION__); \
    CTG_LOCK(CTG_READ, &gCtgMgmt.lock);              \
    if (atomic_load_8((int8_t*)&gCtgMgmt.exit)) {    \
      CTG_API_NLEAVE();                              \
    }                                                \
  } while (0)  

H
Hongze Cheng 已提交
831 832 833 834 835 836 837 838
#define CTG_API_JENTER()                             \
  do {                                               \
    CTG_API_DEBUG("CTG API enter %s", __FUNCTION__); \
    CTG_LOCK(CTG_READ, &gCtgMgmt.lock);              \
    if (atomic_load_8((int8_t*)&gCtgMgmt.exit)) {    \
      CTG_ERR_JRET(TSDB_CODE_CTG_OUT_OF_SERVICE);    \
    }                                                \
  } while (0)
D
dapan1121 已提交
839

H
Hongze Cheng 已提交
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
#define CTG_API_LEAVE_NOLOCK(c)                      \
  do {                                               \
    int32_t __code = c;                              \
    CTG_API_DEBUG("CTG API leave %s", __FUNCTION__); \
    CTG_RET(__code);                                 \
  } while (0)

#define CTG_API_ENTER_NOLOCK()                            \
  do {                                                    \
    CTG_API_DEBUG("CTG API enter %s", __FUNCTION__);      \
    if (atomic_load_8((int8_t*)&gCtgMgmt.exit)) {         \
      CTG_API_LEAVE_NOLOCK(TSDB_CODE_CTG_OUT_OF_SERVICE); \
    }                                                     \
  } while (0)

void    ctgdShowTableMeta(SCatalog* pCtg, const char* tbName, STableMeta* p);
D
dapan1121 已提交
856
void    ctgdShowClusterCache(SCatalog* pCtg);
D
dapan1121 已提交
857
int32_t ctgdShowCacheInfo(void);
D
dapan1121 已提交
858
int32_t ctgdShowStatInfo(void);
D
dapan1121 已提交
859 860

int32_t ctgRemoveTbMetaFromCache(SCatalog* pCtg, SName* pTableName, bool syncReq);
D
dapan1121 已提交
861
int32_t ctgGetTbMetaFromCache(SCatalog* pCtg, SCtgTbMetaCtx* ctx, STableMeta** pTableMeta);
H
Hongze Cheng 已提交
862 863
int32_t ctgGetTbMetasFromCache(SCatalog* pCtg, SRequestConnInfo* pConn, SCtgTbMetasCtx* ctx, int32_t dbIdx,
                               int32_t* fetchIdx, int32_t baseResIdx, SArray* pList);
D
dapan1121 已提交
864
void*   ctgCloneDbCfgInfo(void* pSrc);
H
Hongze Cheng 已提交
865 866

int32_t ctgOpUpdateVgroup(SCtgCacheOperation* action);
D
dapan1121 已提交
867
int32_t ctgOpUpdateDbCfg(SCtgCacheOperation *operation);
H
Hongze Cheng 已提交
868 869 870 871 872 873 874 875 876 877 878
int32_t ctgOpUpdateTbMeta(SCtgCacheOperation* action);
int32_t ctgOpDropDbCache(SCtgCacheOperation* action);
int32_t ctgOpDropDbVgroup(SCtgCacheOperation* action);
int32_t ctgOpDropStbMeta(SCtgCacheOperation* action);
int32_t ctgOpDropTbMeta(SCtgCacheOperation* action);
int32_t ctgOpUpdateUser(SCtgCacheOperation* action);
int32_t ctgOpUpdateEpset(SCtgCacheOperation* operation);
int32_t ctgAcquireVgInfoFromCache(SCatalog* pCtg, const char* dbFName, SCtgDBCache** pCache);
void    ctgReleaseDBCache(SCatalog* pCtg, SCtgDBCache* dbCache);
void    ctgRUnlockVgInfo(SCtgDBCache* dbCache);
int32_t ctgTbMetaExistInCache(SCatalog* pCtg, char* dbFName, char* tbName, int32_t* exist);
D
dapan1121 已提交
879
int32_t ctgReadTbMetaFromCache(SCatalog* pCtg, SCtgTbMetaCtx* ctx, STableMeta** pTableMeta);
H
Hongze Cheng 已提交
880 881
int32_t ctgReadTbVerFromCache(SCatalog* pCtg, SName* pTableName, int32_t* sver, int32_t* tver, int32_t* tbType,
                              uint64_t* suid, char* stbName);
X
Xiaoyu Wang 已提交
882
int32_t ctgChkAuthFromCache(SCatalog* pCtg, SUserAuthInfo* pReq, bool* inCache, SCtgAuthRsp* pRes);
H
Hongze Cheng 已提交
883 884 885 886 887 888
int32_t ctgDropDbCacheEnqueue(SCatalog* pCtg, const char* dbFName, int64_t dbId);
int32_t ctgDropDbVgroupEnqueue(SCatalog* pCtg, const char* dbFName, bool syncReq);
int32_t ctgDropStbMetaEnqueue(SCatalog* pCtg, const char* dbFName, int64_t dbId, const char* stbName, uint64_t suid,
                              bool syncReq);
int32_t ctgDropTbMetaEnqueue(SCatalog* pCtg, const char* dbFName, int64_t dbId, const char* tbName, bool syncReq);
int32_t ctgUpdateVgroupEnqueue(SCatalog* pCtg, const char* dbFName, int64_t dbId, SDBVgInfo* dbInfo, bool syncReq);
D
dapan1121 已提交
889
int32_t ctgUpdateDbCfgEnqueue(SCatalog *pCtg, const char *dbFName, int64_t dbId, SDbCfgInfo *cfgInfo, bool syncOp);
H
Hongze Cheng 已提交
890 891 892 893
int32_t ctgUpdateTbMetaEnqueue(SCatalog* pCtg, STableMetaOutput* output, bool syncReq);
int32_t ctgUpdateUserEnqueue(SCatalog* pCtg, SGetUserAuthRsp* pAuth, bool syncReq);
int32_t ctgUpdateVgEpsetEnqueue(SCatalog* pCtg, char* dbFName, int32_t vgId, SEpSet* pEpSet);
int32_t ctgUpdateTbIndexEnqueue(SCatalog* pCtg, STableIndex** pIndex, bool syncOp);
D
dapan1121 已提交
894 895
int32_t ctgClearCacheEnqueue(SCatalog* pCtg, bool clearMeta, bool freeCtg, bool stopQueue, bool syncOp);
int32_t ctgMetaRentInit(SCtgRentMgmt* mgmt, uint32_t rentSec, int8_t type, int32_t size);
H
Hongze Cheng 已提交
896 897
int32_t ctgMetaRentAdd(SCtgRentMgmt* mgmt, void* meta, int64_t id, int32_t size);
int32_t ctgMetaRentGet(SCtgRentMgmt* mgmt, void** res, uint32_t* num, int32_t size);
D
dapan1121 已提交
898 899
int32_t ctgUpdateTbMetaToCache(SCatalog* pCtg, STableMetaOutput* pOut, bool syncReq);
int32_t ctgStartUpdateThread();
H
Hongze Cheng 已提交
900 901
int32_t ctgRelaunchGetTbMetaTask(SCtgTask* pTask);
void    ctgReleaseVgInfoToCache(SCatalog* pCtg, SCtgDBCache* dbCache);
D
dapan1121 已提交
902 903
int32_t ctgReadTbIndexFromCache(SCatalog* pCtg, SName* pTableName, SArray** pRes);
int32_t ctgDropTbIndexEnqueue(SCatalog* pCtg, SName* pName, bool syncOp);
H
Hongze Cheng 已提交
904 905 906 907 908
int32_t ctgOpDropTbIndex(SCtgCacheOperation* operation);
int32_t ctgOpUpdateTbIndex(SCtgCacheOperation* operation);
int32_t ctgOpClearCache(SCtgCacheOperation* operation);
int32_t ctgReadTbTypeFromCache(SCatalog* pCtg, char* dbFName, char* tableName, int32_t* tbType);
int32_t ctgGetTbHashVgroupFromCache(SCatalog* pCtg, const SName* pTableName, SVgroupInfo** pVgroup);
D
dapan1121 已提交
909 910

int32_t ctgProcessRspMsg(void* out, int32_t reqType, char* msg, int32_t msgSize, int32_t rspCode, char* target);
H
Hongze Cheng 已提交
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
int32_t ctgGetDBVgInfoFromMnode(SCatalog* pCtg, SRequestConnInfo* pConn, SBuildUseDBInput* input, SUseDbOutput* out,
                                SCtgTaskReq* tReq);
int32_t ctgGetQnodeListFromMnode(SCatalog* pCtg, SRequestConnInfo* pConn, SArray* out, SCtgTask* pTask);
int32_t ctgGetDnodeListFromMnode(SCatalog* pCtg, SRequestConnInfo* pConn, SArray** out, SCtgTask* pTask);
int32_t ctgGetDBCfgFromMnode(SCatalog* pCtg, SRequestConnInfo* pConn, const char* dbFName, SDbCfgInfo* out,
                             SCtgTask* pTask);
int32_t ctgGetIndexInfoFromMnode(SCatalog* pCtg, SRequestConnInfo* pConn, const char* indexName, SIndexInfo* out,
                                 SCtgTask* pTask);
int32_t ctgGetTbIndexFromMnode(SCatalog* pCtg, SRequestConnInfo* pConn, SName* name, STableIndex* out, SCtgTask* pTask);
int32_t ctgGetUdfInfoFromMnode(SCatalog* pCtg, SRequestConnInfo* pConn, const char* funcName, SFuncInfo* out,
                               SCtgTask* pTask);
int32_t ctgGetUserDbAuthFromMnode(SCatalog* pCtg, SRequestConnInfo* pConn, const char* user, SGetUserAuthRsp* out,
                                  SCtgTask* pTask);
int32_t ctgGetTbMetaFromMnodeImpl(SCatalog* pCtg, SRequestConnInfo* pConn, char* dbFName, char* tbName,
                                  STableMetaOutput* out, SCtgTaskReq* tReq);
int32_t ctgGetTbMetaFromMnode(SCatalog* pCtg, SRequestConnInfo* pConn, const SName* pTableName, STableMetaOutput* out,
                              SCtgTaskReq* tReq);
int32_t ctgGetTbMetaFromVnode(SCatalog* pCtg, SRequestConnInfo* pConn, const SName* pTableName, SVgroupInfo* vgroupInfo,
                              STableMetaOutput* out, SCtgTaskReq* tReq);
int32_t ctgGetTableCfgFromVnode(SCatalog* pCtg, SRequestConnInfo* pConn, const SName* pTableName,
                                SVgroupInfo* vgroupInfo, STableCfg** out, SCtgTask* pTask);
int32_t ctgGetTableCfgFromMnode(SCatalog* pCtg, SRequestConnInfo* pConn, const SName* pTableName, STableCfg** out,
                                SCtgTask* pTask);
int32_t ctgGetSvrVerFromMnode(SCatalog* pCtg, SRequestConnInfo* pConn, char** out, SCtgTask* pTask);
int32_t ctgLaunchBatchs(SCatalog* pCtg, SCtgJob* pJob, SHashObj* pBatchs);

int32_t ctgInitJob(SCatalog* pCtg, SRequestConnInfo* pConn, SCtgJob** job, const SCatalogReq* pReq, catalogCallback fp,
                   void* param);
int32_t ctgLaunchJob(SCtgJob* pJob);
int32_t ctgMakeAsyncRes(SCtgJob* pJob);
int32_t ctgLaunchSubTask(SCtgTask* pTask, CTG_TASK_TYPE type, ctgSubTaskCbFp fp, void* param);
int32_t ctgGetTbCfgCb(SCtgTask* pTask);
D
dapan1121 已提交
943
void    ctgFreeHandle(SCatalog* pCatalog);
D
dapan1121 已提交
944

945
void    ctgFreeMsgSendParam(void* param);
H
Hongze Cheng 已提交
946 947 948 949 950
void    ctgFreeBatch(SCtgBatch* pBatch);
void    ctgFreeBatchs(SHashObj* pBatchs);
int32_t ctgCloneVgInfo(SDBVgInfo* src, SDBVgInfo** dst);
int32_t ctgCloneMetaOutput(STableMetaOutput* output, STableMetaOutput** pOutput);
int32_t ctgGenerateVgList(SCatalog* pCtg, SHashObj* vgHash, SArray** pList);
D
dapan1121 已提交
951
void    ctgFreeJob(void* job);
D
dapan1121 已提交
952
void    ctgFreeHandleImpl(SCatalog* pCtg);
D
dapan1121 已提交
953 954
int32_t ctgGetVgInfoFromHashValue(SCatalog* pCtg, SEpSet* pMgmtEps, SDBVgInfo* dbInfo, const SName* pTableName, SVgroupInfo* pVgroup);
int32_t ctgGetVgInfosFromHashValue(SCatalog* pCtg, SEpSet* pMgmgEpSet, SCtgTaskReq* tReq, SDBVgInfo* dbInfo, SCtgTbHashsCtx* pCtx,
H
Hongze Cheng 已提交
955
                                   char* dbFName, SArray* pNames, bool update);
X
Xiaoyu Wang 已提交
956 957
int32_t ctgGetVgIdsFromHashValue(SCatalog* pCtg, SDBVgInfo* dbInfo, char* dbFName, const char* pTbs[], int32_t tbNum,
                                 int32_t* vgId);
D
dapan1121 已提交
958
void    ctgResetTbMetaTask(SCtgTask* pTask);
H
Hongze Cheng 已提交
959
void    ctgFreeDbCache(SCtgDBCache* dbCache);
D
dapan1121 已提交
960
int32_t ctgStbVersionSortCompare(const void* key1, const void* key2);
D
dapan1121 已提交
961
int32_t ctgDbCacheInfoSortCompare(const void* key1, const void* key2);
D
dapan1121 已提交
962
int32_t ctgStbVersionSearchCompare(const void* key1, const void* key2);
D
dapan1121 已提交
963
int32_t ctgDbCacheInfoSearchCompare(const void* key1, const void* key2);
D
dapan1121 已提交
964
void    ctgFreeSTableMetaOutput(STableMetaOutput* pOutput);
D
dapan1121 已提交
965
int32_t ctgUpdateMsgCtx(SCtgMsgCtx* pCtx, int32_t reqType, void* out, char* target);
D
dapan1121 已提交
966
int32_t ctgAddMsgCtx(SArray* pCtxs, int32_t reqType, void* out, char* target);
H
Hongze Cheng 已提交
967 968 969 970
char*   ctgTaskTypeStr(CTG_TASK_TYPE type);
int32_t ctgUpdateSendTargetInfo(SMsgSendInfo* pMsgSendInfo, int32_t msgType, char* dbFName, int32_t vgId);
int32_t ctgGetTablesReqNum(SArray* pList);
int32_t ctgAddFetch(SArray** pFetchs, int32_t dbIdx, int32_t tbIdx, int32_t* fetchIdx, int32_t resIdx, int32_t flag);
D
dapan1121 已提交
971
int32_t ctgCloneTableIndex(SArray* pIndex, SArray** pRes);
H
Hongze Cheng 已提交
972 973 974
void    ctgFreeSTableIndex(void* info);
void    ctgClearSubTaskRes(SCtgSubRes* pRes);
void    ctgFreeQNode(SCtgQNode* node);
D
dapan1121 已提交
975
void    ctgClearHandle(SCatalog* pCtg);
D
dapan1121 已提交
976
void    ctgFreeTbCacheImpl(SCtgTbCache* pCache, bool lock);
D
dapan1121 已提交
977
int32_t ctgRemoveTbMeta(SCatalog* pCtg, SName* pTableName);
X
Xiaoyu Wang 已提交
978 979
int32_t ctgGetTbHashVgroup(SCatalog* pCtg, SRequestConnInfo* pConn, const SName* pTableName, SVgroupInfo* pVgroup,
                           bool* exists);
D
dapan1121 已提交
980
SName*  ctgGetFetchName(SArray* pNames, SCtgFetch* pFetch);
X
Xiaoyu Wang 已提交
981
int32_t ctgdGetOneHandle(SCatalog** pHandle);
982
int     ctgVgInfoComp(const void* lp, const void* rp);
983
int32_t ctgMakeVgArray(SDBVgInfo* dbInfo);
D
dapan1121 已提交
984
int32_t ctgChkSetAuthRes(SCatalog *pCtg, SCtgAuthReq *req, SCtgAuthRsp* res);
D
dapan1121 已提交
985
int32_t ctgReadDBCfgFromCache(SCatalog *pCtg, const char* dbFName, SDbCfgInfo* pDbCfg);
986

X
Xiaoyu Wang 已提交
987 988 989 990 991 992 993 994
int32_t ctgAcquireVgMetaFromCache(SCatalog* pCtg, const char* dbFName, const char* tbName, SCtgDBCache** pDb,
                                  SCtgTbCache** pTb);
int32_t ctgCopyTbMeta(SCatalog* pCtg, SCtgTbMetaCtx* ctx, SCtgDBCache** pDb, SCtgTbCache** pTb, STableMeta** pTableMeta,
                      char* dbFName);
void    ctgReleaseVgMetaToCache(SCatalog* pCtg, SCtgDBCache* dbCache, SCtgTbCache* pCache);
void    ctgReleaseTbMetaToCache(SCatalog* pCtg, SCtgDBCache* dbCache, SCtgTbCache* pCache);
void    ctgGetGlobalCacheStat(SCtgCacheStat* pStat);
int32_t ctgChkSetAuthRes(SCatalog* pCtg, SCtgAuthReq* req, SCtgAuthRsp* res);
D
dapan1121 已提交
995 996 997 998 999 1000 1001
void    ctgGetGlobalCacheSize(uint64_t *pSize);
uint64_t ctgGetTbIndexCacheSize(STableIndex *pIndex);
uint64_t ctgGetTbMetaCacheSize(STableMeta *pMeta);
uint64_t ctgGetDbVgroupCacheSize(SDBVgInfo *pVg);
uint64_t ctgGetUserCacheSize(SGetUserAuthRsp *pAuth);
uint64_t ctgGetClusterCacheSize(SCatalog *pCtg);
void     ctgClearHandleMeta(SCatalog* pCtg, int64_t *pClearedSize, int64_t *pCleardNum, bool *roundDone);
D
dapan1121 已提交
1002
void     ctgClearAllHandleMeta(int64_t *clearedSize, int64_t *clearedNum, bool *roundDone);
D
dapan1121 已提交
1003 1004
void     ctgProcessTimerEvent(void *param, void *tmrId);

1005 1006 1007 1008
int32_t ctgGetTbMeta(SCatalog* pCtg, SRequestConnInfo* pConn, SCtgTbMetaCtx* ctx, STableMeta** pTableMeta);
int32_t ctgGetCachedStbNameFromSuid(SCatalog* pCtg, char* dbFName, uint64_t suid, char **stbName);
int32_t ctgGetTbTagCb(SCtgTask* pTask);
int32_t ctgGetUserCb(SCtgTask* pTask);
X
Xiaoyu Wang 已提交
1009 1010 1011 1012

extern SCatalogMgmt      gCtgMgmt;
extern SCtgDebug         gCTGDebug;
extern SCtgAsyncFps      gCtgAsyncFps[];
D
dapan1121 已提交
1013
extern SCtgCacheItemInfo gCtgStatItem[CTG_CI_MAX_VALUE];
D
dapan1121 已提交
1014

H
Hongze Cheng 已提交
1015 1016 1017 1018
#ifdef __cplusplus
}
#endif

D
dapan 已提交
1019
#endif /*_TD_CATALOG_INT_H_*/