catalogInt.h 6.2 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 30
#define CTG_DEFAULT_CACHE_CLUSTER_NUMBER 6
#define CTG_DEFAULT_CACHE_VGROUP_NUMBER 100
#define CTG_DEFAULT_CACHE_DB_NUMBER 20
#define CTG_DEFAULT_CACHE_TABLEMETA_NUMBER 100000
D
dapan1121 已提交
31 32 33 34
#define CTG_DEFAULT_RENT_SECOND 10
#define CTG_DEFAULT_RENT_SLOT_SIZE 10

#define CTG_RENT_SLOT_SECOND 2
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 51
typedef struct SVgroupListCache {
  int32_t vgroupVersion;
D
dapan1121 已提交
52
  SHashObj *cache;        // key:vgId, value:SVgroupInfo
D
dapan1121 已提交
53 54 55 56 57
} SVgroupListCache;

typedef struct SDBVgroupCache {
  SHashObj *cache;      //key:dbname, value:SDBVgroupInfo
} SDBVgroupCache;
D
dapan1121 已提交
58

D
dapan1121 已提交
59
typedef struct STableMetaCache {
D
dapan1121 已提交
60
  SRWLatch  stableLock;
D
dapan1121 已提交
61 62
  SHashObj *cache;           //key:fulltablename, value:STableMeta
  SHashObj *stableCache;     //key:suid, value:STableMeta*
D
dapan1121 已提交
63 64
} STableMetaCache;

D
dapan1121 已提交
65 66 67
typedef struct SRentSlotInfo {
  SRWLatch lock;
  bool     needSort;
D
dapan1121 已提交
68
  SArray  *meta;  // element is SDbVgVersion or SSTableMetaVersion
D
dapan1121 已提交
69 70 71 72 73 74 75 76 77 78
} SRentSlotInfo;

typedef struct SMetaRentMgmt {
  int8_t         type;
  uint16_t       slotNum;
  uint16_t       slotRIdx;
  int64_t        lastReadMsec;
  SRentSlotInfo *slots;
} SMetaRentMgmt;

D
dapan1121 已提交
79
typedef struct SCatalog {
D
dapan1121 已提交
80
  uint64_t         clusterId;
D
dapan1121 已提交
81 82
  SDBVgroupCache   dbCache;
  STableMetaCache  tableCache;
D
dapan1121 已提交
83 84
  SMetaRentMgmt    dbRent;
  SMetaRentMgmt    stableRent;
H
Haojun Liao 已提交
85 86
} SCatalog;

D
dapan1121 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
typedef struct SCtgApiStat {

} SCtgApiStat;

typedef struct SCtgResourceStat {

} SCtgResourceStat;

typedef struct SCtgCacheStat {

} SCtgCacheStat;

typedef struct SCatalogStat {
  SCtgApiStat      api;
  SCtgResourceStat resource;
  SCtgCacheStat    cache;
} SCatalogStat;

D
dapan1121 已提交
105
typedef struct SCatalogMgmt {
D
dapan1121 已提交
106 107 108
  SHashObj             *pCluster;     //key: clusterId, value: SCatalog*
  SCatalogStat          stat;
  SCatalogCfg           cfg;
D
dapan1121 已提交
109 110
} SCatalogMgmt;

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

D
dapan1121 已提交
113 114 115 116 117
#define CTG_IS_META_NONE(type) ((type) == META_TYPE_NON_TABLE)
#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 已提交
118 119 120 121 122 123
#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 已提交
124
#define CTG_TABLE_NOT_EXIST(code) (code == CTG_ERR_CODE_TABLE_NOT_EXIST) 
D
dapan1121 已提交
125

D
dapan1121 已提交
126 127 128 129 130 131
#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 已提交
132

D
dapan1121 已提交
133 134 135
#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 已提交
136

D
dapan1121 已提交
137 138
#define TD_RWLATCH_WRITE_FLAG_COPY 0x40000000

D
dapan1121 已提交
139 140
#define CTG_LOCK(type, _lock) do {   \
  if (CTG_READ == (type)) {          \
D
dapan1121 已提交
141
    assert(atomic_load_32((_lock)) >= 0);  \
D
dapan1121 已提交
142
    qDebug("CTG RLOCK%p:%d, %s:%d B", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
D
dapan1121 已提交
143
    taosRLockLatch(_lock);           \
D
dapan1121 已提交
144
    qDebug("CTG RLOCK%p:%d, %s:%d E", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
D
dapan1121 已提交
145
    assert(atomic_load_32((_lock)) > 0);  \
D
dapan1121 已提交
146
  } else {                                                \
D
dapan1121 已提交
147
    assert(atomic_load_32((_lock)) >= 0);  \
D
dapan1121 已提交
148
    qDebug("CTG WLOCK%p:%d, %s:%d B", (_lock), atomic_load_32(_lock), __FILE__, __LINE__);  \
D
dapan1121 已提交
149
    taosWLockLatch(_lock);                                \
D
dapan1121 已提交
150
    qDebug("CTG WLOCK%p:%d, %s:%d E", (_lock), atomic_load_32(_lock), __FILE__, __LINE__);  \
D
dapan1121 已提交
151
    assert(atomic_load_32((_lock)) == TD_RWLATCH_WRITE_FLAG_COPY);  \
D
dapan1121 已提交
152 153 154 155 156
  }                                                       \
} while (0)

#define CTG_UNLOCK(type, _lock) do {                       \
  if (CTG_READ == (type)) {                                \
D
dapan1121 已提交
157
    assert(atomic_load_32((_lock)) > 0);  \
D
dapan1121 已提交
158
    qDebug("CTG RULOCK%p:%d, %s:%d B", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
D
dapan1121 已提交
159
    taosRUnLockLatch(_lock);                              \
D
dapan1121 已提交
160
    qDebug("CTG RULOCK%p:%d, %s:%d E", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
D
dapan1121 已提交
161
    assert(atomic_load_32((_lock)) >= 0);  \
D
dapan1121 已提交
162
  } else {                                                \
D
dapan1121 已提交
163
    assert(atomic_load_32((_lock)) == TD_RWLATCH_WRITE_FLAG_COPY);  \
D
dapan1121 已提交
164
    qDebug("CTG WULOCK%p:%d, %s:%d B", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
D
dapan1121 已提交
165
    taosWUnLockLatch(_lock);                              \
D
dapan1121 已提交
166
    qDebug("CTG WULOCK%p:%d, %s:%d E", (_lock), atomic_load_32(_lock), __FILE__, __LINE__); \
D
dapan1121 已提交
167
    assert(atomic_load_32((_lock)) >= 0);  \
D
dapan1121 已提交
168 169 170 171
  }                                                       \
} while (0)


H
Hongze Cheng 已提交
172 173 174 175
#ifdef __cplusplus
}
#endif

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