mndInfoSchema.c 18.6 KB
Newer Older
D
dapan1121 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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/>.
 */

#define _DEFAULT_SOURCE
#include "mndInfoSchema.h"

H
Haojun Liao 已提交
19 20 21
#define SYSTABLE_SCH_TABLE_NAME_LEN ((TSDB_TABLE_NAME_LEN - 1) + VARSTR_HEADER_SIZE)
#define SYSTABLE_SCH_DB_NAME_LEN    ((TSDB_DB_NAME_LEN - 1) + VARSTR_HEADER_SIZE)

H
Haojun Liao 已提交
22 23 24
static const SInfosTableSchema dnodesSchema[] = {{.name = "id",             .bytes = 2,   .type = TSDB_DATA_TYPE_SMALLINT},
                                                 {.name = "endpoint",       .bytes = TSDB_EP_LEN + VARSTR_HEADER_SIZE, .type = TSDB_DATA_TYPE_BINARY},
                                                 {.name = "vnodes",         .bytes = 2,   .type = TSDB_DATA_TYPE_SMALLINT},
H
Haojun Liao 已提交
25
                                                 {.name = "max_vnodes",     .bytes = 2,   .type = TSDB_DATA_TYPE_SMALLINT},
H
Haojun Liao 已提交
26
                                                 {.name = "status",         .bytes = 10 + VARSTR_HEADER_SIZE,  .type = TSDB_DATA_TYPE_BINARY},
D
dapan1121 已提交
27
                                                 {.name = "create_time",    .bytes = 8,   .type = TSDB_DATA_TYPE_TIMESTAMP},
H
Haojun Liao 已提交
28
                                                 {.name = "note",           .bytes = 256 + VARSTR_HEADER_SIZE, .type = TSDB_DATA_TYPE_BINARY},
D
dapan1121 已提交
29 30
                                                };
static const SInfosTableSchema mnodesSchema[] = {{.name = "id",             .bytes = 4,   .type = TSDB_DATA_TYPE_INT},
H
Haojun Liao 已提交
31 32
                                                 {.name = "endpoint",       .bytes = TSDB_EP_LEN + VARSTR_HEADER_SIZE, .type = TSDB_DATA_TYPE_BINARY},
                                                 {.name = "role",           .bytes = 12 + VARSTR_HEADER_SIZE,  .type = TSDB_DATA_TYPE_BINARY},
D
dapan1121 已提交
33 34
                                                 {.name = "role_time",      .bytes = 8,   .type = TSDB_DATA_TYPE_TIMESTAMP},
                                                 {.name = "create_time",    .bytes = 8,   .type = TSDB_DATA_TYPE_TIMESTAMP},
D
dapan1121 已提交
35 36
                                                };
static const SInfosTableSchema modulesSchema[] = {{.name = "id",             .bytes = 4,   .type = TSDB_DATA_TYPE_INT},
H
Haojun Liao 已提交
37
                                                 {.name = "endpoint",        .bytes = 134, .type = TSDB_DATA_TYPE_BINARY},
D
dapan1121 已提交
38
                                                 {.name = "module",          .bytes = 10,  .type = TSDB_DATA_TYPE_BINARY},
D
dapan1121 已提交
39 40
                                                };
static const SInfosTableSchema qnodesSchema[] = {{.name = "id",             .bytes = 4,   .type = TSDB_DATA_TYPE_INT},
H
Haojun Liao 已提交
41
                                                 {.name = "endpoint",       .bytes = 134, .type = TSDB_DATA_TYPE_BINARY},
D
dapan1121 已提交
42
                                                 {.name = "create_time",    .bytes = 8,   .type = TSDB_DATA_TYPE_TIMESTAMP},
D
dapan1121 已提交
43
                                                };
H
Haojun Liao 已提交
44
static const SInfosTableSchema userDBSchema[] = {{.name = "name",             .bytes = SYSTABLE_SCH_DB_NAME_LEN,   .type = TSDB_DATA_TYPE_BINARY},
H
Haojun Liao 已提交
45
                                                 {.name = "create_time",      .bytes = 8,    .type = TSDB_DATA_TYPE_TIMESTAMP},
H
Haojun Liao 已提交
46 47 48 49 50 51
                                                 {.name = "vgroups",          .bytes = 2,    .type = TSDB_DATA_TYPE_SMALLINT},
                                                 {.name = "ntables",          .bytes = 8,    .type = TSDB_DATA_TYPE_BIGINT},
                                                 {.name = "replica",          .bytes = 2,    .type = TSDB_DATA_TYPE_SMALLINT},
                                                 {.name = "quorum",           .bytes = 2,    .type = TSDB_DATA_TYPE_SMALLINT},
                                                 {.name = "days",             .bytes = 2,    .type = TSDB_DATA_TYPE_SMALLINT},
                                                 {.name = "keep",             .bytes = 24 + VARSTR_HEADER_SIZE,    .type = TSDB_DATA_TYPE_BINARY},
D
dapan1121 已提交
52 53 54 55
                                                 {.name = "cache",            .bytes = 4,    .type = TSDB_DATA_TYPE_INT},
                                                 {.name = "blocks",           .bytes = 4,    .type = TSDB_DATA_TYPE_INT},
                                                 {.name = "minrows",          .bytes = 4,    .type = TSDB_DATA_TYPE_INT},
                                                 {.name = "maxrows",          .bytes = 4,    .type = TSDB_DATA_TYPE_INT},
H
Haojun Liao 已提交
56
                                                 {.name = "wallevel",         .bytes = 1,    .type = TSDB_DATA_TYPE_TINYINT},
D
dapan1121 已提交
57
                                                 {.name = "fsync",            .bytes = 4,    .type = TSDB_DATA_TYPE_INT},
H
Haojun Liao 已提交
58 59 60
                                                 {.name = "comp",             .bytes = 1,    .type = TSDB_DATA_TYPE_TINYINT},
                                                 {.name = "cachelast",        .bytes = 1,    .type = TSDB_DATA_TYPE_TINYINT},
                                                 {.name = "precision",        .bytes = 3 + VARSTR_HEADER_SIZE,    .type = TSDB_DATA_TYPE_BINARY},
H
Haojun Liao 已提交
61
//                                                 {.name = "update",           .bytes = 1,   .type = TSDB_DATA_TYPE_TINYINT},  // disable update
D
dapan1121 已提交
62
                                                };
D
dapan1121 已提交
63
static const SInfosTableSchema userFuncSchema[] = {{.name = "name",           .bytes = 32,   .type = TSDB_DATA_TYPE_BINARY},
H
Haojun Liao 已提交
64
                                                   {.name = "create_time",    .bytes = 8,    .type = TSDB_DATA_TYPE_TIMESTAMP},
D
dapan1121 已提交
65 66 67
                                                   {.name = "ntables",        .bytes = 4,    .type = TSDB_DATA_TYPE_INT},
                                                   {.name = "precision",      .bytes = 2,    .type = TSDB_DATA_TYPE_BINARY},
                                                   {.name = "status",         .bytes = 10,   .type = TSDB_DATA_TYPE_BINARY},
D
dapan1121 已提交
68
                                                  };
X
Xiaoyu Wang 已提交
69
static const SInfosTableSchema userIdxSchema[] = {{.name = "db_name",          .bytes = 32,   .type = TSDB_DATA_TYPE_BINARY},
H
Haojun Liao 已提交
70
                                                  {.name = "table_name",       .bytes = SYSTABLE_SCH_DB_NAME_LEN,  .type = TSDB_DATA_TYPE_BINARY},
D
dapan1121 已提交
71
                                                  {.name = "index_database",   .bytes = 32,   .type = TSDB_DATA_TYPE_BINARY},
H
Haojun Liao 已提交
72
                                                  {.name = "index_name",       .bytes = SYSTABLE_SCH_DB_NAME_LEN,  .type = TSDB_DATA_TYPE_BINARY},
D
dapan1121 已提交
73 74 75
                                                  {.name = "column_name",      .bytes = 64,   .type = TSDB_DATA_TYPE_BINARY},
                                                  {.name = "index_type",       .bytes = 10,   .type = TSDB_DATA_TYPE_BINARY},
                                                  {.name = "index_extensions", .bytes = 256,  .type = TSDB_DATA_TYPE_BINARY},
D
dapan1121 已提交
76
                                                 };
H
Haojun Liao 已提交
77 78
static const SInfosTableSchema userStbsSchema[] = {{.name = "stable_name",     .bytes = SYSTABLE_SCH_TABLE_NAME_LEN,  .type = TSDB_DATA_TYPE_BINARY},
                                                   {.name = "db_name",         .bytes = SYSTABLE_SCH_DB_NAME_LEN,   .type = TSDB_DATA_TYPE_BINARY},
H
Haojun Liao 已提交
79
                                                   {.name = "create_time",     .bytes = 8,    .type = TSDB_DATA_TYPE_TIMESTAMP},
D
dapan1121 已提交
80 81 82
                                                   {.name = "columns",         .bytes = 4,    .type = TSDB_DATA_TYPE_INT},
                                                   {.name = "tags",            .bytes = 4,    .type = TSDB_DATA_TYPE_INT},
                                                   {.name = "tables",          .bytes = 4,    .type = TSDB_DATA_TYPE_INT},
H
Haojun Liao 已提交
83 84
                                                   {.name = "last_update",     .bytes = 8,    .type = TSDB_DATA_TYPE_TIMESTAMP},
                                                   {.name = "table_comment",   .bytes = 1024 + VARSTR_HEADER_SIZE,    .type = TSDB_DATA_TYPE_INT},
D
dapan1121 已提交
85
                                                  };
H
Haojun Liao 已提交
86
static const SInfosTableSchema userStreamsSchema[] = {{.name = "stream_name",  .bytes = SYSTABLE_SCH_DB_NAME_LEN,  .type = TSDB_DATA_TYPE_BINARY},
D
dapan1121 已提交
87
                                                      {.name = "user_name",    .bytes = 23,   .type = TSDB_DATA_TYPE_BINARY},
H
Haojun Liao 已提交
88 89
                                                      {.name = "dest_table",   .bytes = SYSTABLE_SCH_DB_NAME_LEN,  .type = TSDB_DATA_TYPE_BINARY},
                                                      {.name = "create_time",  .bytes = 8,    .type = TSDB_DATA_TYPE_TIMESTAMP},
D
dapan1121 已提交
90
                                                      {.name = "sql",          .bytes = 1024, .type = TSDB_DATA_TYPE_BINARY},
D
dapan1121 已提交
91
                                                     };
H
Haojun Liao 已提交
92
static const SInfosTableSchema userTblsSchema[] = {
H
Haojun Liao 已提交
93 94 95
                                                   {.name = "table_name",      .bytes = SYSTABLE_SCH_TABLE_NAME_LEN,  .type = TSDB_DATA_TYPE_BINARY},
                                                   {.name = "db_name",         .bytes = SYSTABLE_SCH_DB_NAME_LEN,   .type = TSDB_DATA_TYPE_BINARY},
                                                   {.name = "create_time",     .bytes = 8,    .type = TSDB_DATA_TYPE_TIMESTAMP},
D
dapan1121 已提交
96
                                                   {.name = "columns",         .bytes = 4,    .type = TSDB_DATA_TYPE_INT},
H
Haojun Liao 已提交
97
                                                   {.name = "stable_name",     .bytes = SYSTABLE_SCH_TABLE_NAME_LEN,  .type = TSDB_DATA_TYPE_BINARY},
H
Haojun Liao 已提交
98
                                                   {.name = "uid",             .bytes = 8,    .type = TSDB_DATA_TYPE_BIGINT},
H
Haojun Liao 已提交
99
                                                   {.name = "vgroup_id",       .bytes = 4,    .type = TSDB_DATA_TYPE_INT},
H
Haojun Liao 已提交
100
                                                   {.name = "ttl",             .bytes = 4,    .type = TSDB_DATA_TYPE_INT},
H
Haojun Liao 已提交
101
                                                   {.name = "table_comment",    .bytes = 4,    .type = TSDB_DATA_TYPE_INT},
D
dapan1121 已提交
102
                                                  };
D
dapan1121 已提交
103
static const SInfosTableSchema userTblDistSchema[] = {{.name = "db_name",                .bytes = 32,   .type = TSDB_DATA_TYPE_BINARY},
H
Haojun Liao 已提交
104
                                                      {.name = "table_name",             .bytes = SYSTABLE_SCH_DB_NAME_LEN,  .type = TSDB_DATA_TYPE_BINARY},
D
dapan1121 已提交
105 106 107 108 109 110 111 112 113 114 115
                                                      {.name = "distributed_histogram",  .bytes = 500,  .type = TSDB_DATA_TYPE_BINARY},
                                                      {.name = "min_of_rows",            .bytes = 4,    .type = TSDB_DATA_TYPE_INT},
                                                      {.name = "max_of_rows",            .bytes = 4,    .type = TSDB_DATA_TYPE_INT},
                                                      {.name = "avg_of_rows",            .bytes = 4,    .type = TSDB_DATA_TYPE_INT},
                                                      {.name = "stddev_of_rows",         .bytes = 4,    .type = TSDB_DATA_TYPE_INT},
                                                      {.name = "rows",                   .bytes = 8,    .type = TSDB_DATA_TYPE_BIGINT},
                                                      {.name = "blocks",                 .bytes = 4,    .type = TSDB_DATA_TYPE_INT},
                                                      {.name = "storage_size",           .bytes = 8,    .type = TSDB_DATA_TYPE_BIGINT},
                                                      {.name = "compression_ratio",      .bytes = 8,    .type = TSDB_DATA_TYPE_DOUBLE},
                                                      {.name = "rows_in_mem",            .bytes = 4,    .type = TSDB_DATA_TYPE_INT},
                                                      {.name = "seek_header_time",       .bytes = 4,    .type = TSDB_DATA_TYPE_INT},
D
dapan1121 已提交
116
                                                     };
H
Haojun Liao 已提交
117 118
static const SInfosTableSchema userUsersSchema[] = {{.name = "name",           .bytes = TSDB_USER_LEN + VARSTR_HEADER_SIZE,   .type = TSDB_DATA_TYPE_BINARY},
                                                    {.name = "privilege",      .bytes = 10 + VARSTR_HEADER_SIZE,  .type = TSDB_DATA_TYPE_BINARY},
D
dapan1121 已提交
119
                                                    {.name = "create_time",    .bytes = 8,    .type = TSDB_DATA_TYPE_TIMESTAMP},
H
Haojun Liao 已提交
120
                                                    {.name = "account",        .bytes = TSDB_USER_LEN + VARSTR_HEADER_SIZE,    .type = TSDB_DATA_TYPE_BINARY},
D
dapan1121 已提交
121
                                                   };
H
Haojun Liao 已提交
122
static const SInfosTableSchema vgroupsSchema[] = {{.name = "vgroup_id",        .bytes = 4,   .type = TSDB_DATA_TYPE_INT},
H
Haojun Liao 已提交
123
                                                  {.name = "db_name",          .bytes = SYSTABLE_SCH_DB_NAME_LEN,   .type = TSDB_DATA_TYPE_BINARY},
D
dapan1121 已提交
124
                                                  {.name = "tables",           .bytes = 4,   .type = TSDB_DATA_TYPE_INT},
H
Haojun Liao 已提交
125
                                                  {.name = "status",           .bytes = 12 + VARSTR_HEADER_SIZE,  .type = TSDB_DATA_TYPE_BINARY},
D
dapan1121 已提交
126 127 128 129 130 131 132 133
                                                  {.name = "onlines",          .bytes = 4,   .type = TSDB_DATA_TYPE_INT},
                                                  {.name = "v1_dnode",         .bytes = 4,   .type = TSDB_DATA_TYPE_INT},
                                                  {.name = "v1_status",        .bytes = 10,  .type = TSDB_DATA_TYPE_BINARY},
                                                  {.name = "v2_dnode",         .bytes = 4,   .type = TSDB_DATA_TYPE_INT},
                                                  {.name = "v2_status",        .bytes = 10,  .type = TSDB_DATA_TYPE_BINARY},
                                                  {.name = "v3_dnode",         .bytes = 4,   .type = TSDB_DATA_TYPE_INT},
                                                  {.name = "v3_status",        .bytes = 10,  .type = TSDB_DATA_TYPE_BINARY},
                                                  {.name = "compacting",       .bytes = 4,   .type = TSDB_DATA_TYPE_INT},
H
Haojun Liao 已提交
134 135
                                                  {.name = "nfiles",           .bytes = 4,   .type = TSDB_DATA_TYPE_INT},
                                                  {.name = "file_size",        .bytes = 4,   .type = TSDB_DATA_TYPE_INT},
D
dapan1121 已提交
136 137 138 139 140 141
                                                 };

static const SInfosTableMeta infosMeta[] = {{TSDB_INS_TABLE_DNODES, dnodesSchema, tListLen(dnodesSchema)},
                                            {TSDB_INS_TABLE_MNODES, mnodesSchema, tListLen(mnodesSchema)},
                                            {TSDB_INS_TABLE_MODULES, modulesSchema, tListLen(modulesSchema)},
                                            {TSDB_INS_TABLE_QNODES, qnodesSchema, tListLen(qnodesSchema)},
X
Xiaoyu Wang 已提交
142
                                            {TSDB_INS_TABLE_USER_DATABASES, userDBSchema, tListLen(userDBSchema)},
D
dapan1121 已提交
143 144 145 146 147 148 149 150 151 152
                                            {TSDB_INS_TABLE_USER_FUNCTIONS, userFuncSchema, tListLen(userFuncSchema)},
                                            {TSDB_INS_TABLE_USER_INDEXES, userIdxSchema, tListLen(userIdxSchema)},
                                            {TSDB_INS_TABLE_USER_STABLES, userStbsSchema, tListLen(userStbsSchema)},
                                            {TSDB_INS_TABLE_USER_STREAMS, userStreamsSchema, tListLen(userStreamsSchema)},
                                            {TSDB_INS_TABLE_USER_TABLES, userTblsSchema, tListLen(userTblsSchema)},
                                            {TSDB_INS_TABLE_USER_TABLE_DISTRIBUTED, userTblDistSchema, tListLen(userTblDistSchema)},
                                            {TSDB_INS_TABLE_USER_USERS, userUsersSchema, tListLen(userUsersSchema)},
                                            {TSDB_INS_TABLE_VGROUPS, vgroupsSchema, tListLen(vgroupsSchema)},
                                 };

H
Haojun Liao 已提交
153
//connection/application/
D
dapan1121 已提交
154
int32_t mndInitInfosTableSchema(const SInfosTableSchema *pSrc, int32_t colNum, SSchema **pDst) {
wafwerar's avatar
wafwerar 已提交
155
  SSchema *schema = taosMemoryCalloc(colNum, sizeof(SSchema));
D
dapan1121 已提交
156 157 158 159 160 161
  if (NULL == schema) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  for (int32_t i = 0; i < colNum; ++i) {
D
dapan1121 已提交
162
    strcpy(schema[i].name, pSrc[i].name);
D
dapan1121 已提交
163
    
D
dapan1121 已提交
164 165 166
    schema[i].type = pSrc[i].type;
    schema[i].colId = i + 1;
    schema[i].bytes = pSrc[i].bytes;
D
dapan1121 已提交
167 168 169 170 171 172 173 174 175 176
  }

  *pDst = schema;
  return TSDB_CODE_SUCCESS;
}

int32_t mndInsInitMeta(SHashObj *hash) {
  STableMetaRsp meta = {0};

  strcpy(meta.dbFName, TSDB_INFORMATION_SCHEMA_DB);
X
Xiaoyu Wang 已提交
177
  meta.tableType = TSDB_SYSTEM_TABLE;
D
dapan1121 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
  meta.sversion = 1;
  meta.tversion = 1;

  for (int32_t i = 0; i < tListLen(infosMeta); ++i) {
    strcpy(meta.tbName, infosMeta[i].name);
    meta.numOfColumns = infosMeta[i].colNum;
    
    if (mndInitInfosTableSchema(infosMeta[i].schema, infosMeta[i].colNum, &meta.pSchemas)) {
      return -1;
    }
    
    if (taosHashPut(hash, meta.tbName, strlen(meta.tbName), &meta, sizeof(meta))) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
  }

  return TSDB_CODE_SUCCESS;
}

int32_t mndBuildInsTableSchema(SMnode *pMnode, const char *dbFName, const char *tbName, STableMetaRsp *pRsp) {
  if (NULL == pMnode->infosMeta) {
    terrno = TSDB_CODE_MND_NOT_READY;
    return -1;
  }

  STableMetaRsp *meta = (STableMetaRsp *)taosHashGet(pMnode->infosMeta, tbName, strlen(tbName));
  if (NULL == meta) {
    mError("invalid information schema table name:%s", tbName);
    terrno = TSDB_CODE_MND_INVALID_INFOS_TBL;
    return -1;
  }

  *pRsp = *meta;
  
wafwerar's avatar
wafwerar 已提交
213
  pRsp->pSchemas = taosMemoryCalloc(meta->numOfColumns, sizeof(SSchema));
D
dapan1121 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
  if (pRsp->pSchemas == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    pRsp->pSchemas = NULL;
    return -1;
  }

  memcpy(pRsp->pSchemas, meta->pSchemas, meta->numOfColumns * sizeof(SSchema));

  return 0;
}

int32_t mndInitInfos(SMnode *pMnode) {
  pMnode->infosMeta = taosHashInit(20, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
  if (pMnode->infosMeta == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  return mndInsInitMeta(pMnode->infosMeta);
}

void mndCleanupInfos(SMnode *pMnode) {
  if (NULL == pMnode->infosMeta) {
    return;
  }
  
  void *pIter = taosHashIterate(pMnode->infosMeta, NULL);
  while (pIter) {
    STableMetaRsp *meta = (STableMetaRsp *)pIter;

wafwerar's avatar
wafwerar 已提交
244
    taosMemoryFreeClear(meta->pSchemas);
D
dapan1121 已提交
245 246 247 248 249 250 251 252 253 254
    
    pIter = taosHashIterate(pMnode->infosMeta, pIter);
  }

  taosHashCleanup(pMnode->infosMeta);
  pMnode->infosMeta = NULL;
}