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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  return TSDB_CODE_SUCCESS;
}

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

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

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

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

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

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

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

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

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

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

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

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

233 234
  pOper->rowSize = len;

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

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

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

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

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

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

  pOper->pObj = pTable;
  return TSDB_CODE_SUCCESS;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  return TSDB_CODE_SUCCESS;
}

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

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

443
static int32_t mnodeSuperTableActionUpdate(SSdbOper *pOper) {
S
slguan 已提交
444
  SSuperTableObj *pNew = pOper->pObj;
445
  SSuperTableObj *pTable = mnodeGetSuperTable(pNew->info.tableId);
S
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
    free(oldSchema);
S
Shengliang Guan 已提交
460 461
  
    mnodeDecTableRef(pTable);
S
slguan 已提交
462
  }
S
Shengliang Guan 已提交
463

S
slguan 已提交
464 465 466
  return TSDB_CODE_SUCCESS;
}

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

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

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

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

  return TSDB_CODE_SUCCESS;
}

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

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

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

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

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

  return TSDB_CODE_SUCCESS;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

617 618 619
  return NULL;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

783
static int32_t mnodeProcessCreateSuperTableMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
784 785
  if (pMsg == NULL) return TSDB_CODE_MND_APP_ERROR;

S
Shengliang Guan 已提交
786
  SCMCreateTableMsg *pCreate = pMsg->rpcMsg.pCont;
S
Shengliang Guan 已提交
787
  SSuperTableObj *   pStable = calloc(1, sizeof(SSuperTableObj));
S
slguan 已提交
788
  if (pStable == NULL) {
S
Shengliang Guan 已提交
789
    mError("app:%p:%p, table:%s, failed to create, no enough memory", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId);
790
    return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
791 792
  }

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

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

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

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

  return code;
S
slguan 已提交
841 842
}

843
static int32_t mnodeProcessDropSuperTableMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
844 845
  if (pMsg == NULL) return TSDB_CODE_MND_APP_ERROR;

S
slguan 已提交
846
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
S
slguan 已提交
847
  if (pStable->numOfTables != 0) {
848 849 850
    SHashMutableIterator *pIter = taosHashCreateIter(pStable->vgHash);
    while (taosHashIterNext(pIter)) {
      int32_t *pVgId = taosHashIterGet(pIter);
851
      SVgObj *pVgroup = mnodeGetVgroup(*pVgId);
guanshengliang's avatar
guanshengliang 已提交
852
      if (pVgroup == NULL) break;
853

S
slguan 已提交
854
      SMDDropSTableMsg *pDrop = rpcMallocCont(sizeof(SMDDropSTableMsg));
guanshengliang's avatar
guanshengliang 已提交
855
      pDrop->contLen = htonl(sizeof(SMDDropSTableMsg));
856
      pDrop->vgId = htonl(pVgroup->vgId);
S
slguan 已提交
857
      pDrop->uid = htobe64(pStable->uid);
S
Shengliang Guan 已提交
858
      mnodeExtractTableName(pStable->info.tableId, pDrop->tableId);
S
Shengliang Guan 已提交
859 860 861

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

869
    mnodeDropAllChildTablesInStable(pStable);
guanshengliang's avatar
guanshengliang 已提交
870 871 872 873 874
  } 
  
  SSdbOper oper = {
    .type = SDB_OPER_GLOBAL,
    .table = tsSuperTableSdb,
875 876
    .pObj = pStable,
    .pMsg = pMsg
guanshengliang's avatar
guanshengliang 已提交
877 878 879
  };
  
  int32_t code = sdbDeleteRow(&oper);
880
  if (code == TSDB_CODE_SUCCESS) {
881
    mLPrint("stable:%s, is dropped from sdb, result:%s", pStable->info.tableId, tstrerror(code));
S
Shengliang Guan 已提交
882
    code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
883 884
  }

885
  return code;
S
slguan 已提交
886 887
}

888
static int32_t mnodeFindSuperTableTagIndex(SSuperTableObj *pStable, const char *tagName) {
guanshengliang's avatar
guanshengliang 已提交
889 890 891 892
  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 已提交
893 894 895 896 897 898
    }
  }

  return -1;
}

899 900 901 902 903 904 905 906 907 908
static int32_t mnodeAddSuperTableTagCb(SMnodeMsg *pMsg, int32_t code) {
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
  mLPrint("app:%p:%p, stable %s, add tag result:%s", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
          tstrerror(code));

  return code;
}

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

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

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

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

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

S
slguan 已提交
933
  SSchema *tschema = (SSchema *)(pStable->schema + pStable->numOfColumns + pStable->numOfTags);
S
slguan 已提交
934 935 936 937
  for (int32_t i = 0; i < ntags; i++) {
    tschema[i].colId = pStable->nextColId++;
  }

S
slguan 已提交
938
  pStable->numOfTags += ntags;
S
TD-355  
Shengliang Guan 已提交
939
  pStable->tversion++;
S
slguan 已提交
940

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

S
slguan 已提交
944
  SSdbOper oper = {
S
slguan 已提交
945
    .type = SDB_OPER_GLOBAL,
S
[TD-17]  
slguan 已提交
946
    .table = tsSuperTableSdb,
947
    .pObj = pStable,
948 949
    .pMsg = pMsg,
    .cb = mnodeAddSuperTableTagCb
S
[TD-17]  
slguan 已提交
950
  };
S
slguan 已提交
951

S
[TD-17]  
slguan 已提交
952
  int32_t code = sdbUpdateRow(&oper);
953 954
  if (code == TSDB_CODE_SUCCESS) {
    code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
[TD-17]  
slguan 已提交
955
  }
S
Shengliang Guan 已提交
956

957
  return code;
S
slguan 已提交
958 959
}

960 961 962 963 964 965 966 967 968
static int32_t mnodeDropSuperTableTagCb(SMnodeMsg *pMsg, int32_t code) {
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
  mLPrint("app:%p:%p, stable %s, drop tag result:%s", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
          tstrerror(code));
  return code;
}

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

S
slguan 已提交
976 977
  memmove(pStable->schema + pStable->numOfColumns + col, pStable->schema + pStable->numOfColumns + col + 1,
          sizeof(SSchema) * (pStable->numOfTags - col - 1));
S
slguan 已提交
978
  pStable->numOfTags--;
S
TD-355  
Shengliang Guan 已提交
979
  pStable->tversion++;
S
slguan 已提交
980

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

S
slguan 已提交
983
  SSdbOper oper = {
S
slguan 已提交
984
    .type = SDB_OPER_GLOBAL,
S
[TD-17]  
slguan 已提交
985
    .table = tsSuperTableSdb,
986
    .pObj = pStable,
987 988
    .pMsg = pMsg,
    .cb = mnodeDropSuperTableTagCb
S
[TD-17]  
slguan 已提交
989
  };
S
slguan 已提交
990

S
[TD-17]  
slguan 已提交
991
  int32_t code = sdbUpdateRow(&oper);
992 993
  if (code == TSDB_CODE_SUCCESS) {
    code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
[TD-17]  
slguan 已提交
994
  }
995 996

  return code;
S
slguan 已提交
997 998
}

999 1000 1001 1002 1003 1004 1005 1006 1007
static int32_t mnodeModifySuperTableTagNameCb(SMnodeMsg *pMsg, int32_t code) {
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
  mLPrint("app:%p:%p, stable %s, modify tag result:%s", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
         tstrerror(code));
  return code;
}

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

  // int32_t  rowSize = 0;
  uint32_t len = strlen(newTagName);
S
slguan 已提交
1017
  if (len >= TSDB_COL_NAME_LEN) {
1018
    return TSDB_CODE_MND_COL_NAME_TOO_LONG;
S
slguan 已提交
1019
  }
S
slguan 已提交
1020

1021
  if (mnodeFindSuperTableTagIndex(pStable, newTagName) >= 0) {
1022
    return TSDB_CODE_MND_TAG_ALREAY_EXIST;
S
slguan 已提交
1023
  }
1024
  
S
slguan 已提交
1025
  // update
S
slguan 已提交
1026
  SSchema *schema = (SSchema *) (pStable->schema + pStable->numOfColumns + col);
B
Bomin Zhang 已提交
1027
  tstrncpy(schema->name, newTagName, sizeof(schema->name));
S
slguan 已提交
1028

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

S
slguan 已提交
1032
  SSdbOper oper = {
S
slguan 已提交
1033
    .type = SDB_OPER_GLOBAL,
S
[TD-17]  
slguan 已提交
1034
    .table = tsSuperTableSdb,
1035
    .pObj = pStable,
1036 1037
    .pMsg = pMsg,
    .cb = mnodeModifySuperTableTagNameCb
S
[TD-17]  
slguan 已提交
1038
  };
S
slguan 已提交
1039

S
[TD-17]  
slguan 已提交
1040
  int32_t code = sdbUpdateRow(&oper);
1041 1042
  if (code == TSDB_CODE_SUCCESS) {
    code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
slguan 已提交
1043
  }
1044 1045

  return code;
S
slguan 已提交
1046 1047
}

1048
static int32_t mnodeFindSuperTableColumnIndex(SSuperTableObj *pStable, char *colName) {
S
slguan 已提交
1049
  SSchema *schema = (SSchema *) pStable->schema;
S
slguan 已提交
1050 1051 1052
  for (int32_t col = 0; col < pStable->numOfColumns; col++) {
    if (strcasecmp(schema[col].name, colName) == 0) {
      return col;
S
slguan 已提交
1053 1054 1055 1056 1057 1058
    }
  }

  return -1;
}

1059 1060 1061 1062 1063 1064 1065 1066
static int32_t mnodeAddSuperTableColumnCb(SMnodeMsg *pMsg, int32_t code) {
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
  mLPrint("app:%p:%p, stable %s, add column result:%s", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
          tstrerror(code));
  return code;
}

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

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

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

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

S
slguan 已提交
1091 1092 1093
  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 已提交
1094

L
liu0x54 已提交
1095
  SSchema *tschema = (SSchema *) (pStable->schema + pStable->numOfColumns);
S
slguan 已提交
1096 1097 1098 1099 1100 1101 1102
  for (int32_t i = 0; i < ncols; i++) {
    tschema[i].colId = pStable->nextColId++;
  }

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

1103
  SAcctObj *pAcct = mnodeGetAcct(pDb->acct);
S
[TD-17]  
slguan 已提交
1104 1105
  if (pAcct != NULL) {
    pAcct->acctInfo.numOfTimeSeries += (ncols * pStable->numOfTables);
1106
    mnodeDecAcctRef(pAcct);
S
[TD-17]  
slguan 已提交
1107
  }
S
slguan 已提交
1108

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

S
slguan 已提交
1111
  SSdbOper oper = {
S
slguan 已提交
1112
    .type = SDB_OPER_GLOBAL,
S
[TD-17]  
slguan 已提交
1113
    .table = tsSuperTableSdb,
1114
    .pObj = pStable,
1115 1116
    .pMsg = pMsg,
    .cb = mnodeAddSuperTableColumnCb
S
[TD-17]  
slguan 已提交
1117 1118 1119
  };

  int32_t code = sdbUpdateRow(&oper);
1120 1121
  if (code == TSDB_CODE_SUCCESS) {
    code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
[TD-17]  
slguan 已提交
1122
  }
1123 1124

  return code;
S
slguan 已提交
1125 1126
}

1127 1128 1129 1130 1131 1132 1133 1134
static int32_t mnodeDropSuperTableColumnCb(SMnodeMsg *pMsg, int32_t code) {
  SSuperTableObj *pStable = (SSuperTableObj *)pMsg->pTable;
  mLPrint("app:%p:%p, stable %s, delete column result:%s", pMsg->rpcMsg.ahandle, pMsg, pStable->info.tableId,
         tstrerror(code));
  return code;
}

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

S
slguan 已提交
1144
  memmove(pStable->schema + col, pStable->schema + col + 1,
S
slguan 已提交
1145 1146 1147 1148 1149 1150 1151 1152
          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);

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

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

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

  int32_t code = sdbUpdateRow(&oper);
1170 1171
  if (code == TSDB_CODE_SUCCESS) {
    code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
[TD-17]  
slguan 已提交
1172
  }
S
Shengliang Guan 已提交
1173

1174
  return code;
S
slguan 已提交
1175 1176
}

S
slguan 已提交
1177
// show super tables
1178 1179
static int32_t mnodeGetShowSuperTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
  SDbObj *pDb = mnodeGetDb(pShow->db);
1180
  if (pDb == NULL) return TSDB_CODE_MND_DB_NOT_SELECTED;
S
slguan 已提交
1181 1182 1183 1184

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

H
Haojun Liao 已提交
1185 1186 1187
  SSchema tbnameSchema = tGetTableNameColumnSchema();
  pShow->bytes[cols] = tbnameSchema.bytes;
  pSchema[cols].type = tbnameSchema.type;
S
slguan 已提交
1188 1189 1190 1191 1192 1193
  strcpy(pSchema[cols].name, "name");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

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

1225
  mnodeDecDbRef(pDb);
S
slguan 已提交
1226 1227 1228
  return 0;
}

S
slguan 已提交
1229
// retrieve super tables
1230
int32_t mnodeRetrieveShowSuperTables(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
S
slguan 已提交
1231 1232 1233 1234 1235 1236 1237
  int32_t         numOfRows = 0;
  char *          pWrite;
  int32_t         cols = 0;
  SSuperTableObj *pTable = NULL;
  char            prefix[20] = {0};
  int32_t         prefixLen;

1238
  SDbObj *pDb = mnodeGetDb(pShow->db);
S
slguan 已提交
1239 1240 1241 1242 1243 1244 1245
  if (pDb == NULL) return 0;

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

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

S
slguan 已提交
1248
  while (numOfRows < rows) {    
1249
    pShow->pIter = mnodeGetNextSuperTable(pShow->pIter, &pTable);
S
slguan 已提交
1250 1251
    if (pTable == NULL) break;
    if (strncmp(pTable->info.tableId, prefix, prefixLen)) {
1252
      mnodeDecTableRef(pTable);
S
slguan 已提交
1253 1254 1255 1256
      continue;
    }

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

B
Bomin Zhang 已提交
1259
    if (pShow->payloadLen > 0 && patternMatch(pShow->payload, stableName, sizeof(stableName) - 1, &info) != TSDB_PATTERN_MATCH) {
1260
      mnodeDecTableRef(pTable);
S
slguan 已提交
1261
      continue;
S
Shengliang Guan 已提交
1262
    }
S
slguan 已提交
1263 1264 1265 1266

    cols = 0;

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

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

S
slguan 已提交
1298 1299 1300
  return numOfRows;
}

1301
void mnodeDropAllSuperTables(SDbObj *pDropDb) {
S
Shengliang Guan 已提交
1302
  void *  pIter= NULL;
S
slguan 已提交
1303 1304 1305 1306
  int32_t numOfTables = 0;
  int32_t dbNameLen = strlen(pDropDb->name);
  SSuperTableObj *pTable = NULL;

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

S
slguan 已提交
1309
  while (1) {
1310
    pIter = mnodeGetNextSuperTable(pIter, &pTable);
S
slguan 已提交
1311
    if (pTable == NULL) break;
S
slguan 已提交
1312 1313

    if (strncmp(pDropDb->name, pTable->info.tableId, dbNameLen) == 0) {
S
slguan 已提交
1314
      SSdbOper oper = {
S
slguan 已提交
1315
        .type = SDB_OPER_LOCAL,
S
slguan 已提交
1316 1317 1318 1319 1320 1321
        .table = tsSuperTableSdb,
        .pObj = pTable,
      };
      sdbDeleteRow(&oper);
      numOfTables ++;
    }
S
slguan 已提交
1322

1323
    mnodeDecTableRef(pTable);
S
slguan 已提交
1324
  }
S
slguan 已提交
1325

S
Shengliang Guan 已提交
1326 1327
  sdbFreeIter(pIter);

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

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

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

B
Bomin Zhang 已提交
1359
  pMsg->rpcRsp.len = pMeta->contLen;
S
slguan 已提交
1360 1361
  pMeta->contLen = htons(pMeta->contLen);

1362
  pMsg->rpcRsp.rsp = pMeta;
S
Shengliang Guan 已提交
1363 1364 1365

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

1369
static int32_t mnodeProcessSuperTableVgroupMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
1370
  SCMSTableVgroupMsg *pInfo = pMsg->rpcMsg.pCont;
H
hjxilinx 已提交
1371
  int32_t numOfTable = htonl(pInfo->numOfTables);
1372

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

S
scripts  
Shengliang Guan 已提交
1384
  SCMSTableVgroupRspMsg *pRsp = rpcMallocCont(contLen);
S
slguan 已提交
1385
  if (pRsp == NULL) {
1386
    return TSDB_CODE_MND_OUT_OF_MEMORY;
1387
  }
1388

1389
  pRsp->numOfTables = 0;
1390
  char *msg = (char *)pRsp + sizeof(SCMSTableVgroupRspMsg);
1391 1392

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

1405 1406
      // even this super table has no corresponding table, still return
      pRsp->numOfTables++;
1407

1408 1409 1410 1411 1412 1413
      SVgroupsInfo *pVgroupInfo = (SVgroupsInfo *)msg;
      pVgroupInfo->numOfVgroups = 0;
      
      msg += sizeof(SVgroupsInfo);
    } else {
      SVgroupsInfo *pVgroupInfo = (SVgroupsInfo *)msg;
1414

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

1422 1423 1424 1425
        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;
1426

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

1430 1431
          pVgroupInfo->vgroups[vgSize].numOfIps++;
        }
1432

1433 1434 1435
        vgSize++;
        mnodeDecVgroupRef(pVgroup);
      }
1436

1437 1438 1439 1440
      taosHashDestroyIter(pIter);
      mnodeDecTableRef(pTable);

      pVgroupInfo->numOfVgroups = htonl(vgSize);
1441

1442 1443 1444 1445
      // one table is done, try the next table
      msg += sizeof(SVgroupsInfo) + vgSize * sizeof(SCMVgroupInfo);
      pRsp->numOfTables++;
    }
1446
  }
S
slguan 已提交
1447

1448 1449
  if (pRsp->numOfTables != numOfTable) {
    rpcFreeCont(pRsp);
1450
    return TSDB_CODE_MND_INVALID_TABLE_NAME;
1451 1452 1453 1454
  } else {
    pRsp->numOfTables = htonl(pRsp->numOfTables);
    pMsg->rpcRsp.rsp = pRsp;
    pMsg->rpcRsp.len = msg - (char *)pRsp;
1455

1456 1457
    return TSDB_CODE_SUCCESS;
  }
S
slguan 已提交
1458 1459
}

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

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

  SMDCreateTableMsg *pCreate = rpcMallocCont(contLen);
  if (pCreate == NULL) {
1484
    terrno = TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
1485 1486 1487
    return NULL;
  }

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

S
Shengliang Guan 已提交
1530
  if (pTable->info.type == TSDB_STREAM_TABLE) {
1531
    memcpy(pCreate->data + totalCols * sizeof(SSchema), pTable->sql, pTable->sqlLen);
S
slguan 已提交
1532 1533 1534 1535 1536
  }

  return pCreate;
}

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

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

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

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

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

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

1590 1591 1592 1593
    pTable->suid = pSuperTable->uid;
    pTable->uid  = (((uint64_t)pTable->vgId) << 40) + ((((uint64_t)pTable->sid) & ((1ul << 24) - 1ul)) << 16) +
                  (sdbGetVersion() & ((1ul << 16) - 1ul));
    pTable->superTable = pSuperTable;
S
slguan 已提交
1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604
  } 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);
1605
      return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620
    }
    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);
1621
        return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
1622 1623 1624
      }
      memcpy(pTable->sql, (char *) (pCreate->schema) + numOfCols * sizeof(SSchema), pTable->sqlLen);
      pTable->sql[pTable->sqlLen - 1] = 0;
S
Shengliang Guan 已提交
1625 1626
      mTrace("app:%p:%p, table:%s, stream sql len:%d sql:%s", pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId,
             pTable->sqlLen, pTable->sql);
S
slguan 已提交
1627 1628
    }
  }
1629 1630 1631 1632

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

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

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

1663
  SVgObj *pVgroup = mnodeGetAvailableVgroup(pMsg->pDb);
S
slguan 已提交
1664
  if (pVgroup == NULL) {
1665
    mTrace("app:%p:%p, table:%s, start to create a new vgroup", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId);
1666
    return mnodeCreateVgroup(pMsg, pMsg->pDb);
S
slguan 已提交
1667 1668
  }

1669
  if (pMsg->retry == 0) {
1670
    if (pMsg->pTable == NULL) {
1671 1672
      int32_t sid = taosAllocateId(pVgroup->idPool);
      if (sid <= 0) {
1673 1674
        mTrace("app:%p:%p, table:%s, no enough sid in vgId:%d", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId,
               pVgroup->vgId);
1675
        return mnodeCreateVgroup(pMsg, pMsg->pDb);
1676 1677
      }

1678 1679 1680 1681
      if (pMsg->pVgroup == NULL) {
        pMsg->pVgroup = pVgroup;
        mnodeIncVgroupRef(pVgroup);
      }
1682

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

1686 1687 1688 1689 1690 1691
      code = mnodeDoCreateChildTable(pMsg, sid);
      if (code != TSDB_CODE_SUCCESS) {
        return code;
      } else {
        return TSDB_CODE_MND_ACTION_IN_PROGRESS;
      }
1692
    }
1693
  } else {
1694
    if (pMsg->pTable == NULL) pMsg->pTable = mnodeGetTable(pCreate->tableId);
1695
  }
1696

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

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

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

S
Shengliang Guan 已提交
1723
  tstrncpy(pDrop->tableId, pTable->info.tableId, TSDB_TABLE_ID_LEN);
S
slguan 已提交
1724 1725 1726 1727 1728
  pDrop->vgId    = htonl(pTable->vgId);
  pDrop->contLen = htonl(sizeof(SMDDropTableMsg));
  pDrop->sid     = htonl(pTable->sid);
  pDrop->uid     = htobe64(pTable->uid);

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

S
Shengliang Guan 已提交
1731
  mPrint("app:%p:%p, table:%s, send drop ctable msg", pMsg->rpcMsg.ahandle, pMsg, pDrop->tableId);
S
slguan 已提交
1732
  SRpcMsg rpcMsg = {
1733
    .handle  = pMsg,
S
slguan 已提交
1734 1735 1736 1737 1738 1739
    .pCont   = pDrop,
    .contLen = sizeof(SMDDropTableMsg),
    .code    = 0,
    .msgType = TSDB_MSG_TYPE_MD_DROP_TABLE
  };

J
jtao1735 已提交
1740
  dnodeSendMsgToDnode(&ipSet, &rpcMsg);
1741

1742
  return TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
slguan 已提交
1743 1744
}

1745
static int32_t mnodeFindNormalTableColumnIndex(SChildTableObj *pTable, char *colName) {
S
slguan 已提交
1746
  SSchema *schema = (SSchema *) pTable->schema;
S
slguan 已提交
1747 1748 1749
  for (int32_t col = 0; col < pTable->numOfColumns; col++) {
    if (strcasecmp(schema[col].name, colName) == 0) {
      return col;
S
slguan 已提交
1750 1751 1752 1753 1754 1755
    }
  }

  return -1;
}

1756
static int32_t mnodeAddNormalTableColumnCb(SMnodeMsg *pMsg, int32_t code) {
1757
  SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;
1758
  mLPrint("app:%p:%p, ctable %s, add column result:%s", pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId,
1759
          tstrerror(code));
1760 1761 1762 1763 1764
  return code;
}

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

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

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

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

S
slguan 已提交
1784
  SSchema *tschema = (SSchema *) (pTable->schema + pTable->numOfColumns);
S
slguan 已提交
1785 1786 1787 1788 1789 1790
  for (int32_t i = 0; i < ncols; i++) {
    tschema[i].colId = pTable->nextColId++;
  }

  pTable->numOfColumns += ncols;
  pTable->sversion++;
S
[TD-17]  
slguan 已提交
1791
  
1792
  SAcctObj *pAcct = mnodeGetAcct(pDb->acct);
S
[TD-17]  
slguan 已提交
1793
  if (pAcct != NULL) {
S
slguan 已提交
1794
    pAcct->acctInfo.numOfTimeSeries += ncols;
1795
    mnodeDecAcctRef(pAcct);
S
[TD-17]  
slguan 已提交
1796
  }
1797 1798 1799

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

S
slguan 已提交
1800
  SSdbOper oper = {
S
slguan 已提交
1801
    .type = SDB_OPER_GLOBAL,
S
[TD-17]  
slguan 已提交
1802
    .table = tsChildTableSdb,
1803
    .pObj = pTable,
1804 1805
    .pMsg = pMsg,
    .cb = mnodeAddNormalTableColumnCb
S
[TD-17]  
slguan 已提交
1806
  };
S
slguan 已提交
1807

S
[TD-17]  
slguan 已提交
1808
  int32_t code = sdbUpdateRow(&oper);
1809 1810
  if (code == TSDB_CODE_SUCCESS) {
    return TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
[TD-17]  
slguan 已提交
1811
  }
S
Shengliang Guan 已提交
1812

1813
  return code;
S
slguan 已提交
1814 1815
}

1816
static int32_t mnodeDropNormalTableColumnCb(SMnodeMsg *pMsg, int32_t code) {
1817
  SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;
1818 1819 1820 1821 1822 1823
  mLPrint("app:%p:%p, ctable %s, drop column result:%s", pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId,
          tstrerror(code));
  return code;
}

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

S
slguan 已提交
1833
  memmove(pTable->schema + col, pTable->schema + col + 1, sizeof(SSchema) * (pTable->numOfColumns - col - 1));
S
slguan 已提交
1834 1835 1836
  pTable->numOfColumns--;
  pTable->sversion++;

1837
  SAcctObj *pAcct = mnodeGetAcct(pDb->acct);
S
[TD-17]  
slguan 已提交
1838 1839
  if (pAcct != NULL) {
    pAcct->acctInfo.numOfTimeSeries--;
1840
    mnodeDecAcctRef(pAcct);
S
[TD-17]  
slguan 已提交
1841
  }
1842 1843 1844

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

S
slguan 已提交
1845
  SSdbOper oper = {
S
slguan 已提交
1846
    .type = SDB_OPER_GLOBAL,
S
[TD-17]  
slguan 已提交
1847
    .table = tsChildTableSdb,
1848
    .pObj = pTable,
1849 1850
    .pMsg = pMsg,
    .cb = mnodeDropNormalTableColumnCb
S
[TD-17]  
slguan 已提交
1851
  };
S
slguan 已提交
1852

S
[TD-17]  
slguan 已提交
1853
  int32_t code = sdbUpdateRow(&oper);
1854
  if (code == TSDB_CODE_SUCCESS) {
1855
    return TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
[TD-17]  
slguan 已提交
1856
  }
S
Shengliang Guan 已提交
1857

1858
  return code;
S
slguan 已提交
1859 1860
}

1861
static int32_t mnodeSetSchemaFromNormalTable(SSchema *pSchema, SChildTableObj *pTable) {
S
slguan 已提交
1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873
  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);
}

1874
static int32_t mnodeDoGetChildTableMeta(SMnodeMsg *pMsg, STableMetaMsg *pMeta) {
S
slguan 已提交
1875 1876 1877
  SDbObj *pDb = pMsg->pDb;
  SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable;

S
slguan 已提交
1878 1879 1880 1881
  pMeta->uid       = htobe64(pTable->uid);
  pMeta->sid       = htonl(pTable->sid);
  pMeta->precision = pDb->cfg.precision;
  pMeta->tableType = pTable->info.type;
S
Shengliang Guan 已提交
1882
  tstrncpy(pMeta->tableId, pTable->info.tableId, TSDB_TABLE_ID_LEN);
S
slguan 已提交
1883 1884 1885

  if (pTable->info.type == TSDB_CHILD_TABLE) {
    pMeta->sversion     = htons(pTable->superTable->sversion);
1886
    pMeta->tversion     = htons(pTable->superTable->tversion);
S
slguan 已提交
1887
    pMeta->numOfTags    = (int8_t)pTable->superTable->numOfTags;
S
slguan 已提交
1888
    pMeta->numOfColumns = htons((int16_t)pTable->superTable->numOfColumns);
1889
    pMeta->contLen      = sizeof(STableMetaMsg) + mnodeSetSchemaFromSuperTable(pMeta->schema, pTable->superTable);
S
slguan 已提交
1890 1891
  } else {
    pMeta->sversion     = htons(pTable->sversion);
1892
    pMeta->tversion     = 0;
S
slguan 已提交
1893 1894
    pMeta->numOfTags    = 0;
    pMeta->numOfColumns = htons((int16_t)pTable->numOfColumns);
1895
    pMeta->contLen      = sizeof(STableMetaMsg) + mnodeSetSchemaFromNormalTable(pMeta->schema, pTable); 
S
slguan 已提交
1896
  }
S
Shengliang Guan 已提交
1897

1898
  if (pMsg->pVgroup == NULL) pMsg->pVgroup = mnodeGetVgroup(pTable->vgId);
1899
  if (pMsg->pVgroup == NULL) {
S
Shengliang Guan 已提交
1900 1901
    mError("app:%p:%p, table:%s, failed to get table meta, vgroup not exist", pMsg->rpcMsg.ahandle, pMsg,
           pTable->info.tableId);
1902
    return TSDB_CODE_MND_VGROUP_NOT_EXIST;
S
slguan 已提交
1903 1904
  }

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

S
Shengliang Guan 已提交
1915 1916
  mTrace("app:%p:%p, table:%s, uid:%" PRIu64 " table meta is retrieved", pMsg->rpcMsg.ahandle, pMsg,
         pTable->info.tableId, pTable->uid);
S
slguan 已提交
1917 1918 1919 1920

  return TSDB_CODE_SUCCESS;
}

S
Shengliang Guan 已提交
1921
static int32_t mnodeAutoCreateChildTable(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
1922
  SCMTableInfoMsg *pInfo = pMsg->rpcMsg.pCont;
1923
  STagData *pTag = (STagData *)pInfo->tags;
1924

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

1933 1934
  size_t size = sizeof(pInfo->tableId);
  tstrncpy(pCreateMsg->tableId, pInfo->tableId, size);
B
Bomin Zhang 已提交
1935
  tstrncpy(pCreateMsg->db, pMsg->pDb->name, sizeof(pCreateMsg->db));
S
slguan 已提交
1936 1937
  pCreateMsg->igExists = 1;
  pCreateMsg->getMeta = 1;
S
slguan 已提交
1938
  pCreateMsg->contLen = htonl(contLen);
B
Bomin Zhang 已提交
1939

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

S
Shengliang Guan 已提交
1944 1945 1946 1947
  rpcFreeCont(pMsg->rpcMsg.pCont);
  pMsg->rpcMsg.msgType = TSDB_MSG_TYPE_CM_CREATE_TABLE;
  pMsg->rpcMsg.pCont = pCreateMsg;
  pMsg->rpcMsg.contLen = contLen;
1948
  
1949
  return TSDB_CODE_MND_ACTION_NEED_REPROCESSED;
S
slguan 已提交
1950
}
S
slguan 已提交
1951

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

1961
  mnodeDoGetChildTableMeta(pMsg, pMeta);
S
slguan 已提交
1962

1963 1964
  pMsg->rpcRsp.len = pMeta->contLen;
  pMsg->rpcRsp.rsp = pMeta;
S
slguan 已提交
1965
  pMeta->contLen = htons(pMeta->contLen);
1966 1967

  return TSDB_CODE_SUCCESS;
S
slguan 已提交
1968 1969
}

1970
void mnodeDropAllChildTablesInVgroups(SVgObj *pVgroup) {
1971 1972 1973 1974 1975 1976 1977
  void *  pIter = NULL;
  int32_t numOfTables = 0;
  SChildTableObj *pTable = NULL;

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

  while (1) {
1978
    pIter = mnodeGetNextChildTable(pIter, &pTable);
1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989
    if (pTable == NULL) break;

    if (pTable->vgId == pVgroup->vgId) {
      SSdbOper oper = {
        .type = SDB_OPER_LOCAL,
        .table = tsChildTableSdb,
        .pObj = pTable,
      };
      sdbDeleteRow(&oper);
      numOfTables++;
    }
1990
    mnodeDecTableRef(pTable);
1991 1992 1993 1994 1995 1996 1997
  }

  sdbFreeIter(pIter);

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

1998
void mnodeDropAllChildTables(SDbObj *pDropDb) {
S
Shengliang Guan 已提交
1999
  void *  pIter = NULL;
S
slguan 已提交
2000 2001 2002 2003
  int32_t numOfTables = 0;
  int32_t dbNameLen = strlen(pDropDb->name);
  SChildTableObj *pTable = NULL;

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

S
slguan 已提交
2006
  while (1) {
2007
    pIter = mnodeGetNextChildTable(pIter, &pTable);
S
slguan 已提交
2008
    if (pTable == NULL) break;
S
slguan 已提交
2009 2010

    if (strncmp(pDropDb->name, pTable->info.tableId, dbNameLen) == 0) {
S
slguan 已提交
2011
      SSdbOper oper = {
S
slguan 已提交
2012
        .type = SDB_OPER_LOCAL,
S
slguan 已提交
2013 2014 2015 2016 2017 2018
        .table = tsChildTableSdb,
        .pObj = pTable,
      };
      sdbDeleteRow(&oper);
      numOfTables++;
    }
2019
    mnodeDecTableRef(pTable);
S
slguan 已提交
2020 2021
  }

S
Shengliang Guan 已提交
2022 2023
  sdbFreeIter(pIter);

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

2027
static void mnodeDropAllChildTablesInStable(SSuperTableObj *pStable) {
S
Shengliang Guan 已提交
2028
  void *  pIter = NULL;
S
slguan 已提交
2029 2030 2031
  int32_t numOfTables = 0;
  SChildTableObj *pTable = NULL;

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

S
slguan 已提交
2034
  while (1) {
2035
    pIter = mnodeGetNextChildTable(pIter, &pTable);
S
slguan 已提交
2036
    if (pTable == NULL) break;
S
slguan 已提交
2037 2038

    if (pTable->superTable == pStable) {
S
slguan 已提交
2039
      SSdbOper oper = {
S
slguan 已提交
2040
        .type = SDB_OPER_LOCAL,
S
slguan 已提交
2041 2042 2043 2044 2045 2046
        .table = tsChildTableSdb,
        .pObj = pTable,
      };
      sdbDeleteRow(&oper);
      numOfTables++;
    }
S
slguan 已提交
2047

2048
    mnodeDecTableRef(pTable);
S
slguan 已提交
2049 2050
  }

S
Shengliang Guan 已提交
2051 2052
  sdbFreeIter(pIter);

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

2056 2057
static SChildTableObj* mnodeGetTableByPos(int32_t vnode, int32_t sid) {
  SVgObj *pVgroup = mnodeGetVgroup(vnode);
2058
  if (pVgroup == NULL) return NULL;
S
slguan 已提交
2059

S
slguan 已提交
2060
  SChildTableObj *pTable = pVgroup->tableList[sid - 1];
2061
  mnodeIncTableRef((STableObj *)pTable);
2062

2063
  mnodeDecVgroupRef(pVgroup);
S
slguan 已提交
2064 2065 2066
  return pTable;
}

2067
static int32_t mnodeProcessTableCfgMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
2068
  SDMConfigTableMsg *pCfg = pMsg->rpcMsg.pCont;
S
Shengliang Guan 已提交
2069 2070 2071
  pCfg->dnodeId = htonl(pCfg->dnodeId);
  pCfg->vgId = htonl(pCfg->vgId);
  pCfg->sid = htonl(pCfg->sid);
S
Shengliang Guan 已提交
2072 2073
  mTrace("app:%p:%p, dnode:%d, vgId:%d sid:%d, receive table config msg", pMsg->rpcMsg.ahandle, pMsg, pCfg->dnodeId,
         pCfg->vgId, pCfg->sid);
S
slguan 已提交
2074

S
Shengliang Guan 已提交
2075
  SChildTableObj *pTable = mnodeGetTableByPos(pCfg->vgId, pCfg->sid);
S
slguan 已提交
2076
  if (pTable == NULL) {
S
Shengliang Guan 已提交
2077 2078
    mError("app:%p:%p, dnode:%d, vgId:%d sid:%d, table not found", pMsg->rpcMsg.ahandle, pMsg, pCfg->dnodeId,
           pCfg->vgId, pCfg->sid);
2079
    return TSDB_CODE_MND_INVALID_TABLE_ID;
S
slguan 已提交
2080 2081
  }

S
Shengliang Guan 已提交
2082 2083
  SMDCreateTableMsg *pCreate = NULL;
  pCreate = mnodeBuildCreateChildTableMsg(NULL, (SChildTableObj *)pTable);
2084
  mnodeDecTableRef(pTable);
S
Shengliang Guan 已提交
2085 2086 2087 2088 2089
    
  if (pCreate == NULL) return terrno;
  
  pMsg->rpcRsp.rsp = pCreate;
  pMsg->rpcRsp.len = htonl(pCreate->contLen);
2090
  return TSDB_CODE_SUCCESS;
S
slguan 已提交
2091 2092
}

S
slguan 已提交
2093
// handle drop child response
2094
static void mnodeProcessDropChildTableRsp(SRpcMsg *rpcMsg) {
S
slguan 已提交
2095 2096
  if (rpcMsg->handle == NULL) return;

2097 2098
  SMnodeMsg *mnodeMsg = rpcMsg->handle;
  mnodeMsg->received++;
S
slguan 已提交
2099

S
Shengliang Guan 已提交
2100 2101
  SChildTableObj *pTable = (SChildTableObj *)mnodeMsg->pTable;
  assert(pTable);
S
Shengliang Guan 已提交
2102 2103
  mPrint("app:%p:%p, table:%s, drop table rsp received, thandle:%p result:%s", mnodeMsg->rpcMsg.ahandle, mnodeMsg,
         pTable->info.tableId, mnodeMsg->rpcMsg.handle, tstrerror(rpcMsg->code));
S
slguan 已提交
2104 2105

  if (rpcMsg->code != TSDB_CODE_SUCCESS) {
S
Shengliang Guan 已提交
2106 2107
    mError("app:%p:%p, table:%s, failed to drop in dnode, reason:%s", mnodeMsg->rpcMsg.ahandle, mnodeMsg,
           pTable->info.tableId, tstrerror(rpcMsg->code));
2108
    dnodeSendRpcMnodeWriteRsp(mnodeMsg, rpcMsg->code);
S
slguan 已提交
2109 2110 2111
    return;
  }

2112 2113
  if (mnodeMsg->pVgroup == NULL) mnodeMsg->pVgroup = mnodeGetVgroup(pTable->vgId);
  if (mnodeMsg->pVgroup == NULL) {
S
Shengliang Guan 已提交
2114
    mError("app:%p:%p, table:%s, failed to get vgroup", mnodeMsg->rpcMsg.ahandle, mnodeMsg, pTable->info.tableId);
2115
    dnodeSendRpcMnodeWriteRsp(mnodeMsg, TSDB_CODE_MND_VGROUP_NOT_EXIST);
S
slguan 已提交
2116 2117
    return;
  }
S
Shengliang Guan 已提交
2118

S
slguan 已提交
2119
  SSdbOper oper = {
S
slguan 已提交
2120
    .type = SDB_OPER_GLOBAL,
S
slguan 已提交
2121 2122 2123
    .table = tsChildTableSdb,
    .pObj = pTable
  };
S
Shengliang Guan 已提交
2124

S
slguan 已提交
2125 2126
  int32_t code = sdbDeleteRow(&oper);
  if (code != TSDB_CODE_SUCCESS) {
S
Shengliang Guan 已提交
2127
    mError("app:%p:%p, table:%s, update ctables sdb error", mnodeMsg->rpcMsg.ahandle, mnodeMsg, pTable->info.tableId);
2128
    dnodeSendRpcMnodeWriteRsp(mnodeMsg, TSDB_CODE_MND_SDB_ERROR);
S
slguan 已提交
2129 2130 2131
    return;
  }

2132
  if (mnodeMsg->pVgroup->numOfTables <= 0) {
S
Shengliang Guan 已提交
2133 2134
    mPrint("app:%p:%p, vgId:%d, all tables is dropped, drop vgroup", mnodeMsg->rpcMsg.ahandle, mnodeMsg,
           mnodeMsg->pVgroup->vgId);
2135
    mnodeDropVgroup(mnodeMsg->pVgroup, NULL);
S
slguan 已提交
2136 2137
  }

2138
  dnodeSendRpcMnodeWriteRsp(mnodeMsg, TSDB_CODE_SUCCESS);
S
slguan 已提交
2139 2140
}

2141 2142 2143 2144
/*
 * handle create table response from dnode
 *   if failed, drop the table cached
 */
2145
static void mnodeProcessCreateChildTableRsp(SRpcMsg *rpcMsg) {
S
slguan 已提交
2146 2147
  if (rpcMsg->handle == NULL) return;

2148 2149
  SMnodeMsg *mnodeMsg = rpcMsg->handle;
  mnodeMsg->received++;
S
slguan 已提交
2150

S
Shengliang Guan 已提交
2151 2152
  SChildTableObj *pTable = (SChildTableObj *)mnodeMsg->pTable;
  assert(pTable);
S
Shengliang Guan 已提交
2153

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

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

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

2175
      dnodeDelayReprocessMnodeWriteMsg(mnodeMsg);
2176
    } else {
2177 2178 2179
      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);

2180
      SSdbOper oper = {.type = SDB_OPER_GLOBAL, .table = tsChildTableSdb, .pObj = pTable};
2181
      sdbDeleteRow(&oper);
2182

2183
      dnodeSendRpcMnodeWriteRsp(mnodeMsg, rpcMsg->code);
S
slguan 已提交
2184 2185 2186 2187
    }
  }
}

S
slguan 已提交
2188
// not implemented yet
2189
static void mnodeProcessAlterTableRsp(SRpcMsg *rpcMsg) {
2190
  mTrace("alter table rsp received, handle:%p code:%s", rpcMsg->handle, tstrerror(rpcMsg->code));
S
slguan 已提交
2191 2192
}

2193
static int32_t mnodeProcessMultiTableMetaMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
2194
  SCMMultiTableInfoMsg *pInfo = pMsg->rpcMsg.pCont;
S
slguan 已提交
2195 2196
  pInfo->numOfTables = htonl(pInfo->numOfTables);

2197
  int32_t totalMallocLen = 4 * 1024 * 1024;  // first malloc 4 MB, subsequent reallocation as twice
S
slguan 已提交
2198 2199
  SMultiTableMeta *pMultiMeta = rpcMallocCont(totalMallocLen);
  if (pMultiMeta == NULL) {
2200
    return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
2201 2202 2203 2204 2205
  }

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

2206
  for (int32_t t = 0; t < pInfo->numOfTables; ++t) {
B
Bomin Zhang 已提交
2207
    char * tableId = (char *)(pInfo->tableIds + t * TSDB_TABLE_ID_LEN);
2208
    SChildTableObj *pTable = mnodeGetChildTable(tableId);
S
slguan 已提交
2209 2210
    if (pTable == NULL) continue;

2211
    if (pMsg->pDb == NULL) pMsg->pDb = mnodeGetDbByTableId(tableId);
2212
    if (pMsg->pDb == NULL) {
2213
      mnodeDecTableRef(pTable);
2214 2215
      continue;
    }
S
slguan 已提交
2216 2217

    int availLen = totalMallocLen - pMultiMeta->contLen;
2218
    if (availLen <= sizeof(STableMetaMsg) + sizeof(SSchema) * (TSDB_MAX_TAGS + TSDB_MAX_COLUMNS + 16)) {
2219 2220 2221
      totalMallocLen *= 2;
      pMultiMeta = rpcReallocCont(pMultiMeta, totalMallocLen);
      if (pMultiMeta == NULL) {
2222
        mnodeDecTableRef(pTable);
2223
        return TSDB_CODE_MND_OUT_OF_MEMORY;
2224 2225
      } else {
        t--;
2226
        mnodeDecTableRef(pTable);
2227 2228
        continue;
      }
S
slguan 已提交
2229 2230 2231
    }

    STableMetaMsg *pMeta = (STableMetaMsg *)(pMultiMeta->metas + pMultiMeta->contLen);
2232
    int32_t code = mnodeDoGetChildTableMeta(pMsg, pMeta);
S
slguan 已提交
2233 2234 2235 2236
    if (code == TSDB_CODE_SUCCESS) {
      pMultiMeta->numOfTables ++;
      pMultiMeta->contLen += pMeta->contLen;
    }
2237

2238
    mnodeDecTableRef(pTable);
S
slguan 已提交
2239 2240
  }

2241 2242 2243 2244
  pMsg->rpcRsp.rsp = pMultiMeta;
  pMsg->rpcRsp.len = pMultiMeta->contLen;

  return TSDB_CODE_SUCCESS;
S
slguan 已提交
2245 2246
}

2247 2248
static int32_t mnodeGetShowTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
  SDbObj *pDb = mnodeGetDb(pShow->db);
2249
  if (pDb == NULL) return TSDB_CODE_MND_DB_NOT_SELECTED;
S
slguan 已提交
2250 2251 2252 2253

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

H
Haojun Liao 已提交
2254 2255 2256
  SSchema s = tGetTableNameColumnSchema();
  pShow->bytes[cols] = s.bytes;
  pSchema[cols].type = s.type;
2257
  strcpy(pSchema[cols].name, "table_name");
S
slguan 已提交
2258 2259 2260 2261 2262
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

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

2291
  mnodeDecDbRef(pDb);
S
slguan 已提交
2292 2293 2294
  return 0;
}

2295 2296
static int32_t mnodeRetrieveShowTables(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
  SDbObj *pDb = mnodeGetDb(pShow->db);
S
slguan 已提交
2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308
  if (pDb == NULL) return 0;

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

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

  while (numOfRows < rows) {
2309
    pShow->pIter = mnodeGetNextChildTable(pShow->pIter, &pTable);
S
slguan 已提交
2310 2311 2312 2313
    if (pTable == NULL) break;

    // not belong to current db
    if (strncmp(pTable->info.tableId, prefix, prefixLen)) {
2314
      mnodeDecTableRef(pTable);
S
slguan 已提交
2315 2316 2317
      continue;
    }

B
Bomin Zhang 已提交
2318
    char tableName[TSDB_TABLE_NAME_LEN] = {0};
S
slguan 已提交
2319
    
2320
    // pattern compare for table name
S
Shengliang Guan 已提交
2321
    mnodeExtractTableName(pTable->info.tableId, tableName);
S
slguan 已提交
2322

B
Bomin Zhang 已提交
2323
    if (pShow->payloadLen > 0 && patternMatch(pShow->payload, tableName, sizeof(tableName) - 1, &info) != TSDB_PATTERN_MATCH) {
2324
      mnodeDecTableRef(pTable);
S
slguan 已提交
2325 2326 2327 2328 2329 2330
      continue;
    }

    int32_t cols = 0;

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

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

    numOfRows++;
2359
    mnodeDecTableRef(pTable);
S
slguan 已提交
2360 2361 2362 2363 2364
  }

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

S
Shengliang Guan 已提交
2365
  mnodeVacuumResult(data, NUM_OF_COLUMNS, numOfRows, rows, pShow);
2366
  mnodeDecDbRef(pDb);
S
slguan 已提交
2367 2368 2369 2370

  return numOfRows;
}

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

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

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

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

S
slguan 已提交
2393
  pAlter->type = htons(pAlter->type);
2394 2395
  pAlter->numOfCols = htons(pAlter->numOfCols);
  pAlter->tagValLen = htonl(pAlter->tagValLen);
S
slguan 已提交
2396

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

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

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

2434
 return code;
S
[TD-17]  
slguan 已提交
2435
}
2436

S
Shengliang Guan 已提交
2437
static int32_t mnodeGetStreamTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
2438
  SDbObj *pDb = mnodeGetDb(pShow->db);
2439
  if (pDb == NULL) return TSDB_CODE_MND_DB_NOT_SELECTED;
2440 2441 2442 2443

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

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

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

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

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

B
Bomin Zhang 已提交
2508
    char tableName[TSDB_TABLE_NAME_LEN] = {0};
2509 2510 2511 2512
    
    // pattern compare for table name
    mnodeExtractTableName(pTable->info.tableId, tableName);

B
Bomin Zhang 已提交
2513
    if (pShow->payloadLen > 0 && patternMatch(pShow->payload, tableName, sizeof(tableName) - 1, &info) != TSDB_PATTERN_MATCH) {
2514 2515 2516 2517 2518 2519 2520 2521
      mnodeDecTableRef(pTable);
      continue;
    }

    int32_t cols = 0;

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

H
Hui Li 已提交
2522
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, tableName, pShow->bytes[cols]);
2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533
    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 已提交
2534
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, pTable->sql, pShow->bytes[cols]);    
2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547
    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;
2548
}