mnodeTable.c 96.6 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
#include "taosmsg.h"
#include "tutil.h"
#include "taoserror.h"
#include "taosmsg.h"
#include "tscompression.h"
S
slguan 已提交
23
#include "tname.h"
S
slguan 已提交
24
#include "tidpool.h"
S
slguan 已提交
25
#include "tglobal.h"
26 27 28
#include "tcompare.h"
#include "tdataformat.h"
#include "tgrant.h"
29
#include "hash.h"
30
#include "mnode.h"
J
jtao1735 已提交
31
#include "dnode.h"
S
Shengliang Guan 已提交
32 33 34 35 36 37 38 39
#include "mnodeDef.h"
#include "mnodeInt.h"
#include "mnodeAcct.h"
#include "mnodeDb.h"
#include "mnodeDnode.h"
#include "mnodeMnode.h"
#include "mnodeProfile.h"
#include "mnodeSdb.h"
40
#include "mnodeShow.h"
S
Shengliang Guan 已提交
41 42 43
#include "mnodeTable.h"
#include "mnodeUser.h"
#include "mnodeVgroup.h"
44 45 46
#include "mnodeWrite.h"
#include "mnodeRead.h"
#include "mnodePeer.h"
S
slguan 已提交
47

48 49
static void *  tsChildTableSdb;
static void *  tsSuperTableSdb;
S
slguan 已提交
50 51
static int32_t tsChildTableUpdateSize;
static int32_t tsSuperTableUpdateSize;
52 53 54 55 56 57 58 59 60 61
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 mnodeGetShowSuperTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn);
62
static int32_t mnodeRetrieveShowSuperTables(SShowObj *pShow, char *data, int32_t rows, void *pConn);
S
Shengliang Guan 已提交
63 64
static int32_t mnodeGetStreamTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn);
static int32_t mnodeRetrieveStreamTables(SShowObj *pShow, char *data, int32_t rows, void *pConn);
65
 
H
Haojun Liao 已提交
66
static int32_t mnodeProcessCreateTableMsg(SMnodeMsg *pMsg);
67 68 69 70
static int32_t mnodeProcessCreateSuperTableMsg(SMnodeMsg *pMsg);
static int32_t mnodeProcessCreateChildTableMsg(SMnodeMsg *pMsg);
static void    mnodeProcessCreateChildTableRsp(SRpcMsg *rpcMsg);

H
Haojun Liao 已提交
71
static int32_t mnodeProcessDropTableMsg(SMnodeMsg *pMsg);
72 73
static int32_t mnodeProcessDropSuperTableMsg(SMnodeMsg *pMsg);
static void    mnodeProcessDropSuperTableRsp(SRpcMsg *rpcMsg);
S
Shengliang Guan 已提交
74
static int32_t mnodeProcessDropChildTableMsg(SMnodeMsg *pMsg);
75 76
static void    mnodeProcessDropChildTableRsp(SRpcMsg *rpcMsg);

H
Haojun Liao 已提交
77 78 79
static int32_t mnodeProcessSuperTableVgroupMsg(SMnodeMsg *pMsg);
static int32_t mnodeProcessMultiTableMetaMsg(SMnodeMsg *pMsg);
static int32_t mnodeProcessTableCfgMsg(SMnodeMsg *pMsg);
80

H
Haojun Liao 已提交
81
static int32_t mnodeProcessTableMetaMsg(SMnodeMsg *pMsg);
82 83
static int32_t mnodeGetSuperTableMeta(SMnodeMsg *pMsg);
static int32_t mnodeGetChildTableMeta(SMnodeMsg *pMsg);
S
Shengliang Guan 已提交
84
static int32_t mnodeAutoCreateChildTable(SMnodeMsg *pMsg);
85

H
Haojun Liao 已提交
86
static int32_t mnodeProcessAlterTableMsg(SMnodeMsg *pMsg);
87 88 89
static void    mnodeProcessAlterTableRsp(SRpcMsg *rpcMsg);

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

S
Shengliang Guan 已提交
91
static void mnodeDestroyChildTable(SChildTableObj *pTable) {
S
Shengliang Guan 已提交
92 93 94 95
  taosTFree(pTable->info.tableId);
  taosTFree(pTable->schema);
  taosTFree(pTable->sql);
  taosTFree(pTable);
S
slguan 已提交
96 97
}

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

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

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

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

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

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

140 141 142 143 144 145
  if (pDb) mnodeAddTableIntoDb(pDb);
  if (pVgroup) mnodeAddTableIntoVgroup(pVgroup, pTable);

  mnodeDecVgroupRef(pVgroup);
  mnodeDecDbRef(pDb);
  mnodeDecAcctRef(pAcct);
S
slguan 已提交
146 147 148 149

  return TSDB_CODE_SUCCESS;
}

150
static int32_t mnodeChildTableActionDelete(SSdbOper *pOper) {
S
slguan 已提交
151 152
  SChildTableObj *pTable = pOper->pObj;
  if (pTable->vgId == 0) {
153
    return TSDB_CODE_MND_VGROUP_NOT_EXIST;
S
slguan 已提交
154 155
  }

156 157 158 159 160 161 162
  SVgObj *pVgroup = NULL;
  SDbObj *pDb = NULL;
  SAcctObj *pAcct = NULL;
  
  pVgroup = mnodeGetVgroup(pTable->vgId);
  if (pVgroup != NULL) pDb = mnodeGetDb(pVgroup->dbName);
  if (pDb != NULL) pAcct = mnodeGetAcct(pDb->acct);
S
slguan 已提交
163 164

  if (pTable->info.type == TSDB_CHILD_TABLE) {
165 166 167 168 169 170
    if (pTable->superTable) {
      grantRestore(TSDB_GRANT_TIMESERIES, pTable->superTable->numOfColumns - 1);
      if (pAcct != NULL) pAcct->acctInfo.numOfTimeSeries -= (pTable->superTable->numOfColumns - 1);
      mnodeRemoveTableFromStable(pTable->superTable, pTable);
      mnodeDecTableRef(pTable->superTable);
    }
S
slguan 已提交
171 172
  } else {
    grantRestore(TSDB_GRANT_TIMESERIES, pTable->numOfColumns - 1);
173
    if (pAcct != NULL) pAcct->acctInfo.numOfTimeSeries -= (pTable->numOfColumns - 1);
S
slguan 已提交
174
  }
175 176 177 178 179 180 181
  
  if (pDb != NULL) mnodeRemoveTableFromDb(pDb);
  if (pVgroup != NULL) mnodeRemoveTableFromVgroup(pVgroup, pTable);

  mnodeDecVgroupRef(pVgroup);
  mnodeDecDbRef(pDb);
  mnodeDecAcctRef(pAcct);
S
slguan 已提交
182 183 184 185
 
  return TSDB_CODE_SUCCESS;
}

186
static int32_t mnodeChildTableActionUpdate(SSdbOper *pOper) {
S
slguan 已提交
187
  SChildTableObj *pNew = pOper->pObj;
188
  SChildTableObj *pTable = mnodeGetChildTable(pNew->info.tableId);
S
slguan 已提交
189
  if (pTable != pNew) {
190
    void *oldTableId = pTable->info.tableId;    
S
slguan 已提交
191 192
    void *oldSql = pTable->sql;
    void *oldSchema = pTable->schema;
193
    void *oldSTable = pTable->superTable;
194
    int32_t oldRefCount = pTable->refCount;
195
    
196
    memcpy(pTable, pNew, sizeof(SChildTableObj));
197
    
198
    pTable->refCount = oldRefCount;
S
slguan 已提交
199 200
    pTable->sql = pNew->sql;
    pTable->schema = pNew->schema;
201 202
    pTable->superTable = oldSTable;
    
S
slguan 已提交
203 204 205
    free(pNew);
    free(oldSql);
    free(oldSchema);
206
    free(oldTableId);
S
slguan 已提交
207
  }
208
  mnodeDecTableRef(pTable);
S
slguan 已提交
209

S
slguan 已提交
210 211 212
  return TSDB_CODE_SUCCESS;
}

213
static int32_t mnodeChildTableActionEncode(SSdbOper *pOper) {
S
slguan 已提交
214 215 216
  SChildTableObj *pTable = pOper->pObj;
  assert(pTable != NULL && pOper->rowData != NULL);

217
  int32_t len = strlen(pTable->info.tableId);
H
Haojun Liao 已提交
218
  if (len >= TSDB_TABLE_FNAME_LEN) return TSDB_CODE_MND_INVALID_TABLE_ID;
219 220 221 222 223 224 225 226 227

  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 已提交
228
    int32_t schemaSize = pTable->numOfColumns * sizeof(SSchema);
229 230 231 232 233 234
    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 已提交
235 236 237
    }
  }

238 239
  pOper->rowSize = len;

S
slguan 已提交
240 241 242
  return TSDB_CODE_SUCCESS;
}

243
static int32_t mnodeChildTableActionDecode(SSdbOper *pOper) {
S
slguan 已提交
244 245
  assert(pOper->rowData != NULL);
  SChildTableObj *pTable = calloc(1, sizeof(SChildTableObj));
246
  if (pTable == NULL) return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
247

248
  int32_t len = strlen(pOper->rowData);
H
Haojun Liao 已提交
249
  if (len >= TSDB_TABLE_FNAME_LEN) {
S
Shuduo Sang 已提交
250
    free(pTable);
251
    return TSDB_CODE_MND_INVALID_TABLE_ID;
S
Shuduo Sang 已提交
252
  }
253 254 255 256 257
  pTable->info.tableId = strdup(pOper->rowData);
  len++;

  memcpy((char*)pTable + sizeof(char *), pOper->rowData + len, tsChildTableUpdateSize);
  len += tsChildTableUpdateSize;
S
slguan 已提交
258 259 260 261 262

  if (pTable->info.type != TSDB_CHILD_TABLE) {
    int32_t schemaSize = pTable->numOfColumns * sizeof(SSchema);
    pTable->schema = (SSchema *)malloc(schemaSize);
    if (pTable->schema == NULL) {
S
Shengliang Guan 已提交
263
      mnodeDestroyChildTable(pTable);
264
      return TSDB_CODE_MND_INVALID_TABLE_TYPE;
S
slguan 已提交
265
    }
266 267 268 269 270 271
    memcpy(pTable->schema, pOper->rowData + len, schemaSize);
    len += schemaSize;

    if (pTable->sqlLen != 0) {
      pTable->sql = malloc(pTable->sqlLen);
      if (pTable->sql == NULL) {
S
Shengliang Guan 已提交
272
        mnodeDestroyChildTable(pTable);
273
        return TSDB_CODE_MND_OUT_OF_MEMORY;
274 275
      }
      memcpy(pTable->sql, pOper->rowData + len, pTable->sqlLen);
S
slguan 已提交
276 277 278 279 280 281 282
    }
  }

  pOper->pObj = pTable;
  return TSDB_CODE_SUCCESS;
}

283
static int32_t mnodeChildTableActionRestored() {
S
Shengliang Guan 已提交
284
  void *pIter = NULL;
S
slguan 已提交
285 286 287
  SChildTableObj *pTable = NULL;

  while (1) {
288
    pIter = mnodeGetNextChildTable(pIter, &pTable);
S
slguan 已提交
289 290
    if (pTable == NULL) break;

291
    SDbObj *pDb = mnodeGetDbByTableId(pTable->info.tableId);
H
Hui Li 已提交
292 293
    if (pDb == NULL || pDb->status != TSDB_DB_STATUS_READY) {
      mError("ctable:%s, failed to get db or db in dropping, discard it", pTable->info.tableId);
S
Shengliang Guan 已提交
294
      SSdbOper desc = {.type = SDB_OPER_LOCAL, .pObj = pTable, .table = tsChildTableSdb};
S
slguan 已提交
295
      sdbDeleteRow(&desc);
296
      mnodeDecTableRef(pTable);
S
Shengliang Guan 已提交
297
      mnodeDecDbRef(pDb);
S
slguan 已提交
298 299
      continue;
    }
300
    mnodeDecDbRef(pDb);
S
slguan 已提交
301

302
    SVgObj *pVgroup = mnodeGetVgroup(pTable->vgId);
S
slguan 已提交
303
    if (pVgroup == NULL) {
S
slguan 已提交
304
      mError("ctable:%s, failed to get vgId:%d sid:%d, discard it", pTable->info.tableId, pTable->vgId, pTable->sid);
S
slguan 已提交
305
      pTable->vgId = 0;
S
Shengliang Guan 已提交
306
      SSdbOper desc = {.type = SDB_OPER_LOCAL, .pObj = pTable, .table = tsChildTableSdb};
S
slguan 已提交
307
      sdbDeleteRow(&desc);
308
      mnodeDecTableRef(pTable);
S
slguan 已提交
309 310
      continue;
    }
311
    mnodeDecVgroupRef(pVgroup);
S
slguan 已提交
312 313

    if (strcmp(pVgroup->dbName, pDb->name) != 0) {
S
slguan 已提交
314
      mError("ctable:%s, db:%s not match with vgId:%d db:%s sid:%d, discard it",
S
slguan 已提交
315 316
             pTable->info.tableId, pDb->name, pTable->vgId, pVgroup->dbName, pTable->sid);
      pTable->vgId = 0;
S
Shengliang Guan 已提交
317
      SSdbOper desc = {.type = SDB_OPER_LOCAL, .pObj = pTable, .table = tsChildTableSdb};
S
slguan 已提交
318
      sdbDeleteRow(&desc);
319
      mnodeDecTableRef(pTable);
S
slguan 已提交
320 321 322 323
      continue;
    }

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

336
    mnodeDecTableRef(pTable);
S
slguan 已提交
337 338
  }

S
Shengliang Guan 已提交
339 340
  sdbFreeIter(pIter);

S
slguan 已提交
341 342 343
  return 0;
}

344
static int32_t mnodeInitChildTables() {
S
slguan 已提交
345
  SChildTableObj tObj;
346
  tsChildTableUpdateSize = (int8_t *)tObj.updateEnd - (int8_t *)&tObj.info.type;
S
slguan 已提交
347 348 349 350

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

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

370
  mDebug("table:ctables is created");
S
slguan 已提交
371 372 373
  return 0;
}

374
static void mnodeCleanupChildTables() {
S
slguan 已提交
375
  sdbCloseTable(tsChildTableSdb);
376
  tsChildTableSdb = NULL;
S
slguan 已提交
377 378
}

379 380 381 382 383 384 385 386
int64_t mnodeGetSuperTableNum() {
  return sdbGetNumOfRows(tsSuperTableSdb);
}

int64_t mnodeGetChildTableNum() {
  return sdbGetNumOfRows(tsChildTableSdb);
}

387
static void mnodeAddTableIntoStable(SSuperTableObj *pStable, SChildTableObj *pCtable) {
388
  atomic_add_fetch_32(&pStable->numOfTables, 1);
S
slguan 已提交
389

390
  if (pStable->vgHash == NULL) {
H
Haojun Liao 已提交
391
    pStable->vgHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, false);
S
slguan 已提交
392 393
  }

394
  if (pStable->vgHash != NULL) {
395 396
    if (taosHashGet(pStable->vgHash, &pCtable->vgId, sizeof(pCtable->vgId)) == NULL) {
      taosHashPut(pStable->vgHash, &pCtable->vgId, sizeof(pCtable->vgId), &pCtable->vgId, sizeof(pCtable->vgId));
S
Shengliang Guan 已提交
397 398
      mDebug("table:%s, vgId:%d is put into stable vgList, sizeOfVgList:%d", pStable->info.tableId, pCtable->vgId,
             (int32_t)taosHashGetSize(pStable->vgHash));
399
    }
400
  }
S
slguan 已提交
401 402
}

403
static void mnodeRemoveTableFromStable(SSuperTableObj *pStable, SChildTableObj *pCtable) {
404
  atomic_sub_fetch_32(&pStable->numOfTables, 1);
405 406 407

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

408
  SVgObj *pVgroup = mnodeGetVgroup(pCtable->vgId);
409
  if (pVgroup == NULL) {
410
    taosHashRemove(pStable->vgHash, (char *)&pCtable->vgId, sizeof(pCtable->vgId));
S
Shengliang Guan 已提交
411 412
    mDebug("table:%s, vgId:%d is remove from stable vgList, sizeOfVgList:%d", pStable->info.tableId, pCtable->vgId,
           (int32_t)taosHashGetSize(pStable->vgHash));
413
  }
414
  mnodeDecVgroupRef(pVgroup);
S
slguan 已提交
415 416
}

S
Shengliang Guan 已提交
417
static void mnodeDestroySuperTable(SSuperTableObj *pStable) {
418 419 420 421
  if (pStable->vgHash != NULL) {
    taosHashCleanup(pStable->vgHash);
    pStable->vgHash = NULL;
  }
S
Shengliang Guan 已提交
422 423 424
  taosTFree(pStable->info.tableId);
  taosTFree(pStable->schema);
  taosTFree(pStable);
S
slguan 已提交
425 426
}

427
static int32_t mnodeSuperTableActionDestroy(SSdbOper *pOper) {
S
Shengliang Guan 已提交
428
  mnodeDestroySuperTable(pOper->pObj);
S
slguan 已提交
429 430 431
  return TSDB_CODE_SUCCESS;
}

432
static int32_t mnodeSuperTableActionInsert(SSdbOper *pOper) {
S
slguan 已提交
433
  SSuperTableObj *pStable = pOper->pObj;
434
  SDbObj *pDb = mnodeGetDbByTableId(pStable->info.tableId);
H
Hui Li 已提交
435
  if (pDb != NULL && pDb->status == TSDB_DB_STATUS_READY) {
436
    mnodeAddSuperTableIntoDb(pDb);
S
slguan 已提交
437
  }
438
  mnodeDecDbRef(pDb);
S
slguan 已提交
439 440 441 442

  return TSDB_CODE_SUCCESS;
}

443
static int32_t mnodeSuperTableActionDelete(SSdbOper *pOper) {
S
slguan 已提交
444
  SSuperTableObj *pStable = pOper->pObj;
445
  SDbObj *pDb = mnodeGetDbByTableId(pStable->info.tableId);
S
slguan 已提交
446
  if (pDb != NULL) {
447 448
    mnodeRemoveSuperTableFromDb(pDb);
    mnodeDropAllChildTablesInStable((SSuperTableObj *)pStable);
S
slguan 已提交
449
  }
450
  mnodeDecDbRef(pDb);
S
slguan 已提交
451

S
slguan 已提交
452 453 454
  return TSDB_CODE_SUCCESS;
}

455
static int32_t mnodeSuperTableActionUpdate(SSdbOper *pOper) {
S
slguan 已提交
456
  SSuperTableObj *pNew = pOper->pObj;
457
  SSuperTableObj *pTable = mnodeGetSuperTable(pNew->info.tableId);
S
Shengliang Guan 已提交
458
  if (pTable != NULL && pTable != pNew) {
459
    void *oldTableId = pTable->info.tableId;
S
slguan 已提交
460
    void *oldSchema = pTable->schema;
461 462
    void *oldVgHash = pTable->vgHash;
    int32_t oldRefCount = pTable->refCount;
S
Shengliang Guan 已提交
463
    int32_t oldNumOfTables = pTable->numOfTables;
464

465
    memcpy(pTable, pNew, sizeof(SSuperTableObj));
466 467 468

    pTable->vgHash = oldVgHash;
    pTable->refCount = oldRefCount;
S
slguan 已提交
469
    pTable->schema = pNew->schema;
S
Shengliang Guan 已提交
470
    pTable->numOfTables = oldNumOfTables;
471
    free(pNew);
472
    free(oldTableId);
S
slguan 已提交
473 474
    free(oldSchema);
  }
S
Shengliang Guan 已提交
475

S
Shengliang Guan 已提交
476
  mnodeDecTableRef(pTable);
S
slguan 已提交
477 478 479
  return TSDB_CODE_SUCCESS;
}

480
static int32_t mnodeSuperTableActionEncode(SSdbOper *pOper) {
S
slguan 已提交
481 482 483
  SSuperTableObj *pStable = pOper->pObj;
  assert(pOper->pObj != NULL && pOper->rowData != NULL);

484
  int32_t len = strlen(pStable->info.tableId);
H
Haojun Liao 已提交
485
  if (len >= TSDB_TABLE_FNAME_LEN) len = TSDB_CODE_MND_INVALID_TABLE_ID;
S
slguan 已提交
486

487 488 489 490 491 492 493 494 495 496
  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 已提交
497

498
  pOper->rowSize = len;
S
slguan 已提交
499 500 501 502

  return TSDB_CODE_SUCCESS;
}

503
static int32_t mnodeSuperTableActionDecode(SSdbOper *pOper) {
S
slguan 已提交
504 505
  assert(pOper->rowData != NULL);
  SSuperTableObj *pStable = (SSuperTableObj *) calloc(1, sizeof(SSuperTableObj));
506
  if (pStable == NULL) return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
507

508
  int32_t len = strlen(pOper->rowData);
H
Haojun Liao 已提交
509
  if (len >= TSDB_TABLE_FNAME_LEN){
510
    free(pStable);
511
    return TSDB_CODE_MND_INVALID_TABLE_ID;
512
  }
513 514 515 516 517
  pStable->info.tableId = strdup(pOper->rowData);
  len++;

  memcpy((char*)pStable + sizeof(char *), pOper->rowData + len, tsSuperTableUpdateSize);
  len += tsSuperTableUpdateSize;
S
slguan 已提交
518 519 520 521

  int32_t schemaSize = sizeof(SSchema) * (pStable->numOfColumns + pStable->numOfTags);
  pStable->schema = malloc(schemaSize);
  if (pStable->schema == NULL) {
S
Shengliang Guan 已提交
522
    mnodeDestroySuperTable(pStable);
523
    return TSDB_CODE_MND_NOT_SUPER_TABLE;
S
slguan 已提交
524 525
  }

526 527
  memcpy(pStable->schema, pOper->rowData + len, schemaSize);
  
S
slguan 已提交
528 529 530 531 532
  pOper->pObj = pStable;

  return TSDB_CODE_SUCCESS;
}

533
static int32_t mnodeSuperTableActionRestored() {
S
slguan 已提交
534 535 536
  return 0;
}

537
static int32_t mnodeInitSuperTables() {
S
slguan 已提交
538
  SSuperTableObj tObj;
539
  tsSuperTableUpdateSize = (int8_t *)tObj.updateEnd - (int8_t *)&tObj.info.type;
S
slguan 已提交
540 541

  SSdbTableDesc tableDesc = {
S
slguan 已提交
542
    .tableId      = SDB_TABLE_STABLE,
S
slguan 已提交
543
    .tableName    = "stables",
S
Shengliang Guan 已提交
544
    .hashSessions = TSDB_DEFAULT_STABLES_HASH_SIZE,
H
Haojun Liao 已提交
545
    .maxRowSize   = sizeof(SSuperTableObj) + sizeof(SSchema) * (TSDB_MAX_TAGS + TSDB_MAX_COLUMNS + 16) + TSDB_TABLE_FNAME_LEN,
S
slguan 已提交
546
    .refCountPos  = (int8_t *)(&tObj.refCount) - (int8_t *)&tObj,
547
    .keyType      = SDB_KEY_VAR_STRING,
548 549 550 551 552 553 554
    .insertFp     = mnodeSuperTableActionInsert,
    .deleteFp     = mnodeSuperTableActionDelete,
    .updateFp     = mnodeSuperTableActionUpdate,
    .encodeFp     = mnodeSuperTableActionEncode,
    .decodeFp     = mnodeSuperTableActionDecode,
    .destroyFp    = mnodeSuperTableActionDestroy,
    .restoredFp   = mnodeSuperTableActionRestored
S
slguan 已提交
555 556 557 558 559 560 561 562
  };

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

563
  mDebug("table:stables is created");
S
slguan 已提交
564 565 566
  return 0;
}

567
static void mnodeCleanupSuperTables() {
S
slguan 已提交
568
  sdbCloseTable(tsSuperTableSdb);
569
  tsSuperTableSdb = NULL;
S
slguan 已提交
570 571
}

572 573
int32_t mnodeInitTables() {
  int32_t code = mnodeInitSuperTables();
S
slguan 已提交
574 575
  if (code != TSDB_CODE_SUCCESS) {
    return code;
H
hzcheng 已提交
576 577
  }

578
  code = mnodeInitChildTables();
S
slguan 已提交
579 580 581
  if (code != TSDB_CODE_SUCCESS) {
    return code;
  }
H
hzcheng 已提交
582

583 584 585 586 587 588
  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 已提交
589
  
590 591 592 593
  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 已提交
594

595
  mnodeAddPeerMsgHandle(TSDB_MSG_TYPE_DM_CONFIG_TABLE, mnodeProcessTableCfgMsg);
S
slguan 已提交
596

597 598 599 600
  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
Shengliang Guan 已提交
601 602
  mnodeAddShowMetaHandle(TSDB_MGMT_TABLE_STREAMTABLES, mnodeGetStreamTableMeta);
  mnodeAddShowRetrieveHandle(TSDB_MGMT_TABLE_STREAMTABLES, mnodeRetrieveStreamTables);
603

S
slguan 已提交
604
  return TSDB_CODE_SUCCESS;
H
hzcheng 已提交
605 606
}

607
static void *mnodeGetChildTable(char *tableId) {
S
slguan 已提交
608 609 610
  return sdbGetRow(tsChildTableSdb, tableId);
}

611
static void *mnodeGetSuperTable(char *tableId) {
S
slguan 已提交
612 613 614
  return sdbGetRow(tsSuperTableSdb, tableId);
}

615
static void *mnodeGetSuperTableByUid(uint64_t uid) {
616
  SSuperTableObj *pStable = NULL;
S
Shengliang Guan 已提交
617
  void *pIter = NULL;
618 619

  while (1) {
620
    pIter = mnodeGetNextSuperTable(pIter, &pStable);
621 622
    if (pStable == NULL) break;
    if (pStable->uid == uid) {
623
      sdbFreeIter(pIter);
624 625
      return pStable;
    }
626
    mnodeDecTableRef(pStable);
627 628
  }

S
Shengliang Guan 已提交
629 630
  sdbFreeIter(pIter);

631 632 633
  return NULL;
}

634 635
void *mnodeGetTable(char *tableId) {
  void *pTable = mnodeGetSuperTable(tableId);
636 637
  if (pTable != NULL) {
    return pTable;
H
hzcheng 已提交
638 639
  }

640
  pTable = mnodeGetChildTable(tableId);
641 642
  if (pTable != NULL) {
    return pTable;
S
#1177  
slguan 已提交
643
  }
H
hzcheng 已提交
644

S
slguan 已提交
645
  return NULL;
S
#1177  
slguan 已提交
646
}
H
hzcheng 已提交
647

648
void *mnodeGetNextChildTable(void *pIter, SChildTableObj **pTable) {
S
Shengliang Guan 已提交
649
  return sdbFetchRow(tsChildTableSdb, pIter, (void **)pTable);
650 651
}

652
void *mnodeGetNextSuperTable(void *pIter, SSuperTableObj **pTable) {
S
Shengliang Guan 已提交
653
  return sdbFetchRow(tsSuperTableSdb, pIter, (void **)pTable);
654 655
}

656
void mnodeIncTableRef(void *p1) {
S
slguan 已提交
657
  STableObj *pTable = (STableObj *)p1;
S
slguan 已提交
658 659 660 661 662 663 664
  if (pTable->type == TSDB_SUPER_TABLE) {
    sdbIncRef(tsSuperTableSdb, pTable);
  } else {
    sdbIncRef(tsChildTableSdb, pTable);
  }
}

665
void mnodeDecTableRef(void *p1) {
S
slguan 已提交
666 667
  if (p1 == NULL) return;

S
slguan 已提交
668
  STableObj *pTable = (STableObj *)p1;
S
slguan 已提交
669 670 671 672 673 674 675
  if (pTable->type == TSDB_SUPER_TABLE) {
    sdbDecRef(tsSuperTableSdb, pTable);
  } else {
    sdbDecRef(tsChildTableSdb, pTable);
  }
}

676 677 678
void mnodeCleanupTables() {
  mnodeCleanupChildTables();
  mnodeCleanupSuperTables();
S
#1177  
slguan 已提交
679
}
H
hzcheng 已提交
680

681
// todo move to name.h, add length of table name
S
Shengliang Guan 已提交
682
static void mnodeExtractTableName(char* tableId, char* name) {
683 684 685 686 687
  int pos = -1;
  int num = 0;
  for (pos = 0; tableId[pos] != 0; ++pos) {
    if (tableId[pos] == '.') num++;
    if (num == 2) break;
S
slguan 已提交
688 689
  }

690 691
  if (num == 2) {
    strcpy(name, tableId + pos + 1);
S
slguan 已提交
692
  }
H
hzcheng 已提交
693 694
}

695
static int32_t mnodeProcessCreateTableMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
696
  SCMCreateTableMsg *pCreate = pMsg->rpcMsg.pCont;
S
slguan 已提交
697
  
698
  if (pMsg->pDb == NULL) pMsg->pDb = mnodeGetDb(pCreate->db);
H
Hui Li 已提交
699
  if (pMsg->pDb == NULL) {
S
Shengliang Guan 已提交
700
    mError("app:%p:%p, table:%s, failed to create, db not selected", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId);
701
    return TSDB_CODE_MND_DB_NOT_SELECTED;
S
slguan 已提交
702
  }
H
Hui Li 已提交
703 704 705 706 707
  
  if (pMsg->pDb->status != TSDB_DB_STATUS_READY) {
    mError("db:%s, status:%d, in dropping", pMsg->pDb->name, pMsg->pDb->status);
    return TSDB_CODE_MND_DB_IN_DROPPING;
  }
S
slguan 已提交
708

709
  if (pMsg->pTable == NULL) pMsg->pTable = mnodeGetTable(pCreate->tableId);
710
  if (pMsg->pTable != NULL && pMsg->retry == 0) {
S
slguan 已提交
711
    if (pCreate->getMeta) {
712
      mDebug("app:%p:%p, table:%s, continue to get meta", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId);
713
      return mnodeGetChildTableMeta(pMsg);
S
slguan 已提交
714
    } else if (pCreate->igExists) {
715
      mDebug("app:%p:%p, table:%s, is already exist", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId);
716
      return TSDB_CODE_SUCCESS;
S
slguan 已提交
717
    } else {
S
Shengliang Guan 已提交
718 719
      mError("app:%p:%p, table:%s, failed to create, table already exist", pMsg->rpcMsg.ahandle, pMsg,
             pCreate->tableId);
720
      return TSDB_CODE_MND_TABLE_ALREADY_EXIST;
S
slguan 已提交
721
    }
S
slguan 已提交
722 723
  }

724
  if (pCreate->numOfTags != 0) {
725
    mDebug("app:%p:%p, table:%s, create stable msg is received from thandle:%p", pMsg->rpcMsg.ahandle, pMsg,
726
           pCreate->tableId, pMsg->rpcMsg.handle);
727
    return mnodeProcessCreateSuperTableMsg(pMsg);
728
  } else {
729
    mDebug("app:%p:%p, table:%s, create ctable msg is received from thandle:%p", pMsg->rpcMsg.ahandle, pMsg,
730
           pCreate->tableId, pMsg->rpcMsg.handle);
731
    return mnodeProcessCreateChildTableMsg(pMsg);
S
slguan 已提交
732 733 734
  }
}

735
static int32_t mnodeProcessDropTableMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
736
  SCMDropTableMsg *pDrop = pMsg->rpcMsg.pCont;
737
  if (pMsg->pDb == NULL) pMsg->pDb = mnodeGetDbByTableId(pDrop->tableId);
H
Hui Li 已提交
738 739
  if (pMsg->pDb == NULL) {
    mError("app:%p:%p, table:%s, failed to drop table, db not selected or db in dropping", pMsg->rpcMsg.ahandle, pMsg, pDrop->tableId);
740
    return TSDB_CODE_MND_DB_NOT_SELECTED;
S
slguan 已提交
741
  }
H
Hui Li 已提交
742 743 744 745 746
  
  if (pMsg->pDb->status != TSDB_DB_STATUS_READY) {
    mError("db:%s, status:%d, in dropping", pMsg->pDb->name, pMsg->pDb->status);
    return TSDB_CODE_MND_DB_IN_DROPPING;
  }
S
slguan 已提交
747

748
  if (mnodeCheckIsMonitorDB(pMsg->pDb->name, tsMonitorDbName)) {
S
Shengliang Guan 已提交
749 750
    mError("app:%p:%p, table:%s, failed to drop table, in monitor database", pMsg->rpcMsg.ahandle, pMsg,
           pDrop->tableId);
751
    return TSDB_CODE_MND_MONITOR_DB_FORBIDDEN;
752 753
  }

754
  if (pMsg->pTable == NULL) pMsg->pTable = mnodeGetTable(pDrop->tableId);
S
slguan 已提交
755
  if (pMsg->pTable == NULL) {
S
slguan 已提交
756
    if (pDrop->igNotExists) {
H
Haojun Liao 已提交
757
      mDebug("app:%p:%p, table:%s, table is not exist, treat as success", pMsg->rpcMsg.ahandle, pMsg, pDrop->tableId);
758
      return TSDB_CODE_SUCCESS;
S
slguan 已提交
759
    } else {
S
Shengliang Guan 已提交
760
      mError("app:%p:%p, table:%s, failed to drop table, table not exist", pMsg->rpcMsg.ahandle, pMsg, pDrop->tableId);
761
      return TSDB_CODE_MND_INVALID_TABLE_NAME;
S
slguan 已提交
762 763 764
    }
  }

S
slguan 已提交
765
  if (pMsg->pTable->type == TSDB_SUPER_TABLE) {
S
Shengliang Guan 已提交
766 767 768
    SSuperTableObj *pSTable = (SSuperTableObj *)pMsg->pTable;
    mInfo("app:%p:%p, table:%s, start to drop stable, uid:%" PRIu64 ", numOfChildTables:%d, sizeOfVgList:%d",
          pMsg->rpcMsg.ahandle, pMsg, pDrop->tableId, pSTable->uid, pSTable->numOfTables, (int32_t)taosHashGetSize(pSTable->vgHash));
769
    return mnodeProcessDropSuperTableMsg(pMsg);
S
slguan 已提交
770
  } else {
S
Shengliang Guan 已提交
771 772 773
    SChildTableObj *pCTable = (SChildTableObj *)pMsg->pTable;
    mInfo("app:%p:%p, table:%s, start to drop ctable, vgId:%d sid:%d uid:%" PRIu64, pMsg->rpcMsg.ahandle, pMsg,
          pDrop->tableId, pCTable->vgId, pCTable->sid, pCTable->uid);
S
Shengliang Guan 已提交
774
    return mnodeProcessDropChildTableMsg(pMsg);
S
slguan 已提交
775 776 777
  }
}

778
static int32_t mnodeProcessTableMetaMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
779
  SCMTableInfoMsg *pInfo = pMsg->rpcMsg.pCont;
S
slguan 已提交
780
  pInfo->createFlag = htons(pInfo->createFlag);
781
  mDebug("app:%p:%p, table:%s, table meta msg is received from thandle:%p, createFlag:%d", pMsg->rpcMsg.ahandle, pMsg,
S
Shengliang Guan 已提交
782
         pInfo->tableId, pMsg->rpcMsg.handle, pInfo->createFlag);
S
slguan 已提交
783

784
  if (pMsg->pDb == NULL) pMsg->pDb = mnodeGetDbByTableId(pInfo->tableId);
H
Hui Li 已提交
785
  if (pMsg->pDb == NULL) {
S
Shengliang Guan 已提交
786 787
    mError("app:%p:%p, table:%s, failed to get table meta, db not selected", pMsg->rpcMsg.ahandle, pMsg,
           pInfo->tableId);
788
    return TSDB_CODE_MND_DB_NOT_SELECTED;
S
slguan 已提交
789
  }
H
Hui Li 已提交
790 791 792 793 794
  
  if (pMsg->pDb->status != TSDB_DB_STATUS_READY) {
    mError("db:%s, status:%d, in dropping", pMsg->pDb->name, pMsg->pDb->status);
    return TSDB_CODE_MND_DB_IN_DROPPING;
  }
S
slguan 已提交
795

796
  if (pMsg->pTable == NULL) pMsg->pTable = mnodeGetTable(pInfo->tableId);
S
slguan 已提交
797
  if (pMsg->pTable == NULL) {
S
slguan 已提交
798
    if (!pInfo->createFlag) {
S
Shengliang Guan 已提交
799 800
      mError("app:%p:%p, table:%s, failed to get table meta, table not exist", pMsg->rpcMsg.ahandle, pMsg,
             pInfo->tableId);
801
      return TSDB_CODE_MND_INVALID_TABLE_NAME;
S
slguan 已提交
802
    } else {
803
      mDebug("app:%p:%p, table:%s, failed to get table meta, start auto create table ", pMsg->rpcMsg.ahandle, pMsg,
S
Shengliang Guan 已提交
804
             pInfo->tableId);
S
Shengliang Guan 已提交
805
      return mnodeAutoCreateChildTable(pMsg);
S
slguan 已提交
806
    }
S
slguan 已提交
807
  } else {
S
slguan 已提交
808
    if (pMsg->pTable->type != TSDB_SUPER_TABLE) {
809
      return mnodeGetChildTableMeta(pMsg);
S
slguan 已提交
810
    } else {
811
      return mnodeGetSuperTableMeta(pMsg);
S
slguan 已提交
812 813 814 815
    }
  }
}

816 817
static int32_t mnodeCreateSuperTableCb(SMnodeMsg *pMsg, int32_t code) {
  SSuperTableObj *pTable = (SSuperTableObj *)pMsg->pTable;
818 819 820
  assert(pTable);

  if (code == TSDB_CODE_SUCCESS) {
S
Shengliang Guan 已提交
821
    mLInfo("stable:%s, is created in sdb, uid:%" PRIu64, pTable->info.tableId, pTable->uid);
822 823 824 825 826
  } else {
    mError("app:%p:%p, stable:%s, failed to create in sdb, reason:%s", pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId,
           tstrerror(code));
    SSdbOper desc = {.type = SDB_OPER_GLOBAL, .pObj = pTable, .table = tsSuperTableSdb};
    sdbDeleteRow(&desc);
827 828 829 830 831
  }

  return code;
}

832
static int32_t mnodeProcessCreateSuperTableMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
833 834
  if (pMsg == NULL) return TSDB_CODE_MND_APP_ERROR;

S
Shengliang Guan 已提交
835
  SCMCreateTableMsg *pCreate = pMsg->rpcMsg.pCont;
S
Shengliang Guan 已提交
836
  SSuperTableObj *   pStable = calloc(1, sizeof(SSuperTableObj));
S
slguan 已提交
837
  if (pStable == NULL) {
S
Shengliang Guan 已提交
838
    mError("app:%p:%p, table:%s, failed to create, no enough memory", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId);
839
    return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
840 841
  }

842
  pStable->info.tableId = strdup(pCreate->tableId);
S
slguan 已提交
843 844 845 846
  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 已提交
847
  pStable->tversion     = 0;
S
slguan 已提交
848 849 850
  pStable->numOfColumns = htons(pCreate->numOfColumns);
  pStable->numOfTags    = htons(pCreate->numOfTags);

S
slguan 已提交
851
  int32_t numOfCols = pStable->numOfColumns + pStable->numOfTags;
S
slguan 已提交
852 853 854 855
  int32_t schemaSize = numOfCols * sizeof(SSchema);
  pStable->schema = (SSchema *)calloc(1, schemaSize);
  if (pStable->schema == NULL) {
    free(pStable);
S
Shengliang Guan 已提交
856
    mError("app:%p:%p, table:%s, failed to create, no schema input", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId);
857
    return TSDB_CODE_MND_INVALID_TABLE_NAME;
S
slguan 已提交
858 859 860 861 862 863 864 865
  }
  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 已提交
866 867 868
    
    // 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 已提交
869 870
  }

871 872 873
  pMsg->pTable = (STableObj *)pStable;
  mnodeIncTableRef(pMsg->pTable);

S
slguan 已提交
874
  SSdbOper oper = {
S
slguan 已提交
875
    .type = SDB_OPER_GLOBAL,
S
slguan 已提交
876 877
    .table = tsSuperTableSdb,
    .pObj = pStable,
878
    .rowSize = sizeof(SSuperTableObj) + schemaSize,
879
    .pMsg = pMsg,
S
Shengliang Guan 已提交
880
    .writeCb = mnodeCreateSuperTableCb
S
slguan 已提交
881 882 883
  };

  int32_t code = sdbInsertRow(&oper);
S
Shengliang Guan 已提交
884
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
885
    mnodeDestroySuperTable(pStable);
886
    pMsg->pTable = NULL;
S
Shengliang Guan 已提交
887
    mError("app:%p:%p, table:%s, failed to create, sdb error", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId);
S
slguan 已提交
888
  }
S
Shengliang Guan 已提交
889 890

  return code;
S
slguan 已提交
891 892
}

893 894
static int32_t mnodeDropSuperTableCb(SMnodeMsg *pMsg, int32_t code) {
  SSuperTableObj *pTable = (SSuperTableObj *)pMsg->pTable;
895
  if (code != TSDB_CODE_SUCCESS) {
S
Shengliang Guan 已提交
896
    mError("app:%p:%p, stable:%s, failed to drop, sdb error", pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId);
897
  } else {
898
    mLInfo("app:%p:%p, stable:%s, is dropped from sdb", pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId);
899 900 901 902
  }

  return code;
}
903

904
static int32_t mnodeProcessDropSuperTableMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
905 906
  if (pMsg == NULL) return TSDB_CODE_MND_APP_ERROR;

S
slguan 已提交
907
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
908
   if (pStable->vgHash != NULL /*pStable->numOfTables != 0*/) {
909 910 911
    SHashMutableIterator *pIter = taosHashCreateIter(pStable->vgHash);
    while (taosHashIterNext(pIter)) {
      int32_t *pVgId = taosHashIterGet(pIter);
912
      SVgObj *pVgroup = mnodeGetVgroup(*pVgId);
guanshengliang's avatar
guanshengliang 已提交
913
      if (pVgroup == NULL) break;
914

S
slguan 已提交
915
      SMDDropSTableMsg *pDrop = rpcMallocCont(sizeof(SMDDropSTableMsg));
guanshengliang's avatar
guanshengliang 已提交
916
      pDrop->contLen = htonl(sizeof(SMDDropSTableMsg));
917
      pDrop->vgId = htonl(pVgroup->vgId);
S
slguan 已提交
918
      pDrop->uid = htobe64(pStable->uid);
S
Shengliang Guan 已提交
919
      mnodeExtractTableName(pStable->info.tableId, pDrop->tableId);
S
Shengliang Guan 已提交
920

921
      mInfo("app:%p:%p, stable:%s, send drop stable msg to vgId:%d", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
S
Shengliang Guan 已提交
922
             pVgroup->vgId);
923
      SRpcEpSet epSet = mnodeGetEpSetFromVgroup(pVgroup);
S
Shengliang Guan 已提交
924
      SRpcMsg   rpcMsg = {.pCont = pDrop, .contLen = sizeof(SMDDropSTableMsg), .msgType = TSDB_MSG_TYPE_MD_DROP_STABLE};
925
      dnodeSendMsgToDnode(&epSet, &rpcMsg);
926
      mnodeDecVgroupRef(pVgroup);
S
slguan 已提交
927
    }
928
    taosHashDestroyIter(pIter);
929

930
    mnodeDropAllChildTablesInStable(pStable);
guanshengliang's avatar
guanshengliang 已提交
931 932 933
  } 
  
  SSdbOper oper = {
934
    .type  = SDB_OPER_GLOBAL,
guanshengliang's avatar
guanshengliang 已提交
935
    .table = tsSuperTableSdb,
936 937
    .pObj  = pStable,
    .pMsg  = pMsg,
S
Shengliang Guan 已提交
938
    .writeCb = mnodeDropSuperTableCb
guanshengliang's avatar
guanshengliang 已提交
939
  };
940

guanshengliang's avatar
guanshengliang 已提交
941
  int32_t code = sdbDeleteRow(&oper);
S
Shengliang Guan 已提交
942 943 944
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
    mError("app:%p:%p, table:%s, failed to drop, reason:%s", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
           tstrerror(code));
945
  }
S
Shengliang Guan 已提交
946

947
  return code;
S
slguan 已提交
948 949
}

950
static int32_t mnodeFindSuperTableTagIndex(SSuperTableObj *pStable, const char *tagName) {
guanshengliang's avatar
guanshengliang 已提交
951 952 953 954
  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 已提交
955 956 957 958 959 960
    }
  }

  return -1;
}

961 962
static int32_t mnodeAddSuperTableTagCb(SMnodeMsg *pMsg, int32_t code) {
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
963
  mLInfo("app:%p:%p, stable %s, add tag result:%s", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
964 965 966 967 968 969 970
          tstrerror(code));

  return code;
}

static int32_t mnodeAddSuperTableTag(SMnodeMsg *pMsg, SSchema schema[], int32_t ntags) {
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
S
slguan 已提交
971
  if (pStable->numOfTags + ntags > TSDB_MAX_TAGS) {
S
Shengliang Guan 已提交
972
    mError("app:%p:%p, stable:%s, add tag, too many tags", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId);
973
    return TSDB_CODE_MND_TOO_MANY_TAGS;
S
slguan 已提交
974 975
  }

S
slguan 已提交
976
  for (int32_t i = 0; i < ntags; i++) {
977
    if (mnodeFindSuperTableColumnIndex(pStable, schema[i].name) > 0) {
S
Shengliang Guan 已提交
978 979
      mError("app:%p:%p, stable:%s, add tag, column:%s already exist", pMsg->rpcMsg.ahandle, pMsg,
             pStable->info.tableId, schema[i].name);
980
      return TSDB_CODE_MND_TAG_ALREAY_EXIST;
S
slguan 已提交
981 982
    }

983
    if (mnodeFindSuperTableTagIndex(pStable, schema[i].name) > 0) {
S
Shengliang Guan 已提交
984 985
      mError("app:%p:%p, stable:%s, add tag, tag:%s already exist", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
             schema[i].name);
986
      return TSDB_CODE_MND_FIELD_ALREAY_EXIST;
S
slguan 已提交
987 988 989 990 991 992
    }
  }

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

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

S
slguan 已提交
995
  SSchema *tschema = (SSchema *)(pStable->schema + pStable->numOfColumns + pStable->numOfTags);
S
slguan 已提交
996 997 998 999
  for (int32_t i = 0; i < ntags; i++) {
    tschema[i].colId = pStable->nextColId++;
  }

S
slguan 已提交
1000
  pStable->numOfTags += ntags;
S
TD-355  
Shengliang Guan 已提交
1001
  pStable->tversion++;
S
slguan 已提交
1002

1003
  mInfo("app:%p:%p, stable %s, start to add tag %s", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
1004 1005
         schema[0].name);

S
slguan 已提交
1006
  SSdbOper oper = {
S
slguan 已提交
1007
    .type = SDB_OPER_GLOBAL,
S
[TD-17]  
slguan 已提交
1008
    .table = tsSuperTableSdb,
1009
    .pObj = pStable,
1010
    .pMsg = pMsg,
S
Shengliang Guan 已提交
1011
    .writeCb = mnodeAddSuperTableTagCb
S
[TD-17]  
slguan 已提交
1012
  };
S
slguan 已提交
1013

S
Shengliang Guan 已提交
1014
  return sdbUpdateRow(&oper);
S
slguan 已提交
1015 1016
}

1017 1018
static int32_t mnodeDropSuperTableTagCb(SMnodeMsg *pMsg, int32_t code) {
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
1019
  mLInfo("app:%p:%p, stable %s, drop tag result:%s", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
1020 1021 1022 1023 1024 1025
          tstrerror(code));
  return code;
}

static int32_t mnodeDropSuperTableTag(SMnodeMsg *pMsg, char *tagName) {
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
1026
  int32_t col = mnodeFindSuperTableTagIndex(pStable, tagName);
S
slguan 已提交
1027
  if (col < 0) {
S
Shengliang Guan 已提交
1028 1029
    mError("app:%p:%p, stable:%s, drop tag, tag:%s not exist", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
           tagName);
1030
    return TSDB_CODE_MND_TAG_NOT_EXIST;
S
slguan 已提交
1031
  }
S
slguan 已提交
1032

S
slguan 已提交
1033 1034
  memmove(pStable->schema + pStable->numOfColumns + col, pStable->schema + pStable->numOfColumns + col + 1,
          sizeof(SSchema) * (pStable->numOfTags - col - 1));
S
slguan 已提交
1035
  pStable->numOfTags--;
S
TD-355  
Shengliang Guan 已提交
1036
  pStable->tversion++;
S
slguan 已提交
1037

1038
  mInfo("app:%p:%p, stable %s, start to drop tag %s", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId, tagName);
1039

S
slguan 已提交
1040
  SSdbOper oper = {
S
slguan 已提交
1041
    .type = SDB_OPER_GLOBAL,
S
[TD-17]  
slguan 已提交
1042
    .table = tsSuperTableSdb,
1043
    .pObj = pStable,
1044
    .pMsg = pMsg,
S
Shengliang Guan 已提交
1045
    .writeCb = mnodeDropSuperTableTagCb
S
[TD-17]  
slguan 已提交
1046
  };
S
slguan 已提交
1047

S
Shengliang Guan 已提交
1048
  return sdbUpdateRow(&oper);
S
slguan 已提交
1049 1050
}

1051 1052
static int32_t mnodeModifySuperTableTagNameCb(SMnodeMsg *pMsg, int32_t code) {
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
1053
  mLInfo("app:%p:%p, stable %s, modify tag result:%s", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
1054 1055 1056 1057 1058 1059
         tstrerror(code));
  return code;
}

static int32_t mnodeModifySuperTableTagName(SMnodeMsg *pMsg, char *oldTagName, char *newTagName) {
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
1060
  int32_t col = mnodeFindSuperTableTagIndex(pStable, oldTagName);
S
slguan 已提交
1061
  if (col < 0) {
S
Shengliang Guan 已提交
1062 1063
    mError("app:%p:%p, stable:%s, failed to modify table tag, oldName: %s, newName: %s", pMsg->rpcMsg.ahandle, pMsg,
           pStable->info.tableId, oldTagName, newTagName);
1064
    return TSDB_CODE_MND_TAG_NOT_EXIST;
S
slguan 已提交
1065 1066 1067 1068
  }

  // int32_t  rowSize = 0;
  uint32_t len = strlen(newTagName);
S
slguan 已提交
1069
  if (len >= TSDB_COL_NAME_LEN) {
1070
    return TSDB_CODE_MND_COL_NAME_TOO_LONG;
S
slguan 已提交
1071
  }
S
slguan 已提交
1072

1073
  if (mnodeFindSuperTableTagIndex(pStable, newTagName) >= 0) {
1074
    return TSDB_CODE_MND_TAG_ALREAY_EXIST;
S
slguan 已提交
1075
  }
1076
  
S
slguan 已提交
1077
  // update
S
slguan 已提交
1078
  SSchema *schema = (SSchema *) (pStable->schema + pStable->numOfColumns + col);
B
Bomin Zhang 已提交
1079
  tstrncpy(schema->name, newTagName, sizeof(schema->name));
S
slguan 已提交
1080

1081
  mInfo("app:%p:%p, stable %s, start to modify tag %s to %s", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
1082 1083
         oldTagName, newTagName);

S
slguan 已提交
1084
  SSdbOper oper = {
S
slguan 已提交
1085
    .type = SDB_OPER_GLOBAL,
S
[TD-17]  
slguan 已提交
1086
    .table = tsSuperTableSdb,
1087
    .pObj = pStable,
1088
    .pMsg = pMsg,
S
Shengliang Guan 已提交
1089
    .writeCb = mnodeModifySuperTableTagNameCb
S
[TD-17]  
slguan 已提交
1090
  };
S
slguan 已提交
1091

S
Shengliang Guan 已提交
1092
  return sdbUpdateRow(&oper);
S
slguan 已提交
1093 1094
}

1095
static int32_t mnodeFindSuperTableColumnIndex(SSuperTableObj *pStable, char *colName) {
S
slguan 已提交
1096
  SSchema *schema = (SSchema *) pStable->schema;
S
slguan 已提交
1097 1098 1099
  for (int32_t col = 0; col < pStable->numOfColumns; col++) {
    if (strcasecmp(schema[col].name, colName) == 0) {
      return col;
S
slguan 已提交
1100 1101 1102 1103 1104 1105
    }
  }

  return -1;
}

1106 1107
static int32_t mnodeAddSuperTableColumnCb(SMnodeMsg *pMsg, int32_t code) {
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
1108
  mLInfo("app:%p:%p, stable %s, add column result:%s", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
1109 1110 1111 1112 1113
          tstrerror(code));
  return code;
}

static int32_t mnodeAddSuperTableColumn(SMnodeMsg *pMsg, SSchema schema[], int32_t ncols) {
1114
  SDbObj *pDb = pMsg->pDb;
1115
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
S
slguan 已提交
1116
  if (ncols <= 0) {
B
Bomin Zhang 已提交
1117
    mError("app:%p:%p, stable:%s, add column, ncols:%d <= 0", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId, ncols);
1118
    return TSDB_CODE_MND_APP_ERROR;
S
slguan 已提交
1119 1120 1121
  }

  for (int32_t i = 0; i < ncols; i++) {
1122
    if (mnodeFindSuperTableColumnIndex(pStable, schema[i].name) > 0) {
S
Shengliang Guan 已提交
1123 1124
      mError("app:%p:%p, stable:%s, add column, column:%s already exist", pMsg->rpcMsg.ahandle, pMsg,
             pStable->info.tableId, schema[i].name);
1125
      return TSDB_CODE_MND_FIELD_ALREAY_EXIST;
S
slguan 已提交
1126 1127
    }

1128
    if (mnodeFindSuperTableTagIndex(pStable, schema[i].name) > 0) {
S
Shengliang Guan 已提交
1129 1130
      mError("app:%p:%p, stable:%s, add column, tag:%s already exist", pMsg->rpcMsg.ahandle, pMsg,
             pStable->info.tableId, schema[i].name);
1131
      return TSDB_CODE_MND_TAG_ALREAY_EXIST;
S
slguan 已提交
1132 1133 1134 1135 1136 1137
    }
  }

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

S
slguan 已提交
1138 1139 1140
  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 已提交
1141

L
liu0x54 已提交
1142
  SSchema *tschema = (SSchema *) (pStable->schema + pStable->numOfColumns);
S
slguan 已提交
1143 1144 1145 1146 1147 1148 1149
  for (int32_t i = 0; i < ncols; i++) {
    tschema[i].colId = pStable->nextColId++;
  }

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

1150
  SAcctObj *pAcct = mnodeGetAcct(pDb->acct);
S
[TD-17]  
slguan 已提交
1151 1152
  if (pAcct != NULL) {
    pAcct->acctInfo.numOfTimeSeries += (ncols * pStable->numOfTables);
1153
    mnodeDecAcctRef(pAcct);
S
[TD-17]  
slguan 已提交
1154
  }
S
slguan 已提交
1155

1156
  mInfo("app:%p:%p, stable %s, start to add column", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId);
1157

S
slguan 已提交
1158
  SSdbOper oper = {
S
slguan 已提交
1159
    .type = SDB_OPER_GLOBAL,
S
[TD-17]  
slguan 已提交
1160
    .table = tsSuperTableSdb,
1161
    .pObj = pStable,
1162
    .pMsg = pMsg,
S
Shengliang Guan 已提交
1163
    .writeCb = mnodeAddSuperTableColumnCb
S
[TD-17]  
slguan 已提交
1164 1165
  };

S
Shengliang Guan 已提交
1166
 return sdbUpdateRow(&oper);
S
slguan 已提交
1167 1168
}

1169 1170
static int32_t mnodeDropSuperTableColumnCb(SMnodeMsg *pMsg, int32_t code) {
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
1171
  mLInfo("app:%p:%p, stable %s, delete column result:%s", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
1172 1173 1174 1175 1176
         tstrerror(code));
  return code;
}

static int32_t mnodeDropSuperTableColumn(SMnodeMsg *pMsg, char *colName) {
1177
  SDbObj *pDb = pMsg->pDb;
1178
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
1179
  int32_t col = mnodeFindSuperTableColumnIndex(pStable, colName);
S
slguan 已提交
1180
  if (col <= 0) {
S
Shengliang Guan 已提交
1181 1182
    mError("app:%p:%p, stable:%s, drop column, column:%s not exist", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
           colName);
1183
    return TSDB_CODE_MND_FIELD_NOT_EXIST;
S
slguan 已提交
1184 1185
  }

S
slguan 已提交
1186
  memmove(pStable->schema + col, pStable->schema + col + 1,
S
slguan 已提交
1187 1188 1189 1190 1191 1192 1193 1194
          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);

1195
  SAcctObj *pAcct = mnodeGetAcct(pDb->acct);
S
[TD-17]  
slguan 已提交
1196 1197
  if (pAcct != NULL) {
    pAcct->acctInfo.numOfTimeSeries -= pStable->numOfTables;
1198
    mnodeDecAcctRef(pAcct);
S
[TD-17]  
slguan 已提交
1199
  }
S
slguan 已提交
1200

1201
  mInfo("app:%p:%p, stable %s, start to delete column", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId);
1202

S
slguan 已提交
1203
  SSdbOper oper = {
S
slguan 已提交
1204
    .type = SDB_OPER_GLOBAL,
S
[TD-17]  
slguan 已提交
1205
    .table = tsSuperTableSdb,
1206
    .pObj = pStable,
1207
    .pMsg = pMsg,
S
Shengliang Guan 已提交
1208
    .writeCb = mnodeDropSuperTableColumnCb
S
[TD-17]  
slguan 已提交
1209 1210
  };

S
Shengliang Guan 已提交
1211
  return sdbUpdateRow(&oper);
S
slguan 已提交
1212 1213
}

S
Shengliang Guan 已提交
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
static int32_t mnodeChangeSuperTableColumnCb(SMnodeMsg *pMsg, int32_t code) {
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
  mLInfo("app:%p:%p, stable %s, change column result:%s", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
         tstrerror(code));
  return code;
}

static int32_t mnodeChangeSuperTableColumn(SMnodeMsg *pMsg, char *oldName, char *newName) {
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
  int32_t col = mnodeFindSuperTableColumnIndex(pStable, oldName);
  if (col < 0) {
    mError("app:%p:%p, stable:%s, change column, oldName: %s, newName: %s", pMsg->rpcMsg.ahandle, pMsg,
           pStable->info.tableId, oldName, newName);
    return TSDB_CODE_MND_FIELD_NOT_EXIST;
  }

  // int32_t  rowSize = 0;
  uint32_t len = strlen(newName);
  if (len >= TSDB_COL_NAME_LEN) {
    return TSDB_CODE_MND_COL_NAME_TOO_LONG;
  }

  if (mnodeFindSuperTableColumnIndex(pStable, newName) >= 0) {
    return TSDB_CODE_MND_FIELD_ALREAY_EXIST;
  }
  
  // update
  SSchema *schema = (SSchema *) (pStable->schema + col);
  tstrncpy(schema->name, newName, sizeof(schema->name));

  mInfo("app:%p:%p, stable %s, start to modify column %s to %s", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
         oldName, newName);

  SSdbOper oper = {
    .type = SDB_OPER_GLOBAL,
    .table = tsSuperTableSdb,
    .pObj = pStable,
    .pMsg = pMsg,
S
Shengliang Guan 已提交
1252
    .writeCb = mnodeChangeSuperTableColumnCb
S
Shengliang Guan 已提交
1253 1254
  };

S
Shengliang Guan 已提交
1255
  return sdbUpdateRow(&oper);
S
Shengliang Guan 已提交
1256 1257
}

S
slguan 已提交
1258
// show super tables
1259 1260
static int32_t mnodeGetShowSuperTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
  SDbObj *pDb = mnodeGetDb(pShow->db);
1261
  if (pDb == NULL) return TSDB_CODE_MND_DB_NOT_SELECTED;
H
Hui Li 已提交
1262 1263 1264
    
  if (pDb->status != TSDB_DB_STATUS_READY) {
    mError("db:%s, status:%d, in dropping", pDb->name, pDb->status);
S
Shengliang Guan 已提交
1265
    mnodeDecDbRef(pDb);
H
Hui Li 已提交
1266 1267
    return TSDB_CODE_MND_DB_IN_DROPPING;
  }
S
slguan 已提交
1268 1269 1270 1271

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

H
Haojun Liao 已提交
1272 1273 1274
  SSchema tbnameSchema = tGetTableNameColumnSchema();
  pShow->bytes[cols] = tbnameSchema.bytes;
  pSchema[cols].type = tbnameSchema.type;
S
slguan 已提交
1275 1276 1277 1278 1279 1280
  strcpy(pSchema[cols].name, "name");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
1281
  strcpy(pSchema[cols].name, "created_time");
S
slguan 已提交
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
  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];

1312
  mnodeDecDbRef(pDb);
S
slguan 已提交
1313 1314 1315
  return 0;
}

S
slguan 已提交
1316
// retrieve super tables
1317
int32_t mnodeRetrieveShowSuperTables(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
S
slguan 已提交
1318 1319 1320 1321
  int32_t         numOfRows = 0;
  char *          pWrite;
  int32_t         cols = 0;
  SSuperTableObj *pTable = NULL;
S
Shengliang Guan 已提交
1322
  char            prefix[64] = {0};
S
slguan 已提交
1323 1324
  int32_t         prefixLen;

1325
  SDbObj *pDb = mnodeGetDb(pShow->db);
S
slguan 已提交
1326
  if (pDb == NULL) return 0;
H
Hui Li 已提交
1327 1328 1329
  
  if (pDb->status != TSDB_DB_STATUS_READY) {
    mError("db:%s, status:%d, in dropping", pDb->name, pDb->status);
S
Shengliang Guan 已提交
1330
    mnodeDecDbRef(pDb);
H
Hui Li 已提交
1331 1332
    return 0;
  }
S
slguan 已提交
1333

S
Shengliang Guan 已提交
1334
  tstrncpy(prefix, pDb->name, 64);
S
slguan 已提交
1335 1336 1337 1338
  strcat(prefix, TS_PATH_DELIMITER);
  prefixLen = strlen(prefix);

  SPatternCompareInfo info = PATTERN_COMPARE_INFO_INITIALIZER;
B
Bomin Zhang 已提交
1339
  char stableName[TSDB_TABLE_NAME_LEN] = {0};
S
slguan 已提交
1340

S
slguan 已提交
1341
  while (numOfRows < rows) {    
1342
    pShow->pIter = mnodeGetNextSuperTable(pShow->pIter, &pTable);
S
slguan 已提交
1343 1344
    if (pTable == NULL) break;
    if (strncmp(pTable->info.tableId, prefix, prefixLen)) {
1345
      mnodeDecTableRef(pTable);
S
slguan 已提交
1346 1347 1348 1349
      continue;
    }

    memset(stableName, 0, tListLen(stableName));
S
Shengliang Guan 已提交
1350
    mnodeExtractTableName(pTable->info.tableId, stableName);
S
slguan 已提交
1351

B
Bomin Zhang 已提交
1352
    if (pShow->payloadLen > 0 && patternMatch(pShow->payload, stableName, sizeof(stableName) - 1, &info) != TSDB_PATTERN_MATCH) {
1353
      mnodeDecTableRef(pTable);
S
slguan 已提交
1354
      continue;
S
Shengliang Guan 已提交
1355
    }
S
slguan 已提交
1356 1357 1358 1359

    cols = 0;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
H
hjxilinx 已提交
1360
  
B
Bomin Zhang 已提交
1361
    int16_t len = strnlen(stableName, TSDB_TABLE_NAME_LEN - 1);
H
hjxilinx 已提交
1362 1363 1364 1365
    *(int16_t*) pWrite = len;
    pWrite += sizeof(int16_t); // todo refactor
  
    strncpy(pWrite, stableName, len);
S
slguan 已提交
1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
    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++;
1385
    mnodeDecTableRef(pTable);
S
slguan 已提交
1386 1387 1388
  }

  pShow->numOfReads += numOfRows;
Y
yihaoDeng 已提交
1389

1390
  mnodeVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
1391
  mnodeDecDbRef(pDb);
S
slguan 已提交
1392

S
slguan 已提交
1393 1394 1395
  return numOfRows;
}

1396
void mnodeDropAllSuperTables(SDbObj *pDropDb) {
S
Shengliang Guan 已提交
1397
  void *  pIter= NULL;
S
slguan 已提交
1398 1399 1400 1401
  int32_t numOfTables = 0;
  int32_t dbNameLen = strlen(pDropDb->name);
  SSuperTableObj *pTable = NULL;

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

S
slguan 已提交
1404
  while (1) {
1405
    pIter = mnodeGetNextSuperTable(pIter, &pTable);
S
slguan 已提交
1406
    if (pTable == NULL) break;
S
slguan 已提交
1407 1408

    if (strncmp(pDropDb->name, pTable->info.tableId, dbNameLen) == 0) {
S
slguan 已提交
1409
      SSdbOper oper = {
S
slguan 已提交
1410
        .type = SDB_OPER_LOCAL,
S
slguan 已提交
1411 1412 1413 1414 1415 1416
        .table = tsSuperTableSdb,
        .pObj = pTable,
      };
      sdbDeleteRow(&oper);
      numOfTables ++;
    }
S
slguan 已提交
1417

1418
    mnodeDecTableRef(pTable);
S
slguan 已提交
1419
  }
S
slguan 已提交
1420

S
Shengliang Guan 已提交
1421 1422
  sdbFreeIter(pIter);

1423
  mInfo("db:%s, all super tables:%d is dropped from sdb", pDropDb->name, numOfTables);
S
slguan 已提交
1424 1425
}

1426
static int32_t mnodeSetSchemaFromSuperTable(SSchema *pSchema, SSuperTableObj *pTable) {
S
slguan 已提交
1427
  int32_t numOfCols = pTable->numOfColumns + pTable->numOfTags;
1428 1429
  assert(numOfCols <= TSDB_MAX_COLUMNS);
  
S
slguan 已提交
1430
  for (int32_t i = 0; i < numOfCols; ++i) {
B
Bomin Zhang 已提交
1431
    tstrncpy(pSchema->name, pTable->schema[i].name, sizeof(pSchema->name));
S
slguan 已提交
1432 1433 1434 1435 1436 1437 1438 1439 1440
    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);
}

1441
static int32_t mnodeGetSuperTableMeta(SMnodeMsg *pMsg) {
S
slguan 已提交
1442
  SSuperTableObj *pTable = (SSuperTableObj *)pMsg->pTable;
1443
  STableMetaMsg *pMeta   = rpcMallocCont(sizeof(STableMetaMsg) + sizeof(SSchema) * (TSDB_MAX_TAGS + TSDB_MAX_COLUMNS + 16));
1444 1445 1446
  if (pMeta == NULL) {
    return TSDB_CODE_MND_OUT_OF_MEMORY;
  }
S
slguan 已提交
1447 1448
  pMeta->uid          = htobe64(pTable->uid);
  pMeta->sversion     = htons(pTable->sversion);
1449
  pMeta->tversion     = htons(pTable->tversion);
S
slguan 已提交
1450
  pMeta->precision    = pMsg->pDb->cfg.precision;
S
slguan 已提交
1451 1452 1453
  pMeta->numOfTags    = (uint8_t)pTable->numOfTags;
  pMeta->numOfColumns = htons((int16_t)pTable->numOfColumns);
  pMeta->tableType    = pTable->info.type;
1454
  pMeta->contLen      = sizeof(STableMetaMsg) + mnodeSetSchemaFromSuperTable(pMeta->schema, pTable);
B
Bomin Zhang 已提交
1455
  tstrncpy(pMeta->tableId, pTable->info.tableId, sizeof(pMeta->tableId));
S
slguan 已提交
1456

B
Bomin Zhang 已提交
1457
  pMsg->rpcRsp.len = pMeta->contLen;
S
slguan 已提交
1458 1459
  pMeta->contLen = htons(pMeta->contLen);

1460
  pMsg->rpcRsp.rsp = pMeta;
S
Shengliang Guan 已提交
1461

1462
  mDebug("app:%p:%p, stable:%s, uid:%" PRIu64 " table meta is retrieved", pMsg->rpcMsg.ahandle, pMsg,
S
Shengliang Guan 已提交
1463
         pTable->info.tableId, pTable->uid);
1464
  return TSDB_CODE_SUCCESS;
S
slguan 已提交
1465 1466
}

1467
static int32_t mnodeProcessSuperTableVgroupMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
1468
  SCMSTableVgroupMsg *pInfo = pMsg->rpcMsg.pCont;
H
hjxilinx 已提交
1469
  int32_t numOfTable = htonl(pInfo->numOfTables);
1470

S
scripts  
Shengliang Guan 已提交
1471
  // reserve space
S
Shengliang Guan 已提交
1472
  int32_t contLen = sizeof(SCMSTableVgroupRspMsg) + 32 * sizeof(SCMVgroupInfo) + sizeof(SVgroupsInfo); 
1473
  for (int32_t i = 0; i < numOfTable; ++i) {
H
Haojun Liao 已提交
1474
    char *stableName = (char*)pInfo + sizeof(SCMSTableVgroupMsg) + (TSDB_TABLE_FNAME_LEN) * i;
1475
    SSuperTableObj *pTable = mnodeGetSuperTable(stableName);
1476
    if (pTable != NULL && pTable->vgHash != NULL) {
1477
      contLen += (taosHashGetSize(pTable->vgHash) * sizeof(SCMVgroupInfo) + sizeof(SVgroupsInfo));
1478
    } 
1479
    mnodeDecTableRef(pTable);
1480 1481
  }

S
scripts  
Shengliang Guan 已提交
1482
  SCMSTableVgroupRspMsg *pRsp = rpcMallocCont(contLen);
S
slguan 已提交
1483
  if (pRsp == NULL) {
1484
    return TSDB_CODE_MND_OUT_OF_MEMORY;
1485
  }
1486

1487
  pRsp->numOfTables = 0;
1488
  char *msg = (char *)pRsp + sizeof(SCMSTableVgroupRspMsg);
1489 1490

  for (int32_t i = 0; i < numOfTable; ++i) {
H
Haojun Liao 已提交
1491
    char *          stableName = (char *)pInfo + sizeof(SCMSTableVgroupMsg) + (TSDB_TABLE_FNAME_LEN)*i;
1492
    SSuperTableObj *pTable = mnodeGetSuperTable(stableName);
1493
    if (pTable == NULL) {
S
Shengliang Guan 已提交
1494
      mError("app:%p:%p, stable:%s, not exist while get stable vgroup info", pMsg->rpcMsg.ahandle, pMsg, stableName);
1495 1496 1497 1498
      mnodeDecTableRef(pTable);
      continue;
    }
    if (pTable->vgHash == NULL) {
1499
      mError("app:%p:%p, stable:%s, no vgroup exist while get stable vgroup info", pMsg->rpcMsg.ahandle, pMsg,
S
Shengliang Guan 已提交
1500
             stableName);
1501 1502
      mnodeDecTableRef(pTable);

1503 1504
      // even this super table has no corresponding table, still return
      pRsp->numOfTables++;
1505

1506 1507 1508 1509 1510 1511
      SVgroupsInfo *pVgroupInfo = (SVgroupsInfo *)msg;
      pVgroupInfo->numOfVgroups = 0;
      
      msg += sizeof(SVgroupsInfo);
    } else {
      SVgroupsInfo *pVgroupInfo = (SVgroupsInfo *)msg;
1512

1513 1514 1515 1516 1517 1518
      SHashMutableIterator *pIter = taosHashCreateIter(pTable->vgHash);
      int32_t               vgSize = 0;
      while (taosHashIterNext(pIter)) {
        int32_t *pVgId = taosHashIterGet(pIter);
        SVgObj * pVgroup = mnodeGetVgroup(*pVgId);
        if (pVgroup == NULL) continue;
1519

1520 1521 1522 1523
        pVgroupInfo->vgroups[vgSize].vgId = htonl(pVgroup->vgId);
        for (int32_t vn = 0; vn < pVgroup->numOfVnodes; ++vn) {
          SDnodeObj *pDnode = pVgroup->vnodeGid[vn].pDnode;
          if (pDnode == NULL) break;
1524

1525 1526
          tstrncpy(pVgroupInfo->vgroups[vgSize].epAddr[vn].fqdn, pDnode->dnodeFqdn, TSDB_FQDN_LEN);
          pVgroupInfo->vgroups[vgSize].epAddr[vn].port = htons(pDnode->dnodePort);
1527

1528
          pVgroupInfo->vgroups[vgSize].numOfEps++;
1529
        }
1530

1531 1532 1533
        vgSize++;
        mnodeDecVgroupRef(pVgroup);
      }
1534

1535 1536 1537 1538
      taosHashDestroyIter(pIter);
      mnodeDecTableRef(pTable);

      pVgroupInfo->numOfVgroups = htonl(vgSize);
1539

1540 1541 1542 1543
      // one table is done, try the next table
      msg += sizeof(SVgroupsInfo) + vgSize * sizeof(SCMVgroupInfo);
      pRsp->numOfTables++;
    }
1544
  }
S
slguan 已提交
1545

1546 1547
  if (pRsp->numOfTables != numOfTable) {
    rpcFreeCont(pRsp);
1548
    return TSDB_CODE_MND_INVALID_TABLE_NAME;
1549 1550 1551 1552
  } else {
    pRsp->numOfTables = htonl(pRsp->numOfTables);
    pMsg->rpcRsp.rsp = pRsp;
    pMsg->rpcRsp.len = msg - (char *)pRsp;
1553

1554 1555
    return TSDB_CODE_SUCCESS;
  }
S
slguan 已提交
1556 1557
}

1558
static void mnodeProcessDropSuperTableRsp(SRpcMsg *rpcMsg) {
1559
  mInfo("drop stable rsp received, result:%s", tstrerror(rpcMsg->code));
S
slguan 已提交
1560 1561
}

S
Shengliang Guan 已提交
1562
static void *mnodeBuildCreateChildTableMsg(SCMCreateTableMsg *pMsg, SChildTableObj *pTable) {
1563
  STagData *  pTagData = NULL;
S
slguan 已提交
1564 1565 1566
  int32_t tagDataLen = 0;
  int32_t totalCols = 0;
  int32_t contLen = 0;
S
Shengliang Guan 已提交
1567
  if (pTable->info.type == TSDB_CHILD_TABLE) {
S
slguan 已提交
1568
    totalCols = pTable->superTable->numOfColumns + pTable->superTable->numOfTags;
S
Shengliang Guan 已提交
1569
    contLen = sizeof(SMDCreateTableMsg) + totalCols * sizeof(SSchema) + pTable->sqlLen;
S
Shengliang Guan 已提交
1570 1571
    if (pMsg != NULL) {
      pTagData = (STagData *)pMsg->schema;
S
Shengliang Guan 已提交
1572
      tagDataLen = htonl(pTagData->dataLen);
H
Hongze Cheng 已提交
1573
      contLen += tagDataLen;
S
Shengliang Guan 已提交
1574
    }
S
slguan 已提交
1575 1576 1577 1578 1579 1580 1581
  } else {
    totalCols = pTable->numOfColumns;
    contLen = sizeof(SMDCreateTableMsg) + totalCols * sizeof(SSchema) + pTable->sqlLen;
  }

  SMDCreateTableMsg *pCreate = rpcMallocCont(contLen);
  if (pCreate == NULL) {
1582
    terrno = TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
1583 1584 1585
    return NULL;
  }

S
Shengliang Guan 已提交
1586
  mnodeExtractTableName(pTable->info.tableId, pCreate->tableId);
S
slguan 已提交
1587 1588 1589 1590 1591 1592 1593 1594 1595
  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
Shengliang Guan 已提交
1596
    mnodeExtractTableName(pTable->superTable->info.tableId, pCreate->superTableId);
S
slguan 已提交
1597 1598 1599
    pCreate->numOfColumns  = htons(pTable->superTable->numOfColumns);
    pCreate->numOfTags     = htons(pTable->superTable->numOfTags);
    pCreate->sversion      = htonl(pTable->superTable->sversion);
1600
    pCreate->tversion      = htonl(pTable->superTable->tversion);
S
slguan 已提交
1601 1602 1603 1604 1605 1606
    pCreate->tagDataLen    = htonl(tagDataLen);
    pCreate->superTableUid = htobe64(pTable->superTable->uid);
  } else {
    pCreate->numOfColumns  = htons(pTable->numOfColumns);
    pCreate->numOfTags     = 0;
    pCreate->sversion      = htonl(pTable->sversion);
1607
    pCreate->tversion      = 0;
S
slguan 已提交
1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624
    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) {
1625
    memcpy(pCreate->data + totalCols * sizeof(SSchema), pTagData->data, tagDataLen);
1626 1627
  }

S
Shengliang Guan 已提交
1628
  if (pTable->info.type == TSDB_STREAM_TABLE) {
1629
    memcpy(pCreate->data + totalCols * sizeof(SSchema), pTable->sql, pTable->sqlLen);
S
slguan 已提交
1630 1631 1632 1633 1634
  }

  return pCreate;
}

S
Shengliang Guan 已提交
1635
static int32_t mnodeDoCreateChildTableFp(SMnodeMsg *pMsg) {
1636
  SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;
S
Shengliang Guan 已提交
1637 1638
  assert(pTable);

S
Shengliang Guan 已提交
1639 1640
  mDebug("app:%p:%p, table:%s, created in mnode, vgId:%d sid:%d, uid:%" PRIu64, pMsg->rpcMsg.ahandle, pMsg,
         pTable->info.tableId, pTable->vgId, pTable->sid, pTable->uid);
1641

1642
  SCMCreateTableMsg *pCreate = pMsg->rpcMsg.pCont;
1643
  SMDCreateTableMsg *pMDCreate = mnodeBuildCreateChildTableMsg(pCreate, pTable);
1644 1645 1646 1647
  if (pMDCreate == NULL) {
    return terrno;
  }

1648
  SRpcEpSet epSet = mnodeGetEpSetFromVgroup(pMsg->pVgroup);
1649
  SRpcMsg rpcMsg = {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
1650
      .ahandle = pMsg,
1651 1652 1653 1654 1655 1656
      .pCont   = pMDCreate,
      .contLen = htonl(pMDCreate->contLen),
      .code    = 0,
      .msgType = TSDB_MSG_TYPE_MD_CREATE_TABLE
  };

1657
  dnodeSendMsgToDnode(&epSet, &rpcMsg);
1658 1659 1660
  return TSDB_CODE_MND_ACTION_IN_PROGRESS;
}

S
Shengliang Guan 已提交
1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688
static int32_t mnodeDoCreateChildTableCb(SMnodeMsg *pMsg, int32_t code) {
  SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;
  SCMCreateTableMsg *pCreate = pMsg->rpcMsg.pCont;
  assert(pTable);

  if (code == TSDB_CODE_SUCCESS) {
    if (pCreate->getMeta) {
      mDebug("app:%p:%p, table:%s, created in dnode and continue to get meta, thandle:%p", pMsg->rpcMsg.ahandle, pMsg,
             pTable->info.tableId, pMsg->rpcMsg.handle);

      pMsg->retry = 0;
      dnodeReprocessMnodeWriteMsg(pMsg);
    } else {
      mDebug("app:%p:%p, table:%s, created in dnode, thandle:%p", pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId,
             pMsg->rpcMsg.handle);

      dnodeSendRpcMnodeWriteRsp(pMsg, TSDB_CODE_SUCCESS);
    }
    return TSDB_CODE_MND_ACTION_IN_PROGRESS;
  } else {
    mError("app:%p:%p, table:%s, failed to create table sid:%d, uid:%" PRIu64 ", reason:%s", pMsg->rpcMsg.ahandle, pMsg,
           pTable->info.tableId, pTable->sid, pTable->uid, tstrerror(code));
    SSdbOper desc = {.type = SDB_OPER_GLOBAL, .pObj = pTable, .table = tsChildTableSdb};
    sdbDeleteRow(&desc);
    return code;
  }
}

1689
static int32_t mnodeDoCreateChildTable(SMnodeMsg *pMsg, int32_t tid) {
1690
  SVgObj *pVgroup = pMsg->pVgroup;
1691
  SCMCreateTableMsg *pCreate = pMsg->rpcMsg.pCont;
1692
  SChildTableObj *pTable = calloc(1, sizeof(SChildTableObj));
S
slguan 已提交
1693
  if (pTable == NULL) {
S
Shengliang Guan 已提交
1694
    mError("app:%p:%p, table:%s, failed to alloc memory", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId);
1695
    return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
1696 1697 1698 1699 1700 1701 1702 1703
  }

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

1704 1705 1706 1707
  pTable->info.tableId = strdup(pCreate->tableId);  
  pTable->createdTime  = taosGetTimestampMs();
  pTable->sid          = tid;
  pTable->vgId         = pVgroup->vgId;
S
slguan 已提交
1708 1709
    
  if (pTable->info.type == TSDB_CHILD_TABLE) {
1710
    STagData *pTagData = (STagData *) pCreate->schema;  // it is a tag key
1711 1712
    if (pMsg->pSTable == NULL) pMsg->pSTable = mnodeGetSuperTable(pTagData->name);
    if (pMsg->pSTable == NULL) {
S
Shengliang Guan 已提交
1713 1714
      mError("app:%p:%p, table:%s, corresponding super table:%s does not exist", pMsg->rpcMsg.ahandle, pMsg,
             pCreate->tableId, pTagData->name);
1715
      mnodeDestroyChildTable(pTable);
1716
      return TSDB_CODE_MND_INVALID_TABLE_NAME;
S
slguan 已提交
1717
    }
1718

1719
    pTable->suid = pMsg->pSTable->uid;
1720 1721
    pTable->uid = (((uint64_t)pTable->vgId) << 48) + ((((uint64_t)pTable->sid) & ((1ul << 24) - 1ul)) << 24) +
                  ((sdbGetVersion() & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
1722
    pTable->superTable = pMsg->pSTable;
S
slguan 已提交
1723
  } else {
1724 1725 1726 1727 1728 1729 1730 1731
    if (pTable->info.type == TSDB_SUPER_TABLE) {
      int64_t us = taosGetTimestampUs();
      pTable->uid = (us << 24) + ((sdbGetVersion() & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
    } else {
      pTable->uid = (((uint64_t)pTable->vgId) << 48) + ((((uint64_t)pTable->sid) & ((1ul << 24) - 1ul)) << 24) +
                    ((sdbGetVersion() & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
    }

S
slguan 已提交
1732 1733 1734 1735 1736 1737 1738 1739 1740
    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);
1741
      return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
    }
    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);
1757
        return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
1758 1759 1760
      }
      memcpy(pTable->sql, (char *) (pCreate->schema) + numOfCols * sizeof(SSchema), pTable->sqlLen);
      pTable->sql[pTable->sqlLen - 1] = 0;
S
Shengliang Guan 已提交
1761
      mDebug("app:%p:%p, table:%s, stream sql len:%d sql:%s", pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId,
S
Shengliang Guan 已提交
1762
             pTable->sqlLen, pTable->sql);
S
slguan 已提交
1763 1764
    }
  }
1765 1766 1767 1768

  pMsg->pTable = (STableObj *)pTable;
  mnodeIncTableRef(pMsg->pTable);

S
Shengliang Guan 已提交
1769 1770 1771 1772 1773 1774 1775
  SSdbOper desc = {
    .type  = SDB_OPER_GLOBAL,
    .pObj  = pTable,
    .table = tsChildTableSdb,
    .pMsg  = pMsg,
    .reqFp = mnodeDoCreateChildTableFp
  };
S
slguan 已提交
1776
  
1777
  int32_t code = sdbInsertRow(&desc);
S
Shengliang Guan 已提交
1778
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
1779 1780
    mnodeDestroyChildTable(pTable);
    pMsg->pTable = NULL;
S
Shengliang Guan 已提交
1781
    mError("app:%p:%p, table:%s, failed to create, reason:%s", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId,
S
Shengliang Guan 已提交
1782
           tstrerror(code));
S
TD-1038  
Shengliang Guan 已提交
1783 1784 1785
  } else {
    mDebug("app:%p:%p, table:%s, allocated in vgroup, vgId:%d sid:%d uid:%" PRIu64, pMsg->rpcMsg.ahandle, pMsg,
           pTable->info.tableId, pVgroup->vgId, pTable->sid, pTable->uid);
1786
  }
S
Shengliang Guan 已提交
1787 1788

  return code;
S
slguan 已提交
1789 1790
}

1791
static int32_t mnodeProcessCreateChildTableMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
1792
  SCMCreateTableMsg *pCreate = pMsg->rpcMsg.pCont;
S
slguan 已提交
1793 1794
  int32_t code = grantCheck(TSDB_GRANT_TIMESERIES);
  if (code != TSDB_CODE_SUCCESS) {
1795 1796
    mError("app:%p:%p, table:%s, failed to create, grant timeseries failed", pMsg->rpcMsg.ahandle, pMsg,
           pCreate->tableId);
1797
    return code;
S
slguan 已提交
1798 1799
  }

1800
  if (pMsg->retry == 0) {
1801
    if (pMsg->pTable == NULL) {
S
Shengliang Guan 已提交
1802 1803
      SVgObj *pVgroup = NULL;
      int32_t sid = 0;
1804 1805 1806 1807 1808
      code = mnodeGetAvailableVgroup(pMsg, &pVgroup, &sid);
      if (code != TSDB_CODE_SUCCESS) {
        mDebug("app:%p:%p, table:%s, failed to get available vgroup, reason:%s", pMsg->rpcMsg.ahandle, pMsg,
               pCreate->tableId, tstrerror(code));
        return code;
1809 1810
      }

S
Shengliang Guan 已提交
1811 1812
      if (pMsg->pVgroup != NULL) {
        mnodeDecVgroupRef(pMsg->pVgroup);
1813
      }
1814

S
Shengliang Guan 已提交
1815 1816 1817
      pMsg->pVgroup = pVgroup;
      mnodeIncVgroupRef(pVgroup);

1818
      return mnodeDoCreateChildTable(pMsg, sid);
1819
    }
1820
  } else {
1821
    if (pMsg->pTable == NULL) pMsg->pTable = mnodeGetTable(pCreate->tableId);
1822
  }
1823

S
slguan 已提交
1824
  if (pMsg->pTable == NULL) {
B
Bomin Zhang 已提交
1825
    mError("app:%p:%p, table:%s, object not found, retry:%d reason:%s", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId, pMsg->retry,
1826
           tstrerror(terrno));
1827
    return terrno;
1828
  } else {
1829
    mDebug("app:%p:%p, table:%s, send create msg to vnode again", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId);
S
Shengliang Guan 已提交
1830
    return mnodeDoCreateChildTableFp(pMsg);
S
slguan 已提交
1831 1832 1833
  }
}

S
Shengliang Guan 已提交
1834
static int32_t mnodeSendDropChildTableMsg(SMnodeMsg *pMsg, bool needReturn) {
S
slguan 已提交
1835
  SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;
S
Shengliang Guan 已提交
1836
  mLInfo("app:%p:%p, ctable:%s, is dropped from sdb", pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId);
S
slguan 已提交
1837 1838 1839

  SMDDropTableMsg *pDrop = rpcMallocCont(sizeof(SMDDropTableMsg));
  if (pDrop == NULL) {
S
Shengliang Guan 已提交
1840
    mError("app:%p:%p, ctable:%s, failed to drop ctable, no enough memory", pMsg->rpcMsg.ahandle, pMsg,
S
Shengliang Guan 已提交
1841
           pTable->info.tableId);
1842
    return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
1843 1844
  }

H
Haojun Liao 已提交
1845
  tstrncpy(pDrop->tableId, pTable->info.tableId, TSDB_TABLE_FNAME_LEN);
S
slguan 已提交
1846 1847 1848 1849 1850
  pDrop->vgId    = htonl(pTable->vgId);
  pDrop->contLen = htonl(sizeof(SMDDropTableMsg));
  pDrop->sid     = htonl(pTable->sid);
  pDrop->uid     = htobe64(pTable->uid);

1851
  SRpcEpSet epSet = mnodeGetEpSetFromVgroup(pMsg->pVgroup);
S
slguan 已提交
1852

S
Shengliang Guan 已提交
1853
  mInfo("app:%p:%p, ctable:%s, send drop ctable msg, vgId:%d sid:%d uid:%" PRIu64, pMsg->rpcMsg.ahandle, pMsg,
S
Shengliang Guan 已提交
1854 1855
        pDrop->tableId, pTable->vgId, pTable->sid, pTable->uid);

S
slguan 已提交
1856
  SRpcMsg rpcMsg = {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
1857
    .ahandle = pMsg,
S
slguan 已提交
1858 1859 1860 1861 1862 1863
    .pCont   = pDrop,
    .contLen = sizeof(SMDDropTableMsg),
    .code    = 0,
    .msgType = TSDB_MSG_TYPE_MD_DROP_TABLE
  };

1864 1865
  if (!needReturn) rpcMsg.ahandle = NULL;

1866
  dnodeSendMsgToDnode(&epSet, &rpcMsg);
1867

1868
  return TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
slguan 已提交
1869 1870
}

S
Shengliang Guan 已提交
1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894
static int32_t mnodeDropChildTableCb(SMnodeMsg *pMsg, int32_t code) {
  if (code != TSDB_CODE_SUCCESS) {
    SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;
    mError("app:%p:%p, ctable:%s, failed to drop, sdb error", pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId);
    return code;
  } 

  return mnodeSendDropChildTableMsg(pMsg, true);
}

static int32_t mnodeProcessDropChildTableMsg(SMnodeMsg *pMsg) {
  SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;
  if (pMsg->pVgroup == NULL) pMsg->pVgroup = mnodeGetVgroup(pTable->vgId);
  if (pMsg->pVgroup == NULL) {
    mError("app:%p:%p, table:%s, failed to drop ctable, vgroup not exist", pMsg->rpcMsg.ahandle, pMsg,
           pTable->info.tableId);
    return TSDB_CODE_MND_APP_ERROR;
  }

  SSdbOper oper = {
    .type  = SDB_OPER_GLOBAL,
    .table = tsChildTableSdb,
    .pObj  = pTable,
    .pMsg  = pMsg,
S
Shengliang Guan 已提交
1895
    .writeCb = mnodeDropChildTableCb
S
Shengliang Guan 已提交
1896 1897 1898
  };

  int32_t code = sdbDeleteRow(&oper);
S
Shengliang Guan 已提交
1899 1900 1901
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
    mError("app:%p:%p, ctable:%s, failed to drop, reason:%s", pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId,
           tstrerror(code));
S
Shengliang Guan 已提交
1902
  }
S
Shengliang Guan 已提交
1903

S
Shengliang Guan 已提交
1904 1905 1906
  return code;
}

1907
static int32_t mnodeFindNormalTableColumnIndex(SChildTableObj *pTable, char *colName) {
S
slguan 已提交
1908
  SSchema *schema = (SSchema *) pTable->schema;
S
slguan 已提交
1909 1910 1911
  for (int32_t col = 0; col < pTable->numOfColumns; col++) {
    if (strcasecmp(schema[col].name, colName) == 0) {
      return col;
S
slguan 已提交
1912 1913 1914 1915 1916 1917
    }
  }

  return -1;
}

1918
static int32_t mnodeAlterNormalTableColumnCb(SMnodeMsg *pMsg, int32_t code) {
1919
  SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;
1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940
  if (code != TSDB_CODE_SUCCESS) {
    mError("app:%p:%p, ctable %s, failed to alter column, reason:%s", pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId,
           tstrerror(code));
    return code;
  }

  SMDCreateTableMsg *pMDCreate = mnodeBuildCreateChildTableMsg(NULL, pTable);
  if (pMDCreate == NULL) {
    return terrno;
  }

  if (pMsg->pVgroup == NULL) {
    pMsg->pVgroup = mnodeGetVgroup(pTable->vgId);
    if (pMsg->pVgroup == NULL) {
      rpcFreeCont(pMDCreate);
      mError("app:%p:%p, ctable %s, vgId:%d not exist in mnode", pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId,
             pTable->vgId);
      return TSDB_CODE_MND_VGROUP_NOT_EXIST;
    }
  }

1941
  SRpcEpSet epSet = mnodeGetEpSetFromVgroup(pMsg->pVgroup);
1942
  SRpcMsg rpcMsg = {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
1943
      .ahandle = pMsg,
1944 1945 1946 1947 1948 1949
      .pCont   = pMDCreate,
      .contLen = htonl(pMDCreate->contLen),
      .code    = 0,
      .msgType = TSDB_MSG_TYPE_MD_ALTER_TABLE
  };

1950
  mDebug("app:%p:%p, ctable %s, send alter column msg to vgId:%d", pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId,
1951 1952
         pMsg->pVgroup->vgId);

1953
  dnodeSendMsgToDnode(&epSet, &rpcMsg);
1954
  return TSDB_CODE_MND_ACTION_IN_PROGRESS;
1955 1956 1957 1958
}

static int32_t mnodeAddNormalTableColumn(SMnodeMsg *pMsg, SSchema schema[], int32_t ncols) {
  SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;
1959
  SDbObj *pDb = pMsg->pDb;
S
slguan 已提交
1960
  if (ncols <= 0) {
B
Bomin Zhang 已提交
1961
    mError("app:%p:%p, ctable:%s, add column, ncols:%d <= 0", pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId, ncols);
1962
    return TSDB_CODE_MND_APP_ERROR;
S
slguan 已提交
1963 1964 1965
  }

  for (int32_t i = 0; i < ncols; i++) {
1966
    if (mnodeFindNormalTableColumnIndex(pTable, schema[i].name) > 0) {
1967
      mError("app:%p:%p, ctable:%s, add column, column:%s already exist", pMsg->rpcMsg.ahandle, pMsg,
S
Shengliang Guan 已提交
1968
             pTable->info.tableId, schema[i].name);
1969
      return TSDB_CODE_MND_FIELD_ALREAY_EXIST;
S
slguan 已提交
1970 1971 1972 1973 1974 1975
    }
  }

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

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

S
slguan 已提交
1978
  SSchema *tschema = (SSchema *) (pTable->schema + pTable->numOfColumns);
S
slguan 已提交
1979 1980 1981 1982 1983 1984
  for (int32_t i = 0; i < ncols; i++) {
    tschema[i].colId = pTable->nextColId++;
  }

  pTable->numOfColumns += ncols;
  pTable->sversion++;
S
[TD-17]  
slguan 已提交
1985
  
1986
  SAcctObj *pAcct = mnodeGetAcct(pDb->acct);
S
[TD-17]  
slguan 已提交
1987
  if (pAcct != NULL) {
S
slguan 已提交
1988
    pAcct->acctInfo.numOfTimeSeries += ncols;
1989
    mnodeDecAcctRef(pAcct);
S
[TD-17]  
slguan 已提交
1990
  }
1991

1992
  mInfo("app:%p:%p, ctable %s, start to add column", pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId);
1993

S
slguan 已提交
1994
  SSdbOper oper = {
S
slguan 已提交
1995
    .type = SDB_OPER_GLOBAL,
S
[TD-17]  
slguan 已提交
1996
    .table = tsChildTableSdb,
1997
    .pObj = pTable,
1998
    .pMsg = pMsg,
S
Shengliang Guan 已提交
1999
    .writeCb = mnodeAlterNormalTableColumnCb
S
[TD-17]  
slguan 已提交
2000
  };
S
slguan 已提交
2001

S
Shengliang Guan 已提交
2002
  return sdbUpdateRow(&oper);
S
slguan 已提交
2003 2004
}

2005
static int32_t mnodeDropNormalTableColumn(SMnodeMsg *pMsg, char *colName) {
2006
  SDbObj *pDb = pMsg->pDb;
2007
  SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;
2008
  int32_t col = mnodeFindNormalTableColumnIndex(pTable, colName);
S
slguan 已提交
2009
  if (col <= 0) {
2010
    mError("app:%p:%p, ctable:%s, drop column, column:%s not exist", pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId,
S
Shengliang Guan 已提交
2011
           colName);
2012
    return TSDB_CODE_MND_FIELD_NOT_EXIST;
S
slguan 已提交
2013 2014
  }

S
slguan 已提交
2015
  memmove(pTable->schema + col, pTable->schema + col + 1, sizeof(SSchema) * (pTable->numOfColumns - col - 1));
S
slguan 已提交
2016 2017 2018
  pTable->numOfColumns--;
  pTable->sversion++;

2019
  SAcctObj *pAcct = mnodeGetAcct(pDb->acct);
S
[TD-17]  
slguan 已提交
2020 2021
  if (pAcct != NULL) {
    pAcct->acctInfo.numOfTimeSeries--;
2022
    mnodeDecAcctRef(pAcct);
S
[TD-17]  
slguan 已提交
2023
  }
2024

2025
  mInfo("app:%p:%p, ctable %s, start to drop column %s", pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId, colName);
2026

S
slguan 已提交
2027
  SSdbOper oper = {
S
slguan 已提交
2028
    .type = SDB_OPER_GLOBAL,
S
[TD-17]  
slguan 已提交
2029
    .table = tsChildTableSdb,
2030
    .pObj = pTable,
2031
    .pMsg = pMsg,
S
Shengliang Guan 已提交
2032
    .writeCb = mnodeAlterNormalTableColumnCb
S
[TD-17]  
slguan 已提交
2033
  };
S
slguan 已提交
2034

S
Shengliang Guan 已提交
2035
  return sdbUpdateRow(&oper);
S
slguan 已提交
2036 2037
}

S
Shengliang Guan 已提交
2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068
static int32_t mnodeChangeNormalTableColumn(SMnodeMsg *pMsg, char *oldName, char *newName) {
  SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;
  int32_t col = mnodeFindNormalTableColumnIndex(pTable, oldName);
  if (col < 0) {
    mError("app:%p:%p, ctable:%s, change column, oldName: %s, newName: %s", pMsg->rpcMsg.ahandle, pMsg,
           pTable->info.tableId, oldName, newName);
    return TSDB_CODE_MND_FIELD_NOT_EXIST;
  }

  // int32_t  rowSize = 0;
  uint32_t len = strlen(newName);
  if (len >= TSDB_COL_NAME_LEN) {
    return TSDB_CODE_MND_COL_NAME_TOO_LONG;
  }

  if (mnodeFindNormalTableColumnIndex(pTable, newName) >= 0) {
    return TSDB_CODE_MND_FIELD_ALREAY_EXIST;
  }
  
  // update
  SSchema *schema = (SSchema *) (pTable->schema + col);
  tstrncpy(schema->name, newName, sizeof(schema->name));

  mInfo("app:%p:%p, ctable %s, start to modify column %s to %s", pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId,
         oldName, newName);

  SSdbOper oper = {
    .type = SDB_OPER_GLOBAL,
    .table = tsChildTableSdb,
    .pObj = pTable,
    .pMsg = pMsg,
S
Shengliang Guan 已提交
2069
    .writeCb = mnodeAlterNormalTableColumnCb
S
Shengliang Guan 已提交
2070 2071
  };

S
Shengliang Guan 已提交
2072
  return sdbUpdateRow(&oper);
S
Shengliang Guan 已提交
2073 2074
}

2075
static int32_t mnodeSetSchemaFromNormalTable(SSchema *pSchema, SChildTableObj *pTable) {
S
slguan 已提交
2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087
  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);
}

2088
static int32_t mnodeDoGetChildTableMeta(SMnodeMsg *pMsg, STableMetaMsg *pMeta) {
S
slguan 已提交
2089 2090 2091
  SDbObj *pDb = pMsg->pDb;
  SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;

S
slguan 已提交
2092 2093 2094 2095
  pMeta->uid       = htobe64(pTable->uid);
  pMeta->sid       = htonl(pTable->sid);
  pMeta->precision = pDb->cfg.precision;
  pMeta->tableType = pTable->info.type;
H
Haojun Liao 已提交
2096
  tstrncpy(pMeta->tableId, pTable->info.tableId, TSDB_TABLE_FNAME_LEN);
2097
  if (pTable->superTable != NULL) {
2098 2099
    tstrncpy(pMeta->sTableId, pTable->superTable->info.tableId, TSDB_TABLE_FNAME_LEN);
  }
S
slguan 已提交
2100

2101
  if (pTable->info.type == TSDB_CHILD_TABLE && pTable->superTable != NULL) {
S
slguan 已提交
2102
    pMeta->sversion     = htons(pTable->superTable->sversion);
2103
    pMeta->tversion     = htons(pTable->superTable->tversion);
S
slguan 已提交
2104
    pMeta->numOfTags    = (int8_t)pTable->superTable->numOfTags;
S
slguan 已提交
2105
    pMeta->numOfColumns = htons((int16_t)pTable->superTable->numOfColumns);
2106
    pMeta->contLen      = sizeof(STableMetaMsg) + mnodeSetSchemaFromSuperTable(pMeta->schema, pTable->superTable);
S
slguan 已提交
2107 2108
  } else {
    pMeta->sversion     = htons(pTable->sversion);
2109
    pMeta->tversion     = 0;
S
slguan 已提交
2110 2111
    pMeta->numOfTags    = 0;
    pMeta->numOfColumns = htons((int16_t)pTable->numOfColumns);
2112
    pMeta->contLen      = sizeof(STableMetaMsg) + mnodeSetSchemaFromNormalTable(pMeta->schema, pTable); 
S
slguan 已提交
2113
  }
S
Shengliang Guan 已提交
2114

2115
  if (pMsg->pVgroup == NULL) pMsg->pVgroup = mnodeGetVgroup(pTable->vgId);
2116
  if (pMsg->pVgroup == NULL) {
S
Shengliang Guan 已提交
2117 2118
    mError("app:%p:%p, table:%s, failed to get table meta, vgroup not exist", pMsg->rpcMsg.ahandle, pMsg,
           pTable->info.tableId);
2119
    return TSDB_CODE_MND_VGROUP_NOT_EXIST;
S
slguan 已提交
2120 2121
  }

2122
  for (int32_t i = 0; i < pMsg->pVgroup->numOfVnodes; ++i) {
2123
    SDnodeObj *pDnode = mnodeGetDnode(pMsg->pVgroup->vnodeGid[i].dnodeId);
S
slguan 已提交
2124
    if (pDnode == NULL) break;
2125 2126 2127
    strcpy(pMeta->vgroup.epAddr[i].fqdn, pDnode->dnodeFqdn);
    pMeta->vgroup.epAddr[i].port = htons(pDnode->dnodePort + TSDB_PORT_DNODESHELL);
    pMeta->vgroup.numOfEps++;
2128
    mnodeDecDnodeRef(pDnode);
S
slguan 已提交
2129
  }
2130
  pMeta->vgroup.vgId = htonl(pMsg->pVgroup->vgId);
S
slguan 已提交
2131

S
TD-1520  
Shengliang Guan 已提交
2132 2133
  mDebug("app:%p:%p, table:%s, uid:%" PRIu64 " table meta is retrieved, vgId:%d sid:%d", pMsg->rpcMsg.ahandle, pMsg,
         pTable->info.tableId, pTable->uid, pTable->vgId, pTable->sid);
S
slguan 已提交
2134 2135 2136 2137

  return TSDB_CODE_SUCCESS;
}

S
Shengliang Guan 已提交
2138
static int32_t mnodeAutoCreateChildTable(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
2139
  SCMTableInfoMsg *pInfo = pMsg->rpcMsg.pCont;
2140 2141 2142 2143 2144 2145 2146
  STagData *pTags = (STagData *)pInfo->tags;
  int32_t tagLen = htonl(pTags->dataLen);
  if (pTags->name[0] == 0) {
    mError("app:%p:%p, table:%s, failed to create table on demand for stable is empty, tagLen:%d", pMsg->rpcMsg.ahandle,
           pMsg, pInfo->tableId, tagLen);
    return TSDB_CODE_MND_INVALID_STABLE_NAME; 
  }
2147

2148
  int32_t contLen = sizeof(SCMCreateTableMsg) + offsetof(STagData, data) + tagLen;
S
slguan 已提交
2149 2150
  SCMCreateTableMsg *pCreateMsg = rpcMallocCont(contLen);
  if (pCreateMsg == NULL) {
S
Shengliang Guan 已提交
2151 2152
    mError("app:%p:%p, table:%s, failed to create table while get meta info, no enough memory", pMsg->rpcMsg.ahandle,
           pMsg, pInfo->tableId);
2153
    return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
2154
  }
S
slguan 已提交
2155

2156 2157
  size_t size = sizeof(pInfo->tableId);
  tstrncpy(pCreateMsg->tableId, pInfo->tableId, size);
B
Bomin Zhang 已提交
2158
  tstrncpy(pCreateMsg->db, pMsg->pDb->name, sizeof(pCreateMsg->db));
S
slguan 已提交
2159 2160
  pCreateMsg->igExists = 1;
  pCreateMsg->getMeta = 1;
S
slguan 已提交
2161
  pCreateMsg->contLen = htonl(contLen);
B
Bomin Zhang 已提交
2162

S
Shengliang Guan 已提交
2163
  memcpy(pCreateMsg->schema, pTags, contLen - sizeof(SCMCreateTableMsg));
2164 2165
  mDebug("app:%p:%p, table:%s, start to create on demand, tagLen:%d stable:%s",
         pMsg->rpcMsg.ahandle, pMsg, pInfo->tableId, tagLen, pTags->name);
S
slguan 已提交
2166

S
Shengliang Guan 已提交
2167 2168 2169 2170
  rpcFreeCont(pMsg->rpcMsg.pCont);
  pMsg->rpcMsg.msgType = TSDB_MSG_TYPE_CM_CREATE_TABLE;
  pMsg->rpcMsg.pCont = pCreateMsg;
  pMsg->rpcMsg.contLen = contLen;
2171
  
2172
  return TSDB_CODE_MND_ACTION_NEED_REPROCESSED;
S
slguan 已提交
2173
}
S
slguan 已提交
2174

2175
static int32_t mnodeGetChildTableMeta(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
2176 2177
  STableMetaMsg *pMeta =
      rpcMallocCont(sizeof(STableMetaMsg) + sizeof(SSchema) * (TSDB_MAX_TAGS + TSDB_MAX_COLUMNS + 16));
S
slguan 已提交
2178
  if (pMeta == NULL) {
S
Shengliang Guan 已提交
2179 2180
    mError("app:%p:%p, table:%s, failed to get table meta, no enough memory", pMsg->rpcMsg.ahandle, pMsg,
           pMsg->pTable->tableId);
2181
    return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
2182 2183
  }

2184
  mnodeDoGetChildTableMeta(pMsg, pMeta);
S
slguan 已提交
2185

2186 2187
  pMsg->rpcRsp.len = pMeta->contLen;
  pMsg->rpcRsp.rsp = pMeta;
S
slguan 已提交
2188
  pMeta->contLen = htons(pMeta->contLen);
2189 2190

  return TSDB_CODE_SUCCESS;
S
slguan 已提交
2191 2192
}

2193
void mnodeDropAllChildTablesInVgroups(SVgObj *pVgroup) {
2194 2195 2196 2197
  void *  pIter = NULL;
  int32_t numOfTables = 0;
  SChildTableObj *pTable = NULL;

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

  while (1) {
2201
    pIter = mnodeGetNextChildTable(pIter, &pTable);
2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212
    if (pTable == NULL) break;

    if (pTable->vgId == pVgroup->vgId) {
      SSdbOper oper = {
        .type = SDB_OPER_LOCAL,
        .table = tsChildTableSdb,
        .pObj = pTable,
      };
      sdbDeleteRow(&oper);
      numOfTables++;
    }
2213
    mnodeDecTableRef(pTable);
2214 2215 2216 2217
  }

  sdbFreeIter(pIter);

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

2221
void mnodeDropAllChildTables(SDbObj *pDropDb) {
S
Shengliang Guan 已提交
2222
  void *  pIter = NULL;
S
slguan 已提交
2223 2224 2225 2226
  int32_t numOfTables = 0;
  int32_t dbNameLen = strlen(pDropDb->name);
  SChildTableObj *pTable = NULL;

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

S
slguan 已提交
2229
  while (1) {
2230
    pIter = mnodeGetNextChildTable(pIter, &pTable);
S
slguan 已提交
2231
    if (pTable == NULL) break;
S
slguan 已提交
2232 2233

    if (strncmp(pDropDb->name, pTable->info.tableId, dbNameLen) == 0) {
S
slguan 已提交
2234
      SSdbOper oper = {
S
slguan 已提交
2235
        .type = SDB_OPER_LOCAL,
S
slguan 已提交
2236 2237 2238 2239 2240 2241
        .table = tsChildTableSdb,
        .pObj = pTable,
      };
      sdbDeleteRow(&oper);
      numOfTables++;
    }
2242
    mnodeDecTableRef(pTable);
S
slguan 已提交
2243 2244
  }

S
Shengliang Guan 已提交
2245 2246
  sdbFreeIter(pIter);

2247
  mInfo("db:%s, all child tables:%d is dropped from sdb", pDropDb->name, numOfTables);
S
slguan 已提交
2248 2249
}

2250
static void mnodeDropAllChildTablesInStable(SSuperTableObj *pStable) {
S
Shengliang Guan 已提交
2251
  void *  pIter = NULL;
S
slguan 已提交
2252 2253 2254
  int32_t numOfTables = 0;
  SChildTableObj *pTable = NULL;

S
Shengliang Guan 已提交
2255
  mInfo("stable:%s, all child tables:%d will dropped from sdb", pStable->info.tableId, pStable->numOfTables);
S
slguan 已提交
2256

S
slguan 已提交
2257
  while (1) {
2258
    pIter = mnodeGetNextChildTable(pIter, &pTable);
S
slguan 已提交
2259
    if (pTable == NULL) break;
S
slguan 已提交
2260 2261

    if (pTable->superTable == pStable) {
S
slguan 已提交
2262
      SSdbOper oper = {
S
slguan 已提交
2263
        .type = SDB_OPER_LOCAL,
S
slguan 已提交
2264 2265 2266 2267 2268 2269
        .table = tsChildTableSdb,
        .pObj = pTable,
      };
      sdbDeleteRow(&oper);
      numOfTables++;
    }
S
slguan 已提交
2270

2271
    mnodeDecTableRef(pTable);
S
slguan 已提交
2272 2273
  }

S
Shengliang Guan 已提交
2274 2275
  sdbFreeIter(pIter);

2276
  mInfo("stable:%s, all child tables:%d is dropped from sdb", pStable->info.tableId, numOfTables);
S
slguan 已提交
2277 2278
}

2279
#if 0
2280 2281
static SChildTableObj* mnodeGetTableByPos(int32_t vnode, int32_t sid) {
  SVgObj *pVgroup = mnodeGetVgroup(vnode);
2282
  if (pVgroup == NULL) return NULL;
S
slguan 已提交
2283

S
slguan 已提交
2284
  SChildTableObj *pTable = pVgroup->tableList[sid - 1];
2285
  mnodeIncTableRef((STableObj *)pTable);
2286

2287
  mnodeDecVgroupRef(pVgroup);
S
slguan 已提交
2288 2289
  return pTable;
}
2290
#endif
S
slguan 已提交
2291

2292
static int32_t mnodeProcessTableCfgMsg(SMnodeMsg *pMsg) {
2293 2294
  return TSDB_CODE_COM_OPS_NOT_SUPPORT;
#if 0  
S
Shengliang Guan 已提交
2295
  SDMConfigTableMsg *pCfg = pMsg->rpcMsg.pCont;
S
Shengliang Guan 已提交
2296 2297 2298
  pCfg->dnodeId = htonl(pCfg->dnodeId);
  pCfg->vgId = htonl(pCfg->vgId);
  pCfg->sid = htonl(pCfg->sid);
2299
  mDebug("app:%p:%p, dnode:%d, vgId:%d sid:%d, receive table config msg", pMsg->rpcMsg.ahandle, pMsg, pCfg->dnodeId,
S
Shengliang Guan 已提交
2300
         pCfg->vgId, pCfg->sid);
S
slguan 已提交
2301

S
Shengliang Guan 已提交
2302
  SChildTableObj *pTable = mnodeGetTableByPos(pCfg->vgId, pCfg->sid);
S
slguan 已提交
2303
  if (pTable == NULL) {
S
Shengliang Guan 已提交
2304 2305
    mError("app:%p:%p, dnode:%d, vgId:%d sid:%d, table not found", pMsg->rpcMsg.ahandle, pMsg, pCfg->dnodeId,
           pCfg->vgId, pCfg->sid);
2306
    return TSDB_CODE_MND_INVALID_TABLE_ID;
S
slguan 已提交
2307 2308
  }

S
Shengliang Guan 已提交
2309 2310
  SMDCreateTableMsg *pCreate = NULL;
  pCreate = mnodeBuildCreateChildTableMsg(NULL, (SChildTableObj *)pTable);
2311
  mnodeDecTableRef(pTable);
S
Shengliang Guan 已提交
2312 2313 2314 2315 2316
    
  if (pCreate == NULL) return terrno;
  
  pMsg->rpcRsp.rsp = pCreate;
  pMsg->rpcRsp.len = htonl(pCreate->contLen);
2317
  return TSDB_CODE_SUCCESS;
2318
#endif  
S
slguan 已提交
2319 2320
}

S
slguan 已提交
2321
// handle drop child response
2322
static void mnodeProcessDropChildTableRsp(SRpcMsg *rpcMsg) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
2323
  if (rpcMsg->ahandle == NULL) return;
S
slguan 已提交
2324

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
2325
  SMnodeMsg *mnodeMsg = rpcMsg->ahandle;
2326
  mnodeMsg->received++;
S
slguan 已提交
2327

S
Shengliang Guan 已提交
2328 2329
  SChildTableObj *pTable = (SChildTableObj *)mnodeMsg->pTable;
  assert(pTable);
S
Shengliang Guan 已提交
2330 2331 2332 2333

  mInfo("app:%p:%p, table:%s, drop table rsp received, vgId:%d sid:%d uid:%" PRIu64 ", thandle:%p result:%s",
        mnodeMsg->rpcMsg.ahandle, mnodeMsg, pTable->info.tableId, pTable->vgId, pTable->sid, pTable->uid,
        mnodeMsg->rpcMsg.handle, tstrerror(rpcMsg->code));
S
slguan 已提交
2334 2335

  if (rpcMsg->code != TSDB_CODE_SUCCESS) {
S
Shengliang Guan 已提交
2336 2337 2338
    mError("app:%p:%p, table:%s, failed to drop in dnode, vgId:%d sid:%d uid:%" PRIu64 ", reason:%s",
           mnodeMsg->rpcMsg.ahandle, mnodeMsg, pTable->info.tableId, pTable->vgId, pTable->sid, pTable->uid,
           tstrerror(rpcMsg->code));
2339
    dnodeSendRpcMnodeWriteRsp(mnodeMsg, rpcMsg->code);
S
slguan 已提交
2340 2341 2342
    return;
  }

2343 2344
  if (mnodeMsg->pVgroup == NULL) mnodeMsg->pVgroup = mnodeGetVgroup(pTable->vgId);
  if (mnodeMsg->pVgroup == NULL) {
S
Shengliang Guan 已提交
2345
    mError("app:%p:%p, table:%s, failed to get vgroup", mnodeMsg->rpcMsg.ahandle, mnodeMsg, pTable->info.tableId);
2346
    dnodeSendRpcMnodeWriteRsp(mnodeMsg, TSDB_CODE_MND_VGROUP_NOT_EXIST);
S
slguan 已提交
2347 2348
    return;
  }
S
Shengliang Guan 已提交
2349

2350
  if (mnodeMsg->pVgroup->numOfTables <= 0) {
2351
    mInfo("app:%p:%p, vgId:%d, all tables is dropped, drop vgroup", mnodeMsg->rpcMsg.ahandle, mnodeMsg,
S
Shengliang Guan 已提交
2352
           mnodeMsg->pVgroup->vgId);
2353
    mnodeDropVgroup(mnodeMsg->pVgroup, NULL);
S
slguan 已提交
2354 2355
  }

2356
  dnodeSendRpcMnodeWriteRsp(mnodeMsg, TSDB_CODE_SUCCESS);
S
slguan 已提交
2357 2358
}

2359 2360 2361 2362
/*
 * handle create table response from dnode
 *   if failed, drop the table cached
 */
2363
static void mnodeProcessCreateChildTableRsp(SRpcMsg *rpcMsg) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
2364
  if (rpcMsg->ahandle == NULL) return;
S
slguan 已提交
2365

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
2366
  SMnodeMsg *mnodeMsg = rpcMsg->ahandle;
2367
  mnodeMsg->received++;
S
slguan 已提交
2368

S
Shengliang Guan 已提交
2369 2370
  SChildTableObj *pTable = (SChildTableObj *)mnodeMsg->pTable;
  assert(pTable);
S
Shengliang Guan 已提交
2371

2372 2373 2374 2375
  // If the table is deleted by another thread during creation, stop creating and send drop msg to vnode
  if (sdbCheckRowDeleted(tsChildTableSdb, pTable)) {
    mDebug("app:%p:%p, table:%s, create table rsp received, but a deleting opertion incoming, vgId:%d sid:%d uid:%" PRIu64,
           mnodeMsg->rpcMsg.ahandle, mnodeMsg, pTable->info.tableId, pTable->vgId, pTable->sid, pTable->uid);
S
TD-1038  
Shengliang Guan 已提交
2376 2377 2378

    // if the vgroup is already dropped from hash, it can't be accquired by pTable->vgId
    // so the refCount of vgroup can not be decreased
S
Shengliang Guan 已提交
2379 2380 2381 2382 2383
    // SVgObj *pVgroup = mnodeGetVgroup(pTable->vgId);
    // if (pVgroup == NULL) {
    //   mnodeRemoveTableFromVgroup(mnodeMsg->pVgroup, pTable);
    // }
    // mnodeDecVgroupRef(pVgroup);
S
TD-1038  
Shengliang Guan 已提交
2384

S
Shengliang Guan 已提交
2385
    mnodeSendDropChildTableMsg(mnodeMsg, false);
2386
    rpcMsg->code = TSDB_CODE_SUCCESS;
S
Shengliang Guan 已提交
2387 2388
    dnodeSendRpcMnodeWriteRsp(mnodeMsg, rpcMsg->code);
    return;
2389 2390
  }

2391
  if (rpcMsg->code == TSDB_CODE_SUCCESS || rpcMsg->code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
S
Shengliang Guan 已提交
2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404
     SSdbOper desc = {
      .type  = SDB_OPER_GLOBAL,
      .pObj  = pTable,
      .table = tsChildTableSdb,
      .pMsg  = mnodeMsg,
      .writeCb = mnodeDoCreateChildTableCb
    };
    
    int32_t code = sdbInsertRowImp(&desc);
    if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
      mnodeMsg->pTable = NULL;
      mnodeDestroyChildTable(pTable);
      dnodeSendRpcMnodeWriteRsp(mnodeMsg, code);
2405 2406
    }
  } else {
S
Shengliang Guan 已提交
2407
    if (mnodeMsg->retry++ < 10) {
2408
      mDebug("app:%p:%p, table:%s, create table rsp received, need retry, times:%d result:%s thandle:%p",
S
Shengliang Guan 已提交
2409
             mnodeMsg->rpcMsg.ahandle, mnodeMsg, pTable->info.tableId, mnodeMsg->retry, tstrerror(rpcMsg->code),
2410 2411
             mnodeMsg->rpcMsg.handle);

2412
      dnodeDelayReprocessMnodeWriteMsg(mnodeMsg);
2413
    } else {
2414 2415 2416
      mError("app:%p:%p, table:%s, failed to create in dnode, result:%s thandle:%p", mnodeMsg->rpcMsg.ahandle, mnodeMsg,
             pTable->info.tableId, tstrerror(rpcMsg->code), mnodeMsg->rpcMsg.handle);

2417
      SSdbOper oper = {.type = SDB_OPER_GLOBAL, .table = tsChildTableSdb, .pObj = pTable};
2418
      sdbDeleteRow(&oper);
2419

2420
      dnodeSendRpcMnodeWriteRsp(mnodeMsg, rpcMsg->code);
S
slguan 已提交
2421 2422 2423 2424
    }
  }
}

2425
static void mnodeProcessAlterTableRsp(SRpcMsg *rpcMsg) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
2426
  if (rpcMsg->ahandle == NULL) return;
2427

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
2428
  SMnodeMsg *mnodeMsg = rpcMsg->ahandle;
2429 2430 2431 2432 2433 2434
  mnodeMsg->received++;

  SChildTableObj *pTable = (SChildTableObj *)mnodeMsg->pTable;
  assert(pTable);

  if (rpcMsg->code == TSDB_CODE_SUCCESS || rpcMsg->code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
2435
    mDebug("app:%p:%p, ctable:%s, altered in dnode, thandle:%p result:%s", mnodeMsg->rpcMsg.ahandle, mnodeMsg,
2436 2437 2438 2439 2440
           pTable->info.tableId, mnodeMsg->rpcMsg.handle, tstrerror(rpcMsg->code));

    dnodeSendRpcMnodeWriteRsp(mnodeMsg, TSDB_CODE_SUCCESS);
  } else {
    if (mnodeMsg->retry++ < 3) {
2441
      mDebug("app:%p:%p, table:%s, alter table rsp received, need retry, times:%d result:%s thandle:%p",
2442 2443 2444 2445 2446 2447 2448 2449 2450 2451
             mnodeMsg->rpcMsg.ahandle, mnodeMsg, pTable->info.tableId, mnodeMsg->retry, tstrerror(rpcMsg->code),
             mnodeMsg->rpcMsg.handle);

      dnodeDelayReprocessMnodeWriteMsg(mnodeMsg);
    } else {
      mError("app:%p:%p, table:%s, failed to alter in dnode, result:%s thandle:%p", mnodeMsg->rpcMsg.ahandle, mnodeMsg,
             pTable->info.tableId, tstrerror(rpcMsg->code), mnodeMsg->rpcMsg.handle);
      dnodeSendRpcMnodeWriteRsp(mnodeMsg, rpcMsg->code);
    }
  }
S
slguan 已提交
2452 2453
}

2454
static int32_t mnodeProcessMultiTableMetaMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
2455
  SCMMultiTableInfoMsg *pInfo = pMsg->rpcMsg.pCont;
S
slguan 已提交
2456 2457
  pInfo->numOfTables = htonl(pInfo->numOfTables);

2458
  int32_t totalMallocLen = 4 * 1024 * 1024;  // first malloc 4 MB, subsequent reallocation as twice
S
slguan 已提交
2459 2460
  SMultiTableMeta *pMultiMeta = rpcMallocCont(totalMallocLen);
  if (pMultiMeta == NULL) {
2461
    return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
2462 2463 2464 2465 2466
  }

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

2467
  for (int32_t t = 0; t < pInfo->numOfTables; ++t) {
H
Haojun Liao 已提交
2468
    char * tableId = (char *)(pInfo->tableIds + t * TSDB_TABLE_FNAME_LEN);
2469
    SChildTableObj *pTable = mnodeGetChildTable(tableId);
S
slguan 已提交
2470 2471
    if (pTable == NULL) continue;

2472
    if (pMsg->pDb == NULL) pMsg->pDb = mnodeGetDbByTableId(tableId);
H
Hui Li 已提交
2473
    if (pMsg->pDb == NULL || pMsg->pDb->status != TSDB_DB_STATUS_READY) {
2474
      mnodeDecTableRef(pTable);
2475 2476
      continue;
    }
S
slguan 已提交
2477 2478

    int availLen = totalMallocLen - pMultiMeta->contLen;
2479
    if (availLen <= sizeof(STableMetaMsg) + sizeof(SSchema) * (TSDB_MAX_TAGS + TSDB_MAX_COLUMNS + 16)) {
2480 2481 2482
      totalMallocLen *= 2;
      pMultiMeta = rpcReallocCont(pMultiMeta, totalMallocLen);
      if (pMultiMeta == NULL) {
2483
        mnodeDecTableRef(pTable);
2484
        return TSDB_CODE_MND_OUT_OF_MEMORY;
2485 2486
      } else {
        t--;
2487
        mnodeDecTableRef(pTable);
2488 2489
        continue;
      }
S
slguan 已提交
2490 2491 2492
    }

    STableMetaMsg *pMeta = (STableMetaMsg *)(pMultiMeta->metas + pMultiMeta->contLen);
2493
    int32_t code = mnodeDoGetChildTableMeta(pMsg, pMeta);
S
slguan 已提交
2494 2495 2496 2497
    if (code == TSDB_CODE_SUCCESS) {
      pMultiMeta->numOfTables ++;
      pMultiMeta->contLen += pMeta->contLen;
    }
2498

2499
    mnodeDecTableRef(pTable);
S
slguan 已提交
2500 2501
  }

2502 2503 2504 2505
  pMsg->rpcRsp.rsp = pMultiMeta;
  pMsg->rpcRsp.len = pMultiMeta->contLen;

  return TSDB_CODE_SUCCESS;
S
slguan 已提交
2506 2507
}

2508 2509
static int32_t mnodeGetShowTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
  SDbObj *pDb = mnodeGetDb(pShow->db);
2510
  if (pDb == NULL) return TSDB_CODE_MND_DB_NOT_SELECTED;
H
Hui Li 已提交
2511 2512 2513
  
  if (pDb->status != TSDB_DB_STATUS_READY) {
    mError("db:%s, status:%d, in dropping", pDb->name, pDb->status);
S
Shengliang Guan 已提交
2514
    mnodeDecDbRef(pDb);
H
Hui Li 已提交
2515 2516
    return TSDB_CODE_MND_DB_IN_DROPPING;
  }
S
slguan 已提交
2517 2518 2519 2520

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

H
Haojun Liao 已提交
2521 2522 2523
  SSchema s = tGetTableNameColumnSchema();
  pShow->bytes[cols] = s.bytes;
  pSchema[cols].type = s.type;
2524
  strcpy(pSchema[cols].name, "table_name");
S
slguan 已提交
2525 2526 2527 2528 2529
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
2530
  strcpy(pSchema[cols].name, "created_time");
S
slguan 已提交
2531 2532 2533 2534 2535 2536 2537 2538 2539
  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++;

H
Haojun Liao 已提交
2540
  SSchema tbCol = tGetTableNameColumnSchema();
H
Hui Li 已提交
2541
  pShow->bytes[cols] = tbCol.bytes + VARSTR_HEADER_SIZE;
H
Haojun Liao 已提交
2542
  pSchema[cols].type = tbCol.type;
2543
  strcpy(pSchema[cols].name, "stable_name");
S
slguan 已提交
2544 2545 2546
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

H
Haojun Liao 已提交
2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565
  pShow->bytes[cols] = 8; // table uid
  pSchema[cols].type = TSDB_DATA_TYPE_BIGINT;
  strcpy(pSchema[cols].name, "uid");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

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

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


S
slguan 已提交
2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576
  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];

2577
  mnodeDecDbRef(pDb);
S
slguan 已提交
2578 2579 2580
  return 0;
}

2581 2582
static int32_t mnodeRetrieveShowTables(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
  SDbObj *pDb = mnodeGetDb(pShow->db);
S
slguan 已提交
2583
  if (pDb == NULL) return 0;
H
Hui Li 已提交
2584 2585 2586
  
  if (pDb->status != TSDB_DB_STATUS_READY) {
    mError("db:%s, status:%d, in dropping", pDb->name, pDb->status);
S
Shengliang Guan 已提交
2587
    mnodeDecDbRef(pDb);
H
Hui Li 已提交
2588 2589
    return 0;
  }
S
slguan 已提交
2590

H
Haojun Liao 已提交
2591
  int32_t cols       = 0;
S
slguan 已提交
2592 2593 2594 2595 2596
  int32_t numOfRows  = 0;
  SChildTableObj *pTable = NULL;
  SPatternCompareInfo info = PATTERN_COMPARE_INFO_INITIALIZER;

  char prefix[64] = {0};
S
Shengliang Guan 已提交
2597
  tstrncpy(prefix, pDb->name, 64);
S
slguan 已提交
2598 2599 2600
  strcat(prefix, TS_PATH_DELIMITER);
  int32_t prefixLen = strlen(prefix);

B
Bomin Zhang 已提交
2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611
  char* pattern = NULL;
  if (pShow->payloadLen > 0) {
    pattern = (char*)malloc(pShow->payloadLen + 1);
    if (pattern == NULL) {
      terrno = TSDB_CODE_QRY_OUT_OF_MEMORY;
      return 0;
    }
    memcpy(pattern, pShow->payload, pShow->payloadLen);
    pattern[pShow->payloadLen] = 0;
  }

S
slguan 已提交
2612
  while (numOfRows < rows) {
2613
    pShow->pIter = mnodeGetNextChildTable(pShow->pIter, &pTable);
S
slguan 已提交
2614 2615 2616 2617
    if (pTable == NULL) break;

    // not belong to current db
    if (strncmp(pTable->info.tableId, prefix, prefixLen)) {
2618
      mnodeDecTableRef(pTable);
S
slguan 已提交
2619 2620 2621
      continue;
    }

B
Bomin Zhang 已提交
2622
    char tableName[TSDB_TABLE_NAME_LEN] = {0};
S
slguan 已提交
2623
    
2624
    // pattern compare for table name
S
Shengliang Guan 已提交
2625
    mnodeExtractTableName(pTable->info.tableId, tableName);
S
slguan 已提交
2626

B
Bomin Zhang 已提交
2627
    if (pattern != NULL && patternMatch(pattern, tableName, sizeof(tableName) - 1, &info) != TSDB_PATTERN_MATCH) {
2628
      mnodeDecTableRef(pTable);
S
slguan 已提交
2629 2630 2631
      continue;
    }

H
Haojun Liao 已提交
2632
    cols = 0;
S
slguan 已提交
2633
    char *pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
H
hjxilinx 已提交
2634

H
Hui Li 已提交
2635
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, tableName, pShow->bytes[cols]);
S
slguan 已提交
2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651
    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;
2652
    
B
Bomin Zhang 已提交
2653
    memset(tableName, 0, sizeof(tableName));
S
slguan 已提交
2654
    if (pTable->info.type == TSDB_CHILD_TABLE) {
S
Shengliang Guan 已提交
2655
      mnodeExtractTableName(pTable->superTable->info.tableId, tableName);
H
Hui Li 已提交
2656
      STR_WITH_MAXSIZE_TO_VARSTR(pWrite, tableName, pShow->bytes[cols]);
S
slguan 已提交
2657
    }
2658
    
S
slguan 已提交
2659 2660
    cols++;

H
Haojun Liao 已提交
2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676
    // uid
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int64_t*) pWrite = pTable->uid;
    cols++;


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

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

S
slguan 已提交
2677
    numOfRows++;
2678
    mnodeDecTableRef(pTable);
S
slguan 已提交
2679 2680 2681 2682
  }

  pShow->numOfReads += numOfRows;

2683
  mnodeVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
2684
  mnodeDecDbRef(pDb);
B
Bomin Zhang 已提交
2685
  free(pattern);
S
slguan 已提交
2686 2687 2688 2689

  return numOfRows;
}

2690
static int32_t mnodeProcessAlterTableMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
2691
  SCMAlterTableMsg *pAlter = pMsg->rpcMsg.pCont;
2692
  mDebug("app:%p:%p, table:%s, alter table msg is received from thandle:%p", pMsg->rpcMsg.ahandle, pMsg,
S
Shengliang Guan 已提交
2693
         pAlter->tableId, pMsg->rpcMsg.handle);
S
[TD-17]  
slguan 已提交
2694

2695
  if (pMsg->pDb == NULL) pMsg->pDb = mnodeGetDbByTableId(pAlter->tableId);
H
Hui Li 已提交
2696
  if (pMsg->pDb == NULL) {
S
Shengliang Guan 已提交
2697
    mError("app:%p:%p, table:%s, failed to alter table, db not selected", pMsg->rpcMsg.ahandle, pMsg, pAlter->tableId);
2698
    return TSDB_CODE_MND_DB_NOT_SELECTED;
S
[TD-17]  
slguan 已提交
2699
  }
H
Hui Li 已提交
2700 2701 2702 2703 2704
  
  if (pMsg->pDb->status != TSDB_DB_STATUS_READY) {
    mError("db:%s, status:%d, in dropping", pMsg->pDb->name, pMsg->pDb->status);
    return TSDB_CODE_MND_DB_IN_DROPPING;
  }
S
[TD-17]  
slguan 已提交
2705

2706
  if (mnodeCheckIsMonitorDB(pMsg->pDb->name, tsMonitorDbName)) {
S
Shengliang Guan 已提交
2707
    mError("app:%p:%p, table:%s, failed to alter table, its log db", pMsg->rpcMsg.ahandle, pMsg, pAlter->tableId);
2708
    return TSDB_CODE_MND_MONITOR_DB_FORBIDDEN;
S
[TD-17]  
slguan 已提交
2709 2710
  }

2711
  if (pMsg->pTable == NULL) pMsg->pTable = mnodeGetTable(pAlter->tableId);
S
[TD-17]  
slguan 已提交
2712
  if (pMsg->pTable == NULL) {
S
Shengliang Guan 已提交
2713
    mError("app:%p:%p, table:%s, failed to alter table, table not exist", pMsg->rpcMsg.ahandle, pMsg, pAlter->tableId);
2714
    return TSDB_CODE_MND_INVALID_TABLE_NAME;
S
[TD-17]  
slguan 已提交
2715 2716
  }

S
slguan 已提交
2717
  pAlter->type = htons(pAlter->type);
2718 2719
  pAlter->numOfCols = htons(pAlter->numOfCols);
  pAlter->tagValLen = htonl(pAlter->tagValLen);
S
slguan 已提交
2720

S
[TD-17]  
slguan 已提交
2721
  if (pAlter->numOfCols > 2) {
S
Shengliang Guan 已提交
2722 2723
    mError("app:%p:%p, table:%s, error numOfCols:%d in alter table", pMsg->rpcMsg.ahandle, pMsg, pAlter->tableId,
           pAlter->numOfCols);
2724
    return TSDB_CODE_MND_APP_ERROR;
S
[TD-17]  
slguan 已提交
2725 2726 2727 2728 2729 2730
  }

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

2731
  int32_t code = TSDB_CODE_COM_OPS_NOT_SUPPORT;
S
[TD-17]  
slguan 已提交
2732
  if (pMsg->pTable->type == TSDB_SUPER_TABLE) {
2733
    mDebug("app:%p:%p, table:%s, start to alter stable", pMsg->rpcMsg.ahandle, pMsg, pAlter->tableId);
S
[TD-17]  
slguan 已提交
2734
    if (pAlter->type == TSDB_ALTER_TABLE_ADD_TAG_COLUMN) {
2735
      code = mnodeAddSuperTableTag(pMsg, pAlter->schema, 1);
S
[TD-17]  
slguan 已提交
2736
    } else if (pAlter->type == TSDB_ALTER_TABLE_DROP_TAG_COLUMN) {
2737
      code = mnodeDropSuperTableTag(pMsg, pAlter->schema[0].name);
S
[TD-17]  
slguan 已提交
2738
    } else if (pAlter->type == TSDB_ALTER_TABLE_CHANGE_TAG_COLUMN) {
2739
      code = mnodeModifySuperTableTagName(pMsg, pAlter->schema[0].name, pAlter->schema[1].name);
S
[TD-17]  
slguan 已提交
2740
    } else if (pAlter->type == TSDB_ALTER_TABLE_ADD_COLUMN) {
2741
      code = mnodeAddSuperTableColumn(pMsg, pAlter->schema, 1);
S
[TD-17]  
slguan 已提交
2742
    } else if (pAlter->type == TSDB_ALTER_TABLE_DROP_COLUMN) {
2743
      code = mnodeDropSuperTableColumn(pMsg, pAlter->schema[0].name);
S
Shengliang Guan 已提交
2744 2745
    } else if (pAlter->type == TSDB_ALTER_TABLE_CHANGE_COLUMN) {
      code = mnodeChangeSuperTableColumn(pMsg, pAlter->schema[0].name, pAlter->schema[1].name);
S
[TD-17]  
slguan 已提交
2746 2747
    } else {
    }
S
slguan 已提交
2748
  } else {
2749
    mDebug("app:%p:%p, table:%s, start to alter ctable", pMsg->rpcMsg.ahandle, pMsg, pAlter->tableId);
S
[TD-17]  
slguan 已提交
2750
    if (pAlter->type == TSDB_ALTER_TABLE_UPDATE_TAG_VAL) {
2751
      return TSDB_CODE_COM_OPS_NOT_SUPPORT;
S
[TD-17]  
slguan 已提交
2752
    } else if (pAlter->type == TSDB_ALTER_TABLE_ADD_COLUMN) {
2753
      code = mnodeAddNormalTableColumn(pMsg, pAlter->schema, 1);
S
[TD-17]  
slguan 已提交
2754
    } else if (pAlter->type == TSDB_ALTER_TABLE_DROP_COLUMN) {
2755
      code = mnodeDropNormalTableColumn(pMsg, pAlter->schema[0].name);
S
Shengliang Guan 已提交
2756 2757
    } else if (pAlter->type == TSDB_ALTER_TABLE_CHANGE_COLUMN) {
      code = mnodeChangeNormalTableColumn(pMsg, pAlter->schema[0].name, pAlter->schema[1].name);
S
[TD-17]  
slguan 已提交
2758 2759
    } else {
    }
S
slguan 已提交
2760 2761
  }

2762
 return code;
S
[TD-17]  
slguan 已提交
2763
}
2764

S
Shengliang Guan 已提交
2765
static int32_t mnodeGetStreamTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
2766
  SDbObj *pDb = mnodeGetDb(pShow->db);
2767
  if (pDb == NULL) return TSDB_CODE_MND_DB_NOT_SELECTED;
H
Hui Li 已提交
2768 2769 2770
  
  if (pDb->status != TSDB_DB_STATUS_READY) {
    mError("db:%s, status:%d, in dropping", pDb->name, pDb->status);
S
Shengliang Guan 已提交
2771
    mnodeDecDbRef(pDb);
H
Hui Li 已提交
2772 2773
    return TSDB_CODE_MND_DB_IN_DROPPING;
  }
2774 2775 2776 2777

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

H
Haojun Liao 已提交
2778 2779 2780
  SSchema tbnameColSchema = tGetTableNameColumnSchema();
  pShow->bytes[cols] = tbnameColSchema.bytes;
  pSchema[cols].type = tbnameColSchema.type;
2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817
  strcpy(pSchema[cols].name, "table_name");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
  strcpy(pSchema[cols].name, "created_time");
  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] = TSDB_MAX_SQL_SHOW_LEN + VARSTR_HEADER_SIZE;
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
  strcpy(pSchema[cols].name, "sql");
  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];

  mnodeDecDbRef(pDb);
  return 0;
}

S
Shengliang Guan 已提交
2818
static int32_t mnodeRetrieveStreamTables(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
2819 2820
  SDbObj *pDb = mnodeGetDb(pShow->db);
  if (pDb == NULL) return 0;
H
Hui Li 已提交
2821 2822 2823
  
  if (pDb->status != TSDB_DB_STATUS_READY) {
    mError("db:%s, status:%d, in dropping", pDb->name, pDb->status);
S
Shengliang Guan 已提交
2824
    mnodeDecDbRef(pDb);
H
Hui Li 已提交
2825 2826
    return 0;
  }
2827 2828 2829 2830 2831 2832
  
  int32_t numOfRows  = 0;
  SChildTableObj *pTable = NULL;
  SPatternCompareInfo info = PATTERN_COMPARE_INFO_INITIALIZER;

  char prefix[64] = {0};
S
Shengliang Guan 已提交
2833
  tstrncpy(prefix, pDb->name, 64);
2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846
  strcat(prefix, TS_PATH_DELIMITER);
  int32_t prefixLen = strlen(prefix);

  while (numOfRows < rows) {
    pShow->pIter = mnodeGetNextChildTable(pShow->pIter, &pTable);
    if (pTable == NULL) break;
    
    // not belong to current db
    if (strncmp(pTable->info.tableId, prefix, prefixLen) || pTable->info.type != TSDB_STREAM_TABLE) {
      mnodeDecTableRef(pTable);
      continue;
    }

B
Bomin Zhang 已提交
2847
    char tableName[TSDB_TABLE_NAME_LEN] = {0};
2848 2849 2850 2851
    
    // pattern compare for table name
    mnodeExtractTableName(pTable->info.tableId, tableName);

B
Bomin Zhang 已提交
2852
    if (pShow->payloadLen > 0 && patternMatch(pShow->payload, tableName, sizeof(tableName) - 1, &info) != TSDB_PATTERN_MATCH) {
2853 2854 2855 2856 2857 2858 2859 2860
      mnodeDecTableRef(pTable);
      continue;
    }

    int32_t cols = 0;

    char *pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;

H
Hui Li 已提交
2861
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, tableName, pShow->bytes[cols]);
2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872
    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;
H
Hui Li 已提交
2873
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, pTable->sql, pShow->bytes[cols]);    
2874 2875 2876 2877 2878 2879 2880 2881
    cols++;

    numOfRows++;
    mnodeDecTableRef(pTable);
  }

  pShow->numOfReads += numOfRows;

2882
  mnodeVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
2883 2884 2885
  mnodeDecDbRef(pDb);

  return numOfRows;
2886
}