mnodeTable.c 84.4 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

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

49 50
static void *  tsChildTableSdb;
static void *  tsSuperTableSdb;
S
slguan 已提交
51 52
static int32_t tsChildTableUpdateSize;
static int32_t tsSuperTableUpdateSize;
53 54 55 56 57 58 59 60 61 62
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);
63
static int32_t mnodeRetrieveShowSuperTables(SShowObj *pShow, char *data, int32_t rows, void *pConn);
S
Shengliang Guan 已提交
64 65
static int32_t mnodeGetStreamTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn);
static int32_t mnodeRetrieveStreamTables(SShowObj *pShow, char *data, int32_t rows, void *pConn);
66
 
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
static int32_t mnodeProcessCreateTableMsg(SMnodeMsg *mnodeMsg);
static int32_t mnodeProcessCreateSuperTableMsg(SMnodeMsg *pMsg);
static int32_t mnodeProcessCreateChildTableMsg(SMnodeMsg *pMsg);
static void    mnodeProcessCreateChildTableRsp(SRpcMsg *rpcMsg);

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

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

static int32_t mnodeProcessTableMetaMsg(SMnodeMsg *mnodeMsg);
static int32_t mnodeGetSuperTableMeta(SMnodeMsg *pMsg);
static int32_t mnodeGetChildTableMeta(SMnodeMsg *pMsg);
S
Shengliang Guan 已提交
85
static int32_t mnodeAutoCreateChildTable(SMnodeMsg *pMsg);
86 87 88 89 90

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

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

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

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

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

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

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

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

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

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

  return TSDB_CODE_SUCCESS;
}

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

151 152 153 154 155 156 157
  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 已提交
158 159

  if (pTable->info.type == TSDB_CHILD_TABLE) {
160 161 162 163 164 165
    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 已提交
166 167
  } else {
    grantRestore(TSDB_GRANT_TIMESERIES, pTable->numOfColumns - 1);
168
    if (pAcct != NULL) pAcct->acctInfo.numOfTimeSeries -= (pTable->numOfColumns - 1);
S
slguan 已提交
169
  }
170 171 172 173 174 175 176
  
  if (pDb != NULL) mnodeRemoveTableFromDb(pDb);
  if (pVgroup != NULL) mnodeRemoveTableFromVgroup(pVgroup, pTable);

  mnodeDecVgroupRef(pVgroup);
  mnodeDecDbRef(pDb);
  mnodeDecAcctRef(pAcct);
S
slguan 已提交
177 178 179 180
 
  return TSDB_CODE_SUCCESS;
}

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

S
slguan 已提交
205 206 207
  return TSDB_CODE_SUCCESS;
}

208
static int32_t mnodeChildTableActionEncode(SSdbOper *pOper) {
S
slguan 已提交
209 210 211
  SChildTableObj *pTable = pOper->pObj;
  assert(pTable != NULL && pOper->rowData != NULL);

212
  int32_t len = strlen(pTable->info.tableId);
B
Bomin Zhang 已提交
213
  if (len >= TSDB_TABLE_ID_LEN) return TSDB_CODE_MND_INVALID_TABLE_ID;
214 215 216 217 218 219 220 221 222

  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 已提交
223
    int32_t schemaSize = pTable->numOfColumns * sizeof(SSchema);
224 225 226 227 228 229
    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 已提交
230 231 232
    }
  }

233 234
  pOper->rowSize = len;

S
slguan 已提交
235 236 237
  return TSDB_CODE_SUCCESS;
}

238
static int32_t mnodeChildTableActionDecode(SSdbOper *pOper) {
S
slguan 已提交
239 240
  assert(pOper->rowData != NULL);
  SChildTableObj *pTable = calloc(1, sizeof(SChildTableObj));
241
  if (pTable == NULL) return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
242

243
  int32_t len = strlen(pOper->rowData);
B
Bomin Zhang 已提交
244
  if (len >= TSDB_TABLE_ID_LEN) {
S
Shuduo Sang 已提交
245
    free(pTable);
246
    return TSDB_CODE_MND_INVALID_TABLE_ID;
S
Shuduo Sang 已提交
247
  }
248 249 250 251 252
  pTable->info.tableId = strdup(pOper->rowData);
  len++;

  memcpy((char*)pTable + sizeof(char *), pOper->rowData + len, tsChildTableUpdateSize);
  len += tsChildTableUpdateSize;
S
slguan 已提交
253 254 255 256 257

  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 已提交
258
      mnodeDestroyChildTable(pTable);
259
      return TSDB_CODE_MND_INVALID_TABLE_TYPE;
S
slguan 已提交
260
    }
261 262 263 264 265 266
    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 已提交
267
        mnodeDestroyChildTable(pTable);
268
        return TSDB_CODE_MND_OUT_OF_MEMORY;
269 270
      }
      memcpy(pTable->sql, pOper->rowData + len, pTable->sqlLen);
S
slguan 已提交
271 272 273 274 275 276 277
    }
  }

  pOper->pObj = pTable;
  return TSDB_CODE_SUCCESS;
}

278
static int32_t mnodeChildTableActionRestored() {
S
Shengliang Guan 已提交
279
  void *pIter = NULL;
S
slguan 已提交
280 281 282
  SChildTableObj *pTable = NULL;

  while (1) {
283
    pIter = mnodeGetNextChildTable(pIter, &pTable);
S
slguan 已提交
284 285
    if (pTable == NULL) break;

286
    SDbObj *pDb = mnodeGetDbByTableId(pTable->info.tableId);
S
slguan 已提交
287 288
    if (pDb == NULL) {
      mError("ctable:%s, failed to get db, discard it", pTable->info.tableId);
S
Shengliang Guan 已提交
289
      SSdbOper desc = {.type = SDB_OPER_LOCAL, .pObj = pTable, .table = tsChildTableSdb};
S
slguan 已提交
290
      sdbDeleteRow(&desc);
291
      mnodeDecTableRef(pTable);
S
slguan 已提交
292 293
      continue;
    }
294
    mnodeDecDbRef(pDb);
S
slguan 已提交
295

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

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

    if (pVgroup->tableList == NULL) {
S
slguan 已提交
318
      mError("ctable:%s, vgId:%d tableList is null", pTable->info.tableId, pTable->vgId);
S
slguan 已提交
319
      pTable->vgId = 0;
S
Shengliang Guan 已提交
320
      SSdbOper desc = {.type = SDB_OPER_LOCAL, .pObj = pTable, .table = tsChildTableSdb};
S
slguan 已提交
321
      sdbDeleteRow(&desc);
322
      mnodeDecTableRef(pTable);
S
slguan 已提交
323 324 325 326
      continue;
    }

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

339
    mnodeDecTableRef(pTable);
S
slguan 已提交
340 341
  }

S
Shengliang Guan 已提交
342 343
  sdbFreeIter(pIter);

S
slguan 已提交
344 345 346
  return 0;
}

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

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

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

S
slguan 已提交
373
  mTrace("table:ctables is created");
S
slguan 已提交
374 375 376
  return 0;
}

377
static void mnodeCleanupChildTables() {
S
slguan 已提交
378 379 380
  sdbCloseTable(tsChildTableSdb);
}

381
static void mnodeAddTableIntoStable(SSuperTableObj *pStable, SChildTableObj *pCtable) {
382
  pStable->numOfTables++;
S
slguan 已提交
383

384
  if (pStable->vgHash == NULL) {
385
    pStable->vgHash = taosHashInit(100000, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false);
S
slguan 已提交
386 387
  }

388 389 390
  if (pStable->vgHash != NULL) {
    taosHashPut(pStable->vgHash, (char *)&pCtable->vgId, sizeof(pCtable->vgId), &pCtable->vgId, sizeof(pCtable->vgId));
  }
S
slguan 已提交
391 392
}

393
static void mnodeRemoveTableFromStable(SSuperTableObj *pStable, SChildTableObj *pCtable) {
S
slguan 已提交
394
  pStable->numOfTables--;
395 396 397

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

398
  SVgObj *pVgroup = mnodeGetVgroup(pCtable->vgId);
399
  if (pVgroup == NULL) {
400 401
    taosHashRemove(pStable->vgHash, (char *)&pCtable->vgId, sizeof(pCtable->vgId));
  }
402
  mnodeDecVgroupRef(pVgroup);
S
slguan 已提交
403 404
}

S
Shengliang Guan 已提交
405
static void mnodeDestroySuperTable(SSuperTableObj *pStable) {
406 407 408 409
  if (pStable->vgHash != NULL) {
    taosHashCleanup(pStable->vgHash);
    pStable->vgHash = NULL;
  }
410
  tfree(pStable->info.tableId);
S
slguan 已提交
411 412 413 414
  tfree(pStable->schema);
  tfree(pStable);
}

415
static int32_t mnodeSuperTableActionDestroy(SSdbOper *pOper) {
S
Shengliang Guan 已提交
416
  mnodeDestroySuperTable(pOper->pObj);
S
slguan 已提交
417 418 419
  return TSDB_CODE_SUCCESS;
}

420
static int32_t mnodeSuperTableActionInsert(SSdbOper *pOper) {
S
slguan 已提交
421
  SSuperTableObj *pStable = pOper->pObj;
422
  SDbObj *pDb = mnodeGetDbByTableId(pStable->info.tableId);
S
slguan 已提交
423
  if (pDb != NULL) {
424
    mnodeAddSuperTableIntoDb(pDb);
S
slguan 已提交
425
  }
426
  mnodeDecDbRef(pDb);
S
slguan 已提交
427 428 429 430

  return TSDB_CODE_SUCCESS;
}

431
static int32_t mnodeSuperTableActionDelete(SSdbOper *pOper) {
S
slguan 已提交
432
  SSuperTableObj *pStable = pOper->pObj;
433
  SDbObj *pDb = mnodeGetDbByTableId(pStable->info.tableId);
S
slguan 已提交
434
  if (pDb != NULL) {
435 436
    mnodeRemoveSuperTableFromDb(pDb);
    mnodeDropAllChildTablesInStable((SSuperTableObj *)pStable);
S
slguan 已提交
437
  }
438
  mnodeDecDbRef(pDb);
S
slguan 已提交
439

S
slguan 已提交
440 441 442
  return TSDB_CODE_SUCCESS;
}

443
static int32_t mnodeSuperTableActionUpdate(SSdbOper *pOper) {
S
slguan 已提交
444
  SSuperTableObj *pNew = pOper->pObj;
445
  SSuperTableObj *pTable = mnodeGetSuperTable(pNew->info.tableId);
S
slguan 已提交
446
  if (pTable != pNew) {
447
    void *oldTableId = pTable->info.tableId;
S
slguan 已提交
448
    void *oldSchema = pTable->schema;
449 450 451
    void *oldVgHash = pTable->vgHash;
    int32_t oldRefCount = pTable->refCount;

452
    memcpy(pTable, pNew, sizeof(SSuperTableObj));
453 454 455

    pTable->vgHash = oldVgHash;
    pTable->refCount = oldRefCount;
S
slguan 已提交
456
    pTable->schema = pNew->schema;
457
    free(pNew);
458
    free(oldTableId);
S
slguan 已提交
459 460
    free(oldSchema);
  }
461
  mnodeDecTableRef(pTable);
S
slguan 已提交
462 463 464
  return TSDB_CODE_SUCCESS;
}

465
static int32_t mnodeSuperTableActionEncode(SSdbOper *pOper) {
S
slguan 已提交
466 467 468
  SSuperTableObj *pStable = pOper->pObj;
  assert(pOper->pObj != NULL && pOper->rowData != NULL);

469
  int32_t len = strlen(pStable->info.tableId);
B
Bomin Zhang 已提交
470
  if (len >= TSDB_TABLE_ID_LEN) len = TSDB_CODE_MND_INVALID_TABLE_ID;
S
slguan 已提交
471

472 473 474 475 476 477 478 479 480 481
  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 已提交
482

483
  pOper->rowSize = len;
S
slguan 已提交
484 485 486 487

  return TSDB_CODE_SUCCESS;
}

488
static int32_t mnodeSuperTableActionDecode(SSdbOper *pOper) {
S
slguan 已提交
489 490
  assert(pOper->rowData != NULL);
  SSuperTableObj *pStable = (SSuperTableObj *) calloc(1, sizeof(SSuperTableObj));
491
  if (pStable == NULL) return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
492

493
  int32_t len = strlen(pOper->rowData);
B
Bomin Zhang 已提交
494
  if (len >= TSDB_TABLE_ID_LEN){
495
    free(pStable);
496
    return TSDB_CODE_MND_INVALID_TABLE_ID;
497
  }
498 499 500 501 502
  pStable->info.tableId = strdup(pOper->rowData);
  len++;

  memcpy((char*)pStable + sizeof(char *), pOper->rowData + len, tsSuperTableUpdateSize);
  len += tsSuperTableUpdateSize;
S
slguan 已提交
503 504 505 506

  int32_t schemaSize = sizeof(SSchema) * (pStable->numOfColumns + pStable->numOfTags);
  pStable->schema = malloc(schemaSize);
  if (pStable->schema == NULL) {
S
Shengliang Guan 已提交
507
    mnodeDestroySuperTable(pStable);
508
    return TSDB_CODE_MND_NOT_SUPER_TABLE;
S
slguan 已提交
509 510
  }

511 512
  memcpy(pStable->schema, pOper->rowData + len, schemaSize);
  
S
slguan 已提交
513 514 515 516 517
  pOper->pObj = pStable;

  return TSDB_CODE_SUCCESS;
}

518
static int32_t mnodeSuperTableActionRestored() {
S
slguan 已提交
519 520 521
  return 0;
}

522
static int32_t mnodeInitSuperTables() {
S
slguan 已提交
523
  SSuperTableObj tObj;
524
  tsSuperTableUpdateSize = (int8_t *)tObj.updateEnd - (int8_t *)&tObj.info.type;
S
slguan 已提交
525 526

  SSdbTableDesc tableDesc = {
S
slguan 已提交
527
    .tableId      = SDB_TABLE_STABLE,
S
slguan 已提交
528
    .tableName    = "stables",
S
Shengliang Guan 已提交
529
    .hashSessions = TSDB_DEFAULT_STABLES_HASH_SIZE,
530
    .maxRowSize   = sizeof(SSuperTableObj) + sizeof(SSchema) * (TSDB_MAX_TAGS + TSDB_MAX_COLUMNS + 16) + TSDB_TABLE_ID_LEN,
S
slguan 已提交
531
    .refCountPos  = (int8_t *)(&tObj.refCount) - (int8_t *)&tObj,
532
    .keyType      = SDB_KEY_VAR_STRING,
533 534 535 536 537 538 539
    .insertFp     = mnodeSuperTableActionInsert,
    .deleteFp     = mnodeSuperTableActionDelete,
    .updateFp     = mnodeSuperTableActionUpdate,
    .encodeFp     = mnodeSuperTableActionEncode,
    .decodeFp     = mnodeSuperTableActionDecode,
    .destroyFp    = mnodeSuperTableActionDestroy,
    .restoredFp   = mnodeSuperTableActionRestored
S
slguan 已提交
540 541 542 543 544 545 546 547
  };

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

S
slguan 已提交
548
  mTrace("table:stables is created");
S
slguan 已提交
549 550 551
  return 0;
}

552
static void mnodeCleanupSuperTables() {
S
slguan 已提交
553 554 555
  sdbCloseTable(tsSuperTableSdb);
}

556 557
int32_t mnodeInitTables() {
  int32_t code = mnodeInitSuperTables();
S
slguan 已提交
558 559
  if (code != TSDB_CODE_SUCCESS) {
    return code;
H
hzcheng 已提交
560 561
  }

562
  code = mnodeInitChildTables();
S
slguan 已提交
563 564 565
  if (code != TSDB_CODE_SUCCESS) {
    return code;
  }
H
hzcheng 已提交
566

567 568 569 570 571 572
  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 已提交
573
  
574 575 576 577
  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 已提交
578

579
  mnodeAddPeerMsgHandle(TSDB_MSG_TYPE_DM_CONFIG_TABLE, mnodeProcessTableCfgMsg);
S
slguan 已提交
580

581 582 583 584
  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 已提交
585 586
  mnodeAddShowMetaHandle(TSDB_MGMT_TABLE_STREAMTABLES, mnodeGetStreamTableMeta);
  mnodeAddShowRetrieveHandle(TSDB_MGMT_TABLE_STREAMTABLES, mnodeRetrieveStreamTables);
587

S
slguan 已提交
588
  return TSDB_CODE_SUCCESS;
H
hzcheng 已提交
589 590
}

591
static void *mnodeGetChildTable(char *tableId) {
S
slguan 已提交
592 593 594
  return sdbGetRow(tsChildTableSdb, tableId);
}

595
static void *mnodeGetSuperTable(char *tableId) {
S
slguan 已提交
596 597 598
  return sdbGetRow(tsSuperTableSdb, tableId);
}

599
static void *mnodeGetSuperTableByUid(uint64_t uid) {
600
  SSuperTableObj *pStable = NULL;
S
Shengliang Guan 已提交
601
  void *pIter = NULL;
602 603

  while (1) {
604
    pIter = mnodeGetNextSuperTable(pIter, &pStable);
605 606
    if (pStable == NULL) break;
    if (pStable->uid == uid) {
607
      sdbFreeIter(pIter);
608 609
      return pStable;
    }
610
    mnodeDecTableRef(pStable);
611 612
  }

S
Shengliang Guan 已提交
613 614
  sdbFreeIter(pIter);

615 616 617
  return NULL;
}

618 619
void *mnodeGetTable(char *tableId) {
  void *pTable = mnodeGetSuperTable(tableId);
620 621
  if (pTable != NULL) {
    return pTable;
H
hzcheng 已提交
622 623
  }

624
  pTable = mnodeGetChildTable(tableId);
625 626
  if (pTable != NULL) {
    return pTable;
S
#1177  
slguan 已提交
627
  }
H
hzcheng 已提交
628

S
slguan 已提交
629
  return NULL;
S
#1177  
slguan 已提交
630
}
H
hzcheng 已提交
631

632
void *mnodeGetNextChildTable(void *pIter, SChildTableObj **pTable) {
S
Shengliang Guan 已提交
633
  return sdbFetchRow(tsChildTableSdb, pIter, (void **)pTable);
634 635
}

636
void *mnodeGetNextSuperTable(void *pIter, SSuperTableObj **pTable) {
S
Shengliang Guan 已提交
637
  return sdbFetchRow(tsSuperTableSdb, pIter, (void **)pTable);
638 639
}

640
void mnodeIncTableRef(void *p1) {
S
slguan 已提交
641
  STableObj *pTable = (STableObj *)p1;
S
slguan 已提交
642 643 644 645 646 647 648
  if (pTable->type == TSDB_SUPER_TABLE) {
    sdbIncRef(tsSuperTableSdb, pTable);
  } else {
    sdbIncRef(tsChildTableSdb, pTable);
  }
}

649
void mnodeDecTableRef(void *p1) {
S
slguan 已提交
650 651
  if (p1 == NULL) return;

S
slguan 已提交
652
  STableObj *pTable = (STableObj *)p1;
S
slguan 已提交
653 654 655 656 657 658 659
  if (pTable->type == TSDB_SUPER_TABLE) {
    sdbDecRef(tsSuperTableSdb, pTable);
  } else {
    sdbDecRef(tsChildTableSdb, pTable);
  }
}

660 661 662
void mnodeCleanupTables() {
  mnodeCleanupChildTables();
  mnodeCleanupSuperTables();
S
#1177  
slguan 已提交
663
}
H
hzcheng 已提交
664

665
// todo move to name.h, add length of table name
S
Shengliang Guan 已提交
666
static void mnodeExtractTableName(char* tableId, char* name) {
667 668 669 670 671
  int pos = -1;
  int num = 0;
  for (pos = 0; tableId[pos] != 0; ++pos) {
    if (tableId[pos] == '.') num++;
    if (num == 2) break;
S
slguan 已提交
672 673
  }

674 675
  if (num == 2) {
    strcpy(name, tableId + pos + 1);
S
slguan 已提交
676
  }
H
hzcheng 已提交
677 678
}

679
static int32_t mnodeProcessCreateTableMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
680
  SCMCreateTableMsg *pCreate = pMsg->rpcMsg.pCont;
S
slguan 已提交
681
  
682
  if (pMsg->pDb == NULL) pMsg->pDb = mnodeGetDb(pCreate->db);
S
slguan 已提交
683
  if (pMsg->pDb == NULL || pMsg->pDb->status != TSDB_DB_STATUS_READY) {
S
Shengliang Guan 已提交
684
    mError("app:%p:%p, table:%s, failed to create, db not selected", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId);
685
    return TSDB_CODE_MND_DB_NOT_SELECTED;
S
slguan 已提交
686 687
  }

688
  if (pMsg->pTable == NULL) pMsg->pTable = mnodeGetTable(pCreate->tableId);
689
  if (pMsg->pTable != NULL && pMsg->retry == 0) {
S
slguan 已提交
690
    if (pCreate->getMeta) {
S
Shengliang Guan 已提交
691
      mTrace("app:%p:%p, table:%s, continue to get meta", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId);
692
      return mnodeGetChildTableMeta(pMsg);
S
slguan 已提交
693
    } else if (pCreate->igExists) {
S
Shengliang Guan 已提交
694
      mTrace("app:%p:%p, table:%s, is already exist", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId);
695
      return TSDB_CODE_SUCCESS;
S
slguan 已提交
696
    } else {
S
Shengliang Guan 已提交
697 698
      mError("app:%p:%p, table:%s, failed to create, table already exist", pMsg->rpcMsg.ahandle, pMsg,
             pCreate->tableId);
699
      return TSDB_CODE_MND_TABLE_ALREADY_EXIST;
S
slguan 已提交
700
    }
S
slguan 已提交
701 702
  }

703
  if (pCreate->numOfTags != 0) {
704 705
    mTrace("app:%p:%p, table:%s, create stable msg is received from thandle:%p", pMsg->rpcMsg.ahandle, pMsg,
           pCreate->tableId, pMsg->rpcMsg.handle);
706
    return mnodeProcessCreateSuperTableMsg(pMsg);
707
  } else {
708 709
    mTrace("app:%p:%p, table:%s, create ctable msg is received from thandle:%p", pMsg->rpcMsg.ahandle, pMsg,
           pCreate->tableId, pMsg->rpcMsg.handle);
710
    return mnodeProcessCreateChildTableMsg(pMsg);
S
slguan 已提交
711 712 713
  }
}

714
static int32_t mnodeProcessDropTableMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
715
  SCMDropTableMsg *pDrop = pMsg->rpcMsg.pCont;
716
  if (pMsg->pDb == NULL) pMsg->pDb = mnodeGetDbByTableId(pDrop->tableId);
S
slguan 已提交
717
  if (pMsg->pDb == NULL || pMsg->pDb->status != TSDB_DB_STATUS_READY) {
S
Shengliang Guan 已提交
718
    mError("app:%p:%p, table:%s, failed to drop table, db not selected", pMsg->rpcMsg.ahandle, pMsg, pDrop->tableId);
719
    return TSDB_CODE_MND_DB_NOT_SELECTED;
S
slguan 已提交
720 721
  }

722
  if (mnodeCheckIsMonitorDB(pMsg->pDb->name, tsMonitorDbName)) {
S
Shengliang Guan 已提交
723 724
    mError("app:%p:%p, table:%s, failed to drop table, in monitor database", pMsg->rpcMsg.ahandle, pMsg,
           pDrop->tableId);
725
    return TSDB_CODE_MND_MONITOR_DB_FORBIDDEN;
726 727
  }

728
  if (pMsg->pTable == NULL) pMsg->pTable = mnodeGetTable(pDrop->tableId);
S
slguan 已提交
729
  if (pMsg->pTable == NULL) {
S
slguan 已提交
730
    if (pDrop->igNotExists) {
S
Shengliang Guan 已提交
731
      mTrace("app:%p:%p, table:%s, table is not exist, think drop success", pMsg->rpcMsg.ahandle, pMsg, pDrop->tableId);
732
      return TSDB_CODE_SUCCESS;
S
slguan 已提交
733
    } else {
S
Shengliang Guan 已提交
734
      mError("app:%p:%p, table:%s, failed to drop table, table not exist", pMsg->rpcMsg.ahandle, pMsg, pDrop->tableId);
735
      return TSDB_CODE_MND_INVALID_TABLE_NAME;
S
slguan 已提交
736 737 738
    }
  }

S
slguan 已提交
739
  if (pMsg->pTable->type == TSDB_SUPER_TABLE) {
S
Shengliang Guan 已提交
740
    mPrint("app:%p:%p, table:%s, start to drop stable", pMsg->rpcMsg.ahandle, pMsg, pDrop->tableId);
741
    return mnodeProcessDropSuperTableMsg(pMsg);
S
slguan 已提交
742
  } else {
S
Shengliang Guan 已提交
743
    mPrint("app:%p:%p, table:%s, start to drop ctable", pMsg->rpcMsg.ahandle, pMsg, pDrop->tableId);
744
    return mnodeProcessDropChildTableMsg(pMsg);
S
slguan 已提交
745 746 747
  }
}

748
static int32_t mnodeProcessTableMetaMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
749
  SCMTableInfoMsg *pInfo = pMsg->rpcMsg.pCont;
S
slguan 已提交
750
  pInfo->createFlag = htons(pInfo->createFlag);
S
Shengliang Guan 已提交
751 752
  mTrace("app:%p:%p, table:%s, table meta msg is received from thandle:%p, createFlag:%d", pMsg->rpcMsg.ahandle, pMsg,
         pInfo->tableId, pMsg->rpcMsg.handle, pInfo->createFlag);
S
slguan 已提交
753

754
  if (pMsg->pDb == NULL) pMsg->pDb = mnodeGetDbByTableId(pInfo->tableId);
S
slguan 已提交
755
  if (pMsg->pDb == NULL || pMsg->pDb->status != TSDB_DB_STATUS_READY) {
S
Shengliang Guan 已提交
756 757
    mError("app:%p:%p, table:%s, failed to get table meta, db not selected", pMsg->rpcMsg.ahandle, pMsg,
           pInfo->tableId);
758
    return TSDB_CODE_MND_DB_NOT_SELECTED;
S
slguan 已提交
759 760
  }

761
  if (pMsg->pTable == NULL) pMsg->pTable = mnodeGetTable(pInfo->tableId);
S
slguan 已提交
762
  if (pMsg->pTable == NULL) {
S
slguan 已提交
763
    if (!pInfo->createFlag) {
S
Shengliang Guan 已提交
764 765
      mError("app:%p:%p, table:%s, failed to get table meta, table not exist", pMsg->rpcMsg.ahandle, pMsg,
             pInfo->tableId);
766
      return TSDB_CODE_MND_INVALID_TABLE_NAME;
S
slguan 已提交
767
    } else {
S
Shengliang Guan 已提交
768 769
      mTrace("app:%p:%p, table:%s, failed to get table meta, start auto create table ", pMsg->rpcMsg.ahandle, pMsg,
             pInfo->tableId);
S
Shengliang Guan 已提交
770
      return mnodeAutoCreateChildTable(pMsg);
S
slguan 已提交
771
    }
S
slguan 已提交
772
  } else {
S
slguan 已提交
773
    if (pMsg->pTable->type != TSDB_SUPER_TABLE) {
774
      return mnodeGetChildTableMeta(pMsg);
S
slguan 已提交
775
    } else {
776
      return mnodeGetSuperTableMeta(pMsg);
S
slguan 已提交
777 778 779 780
    }
  }
}

781
static int32_t mnodeProcessCreateSuperTableMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
782
  SCMCreateTableMsg *pCreate = pMsg->rpcMsg.pCont;
S
Shengliang Guan 已提交
783
  SSuperTableObj *   pStable = calloc(1, sizeof(SSuperTableObj));
S
slguan 已提交
784
  if (pStable == NULL) {
S
Shengliang Guan 已提交
785
    mError("app:%p:%p, table:%s, failed to create, no enough memory", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId);
786
    return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
787 788
  }

789
  pStable->info.tableId = strdup(pCreate->tableId);
S
slguan 已提交
790 791 792 793
  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 已提交
794
  pStable->tversion     = 0;
S
slguan 已提交
795 796 797
  pStable->numOfColumns = htons(pCreate->numOfColumns);
  pStable->numOfTags    = htons(pCreate->numOfTags);

S
slguan 已提交
798
  int32_t numOfCols = pStable->numOfColumns + pStable->numOfTags;
S
slguan 已提交
799 800 801 802
  int32_t schemaSize = numOfCols * sizeof(SSchema);
  pStable->schema = (SSchema *)calloc(1, schemaSize);
  if (pStable->schema == NULL) {
    free(pStable);
S
Shengliang Guan 已提交
803
    mError("app:%p:%p, table:%s, failed to create, no schema input", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId);
804
    return TSDB_CODE_MND_INVALID_TABLE_NAME;
S
slguan 已提交
805 806 807 808 809 810 811 812
  }
  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 已提交
813 814 815
    
    // 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 已提交
816 817
  }

S
slguan 已提交
818
  SSdbOper oper = {
S
slguan 已提交
819
    .type = SDB_OPER_GLOBAL,
S
slguan 已提交
820 821
    .table = tsSuperTableSdb,
    .pObj = pStable,
822 823
    .rowSize = sizeof(SSuperTableObj) + schemaSize,
    .pMsg = pMsg
S
slguan 已提交
824 825 826 827
  };

  int32_t code = sdbInsertRow(&oper);
  if (code != TSDB_CODE_SUCCESS) {
S
Shengliang Guan 已提交
828
    mnodeDestroySuperTable(pStable);
S
Shengliang Guan 已提交
829
    mError("app:%p:%p, table:%s, failed to create, sdb error", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId);
S
slguan 已提交
830
  } else {
S
Shengliang Guan 已提交
831 832
    mLPrint("app:%p:%p, table:%s, is created, tags:%d fields:%d", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
            pStable->numOfTags, pStable->numOfColumns);
833
    if (pMsg != NULL) code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
slguan 已提交
834
  }
835 836

  return code;
S
slguan 已提交
837 838
}

839
static int32_t mnodeProcessDropSuperTableMsg(SMnodeMsg *pMsg) {
S
slguan 已提交
840
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
S
slguan 已提交
841
  if (pStable->numOfTables != 0) {
842 843 844
    SHashMutableIterator *pIter = taosHashCreateIter(pStable->vgHash);
    while (taosHashIterNext(pIter)) {
      int32_t *pVgId = taosHashIterGet(pIter);
845
      SVgObj *pVgroup = mnodeGetVgroup(*pVgId);
guanshengliang's avatar
guanshengliang 已提交
846
      if (pVgroup == NULL) break;
847

S
slguan 已提交
848
      SMDDropSTableMsg *pDrop = rpcMallocCont(sizeof(SMDDropSTableMsg));
guanshengliang's avatar
guanshengliang 已提交
849
      pDrop->contLen = htonl(sizeof(SMDDropSTableMsg));
850
      pDrop->vgId = htonl(pVgroup->vgId);
S
slguan 已提交
851
      pDrop->uid = htobe64(pStable->uid);
S
Shengliang Guan 已提交
852
      mnodeExtractTableName(pStable->info.tableId, pDrop->tableId);
S
Shengliang Guan 已提交
853 854 855

      mPrint("app:%p:%p, stable:%s, send drop stable msg to vgId:%d", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
             pVgroup->vgId);
856
      SRpcIpSet ipSet = mnodeGetIpSetFromVgroup(pVgroup);
S
Shengliang Guan 已提交
857
      SRpcMsg   rpcMsg = {.pCont = pDrop, .contLen = sizeof(SMDDropSTableMsg), .msgType = TSDB_MSG_TYPE_MD_DROP_STABLE};
guanshengliang's avatar
guanshengliang 已提交
858
      dnodeSendMsgToDnode(&ipSet, &rpcMsg);
859
      mnodeDecVgroupRef(pVgroup);
S
slguan 已提交
860
    }
861
    taosHashDestroyIter(pIter);
862

863
    mnodeDropAllChildTablesInStable(pStable);
guanshengliang's avatar
guanshengliang 已提交
864 865 866 867 868
  } 
  
  SSdbOper oper = {
    .type = SDB_OPER_GLOBAL,
    .table = tsSuperTableSdb,
869 870
    .pObj = pStable,
    .pMsg = pMsg
guanshengliang's avatar
guanshengliang 已提交
871 872 873
  };
  
  int32_t code = sdbDeleteRow(&oper);
874
  if (code == TSDB_CODE_SUCCESS) {
875 876 877 878
    mLPrint("stable:%s, is dropped from sdb, result:%s", pStable->info.tableId, tstrerror(code));
    if (pMsg != NULL) code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
  }

879
  return code;
S
slguan 已提交
880 881
}

882
static int32_t mnodeFindSuperTableTagIndex(SSuperTableObj *pStable, const char *tagName) {
guanshengliang's avatar
guanshengliang 已提交
883 884 885 886
  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 已提交
887 888 889 890 891 892
    }
  }

  return -1;
}

893 894 895 896 897 898 899 900 901 902
static int32_t mnodeAddSuperTableTagCb(SMnodeMsg *pMsg, int32_t code) {
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
  mLPrint("app:%p:%p, stable %s, add tag result:%s", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
          tstrerror(code));

  return code;
}

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

S
slguan 已提交
908
  for (int32_t i = 0; i < ntags; i++) {
909
    if (mnodeFindSuperTableColumnIndex(pStable, schema[i].name) > 0) {
S
Shengliang Guan 已提交
910 911
      mError("app:%p:%p, stable:%s, add tag, column:%s already exist", pMsg->rpcMsg.ahandle, pMsg,
             pStable->info.tableId, schema[i].name);
912
      return TSDB_CODE_MND_TAG_ALREAY_EXIST;
S
slguan 已提交
913 914
    }

915
    if (mnodeFindSuperTableTagIndex(pStable, schema[i].name) > 0) {
S
Shengliang Guan 已提交
916 917
      mError("app:%p:%p, stable:%s, add tag, tag:%s already exist", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
             schema[i].name);
918
      return TSDB_CODE_MND_FIELD_ALREAY_EXIST;
S
slguan 已提交
919 920 921 922 923 924
    }
  }

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

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

S
slguan 已提交
927
  SSchema *tschema = (SSchema *)(pStable->schema + pStable->numOfColumns + pStable->numOfTags);
S
slguan 已提交
928 929 930 931
  for (int32_t i = 0; i < ntags; i++) {
    tschema[i].colId = pStable->nextColId++;
  }

S
slguan 已提交
932
  pStable->numOfTags += ntags;
S
TD-355  
Shengliang Guan 已提交
933
  pStable->tversion++;
S
slguan 已提交
934

935 936 937
  mPrint("app:%p:%p, stable %s, start to add tag %s", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
         schema[0].name);

S
slguan 已提交
938
  SSdbOper oper = {
S
slguan 已提交
939
    .type = SDB_OPER_GLOBAL,
S
[TD-17]  
slguan 已提交
940
    .table = tsSuperTableSdb,
941
    .pObj = pStable,
942 943
    .pMsg = pMsg,
    .cb = mnodeAddSuperTableTagCb
S
[TD-17]  
slguan 已提交
944
  };
S
slguan 已提交
945

S
[TD-17]  
slguan 已提交
946
  int32_t code = sdbUpdateRow(&oper);
947 948
  if (code == TSDB_CODE_SUCCESS) {
    code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
[TD-17]  
slguan 已提交
949
  }
S
Shengliang Guan 已提交
950

951
  return code;
S
slguan 已提交
952 953
}

954 955 956 957 958 959 960 961 962
static int32_t mnodeDropSuperTableTagCb(SMnodeMsg *pMsg, int32_t code) {
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
  mLPrint("app:%p:%p, stable %s, drop tag result:%s", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
          tstrerror(code));
  return code;
}

static int32_t mnodeDropSuperTableTag(SMnodeMsg *pMsg, char *tagName) {
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
963
  int32_t col = mnodeFindSuperTableTagIndex(pStable, tagName);
S
slguan 已提交
964
  if (col < 0) {
S
Shengliang Guan 已提交
965 966
    mError("app:%p:%p, stable:%s, drop tag, tag:%s not exist", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
           tagName);
967
    return TSDB_CODE_MND_TAG_NOT_EXIST;
S
slguan 已提交
968
  }
S
slguan 已提交
969

S
slguan 已提交
970 971
  memmove(pStable->schema + pStable->numOfColumns + col, pStable->schema + pStable->numOfColumns + col + 1,
          sizeof(SSchema) * (pStable->numOfTags - col - 1));
S
slguan 已提交
972
  pStable->numOfTags--;
S
TD-355  
Shengliang Guan 已提交
973
  pStable->tversion++;
S
slguan 已提交
974

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

S
slguan 已提交
977
  SSdbOper oper = {
S
slguan 已提交
978
    .type = SDB_OPER_GLOBAL,
S
[TD-17]  
slguan 已提交
979
    .table = tsSuperTableSdb,
980
    .pObj = pStable,
981 982
    .pMsg = pMsg,
    .cb = mnodeDropSuperTableTagCb
S
[TD-17]  
slguan 已提交
983
  };
S
slguan 已提交
984

S
[TD-17]  
slguan 已提交
985
  int32_t code = sdbUpdateRow(&oper);
986 987
  if (code == TSDB_CODE_SUCCESS) {
    code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
[TD-17]  
slguan 已提交
988
  }
989 990

  return code;
S
slguan 已提交
991 992
}

993 994 995 996 997 998 999 1000 1001
static int32_t mnodeModifySuperTableTagNameCb(SMnodeMsg *pMsg, int32_t code) {
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
  mLPrint("app:%p:%p, stable %s, modify tag result:%s", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
         tstrerror(code));
  return code;
}

static int32_t mnodeModifySuperTableTagName(SMnodeMsg *pMsg, char *oldTagName, char *newTagName) {
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
1002
  int32_t col = mnodeFindSuperTableTagIndex(pStable, oldTagName);
S
slguan 已提交
1003
  if (col < 0) {
S
Shengliang Guan 已提交
1004 1005
    mError("app:%p:%p, stable:%s, failed to modify table tag, oldName: %s, newName: %s", pMsg->rpcMsg.ahandle, pMsg,
           pStable->info.tableId, oldTagName, newTagName);
1006
    return TSDB_CODE_MND_TAG_NOT_EXIST;
S
slguan 已提交
1007 1008 1009 1010
  }

  // int32_t  rowSize = 0;
  uint32_t len = strlen(newTagName);
S
slguan 已提交
1011
  if (len >= TSDB_COL_NAME_LEN) {
1012
    return TSDB_CODE_MND_COL_NAME_TOO_LONG;
S
slguan 已提交
1013
  }
S
slguan 已提交
1014

1015
  if (mnodeFindSuperTableTagIndex(pStable, newTagName) >= 0) {
1016
    return TSDB_CODE_MND_TAG_ALREAY_EXIST;
S
slguan 已提交
1017
  }
1018
  
S
slguan 已提交
1019
  // update
S
slguan 已提交
1020
  SSchema *schema = (SSchema *) (pStable->schema + pStable->numOfColumns + col);
B
Bomin Zhang 已提交
1021
  tstrncpy(schema->name, newTagName, sizeof(schema->name));
S
slguan 已提交
1022

1023 1024 1025
  mPrint("app:%p:%p, stable %s, start to modify tag %s to %s", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
         oldTagName, newTagName);

S
slguan 已提交
1026
  SSdbOper oper = {
S
slguan 已提交
1027
    .type = SDB_OPER_GLOBAL,
S
[TD-17]  
slguan 已提交
1028
    .table = tsSuperTableSdb,
1029
    .pObj = pStable,
1030 1031
    .pMsg = pMsg,
    .cb = mnodeModifySuperTableTagNameCb
S
[TD-17]  
slguan 已提交
1032
  };
S
slguan 已提交
1033

S
[TD-17]  
slguan 已提交
1034
  int32_t code = sdbUpdateRow(&oper);
1035 1036
  if (code == TSDB_CODE_SUCCESS) {
    code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
slguan 已提交
1037
  }
1038 1039

  return code;
S
slguan 已提交
1040 1041
}

1042
static int32_t mnodeFindSuperTableColumnIndex(SSuperTableObj *pStable, char *colName) {
S
slguan 已提交
1043
  SSchema *schema = (SSchema *) pStable->schema;
S
slguan 已提交
1044 1045 1046
  for (int32_t col = 0; col < pStable->numOfColumns; col++) {
    if (strcasecmp(schema[col].name, colName) == 0) {
      return col;
S
slguan 已提交
1047 1048 1049 1050 1051 1052
    }
  }

  return -1;
}

1053 1054 1055 1056 1057 1058 1059 1060
static int32_t mnodeAddSuperTableColumnCb(SMnodeMsg *pMsg, int32_t code) {
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
  mLPrint("app:%p:%p, stable %s, add column result:%s", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
          tstrerror(code));
  return code;
}

static int32_t mnodeAddSuperTableColumn(SMnodeMsg *pMsg, SSchema schema[], int32_t ncols) {
1061
  SDbObj *pDb = pMsg->pDb;
1062
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
S
slguan 已提交
1063
  if (ncols <= 0) {
B
Bomin Zhang 已提交
1064
    mError("app:%p:%p, stable:%s, add column, ncols:%d <= 0", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId, ncols);
1065
    return TSDB_CODE_MND_APP_ERROR;
S
slguan 已提交
1066 1067 1068
  }

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

1075
    if (mnodeFindSuperTableTagIndex(pStable, schema[i].name) > 0) {
S
Shengliang Guan 已提交
1076 1077
      mError("app:%p:%p, stable:%s, add column, tag:%s already exist", pMsg->rpcMsg.ahandle, pMsg,
             pStable->info.tableId, schema[i].name);
1078
      return TSDB_CODE_MND_TAG_ALREAY_EXIST;
S
slguan 已提交
1079 1080 1081 1082 1083 1084
    }
  }

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

S
slguan 已提交
1085 1086 1087
  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 已提交
1088

L
liu0x54 已提交
1089
  SSchema *tschema = (SSchema *) (pStable->schema + pStable->numOfColumns);
S
slguan 已提交
1090 1091 1092 1093 1094 1095 1096
  for (int32_t i = 0; i < ncols; i++) {
    tschema[i].colId = pStable->nextColId++;
  }

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

1097
  SAcctObj *pAcct = mnodeGetAcct(pDb->acct);
S
[TD-17]  
slguan 已提交
1098 1099
  if (pAcct != NULL) {
    pAcct->acctInfo.numOfTimeSeries += (ncols * pStable->numOfTables);
1100
    mnodeDecAcctRef(pAcct);
S
[TD-17]  
slguan 已提交
1101
  }
S
slguan 已提交
1102

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

S
slguan 已提交
1105
  SSdbOper oper = {
S
slguan 已提交
1106
    .type = SDB_OPER_GLOBAL,
S
[TD-17]  
slguan 已提交
1107
    .table = tsSuperTableSdb,
1108
    .pObj = pStable,
1109 1110
    .pMsg = pMsg,
    .cb = mnodeAddSuperTableColumnCb
S
[TD-17]  
slguan 已提交
1111 1112 1113
  };

  int32_t code = sdbUpdateRow(&oper);
1114 1115
  if (code == TSDB_CODE_SUCCESS) {
    code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
[TD-17]  
slguan 已提交
1116
  }
1117 1118

  return code;
S
slguan 已提交
1119 1120
}

1121 1122 1123 1124 1125 1126 1127 1128
static int32_t mnodeDropSuperTableColumnCb(SMnodeMsg *pMsg, int32_t code) {
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
  mLPrint("app:%p:%p, stable %s, delete column result:%s", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
         tstrerror(code));
  return code;
}

static int32_t mnodeDropSuperTableColumn(SMnodeMsg *pMsg, char *colName) {
1129
  SDbObj *pDb = pMsg->pDb;
1130
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
1131
  int32_t col = mnodeFindSuperTableColumnIndex(pStable, colName);
S
slguan 已提交
1132
  if (col <= 0) {
S
Shengliang Guan 已提交
1133 1134
    mError("app:%p:%p, stable:%s, drop column, column:%s not exist", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
           colName);
1135
    return TSDB_CODE_MND_FIELD_NOT_EXIST;
S
slguan 已提交
1136 1137
  }

S
slguan 已提交
1138
  memmove(pStable->schema + col, pStable->schema + col + 1,
S
slguan 已提交
1139 1140 1141 1142 1143 1144 1145 1146
          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);

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

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

S
slguan 已提交
1155
  SSdbOper oper = {
S
slguan 已提交
1156
    .type = SDB_OPER_GLOBAL,
S
[TD-17]  
slguan 已提交
1157
    .table = tsSuperTableSdb,
1158
    .pObj = pStable,
1159 1160
    .pMsg = pMsg,
    .cb = mnodeDropSuperTableColumnCb
S
[TD-17]  
slguan 已提交
1161 1162 1163
  };

  int32_t code = sdbUpdateRow(&oper);
1164 1165
  if (code == TSDB_CODE_SUCCESS) {
    code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
[TD-17]  
slguan 已提交
1166
  }
S
Shengliang Guan 已提交
1167

1168
  return code;
S
slguan 已提交
1169 1170
}

S
slguan 已提交
1171
// show super tables
1172 1173
static int32_t mnodeGetShowSuperTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
  SDbObj *pDb = mnodeGetDb(pShow->db);
1174
  if (pDb == NULL) return TSDB_CODE_MND_DB_NOT_SELECTED;
S
slguan 已提交
1175 1176 1177 1178

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

H
Haojun Liao 已提交
1179 1180 1181
  SSchema tbnameSchema = tGetTableNameColumnSchema();
  pShow->bytes[cols] = tbnameSchema.bytes;
  pSchema[cols].type = tbnameSchema.type;
S
slguan 已提交
1182 1183 1184 1185 1186 1187
  strcpy(pSchema[cols].name, "name");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
1188
  strcpy(pSchema[cols].name, "created_time");
S
slguan 已提交
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
  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];

1219
  mnodeDecDbRef(pDb);
S
slguan 已提交
1220 1221 1222
  return 0;
}

S
slguan 已提交
1223
// retrieve super tables
1224
int32_t mnodeRetrieveShowSuperTables(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
S
slguan 已提交
1225 1226 1227 1228 1229 1230 1231
  int32_t         numOfRows = 0;
  char *          pWrite;
  int32_t         cols = 0;
  SSuperTableObj *pTable = NULL;
  char            prefix[20] = {0};
  int32_t         prefixLen;

1232
  SDbObj *pDb = mnodeGetDb(pShow->db);
S
slguan 已提交
1233 1234 1235 1236 1237 1238 1239
  if (pDb == NULL) return 0;

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

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

S
slguan 已提交
1242
  while (numOfRows < rows) {    
1243
    pShow->pIter = mnodeGetNextSuperTable(pShow->pIter, &pTable);
S
slguan 已提交
1244 1245
    if (pTable == NULL) break;
    if (strncmp(pTable->info.tableId, prefix, prefixLen)) {
1246
      mnodeDecTableRef(pTable);
S
slguan 已提交
1247 1248 1249 1250
      continue;
    }

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

B
Bomin Zhang 已提交
1253
    if (pShow->payloadLen > 0 && patternMatch(pShow->payload, stableName, sizeof(stableName) - 1, &info) != TSDB_PATTERN_MATCH) {
1254
      mnodeDecTableRef(pTable);
S
slguan 已提交
1255
      continue;
S
Shengliang Guan 已提交
1256
    }
S
slguan 已提交
1257 1258 1259 1260

    cols = 0;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
H
hjxilinx 已提交
1261
  
B
Bomin Zhang 已提交
1262
    int16_t len = strnlen(stableName, TSDB_DB_NAME_LEN - 1);
H
hjxilinx 已提交
1263 1264 1265 1266
    *(int16_t*) pWrite = len;
    pWrite += sizeof(int16_t); // todo refactor
  
    strncpy(pWrite, stableName, len);
S
slguan 已提交
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
    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++;
1286
    mnodeDecTableRef(pTable);
S
slguan 已提交
1287 1288 1289
  }

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

S
slguan 已提交
1292 1293 1294
  return numOfRows;
}

1295
void mnodeDropAllSuperTables(SDbObj *pDropDb) {
S
Shengliang Guan 已提交
1296
  void *  pIter= NULL;
S
slguan 已提交
1297 1298 1299 1300
  int32_t numOfTables = 0;
  int32_t dbNameLen = strlen(pDropDb->name);
  SSuperTableObj *pTable = NULL;

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

S
slguan 已提交
1303
  while (1) {
1304
    pIter = mnodeGetNextSuperTable(pIter, &pTable);
S
slguan 已提交
1305
    if (pTable == NULL) break;
S
slguan 已提交
1306 1307

    if (strncmp(pDropDb->name, pTable->info.tableId, dbNameLen) == 0) {
S
slguan 已提交
1308
      SSdbOper oper = {
S
slguan 已提交
1309
        .type = SDB_OPER_LOCAL,
S
slguan 已提交
1310 1311 1312 1313 1314 1315
        .table = tsSuperTableSdb,
        .pObj = pTable,
      };
      sdbDeleteRow(&oper);
      numOfTables ++;
    }
S
slguan 已提交
1316

1317
    mnodeDecTableRef(pTable);
S
slguan 已提交
1318
  }
S
slguan 已提交
1319

S
Shengliang Guan 已提交
1320 1321
  sdbFreeIter(pIter);

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

1325
static int32_t mnodeSetSchemaFromSuperTable(SSchema *pSchema, SSuperTableObj *pTable) {
S
slguan 已提交
1326
  int32_t numOfCols = pTable->numOfColumns + pTable->numOfTags;
1327 1328
  assert(numOfCols <= TSDB_MAX_COLUMNS);
  
S
slguan 已提交
1329
  for (int32_t i = 0; i < numOfCols; ++i) {
B
Bomin Zhang 已提交
1330
    tstrncpy(pSchema->name, pTable->schema[i].name, sizeof(pSchema->name));
S
slguan 已提交
1331 1332 1333 1334 1335 1336 1337 1338 1339
    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);
}

1340
static int32_t mnodeGetSuperTableMeta(SMnodeMsg *pMsg) {
S
slguan 已提交
1341
  SSuperTableObj *pTable = (SSuperTableObj *)pMsg->pTable;
1342
  STableMetaMsg *pMeta   = rpcMallocCont(sizeof(STableMetaMsg) + sizeof(SSchema) * (TSDB_MAX_TAGS + TSDB_MAX_COLUMNS + 16));
S
slguan 已提交
1343 1344
  pMeta->uid          = htobe64(pTable->uid);
  pMeta->sversion     = htons(pTable->sversion);
1345
  pMeta->tversion     = htons(pTable->tversion);
S
slguan 已提交
1346
  pMeta->precision    = pMsg->pDb->cfg.precision;
S
slguan 已提交
1347 1348 1349
  pMeta->numOfTags    = (uint8_t)pTable->numOfTags;
  pMeta->numOfColumns = htons((int16_t)pTable->numOfColumns);
  pMeta->tableType    = pTable->info.type;
1350
  pMeta->contLen      = sizeof(STableMetaMsg) + mnodeSetSchemaFromSuperTable(pMeta->schema, pTable);
B
Bomin Zhang 已提交
1351
  tstrncpy(pMeta->tableId, pTable->info.tableId, sizeof(pMeta->tableId));
S
slguan 已提交
1352

B
Bomin Zhang 已提交
1353
  pMsg->rpcRsp.len = pMeta->contLen;
S
slguan 已提交
1354 1355
  pMeta->contLen = htons(pMeta->contLen);

1356
  pMsg->rpcRsp.rsp = pMeta;
S
Shengliang Guan 已提交
1357 1358 1359

  mTrace("app:%p:%p, stable:%s, uid:%" PRIu64 " table meta is retrieved", pMsg->rpcMsg.ahandle, pMsg,
         pTable->info.tableId, pTable->uid);
1360
  return TSDB_CODE_SUCCESS;
S
slguan 已提交
1361 1362
}

1363
static int32_t mnodeProcessSuperTableVgroupMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
1364
  SCMSTableVgroupMsg *pInfo = pMsg->rpcMsg.pCont;
H
hjxilinx 已提交
1365
  int32_t numOfTable = htonl(pInfo->numOfTables);
1366

S
scripts  
Shengliang Guan 已提交
1367
  // reserve space
S
Shengliang Guan 已提交
1368
  int32_t contLen = sizeof(SCMSTableVgroupRspMsg) + 32 * sizeof(SCMVgroupInfo) + sizeof(SVgroupsInfo); 
1369 1370
  for (int32_t i = 0; i < numOfTable; ++i) {
    char *stableName = (char*)pInfo + sizeof(SCMSTableVgroupMsg) + (TSDB_TABLE_ID_LEN) * i;
1371
    SSuperTableObj *pTable = mnodeGetSuperTable(stableName);
1372
    if (pTable != NULL && pTable->vgHash != NULL) {
1373
      contLen += (taosHashGetSize(pTable->vgHash) * sizeof(SCMVgroupInfo) + sizeof(SVgroupsInfo));
1374
    } 
1375
    mnodeDecTableRef(pTable);
1376 1377
  }

S
scripts  
Shengliang Guan 已提交
1378
  SCMSTableVgroupRspMsg *pRsp = rpcMallocCont(contLen);
S
slguan 已提交
1379
  if (pRsp == NULL) {
1380
    return TSDB_CODE_MND_OUT_OF_MEMORY;
1381
  }
1382

1383
  pRsp->numOfTables = 0;
1384
  char *msg = (char *)pRsp + sizeof(SCMSTableVgroupRspMsg);
1385 1386

  for (int32_t i = 0; i < numOfTable; ++i) {
1387
    char *          stableName = (char *)pInfo + sizeof(SCMSTableVgroupMsg) + (TSDB_TABLE_ID_LEN)*i;
1388
    SSuperTableObj *pTable = mnodeGetSuperTable(stableName);
1389
    if (pTable == NULL) {
S
Shengliang Guan 已提交
1390
      mError("app:%p:%p, stable:%s, not exist while get stable vgroup info", pMsg->rpcMsg.ahandle, pMsg, stableName);
1391 1392 1393 1394
      mnodeDecTableRef(pTable);
      continue;
    }
    if (pTable->vgHash == NULL) {
S
Shengliang Guan 已提交
1395 1396
      mError("app:%p:%p, stable:%s, not vgroup exist while get stable vgroup info", pMsg->rpcMsg.ahandle, pMsg,
             stableName);
1397 1398
      mnodeDecTableRef(pTable);

1399 1400
      // even this super table has no corresponding table, still return
      pRsp->numOfTables++;
1401

1402 1403 1404 1405 1406 1407
      SVgroupsInfo *pVgroupInfo = (SVgroupsInfo *)msg;
      pVgroupInfo->numOfVgroups = 0;
      
      msg += sizeof(SVgroupsInfo);
    } else {
      SVgroupsInfo *pVgroupInfo = (SVgroupsInfo *)msg;
1408

1409 1410 1411 1412 1413 1414
      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;
1415

1416 1417 1418 1419
        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;
1420

1421 1422
          strncpy(pVgroupInfo->vgroups[vgSize].ipAddr[vn].fqdn, pDnode->dnodeFqdn, tListLen(pDnode->dnodeFqdn));
          pVgroupInfo->vgroups[vgSize].ipAddr[vn].port = htons(pDnode->dnodePort);
1423

1424 1425
          pVgroupInfo->vgroups[vgSize].numOfIps++;
        }
1426

1427 1428 1429
        vgSize++;
        mnodeDecVgroupRef(pVgroup);
      }
1430

1431 1432 1433 1434
      taosHashDestroyIter(pIter);
      mnodeDecTableRef(pTable);

      pVgroupInfo->numOfVgroups = htonl(vgSize);
1435

1436 1437 1438 1439
      // one table is done, try the next table
      msg += sizeof(SVgroupsInfo) + vgSize * sizeof(SCMVgroupInfo);
      pRsp->numOfTables++;
    }
1440
  }
S
slguan 已提交
1441

1442 1443
  if (pRsp->numOfTables != numOfTable) {
    rpcFreeCont(pRsp);
1444
    return TSDB_CODE_MND_INVALID_TABLE_NAME;
1445 1446 1447 1448
  } else {
    pRsp->numOfTables = htonl(pRsp->numOfTables);
    pMsg->rpcRsp.rsp = pRsp;
    pMsg->rpcRsp.len = msg - (char *)pRsp;
1449

1450 1451
    return TSDB_CODE_SUCCESS;
  }
S
slguan 已提交
1452 1453
}

1454
static void mnodeProcessDropSuperTableRsp(SRpcMsg *rpcMsg) {
guanshengliang's avatar
guanshengliang 已提交
1455
  mPrint("drop stable rsp received, result:%s", tstrerror(rpcMsg->code));
S
slguan 已提交
1456 1457
}

S
Shengliang Guan 已提交
1458
static void *mnodeBuildCreateChildTableMsg(SCMCreateTableMsg *pMsg, SChildTableObj *pTable) {
1459
  STagData *  pTagData = NULL;
S
slguan 已提交
1460 1461 1462
  int32_t tagDataLen = 0;
  int32_t totalCols = 0;
  int32_t contLen = 0;
S
Shengliang Guan 已提交
1463
  if (pTable->info.type == TSDB_CHILD_TABLE) {
S
slguan 已提交
1464 1465
    totalCols = pTable->superTable->numOfColumns + pTable->superTable->numOfTags;
    contLen = sizeof(SMDCreateTableMsg) + totalCols * sizeof(SSchema) + tagDataLen + pTable->sqlLen;
S
Shengliang Guan 已提交
1466 1467 1468
    if (pMsg != NULL) {
      pTagData = (STagData *)pMsg->schema;
      tagDataLen = ntohl(pTagData->dataLen);
H
Hongze Cheng 已提交
1469
      contLen += tagDataLen;
S
Shengliang Guan 已提交
1470
    }
S
slguan 已提交
1471 1472 1473 1474 1475 1476 1477
  } else {
    totalCols = pTable->numOfColumns;
    contLen = sizeof(SMDCreateTableMsg) + totalCols * sizeof(SSchema) + pTable->sqlLen;
  }

  SMDCreateTableMsg *pCreate = rpcMallocCont(contLen);
  if (pCreate == NULL) {
1478
    terrno = TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
1479 1480 1481
    return NULL;
  }

S
Shengliang Guan 已提交
1482
  mnodeExtractTableName(pTable->info.tableId, pCreate->tableId);
S
slguan 已提交
1483 1484 1485 1486 1487 1488 1489 1490 1491
  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 已提交
1492
    mnodeExtractTableName(pTable->superTable->info.tableId, pCreate->superTableId);
S
slguan 已提交
1493 1494 1495
    pCreate->numOfColumns  = htons(pTable->superTable->numOfColumns);
    pCreate->numOfTags     = htons(pTable->superTable->numOfTags);
    pCreate->sversion      = htonl(pTable->superTable->sversion);
1496
    pCreate->tversion      = htonl(pTable->superTable->tversion);
S
slguan 已提交
1497 1498 1499 1500 1501 1502
    pCreate->tagDataLen    = htonl(tagDataLen);
    pCreate->superTableUid = htobe64(pTable->superTable->uid);
  } else {
    pCreate->numOfColumns  = htons(pTable->numOfColumns);
    pCreate->numOfTags     = 0;
    pCreate->sversion      = htonl(pTable->sversion);
1503
    pCreate->tversion      = 0;
S
slguan 已提交
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520
    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) {
1521
    memcpy(pCreate->data + totalCols * sizeof(SSchema), pTagData->data, tagDataLen);
1522 1523
  }

S
Shengliang Guan 已提交
1524
  if (pTable->info.type == TSDB_STREAM_TABLE) {
1525
    memcpy(pCreate->data + totalCols * sizeof(SSchema), pTable->sql, pTable->sqlLen);
S
slguan 已提交
1526 1527 1528 1529 1530
  }

  return pCreate;
}

1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552
static int32_t mnodeDoCreateChildTableCb(SMnodeMsg *pMsg, int32_t code) {
  if (code != TSDB_CODE_SUCCESS) return code;
  
  SCMCreateTableMsg *pCreate = pMsg->rpcMsg.pCont;
  SMDCreateTableMsg *pMDCreate = mnodeBuildCreateChildTableMsg(pCreate, (SChildTableObj *)pMsg->pTable);
  if (pMDCreate == NULL) {
    return terrno;
  }

  SRpcIpSet ipSet = mnodeGetIpSetFromVgroup(pMsg->pVgroup);
  SRpcMsg rpcMsg = {
      .handle  = pMsg,
      .pCont   = pMDCreate,
      .contLen = htonl(pMDCreate->contLen),
      .code    = 0,
      .msgType = TSDB_MSG_TYPE_MD_CREATE_TABLE
  };

  dnodeSendMsgToDnode(&ipSet, &rpcMsg);
  return TSDB_CODE_MND_ACTION_IN_PROGRESS;
}

1553
static int32_t mnodeDoCreateChildTable(SMnodeMsg *pMsg, int32_t tid) {
1554
  SVgObj *pVgroup = pMsg->pVgroup;
1555
  SCMCreateTableMsg *pCreate = pMsg->rpcMsg.pCont;
1556
  SChildTableObj *pTable = calloc(1, sizeof(SChildTableObj));
S
slguan 已提交
1557
  if (pTable == NULL) {
S
Shengliang Guan 已提交
1558
    mError("app:%p:%p, table:%s, failed to alloc memory", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId);
1559
    return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
1560 1561 1562 1563 1564 1565 1566 1567
  }

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

1568 1569 1570 1571
  pTable->info.tableId = strdup(pCreate->tableId);  
  pTable->createdTime  = taosGetTimestampMs();
  pTable->sid          = tid;
  pTable->vgId         = pVgroup->vgId;
S
slguan 已提交
1572 1573
    
  if (pTable->info.type == TSDB_CHILD_TABLE) {
1574
    STagData *pTagData = (STagData *) pCreate->schema;  // it is a tag key
1575
    SSuperTableObj *pSuperTable = mnodeGetSuperTable(pTagData->name);
S
slguan 已提交
1576
    if (pSuperTable == NULL) {
S
Shengliang Guan 已提交
1577 1578
      mError("app:%p:%p, table:%s, corresponding super table:%s does not exist", pMsg->rpcMsg.ahandle, pMsg,
             pCreate->tableId, pTagData->name);
1579
      mnodeDestroyChildTable(pTable);
1580
      return TSDB_CODE_MND_INVALID_TABLE_NAME;
S
slguan 已提交
1581
    }
1582
    mnodeDecTableRef(pSuperTable);
S
slguan 已提交
1583

1584 1585 1586 1587
    pTable->suid = pSuperTable->uid;
    pTable->uid  = (((uint64_t)pTable->vgId) << 40) + ((((uint64_t)pTable->sid) & ((1ul << 24) - 1ul)) << 16) +
                  (sdbGetVersion() & ((1ul << 16) - 1ul));
    pTable->superTable = pSuperTable;
S
slguan 已提交
1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598
  } else {
    pTable->uid          = (((uint64_t) pTable->createdTime) << 16) + (sdbGetVersion() & ((1ul << 16) - 1ul));
    pTable->sversion     = 0;
    pTable->numOfColumns = htons(pCreate->numOfColumns);
    pTable->sqlLen       = htons(pCreate->sqlLen);

    int32_t numOfCols  = pTable->numOfColumns;
    int32_t schemaSize = numOfCols * sizeof(SSchema);
    pTable->schema     = (SSchema *) calloc(1, schemaSize);
    if (pTable->schema == NULL) {
      free(pTable);
1599
      return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614
    }
    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);
1615
        return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
1616 1617 1618
      }
      memcpy(pTable->sql, (char *) (pCreate->schema) + numOfCols * sizeof(SSchema), pTable->sqlLen);
      pTable->sql[pTable->sqlLen - 1] = 0;
S
Shengliang Guan 已提交
1619 1620
      mTrace("app:%p:%p, table:%s, stream sql len:%d sql:%s", pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId,
             pTable->sqlLen, pTable->sql);
S
slguan 已提交
1621 1622
    }
  }
1623 1624 1625 1626

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

S
slguan 已提交
1627
  SSdbOper desc = {0};
S
slguan 已提交
1628
  desc.type = SDB_OPER_GLOBAL;
S
slguan 已提交
1629 1630
  desc.pObj = pTable;
  desc.table = tsChildTableSdb;
1631 1632
  desc.pMsg = pMsg;
  desc.cb = mnodeDoCreateChildTableCb;
S
slguan 已提交
1633
  
1634 1635
  int32_t code = sdbInsertRow(&desc);
  if (code != TSDB_CODE_SUCCESS) {
S
slguan 已提交
1636
    free(pTable);
S
Shengliang Guan 已提交
1637 1638
    mError("app:%p:%p, table:%s, update sdb error, reason:%s", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId,
           tstrerror(code));
1639 1640
    pMsg->pTable = NULL;
    return code;
1641
  } else {
S
Shengliang Guan 已提交
1642 1643
    mTrace("app:%p:%p, table:%s, create table in vgroup:%d, id:%d, uid:%" PRIu64, pMsg->rpcMsg.ahandle, pMsg,
           pTable->info.tableId, pVgroup->vgId, pTable->sid, pTable->uid);
1644
    return TSDB_CODE_SUCCESS;
S
slguan 已提交
1645 1646 1647
  }
}

1648
static int32_t mnodeProcessCreateChildTableMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
1649
  SCMCreateTableMsg *pCreate = pMsg->rpcMsg.pCont;
S
slguan 已提交
1650 1651
  int32_t code = grantCheck(TSDB_GRANT_TIMESERIES);
  if (code != TSDB_CODE_SUCCESS) {
1652 1653
    mError("app:%p:%p, table:%s, failed to create, grant timeseries failed", pMsg->rpcMsg.ahandle, pMsg,
           pCreate->tableId);
1654
    return code;
S
slguan 已提交
1655 1656
  }

1657
  SVgObj *pVgroup = mnodeGetAvailableVgroup(pMsg->pDb);
S
slguan 已提交
1658
  if (pVgroup == NULL) {
1659
    mTrace("app:%p:%p, table:%s, start to create a new vgroup", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId);
1660
    return mnodeCreateVgroup(pMsg, pMsg->pDb);
S
slguan 已提交
1661 1662
  }

1663
  if (pMsg->retry == 0) {
1664
    if (pMsg->pTable == NULL) {
1665 1666
      int32_t sid = taosAllocateId(pVgroup->idPool);
      if (sid <= 0) {
1667 1668
        mTrace("app:%p:%p, table:%s, no enough sid in vgId:%d", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId,
               pVgroup->vgId);
1669
        return mnodeCreateVgroup(pMsg, pMsg->pDb);
1670 1671
      }

1672 1673 1674 1675
      if (pMsg->pVgroup == NULL) {
        pMsg->pVgroup = pVgroup;
        mnodeIncVgroupRef(pVgroup);
      }
1676

1677 1678
      mTrace("app:%p:%p, table:%s, create table in vgroup, vgId:%d sid:%d", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId,
             pVgroup->vgId, sid);
1679

1680 1681 1682 1683 1684 1685
      code = mnodeDoCreateChildTable(pMsg, sid);
      if (code != TSDB_CODE_SUCCESS) {
        return code;
      } else {
        return TSDB_CODE_MND_ACTION_IN_PROGRESS;
      }
1686
    }
1687
  } else {
1688
    if (pMsg->pTable == NULL) pMsg->pTable = mnodeGetTable(pCreate->tableId);
1689
  }
1690

S
slguan 已提交
1691
  if (pMsg->pTable == NULL) {
B
Bomin Zhang 已提交
1692
    mError("app:%p:%p, table:%s, object not found, retry:%d reason:%s", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId, pMsg->retry,
1693
           tstrerror(terrno));
1694
    return terrno;
1695
  } else {
1696 1697
    mTrace("app:%p:%p, table:%s, send create msg to vnode again", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId);
    return mnodeDoCreateChildTableCb(pMsg, TSDB_CODE_SUCCESS);
S
slguan 已提交
1698 1699 1700
  }
}

1701
static int32_t mnodeProcessDropChildTableMsg(SMnodeMsg *pMsg) {
S
slguan 已提交
1702
  SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;
1703
  if (pMsg->pVgroup == NULL) pMsg->pVgroup = mnodeGetVgroup(pTable->vgId);
1704
  if (pMsg->pVgroup == NULL) {
S
Shengliang Guan 已提交
1705 1706
    mError("app:%p:%p, table:%s, failed to drop ctable, vgroup not exist", pMsg->rpcMsg.ahandle, pMsg,
           pTable->info.tableId);
1707
    return TSDB_CODE_MND_APP_ERROR;
S
slguan 已提交
1708 1709 1710 1711
  }

  SMDDropTableMsg *pDrop = rpcMallocCont(sizeof(SMDDropTableMsg));
  if (pDrop == NULL) {
S
Shengliang Guan 已提交
1712 1713
    mError("app:%p:%p, table:%s, failed to drop ctable, no enough memory", pMsg->rpcMsg.ahandle, pMsg,
           pTable->info.tableId);
1714
    return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
1715 1716 1717 1718 1719 1720 1721 1722
  }

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

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

S
Shengliang Guan 已提交
1725
  mPrint("app:%p:%p, table:%s, send drop ctable msg", pMsg->rpcMsg.ahandle, pMsg, pDrop->tableId);
S
slguan 已提交
1726
  SRpcMsg rpcMsg = {
1727
    .handle  = pMsg,
S
slguan 已提交
1728 1729 1730 1731 1732 1733
    .pCont   = pDrop,
    .contLen = sizeof(SMDDropTableMsg),
    .code    = 0,
    .msgType = TSDB_MSG_TYPE_MD_DROP_TABLE
  };

J
jtao1735 已提交
1734
  dnodeSendMsgToDnode(&ipSet, &rpcMsg);
1735

1736
  return TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
slguan 已提交
1737 1738
}

1739
static int32_t mnodeFindNormalTableColumnIndex(SChildTableObj *pTable, char *colName) {
S
slguan 已提交
1740
  SSchema *schema = (SSchema *) pTable->schema;
S
slguan 已提交
1741 1742 1743
  for (int32_t col = 0; col < pTable->numOfColumns; col++) {
    if (strcasecmp(schema[col].name, colName) == 0) {
      return col;
S
slguan 已提交
1744 1745 1746 1747 1748 1749
    }
  }

  return -1;
}

1750
static int32_t mnodeAddNormalTableColumnCb(SMnodeMsg *pMsg, int32_t code) {
1751
  SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;
1752
  mLPrint("app:%p:%p, ctable %s, add column result:%s", pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId,
1753
          tstrerror(code));
1754 1755 1756 1757 1758
  return code;
}

static int32_t mnodeAddNormalTableColumn(SMnodeMsg *pMsg, SSchema schema[], int32_t ncols) {
  SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;
1759
  SDbObj *pDb = pMsg->pDb;
S
slguan 已提交
1760
  if (ncols <= 0) {
B
Bomin Zhang 已提交
1761
    mError("app:%p:%p, ctable:%s, add column, ncols:%d <= 0", pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId, ncols);
1762
    return TSDB_CODE_MND_APP_ERROR;
S
slguan 已提交
1763 1764 1765
  }

  for (int32_t i = 0; i < ncols; i++) {
1766
    if (mnodeFindNormalTableColumnIndex(pTable, schema[i].name) > 0) {
1767
      mError("app:%p:%p, ctable:%s, add column, column:%s already exist", pMsg->rpcMsg.ahandle, pMsg,
S
Shengliang Guan 已提交
1768
             pTable->info.tableId, schema[i].name);
1769
      return TSDB_CODE_MND_FIELD_ALREAY_EXIST;
S
slguan 已提交
1770 1771 1772 1773 1774 1775
    }
  }

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

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

S
slguan 已提交
1778
  SSchema *tschema = (SSchema *) (pTable->schema + pTable->numOfColumns);
S
slguan 已提交
1779 1780 1781 1782 1783 1784
  for (int32_t i = 0; i < ncols; i++) {
    tschema[i].colId = pTable->nextColId++;
  }

  pTable->numOfColumns += ncols;
  pTable->sversion++;
S
[TD-17]  
slguan 已提交
1785
  
1786
  SAcctObj *pAcct = mnodeGetAcct(pDb->acct);
S
[TD-17]  
slguan 已提交
1787
  if (pAcct != NULL) {
S
slguan 已提交
1788
    pAcct->acctInfo.numOfTimeSeries += ncols;
1789
    mnodeDecAcctRef(pAcct);
S
[TD-17]  
slguan 已提交
1790
  }
1791 1792 1793

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

S
slguan 已提交
1794
  SSdbOper oper = {
S
slguan 已提交
1795
    .type = SDB_OPER_GLOBAL,
S
[TD-17]  
slguan 已提交
1796
    .table = tsChildTableSdb,
1797
    .pObj = pTable,
1798 1799
    .pMsg = pMsg,
    .cb = mnodeAddNormalTableColumnCb
S
[TD-17]  
slguan 已提交
1800
  };
S
slguan 已提交
1801

S
[TD-17]  
slguan 已提交
1802
  int32_t code = sdbUpdateRow(&oper);
1803 1804
  if (code == TSDB_CODE_SUCCESS) {
    return TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
[TD-17]  
slguan 已提交
1805
  }
S
Shengliang Guan 已提交
1806

1807
  return code;
S
slguan 已提交
1808 1809
}

1810
static int32_t mnodeDropNormalTableColumnCb(SMnodeMsg *pMsg, int32_t code) {
1811
  SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;
1812 1813 1814 1815 1816 1817
  mLPrint("app:%p:%p, ctable %s, drop column result:%s", pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId,
          tstrerror(code));
  return code;
}

static int32_t mnodeDropNormalTableColumn(SMnodeMsg *pMsg, char *colName) {
1818
  SDbObj *pDb = pMsg->pDb;
1819
  SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;
1820
  int32_t col = mnodeFindNormalTableColumnIndex(pTable, colName);
S
slguan 已提交
1821
  if (col <= 0) {
1822
    mError("app:%p:%p, ctable:%s, drop column, column:%s not exist", pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId,
S
Shengliang Guan 已提交
1823
           colName);
1824
    return TSDB_CODE_MND_FIELD_NOT_EXIST;
S
slguan 已提交
1825 1826
  }

S
slguan 已提交
1827
  memmove(pTable->schema + col, pTable->schema + col + 1, sizeof(SSchema) * (pTable->numOfColumns - col - 1));
S
slguan 已提交
1828 1829 1830
  pTable->numOfColumns--;
  pTable->sversion++;

1831
  SAcctObj *pAcct = mnodeGetAcct(pDb->acct);
S
[TD-17]  
slguan 已提交
1832 1833
  if (pAcct != NULL) {
    pAcct->acctInfo.numOfTimeSeries--;
1834
    mnodeDecAcctRef(pAcct);
S
[TD-17]  
slguan 已提交
1835
  }
1836 1837 1838

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

S
slguan 已提交
1839
  SSdbOper oper = {
S
slguan 已提交
1840
    .type = SDB_OPER_GLOBAL,
S
[TD-17]  
slguan 已提交
1841
    .table = tsChildTableSdb,
1842
    .pObj = pTable,
1843 1844
    .pMsg = pMsg,
    .cb = mnodeDropNormalTableColumnCb
S
[TD-17]  
slguan 已提交
1845
  };
S
slguan 已提交
1846

S
[TD-17]  
slguan 已提交
1847
  int32_t code = sdbUpdateRow(&oper);
1848
  if (code == TSDB_CODE_SUCCESS) {
1849
    return TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
[TD-17]  
slguan 已提交
1850
  }
S
Shengliang Guan 已提交
1851

1852
  return code;
S
slguan 已提交
1853 1854
}

1855
static int32_t mnodeSetSchemaFromNormalTable(SSchema *pSchema, SChildTableObj *pTable) {
S
slguan 已提交
1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867
  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);
}

1868
static int32_t mnodeDoGetChildTableMeta(SMnodeMsg *pMsg, STableMetaMsg *pMeta) {
S
slguan 已提交
1869 1870 1871
  SDbObj *pDb = pMsg->pDb;
  SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;

S
slguan 已提交
1872 1873 1874 1875
  pMeta->uid       = htobe64(pTable->uid);
  pMeta->sid       = htonl(pTable->sid);
  pMeta->precision = pDb->cfg.precision;
  pMeta->tableType = pTable->info.type;
S
Shuduo Sang 已提交
1876
  strncpy(pMeta->tableId, pTable->info.tableId, strlen(pTable->info.tableId));
S
slguan 已提交
1877 1878 1879

  if (pTable->info.type == TSDB_CHILD_TABLE) {
    pMeta->sversion     = htons(pTable->superTable->sversion);
1880
    pMeta->tversion     = htons(pTable->superTable->tversion);
S
slguan 已提交
1881
    pMeta->numOfTags    = (int8_t)pTable->superTable->numOfTags;
S
slguan 已提交
1882
    pMeta->numOfColumns = htons((int16_t)pTable->superTable->numOfColumns);
1883
    pMeta->contLen      = sizeof(STableMetaMsg) + mnodeSetSchemaFromSuperTable(pMeta->schema, pTable->superTable);
S
slguan 已提交
1884 1885
  } else {
    pMeta->sversion     = htons(pTable->sversion);
1886
    pMeta->tversion     = 0;
S
slguan 已提交
1887 1888
    pMeta->numOfTags    = 0;
    pMeta->numOfColumns = htons((int16_t)pTable->numOfColumns);
1889
    pMeta->contLen      = sizeof(STableMetaMsg) + mnodeSetSchemaFromNormalTable(pMeta->schema, pTable); 
S
slguan 已提交
1890
  }
S
Shengliang Guan 已提交
1891

1892
  if (pMsg->pVgroup == NULL) pMsg->pVgroup = mnodeGetVgroup(pTable->vgId);
1893
  if (pMsg->pVgroup == NULL) {
S
Shengliang Guan 已提交
1894 1895
    mError("app:%p:%p, table:%s, failed to get table meta, vgroup not exist", pMsg->rpcMsg.ahandle, pMsg,
           pTable->info.tableId);
1896
    return TSDB_CODE_MND_VGROUP_NOT_EXIST;
S
slguan 已提交
1897 1898
  }

1899
  for (int32_t i = 0; i < pMsg->pVgroup->numOfVnodes; ++i) {
1900
    SDnodeObj *pDnode = mnodeGetDnode(pMsg->pVgroup->vnodeGid[i].dnodeId);
S
slguan 已提交
1901
    if (pDnode == NULL) break;
J
jtao1735 已提交
1902
    strcpy(pMeta->vgroup.ipAddr[i].fqdn, pDnode->dnodeFqdn);
J
jtao1735 已提交
1903
    pMeta->vgroup.ipAddr[i].port = htons(pDnode->dnodePort + TSDB_PORT_DNODESHELL);
S
slguan 已提交
1904
    pMeta->vgroup.numOfIps++;
1905
    mnodeDecDnodeRef(pDnode);
S
slguan 已提交
1906
  }
1907
  pMeta->vgroup.vgId = htonl(pMsg->pVgroup->vgId);
S
slguan 已提交
1908

S
Shengliang Guan 已提交
1909 1910
  mTrace("app:%p:%p, table:%s, uid:%" PRIu64 " table meta is retrieved", pMsg->rpcMsg.ahandle, pMsg,
         pTable->info.tableId, pTable->uid);
S
slguan 已提交
1911 1912 1913 1914

  return TSDB_CODE_SUCCESS;
}

S
Shengliang Guan 已提交
1915
static int32_t mnodeAutoCreateChildTable(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
1916
  SCMTableInfoMsg *pInfo = pMsg->rpcMsg.pCont;
1917
  STagData *pTag = (STagData *)pInfo->tags;
1918 1919

  int32_t contLen = sizeof(SCMCreateTableMsg) + offsetof(STagData, data) + ntohl(pTag->dataLen);
S
slguan 已提交
1920 1921
  SCMCreateTableMsg *pCreateMsg = rpcMallocCont(contLen);
  if (pCreateMsg == NULL) {
S
Shengliang Guan 已提交
1922 1923
    mError("app:%p:%p, table:%s, failed to create table while get meta info, no enough memory", pMsg->rpcMsg.ahandle,
           pMsg, pInfo->tableId);
1924
    return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
1925
  }
S
slguan 已提交
1926

1927 1928
  size_t size = sizeof(pInfo->tableId);
  tstrncpy(pCreateMsg->tableId, pInfo->tableId, size);
B
Bomin Zhang 已提交
1929
  tstrncpy(pCreateMsg->db, pMsg->pDb->name, sizeof(pCreateMsg->db));
S
slguan 已提交
1930 1931
  pCreateMsg->igExists = 1;
  pCreateMsg->getMeta = 1;
S
slguan 已提交
1932
  pCreateMsg->contLen = htonl(contLen);
B
Bomin Zhang 已提交
1933

1934
  memcpy(pCreateMsg->schema, pInfo->tags, contLen - sizeof(SCMCreateTableMsg));
S
Shengliang Guan 已提交
1935 1936
  mTrace("app:%p:%p, table:%s, start to create on demand, stable:%s", pMsg->rpcMsg.ahandle, pMsg, pInfo->tableId,
         ((STagData *)(pCreateMsg->schema))->name);
S
slguan 已提交
1937

S
Shengliang Guan 已提交
1938 1939 1940 1941
  rpcFreeCont(pMsg->rpcMsg.pCont);
  pMsg->rpcMsg.msgType = TSDB_MSG_TYPE_CM_CREATE_TABLE;
  pMsg->rpcMsg.pCont = pCreateMsg;
  pMsg->rpcMsg.contLen = contLen;
1942
  
1943
  return TSDB_CODE_MND_ACTION_NEED_REPROCESSED;
S
slguan 已提交
1944
}
S
slguan 已提交
1945

1946
static int32_t mnodeGetChildTableMeta(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
1947 1948
  STableMetaMsg *pMeta =
      rpcMallocCont(sizeof(STableMetaMsg) + sizeof(SSchema) * (TSDB_MAX_TAGS + TSDB_MAX_COLUMNS + 16));
S
slguan 已提交
1949
  if (pMeta == NULL) {
S
Shengliang Guan 已提交
1950 1951
    mError("app:%p:%p, table:%s, failed to get table meta, no enough memory", pMsg->rpcMsg.ahandle, pMsg,
           pMsg->pTable->tableId);
1952
    return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
1953 1954
  }

1955
  mnodeDoGetChildTableMeta(pMsg, pMeta);
S
slguan 已提交
1956

1957 1958
  pMsg->rpcRsp.len = pMeta->contLen;
  pMsg->rpcRsp.rsp = pMeta;
S
slguan 已提交
1959
  pMeta->contLen = htons(pMeta->contLen);
1960 1961

  return TSDB_CODE_SUCCESS;
S
slguan 已提交
1962 1963
}

1964
void mnodeDropAllChildTablesInVgroups(SVgObj *pVgroup) {
1965 1966 1967 1968 1969 1970 1971
  void *  pIter = NULL;
  int32_t numOfTables = 0;
  SChildTableObj *pTable = NULL;

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

  while (1) {
1972
    pIter = mnodeGetNextChildTable(pIter, &pTable);
1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983
    if (pTable == NULL) break;

    if (pTable->vgId == pVgroup->vgId) {
      SSdbOper oper = {
        .type = SDB_OPER_LOCAL,
        .table = tsChildTableSdb,
        .pObj = pTable,
      };
      sdbDeleteRow(&oper);
      numOfTables++;
    }
1984
    mnodeDecTableRef(pTable);
1985 1986 1987 1988 1989 1990 1991
  }

  sdbFreeIter(pIter);

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

1992
void mnodeDropAllChildTables(SDbObj *pDropDb) {
S
Shengliang Guan 已提交
1993
  void *  pIter = NULL;
S
slguan 已提交
1994 1995 1996 1997
  int32_t numOfTables = 0;
  int32_t dbNameLen = strlen(pDropDb->name);
  SChildTableObj *pTable = NULL;

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

S
slguan 已提交
2000
  while (1) {
2001
    pIter = mnodeGetNextChildTable(pIter, &pTable);
S
slguan 已提交
2002
    if (pTable == NULL) break;
S
slguan 已提交
2003 2004

    if (strncmp(pDropDb->name, pTable->info.tableId, dbNameLen) == 0) {
S
slguan 已提交
2005
      SSdbOper oper = {
S
slguan 已提交
2006
        .type = SDB_OPER_LOCAL,
S
slguan 已提交
2007 2008 2009 2010 2011 2012
        .table = tsChildTableSdb,
        .pObj = pTable,
      };
      sdbDeleteRow(&oper);
      numOfTables++;
    }
2013
    mnodeDecTableRef(pTable);
S
slguan 已提交
2014 2015
  }

S
Shengliang Guan 已提交
2016 2017
  sdbFreeIter(pIter);

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

2021
static void mnodeDropAllChildTablesInStable(SSuperTableObj *pStable) {
S
Shengliang Guan 已提交
2022
  void *  pIter = NULL;
S
slguan 已提交
2023 2024 2025
  int32_t numOfTables = 0;
  SChildTableObj *pTable = NULL;

B
Bomin Zhang 已提交
2026
  mPrint("stable:%s, all child tables(%d) will dropped from sdb", pStable->info.tableId, numOfTables);
S
slguan 已提交
2027

S
slguan 已提交
2028
  while (1) {
2029
    pIter = mnodeGetNextChildTable(pIter, &pTable);
S
slguan 已提交
2030
    if (pTable == NULL) break;
S
slguan 已提交
2031 2032

    if (pTable->superTable == pStable) {
S
slguan 已提交
2033
      SSdbOper oper = {
S
slguan 已提交
2034
        .type = SDB_OPER_LOCAL,
S
slguan 已提交
2035 2036 2037 2038 2039 2040
        .table = tsChildTableSdb,
        .pObj = pTable,
      };
      sdbDeleteRow(&oper);
      numOfTables++;
    }
S
slguan 已提交
2041

2042
    mnodeDecTableRef(pTable);
S
slguan 已提交
2043 2044
  }

S
Shengliang Guan 已提交
2045 2046
  sdbFreeIter(pIter);

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

2050 2051
static SChildTableObj* mnodeGetTableByPos(int32_t vnode, int32_t sid) {
  SVgObj *pVgroup = mnodeGetVgroup(vnode);
2052
  if (pVgroup == NULL) return NULL;
S
slguan 已提交
2053

S
slguan 已提交
2054
  SChildTableObj *pTable = pVgroup->tableList[sid - 1];
2055
  mnodeIncTableRef((STableObj *)pTable);
2056

2057
  mnodeDecVgroupRef(pVgroup);
S
slguan 已提交
2058 2059 2060
  return pTable;
}

2061
static int32_t mnodeProcessTableCfgMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
2062
  SDMConfigTableMsg *pCfg = pMsg->rpcMsg.pCont;
S
Shengliang Guan 已提交
2063 2064 2065
  pCfg->dnodeId = htonl(pCfg->dnodeId);
  pCfg->vgId = htonl(pCfg->vgId);
  pCfg->sid = htonl(pCfg->sid);
S
Shengliang Guan 已提交
2066 2067
  mTrace("app:%p:%p, dnode:%d, vgId:%d sid:%d, receive table config msg", pMsg->rpcMsg.ahandle, pMsg, pCfg->dnodeId,
         pCfg->vgId, pCfg->sid);
S
slguan 已提交
2068

S
Shengliang Guan 已提交
2069
  SChildTableObj *pTable = mnodeGetTableByPos(pCfg->vgId, pCfg->sid);
S
slguan 已提交
2070
  if (pTable == NULL) {
S
Shengliang Guan 已提交
2071 2072
    mError("app:%p:%p, dnode:%d, vgId:%d sid:%d, table not found", pMsg->rpcMsg.ahandle, pMsg, pCfg->dnodeId,
           pCfg->vgId, pCfg->sid);
2073
    return TSDB_CODE_MND_INVALID_TABLE_ID;
S
slguan 已提交
2074 2075
  }

S
Shengliang Guan 已提交
2076 2077
  SMDCreateTableMsg *pCreate = NULL;
  pCreate = mnodeBuildCreateChildTableMsg(NULL, (SChildTableObj *)pTable);
2078
  mnodeDecTableRef(pTable);
S
Shengliang Guan 已提交
2079 2080 2081 2082 2083
    
  if (pCreate == NULL) return terrno;
  
  pMsg->rpcRsp.rsp = pCreate;
  pMsg->rpcRsp.len = htonl(pCreate->contLen);
2084
  return TSDB_CODE_SUCCESS;
S
slguan 已提交
2085 2086
}

S
slguan 已提交
2087
// handle drop child response
2088
static void mnodeProcessDropChildTableRsp(SRpcMsg *rpcMsg) {
S
slguan 已提交
2089 2090
  if (rpcMsg->handle == NULL) return;

2091 2092
  SMnodeMsg *mnodeMsg = rpcMsg->handle;
  mnodeMsg->received++;
S
slguan 已提交
2093

S
Shengliang Guan 已提交
2094 2095
  SChildTableObj *pTable = (SChildTableObj *)mnodeMsg->pTable;
  assert(pTable);
S
Shengliang Guan 已提交
2096 2097
  mPrint("app:%p:%p, table:%s, drop table rsp received, thandle:%p result:%s", mnodeMsg->rpcMsg.ahandle, mnodeMsg,
         pTable->info.tableId, mnodeMsg->rpcMsg.handle, tstrerror(rpcMsg->code));
S
slguan 已提交
2098 2099

  if (rpcMsg->code != TSDB_CODE_SUCCESS) {
S
Shengliang Guan 已提交
2100 2101
    mError("app:%p:%p, table:%s, failed to drop in dnode, reason:%s", mnodeMsg->rpcMsg.ahandle, mnodeMsg,
           pTable->info.tableId, tstrerror(rpcMsg->code));
2102
    dnodeSendRpcMnodeWriteRsp(mnodeMsg, rpcMsg->code);
S
slguan 已提交
2103 2104 2105
    return;
  }

2106 2107
  if (mnodeMsg->pVgroup == NULL) mnodeMsg->pVgroup = mnodeGetVgroup(pTable->vgId);
  if (mnodeMsg->pVgroup == NULL) {
S
Shengliang Guan 已提交
2108
    mError("app:%p:%p, table:%s, failed to get vgroup", mnodeMsg->rpcMsg.ahandle, mnodeMsg, pTable->info.tableId);
2109
    dnodeSendRpcMnodeWriteRsp(mnodeMsg, TSDB_CODE_MND_VGROUP_NOT_EXIST);
S
slguan 已提交
2110 2111
    return;
  }
S
Shengliang Guan 已提交
2112

S
slguan 已提交
2113
  SSdbOper oper = {
S
slguan 已提交
2114
    .type = SDB_OPER_GLOBAL,
S
slguan 已提交
2115 2116 2117
    .table = tsChildTableSdb,
    .pObj = pTable
  };
S
Shengliang Guan 已提交
2118

S
slguan 已提交
2119 2120
  int32_t code = sdbDeleteRow(&oper);
  if (code != TSDB_CODE_SUCCESS) {
S
Shengliang Guan 已提交
2121
    mError("app:%p:%p, table:%s, update ctables sdb error", mnodeMsg->rpcMsg.ahandle, mnodeMsg, pTable->info.tableId);
2122
    dnodeSendRpcMnodeWriteRsp(mnodeMsg, TSDB_CODE_MND_SDB_ERROR);
S
slguan 已提交
2123 2124 2125
    return;
  }

2126
  if (mnodeMsg->pVgroup->numOfTables <= 0) {
S
Shengliang Guan 已提交
2127 2128
    mPrint("app:%p:%p, vgId:%d, all tables is dropped, drop vgroup", mnodeMsg->rpcMsg.ahandle, mnodeMsg,
           mnodeMsg->pVgroup->vgId);
2129
    mnodeDropVgroup(mnodeMsg->pVgroup, NULL);
S
slguan 已提交
2130 2131
  }

2132
  dnodeSendRpcMnodeWriteRsp(mnodeMsg, TSDB_CODE_SUCCESS);
S
slguan 已提交
2133 2134
}

2135 2136 2137 2138
/*
 * handle create table response from dnode
 *   if failed, drop the table cached
 */
2139
static void mnodeProcessCreateChildTableRsp(SRpcMsg *rpcMsg) {
S
slguan 已提交
2140 2141
  if (rpcMsg->handle == NULL) return;

2142 2143
  SMnodeMsg *mnodeMsg = rpcMsg->handle;
  mnodeMsg->received++;
S
slguan 已提交
2144

S
Shengliang Guan 已提交
2145 2146
  SChildTableObj *pTable = (SChildTableObj *)mnodeMsg->pTable;
  assert(pTable);
S
Shengliang Guan 已提交
2147

2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163
  if (rpcMsg->code == TSDB_CODE_SUCCESS || rpcMsg->code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
    SCMCreateTableMsg *pCreate = mnodeMsg->rpcMsg.pCont;
    if (pCreate->getMeta) {
      mTrace("app:%p:%p, table:%s, created in dnode and continue to get meta, thandle:%p result:%s",
             mnodeMsg->rpcMsg.ahandle, mnodeMsg, pTable->info.tableId, mnodeMsg->rpcMsg.handle,
             tstrerror(rpcMsg->code));

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

      dnodeSendRpcMnodeWriteRsp(mnodeMsg, TSDB_CODE_SUCCESS);
    }
  } else {
S
Shengliang Guan 已提交
2164
    if (mnodeMsg->retry++ < 10) {
2165
      mTrace("app:%p:%p, table:%s, create table rsp received, need retry, times:%d result:%s thandle:%p",
S
Shengliang Guan 已提交
2166
             mnodeMsg->rpcMsg.ahandle, mnodeMsg, pTable->info.tableId, mnodeMsg->retry, tstrerror(rpcMsg->code),
2167 2168
             mnodeMsg->rpcMsg.handle);

2169
      dnodeDelayReprocessMnodeWriteMsg(mnodeMsg);
2170
    } else {
2171 2172 2173
      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);

2174
      SSdbOper oper = {.type = SDB_OPER_GLOBAL, .table = tsChildTableSdb, .pObj = pTable};
2175
      sdbDeleteRow(&oper);
2176

2177
      dnodeSendRpcMnodeWriteRsp(mnodeMsg, rpcMsg->code);
S
slguan 已提交
2178 2179 2180 2181
    }
  }
}

S
slguan 已提交
2182
// not implemented yet
2183
static void mnodeProcessAlterTableRsp(SRpcMsg *rpcMsg) {
2184
  mTrace("alter table rsp received, handle:%p code:%s", rpcMsg->handle, tstrerror(rpcMsg->code));
S
slguan 已提交
2185 2186
}

2187
static int32_t mnodeProcessMultiTableMetaMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
2188
  SCMMultiTableInfoMsg *pInfo = pMsg->rpcMsg.pCont;
S
slguan 已提交
2189 2190
  pInfo->numOfTables = htonl(pInfo->numOfTables);

2191
  int32_t totalMallocLen = 4 * 1024 * 1024;  // first malloc 4 MB, subsequent reallocation as twice
S
slguan 已提交
2192 2193
  SMultiTableMeta *pMultiMeta = rpcMallocCont(totalMallocLen);
  if (pMultiMeta == NULL) {
2194
    return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
2195 2196 2197 2198 2199
  }

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

2200
  for (int32_t t = 0; t < pInfo->numOfTables; ++t) {
B
Bomin Zhang 已提交
2201
    char * tableId = (char *)(pInfo->tableIds + t * TSDB_TABLE_ID_LEN);
2202
    SChildTableObj *pTable = mnodeGetChildTable(tableId);
S
slguan 已提交
2203 2204
    if (pTable == NULL) continue;

2205
    if (pMsg->pDb == NULL) pMsg->pDb = mnodeGetDbByTableId(tableId);
2206
    if (pMsg->pDb == NULL) {
2207
      mnodeDecTableRef(pTable);
2208 2209
      continue;
    }
S
slguan 已提交
2210 2211

    int availLen = totalMallocLen - pMultiMeta->contLen;
2212
    if (availLen <= sizeof(STableMetaMsg) + sizeof(SSchema) * (TSDB_MAX_TAGS + TSDB_MAX_COLUMNS + 16)) {
2213 2214 2215
      totalMallocLen *= 2;
      pMultiMeta = rpcReallocCont(pMultiMeta, totalMallocLen);
      if (pMultiMeta == NULL) {
2216
        mnodeDecTableRef(pTable);
2217
        return TSDB_CODE_MND_OUT_OF_MEMORY;
2218 2219
      } else {
        t--;
2220
        mnodeDecTableRef(pTable);
2221 2222
        continue;
      }
S
slguan 已提交
2223 2224 2225
    }

    STableMetaMsg *pMeta = (STableMetaMsg *)(pMultiMeta->metas + pMultiMeta->contLen);
2226
    int32_t code = mnodeDoGetChildTableMeta(pMsg, pMeta);
S
slguan 已提交
2227 2228 2229 2230
    if (code == TSDB_CODE_SUCCESS) {
      pMultiMeta->numOfTables ++;
      pMultiMeta->contLen += pMeta->contLen;
    }
2231

2232
    mnodeDecTableRef(pTable);
S
slguan 已提交
2233 2234
  }

2235 2236 2237 2238
  pMsg->rpcRsp.rsp = pMultiMeta;
  pMsg->rpcRsp.len = pMultiMeta->contLen;

  return TSDB_CODE_SUCCESS;
S
slguan 已提交
2239 2240
}

2241 2242
static int32_t mnodeGetShowTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
  SDbObj *pDb = mnodeGetDb(pShow->db);
2243
  if (pDb == NULL) return TSDB_CODE_MND_DB_NOT_SELECTED;
S
slguan 已提交
2244 2245 2246 2247

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

H
Haojun Liao 已提交
2248 2249 2250
  SSchema s = tGetTableNameColumnSchema();
  pShow->bytes[cols] = s.bytes;
  pSchema[cols].type = s.type;
2251
  strcpy(pSchema[cols].name, "table_name");
S
slguan 已提交
2252 2253 2254 2255 2256
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
2257
  strcpy(pSchema[cols].name, "created_time");
S
slguan 已提交
2258 2259 2260 2261 2262 2263 2264 2265 2266
  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 已提交
2267
  SSchema tbCol = tGetTableNameColumnSchema();
H
Hui Li 已提交
2268
  pShow->bytes[cols] = tbCol.bytes + VARSTR_HEADER_SIZE;
H
Haojun Liao 已提交
2269
  pSchema[cols].type = tbCol.type;
2270
  strcpy(pSchema[cols].name, "stable_name");
S
slguan 已提交
2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284
  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];

2285
  mnodeDecDbRef(pDb);
S
slguan 已提交
2286 2287 2288
  return 0;
}

2289 2290
static int32_t mnodeRetrieveShowTables(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
  SDbObj *pDb = mnodeGetDb(pShow->db);
S
slguan 已提交
2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302
  if (pDb == NULL) return 0;

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

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

  while (numOfRows < rows) {
2303
    pShow->pIter = mnodeGetNextChildTable(pShow->pIter, &pTable);
S
slguan 已提交
2304 2305 2306 2307
    if (pTable == NULL) break;

    // not belong to current db
    if (strncmp(pTable->info.tableId, prefix, prefixLen)) {
2308
      mnodeDecTableRef(pTable);
S
slguan 已提交
2309 2310 2311
      continue;
    }

B
Bomin Zhang 已提交
2312
    char tableName[TSDB_TABLE_NAME_LEN] = {0};
S
slguan 已提交
2313
    
2314
    // pattern compare for table name
S
Shengliang Guan 已提交
2315
    mnodeExtractTableName(pTable->info.tableId, tableName);
S
slguan 已提交
2316

B
Bomin Zhang 已提交
2317
    if (pShow->payloadLen > 0 && patternMatch(pShow->payload, tableName, sizeof(tableName) - 1, &info) != TSDB_PATTERN_MATCH) {
2318
      mnodeDecTableRef(pTable);
S
slguan 已提交
2319 2320 2321 2322 2323 2324
      continue;
    }

    int32_t cols = 0;

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

H
Hui Li 已提交
2326
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, tableName, pShow->bytes[cols]);
S
slguan 已提交
2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342
    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;
2343
    
B
Bomin Zhang 已提交
2344
    memset(tableName, 0, sizeof(tableName));
S
slguan 已提交
2345
    if (pTable->info.type == TSDB_CHILD_TABLE) {
S
Shengliang Guan 已提交
2346
      mnodeExtractTableName(pTable->superTable->info.tableId, tableName);
H
Hui Li 已提交
2347
      STR_WITH_MAXSIZE_TO_VARSTR(pWrite, tableName, pShow->bytes[cols]);
S
slguan 已提交
2348
    }
2349
    
S
slguan 已提交
2350 2351 2352
    cols++;

    numOfRows++;
2353
    mnodeDecTableRef(pTable);
S
slguan 已提交
2354 2355 2356 2357 2358
  }

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

S
Shengliang Guan 已提交
2359
  mnodeVacuumResult(data, NUM_OF_COLUMNS, numOfRows, rows, pShow);
2360
  mnodeDecDbRef(pDb);
S
slguan 已提交
2361 2362 2363 2364

  return numOfRows;
}

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

2370
  if (pMsg->pDb == NULL) pMsg->pDb = mnodeGetDbByTableId(pAlter->tableId);
S
slguan 已提交
2371
  if (pMsg->pDb == NULL || pMsg->pDb->status != TSDB_DB_STATUS_READY) {
S
Shengliang Guan 已提交
2372
    mError("app:%p:%p, table:%s, failed to alter table, db not selected", pMsg->rpcMsg.ahandle, pMsg, pAlter->tableId);
2373
    return TSDB_CODE_MND_DB_NOT_SELECTED;
S
[TD-17]  
slguan 已提交
2374 2375
  }

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

2381
  if (pMsg->pTable == NULL) pMsg->pTable = mnodeGetTable(pAlter->tableId);
S
[TD-17]  
slguan 已提交
2382
  if (pMsg->pTable == NULL) {
S
Shengliang Guan 已提交
2383 2384
    mError("app:%p:%p, table:%s, failed to alter table, table not exist", pMsg->rpcMsg.ahandle, pMsg,
           pMsg->pTable->tableId);
2385
    return TSDB_CODE_MND_INVALID_TABLE_NAME;
S
[TD-17]  
slguan 已提交
2386 2387
  }

S
slguan 已提交
2388
  pAlter->type = htons(pAlter->type);
2389 2390
  pAlter->numOfCols = htons(pAlter->numOfCols);
  pAlter->tagValLen = htonl(pAlter->tagValLen);
S
slguan 已提交
2391

S
[TD-17]  
slguan 已提交
2392
  if (pAlter->numOfCols > 2) {
S
Shengliang Guan 已提交
2393 2394
    mError("app:%p:%p, table:%s, error numOfCols:%d in alter table", pMsg->rpcMsg.ahandle, pMsg, pAlter->tableId,
           pAlter->numOfCols);
2395
    return TSDB_CODE_MND_APP_ERROR;
S
[TD-17]  
slguan 已提交
2396 2397 2398 2399 2400 2401
  }

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

2402
  int32_t code = TSDB_CODE_COM_OPS_NOT_SUPPORT;
S
[TD-17]  
slguan 已提交
2403
  if (pMsg->pTable->type == TSDB_SUPER_TABLE) {
S
Shengliang Guan 已提交
2404
    mTrace("app:%p:%p, table:%s, start to alter stable", pMsg->rpcMsg.ahandle, pMsg, pAlter->tableId);
S
[TD-17]  
slguan 已提交
2405
    if (pAlter->type == TSDB_ALTER_TABLE_ADD_TAG_COLUMN) {
2406
      code = mnodeAddSuperTableTag(pMsg, pAlter->schema, 1);
S
[TD-17]  
slguan 已提交
2407
    } else if (pAlter->type == TSDB_ALTER_TABLE_DROP_TAG_COLUMN) {
2408
      code = mnodeDropSuperTableTag(pMsg, pAlter->schema[0].name);
S
[TD-17]  
slguan 已提交
2409
    } else if (pAlter->type == TSDB_ALTER_TABLE_CHANGE_TAG_COLUMN) {
2410
      code = mnodeModifySuperTableTagName(pMsg, pAlter->schema[0].name, pAlter->schema[1].name);
S
[TD-17]  
slguan 已提交
2411
    } else if (pAlter->type == TSDB_ALTER_TABLE_ADD_COLUMN) {
2412
      code = mnodeAddSuperTableColumn(pMsg, pAlter->schema, 1);
S
[TD-17]  
slguan 已提交
2413
    } else if (pAlter->type == TSDB_ALTER_TABLE_DROP_COLUMN) {
2414
      code = mnodeDropSuperTableColumn(pMsg, pAlter->schema[0].name);
S
[TD-17]  
slguan 已提交
2415 2416
    } else {
    }
S
slguan 已提交
2417
  } else {
S
Shengliang Guan 已提交
2418
    mTrace("app:%p:%p, table:%s, start to alter ctable", pMsg->rpcMsg.ahandle, pMsg, pAlter->tableId);
S
[TD-17]  
slguan 已提交
2419
    if (pAlter->type == TSDB_ALTER_TABLE_UPDATE_TAG_VAL) {
2420
      return TSDB_CODE_COM_OPS_NOT_SUPPORT;
S
[TD-17]  
slguan 已提交
2421
    } else if (pAlter->type == TSDB_ALTER_TABLE_ADD_COLUMN) {
2422
      code = mnodeAddNormalTableColumn(pMsg, pAlter->schema, 1);
S
[TD-17]  
slguan 已提交
2423
    } else if (pAlter->type == TSDB_ALTER_TABLE_DROP_COLUMN) {
2424
      code = mnodeDropNormalTableColumn(pMsg, pAlter->schema[0].name);
S
[TD-17]  
slguan 已提交
2425 2426
    } else {
    }
S
slguan 已提交
2427 2428
  }

2429
 return code;
S
[TD-17]  
slguan 已提交
2430
}
2431

S
Shengliang Guan 已提交
2432
static int32_t mnodeGetStreamTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
2433
  SDbObj *pDb = mnodeGetDb(pShow->db);
2434
  if (pDb == NULL) return TSDB_CODE_MND_DB_NOT_SELECTED;
2435 2436 2437 2438

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

H
Haojun Liao 已提交
2439 2440 2441
  SSchema tbnameColSchema = tGetTableNameColumnSchema();
  pShow->bytes[cols] = tbnameColSchema.bytes;
  pSchema[cols].type = tbnameColSchema.type;
2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478
  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 已提交
2479
static int32_t mnodeRetrieveStreamTables(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502
  SDbObj *pDb = mnodeGetDb(pShow->db);
  if (pDb == NULL) return 0;

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

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

  while (numOfRows < rows) {
    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 已提交
2503
    char tableName[TSDB_TABLE_NAME_LEN] = {0};
2504 2505 2506 2507
    
    // pattern compare for table name
    mnodeExtractTableName(pTable->info.tableId, tableName);

B
Bomin Zhang 已提交
2508
    if (pShow->payloadLen > 0 && patternMatch(pShow->payload, tableName, sizeof(tableName) - 1, &info) != TSDB_PATTERN_MATCH) {
2509 2510 2511 2512 2513 2514 2515 2516
      mnodeDecTableRef(pTable);
      continue;
    }

    int32_t cols = 0;

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

H
Hui Li 已提交
2517
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, tableName, pShow->bytes[cols]);
2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528
    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 已提交
2529
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, pTable->sql, pShow->bytes[cols]);    
2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542
    cols++;

    numOfRows++;
    mnodeDecTableRef(pTable);
  }

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

  mnodeVacuumResult(data, NUM_OF_COLUMNS, numOfRows, rows, pShow);
  mnodeDecDbRef(pDb);

  return numOfRows;
2543
}