mnodeTable.c 73.0 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

S
slguan 已提交
16
#define _DEFAULT_SOURCE
H
hjxilinx 已提交
17
#include "os.h"
S
slguan 已提交
18 19 20 21 22 23
#include "taosmsg.h"
#include "ttime.h"
#include "tutil.h"
#include "taoserror.h"
#include "taosmsg.h"
#include "tscompression.h"
S
slguan 已提交
24
#include "tname.h"
S
slguan 已提交
25
#include "tidpool.h"
S
slguan 已提交
26
#include "tglobal.h"
27 28 29
#include "tcompare.h"
#include "tdataformat.h"
#include "tgrant.h"
30
#include "hash.h"
31
#include "mnode.h"
J
jtao1735 已提交
32
#include "dnode.h"
S
Shengliang Guan 已提交
33 34 35 36 37 38 39 40
#include "mnodeDef.h"
#include "mnodeInt.h"
#include "mnodeAcct.h"
#include "mnodeDb.h"
#include "mnodeDnode.h"
#include "mnodeMnode.h"
#include "mnodeProfile.h"
#include "mnodeSdb.h"
41
#include "mnodeShow.h"
S
Shengliang Guan 已提交
42 43 44
#include "mnodeTable.h"
#include "mnodeUser.h"
#include "mnodeVgroup.h"
45 46 47
#include "mnodeWrite.h"
#include "mnodeRead.h"
#include "mnodePeer.h"
S
slguan 已提交
48

49 50
static void *  tsChildTableSdb;
static void *  tsSuperTableSdb;
S
slguan 已提交
51 52
static int32_t tsChildTableUpdateSize;
static int32_t tsSuperTableUpdateSize;
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
static void *  mnodeGetChildTable(char *tableId);
static void *  mnodeGetSuperTable(char *tableId);
static void *  mnodeGetSuperTableByUid(uint64_t uid);
static void    mnodeDropAllChildTablesInStable(SSuperTableObj *pStable);
static void    mnodeAddTableIntoStable(SSuperTableObj *pStable, SChildTableObj *pCtable);
static void    mnodeRemoveTableFromStable(SSuperTableObj *pStable, SChildTableObj *pCtable);

static int32_t mnodeGetShowTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn);
static int32_t mnodeRetrieveShowTables(SShowObj *pShow, char *data, int32_t rows, void *pConn);
static int32_t mnodeRetrieveShowSuperTables(SShowObj *pShow, char *data, int32_t rows, void *pConn);
static int32_t mnodeGetShowSuperTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn);

static int32_t mnodeProcessCreateTableMsg(SMnodeMsg *mnodeMsg);
static int32_t mnodeProcessCreateSuperTableMsg(SMnodeMsg *pMsg);
static int32_t mnodeProcessCreateChildTableMsg(SMnodeMsg *pMsg);
static void    mnodeProcessCreateChildTableRsp(SRpcMsg *rpcMsg);

static int32_t mnodeProcessDropTableMsg(SMnodeMsg *mnodeMsg);
static int32_t mnodeProcessDropSuperTableMsg(SMnodeMsg *pMsg);
static void    mnodeProcessDropSuperTableRsp(SRpcMsg *rpcMsg);
static int32_t mnodeProcessDropChildTableMsg(SMnodeMsg *pMsg);
static void    mnodeProcessDropChildTableRsp(SRpcMsg *rpcMsg);

static int32_t mnodeProcessSuperTableVgroupMsg(SMnodeMsg *mnodeMsg);
static int32_t mnodeProcessMultiTableMetaMsg(SMnodeMsg *mnodeMsg);
static int32_t mnodeProcessTableCfgMsg(SMnodeMsg *mnodeMsg);

static int32_t mnodeProcessTableMetaMsg(SMnodeMsg *mnodeMsg);
static int32_t mnodeGetSuperTableMeta(SMnodeMsg *pMsg);
static int32_t mnodeGetChildTableMeta(SMnodeMsg *pMsg);
static int32_t mgmtAutoCreateChildTable(SMnodeMsg *pMsg);

static int32_t mnodeProcessAlterTableMsg(SMnodeMsg *mnodeMsg);
static void    mnodeProcessAlterTableRsp(SRpcMsg *rpcMsg);

static int32_t mnodeFindSuperTableColumnIndex(SSuperTableObj *pStable, char *colName);
S
slguan 已提交
89

S
slguan 已提交
90
static void mgmtDestroyChildTable(SChildTableObj *pTable) {
91
  tfree(pTable->info.tableId);
S
slguan 已提交
92 93 94 95 96
  tfree(pTable->schema);
  tfree(pTable->sql);
  tfree(pTable);
}

97
static int32_t mnodeChildTableActionDestroy(SSdbOper *pOper) {
S
slguan 已提交
98 99 100 101
  mgmtDestroyChildTable(pOper->pObj);
  return TSDB_CODE_SUCCESS;
}

102
static int32_t mnodeChildTableActionInsert(SSdbOper *pOper) {
S
slguan 已提交
103 104
  SChildTableObj *pTable = pOper->pObj;

105
  SVgObj *pVgroup = mnodeGetVgroup(pTable->vgId);
S
slguan 已提交
106
  if (pVgroup == NULL) {
S
slguan 已提交
107
    mError("ctable:%s, not in vgId:%d", pTable->info.tableId, pTable->vgId);
S
slguan 已提交
108 109
    return TSDB_CODE_INVALID_VGROUP_ID;
  }
110
  mnodeDecVgroupRef(pVgroup);
S
slguan 已提交
111

112
  SDbObj *pDb = mnodeGetDb(pVgroup->dbName);
S
slguan 已提交
113
  if (pDb == NULL) {
S
slguan 已提交
114
    mError("ctable:%s, vgId:%d not in db:%s", pTable->info.tableId, pVgroup->vgId, pVgroup->dbName);
S
slguan 已提交
115 116
    return TSDB_CODE_INVALID_DB;
  }
117
  mnodeDecDbRef(pDb);
S
slguan 已提交
118

119
  SAcctObj *pAcct = mnodeGetAcct(pDb->acct);
S
slguan 已提交
120
  if (pAcct == NULL) {
S
slguan 已提交
121
    mError("ctable:%s, acct:%s not exists", pTable->info.tableId, pDb->acct);
S
slguan 已提交
122 123
    return TSDB_CODE_INVALID_ACCT;
  }
124
  mnodeDecAcctRef(pAcct);
S
slguan 已提交
125 126

  if (pTable->info.type == TSDB_CHILD_TABLE) {
127
    // add ref
128 129
    pTable->superTable = mnodeGetSuperTableByUid(pTable->suid);
    mnodeAddTableIntoStable(pTable->superTable, pTable);
S
slguan 已提交
130 131 132 133 134 135
    grantAdd(TSDB_GRANT_TIMESERIES, pTable->superTable->numOfColumns - 1);
    pAcct->acctInfo.numOfTimeSeries += (pTable->superTable->numOfColumns - 1);
  } else {
    grantAdd(TSDB_GRANT_TIMESERIES, pTable->numOfColumns - 1);
    pAcct->acctInfo.numOfTimeSeries += (pTable->numOfColumns - 1);
  }
S
slguan 已提交
136

137 138
  mnodeAddTableIntoDb(pDb);
  mnodeAddTableIntoVgroup(pVgroup, pTable);
S
slguan 已提交
139 140 141 142

  return TSDB_CODE_SUCCESS;
}

143
static int32_t mnodeChildTableActionDelete(SSdbOper *pOper) {
S
slguan 已提交
144 145 146 147 148
  SChildTableObj *pTable = pOper->pObj;
  if (pTable->vgId == 0) {
    return TSDB_CODE_INVALID_VGROUP_ID;
  }

149
  SVgObj *pVgroup = mnodeGetVgroup(pTable->vgId);
S
slguan 已提交
150 151 152
  if (pVgroup == NULL) {
    return TSDB_CODE_INVALID_VGROUP_ID;
  }
153
  mnodeDecVgroupRef(pVgroup);
S
slguan 已提交
154

155
  SDbObj *pDb = mnodeGetDb(pVgroup->dbName);
S
slguan 已提交
156
  if (pDb == NULL) {
S
slguan 已提交
157
    mError("ctable:%s, vgId:%d not in DB:%s", pTable->info.tableId, pVgroup->vgId, pVgroup->dbName);
S
slguan 已提交
158 159
    return TSDB_CODE_INVALID_DB;
  }
160
  mnodeDecDbRef(pDb);
S
slguan 已提交
161

162
  SAcctObj *pAcct = mnodeGetAcct(pDb->acct);
S
slguan 已提交
163
  if (pAcct == NULL) {
S
slguan 已提交
164
    mError("ctable:%s, acct:%s not exists", pTable->info.tableId, pDb->acct);
S
slguan 已提交
165 166
    return TSDB_CODE_INVALID_ACCT;
  }
167
  mnodeDecAcctRef(pAcct);
S
slguan 已提交
168 169 170 171

  if (pTable->info.type == TSDB_CHILD_TABLE) {
    grantRestore(TSDB_GRANT_TIMESERIES, pTable->superTable->numOfColumns - 1);
    pAcct->acctInfo.numOfTimeSeries -= (pTable->superTable->numOfColumns - 1);
172 173
    mnodeRemoveTableFromStable(pTable->superTable, pTable);
    mnodeDecTableRef(pTable->superTable);
S
slguan 已提交
174 175 176 177
  } else {
    grantRestore(TSDB_GRANT_TIMESERIES, pTable->numOfColumns - 1);
    pAcct->acctInfo.numOfTimeSeries -= (pTable->numOfColumns - 1);
  }
178 179
  mnodeRemoveTableFromDb(pDb);
  mnodeRemoveTableFromVgroup(pVgroup, pTable);
S
slguan 已提交
180 181 182 183
 
  return TSDB_CODE_SUCCESS;
}

184
static int32_t mnodeChildTableActionUpdate(SSdbOper *pOper) {
S
slguan 已提交
185
  SChildTableObj *pNew = pOper->pObj;
186
  SChildTableObj *pTable = mnodeGetChildTable(pNew->info.tableId);
S
slguan 已提交
187
  if (pTable != pNew) {
188
    void *oldTableId = pTable->info.tableId;
S
slguan 已提交
189 190 191 192 193 194 195 196
    void *oldSql = pTable->sql;
    void *oldSchema = pTable->schema;
    memcpy(pTable, pNew, pOper->rowSize);
    pTable->sql = pNew->sql;
    pTable->schema = pNew->schema;
    free(pNew);
    free(oldSql);
    free(oldSchema);
197
    free(oldTableId);
S
slguan 已提交
198
  }
199
  mnodeDecTableRef(pTable);
S
slguan 已提交
200

S
slguan 已提交
201 202 203
  return TSDB_CODE_SUCCESS;
}

204
static int32_t mnodeChildTableActionEncode(SSdbOper *pOper) {
S
slguan 已提交
205 206 207
  SChildTableObj *pTable = pOper->pObj;
  assert(pTable != NULL && pOper->rowData != NULL);

208 209 210 211 212 213 214 215 216 217 218
  int32_t len = strlen(pTable->info.tableId);
  if (len > TSDB_TABLE_ID_LEN) return TSDB_CODE_INVALID_TABLE_ID;

  memcpy(pOper->rowData, pTable->info.tableId, len);
  memset(pOper->rowData + len, 0, 1);
  len++;

  memcpy(pOper->rowData + len, (char*)pTable + sizeof(char *), tsChildTableUpdateSize);
  len += tsChildTableUpdateSize;

  if (pTable->info.type != TSDB_CHILD_TABLE) {
S
slguan 已提交
219
    int32_t schemaSize = pTable->numOfColumns * sizeof(SSchema);
220 221 222 223 224 225
    memcpy(pOper->rowData + len, pTable->schema, schemaSize);
    len += schemaSize;

    if (pTable->sqlLen != 0) {
      memcpy(pOper->rowData + len, pTable->sql, pTable->sqlLen);
      len += pTable->sqlLen;
S
slguan 已提交
226 227 228
    }
  }

229 230
  pOper->rowSize = len;

S
slguan 已提交
231 232 233
  return TSDB_CODE_SUCCESS;
}

234
static int32_t mnodeChildTableActionDecode(SSdbOper *pOper) {
S
slguan 已提交
235 236
  assert(pOper->rowData != NULL);
  SChildTableObj *pTable = calloc(1, sizeof(SChildTableObj));
237
  if (pTable == NULL) return TSDB_CODE_SERV_OUT_OF_MEMORY;
S
slguan 已提交
238

239 240 241 242 243 244 245
  int32_t len = strlen(pOper->rowData);
  if (len > TSDB_TABLE_ID_LEN) return TSDB_CODE_INVALID_TABLE_ID;
  pTable->info.tableId = strdup(pOper->rowData);
  len++;

  memcpy((char*)pTable + sizeof(char *), pOper->rowData + len, tsChildTableUpdateSize);
  len += tsChildTableUpdateSize;
S
slguan 已提交
246 247 248 249 250 251

  if (pTable->info.type != TSDB_CHILD_TABLE) {
    int32_t schemaSize = pTable->numOfColumns * sizeof(SSchema);
    pTable->schema = (SSchema *)malloc(schemaSize);
    if (pTable->schema == NULL) {
      mgmtDestroyChildTable(pTable);
252
      return TSDB_CODE_INVALID_TABLE_TYPE;
S
slguan 已提交
253
    }
254 255 256 257 258 259 260 261 262 263
    memcpy(pTable->schema, pOper->rowData + len, schemaSize);
    len += schemaSize;

    if (pTable->sqlLen != 0) {
      pTable->sql = malloc(pTable->sqlLen);
      if (pTable->sql == NULL) {
        mgmtDestroyChildTable(pTable);
        return TSDB_CODE_SERV_OUT_OF_MEMORY;
      }
      memcpy(pTable->sql, pOper->rowData + len, pTable->sqlLen);
S
slguan 已提交
264 265 266 267 268 269 270
    }
  }

  pOper->pObj = pTable;
  return TSDB_CODE_SUCCESS;
}

271
static int32_t mnodeChildTableActionRestored() {
S
Shengliang Guan 已提交
272
  void *pIter = NULL;
S
slguan 已提交
273 274 275
  SChildTableObj *pTable = NULL;

  while (1) {
276
    pIter = mnodeGetNextChildTable(pIter, &pTable);
S
slguan 已提交
277 278
    if (pTable == NULL) break;

279
    SDbObj *pDb = mnodeGetDbByTableId(pTable->info.tableId);
S
slguan 已提交
280 281
    if (pDb == NULL) {
      mError("ctable:%s, failed to get db, discard it", pTable->info.tableId);
S
Shengliang Guan 已提交
282
      SSdbOper desc = {.type = SDB_OPER_LOCAL, .pObj = pTable, .table = tsChildTableSdb};
S
slguan 已提交
283
      sdbDeleteRow(&desc);
284
      mnodeDecTableRef(pTable);
S
slguan 已提交
285 286
      continue;
    }
287
    mnodeDecDbRef(pDb);
S
slguan 已提交
288

289
    SVgObj *pVgroup = mnodeGetVgroup(pTable->vgId);
S
slguan 已提交
290
    if (pVgroup == NULL) {
S
slguan 已提交
291
      mError("ctable:%s, failed to get vgId:%d sid:%d, discard it", pTable->info.tableId, pTable->vgId, pTable->sid);
S
slguan 已提交
292
      pTable->vgId = 0;
S
Shengliang Guan 已提交
293
      SSdbOper desc = {.type = SDB_OPER_LOCAL, .pObj = pTable, .table = tsChildTableSdb};
S
slguan 已提交
294
      sdbDeleteRow(&desc);
295
      mnodeDecTableRef(pTable);
S
slguan 已提交
296 297
      continue;
    }
298
    mnodeDecVgroupRef(pVgroup);
S
slguan 已提交
299 300

    if (strcmp(pVgroup->dbName, pDb->name) != 0) {
S
slguan 已提交
301
      mError("ctable:%s, db:%s not match with vgId:%d db:%s sid:%d, discard it",
S
slguan 已提交
302 303
             pTable->info.tableId, pDb->name, pTable->vgId, pVgroup->dbName, pTable->sid);
      pTable->vgId = 0;
S
Shengliang Guan 已提交
304
      SSdbOper desc = {.type = SDB_OPER_LOCAL, .pObj = pTable, .table = tsChildTableSdb};
S
slguan 已提交
305
      sdbDeleteRow(&desc);
306
      mnodeDecTableRef(pTable);
S
slguan 已提交
307 308 309 310
      continue;
    }

    if (pVgroup->tableList == NULL) {
S
slguan 已提交
311
      mError("ctable:%s, vgId:%d tableList is null", pTable->info.tableId, pTable->vgId);
S
slguan 已提交
312
      pTable->vgId = 0;
S
Shengliang Guan 已提交
313
      SSdbOper desc = {.type = SDB_OPER_LOCAL, .pObj = pTable, .table = tsChildTableSdb};
S
slguan 已提交
314
      sdbDeleteRow(&desc);
315
      mnodeDecTableRef(pTable);
S
slguan 已提交
316 317 318 319
      continue;
    }

    if (pTable->info.type == TSDB_CHILD_TABLE) {
320
      SSuperTableObj *pSuperTable = mnodeGetSuperTableByUid(pTable->suid);
S
slguan 已提交
321
      if (pSuperTable == NULL) {
322
        mError("ctable:%s, stable:%" PRIu64 " not exist", pTable->info.tableId, pTable->suid);
S
slguan 已提交
323
        pTable->vgId = 0;
S
Shengliang Guan 已提交
324
        SSdbOper desc = {.type = SDB_OPER_LOCAL, .pObj = pTable, .table = tsChildTableSdb};
S
slguan 已提交
325
        sdbDeleteRow(&desc);
326
        mnodeDecTableRef(pTable);
S
slguan 已提交
327 328
        continue;
      }
329
      mnodeDecTableRef(pSuperTable);
S
slguan 已提交
330
    }
S
Shengliang Guan 已提交
331

332
    mnodeDecTableRef(pTable);
S
slguan 已提交
333 334
  }

S
Shengliang Guan 已提交
335 336
  sdbFreeIter(pIter);

S
slguan 已提交
337 338 339
  return 0;
}

340
static int32_t mnodeInitChildTables() {
S
slguan 已提交
341
  SChildTableObj tObj;
342
  tsChildTableUpdateSize = (int8_t *)tObj.updateEnd - (int8_t *)&tObj.info.type;
S
slguan 已提交
343 344 345 346

  SSdbTableDesc tableDesc = {
    .tableId      = SDB_TABLE_CTABLE,
    .tableName    = "ctables",
S
Shengliang Guan 已提交
347
    .hashSessions = TSDB_DEFAULT_CTABLES_HASH_SIZE,
348
    .maxRowSize   = sizeof(SChildTableObj) + sizeof(SSchema) * (TSDB_MAX_TAGS + TSDB_MAX_COLUMNS + 16) + TSDB_TABLE_ID_LEN + TSDB_CQ_SQL_SIZE,
S
slguan 已提交
349
    .refCountPos  = (int8_t *)(&tObj.refCount) - (int8_t *)&tObj,
350
    .keyType      = SDB_KEY_VAR_STRING,
351 352 353 354 355 356 357
    .insertFp     = mnodeChildTableActionInsert,
    .deleteFp     = mnodeChildTableActionDelete,
    .updateFp     = mnodeChildTableActionUpdate,
    .encodeFp     = mnodeChildTableActionEncode,
    .decodeFp     = mnodeChildTableActionDecode,
    .destroyFp    = mnodeChildTableActionDestroy,
    .restoredFp   = mnodeChildTableActionRestored
S
slguan 已提交
358 359 360 361 362 363 364 365
  };

  tsChildTableSdb = sdbOpenTable(&tableDesc);
  if (tsChildTableSdb == NULL) {
    mError("failed to init child table data");
    return -1;
  }

S
slguan 已提交
366
  mTrace("table:ctables is created");
S
slguan 已提交
367 368 369
  return 0;
}

370
static void mnodeCleanupChildTables() {
S
slguan 已提交
371 372 373
  sdbCloseTable(tsChildTableSdb);
}

374
static void mnodeAddTableIntoStable(SSuperTableObj *pStable, SChildTableObj *pCtable) {
375
  pStable->numOfTables++;
S
slguan 已提交
376

377 378
  if (pStable->vgHash == NULL) {
    pStable->vgHash = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false);
S
slguan 已提交
379 380
  }

381 382 383
  if (pStable->vgHash != NULL) {
    taosHashPut(pStable->vgHash, (char *)&pCtable->vgId, sizeof(pCtable->vgId), &pCtable->vgId, sizeof(pCtable->vgId));
  }
S
slguan 已提交
384 385
}

386
static void mnodeRemoveTableFromStable(SSuperTableObj *pStable, SChildTableObj *pCtable) {
S
slguan 已提交
387
  pStable->numOfTables--;
388 389 390

  if (pStable->vgHash == NULL) return;

391
  SVgObj *pVgroup = mnodeGetVgroup(pCtable->vgId);
392
  if (pVgroup == NULL) {
393 394
    taosHashRemove(pStable->vgHash, (char *)&pCtable->vgId, sizeof(pCtable->vgId));
  }
395
  mnodeDecVgroupRef(pVgroup);
S
slguan 已提交
396 397
}

S
slguan 已提交
398
static void mgmtDestroySuperTable(SSuperTableObj *pStable) {
399 400 401 402
  if (pStable->vgHash != NULL) {
    taosHashCleanup(pStable->vgHash);
    pStable->vgHash = NULL;
  }
403
  tfree(pStable->info.tableId);
S
slguan 已提交
404 405 406 407
  tfree(pStable->schema);
  tfree(pStable);
}

408
static int32_t mnodeSuperTableActionDestroy(SSdbOper *pOper) {
S
slguan 已提交
409 410 411 412
  mgmtDestroySuperTable(pOper->pObj);
  return TSDB_CODE_SUCCESS;
}

413
static int32_t mnodeSuperTableActionInsert(SSdbOper *pOper) {
S
slguan 已提交
414
  SSuperTableObj *pStable = pOper->pObj;
415
  SDbObj *pDb = mnodeGetDbByTableId(pStable->info.tableId);
S
slguan 已提交
416
  if (pDb != NULL) {
417
    mnodeAddSuperTableIntoDb(pDb);
S
slguan 已提交
418
  }
419
  mnodeDecDbRef(pDb);
S
slguan 已提交
420 421 422 423

  return TSDB_CODE_SUCCESS;
}

424
static int32_t mnodeSuperTableActionDelete(SSdbOper *pOper) {
S
slguan 已提交
425
  SSuperTableObj *pStable = pOper->pObj;
426
  SDbObj *pDb = mnodeGetDbByTableId(pStable->info.tableId);
S
slguan 已提交
427
  if (pDb != NULL) {
428 429
    mnodeRemoveSuperTableFromDb(pDb);
    mnodeDropAllChildTablesInStable((SSuperTableObj *)pStable);
S
slguan 已提交
430
  }
431
  mnodeDecDbRef(pDb);
S
slguan 已提交
432

S
slguan 已提交
433 434 435
  return TSDB_CODE_SUCCESS;
}

436
static int32_t mnodeSuperTableActionUpdate(SSdbOper *pOper) {
S
slguan 已提交
437
  SSuperTableObj *pNew = pOper->pObj;
438
  SSuperTableObj *pTable = mnodeGetSuperTable(pNew->info.tableId);
S
slguan 已提交
439
  if (pTable != pNew) {
440
    void *oldTableId = pTable->info.tableId;
S
slguan 已提交
441 442 443
    void *oldSchema = pTable->schema;
    memcpy(pTable, pNew, pOper->rowSize);
    pTable->schema = pNew->schema;
444
    free(pNew->vgHash);
445
    free(pNew);
446
    free(oldTableId);
S
slguan 已提交
447 448
    free(oldSchema);
  }
449
  mnodeDecTableRef(pTable);
S
slguan 已提交
450 451 452
  return TSDB_CODE_SUCCESS;
}

453
static int32_t mnodeSuperTableActionEncode(SSdbOper *pOper) {
S
slguan 已提交
454 455 456
  SSuperTableObj *pStable = pOper->pObj;
  assert(pOper->pObj != NULL && pOper->rowData != NULL);

457 458
  int32_t len = strlen(pStable->info.tableId);
  if (len > TSDB_TABLE_ID_LEN) len = TSDB_CODE_INVALID_TABLE_ID;
S
slguan 已提交
459

460 461 462 463 464 465 466 467 468 469
  memcpy(pOper->rowData, pStable->info.tableId, len);
  memset(pOper->rowData + len, 0, 1);
  len++;

  memcpy(pOper->rowData + len, (char*)pStable + sizeof(char *), tsSuperTableUpdateSize);
  len += tsSuperTableUpdateSize;

  int32_t schemaSize = sizeof(SSchema) * (pStable->numOfColumns + pStable->numOfTags);
  memcpy(pOper->rowData + len, pStable->schema, schemaSize);
  len += schemaSize;
S
slguan 已提交
470

471
  pOper->rowSize = len;
S
slguan 已提交
472 473 474 475

  return TSDB_CODE_SUCCESS;
}

476
static int32_t mnodeSuperTableActionDecode(SSdbOper *pOper) {
S
slguan 已提交
477 478 479 480
  assert(pOper->rowData != NULL);
  SSuperTableObj *pStable = (SSuperTableObj *) calloc(1, sizeof(SSuperTableObj));
  if (pStable == NULL) return TSDB_CODE_SERV_OUT_OF_MEMORY;

481 482 483 484 485 486 487
  int32_t len = strlen(pOper->rowData);
  if (len > TSDB_TABLE_ID_LEN) return TSDB_CODE_INVALID_TABLE_ID;
  pStable->info.tableId = strdup(pOper->rowData);
  len++;

  memcpy((char*)pStable + sizeof(char *), pOper->rowData + len, tsSuperTableUpdateSize);
  len += tsSuperTableUpdateSize;
S
slguan 已提交
488 489 490 491 492

  int32_t schemaSize = sizeof(SSchema) * (pStable->numOfColumns + pStable->numOfTags);
  pStable->schema = malloc(schemaSize);
  if (pStable->schema == NULL) {
    mgmtDestroySuperTable(pStable);
493
    return TSDB_CODE_NOT_SUPER_TABLE;
S
slguan 已提交
494 495
  }

496 497
  memcpy(pStable->schema, pOper->rowData + len, schemaSize);
  
S
slguan 已提交
498 499 500 501 502
  pOper->pObj = pStable;

  return TSDB_CODE_SUCCESS;
}

503
static int32_t mnodeSuperTableActionRestored() {
S
slguan 已提交
504 505 506
  return 0;
}

507
static int32_t mnodeInitSuperTables() {
S
slguan 已提交
508
  SSuperTableObj tObj;
509
  tsSuperTableUpdateSize = (int8_t *)tObj.updateEnd - (int8_t *)&tObj.info.type;
S
slguan 已提交
510 511

  SSdbTableDesc tableDesc = {
S
slguan 已提交
512
    .tableId      = SDB_TABLE_STABLE,
S
slguan 已提交
513
    .tableName    = "stables",
S
Shengliang Guan 已提交
514
    .hashSessions = TSDB_DEFAULT_STABLES_HASH_SIZE,
515
    .maxRowSize   = sizeof(SSuperTableObj) + sizeof(SSchema) * (TSDB_MAX_TAGS + TSDB_MAX_COLUMNS + 16) + TSDB_TABLE_ID_LEN,
S
slguan 已提交
516
    .refCountPos  = (int8_t *)(&tObj.refCount) - (int8_t *)&tObj,
517
    .keyType      = SDB_KEY_VAR_STRING,
518 519 520 521 522 523 524
    .insertFp     = mnodeSuperTableActionInsert,
    .deleteFp     = mnodeSuperTableActionDelete,
    .updateFp     = mnodeSuperTableActionUpdate,
    .encodeFp     = mnodeSuperTableActionEncode,
    .decodeFp     = mnodeSuperTableActionDecode,
    .destroyFp    = mnodeSuperTableActionDestroy,
    .restoredFp   = mnodeSuperTableActionRestored
S
slguan 已提交
525 526 527 528 529 530 531 532
  };

  tsSuperTableSdb = sdbOpenTable(&tableDesc);
  if (tsSuperTableSdb == NULL) {
    mError("failed to init stables data");
    return -1;
  }

S
slguan 已提交
533
  mTrace("table:stables is created");
S
slguan 已提交
534 535 536
  return 0;
}

537
static void mnodeCleanupSuperTables() {
S
slguan 已提交
538 539 540
  sdbCloseTable(tsSuperTableSdb);
}

541 542
int32_t mnodeInitTables() {
  int32_t code = mnodeInitSuperTables();
S
slguan 已提交
543 544
  if (code != TSDB_CODE_SUCCESS) {
    return code;
H
hzcheng 已提交
545 546
  }

547
  code = mnodeInitChildTables();
S
slguan 已提交
548 549 550
  if (code != TSDB_CODE_SUCCESS) {
    return code;
  }
H
hzcheng 已提交
551

552 553 554 555 556 557
  mnodeAddReadMsgHandle(TSDB_MSG_TYPE_CM_TABLES_META, mnodeProcessMultiTableMetaMsg);
  mnodeAddWriteMsgHandle(TSDB_MSG_TYPE_CM_CREATE_TABLE, mnodeProcessCreateTableMsg);
  mnodeAddWriteMsgHandle(TSDB_MSG_TYPE_CM_DROP_TABLE, mnodeProcessDropTableMsg);
  mnodeAddWriteMsgHandle(TSDB_MSG_TYPE_CM_ALTER_TABLE, mnodeProcessAlterTableMsg);
  mnodeAddReadMsgHandle(TSDB_MSG_TYPE_CM_TABLE_META, mnodeProcessTableMetaMsg);
  mnodeAddReadMsgHandle(TSDB_MSG_TYPE_CM_STABLE_VGROUP, mnodeProcessSuperTableVgroupMsg);
S
slguan 已提交
558
  
559 560 561 562
  mnodeAddPeerRspHandle(TSDB_MSG_TYPE_MD_CREATE_TABLE_RSP, mnodeProcessCreateChildTableRsp);
  mnodeAddPeerRspHandle(TSDB_MSG_TYPE_MD_DROP_TABLE_RSP, mnodeProcessDropChildTableRsp);
  mnodeAddPeerRspHandle(TSDB_MSG_TYPE_MD_DROP_STABLE_RSP, mnodeProcessDropSuperTableRsp);
  mnodeAddPeerRspHandle(TSDB_MSG_TYPE_MD_ALTER_TABLE_RSP, mnodeProcessAlterTableRsp);
S
slguan 已提交
563

564
  mnodeAddPeerMsgHandle(TSDB_MSG_TYPE_DM_CONFIG_TABLE, mnodeProcessTableCfgMsg);
S
slguan 已提交
565

566 567 568 569
  mnodeAddShowMetaHandle(TSDB_MGMT_TABLE_TABLE, mnodeGetShowTableMeta);
  mnodeAddShowRetrieveHandle(TSDB_MGMT_TABLE_TABLE, mnodeRetrieveShowTables);
  mnodeAddShowMetaHandle(TSDB_MGMT_TABLE_METRIC, mnodeGetShowSuperTableMeta);
  mnodeAddShowRetrieveHandle(TSDB_MGMT_TABLE_METRIC, mnodeRetrieveShowSuperTables);
S
slguan 已提交
570
  
S
slguan 已提交
571
  return TSDB_CODE_SUCCESS;
H
hzcheng 已提交
572 573
}

574
static void *mnodeGetChildTable(char *tableId) {
S
slguan 已提交
575 576 577
  return sdbGetRow(tsChildTableSdb, tableId);
}

578
static void *mnodeGetSuperTable(char *tableId) {
S
slguan 已提交
579 580 581
  return sdbGetRow(tsSuperTableSdb, tableId);
}

582
static void *mnodeGetSuperTableByUid(uint64_t uid) {
583
  SSuperTableObj *pStable = NULL;
S
Shengliang Guan 已提交
584
  void *pIter = NULL;
585 586

  while (1) {
587
    pIter = mnodeGetNextSuperTable(pIter, &pStable);
588 589
    if (pStable == NULL) break;
    if (pStable->uid == uid) {
590
      sdbFreeIter(pIter);
591 592
      return pStable;
    }
593
    mnodeDecTableRef(pStable);
594 595
  }

S
Shengliang Guan 已提交
596 597
  sdbFreeIter(pIter);

598 599 600
  return NULL;
}

601 602
void *mnodeGetTable(char *tableId) {
  void *pTable = mnodeGetSuperTable(tableId);
603 604
  if (pTable != NULL) {
    return pTable;
H
hzcheng 已提交
605 606
  }

607
  pTable = mnodeGetChildTable(tableId);
608 609
  if (pTable != NULL) {
    return pTable;
S
#1177  
slguan 已提交
610
  }
H
hzcheng 已提交
611

S
slguan 已提交
612
  return NULL;
S
#1177  
slguan 已提交
613
}
H
hzcheng 已提交
614

615
void *mnodeGetNextChildTable(void *pIter, SChildTableObj **pTable) {
S
Shengliang Guan 已提交
616
  return sdbFetchRow(tsChildTableSdb, pIter, (void **)pTable);
617 618
}

619
void *mnodeGetNextSuperTable(void *pIter, SSuperTableObj **pTable) {
S
Shengliang Guan 已提交
620
  return sdbFetchRow(tsSuperTableSdb, pIter, (void **)pTable);
621 622
}

623
void mnodeIncTableRef(void *p1) {
S
slguan 已提交
624
  STableObj *pTable = (STableObj *)p1;
S
slguan 已提交
625 626 627 628 629 630 631
  if (pTable->type == TSDB_SUPER_TABLE) {
    sdbIncRef(tsSuperTableSdb, pTable);
  } else {
    sdbIncRef(tsChildTableSdb, pTable);
  }
}

632
void mnodeDecTableRef(void *p1) {
S
slguan 已提交
633 634
  if (p1 == NULL) return;

S
slguan 已提交
635
  STableObj *pTable = (STableObj *)p1;
S
slguan 已提交
636 637 638 639 640 641 642
  if (pTable->type == TSDB_SUPER_TABLE) {
    sdbDecRef(tsSuperTableSdb, pTable);
  } else {
    sdbDecRef(tsChildTableSdb, pTable);
  }
}

643 644 645
void mnodeCleanupTables() {
  mnodeCleanupChildTables();
  mnodeCleanupSuperTables();
S
#1177  
slguan 已提交
646
}
H
hzcheng 已提交
647

648
// todo move to name.h, add length of table name
S
slguan 已提交
649
static void mgmtExtractTableName(char* tableId, char* name) {
650 651 652 653 654
  int pos = -1;
  int num = 0;
  for (pos = 0; tableId[pos] != 0; ++pos) {
    if (tableId[pos] == '.') num++;
    if (num == 2) break;
S
slguan 已提交
655 656
  }

657 658
  if (num == 2) {
    strcpy(name, tableId + pos + 1);
S
slguan 已提交
659
  }
H
hzcheng 已提交
660 661
}

662
static int32_t mnodeProcessCreateTableMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
663
  SCMCreateTableMsg *pCreate = pMsg->rpcMsg.pCont;
S
slguan 已提交
664
  
665
  if (pMsg->pDb == NULL) pMsg->pDb = mnodeGetDb(pCreate->db);
S
slguan 已提交
666 667
  if (pMsg->pDb == NULL || pMsg->pDb->status != TSDB_DB_STATUS_READY) {
    mError("table:%s, failed to create, db not selected", pCreate->tableId);
668
    return TSDB_CODE_DB_NOT_SELECTED;
S
slguan 已提交
669 670
  }

671
  if (pMsg->pTable == NULL) pMsg->pTable = mnodeGetTable(pCreate->tableId);
672
  if (pMsg->pTable != NULL && pMsg->retry == 0) {
S
slguan 已提交
673 674
    if (pCreate->getMeta) {
      mTrace("table:%s, continue to get meta", pCreate->tableId);
675
      return mnodeGetChildTableMeta(pMsg);
S
slguan 已提交
676
    } else if (pCreate->igExists) {
S
slguan 已提交
677
      mTrace("table:%s, is already exist", pCreate->tableId);
678
      return TSDB_CODE_SUCCESS;
S
slguan 已提交
679 680
    } else {
      mError("table:%s, failed to create, table already exist", pCreate->tableId);
681
      return TSDB_CODE_TABLE_ALREADY_EXIST;
S
slguan 已提交
682
    }
S
slguan 已提交
683 684
  }

685
  if (pCreate->numOfTags != 0) {
S
Shengliang Guan 已提交
686
    mTrace("table:%s, create msg is received from thandle:%p", pCreate->tableId, pMsg->rpcMsg.handle);
687
    return mnodeProcessCreateSuperTableMsg(pMsg);
688
  } else {
S
Shengliang Guan 已提交
689
    mTrace("table:%s, create msg is received from thandle:%p", pCreate->tableId, pMsg->rpcMsg.handle);
690
    return mnodeProcessCreateChildTableMsg(pMsg);
S
slguan 已提交
691 692 693
  }
}

694
static int32_t mnodeProcessDropTableMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
695
  SCMDropTableMsg *pDrop = pMsg->rpcMsg.pCont;
696
  if (pMsg->pDb == NULL) pMsg->pDb = mnodeGetDbByTableId(pDrop->tableId);
S
slguan 已提交
697
  if (pMsg->pDb == NULL || pMsg->pDb->status != TSDB_DB_STATUS_READY) {
S
slguan 已提交
698
    mError("table:%s, failed to drop table, db not selected", pDrop->tableId);
699
    return TSDB_CODE_DB_NOT_SELECTED;
S
slguan 已提交
700 701
  }

702
  if (mnodeCheckIsMonitorDB(pMsg->pDb->name, tsMonitorDbName)) {
703
    mError("table:%s, failed to drop table, in monitor database", pDrop->tableId);
704
    return TSDB_CODE_MONITOR_DB_FORBIDDEN;
705 706
  }

707
  if (pMsg->pTable == NULL) pMsg->pTable = mnodeGetTable(pDrop->tableId);
S
slguan 已提交
708
  if (pMsg->pTable == NULL) {
S
slguan 已提交
709 710
    if (pDrop->igNotExists) {
      mTrace("table:%s, table is not exist, think drop success", pDrop->tableId);
711
      return TSDB_CODE_SUCCESS;
S
slguan 已提交
712 713
    } else {
      mError("table:%s, failed to drop table, table not exist", pDrop->tableId);
714
      return TSDB_CODE_INVALID_TABLE;
S
slguan 已提交
715 716 717
    }
  }

S
slguan 已提交
718
  if (pMsg->pTable->type == TSDB_SUPER_TABLE) {
guanshengliang's avatar
guanshengliang 已提交
719
    mPrint("table:%s, start to drop stable", pDrop->tableId);
720
    return mnodeProcessDropSuperTableMsg(pMsg);
S
slguan 已提交
721
  } else {
guanshengliang's avatar
guanshengliang 已提交
722
    mPrint("table:%s, start to drop ctable", pDrop->tableId);
723
    return mnodeProcessDropChildTableMsg(pMsg);
S
slguan 已提交
724 725 726
  }
}

727
static int32_t mnodeProcessTableMetaMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
728
  SCMTableInfoMsg *pInfo = pMsg->rpcMsg.pCont;
S
slguan 已提交
729
  pInfo->createFlag = htons(pInfo->createFlag);
S
Shengliang Guan 已提交
730
  mTrace("table:%s, table meta msg is received from thandle:%p, createFlag:%d", pInfo->tableId, pMsg->rpcMsg.handle, pInfo->createFlag);
S
slguan 已提交
731

732
  if (pMsg->pDb == NULL) pMsg->pDb = mnodeGetDbByTableId(pInfo->tableId);
S
slguan 已提交
733
  if (pMsg->pDb == NULL || pMsg->pDb->status != TSDB_DB_STATUS_READY) {
S
slguan 已提交
734
    mError("table:%s, failed to get table meta, db not selected", pInfo->tableId);
735
    return TSDB_CODE_DB_NOT_SELECTED;
S
slguan 已提交
736 737
  }

738
  if (pMsg->pTable == NULL) pMsg->pTable = mnodeGetTable(pInfo->tableId);
S
slguan 已提交
739
  if (pMsg->pTable == NULL) {
S
slguan 已提交
740
    if (!pInfo->createFlag) {
S
slguan 已提交
741
      mError("table:%s, failed to get table meta, table not exist", pInfo->tableId);
742
      return TSDB_CODE_INVALID_TABLE;
S
slguan 已提交
743
    } else {
744
      mTrace("table:%s, failed to get table meta, start auto create table ", pInfo->tableId);
745
      return mgmtAutoCreateChildTable(pMsg);
S
slguan 已提交
746
    }
S
slguan 已提交
747
  } else {
S
slguan 已提交
748
    if (pMsg->pTable->type != TSDB_SUPER_TABLE) {
749
      return mnodeGetChildTableMeta(pMsg);
S
slguan 已提交
750
    } else {
751
      return mnodeGetSuperTableMeta(pMsg);
S
slguan 已提交
752 753 754 755
    }
  }
}

756
static int32_t mnodeProcessCreateSuperTableMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
757
  SCMCreateTableMsg *pCreate = pMsg->rpcMsg.pCont;
758
  SSuperTableObj *pStable = calloc(1, sizeof(SSuperTableObj));
S
slguan 已提交
759
  if (pStable == NULL) {
S
slguan 已提交
760
    mError("table:%s, failed to create, no enough memory", pCreate->tableId);
761
    return TSDB_CODE_SERV_OUT_OF_MEMORY;
S
slguan 已提交
762 763
  }

764
  pStable->info.tableId = strdup(pCreate->tableId);
S
slguan 已提交
765 766 767 768
  pStable->info.type    = TSDB_SUPER_TABLE;
  pStable->createdTime  = taosGetTimestampMs();
  pStable->uid          = (((uint64_t) pStable->createdTime) << 16) + (sdbGetVersion() & ((1ul << 16) - 1ul));
  pStable->sversion     = 0;
S
TD-355  
Shengliang Guan 已提交
769
  pStable->tversion     = 0;
S
slguan 已提交
770 771 772
  pStable->numOfColumns = htons(pCreate->numOfColumns);
  pStable->numOfTags    = htons(pCreate->numOfTags);

S
slguan 已提交
773
  int32_t numOfCols = pStable->numOfColumns + pStable->numOfTags;
S
slguan 已提交
774 775 776 777
  int32_t schemaSize = numOfCols * sizeof(SSchema);
  pStable->schema = (SSchema *)calloc(1, schemaSize);
  if (pStable->schema == NULL) {
    free(pStable);
S
slguan 已提交
778
    mError("table:%s, failed to create, no schema input", pCreate->tableId);
779
    return TSDB_CODE_INVALID_TABLE;
S
slguan 已提交
780 781 782 783 784 785 786 787
  }
  memcpy(pStable->schema, pCreate->schema, numOfCols * sizeof(SSchema));

  pStable->nextColId = 0;
  for (int32_t col = 0; col < numOfCols; col++) {
    SSchema *tschema = pStable->schema;
    tschema[col].colId = pStable->nextColId++;
    tschema[col].bytes = htons(tschema[col].bytes);
H
hjxilinx 已提交
788 789 790
    
    // todo 1. check the length of each column; 2. check the total length of all columns
    assert(tschema[col].type >= TSDB_DATA_TYPE_BOOL && tschema[col].type <= TSDB_DATA_TYPE_NCHAR);
S
slguan 已提交
791 792
  }

S
slguan 已提交
793
  SSdbOper oper = {
S
slguan 已提交
794
    .type = SDB_OPER_GLOBAL,
S
slguan 已提交
795 796 797 798 799 800 801 802
    .table = tsSuperTableSdb,
    .pObj = pStable,
    .rowSize = sizeof(SSuperTableObj) + schemaSize
  };

  int32_t code = sdbInsertRow(&oper);
  if (code != TSDB_CODE_SUCCESS) {
    mgmtDestroySuperTable(pStable);
S
slguan 已提交
803
    mError("table:%s, failed to create, sdb error", pCreate->tableId);
804
    return TSDB_CODE_SDB_ERROR;
S
slguan 已提交
805
  } else {
S
slguan 已提交
806
    mLPrint("table:%s, is created, tags:%d fields:%d", pStable->info.tableId, pStable->numOfTags, pStable->numOfColumns);
807
    return TSDB_CODE_SUCCESS;
S
slguan 已提交
808 809 810
  }
}

811
static int32_t mnodeProcessDropSuperTableMsg(SMnodeMsg *pMsg) {
S
slguan 已提交
812
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
S
slguan 已提交
813
  if (pStable->numOfTables != 0) {
814 815 816
    SHashMutableIterator *pIter = taosHashCreateIter(pStable->vgHash);
    while (taosHashIterNext(pIter)) {
      int32_t *pVgId = taosHashIterGet(pIter);
817
      SVgObj *pVgroup = mnodeGetVgroup(*pVgId);
guanshengliang's avatar
guanshengliang 已提交
818
      if (pVgroup == NULL) break;
819

S
slguan 已提交
820
      SMDDropSTableMsg *pDrop = rpcMallocCont(sizeof(SMDDropSTableMsg));
guanshengliang's avatar
guanshengliang 已提交
821
      pDrop->contLen = htonl(sizeof(SMDDropSTableMsg));
822
      pDrop->vgId = htonl(pVgroup->vgId);
S
slguan 已提交
823 824
      pDrop->uid = htobe64(pStable->uid);
      mgmtExtractTableName(pStable->info.tableId, pDrop->tableId);
guanshengliang's avatar
guanshengliang 已提交
825
        
826
      mPrint("stable:%s, send drop stable msg to vgId:%d", pStable->info.tableId, pVgroup->vgId);
827
      SRpcIpSet ipSet = mnodeGetIpSetFromVgroup(pVgroup);
guanshengliang's avatar
guanshengliang 已提交
828 829
      SRpcMsg rpcMsg = {.pCont = pDrop, .contLen = sizeof(SMDDropSTableMsg), .msgType = TSDB_MSG_TYPE_MD_DROP_STABLE};
      dnodeSendMsgToDnode(&ipSet, &rpcMsg);
830
      mnodeDecVgroupRef(pVgroup);
S
slguan 已提交
831
    }
832
    taosHashDestroyIter(pIter);
833

834
    mnodeDropAllChildTablesInStable(pStable);
guanshengliang's avatar
guanshengliang 已提交
835 836 837 838 839 840 841 842 843 844
  } 
  
  SSdbOper oper = {
    .type = SDB_OPER_GLOBAL,
    .table = tsSuperTableSdb,
    .pObj = pStable
  };
  
  int32_t code = sdbDeleteRow(&oper);
  mLPrint("stable:%s, is dropped from sdb, result:%s", pStable->info.tableId, tstrerror(code));
845
  return code;
S
slguan 已提交
846 847
}

848
static int32_t mnodeFindSuperTableTagIndex(SSuperTableObj *pStable, const char *tagName) {
guanshengliang's avatar
guanshengliang 已提交
849 850 851 852
  SSchema *schema = (SSchema *) pStable->schema;
  for (int32_t tag = 0; tag < pStable->numOfTags; tag++) {
    if (strcasecmp(schema[pStable->numOfColumns + tag].name, tagName) == 0) {
      return tag;
S
slguan 已提交
853 854 855 856 857 858
    }
  }

  return -1;
}

859
static int32_t mnodeAddSuperTableTag(SSuperTableObj *pStable, SSchema schema[], int32_t ntags) {
S
slguan 已提交
860
  if (pStable->numOfTags + ntags > TSDB_MAX_TAGS) {
S
slguan 已提交
861 862
    mError("stable:%s, add tag, too many tags", pStable->info.tableId);
    return TSDB_CODE_TOO_MANY_TAGS;
S
slguan 已提交
863 864
  }

S
slguan 已提交
865
  for (int32_t i = 0; i < ntags; i++) {
866
    if (mnodeFindSuperTableColumnIndex(pStable, schema[i].name) > 0) {
S
slguan 已提交
867 868 869 870
      mError("stable:%s, add tag, column:%s already exist", pStable->info.tableId, schema[i].name);
      return TSDB_CODE_TAG_ALREAY_EXIST;
    }

871
    if (mnodeFindSuperTableTagIndex(pStable, schema[i].name) > 0) {
S
slguan 已提交
872 873
      mError("stable:%s, add tag, tag:%s already exist", pStable->info.tableId, schema[i].name);
      return TSDB_CODE_FIELD_ALREAY_EXIST;
S
slguan 已提交
874 875 876 877 878 879
    }
  }

  int32_t schemaSize = sizeof(SSchema) * (pStable->numOfTags + pStable->numOfColumns);
  pStable->schema = realloc(pStable->schema, schemaSize + sizeof(SSchema) * ntags);

S
slguan 已提交
880
  memcpy(pStable->schema + pStable->numOfColumns + pStable->numOfTags, schema, sizeof(SSchema) * ntags);
S
slguan 已提交
881

S
slguan 已提交
882
  SSchema *tschema = (SSchema *)(pStable->schema + pStable->numOfColumns + pStable->numOfTags);
S
slguan 已提交
883 884 885 886
  for (int32_t i = 0; i < ntags; i++) {
    tschema[i].colId = pStable->nextColId++;
  }

S
slguan 已提交
887
  pStable->numOfTags += ntags;
S
TD-355  
Shengliang Guan 已提交
888
  pStable->tversion++;
S
slguan 已提交
889

S
slguan 已提交
890
  SSdbOper oper = {
S
slguan 已提交
891
    .type = SDB_OPER_GLOBAL,
S
[TD-17]  
slguan 已提交
892
    .table = tsSuperTableSdb,
893
    .pObj = pStable
S
[TD-17]  
slguan 已提交
894
  };
S
slguan 已提交
895

S
[TD-17]  
slguan 已提交
896 897 898 899 900
  int32_t code = sdbUpdateRow(&oper);
  if (code != TSDB_CODE_SUCCESS) {
    return TSDB_CODE_SDB_ERROR;
  }

S
slguan 已提交
901
  mPrint("stable %s, succeed to add tag %s", pStable->info.tableId, schema[0].name);
S
slguan 已提交
902 903 904
  return TSDB_CODE_SUCCESS;
}

905 906
static int32_t mnodeDropSuperTableTag(SSuperTableObj *pStable, char *tagName) {
  int32_t col = mnodeFindSuperTableTagIndex(pStable, tagName);
S
slguan 已提交
907 908 909
  if (col < 0) {
    mError("stable:%s, drop tag, tag:%s not exist", pStable->info.tableId, tagName);
    return TSDB_CODE_TAG_NOT_EXIST;
S
slguan 已提交
910
  }
S
slguan 已提交
911

S
slguan 已提交
912 913
  memmove(pStable->schema + pStable->numOfColumns + col, pStable->schema + pStable->numOfColumns + col + 1,
          sizeof(SSchema) * (pStable->numOfTags - col - 1));
S
slguan 已提交
914
  pStable->numOfTags--;
S
TD-355  
Shengliang Guan 已提交
915
  pStable->tversion++;
S
slguan 已提交
916

S
slguan 已提交
917
  SSdbOper oper = {
S
slguan 已提交
918
    .type = SDB_OPER_GLOBAL,
S
[TD-17]  
slguan 已提交
919
    .table = tsSuperTableSdb,
920
    .pObj = pStable
S
[TD-17]  
slguan 已提交
921
  };
S
slguan 已提交
922

S
[TD-17]  
slguan 已提交
923 924 925 926 927
  int32_t code = sdbUpdateRow(&oper);
  if (code != TSDB_CODE_SUCCESS) {
    return TSDB_CODE_SDB_ERROR;
  }
  
S
slguan 已提交
928
  mPrint("stable %s, succeed to drop tag %s", pStable->info.tableId, tagName);
S
slguan 已提交
929 930 931
  return TSDB_CODE_SUCCESS;
}

932 933
static int32_t mnodeModifySuperTableTagName(SSuperTableObj *pStable, char *oldTagName, char *newTagName) {
  int32_t col = mnodeFindSuperTableTagIndex(pStable, oldTagName);
S
slguan 已提交
934
  if (col < 0) {
S
slguan 已提交
935 936
    mError("stable:%s, failed to modify table tag, oldName: %s, newName: %s", pStable->info.tableId, oldTagName, newTagName);
    return TSDB_CODE_TAG_NOT_EXIST;
S
slguan 已提交
937 938 939 940
  }

  // int32_t  rowSize = 0;
  uint32_t len = strlen(newTagName);
S
slguan 已提交
941 942 943
  if (len >= TSDB_COL_NAME_LEN) {
    return TSDB_CODE_COL_NAME_TOO_LONG;
  }
S
slguan 已提交
944

945
  if (mnodeFindSuperTableTagIndex(pStable, newTagName) >= 0) {
S
slguan 已提交
946
    return TSDB_CODE_TAG_ALREAY_EXIST;
S
slguan 已提交
947 948 949
  }

  // update
S
slguan 已提交
950
  SSchema *schema = (SSchema *) (pStable->schema + pStable->numOfColumns + col);
S
slguan 已提交
951 952
  strncpy(schema->name, newTagName, TSDB_COL_NAME_LEN);

S
slguan 已提交
953
  SSdbOper oper = {
S
slguan 已提交
954
    .type = SDB_OPER_GLOBAL,
S
[TD-17]  
slguan 已提交
955
    .table = tsSuperTableSdb,
956
    .pObj = pStable
S
[TD-17]  
slguan 已提交
957
  };
S
slguan 已提交
958

S
[TD-17]  
slguan 已提交
959 960 961
  int32_t code = sdbUpdateRow(&oper);
  if (code != TSDB_CODE_SUCCESS) {
    return TSDB_CODE_SDB_ERROR;
S
slguan 已提交
962
  }
S
[TD-17]  
slguan 已提交
963
  
S
slguan 已提交
964
  mPrint("stable %s, succeed to modify tag %s to %s", pStable->info.tableId, oldTagName, newTagName);
S
slguan 已提交
965 966 967
  return TSDB_CODE_SUCCESS;
}

968
static int32_t mnodeFindSuperTableColumnIndex(SSuperTableObj *pStable, char *colName) {
S
slguan 已提交
969
  SSchema *schema = (SSchema *) pStable->schema;
S
slguan 已提交
970 971 972
  for (int32_t col = 0; col < pStable->numOfColumns; col++) {
    if (strcasecmp(schema[col].name, colName) == 0) {
      return col;
S
slguan 已提交
973 974 975 976 977 978
    }
  }

  return -1;
}

979
static int32_t mnodeAddSuperTableColumn(SDbObj *pDb, SSuperTableObj *pStable, SSchema schema[], int32_t ncols) {
S
slguan 已提交
980
  if (ncols <= 0) {
S
slguan 已提交
981
    mError("stable:%s, add column, ncols:%d <= 0", pStable->info.tableId);
S
slguan 已提交
982 983 984 985
    return TSDB_CODE_APP_ERROR;
  }

  for (int32_t i = 0; i < ncols; i++) {
986
    if (mnodeFindSuperTableColumnIndex(pStable, schema[i].name) > 0) {
S
slguan 已提交
987 988 989 990
      mError("stable:%s, add column, column:%s already exist", pStable->info.tableId, schema[i].name);
      return TSDB_CODE_FIELD_ALREAY_EXIST;
    }

991
    if (mnodeFindSuperTableTagIndex(pStable, schema[i].name) > 0) {
S
slguan 已提交
992 993
      mError("stable:%s, add column, tag:%s already exist", pStable->info.tableId, schema[i].name);
      return TSDB_CODE_TAG_ALREAY_EXIST;
S
slguan 已提交
994 995 996 997 998 999
    }
  }

  int32_t schemaSize = sizeof(SSchema) * (pStable->numOfTags + pStable->numOfColumns);
  pStable->schema = realloc(pStable->schema, schemaSize + sizeof(SSchema) * ncols);

S
slguan 已提交
1000 1001 1002
  memmove(pStable->schema + pStable->numOfColumns + ncols, pStable->schema + pStable->numOfColumns,
          sizeof(SSchema) * pStable->numOfTags);
  memcpy(pStable->schema + pStable->numOfColumns, schema, sizeof(SSchema) * ncols);
S
slguan 已提交
1003 1004 1005 1006 1007 1008 1009 1010 1011

  SSchema *tschema = (SSchema *) (pStable->schema + sizeof(SSchema) * pStable->numOfColumns);
  for (int32_t i = 0; i < ncols; i++) {
    tschema[i].colId = pStable->nextColId++;
  }

  pStable->numOfColumns += ncols;
  pStable->sversion++;

1012
  SAcctObj *pAcct = mnodeGetAcct(pDb->acct);
S
[TD-17]  
slguan 已提交
1013 1014
  if (pAcct != NULL) {
    pAcct->acctInfo.numOfTimeSeries += (ncols * pStable->numOfTables);
1015
    mnodeDecAcctRef(pAcct);
S
[TD-17]  
slguan 已提交
1016
  }
S
slguan 已提交
1017

S
slguan 已提交
1018
  SSdbOper oper = {
S
slguan 已提交
1019
    .type = SDB_OPER_GLOBAL,
S
[TD-17]  
slguan 已提交
1020
    .table = tsSuperTableSdb,
1021
    .pObj = pStable
S
[TD-17]  
slguan 已提交
1022 1023 1024 1025 1026 1027 1028
  };

  int32_t code = sdbUpdateRow(&oper);
  if (code != TSDB_CODE_SUCCESS) {
    return TSDB_CODE_SDB_ERROR;
  }
  
S
slguan 已提交
1029
  mPrint("stable %s, succeed to add column", pStable->info.tableId);
S
slguan 已提交
1030 1031 1032
  return TSDB_CODE_SUCCESS;
}

1033 1034
static int32_t mnodeDropSuperTableColumn(SDbObj *pDb, SSuperTableObj *pStable, char *colName) {
  int32_t col = mnodeFindSuperTableColumnIndex(pStable, colName);
S
slguan 已提交
1035 1036 1037
  if (col <= 0) {
    mError("stable:%s, drop column, column:%s not exist", pStable->info.tableId, colName);
    return TSDB_CODE_FIELD_NOT_EXIST;
S
slguan 已提交
1038 1039
  }

S
slguan 已提交
1040
  memmove(pStable->schema + col, pStable->schema + col + 1,
S
slguan 已提交
1041 1042 1043 1044 1045 1046 1047 1048
          sizeof(SSchema) * (pStable->numOfColumns + pStable->numOfTags - col - 1));

  pStable->numOfColumns--;
  pStable->sversion++;

  int32_t schemaSize = sizeof(SSchema) * (pStable->numOfTags + pStable->numOfColumns);
  pStable->schema = realloc(pStable->schema, schemaSize);

1049
  SAcctObj *pAcct = mnodeGetAcct(pDb->acct);
S
[TD-17]  
slguan 已提交
1050 1051
  if (pAcct != NULL) {
    pAcct->acctInfo.numOfTimeSeries -= pStable->numOfTables;
1052
    mnodeDecAcctRef(pAcct);
S
[TD-17]  
slguan 已提交
1053
  }
S
slguan 已提交
1054

S
slguan 已提交
1055
  SSdbOper oper = {
S
slguan 已提交
1056
    .type = SDB_OPER_GLOBAL,
S
[TD-17]  
slguan 已提交
1057
    .table = tsSuperTableSdb,
1058
    .pObj = pStable
S
[TD-17]  
slguan 已提交
1059 1060 1061 1062 1063 1064 1065
  };

  int32_t code = sdbUpdateRow(&oper);
  if (code != TSDB_CODE_SUCCESS) {
    return TSDB_CODE_SDB_ERROR;
  }
  
S
slguan 已提交
1066
  mPrint("stable %s, succeed to delete column", pStable->info.tableId);
S
slguan 已提交
1067 1068 1069
  return TSDB_CODE_SUCCESS;
}

S
slguan 已提交
1070
// show super tables
1071 1072
static int32_t mnodeGetShowSuperTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
  SDbObj *pDb = mnodeGetDb(pShow->db);
S
[TD-17]  
slguan 已提交
1073
  if (pDb == NULL) return TSDB_CODE_DB_NOT_SELECTED;
S
slguan 已提交
1074 1075 1076 1077

  int32_t cols = 0;
  SSchema *pSchema = pMeta->schema;

1078
  pShow->bytes[cols] = TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE;
S
slguan 已提交
1079 1080 1081 1082 1083 1084 1085
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
  strcpy(pSchema[cols].name, "name");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
1086
  strcpy(pSchema[cols].name, "created_time");
S
slguan 已提交
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 2;
  pSchema[cols].type = TSDB_DATA_TYPE_SMALLINT;
  strcpy(pSchema[cols].name, "columns");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 2;
  pSchema[cols].type = TSDB_DATA_TYPE_SMALLINT;
  strcpy(pSchema[cols].name, "tags");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 4;
  pSchema[cols].type = TSDB_DATA_TYPE_INT;
  strcpy(pSchema[cols].name, "tables");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pMeta->numOfColumns = htons(cols);
  pShow->numOfColumns = cols;

  pShow->offset[0] = 0;
  for (int32_t i = 1; i < cols; ++i) pShow->offset[i] = pShow->offset[i - 1] + pShow->bytes[i - 1];

  pShow->numOfRows = pDb->numOfSuperTables;
  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];

1117
  mnodeDecDbRef(pDb);
S
slguan 已提交
1118 1119 1120
  return 0;
}

S
slguan 已提交
1121
// retrieve super tables
1122
int32_t mnodeRetrieveShowSuperTables(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
S
slguan 已提交
1123 1124 1125 1126 1127 1128 1129
  int32_t         numOfRows = 0;
  char *          pWrite;
  int32_t         cols = 0;
  SSuperTableObj *pTable = NULL;
  char            prefix[20] = {0};
  int32_t         prefixLen;

1130
  SDbObj *pDb = mnodeGetDb(pShow->db);
S
slguan 已提交
1131 1132 1133 1134 1135 1136 1137
  if (pDb == NULL) return 0;

  strcpy(prefix, pDb->name);
  strcat(prefix, TS_PATH_DELIMITER);
  prefixLen = strlen(prefix);

  SPatternCompareInfo info = PATTERN_COMPARE_INFO_INITIALIZER;
1138
  char stableName[TSDB_TABLE_NAME_LEN + 1] = {0};
S
slguan 已提交
1139

S
slguan 已提交
1140
  while (numOfRows < rows) {    
1141
    pShow->pIter = mnodeGetNextSuperTable(pShow->pIter, &pTable);
S
slguan 已提交
1142 1143
    if (pTable == NULL) break;
    if (strncmp(pTable->info.tableId, prefix, prefixLen)) {
1144
      mnodeDecTableRef(pTable);
S
slguan 已提交
1145 1146 1147 1148 1149 1150
      continue;
    }

    memset(stableName, 0, tListLen(stableName));
    mgmtExtractTableName(pTable->info.tableId, stableName);

S
Shengliang Guan 已提交
1151
    if (pShow->payloadLen > 0 && patternMatch(pShow->payload, stableName, TSDB_TABLE_NAME_LEN, &info) != TSDB_PATTERN_MATCH) {
1152
      mnodeDecTableRef(pTable);
S
slguan 已提交
1153
      continue;
S
Shengliang Guan 已提交
1154
    }
S
slguan 已提交
1155 1156 1157 1158

    cols = 0;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
H
hjxilinx 已提交
1159 1160 1161 1162 1163 1164
  
    int16_t len = strnlen(stableName, TSDB_DB_NAME_LEN);
    *(int16_t*) pWrite = len;
    pWrite += sizeof(int16_t); // todo refactor
  
    strncpy(pWrite, stableName, len);
S
slguan 已提交
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int64_t *)pWrite = pTable->createdTime;
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int16_t *)pWrite = pTable->numOfColumns;
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int16_t *)pWrite = pTable->numOfTags;
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int32_t *)pWrite = pTable->numOfTables;
    cols++;

    numOfRows++;
1184
    mnodeDecTableRef(pTable);
S
slguan 已提交
1185 1186 1187
  }

  pShow->numOfReads += numOfRows;
1188
  mnodeDecDbRef(pDb);
S
slguan 已提交
1189

S
slguan 已提交
1190 1191 1192
  return numOfRows;
}

1193
void mnodeDropAllSuperTables(SDbObj *pDropDb) {
S
Shengliang Guan 已提交
1194
  void *  pIter= NULL;
S
slguan 已提交
1195 1196 1197 1198
  int32_t numOfTables = 0;
  int32_t dbNameLen = strlen(pDropDb->name);
  SSuperTableObj *pTable = NULL;

S
slguan 已提交
1199 1200
  mPrint("db:%s, all super tables will be dropped from sdb", pDropDb->name);

S
slguan 已提交
1201
  while (1) {
1202
    pIter = mnodeGetNextSuperTable(pIter, &pTable);
S
slguan 已提交
1203
    if (pTable == NULL) break;
S
slguan 已提交
1204 1205

    if (strncmp(pDropDb->name, pTable->info.tableId, dbNameLen) == 0) {
S
slguan 已提交
1206
      SSdbOper oper = {
S
slguan 已提交
1207
        .type = SDB_OPER_LOCAL,
S
slguan 已提交
1208 1209 1210 1211 1212 1213
        .table = tsSuperTableSdb,
        .pObj = pTable,
      };
      sdbDeleteRow(&oper);
      numOfTables ++;
    }
S
slguan 已提交
1214

1215
    mnodeDecTableRef(pTable);
S
slguan 已提交
1216
  }
S
slguan 已提交
1217

S
Shengliang Guan 已提交
1218 1219
  sdbFreeIter(pIter);

S
slguan 已提交
1220
  mPrint("db:%s, all super tables:%d is dropped from sdb", pDropDb->name, numOfTables);
S
slguan 已提交
1221 1222
}

1223
static int32_t mnodeSetSchemaFromSuperTable(SSchema *pSchema, SSuperTableObj *pTable) {
S
slguan 已提交
1224
  int32_t numOfCols = pTable->numOfColumns + pTable->numOfTags;
1225 1226
  assert(numOfCols <= TSDB_MAX_COLUMNS);
  
S
slguan 已提交
1227
  for (int32_t i = 0; i < numOfCols; ++i) {
1228
    strncpy(pSchema->name, pTable->schema[i].name, TSDB_COL_NAME_LEN);
S
slguan 已提交
1229 1230 1231 1232 1233 1234 1235 1236 1237
    pSchema->type  = pTable->schema[i].type;
    pSchema->bytes = htons(pTable->schema[i].bytes);
    pSchema->colId = htons(pTable->schema[i].colId);
    pSchema++;
  }

  return (pTable->numOfColumns + pTable->numOfTags) * sizeof(SSchema);
}

1238
static int32_t mnodeGetSuperTableMeta(SMnodeMsg *pMsg) {
S
slguan 已提交
1239
  SSuperTableObj *pTable = (SSuperTableObj *)pMsg->pTable;
1240
  STableMetaMsg *pMeta   = rpcMallocCont(sizeof(STableMetaMsg) + sizeof(SSchema) * (TSDB_MAX_TAGS + TSDB_MAX_COLUMNS + 16));
S
slguan 已提交
1241 1242
  pMeta->uid          = htobe64(pTable->uid);
  pMeta->sversion     = htons(pTable->sversion);
1243
  pMeta->tversion     = htons(pTable->tversion);
S
slguan 已提交
1244
  pMeta->precision    = pMsg->pDb->cfg.precision;
S
slguan 已提交
1245 1246 1247
  pMeta->numOfTags    = (uint8_t)pTable->numOfTags;
  pMeta->numOfColumns = htons((int16_t)pTable->numOfColumns);
  pMeta->tableType    = pTable->info.type;
1248
  pMeta->contLen      = sizeof(STableMetaMsg) + mnodeSetSchemaFromSuperTable(pMeta->schema, pTable);
H
hjxilinx 已提交
1249
  strncpy(pMeta->tableId, pTable->info.tableId, TSDB_TABLE_ID_LEN);
S
slguan 已提交
1250 1251 1252

  pMeta->contLen = htons(pMeta->contLen);

1253 1254 1255
  pMsg->rpcRsp.rsp = pMeta;
  pMsg->rpcRsp.len = pMeta->contLen;
  
S
Shengliang Guan 已提交
1256
  mTrace("stable:%s, uid:%" PRIu64 " table meta is retrieved", pTable->info.tableId, pTable->uid);
1257
  return TSDB_CODE_SUCCESS;
S
slguan 已提交
1258 1259
}

1260
static int32_t mnodeProcessSuperTableVgroupMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
1261
  SCMSTableVgroupMsg *pInfo = pMsg->rpcMsg.pCont;
H
hjxilinx 已提交
1262
  int32_t numOfTable = htonl(pInfo->numOfTables);
1263

S
scripts  
Shengliang Guan 已提交
1264
  // reserve space
S
Shengliang Guan 已提交
1265
  int32_t contLen = sizeof(SCMSTableVgroupRspMsg) + 32 * sizeof(SCMVgroupInfo) + sizeof(SVgroupsInfo); 
1266 1267
  for (int32_t i = 0; i < numOfTable; ++i) {
    char *stableName = (char*)pInfo + sizeof(SCMSTableVgroupMsg) + (TSDB_TABLE_ID_LEN) * i;
1268
    SSuperTableObj *pTable = mnodeGetSuperTable(stableName);
1269 1270 1271
    if (pTable->vgHash != NULL) {
      contLen += (taosHashGetSize(pTable->vgHash) * sizeof(SCMVgroupInfo) + sizeof(SVgroupsInfo));
    }
1272
    mnodeDecTableRef(pTable);
1273 1274
  }

S
scripts  
Shengliang Guan 已提交
1275
  SCMSTableVgroupRspMsg *pRsp = rpcMallocCont(contLen);
S
slguan 已提交
1276
  if (pRsp == NULL) {
1277
    return TSDB_CODE_SERV_OUT_OF_MEMORY;
1278
  }
1279

H
hjxilinx 已提交
1280
  pRsp->numOfTables = htonl(numOfTable);
S
Shengliang Guan 已提交
1281
  char* msg = (char*) pRsp + sizeof(SCMSTableVgroupRspMsg);
1282 1283

  for (int32_t i = 0; i < numOfTable; ++i) {
S
Shengliang Guan 已提交
1284
    char *stableName = (char*)pInfo + sizeof(SCMSTableVgroupMsg) + (TSDB_TABLE_ID_LEN) * i;
1285
    SSuperTableObj *pTable = mnodeGetSuperTable(stableName);
S
scripts  
Shengliang Guan 已提交
1286
    SVgroupsInfo *pVgroupInfo = (SVgroupsInfo *)msg;
1287 1288 1289 1290 1291

    SHashMutableIterator *pIter = taosHashCreateIter(pTable->vgHash);
    int32_t vgSize = 0;
    while (taosHashIterNext(pIter)) {
      int32_t *pVgId = taosHashIterGet(pIter);
1292
      SVgObj * pVgroup = mnodeGetVgroup(*pVgId);
S
scripts  
Shengliang Guan 已提交
1293
      if (pVgroup == NULL) continue;
1294

S
scripts  
Shengliang Guan 已提交
1295 1296 1297
      pVgroupInfo->vgroups[vgSize].vgId = htonl(pVgroup->vgId);
      for (int32_t vn = 0; vn < pVgroup->numOfVnodes; ++vn) {
        SDnodeObj *pDnode = pVgroup->vnodeGid[vn].pDnode;
H
hjxilinx 已提交
1298
        if (pDnode == NULL) break;
1299

S
scripts  
Shengliang Guan 已提交
1300
        strncpy(pVgroupInfo->vgroups[vgSize].ipAddr[vn].fqdn, pDnode->dnodeFqdn, tListLen(pDnode->dnodeFqdn));
1301
        pVgroupInfo->vgroups[vgSize].ipAddr[vn].port = htons(pDnode->dnodePort);
1302

S
scripts  
Shengliang Guan 已提交
1303
        pVgroupInfo->vgroups[vgSize].numOfIps++;
H
hjxilinx 已提交
1304
      }
1305 1306

      vgSize++;
1307
      mnodeDecVgroupRef(pVgroup);
S
slguan 已提交
1308
    }
1309

1310 1311
    taosHashDestroyIter(pIter);

S
scripts  
Shengliang Guan 已提交
1312
    pVgroupInfo->numOfVgroups = htonl(vgSize);
1313

H
hjxilinx 已提交
1314
    // one table is done, try the next table
1315
    msg += sizeof(SVgroupsInfo) + vgSize * sizeof(SCMVgroupInfo);
1316
  }
S
slguan 已提交
1317

1318 1319 1320 1321
  pMsg->rpcRsp.rsp = pRsp;
  pMsg->rpcRsp.len = msg - (char *)pRsp;

  return TSDB_CODE_SUCCESS;
S
slguan 已提交
1322 1323
}

1324
static void mnodeProcessDropSuperTableRsp(SRpcMsg *rpcMsg) {
guanshengliang's avatar
guanshengliang 已提交
1325
  mPrint("drop stable rsp received, result:%s", tstrerror(rpcMsg->code));
S
slguan 已提交
1326 1327 1328
}

static void *mgmtBuildCreateChildTableMsg(SCMCreateTableMsg *pMsg, SChildTableObj *pTable) {
1329
  STagData *  pTagData = NULL;
S
slguan 已提交
1330 1331 1332 1333
  int32_t tagDataLen = 0;
  int32_t totalCols = 0;
  int32_t contLen = 0;
  if (pTable->info.type == TSDB_CHILD_TABLE && pMsg != NULL) {
1334 1335
    pTagData = (STagData*)pMsg->schema;
    tagDataLen = ntohl(pTagData->dataLen);
S
slguan 已提交
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
    totalCols = pTable->superTable->numOfColumns + pTable->superTable->numOfTags;
    contLen = sizeof(SMDCreateTableMsg) + totalCols * sizeof(SSchema) + tagDataLen + pTable->sqlLen;
  } else {
    totalCols = pTable->numOfColumns;
    contLen = sizeof(SMDCreateTableMsg) + totalCols * sizeof(SSchema) + pTable->sqlLen;
  }

  SMDCreateTableMsg *pCreate = rpcMallocCont(contLen);
  if (pCreate == NULL) {
    terrno = TSDB_CODE_SERV_OUT_OF_MEMORY;
    return NULL;
  }

S
slguan 已提交
1349
  mgmtExtractTableName(pTable->info.tableId, pCreate->tableId);
S
slguan 已提交
1350 1351 1352 1353 1354 1355 1356 1357 1358
  pCreate->contLen       = htonl(contLen);
  pCreate->vgId          = htonl(pTable->vgId);
  pCreate->tableType     = pTable->info.type;
  pCreate->createdTime   = htobe64(pTable->createdTime);
  pCreate->sid           = htonl(pTable->sid);
  pCreate->sqlDataLen    = htonl(pTable->sqlLen);
  pCreate->uid           = htobe64(pTable->uid);
  
  if (pTable->info.type == TSDB_CHILD_TABLE) {
S
slguan 已提交
1359
    mgmtExtractTableName(pTable->superTable->info.tableId, pCreate->superTableId);
S
slguan 已提交
1360 1361 1362
    pCreate->numOfColumns  = htons(pTable->superTable->numOfColumns);
    pCreate->numOfTags     = htons(pTable->superTable->numOfTags);
    pCreate->sversion      = htonl(pTable->superTable->sversion);
1363
    pCreate->tversion      = htonl(pTable->superTable->tversion);
S
slguan 已提交
1364 1365 1366 1367 1368 1369
    pCreate->tagDataLen    = htonl(tagDataLen);
    pCreate->superTableUid = htobe64(pTable->superTable->uid);
  } else {
    pCreate->numOfColumns  = htons(pTable->numOfColumns);
    pCreate->numOfTags     = 0;
    pCreate->sversion      = htonl(pTable->sversion);
1370
    pCreate->tversion      = 0;
S
slguan 已提交
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
    pCreate->tagDataLen    = 0;
    pCreate->superTableUid = 0;
  }
 
  SSchema *pSchema = (SSchema *) pCreate->data;
  if (pTable->info.type == TSDB_CHILD_TABLE) {
    memcpy(pSchema, pTable->superTable->schema, totalCols * sizeof(SSchema));
  } else {
    memcpy(pSchema, pTable->schema, totalCols * sizeof(SSchema));
  }
  for (int32_t col = 0; col < totalCols; ++col) {
    pSchema->bytes = htons(pSchema->bytes);
    pSchema->colId = htons(pSchema->colId);
    pSchema++;
  }

  if (pTable->info.type == TSDB_CHILD_TABLE && pMsg != NULL) {
1388
    memcpy(pCreate->data + totalCols * sizeof(SSchema), pTagData->data, tagDataLen);
S
slguan 已提交
1389 1390 1391 1392 1393 1394
    memcpy(pCreate->data + totalCols * sizeof(SSchema) + tagDataLen, pTable->sql, pTable->sqlLen);
  }

  return pCreate;
}

1395
static SChildTableObj* mnodeDoCreateChildTable(SCMCreateTableMsg *pCreate, SVgObj *pVgroup, int32_t tid) {
1396
  SChildTableObj *pTable = calloc(1, sizeof(SChildTableObj));
S
slguan 已提交
1397
  if (pTable == NULL) {
S
slguan 已提交
1398
    mError("table:%s, failed to alloc memory", pCreate->tableId);
S
slguan 已提交
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
    terrno = TSDB_CODE_SERV_OUT_OF_MEMORY;
    return NULL;
  }

  if (pCreate->numOfColumns == 0) {
    pTable->info.type = TSDB_CHILD_TABLE;
  } else {
    pTable->info.type = TSDB_NORMAL_TABLE;
  }

1409 1410 1411 1412
  pTable->info.tableId = strdup(pCreate->tableId);  
  pTable->createdTime  = taosGetTimestampMs();
  pTable->sid          = tid;
  pTable->vgId         = pVgroup->vgId;
S
slguan 已提交
1413 1414
    
  if (pTable->info.type == TSDB_CHILD_TABLE) {
1415
    STagData *pTagData = (STagData *) pCreate->schema;  // it is a tag key
1416
    SSuperTableObj *pSuperTable = mnodeGetSuperTable(pTagData->name);
S
slguan 已提交
1417
    if (pSuperTable == NULL) {
1418
      mError("table:%s, corresponding super table:%s does not exist", pCreate->tableId, pTagData->name);
S
slguan 已提交
1419 1420 1421 1422
      free(pTable);
      terrno = TSDB_CODE_INVALID_TABLE;
      return NULL;
    }
1423
    mnodeDecTableRef(pSuperTable);
S
slguan 已提交
1424

1425 1426 1427 1428
    pTable->suid = pSuperTable->uid;
    pTable->uid  = (((uint64_t)pTable->vgId) << 40) + ((((uint64_t)pTable->sid) & ((1ul << 24) - 1ul)) << 16) +
                  (sdbGetVersion() & ((1ul << 16) - 1ul));
    pTable->superTable = pSuperTable;
S
slguan 已提交
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
  } else {
    pTable->uid          = (((uint64_t) pTable->createdTime) << 16) + (sdbGetVersion() & ((1ul << 16) - 1ul));
    pTable->sversion     = 0;
    pTable->numOfColumns = htons(pCreate->numOfColumns);
    pTable->sqlLen       = htons(pCreate->sqlLen);

    int32_t numOfCols  = pTable->numOfColumns;
    int32_t schemaSize = numOfCols * sizeof(SSchema);
    pTable->schema     = (SSchema *) calloc(1, schemaSize);
    if (pTable->schema == NULL) {
      free(pTable);
      terrno = TSDB_CODE_SERV_OUT_OF_MEMORY;
      return NULL;
    }
    memcpy(pTable->schema, pCreate->schema, numOfCols * sizeof(SSchema));

    pTable->nextColId = 0;
    for (int32_t col = 0; col < numOfCols; col++) {
      SSchema *tschema   = pTable->schema;
      tschema[col].colId = pTable->nextColId++;
      tschema[col].bytes = htons(tschema[col].bytes);
    }

    if (pTable->sqlLen != 0) {
      pTable->info.type = TSDB_STREAM_TABLE;
      pTable->sql = calloc(1, pTable->sqlLen);
      if (pTable->sql == NULL) {
        free(pTable);
        terrno = TSDB_CODE_SERV_OUT_OF_MEMORY;
        return NULL;
      }
      memcpy(pTable->sql, (char *) (pCreate->schema) + numOfCols * sizeof(SSchema), pTable->sqlLen);
      pTable->sql[pTable->sqlLen - 1] = 0;
      mTrace("table:%s, stream sql len:%d sql:%s", pTable->info.tableId, pTable->sqlLen, pTable->sql);
    }
  }
  
S
slguan 已提交
1466
  SSdbOper desc = {0};
S
slguan 已提交
1467
  desc.type = SDB_OPER_GLOBAL;
S
slguan 已提交
1468 1469 1470 1471 1472
  desc.pObj = pTable;
  desc.table = tsChildTableSdb;
  
  if (sdbInsertRow(&desc) != TSDB_CODE_SUCCESS) {
    free(pTable);
S
slguan 已提交
1473
    mError("table:%s, update sdb error", pCreate->tableId);
S
slguan 已提交
1474 1475 1476 1477
    terrno = TSDB_CODE_SDB_ERROR;
    return NULL;
  }

S
Shengliang Guan 已提交
1478
  mTrace("table:%s, create table in vgroup:%d, id:%d, uid:%" PRIu64 , pTable->info.tableId, pVgroup->vgId, pTable->sid, pTable->uid);
S
slguan 已提交
1479 1480 1481
  return pTable;
}

1482
static int32_t mnodeProcessCreateChildTableMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
1483
  SCMCreateTableMsg *pCreate = pMsg->rpcMsg.pCont;
S
slguan 已提交
1484 1485
  int32_t code = grantCheck(TSDB_GRANT_TIMESERIES);
  if (code != TSDB_CODE_SUCCESS) {
S
slguan 已提交
1486
    mError("table:%s, failed to create, grant timeseries failed", pCreate->tableId);
1487
    return code;
S
slguan 已提交
1488 1489
  }

1490
  SVgObj *pVgroup = mnodeGetAvailableVgroup(pMsg->pDb);
S
slguan 已提交
1491
  if (pVgroup == NULL) {
S
slguan 已提交
1492
    mTrace("table:%s, start to create a new vgroup", pCreate->tableId);
1493
    return mnodeCreateVgroup(pMsg, pMsg->pDb);
S
slguan 已提交
1494 1495
  }

1496
  if (pMsg->retry == 0) {
1497
    if (pMsg->pTable == NULL) {
1498 1499 1500
      int32_t sid = taosAllocateId(pVgroup->idPool);
      if (sid <= 0) {
        mTrace("tables:%s, no enough sid in vgId:%d", pCreate->tableId, pVgroup->vgId);
1501
        return mnodeCreateVgroup(pMsg, pMsg->pDb);
1502 1503
      }

1504
      pMsg->pTable = (STableObj *)mnodeDoCreateChildTable(pCreate, pVgroup, sid);
1505
      if (pMsg->pTable == NULL) {
1506
        return terrno;
1507 1508
      }

1509
      mnodeIncTableRef(pMsg->pTable);
1510
    }
1511
  } else {
1512
    if (pMsg->pTable == NULL) pMsg->pTable = mnodeGetTable(pCreate->tableId);
1513
  }
1514

S
slguan 已提交
1515
  if (pMsg->pTable == NULL) {
1516
    return terrno;
S
slguan 已提交
1517 1518
  }

S
slguan 已提交
1519
  SMDCreateTableMsg *pMDCreate = mgmtBuildCreateChildTableMsg(pCreate, (SChildTableObj *) pMsg->pTable);
S
slguan 已提交
1520
  if (pMDCreate == NULL) {
1521
    return terrno;
S
slguan 已提交
1522 1523
  }

1524
  SRpcIpSet ipSet = mnodeGetIpSetFromVgroup(pVgroup);
S
slguan 已提交
1525
  SRpcMsg rpcMsg = {
1526
      .handle  = pMsg,
S
slguan 已提交
1527 1528 1529 1530 1531 1532
      .pCont   = pMDCreate,
      .contLen = htonl(pMDCreate->contLen),
      .code    = 0,
      .msgType = TSDB_MSG_TYPE_MD_CREATE_TABLE
  };

J
jtao1735 已提交
1533
  dnodeSendMsgToDnode(&ipSet, &rpcMsg);
1534 1535

  return TSDB_CODE_ACTION_IN_PROGRESS;
S
slguan 已提交
1536 1537
}

1538
static int32_t mnodeProcessDropChildTableMsg(SMnodeMsg *pMsg) {
S
slguan 已提交
1539
  SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;
1540
  if (pMsg->pVgroup == NULL) pMsg->pVgroup = mnodeGetVgroup(pTable->vgId);
1541
  if (pMsg->pVgroup == NULL) {
S
slguan 已提交
1542
    mError("table:%s, failed to drop ctable, vgroup not exist", pTable->info.tableId);
1543
    return TSDB_CODE_OTHERS;
S
slguan 已提交
1544 1545 1546 1547
  }

  SMDDropTableMsg *pDrop = rpcMallocCont(sizeof(SMDDropTableMsg));
  if (pDrop == NULL) {
S
slguan 已提交
1548
    mError("table:%s, failed to drop ctable, no enough memory", pTable->info.tableId);
1549
    return TSDB_CODE_SERV_OUT_OF_MEMORY;
S
slguan 已提交
1550 1551 1552 1553 1554 1555 1556 1557
  }

  strcpy(pDrop->tableId, pTable->info.tableId);
  pDrop->vgId    = htonl(pTable->vgId);
  pDrop->contLen = htonl(sizeof(SMDDropTableMsg));
  pDrop->sid     = htonl(pTable->sid);
  pDrop->uid     = htobe64(pTable->uid);

1558
  SRpcIpSet ipSet = mnodeGetIpSetFromVgroup(pMsg->pVgroup);
S
slguan 已提交
1559

guanshengliang's avatar
guanshengliang 已提交
1560
  mPrint("table:%s, send drop ctable msg", pDrop->tableId);
S
slguan 已提交
1561
  SRpcMsg rpcMsg = {
1562
    .handle  = pMsg,
S
slguan 已提交
1563 1564 1565 1566 1567 1568
    .pCont   = pDrop,
    .contLen = sizeof(SMDDropTableMsg),
    .code    = 0,
    .msgType = TSDB_MSG_TYPE_MD_DROP_TABLE
  };

J
jtao1735 已提交
1569
  dnodeSendMsgToDnode(&ipSet, &rpcMsg);
1570 1571

  return TSDB_CODE_ACTION_IN_PROGRESS;
S
slguan 已提交
1572 1573
}

1574
static int32_t mnodeModifyChildTableTagValue(SChildTableObj *pTable, char *tagName, char *nContent) {
S
[TD-17]  
slguan 已提交
1575
  return TSDB_CODE_OPS_NOT_SUPPORT;
S
slguan 已提交
1576 1577
}

1578
static int32_t mnodeFindNormalTableColumnIndex(SChildTableObj *pTable, char *colName) {
S
slguan 已提交
1579
  SSchema *schema = (SSchema *) pTable->schema;
S
slguan 已提交
1580 1581 1582
  for (int32_t col = 0; col < pTable->numOfColumns; col++) {
    if (strcasecmp(schema[col].name, colName) == 0) {
      return col;
S
slguan 已提交
1583 1584 1585 1586 1587 1588
    }
  }

  return -1;
}

1589
static int32_t mnodeAddNormalTableColumn(SDbObj *pDb, SChildTableObj *pTable, SSchema schema[], int32_t ncols) {
S
slguan 已提交
1590
  if (ncols <= 0) {
S
slguan 已提交
1591
    mError("table:%s, add column, ncols:%d <= 0", pTable->info.tableId);
S
slguan 已提交
1592 1593 1594 1595
    return TSDB_CODE_APP_ERROR;
  }

  for (int32_t i = 0; i < ncols; i++) {
1596
    if (mnodeFindNormalTableColumnIndex(pTable, schema[i].name) > 0) {
S
slguan 已提交
1597 1598
      mError("table:%s, add column, column:%s already exist", pTable->info.tableId, schema[i].name);
      return TSDB_CODE_FIELD_ALREAY_EXIST;
S
slguan 已提交
1599 1600 1601 1602 1603 1604
    }
  }

  int32_t schemaSize = pTable->numOfColumns * sizeof(SSchema);
  pTable->schema = realloc(pTable->schema, schemaSize + sizeof(SSchema) * ncols);

S
slguan 已提交
1605
  memcpy(pTable->schema + pTable->numOfColumns, schema, sizeof(SSchema) * ncols);
S
slguan 已提交
1606

S
slguan 已提交
1607
  SSchema *tschema = (SSchema *) (pTable->schema + pTable->numOfColumns);
S
slguan 已提交
1608 1609 1610 1611 1612 1613
  for (int32_t i = 0; i < ncols; i++) {
    tschema[i].colId = pTable->nextColId++;
  }

  pTable->numOfColumns += ncols;
  pTable->sversion++;
S
[TD-17]  
slguan 已提交
1614
  
1615
  SAcctObj *pAcct = mnodeGetAcct(pDb->acct);
S
[TD-17]  
slguan 已提交
1616
  if (pAcct != NULL) {
S
slguan 已提交
1617
    pAcct->acctInfo.numOfTimeSeries += ncols;
1618
    mnodeDecAcctRef(pAcct);
S
[TD-17]  
slguan 已提交
1619 1620
  }
 
S
slguan 已提交
1621
  SSdbOper oper = {
S
slguan 已提交
1622
    .type = SDB_OPER_GLOBAL,
S
[TD-17]  
slguan 已提交
1623
    .table = tsChildTableSdb,
1624
    .pObj = pTable
S
[TD-17]  
slguan 已提交
1625
  };
S
slguan 已提交
1626

S
[TD-17]  
slguan 已提交
1627 1628 1629 1630 1631 1632
  int32_t code = sdbUpdateRow(&oper);
  if (code != TSDB_CODE_SUCCESS) {
    return TSDB_CODE_SDB_ERROR;
  }
  
  mPrint("table %s, succeed to add column", pTable->info.tableId);
S
slguan 已提交
1633 1634 1635
  return TSDB_CODE_SUCCESS;
}

1636 1637
static int32_t mnodeDropNormalTableColumn(SDbObj *pDb, SChildTableObj *pTable, char *colName) {
  int32_t col = mnodeFindNormalTableColumnIndex(pTable, colName);
S
slguan 已提交
1638 1639 1640
  if (col <= 0) {
    mError("table:%s, drop column, column:%s not exist", pTable->info.tableId, colName);
    return TSDB_CODE_FIELD_NOT_EXIST;
S
slguan 已提交
1641 1642
  }

S
slguan 已提交
1643
  memmove(pTable->schema + col, pTable->schema + col + 1, sizeof(SSchema) * (pTable->numOfColumns - col - 1));
S
slguan 已提交
1644 1645 1646
  pTable->numOfColumns--;
  pTable->sversion++;

1647
  SAcctObj *pAcct = mnodeGetAcct(pDb->acct);
S
[TD-17]  
slguan 已提交
1648 1649
  if (pAcct != NULL) {
    pAcct->acctInfo.numOfTimeSeries--;
1650
    mnodeDecAcctRef(pAcct);
S
[TD-17]  
slguan 已提交
1651 1652
  }
 
S
slguan 已提交
1653
  SSdbOper oper = {
S
slguan 已提交
1654
    .type = SDB_OPER_GLOBAL,
S
[TD-17]  
slguan 已提交
1655
    .table = tsChildTableSdb,
1656
    .pObj = pTable
S
[TD-17]  
slguan 已提交
1657
  };
S
slguan 已提交
1658

S
[TD-17]  
slguan 已提交
1659 1660 1661 1662 1663
  int32_t code = sdbUpdateRow(&oper);
  if (code != TSDB_CODE_SUCCESS) {
    return TSDB_CODE_SDB_ERROR;
  }
  
S
slguan 已提交
1664
  mPrint("table %s, succeed to drop column %s", pTable->info.tableId, colName);
S
slguan 已提交
1665 1666 1667
  return TSDB_CODE_SUCCESS;
}

1668
static int32_t mnodeSetSchemaFromNormalTable(SSchema *pSchema, SChildTableObj *pTable) {
S
slguan 已提交
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680
  int32_t numOfCols = pTable->numOfColumns;
  for (int32_t i = 0; i < numOfCols; ++i) {
    strcpy(pSchema->name, pTable->schema[i].name);
    pSchema->type  = pTable->schema[i].type;
    pSchema->bytes = htons(pTable->schema[i].bytes);
    pSchema->colId = htons(pTable->schema[i].colId);
    pSchema++;
  }

  return numOfCols * sizeof(SSchema);
}

1681
static int32_t mnodeDoGetChildTableMeta(SMnodeMsg *pMsg, STableMetaMsg *pMeta) {
S
slguan 已提交
1682 1683 1684
  SDbObj *pDb = pMsg->pDb;
  SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;

S
slguan 已提交
1685 1686 1687 1688
  pMeta->uid       = htobe64(pTable->uid);
  pMeta->sid       = htonl(pTable->sid);
  pMeta->precision = pDb->cfg.precision;
  pMeta->tableType = pTable->info.type;
S
Shuduo Sang 已提交
1689
  strncpy(pMeta->tableId, pTable->info.tableId, strlen(pTable->info.tableId));
S
slguan 已提交
1690 1691 1692

  if (pTable->info.type == TSDB_CHILD_TABLE) {
    pMeta->sversion     = htons(pTable->superTable->sversion);
1693
    pMeta->tversion     = htons(pTable->superTable->tversion);
S
slguan 已提交
1694
    pMeta->numOfTags    = (int8_t)pTable->superTable->numOfTags;
S
slguan 已提交
1695
    pMeta->numOfColumns = htons((int16_t)pTable->superTable->numOfColumns);
1696
    pMeta->contLen      = sizeof(STableMetaMsg) + mnodeSetSchemaFromSuperTable(pMeta->schema, pTable->superTable);
S
slguan 已提交
1697 1698
  } else {
    pMeta->sversion     = htons(pTable->sversion);
1699
    pMeta->tversion     = 0;
S
slguan 已提交
1700 1701
    pMeta->numOfTags    = 0;
    pMeta->numOfColumns = htons((int16_t)pTable->numOfColumns);
1702
    pMeta->contLen      = sizeof(STableMetaMsg) + mnodeSetSchemaFromNormalTable(pMeta->schema, pTable); 
S
slguan 已提交
1703 1704
  }
  
1705
  if (pMsg->pVgroup == NULL) pMsg->pVgroup = mnodeGetVgroup(pTable->vgId);
1706
  if (pMsg->pVgroup == NULL) {
S
slguan 已提交
1707 1708 1709 1710
    mError("table:%s, failed to get table meta, db not selected", pTable->info.tableId);
    return TSDB_CODE_INVALID_VGROUP_ID;
  }

1711
  for (int32_t i = 0; i < pMsg->pVgroup->numOfVnodes; ++i) {
1712
    SDnodeObj *pDnode = mnodeGetDnode(pMsg->pVgroup->vnodeGid[i].dnodeId);
S
slguan 已提交
1713
    if (pDnode == NULL) break;
J
jtao1735 已提交
1714
    strcpy(pMeta->vgroup.ipAddr[i].fqdn, pDnode->dnodeFqdn);
J
jtao1735 已提交
1715
    pMeta->vgroup.ipAddr[i].port = htons(pDnode->dnodePort + TSDB_PORT_DNODESHELL);
S
slguan 已提交
1716
    pMeta->vgroup.numOfIps++;
1717
    mnodeDecDnodeRef(pDnode);
S
slguan 已提交
1718
  }
1719
  pMeta->vgroup.vgId = htonl(pMsg->pVgroup->vgId);
S
slguan 已提交
1720 1721 1722 1723 1724 1725

  mTrace("table:%s, uid:%" PRIu64 " table meta is retrieved", pTable->info.tableId, pTable->uid);

  return TSDB_CODE_SUCCESS;
}

1726
static int32_t mgmtAutoCreateChildTable(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
1727
  SCMTableInfoMsg *pInfo = pMsg->rpcMsg.pCont;
1728 1729 1730
  STagData* pTag = (STagData*)pInfo->tags;

  int32_t contLen = sizeof(SCMCreateTableMsg) + offsetof(STagData, data) + ntohl(pTag->dataLen);
S
slguan 已提交
1731 1732 1733
  SCMCreateTableMsg *pCreateMsg = rpcMallocCont(contLen);
  if (pCreateMsg == NULL) {
    mError("table:%s, failed to create table while get meta info, no enough memory", pInfo->tableId);
1734
    return TSDB_CODE_SERV_OUT_OF_MEMORY;
S
slguan 已提交
1735
  }
S
slguan 已提交
1736

S
slguan 已提交
1737 1738 1739 1740
  strncpy(pCreateMsg->tableId, pInfo->tableId, tListLen(pInfo->tableId));
  strcpy(pCreateMsg->db, pMsg->pDb->name);
  pCreateMsg->igExists = 1;
  pCreateMsg->getMeta = 1;
S
slguan 已提交
1741
  pCreateMsg->contLen = htonl(contLen);
B
Bomin Zhang 已提交
1742

1743
  memcpy(pCreateMsg->schema, pInfo->tags, contLen - sizeof(SCMCreateTableMsg));
S
slguan 已提交
1744

S
Shengliang Guan 已提交
1745 1746 1747 1748
  rpcFreeCont(pMsg->rpcMsg.pCont);
  pMsg->rpcMsg.msgType = TSDB_MSG_TYPE_CM_CREATE_TABLE;
  pMsg->rpcMsg.pCont = pCreateMsg;
  pMsg->rpcMsg.contLen = contLen;
S
slguan 已提交
1749

S
Shengliang Guan 已提交
1750
  mTrace("table:%s, start to create on demand, stable:%s", pInfo->tableId, pInfo->tags);
1751 1752

  return TSDB_CODE_ACTION_NEED_REPROCESSED;
S
slguan 已提交
1753
}
S
slguan 已提交
1754

1755
static int32_t mnodeGetChildTableMeta(SMnodeMsg *pMsg) {
1756
  STableMetaMsg *pMeta = rpcMallocCont(sizeof(STableMetaMsg) + sizeof(SSchema) * (TSDB_MAX_TAGS + TSDB_MAX_COLUMNS + 16));
S
slguan 已提交
1757
  if (pMeta == NULL) {
S
slguan 已提交
1758
    mError("table:%s, failed to get table meta, no enough memory", pMsg->pTable->tableId);
1759
    return TSDB_CODE_SERV_OUT_OF_MEMORY;
S
slguan 已提交
1760 1761
  }

1762
  mnodeDoGetChildTableMeta(pMsg, pMeta);
S
slguan 已提交
1763

1764 1765
  pMsg->rpcRsp.len = pMeta->contLen;
  pMsg->rpcRsp.rsp = pMeta;
S
slguan 已提交
1766
  pMeta->contLen = htons(pMeta->contLen);
1767 1768

  return TSDB_CODE_SUCCESS;
S
slguan 已提交
1769 1770
}

1771
void mnodeDropAllChildTablesInVgroups(SVgObj *pVgroup) {
1772 1773 1774 1775 1776 1777 1778
  void *  pIter = NULL;
  int32_t numOfTables = 0;
  SChildTableObj *pTable = NULL;

  mPrint("vgId:%d, all child tables will be dropped from sdb", pVgroup->vgId);

  while (1) {
1779
    pIter = mnodeGetNextChildTable(pIter, &pTable);
1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790
    if (pTable == NULL) break;

    if (pTable->vgId == pVgroup->vgId) {
      SSdbOper oper = {
        .type = SDB_OPER_LOCAL,
        .table = tsChildTableSdb,
        .pObj = pTable,
      };
      sdbDeleteRow(&oper);
      numOfTables++;
    }
1791
    mnodeDecTableRef(pTable);
1792 1793 1794 1795 1796 1797 1798
  }

  sdbFreeIter(pIter);

  mPrint("vgId:%d, all child tables is dropped from sdb", pVgroup->vgId);
}

1799
void mnodeDropAllChildTables(SDbObj *pDropDb) {
S
Shengliang Guan 已提交
1800
  void *  pIter = NULL;
S
slguan 已提交
1801 1802 1803 1804
  int32_t numOfTables = 0;
  int32_t dbNameLen = strlen(pDropDb->name);
  SChildTableObj *pTable = NULL;

S
slguan 已提交
1805 1806
  mPrint("db:%s, all child tables will be dropped from sdb", pDropDb->name);

S
slguan 已提交
1807
  while (1) {
1808
    pIter = mnodeGetNextChildTable(pIter, &pTable);
S
slguan 已提交
1809
    if (pTable == NULL) break;
S
slguan 已提交
1810 1811

    if (strncmp(pDropDb->name, pTable->info.tableId, dbNameLen) == 0) {
S
slguan 已提交
1812
      SSdbOper oper = {
S
slguan 已提交
1813
        .type = SDB_OPER_LOCAL,
S
slguan 已提交
1814 1815 1816 1817 1818 1819
        .table = tsChildTableSdb,
        .pObj = pTable,
      };
      sdbDeleteRow(&oper);
      numOfTables++;
    }
1820
    mnodeDecTableRef(pTable);
S
slguan 已提交
1821 1822
  }

S
Shengliang Guan 已提交
1823 1824
  sdbFreeIter(pIter);

S
slguan 已提交
1825
  mPrint("db:%s, all child tables:%d is dropped from sdb", pDropDb->name, numOfTables);
S
slguan 已提交
1826 1827
}

1828
static void mnodeDropAllChildTablesInStable(SSuperTableObj *pStable) {
S
Shengliang Guan 已提交
1829
  void *  pIter = NULL;
S
slguan 已提交
1830 1831 1832
  int32_t numOfTables = 0;
  SChildTableObj *pTable = NULL;

S
slguan 已提交
1833 1834
  mPrint("stable:%s, all child tables will dropped from sdb", pStable->info.tableId, numOfTables);

S
slguan 已提交
1835
  while (1) {
1836
    pIter = mnodeGetNextChildTable(pIter, &pTable);
S
slguan 已提交
1837
    if (pTable == NULL) break;
S
slguan 已提交
1838 1839

    if (pTable->superTable == pStable) {
S
slguan 已提交
1840
      SSdbOper oper = {
S
slguan 已提交
1841
        .type = SDB_OPER_LOCAL,
S
slguan 已提交
1842 1843 1844 1845 1846 1847
        .table = tsChildTableSdb,
        .pObj = pTable,
      };
      sdbDeleteRow(&oper);
      numOfTables++;
    }
S
slguan 已提交
1848

1849
    mnodeDecTableRef(pTable);
S
slguan 已提交
1850 1851
  }

S
Shengliang Guan 已提交
1852 1853
  sdbFreeIter(pIter);

S
slguan 已提交
1854
  mPrint("stable:%s, all child tables:%d is dropped from sdb", pStable->info.tableId, numOfTables);
S
slguan 已提交
1855 1856
}

1857 1858
static SChildTableObj* mnodeGetTableByPos(int32_t vnode, int32_t sid) {
  SVgObj *pVgroup = mnodeGetVgroup(vnode);
1859
  if (pVgroup == NULL) return NULL;
S
slguan 已提交
1860

S
slguan 已提交
1861
  SChildTableObj *pTable = pVgroup->tableList[sid - 1];
1862
  mnodeIncTableRef((STableObj *)pTable);
1863

1864
  mnodeDecVgroupRef(pVgroup);
S
slguan 已提交
1865 1866 1867
  return pTable;
}

1868
static int32_t mnodeProcessTableCfgMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
1869
  SDMConfigTableMsg *pCfg = pMsg->rpcMsg.pCont;
S
slguan 已提交
1870 1871 1872 1873 1874
  pCfg->dnode = htonl(pCfg->dnode);
  pCfg->vnode = htonl(pCfg->vnode);
  pCfg->sid   = htonl(pCfg->sid);
  mTrace("dnode:%s, vnode:%d, sid:%d, receive table config msg", taosIpStr(pCfg->dnode), pCfg->vnode, pCfg->sid);

1875
  SChildTableObj *pTable = mnodeGetTableByPos(pCfg->vnode, pCfg->sid);
S
slguan 已提交
1876 1877
  if (pTable == NULL) {
    mError("dnode:%s, vnode:%d, sid:%d, table not found", taosIpStr(pCfg->dnode), pCfg->vnode, pCfg->sid);
1878
    return TSDB_CODE_NOT_ACTIVE_TABLE;
S
slguan 已提交
1879 1880 1881 1882 1883
  }

  SMDCreateTableMsg *pMDCreate = NULL;
  pMDCreate = mgmtBuildCreateChildTableMsg(NULL, (SChildTableObj *) pTable);
  if (pMDCreate == NULL) {
1884 1885
    mnodeDecTableRef(pTable);
    return terrno;
S
slguan 已提交
1886
  }
1887

1888 1889
  SDnodeObj *pDnode = mnodeGetDnode(pCfg->dnode);
  SRpcIpSet ipSet = mnodeGetIpSetFromIp(pDnode->dnodeEp);
S
slguan 已提交
1890 1891 1892 1893 1894 1895 1896
  SRpcMsg rpcRsp = {
      .handle  = NULL,
      .pCont   = pMDCreate,
      .contLen = htonl(pMDCreate->contLen),
      .code    = 0,
      .msgType = TSDB_MSG_TYPE_MD_CREATE_TABLE
  };
J
jtao1735 已提交
1897
  dnodeSendMsgToDnode(&ipSet, &rpcRsp);
1898

1899 1900 1901 1902
  mnodeDecTableRef(pTable);
  mnodeDecDnodeRef(pDnode);

  return TSDB_CODE_SUCCESS;
S
slguan 已提交
1903 1904
}

S
slguan 已提交
1905
// handle drop child response
1906
static void mnodeProcessDropChildTableRsp(SRpcMsg *rpcMsg) {
S
slguan 已提交
1907 1908
  if (rpcMsg->handle == NULL) return;

1909 1910
  SMnodeMsg *mnodeMsg = rpcMsg->handle;
  mnodeMsg->received++;
S
slguan 已提交
1911

S
Shengliang Guan 已提交
1912 1913 1914
  SChildTableObj *pTable = (SChildTableObj *)mnodeMsg->pTable;
  assert(pTable);
  mPrint("table:%s, drop table rsp received, thandle:%p result:%s", pTable->info.tableId, mnodeMsg->rpcMsg.handle, tstrerror(rpcMsg->code));
S
slguan 已提交
1915 1916 1917

  if (rpcMsg->code != TSDB_CODE_SUCCESS) {
    mError("table:%s, failed to drop in dnode, reason:%s", pTable->info.tableId, tstrerror(rpcMsg->code));
1918 1919
    dnodeSendRpcMnodeWriteRsp(mnodeMsg, rpcMsg->code);
    mnodeDecTableRef(pTable);
S
slguan 已提交
1920 1921 1922
    return;
  }

1923 1924
  if (mnodeMsg->pVgroup == NULL) mnodeMsg->pVgroup = mnodeGetVgroup(pTable->vgId);
  if (mnodeMsg->pVgroup == NULL) {
S
slguan 已提交
1925
    mError("table:%s, failed to get vgroup", pTable->info.tableId);
1926
    dnodeSendRpcMnodeWriteRsp(mnodeMsg, TSDB_CODE_INVALID_VGROUP_ID);
S
slguan 已提交
1927 1928 1929
    return;
  }
  
S
slguan 已提交
1930
  SSdbOper oper = {
S
slguan 已提交
1931
    .type = SDB_OPER_GLOBAL,
S
slguan 已提交
1932 1933 1934 1935 1936 1937 1938
    .table = tsChildTableSdb,
    .pObj = pTable
  };
  
  int32_t code = sdbDeleteRow(&oper);
  if (code != TSDB_CODE_SUCCESS) {
    mError("table:%s, update ctables sdb error", pTable->info.tableId);
1939
    dnodeSendRpcMnodeWriteRsp(mnodeMsg, TSDB_CODE_SDB_ERROR);
S
slguan 已提交
1940 1941 1942
    return;
  }

1943 1944 1945
  if (mnodeMsg->pVgroup->numOfTables <= 0) {
    mPrint("vgId:%d, all tables is dropped, drop vgroup", mnodeMsg->pVgroup->vgId);
    mnodeDropVgroup(mnodeMsg->pVgroup, NULL);
S
slguan 已提交
1946 1947
  }

1948
  dnodeSendRpcMnodeWriteRsp(mnodeMsg, TSDB_CODE_SUCCESS);
S
slguan 已提交
1949 1950
}

S
slguan 已提交
1951 1952
// handle create table response from dnode
// if failed, drop the table cached
1953
static void mnodeProcessCreateChildTableRsp(SRpcMsg *rpcMsg) {
S
slguan 已提交
1954 1955
  if (rpcMsg->handle == NULL) return;

1956 1957
  SMnodeMsg *mnodeMsg = rpcMsg->handle;
  mnodeMsg->received++;
S
slguan 已提交
1958

S
Shengliang Guan 已提交
1959 1960 1961
  SChildTableObj *pTable = (SChildTableObj *)mnodeMsg->pTable;
  assert(pTable);
  mTrace("table:%s, create table rsp received, thandle:%p result:%s", pTable->info.tableId, mnodeMsg->rpcMsg.handle,
1962
         tstrerror(rpcMsg->code));
S
slguan 已提交
1963 1964

  if (rpcMsg->code != TSDB_CODE_SUCCESS) {
S
Shengliang Guan 已提交
1965
    if (mnodeMsg->retry++ < 10) {
1966
      mTrace("table:%s, create table rsp received, retry:%d thandle:%p result:%s", pTable->info.tableId,
S
Shengliang Guan 已提交
1967
             mnodeMsg->retry, mnodeMsg->rpcMsg.handle, tstrerror(rpcMsg->code));
1968
      dnodeDelayReprocessMnodeWriteMsg(mnodeMsg);
1969 1970
    } else {
      mError("table:%s, failed to create in dnode, thandle:%p result:%s", pTable->info.tableId,
S
Shengliang Guan 已提交
1971
             mnodeMsg->rpcMsg.handle, tstrerror(rpcMsg->code));
1972
      
S
slguan 已提交
1973
      SSdbOper oper = {
S
slguan 已提交
1974
        .type = SDB_OPER_GLOBAL,
1975 1976 1977 1978
        .table = tsChildTableSdb,
        .pObj = pTable
      };
      sdbDeleteRow(&oper);
S
slguan 已提交
1979
    
1980
      dnodeSendRpcMnodeWriteRsp(mnodeMsg, rpcMsg->code);
1981
    }    
S
slguan 已提交
1982
  } else {
S
Shengliang Guan 已提交
1983
    mTrace("table:%s, created in dnode, thandle:%p result:%s", pTable->info.tableId, mnodeMsg->rpcMsg.handle,
1984
           tstrerror(rpcMsg->code));
S
Shengliang Guan 已提交
1985
    SCMCreateTableMsg *pCreate = mnodeMsg->rpcMsg.pCont;
S
slguan 已提交
1986 1987
    if (pCreate->getMeta) {
      mTrace("table:%s, continue to get meta", pTable->info.tableId);
1988 1989
      mnodeMsg->retry = 0;
      dnodeReprocessMnodeWriteMsg(mnodeMsg);
S
slguan 已提交
1990
    } else {
1991
      dnodeSendRpcMnodeWriteRsp(mnodeMsg, rpcMsg->code);
S
slguan 已提交
1992 1993 1994 1995
    }
  }
}

S
slguan 已提交
1996
// not implemented yet
1997
static void mnodeProcessAlterTableRsp(SRpcMsg *rpcMsg) {
1998
  mTrace("alter table rsp received, handle:%p code:%s", rpcMsg->handle, tstrerror(rpcMsg->code));
S
slguan 已提交
1999 2000
}

2001
static int32_t mnodeProcessMultiTableMetaMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
2002
  SCMMultiTableInfoMsg *pInfo = pMsg->rpcMsg.pCont;
S
slguan 已提交
2003 2004
  pInfo->numOfTables = htonl(pInfo->numOfTables);

2005
  int32_t totalMallocLen = 4 * 1024 * 1024;  // first malloc 4 MB, subsequent reallocation as twice
S
slguan 已提交
2006 2007
  SMultiTableMeta *pMultiMeta = rpcMallocCont(totalMallocLen);
  if (pMultiMeta == NULL) {
2008
    return TSDB_CODE_SERV_OUT_OF_MEMORY;
S
slguan 已提交
2009 2010 2011 2012 2013
  }

  pMultiMeta->contLen = sizeof(SMultiTableMeta);
  pMultiMeta->numOfTables = 0;

2014 2015
  for (int32_t t = 0; t < pInfo->numOfTables; ++t) {
    char * tableId = (char *)(pInfo->tableIds + t * TSDB_TABLE_ID_LEN + 1);
2016
    SChildTableObj *pTable = mnodeGetChildTable(tableId);
S
slguan 已提交
2017 2018
    if (pTable == NULL) continue;

2019
    if (pMsg->pDb == NULL) pMsg->pDb = mnodeGetDbByTableId(tableId);
2020
    if (pMsg->pDb == NULL) {
2021
      mnodeDecTableRef(pTable);
2022 2023
      continue;
    }
S
slguan 已提交
2024 2025

    int availLen = totalMallocLen - pMultiMeta->contLen;
2026
    if (availLen <= sizeof(STableMetaMsg) + sizeof(SSchema) * (TSDB_MAX_TAGS + TSDB_MAX_COLUMNS + 16)) {
2027 2028 2029
      totalMallocLen *= 2;
      pMultiMeta = rpcReallocCont(pMultiMeta, totalMallocLen);
      if (pMultiMeta == NULL) {
2030 2031
        mnodeDecTableRef(pTable);
        return TSDB_CODE_SERV_OUT_OF_MEMORY;
2032 2033
      } else {
        t--;
2034
        mnodeDecTableRef(pTable);
2035 2036
        continue;
      }
S
slguan 已提交
2037 2038 2039
    }

    STableMetaMsg *pMeta = (STableMetaMsg *)(pMultiMeta->metas + pMultiMeta->contLen);
2040
    int32_t code = mnodeDoGetChildTableMeta(pMsg, pMeta);
S
slguan 已提交
2041 2042 2043 2044
    if (code == TSDB_CODE_SUCCESS) {
      pMultiMeta->numOfTables ++;
      pMultiMeta->contLen += pMeta->contLen;
    }
2045

2046
    mnodeDecTableRef(pTable);
S
slguan 已提交
2047 2048
  }

2049 2050 2051 2052
  pMsg->rpcRsp.rsp = pMultiMeta;
  pMsg->rpcRsp.len = pMultiMeta->contLen;

  return TSDB_CODE_SUCCESS;
S
slguan 已提交
2053 2054
}

2055 2056
static int32_t mnodeGetShowTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
  SDbObj *pDb = mnodeGetDb(pShow->db);
S
slguan 已提交
2057
  if (pDb == NULL) return TSDB_CODE_DB_NOT_SELECTED;
S
slguan 已提交
2058 2059 2060 2061

  int32_t cols = 0;
  SSchema *pSchema = pMeta->schema;

2062
  pShow->bytes[cols] = TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE;
S
slguan 已提交
2063
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
2064
  strcpy(pSchema[cols].name, "table_name");
S
slguan 已提交
2065 2066 2067 2068 2069
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
2070
  strcpy(pSchema[cols].name, "created_time");
S
slguan 已提交
2071 2072 2073 2074 2075 2076 2077 2078 2079
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 2;
  pSchema[cols].type = TSDB_DATA_TYPE_SMALLINT;
  strcpy(pSchema[cols].name, "columns");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

2080
  pShow->bytes[cols] = TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE;
S
slguan 已提交
2081
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
2082
  strcpy(pSchema[cols].name, "stable_name");
S
slguan 已提交
2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pMeta->numOfColumns = htons(cols);
  pShow->numOfColumns = cols;

  pShow->offset[0] = 0;
  for (int32_t i = 1; i < cols; ++i) {
    pShow->offset[i] = pShow->offset[i - 1] + pShow->bytes[i - 1];
  }

  pShow->numOfRows = pDb->numOfTables;
  pShow->rowSize   = pShow->offset[cols - 1] + pShow->bytes[cols - 1];

2097
  mnodeDecDbRef(pDb);
S
slguan 已提交
2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108
  return 0;
}

static void mgmtVacuumResult(char *data, int32_t numOfCols, int32_t rows, int32_t capacity, SShowObj *pShow) {
  if (rows < capacity) {
    for (int32_t i = 0; i < numOfCols; ++i) {
      memmove(data + pShow->offset[i] * rows, data + pShow->offset[i] * capacity, pShow->bytes[i] * rows);
    }
  }
}

2109 2110
static int32_t mnodeRetrieveShowTables(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
  SDbObj *pDb = mnodeGetDb(pShow->db);
S
slguan 已提交
2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122
  if (pDb == NULL) return 0;

  int32_t numOfRows  = 0;
  SChildTableObj *pTable = NULL;
  SPatternCompareInfo info = PATTERN_COMPARE_INFO_INITIALIZER;

  char prefix[64] = {0};
  strcpy(prefix, pDb->name);
  strcat(prefix, TS_PATH_DELIMITER);
  int32_t prefixLen = strlen(prefix);

  while (numOfRows < rows) {
2123
    pShow->pIter = mnodeGetNextChildTable(pShow->pIter, &pTable);
S
slguan 已提交
2124 2125 2126 2127
    if (pTable == NULL) break;

    // not belong to current db
    if (strncmp(pTable->info.tableId, prefix, prefixLen)) {
2128
      mnodeDecTableRef(pTable);
S
slguan 已提交
2129 2130 2131
      continue;
    }

2132
    char tableName[TSDB_TABLE_NAME_LEN + 1] = {0};
S
slguan 已提交
2133
    
2134
    // pattern compare for table name
S
slguan 已提交
2135 2136
    mgmtExtractTableName(pTable->info.tableId, tableName);

S
Shengliang Guan 已提交
2137
    if (pShow->payloadLen > 0 && patternMatch(pShow->payload, tableName, TSDB_TABLE_NAME_LEN, &info) != TSDB_PATTERN_MATCH) {
2138
      mnodeDecTableRef(pTable);
S
slguan 已提交
2139 2140 2141 2142 2143 2144
      continue;
    }

    int32_t cols = 0;

    char *pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
H
hjxilinx 已提交
2145

2146
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, tableName, TSDB_TABLE_NAME_LEN);
S
slguan 已提交
2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int64_t *) pWrite = pTable->createdTime;
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    if (pTable->info.type == TSDB_CHILD_TABLE) {
      *(int16_t *)pWrite = pTable->superTable->numOfColumns;
    } else {
      *(int16_t *)pWrite = pTable->numOfColumns;
    }

    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
2163 2164
    
    memset(tableName, 0, tListLen(tableName));
S
slguan 已提交
2165
    if (pTable->info.type == TSDB_CHILD_TABLE) {
2166 2167
      mgmtExtractTableName(pTable->superTable->info.tableId, tableName);
      STR_WITH_MAXSIZE_TO_VARSTR(pWrite, tableName, TSDB_TABLE_NAME_LEN);
S
slguan 已提交
2168
    }
2169
    
S
slguan 已提交
2170 2171 2172
    cols++;

    numOfRows++;
2173
    mnodeDecTableRef(pTable);
S
slguan 已提交
2174 2175 2176 2177 2178 2179
  }

  pShow->numOfReads += numOfRows;
  const int32_t NUM_OF_COLUMNS = 4;

  mgmtVacuumResult(data, NUM_OF_COLUMNS, numOfRows, rows, pShow);
2180
  mnodeDecDbRef(pDb);
S
slguan 已提交
2181 2182 2183 2184

  return numOfRows;
}

2185
static int32_t mnodeProcessAlterTableMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
2186 2187
  SCMAlterTableMsg *pAlter = pMsg->rpcMsg.pCont;
  mTrace("table:%s, alter table msg is received from thandle:%p", pAlter->tableId, pMsg->rpcMsg.handle);
S
[TD-17]  
slguan 已提交
2188

2189
  if (pMsg->pDb == NULL) pMsg->pDb = mnodeGetDbByTableId(pAlter->tableId);
S
slguan 已提交
2190
  if (pMsg->pDb == NULL || pMsg->pDb->status != TSDB_DB_STATUS_READY) {
S
[TD-17]  
slguan 已提交
2191
    mError("table:%s, failed to alter table, db not selected", pAlter->tableId);
2192
    return TSDB_CODE_DB_NOT_SELECTED;
S
[TD-17]  
slguan 已提交
2193 2194
  }

2195
  if (mnodeCheckIsMonitorDB(pMsg->pDb->name, tsMonitorDbName)) {
S
[TD-17]  
slguan 已提交
2196
    mError("table:%s, failed to alter table, its log db", pAlter->tableId);
2197
    return TSDB_CODE_MONITOR_DB_FORBIDDEN;
S
[TD-17]  
slguan 已提交
2198 2199
  }

2200
  if (pMsg->pTable == NULL) pMsg->pTable = mnodeGetTable(pAlter->tableId);
S
[TD-17]  
slguan 已提交
2201 2202
  if (pMsg->pTable == NULL) {
    mError("table:%s, failed to alter table, table not exist", pMsg->pTable->tableId);
2203
    return TSDB_CODE_INVALID_TABLE;
S
[TD-17]  
slguan 已提交
2204 2205
  }

S
slguan 已提交
2206
  pAlter->type = htons(pAlter->type);
2207 2208
  pAlter->numOfCols = htons(pAlter->numOfCols);
  pAlter->tagValLen = htonl(pAlter->tagValLen);
S
slguan 已提交
2209

S
[TD-17]  
slguan 已提交
2210 2211
  if (pAlter->numOfCols > 2) {
    mError("table:%s, error numOfCols:%d in alter table", pAlter->tableId, pAlter->numOfCols);
2212
    return TSDB_CODE_APP_ERROR;
S
[TD-17]  
slguan 已提交
2213 2214 2215 2216 2217 2218
  }

  for (int32_t i = 0; i < pAlter->numOfCols; ++i) {
    pAlter->schema[i].bytes = htons(pAlter->schema[i].bytes);
  }

S
slguan 已提交
2219
  int32_t code = TSDB_CODE_OPS_NOT_SUPPORT;
S
[TD-17]  
slguan 已提交
2220 2221 2222 2223
  if (pMsg->pTable->type == TSDB_SUPER_TABLE) {
    SSuperTableObj *pTable = (SSuperTableObj *)pMsg->pTable;
    mTrace("table:%s, start to alter stable", pAlter->tableId);
    if (pAlter->type == TSDB_ALTER_TABLE_ADD_TAG_COLUMN) {
2224
      code = mnodeAddSuperTableTag(pTable, pAlter->schema, 1);
S
[TD-17]  
slguan 已提交
2225
    } else if (pAlter->type == TSDB_ALTER_TABLE_DROP_TAG_COLUMN) {
2226
      code = mnodeDropSuperTableTag(pTable, pAlter->schema[0].name);
S
[TD-17]  
slguan 已提交
2227
    } else if (pAlter->type == TSDB_ALTER_TABLE_CHANGE_TAG_COLUMN) {
2228
      code = mnodeModifySuperTableTagName(pTable, pAlter->schema[0].name, pAlter->schema[1].name);
S
[TD-17]  
slguan 已提交
2229
    } else if (pAlter->type == TSDB_ALTER_TABLE_ADD_COLUMN) {
2230
      code = mnodeAddSuperTableColumn(pMsg->pDb, pTable, pAlter->schema, 1);
S
[TD-17]  
slguan 已提交
2231
    } else if (pAlter->type == TSDB_ALTER_TABLE_DROP_COLUMN) {
2232
      code = mnodeDropSuperTableColumn(pMsg->pDb, pTable, pAlter->schema[0].name);
S
[TD-17]  
slguan 已提交
2233 2234
    } else {
    }
S
slguan 已提交
2235
  } else {
S
[TD-17]  
slguan 已提交
2236 2237 2238
    mTrace("table:%s, start to alter ctable", pAlter->tableId);
    SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;
    if (pAlter->type == TSDB_ALTER_TABLE_UPDATE_TAG_VAL) {
2239
      char *tagVal = (char*)(pAlter->schema + pAlter->numOfCols);
2240
      code = mnodeModifyChildTableTagValue(pTable, pAlter->schema[0].name, tagVal);
S
[TD-17]  
slguan 已提交
2241
    } else if (pAlter->type == TSDB_ALTER_TABLE_ADD_COLUMN) {
2242
      code = mnodeAddNormalTableColumn(pMsg->pDb, pTable, pAlter->schema, 1);
S
[TD-17]  
slguan 已提交
2243
    } else if (pAlter->type == TSDB_ALTER_TABLE_DROP_COLUMN) {
2244
      code = mnodeDropNormalTableColumn(pMsg->pDb, pTable, pAlter->schema[0].name);
S
[TD-17]  
slguan 已提交
2245 2246
    } else {
    }
S
slguan 已提交
2247 2248
  }

2249
  return code;
S
[TD-17]  
slguan 已提交
2250
}