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

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

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

static int32_t mnodeGetShowTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn);
static int32_t mnodeRetrieveShowTables(SShowObj *pShow, char *data, int32_t rows, void *pConn);
static int32_t mnodeGetShowSuperTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn);
63
static int32_t mnodeRetrieveShowSuperTables(SShowObj *pShow, char *data, int32_t rows, void *pConn);
S
Shengliang Guan 已提交
64 65
static int32_t mnodeGetStreamTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn);
static int32_t mnodeRetrieveStreamTables(SShowObj *pShow, char *data, int32_t rows, void *pConn);
66
 
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
static int32_t mnodeProcessCreateTableMsg(SMnodeMsg *mnodeMsg);
static int32_t mnodeProcessCreateSuperTableMsg(SMnodeMsg *pMsg);
static int32_t mnodeProcessCreateChildTableMsg(SMnodeMsg *pMsg);
static void    mnodeProcessCreateChildTableRsp(SRpcMsg *rpcMsg);

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

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

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

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

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

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

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

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

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

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

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

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

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

  return TSDB_CODE_SUCCESS;
}

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

151 152 153 154 155 156 157
  SVgObj *pVgroup = NULL;
  SDbObj *pDb = NULL;
  SAcctObj *pAcct = NULL;
  
  pVgroup = mnodeGetVgroup(pTable->vgId);
  if (pVgroup != NULL) pDb = mnodeGetDb(pVgroup->dbName);
  if (pDb != NULL) pAcct = mnodeGetAcct(pDb->acct);
S
slguan 已提交
158 159

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

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

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

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

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

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

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

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

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

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

233 234
  pOper->rowSize = len;

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

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

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

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

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

    if (pTable->sqlLen != 0) {
      pTable->sql = malloc(pTable->sqlLen);
      if (pTable->sql == NULL) {
S
Shengliang Guan 已提交
267
        mnodeDestroyChildTable(pTable);
268
        return TSDB_CODE_MND_OUT_OF_MEMORY;
269 270
      }
      memcpy(pTable->sql, pOper->rowData + len, pTable->sqlLen);
S
slguan 已提交
271 272 273 274 275 276 277
    }
  }

  pOper->pObj = pTable;
  return TSDB_CODE_SUCCESS;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  return TSDB_CODE_SUCCESS;
}

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

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

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

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

    pTable->vgHash = oldVgHash;
    pTable->refCount = oldRefCount;
S
slguan 已提交
456
    pTable->schema = pNew->schema;
457
    free(pNew);
458
    free(oldTableId);
S
slguan 已提交
459 460
    free(oldSchema);
  }
S
Shengliang Guan 已提交
461

S
Shengliang Guan 已提交
462
  mnodeDecTableRef(pTable);
S
slguan 已提交
463 464 465
  return TSDB_CODE_SUCCESS;
}

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

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

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

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

  return TSDB_CODE_SUCCESS;
}

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

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

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

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

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

  return TSDB_CODE_SUCCESS;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

616 617 618
  return NULL;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

782 783 784
static int32_t mnodeCreateSuperTableCb(SMnodeMsg *pMsg, int32_t code) {
  SSuperTableObj *pTable = (SSuperTableObj *)pMsg->pTable;
  if (pTable != NULL) {
785
    mLInfo("app:%p:%p, stable:%s, is created in sdb, result:%s", pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId,
786 787 788 789 790 791
            tstrerror(code));
  }

  return code;
}

792
static int32_t mnodeProcessCreateSuperTableMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
793 794
  if (pMsg == NULL) return TSDB_CODE_MND_APP_ERROR;

S
Shengliang Guan 已提交
795
  SCMCreateTableMsg *pCreate = pMsg->rpcMsg.pCont;
S
Shengliang Guan 已提交
796
  SSuperTableObj *   pStable = calloc(1, sizeof(SSuperTableObj));
S
slguan 已提交
797
  if (pStable == NULL) {
S
Shengliang Guan 已提交
798
    mError("app:%p:%p, table:%s, failed to create, no enough memory", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId);
799
    return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
800 801
  }

802
  pStable->info.tableId = strdup(pCreate->tableId);
S
slguan 已提交
803 804 805 806
  pStable->info.type    = TSDB_SUPER_TABLE;
  pStable->createdTime  = taosGetTimestampMs();
  pStable->uid          = (((uint64_t) pStable->createdTime) << 16) + (sdbGetVersion() & ((1ul << 16) - 1ul));
  pStable->sversion     = 0;
S
TD-355  
Shengliang Guan 已提交
807
  pStable->tversion     = 0;
S
slguan 已提交
808 809 810
  pStable->numOfColumns = htons(pCreate->numOfColumns);
  pStable->numOfTags    = htons(pCreate->numOfTags);

S
slguan 已提交
811
  int32_t numOfCols = pStable->numOfColumns + pStable->numOfTags;
S
slguan 已提交
812 813 814 815
  int32_t schemaSize = numOfCols * sizeof(SSchema);
  pStable->schema = (SSchema *)calloc(1, schemaSize);
  if (pStable->schema == NULL) {
    free(pStable);
S
Shengliang Guan 已提交
816
    mError("app:%p:%p, table:%s, failed to create, no schema input", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId);
817
    return TSDB_CODE_MND_INVALID_TABLE_NAME;
S
slguan 已提交
818 819 820 821 822 823 824 825
  }
  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 已提交
826 827 828
    
    // 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 已提交
829 830
  }

831 832 833
  pMsg->pTable = (STableObj *)pStable;
  mnodeIncTableRef(pMsg->pTable);

S
slguan 已提交
834
  SSdbOper oper = {
S
slguan 已提交
835
    .type = SDB_OPER_GLOBAL,
S
slguan 已提交
836 837
    .table = tsSuperTableSdb,
    .pObj = pStable,
838
    .rowSize = sizeof(SSuperTableObj) + schemaSize,
839 840
    .pMsg = pMsg,
    .cb = mnodeCreateSuperTableCb
S
slguan 已提交
841 842 843 844
  };

  int32_t code = sdbInsertRow(&oper);
  if (code != TSDB_CODE_SUCCESS) {
S
Shengliang Guan 已提交
845
    mnodeDestroySuperTable(pStable);
846
    pMsg->pTable = NULL;
S
Shengliang Guan 已提交
847
    mError("app:%p:%p, table:%s, failed to create, sdb error", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId);
848
    return code;
S
slguan 已提交
849
  } else {
850
    return TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
slguan 已提交
851 852 853
  }
}

854 855
static int32_t mnodeDropSuperTableCb(SMnodeMsg *pMsg, int32_t code) {
  SSuperTableObj *pTable = (SSuperTableObj *)pMsg->pTable;
856 857 858
  if (code != TSDB_CODE_SUCCESS) {
    mError("app:%p:%p, table:%s, failed to drop, sdb error", pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId);
  } else {
859
    mLInfo("app:%p:%p, stable:%s, is dropped from sdb", pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId);
860 861 862 863
  }

  return code;
}
864

865
static int32_t mnodeProcessDropSuperTableMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
866 867
  if (pMsg == NULL) return TSDB_CODE_MND_APP_ERROR;

S
slguan 已提交
868
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
S
slguan 已提交
869
  if (pStable->numOfTables != 0) {
870 871 872
    SHashMutableIterator *pIter = taosHashCreateIter(pStable->vgHash);
    while (taosHashIterNext(pIter)) {
      int32_t *pVgId = taosHashIterGet(pIter);
873
      SVgObj *pVgroup = mnodeGetVgroup(*pVgId);
guanshengliang's avatar
guanshengliang 已提交
874
      if (pVgroup == NULL) break;
875

S
slguan 已提交
876
      SMDDropSTableMsg *pDrop = rpcMallocCont(sizeof(SMDDropSTableMsg));
guanshengliang's avatar
guanshengliang 已提交
877
      pDrop->contLen = htonl(sizeof(SMDDropSTableMsg));
878
      pDrop->vgId = htonl(pVgroup->vgId);
S
slguan 已提交
879
      pDrop->uid = htobe64(pStable->uid);
S
Shengliang Guan 已提交
880
      mnodeExtractTableName(pStable->info.tableId, pDrop->tableId);
S
Shengliang Guan 已提交
881

882
      mInfo("app:%p:%p, stable:%s, send drop stable msg to vgId:%d", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
S
Shengliang Guan 已提交
883
             pVgroup->vgId);
884
      SRpcIpSet ipSet = mnodeGetIpSetFromVgroup(pVgroup);
S
Shengliang Guan 已提交
885
      SRpcMsg   rpcMsg = {.pCont = pDrop, .contLen = sizeof(SMDDropSTableMsg), .msgType = TSDB_MSG_TYPE_MD_DROP_STABLE};
guanshengliang's avatar
guanshengliang 已提交
886
      dnodeSendMsgToDnode(&ipSet, &rpcMsg);
887
      mnodeDecVgroupRef(pVgroup);
S
slguan 已提交
888
    }
889
    taosHashDestroyIter(pIter);
890

891
    mnodeDropAllChildTablesInStable(pStable);
guanshengliang's avatar
guanshengliang 已提交
892 893 894
  } 
  
  SSdbOper oper = {
895
    .type  = SDB_OPER_GLOBAL,
guanshengliang's avatar
guanshengliang 已提交
896
    .table = tsSuperTableSdb,
897 898 899
    .pObj  = pStable,
    .pMsg  = pMsg,
    .cb    = mnodeDropSuperTableCb
guanshengliang's avatar
guanshengliang 已提交
900
  };
901

guanshengliang's avatar
guanshengliang 已提交
902
  int32_t code = sdbDeleteRow(&oper);
903
  if (code == TSDB_CODE_SUCCESS) {
904
    return TSDB_CODE_MND_ACTION_IN_PROGRESS;
905
  }
906
  return code;
S
slguan 已提交
907 908
}

909
static int32_t mnodeFindSuperTableTagIndex(SSuperTableObj *pStable, const char *tagName) {
guanshengliang's avatar
guanshengliang 已提交
910 911 912 913
  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 已提交
914 915 916 917 918 919
    }
  }

  return -1;
}

920 921
static int32_t mnodeAddSuperTableTagCb(SMnodeMsg *pMsg, int32_t code) {
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
922
  mLInfo("app:%p:%p, stable %s, add tag result:%s", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
923 924 925 926 927 928 929
          tstrerror(code));

  return code;
}

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

S
slguan 已提交
935
  for (int32_t i = 0; i < ntags; i++) {
936
    if (mnodeFindSuperTableColumnIndex(pStable, schema[i].name) > 0) {
S
Shengliang Guan 已提交
937 938
      mError("app:%p:%p, stable:%s, add tag, column:%s already exist", pMsg->rpcMsg.ahandle, pMsg,
             pStable->info.tableId, schema[i].name);
939
      return TSDB_CODE_MND_TAG_ALREAY_EXIST;
S
slguan 已提交
940 941
    }

942
    if (mnodeFindSuperTableTagIndex(pStable, schema[i].name) > 0) {
S
Shengliang Guan 已提交
943 944
      mError("app:%p:%p, stable:%s, add tag, tag:%s already exist", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
             schema[i].name);
945
      return TSDB_CODE_MND_FIELD_ALREAY_EXIST;
S
slguan 已提交
946 947 948 949 950 951
    }
  }

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

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

S
slguan 已提交
954
  SSchema *tschema = (SSchema *)(pStable->schema + pStable->numOfColumns + pStable->numOfTags);
S
slguan 已提交
955 956 957 958
  for (int32_t i = 0; i < ntags; i++) {
    tschema[i].colId = pStable->nextColId++;
  }

S
slguan 已提交
959
  pStable->numOfTags += ntags;
S
TD-355  
Shengliang Guan 已提交
960
  pStable->tversion++;
S
slguan 已提交
961

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

S
slguan 已提交
965
  SSdbOper oper = {
S
slguan 已提交
966
    .type = SDB_OPER_GLOBAL,
S
[TD-17]  
slguan 已提交
967
    .table = tsSuperTableSdb,
968
    .pObj = pStable,
969 970
    .pMsg = pMsg,
    .cb = mnodeAddSuperTableTagCb
S
[TD-17]  
slguan 已提交
971
  };
S
slguan 已提交
972

S
[TD-17]  
slguan 已提交
973
  int32_t code = sdbUpdateRow(&oper);
974 975
  if (code == TSDB_CODE_SUCCESS) {
    code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
[TD-17]  
slguan 已提交
976
  }
S
Shengliang Guan 已提交
977

978
  return code;
S
slguan 已提交
979 980
}

981 982
static int32_t mnodeDropSuperTableTagCb(SMnodeMsg *pMsg, int32_t code) {
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
983
  mLInfo("app:%p:%p, stable %s, drop tag result:%s", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
984 985 986 987 988 989
          tstrerror(code));
  return code;
}

static int32_t mnodeDropSuperTableTag(SMnodeMsg *pMsg, char *tagName) {
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
990
  int32_t col = mnodeFindSuperTableTagIndex(pStable, tagName);
S
slguan 已提交
991
  if (col < 0) {
S
Shengliang Guan 已提交
992 993
    mError("app:%p:%p, stable:%s, drop tag, tag:%s not exist", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
           tagName);
994
    return TSDB_CODE_MND_TAG_NOT_EXIST;
S
slguan 已提交
995
  }
S
slguan 已提交
996

S
slguan 已提交
997 998
  memmove(pStable->schema + pStable->numOfColumns + col, pStable->schema + pStable->numOfColumns + col + 1,
          sizeof(SSchema) * (pStable->numOfTags - col - 1));
S
slguan 已提交
999
  pStable->numOfTags--;
S
TD-355  
Shengliang Guan 已提交
1000
  pStable->tversion++;
S
slguan 已提交
1001

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

S
slguan 已提交
1004
  SSdbOper oper = {
S
slguan 已提交
1005
    .type = SDB_OPER_GLOBAL,
S
[TD-17]  
slguan 已提交
1006
    .table = tsSuperTableSdb,
1007
    .pObj = pStable,
1008 1009
    .pMsg = pMsg,
    .cb = mnodeDropSuperTableTagCb
S
[TD-17]  
slguan 已提交
1010
  };
S
slguan 已提交
1011

S
[TD-17]  
slguan 已提交
1012
  int32_t code = sdbUpdateRow(&oper);
1013 1014
  if (code == TSDB_CODE_SUCCESS) {
    code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
[TD-17]  
slguan 已提交
1015
  }
1016 1017

  return code;
S
slguan 已提交
1018 1019
}

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

static int32_t mnodeModifySuperTableTagName(SMnodeMsg *pMsg, char *oldTagName, char *newTagName) {
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
1029
  int32_t col = mnodeFindSuperTableTagIndex(pStable, oldTagName);
S
slguan 已提交
1030
  if (col < 0) {
S
Shengliang Guan 已提交
1031 1032
    mError("app:%p:%p, stable:%s, failed to modify table tag, oldName: %s, newName: %s", pMsg->rpcMsg.ahandle, pMsg,
           pStable->info.tableId, oldTagName, newTagName);
1033
    return TSDB_CODE_MND_TAG_NOT_EXIST;
S
slguan 已提交
1034 1035 1036 1037
  }

  // int32_t  rowSize = 0;
  uint32_t len = strlen(newTagName);
S
slguan 已提交
1038
  if (len >= TSDB_COL_NAME_LEN) {
1039
    return TSDB_CODE_MND_COL_NAME_TOO_LONG;
S
slguan 已提交
1040
  }
S
slguan 已提交
1041

1042
  if (mnodeFindSuperTableTagIndex(pStable, newTagName) >= 0) {
1043
    return TSDB_CODE_MND_TAG_ALREAY_EXIST;
S
slguan 已提交
1044
  }
1045
  
S
slguan 已提交
1046
  // update
S
slguan 已提交
1047
  SSchema *schema = (SSchema *) (pStable->schema + pStable->numOfColumns + col);
B
Bomin Zhang 已提交
1048
  tstrncpy(schema->name, newTagName, sizeof(schema->name));
S
slguan 已提交
1049

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

S
slguan 已提交
1053
  SSdbOper oper = {
S
slguan 已提交
1054
    .type = SDB_OPER_GLOBAL,
S
[TD-17]  
slguan 已提交
1055
    .table = tsSuperTableSdb,
1056
    .pObj = pStable,
1057 1058
    .pMsg = pMsg,
    .cb = mnodeModifySuperTableTagNameCb
S
[TD-17]  
slguan 已提交
1059
  };
S
slguan 已提交
1060

S
[TD-17]  
slguan 已提交
1061
  int32_t code = sdbUpdateRow(&oper);
1062 1063
  if (code == TSDB_CODE_SUCCESS) {
    code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
slguan 已提交
1064
  }
1065 1066

  return code;
S
slguan 已提交
1067 1068
}

1069
static int32_t mnodeFindSuperTableColumnIndex(SSuperTableObj *pStable, char *colName) {
S
slguan 已提交
1070
  SSchema *schema = (SSchema *) pStable->schema;
S
slguan 已提交
1071 1072 1073
  for (int32_t col = 0; col < pStable->numOfColumns; col++) {
    if (strcasecmp(schema[col].name, colName) == 0) {
      return col;
S
slguan 已提交
1074 1075 1076 1077 1078 1079
    }
  }

  return -1;
}

1080 1081
static int32_t mnodeAddSuperTableColumnCb(SMnodeMsg *pMsg, int32_t code) {
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
1082
  mLInfo("app:%p:%p, stable %s, add column result:%s", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
1083 1084 1085 1086 1087
          tstrerror(code));
  return code;
}

static int32_t mnodeAddSuperTableColumn(SMnodeMsg *pMsg, SSchema schema[], int32_t ncols) {
1088
  SDbObj *pDb = pMsg->pDb;
1089
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
S
slguan 已提交
1090
  if (ncols <= 0) {
B
Bomin Zhang 已提交
1091
    mError("app:%p:%p, stable:%s, add column, ncols:%d <= 0", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId, ncols);
1092
    return TSDB_CODE_MND_APP_ERROR;
S
slguan 已提交
1093 1094 1095
  }

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

1102
    if (mnodeFindSuperTableTagIndex(pStable, schema[i].name) > 0) {
S
Shengliang Guan 已提交
1103 1104
      mError("app:%p:%p, stable:%s, add column, tag:%s already exist", pMsg->rpcMsg.ahandle, pMsg,
             pStable->info.tableId, schema[i].name);
1105
      return TSDB_CODE_MND_TAG_ALREAY_EXIST;
S
slguan 已提交
1106 1107 1108 1109 1110 1111
    }
  }

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

S
slguan 已提交
1112 1113 1114
  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 已提交
1115

L
liu0x54 已提交
1116
  SSchema *tschema = (SSchema *) (pStable->schema + pStable->numOfColumns);
S
slguan 已提交
1117 1118 1119 1120 1121 1122 1123
  for (int32_t i = 0; i < ncols; i++) {
    tschema[i].colId = pStable->nextColId++;
  }

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

1124
  SAcctObj *pAcct = mnodeGetAcct(pDb->acct);
S
[TD-17]  
slguan 已提交
1125 1126
  if (pAcct != NULL) {
    pAcct->acctInfo.numOfTimeSeries += (ncols * pStable->numOfTables);
1127
    mnodeDecAcctRef(pAcct);
S
[TD-17]  
slguan 已提交
1128
  }
S
slguan 已提交
1129

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

S
slguan 已提交
1132
  SSdbOper oper = {
S
slguan 已提交
1133
    .type = SDB_OPER_GLOBAL,
S
[TD-17]  
slguan 已提交
1134
    .table = tsSuperTableSdb,
1135
    .pObj = pStable,
1136 1137
    .pMsg = pMsg,
    .cb = mnodeAddSuperTableColumnCb
S
[TD-17]  
slguan 已提交
1138 1139 1140
  };

  int32_t code = sdbUpdateRow(&oper);
1141 1142
  if (code == TSDB_CODE_SUCCESS) {
    code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
[TD-17]  
slguan 已提交
1143
  }
1144 1145

  return code;
S
slguan 已提交
1146 1147
}

1148 1149
static int32_t mnodeDropSuperTableColumnCb(SMnodeMsg *pMsg, int32_t code) {
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
1150
  mLInfo("app:%p:%p, stable %s, delete column result:%s", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
1151 1152 1153 1154 1155
         tstrerror(code));
  return code;
}

static int32_t mnodeDropSuperTableColumn(SMnodeMsg *pMsg, char *colName) {
1156
  SDbObj *pDb = pMsg->pDb;
1157
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
1158
  int32_t col = mnodeFindSuperTableColumnIndex(pStable, colName);
S
slguan 已提交
1159
  if (col <= 0) {
S
Shengliang Guan 已提交
1160 1161
    mError("app:%p:%p, stable:%s, drop column, column:%s not exist", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
           colName);
1162
    return TSDB_CODE_MND_FIELD_NOT_EXIST;
S
slguan 已提交
1163 1164
  }

S
slguan 已提交
1165
  memmove(pStable->schema + col, pStable->schema + col + 1,
S
slguan 已提交
1166 1167 1168 1169 1170 1171 1172 1173
          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);

1174
  SAcctObj *pAcct = mnodeGetAcct(pDb->acct);
S
[TD-17]  
slguan 已提交
1175 1176
  if (pAcct != NULL) {
    pAcct->acctInfo.numOfTimeSeries -= pStable->numOfTables;
1177
    mnodeDecAcctRef(pAcct);
S
[TD-17]  
slguan 已提交
1178
  }
S
slguan 已提交
1179

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

S
slguan 已提交
1182
  SSdbOper oper = {
S
slguan 已提交
1183
    .type = SDB_OPER_GLOBAL,
S
[TD-17]  
slguan 已提交
1184
    .table = tsSuperTableSdb,
1185
    .pObj = pStable,
1186 1187
    .pMsg = pMsg,
    .cb = mnodeDropSuperTableColumnCb
S
[TD-17]  
slguan 已提交
1188 1189 1190
  };

  int32_t code = sdbUpdateRow(&oper);
1191 1192
  if (code == TSDB_CODE_SUCCESS) {
    code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
[TD-17]  
slguan 已提交
1193
  }
S
Shengliang Guan 已提交
1194

1195
  return code;
S
slguan 已提交
1196 1197
}

S
slguan 已提交
1198
// show super tables
1199 1200
static int32_t mnodeGetShowSuperTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
  SDbObj *pDb = mnodeGetDb(pShow->db);
1201
  if (pDb == NULL) return TSDB_CODE_MND_DB_NOT_SELECTED;
S
slguan 已提交
1202 1203 1204 1205

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

H
Haojun Liao 已提交
1206 1207 1208
  SSchema tbnameSchema = tGetTableNameColumnSchema();
  pShow->bytes[cols] = tbnameSchema.bytes;
  pSchema[cols].type = tbnameSchema.type;
S
slguan 已提交
1209 1210 1211 1212 1213 1214
  strcpy(pSchema[cols].name, "name");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
1215
  strcpy(pSchema[cols].name, "created_time");
S
slguan 已提交
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
  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];

1246
  mnodeDecDbRef(pDb);
S
slguan 已提交
1247 1248 1249
  return 0;
}

S
slguan 已提交
1250
// retrieve super tables
1251
int32_t mnodeRetrieveShowSuperTables(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
S
slguan 已提交
1252 1253 1254 1255
  int32_t         numOfRows = 0;
  char *          pWrite;
  int32_t         cols = 0;
  SSuperTableObj *pTable = NULL;
S
Shengliang Guan 已提交
1256
  char            prefix[64] = {0};
S
slguan 已提交
1257 1258
  int32_t         prefixLen;

1259
  SDbObj *pDb = mnodeGetDb(pShow->db);
S
slguan 已提交
1260 1261
  if (pDb == NULL) return 0;

S
Shengliang Guan 已提交
1262
  tstrncpy(prefix, pDb->name, 64);
S
slguan 已提交
1263 1264 1265 1266
  strcat(prefix, TS_PATH_DELIMITER);
  prefixLen = strlen(prefix);

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

S
slguan 已提交
1269
  while (numOfRows < rows) {    
1270
    pShow->pIter = mnodeGetNextSuperTable(pShow->pIter, &pTable);
S
slguan 已提交
1271 1272
    if (pTable == NULL) break;
    if (strncmp(pTable->info.tableId, prefix, prefixLen)) {
1273
      mnodeDecTableRef(pTable);
S
slguan 已提交
1274 1275 1276 1277
      continue;
    }

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

B
Bomin Zhang 已提交
1280
    if (pShow->payloadLen > 0 && patternMatch(pShow->payload, stableName, sizeof(stableName) - 1, &info) != TSDB_PATTERN_MATCH) {
1281
      mnodeDecTableRef(pTable);
S
slguan 已提交
1282
      continue;
S
Shengliang Guan 已提交
1283
    }
S
slguan 已提交
1284 1285 1286 1287

    cols = 0;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
H
hjxilinx 已提交
1288
  
B
Bomin Zhang 已提交
1289
    int16_t len = strnlen(stableName, TSDB_DB_NAME_LEN - 1);
H
hjxilinx 已提交
1290 1291 1292 1293
    *(int16_t*) pWrite = len;
    pWrite += sizeof(int16_t); // todo refactor
  
    strncpy(pWrite, stableName, len);
S
slguan 已提交
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
    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++;
1313
    mnodeDecTableRef(pTable);
S
slguan 已提交
1314 1315 1316
  }

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

S
slguan 已提交
1319 1320 1321
  return numOfRows;
}

1322
void mnodeDropAllSuperTables(SDbObj *pDropDb) {
S
Shengliang Guan 已提交
1323
  void *  pIter= NULL;
S
slguan 已提交
1324 1325 1326 1327
  int32_t numOfTables = 0;
  int32_t dbNameLen = strlen(pDropDb->name);
  SSuperTableObj *pTable = NULL;

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

S
slguan 已提交
1330
  while (1) {
1331
    pIter = mnodeGetNextSuperTable(pIter, &pTable);
S
slguan 已提交
1332
    if (pTable == NULL) break;
S
slguan 已提交
1333 1334

    if (strncmp(pDropDb->name, pTable->info.tableId, dbNameLen) == 0) {
S
slguan 已提交
1335
      SSdbOper oper = {
S
slguan 已提交
1336
        .type = SDB_OPER_LOCAL,
S
slguan 已提交
1337 1338 1339 1340 1341 1342
        .table = tsSuperTableSdb,
        .pObj = pTable,
      };
      sdbDeleteRow(&oper);
      numOfTables ++;
    }
S
slguan 已提交
1343

1344
    mnodeDecTableRef(pTable);
S
slguan 已提交
1345
  }
S
slguan 已提交
1346

S
Shengliang Guan 已提交
1347 1348
  sdbFreeIter(pIter);

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

1352
static int32_t mnodeSetSchemaFromSuperTable(SSchema *pSchema, SSuperTableObj *pTable) {
S
slguan 已提交
1353
  int32_t numOfCols = pTable->numOfColumns + pTable->numOfTags;
1354 1355
  assert(numOfCols <= TSDB_MAX_COLUMNS);
  
S
slguan 已提交
1356
  for (int32_t i = 0; i < numOfCols; ++i) {
B
Bomin Zhang 已提交
1357
    tstrncpy(pSchema->name, pTable->schema[i].name, sizeof(pSchema->name));
S
slguan 已提交
1358 1359 1360 1361 1362 1363 1364 1365 1366
    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);
}

1367
static int32_t mnodeGetSuperTableMeta(SMnodeMsg *pMsg) {
S
slguan 已提交
1368
  SSuperTableObj *pTable = (SSuperTableObj *)pMsg->pTable;
1369
  STableMetaMsg *pMeta   = rpcMallocCont(sizeof(STableMetaMsg) + sizeof(SSchema) * (TSDB_MAX_TAGS + TSDB_MAX_COLUMNS + 16));
S
slguan 已提交
1370 1371
  pMeta->uid          = htobe64(pTable->uid);
  pMeta->sversion     = htons(pTable->sversion);
1372
  pMeta->tversion     = htons(pTable->tversion);
S
slguan 已提交
1373
  pMeta->precision    = pMsg->pDb->cfg.precision;
S
slguan 已提交
1374 1375 1376
  pMeta->numOfTags    = (uint8_t)pTable->numOfTags;
  pMeta->numOfColumns = htons((int16_t)pTable->numOfColumns);
  pMeta->tableType    = pTable->info.type;
1377
  pMeta->contLen      = sizeof(STableMetaMsg) + mnodeSetSchemaFromSuperTable(pMeta->schema, pTable);
B
Bomin Zhang 已提交
1378
  tstrncpy(pMeta->tableId, pTable->info.tableId, sizeof(pMeta->tableId));
S
slguan 已提交
1379

B
Bomin Zhang 已提交
1380
  pMsg->rpcRsp.len = pMeta->contLen;
S
slguan 已提交
1381 1382
  pMeta->contLen = htons(pMeta->contLen);

1383
  pMsg->rpcRsp.rsp = pMeta;
S
Shengliang Guan 已提交
1384

1385
  mDebug("app:%p:%p, stable:%s, uid:%" PRIu64 " table meta is retrieved", pMsg->rpcMsg.ahandle, pMsg,
S
Shengliang Guan 已提交
1386
         pTable->info.tableId, pTable->uid);
1387
  return TSDB_CODE_SUCCESS;
S
slguan 已提交
1388 1389
}

1390
static int32_t mnodeProcessSuperTableVgroupMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
1391
  SCMSTableVgroupMsg *pInfo = pMsg->rpcMsg.pCont;
H
hjxilinx 已提交
1392
  int32_t numOfTable = htonl(pInfo->numOfTables);
1393

S
scripts  
Shengliang Guan 已提交
1394
  // reserve space
S
Shengliang Guan 已提交
1395
  int32_t contLen = sizeof(SCMSTableVgroupRspMsg) + 32 * sizeof(SCMVgroupInfo) + sizeof(SVgroupsInfo); 
1396 1397
  for (int32_t i = 0; i < numOfTable; ++i) {
    char *stableName = (char*)pInfo + sizeof(SCMSTableVgroupMsg) + (TSDB_TABLE_ID_LEN) * i;
1398
    SSuperTableObj *pTable = mnodeGetSuperTable(stableName);
1399
    if (pTable != NULL && pTable->vgHash != NULL) {
1400
      contLen += (taosHashGetSize(pTable->vgHash) * sizeof(SCMVgroupInfo) + sizeof(SVgroupsInfo));
1401
    } 
1402
    mnodeDecTableRef(pTable);
1403 1404
  }

S
scripts  
Shengliang Guan 已提交
1405
  SCMSTableVgroupRspMsg *pRsp = rpcMallocCont(contLen);
S
slguan 已提交
1406
  if (pRsp == NULL) {
1407
    return TSDB_CODE_MND_OUT_OF_MEMORY;
1408
  }
1409

1410
  pRsp->numOfTables = 0;
1411
  char *msg = (char *)pRsp + sizeof(SCMSTableVgroupRspMsg);
1412 1413

  for (int32_t i = 0; i < numOfTable; ++i) {
1414
    char *          stableName = (char *)pInfo + sizeof(SCMSTableVgroupMsg) + (TSDB_TABLE_ID_LEN)*i;
1415
    SSuperTableObj *pTable = mnodeGetSuperTable(stableName);
1416
    if (pTable == NULL) {
S
Shengliang Guan 已提交
1417
      mError("app:%p:%p, stable:%s, not exist while get stable vgroup info", pMsg->rpcMsg.ahandle, pMsg, stableName);
1418 1419 1420 1421
      mnodeDecTableRef(pTable);
      continue;
    }
    if (pTable->vgHash == NULL) {
1422
      mError("app:%p:%p, stable:%s, no vgroup exist while get stable vgroup info", pMsg->rpcMsg.ahandle, pMsg,
S
Shengliang Guan 已提交
1423
             stableName);
1424 1425
      mnodeDecTableRef(pTable);

1426 1427
      // even this super table has no corresponding table, still return
      pRsp->numOfTables++;
1428

1429 1430 1431 1432 1433 1434
      SVgroupsInfo *pVgroupInfo = (SVgroupsInfo *)msg;
      pVgroupInfo->numOfVgroups = 0;
      
      msg += sizeof(SVgroupsInfo);
    } else {
      SVgroupsInfo *pVgroupInfo = (SVgroupsInfo *)msg;
1435

1436 1437 1438 1439 1440 1441
      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;
1442

1443 1444 1445 1446
        pVgroupInfo->vgroups[vgSize].vgId = htonl(pVgroup->vgId);
        for (int32_t vn = 0; vn < pVgroup->numOfVnodes; ++vn) {
          SDnodeObj *pDnode = pVgroup->vnodeGid[vn].pDnode;
          if (pDnode == NULL) break;
1447

S
Shengliang Guan 已提交
1448
          tstrncpy(pVgroupInfo->vgroups[vgSize].ipAddr[vn].fqdn, pDnode->dnodeFqdn, TSDB_FQDN_LEN);
1449
          pVgroupInfo->vgroups[vgSize].ipAddr[vn].port = htons(pDnode->dnodePort);
1450

1451 1452
          pVgroupInfo->vgroups[vgSize].numOfIps++;
        }
1453

1454 1455 1456
        vgSize++;
        mnodeDecVgroupRef(pVgroup);
      }
1457

1458 1459 1460 1461
      taosHashDestroyIter(pIter);
      mnodeDecTableRef(pTable);

      pVgroupInfo->numOfVgroups = htonl(vgSize);
1462

1463 1464 1465 1466
      // one table is done, try the next table
      msg += sizeof(SVgroupsInfo) + vgSize * sizeof(SCMVgroupInfo);
      pRsp->numOfTables++;
    }
1467
  }
S
slguan 已提交
1468

1469 1470
  if (pRsp->numOfTables != numOfTable) {
    rpcFreeCont(pRsp);
1471
    return TSDB_CODE_MND_INVALID_TABLE_NAME;
1472 1473 1474 1475
  } else {
    pRsp->numOfTables = htonl(pRsp->numOfTables);
    pMsg->rpcRsp.rsp = pRsp;
    pMsg->rpcRsp.len = msg - (char *)pRsp;
1476

1477 1478
    return TSDB_CODE_SUCCESS;
  }
S
slguan 已提交
1479 1480
}

1481
static void mnodeProcessDropSuperTableRsp(SRpcMsg *rpcMsg) {
1482
  mInfo("drop stable rsp received, result:%s", tstrerror(rpcMsg->code));
S
slguan 已提交
1483 1484
}

S
Shengliang Guan 已提交
1485
static void *mnodeBuildCreateChildTableMsg(SCMCreateTableMsg *pMsg, SChildTableObj *pTable) {
1486
  STagData *  pTagData = NULL;
S
slguan 已提交
1487 1488 1489
  int32_t tagDataLen = 0;
  int32_t totalCols = 0;
  int32_t contLen = 0;
S
Shengliang Guan 已提交
1490
  if (pTable->info.type == TSDB_CHILD_TABLE) {
S
slguan 已提交
1491
    totalCols = pTable->superTable->numOfColumns + pTable->superTable->numOfTags;
S
Shengliang Guan 已提交
1492
    contLen = sizeof(SMDCreateTableMsg) + totalCols * sizeof(SSchema) + pTable->sqlLen;
S
Shengliang Guan 已提交
1493 1494
    if (pMsg != NULL) {
      pTagData = (STagData *)pMsg->schema;
S
Shengliang Guan 已提交
1495
      tagDataLen = htonl(pTagData->dataLen);
H
Hongze Cheng 已提交
1496
      contLen += tagDataLen;
S
Shengliang Guan 已提交
1497
    }
S
slguan 已提交
1498 1499 1500 1501 1502 1503 1504
  } else {
    totalCols = pTable->numOfColumns;
    contLen = sizeof(SMDCreateTableMsg) + totalCols * sizeof(SSchema) + pTable->sqlLen;
  }

  SMDCreateTableMsg *pCreate = rpcMallocCont(contLen);
  if (pCreate == NULL) {
1505
    terrno = TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
1506 1507 1508
    return NULL;
  }

S
Shengliang Guan 已提交
1509
  mnodeExtractTableName(pTable->info.tableId, pCreate->tableId);
S
slguan 已提交
1510 1511 1512 1513 1514 1515 1516 1517 1518
  pCreate->contLen       = htonl(contLen);
  pCreate->vgId          = htonl(pTable->vgId);
  pCreate->tableType     = pTable->info.type;
  pCreate->createdTime   = htobe64(pTable->createdTime);
  pCreate->sid           = htonl(pTable->sid);
  pCreate->sqlDataLen    = htonl(pTable->sqlLen);
  pCreate->uid           = htobe64(pTable->uid);
  
  if (pTable->info.type == TSDB_CHILD_TABLE) {
S
Shengliang Guan 已提交
1519
    mnodeExtractTableName(pTable->superTable->info.tableId, pCreate->superTableId);
S
slguan 已提交
1520 1521 1522
    pCreate->numOfColumns  = htons(pTable->superTable->numOfColumns);
    pCreate->numOfTags     = htons(pTable->superTable->numOfTags);
    pCreate->sversion      = htonl(pTable->superTable->sversion);
1523
    pCreate->tversion      = htonl(pTable->superTable->tversion);
S
slguan 已提交
1524 1525 1526 1527 1528 1529
    pCreate->tagDataLen    = htonl(tagDataLen);
    pCreate->superTableUid = htobe64(pTable->superTable->uid);
  } else {
    pCreate->numOfColumns  = htons(pTable->numOfColumns);
    pCreate->numOfTags     = 0;
    pCreate->sversion      = htonl(pTable->sversion);
1530
    pCreate->tversion      = 0;
S
slguan 已提交
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547
    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) {
1548
    memcpy(pCreate->data + totalCols * sizeof(SSchema), pTagData->data, tagDataLen);
1549 1550
  }

S
Shengliang Guan 已提交
1551
  if (pTable->info.type == TSDB_STREAM_TABLE) {
1552
    memcpy(pCreate->data + totalCols * sizeof(SSchema), pTable->sql, pTable->sqlLen);
S
slguan 已提交
1553 1554 1555 1556 1557
  }

  return pCreate;
}

1558
static int32_t mnodeDoCreateChildTableCb(SMnodeMsg *pMsg, int32_t code) {
1559
  SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;
S
Shengliang Guan 已提交
1560 1561 1562 1563
  assert(pTable);

  mDebug("app:%p:%p, table:%s, create table in id:%d, uid:%" PRIu64 ", result:%s", pMsg->rpcMsg.ahandle, pMsg,
         pTable->info.tableId, pTable->sid, pTable->uid, tstrerror(code));
1564

1565
  if (code != TSDB_CODE_SUCCESS) return code;
1566

1567
  SCMCreateTableMsg *pCreate = pMsg->rpcMsg.pCont;
1568
  SMDCreateTableMsg *pMDCreate = mnodeBuildCreateChildTableMsg(pCreate, pTable);
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
  if (pMDCreate == NULL) {
    return terrno;
  }

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

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

1586
static int32_t mnodeDoCreateChildTable(SMnodeMsg *pMsg, int32_t tid) {
1587
  SVgObj *pVgroup = pMsg->pVgroup;
1588
  SCMCreateTableMsg *pCreate = pMsg->rpcMsg.pCont;
1589
  SChildTableObj *pTable = calloc(1, sizeof(SChildTableObj));
S
slguan 已提交
1590
  if (pTable == NULL) {
S
Shengliang Guan 已提交
1591
    mError("app:%p:%p, table:%s, failed to alloc memory", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId);
1592
    return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
1593 1594 1595 1596 1597 1598 1599 1600
  }

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

1601 1602 1603 1604
  pTable->info.tableId = strdup(pCreate->tableId);  
  pTable->createdTime  = taosGetTimestampMs();
  pTable->sid          = tid;
  pTable->vgId         = pVgroup->vgId;
S
slguan 已提交
1605 1606
    
  if (pTable->info.type == TSDB_CHILD_TABLE) {
1607
    STagData *pTagData = (STagData *) pCreate->schema;  // it is a tag key
1608 1609
    if (pMsg->pSTable == NULL) pMsg->pSTable = mnodeGetSuperTable(pTagData->name);
    if (pMsg->pSTable == NULL) {
S
Shengliang Guan 已提交
1610 1611
      mError("app:%p:%p, table:%s, corresponding super table:%s does not exist", pMsg->rpcMsg.ahandle, pMsg,
             pCreate->tableId, pTagData->name);
1612
      mnodeDestroyChildTable(pTable);
1613
      return TSDB_CODE_MND_INVALID_TABLE_NAME;
S
slguan 已提交
1614
    }
1615 1616
    
    pTable->suid = pMsg->pSTable->uid;
1617 1618
    pTable->uid  = (((uint64_t)pTable->vgId) << 40) + ((((uint64_t)pTable->sid) & ((1ul << 24) - 1ul)) << 16) +
                  (sdbGetVersion() & ((1ul << 16) - 1ul));
1619
    pTable->superTable = pMsg->pSTable;
S
slguan 已提交
1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630
  } else {
    pTable->uid          = (((uint64_t) pTable->createdTime) << 16) + (sdbGetVersion() & ((1ul << 16) - 1ul));
    pTable->sversion     = 0;
    pTable->numOfColumns = htons(pCreate->numOfColumns);
    pTable->sqlLen       = htons(pCreate->sqlLen);

    int32_t numOfCols  = pTable->numOfColumns;
    int32_t schemaSize = numOfCols * sizeof(SSchema);
    pTable->schema     = (SSchema *) calloc(1, schemaSize);
    if (pTable->schema == NULL) {
      free(pTable);
1631
      return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646
    }
    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);
1647
        return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
1648 1649 1650
      }
      memcpy(pTable->sql, (char *) (pCreate->schema) + numOfCols * sizeof(SSchema), pTable->sqlLen);
      pTable->sql[pTable->sqlLen - 1] = 0;
1651
      mDebug("app:%p:%p, table:%s, stream sql len:%d sql:%s", pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId,
S
Shengliang Guan 已提交
1652
             pTable->sqlLen, pTable->sql);
S
slguan 已提交
1653 1654
    }
  }
1655 1656 1657 1658

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

S
slguan 已提交
1659
  SSdbOper desc = {0};
S
slguan 已提交
1660
  desc.type = SDB_OPER_GLOBAL;
S
slguan 已提交
1661 1662
  desc.pObj = pTable;
  desc.table = tsChildTableSdb;
1663 1664
  desc.pMsg = pMsg;
  desc.cb = mnodeDoCreateChildTableCb;
S
slguan 已提交
1665
  
1666 1667
  int32_t code = sdbInsertRow(&desc);
  if (code != TSDB_CODE_SUCCESS) {
1668 1669
    mnodeDestroyChildTable(pTable);
    pMsg->pTable = NULL;
S
Shengliang Guan 已提交
1670 1671
    mError("app:%p:%p, table:%s, update sdb error, reason:%s", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId,
           tstrerror(code));
1672 1673 1674 1675
    return code;       
  } else {
    return TSDB_CODE_MND_ACTION_IN_PROGRESS;
  }
S
slguan 已提交
1676 1677
}

1678
static int32_t mnodeProcessCreateChildTableMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
1679
  SCMCreateTableMsg *pCreate = pMsg->rpcMsg.pCont;
S
slguan 已提交
1680 1681
  int32_t code = grantCheck(TSDB_GRANT_TIMESERIES);
  if (code != TSDB_CODE_SUCCESS) {
1682 1683
    mError("app:%p:%p, table:%s, failed to create, grant timeseries failed", pMsg->rpcMsg.ahandle, pMsg,
           pCreate->tableId);
1684
    return code;
S
slguan 已提交
1685 1686
  }

1687
  SVgObj *pVgroup = mnodeGetAvailableVgroup(pMsg->pDb);
S
slguan 已提交
1688
  if (pVgroup == NULL) {
1689
    mDebug("app:%p:%p, table:%s, start to create a new vgroup", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId);
1690
    return mnodeCreateVgroup(pMsg, pMsg->pDb);
S
slguan 已提交
1691 1692
  }

1693
  if (pMsg->retry == 0) {
1694
    if (pMsg->pTable == NULL) {
1695 1696
      int32_t sid = taosAllocateId(pVgroup->idPool);
      if (sid <= 0) {
1697
        mDebug("app:%p:%p, table:%s, no enough sid in vgId:%d", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId,
1698
               pVgroup->vgId);
1699
        return mnodeCreateVgroup(pMsg, pMsg->pDb);
1700 1701
      }

1702 1703 1704 1705
      if (pMsg->pVgroup == NULL) {
        pMsg->pVgroup = pVgroup;
        mnodeIncVgroupRef(pVgroup);
      }
1706

1707
      mDebug("app:%p:%p, table:%s, create table in vgroup, vgId:%d sid:%d", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId,
1708
             pVgroup->vgId, sid);
1709

1710
      return mnodeDoCreateChildTable(pMsg, sid);
1711
    }
1712
  } else {
1713
    if (pMsg->pTable == NULL) pMsg->pTable = mnodeGetTable(pCreate->tableId);
1714
  }
1715

S
slguan 已提交
1716
  if (pMsg->pTable == NULL) {
B
Bomin Zhang 已提交
1717
    mError("app:%p:%p, table:%s, object not found, retry:%d reason:%s", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId, pMsg->retry,
1718
           tstrerror(terrno));
1719
    return terrno;
1720
  } else {
1721
    mDebug("app:%p:%p, table:%s, send create msg to vnode again", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId);
1722
    return mnodeDoCreateChildTableCb(pMsg, TSDB_CODE_SUCCESS);
S
slguan 已提交
1723 1724 1725
  }
}

1726
static int32_t mnodeProcessDropChildTableMsg(SMnodeMsg *pMsg) {
S
slguan 已提交
1727
  SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;
1728
  if (pMsg->pVgroup == NULL) pMsg->pVgroup = mnodeGetVgroup(pTable->vgId);
1729
  if (pMsg->pVgroup == NULL) {
S
Shengliang Guan 已提交
1730 1731
    mError("app:%p:%p, table:%s, failed to drop ctable, vgroup not exist", pMsg->rpcMsg.ahandle, pMsg,
           pTable->info.tableId);
1732
    return TSDB_CODE_MND_APP_ERROR;
S
slguan 已提交
1733 1734 1735 1736
  }

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

S
Shengliang Guan 已提交
1742
  tstrncpy(pDrop->tableId, pTable->info.tableId, TSDB_TABLE_ID_LEN);
S
slguan 已提交
1743 1744 1745 1746 1747
  pDrop->vgId    = htonl(pTable->vgId);
  pDrop->contLen = htonl(sizeof(SMDDropTableMsg));
  pDrop->sid     = htonl(pTable->sid);
  pDrop->uid     = htobe64(pTable->uid);

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

1750
  mInfo("app:%p:%p, table:%s, send drop ctable msg", pMsg->rpcMsg.ahandle, pMsg, pDrop->tableId);
S
slguan 已提交
1751
  SRpcMsg rpcMsg = {
1752
    .handle  = pMsg,
S
slguan 已提交
1753 1754 1755 1756 1757 1758
    .pCont   = pDrop,
    .contLen = sizeof(SMDDropTableMsg),
    .code    = 0,
    .msgType = TSDB_MSG_TYPE_MD_DROP_TABLE
  };

J
jtao1735 已提交
1759
  dnodeSendMsgToDnode(&ipSet, &rpcMsg);
1760

1761
  return TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
slguan 已提交
1762 1763
}

1764
static int32_t mnodeFindNormalTableColumnIndex(SChildTableObj *pTable, char *colName) {
S
slguan 已提交
1765
  SSchema *schema = (SSchema *) pTable->schema;
S
slguan 已提交
1766 1767 1768
  for (int32_t col = 0; col < pTable->numOfColumns; col++) {
    if (strcasecmp(schema[col].name, colName) == 0) {
      return col;
S
slguan 已提交
1769 1770 1771 1772 1773 1774
    }
  }

  return -1;
}

1775
static int32_t mnodeAlterNormalTableColumnCb(SMnodeMsg *pMsg, int32_t code) {
1776
  SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;
1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806
  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;
    }
  }

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

1807
  mDebug("app:%p:%p, ctable %s, send alter column msg to vgId:%d", pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId,
1808 1809 1810 1811
         pMsg->pVgroup->vgId);

  dnodeSendMsgToDnode(&ipSet, &rpcMsg);
  return TSDB_CODE_MND_ACTION_IN_PROGRESS;
1812 1813 1814 1815
}

static int32_t mnodeAddNormalTableColumn(SMnodeMsg *pMsg, SSchema schema[], int32_t ncols) {
  SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;
1816
  SDbObj *pDb = pMsg->pDb;
S
slguan 已提交
1817
  if (ncols <= 0) {
B
Bomin Zhang 已提交
1818
    mError("app:%p:%p, ctable:%s, add column, ncols:%d <= 0", pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId, ncols);
1819
    return TSDB_CODE_MND_APP_ERROR;
S
slguan 已提交
1820 1821 1822
  }

  for (int32_t i = 0; i < ncols; i++) {
1823
    if (mnodeFindNormalTableColumnIndex(pTable, schema[i].name) > 0) {
1824
      mError("app:%p:%p, ctable:%s, add column, column:%s already exist", pMsg->rpcMsg.ahandle, pMsg,
S
Shengliang Guan 已提交
1825
             pTable->info.tableId, schema[i].name);
1826
      return TSDB_CODE_MND_FIELD_ALREAY_EXIST;
S
slguan 已提交
1827 1828 1829 1830 1831 1832
    }
  }

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

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

S
slguan 已提交
1835
  SSchema *tschema = (SSchema *) (pTable->schema + pTable->numOfColumns);
S
slguan 已提交
1836 1837 1838 1839 1840 1841
  for (int32_t i = 0; i < ncols; i++) {
    tschema[i].colId = pTable->nextColId++;
  }

  pTable->numOfColumns += ncols;
  pTable->sversion++;
S
[TD-17]  
slguan 已提交
1842
  
1843
  SAcctObj *pAcct = mnodeGetAcct(pDb->acct);
S
[TD-17]  
slguan 已提交
1844
  if (pAcct != NULL) {
S
slguan 已提交
1845
    pAcct->acctInfo.numOfTimeSeries += ncols;
1846
    mnodeDecAcctRef(pAcct);
S
[TD-17]  
slguan 已提交
1847
  }
1848

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

S
slguan 已提交
1851
  SSdbOper oper = {
S
slguan 已提交
1852
    .type = SDB_OPER_GLOBAL,
S
[TD-17]  
slguan 已提交
1853
    .table = tsChildTableSdb,
1854
    .pObj = pTable,
1855
    .pMsg = pMsg,
1856
    .cb = mnodeAlterNormalTableColumnCb
S
[TD-17]  
slguan 已提交
1857
  };
S
slguan 已提交
1858

S
[TD-17]  
slguan 已提交
1859
  int32_t code = sdbUpdateRow(&oper);
1860 1861
  if (code == TSDB_CODE_SUCCESS) {
    return TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
[TD-17]  
slguan 已提交
1862
  }
S
Shengliang Guan 已提交
1863

1864
  return code;
S
slguan 已提交
1865 1866
}

1867
static int32_t mnodeDropNormalTableColumn(SMnodeMsg *pMsg, char *colName) {
1868
  SDbObj *pDb = pMsg->pDb;
1869
  SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;
1870
  int32_t col = mnodeFindNormalTableColumnIndex(pTable, colName);
S
slguan 已提交
1871
  if (col <= 0) {
1872
    mError("app:%p:%p, ctable:%s, drop column, column:%s not exist", pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId,
S
Shengliang Guan 已提交
1873
           colName);
1874
    return TSDB_CODE_MND_FIELD_NOT_EXIST;
S
slguan 已提交
1875 1876
  }

S
slguan 已提交
1877
  memmove(pTable->schema + col, pTable->schema + col + 1, sizeof(SSchema) * (pTable->numOfColumns - col - 1));
S
slguan 已提交
1878 1879 1880
  pTable->numOfColumns--;
  pTable->sversion++;

1881
  SAcctObj *pAcct = mnodeGetAcct(pDb->acct);
S
[TD-17]  
slguan 已提交
1882 1883
  if (pAcct != NULL) {
    pAcct->acctInfo.numOfTimeSeries--;
1884
    mnodeDecAcctRef(pAcct);
S
[TD-17]  
slguan 已提交
1885
  }
1886

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

S
slguan 已提交
1889
  SSdbOper oper = {
S
slguan 已提交
1890
    .type = SDB_OPER_GLOBAL,
S
[TD-17]  
slguan 已提交
1891
    .table = tsChildTableSdb,
1892
    .pObj = pTable,
1893
    .pMsg = pMsg,
1894
    .cb = mnodeAlterNormalTableColumnCb
S
[TD-17]  
slguan 已提交
1895
  };
S
slguan 已提交
1896

S
[TD-17]  
slguan 已提交
1897
  int32_t code = sdbUpdateRow(&oper);
1898
  if (code == TSDB_CODE_SUCCESS) {
1899
    return TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
[TD-17]  
slguan 已提交
1900
  }
S
Shengliang Guan 已提交
1901

1902
  return code;
S
slguan 已提交
1903 1904
}

1905
static int32_t mnodeSetSchemaFromNormalTable(SSchema *pSchema, SChildTableObj *pTable) {
S
slguan 已提交
1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917
  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);
}

1918
static int32_t mnodeDoGetChildTableMeta(SMnodeMsg *pMsg, STableMetaMsg *pMeta) {
S
slguan 已提交
1919 1920 1921
  SDbObj *pDb = pMsg->pDb;
  SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;

S
slguan 已提交
1922 1923 1924 1925
  pMeta->uid       = htobe64(pTable->uid);
  pMeta->sid       = htonl(pTable->sid);
  pMeta->precision = pDb->cfg.precision;
  pMeta->tableType = pTable->info.type;
S
Shengliang Guan 已提交
1926
  tstrncpy(pMeta->tableId, pTable->info.tableId, TSDB_TABLE_ID_LEN);
S
slguan 已提交
1927 1928 1929

  if (pTable->info.type == TSDB_CHILD_TABLE) {
    pMeta->sversion     = htons(pTable->superTable->sversion);
1930
    pMeta->tversion     = htons(pTable->superTable->tversion);
S
slguan 已提交
1931
    pMeta->numOfTags    = (int8_t)pTable->superTable->numOfTags;
S
slguan 已提交
1932
    pMeta->numOfColumns = htons((int16_t)pTable->superTable->numOfColumns);
1933
    pMeta->contLen      = sizeof(STableMetaMsg) + mnodeSetSchemaFromSuperTable(pMeta->schema, pTable->superTable);
S
slguan 已提交
1934 1935
  } else {
    pMeta->sversion     = htons(pTable->sversion);
1936
    pMeta->tversion     = 0;
S
slguan 已提交
1937 1938
    pMeta->numOfTags    = 0;
    pMeta->numOfColumns = htons((int16_t)pTable->numOfColumns);
1939
    pMeta->contLen      = sizeof(STableMetaMsg) + mnodeSetSchemaFromNormalTable(pMeta->schema, pTable); 
S
slguan 已提交
1940
  }
S
Shengliang Guan 已提交
1941

1942
  if (pMsg->pVgroup == NULL) pMsg->pVgroup = mnodeGetVgroup(pTable->vgId);
1943
  if (pMsg->pVgroup == NULL) {
S
Shengliang Guan 已提交
1944 1945
    mError("app:%p:%p, table:%s, failed to get table meta, vgroup not exist", pMsg->rpcMsg.ahandle, pMsg,
           pTable->info.tableId);
1946
    return TSDB_CODE_MND_VGROUP_NOT_EXIST;
S
slguan 已提交
1947 1948
  }

1949
  for (int32_t i = 0; i < pMsg->pVgroup->numOfVnodes; ++i) {
1950
    SDnodeObj *pDnode = mnodeGetDnode(pMsg->pVgroup->vnodeGid[i].dnodeId);
S
slguan 已提交
1951
    if (pDnode == NULL) break;
J
jtao1735 已提交
1952
    strcpy(pMeta->vgroup.ipAddr[i].fqdn, pDnode->dnodeFqdn);
J
jtao1735 已提交
1953
    pMeta->vgroup.ipAddr[i].port = htons(pDnode->dnodePort + TSDB_PORT_DNODESHELL);
S
slguan 已提交
1954
    pMeta->vgroup.numOfIps++;
1955
    mnodeDecDnodeRef(pDnode);
S
slguan 已提交
1956
  }
1957
  pMeta->vgroup.vgId = htonl(pMsg->pVgroup->vgId);
S
slguan 已提交
1958

1959
  mDebug("app:%p:%p, table:%s, uid:%" PRIu64 " table meta is retrieved", pMsg->rpcMsg.ahandle, pMsg,
S
Shengliang Guan 已提交
1960
         pTable->info.tableId, pTable->uid);
S
slguan 已提交
1961 1962 1963 1964

  return TSDB_CODE_SUCCESS;
}

S
Shengliang Guan 已提交
1965
static int32_t mnodeAutoCreateChildTable(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
1966
  SCMTableInfoMsg *pInfo = pMsg->rpcMsg.pCont;
1967
  STagData *pTag = (STagData *)pInfo->tags;
1968

S
Shengliang Guan 已提交
1969
  int32_t contLen = sizeof(SCMCreateTableMsg) + offsetof(STagData, data) + htonl(pTag->dataLen);
S
slguan 已提交
1970 1971
  SCMCreateTableMsg *pCreateMsg = rpcMallocCont(contLen);
  if (pCreateMsg == NULL) {
S
Shengliang Guan 已提交
1972 1973
    mError("app:%p:%p, table:%s, failed to create table while get meta info, no enough memory", pMsg->rpcMsg.ahandle,
           pMsg, pInfo->tableId);
1974
    return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
1975
  }
S
slguan 已提交
1976

1977 1978
  size_t size = sizeof(pInfo->tableId);
  tstrncpy(pCreateMsg->tableId, pInfo->tableId, size);
B
Bomin Zhang 已提交
1979
  tstrncpy(pCreateMsg->db, pMsg->pDb->name, sizeof(pCreateMsg->db));
S
slguan 已提交
1980 1981
  pCreateMsg->igExists = 1;
  pCreateMsg->getMeta = 1;
S
slguan 已提交
1982
  pCreateMsg->contLen = htonl(contLen);
B
Bomin Zhang 已提交
1983

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

S
Shengliang Guan 已提交
1988 1989 1990 1991
  rpcFreeCont(pMsg->rpcMsg.pCont);
  pMsg->rpcMsg.msgType = TSDB_MSG_TYPE_CM_CREATE_TABLE;
  pMsg->rpcMsg.pCont = pCreateMsg;
  pMsg->rpcMsg.contLen = contLen;
1992
  
1993
  return TSDB_CODE_MND_ACTION_NEED_REPROCESSED;
S
slguan 已提交
1994
}
S
slguan 已提交
1995

1996
static int32_t mnodeGetChildTableMeta(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
1997 1998
  STableMetaMsg *pMeta =
      rpcMallocCont(sizeof(STableMetaMsg) + sizeof(SSchema) * (TSDB_MAX_TAGS + TSDB_MAX_COLUMNS + 16));
S
slguan 已提交
1999
  if (pMeta == NULL) {
S
Shengliang Guan 已提交
2000 2001
    mError("app:%p:%p, table:%s, failed to get table meta, no enough memory", pMsg->rpcMsg.ahandle, pMsg,
           pMsg->pTable->tableId);
2002
    return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
2003 2004
  }

2005
  mnodeDoGetChildTableMeta(pMsg, pMeta);
S
slguan 已提交
2006

2007 2008
  pMsg->rpcRsp.len = pMeta->contLen;
  pMsg->rpcRsp.rsp = pMeta;
S
slguan 已提交
2009
  pMeta->contLen = htons(pMeta->contLen);
2010 2011

  return TSDB_CODE_SUCCESS;
S
slguan 已提交
2012 2013
}

2014
void mnodeDropAllChildTablesInVgroups(SVgObj *pVgroup) {
2015 2016 2017 2018
  void *  pIter = NULL;
  int32_t numOfTables = 0;
  SChildTableObj *pTable = NULL;

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

  while (1) {
2022
    pIter = mnodeGetNextChildTable(pIter, &pTable);
2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033
    if (pTable == NULL) break;

    if (pTable->vgId == pVgroup->vgId) {
      SSdbOper oper = {
        .type = SDB_OPER_LOCAL,
        .table = tsChildTableSdb,
        .pObj = pTable,
      };
      sdbDeleteRow(&oper);
      numOfTables++;
    }
2034
    mnodeDecTableRef(pTable);
2035 2036 2037 2038
  }

  sdbFreeIter(pIter);

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

2042
void mnodeDropAllChildTables(SDbObj *pDropDb) {
S
Shengliang Guan 已提交
2043
  void *  pIter = NULL;
S
slguan 已提交
2044 2045 2046 2047
  int32_t numOfTables = 0;
  int32_t dbNameLen = strlen(pDropDb->name);
  SChildTableObj *pTable = NULL;

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

S
slguan 已提交
2050
  while (1) {
2051
    pIter = mnodeGetNextChildTable(pIter, &pTable);
S
slguan 已提交
2052
    if (pTable == NULL) break;
S
slguan 已提交
2053 2054

    if (strncmp(pDropDb->name, pTable->info.tableId, dbNameLen) == 0) {
S
slguan 已提交
2055
      SSdbOper oper = {
S
slguan 已提交
2056
        .type = SDB_OPER_LOCAL,
S
slguan 已提交
2057 2058 2059 2060 2061 2062
        .table = tsChildTableSdb,
        .pObj = pTable,
      };
      sdbDeleteRow(&oper);
      numOfTables++;
    }
2063
    mnodeDecTableRef(pTable);
S
slguan 已提交
2064 2065
  }

S
Shengliang Guan 已提交
2066 2067
  sdbFreeIter(pIter);

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

2071
static void mnodeDropAllChildTablesInStable(SSuperTableObj *pStable) {
S
Shengliang Guan 已提交
2072
  void *  pIter = NULL;
S
slguan 已提交
2073 2074 2075
  int32_t numOfTables = 0;
  SChildTableObj *pTable = NULL;

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

S
slguan 已提交
2078
  while (1) {
2079
    pIter = mnodeGetNextChildTable(pIter, &pTable);
S
slguan 已提交
2080
    if (pTable == NULL) break;
S
slguan 已提交
2081 2082

    if (pTable->superTable == pStable) {
S
slguan 已提交
2083
      SSdbOper oper = {
S
slguan 已提交
2084
        .type = SDB_OPER_LOCAL,
S
slguan 已提交
2085 2086 2087 2088 2089 2090
        .table = tsChildTableSdb,
        .pObj = pTable,
      };
      sdbDeleteRow(&oper);
      numOfTables++;
    }
S
slguan 已提交
2091

2092
    mnodeDecTableRef(pTable);
S
slguan 已提交
2093 2094
  }

S
Shengliang Guan 已提交
2095 2096
  sdbFreeIter(pIter);

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

2100 2101
static SChildTableObj* mnodeGetTableByPos(int32_t vnode, int32_t sid) {
  SVgObj *pVgroup = mnodeGetVgroup(vnode);
2102
  if (pVgroup == NULL) return NULL;
S
slguan 已提交
2103

S
slguan 已提交
2104
  SChildTableObj *pTable = pVgroup->tableList[sid - 1];
2105
  mnodeIncTableRef((STableObj *)pTable);
2106

2107
  mnodeDecVgroupRef(pVgroup);
S
slguan 已提交
2108 2109 2110
  return pTable;
}

2111
static int32_t mnodeProcessTableCfgMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
2112
  SDMConfigTableMsg *pCfg = pMsg->rpcMsg.pCont;
S
Shengliang Guan 已提交
2113 2114 2115
  pCfg->dnodeId = htonl(pCfg->dnodeId);
  pCfg->vgId = htonl(pCfg->vgId);
  pCfg->sid = htonl(pCfg->sid);
2116
  mDebug("app:%p:%p, dnode:%d, vgId:%d sid:%d, receive table config msg", pMsg->rpcMsg.ahandle, pMsg, pCfg->dnodeId,
S
Shengliang Guan 已提交
2117
         pCfg->vgId, pCfg->sid);
S
slguan 已提交
2118

S
Shengliang Guan 已提交
2119
  SChildTableObj *pTable = mnodeGetTableByPos(pCfg->vgId, pCfg->sid);
S
slguan 已提交
2120
  if (pTable == NULL) {
S
Shengliang Guan 已提交
2121 2122
    mError("app:%p:%p, dnode:%d, vgId:%d sid:%d, table not found", pMsg->rpcMsg.ahandle, pMsg, pCfg->dnodeId,
           pCfg->vgId, pCfg->sid);
2123
    return TSDB_CODE_MND_INVALID_TABLE_ID;
S
slguan 已提交
2124 2125
  }

S
Shengliang Guan 已提交
2126 2127
  SMDCreateTableMsg *pCreate = NULL;
  pCreate = mnodeBuildCreateChildTableMsg(NULL, (SChildTableObj *)pTable);
2128
  mnodeDecTableRef(pTable);
S
Shengliang Guan 已提交
2129 2130 2131 2132 2133
    
  if (pCreate == NULL) return terrno;
  
  pMsg->rpcRsp.rsp = pCreate;
  pMsg->rpcRsp.len = htonl(pCreate->contLen);
2134
  return TSDB_CODE_SUCCESS;
S
slguan 已提交
2135 2136
}

S
slguan 已提交
2137
// handle drop child response
2138
static void mnodeProcessDropChildTableRsp(SRpcMsg *rpcMsg) {
S
slguan 已提交
2139 2140
  if (rpcMsg->handle == NULL) return;

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

S
Shengliang Guan 已提交
2144 2145
  SChildTableObj *pTable = (SChildTableObj *)mnodeMsg->pTable;
  assert(pTable);
2146
  mInfo("app:%p:%p, table:%s, drop table rsp received, thandle:%p result:%s", mnodeMsg->rpcMsg.ahandle, mnodeMsg,
S
Shengliang Guan 已提交
2147
         pTable->info.tableId, mnodeMsg->rpcMsg.handle, tstrerror(rpcMsg->code));
S
slguan 已提交
2148 2149

  if (rpcMsg->code != TSDB_CODE_SUCCESS) {
S
Shengliang Guan 已提交
2150 2151
    mError("app:%p:%p, table:%s, failed to drop in dnode, reason:%s", mnodeMsg->rpcMsg.ahandle, mnodeMsg,
           pTable->info.tableId, tstrerror(rpcMsg->code));
2152
    dnodeSendRpcMnodeWriteRsp(mnodeMsg, rpcMsg->code);
S
slguan 已提交
2153 2154 2155
    return;
  }

2156 2157
  if (mnodeMsg->pVgroup == NULL) mnodeMsg->pVgroup = mnodeGetVgroup(pTable->vgId);
  if (mnodeMsg->pVgroup == NULL) {
S
Shengliang Guan 已提交
2158
    mError("app:%p:%p, table:%s, failed to get vgroup", mnodeMsg->rpcMsg.ahandle, mnodeMsg, pTable->info.tableId);
2159
    dnodeSendRpcMnodeWriteRsp(mnodeMsg, TSDB_CODE_MND_VGROUP_NOT_EXIST);
S
slguan 已提交
2160 2161
    return;
  }
S
Shengliang Guan 已提交
2162

S
slguan 已提交
2163
  SSdbOper oper = {
S
slguan 已提交
2164
    .type = SDB_OPER_GLOBAL,
S
slguan 已提交
2165 2166 2167
    .table = tsChildTableSdb,
    .pObj = pTable
  };
S
Shengliang Guan 已提交
2168

S
slguan 已提交
2169 2170
  int32_t code = sdbDeleteRow(&oper);
  if (code != TSDB_CODE_SUCCESS) {
S
Shengliang Guan 已提交
2171
    mError("app:%p:%p, table:%s, update ctables sdb error", mnodeMsg->rpcMsg.ahandle, mnodeMsg, pTable->info.tableId);
2172
    dnodeSendRpcMnodeWriteRsp(mnodeMsg, TSDB_CODE_MND_SDB_ERROR);
S
slguan 已提交
2173 2174 2175
    return;
  }

2176
  if (mnodeMsg->pVgroup->numOfTables <= 0) {
2177
    mInfo("app:%p:%p, vgId:%d, all tables is dropped, drop vgroup", mnodeMsg->rpcMsg.ahandle, mnodeMsg,
S
Shengliang Guan 已提交
2178
           mnodeMsg->pVgroup->vgId);
2179
    mnodeDropVgroup(mnodeMsg->pVgroup, NULL);
S
slguan 已提交
2180 2181
  }

2182
  dnodeSendRpcMnodeWriteRsp(mnodeMsg, TSDB_CODE_SUCCESS);
S
slguan 已提交
2183 2184
}

2185 2186 2187 2188
/*
 * handle create table response from dnode
 *   if failed, drop the table cached
 */
2189
static void mnodeProcessCreateChildTableRsp(SRpcMsg *rpcMsg) {
S
slguan 已提交
2190 2191
  if (rpcMsg->handle == NULL) return;

2192 2193
  SMnodeMsg *mnodeMsg = rpcMsg->handle;
  mnodeMsg->received++;
S
slguan 已提交
2194

S
Shengliang Guan 已提交
2195 2196
  SChildTableObj *pTable = (SChildTableObj *)mnodeMsg->pTable;
  assert(pTable);
S
Shengliang Guan 已提交
2197

2198 2199 2200
  if (rpcMsg->code == TSDB_CODE_SUCCESS || rpcMsg->code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
    SCMCreateTableMsg *pCreate = mnodeMsg->rpcMsg.pCont;
    if (pCreate->getMeta) {
2201
      mDebug("app:%p:%p, table:%s, created in dnode and continue to get meta, thandle:%p result:%s",
2202 2203 2204 2205 2206 2207
             mnodeMsg->rpcMsg.ahandle, mnodeMsg, pTable->info.tableId, mnodeMsg->rpcMsg.handle,
             tstrerror(rpcMsg->code));

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

      dnodeSendRpcMnodeWriteRsp(mnodeMsg, TSDB_CODE_SUCCESS);
    }
  } else {
S
Shengliang Guan 已提交
2214
    if (mnodeMsg->retry++ < 10) {
2215
      mDebug("app:%p:%p, table:%s, create table rsp received, need retry, times:%d result:%s thandle:%p",
S
Shengliang Guan 已提交
2216
             mnodeMsg->rpcMsg.ahandle, mnodeMsg, pTable->info.tableId, mnodeMsg->retry, tstrerror(rpcMsg->code),
2217 2218
             mnodeMsg->rpcMsg.handle);

2219
      dnodeDelayReprocessMnodeWriteMsg(mnodeMsg);
2220
    } else {
2221 2222 2223
      mError("app:%p:%p, table:%s, failed to create in dnode, result:%s thandle:%p", mnodeMsg->rpcMsg.ahandle, mnodeMsg,
             pTable->info.tableId, tstrerror(rpcMsg->code), mnodeMsg->rpcMsg.handle);

2224
      SSdbOper oper = {.type = SDB_OPER_GLOBAL, .table = tsChildTableSdb, .pObj = pTable};
2225
      sdbDeleteRow(&oper);
2226

2227
      dnodeSendRpcMnodeWriteRsp(mnodeMsg, rpcMsg->code);
S
slguan 已提交
2228 2229 2230 2231
    }
  }
}

2232
static void mnodeProcessAlterTableRsp(SRpcMsg *rpcMsg) {
2233 2234 2235 2236 2237 2238 2239 2240 2241
  if (rpcMsg->handle == NULL) return;

  SMnodeMsg *mnodeMsg = rpcMsg->handle;
  mnodeMsg->received++;

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

  if (rpcMsg->code == TSDB_CODE_SUCCESS || rpcMsg->code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
2242
    mDebug("app:%p:%p, ctable:%s, altered in dnode, thandle:%p result:%s", mnodeMsg->rpcMsg.ahandle, mnodeMsg,
2243 2244 2245 2246 2247
           pTable->info.tableId, mnodeMsg->rpcMsg.handle, tstrerror(rpcMsg->code));

    dnodeSendRpcMnodeWriteRsp(mnodeMsg, TSDB_CODE_SUCCESS);
  } else {
    if (mnodeMsg->retry++ < 3) {
2248
      mDebug("app:%p:%p, table:%s, alter table rsp received, need retry, times:%d result:%s thandle:%p",
2249 2250 2251 2252 2253 2254 2255 2256 2257 2258
             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 已提交
2259 2260
}

2261
static int32_t mnodeProcessMultiTableMetaMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
2262
  SCMMultiTableInfoMsg *pInfo = pMsg->rpcMsg.pCont;
S
slguan 已提交
2263 2264
  pInfo->numOfTables = htonl(pInfo->numOfTables);

2265
  int32_t totalMallocLen = 4 * 1024 * 1024;  // first malloc 4 MB, subsequent reallocation as twice
S
slguan 已提交
2266 2267
  SMultiTableMeta *pMultiMeta = rpcMallocCont(totalMallocLen);
  if (pMultiMeta == NULL) {
2268
    return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
2269 2270 2271 2272 2273
  }

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

2274
  for (int32_t t = 0; t < pInfo->numOfTables; ++t) {
B
Bomin Zhang 已提交
2275
    char * tableId = (char *)(pInfo->tableIds + t * TSDB_TABLE_ID_LEN);
2276
    SChildTableObj *pTable = mnodeGetChildTable(tableId);
S
slguan 已提交
2277 2278
    if (pTable == NULL) continue;

2279
    if (pMsg->pDb == NULL) pMsg->pDb = mnodeGetDbByTableId(tableId);
2280
    if (pMsg->pDb == NULL) {
2281
      mnodeDecTableRef(pTable);
2282 2283
      continue;
    }
S
slguan 已提交
2284 2285

    int availLen = totalMallocLen - pMultiMeta->contLen;
2286
    if (availLen <= sizeof(STableMetaMsg) + sizeof(SSchema) * (TSDB_MAX_TAGS + TSDB_MAX_COLUMNS + 16)) {
2287 2288 2289
      totalMallocLen *= 2;
      pMultiMeta = rpcReallocCont(pMultiMeta, totalMallocLen);
      if (pMultiMeta == NULL) {
2290
        mnodeDecTableRef(pTable);
2291
        return TSDB_CODE_MND_OUT_OF_MEMORY;
2292 2293
      } else {
        t--;
2294
        mnodeDecTableRef(pTable);
2295 2296
        continue;
      }
S
slguan 已提交
2297 2298 2299
    }

    STableMetaMsg *pMeta = (STableMetaMsg *)(pMultiMeta->metas + pMultiMeta->contLen);
2300
    int32_t code = mnodeDoGetChildTableMeta(pMsg, pMeta);
S
slguan 已提交
2301 2302 2303 2304
    if (code == TSDB_CODE_SUCCESS) {
      pMultiMeta->numOfTables ++;
      pMultiMeta->contLen += pMeta->contLen;
    }
2305

2306
    mnodeDecTableRef(pTable);
S
slguan 已提交
2307 2308
  }

2309 2310 2311 2312
  pMsg->rpcRsp.rsp = pMultiMeta;
  pMsg->rpcRsp.len = pMultiMeta->contLen;

  return TSDB_CODE_SUCCESS;
S
slguan 已提交
2313 2314
}

2315 2316
static int32_t mnodeGetShowTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
  SDbObj *pDb = mnodeGetDb(pShow->db);
2317
  if (pDb == NULL) return TSDB_CODE_MND_DB_NOT_SELECTED;
S
slguan 已提交
2318 2319 2320 2321

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

H
Haojun Liao 已提交
2322 2323 2324
  SSchema s = tGetTableNameColumnSchema();
  pShow->bytes[cols] = s.bytes;
  pSchema[cols].type = s.type;
2325
  strcpy(pSchema[cols].name, "table_name");
S
slguan 已提交
2326 2327 2328 2329 2330
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
2331
  strcpy(pSchema[cols].name, "created_time");
S
slguan 已提交
2332 2333 2334 2335 2336 2337 2338 2339 2340
  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 已提交
2341
  SSchema tbCol = tGetTableNameColumnSchema();
H
Hui Li 已提交
2342
  pShow->bytes[cols] = tbCol.bytes + VARSTR_HEADER_SIZE;
H
Haojun Liao 已提交
2343
  pSchema[cols].type = tbCol.type;
2344
  strcpy(pSchema[cols].name, "stable_name");
S
slguan 已提交
2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358
  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];

2359
  mnodeDecDbRef(pDb);
S
slguan 已提交
2360 2361 2362
  return 0;
}

2363 2364
static int32_t mnodeRetrieveShowTables(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
  SDbObj *pDb = mnodeGetDb(pShow->db);
S
slguan 已提交
2365 2366 2367 2368 2369 2370 2371
  if (pDb == NULL) return 0;

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

  char prefix[64] = {0};
S
Shengliang Guan 已提交
2372
  tstrncpy(prefix, pDb->name, 64);
S
slguan 已提交
2373 2374 2375
  strcat(prefix, TS_PATH_DELIMITER);
  int32_t prefixLen = strlen(prefix);

B
Bomin Zhang 已提交
2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386
  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 已提交
2387
  while (numOfRows < rows) {
2388
    pShow->pIter = mnodeGetNextChildTable(pShow->pIter, &pTable);
S
slguan 已提交
2389 2390 2391 2392
    if (pTable == NULL) break;

    // not belong to current db
    if (strncmp(pTable->info.tableId, prefix, prefixLen)) {
2393
      mnodeDecTableRef(pTable);
S
slguan 已提交
2394 2395 2396
      continue;
    }

B
Bomin Zhang 已提交
2397
    char tableName[TSDB_TABLE_NAME_LEN] = {0};
S
slguan 已提交
2398
    
2399
    // pattern compare for table name
S
Shengliang Guan 已提交
2400
    mnodeExtractTableName(pTable->info.tableId, tableName);
S
slguan 已提交
2401

B
Bomin Zhang 已提交
2402
    if (pattern != NULL && patternMatch(pattern, tableName, sizeof(tableName) - 1, &info) != TSDB_PATTERN_MATCH) {
2403
      mnodeDecTableRef(pTable);
S
slguan 已提交
2404 2405 2406 2407 2408 2409
      continue;
    }

    int32_t cols = 0;

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

H
Hui Li 已提交
2411
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, tableName, pShow->bytes[cols]);
S
slguan 已提交
2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427
    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;
2428
    
B
Bomin Zhang 已提交
2429
    memset(tableName, 0, sizeof(tableName));
S
slguan 已提交
2430
    if (pTable->info.type == TSDB_CHILD_TABLE) {
S
Shengliang Guan 已提交
2431
      mnodeExtractTableName(pTable->superTable->info.tableId, tableName);
H
Hui Li 已提交
2432
      STR_WITH_MAXSIZE_TO_VARSTR(pWrite, tableName, pShow->bytes[cols]);
S
slguan 已提交
2433
    }
2434
    
S
slguan 已提交
2435 2436 2437
    cols++;

    numOfRows++;
2438
    mnodeDecTableRef(pTable);
S
slguan 已提交
2439 2440 2441 2442 2443
  }

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

S
Shengliang Guan 已提交
2444
  mnodeVacuumResult(data, NUM_OF_COLUMNS, numOfRows, rows, pShow);
2445
  mnodeDecDbRef(pDb);
B
Bomin Zhang 已提交
2446
  free(pattern);
S
slguan 已提交
2447 2448 2449 2450

  return numOfRows;
}

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

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

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

2467
  if (pMsg->pTable == NULL) pMsg->pTable = mnodeGetTable(pAlter->tableId);
S
[TD-17]  
slguan 已提交
2468
  if (pMsg->pTable == NULL) {
S
Shengliang Guan 已提交
2469
    mError("app:%p:%p, table:%s, failed to alter table, table not exist", pMsg->rpcMsg.ahandle, pMsg, pAlter->tableId);
2470
    return TSDB_CODE_MND_INVALID_TABLE_NAME;
S
[TD-17]  
slguan 已提交
2471 2472
  }

S
slguan 已提交
2473
  pAlter->type = htons(pAlter->type);
2474 2475
  pAlter->numOfCols = htons(pAlter->numOfCols);
  pAlter->tagValLen = htonl(pAlter->tagValLen);
S
slguan 已提交
2476

S
[TD-17]  
slguan 已提交
2477
  if (pAlter->numOfCols > 2) {
S
Shengliang Guan 已提交
2478 2479
    mError("app:%p:%p, table:%s, error numOfCols:%d in alter table", pMsg->rpcMsg.ahandle, pMsg, pAlter->tableId,
           pAlter->numOfCols);
2480
    return TSDB_CODE_MND_APP_ERROR;
S
[TD-17]  
slguan 已提交
2481 2482 2483 2484 2485 2486
  }

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

2487
  int32_t code = TSDB_CODE_COM_OPS_NOT_SUPPORT;
S
[TD-17]  
slguan 已提交
2488
  if (pMsg->pTable->type == TSDB_SUPER_TABLE) {
2489
    mDebug("app:%p:%p, table:%s, start to alter stable", pMsg->rpcMsg.ahandle, pMsg, pAlter->tableId);
S
[TD-17]  
slguan 已提交
2490
    if (pAlter->type == TSDB_ALTER_TABLE_ADD_TAG_COLUMN) {
2491
      code = mnodeAddSuperTableTag(pMsg, pAlter->schema, 1);
S
[TD-17]  
slguan 已提交
2492
    } else if (pAlter->type == TSDB_ALTER_TABLE_DROP_TAG_COLUMN) {
2493
      code = mnodeDropSuperTableTag(pMsg, pAlter->schema[0].name);
S
[TD-17]  
slguan 已提交
2494
    } else if (pAlter->type == TSDB_ALTER_TABLE_CHANGE_TAG_COLUMN) {
2495
      code = mnodeModifySuperTableTagName(pMsg, pAlter->schema[0].name, pAlter->schema[1].name);
S
[TD-17]  
slguan 已提交
2496
    } else if (pAlter->type == TSDB_ALTER_TABLE_ADD_COLUMN) {
2497
      code = mnodeAddSuperTableColumn(pMsg, pAlter->schema, 1);
S
[TD-17]  
slguan 已提交
2498
    } else if (pAlter->type == TSDB_ALTER_TABLE_DROP_COLUMN) {
2499
      code = mnodeDropSuperTableColumn(pMsg, pAlter->schema[0].name);
S
[TD-17]  
slguan 已提交
2500 2501
    } else {
    }
S
slguan 已提交
2502
  } else {
2503
    mDebug("app:%p:%p, table:%s, start to alter ctable", pMsg->rpcMsg.ahandle, pMsg, pAlter->tableId);
S
[TD-17]  
slguan 已提交
2504
    if (pAlter->type == TSDB_ALTER_TABLE_UPDATE_TAG_VAL) {
2505
      return TSDB_CODE_COM_OPS_NOT_SUPPORT;
S
[TD-17]  
slguan 已提交
2506
    } else if (pAlter->type == TSDB_ALTER_TABLE_ADD_COLUMN) {
2507
      code = mnodeAddNormalTableColumn(pMsg, pAlter->schema, 1);
S
[TD-17]  
slguan 已提交
2508
    } else if (pAlter->type == TSDB_ALTER_TABLE_DROP_COLUMN) {
2509
      code = mnodeDropNormalTableColumn(pMsg, pAlter->schema[0].name);
S
[TD-17]  
slguan 已提交
2510 2511
    } else {
    }
S
slguan 已提交
2512 2513
  }

2514
 return code;
S
[TD-17]  
slguan 已提交
2515
}
2516

S
Shengliang Guan 已提交
2517
static int32_t mnodeGetStreamTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
2518
  SDbObj *pDb = mnodeGetDb(pShow->db);
2519
  if (pDb == NULL) return TSDB_CODE_MND_DB_NOT_SELECTED;
2520 2521 2522 2523

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

H
Haojun Liao 已提交
2524 2525 2526
  SSchema tbnameColSchema = tGetTableNameColumnSchema();
  pShow->bytes[cols] = tbnameColSchema.bytes;
  pSchema[cols].type = tbnameColSchema.type;
2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563
  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 已提交
2564
static int32_t mnodeRetrieveStreamTables(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
2565 2566 2567 2568 2569 2570 2571 2572 2573
  SDbObj *pDb = mnodeGetDb(pShow->db);
  if (pDb == NULL) return 0;

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

  char prefix[64] = {0};
S
Shengliang Guan 已提交
2574
  tstrncpy(prefix, pDb->name, 64);
2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587
  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 已提交
2588
    char tableName[TSDB_TABLE_NAME_LEN] = {0};
2589 2590 2591 2592
    
    // pattern compare for table name
    mnodeExtractTableName(pTable->info.tableId, tableName);

B
Bomin Zhang 已提交
2593
    if (pShow->payloadLen > 0 && patternMatch(pShow->payload, tableName, sizeof(tableName) - 1, &info) != TSDB_PATTERN_MATCH) {
2594 2595 2596 2597 2598 2599 2600 2601
      mnodeDecTableRef(pTable);
      continue;
    }

    int32_t cols = 0;

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

H
Hui Li 已提交
2602
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, tableName, pShow->bytes[cols]);
2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613
    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 已提交
2614
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, pTable->sql, pShow->bytes[cols]);    
2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627
    cols++;

    numOfRows++;
    mnodeDecTableRef(pTable);
  }

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

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

  return numOfRows;
2628
}