catalogInt.h 11.3 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"
S
common  
Shengliang Guan 已提交
24
#include "tcommon.h"
D
dapan1121 已提交
25
#include "query.h"
H
Haojun Liao 已提交
26

D
dapan1121 已提交
27 28 29
#define CTG_DEFAULT_CACHE_CLUSTER_NUMBER 6
#define CTG_DEFAULT_CACHE_VGROUP_NUMBER 100
#define CTG_DEFAULT_CACHE_DB_NUMBER 20
D
dapan1121 已提交
30
#define CTG_DEFAULT_CACHE_TBLMETA_NUMBER 1000
D
dapan1121 已提交
31 32
#define CTG_DEFAULT_RENT_SECOND 10
#define CTG_DEFAULT_RENT_SLOT_SIZE 10
D
dapan1121 已提交
33
#define CTG_DEFAULT_MAX_RETRY_TIMES 3
D
dapan1121 已提交
34

D
dapan1121 已提交
35
#define CTG_RENT_SLOT_SECOND 1.5
D
dapan1121 已提交
36 37

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

D
dapan1121 已提交
39 40
#define CTG_ERR_CODE_TABLE_NOT_EXIST TSDB_CODE_TDB_INVALID_TABLE_ID

D
dapan1121 已提交
41 42 43 44 45
enum {
  CTG_READ = 1,
  CTG_WRITE,
};

D
dapan1121 已提交
46 47 48 49 50
enum {
  CTG_RENT_DB = 1,
  CTG_RENT_STABLE,
};

D
dapan1121 已提交
51 52 53 54 55 56
enum {
  CTG_ACT_UPDATE_VG = 0,
  CTG_ACT_UPDATE_TBL,
  CTG_ACT_REMOVE_DB,
  CTG_ACT_REMOVE_STB,
  CTG_ACT_REMOVE_TBL,
D
dapan 已提交
57
  CTG_ACT_UPDATE_USER,
D
dapan1121 已提交
58 59 60
  CTG_ACT_MAX
};

D
dapan1121 已提交
61
typedef struct SCtgDebug {
D
dapan1121 已提交
62 63 64 65
  bool     lockEnable;
  bool     cacheEnable;
  bool     apiEnable;
  bool     metaEnable;
D
dapan1121 已提交
66
  uint32_t showCachePeriodSec;
D
dapan1121 已提交
67
} SCtgDebug;
68 69


D
dapan1121 已提交
70 71
typedef struct SCtgTbMetaCache {
  SRWLatch  stbLock;
D
dapan1121 已提交
72 73
  SRWLatch  metaLock;        // RC between cache destroy and all other operations
  SHashObj *metaCache;       //key:tbname, value:STableMeta
D
dapan1121 已提交
74 75
  SHashObj *stbCache;        //key:suid, value:STableMeta*
} SCtgTbMetaCache;
D
dapan1121 已提交
76

D
dapan1121 已提交
77 78
typedef struct SCtgDBCache {
  SRWLatch         vgLock;
D
dapan1121 已提交
79
  uint64_t         dbId;
D
dapan1121 已提交
80
  int8_t           deleted;
D
dapan1121 已提交
81
  SDBVgInfo       *vgInfo;  
D
dapan1121 已提交
82 83
  SCtgTbMetaCache  tbCache;
} SCtgDBCache;
D
dapan1121 已提交
84

D
dapan1121 已提交
85
typedef struct SCtgRentSlot {
D
dapan1121 已提交
86 87
  SRWLatch lock;
  bool     needSort;
D
dapan1121 已提交
88
  SArray  *meta;  // element is SDbVgVersion or SSTableMetaVersion
D
dapan1121 已提交
89
} SCtgRentSlot;
D
dapan1121 已提交
90

D
dapan1121 已提交
91
typedef struct SCtgRentMgmt {
D
dapan1121 已提交
92 93 94 95
  int8_t         type;
  uint16_t       slotNum;
  uint16_t       slotRIdx;
  int64_t        lastReadMsec;
D
dapan1121 已提交
96 97
  SCtgRentSlot  *slots;
} SCtgRentMgmt;
D
dapan1121 已提交
98

D
dapan 已提交
99 100 101 102 103 104 105 106 107
typedef struct SCtgUserAuth {
  int32_t   version;
  SRWLatch  lock;
  bool      superUser;
  SHashObj *createdDbs;
  SHashObj *readDbs;
  SHashObj *writeDbs;
} SCtgUserAuth;

D
dapan1121 已提交
108
typedef struct SCatalog {
D
dapan1121 已提交
109
  uint64_t         clusterId;  
D
dapan 已提交
110
  SHashObj        *userCache;    //key:user, value:SCtgUserAuth
D
dapan1121 已提交
111 112 113
  SHashObj        *dbCache;      //key:dbname, value:SCtgDBCache
  SCtgRentMgmt     dbRent;
  SCtgRentMgmt     stbRent;
H
Haojun Liao 已提交
114 115
} SCatalog;

D
dapan1121 已提交
116 117
typedef struct SCtgApiStat {

wafwerar's avatar
wafwerar 已提交
118 119 120 121
#ifdef WINDOWS
  size_t avoidCompilationErrors;
#endif

D
dapan1121 已提交
122 123
} SCtgApiStat;

D
dapan1121 已提交
124
typedef struct SCtgRuntimeStat {
D
dapan 已提交
125 126
  uint64_t qNum;
  uint64_t qDoneNum;
D
dapan1121 已提交
127
} SCtgRuntimeStat;
D
dapan1121 已提交
128 129

typedef struct SCtgCacheStat {
D
dapan1121 已提交
130 131 132
  uint64_t clusterNum;
  uint64_t dbNum;
  uint64_t tblNum;
D
dapan1121 已提交
133
  uint64_t stblNum;
D
dapan1121 已提交
134 135 136 137
  uint64_t vgHitNum;
  uint64_t vgMissNum;
  uint64_t tblHitNum;
  uint64_t tblMissNum;
D
dapan 已提交
138 139
  uint64_t userHitNum;
  uint64_t userMissNum;
D
dapan1121 已提交
140 141 142 143
} SCtgCacheStat;

typedef struct SCatalogStat {
  SCtgApiStat      api;
D
dapan1121 已提交
144
  SCtgRuntimeStat  runtime;
D
dapan1121 已提交
145 146 147
  SCtgCacheStat    cache;
} SCatalogStat;

D
dapan1121 已提交
148 149 150 151
typedef struct SCtgUpdateMsgHeader {
  SCatalog* pCtg;
} SCtgUpdateMsgHeader;

D
dapan1121 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
typedef struct SCtgUpdateVgMsg {
  SCatalog* pCtg;
  char  dbFName[TSDB_DB_FNAME_LEN];
  uint64_t dbId;
  SDBVgInfo* dbInfo;
} SCtgUpdateVgMsg;

typedef struct SCtgUpdateTblMsg {
  SCatalog* pCtg;
  STableMetaOutput* output;
} SCtgUpdateTblMsg;

typedef struct SCtgRemoveDBMsg {
  SCatalog* pCtg;
  char  dbFName[TSDB_DB_FNAME_LEN];
  uint64_t dbId;
} SCtgRemoveDBMsg;

typedef struct SCtgRemoveStbMsg {
  SCatalog* pCtg;
  char  dbFName[TSDB_DB_FNAME_LEN];
  char  stbName[TSDB_TABLE_NAME_LEN];
  uint64_t dbId;
  uint64_t suid;
} SCtgRemoveStbMsg;

D
dapan1121 已提交
178 179 180 181 182 183 184
typedef struct SCtgRemoveTblMsg {
  SCatalog* pCtg;
  char  dbFName[TSDB_DB_FNAME_LEN];
  char  tbName[TSDB_TABLE_NAME_LEN];
  uint64_t dbId;
} SCtgRemoveTblMsg;

D
dapan 已提交
185 186 187
typedef struct SCtgUpdateUserMsg {
  SCatalog* pCtg;
  SGetUserAuthRsp userAuth;
D
dapan 已提交
188
} SCtgUpdateUserMsg;
D
dapan 已提交
189

D
dapan1121 已提交
190

D
dapan1121 已提交
191
typedef struct SCtgMetaAction {
D
dapan1121 已提交
192 193 194 195
  int32_t  act;
  void    *data;
  bool     syncReq;
  uint64_t seqId;
D
dapan1121 已提交
196 197 198 199 200 201 202
} SCtgMetaAction;

typedef struct SCtgQNode {
  SCtgMetaAction         action;
  struct SCtgQNode      *next;
} SCtgQNode;

D
dapan1121 已提交
203
typedef struct SCtgQueue {
D
dapan1121 已提交
204
  SRWLatch              qlock;
D
dapan1121 已提交
205 206
  uint64_t              seqId;
  uint64_t              seqDone;
D
dapan1121 已提交
207 208
  SCtgQNode            *head;
  SCtgQNode            *tail;
D
dapan1121 已提交
209 210
  tsem_t                reqSem;  
  tsem_t                rspSem;  
D
dapan 已提交
211
  uint64_t              qRemainNum;
D
dapan1121 已提交
212 213 214 215 216 217
} SCtgQueue;

typedef struct SCatalogMgmt {
  bool                  exit;
  SRWLatch              lock;
  SCtgQueue             queue;
D
dapan1121 已提交
218
  TdThread              updateThread;  
D
dapan1121 已提交
219 220 221
  SHashObj             *pCluster;     //key: clusterId, value: SCatalog*
  SCatalogStat          stat;
  SCatalogCfg           cfg;
D
dapan1121 已提交
222 223
} SCatalogMgmt;

D
dapan1121 已提交
224
typedef uint32_t (*tableNameHashFp)(const char *, uint32_t);
D
dapan1121 已提交
225
typedef int32_t (*ctgActFunc)(SCtgMetaAction *);
D
dapan 已提交
226

D
dapan 已提交
227 228 229 230 231 232
typedef struct SCtgAction {
  int32_t    actId;
  char       name[32];
  ctgActFunc func;
} SCtgAction;

D
dapan1121 已提交
233 234
#define CTG_QUEUE_ADD() atomic_add_fetch_64(&gCtgMgmt.queue.qRemainNum, 1)
#define CTG_QUEUE_SUB() atomic_sub_fetch_64(&gCtgMgmt.queue.qRemainNum, 1)
D
dapan 已提交
235

D
dapan1121 已提交
236 237 238
#define CTG_STAT_ADD(_item, _n) atomic_add_fetch_64(&(_item), _n)
#define CTG_STAT_SUB(_item, _n) atomic_sub_fetch_64(&(_item), _n)
#define CTG_STAT_GET(_item) atomic_load_64(&(_item))
D
dapan1121 已提交
239

D
dapan1121 已提交
240 241 242
#define CTG_RUNTIME_STAT_ADD(item, n) (CTG_STAT_ADD(gCtgMgmt.stat.runtime.item, n))
#define CTG_CACHE_STAT_ADD(item, n) (CTG_STAT_ADD(gCtgMgmt.stat.cache.item, n))
#define CTG_CACHE_STAT_SUB(item, n) (CTG_STAT_SUB(gCtgMgmt.stat.cache.item, n))
D
dapan 已提交
243

D
dapan1121 已提交
244
#define CTG_IS_META_NULL(type) ((type) == META_TYPE_NULL_TABLE)
D
dapan1121 已提交
245 246 247 248
#define CTG_IS_META_CTABLE(type) ((type) == META_TYPE_CTABLE)
#define CTG_IS_META_TABLE(type) ((type) == META_TYPE_TABLE)
#define CTG_IS_META_BOTH(type) ((type) == META_TYPE_BOTH_TABLE)

D
dapan1121 已提交
249 250 251
#define CTG_FLAG_STB          0x1
#define CTG_FLAG_NOT_STB      0x2
#define CTG_FLAG_UNKNOWN_STB  0x4
D
dapan1121 已提交
252
#define CTG_FLAG_SYS_DB       0x8
D
dapan1121 已提交
253 254
#define CTG_FLAG_FORCE_UPDATE 0x10

D
dapan 已提交
255 256
#define CTG_FLAG_SET(_flag, _v) ((_flag) |= (_v))

D
dapan1121 已提交
257 258 259
#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)
D
dapan1121 已提交
260
#define CTG_FLAG_IS_SYS_DB(_flag) ((_flag) & CTG_FLAG_SYS_DB)
D
dapan1121 已提交
261
#define CTG_FLAG_IS_FORCE_UPDATE(_flag) ((_flag) & CTG_FLAG_FORCE_UPDATE)
D
dapan1121 已提交
262
#define CTG_FLAG_SET_SYS_DB(_flag) ((_flag) |= CTG_FLAG_SYS_DB)
D
dapan1121 已提交
263 264 265
#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 已提交
266

D
dapan1121 已提交
267
#define CTG_IS_SYS_DBNAME(_dbname) (((*(_dbname) == 'i') && (0 == strcmp(_dbname, TSDB_INFORMATION_SCHEMA_DB))) || ((*(_dbname) == 'p') && (0 == strcmp(_dbname, TSDB_PERFORMANCE_SCHEMA_DB))))
D
dapan1121 已提交
268

D
dapan1121 已提交
269 270
#define CTG_META_SIZE(pMeta) (sizeof(STableMeta) + ((pMeta)->tableInfo.numOfTags + (pMeta)->tableInfo.numOfColumns) * sizeof(SSchema))

D
dapan1121 已提交
271
#define CTG_TABLE_NOT_EXIST(code) (code == CTG_ERR_CODE_TABLE_NOT_EXIST) 
D
dapan1121 已提交
272
#define CTG_DB_NOT_EXIST(code) (code == TSDB_CODE_MND_DB_NOT_EXIST) 
D
dapan1121 已提交
273

D
dapan1121 已提交
274 275 276 277 278 279
#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__)
D
dapan 已提交
280

D
dapan1121 已提交
281 282 283
#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)
284

D
dapan1121 已提交
285 286
#define TD_RWLATCH_WRITE_FLAG_COPY 0x40000000

D
dapan1121 已提交
287 288
#define CTG_IS_LOCKED(_lock) atomic_load_32((_lock))

D
dapan1121 已提交
289 290
#define CTG_LOCK(type, _lock) do {   \
  if (CTG_READ == (type)) {          \
D
dapan1121 已提交
291
    assert(atomic_load_32((_lock)) >= 0);  \
292
    CTG_LOCK_DEBUG("CTG RLOCK%p:%d, %s:%d B", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
D
dapan1121 已提交
293
    taosRLockLatch(_lock);           \
294
    CTG_LOCK_DEBUG("CTG RLOCK%p:%d, %s:%d E", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
D
dapan1121 已提交
295
    assert(atomic_load_32((_lock)) > 0);  \
D
dapan1121 已提交
296
  } else {                                                \
D
dapan1121 已提交
297
    assert(atomic_load_32((_lock)) >= 0);  \
298
    CTG_LOCK_DEBUG("CTG WLOCK%p:%d, %s:%d B", (_lock), atomic_load_32(_lock), __FILE__, __LINE__);  \
D
dapan1121 已提交
299
    taosWLockLatch(_lock);                                \
300
    CTG_LOCK_DEBUG("CTG WLOCK%p:%d, %s:%d E", (_lock), atomic_load_32(_lock), __FILE__, __LINE__);  \
D
dapan1121 已提交
301
    assert(atomic_load_32((_lock)) == TD_RWLATCH_WRITE_FLAG_COPY);  \
D
dapan1121 已提交
302 303 304 305 306
  }                                                       \
} while (0)

#define CTG_UNLOCK(type, _lock) do {                       \
  if (CTG_READ == (type)) {                                \
D
dapan1121 已提交
307
    assert(atomic_load_32((_lock)) > 0);  \
308
    CTG_LOCK_DEBUG("CTG RULOCK%p:%d, %s:%d B", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
D
dapan1121 已提交
309
    taosRUnLockLatch(_lock);                              \
310
    CTG_LOCK_DEBUG("CTG RULOCK%p:%d, %s:%d E", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
D
dapan1121 已提交
311
    assert(atomic_load_32((_lock)) >= 0);  \
D
dapan1121 已提交
312
  } else {                                                \
D
dapan1121 已提交
313
    assert(atomic_load_32((_lock)) == TD_RWLATCH_WRITE_FLAG_COPY);  \
314
    CTG_LOCK_DEBUG("CTG WULOCK%p:%d, %s:%d B", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
D
dapan1121 已提交
315
    taosWUnLockLatch(_lock);                              \
316
    CTG_LOCK_DEBUG("CTG WULOCK%p:%d, %s:%d E", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
D
dapan1121 已提交
317
    assert(atomic_load_32((_lock)) >= 0);  \
D
dapan1121 已提交
318 319 320
  }                                                       \
} while (0)

D
dapan1121 已提交
321 322 323 324 325
  
#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; } } while (0)

D
dapan 已提交
326
#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)
wafwerar's avatar
wafwerar 已提交
327
#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 已提交
328

D
dapan1121 已提交
329

D
dapan1121 已提交
330 331 332
extern void ctgdShowTableMeta(SCatalog* pCtg, const char *tbName, STableMeta* p);
extern void ctgdShowClusterCache(SCatalog* pCtg);
extern int32_t ctgdShowCacheInfo(void);
D
dapan1121 已提交
333

H
Hongze Cheng 已提交
334 335 336 337
#ifdef __cplusplus
}
#endif

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