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

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

48 49
static void *  tsChildTableSdb;
static void *  tsSuperTableSdb;
S
slguan 已提交
50 51
static int32_t tsChildTableUpdateSize;
static int32_t tsSuperTableUpdateSize;
52 53 54 55 56 57 58 59 60 61
static void *  mnodeGetChildTable(char *tableId);
static void *  mnodeGetSuperTable(char *tableId);
static void *  mnodeGetSuperTableByUid(uint64_t uid);
static void    mnodeDropAllChildTablesInStable(SSuperTableObj *pStable);
static void    mnodeAddTableIntoStable(SSuperTableObj *pStable, SChildTableObj *pCtable);
static void    mnodeRemoveTableFromStable(SSuperTableObj *pStable, SChildTableObj *pCtable);

static int32_t mnodeGetShowTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn);
static int32_t mnodeRetrieveShowTables(SShowObj *pShow, char *data, int32_t rows, void *pConn);
static int32_t mnodeGetShowSuperTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn);
62
static int32_t mnodeRetrieveShowSuperTables(SShowObj *pShow, char *data, int32_t rows, void *pConn);
S
Shengliang Guan 已提交
63 64
static int32_t mnodeGetStreamTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn);
static int32_t mnodeRetrieveStreamTables(SShowObj *pShow, char *data, int32_t rows, void *pConn);
65
 
H
Haojun Liao 已提交
66
static int32_t mnodeProcessCreateTableMsg(SMnodeMsg *pMsg);
67 68 69 70
static int32_t mnodeProcessCreateSuperTableMsg(SMnodeMsg *pMsg);
static int32_t mnodeProcessCreateChildTableMsg(SMnodeMsg *pMsg);
static void    mnodeProcessCreateChildTableRsp(SRpcMsg *rpcMsg);

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

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

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

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

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

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

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

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

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

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

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

  if (pTable->info.type == TSDB_CHILD_TABLE) {
128
    // add ref
129
    pTable->superTable = mnodeGetSuperTableByUid(pTable->suid);
S
Shengliang Guan 已提交
130 131 132 133
    if (pTable->superTable != NULL) {
      mnodeAddTableIntoStable(pTable->superTable, pTable);
      grantAdd(TSDB_GRANT_TIMESERIES, pTable->superTable->numOfColumns - 1);
      if (pAcct) pAcct->acctInfo.numOfTimeSeries += (pTable->superTable->numOfColumns - 1);
S
TD-1719  
Shengliang Guan 已提交
134 135
    } else {
      mError("table:%s:%p, correspond stable not found suid:%" PRIu64, pTable->info.tableId, pTable, pTable->suid);
S
Shengliang Guan 已提交
136
    }
S
slguan 已提交
137 138
  } else {
    grantAdd(TSDB_GRANT_TIMESERIES, pTable->numOfColumns - 1);
139
    if (pAcct) pAcct->acctInfo.numOfTimeSeries += (pTable->numOfColumns - 1);
S
slguan 已提交
140
  }
S
slguan 已提交
141

142 143 144 145 146 147
  if (pDb) mnodeAddTableIntoDb(pDb);
  if (pVgroup) mnodeAddTableIntoVgroup(pVgroup, pTable);

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

  return TSDB_CODE_SUCCESS;
}

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

158 159 160 161 162 163 164
  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 已提交
165 166

  if (pTable->info.type == TSDB_CHILD_TABLE) {
167 168 169 170 171 172
    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 已提交
173 174
  } else {
    grantRestore(TSDB_GRANT_TIMESERIES, pTable->numOfColumns - 1);
175
    if (pAcct != NULL) pAcct->acctInfo.numOfTimeSeries -= (pTable->numOfColumns - 1);
S
slguan 已提交
176
  }
177 178 179 180 181 182 183
  
  if (pDb != NULL) mnodeRemoveTableFromDb(pDb);
  if (pVgroup != NULL) mnodeRemoveTableFromVgroup(pVgroup, pTable);

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

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

S
slguan 已提交
212 213 214
  return TSDB_CODE_SUCCESS;
}

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

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

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

240 241
  pOper->rowSize = len;

S
slguan 已提交
242 243 244
  return TSDB_CODE_SUCCESS;
}

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

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

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

  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 已提交
265
      mnodeDestroyChildTable(pTable);
266
      return TSDB_CODE_MND_INVALID_TABLE_TYPE;
S
slguan 已提交
267
    }
268 269 270 271 272 273
    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 已提交
274
        mnodeDestroyChildTable(pTable);
275
        return TSDB_CODE_MND_OUT_OF_MEMORY;
276 277
      }
      memcpy(pTable->sql, pOper->rowData + len, pTable->sqlLen);
S
slguan 已提交
278 279 280 281 282 283 284
    }
  }

  pOper->pObj = pTable;
  return TSDB_CODE_SUCCESS;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  return TSDB_CODE_SUCCESS;
}

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

S
slguan 已提交
454 455 456
  return TSDB_CODE_SUCCESS;
}

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

467
    memcpy(pTable, pNew, sizeof(SSuperTableObj));
468 469 470

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

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

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

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

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

500
  pOper->rowSize = len;
S
slguan 已提交
501 502 503 504

  return TSDB_CODE_SUCCESS;
}

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

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

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

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

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

  return TSDB_CODE_SUCCESS;
}

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

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

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

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

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

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

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

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

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

597
  mnodeAddPeerMsgHandle(TSDB_MSG_TYPE_DM_CONFIG_TABLE, mnodeProcessTableCfgMsg);
S
slguan 已提交
598

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

S
slguan 已提交
606
  return TSDB_CODE_SUCCESS;
H
hzcheng 已提交
607 608
}

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

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

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

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

S
Shengliang Guan 已提交
631 632
  sdbFreeIter(pIter);

633 634 635
  return NULL;
}

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

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

S
slguan 已提交
647
  return NULL;
S
#1177  
slguan 已提交
648
}
H
hzcheng 已提交
649

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  return code;
}

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

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

S
TD-1719  
Shengliang Guan 已提交
844
  int64_t us = taosGetTimestampUs();
845
  pStable->info.tableId = strdup(pCreate->tableId);
S
slguan 已提交
846 847
  pStable->info.type    = TSDB_SUPER_TABLE;
  pStable->createdTime  = taosGetTimestampMs();
S
TD-1719  
Shengliang Guan 已提交
848
  pStable->uid          = (us << 24) + ((sdbGetVersion() & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
S
slguan 已提交
849
  pStable->sversion     = 0;
S
TD-355  
Shengliang Guan 已提交
850
  pStable->tversion     = 0;
S
slguan 已提交
851 852 853
  pStable->numOfColumns = htons(pCreate->numOfColumns);
  pStable->numOfTags    = htons(pCreate->numOfTags);

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

874 875 876
  pMsg->pTable = (STableObj *)pStable;
  mnodeIncTableRef(pMsg->pTable);

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

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

  return code;
S
slguan 已提交
894 895
}

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

  return code;
}
906

907
static int32_t mnodeProcessDropSuperTableMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
908 909
  if (pMsg == NULL) return TSDB_CODE_MND_APP_ERROR;

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

S
TD-1732  
Shengliang Guan 已提交
918 919
      SDropSTableMsg *pDrop = rpcMallocCont(sizeof(SDropSTableMsg));
      pDrop->contLen = htonl(sizeof(SDropSTableMsg));
920
      pDrop->vgId = htonl(pVgroup->vgId);
S
slguan 已提交
921
      pDrop->uid = htobe64(pStable->uid);
S
Shengliang Guan 已提交
922
      mnodeExtractTableName(pStable->info.tableId, pDrop->tableId);
S
Shengliang Guan 已提交
923

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

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

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

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

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

  return -1;
}

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

  return code;
}

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

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

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

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

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

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

S
slguan 已提交
1003
  pStable->numOfTags += ntags;
S
TD-355  
Shengliang Guan 已提交
1004
  pStable->tversion++;
S
slguan 已提交
1005

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

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

S
Shengliang Guan 已提交
1017
  return sdbUpdateRow(&oper);
S
slguan 已提交
1018 1019
}

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

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

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

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

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

S
Shengliang Guan 已提交
1051
  return sdbUpdateRow(&oper);
S
slguan 已提交
1052 1053
}

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

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

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

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

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

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

S
Shengliang Guan 已提交
1095
  return sdbUpdateRow(&oper);
S
slguan 已提交
1096 1097
}

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

  return -1;
}

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

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

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

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

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

S
slguan 已提交
1141 1142 1143
  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 已提交
1144

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

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

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

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

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

S
Shengliang Guan 已提交
1169
 return sdbUpdateRow(&oper);
S
slguan 已提交
1170 1171
}

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

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

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

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

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

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

S
Shengliang Guan 已提交
1214
  return sdbUpdateRow(&oper);
S
slguan 已提交
1215 1216
}

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

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

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

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

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

  SSdbOper oper = {
    .type = SDB_OPER_GLOBAL,
    .table = tsSuperTableSdb,
    .pObj = pStable,
    .pMsg = pMsg,
S
Shengliang Guan 已提交
1255
    .writeCb = mnodeChangeSuperTableColumnCb
S
Shengliang Guan 已提交
1256 1257
  };

S
Shengliang Guan 已提交
1258
  return sdbUpdateRow(&oper);
S
Shengliang Guan 已提交
1259 1260
}

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

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

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

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

1315
  mnodeDecDbRef(pDb);
S
slguan 已提交
1316 1317 1318
  return 0;
}

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

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

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

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

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

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

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

    cols = 0;

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

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

1393
  mnodeVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
1394
  mnodeDecDbRef(pDb);
S
slguan 已提交
1395

S
slguan 已提交
1396 1397 1398
  return numOfRows;
}

1399
void mnodeDropAllSuperTables(SDbObj *pDropDb) {
S
Shengliang Guan 已提交
1400
  void *  pIter= NULL;
S
slguan 已提交
1401 1402 1403
  int32_t numOfTables = 0;
  SSuperTableObj *pTable = NULL;

S
TD-1743  
Shengliang Guan 已提交
1404 1405 1406 1407 1408
  char prefix[64] = {0};
  tstrncpy(prefix, pDropDb->name, 64);
  strcat(prefix, TS_PATH_DELIMITER);
  int32_t prefixLen = strlen(prefix);

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

S
slguan 已提交
1411
  while (1) {
1412
    pIter = mnodeGetNextSuperTable(pIter, &pTable);
S
slguan 已提交
1413
    if (pTable == NULL) break;
S
slguan 已提交
1414

S
TD-1743  
Shengliang Guan 已提交
1415
    if (strncmp(prefix, pTable->info.tableId, prefixLen) == 0) {
S
slguan 已提交
1416
      SSdbOper oper = {
S
slguan 已提交
1417
        .type = SDB_OPER_LOCAL,
S
slguan 已提交
1418 1419 1420 1421 1422 1423
        .table = tsSuperTableSdb,
        .pObj = pTable,
      };
      sdbDeleteRow(&oper);
      numOfTables ++;
    }
S
slguan 已提交
1424

1425
    mnodeDecTableRef(pTable);
S
slguan 已提交
1426
  }
S
slguan 已提交
1427

S
Shengliang Guan 已提交
1428 1429
  sdbFreeIter(pIter);

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

1433
static int32_t mnodeSetSchemaFromSuperTable(SSchema *pSchema, SSuperTableObj *pTable) {
S
slguan 已提交
1434
  int32_t numOfCols = pTable->numOfColumns + pTable->numOfTags;
1435 1436
  assert(numOfCols <= TSDB_MAX_COLUMNS);
  
S
slguan 已提交
1437
  for (int32_t i = 0; i < numOfCols; ++i) {
B
Bomin Zhang 已提交
1438
    tstrncpy(pSchema->name, pTable->schema[i].name, sizeof(pSchema->name));
S
slguan 已提交
1439 1440 1441 1442 1443 1444 1445 1446 1447
    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);
}

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

B
Bomin Zhang 已提交
1464
  pMsg->rpcRsp.len = pMeta->contLen;
S
slguan 已提交
1465 1466
  pMeta->contLen = htons(pMeta->contLen);

1467
  pMsg->rpcRsp.rsp = pMeta;
S
Shengliang Guan 已提交
1468

1469
  mDebug("app:%p:%p, stable:%s, uid:%" PRIu64 " table meta is retrieved", pMsg->rpcMsg.ahandle, pMsg,
S
Shengliang Guan 已提交
1470
         pTable->info.tableId, pTable->uid);
1471
  return TSDB_CODE_SUCCESS;
S
slguan 已提交
1472 1473
}

1474
static int32_t mnodeProcessSuperTableVgroupMsg(SMnodeMsg *pMsg) {
S
TD-1732  
Shengliang Guan 已提交
1475
  SSTableVgroupMsg *pInfo = pMsg->rpcMsg.pCont;
H
hjxilinx 已提交
1476
  int32_t numOfTable = htonl(pInfo->numOfTables);
1477

S
scripts  
Shengliang Guan 已提交
1478
  // reserve space
S
TD-1732  
Shengliang Guan 已提交
1479
  int32_t contLen = sizeof(SSTableVgroupRspMsg) + 32 * sizeof(SVgroupMsg) + sizeof(SVgroupsMsg);
1480
  for (int32_t i = 0; i < numOfTable; ++i) {
S
TD-1732  
Shengliang Guan 已提交
1481
    char *stableName = (char *)pInfo + sizeof(SSTableVgroupMsg) + (TSDB_TABLE_FNAME_LEN)*i;
1482
    SSuperTableObj *pTable = mnodeGetSuperTable(stableName);
1483
    if (pTable != NULL && pTable->vgHash != NULL) {
S
TD-1732  
Shengliang Guan 已提交
1484 1485 1486
      contLen += (taosHashGetSize(pTable->vgHash) * sizeof(SVgroupMsg) + sizeof(SVgroupsMsg));
    }

1487
    mnodeDecTableRef(pTable);
1488 1489
  }

S
TD-1732  
Shengliang Guan 已提交
1490
  SSTableVgroupRspMsg *pRsp = rpcMallocCont(contLen);
S
slguan 已提交
1491
  if (pRsp == NULL) {
1492
    return TSDB_CODE_MND_OUT_OF_MEMORY;
1493
  }
1494

1495
  pRsp->numOfTables = 0;
S
TD-1732  
Shengliang Guan 已提交
1496
  char *msg = (char *)pRsp + sizeof(SSTableVgroupRspMsg);
1497 1498

  for (int32_t i = 0; i < numOfTable; ++i) {
S
TD-1732  
Shengliang Guan 已提交
1499
    char *          stableName = (char *)pInfo + sizeof(SSTableVgroupMsg) + (TSDB_TABLE_FNAME_LEN)*i;
1500
    SSuperTableObj *pTable = mnodeGetSuperTable(stableName);
1501
    if (pTable == NULL) {
S
Shengliang Guan 已提交
1502
      mError("app:%p:%p, stable:%s, not exist while get stable vgroup info", pMsg->rpcMsg.ahandle, pMsg, stableName);
1503 1504 1505 1506
      mnodeDecTableRef(pTable);
      continue;
    }
    if (pTable->vgHash == NULL) {
S
TD-1719  
Shengliang Guan 已提交
1507
      mDebug("app:%p:%p, stable:%s, no vgroup exist while get stable vgroup info", pMsg->rpcMsg.ahandle, pMsg,
S
Shengliang Guan 已提交
1508
             stableName);
1509 1510
      mnodeDecTableRef(pTable);

1511 1512
      // even this super table has no corresponding table, still return
      pRsp->numOfTables++;
1513

H
Haojun Liao 已提交
1514 1515
      SVgroupsMsg *pVgroupMsg = (SVgroupsMsg *)msg;
      pVgroupMsg->numOfVgroups = 0;
1516
      
H
Haojun Liao 已提交
1517
      msg += sizeof(SVgroupsMsg);
1518
    } else {
H
Haojun Liao 已提交
1519
      SVgroupsMsg *pVgroupMsg = (SVgroupsMsg *)msg;
1520

1521 1522 1523 1524 1525 1526
      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;
1527

H
Haojun Liao 已提交
1528
        pVgroupMsg->vgroups[vgSize].vgId = htonl(pVgroup->vgId);
H
Haojun Liao 已提交
1529 1530
        pVgroupMsg->vgroups[vgSize].numOfEps = 0;

1531 1532 1533
        for (int32_t vn = 0; vn < pVgroup->numOfVnodes; ++vn) {
          SDnodeObj *pDnode = pVgroup->vnodeGid[vn].pDnode;
          if (pDnode == NULL) break;
1534

H
Haojun Liao 已提交
1535 1536
          tstrncpy(pVgroupMsg->vgroups[vgSize].epAddr[vn].fqdn, pDnode->dnodeFqdn, TSDB_FQDN_LEN);
          pVgroupMsg->vgroups[vgSize].epAddr[vn].port = htons(pDnode->dnodePort);
1537

H
Haojun Liao 已提交
1538
          pVgroupMsg->vgroups[vgSize].numOfEps++;
1539
        }
1540

1541 1542 1543
        vgSize++;
        mnodeDecVgroupRef(pVgroup);
      }
1544

1545 1546 1547
      taosHashDestroyIter(pIter);
      mnodeDecTableRef(pTable);

H
Haojun Liao 已提交
1548
      pVgroupMsg->numOfVgroups = htonl(vgSize);
1549

1550
      // one table is done, try the next table
S
TD-1732  
Shengliang Guan 已提交
1551
      msg += sizeof(SVgroupsMsg) + vgSize * sizeof(SVgroupMsg);
1552 1553
      pRsp->numOfTables++;
    }
1554
  }
S
slguan 已提交
1555

1556 1557
  if (pRsp->numOfTables != numOfTable) {
    rpcFreeCont(pRsp);
1558
    return TSDB_CODE_MND_INVALID_TABLE_NAME;
1559 1560 1561 1562
  } else {
    pRsp->numOfTables = htonl(pRsp->numOfTables);
    pMsg->rpcRsp.rsp = pRsp;
    pMsg->rpcRsp.len = msg - (char *)pRsp;
1563

1564 1565
    return TSDB_CODE_SUCCESS;
  }
S
slguan 已提交
1566 1567
}

1568
static void mnodeProcessDropSuperTableRsp(SRpcMsg *rpcMsg) {
1569
  mInfo("drop stable rsp received, result:%s", tstrerror(rpcMsg->code));
S
slguan 已提交
1570 1571
}

S
Shengliang Guan 已提交
1572
static void *mnodeBuildCreateChildTableMsg(SCMCreateTableMsg *pMsg, SChildTableObj *pTable) {
1573
  STagData *  pTagData = NULL;
S
slguan 已提交
1574 1575 1576
  int32_t tagDataLen = 0;
  int32_t totalCols = 0;
  int32_t contLen = 0;
S
Shengliang Guan 已提交
1577
  if (pTable->info.type == TSDB_CHILD_TABLE) {
S
slguan 已提交
1578
    totalCols = pTable->superTable->numOfColumns + pTable->superTable->numOfTags;
S
Shengliang Guan 已提交
1579
    contLen = sizeof(SMDCreateTableMsg) + totalCols * sizeof(SSchema) + pTable->sqlLen;
S
Shengliang Guan 已提交
1580 1581
    if (pMsg != NULL) {
      pTagData = (STagData *)pMsg->schema;
S
Shengliang Guan 已提交
1582
      tagDataLen = htonl(pTagData->dataLen);
H
Hongze Cheng 已提交
1583
      contLen += tagDataLen;
S
Shengliang Guan 已提交
1584
    }
S
slguan 已提交
1585 1586 1587 1588 1589 1590 1591
  } else {
    totalCols = pTable->numOfColumns;
    contLen = sizeof(SMDCreateTableMsg) + totalCols * sizeof(SSchema) + pTable->sqlLen;
  }

  SMDCreateTableMsg *pCreate = rpcMallocCont(contLen);
  if (pCreate == NULL) {
1592
    terrno = TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
1593 1594 1595
    return NULL;
  }

S
Shengliang Guan 已提交
1596
  mnodeExtractTableName(pTable->info.tableId, pCreate->tableId);
S
slguan 已提交
1597 1598 1599 1600
  pCreate->contLen       = htonl(contLen);
  pCreate->vgId          = htonl(pTable->vgId);
  pCreate->tableType     = pTable->info.type;
  pCreate->createdTime   = htobe64(pTable->createdTime);
H
Haojun Liao 已提交
1601
  pCreate->tid           = htonl(pTable->tid);
S
slguan 已提交
1602 1603 1604 1605
  pCreate->sqlDataLen    = htonl(pTable->sqlLen);
  pCreate->uid           = htobe64(pTable->uid);
  
  if (pTable->info.type == TSDB_CHILD_TABLE) {
S
Shengliang Guan 已提交
1606
    mnodeExtractTableName(pTable->superTable->info.tableId, pCreate->superTableId);
S
slguan 已提交
1607 1608 1609
    pCreate->numOfColumns  = htons(pTable->superTable->numOfColumns);
    pCreate->numOfTags     = htons(pTable->superTable->numOfTags);
    pCreate->sversion      = htonl(pTable->superTable->sversion);
1610
    pCreate->tversion      = htonl(pTable->superTable->tversion);
S
slguan 已提交
1611 1612 1613 1614 1615 1616
    pCreate->tagDataLen    = htonl(tagDataLen);
    pCreate->superTableUid = htobe64(pTable->superTable->uid);
  } else {
    pCreate->numOfColumns  = htons(pTable->numOfColumns);
    pCreate->numOfTags     = 0;
    pCreate->sversion      = htonl(pTable->sversion);
1617
    pCreate->tversion      = 0;
S
slguan 已提交
1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
    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) {
1635
    memcpy(pCreate->data + totalCols * sizeof(SSchema), pTagData->data, tagDataLen);
1636 1637
  }

S
Shengliang Guan 已提交
1638
  if (pTable->info.type == TSDB_STREAM_TABLE) {
1639
    memcpy(pCreate->data + totalCols * sizeof(SSchema), pTable->sql, pTable->sqlLen);
S
slguan 已提交
1640 1641 1642 1643 1644
  }

  return pCreate;
}

S
Shengliang Guan 已提交
1645
static int32_t mnodeDoCreateChildTableFp(SMnodeMsg *pMsg) {
1646
  SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;
S
Shengliang Guan 已提交
1647 1648
  assert(pTable);

S
Shengliang Guan 已提交
1649
  mDebug("app:%p:%p, table:%s, created in mnode, vgId:%d sid:%d, uid:%" PRIu64, pMsg->rpcMsg.ahandle, pMsg,
H
Haojun Liao 已提交
1650
         pTable->info.tableId, pTable->vgId, pTable->tid, pTable->uid);
1651

1652
  SCMCreateTableMsg *pCreate = pMsg->rpcMsg.pCont;
1653
  SMDCreateTableMsg *pMDCreate = mnodeBuildCreateChildTableMsg(pCreate, pTable);
1654 1655 1656 1657
  if (pMDCreate == NULL) {
    return terrno;
  }

1658
  SRpcEpSet epSet = mnodeGetEpSetFromVgroup(pMsg->pVgroup);
1659
  SRpcMsg rpcMsg = {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
1660
      .ahandle = pMsg,
1661 1662 1663 1664 1665 1666
      .pCont   = pMDCreate,
      .contLen = htonl(pMDCreate->contLen),
      .code    = 0,
      .msgType = TSDB_MSG_TYPE_MD_CREATE_TABLE
  };

1667
  dnodeSendMsgToDnode(&epSet, &rpcMsg);
1668 1669 1670
  return TSDB_CODE_MND_ACTION_IN_PROGRESS;
}

S
Shengliang Guan 已提交
1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691
static int32_t mnodeDoCreateChildTableCb(SMnodeMsg *pMsg, int32_t code) {
  SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;
  SCMCreateTableMsg *pCreate = pMsg->rpcMsg.pCont;
  assert(pTable);

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

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

      dnodeSendRpcMnodeWriteRsp(pMsg, TSDB_CODE_SUCCESS);
    }
    return TSDB_CODE_MND_ACTION_IN_PROGRESS;
  } else {
    mError("app:%p:%p, table:%s, failed to create table sid:%d, uid:%" PRIu64 ", reason:%s", pMsg->rpcMsg.ahandle, pMsg,
H
Haojun Liao 已提交
1692
           pTable->info.tableId, pTable->tid, pTable->uid, tstrerror(code));
S
Shengliang Guan 已提交
1693 1694 1695 1696 1697 1698
    SSdbOper desc = {.type = SDB_OPER_GLOBAL, .pObj = pTable, .table = tsChildTableSdb};
    sdbDeleteRow(&desc);
    return code;
  }
}

1699
static int32_t mnodeDoCreateChildTable(SMnodeMsg *pMsg, int32_t tid) {
1700
  SVgObj *pVgroup = pMsg->pVgroup;
1701
  SCMCreateTableMsg *pCreate = pMsg->rpcMsg.pCont;
1702
  SChildTableObj *pTable = calloc(1, sizeof(SChildTableObj));
S
slguan 已提交
1703
  if (pTable == NULL) {
S
Shengliang Guan 已提交
1704
    mError("app:%p:%p, table:%s, failed to alloc memory", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId);
1705
    return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
1706 1707 1708 1709 1710 1711 1712 1713
  }

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

1714 1715
  pTable->info.tableId = strdup(pCreate->tableId);  
  pTable->createdTime  = taosGetTimestampMs();
H
Haojun Liao 已提交
1716
  pTable->tid          = tid;
1717
  pTable->vgId         = pVgroup->vgId;
S
TD-1719  
Shengliang Guan 已提交
1718

S
slguan 已提交
1719
  if (pTable->info.type == TSDB_CHILD_TABLE) {
S
TD-1719  
Shengliang Guan 已提交
1720
    STagData *pTagData = (STagData *)pCreate->schema;  // it is a tag key
1721 1722
    if (pMsg->pSTable == NULL) pMsg->pSTable = mnodeGetSuperTable(pTagData->name);
    if (pMsg->pSTable == NULL) {
S
Shengliang Guan 已提交
1723 1724
      mError("app:%p:%p, table:%s, corresponding super table:%s does not exist", pMsg->rpcMsg.ahandle, pMsg,
             pCreate->tableId, pTagData->name);
1725
      mnodeDestroyChildTable(pTable);
1726
      return TSDB_CODE_MND_INVALID_TABLE_NAME;
S
slguan 已提交
1727
    }
1728

1729
    pTable->suid = pMsg->pSTable->uid;
H
Haojun Liao 已提交
1730
    pTable->uid = (((uint64_t)pTable->vgId) << 48) + ((((uint64_t)pTable->tid) & ((1ul << 24) - 1ul)) << 24) +
1731
                  ((sdbGetVersion() & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
1732
    pTable->superTable = pMsg->pSTable;
S
slguan 已提交
1733
  } else {
1734 1735 1736 1737
    if (pTable->info.type == TSDB_SUPER_TABLE) {
      int64_t us = taosGetTimestampUs();
      pTable->uid = (us << 24) + ((sdbGetVersion() & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
    } else {
H
Haojun Liao 已提交
1738
      pTable->uid = (((uint64_t)pTable->vgId) << 48) + ((((uint64_t)pTable->tid) & ((1ul << 24) - 1ul)) << 24) +
1739 1740 1741
                    ((sdbGetVersion() & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
    }

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

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

S
Shengliang Guan 已提交
1779 1780 1781 1782 1783 1784 1785
  SSdbOper desc = {
    .type  = SDB_OPER_GLOBAL,
    .pObj  = pTable,
    .table = tsChildTableSdb,
    .pMsg  = pMsg,
    .reqFp = mnodeDoCreateChildTableFp
  };
S
slguan 已提交
1786
  
1787
  int32_t code = sdbInsertRow(&desc);
S
Shengliang Guan 已提交
1788
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
1789 1790
    mnodeDestroyChildTable(pTable);
    pMsg->pTable = NULL;
S
Shengliang Guan 已提交
1791
    mError("app:%p:%p, table:%s, failed to create, reason:%s", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId,
S
Shengliang Guan 已提交
1792
           tstrerror(code));
S
TD-1038  
Shengliang Guan 已提交
1793 1794
  } else {
    mDebug("app:%p:%p, table:%s, allocated in vgroup, vgId:%d sid:%d uid:%" PRIu64, pMsg->rpcMsg.ahandle, pMsg,
H
Haojun Liao 已提交
1795
           pTable->info.tableId, pVgroup->vgId, pTable->tid, pTable->uid);
1796
  }
S
Shengliang Guan 已提交
1797 1798

  return code;
S
slguan 已提交
1799 1800
}

1801
static int32_t mnodeProcessCreateChildTableMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
1802
  SCMCreateTableMsg *pCreate = pMsg->rpcMsg.pCont;
S
slguan 已提交
1803 1804
  int32_t code = grantCheck(TSDB_GRANT_TIMESERIES);
  if (code != TSDB_CODE_SUCCESS) {
1805 1806
    mError("app:%p:%p, table:%s, failed to create, grant timeseries failed", pMsg->rpcMsg.ahandle, pMsg,
           pCreate->tableId);
1807
    return code;
S
slguan 已提交
1808 1809
  }

1810
  if (pMsg->retry == 0) {
1811
    if (pMsg->pTable == NULL) {
S
Shengliang Guan 已提交
1812
      SVgObj *pVgroup = NULL;
H
Haojun Liao 已提交
1813 1814
      int32_t tid = 0;
      code = mnodeGetAvailableVgroup(pMsg, &pVgroup, &tid);
1815 1816 1817 1818
      if (code != TSDB_CODE_SUCCESS) {
        mDebug("app:%p:%p, table:%s, failed to get available vgroup, reason:%s", pMsg->rpcMsg.ahandle, pMsg,
               pCreate->tableId, tstrerror(code));
        return code;
1819 1820
      }

S
Shengliang Guan 已提交
1821 1822
      if (pMsg->pVgroup != NULL) {
        mnodeDecVgroupRef(pMsg->pVgroup);
1823
      }
1824

S
Shengliang Guan 已提交
1825 1826 1827
      pMsg->pVgroup = pVgroup;
      mnodeIncVgroupRef(pVgroup);

H
Haojun Liao 已提交
1828
      return mnodeDoCreateChildTable(pMsg, tid);
1829
    }
1830
  } else {
1831
    if (pMsg->pTable == NULL) pMsg->pTable = mnodeGetTable(pCreate->tableId);
1832
  }
1833

S
slguan 已提交
1834
  if (pMsg->pTable == NULL) {
B
Bomin Zhang 已提交
1835
    mError("app:%p:%p, table:%s, object not found, retry:%d reason:%s", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId, pMsg->retry,
1836
           tstrerror(terrno));
1837
    return terrno;
1838
  } else {
1839
    mDebug("app:%p:%p, table:%s, send create msg to vnode again", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId);
S
Shengliang Guan 已提交
1840
    return mnodeDoCreateChildTableFp(pMsg);
S
slguan 已提交
1841 1842 1843
  }
}

S
Shengliang Guan 已提交
1844
static int32_t mnodeSendDropChildTableMsg(SMnodeMsg *pMsg, bool needReturn) {
S
slguan 已提交
1845
  SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;
S
Shengliang Guan 已提交
1846
  mLInfo("app:%p:%p, ctable:%s, is dropped from sdb", pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId);
S
slguan 已提交
1847 1848 1849

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

H
Haojun Liao 已提交
1855
  tstrncpy(pDrop->tableId, pTable->info.tableId, TSDB_TABLE_FNAME_LEN);
S
slguan 已提交
1856 1857
  pDrop->vgId    = htonl(pTable->vgId);
  pDrop->contLen = htonl(sizeof(SMDDropTableMsg));
H
Haojun Liao 已提交
1858
  pDrop->tid     = htonl(pTable->tid);
S
slguan 已提交
1859 1860
  pDrop->uid     = htobe64(pTable->uid);

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

S
Shengliang Guan 已提交
1863
  mInfo("app:%p:%p, ctable:%s, send drop ctable msg, vgId:%d sid:%d uid:%" PRIu64, pMsg->rpcMsg.ahandle, pMsg,
H
Haojun Liao 已提交
1864
        pDrop->tableId, pTable->vgId, pTable->tid, pTable->uid);
S
Shengliang Guan 已提交
1865

S
slguan 已提交
1866
  SRpcMsg rpcMsg = {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
1867
    .ahandle = pMsg,
S
slguan 已提交
1868 1869 1870 1871 1872 1873
    .pCont   = pDrop,
    .contLen = sizeof(SMDDropTableMsg),
    .code    = 0,
    .msgType = TSDB_MSG_TYPE_MD_DROP_TABLE
  };

1874 1875
  if (!needReturn) rpcMsg.ahandle = NULL;

1876
  dnodeSendMsgToDnode(&epSet, &rpcMsg);
1877

1878
  return TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
slguan 已提交
1879 1880
}

S
Shengliang Guan 已提交
1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904
static int32_t mnodeDropChildTableCb(SMnodeMsg *pMsg, int32_t code) {
  if (code != TSDB_CODE_SUCCESS) {
    SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;
    mError("app:%p:%p, ctable:%s, failed to drop, sdb error", pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId);
    return code;
  } 

  return mnodeSendDropChildTableMsg(pMsg, true);
}

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

  SSdbOper oper = {
    .type  = SDB_OPER_GLOBAL,
    .table = tsChildTableSdb,
    .pObj  = pTable,
    .pMsg  = pMsg,
S
Shengliang Guan 已提交
1905
    .writeCb = mnodeDropChildTableCb
S
Shengliang Guan 已提交
1906 1907 1908
  };

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

S
Shengliang Guan 已提交
1914 1915 1916
  return code;
}

1917
static int32_t mnodeFindNormalTableColumnIndex(SChildTableObj *pTable, char *colName) {
S
slguan 已提交
1918
  SSchema *schema = (SSchema *) pTable->schema;
S
slguan 已提交
1919 1920 1921
  for (int32_t col = 0; col < pTable->numOfColumns; col++) {
    if (strcasecmp(schema[col].name, colName) == 0) {
      return col;
S
slguan 已提交
1922 1923 1924 1925 1926 1927
    }
  }

  return -1;
}

1928
static int32_t mnodeAlterNormalTableColumnCb(SMnodeMsg *pMsg, int32_t code) {
1929
  SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;
1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950
  if (code != TSDB_CODE_SUCCESS) {
    mError("app:%p:%p, ctable %s, failed to alter column, reason:%s", pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId,
           tstrerror(code));
    return code;
  }

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

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

1951
  SRpcEpSet epSet = mnodeGetEpSetFromVgroup(pMsg->pVgroup);
1952
  SRpcMsg rpcMsg = {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
1953
      .ahandle = pMsg,
1954 1955 1956 1957 1958 1959
      .pCont   = pMDCreate,
      .contLen = htonl(pMDCreate->contLen),
      .code    = 0,
      .msgType = TSDB_MSG_TYPE_MD_ALTER_TABLE
  };

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

1963
  dnodeSendMsgToDnode(&epSet, &rpcMsg);
1964
  return TSDB_CODE_MND_ACTION_IN_PROGRESS;
1965 1966 1967 1968
}

static int32_t mnodeAddNormalTableColumn(SMnodeMsg *pMsg, SSchema schema[], int32_t ncols) {
  SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;
1969
  SDbObj *pDb = pMsg->pDb;
S
slguan 已提交
1970
  if (ncols <= 0) {
B
Bomin Zhang 已提交
1971
    mError("app:%p:%p, ctable:%s, add column, ncols:%d <= 0", pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId, ncols);
1972
    return TSDB_CODE_MND_APP_ERROR;
S
slguan 已提交
1973 1974 1975
  }

  for (int32_t i = 0; i < ncols; i++) {
1976
    if (mnodeFindNormalTableColumnIndex(pTable, schema[i].name) > 0) {
1977
      mError("app:%p:%p, ctable:%s, add column, column:%s already exist", pMsg->rpcMsg.ahandle, pMsg,
S
Shengliang Guan 已提交
1978
             pTable->info.tableId, schema[i].name);
1979
      return TSDB_CODE_MND_FIELD_ALREAY_EXIST;
S
slguan 已提交
1980 1981 1982 1983 1984 1985
    }
  }

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

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

S
slguan 已提交
1988
  SSchema *tschema = (SSchema *) (pTable->schema + pTable->numOfColumns);
S
slguan 已提交
1989 1990 1991 1992 1993 1994
  for (int32_t i = 0; i < ncols; i++) {
    tschema[i].colId = pTable->nextColId++;
  }

  pTable->numOfColumns += ncols;
  pTable->sversion++;
S
[TD-17]  
slguan 已提交
1995
  
1996
  SAcctObj *pAcct = mnodeGetAcct(pDb->acct);
S
[TD-17]  
slguan 已提交
1997
  if (pAcct != NULL) {
S
slguan 已提交
1998
    pAcct->acctInfo.numOfTimeSeries += ncols;
1999
    mnodeDecAcctRef(pAcct);
S
[TD-17]  
slguan 已提交
2000
  }
2001

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

S
slguan 已提交
2004
  SSdbOper oper = {
S
slguan 已提交
2005
    .type = SDB_OPER_GLOBAL,
S
[TD-17]  
slguan 已提交
2006
    .table = tsChildTableSdb,
2007
    .pObj = pTable,
2008
    .pMsg = pMsg,
S
Shengliang Guan 已提交
2009
    .writeCb = mnodeAlterNormalTableColumnCb
S
[TD-17]  
slguan 已提交
2010
  };
S
slguan 已提交
2011

S
Shengliang Guan 已提交
2012
  return sdbUpdateRow(&oper);
S
slguan 已提交
2013 2014
}

2015
static int32_t mnodeDropNormalTableColumn(SMnodeMsg *pMsg, char *colName) {
2016
  SDbObj *pDb = pMsg->pDb;
2017
  SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;
2018
  int32_t col = mnodeFindNormalTableColumnIndex(pTable, colName);
S
slguan 已提交
2019
  if (col <= 0) {
2020
    mError("app:%p:%p, ctable:%s, drop column, column:%s not exist", pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId,
S
Shengliang Guan 已提交
2021
           colName);
2022
    return TSDB_CODE_MND_FIELD_NOT_EXIST;
S
slguan 已提交
2023 2024
  }

S
slguan 已提交
2025
  memmove(pTable->schema + col, pTable->schema + col + 1, sizeof(SSchema) * (pTable->numOfColumns - col - 1));
S
slguan 已提交
2026 2027 2028
  pTable->numOfColumns--;
  pTable->sversion++;

2029
  SAcctObj *pAcct = mnodeGetAcct(pDb->acct);
S
[TD-17]  
slguan 已提交
2030 2031
  if (pAcct != NULL) {
    pAcct->acctInfo.numOfTimeSeries--;
2032
    mnodeDecAcctRef(pAcct);
S
[TD-17]  
slguan 已提交
2033
  }
2034

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

S
slguan 已提交
2037
  SSdbOper oper = {
S
slguan 已提交
2038
    .type = SDB_OPER_GLOBAL,
S
[TD-17]  
slguan 已提交
2039
    .table = tsChildTableSdb,
2040
    .pObj = pTable,
2041
    .pMsg = pMsg,
S
Shengliang Guan 已提交
2042
    .writeCb = mnodeAlterNormalTableColumnCb
S
[TD-17]  
slguan 已提交
2043
  };
S
slguan 已提交
2044

S
Shengliang Guan 已提交
2045
  return sdbUpdateRow(&oper);
S
slguan 已提交
2046 2047
}

S
Shengliang Guan 已提交
2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078
static int32_t mnodeChangeNormalTableColumn(SMnodeMsg *pMsg, char *oldName, char *newName) {
  SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;
  int32_t col = mnodeFindNormalTableColumnIndex(pTable, oldName);
  if (col < 0) {
    mError("app:%p:%p, ctable:%s, change column, oldName: %s, newName: %s", pMsg->rpcMsg.ahandle, pMsg,
           pTable->info.tableId, oldName, newName);
    return TSDB_CODE_MND_FIELD_NOT_EXIST;
  }

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

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

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

  SSdbOper oper = {
    .type = SDB_OPER_GLOBAL,
    .table = tsChildTableSdb,
    .pObj = pTable,
    .pMsg = pMsg,
S
Shengliang Guan 已提交
2079
    .writeCb = mnodeAlterNormalTableColumnCb
S
Shengliang Guan 已提交
2080 2081
  };

S
Shengliang Guan 已提交
2082
  return sdbUpdateRow(&oper);
S
Shengliang Guan 已提交
2083 2084
}

2085
static int32_t mnodeSetSchemaFromNormalTable(SSchema *pSchema, SChildTableObj *pTable) {
S
slguan 已提交
2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097
  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);
}

2098
static int32_t mnodeDoGetChildTableMeta(SMnodeMsg *pMsg, STableMetaMsg *pMeta) {
S
slguan 已提交
2099 2100 2101
  SDbObj *pDb = pMsg->pDb;
  SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;

S
slguan 已提交
2102
  pMeta->uid       = htobe64(pTable->uid);
H
Haojun Liao 已提交
2103
  pMeta->tid       = htonl(pTable->tid);
S
slguan 已提交
2104 2105
  pMeta->precision = pDb->cfg.precision;
  pMeta->tableType = pTable->info.type;
H
Haojun Liao 已提交
2106
  tstrncpy(pMeta->tableId, pTable->info.tableId, TSDB_TABLE_FNAME_LEN);
2107
  if (pTable->superTable != NULL) {
2108 2109
    tstrncpy(pMeta->sTableId, pTable->superTable->info.tableId, TSDB_TABLE_FNAME_LEN);
  }
S
slguan 已提交
2110

2111
  if (pTable->info.type == TSDB_CHILD_TABLE && pTable->superTable != NULL) {
S
slguan 已提交
2112
    pMeta->sversion     = htons(pTable->superTable->sversion);
2113
    pMeta->tversion     = htons(pTable->superTable->tversion);
S
slguan 已提交
2114
    pMeta->numOfTags    = (int8_t)pTable->superTable->numOfTags;
S
slguan 已提交
2115
    pMeta->numOfColumns = htons((int16_t)pTable->superTable->numOfColumns);
2116
    pMeta->contLen      = sizeof(STableMetaMsg) + mnodeSetSchemaFromSuperTable(pMeta->schema, pTable->superTable);
S
slguan 已提交
2117 2118
  } else {
    pMeta->sversion     = htons(pTable->sversion);
2119
    pMeta->tversion     = 0;
S
slguan 已提交
2120 2121
    pMeta->numOfTags    = 0;
    pMeta->numOfColumns = htons((int16_t)pTable->numOfColumns);
2122
    pMeta->contLen      = sizeof(STableMetaMsg) + mnodeSetSchemaFromNormalTable(pMeta->schema, pTable); 
S
slguan 已提交
2123
  }
S
Shengliang Guan 已提交
2124

2125
  if (pMsg->pVgroup == NULL) pMsg->pVgroup = mnodeGetVgroup(pTable->vgId);
2126
  if (pMsg->pVgroup == NULL) {
S
Shengliang Guan 已提交
2127 2128
    mError("app:%p:%p, table:%s, failed to get table meta, vgroup not exist", pMsg->rpcMsg.ahandle, pMsg,
           pTable->info.tableId);
2129
    return TSDB_CODE_MND_VGROUP_NOT_EXIST;
S
slguan 已提交
2130 2131
  }

2132
  for (int32_t i = 0; i < pMsg->pVgroup->numOfVnodes; ++i) {
2133
    SDnodeObj *pDnode = mnodeGetDnode(pMsg->pVgroup->vnodeGid[i].dnodeId);
S
slguan 已提交
2134
    if (pDnode == NULL) break;
2135 2136 2137
    strcpy(pMeta->vgroup.epAddr[i].fqdn, pDnode->dnodeFqdn);
    pMeta->vgroup.epAddr[i].port = htons(pDnode->dnodePort + TSDB_PORT_DNODESHELL);
    pMeta->vgroup.numOfEps++;
2138
    mnodeDecDnodeRef(pDnode);
S
slguan 已提交
2139
  }
2140
  pMeta->vgroup.vgId = htonl(pMsg->pVgroup->vgId);
S
slguan 已提交
2141

S
TD-1520  
Shengliang Guan 已提交
2142
  mDebug("app:%p:%p, table:%s, uid:%" PRIu64 " table meta is retrieved, vgId:%d sid:%d", pMsg->rpcMsg.ahandle, pMsg,
H
Haojun Liao 已提交
2143
         pTable->info.tableId, pTable->uid, pTable->vgId, pTable->tid);
S
slguan 已提交
2144 2145 2146 2147

  return TSDB_CODE_SUCCESS;
}

S
Shengliang Guan 已提交
2148
static int32_t mnodeAutoCreateChildTable(SMnodeMsg *pMsg) {
S
TD-1732  
Shengliang Guan 已提交
2149
  STableInfoMsg *pInfo = pMsg->rpcMsg.pCont;
2150 2151 2152 2153 2154 2155 2156
  STagData *pTags = (STagData *)pInfo->tags;
  int32_t tagLen = htonl(pTags->dataLen);
  if (pTags->name[0] == 0) {
    mError("app:%p:%p, table:%s, failed to create table on demand for stable is empty, tagLen:%d", pMsg->rpcMsg.ahandle,
           pMsg, pInfo->tableId, tagLen);
    return TSDB_CODE_MND_INVALID_STABLE_NAME; 
  }
2157

2158
  int32_t contLen = sizeof(SCMCreateTableMsg) + offsetof(STagData, data) + tagLen;
S
slguan 已提交
2159 2160
  SCMCreateTableMsg *pCreateMsg = rpcMallocCont(contLen);
  if (pCreateMsg == NULL) {
S
Shengliang Guan 已提交
2161 2162
    mError("app:%p:%p, table:%s, failed to create table while get meta info, no enough memory", pMsg->rpcMsg.ahandle,
           pMsg, pInfo->tableId);
2163
    return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
2164
  }
S
slguan 已提交
2165

2166 2167
  size_t size = sizeof(pInfo->tableId);
  tstrncpy(pCreateMsg->tableId, pInfo->tableId, size);
B
Bomin Zhang 已提交
2168
  tstrncpy(pCreateMsg->db, pMsg->pDb->name, sizeof(pCreateMsg->db));
S
slguan 已提交
2169 2170
  pCreateMsg->igExists = 1;
  pCreateMsg->getMeta = 1;
S
slguan 已提交
2171
  pCreateMsg->contLen = htonl(contLen);
B
Bomin Zhang 已提交
2172

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

S
Shengliang Guan 已提交
2177 2178 2179 2180
  rpcFreeCont(pMsg->rpcMsg.pCont);
  pMsg->rpcMsg.msgType = TSDB_MSG_TYPE_CM_CREATE_TABLE;
  pMsg->rpcMsg.pCont = pCreateMsg;
  pMsg->rpcMsg.contLen = contLen;
2181
  
2182
  return TSDB_CODE_MND_ACTION_NEED_REPROCESSED;
S
slguan 已提交
2183
}
S
slguan 已提交
2184

2185
static int32_t mnodeGetChildTableMeta(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
2186 2187
  STableMetaMsg *pMeta =
      rpcMallocCont(sizeof(STableMetaMsg) + sizeof(SSchema) * (TSDB_MAX_TAGS + TSDB_MAX_COLUMNS + 16));
S
slguan 已提交
2188
  if (pMeta == NULL) {
S
Shengliang Guan 已提交
2189 2190
    mError("app:%p:%p, table:%s, failed to get table meta, no enough memory", pMsg->rpcMsg.ahandle, pMsg,
           pMsg->pTable->tableId);
2191
    return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
2192 2193
  }

2194
  mnodeDoGetChildTableMeta(pMsg, pMeta);
S
slguan 已提交
2195

2196 2197
  pMsg->rpcRsp.len = pMeta->contLen;
  pMsg->rpcRsp.rsp = pMeta;
S
slguan 已提交
2198
  pMeta->contLen = htons(pMeta->contLen);
2199 2200

  return TSDB_CODE_SUCCESS;
S
slguan 已提交
2201 2202
}

2203
void mnodeDropAllChildTablesInVgroups(SVgObj *pVgroup) {
2204 2205 2206 2207
  void *  pIter = NULL;
  int32_t numOfTables = 0;
  SChildTableObj *pTable = NULL;

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

  while (1) {
2211
    pIter = mnodeGetNextChildTable(pIter, &pTable);
2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222
    if (pTable == NULL) break;

    if (pTable->vgId == pVgroup->vgId) {
      SSdbOper oper = {
        .type = SDB_OPER_LOCAL,
        .table = tsChildTableSdb,
        .pObj = pTable,
      };
      sdbDeleteRow(&oper);
      numOfTables++;
    }
2223
    mnodeDecTableRef(pTable);
2224 2225 2226 2227
  }

  sdbFreeIter(pIter);

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

2231
void mnodeDropAllChildTables(SDbObj *pDropDb) {
S
Shengliang Guan 已提交
2232
  void *  pIter = NULL;
S
slguan 已提交
2233 2234 2235
  int32_t numOfTables = 0;
  SChildTableObj *pTable = NULL;

2236 2237 2238 2239 2240
  char prefix[64] = {0};
  tstrncpy(prefix, pDropDb->name, 64);
  strcat(prefix, TS_PATH_DELIMITER);
  int32_t prefixLen = strlen(prefix);

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

S
slguan 已提交
2243
  while (1) {
2244
    pIter = mnodeGetNextChildTable(pIter, &pTable);
S
slguan 已提交
2245
    if (pTable == NULL) break;
S
slguan 已提交
2246

2247
    if (strncmp(prefix, pTable->info.tableId, prefixLen) == 0) {
S
slguan 已提交
2248
      SSdbOper oper = {
S
slguan 已提交
2249
        .type = SDB_OPER_LOCAL,
S
slguan 已提交
2250 2251 2252 2253 2254 2255
        .table = tsChildTableSdb,
        .pObj = pTable,
      };
      sdbDeleteRow(&oper);
      numOfTables++;
    }
2256
    mnodeDecTableRef(pTable);
S
slguan 已提交
2257 2258
  }

S
Shengliang Guan 已提交
2259 2260
  sdbFreeIter(pIter);

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

2264
static void mnodeDropAllChildTablesInStable(SSuperTableObj *pStable) {
S
Shengliang Guan 已提交
2265
  void *  pIter = NULL;
S
slguan 已提交
2266 2267 2268
  int32_t numOfTables = 0;
  SChildTableObj *pTable = NULL;

S
TD-1719  
Shengliang Guan 已提交
2269 2270
  mInfo("stable:%s uid:%" PRIu64 ", all child tables:%d will be dropped from sdb", pStable->info.tableId, pStable->uid,
        pStable->numOfTables);
S
slguan 已提交
2271

S
slguan 已提交
2272
  while (1) {
2273
    pIter = mnodeGetNextChildTable(pIter, &pTable);
S
slguan 已提交
2274
    if (pTable == NULL) break;
S
slguan 已提交
2275 2276

    if (pTable->superTable == pStable) {
S
slguan 已提交
2277
      SSdbOper oper = {
S
slguan 已提交
2278
        .type = SDB_OPER_LOCAL,
S
slguan 已提交
2279 2280 2281 2282 2283 2284
        .table = tsChildTableSdb,
        .pObj = pTable,
      };
      sdbDeleteRow(&oper);
      numOfTables++;
    }
S
slguan 已提交
2285

2286
    mnodeDecTableRef(pTable);
S
slguan 已提交
2287 2288
  }

S
Shengliang Guan 已提交
2289 2290
  sdbFreeIter(pIter);

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

2294
#if 0
H
Haojun Liao 已提交
2295
static SChildTableObj* mnodeGetTableByPos(int32_t vnode, int32_t tid) {
2296
  SVgObj *pVgroup = mnodeGetVgroup(vnode);
2297
  if (pVgroup == NULL) return NULL;
S
slguan 已提交
2298

H
Haojun Liao 已提交
2299
  SChildTableObj *pTable = pVgroup->tableList[tid - 1];
2300
  mnodeIncTableRef((STableObj *)pTable);
2301

2302
  mnodeDecVgroupRef(pVgroup);
S
slguan 已提交
2303 2304
  return pTable;
}
2305
#endif
S
slguan 已提交
2306

2307
static int32_t mnodeProcessTableCfgMsg(SMnodeMsg *pMsg) {
2308 2309
  return TSDB_CODE_COM_OPS_NOT_SUPPORT;
#if 0  
S
TD-1732  
Shengliang Guan 已提交
2310
  SConfigTableMsg *pCfg = pMsg->rpcMsg.pCont;
S
Shengliang Guan 已提交
2311 2312 2313
  pCfg->dnodeId = htonl(pCfg->dnodeId);
  pCfg->vgId = htonl(pCfg->vgId);
  pCfg->sid = htonl(pCfg->sid);
2314
  mDebug("app:%p:%p, dnode:%d, vgId:%d sid:%d, receive table config msg", pMsg->rpcMsg.ahandle, pMsg, pCfg->dnodeId,
S
Shengliang Guan 已提交
2315
         pCfg->vgId, pCfg->sid);
S
slguan 已提交
2316

S
Shengliang Guan 已提交
2317
  SChildTableObj *pTable = mnodeGetTableByPos(pCfg->vgId, pCfg->sid);
S
slguan 已提交
2318
  if (pTable == NULL) {
S
Shengliang Guan 已提交
2319 2320
    mError("app:%p:%p, dnode:%d, vgId:%d sid:%d, table not found", pMsg->rpcMsg.ahandle, pMsg, pCfg->dnodeId,
           pCfg->vgId, pCfg->sid);
2321
    return TSDB_CODE_MND_INVALID_TABLE_ID;
S
slguan 已提交
2322 2323
  }

S
Shengliang Guan 已提交
2324 2325
  SMDCreateTableMsg *pCreate = NULL;
  pCreate = mnodeBuildCreateChildTableMsg(NULL, (SChildTableObj *)pTable);
2326
  mnodeDecTableRef(pTable);
S
Shengliang Guan 已提交
2327 2328 2329 2330 2331
    
  if (pCreate == NULL) return terrno;
  
  pMsg->rpcRsp.rsp = pCreate;
  pMsg->rpcRsp.len = htonl(pCreate->contLen);
2332
  return TSDB_CODE_SUCCESS;
2333
#endif  
S
slguan 已提交
2334 2335
}

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

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

S
Shengliang Guan 已提交
2343 2344
  SChildTableObj *pTable = (SChildTableObj *)mnodeMsg->pTable;
  assert(pTable);
S
Shengliang Guan 已提交
2345 2346

  mInfo("app:%p:%p, table:%s, drop table rsp received, vgId:%d sid:%d uid:%" PRIu64 ", thandle:%p result:%s",
H
Haojun Liao 已提交
2347
        mnodeMsg->rpcMsg.ahandle, mnodeMsg, pTable->info.tableId, pTable->vgId, pTable->tid, pTable->uid,
S
Shengliang Guan 已提交
2348
        mnodeMsg->rpcMsg.handle, tstrerror(rpcMsg->code));
S
slguan 已提交
2349 2350

  if (rpcMsg->code != TSDB_CODE_SUCCESS) {
S
Shengliang Guan 已提交
2351
    mError("app:%p:%p, table:%s, failed to drop in dnode, vgId:%d sid:%d uid:%" PRIu64 ", reason:%s",
H
Haojun Liao 已提交
2352
           mnodeMsg->rpcMsg.ahandle, mnodeMsg, pTable->info.tableId, pTable->vgId, pTable->tid, pTable->uid,
S
Shengliang Guan 已提交
2353
           tstrerror(rpcMsg->code));
2354
    dnodeSendRpcMnodeWriteRsp(mnodeMsg, rpcMsg->code);
S
slguan 已提交
2355 2356 2357
    return;
  }

2358 2359
  if (mnodeMsg->pVgroup == NULL) mnodeMsg->pVgroup = mnodeGetVgroup(pTable->vgId);
  if (mnodeMsg->pVgroup == NULL) {
S
Shengliang Guan 已提交
2360
    mError("app:%p:%p, table:%s, failed to get vgroup", mnodeMsg->rpcMsg.ahandle, mnodeMsg, pTable->info.tableId);
2361
    dnodeSendRpcMnodeWriteRsp(mnodeMsg, TSDB_CODE_MND_VGROUP_NOT_EXIST);
S
slguan 已提交
2362 2363
    return;
  }
S
Shengliang Guan 已提交
2364

2365
  if (mnodeMsg->pVgroup->numOfTables <= 0) {
2366
    mInfo("app:%p:%p, vgId:%d, all tables is dropped, drop vgroup", mnodeMsg->rpcMsg.ahandle, mnodeMsg,
S
Shengliang Guan 已提交
2367
           mnodeMsg->pVgroup->vgId);
2368
    mnodeDropVgroup(mnodeMsg->pVgroup, NULL);
S
slguan 已提交
2369 2370
  }

2371
  dnodeSendRpcMnodeWriteRsp(mnodeMsg, TSDB_CODE_SUCCESS);
S
slguan 已提交
2372 2373
}

2374 2375 2376 2377
/*
 * handle create table response from dnode
 *   if failed, drop the table cached
 */
2378
static void mnodeProcessCreateChildTableRsp(SRpcMsg *rpcMsg) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
2379
  if (rpcMsg->ahandle == NULL) return;
S
slguan 已提交
2380

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

S
Shengliang Guan 已提交
2384 2385
  SChildTableObj *pTable = (SChildTableObj *)mnodeMsg->pTable;
  assert(pTable);
S
Shengliang Guan 已提交
2386

2387 2388 2389
  // If the table is deleted by another thread during creation, stop creating and send drop msg to vnode
  if (sdbCheckRowDeleted(tsChildTableSdb, pTable)) {
    mDebug("app:%p:%p, table:%s, create table rsp received, but a deleting opertion incoming, vgId:%d sid:%d uid:%" PRIu64,
H
Haojun Liao 已提交
2390
           mnodeMsg->rpcMsg.ahandle, mnodeMsg, pTable->info.tableId, pTable->vgId, pTable->tid, pTable->uid);
S
TD-1038  
Shengliang Guan 已提交
2391 2392 2393

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

S
Shengliang Guan 已提交
2400
    mnodeSendDropChildTableMsg(mnodeMsg, false);
2401
    rpcMsg->code = TSDB_CODE_SUCCESS;
S
Shengliang Guan 已提交
2402 2403
    dnodeSendRpcMnodeWriteRsp(mnodeMsg, rpcMsg->code);
    return;
2404 2405
  }

2406
  if (rpcMsg->code == TSDB_CODE_SUCCESS || rpcMsg->code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
S
Shengliang Guan 已提交
2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419
     SSdbOper desc = {
      .type  = SDB_OPER_GLOBAL,
      .pObj  = pTable,
      .table = tsChildTableSdb,
      .pMsg  = mnodeMsg,
      .writeCb = mnodeDoCreateChildTableCb
    };
    
    int32_t code = sdbInsertRowImp(&desc);
    if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
      mnodeMsg->pTable = NULL;
      mnodeDestroyChildTable(pTable);
      dnodeSendRpcMnodeWriteRsp(mnodeMsg, code);
2420 2421
    }
  } else {
S
Shengliang Guan 已提交
2422
    if (mnodeMsg->retry++ < 10) {
S
TD-1725  
Shengliang Guan 已提交
2423 2424
      mDebug("app:%p:%p, table:%s, create table rsp received, need retry, times:%d vgId:%d sid:%d uid:%" PRIu64
             " result:%s thandle:%p",
H
Haojun Liao 已提交
2425
             mnodeMsg->rpcMsg.ahandle, mnodeMsg, pTable->info.tableId, mnodeMsg->retry, pTable->vgId, pTable->tid,
S
TD-1725  
Shengliang Guan 已提交
2426
             pTable->uid, tstrerror(rpcMsg->code), mnodeMsg->rpcMsg.handle);
2427

2428
      dnodeDelayReprocessMnodeWriteMsg(mnodeMsg);
2429
    } else {
S
TD-1725  
Shengliang Guan 已提交
2430
      mError("app:%p:%p, table:%s, failed to create in dnode, vgId:%d sid:%d uid:%" PRIu64 ", result:%s thandle:%p",
H
Haojun Liao 已提交
2431
             mnodeMsg->rpcMsg.ahandle, mnodeMsg, pTable->info.tableId, pTable->vgId, pTable->tid, pTable->uid,
S
TD-1725  
Shengliang Guan 已提交
2432
             tstrerror(rpcMsg->code), mnodeMsg->rpcMsg.handle);
2433

2434
      SSdbOper oper = {.type = SDB_OPER_GLOBAL, .table = tsChildTableSdb, .pObj = pTable};
2435
      sdbDeleteRow(&oper);
2436

2437
      dnodeSendRpcMnodeWriteRsp(mnodeMsg, rpcMsg->code);
S
slguan 已提交
2438 2439 2440 2441
    }
  }
}

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

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
2445
  SMnodeMsg *mnodeMsg = rpcMsg->ahandle;
2446 2447 2448 2449 2450 2451
  mnodeMsg->received++;

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

  if (rpcMsg->code == TSDB_CODE_SUCCESS || rpcMsg->code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
2452
    mDebug("app:%p:%p, ctable:%s, altered in dnode, thandle:%p result:%s", mnodeMsg->rpcMsg.ahandle, mnodeMsg,
2453 2454 2455 2456 2457
           pTable->info.tableId, mnodeMsg->rpcMsg.handle, tstrerror(rpcMsg->code));

    dnodeSendRpcMnodeWriteRsp(mnodeMsg, TSDB_CODE_SUCCESS);
  } else {
    if (mnodeMsg->retry++ < 3) {
2458
      mDebug("app:%p:%p, table:%s, alter table rsp received, need retry, times:%d result:%s thandle:%p",
2459 2460 2461 2462 2463 2464 2465 2466 2467 2468
             mnodeMsg->rpcMsg.ahandle, mnodeMsg, pTable->info.tableId, mnodeMsg->retry, tstrerror(rpcMsg->code),
             mnodeMsg->rpcMsg.handle);

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

2471
static int32_t mnodeProcessMultiTableMetaMsg(SMnodeMsg *pMsg) {
S
TD-1732  
Shengliang Guan 已提交
2472
  SMultiTableInfoMsg *pInfo = pMsg->rpcMsg.pCont;
S
slguan 已提交
2473 2474
  pInfo->numOfTables = htonl(pInfo->numOfTables);

2475
  int32_t totalMallocLen = 4 * 1024 * 1024;  // first malloc 4 MB, subsequent reallocation as twice
S
slguan 已提交
2476 2477
  SMultiTableMeta *pMultiMeta = rpcMallocCont(totalMallocLen);
  if (pMultiMeta == NULL) {
2478
    return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
2479 2480 2481 2482 2483
  }

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

2484
  for (int32_t t = 0; t < pInfo->numOfTables; ++t) {
H
Haojun Liao 已提交
2485
    char * tableId = (char *)(pInfo->tableIds + t * TSDB_TABLE_FNAME_LEN);
2486
    SChildTableObj *pTable = mnodeGetChildTable(tableId);
S
slguan 已提交
2487 2488
    if (pTable == NULL) continue;

2489
    if (pMsg->pDb == NULL) pMsg->pDb = mnodeGetDbByTableId(tableId);
H
Hui Li 已提交
2490
    if (pMsg->pDb == NULL || pMsg->pDb->status != TSDB_DB_STATUS_READY) {
2491
      mnodeDecTableRef(pTable);
2492 2493
      continue;
    }
S
slguan 已提交
2494 2495

    int availLen = totalMallocLen - pMultiMeta->contLen;
2496
    if (availLen <= sizeof(STableMetaMsg) + sizeof(SSchema) * (TSDB_MAX_TAGS + TSDB_MAX_COLUMNS + 16)) {
2497 2498 2499
      totalMallocLen *= 2;
      pMultiMeta = rpcReallocCont(pMultiMeta, totalMallocLen);
      if (pMultiMeta == NULL) {
2500
        mnodeDecTableRef(pTable);
2501
        return TSDB_CODE_MND_OUT_OF_MEMORY;
2502 2503
      } else {
        t--;
2504
        mnodeDecTableRef(pTable);
2505 2506
        continue;
      }
S
slguan 已提交
2507 2508 2509
    }

    STableMetaMsg *pMeta = (STableMetaMsg *)(pMultiMeta->metas + pMultiMeta->contLen);
2510
    int32_t code = mnodeDoGetChildTableMeta(pMsg, pMeta);
S
slguan 已提交
2511 2512 2513 2514
    if (code == TSDB_CODE_SUCCESS) {
      pMultiMeta->numOfTables ++;
      pMultiMeta->contLen += pMeta->contLen;
    }
2515

2516
    mnodeDecTableRef(pTable);
S
slguan 已提交
2517 2518
  }

2519 2520 2521 2522
  pMsg->rpcRsp.rsp = pMultiMeta;
  pMsg->rpcRsp.len = pMultiMeta->contLen;

  return TSDB_CODE_SUCCESS;
S
slguan 已提交
2523 2524
}

2525 2526
static int32_t mnodeGetShowTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
  SDbObj *pDb = mnodeGetDb(pShow->db);
2527
  if (pDb == NULL) return TSDB_CODE_MND_DB_NOT_SELECTED;
H
Hui Li 已提交
2528 2529 2530
  
  if (pDb->status != TSDB_DB_STATUS_READY) {
    mError("db:%s, status:%d, in dropping", pDb->name, pDb->status);
S
Shengliang Guan 已提交
2531
    mnodeDecDbRef(pDb);
H
Hui Li 已提交
2532 2533
    return TSDB_CODE_MND_DB_IN_DROPPING;
  }
S
slguan 已提交
2534 2535 2536 2537

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

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

  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
2547
  strcpy(pSchema[cols].name, "created_time");
S
slguan 已提交
2548 2549 2550 2551 2552 2553 2554 2555 2556
  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 已提交
2557
  SSchema tbCol = tGetTableNameColumnSchema();
H
Hui Li 已提交
2558
  pShow->bytes[cols] = tbCol.bytes + VARSTR_HEADER_SIZE;
H
Haojun Liao 已提交
2559
  pSchema[cols].type = tbCol.type;
2560
  strcpy(pSchema[cols].name, "stable_name");
S
slguan 已提交
2561 2562 2563
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

H
Haojun Liao 已提交
2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582
  pShow->bytes[cols] = 8; // table uid
  pSchema[cols].type = TSDB_DATA_TYPE_BIGINT;
  strcpy(pSchema[cols].name, "uid");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

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

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


S
slguan 已提交
2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593
  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];

2594
  mnodeDecDbRef(pDb);
S
slguan 已提交
2595 2596 2597
  return 0;
}

2598 2599
static int32_t mnodeRetrieveShowTables(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
  SDbObj *pDb = mnodeGetDb(pShow->db);
S
slguan 已提交
2600
  if (pDb == NULL) return 0;
H
Hui Li 已提交
2601 2602 2603
  
  if (pDb->status != TSDB_DB_STATUS_READY) {
    mError("db:%s, status:%d, in dropping", pDb->name, pDb->status);
S
Shengliang Guan 已提交
2604
    mnodeDecDbRef(pDb);
H
Hui Li 已提交
2605 2606
    return 0;
  }
S
slguan 已提交
2607

H
Haojun Liao 已提交
2608
  int32_t cols       = 0;
S
slguan 已提交
2609 2610 2611 2612 2613
  int32_t numOfRows  = 0;
  SChildTableObj *pTable = NULL;
  SPatternCompareInfo info = PATTERN_COMPARE_INFO_INITIALIZER;

  char prefix[64] = {0};
S
Shengliang Guan 已提交
2614
  tstrncpy(prefix, pDb->name, 64);
S
slguan 已提交
2615 2616 2617
  strcat(prefix, TS_PATH_DELIMITER);
  int32_t prefixLen = strlen(prefix);

B
Bomin Zhang 已提交
2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628
  char* pattern = NULL;
  if (pShow->payloadLen > 0) {
    pattern = (char*)malloc(pShow->payloadLen + 1);
    if (pattern == NULL) {
      terrno = TSDB_CODE_QRY_OUT_OF_MEMORY;
      return 0;
    }
    memcpy(pattern, pShow->payload, pShow->payloadLen);
    pattern[pShow->payloadLen] = 0;
  }

S
slguan 已提交
2629
  while (numOfRows < rows) {
2630
    pShow->pIter = mnodeGetNextChildTable(pShow->pIter, &pTable);
S
slguan 已提交
2631 2632 2633 2634
    if (pTable == NULL) break;

    // not belong to current db
    if (strncmp(pTable->info.tableId, prefix, prefixLen)) {
2635
      mnodeDecTableRef(pTable);
S
slguan 已提交
2636 2637 2638
      continue;
    }

B
Bomin Zhang 已提交
2639
    char tableName[TSDB_TABLE_NAME_LEN] = {0};
S
slguan 已提交
2640
    
2641
    // pattern compare for table name
S
Shengliang Guan 已提交
2642
    mnodeExtractTableName(pTable->info.tableId, tableName);
S
slguan 已提交
2643

B
Bomin Zhang 已提交
2644
    if (pattern != NULL && patternMatch(pattern, tableName, sizeof(tableName) - 1, &info) != TSDB_PATTERN_MATCH) {
2645
      mnodeDecTableRef(pTable);
S
slguan 已提交
2646 2647 2648
      continue;
    }

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

H
Hui Li 已提交
2652
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, tableName, pShow->bytes[cols]);
S
slguan 已提交
2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668
    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;
2669
    
B
Bomin Zhang 已提交
2670
    memset(tableName, 0, sizeof(tableName));
S
slguan 已提交
2671
    if (pTable->info.type == TSDB_CHILD_TABLE) {
S
Shengliang Guan 已提交
2672
      mnodeExtractTableName(pTable->superTable->info.tableId, tableName);
H
Hui Li 已提交
2673
      STR_WITH_MAXSIZE_TO_VARSTR(pWrite, tableName, pShow->bytes[cols]);
S
slguan 已提交
2674
    }
2675
    
S
slguan 已提交
2676 2677
    cols++;

H
Haojun Liao 已提交
2678 2679 2680 2681 2682 2683 2684 2685
    // uid
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int64_t*) pWrite = pTable->uid;
    cols++;


    // tid
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
H
Haojun Liao 已提交
2686
    *(int32_t*) pWrite = pTable->tid;
H
Haojun Liao 已提交
2687 2688 2689 2690 2691 2692 2693
    cols++;

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

S
slguan 已提交
2694
    numOfRows++;
2695
    mnodeDecTableRef(pTable);
S
slguan 已提交
2696 2697 2698 2699
  }

  pShow->numOfReads += numOfRows;

2700
  mnodeVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
2701
  mnodeDecDbRef(pDb);
B
Bomin Zhang 已提交
2702
  free(pattern);
S
slguan 已提交
2703 2704 2705 2706

  return numOfRows;
}

2707
static int32_t mnodeProcessAlterTableMsg(SMnodeMsg *pMsg) {
S
TD-1732  
Shengliang Guan 已提交
2708
  SAlterTableMsg *pAlter = pMsg->rpcMsg.pCont;
2709
  mDebug("app:%p:%p, table:%s, alter table msg is received from thandle:%p", pMsg->rpcMsg.ahandle, pMsg,
S
Shengliang Guan 已提交
2710
         pAlter->tableId, pMsg->rpcMsg.handle);
S
[TD-17]  
slguan 已提交
2711

2712
  if (pMsg->pDb == NULL) pMsg->pDb = mnodeGetDbByTableId(pAlter->tableId);
H
Hui Li 已提交
2713
  if (pMsg->pDb == NULL) {
S
Shengliang Guan 已提交
2714
    mError("app:%p:%p, table:%s, failed to alter table, db not selected", pMsg->rpcMsg.ahandle, pMsg, pAlter->tableId);
2715
    return TSDB_CODE_MND_DB_NOT_SELECTED;
S
[TD-17]  
slguan 已提交
2716
  }
H
Hui Li 已提交
2717 2718 2719 2720 2721
  
  if (pMsg->pDb->status != TSDB_DB_STATUS_READY) {
    mError("db:%s, status:%d, in dropping", pMsg->pDb->name, pMsg->pDb->status);
    return TSDB_CODE_MND_DB_IN_DROPPING;
  }
S
[TD-17]  
slguan 已提交
2722

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

2728
  if (pMsg->pTable == NULL) pMsg->pTable = mnodeGetTable(pAlter->tableId);
S
[TD-17]  
slguan 已提交
2729
  if (pMsg->pTable == NULL) {
S
Shengliang Guan 已提交
2730
    mError("app:%p:%p, table:%s, failed to alter table, table not exist", pMsg->rpcMsg.ahandle, pMsg, pAlter->tableId);
2731
    return TSDB_CODE_MND_INVALID_TABLE_NAME;
S
[TD-17]  
slguan 已提交
2732 2733
  }

S
slguan 已提交
2734
  pAlter->type = htons(pAlter->type);
2735 2736
  pAlter->numOfCols = htons(pAlter->numOfCols);
  pAlter->tagValLen = htonl(pAlter->tagValLen);
S
slguan 已提交
2737

S
[TD-17]  
slguan 已提交
2738
  if (pAlter->numOfCols > 2) {
S
Shengliang Guan 已提交
2739 2740
    mError("app:%p:%p, table:%s, error numOfCols:%d in alter table", pMsg->rpcMsg.ahandle, pMsg, pAlter->tableId,
           pAlter->numOfCols);
2741
    return TSDB_CODE_MND_APP_ERROR;
S
[TD-17]  
slguan 已提交
2742 2743 2744 2745 2746 2747
  }

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

2748
  int32_t code = TSDB_CODE_COM_OPS_NOT_SUPPORT;
S
[TD-17]  
slguan 已提交
2749
  if (pMsg->pTable->type == TSDB_SUPER_TABLE) {
2750
    mDebug("app:%p:%p, table:%s, start to alter stable", pMsg->rpcMsg.ahandle, pMsg, pAlter->tableId);
S
[TD-17]  
slguan 已提交
2751
    if (pAlter->type == TSDB_ALTER_TABLE_ADD_TAG_COLUMN) {
2752
      code = mnodeAddSuperTableTag(pMsg, pAlter->schema, 1);
S
[TD-17]  
slguan 已提交
2753
    } else if (pAlter->type == TSDB_ALTER_TABLE_DROP_TAG_COLUMN) {
2754
      code = mnodeDropSuperTableTag(pMsg, pAlter->schema[0].name);
S
[TD-17]  
slguan 已提交
2755
    } else if (pAlter->type == TSDB_ALTER_TABLE_CHANGE_TAG_COLUMN) {
2756
      code = mnodeModifySuperTableTagName(pMsg, pAlter->schema[0].name, pAlter->schema[1].name);
S
[TD-17]  
slguan 已提交
2757
    } else if (pAlter->type == TSDB_ALTER_TABLE_ADD_COLUMN) {
2758
      code = mnodeAddSuperTableColumn(pMsg, pAlter->schema, 1);
S
[TD-17]  
slguan 已提交
2759
    } else if (pAlter->type == TSDB_ALTER_TABLE_DROP_COLUMN) {
2760
      code = mnodeDropSuperTableColumn(pMsg, pAlter->schema[0].name);
S
Shengliang Guan 已提交
2761 2762
    } else if (pAlter->type == TSDB_ALTER_TABLE_CHANGE_COLUMN) {
      code = mnodeChangeSuperTableColumn(pMsg, pAlter->schema[0].name, pAlter->schema[1].name);
S
[TD-17]  
slguan 已提交
2763 2764
    } else {
    }
S
slguan 已提交
2765
  } else {
2766
    mDebug("app:%p:%p, table:%s, start to alter ctable", pMsg->rpcMsg.ahandle, pMsg, pAlter->tableId);
S
[TD-17]  
slguan 已提交
2767
    if (pAlter->type == TSDB_ALTER_TABLE_UPDATE_TAG_VAL) {
2768
      return TSDB_CODE_COM_OPS_NOT_SUPPORT;
S
[TD-17]  
slguan 已提交
2769
    } else if (pAlter->type == TSDB_ALTER_TABLE_ADD_COLUMN) {
2770
      code = mnodeAddNormalTableColumn(pMsg, pAlter->schema, 1);
S
[TD-17]  
slguan 已提交
2771
    } else if (pAlter->type == TSDB_ALTER_TABLE_DROP_COLUMN) {
2772
      code = mnodeDropNormalTableColumn(pMsg, pAlter->schema[0].name);
S
Shengliang Guan 已提交
2773 2774
    } else if (pAlter->type == TSDB_ALTER_TABLE_CHANGE_COLUMN) {
      code = mnodeChangeNormalTableColumn(pMsg, pAlter->schema[0].name, pAlter->schema[1].name);
S
[TD-17]  
slguan 已提交
2775 2776
    } else {
    }
S
slguan 已提交
2777 2778
  }

2779
 return code;
S
[TD-17]  
slguan 已提交
2780
}
2781

S
Shengliang Guan 已提交
2782
static int32_t mnodeGetStreamTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
2783
  SDbObj *pDb = mnodeGetDb(pShow->db);
2784
  if (pDb == NULL) return TSDB_CODE_MND_DB_NOT_SELECTED;
H
Hui Li 已提交
2785 2786 2787
  
  if (pDb->status != TSDB_DB_STATUS_READY) {
    mError("db:%s, status:%d, in dropping", pDb->name, pDb->status);
S
Shengliang Guan 已提交
2788
    mnodeDecDbRef(pDb);
H
Hui Li 已提交
2789 2790
    return TSDB_CODE_MND_DB_IN_DROPPING;
  }
2791 2792 2793 2794

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

H
Haojun Liao 已提交
2795 2796 2797
  SSchema tbnameColSchema = tGetTableNameColumnSchema();
  pShow->bytes[cols] = tbnameColSchema.bytes;
  pSchema[cols].type = tbnameColSchema.type;
2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834
  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 已提交
2835
static int32_t mnodeRetrieveStreamTables(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
2836 2837
  SDbObj *pDb = mnodeGetDb(pShow->db);
  if (pDb == NULL) return 0;
H
Hui Li 已提交
2838 2839 2840
  
  if (pDb->status != TSDB_DB_STATUS_READY) {
    mError("db:%s, status:%d, in dropping", pDb->name, pDb->status);
S
Shengliang Guan 已提交
2841
    mnodeDecDbRef(pDb);
H
Hui Li 已提交
2842 2843
    return 0;
  }
2844 2845 2846 2847 2848 2849
  
  int32_t numOfRows  = 0;
  SChildTableObj *pTable = NULL;
  SPatternCompareInfo info = PATTERN_COMPARE_INFO_INITIALIZER;

  char prefix[64] = {0};
S
Shengliang Guan 已提交
2850
  tstrncpy(prefix, pDb->name, 64);
2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863
  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 已提交
2864
    char tableName[TSDB_TABLE_NAME_LEN] = {0};
2865 2866 2867 2868
    
    // pattern compare for table name
    mnodeExtractTableName(pTable->info.tableId, tableName);

B
Bomin Zhang 已提交
2869
    if (pShow->payloadLen > 0 && patternMatch(pShow->payload, tableName, sizeof(tableName) - 1, &info) != TSDB_PATTERN_MATCH) {
2870 2871 2872 2873 2874 2875 2876 2877
      mnodeDecTableRef(pTable);
      continue;
    }

    int32_t cols = 0;

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

H
Hui Li 已提交
2878
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, tableName, pShow->bytes[cols]);
2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889
    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 已提交
2890
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, pTable->sql, pShow->bytes[cols]);    
2891 2892 2893 2894 2895 2896 2897 2898
    cols++;

    numOfRows++;
    mnodeDecTableRef(pTable);
  }

  pShow->numOfReads += numOfRows;

2899
  mnodeVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
2900 2901 2902
  mnodeDecDbRef(pDb);

  return numOfRows;
2903
}