catalogInt.h 6.9 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 "common.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
dapan 已提交
30
#define CTG_DEFAULT_CACHE_TABLEMETA_NUMBER 10000
D
dapan1121 已提交
31 32 33
#define CTG_DEFAULT_RENT_SECOND 10
#define CTG_DEFAULT_RENT_SLOT_SIZE 10

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

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

D
dapan1121 已提交
38 39
#define CTG_ERR_CODE_TABLE_NOT_EXIST TSDB_CODE_TDB_INVALID_TABLE_ID

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

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

D
dapan1121 已提交
50
typedef struct SCtgDebug {
D
dapan1121 已提交
51 52 53
  bool     lockDebug;
  bool     cacheDebug;
  uint32_t showCachePeriodSec;
D
dapan1121 已提交
54
} SCtgDebug;
55 56


D
dapan1121 已提交
57 58
typedef struct SCtgTbMetaCache {
  SRWLatch  stbLock;
D
dapan1121 已提交
59 60
  SRWLatch  metaLock;        // RC between cache destroy and all other operations
  SHashObj *metaCache;       //key:tbname, value:STableMeta
D
dapan1121 已提交
61 62
  SHashObj *stbCache;        //key:suid, value:STableMeta*
} SCtgTbMetaCache;
D
dapan1121 已提交
63

D
dapan1121 已提交
64 65
typedef struct SCtgDBCache {
  SRWLatch         vgLock;
D
dapan1121 已提交
66
  uint64_t         dbId;
D
dapan1121 已提交
67 68 69 70
  int8_t           deleted;
  SDBVgroupInfo   *vgInfo;  
  SCtgTbMetaCache  tbCache;
} SCtgDBCache;
D
dapan1121 已提交
71

D
dapan1121 已提交
72
typedef struct SCtgRentSlot {
D
dapan1121 已提交
73 74
  SRWLatch lock;
  bool     needSort;
D
dapan1121 已提交
75
  SArray  *meta;  // element is SDbVgVersion or SSTableMetaVersion
D
dapan1121 已提交
76
} SCtgRentSlot;
D
dapan1121 已提交
77

D
dapan1121 已提交
78
typedef struct SCtgRentMgmt {
D
dapan1121 已提交
79 80 81 82
  int8_t         type;
  uint16_t       slotNum;
  uint16_t       slotRIdx;
  int64_t        lastReadMsec;
D
dapan1121 已提交
83 84
  SCtgRentSlot  *slots;
} SCtgRentMgmt;
D
dapan1121 已提交
85

D
dapan1121 已提交
86
typedef struct SCatalog {
D
dapan1121 已提交
87
  uint64_t         clusterId;  
D
dapan1121 已提交
88
  SRWLatch         dbLock;
D
dapan1121 已提交
89 90 91
  SHashObj        *dbCache;      //key:dbname, value:SCtgDBCache
  SCtgRentMgmt     dbRent;
  SCtgRentMgmt     stbRent;
H
Haojun Liao 已提交
92 93
} SCatalog;

D
dapan1121 已提交
94 95 96 97
typedef struct SCtgApiStat {

} SCtgApiStat;

D
dapan1121 已提交
98
typedef struct SCtgRuntimeStat {
D
dapan1121 已提交
99

D
dapan1121 已提交
100
} SCtgRuntimeStat;
D
dapan1121 已提交
101 102 103 104 105 106 107

typedef struct SCtgCacheStat {

} SCtgCacheStat;

typedef struct SCatalogStat {
  SCtgApiStat      api;
D
dapan1121 已提交
108
  SCtgRuntimeStat  runtime;
D
dapan1121 已提交
109 110 111
  SCtgCacheStat    cache;
} SCatalogStat;

D
dapan1121 已提交
112
typedef struct SCatalogMgmt {
D
dapan1121 已提交
113 114
  bool                  exit;
  SRWLatch              lock;
D
dapan1121 已提交
115 116 117
  SHashObj             *pCluster;     //key: clusterId, value: SCatalog*
  SCatalogStat          stat;
  SCatalogCfg           cfg;
D
dapan1121 已提交
118 119
} SCatalogMgmt;

D
dapan1121 已提交
120
typedef uint32_t (*tableNameHashFp)(const char *, uint32_t);
D
dapan 已提交
121

D
dapan1121 已提交
122
#define CTG_IS_META_NULL(type) ((type) == META_TYPE_NULL_TABLE)
D
dapan1121 已提交
123 124 125 126
#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 已提交
127 128 129 130 131 132
#define CTG_IS_STABLE(isSTable) (1 == (isSTable))
#define CTG_IS_NOT_STABLE(isSTable) (0 == (isSTable))
#define CTG_IS_UNKNOWN_STABLE(isSTable) ((isSTable) < 0)
#define CTG_SET_STABLE(isSTable, tbType) do { (isSTable) = ((tbType) == TSDB_SUPER_TABLE) ? 1 : ((tbType) > TSDB_SUPER_TABLE ? 0 : -1); } while (0)
#define CTG_TBTYPE_MATCH(isSTable, tbType) (CTG_IS_UNKNOWN_STABLE(isSTable) || (CTG_IS_STABLE(isSTable) && (tbType) == TSDB_SUPER_TABLE) || (CTG_IS_NOT_STABLE(isSTable) && (tbType) != TSDB_SUPER_TABLE))

D
dapan1121 已提交
133
#define CTG_TABLE_NOT_EXIST(code) (code == CTG_ERR_CODE_TABLE_NOT_EXIST) 
D
dapan1121 已提交
134

D
dapan1121 已提交
135 136 137 138 139 140
#define ctgFatal(param, ...)  qFatal("CTG:%p " param, pCatalog, __VA_ARGS__)
#define ctgError(param, ...)  qError("CTG:%p " param, pCatalog, __VA_ARGS__)
#define ctgWarn(param, ...)   qWarn("CTG:%p " param, pCatalog, __VA_ARGS__)
#define ctgInfo(param, ...)   qInfo("CTG:%p " param, pCatalog, __VA_ARGS__)
#define ctgDebug(param, ...)  qDebug("CTG:%p " param, pCatalog, __VA_ARGS__)
#define ctgTrace(param, ...)  qTrace("CTG:%p " param, pCatalog, __VA_ARGS__)
D
dapan 已提交
141

D
dapan1121 已提交
142 143
#define CTG_LOCK_DEBUG(...) do { if (gCTGDebug.lockDebug) { qDebug(__VA_ARGS__); } } while (0)
#define CTG_CACHE_DEBUG(...) do { if (gCTGDebug.cacheDebug) { qDebug(__VA_ARGS__); } } while (0)
144

D
dapan1121 已提交
145 146
#define TD_RWLATCH_WRITE_FLAG_COPY 0x40000000

D
dapan1121 已提交
147 148
#define CTG_LOCK(type, _lock) do {   \
  if (CTG_READ == (type)) {          \
D
dapan1121 已提交
149
    assert(atomic_load_32((_lock)) >= 0);  \
150
    CTG_LOCK_DEBUG("CTG RLOCK%p:%d, %s:%d B", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
D
dapan1121 已提交
151
    taosRLockLatch(_lock);           \
152
    CTG_LOCK_DEBUG("CTG RLOCK%p:%d, %s:%d E", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
D
dapan1121 已提交
153
    assert(atomic_load_32((_lock)) > 0);  \
D
dapan1121 已提交
154
  } else {                                                \
D
dapan1121 已提交
155
    assert(atomic_load_32((_lock)) >= 0);  \
156
    CTG_LOCK_DEBUG("CTG WLOCK%p:%d, %s:%d B", (_lock), atomic_load_32(_lock), __FILE__, __LINE__);  \
D
dapan1121 已提交
157
    taosWLockLatch(_lock);                                \
158
    CTG_LOCK_DEBUG("CTG WLOCK%p:%d, %s:%d E", (_lock), atomic_load_32(_lock), __FILE__, __LINE__);  \
D
dapan1121 已提交
159
    assert(atomic_load_32((_lock)) == TD_RWLATCH_WRITE_FLAG_COPY);  \
D
dapan1121 已提交
160 161 162 163 164
  }                                                       \
} while (0)

#define CTG_UNLOCK(type, _lock) do {                       \
  if (CTG_READ == (type)) {                                \
D
dapan1121 已提交
165
    assert(atomic_load_32((_lock)) > 0);  \
166
    CTG_LOCK_DEBUG("CTG RULOCK%p:%d, %s:%d B", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
D
dapan1121 已提交
167
    taosRUnLockLatch(_lock);                              \
168
    CTG_LOCK_DEBUG("CTG RULOCK%p:%d, %s:%d E", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
D
dapan1121 已提交
169
    assert(atomic_load_32((_lock)) >= 0);  \
D
dapan1121 已提交
170
  } else {                                                \
D
dapan1121 已提交
171
    assert(atomic_load_32((_lock)) == TD_RWLATCH_WRITE_FLAG_COPY);  \
172
    CTG_LOCK_DEBUG("CTG WULOCK%p:%d, %s:%d B", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
D
dapan1121 已提交
173
    taosWUnLockLatch(_lock);                              \
174
    CTG_LOCK_DEBUG("CTG WULOCK%p:%d, %s:%d E", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
D
dapan1121 已提交
175
    assert(atomic_load_32((_lock)) >= 0);  \
D
dapan1121 已提交
176 177 178
  }                                                       \
} while (0)

D
dapan1121 已提交
179 180 181 182 183 184 185
  
#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)

#define CTG_API_ENTER() do { CTG_LOCK(CTG_READ, &ctgMgmt.lock); if (atomic_load_8(&ctgMgmt.exit)) { CTG_RET(TSDB_CODE_CTG_OUT_OF_SERVICE); }  } while (0)
#define CTG_API_LEAVE(c) do { int32_t __code = c; CTG_UNLOCK(CTG_READ, &ctgMgmt.lock); CTG_RET(__code); } while (0)
D
dapan1121 已提交
186

D
dapan1121 已提交
187 188


H
Hongze Cheng 已提交
189 190 191 192
#ifdef __cplusplus
}
#endif

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