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

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

S
TD-1793  
Shengliang Guan 已提交
46 47
#define ALTER_CTABLE_RETRY_TIMES  3
#define CREATE_CTABLE_RETRY_TIMES 10
S
TD-1793  
Shengliang Guan 已提交
48
#define CREATE_CTABLE_RETRY_SEC   14
S
TD-1793  
Shengliang Guan 已提交
49

S
TD-1853  
Shengliang Guan 已提交
50 51 52 53 54 55 56 57
int64_t          tsCTableRid = -1;
static void *    tsChildTableSdb;
int64_t          tsSTableRid = -1;
static void *    tsSuperTableSdb;
static SHashObj *tsSTableUidHash;
static int32_t   tsChildTableUpdateSize;
static int32_t   tsSuperTableUpdateSize;

58 59 60
static void *  mnodeGetChildTable(char *tableId);
static void *  mnodeGetSuperTable(char *tableId);
static void *  mnodeGetSuperTableByUid(uint64_t uid);
S
TD-1915  
Shengliang Guan 已提交
61 62 63
static void    mnodeDropAllChildTablesInStable(SSTableObj *pStable);
static void    mnodeAddTableIntoStable(SSTableObj *pStable, SCTableObj *pCtable);
static void    mnodeRemoveTableFromStable(SSTableObj *pStable, SCTableObj *pCtable);
64 65 66 67

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);
68
static int32_t mnodeRetrieveShowSuperTables(SShowObj *pShow, char *data, int32_t rows, void *pConn);
S
Shengliang Guan 已提交
69 70
static int32_t mnodeGetStreamTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn);
static int32_t mnodeRetrieveStreamTables(SShowObj *pShow, char *data, int32_t rows, void *pConn);
71
 
H
Haojun Liao 已提交
72
static int32_t mnodeProcessCreateTableMsg(SMnodeMsg *pMsg);
73 74 75 76
static int32_t mnodeProcessCreateSuperTableMsg(SMnodeMsg *pMsg);
static int32_t mnodeProcessCreateChildTableMsg(SMnodeMsg *pMsg);
static void    mnodeProcessCreateChildTableRsp(SRpcMsg *rpcMsg);

H
Haojun Liao 已提交
77
static int32_t mnodeProcessDropTableMsg(SMnodeMsg *pMsg);
78 79
static int32_t mnodeProcessDropSuperTableMsg(SMnodeMsg *pMsg);
static void    mnodeProcessDropSuperTableRsp(SRpcMsg *rpcMsg);
S
Shengliang Guan 已提交
80
static int32_t mnodeProcessDropChildTableMsg(SMnodeMsg *pMsg);
81 82
static void    mnodeProcessDropChildTableRsp(SRpcMsg *rpcMsg);

H
Haojun Liao 已提交
83 84 85
static int32_t mnodeProcessSuperTableVgroupMsg(SMnodeMsg *pMsg);
static int32_t mnodeProcessMultiTableMetaMsg(SMnodeMsg *pMsg);
static int32_t mnodeProcessTableCfgMsg(SMnodeMsg *pMsg);
86

H
Haojun Liao 已提交
87
static int32_t mnodeProcessTableMetaMsg(SMnodeMsg *pMsg);
88 89
static int32_t mnodeGetSuperTableMeta(SMnodeMsg *pMsg);
static int32_t mnodeGetChildTableMeta(SMnodeMsg *pMsg);
S
Shengliang Guan 已提交
90
static int32_t mnodeAutoCreateChildTable(SMnodeMsg *pMsg);
91

H
Haojun Liao 已提交
92
static int32_t mnodeProcessAlterTableMsg(SMnodeMsg *pMsg);
93 94
static void    mnodeProcessAlterTableRsp(SRpcMsg *rpcMsg);

S
TD-1915  
Shengliang Guan 已提交
95
static int32_t mnodeFindSuperTableColumnIndex(SSTableObj *pStable, char *colName);
S
slguan 已提交
96

S
TD-1915  
Shengliang Guan 已提交
97
static void mnodeDestroyChildTable(SCTableObj *pTable) {
S
TD-1848  
Shengliang Guan 已提交
98 99 100 101
  tfree(pTable->info.tableId);
  tfree(pTable->schema);
  tfree(pTable->sql);
  tfree(pTable);
S
slguan 已提交
102 103
}

S
TD-2046  
Shengliang Guan 已提交
104 105
static int32_t mnodeChildTableActionDestroy(SSdbRow *pRow) {
  mnodeDestroyChildTable(pRow->pObj);
S
slguan 已提交
106 107 108
  return TSDB_CODE_SUCCESS;
}

S
TD-2046  
Shengliang Guan 已提交
109 110
static int32_t mnodeChildTableActionInsert(SSdbRow *pRow) {
  SCTableObj *pTable = pRow->pObj;
S
slguan 已提交
111

112
  SVgObj *pVgroup = mnodeGetVgroup(pTable->vgId);
S
slguan 已提交
113
  if (pVgroup == NULL) {
S
slguan 已提交
114
    mError("ctable:%s, not in vgId:%d", pTable->info.tableId, pTable->vgId);
S
slguan 已提交
115 116
  }

117 118 119 120 121 122
  SDbObj *pDb = NULL;
  if (pVgroup != NULL) {
    pDb = mnodeGetDb(pVgroup->dbName);
    if (pDb == NULL) {
      mError("ctable:%s, vgId:%d not in db:%s", pTable->info.tableId, pVgroup->vgId, pVgroup->dbName);
    }
H
Hui Li 已提交
123
  }
S
slguan 已提交
124

125 126 127 128 129 130
  SAcctObj *pAcct = NULL;
  if (pDb != NULL) {
    pAcct = mnodeGetAcct(pDb->acct);
    if (pAcct == NULL) {
      mError("ctable:%s, acct:%s not exists", pTable->info.tableId, pDb->acct);
    }
S
slguan 已提交
131 132 133
  }

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

148 149 150 151 152 153
  if (pDb) mnodeAddTableIntoDb(pDb);
  if (pVgroup) mnodeAddTableIntoVgroup(pVgroup, pTable);

  mnodeDecVgroupRef(pVgroup);
  mnodeDecDbRef(pDb);
  mnodeDecAcctRef(pAcct);
S
slguan 已提交
154 155 156 157

  return TSDB_CODE_SUCCESS;
}

S
TD-2046  
Shengliang Guan 已提交
158 159
static int32_t mnodeChildTableActionDelete(SSdbRow *pRow) {
  SCTableObj *pTable = pRow->pObj;
S
slguan 已提交
160
  if (pTable->vgId == 0) {
161
    return TSDB_CODE_MND_VGROUP_NOT_EXIST;
S
slguan 已提交
162 163
  }

164 165 166 167 168 169 170
  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 已提交
171 172

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

  mnodeDecVgroupRef(pVgroup);
  mnodeDecDbRef(pDb);
  mnodeDecAcctRef(pAcct);
S
slguan 已提交
190 191 192 193
 
  return TSDB_CODE_SUCCESS;
}

S
TD-2046  
Shengliang Guan 已提交
194 195
static int32_t mnodeChildTableActionUpdate(SSdbRow *pRow) {
  SCTableObj *pNew = pRow->pObj;
S
TD-1915  
Shengliang Guan 已提交
196
  SCTableObj *pTable = mnodeGetChildTable(pNew->info.tableId);
S
slguan 已提交
197
  if (pTable != pNew) {
198
    void *oldTableId = pTable->info.tableId;    
S
slguan 已提交
199 200
    void *oldSql = pTable->sql;
    void *oldSchema = pTable->schema;
201
    void *oldSTable = pTable->superTable;
202
    int32_t oldRefCount = pTable->refCount;
203
    
S
TD-1915  
Shengliang Guan 已提交
204
    memcpy(pTable, pNew, sizeof(SCTableObj));
205
    
206
    pTable->refCount = oldRefCount;
S
slguan 已提交
207 208
    pTable->sql = pNew->sql;
    pTable->schema = pNew->schema;
209 210
    pTable->superTable = oldSTable;
    
S
slguan 已提交
211 212 213
    free(pNew);
    free(oldSql);
    free(oldSchema);
214
    free(oldTableId);
S
slguan 已提交
215
  }
216
  mnodeDecTableRef(pTable);
S
slguan 已提交
217

S
slguan 已提交
218 219 220
  return TSDB_CODE_SUCCESS;
}

S
TD-2046  
Shengliang Guan 已提交
221 222 223
static int32_t mnodeChildTableActionEncode(SSdbRow *pRow) {
  SCTableObj *pTable = pRow->pObj;
  assert(pTable != NULL && pRow->rowData != NULL);
S
slguan 已提交
224

S
TD-1207  
Shengliang Guan 已提交
225
  int32_t len = (int32_t)strlen(pTable->info.tableId);
H
Haojun Liao 已提交
226
  if (len >= TSDB_TABLE_FNAME_LEN) return TSDB_CODE_MND_INVALID_TABLE_ID;
227

S
TD-2046  
Shengliang Guan 已提交
228
  memcpy(pRow->rowData, pTable->info.tableId, len);
S
TD-1207  
Shengliang Guan 已提交
229
  memset((char *)pRow->rowData + len, 0, 1);
230 231
  len++;

S
TD-1207  
Shengliang Guan 已提交
232
  memcpy((char *)pRow->rowData + len, (char *)pTable + sizeof(char *), tsChildTableUpdateSize);
233 234 235
  len += tsChildTableUpdateSize;

  if (pTable->info.type != TSDB_CHILD_TABLE) {
S
slguan 已提交
236
    int32_t schemaSize = pTable->numOfColumns * sizeof(SSchema);
S
TD-1207  
Shengliang Guan 已提交
237
    memcpy((char *)pRow->rowData + len, pTable->schema, schemaSize);
238 239 240
    len += schemaSize;

    if (pTable->sqlLen != 0) {
S
TD-1207  
Shengliang Guan 已提交
241
      memcpy((char *)pRow->rowData + len, pTable->sql, pTable->sqlLen);
242
      len += pTable->sqlLen;
S
slguan 已提交
243 244 245
    }
  }

S
TD-2046  
Shengliang Guan 已提交
246
  pRow->rowSize = len;
247

S
slguan 已提交
248 249 250
  return TSDB_CODE_SUCCESS;
}

S
TD-2046  
Shengliang Guan 已提交
251 252
static int32_t mnodeChildTableActionDecode(SSdbRow *pRow) {
  assert(pRow->rowData != NULL);
S
TD-1915  
Shengliang Guan 已提交
253
  SCTableObj *pTable = calloc(1, sizeof(SCTableObj));
254
  if (pTable == NULL) return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
255

S
TD-1207  
Shengliang Guan 已提交
256
  int32_t len = (int32_t)strlen(pRow->rowData);
H
Haojun Liao 已提交
257
  if (len >= TSDB_TABLE_FNAME_LEN) {
S
Shuduo Sang 已提交
258
    free(pTable);
259
    return TSDB_CODE_MND_INVALID_TABLE_ID;
S
Shuduo Sang 已提交
260
  }
S
TD-2046  
Shengliang Guan 已提交
261
  pTable->info.tableId = strdup(pRow->rowData);
262 263
  len++;

S
TD-1207  
Shengliang Guan 已提交
264
  memcpy((char *)pTable + sizeof(char *), (char *)pRow->rowData + len, tsChildTableUpdateSize);
265
  len += tsChildTableUpdateSize;
S
slguan 已提交
266 267 268 269 270

  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 已提交
271
      mnodeDestroyChildTable(pTable);
272
      return TSDB_CODE_MND_INVALID_TABLE_TYPE;
S
slguan 已提交
273
    }
S
TD-1207  
Shengliang Guan 已提交
274
    memcpy(pTable->schema, (char *)pRow->rowData + len, schemaSize);
275 276 277 278 279
    len += schemaSize;

    if (pTable->sqlLen != 0) {
      pTable->sql = malloc(pTable->sqlLen);
      if (pTable->sql == NULL) {
S
Shengliang Guan 已提交
280
        mnodeDestroyChildTable(pTable);
281
        return TSDB_CODE_MND_OUT_OF_MEMORY;
282
      }
S
TD-1207  
Shengliang Guan 已提交
283
      memcpy(pTable->sql, (char *)pRow->rowData + len, pTable->sqlLen);
S
slguan 已提交
284 285 286
    }
  }

S
TD-2046  
Shengliang Guan 已提交
287
  pRow->pObj = pTable;
S
slguan 已提交
288 289 290
  return TSDB_CODE_SUCCESS;
}

291
static int32_t mnodeChildTableActionRestored() {
S
TD-1853  
Shengliang Guan 已提交
292
#if 0
S
Shengliang Guan 已提交
293
  void *pIter = NULL;
S
TD-1915  
Shengliang Guan 已提交
294
  SCTableObj *pTable = NULL;
S
slguan 已提交
295 296

  while (1) {
297
    pIter = mnodeGetNextChildTable(pIter, &pTable);
S
slguan 已提交
298 299
    if (pTable == NULL) break;

300
    SDbObj *pDb = mnodeGetDbByTableId(pTable->info.tableId);
H
Hui Li 已提交
301 302
    if (pDb == NULL || pDb->status != TSDB_DB_STATUS_READY) {
      mError("ctable:%s, failed to get db or db in dropping, discard it", pTable->info.tableId);
S
TD-2046  
Shengliang Guan 已提交
303
      SSdbRow desc = {.type = SDB_OPER_LOCAL, .pObj = pTable, .pTable = tsChildTableSdb};
S
slguan 已提交
304
      sdbDeleteRow(&desc);
305
      mnodeDecTableRef(pTable);
S
Shengliang Guan 已提交
306
      mnodeDecDbRef(pDb);
S
slguan 已提交
307 308
      continue;
    }
309
    mnodeDecDbRef(pDb);
S
slguan 已提交
310

311
    SVgObj *pVgroup = mnodeGetVgroup(pTable->vgId);
S
slguan 已提交
312
    if (pVgroup == NULL) {
H
Haojun Liao 已提交
313
      mError("ctable:%s, failed to get vgId:%d tid:%d, discard it", pTable->info.tableId, pTable->vgId, pTable->tid);
S
slguan 已提交
314
      pTable->vgId = 0;
S
TD-2046  
Shengliang Guan 已提交
315
      SSdbRow desc = {.type = SDB_OPER_LOCAL, .pObj = pTable, .pTable = tsChildTableSdb};
S
slguan 已提交
316
      sdbDeleteRow(&desc);
317
      mnodeDecTableRef(pTable);
S
slguan 已提交
318 319
      continue;
    }
320
    mnodeDecVgroupRef(pVgroup);
S
slguan 已提交
321 322

    if (strcmp(pVgroup->dbName, pDb->name) != 0) {
S
slguan 已提交
323
      mError("ctable:%s, db:%s not match with vgId:%d db:%s sid:%d, discard it",
H
Haojun Liao 已提交
324
             pTable->info.tableId, pDb->name, pTable->vgId, pVgroup->dbName, pTable->tid);
S
slguan 已提交
325
      pTable->vgId = 0;
S
TD-2046  
Shengliang Guan 已提交
326
      SSdbRow desc = {.type = SDB_OPER_LOCAL, .pObj = pTable, .pTable = tsChildTableSdb};
S
slguan 已提交
327
      sdbDeleteRow(&desc);
328
      mnodeDecTableRef(pTable);
S
slguan 已提交
329 330 331 332
      continue;
    }

    if (pTable->info.type == TSDB_CHILD_TABLE) {
S
TD-1915  
Shengliang Guan 已提交
333
      SSTableObj *pSuperTable = mnodeGetSuperTableByUid(pTable->suid);
S
slguan 已提交
334
      if (pSuperTable == NULL) {
335
        mError("ctable:%s, stable:%" PRIu64 " not exist", pTable->info.tableId, pTable->suid);
S
slguan 已提交
336
        pTable->vgId = 0;
S
TD-2046  
Shengliang Guan 已提交
337
        SSdbRow desc = {.type = SDB_OPER_LOCAL, .pObj = pTable, .pTable = tsChildTableSdb};
S
slguan 已提交
338
        sdbDeleteRow(&desc);
339
        mnodeDecTableRef(pTable);
S
slguan 已提交
340 341
        continue;
      }
342
      mnodeDecTableRef(pSuperTable);
S
slguan 已提交
343
    }
S
Shengliang Guan 已提交
344

345
    mnodeDecTableRef(pTable);
S
slguan 已提交
346 347
  }

S
Shengliang Guan 已提交
348
  mnodeCancelGetNextChildTable(pIter);
S
TD-1853  
Shengliang Guan 已提交
349
#endif
S
slguan 已提交
350 351 352
  return 0;
}

353
static int32_t mnodeInitChildTables() {
S
TD-1915  
Shengliang Guan 已提交
354
  SCTableObj tObj;
S
TD-1207  
Shengliang Guan 已提交
355
  tsChildTableUpdateSize = (int32_t)((int8_t *)tObj.updateEnd - (int8_t *)&tObj.info.type);
S
slguan 已提交
356

S
TD-2264  
Shengliang Guan 已提交
357
  SSdbTableDesc desc = {
S
TD-2046  
Shengliang Guan 已提交
358 359
    .id           = SDB_TABLE_CTABLE,
    .name         = "ctables",
S
Shengliang Guan 已提交
360
    .hashSessions = TSDB_DEFAULT_CTABLES_HASH_SIZE,
S
TD-1915  
Shengliang Guan 已提交
361
    .maxRowSize   = sizeof(SCTableObj) + sizeof(SSchema) * (TSDB_MAX_TAGS + TSDB_MAX_COLUMNS + 16) + TSDB_TABLE_FNAME_LEN + TSDB_CQ_SQL_SIZE,
S
TD-1207  
Shengliang Guan 已提交
362
    .refCountPos  = (int32_t)((int8_t *)(&tObj.refCount) - (int8_t *)&tObj),
363
    .keyType      = SDB_KEY_VAR_STRING,
S
TD-2046  
Shengliang Guan 已提交
364 365 366 367 368 369
    .fpInsert     = mnodeChildTableActionInsert,
    .fpDelete     = mnodeChildTableActionDelete,
    .fpUpdate     = mnodeChildTableActionUpdate,
    .fpEncode     = mnodeChildTableActionEncode,
    .fpDecode     = mnodeChildTableActionDecode,
    .fpDestroy    = mnodeChildTableActionDestroy,
S
TD-2046  
Shengliang Guan 已提交
370
    .fpRestored   = mnodeChildTableActionRestored
S
slguan 已提交
371 372
  };

S
TD-2264  
Shengliang Guan 已提交
373 374
  tsCTableRid = sdbOpenTable(&desc);
  tsChildTableSdb = sdbGetTableByRid(tsCTableRid);
S
slguan 已提交
375 376 377 378 379
  if (tsChildTableSdb == NULL) {
    mError("failed to init child table data");
    return -1;
  }

380
  mDebug("table:ctables is created");
S
slguan 已提交
381 382 383
  return 0;
}

384
static void mnodeCleanupChildTables() {
S
TD-2264  
Shengliang Guan 已提交
385
  sdbCloseTable(tsCTableRid);
386
  tsChildTableSdb = NULL;
S
slguan 已提交
387 388
}

389 390 391 392 393 394 395 396
int64_t mnodeGetSuperTableNum() {
  return sdbGetNumOfRows(tsSuperTableSdb);
}

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

S
TD-1915  
Shengliang Guan 已提交
397
static void mnodeAddTableIntoStable(SSTableObj *pStable, SCTableObj *pCtable) {
398
  atomic_add_fetch_32(&pStable->numOfTables, 1);
S
slguan 已提交
399

400
  if (pStable->vgHash == NULL) {
S
TD-2363  
Shengliang Guan 已提交
401
    pStable->vgHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
S
TD-2367  
Shengliang Guan 已提交
402
    mDebug("table:%s, create hash:%p", pStable->info.tableId, pStable->vgHash);
S
slguan 已提交
403 404
  }

405
  if (pStable->vgHash != NULL) {
406 407
    if (taosHashGet(pStable->vgHash, &pCtable->vgId, sizeof(pCtable->vgId)) == NULL) {
      taosHashPut(pStable->vgHash, &pCtable->vgId, sizeof(pCtable->vgId), &pCtable->vgId, sizeof(pCtable->vgId));
S
TD-2367  
Shengliang Guan 已提交
408 409
      mDebug("table:%s, vgId:%d is put into stable hash:%p, sizeOfVgList:%d", pStable->info.tableId, pCtable->vgId,
             pStable->vgHash, taosHashGetSize(pStable->vgHash));
410
    }
411
  }
S
slguan 已提交
412 413
}

S
TD-1915  
Shengliang Guan 已提交
414
static void mnodeRemoveTableFromStable(SSTableObj *pStable, SCTableObj *pCtable) {
415
  atomic_sub_fetch_32(&pStable->numOfTables, 1);
416 417 418

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

419
  SVgObj *pVgroup = mnodeGetVgroup(pCtable->vgId);
420
  if (pVgroup == NULL) {
S
Shengliang Guan 已提交
421
    taosHashRemove(pStable->vgHash, &pCtable->vgId, sizeof(pCtable->vgId));
S
TD-2367  
Shengliang Guan 已提交
422 423
    mDebug("table:%s, vgId:%d is remove from stable hash:%p sizeOfVgList:%d", pStable->info.tableId, pCtable->vgId,
           pStable->vgHash, taosHashGetSize(pStable->vgHash));
424
  }
425
  mnodeDecVgroupRef(pVgroup);
S
slguan 已提交
426 427
}

S
TD-1915  
Shengliang Guan 已提交
428
static void mnodeDestroySuperTable(SSTableObj *pStable) {
S
TD-2367  
Shengliang Guan 已提交
429
  mDebug("table:%s, is destroyed, stable hash:%p", pStable->info.tableId, pStable->vgHash);
430 431 432 433
  if (pStable->vgHash != NULL) {
    taosHashCleanup(pStable->vgHash);
    pStable->vgHash = NULL;
  }
S
TD-1848  
Shengliang Guan 已提交
434 435 436
  tfree(pStable->info.tableId);
  tfree(pStable->schema);
  tfree(pStable);
S
slguan 已提交
437 438
}

S
TD-2046  
Shengliang Guan 已提交
439 440
static int32_t mnodeSuperTableActionDestroy(SSdbRow *pRow) {
  mnodeDestroySuperTable(pRow->pObj);
S
slguan 已提交
441 442 443
  return TSDB_CODE_SUCCESS;
}

S
TD-2046  
Shengliang Guan 已提交
444 445
static int32_t mnodeSuperTableActionInsert(SSdbRow *pRow) {
  SSTableObj *pStable = pRow->pObj;
446
  SDbObj *pDb = mnodeGetDbByTableId(pStable->info.tableId);
H
Hui Li 已提交
447
  if (pDb != NULL && pDb->status == TSDB_DB_STATUS_READY) {
448
    mnodeAddSuperTableIntoDb(pDb);
S
slguan 已提交
449
  }
450
  mnodeDecDbRef(pDb);
S
slguan 已提交
451

S
TD-1853  
Shengliang Guan 已提交
452
  taosHashPut(tsSTableUidHash, &pStable->uid, sizeof(int64_t), &pStable, sizeof(int64_t));
S
slguan 已提交
453 454 455
  return TSDB_CODE_SUCCESS;
}

S
TD-2046  
Shengliang Guan 已提交
456 457
static int32_t mnodeSuperTableActionDelete(SSdbRow *pRow) {
  SSTableObj *pStable = pRow->pObj;
458
  SDbObj *pDb = mnodeGetDbByTableId(pStable->info.tableId);
S
slguan 已提交
459
  if (pDb != NULL) {
460
    mnodeRemoveSuperTableFromDb(pDb);
S
TD-1915  
Shengliang Guan 已提交
461
    mnodeDropAllChildTablesInStable((SSTableObj *)pStable);
S
slguan 已提交
462
  }
463
  mnodeDecDbRef(pDb);
S
slguan 已提交
464

S
TD-1853  
Shengliang Guan 已提交
465
  taosHashRemove(tsSTableUidHash, &pStable->uid, sizeof(int64_t));
S
slguan 已提交
466 467 468
  return TSDB_CODE_SUCCESS;
}

S
TD-2046  
Shengliang Guan 已提交
469 470
static int32_t mnodeSuperTableActionUpdate(SSdbRow *pRow) {
  SSTableObj *pNew = pRow->pObj;
S
TD-1915  
Shengliang Guan 已提交
471
  SSTableObj *pTable = mnodeGetSuperTable(pNew->info.tableId);
S
Shengliang Guan 已提交
472
  if (pTable != NULL && pTable != pNew) {
S
TD-2367  
Shengliang Guan 已提交
473 474 475
    mDebug("table:%s, will be updated, hash:%p sizeOfVgList:%d, new hash:%p sizeOfVgList:%d", pTable->info.tableId,
           pTable->vgHash, taosHashGetSize(pTable->vgHash), pNew->vgHash, taosHashGetSize(pNew->vgHash));

476
    void *oldTableId = pTable->info.tableId;
S
slguan 已提交
477
    void *oldSchema = pTable->schema;
478 479
    void *oldVgHash = pTable->vgHash;
    int32_t oldRefCount = pTable->refCount;
S
Shengliang Guan 已提交
480
    int32_t oldNumOfTables = pTable->numOfTables;
481

S
TD-1915  
Shengliang Guan 已提交
482
    memcpy(pTable, pNew, sizeof(SSTableObj));
483 484 485

    pTable->vgHash = oldVgHash;
    pTable->refCount = oldRefCount;
S
slguan 已提交
486
    pTable->schema = pNew->schema;
S
Shengliang Guan 已提交
487
    pTable->numOfTables = oldNumOfTables;
488
    free(pNew);
489
    free(oldTableId);
S
slguan 已提交
490
    free(oldSchema);
S
TD-2367  
Shengliang Guan 已提交
491 492 493

    mDebug("table:%s, update finished, hash:%p sizeOfVgList:%d", pTable->info.tableId, pTable->vgHash,
           taosHashGetSize(pTable->vgHash));
S
slguan 已提交
494
  }
S
Shengliang Guan 已提交
495

S
Shengliang Guan 已提交
496
  mnodeDecTableRef(pTable);
S
slguan 已提交
497 498 499
  return TSDB_CODE_SUCCESS;
}

S
TD-2046  
Shengliang Guan 已提交
500 501 502
static int32_t mnodeSuperTableActionEncode(SSdbRow *pRow) {
  SSTableObj *pStable = pRow->pObj;
  assert(pRow->pObj != NULL && pRow->rowData != NULL);
S
slguan 已提交
503

S
TD-1207  
Shengliang Guan 已提交
504
  int32_t len = (int32_t)strlen(pStable->info.tableId);
H
Haojun Liao 已提交
505
  if (len >= TSDB_TABLE_FNAME_LEN) len = TSDB_CODE_MND_INVALID_TABLE_ID;
S
slguan 已提交
506

S
TD-2046  
Shengliang Guan 已提交
507
  memcpy(pRow->rowData, pStable->info.tableId, len);
S
TD-1207  
Shengliang Guan 已提交
508
  memset((char *)pRow->rowData + len, 0, 1);
509 510
  len++;

S
TD-1207  
Shengliang Guan 已提交
511
  memcpy((char *)pRow->rowData + len, (char *)pStable + sizeof(char *), tsSuperTableUpdateSize);
512 513 514
  len += tsSuperTableUpdateSize;

  int32_t schemaSize = sizeof(SSchema) * (pStable->numOfColumns + pStable->numOfTags);
S
TD-1207  
Shengliang Guan 已提交
515
  memcpy((char *)pRow->rowData + len, pStable->schema, schemaSize);
516
  len += schemaSize;
S
slguan 已提交
517

S
TD-2046  
Shengliang Guan 已提交
518
  pRow->rowSize = len;
S
slguan 已提交
519 520 521 522

  return TSDB_CODE_SUCCESS;
}

S
TD-2046  
Shengliang Guan 已提交
523 524
static int32_t mnodeSuperTableActionDecode(SSdbRow *pRow) {
  assert(pRow->rowData != NULL);
S
TD-1915  
Shengliang Guan 已提交
525
  SSTableObj *pStable = (SSTableObj *) calloc(1, sizeof(SSTableObj));
526
  if (pStable == NULL) return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
527

S
TD-1207  
Shengliang Guan 已提交
528
  int32_t len = (int32_t)strlen(pRow->rowData);
H
Haojun Liao 已提交
529
  if (len >= TSDB_TABLE_FNAME_LEN){
530
    free(pStable);
531
    return TSDB_CODE_MND_INVALID_TABLE_ID;
532
  }
S
TD-2046  
Shengliang Guan 已提交
533
  pStable->info.tableId = strdup(pRow->rowData);
534 535
  len++;

S
TD-1207  
Shengliang Guan 已提交
536
  memcpy((char *)pStable + sizeof(char *), (char *)pRow->rowData + len, tsSuperTableUpdateSize);
537
  len += tsSuperTableUpdateSize;
S
slguan 已提交
538 539 540 541

  int32_t schemaSize = sizeof(SSchema) * (pStable->numOfColumns + pStable->numOfTags);
  pStable->schema = malloc(schemaSize);
  if (pStable->schema == NULL) {
S
Shengliang Guan 已提交
542
    mnodeDestroySuperTable(pStable);
543
    return TSDB_CODE_MND_NOT_SUPER_TABLE;
S
slguan 已提交
544 545
  }

S
TD-1207  
Shengliang Guan 已提交
546
  memcpy(pStable->schema, (char *)pRow->rowData + len, schemaSize);
547
  
S
TD-2046  
Shengliang Guan 已提交
548
  pRow->pObj = pStable;
S
slguan 已提交
549 550 551 552

  return TSDB_CODE_SUCCESS;
}

553
static int32_t mnodeSuperTableActionRestored() {
S
slguan 已提交
554 555 556
  return 0;
}

557
static int32_t mnodeInitSuperTables() {
S
TD-1915  
Shengliang Guan 已提交
558
  SSTableObj tObj;
S
TD-1207  
Shengliang Guan 已提交
559
  tsSuperTableUpdateSize = (int32_t)((int8_t *)tObj.updateEnd - (int8_t *)&tObj.info.type);
S
slguan 已提交
560

S
TD-2264  
Shengliang Guan 已提交
561
  SSdbTableDesc desc = {
S
TD-2046  
Shengliang Guan 已提交
562 563
    .id           = SDB_TABLE_STABLE,
    .name         = "stables",
S
Shengliang Guan 已提交
564
    .hashSessions = TSDB_DEFAULT_STABLES_HASH_SIZE,
S
TD-1915  
Shengliang Guan 已提交
565
    .maxRowSize   = sizeof(SSTableObj) + sizeof(SSchema) * (TSDB_MAX_TAGS + TSDB_MAX_COLUMNS + 16) + TSDB_TABLE_FNAME_LEN,
S
TD-1207  
Shengliang Guan 已提交
566
    .refCountPos  = (int32_t)((int8_t *)(&tObj.refCount) - (int8_t *)&tObj),
567
    .keyType      = SDB_KEY_VAR_STRING,
S
TD-2046  
Shengliang Guan 已提交
568 569 570 571 572 573
    .fpInsert     = mnodeSuperTableActionInsert,
    .fpDelete     = mnodeSuperTableActionDelete,
    .fpUpdate     = mnodeSuperTableActionUpdate,
    .fpEncode     = mnodeSuperTableActionEncode,
    .fpDecode     = mnodeSuperTableActionDecode,
    .fpDestroy    = mnodeSuperTableActionDestroy,
S
TD-2046  
Shengliang Guan 已提交
574
    .fpRestored   = mnodeSuperTableActionRestored
S
slguan 已提交
575 576
  };

S
TD-1853  
Shengliang Guan 已提交
577
  tsSTableUidHash = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
S
TD-2264  
Shengliang Guan 已提交
578 579
  tsSTableRid = sdbOpenTable(&desc);
  tsSuperTableSdb = sdbGetTableByRid(tsSTableRid);
S
slguan 已提交
580 581 582 583 584
  if (tsSuperTableSdb == NULL) {
    mError("failed to init stables data");
    return -1;
  }

585
  mDebug("table:stables is created");
S
slguan 已提交
586 587 588
  return 0;
}

589
static void mnodeCleanupSuperTables() {
S
TD-2264  
Shengliang Guan 已提交
590
  sdbCloseTable(tsSTableRid);
591
  tsSuperTableSdb = NULL;
S
TD-1853  
Shengliang Guan 已提交
592 593 594

  taosHashCleanup(tsSTableUidHash);
  tsSTableUidHash = NULL;
S
slguan 已提交
595 596
}

597 598
int32_t mnodeInitTables() {
  int32_t code = mnodeInitSuperTables();
S
slguan 已提交
599 600
  if (code != TSDB_CODE_SUCCESS) {
    return code;
H
hzcheng 已提交
601 602
  }

603
  code = mnodeInitChildTables();
S
slguan 已提交
604 605 606
  if (code != TSDB_CODE_SUCCESS) {
    return code;
  }
H
hzcheng 已提交
607

608 609 610 611 612 613
  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 已提交
614
  
615 616 617 618
  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 已提交
619

620
  mnodeAddPeerMsgHandle(TSDB_MSG_TYPE_DM_CONFIG_TABLE, mnodeProcessTableCfgMsg);
S
slguan 已提交
621

622 623
  mnodeAddShowMetaHandle(TSDB_MGMT_TABLE_TABLE, mnodeGetShowTableMeta);
  mnodeAddShowRetrieveHandle(TSDB_MGMT_TABLE_TABLE, mnodeRetrieveShowTables);
S
Shengliang Guan 已提交
624
  mnodeAddShowFreeIterHandle(TSDB_MGMT_TABLE_TABLE, mnodeCancelGetNextChildTable);
625 626
  mnodeAddShowMetaHandle(TSDB_MGMT_TABLE_METRIC, mnodeGetShowSuperTableMeta);
  mnodeAddShowRetrieveHandle(TSDB_MGMT_TABLE_METRIC, mnodeRetrieveShowSuperTables);
S
Shengliang Guan 已提交
627
  mnodeAddShowFreeIterHandle(TSDB_MGMT_TABLE_METRIC, mnodeCancelGetNextSuperTable);
S
Shengliang Guan 已提交
628 629
  mnodeAddShowMetaHandle(TSDB_MGMT_TABLE_STREAMTABLES, mnodeGetStreamTableMeta);
  mnodeAddShowRetrieveHandle(TSDB_MGMT_TABLE_STREAMTABLES, mnodeRetrieveStreamTables);
S
Shengliang Guan 已提交
630
  mnodeAddShowFreeIterHandle(TSDB_MGMT_TABLE_STREAMTABLES, mnodeCancelGetNextChildTable);
631

S
slguan 已提交
632
  return TSDB_CODE_SUCCESS;
H
hzcheng 已提交
633 634
}

635
static void *mnodeGetChildTable(char *tableId) {
S
slguan 已提交
636 637 638
  return sdbGetRow(tsChildTableSdb, tableId);
}

639
static void *mnodeGetSuperTable(char *tableId) {
S
slguan 已提交
640 641 642
  return sdbGetRow(tsSuperTableSdb, tableId);
}

643
static void *mnodeGetSuperTableByUid(uint64_t uid) {
S
TD-1853  
Shengliang Guan 已提交
644 645
  SSTableObj **ppStable = taosHashGet(tsSTableUidHash, &uid, sizeof(int64_t));
  if (ppStable == NULL || *ppStable == NULL) return NULL;
646

S
TD-1853  
Shengliang Guan 已提交
647 648 649
  SSTableObj *pStable = *ppStable;
  mnodeIncTableRef(pStable);
  return pStable;
650 651
}

652 653
void *mnodeGetTable(char *tableId) {
  void *pTable = mnodeGetSuperTable(tableId);
654 655
  if (pTable != NULL) {
    return pTable;
H
hzcheng 已提交
656 657
  }

658
  pTable = mnodeGetChildTable(tableId);
659 660
  if (pTable != NULL) {
    return pTable;
S
#1177  
slguan 已提交
661
  }
H
hzcheng 已提交
662

S
slguan 已提交
663
  return NULL;
S
#1177  
slguan 已提交
664
}
H
hzcheng 已提交
665

S
TD-1915  
Shengliang Guan 已提交
666
void *mnodeGetNextChildTable(void *pIter, SCTableObj **pTable) {
S
Shengliang Guan 已提交
667
  return sdbFetchRow(tsChildTableSdb, pIter, (void **)pTable);
668 669
}

S
Shengliang Guan 已提交
670 671 672 673
void mnodeCancelGetNextChildTable(void *pIter) {
  sdbFreeIter(tsChildTableSdb, pIter);
}

S
TD-1915  
Shengliang Guan 已提交
674
void *mnodeGetNextSuperTable(void *pIter, SSTableObj **pTable) {
S
Shengliang Guan 已提交
675
  return sdbFetchRow(tsSuperTableSdb, pIter, (void **)pTable);
676 677
}

S
Shengliang Guan 已提交
678 679 680 681
void mnodeCancelGetNextSuperTable(void *pIter) {
  sdbFreeIter(tsSuperTableSdb, pIter);
}

682
void mnodeIncTableRef(void *p1) {
S
slguan 已提交
683
  STableObj *pTable = (STableObj *)p1;
S
slguan 已提交
684 685 686 687 688 689 690
  if (pTable->type == TSDB_SUPER_TABLE) {
    sdbIncRef(tsSuperTableSdb, pTable);
  } else {
    sdbIncRef(tsChildTableSdb, pTable);
  }
}

691
void mnodeDecTableRef(void *p1) {
S
slguan 已提交
692 693
  if (p1 == NULL) return;

S
slguan 已提交
694
  STableObj *pTable = (STableObj *)p1;
S
slguan 已提交
695 696 697 698 699 700 701
  if (pTable->type == TSDB_SUPER_TABLE) {
    sdbDecRef(tsSuperTableSdb, pTable);
  } else {
    sdbDecRef(tsChildTableSdb, pTable);
  }
}

702 703 704
void mnodeCleanupTables() {
  mnodeCleanupChildTables();
  mnodeCleanupSuperTables();
S
#1177  
slguan 已提交
705
}
H
hzcheng 已提交
706

707
// todo move to name.h, add length of table name
S
Shengliang Guan 已提交
708
static void mnodeExtractTableName(char* tableId, char* name) {
709 710 711 712 713
  int pos = -1;
  int num = 0;
  for (pos = 0; tableId[pos] != 0; ++pos) {
    if (tableId[pos] == '.') num++;
    if (num == 2) break;
S
slguan 已提交
714 715
  }

716 717
  if (num == 2) {
    strcpy(name, tableId + pos + 1);
S
slguan 已提交
718
  }
H
hzcheng 已提交
719 720
}

721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
static SMnodeMsg *mnodeCreateSubMsg(SMnodeMsg *pBatchMasterMsg, int32_t contSize) {
  SMnodeMsg *pSubMsg = taosAllocateQitem(sizeof(*pBatchMasterMsg) + contSize);
  *pSubMsg = *pBatchMasterMsg;

  //pSubMsg->pCont = (char *) pSubMsg + sizeof(SMnodeMsg);
  pSubMsg->rpcMsg.pCont = pSubMsg->pCont;
  pSubMsg->successed = 0;
  pSubMsg->expected = 0;
  SCMCreateTableMsg *pCM = pSubMsg->rpcMsg.pCont;
  pCM->numOfTables = htonl(1);
  pCM->contLen = htonl(contSize);

  return pSubMsg;
}

void mnodeDestroySubMsg(SMnodeMsg *pSubMsg) {
  if (pSubMsg) {
    // pUser is retained in batch master msg
    if (pSubMsg->pDb) mnodeDecDbRef(pSubMsg->pDb);
    if (pSubMsg->pVgroup) mnodeDecVgroupRef(pSubMsg->pVgroup);
    if (pSubMsg->pTable) mnodeDecTableRef(pSubMsg->pTable);
    if (pSubMsg->pSTable) mnodeDecTableRef(pSubMsg->pSTable);
    if (pSubMsg->pAcct) mnodeDecAcctRef(pSubMsg->pAcct);
    if (pSubMsg->pDnode) mnodeDecDnodeRef(pSubMsg->pDnode);

    taosFreeQitem(pSubMsg);
  }
}

static int32_t mnodeValidateCreateTableMsg(SCreateTableMsg *pCreateTable, SMnodeMsg *pMsg) {
  if (pMsg->pDb == NULL) pMsg->pDb = mnodeGetDb(pCreateTable->db);
  if (pMsg->pDb == NULL) {
    mError("msg:%p, app:%p table:%s, failed to create, db not selected", pMsg, pMsg->rpcMsg.ahandle, pCreateTable->tableId);
    return TSDB_CODE_MND_DB_NOT_SELECTED;
  }

  if (pMsg->pDb->status != TSDB_DB_STATUS_READY) {
    mError("db:%s, status:%d, in dropping", pMsg->pDb->name, pMsg->pDb->status);
    return TSDB_CODE_MND_DB_IN_DROPPING;
  }

  if (pMsg->pTable == NULL) pMsg->pTable = mnodeGetTable(pCreateTable->tableId);
  if (pMsg->pTable != NULL && pMsg->retry == 0) {
    if (pCreateTable->getMeta) {
      mDebug("msg:%p, app:%p table:%s, continue to get meta", pMsg, pMsg->rpcMsg.ahandle, pCreateTable->tableId);
      return mnodeGetChildTableMeta(pMsg);
    } else if (pCreateTable->igExists) {
      mDebug("msg:%p, app:%p table:%s, is already exist", pMsg, pMsg->rpcMsg.ahandle, pCreateTable->tableId);
      return TSDB_CODE_SUCCESS;
    } else {
      mError("msg:%p, app:%p table:%s, failed to create, table already exist", pMsg, pMsg->rpcMsg.ahandle,
             pCreateTable->tableId);
      return TSDB_CODE_MND_TABLE_ALREADY_EXIST;
    }
  }

  if (pCreateTable->numOfTags != 0) {
    mDebug("msg:%p, app:%p table:%s, create stable msg is received from thandle:%p", pMsg, pMsg->rpcMsg.ahandle,
           pCreateTable->tableId, pMsg->rpcMsg.handle);
    return mnodeProcessCreateSuperTableMsg(pMsg);
  } else {
    mDebug("msg:%p, app:%p table:%s, create ctable msg is received from thandle:%p", pMsg, pMsg->rpcMsg.ahandle,
           pCreateTable->tableId, pMsg->rpcMsg.handle);
    return mnodeProcessCreateChildTableMsg(pMsg);
  }
}

static int32_t mnodeProcessBatchCreateTableMsg(SMnodeMsg *pMsg) {
  if (pMsg->pBatchMasterMsg == NULL) { // batch master first round
    pMsg->pBatchMasterMsg = pMsg;

    SCMCreateTableMsg *pCreate = pMsg->rpcMsg.pCont;
    int32_t numOfTables = htonl(pCreate->numOfTables);
    int32_t contentLen = htonl(pCreate->contLen);
    pMsg->expected = numOfTables;

    int32_t code = TSDB_CODE_SUCCESS;
    SCreateTableMsg *pCreateTable = (SCreateTableMsg*) ((char*) pCreate + sizeof(SCMCreateTableMsg));
    for (SCreateTableMsg *p = pCreateTable; p < (SCreateTableMsg *) ((char *) pCreate + contentLen); p = (SCreateTableMsg *) ((char *) p + htonl(p->len))) {
      SMnodeMsg *pSubMsg = mnodeCreateSubMsg(pMsg, sizeof(SCMCreateTableMsg) + htonl(p->len));
      memcpy(pSubMsg->pCont + sizeof(SCMCreateTableMsg), p, htonl(p->len));
      code = mnodeValidateCreateTableMsg(p, pSubMsg);

      if (code == TSDB_CODE_SUCCESS || code == TSDB_CODE_MND_TABLE_ALREADY_EXIST) {
	++pSubMsg->pBatchMasterMsg->successed;
	mnodeDestroySubMsg(pSubMsg);
	continue;
      }

      if (code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
	mnodeDestroySubMsg(pSubMsg);
	return code;
      }
    }

    if (pMsg->successed >= pMsg->expected) {
      return code;
    } else {
      return TSDB_CODE_MND_ACTION_IN_PROGRESS;
    }
  } else {
    if (pMsg->pBatchMasterMsg != pMsg) { // batch sub replay
      SCMCreateTableMsg *pCreate = pMsg->rpcMsg.pCont;
      SCreateTableMsg *pCreateTable = (SCreateTableMsg*) ((char*) pCreate + sizeof(SCMCreateTableMsg));
      int32_t code = mnodeValidateCreateTableMsg(pCreateTable, pMsg);
      if (code == TSDB_CODE_SUCCESS || code == TSDB_CODE_MND_TABLE_ALREADY_EXIST) {
827 828 829 830 831 832 833
        ++pMsg->pBatchMasterMsg->successed;
        mnodeDestroySubMsg(pMsg);
      } else if (code == TSDB_CODE_MND_ACTION_NEED_REPROCESSED) {
        return code;
      } else if (code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
        ++pMsg->pBatchMasterMsg->received;
        mnodeDestroySubMsg(pMsg);
834 835 836 837
      }

      if (pMsg->pBatchMasterMsg->successed + pMsg->pBatchMasterMsg->received
	  >= pMsg->pBatchMasterMsg->expected) {
838
        dnodeSendRpcMWriteRsp(pMsg->pBatchMasterMsg, TSDB_CODE_SUCCESS);
839
      }
840 841

      return TSDB_CODE_MND_ACTION_IN_PROGRESS;
842 843
    } else { // batch master replay, reprocess the whole batch
      assert(0);
S
TD-1207  
Shengliang Guan 已提交
844
      return TSDB_CODE_MND_MSG_NOT_PROCESSED;
845 846 847 848
    }
  }
}

849
static int32_t mnodeProcessCreateTableMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
850
  SCMCreateTableMsg *pCreate = pMsg->rpcMsg.pCont;
851 852 853 854 855 856 857

  int32_t numOfTables = htonl(pCreate->numOfTables);
  int32_t contentLen = htonl(pCreate->contLen);
  if (numOfTables == 0 || contentLen == 0) {
    // todo return error
  }

858 859 860 861 862
  // batch master msg first round or reprocessing and batch sub msg reprocessing
  if (numOfTables > 1 || pMsg->pBatchMasterMsg != NULL) {
    return mnodeProcessBatchCreateTableMsg(pMsg);
  }

863 864
  SCreateTableMsg *p = (SCreateTableMsg*)((char*) pCreate + sizeof(SCMCreateTableMsg));
  if (pMsg->pDb == NULL) pMsg->pDb = mnodeGetDb(p->db);
H
Hui Li 已提交
865
  if (pMsg->pDb == NULL) {
866
    mError("msg:%p, app:%p table:%s, failed to create, db not selected", pMsg, pMsg->rpcMsg.ahandle, p->tableId);
867
    return TSDB_CODE_MND_DB_NOT_SELECTED;
S
slguan 已提交
868
  }
H
Hui Li 已提交
869 870 871 872 873
  
  if (pMsg->pDb->status != TSDB_DB_STATUS_READY) {
    mError("db:%s, status:%d, in dropping", pMsg->pDb->name, pMsg->pDb->status);
    return TSDB_CODE_MND_DB_IN_DROPPING;
  }
S
slguan 已提交
874

875
  if (pMsg->pTable == NULL) pMsg->pTable = mnodeGetTable(p->tableId);
876
  if (pMsg->pTable != NULL && pMsg->retry == 0) {
877 878
    if (p->getMeta) {
      mDebug("msg:%p, app:%p table:%s, continue to get meta", pMsg, pMsg->rpcMsg.ahandle, p->tableId);
879
      return mnodeGetChildTableMeta(pMsg);
880 881
    } else if (p->igExists) {
      mDebug("msg:%p, app:%p table:%s, is already exist", pMsg, pMsg->rpcMsg.ahandle, p->tableId);
882
      return TSDB_CODE_SUCCESS;
S
slguan 已提交
883
    } else {
S
Shengliang Guan 已提交
884
      mError("msg:%p, app:%p table:%s, failed to create, table already exist", pMsg, pMsg->rpcMsg.ahandle,
885
             p->tableId);
886
      return TSDB_CODE_MND_TABLE_ALREADY_EXIST;
S
slguan 已提交
887
    }
S
slguan 已提交
888 889
  }

890
  if (p->numOfTags != 0) {
S
Shengliang Guan 已提交
891
    mDebug("msg:%p, app:%p table:%s, create stable msg is received from thandle:%p", pMsg, pMsg->rpcMsg.ahandle,
892
           p->tableId, pMsg->rpcMsg.handle);
893
    return mnodeProcessCreateSuperTableMsg(pMsg);
894
  } else {
S
Shengliang Guan 已提交
895
    mDebug("msg:%p, app:%p table:%s, create ctable msg is received from thandle:%p", pMsg, pMsg->rpcMsg.ahandle,
896
           p->tableId, pMsg->rpcMsg.handle);
897
    return mnodeProcessCreateChildTableMsg(pMsg);
S
slguan 已提交
898 899 900
  }
}

901
static int32_t mnodeProcessDropTableMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
902
  SCMDropTableMsg *pDrop = pMsg->rpcMsg.pCont;
903
  if (pMsg->pDb == NULL) pMsg->pDb = mnodeGetDbByTableId(pDrop->tableId);
H
Hui Li 已提交
904
  if (pMsg->pDb == NULL) {
S
Shengliang Guan 已提交
905 906
    mError("msg:%p, app:%p table:%s, failed to drop table, db not selected or db in dropping", pMsg,
           pMsg->rpcMsg.ahandle, pDrop->tableId);
907
    return TSDB_CODE_MND_DB_NOT_SELECTED;
S
slguan 已提交
908
  }
S
Shengliang Guan 已提交
909

H
Hui Li 已提交
910 911 912 913
  if (pMsg->pDb->status != TSDB_DB_STATUS_READY) {
    mError("db:%s, status:%d, in dropping", pMsg->pDb->name, pMsg->pDb->status);
    return TSDB_CODE_MND_DB_IN_DROPPING;
  }
S
slguan 已提交
914

915
  if (mnodeCheckIsMonitorDB(pMsg->pDb->name, tsMonitorDbName)) {
S
Shengliang Guan 已提交
916
    mError("msg:%p, app:%p table:%s, failed to drop table, in monitor database", pMsg, pMsg->rpcMsg.ahandle,
S
Shengliang Guan 已提交
917
           pDrop->tableId);
918
    return TSDB_CODE_MND_MONITOR_DB_FORBIDDEN;
919 920
  }

921
  if (pMsg->pTable == NULL) pMsg->pTable = mnodeGetTable(pDrop->tableId);
S
slguan 已提交
922
  if (pMsg->pTable == NULL) {
S
slguan 已提交
923
    if (pDrop->igNotExists) {
S
Shengliang Guan 已提交
924
      mDebug("msg:%p, app:%p table:%s is not exist, treat as success", pMsg, pMsg->rpcMsg.ahandle, pDrop->tableId);
925
      return TSDB_CODE_SUCCESS;
S
slguan 已提交
926
    } else {
S
Shengliang Guan 已提交
927
      mError("msg:%p, app:%p table:%s, failed to drop, table not exist", pMsg, pMsg->rpcMsg.ahandle, pDrop->tableId);
928
      return TSDB_CODE_MND_INVALID_TABLE_NAME;
S
slguan 已提交
929 930 931
    }
  }

S
slguan 已提交
932
  if (pMsg->pTable->type == TSDB_SUPER_TABLE) {
S
TD-1915  
Shengliang Guan 已提交
933
    SSTableObj *pSTable = (SSTableObj *)pMsg->pTable;
S
TD-2367  
Shengliang Guan 已提交
934 935
    mInfo("msg:%p, app:%p table:%s, start to drop stable, uid:%" PRIu64 ", numOfChildTables:%d, sizeOfVgList:%d", pMsg,
          pMsg->rpcMsg.ahandle, pDrop->tableId, pSTable->uid, pSTable->numOfTables, taosHashGetSize(pSTable->vgHash));
936
    return mnodeProcessDropSuperTableMsg(pMsg);
S
slguan 已提交
937
  } else {
S
TD-1915  
Shengliang Guan 已提交
938
    SCTableObj *pCTable = (SCTableObj *)pMsg->pTable;
S
Shengliang Guan 已提交
939
    mInfo("msg:%p, app:%p table:%s, start to drop ctable, vgId:%d tid:%d uid:%" PRIu64, pMsg, pMsg->rpcMsg.ahandle,
H
Haojun Liao 已提交
940
          pDrop->tableId, pCTable->vgId, pCTable->tid, pCTable->uid);
S
Shengliang Guan 已提交
941
    return mnodeProcessDropChildTableMsg(pMsg);
S
slguan 已提交
942 943 944
  }
}

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

951
  if (pMsg->pDb == NULL) pMsg->pDb = mnodeGetDbByTableId(pInfo->tableId);
H
Hui Li 已提交
952
  if (pMsg->pDb == NULL) {
S
Shengliang Guan 已提交
953
    mError("msg:%p, app:%p table:%s, failed to get table meta, db not selected", pMsg, pMsg->rpcMsg.ahandle,
S
Shengliang Guan 已提交
954
           pInfo->tableId);
955
    return TSDB_CODE_MND_DB_NOT_SELECTED;
S
slguan 已提交
956
  }
H
Hui Li 已提交
957 958 959 960 961
  
  if (pMsg->pDb->status != TSDB_DB_STATUS_READY) {
    mError("db:%s, status:%d, in dropping", pMsg->pDb->name, pMsg->pDb->status);
    return TSDB_CODE_MND_DB_IN_DROPPING;
  }
S
slguan 已提交
962

963
  if (pMsg->pTable == NULL) pMsg->pTable = mnodeGetTable(pInfo->tableId);
S
slguan 已提交
964
  if (pMsg->pTable == NULL) {
S
slguan 已提交
965
    if (!pInfo->createFlag) {
S
Shengliang Guan 已提交
966
      mError("msg:%p, app:%p table:%s, failed to get table meta, table not exist", pMsg, pMsg->rpcMsg.ahandle,
S
Shengliang Guan 已提交
967
             pInfo->tableId);
968
      return TSDB_CODE_MND_INVALID_TABLE_NAME;
S
slguan 已提交
969
    } else {
S
Shengliang Guan 已提交
970
      mDebug("msg:%p, app:%p table:%s, failed to get table meta, start auto create table ", pMsg, pMsg->rpcMsg.ahandle,
S
Shengliang Guan 已提交
971
             pInfo->tableId);
S
Shengliang Guan 已提交
972
      return mnodeAutoCreateChildTable(pMsg);
S
slguan 已提交
973
    }
S
slguan 已提交
974
  } else {
S
slguan 已提交
975
    if (pMsg->pTable->type != TSDB_SUPER_TABLE) {
976
      return mnodeGetChildTableMeta(pMsg);
S
slguan 已提交
977
    } else {
978
      return mnodeGetSuperTableMeta(pMsg);
S
slguan 已提交
979 980 981 982
    }
  }
}

983
static int32_t mnodeCreateSuperTableCb(SMnodeMsg *pMsg, int32_t code) {
S
TD-1915  
Shengliang Guan 已提交
984
  SSTableObj *pTable = (SSTableObj *)pMsg->pTable;
985 986 987
  assert(pTable);

  if (code == TSDB_CODE_SUCCESS) {
S
Shengliang Guan 已提交
988
    mLInfo("stable:%s, is created in sdb, uid:%" PRIu64, pTable->info.tableId, pTable->uid);
989
  } else {
S
Shengliang Guan 已提交
990
    mError("msg:%p, app:%p stable:%s, failed to create in sdb, reason:%s", pMsg, pMsg->rpcMsg.ahandle, pTable->info.tableId,
991
           tstrerror(code));
S
TD-2046  
Shengliang Guan 已提交
992
    SSdbRow desc = {.type = SDB_OPER_GLOBAL, .pObj = pTable, .pTable = tsSuperTableSdb};
993
    sdbDeleteRow(&desc);
994 995 996 997 998
  }

  return code;
}

999
static int32_t mnodeProcessCreateSuperTableMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
1000 1001
  if (pMsg == NULL) return TSDB_CODE_MND_APP_ERROR;

1002 1003
  SCMCreateTableMsg *pCreate1 = pMsg->rpcMsg.pCont;
  if (pCreate1->numOfTables == 0) {
1004
    return TSDB_CODE_MND_INVALID_CREATE_TABLE_MSG;
1005 1006 1007 1008
  }

  SCreateTableMsg* pCreate = (SCreateTableMsg*)((char*)pCreate1 + sizeof(SCMCreateTableMsg));

S
TD-1915  
Shengliang Guan 已提交
1009
  SSTableObj *   pStable = calloc(1, sizeof(SSTableObj));
S
slguan 已提交
1010
  if (pStable == NULL) {
S
Shengliang Guan 已提交
1011
    mError("msg:%p, app:%p table:%s, failed to create, no enough memory", pMsg, pMsg->rpcMsg.ahandle, pCreate->tableId);
1012
    return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
1013 1014
  }

S
TD-1719  
Shengliang Guan 已提交
1015
  int64_t us = taosGetTimestampUs();
1016
  pStable->info.tableId = strdup(pCreate->tableId);
S
slguan 已提交
1017 1018
  pStable->info.type    = TSDB_SUPER_TABLE;
  pStable->createdTime  = taosGetTimestampMs();
S
TD-1719  
Shengliang Guan 已提交
1019
  pStable->uid          = (us << 24) + ((sdbGetVersion() & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
S
slguan 已提交
1020
  pStable->sversion     = 0;
S
TD-355  
Shengliang Guan 已提交
1021
  pStable->tversion     = 0;
S
slguan 已提交
1022 1023 1024
  pStable->numOfColumns = htons(pCreate->numOfColumns);
  pStable->numOfTags    = htons(pCreate->numOfTags);

S
slguan 已提交
1025
  int32_t numOfCols = pStable->numOfColumns + pStable->numOfTags;
S
slguan 已提交
1026 1027 1028 1029
  int32_t schemaSize = numOfCols * sizeof(SSchema);
  pStable->schema = (SSchema *)calloc(1, schemaSize);
  if (pStable->schema == NULL) {
    free(pStable);
S
Shengliang Guan 已提交
1030
    mError("msg:%p, app:%p table:%s, failed to create, no schema input", pMsg, pMsg->rpcMsg.ahandle, pCreate->tableId);
1031
    return TSDB_CODE_MND_INVALID_TABLE_NAME;
S
slguan 已提交
1032
  }
1033

S
slguan 已提交
1034 1035
  memcpy(pStable->schema, pCreate->schema, numOfCols * sizeof(SSchema));

1036 1037 1038 1039 1040
  if (pStable->numOfColumns > TSDB_MAX_COLUMNS || pStable->numOfTags > TSDB_MAX_TAGS) {
    mError("msg:%p, app:%p table:%s, failed to create, too many columns", pMsg, pMsg->rpcMsg.ahandle, pCreate->tableId);
    return TSDB_CODE_MND_INVALID_TABLE_NAME;
  }

S
slguan 已提交
1041
  pStable->nextColId = 0;
1042 1043 1044 1045

  // TODO extract method to valid the schema
  int32_t schemaLen = 0;
  int32_t tagLen = 0;
S
slguan 已提交
1046 1047 1048 1049
  for (int32_t col = 0; col < numOfCols; col++) {
    SSchema *tschema = pStable->schema;
    tschema[col].colId = pStable->nextColId++;
    tschema[col].bytes = htons(tschema[col].bytes);
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065

    if (col < pStable->numOfTables) {
      schemaLen += tschema[col].bytes;
    } else {
      tagLen += tschema[col].bytes;
    }

    if (!isValidDataType(tschema[col].type)) {
      mError("msg:%p, app:%p table:%s, failed to create, invalid data type in schema", pMsg, pMsg->rpcMsg.ahandle, pCreate->tableId);
      return TSDB_CODE_MND_INVALID_CREATE_TABLE_MSG;
    }
  }

  if (schemaLen > (TSDB_MAX_BYTES_PER_ROW || tagLen > TSDB_MAX_TAGS_LEN)) {
    mError("msg:%p, app:%p table:%s, failed to create, schema is too long", pMsg, pMsg->rpcMsg.ahandle, pCreate->tableId);
    return TSDB_CODE_MND_INVALID_CREATE_TABLE_MSG;
S
slguan 已提交
1066 1067
  }

1068 1069 1070
  pMsg->pTable = (STableObj *)pStable;
  mnodeIncTableRef(pMsg->pTable);

S
TD-2046  
Shengliang Guan 已提交
1071
  SSdbRow row = {
S
TD-2045  
Shengliang Guan 已提交
1072 1073
    .type    = SDB_OPER_GLOBAL,
    .pTable  = tsSuperTableSdb,
S
TD-2046  
Shengliang Guan 已提交
1074
    .pObj    = pStable,
S
TD-1915  
Shengliang Guan 已提交
1075
    .rowSize = sizeof(SSTableObj) + schemaSize,
S
TD-2045  
Shengliang Guan 已提交
1076 1077
    .pMsg    = pMsg,
    .fpRsp   = mnodeCreateSuperTableCb
S
slguan 已提交
1078 1079
  };

S
TD-2046  
Shengliang Guan 已提交
1080
  int32_t code = sdbInsertRow(&row);
S
Shengliang Guan 已提交
1081
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
1082
    mnodeDestroySuperTable(pStable);
1083
    pMsg->pTable = NULL;
S
Shengliang Guan 已提交
1084
    mError("msg:%p, app:%p table:%s, failed to create, sdb error", pMsg, pMsg->rpcMsg.ahandle, pCreate->tableId);
S
slguan 已提交
1085
  }
S
Shengliang Guan 已提交
1086 1087

  return code;
S
slguan 已提交
1088 1089
}

1090
static int32_t mnodeDropSuperTableCb(SMnodeMsg *pMsg, int32_t code) {
S
TD-1915  
Shengliang Guan 已提交
1091
  SSTableObj *pTable = (SSTableObj *)pMsg->pTable;
1092
  if (code != TSDB_CODE_SUCCESS) {
S
Shengliang Guan 已提交
1093
    mError("msg:%p, app:%p stable:%s, failed to drop, sdb error", pMsg, pMsg->rpcMsg.ahandle, pTable->info.tableId);
1094
  } else {
S
Shengliang Guan 已提交
1095
    mLInfo("msg:%p, app:%p stable:%s, is dropped from sdb", pMsg, pMsg->rpcMsg.ahandle, pTable->info.tableId);
1096 1097 1098 1099
  }

  return code;
}
1100

1101
static int32_t mnodeProcessDropSuperTableMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
1102 1103
  if (pMsg == NULL) return TSDB_CODE_MND_APP_ERROR;

S
TD-1915  
Shengliang Guan 已提交
1104
  SSTableObj *pStable = (SSTableObj *)pMsg->pTable;
S
TD-2367  
Shengliang Guan 已提交
1105 1106 1107 1108
  mInfo("msg:%p, app:%p stable:%s will be dropped, hash:%p sizeOfVgList:%d", pMsg, pMsg->rpcMsg.ahandle,
        pStable->info.tableId, pStable->vgHash, taosHashGetSize(pStable->vgHash));

  if (pStable->vgHash != NULL /*pStable->numOfTables != 0*/) {
S
Shengliang Guan 已提交
1109 1110
    int32_t *pVgId = taosHashIterate(pStable->vgHash, NULL);
    while (pVgId) {
1111
      SVgObj *pVgroup = mnodeGetVgroup(*pVgId);
S
Shengliang Guan 已提交
1112
      pVgId = taosHashIterate(pStable->vgHash, pVgId);
guanshengliang's avatar
guanshengliang 已提交
1113
      if (pVgroup == NULL) break;
1114

S
TD-1732  
Shengliang Guan 已提交
1115 1116
      SDropSTableMsg *pDrop = rpcMallocCont(sizeof(SDropSTableMsg));
      pDrop->contLen = htonl(sizeof(SDropSTableMsg));
1117
      pDrop->vgId = htonl(pVgroup->vgId);
S
slguan 已提交
1118
      pDrop->uid = htobe64(pStable->uid);
S
Shengliang Guan 已提交
1119
      mnodeExtractTableName(pStable->info.tableId, pDrop->tableId);
S
Shengliang Guan 已提交
1120

S
TD-2367  
Shengliang Guan 已提交
1121 1122 1123
      mInfo("msg:%p, app:%p stable:%s, send drop stable msg to vgId:%d, hash:%p sizeOfVgList:%d", pMsg,
            pMsg->rpcMsg.ahandle, pStable->info.tableId, pVgroup->vgId, pStable->vgHash,
            taosHashGetSize(pStable->vgHash));
1124
      SRpcEpSet epSet = mnodeGetEpSetFromVgroup(pVgroup);
S
TD-1732  
Shengliang Guan 已提交
1125
      SRpcMsg   rpcMsg = {.pCont = pDrop, .contLen = sizeof(SDropSTableMsg), .msgType = TSDB_MSG_TYPE_MD_DROP_STABLE};
1126
      dnodeSendMsgToDnode(&epSet, &rpcMsg);
1127
      mnodeDecVgroupRef(pVgroup);
S
slguan 已提交
1128
    }
S
Shengliang Guan 已提交
1129 1130

    taosHashCancelIterate(pStable->vgHash, pVgId);
1131

1132
    mnodeDropAllChildTablesInStable(pStable);
guanshengliang's avatar
guanshengliang 已提交
1133 1134
  } 
  
S
TD-2046  
Shengliang Guan 已提交
1135
  SSdbRow row = {
S
TD-2046  
Shengliang Guan 已提交
1136 1137
    .type    = SDB_OPER_GLOBAL,
    .pTable  = tsSuperTableSdb,
S
TD-2046  
Shengliang Guan 已提交
1138
    .pObj    = pStable,
S
TD-2046  
Shengliang Guan 已提交
1139
    .pMsg    = pMsg,
S
TD-2045  
Shengliang Guan 已提交
1140
    .fpRsp   = mnodeDropSuperTableCb
guanshengliang's avatar
guanshengliang 已提交
1141
  };
1142

S
TD-2046  
Shengliang Guan 已提交
1143
  int32_t code = sdbDeleteRow(&row);
S
Shengliang Guan 已提交
1144
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
1145
    mError("msg:%p, app:%p table:%s, failed to drop, reason:%s", pMsg, pMsg->rpcMsg.ahandle, pStable->info.tableId,
S
Shengliang Guan 已提交
1146
           tstrerror(code));
1147
  }
S
Shengliang Guan 已提交
1148

1149
  return code;
S
slguan 已提交
1150 1151
}

S
TD-1915  
Shengliang Guan 已提交
1152
static int32_t mnodeFindSuperTableTagIndex(SSTableObj *pStable, const char *tagName) {
guanshengliang's avatar
guanshengliang 已提交
1153 1154 1155 1156
  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 已提交
1157 1158 1159 1160 1161 1162
    }
  }

  return -1;
}

1163
static int32_t mnodeAddSuperTableTagCb(SMnodeMsg *pMsg, int32_t code) {
S
TD-1915  
Shengliang Guan 已提交
1164
  SSTableObj *pStable = (SSTableObj *)pMsg->pTable;
S
Shengliang Guan 已提交
1165
  mLInfo("msg:%p, app:%p stable %s, add tag result:%s", pMsg, pMsg->rpcMsg.ahandle, pStable->info.tableId,
1166 1167 1168 1169 1170 1171
          tstrerror(code));

  return code;
}

static int32_t mnodeAddSuperTableTag(SMnodeMsg *pMsg, SSchema schema[], int32_t ntags) {
S
TD-1915  
Shengliang Guan 已提交
1172
  SSTableObj *pStable = (SSTableObj *)pMsg->pTable;
S
slguan 已提交
1173
  if (pStable->numOfTags + ntags > TSDB_MAX_TAGS) {
S
Shengliang Guan 已提交
1174
    mError("msg:%p, app:%p stable:%s, add tag, too many tags", pMsg, pMsg->rpcMsg.ahandle, pStable->info.tableId);
1175
    return TSDB_CODE_MND_TOO_MANY_TAGS;
S
slguan 已提交
1176 1177
  }

S
slguan 已提交
1178
  for (int32_t i = 0; i < ntags; i++) {
1179
    if (mnodeFindSuperTableColumnIndex(pStable, schema[i].name) > 0) {
S
Shengliang Guan 已提交
1180
      mError("msg:%p, app:%p stable:%s, add tag, column:%s already exist", pMsg, pMsg->rpcMsg.ahandle,
S
Shengliang Guan 已提交
1181
             pStable->info.tableId, schema[i].name);
1182
      return TSDB_CODE_MND_TAG_ALREAY_EXIST;
S
slguan 已提交
1183 1184
    }

1185
    if (mnodeFindSuperTableTagIndex(pStable, schema[i].name) > 0) {
S
Shengliang Guan 已提交
1186 1187
      mError("msg:%p, app:%p stable:%s, add tag, tag:%s already exist", pMsg, pMsg->rpcMsg.ahandle,
             pStable->info.tableId, schema[i].name);
1188
      return TSDB_CODE_MND_FIELD_ALREAY_EXIST;
S
slguan 已提交
1189 1190 1191 1192 1193 1194
    }
  }

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

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

S
slguan 已提交
1197
  SSchema *tschema = (SSchema *)(pStable->schema + pStable->numOfColumns + pStable->numOfTags);
S
slguan 已提交
1198 1199 1200 1201
  for (int32_t i = 0; i < ntags; i++) {
    tschema[i].colId = pStable->nextColId++;
  }

S
slguan 已提交
1202
  pStable->numOfTags += ntags;
S
TD-355  
Shengliang Guan 已提交
1203
  pStable->tversion++;
S
slguan 已提交
1204

S
Shengliang Guan 已提交
1205
  mInfo("msg:%p, app:%p stable %s, start to add tag %s", pMsg, pMsg->rpcMsg.ahandle, pStable->info.tableId,
1206 1207
         schema[0].name);

S
TD-2046  
Shengliang Guan 已提交
1208
  SSdbRow row = {
S
TD-2045  
Shengliang Guan 已提交
1209 1210
    .type   = SDB_OPER_GLOBAL,
    .pTable = tsSuperTableSdb,
S
TD-2046  
Shengliang Guan 已提交
1211
    .pObj   = pStable,
S
TD-2045  
Shengliang Guan 已提交
1212 1213
    .pMsg   = pMsg,
    .fpRsp  = mnodeAddSuperTableTagCb
S
[TD-17]  
slguan 已提交
1214
  };
S
slguan 已提交
1215

S
TD-2046  
Shengliang Guan 已提交
1216
  return sdbUpdateRow(&row);
S
slguan 已提交
1217 1218
}

1219
static int32_t mnodeDropSuperTableTagCb(SMnodeMsg *pMsg, int32_t code) {
S
TD-1915  
Shengliang Guan 已提交
1220
  SSTableObj *pStable = (SSTableObj *)pMsg->pTable;
S
Shengliang Guan 已提交
1221
  mLInfo("msg:%p, app:%p stable %s, drop tag result:%s", pMsg, pMsg->rpcMsg.ahandle, pStable->info.tableId,
1222 1223 1224 1225 1226
          tstrerror(code));
  return code;
}

static int32_t mnodeDropSuperTableTag(SMnodeMsg *pMsg, char *tagName) {
S
TD-1915  
Shengliang Guan 已提交
1227
  SSTableObj *pStable = (SSTableObj *)pMsg->pTable;
1228
  int32_t col = mnodeFindSuperTableTagIndex(pStable, tagName);
S
slguan 已提交
1229
  if (col < 0) {
S
Shengliang Guan 已提交
1230
    mError("msg:%p, app:%p stable:%s, drop tag, tag:%s not exist", pMsg, pMsg->rpcMsg.ahandle, pStable->info.tableId,
S
Shengliang Guan 已提交
1231
           tagName);
1232
    return TSDB_CODE_MND_TAG_NOT_EXIST;
S
slguan 已提交
1233
  }
S
slguan 已提交
1234

S
slguan 已提交
1235 1236
  memmove(pStable->schema + pStable->numOfColumns + col, pStable->schema + pStable->numOfColumns + col + 1,
          sizeof(SSchema) * (pStable->numOfTags - col - 1));
S
slguan 已提交
1237
  pStable->numOfTags--;
S
TD-355  
Shengliang Guan 已提交
1238
  pStable->tversion++;
S
slguan 已提交
1239

S
Shengliang Guan 已提交
1240
  mInfo("msg:%p, app:%p stable %s, start to drop tag %s", pMsg, pMsg->rpcMsg.ahandle, pStable->info.tableId, tagName);
1241

S
TD-2046  
Shengliang Guan 已提交
1242
  SSdbRow row = {
S
TD-2045  
Shengliang Guan 已提交
1243 1244
    .type   = SDB_OPER_GLOBAL,
    .pTable = tsSuperTableSdb,
S
TD-2046  
Shengliang Guan 已提交
1245
    .pObj   = pStable,
S
TD-2045  
Shengliang Guan 已提交
1246 1247
    .pMsg   = pMsg,
    .fpRsp  = mnodeDropSuperTableTagCb
S
[TD-17]  
slguan 已提交
1248
  };
S
slguan 已提交
1249

S
TD-2046  
Shengliang Guan 已提交
1250
  return sdbUpdateRow(&row);
S
slguan 已提交
1251 1252
}

1253
static int32_t mnodeModifySuperTableTagNameCb(SMnodeMsg *pMsg, int32_t code) {
S
TD-1915  
Shengliang Guan 已提交
1254
  SSTableObj *pStable = (SSTableObj *)pMsg->pTable;
S
Shengliang Guan 已提交
1255
  mLInfo("msg:%p, app:%p stable %s, modify tag result:%s", pMsg, pMsg->rpcMsg.ahandle, pStable->info.tableId,
1256 1257 1258 1259 1260
         tstrerror(code));
  return code;
}

static int32_t mnodeModifySuperTableTagName(SMnodeMsg *pMsg, char *oldTagName, char *newTagName) {
S
TD-1915  
Shengliang Guan 已提交
1261
  SSTableObj *pStable = (SSTableObj *)pMsg->pTable;
1262
  int32_t col = mnodeFindSuperTableTagIndex(pStable, oldTagName);
S
slguan 已提交
1263
  if (col < 0) {
S
Shengliang Guan 已提交
1264
    mError("msg:%p, app:%p stable:%s, failed to modify table tag, oldName: %s, newName: %s", pMsg, pMsg->rpcMsg.ahandle,
S
Shengliang Guan 已提交
1265
           pStable->info.tableId, oldTagName, newTagName);
1266
    return TSDB_CODE_MND_TAG_NOT_EXIST;
S
slguan 已提交
1267 1268 1269
  }

  // int32_t  rowSize = 0;
S
TD-1207  
Shengliang Guan 已提交
1270
  uint32_t len = (int32_t)strlen(newTagName);
S
slguan 已提交
1271
  if (len >= TSDB_COL_NAME_LEN) {
1272
    return TSDB_CODE_MND_COL_NAME_TOO_LONG;
S
slguan 已提交
1273
  }
S
slguan 已提交
1274

1275
  if (mnodeFindSuperTableTagIndex(pStable, newTagName) >= 0) {
1276
    return TSDB_CODE_MND_TAG_ALREAY_EXIST;
S
slguan 已提交
1277
  }
1278
  
S
slguan 已提交
1279
  // update
S
slguan 已提交
1280
  SSchema *schema = (SSchema *) (pStable->schema + pStable->numOfColumns + col);
B
Bomin Zhang 已提交
1281
  tstrncpy(schema->name, newTagName, sizeof(schema->name));
S
slguan 已提交
1282

S
Shengliang Guan 已提交
1283
  mInfo("msg:%p, app:%p stable %s, start to modify tag %s to %s", pMsg, pMsg->rpcMsg.ahandle, pStable->info.tableId,
1284 1285
         oldTagName, newTagName);

S
TD-2046  
Shengliang Guan 已提交
1286
  SSdbRow row = {
S
TD-2045  
Shengliang Guan 已提交
1287 1288
    .type   = SDB_OPER_GLOBAL,
    .pTable = tsSuperTableSdb,
S
TD-2046  
Shengliang Guan 已提交
1289
    .pObj   = pStable,
S
TD-2045  
Shengliang Guan 已提交
1290 1291
    .pMsg   = pMsg,
    .fpRsp  = mnodeModifySuperTableTagNameCb
S
[TD-17]  
slguan 已提交
1292
  };
S
slguan 已提交
1293

S
TD-2046  
Shengliang Guan 已提交
1294
  return sdbUpdateRow(&row);
S
slguan 已提交
1295 1296
}

S
TD-1915  
Shengliang Guan 已提交
1297
static int32_t mnodeFindSuperTableColumnIndex(SSTableObj *pStable, char *colName) {
S
slguan 已提交
1298
  SSchema *schema = (SSchema *) pStable->schema;
S
slguan 已提交
1299 1300 1301
  for (int32_t col = 0; col < pStable->numOfColumns; col++) {
    if (strcasecmp(schema[col].name, colName) == 0) {
      return col;
S
slguan 已提交
1302 1303 1304 1305 1306 1307
    }
  }

  return -1;
}

1308
static int32_t mnodeAddSuperTableColumnCb(SMnodeMsg *pMsg, int32_t code) {
S
TD-1915  
Shengliang Guan 已提交
1309
  SSTableObj *pStable = (SSTableObj *)pMsg->pTable;
S
Shengliang Guan 已提交
1310
  mLInfo("msg:%p, app:%p stable %s, add column result:%s", pMsg, pMsg->rpcMsg.ahandle, pStable->info.tableId,
1311 1312 1313 1314 1315
          tstrerror(code));
  return code;
}

static int32_t mnodeAddSuperTableColumn(SMnodeMsg *pMsg, SSchema schema[], int32_t ncols) {
1316
  SDbObj *pDb = pMsg->pDb;
S
TD-1915  
Shengliang Guan 已提交
1317
  SSTableObj *pStable = (SSTableObj *)pMsg->pTable;
S
slguan 已提交
1318
  if (ncols <= 0) {
S
Shengliang Guan 已提交
1319
    mError("msg:%p, app:%p stable:%s, add column, ncols:%d <= 0", pMsg, pMsg->rpcMsg.ahandle, pStable->info.tableId, ncols);
1320
    return TSDB_CODE_MND_APP_ERROR;
S
slguan 已提交
1321 1322 1323
  }

  for (int32_t i = 0; i < ncols; i++) {
1324
    if (mnodeFindSuperTableColumnIndex(pStable, schema[i].name) > 0) {
S
Shengliang Guan 已提交
1325
      mError("msg:%p, app:%p stable:%s, add column, column:%s already exist", pMsg, pMsg->rpcMsg.ahandle,
S
Shengliang Guan 已提交
1326
             pStable->info.tableId, schema[i].name);
1327
      return TSDB_CODE_MND_FIELD_ALREAY_EXIST;
S
slguan 已提交
1328 1329
    }

1330
    if (mnodeFindSuperTableTagIndex(pStable, schema[i].name) > 0) {
S
Shengliang Guan 已提交
1331
      mError("msg:%p, app:%p stable:%s, add column, tag:%s already exist", pMsg, pMsg->rpcMsg.ahandle,
S
Shengliang Guan 已提交
1332
             pStable->info.tableId, schema[i].name);
1333
      return TSDB_CODE_MND_TAG_ALREAY_EXIST;
S
slguan 已提交
1334 1335 1336 1337 1338 1339
    }
  }

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

S
slguan 已提交
1340 1341 1342
  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 已提交
1343

L
liu0x54 已提交
1344
  SSchema *tschema = (SSchema *) (pStable->schema + pStable->numOfColumns);
S
slguan 已提交
1345 1346 1347 1348 1349 1350 1351
  for (int32_t i = 0; i < ncols; i++) {
    tschema[i].colId = pStable->nextColId++;
  }

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

1352
  SAcctObj *pAcct = mnodeGetAcct(pDb->acct);
S
[TD-17]  
slguan 已提交
1353 1354
  if (pAcct != NULL) {
    pAcct->acctInfo.numOfTimeSeries += (ncols * pStable->numOfTables);
1355
    mnodeDecAcctRef(pAcct);
S
[TD-17]  
slguan 已提交
1356
  }
S
slguan 已提交
1357

S
Shengliang Guan 已提交
1358
  mInfo("msg:%p, app:%p stable %s, start to add column", pMsg, pMsg->rpcMsg.ahandle, pStable->info.tableId);
1359

S
TD-2046  
Shengliang Guan 已提交
1360
  SSdbRow row = {
S
TD-2045  
Shengliang Guan 已提交
1361 1362
    .type   = SDB_OPER_GLOBAL,
    .pTable = tsSuperTableSdb,
S
TD-2046  
Shengliang Guan 已提交
1363
    .pObj   = pStable,
S
TD-2045  
Shengliang Guan 已提交
1364 1365
    .pMsg   = pMsg,
    .fpRsp  = mnodeAddSuperTableColumnCb
S
[TD-17]  
slguan 已提交
1366 1367
  };

S
TD-2046  
Shengliang Guan 已提交
1368
 return sdbUpdateRow(&row);
S
slguan 已提交
1369 1370
}

1371
static int32_t mnodeDropSuperTableColumnCb(SMnodeMsg *pMsg, int32_t code) {
S
TD-1915  
Shengliang Guan 已提交
1372
  SSTableObj *pStable = (SSTableObj *)pMsg->pTable;
S
Shengliang Guan 已提交
1373
  mLInfo("msg:%p, app:%p stable %s, delete column result:%s", pMsg, pMsg->rpcMsg.ahandle, pStable->info.tableId,
1374 1375 1376 1377 1378
         tstrerror(code));
  return code;
}

static int32_t mnodeDropSuperTableColumn(SMnodeMsg *pMsg, char *colName) {
1379
  SDbObj *pDb = pMsg->pDb;
S
TD-1915  
Shengliang Guan 已提交
1380
  SSTableObj *pStable = (SSTableObj *)pMsg->pTable;
1381
  int32_t col = mnodeFindSuperTableColumnIndex(pStable, colName);
S
slguan 已提交
1382
  if (col <= 0) {
S
Shengliang Guan 已提交
1383
    mError("msg:%p, app:%p stable:%s, drop column, column:%s not exist", pMsg, pMsg->rpcMsg.ahandle, pStable->info.tableId,
S
Shengliang Guan 已提交
1384
           colName);
1385
    return TSDB_CODE_MND_FIELD_NOT_EXIST;
S
slguan 已提交
1386 1387
  }

S
slguan 已提交
1388
  memmove(pStable->schema + col, pStable->schema + col + 1,
S
slguan 已提交
1389 1390 1391 1392 1393 1394 1395 1396
          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);

1397
  SAcctObj *pAcct = mnodeGetAcct(pDb->acct);
S
[TD-17]  
slguan 已提交
1398 1399
  if (pAcct != NULL) {
    pAcct->acctInfo.numOfTimeSeries -= pStable->numOfTables;
1400
    mnodeDecAcctRef(pAcct);
S
[TD-17]  
slguan 已提交
1401
  }
S
slguan 已提交
1402

S
Shengliang Guan 已提交
1403
  mInfo("msg:%p, app:%p stable %s, start to delete column", pMsg, pMsg->rpcMsg.ahandle, pStable->info.tableId);
1404

S
TD-2046  
Shengliang Guan 已提交
1405
  SSdbRow row = {
S
TD-2045  
Shengliang Guan 已提交
1406 1407
    .type   = SDB_OPER_GLOBAL,
    .pTable = tsSuperTableSdb,
S
TD-2046  
Shengliang Guan 已提交
1408
    .pObj   = pStable,
S
TD-2045  
Shengliang Guan 已提交
1409 1410
    .pMsg   = pMsg,
    .fpRsp  = mnodeDropSuperTableColumnCb
S
[TD-17]  
slguan 已提交
1411 1412
  };

S
TD-2046  
Shengliang Guan 已提交
1413
  return sdbUpdateRow(&row);
S
slguan 已提交
1414 1415
}

S
Shengliang Guan 已提交
1416
static int32_t mnodeChangeSuperTableColumnCb(SMnodeMsg *pMsg, int32_t code) {
S
TD-1915  
Shengliang Guan 已提交
1417
  SSTableObj *pStable = (SSTableObj *)pMsg->pTable;
S
Shengliang Guan 已提交
1418
  mLInfo("msg:%p, app:%p stable %s, change column result:%s", pMsg, pMsg->rpcMsg.ahandle, pStable->info.tableId,
S
Shengliang Guan 已提交
1419 1420 1421 1422 1423
         tstrerror(code));
  return code;
}

static int32_t mnodeChangeSuperTableColumn(SMnodeMsg *pMsg, char *oldName, char *newName) {
S
TD-1915  
Shengliang Guan 已提交
1424
  SSTableObj *pStable = (SSTableObj *)pMsg->pTable;
S
Shengliang Guan 已提交
1425 1426
  int32_t col = mnodeFindSuperTableColumnIndex(pStable, oldName);
  if (col < 0) {
S
Shengliang Guan 已提交
1427
    mError("msg:%p, app:%p stable:%s, change column, oldName:%s, newName:%s", pMsg, pMsg->rpcMsg.ahandle,
S
Shengliang Guan 已提交
1428 1429 1430 1431 1432
           pStable->info.tableId, oldName, newName);
    return TSDB_CODE_MND_FIELD_NOT_EXIST;
  }

  // int32_t  rowSize = 0;
S
TD-1207  
Shengliang Guan 已提交
1433
  uint32_t len = (uint32_t)strlen(newName);
S
Shengliang Guan 已提交
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
  if (len >= TSDB_COL_NAME_LEN) {
    return TSDB_CODE_MND_COL_NAME_TOO_LONG;
  }

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

S
Shengliang Guan 已提交
1446
  mInfo("msg:%p, app:%p stable %s, start to modify column %s to %s", pMsg, pMsg->rpcMsg.ahandle, pStable->info.tableId,
S
Shengliang Guan 已提交
1447 1448
         oldName, newName);

S
TD-2046  
Shengliang Guan 已提交
1449
  SSdbRow row = {
S
TD-2045  
Shengliang Guan 已提交
1450 1451
    .type   = SDB_OPER_GLOBAL,
    .pTable = tsSuperTableSdb,
S
TD-2046  
Shengliang Guan 已提交
1452
    .pObj   = pStable,
S
TD-2045  
Shengliang Guan 已提交
1453 1454
    .pMsg   = pMsg,
    .fpRsp  = mnodeChangeSuperTableColumnCb
S
Shengliang Guan 已提交
1455 1456
  };

S
TD-2046  
Shengliang Guan 已提交
1457
  return sdbUpdateRow(&row);
S
Shengliang Guan 已提交
1458 1459
}

S
slguan 已提交
1460
// show super tables
1461 1462
static int32_t mnodeGetShowSuperTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
  SDbObj *pDb = mnodeGetDb(pShow->db);
1463
  if (pDb == NULL) return TSDB_CODE_MND_DB_NOT_SELECTED;
H
Hui Li 已提交
1464 1465 1466
    
  if (pDb->status != TSDB_DB_STATUS_READY) {
    mError("db:%s, status:%d, in dropping", pDb->name, pDb->status);
S
Shengliang Guan 已提交
1467
    mnodeDecDbRef(pDb);
H
Hui Li 已提交
1468 1469
    return TSDB_CODE_MND_DB_IN_DROPPING;
  }
S
slguan 已提交
1470 1471 1472 1473

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

H
Haojun Liao 已提交
1474 1475 1476
  SSchema tbnameSchema = tGetTableNameColumnSchema();
  pShow->bytes[cols] = tbnameSchema.bytes;
  pSchema[cols].type = tbnameSchema.type;
S
slguan 已提交
1477 1478 1479 1480 1481 1482
  strcpy(pSchema[cols].name, "name");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
1483
  strcpy(pSchema[cols].name, "created_time");
S
slguan 已提交
1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513
  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];

1514
  mnodeDecDbRef(pDb);
S
slguan 已提交
1515 1516 1517
  return 0;
}

S
slguan 已提交
1518
// retrieve super tables
1519
int32_t mnodeRetrieveShowSuperTables(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
S
slguan 已提交
1520 1521 1522
  int32_t         numOfRows = 0;
  char *          pWrite;
  int32_t         cols = 0;
S
TD-1915  
Shengliang Guan 已提交
1523
  SSTableObj *pTable = NULL;
S
Shengliang Guan 已提交
1524
  char            prefix[64] = {0};
S
slguan 已提交
1525 1526
  int32_t         prefixLen;

1527
  SDbObj *pDb = mnodeGetDb(pShow->db);
S
slguan 已提交
1528
  if (pDb == NULL) return 0;
H
Hui Li 已提交
1529 1530 1531
  
  if (pDb->status != TSDB_DB_STATUS_READY) {
    mError("db:%s, status:%d, in dropping", pDb->name, pDb->status);
S
Shengliang Guan 已提交
1532
    mnodeDecDbRef(pDb);
H
Hui Li 已提交
1533 1534
    return 0;
  }
S
slguan 已提交
1535

S
Shengliang Guan 已提交
1536
  tstrncpy(prefix, pDb->name, 64);
S
slguan 已提交
1537
  strcat(prefix, TS_PATH_DELIMITER);
S
TD-1207  
Shengliang Guan 已提交
1538
  prefixLen = (int32_t)strlen(prefix);
S
slguan 已提交
1539 1540

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

S
slguan 已提交
1543
  while (numOfRows < rows) {    
1544
    pShow->pIter = mnodeGetNextSuperTable(pShow->pIter, &pTable);
S
slguan 已提交
1545 1546
    if (pTable == NULL) break;
    if (strncmp(pTable->info.tableId, prefix, prefixLen)) {
1547
      mnodeDecTableRef(pTable);
S
slguan 已提交
1548 1549 1550 1551
      continue;
    }

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

B
Bomin Zhang 已提交
1554
    if (pShow->payloadLen > 0 && patternMatch(pShow->payload, stableName, sizeof(stableName) - 1, &info) != TSDB_PATTERN_MATCH) {
1555
      mnodeDecTableRef(pTable);
S
slguan 已提交
1556
      continue;
S
Shengliang Guan 已提交
1557
    }
S
slguan 已提交
1558 1559 1560 1561

    cols = 0;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
H
hjxilinx 已提交
1562
  
S
TD-1207  
Shengliang Guan 已提交
1563
    int16_t len = (int16_t)strnlen(stableName, TSDB_TABLE_NAME_LEN - 1);
H
hjxilinx 已提交
1564 1565 1566 1567
    *(int16_t*) pWrite = len;
    pWrite += sizeof(int16_t); // todo refactor
  
    strncpy(pWrite, stableName, len);
S
slguan 已提交
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586
    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++;
1587
    mnodeDecTableRef(pTable);
S
slguan 已提交
1588 1589 1590
  }

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

1592
  mnodeVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
1593
  mnodeDecDbRef(pDb);
S
slguan 已提交
1594

S
slguan 已提交
1595 1596 1597
  return numOfRows;
}

1598
void mnodeDropAllSuperTables(SDbObj *pDropDb) {
S
Shengliang Guan 已提交
1599
  void *  pIter= NULL;
S
slguan 已提交
1600
  int32_t numOfTables = 0;
S
TD-1915  
Shengliang Guan 已提交
1601
  SSTableObj *pTable = NULL;
S
slguan 已提交
1602

S
TD-1743  
Shengliang Guan 已提交
1603 1604 1605
  char prefix[64] = {0};
  tstrncpy(prefix, pDropDb->name, 64);
  strcat(prefix, TS_PATH_DELIMITER);
S
TD-1207  
Shengliang Guan 已提交
1606
  int32_t prefixLen = (int32_t)strlen(prefix);
S
TD-1743  
Shengliang Guan 已提交
1607

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

S
slguan 已提交
1610
  while (1) {
1611
    pIter = mnodeGetNextSuperTable(pIter, &pTable);
S
slguan 已提交
1612
    if (pTable == NULL) break;
S
slguan 已提交
1613

S
TD-1743  
Shengliang Guan 已提交
1614
    if (strncmp(prefix, pTable->info.tableId, prefixLen) == 0) {
S
TD-2046  
Shengliang Guan 已提交
1615
      SSdbRow row = {
S
TD-2046  
Shengliang Guan 已提交
1616 1617
        .type   = SDB_OPER_LOCAL,
        .pTable = tsSuperTableSdb,
S
TD-2046  
Shengliang Guan 已提交
1618
        .pObj   = pTable,
S
slguan 已提交
1619
      };
S
TD-2046  
Shengliang Guan 已提交
1620
      sdbDeleteRow(&row);
S
slguan 已提交
1621 1622
      numOfTables ++;
    }
S
slguan 已提交
1623

1624
    mnodeDecTableRef(pTable);
S
slguan 已提交
1625
  }
S
slguan 已提交
1626

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

S
TD-1915  
Shengliang Guan 已提交
1630
static int32_t mnodeSetSchemaFromSuperTable(SSchema *pSchema, SSTableObj *pTable) {
S
slguan 已提交
1631
  int32_t numOfCols = pTable->numOfColumns + pTable->numOfTags;
1632 1633
  assert(numOfCols <= TSDB_MAX_COLUMNS);
  
S
slguan 已提交
1634
  for (int32_t i = 0; i < numOfCols; ++i) {
B
Bomin Zhang 已提交
1635
    tstrncpy(pSchema->name, pTable->schema[i].name, sizeof(pSchema->name));
S
slguan 已提交
1636 1637 1638 1639 1640 1641 1642 1643 1644
    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);
}

1645
static int32_t mnodeGetSuperTableMeta(SMnodeMsg *pMsg) {
S
TD-1915  
Shengliang Guan 已提交
1646
  SSTableObj *pTable = (SSTableObj *)pMsg->pTable;
1647
  STableMetaMsg *pMeta   = rpcMallocCont(sizeof(STableMetaMsg) + sizeof(SSchema) * (TSDB_MAX_TAGS + TSDB_MAX_COLUMNS + 16));
1648 1649 1650
  if (pMeta == NULL) {
    return TSDB_CODE_MND_OUT_OF_MEMORY;
  }
S
slguan 已提交
1651 1652
  pMeta->uid          = htobe64(pTable->uid);
  pMeta->sversion     = htons(pTable->sversion);
1653
  pMeta->tversion     = htons(pTable->tversion);
S
slguan 已提交
1654
  pMeta->precision    = pMsg->pDb->cfg.precision;
S
slguan 已提交
1655 1656 1657
  pMeta->numOfTags    = (uint8_t)pTable->numOfTags;
  pMeta->numOfColumns = htons((int16_t)pTable->numOfColumns);
  pMeta->tableType    = pTable->info.type;
1658
  pMeta->contLen      = sizeof(STableMetaMsg) + mnodeSetSchemaFromSuperTable(pMeta->schema, pTable);
B
Bomin Zhang 已提交
1659
  tstrncpy(pMeta->tableId, pTable->info.tableId, sizeof(pMeta->tableId));
S
slguan 已提交
1660

B
Bomin Zhang 已提交
1661
  pMsg->rpcRsp.len = pMeta->contLen;
S
slguan 已提交
1662 1663
  pMeta->contLen = htons(pMeta->contLen);

1664
  pMsg->rpcRsp.rsp = pMeta;
S
Shengliang Guan 已提交
1665

S
TD-2367  
Shengliang Guan 已提交
1666 1667
  mDebug("msg:%p, app:%p stable:%s, uid:%" PRIu64 " table meta is retrieved, sizeOfVgList:%d numOfTables:%d", pMsg,
         pMsg->rpcMsg.ahandle, pTable->info.tableId, pTable->uid, taosHashGetSize(pTable->vgHash), pTable->numOfTables);
1668
  return TSDB_CODE_SUCCESS;
S
slguan 已提交
1669 1670
}

1671
static int32_t mnodeProcessSuperTableVgroupMsg(SMnodeMsg *pMsg) {
S
TD-1732  
Shengliang Guan 已提交
1672
  SSTableVgroupMsg *pInfo = pMsg->rpcMsg.pCont;
H
hjxilinx 已提交
1673
  int32_t numOfTable = htonl(pInfo->numOfTables);
1674

S
scripts  
Shengliang Guan 已提交
1675
  // reserve space
S
TD-1732  
Shengliang Guan 已提交
1676
  int32_t contLen = sizeof(SSTableVgroupRspMsg) + 32 * sizeof(SVgroupMsg) + sizeof(SVgroupsMsg);
1677
  for (int32_t i = 0; i < numOfTable; ++i) {
S
TD-1732  
Shengliang Guan 已提交
1678
    char *stableName = (char *)pInfo + sizeof(SSTableVgroupMsg) + (TSDB_TABLE_FNAME_LEN)*i;
S
TD-1915  
Shengliang Guan 已提交
1679
    SSTableObj *pTable = mnodeGetSuperTable(stableName);
1680
    if (pTable != NULL && pTable->vgHash != NULL) {
S
TD-1732  
Shengliang Guan 已提交
1681 1682 1683
      contLen += (taosHashGetSize(pTable->vgHash) * sizeof(SVgroupMsg) + sizeof(SVgroupsMsg));
    }

1684
    mnodeDecTableRef(pTable);
1685 1686
  }

S
TD-1732  
Shengliang Guan 已提交
1687
  SSTableVgroupRspMsg *pRsp = rpcMallocCont(contLen);
S
slguan 已提交
1688
  if (pRsp == NULL) {
1689
    return TSDB_CODE_MND_OUT_OF_MEMORY;
1690
  }
1691

1692
  pRsp->numOfTables = 0;
S
TD-1732  
Shengliang Guan 已提交
1693
  char *msg = (char *)pRsp + sizeof(SSTableVgroupRspMsg);
1694 1695

  for (int32_t i = 0; i < numOfTable; ++i) {
S
TD-2367  
Shengliang Guan 已提交
1696
    char *stableName = (char *)pInfo + sizeof(SSTableVgroupMsg) + (TSDB_TABLE_FNAME_LEN)*i;
S
TD-1915  
Shengliang Guan 已提交
1697
    SSTableObj *pTable = mnodeGetSuperTable(stableName);
1698
    if (pTable == NULL) {
S
Shengliang Guan 已提交
1699
      mError("msg:%p, app:%p stable:%s, not exist while get stable vgroup info", pMsg, pMsg->rpcMsg.ahandle, stableName);
1700 1701 1702 1703
      mnodeDecTableRef(pTable);
      continue;
    }
    if (pTable->vgHash == NULL) {
S
Shengliang Guan 已提交
1704
      mDebug("msg:%p, app:%p stable:%s, no vgroup exist while get stable vgroup info", pMsg, pMsg->rpcMsg.ahandle,
S
Shengliang Guan 已提交
1705
             stableName);
1706 1707
      mnodeDecTableRef(pTable);

1708 1709
      // even this super table has no corresponding table, still return
      pRsp->numOfTables++;
1710

H
Haojun Liao 已提交
1711 1712
      SVgroupsMsg *pVgroupMsg = (SVgroupsMsg *)msg;
      pVgroupMsg->numOfVgroups = 0;
1713
      
H
Haojun Liao 已提交
1714
      msg += sizeof(SVgroupsMsg);
1715
    } else {
H
Haojun Liao 已提交
1716
      SVgroupsMsg *pVgroupMsg = (SVgroupsMsg *)msg;
S
TD-2367  
Shengliang Guan 已提交
1717 1718
      mDebug("msg:%p, app:%p stable:%s, hash:%p sizeOfVgList:%d will be returned", pMsg, pMsg->rpcMsg.ahandle,
             pTable->info.tableId, pTable->vgHash, taosHashGetSize(pTable->vgHash));
1719

S
Shengliang Guan 已提交
1720 1721 1722 1723 1724
      int32_t *pVgId = taosHashIterate(pTable->vgHash, NULL);
      int32_t  vgSize = 0;
      while (pVgId) {
        SVgObj *pVgroup = mnodeGetVgroup(*pVgId);
        pVgId = taosHashIterate(pTable->vgHash, pVgId);
1725
        if (pVgroup == NULL) continue;
1726

H
Haojun Liao 已提交
1727
        pVgroupMsg->vgroups[vgSize].vgId = htonl(pVgroup->vgId);
H
Haojun Liao 已提交
1728 1729
        pVgroupMsg->vgroups[vgSize].numOfEps = 0;

1730 1731 1732
        for (int32_t vn = 0; vn < pVgroup->numOfVnodes; ++vn) {
          SDnodeObj *pDnode = pVgroup->vnodeGid[vn].pDnode;
          if (pDnode == NULL) break;
1733

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

H
Haojun Liao 已提交
1737
          pVgroupMsg->vgroups[vgSize].numOfEps++;
1738
        }
1739

1740 1741 1742
        vgSize++;
        mnodeDecVgroupRef(pVgroup);
      }
1743

S
Shengliang Guan 已提交
1744
      taosHashCancelIterate(pTable->vgHash, pVgId);
1745 1746
      mnodeDecTableRef(pTable);

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

1749
      // one table is done, try the next table
S
TD-1732  
Shengliang Guan 已提交
1750
      msg += sizeof(SVgroupsMsg) + vgSize * sizeof(SVgroupMsg);
1751 1752
      pRsp->numOfTables++;
    }
1753
  }
S
slguan 已提交
1754

1755 1756
  if (pRsp->numOfTables != numOfTable) {
    rpcFreeCont(pRsp);
1757
    return TSDB_CODE_MND_INVALID_TABLE_NAME;
1758
  } else {
S
TD-1207  
Shengliang Guan 已提交
1759
    pRsp->numOfTables = (int32_t)htonl(pRsp->numOfTables);
1760
    pMsg->rpcRsp.rsp = pRsp;
S
TD-1207  
Shengliang Guan 已提交
1761
    pMsg->rpcRsp.len = (int32_t)((char *)msg - (char *)pRsp);
1762

1763 1764
    return TSDB_CODE_SUCCESS;
  }
S
slguan 已提交
1765 1766
}

1767
static void mnodeProcessDropSuperTableRsp(SRpcMsg *rpcMsg) {
1768
  mInfo("drop stable rsp received, result:%s", tstrerror(rpcMsg->code));
S
slguan 已提交
1769 1770
}

1771 1772 1773 1774 1775
static void *mnodeBuildCreateChildTableMsg(SCMCreateTableMsg *pCreateMsg, SCTableObj *pTable) {
  SCreateTableMsg* pMsg = (SCreateTableMsg*) ((char*)pCreateMsg + sizeof(SCMCreateTableMsg));

  char* tagData = NULL;

S
slguan 已提交
1776 1777 1778
  int32_t tagDataLen = 0;
  int32_t totalCols = 0;
  int32_t contLen = 0;
S
Shengliang Guan 已提交
1779
  if (pTable->info.type == TSDB_CHILD_TABLE) {
S
slguan 已提交
1780
    totalCols = pTable->superTable->numOfColumns + pTable->superTable->numOfTags;
S
Shengliang Guan 已提交
1781
    contLen = sizeof(SMDCreateTableMsg) + totalCols * sizeof(SSchema) + pTable->sqlLen;
S
Shengliang Guan 已提交
1782
    if (pMsg != NULL) {
1783 1784 1785 1786
      int32_t nameLen = htonl(*(int32_t*)pMsg->schema);
      char* p = pMsg->schema + nameLen + sizeof(int32_t);

      tagDataLen = htonl(*(int32_t*) p);
H
Hongze Cheng 已提交
1787
      contLen += tagDataLen;
1788 1789

      tagData = p + sizeof(int32_t);
S
Shengliang Guan 已提交
1790
    }
S
slguan 已提交
1791 1792 1793 1794 1795 1796 1797
  } else {
    totalCols = pTable->numOfColumns;
    contLen = sizeof(SMDCreateTableMsg) + totalCols * sizeof(SSchema) + pTable->sqlLen;
  }

  SMDCreateTableMsg *pCreate = rpcMallocCont(contLen);
  if (pCreate == NULL) {
1798
    terrno = TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
1799 1800 1801
    return NULL;
  }

S
Shengliang Guan 已提交
1802
  mnodeExtractTableName(pTable->info.tableId, pCreate->tableId);
S
slguan 已提交
1803 1804 1805 1806
  pCreate->contLen       = htonl(contLen);
  pCreate->vgId          = htonl(pTable->vgId);
  pCreate->tableType     = pTable->info.type;
  pCreate->createdTime   = htobe64(pTable->createdTime);
H
Haojun Liao 已提交
1807
  pCreate->tid           = htonl(pTable->tid);
S
slguan 已提交
1808 1809 1810 1811
  pCreate->sqlDataLen    = htonl(pTable->sqlLen);
  pCreate->uid           = htobe64(pTable->uid);
  
  if (pTable->info.type == TSDB_CHILD_TABLE) {
S
Shengliang Guan 已提交
1812
    mnodeExtractTableName(pTable->superTable->info.tableId, pCreate->superTableId);
S
slguan 已提交
1813 1814 1815
    pCreate->numOfColumns  = htons(pTable->superTable->numOfColumns);
    pCreate->numOfTags     = htons(pTable->superTable->numOfTags);
    pCreate->sversion      = htonl(pTable->superTable->sversion);
1816
    pCreate->tversion      = htonl(pTable->superTable->tversion);
S
slguan 已提交
1817 1818 1819 1820 1821 1822
    pCreate->tagDataLen    = htonl(tagDataLen);
    pCreate->superTableUid = htobe64(pTable->superTable->uid);
  } else {
    pCreate->numOfColumns  = htons(pTable->numOfColumns);
    pCreate->numOfTags     = 0;
    pCreate->sversion      = htonl(pTable->sversion);
1823
    pCreate->tversion      = 0;
S
slguan 已提交
1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840
    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) {
1841
    memcpy(pCreate->data + totalCols * sizeof(SSchema), tagData, tagDataLen);
1842 1843
  }

S
Shengliang Guan 已提交
1844
  if (pTable->info.type == TSDB_STREAM_TABLE) {
1845
    memcpy(pCreate->data + totalCols * sizeof(SSchema), pTable->sql, pTable->sqlLen);
S
slguan 已提交
1846 1847 1848 1849 1850
  }

  return pCreate;
}

S
Shengliang Guan 已提交
1851
static int32_t mnodeDoCreateChildTableFp(SMnodeMsg *pMsg) {
S
TD-1915  
Shengliang Guan 已提交
1852
  SCTableObj *pTable = (SCTableObj *)pMsg->pTable;
S
Shengliang Guan 已提交
1853 1854
  assert(pTable);

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

1858
  SCMCreateTableMsg *pCreate = pMsg->rpcMsg.pCont;
1859
  SMDCreateTableMsg *pMDCreate = mnodeBuildCreateChildTableMsg(pCreate, pTable);
1860 1861 1862 1863
  if (pMDCreate == NULL) {
    return terrno;
  }

1864
  SRpcEpSet epSet = mnodeGetEpSetFromVgroup(pMsg->pVgroup);
1865
  SRpcMsg rpcMsg = {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
1866
      .ahandle = pMsg,
1867 1868 1869 1870 1871 1872
      .pCont   = pMDCreate,
      .contLen = htonl(pMDCreate->contLen),
      .code    = 0,
      .msgType = TSDB_MSG_TYPE_MD_CREATE_TABLE
  };

1873
  dnodeSendMsgToDnode(&epSet, &rpcMsg);
1874 1875 1876
  return TSDB_CODE_MND_ACTION_IN_PROGRESS;
}

S
Shengliang Guan 已提交
1877
static int32_t mnodeDoCreateChildTableCb(SMnodeMsg *pMsg, int32_t code) {
S
TD-1915  
Shengliang Guan 已提交
1878
  SCTableObj *pTable = (SCTableObj *)pMsg->pTable;
1879 1880

  SCreateTableMsg *pCreate = (SCreateTableMsg*) ((char*)pMsg->rpcMsg.pCont + sizeof(SCMCreateTableMsg));
S
Shengliang Guan 已提交
1881 1882 1883 1884
  assert(pTable);

  if (code == TSDB_CODE_SUCCESS) {
    if (pCreate->getMeta) {
S
Shengliang Guan 已提交
1885 1886
      mDebug("msg:%p, app:%p table:%s, created in dnode and continue to get meta, thandle:%p", pMsg,
             pMsg->rpcMsg.ahandle, pTable->info.tableId, pMsg->rpcMsg.handle);
S
Shengliang Guan 已提交
1887 1888

      pMsg->retry = 0;
S
TD-1915  
Shengliang Guan 已提交
1889
      dnodeReprocessMWriteMsg(pMsg);
S
Shengliang Guan 已提交
1890
    } else {
S
Shengliang Guan 已提交
1891
      mDebug("msg:%p, app:%p table:%s, created in dnode, thandle:%p", pMsg, pMsg->rpcMsg.ahandle, pTable->info.tableId,
S
Shengliang Guan 已提交
1892 1893
             pMsg->rpcMsg.handle);

1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905
      if (pMsg->pBatchMasterMsg) {
	++pMsg->pBatchMasterMsg->successed;
	if (pMsg->pBatchMasterMsg->successed + pMsg->pBatchMasterMsg->received
	    >= pMsg->pBatchMasterMsg->expected) {
	  dnodeSendRpcMWriteRsp(pMsg->pBatchMasterMsg, code);
	}

	mnodeDestroySubMsg(pMsg);

	return TSDB_CODE_MND_ACTION_IN_PROGRESS;
      }

S
TD-1915  
Shengliang Guan 已提交
1906
      dnodeSendRpcMWriteRsp(pMsg, TSDB_CODE_SUCCESS);
S
Shengliang Guan 已提交
1907 1908 1909
    }
    return TSDB_CODE_MND_ACTION_IN_PROGRESS;
  } else {
S
Shengliang Guan 已提交
1910 1911
    mError("msg:%p, app:%p table:%s, failed to create table sid:%d, uid:%" PRIu64 ", reason:%s", pMsg,
           pMsg->rpcMsg.ahandle, pTable->info.tableId, pTable->tid, pTable->uid, tstrerror(code));
S
TD-2046  
Shengliang Guan 已提交
1912
    SSdbRow desc = {.type = SDB_OPER_GLOBAL, .pObj = pTable, .pTable = tsChildTableSdb};
S
Shengliang Guan 已提交
1913 1914 1915 1916 1917
    sdbDeleteRow(&desc);
    return code;
  }
}

1918
static int32_t mnodeDoCreateChildTable(SMnodeMsg *pMsg, int32_t tid) {
1919
  SVgObj *pVgroup = pMsg->pVgroup;
1920 1921 1922 1923

  SCMCreateTableMsg *p1 = pMsg->rpcMsg.pCont;
  SCreateTableMsg   *pCreate = (SCreateTableMsg*)((char*)p1 + sizeof(SCMCreateTableMsg));

S
TD-1915  
Shengliang Guan 已提交
1924
  SCTableObj *pTable = calloc(1, sizeof(SCTableObj));
S
slguan 已提交
1925
  if (pTable == NULL) {
S
Shengliang Guan 已提交
1926
    mError("msg:%p, app:%p table:%s, failed to alloc memory", pMsg, pMsg->rpcMsg.ahandle, pCreate->tableId);
1927
    return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
1928 1929
  }

1930 1931
  pTable->info.type    = (pCreate->numOfColumns == 0)? TSDB_CHILD_TABLE:TSDB_NORMAL_TABLE;
  pTable->info.tableId = strdup(pCreate->tableId);
1932
  pTable->createdTime  = taosGetTimestampMs();
H
Haojun Liao 已提交
1933
  pTable->tid          = tid;
1934
  pTable->vgId         = pVgroup->vgId;
S
TD-1719  
Shengliang Guan 已提交
1935

S
slguan 已提交
1936
  if (pTable->info.type == TSDB_CHILD_TABLE) {
1937 1938 1939 1940 1941
    int32_t nameLen = htonl(*(int32_t*) pCreate->schema);
    char* name = (char*)pCreate->schema + sizeof(int32_t);

    char stableName[TSDB_TABLE_FNAME_LEN] = {0};
    memcpy(stableName, name, nameLen);
1942 1943

    char prefix[64] = {0};
1944
    size_t prefixLen = tableIdPrefix(pMsg->pDb->name, prefix, 64);
1945
    if (0 != strncasecmp(prefix, stableName, prefixLen)) {
1946
      mError("msg:%p, app:%p table:%s, corresponding super table:%s not in this db", pMsg, pMsg->rpcMsg.ahandle,
1947
             pCreate->tableId, stableName);
1948
      mnodeDestroyChildTable(pTable);
1949
      return TSDB_CODE_TDB_INVALID_CREATE_TB_MSG;
1950 1951
    }

1952
    if (pMsg->pSTable == NULL) pMsg->pSTable = mnodeGetSuperTable(stableName);
1953
    if (pMsg->pSTable == NULL) {
S
Shengliang Guan 已提交
1954
      mError("msg:%p, app:%p table:%s, corresponding super table:%s does not exist", pMsg, pMsg->rpcMsg.ahandle,
1955
             pCreate->tableId, stableName);
1956
      mnodeDestroyChildTable(pTable);
1957
      return TSDB_CODE_MND_INVALID_TABLE_NAME;
S
slguan 已提交
1958
    }
1959

1960
    pTable->suid = pMsg->pSTable->uid;
H
Haojun Liao 已提交
1961
    pTable->uid = (((uint64_t)pTable->vgId) << 48) + ((((uint64_t)pTable->tid) & ((1ul << 24) - 1ul)) << 24) +
1962
                  ((sdbGetVersion() & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
1963
    pTable->superTable = pMsg->pSTable;
S
slguan 已提交
1964
  } else {
1965 1966 1967 1968
    if (pTable->info.type == TSDB_SUPER_TABLE) {
      int64_t us = taosGetTimestampUs();
      pTable->uid = (us << 24) + ((sdbGetVersion() & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
    } else {
H
Haojun Liao 已提交
1969
      pTable->uid = (((uint64_t)pTable->vgId) << 48) + ((((uint64_t)pTable->tid) & ((1ul << 24) - 1ul)) << 24) +
1970 1971 1972
                    ((sdbGetVersion() & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul));
    }

S
slguan 已提交
1973 1974 1975 1976 1977 1978 1979 1980 1981
    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);
1982
      return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997
    }
    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);
1998
        return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
1999 2000 2001
      }
      memcpy(pTable->sql, (char *) (pCreate->schema) + numOfCols * sizeof(SSchema), pTable->sqlLen);
      pTable->sql[pTable->sqlLen - 1] = 0;
S
Shengliang Guan 已提交
2002
      mDebug("msg:%p, app:%p table:%s, stream sql len:%d sql:%s", pMsg, pMsg->rpcMsg.ahandle, pTable->info.tableId,
S
Shengliang Guan 已提交
2003
             pTable->sqlLen, pTable->sql);
S
slguan 已提交
2004 2005
    }
  }
2006 2007 2008 2009

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

S
TD-2046  
Shengliang Guan 已提交
2010
  SSdbRow desc = {
S
TD-2046  
Shengliang Guan 已提交
2011
    .type   = SDB_OPER_GLOBAL,
S
TD-2046  
Shengliang Guan 已提交
2012
    .pObj   = pTable,
S
TD-2046  
Shengliang Guan 已提交
2013 2014 2015
    .pTable = tsChildTableSdb,
    .pMsg   = pMsg,
    .fpReq  = mnodeDoCreateChildTableFp
S
Shengliang Guan 已提交
2016
  };
S
slguan 已提交
2017
  
2018
  int32_t code = sdbInsertRow(&desc);
S
Shengliang Guan 已提交
2019
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
2020 2021
    mnodeDestroyChildTable(pTable);
    pMsg->pTable = NULL;
S
Shengliang Guan 已提交
2022
    mError("msg:%p, app:%p table:%s, failed to create, reason:%s", pMsg, pMsg->rpcMsg.ahandle, pCreate->tableId,
S
Shengliang Guan 已提交
2023
           tstrerror(code));
S
TD-1038  
Shengliang Guan 已提交
2024
  } else {
S
Shengliang Guan 已提交
2025
    mDebug("msg:%p, app:%p table:%s, allocated in vgroup, vgId:%d sid:%d uid:%" PRIu64, pMsg, pMsg->rpcMsg.ahandle,
H
Haojun Liao 已提交
2026
           pTable->info.tableId, pVgroup->vgId, pTable->tid, pTable->uid);
2027
  }
S
Shengliang Guan 已提交
2028 2029

  return code;
S
slguan 已提交
2030 2031
}

2032
static int32_t mnodeProcessCreateChildTableMsg(SMnodeMsg *pMsg) {
2033
  //SCMCreateTableMsg* p1 = pMsg->rpcMsg.pCont; // there are several tables here.
S
TD-1207  
Shengliang Guan 已提交
2034
  SCreateTableMsg* pCreate = (SCreateTableMsg*)((char *)pMsg->rpcMsg.pCont + sizeof(SCMCreateTableMsg));
2035

S
slguan 已提交
2036 2037
  int32_t code = grantCheck(TSDB_GRANT_TIMESERIES);
  if (code != TSDB_CODE_SUCCESS) {
S
Shengliang Guan 已提交
2038
    mError("msg:%p, app:%p table:%s, failed to create, grant timeseries failed", pMsg, pMsg->rpcMsg.ahandle,
2039
           pCreate->tableId);
2040
    return code;
S
slguan 已提交
2041 2042
  }

2043
  if (pMsg->retry == 0) {
2044
    if (pMsg->pTable == NULL) {
S
Shengliang Guan 已提交
2045
      SVgObj *pVgroup = NULL;
H
Haojun Liao 已提交
2046 2047
      int32_t tid = 0;
      code = mnodeGetAvailableVgroup(pMsg, &pVgroup, &tid);
2048
      if (code != TSDB_CODE_SUCCESS) {
S
Shengliang Guan 已提交
2049
        mDebug("msg:%p, app:%p table:%s, failed to get available vgroup, reason:%s", pMsg, pMsg->rpcMsg.ahandle,
2050 2051
               pCreate->tableId, tstrerror(code));
        return code;
2052 2053
      }

S
Shengliang Guan 已提交
2054 2055
      if (pMsg->pVgroup != NULL) {
        mnodeDecVgroupRef(pMsg->pVgroup);
2056
      }
2057

S
Shengliang Guan 已提交
2058 2059 2060
      pMsg->pVgroup = pVgroup;
      mnodeIncVgroupRef(pVgroup);

H
Haojun Liao 已提交
2061
      return mnodeDoCreateChildTable(pMsg, tid);
2062
    }
2063
  } else {
2064
    if (pMsg->pTable == NULL) pMsg->pTable = mnodeGetTable(pCreate->tableId);
2065
  }
2066

S
slguan 已提交
2067
  if (pMsg->pTable == NULL) {
S
Shengliang Guan 已提交
2068
    mError("msg:%p, app:%p table:%s, object not found, retry:%d reason:%s", pMsg, pMsg->rpcMsg.ahandle, pCreate->tableId, pMsg->retry,
2069
           tstrerror(terrno));
2070
    return terrno;
2071
  } else {
S
Shengliang Guan 已提交
2072
    mDebug("msg:%p, app:%p table:%s, send create msg to vnode again", pMsg, pMsg->rpcMsg.ahandle, pCreate->tableId);
S
Shengliang Guan 已提交
2073
    return mnodeDoCreateChildTableFp(pMsg);
S
slguan 已提交
2074 2075 2076
  }
}

S
Shengliang Guan 已提交
2077
static int32_t mnodeSendDropChildTableMsg(SMnodeMsg *pMsg, bool needReturn) {
S
TD-1915  
Shengliang Guan 已提交
2078
  SCTableObj *pTable = (SCTableObj *)pMsg->pTable;
S
Shengliang Guan 已提交
2079
  mLInfo("msg:%p, app:%p ctable:%s, is dropped from sdb", pMsg, pMsg->rpcMsg.ahandle, pTable->info.tableId);
S
slguan 已提交
2080 2081 2082

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

H
Haojun Liao 已提交
2088
  tstrncpy(pDrop->tableId, pTable->info.tableId, TSDB_TABLE_FNAME_LEN);
S
slguan 已提交
2089 2090
  pDrop->vgId    = htonl(pTable->vgId);
  pDrop->contLen = htonl(sizeof(SMDDropTableMsg));
H
Haojun Liao 已提交
2091
  pDrop->tid     = htonl(pTable->tid);
S
slguan 已提交
2092 2093
  pDrop->uid     = htobe64(pTable->uid);

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

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

S
slguan 已提交
2099
  SRpcMsg rpcMsg = {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
2100
    .ahandle = pMsg,
S
slguan 已提交
2101 2102 2103 2104 2105 2106
    .pCont   = pDrop,
    .contLen = sizeof(SMDDropTableMsg),
    .code    = 0,
    .msgType = TSDB_MSG_TYPE_MD_DROP_TABLE
  };

2107 2108
  if (!needReturn) rpcMsg.ahandle = NULL;

2109
  dnodeSendMsgToDnode(&epSet, &rpcMsg);
2110

2111
  return TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
slguan 已提交
2112 2113
}

S
Shengliang Guan 已提交
2114 2115
static int32_t mnodeDropChildTableCb(SMnodeMsg *pMsg, int32_t code) {
  if (code != TSDB_CODE_SUCCESS) {
S
TD-1915  
Shengliang Guan 已提交
2116
    SCTableObj *pTable = (SCTableObj *)pMsg->pTable;
S
Shengliang Guan 已提交
2117
    mError("msg:%p, app:%p ctable:%s, failed to drop, sdb error", pMsg, pMsg->rpcMsg.ahandle, pTable->info.tableId);
S
Shengliang Guan 已提交
2118 2119 2120 2121 2122 2123 2124
    return code;
  } 

  return mnodeSendDropChildTableMsg(pMsg, true);
}

static int32_t mnodeProcessDropChildTableMsg(SMnodeMsg *pMsg) {
S
TD-1915  
Shengliang Guan 已提交
2125
  SCTableObj *pTable = (SCTableObj *)pMsg->pTable;
S
Shengliang Guan 已提交
2126 2127
  if (pMsg->pVgroup == NULL) pMsg->pVgroup = mnodeGetVgroup(pTable->vgId);
  if (pMsg->pVgroup == NULL) {
S
Shengliang Guan 已提交
2128
    mError("msg:%p, app:%p table:%s, failed to drop ctable, vgroup not exist", pMsg, pMsg->rpcMsg.ahandle,
S
Shengliang Guan 已提交
2129 2130 2131 2132
           pTable->info.tableId);
    return TSDB_CODE_MND_APP_ERROR;
  }

S
TD-2046  
Shengliang Guan 已提交
2133
  SSdbRow row = {
S
TD-2045  
Shengliang Guan 已提交
2134 2135
    .type   = SDB_OPER_GLOBAL,
    .pTable = tsChildTableSdb,
S
TD-2046  
Shengliang Guan 已提交
2136
    .pObj   = pTable,
S
TD-2045  
Shengliang Guan 已提交
2137 2138
    .pMsg   = pMsg,
    .fpRsp  = mnodeDropChildTableCb
S
Shengliang Guan 已提交
2139 2140
  };

S
TD-2046  
Shengliang Guan 已提交
2141
  int32_t code = sdbDeleteRow(&row);
S
Shengliang Guan 已提交
2142
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
2143
    mError("msg:%p, app:%p ctable:%s, failed to drop, reason:%s", pMsg, pMsg->rpcMsg.ahandle, pTable->info.tableId,
S
Shengliang Guan 已提交
2144
           tstrerror(code));
S
Shengliang Guan 已提交
2145
  }
S
Shengliang Guan 已提交
2146

S
Shengliang Guan 已提交
2147 2148 2149
  return code;
}

S
TD-1915  
Shengliang Guan 已提交
2150
static int32_t mnodeFindNormalTableColumnIndex(SCTableObj *pTable, char *colName) {
S
slguan 已提交
2151
  SSchema *schema = (SSchema *) pTable->schema;
S
slguan 已提交
2152 2153 2154
  for (int32_t col = 0; col < pTable->numOfColumns; col++) {
    if (strcasecmp(schema[col].name, colName) == 0) {
      return col;
S
slguan 已提交
2155 2156 2157 2158 2159 2160
    }
  }

  return -1;
}

2161
static int32_t mnodeAlterNormalTableColumnCb(SMnodeMsg *pMsg, int32_t code) {
S
TD-1915  
Shengliang Guan 已提交
2162
  SCTableObj *pTable = (SCTableObj *)pMsg->pTable;
2163
  if (code != TSDB_CODE_SUCCESS) {
S
Shengliang Guan 已提交
2164
    mError("msg:%p, app:%p ctable %s, failed to alter column, reason:%s", pMsg, pMsg->rpcMsg.ahandle, pTable->info.tableId,
2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177
           tstrerror(code));
    return code;
  }

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

  if (pMsg->pVgroup == NULL) {
    pMsg->pVgroup = mnodeGetVgroup(pTable->vgId);
    if (pMsg->pVgroup == NULL) {
      rpcFreeCont(pMDCreate);
S
Shengliang Guan 已提交
2178
      mError("msg:%p, app:%p ctable %s, vgId:%d not exist in mnode", pMsg, pMsg->rpcMsg.ahandle, pTable->info.tableId,
2179 2180 2181 2182 2183
             pTable->vgId);
      return TSDB_CODE_MND_VGROUP_NOT_EXIST;
    }
  }

2184
  SRpcEpSet epSet = mnodeGetEpSetFromVgroup(pMsg->pVgroup);
2185
  SRpcMsg rpcMsg = {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
2186
      .ahandle = pMsg,
2187 2188 2189 2190 2191 2192
      .pCont   = pMDCreate,
      .contLen = htonl(pMDCreate->contLen),
      .code    = 0,
      .msgType = TSDB_MSG_TYPE_MD_ALTER_TABLE
  };

S
Shengliang Guan 已提交
2193
  mDebug("msg:%p, app:%p ctable %s, send alter column msg to vgId:%d", pMsg, pMsg->rpcMsg.ahandle, pTable->info.tableId,
2194 2195
         pMsg->pVgroup->vgId);

2196
  dnodeSendMsgToDnode(&epSet, &rpcMsg);
2197
  return TSDB_CODE_MND_ACTION_IN_PROGRESS;
2198 2199 2200
}

static int32_t mnodeAddNormalTableColumn(SMnodeMsg *pMsg, SSchema schema[], int32_t ncols) {
S
TD-1915  
Shengliang Guan 已提交
2201
  SCTableObj *pTable = (SCTableObj *)pMsg->pTable;
2202
  SDbObj *pDb = pMsg->pDb;
S
slguan 已提交
2203
  if (ncols <= 0) {
S
Shengliang Guan 已提交
2204
    mError("msg:%p, app:%p ctable:%s, add column, ncols:%d <= 0", pMsg, pMsg->rpcMsg.ahandle, pTable->info.tableId, ncols);
2205
    return TSDB_CODE_MND_APP_ERROR;
S
slguan 已提交
2206 2207 2208
  }

  for (int32_t i = 0; i < ncols; i++) {
2209
    if (mnodeFindNormalTableColumnIndex(pTable, schema[i].name) > 0) {
S
Shengliang Guan 已提交
2210
      mError("msg:%p, app:%p ctable:%s, add column, column:%s already exist", pMsg, pMsg->rpcMsg.ahandle,
S
Shengliang Guan 已提交
2211
             pTable->info.tableId, schema[i].name);
2212
      return TSDB_CODE_MND_FIELD_ALREAY_EXIST;
S
slguan 已提交
2213 2214 2215 2216 2217 2218
    }
  }

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

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

S
slguan 已提交
2221
  SSchema *tschema = (SSchema *) (pTable->schema + pTable->numOfColumns);
S
slguan 已提交
2222 2223 2224 2225 2226 2227
  for (int32_t i = 0; i < ncols; i++) {
    tschema[i].colId = pTable->nextColId++;
  }

  pTable->numOfColumns += ncols;
  pTable->sversion++;
S
[TD-17]  
slguan 已提交
2228
  
2229
  SAcctObj *pAcct = mnodeGetAcct(pDb->acct);
S
[TD-17]  
slguan 已提交
2230
  if (pAcct != NULL) {
S
slguan 已提交
2231
    pAcct->acctInfo.numOfTimeSeries += ncols;
2232
    mnodeDecAcctRef(pAcct);
S
[TD-17]  
slguan 已提交
2233
  }
2234

S
Shengliang Guan 已提交
2235
  mInfo("msg:%p, app:%p ctable %s, start to add column", pMsg, pMsg->rpcMsg.ahandle, pTable->info.tableId);
2236

S
TD-2046  
Shengliang Guan 已提交
2237
  SSdbRow row = {
S
TD-2045  
Shengliang Guan 已提交
2238 2239
    .type   = SDB_OPER_GLOBAL,
    .pTable = tsChildTableSdb,
S
TD-2046  
Shengliang Guan 已提交
2240
    .pObj   = pTable,
S
TD-2045  
Shengliang Guan 已提交
2241 2242
    .pMsg   = pMsg,
    .fpRsp  = mnodeAlterNormalTableColumnCb
S
[TD-17]  
slguan 已提交
2243
  };
S
slguan 已提交
2244

S
TD-2046  
Shengliang Guan 已提交
2245
  return sdbUpdateRow(&row);
S
slguan 已提交
2246 2247
}

2248
static int32_t mnodeDropNormalTableColumn(SMnodeMsg *pMsg, char *colName) {
2249
  SDbObj *pDb = pMsg->pDb;
S
TD-1915  
Shengliang Guan 已提交
2250
  SCTableObj *pTable = (SCTableObj *)pMsg->pTable;
2251
  int32_t col = mnodeFindNormalTableColumnIndex(pTable, colName);
S
slguan 已提交
2252
  if (col <= 0) {
S
Shengliang Guan 已提交
2253 2254
    mError("msg:%p, app:%p ctable:%s, drop column, column:%s not exist", pMsg, pMsg->rpcMsg.ahandle,
           pTable->info.tableId, colName);
2255
    return TSDB_CODE_MND_FIELD_NOT_EXIST;
S
slguan 已提交
2256 2257
  }

S
slguan 已提交
2258
  memmove(pTable->schema + col, pTable->schema + col + 1, sizeof(SSchema) * (pTable->numOfColumns - col - 1));
S
slguan 已提交
2259 2260 2261
  pTable->numOfColumns--;
  pTable->sversion++;

2262
  SAcctObj *pAcct = mnodeGetAcct(pDb->acct);
S
[TD-17]  
slguan 已提交
2263 2264
  if (pAcct != NULL) {
    pAcct->acctInfo.numOfTimeSeries--;
2265
    mnodeDecAcctRef(pAcct);
S
[TD-17]  
slguan 已提交
2266
  }
2267

S
Shengliang Guan 已提交
2268
  mInfo("msg:%p, app:%p ctable %s, start to drop column %s", pMsg, pMsg->rpcMsg.ahandle, pTable->info.tableId, colName);
2269

S
TD-2046  
Shengliang Guan 已提交
2270
  SSdbRow row = {
S
TD-2045  
Shengliang Guan 已提交
2271 2272
    .type   = SDB_OPER_GLOBAL,
    .pTable = tsChildTableSdb,
S
TD-2046  
Shengliang Guan 已提交
2273
    .pObj   = pTable,
S
TD-2045  
Shengliang Guan 已提交
2274 2275
    .pMsg   = pMsg,
    .fpRsp  = mnodeAlterNormalTableColumnCb
S
[TD-17]  
slguan 已提交
2276
  };
S
slguan 已提交
2277

S
TD-2046  
Shengliang Guan 已提交
2278
  return sdbUpdateRow(&row);
S
slguan 已提交
2279 2280
}

S
Shengliang Guan 已提交
2281
static int32_t mnodeChangeNormalTableColumn(SMnodeMsg *pMsg, char *oldName, char *newName) {
S
TD-1915  
Shengliang Guan 已提交
2282
  SCTableObj *pTable = (SCTableObj *)pMsg->pTable;
S
Shengliang Guan 已提交
2283 2284
  int32_t col = mnodeFindNormalTableColumnIndex(pTable, oldName);
  if (col < 0) {
S
Shengliang Guan 已提交
2285
    mError("msg:%p, app:%p ctable:%s, change column, oldName: %s, newName: %s", pMsg, pMsg->rpcMsg.ahandle,
S
Shengliang Guan 已提交
2286 2287 2288 2289 2290
           pTable->info.tableId, oldName, newName);
    return TSDB_CODE_MND_FIELD_NOT_EXIST;
  }

  // int32_t  rowSize = 0;
S
TD-1207  
Shengliang Guan 已提交
2291
  uint32_t len = (uint32_t)strlen(newName);
S
Shengliang Guan 已提交
2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303
  if (len >= TSDB_COL_NAME_LEN) {
    return TSDB_CODE_MND_COL_NAME_TOO_LONG;
  }

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

S
Shengliang Guan 已提交
2304
  mInfo("msg:%p, app:%p ctable %s, start to modify column %s to %s", pMsg, pMsg->rpcMsg.ahandle, pTable->info.tableId,
S
Shengliang Guan 已提交
2305 2306
         oldName, newName);

S
TD-2046  
Shengliang Guan 已提交
2307
  SSdbRow row = {
S
TD-2045  
Shengliang Guan 已提交
2308 2309
    .type   = SDB_OPER_GLOBAL,
    .pTable = tsChildTableSdb,
S
TD-2046  
Shengliang Guan 已提交
2310
    .pObj   = pTable,
S
TD-2045  
Shengliang Guan 已提交
2311 2312
    .pMsg   = pMsg,
    .fpRsp  = mnodeAlterNormalTableColumnCb
S
Shengliang Guan 已提交
2313 2314
  };

S
TD-2046  
Shengliang Guan 已提交
2315
  return sdbUpdateRow(&row);
S
Shengliang Guan 已提交
2316 2317
}

S
TD-1915  
Shengliang Guan 已提交
2318
static int32_t mnodeSetSchemaFromNormalTable(SSchema *pSchema, SCTableObj *pTable) {
S
slguan 已提交
2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330
  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);
}

2331
static int32_t mnodeDoGetChildTableMeta(SMnodeMsg *pMsg, STableMetaMsg *pMeta) {
S
slguan 已提交
2332
  SDbObj *pDb = pMsg->pDb;
S
TD-1915  
Shengliang Guan 已提交
2333
  SCTableObj *pTable = (SCTableObj *)pMsg->pTable;
S
slguan 已提交
2334

S
slguan 已提交
2335
  pMeta->uid       = htobe64(pTable->uid);
H
Haojun Liao 已提交
2336
  pMeta->tid       = htonl(pTable->tid);
S
slguan 已提交
2337 2338
  pMeta->precision = pDb->cfg.precision;
  pMeta->tableType = pTable->info.type;
H
Haojun Liao 已提交
2339
  tstrncpy(pMeta->tableId, pTable->info.tableId, TSDB_TABLE_FNAME_LEN);
S
slguan 已提交
2340

2341 2342 2343 2344 2345
  if (pTable->info.type == TSDB_CHILD_TABLE) {
    assert(pTable->superTable != NULL);
    tstrncpy(pMeta->sTableName, pTable->superTable->info.tableId, TSDB_TABLE_FNAME_LEN);

    pMeta->suid         = pTable->superTable->uid;
S
slguan 已提交
2346
    pMeta->sversion     = htons(pTable->superTable->sversion);
2347
    pMeta->tversion     = htons(pTable->superTable->tversion);
S
slguan 已提交
2348
    pMeta->numOfTags    = (int8_t)pTable->superTable->numOfTags;
S
slguan 已提交
2349
    pMeta->numOfColumns = htons((int16_t)pTable->superTable->numOfColumns);
2350
    pMeta->contLen      = sizeof(STableMetaMsg) + mnodeSetSchemaFromSuperTable(pMeta->schema, pTable->superTable);
S
slguan 已提交
2351 2352
  } else {
    pMeta->sversion     = htons(pTable->sversion);
2353
    pMeta->tversion     = 0;
S
slguan 已提交
2354 2355
    pMeta->numOfTags    = 0;
    pMeta->numOfColumns = htons((int16_t)pTable->numOfColumns);
2356
    pMeta->contLen      = sizeof(STableMetaMsg) + mnodeSetSchemaFromNormalTable(pMeta->schema, pTable); 
S
slguan 已提交
2357
  }
S
Shengliang Guan 已提交
2358

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

2366
  for (int32_t i = 0; i < pMsg->pVgroup->numOfVnodes; ++i) {
2367
    SDnodeObj *pDnode = mnodeGetDnode(pMsg->pVgroup->vnodeGid[i].dnodeId);
S
slguan 已提交
2368
    if (pDnode == NULL) break;
2369 2370 2371
    strcpy(pMeta->vgroup.epAddr[i].fqdn, pDnode->dnodeFqdn);
    pMeta->vgroup.epAddr[i].port = htons(pDnode->dnodePort + TSDB_PORT_DNODESHELL);
    pMeta->vgroup.numOfEps++;
2372
    mnodeDecDnodeRef(pDnode);
S
slguan 已提交
2373
  }
2374
  pMeta->vgroup.vgId = htonl(pMsg->pVgroup->vgId);
S
slguan 已提交
2375

S
Shengliang Guan 已提交
2376
  mDebug("msg:%p, app:%p table:%s, uid:%" PRIu64 " table meta is retrieved, vgId:%d sid:%d", pMsg, pMsg->rpcMsg.ahandle,
H
Haojun Liao 已提交
2377
         pTable->info.tableId, pTable->uid, pTable->vgId, pTable->tid);
S
slguan 已提交
2378 2379 2380 2381

  return TSDB_CODE_SUCCESS;
}

S
Shengliang Guan 已提交
2382
static int32_t mnodeAutoCreateChildTable(SMnodeMsg *pMsg) {
S
TD-1732  
Shengliang Guan 已提交
2383
  STableInfoMsg *pInfo = pMsg->rpcMsg.pCont;
2384

2385 2386 2387 2388 2389
  if (pMsg->rpcMsg.contLen <= sizeof(*pInfo)) {
    mError("msg:%p, app:%p table:%s, failed to auto create child table, tags not exist", pMsg, pMsg->rpcMsg.ahandle,
           pInfo->tableId);
    return TSDB_CODE_MND_TAG_NOT_EXIST;
  }
2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401

  char* p = pInfo->tags;
  int32_t nameLen = htonl(*(int32_t*) p);
  p += sizeof(int32_t);
  p += nameLen;

  int32_t tagLen = htonl(*(int32_t*) p);
  p += sizeof(int32_t);

  int32_t totalLen = nameLen + tagLen + sizeof(int32_t)*2;
  if (tagLen == 0 || nameLen == 0) {
    mError("msg:%p, app:%p table:%s, failed to create table on demand for super table is empty, tagLen:%d", pMsg,
S
Shengliang Guan 已提交
2402 2403
           pMsg->rpcMsg.ahandle, pInfo->tableId, tagLen);
    return TSDB_CODE_MND_INVALID_STABLE_NAME;
2404
  }
2405

2406
  int32_t contLen = sizeof(SCMCreateTableMsg) + sizeof(SCreateTableMsg) + totalLen;
S
TD-1915  
Shengliang Guan 已提交
2407
  SCMCreateTableMsg *pCreateMsg = calloc(1, contLen);
S
slguan 已提交
2408
  if (pCreateMsg == NULL) {
S
Shengliang Guan 已提交
2409 2410
    mError("msg:%p, app:%p table:%s, failed to create table while get meta info, no enough memory", pMsg,
           pMsg->rpcMsg.ahandle, pInfo->tableId);
2411
    return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
2412
  }
S
slguan 已提交
2413

2414 2415 2416 2417 2418 2419 2420 2421 2422
  SCreateTableMsg* pCreate = (SCreateTableMsg*) ((char*) pCreateMsg + sizeof(SCMCreateTableMsg));

  size_t size = tListLen(pInfo->tableId);
  tstrncpy(pCreate->tableId, pInfo->tableId, size);
  tstrncpy(pCreate->db, pMsg->pDb->name, sizeof(pCreate->db));
  pCreate->igExists = 1;
  pCreate->getMeta = 1;

  pCreateMsg->numOfTables = htonl(1);
S
slguan 已提交
2423
  pCreateMsg->contLen = htonl(contLen);
B
Bomin Zhang 已提交
2424

2425 2426 2427 2428 2429
  memcpy(pCreate->schema, pInfo->tags, totalLen);

  char name[TSDB_TABLE_FNAME_LEN] = {0};
  memcpy(name, pInfo->tags + sizeof(int32_t), nameLen);

S
Shengliang Guan 已提交
2430
  mDebug("msg:%p, app:%p table:%s, start to create on demand, tagLen:%d stable:%s", pMsg, pMsg->rpcMsg.ahandle,
2431
         pInfo->tableId, tagLen, name);
S
slguan 已提交
2432

S
TD-1915  
Shengliang Guan 已提交
2433 2434 2435
  if (pMsg->rpcMsg.pCont != pMsg->pCont) {
    tfree(pMsg->rpcMsg.pCont);
  }
S
Shengliang Guan 已提交
2436 2437 2438
  pMsg->rpcMsg.msgType = TSDB_MSG_TYPE_CM_CREATE_TABLE;
  pMsg->rpcMsg.pCont = pCreateMsg;
  pMsg->rpcMsg.contLen = contLen;
S
TD-1915  
Shengliang Guan 已提交
2439

2440
  return TSDB_CODE_MND_ACTION_NEED_REPROCESSED;
S
slguan 已提交
2441
}
S
slguan 已提交
2442

2443
static int32_t mnodeGetChildTableMeta(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
2444 2445
  STableMetaMsg *pMeta =
      rpcMallocCont(sizeof(STableMetaMsg) + sizeof(SSchema) * (TSDB_MAX_TAGS + TSDB_MAX_COLUMNS + 16));
S
slguan 已提交
2446
  if (pMeta == NULL) {
S
Shengliang Guan 已提交
2447
    mError("msg:%p, app:%p table:%s, failed to get table meta, no enough memory", pMsg, pMsg->rpcMsg.ahandle,
S
Shengliang Guan 已提交
2448
           pMsg->pTable->tableId);
2449
    return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
2450 2451
  }

2452
  mnodeDoGetChildTableMeta(pMsg, pMeta);
S
slguan 已提交
2453

2454 2455
  pMsg->rpcRsp.len = pMeta->contLen;
  pMsg->rpcRsp.rsp = pMeta;
S
slguan 已提交
2456
  pMeta->contLen = htons(pMeta->contLen);
2457 2458

  return TSDB_CODE_SUCCESS;
S
slguan 已提交
2459 2460
}

2461
void mnodeDropAllChildTablesInVgroups(SVgObj *pVgroup) {
2462 2463
  void *  pIter = NULL;
  int32_t numOfTables = 0;
S
TD-1915  
Shengliang Guan 已提交
2464
  SCTableObj *pTable = NULL;
2465

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

  while (1) {
2469
    pIter = mnodeGetNextChildTable(pIter, &pTable);
2470 2471 2472
    if (pTable == NULL) break;

    if (pTable->vgId == pVgroup->vgId) {
S
TD-2046  
Shengliang Guan 已提交
2473
      SSdbRow row = {
S
TD-2046  
Shengliang Guan 已提交
2474 2475
        .type   = SDB_OPER_LOCAL,
        .pTable = tsChildTableSdb,
S
TD-2046  
Shengliang Guan 已提交
2476
        .pObj   = pTable,
2477
      };
S
TD-2046  
Shengliang Guan 已提交
2478
      sdbDeleteRow(&row);
2479 2480
      numOfTables++;
    }
2481
    mnodeDecTableRef(pTable);
2482 2483
  }

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

2487
void mnodeDropAllChildTables(SDbObj *pDropDb) {
S
Shengliang Guan 已提交
2488
  void *  pIter = NULL;
S
slguan 已提交
2489
  int32_t numOfTables = 0;
S
TD-1915  
Shengliang Guan 已提交
2490
  SCTableObj *pTable = NULL;
S
slguan 已提交
2491

2492 2493 2494
  char prefix[64] = {0};
  tstrncpy(prefix, pDropDb->name, 64);
  strcat(prefix, TS_PATH_DELIMITER);
S
TD-1207  
Shengliang Guan 已提交
2495
  int32_t prefixLen = (int32_t)strlen(prefix);
2496

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

S
slguan 已提交
2499
  while (1) {
2500
    pIter = mnodeGetNextChildTable(pIter, &pTable);
S
slguan 已提交
2501
    if (pTable == NULL) break;
S
slguan 已提交
2502

2503
    if (strncmp(prefix, pTable->info.tableId, prefixLen) == 0) {
S
TD-2046  
Shengliang Guan 已提交
2504
      SSdbRow row = {
S
TD-2046  
Shengliang Guan 已提交
2505 2506
        .type   = SDB_OPER_LOCAL,
        .pTable = tsChildTableSdb,
S
TD-2046  
Shengliang Guan 已提交
2507
        .pObj   = pTable,
S
slguan 已提交
2508
      };
S
TD-2046  
Shengliang Guan 已提交
2509
      sdbDeleteRow(&row);
S
slguan 已提交
2510 2511
      numOfTables++;
    }
2512
    mnodeDecTableRef(pTable);
S
slguan 已提交
2513 2514
  }

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

S
TD-1915  
Shengliang Guan 已提交
2518
static void mnodeDropAllChildTablesInStable(SSTableObj *pStable) {
S
Shengliang Guan 已提交
2519
  void *  pIter = NULL;
S
slguan 已提交
2520
  int32_t numOfTables = 0;
S
TD-1915  
Shengliang Guan 已提交
2521
  SCTableObj *pTable = NULL;
S
slguan 已提交
2522

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

S
slguan 已提交
2526
  while (1) {
2527
    pIter = mnodeGetNextChildTable(pIter, &pTable);
S
slguan 已提交
2528
    if (pTable == NULL) break;
S
slguan 已提交
2529 2530

    if (pTable->superTable == pStable) {
S
TD-2046  
Shengliang Guan 已提交
2531
      SSdbRow row = {
S
TD-2046  
Shengliang Guan 已提交
2532 2533
        .type   = SDB_OPER_LOCAL,
        .pTable = tsChildTableSdb,
S
TD-2046  
Shengliang Guan 已提交
2534
        .pObj   = pTable,
S
slguan 已提交
2535
      };
S
TD-2046  
Shengliang Guan 已提交
2536
      sdbDeleteRow(&row);
S
slguan 已提交
2537 2538
      numOfTables++;
    }
S
slguan 已提交
2539

2540
    mnodeDecTableRef(pTable);
S
slguan 已提交
2541 2542
  }

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

2546
#if 0
S
TD-1915  
Shengliang Guan 已提交
2547
static SCTableObj* mnodeGetTableByPos(int32_t vnode, int32_t tid) {
2548
  SVgObj *pVgroup = mnodeGetVgroup(vnode);
2549
  if (pVgroup == NULL) return NULL;
S
slguan 已提交
2550

S
TD-1915  
Shengliang Guan 已提交
2551
  SCTableObj *pTable = pVgroup->tableList[tid - 1];
2552
  mnodeIncTableRef((STableObj *)pTable);
2553

2554
  mnodeDecVgroupRef(pVgroup);
S
slguan 已提交
2555 2556
  return pTable;
}
2557
#endif
S
slguan 已提交
2558

2559
static int32_t mnodeProcessTableCfgMsg(SMnodeMsg *pMsg) {
2560 2561
  return TSDB_CODE_COM_OPS_NOT_SUPPORT;
#if 0  
S
TD-1732  
Shengliang Guan 已提交
2562
  SConfigTableMsg *pCfg = pMsg->rpcMsg.pCont;
S
Shengliang Guan 已提交
2563 2564 2565
  pCfg->dnodeId = htonl(pCfg->dnodeId);
  pCfg->vgId = htonl(pCfg->vgId);
  pCfg->sid = htonl(pCfg->sid);
S
Shengliang Guan 已提交
2566
  mDebug("msg:%p, app:%p dnode:%d, vgId:%d sid:%d, receive table config msg", pMsg, pMsg->rpcMsg.ahandle, pCfg->dnodeId,
S
Shengliang Guan 已提交
2567
         pCfg->vgId, pCfg->sid);
S
slguan 已提交
2568

S
TD-1915  
Shengliang Guan 已提交
2569
  SCTableObj *pTable = mnodeGetTableByPos(pCfg->vgId, pCfg->sid);
S
slguan 已提交
2570
  if (pTable == NULL) {
S
Shengliang Guan 已提交
2571
    mError("msg:%p, app:%p dnode:%d, vgId:%d sid:%d, table not found", pMsg, pMsg->rpcMsg.ahandle, pCfg->dnodeId,
S
Shengliang Guan 已提交
2572
           pCfg->vgId, pCfg->sid);
2573
    return TSDB_CODE_MND_INVALID_TABLE_ID;
S
slguan 已提交
2574 2575
  }

S
Shengliang Guan 已提交
2576
  SMDCreateTableMsg *pCreate = NULL;
S
TD-1915  
Shengliang Guan 已提交
2577
  pCreate = mnodeBuildCreateChildTableMsg(NULL, (SCTableObj *)pTable);
2578
  mnodeDecTableRef(pTable);
S
Shengliang Guan 已提交
2579 2580 2581 2582 2583
    
  if (pCreate == NULL) return terrno;
  
  pMsg->rpcRsp.rsp = pCreate;
  pMsg->rpcRsp.len = htonl(pCreate->contLen);
2584
  return TSDB_CODE_SUCCESS;
2585
#endif  
S
slguan 已提交
2586 2587
}

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

S
Shengliang Guan 已提交
2592 2593
  SMnodeMsg *pMsg = rpcMsg->ahandle;
  pMsg->received++;
S
slguan 已提交
2594

S
Shengliang Guan 已提交
2595
  SCTableObj *pTable = (SCTableObj *)pMsg->pTable;
S
Shengliang Guan 已提交
2596
  assert(pTable);
S
Shengliang Guan 已提交
2597

S
Shengliang Guan 已提交
2598 2599 2600
  mInfo("msg:%p, app:%p table:%s, drop table rsp received, vgId:%d sid:%d uid:%" PRIu64 ", thandle:%p result:%s", pMsg,
        pMsg->rpcMsg.ahandle, pTable->info.tableId, pTable->vgId, pTable->tid, pTable->uid, pMsg->rpcMsg.handle,
        tstrerror(rpcMsg->code));
S
slguan 已提交
2601 2602

  if (rpcMsg->code != TSDB_CODE_SUCCESS) {
S
Shengliang Guan 已提交
2603 2604 2605
    mError("msg:%p, app:%p table:%s, failed to drop in dnode, vgId:%d sid:%d uid:%" PRIu64 ", reason:%s", pMsg,
           pMsg->rpcMsg.ahandle, pTable->info.tableId, pTable->vgId, pTable->tid, pTable->uid, tstrerror(rpcMsg->code));
    dnodeSendRpcMWriteRsp(pMsg, rpcMsg->code);
S
slguan 已提交
2606 2607 2608
    return;
  }

S
Shengliang Guan 已提交
2609 2610 2611 2612
  if (pMsg->pVgroup == NULL) pMsg->pVgroup = mnodeGetVgroup(pTable->vgId);
  if (pMsg->pVgroup == NULL) {
    mError("msg:%p, app:%p table:%s, failed to get vgroup", pMsg, pMsg->rpcMsg.ahandle, pTable->info.tableId);
    dnodeSendRpcMWriteRsp(pMsg, TSDB_CODE_MND_VGROUP_NOT_EXIST);
S
slguan 已提交
2613 2614
    return;
  }
S
Shengliang Guan 已提交
2615

S
Shengliang Guan 已提交
2616 2617 2618 2619
  if (pMsg->pVgroup->numOfTables <= 0) {
    mInfo("msg:%p, app:%p vgId:%d, all tables is dropped, drop vgroup", pMsg, pMsg->rpcMsg.ahandle,
          pMsg->pVgroup->vgId);
    mnodeDropVgroup(pMsg->pVgroup, NULL);
S
slguan 已提交
2620 2621
  }

S
Shengliang Guan 已提交
2622
  dnodeSendRpcMWriteRsp(pMsg, TSDB_CODE_SUCCESS);
S
slguan 已提交
2623 2624
}

2625 2626 2627 2628
/*
 * handle create table response from dnode
 *   if failed, drop the table cached
 */
2629
static void mnodeProcessCreateChildTableRsp(SRpcMsg *rpcMsg) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
2630
  if (rpcMsg->ahandle == NULL) return;
S
slguan 已提交
2631

S
Shengliang Guan 已提交
2632 2633
  SMnodeMsg *pMsg = rpcMsg->ahandle;
  pMsg->received++;
S
slguan 已提交
2634

S
Shengliang Guan 已提交
2635
  SCTableObj *pTable = (SCTableObj *)pMsg->pTable;
S
Shengliang Guan 已提交
2636
  assert(pTable);
S
Shengliang Guan 已提交
2637

2638 2639
  // If the table is deleted by another thread during creation, stop creating and send drop msg to vnode
  if (sdbCheckRowDeleted(tsChildTableSdb, pTable)) {
S
Shengliang Guan 已提交
2640 2641
    mDebug("msg:%p, app:%p table:%s, create table rsp received, but a deleting opertion incoming, vgId:%d sid:%d uid:%" PRIu64,
           pMsg, pMsg->rpcMsg.ahandle, pTable->info.tableId, pTable->vgId, pTable->tid, pTable->uid);
S
TD-1038  
Shengliang Guan 已提交
2642 2643 2644

    // if the vgroup is already dropped from hash, it can't be accquired by pTable->vgId
    // so the refCount of vgroup can not be decreased
S
Shengliang Guan 已提交
2645 2646
    // SVgObj *pVgroup = mnodeGetVgroup(pTable->vgId);
    // if (pVgroup == NULL) {
S
Shengliang Guan 已提交
2647
    //   mnodeRemoveTableFromVgroup(pMsg->pVgroup, pTable);
S
Shengliang Guan 已提交
2648 2649
    // }
    // mnodeDecVgroupRef(pVgroup);
S
TD-1038  
Shengliang Guan 已提交
2650

S
Shengliang Guan 已提交
2651
    mnodeSendDropChildTableMsg(pMsg, false);
2652
    rpcMsg->code = TSDB_CODE_SUCCESS;
2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665

    if (pMsg->pBatchMasterMsg) {
      ++pMsg->pBatchMasterMsg->successed;
      if (pMsg->pBatchMasterMsg->successed + pMsg->pBatchMasterMsg->received
	  >= pMsg->pBatchMasterMsg->expected) {
	dnodeSendRpcMWriteRsp(pMsg->pBatchMasterMsg, rpcMsg->code);
      }

      mnodeDestroySubMsg(pMsg);

      return;
    }

S
Shengliang Guan 已提交
2666
    dnodeSendRpcMWriteRsp(pMsg, rpcMsg->code);
S
Shengliang Guan 已提交
2667
    return;
2668 2669
  }

2670
  if (rpcMsg->code == TSDB_CODE_SUCCESS || rpcMsg->code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
S
TD-2046  
Shengliang Guan 已提交
2671
     SSdbRow desc = {
S
TD-2045  
Shengliang Guan 已提交
2672
      .type   = SDB_OPER_GLOBAL,
S
TD-2046  
Shengliang Guan 已提交
2673
      .pObj   = pTable,
S
TD-2045  
Shengliang Guan 已提交
2674
      .pTable = tsChildTableSdb,
S
Shengliang Guan 已提交
2675
      .pMsg   = pMsg,
S
TD-2045  
Shengliang Guan 已提交
2676
      .fpRsp  = mnodeDoCreateChildTableCb
S
Shengliang Guan 已提交
2677 2678
    };
    
S
TD-2046  
Shengliang Guan 已提交
2679
    int32_t code = sdbInsertRowToQueue(&desc);
S
Shengliang Guan 已提交
2680
    if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
2681
      pMsg->pTable = NULL;
S
Shengliang Guan 已提交
2682
      mnodeDestroyChildTable(pTable);
2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695

      if (pMsg->pBatchMasterMsg) {
	++pMsg->pBatchMasterMsg->received;
	if (pMsg->pBatchMasterMsg->successed + pMsg->pBatchMasterMsg->received
	    >= pMsg->pBatchMasterMsg->expected) {
	  dnodeSendRpcMWriteRsp(pMsg->pBatchMasterMsg, code);
	}

	mnodeDestroySubMsg(pMsg);

	return;
      }

S
Shengliang Guan 已提交
2696
      dnodeSendRpcMWriteRsp(pMsg, code);
2697 2698
    }
  } else {
S
Shengliang Guan 已提交
2699
    pMsg->retry++;
S
TD-1793  
Shengliang Guan 已提交
2700
    int32_t sec = taosGetTimestampSec();
S
Shengliang Guan 已提交
2701 2702
    if (pMsg->retry < CREATE_CTABLE_RETRY_TIMES && ABS(sec - pMsg->incomingTs) < CREATE_CTABLE_RETRY_SEC) {
      mDebug("msg:%p, app:%p table:%s, create table rsp received, need retry, times:%d vgId:%d sid:%d uid:%" PRIu64
S
TD-1725  
Shengliang Guan 已提交
2703
             " result:%s thandle:%p",
S
Shengliang Guan 已提交
2704 2705
             pMsg, pMsg->rpcMsg.ahandle, pTable->info.tableId, pMsg->retry, pTable->vgId, pTable->tid, pTable->uid,
             tstrerror(rpcMsg->code), pMsg->rpcMsg.handle);
2706

S
Shengliang Guan 已提交
2707
      dnodeDelayReprocessMWriteMsg(pMsg);
2708
    } else {
S
Shengliang Guan 已提交
2709
      mError("msg:%p, app:%p table:%s, failed to create in dnode, vgId:%d sid:%d uid:%" PRIu64
S
TD-1793  
Shengliang Guan 已提交
2710
             ", result:%s thandle:%p incomingTs:%d curTs:%d retryTimes:%d",
S
Shengliang Guan 已提交
2711 2712
             pMsg, pMsg->rpcMsg.ahandle, pTable->info.tableId, pTable->vgId, pTable->tid, pTable->uid,
             tstrerror(rpcMsg->code), pMsg->rpcMsg.handle, pMsg->incomingTs, sec, pMsg->retry);
2713

S
TD-2046  
Shengliang Guan 已提交
2714 2715
      SSdbRow row = {.type = SDB_OPER_GLOBAL, .pTable = tsChildTableSdb, .pObj = pTable};
      sdbDeleteRow(&row);
2716

S
TD-1793  
Shengliang Guan 已提交
2717 2718 2719 2720
      if (rpcMsg->code == TSDB_CODE_APP_NOT_READY) {
        //Avoid retry again in client
        rpcMsg->code = TSDB_CODE_MND_VGROUP_NOT_READY;
      }
2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733

      if (pMsg->pBatchMasterMsg) {
	++pMsg->pBatchMasterMsg->received;
	if (pMsg->pBatchMasterMsg->successed + pMsg->pBatchMasterMsg->received
	    >= pMsg->pBatchMasterMsg->expected) {
	  dnodeSendRpcMWriteRsp(pMsg->pBatchMasterMsg, rpcMsg->code);
	}

	mnodeDestroySubMsg(pMsg);

	return;
      }

S
Shengliang Guan 已提交
2734
      dnodeSendRpcMWriteRsp(pMsg, rpcMsg->code);
S
slguan 已提交
2735 2736 2737 2738
    }
  }
}

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

S
Shengliang Guan 已提交
2742 2743
  SMnodeMsg *pMsg = rpcMsg->ahandle;
  pMsg->received++;
2744

S
Shengliang Guan 已提交
2745
  SCTableObj *pTable = (SCTableObj *)pMsg->pTable;
2746 2747 2748
  assert(pTable);

  if (rpcMsg->code == TSDB_CODE_SUCCESS || rpcMsg->code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
S
Shengliang Guan 已提交
2749 2750
    mDebug("msg:%p, app:%p ctable:%s, altered in dnode, thandle:%p result:%s", pMsg, pMsg->rpcMsg.ahandle,
           pTable->info.tableId, pMsg->rpcMsg.handle, tstrerror(rpcMsg->code));
2751

S
Shengliang Guan 已提交
2752
    dnodeSendRpcMWriteRsp(pMsg, TSDB_CODE_SUCCESS);
2753
  } else {
S
Shengliang Guan 已提交
2754 2755 2756 2757
    if (pMsg->retry++ < ALTER_CTABLE_RETRY_TIMES) {
      mDebug("msg:%p, app:%p table:%s, alter table rsp received, need retry, times:%d result:%s thandle:%p",
             pMsg->rpcMsg.ahandle, pMsg, pTable->info.tableId, pMsg->retry, tstrerror(rpcMsg->code),
             pMsg->rpcMsg.handle);
2758

S
Shengliang Guan 已提交
2759
      dnodeDelayReprocessMWriteMsg(pMsg);
2760
    } else {
S
Shengliang Guan 已提交
2761 2762 2763
      mError("msg:%p, app:%p table:%s, failed to alter in dnode, result:%s thandle:%p", pMsg, pMsg->rpcMsg.ahandle,
             pTable->info.tableId, tstrerror(rpcMsg->code), pMsg->rpcMsg.handle);
      dnodeSendRpcMWriteRsp(pMsg, rpcMsg->code);
2764 2765
    }
  }
S
slguan 已提交
2766 2767
}

2768
static int32_t mnodeProcessMultiTableMetaMsg(SMnodeMsg *pMsg) {
S
TD-1732  
Shengliang Guan 已提交
2769
  SMultiTableInfoMsg *pInfo = pMsg->rpcMsg.pCont;
S
slguan 已提交
2770 2771
  pInfo->numOfTables = htonl(pInfo->numOfTables);

2772
  int32_t totalMallocLen = 4 * 1024 * 1024;  // first malloc 4 MB, subsequent reallocation as twice
S
slguan 已提交
2773 2774
  SMultiTableMeta *pMultiMeta = rpcMallocCont(totalMallocLen);
  if (pMultiMeta == NULL) {
2775
    return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
2776 2777 2778 2779 2780
  }

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

2781
  for (int32_t t = 0; t < pInfo->numOfTables; ++t) {
H
Haojun Liao 已提交
2782
    char * tableId = (char *)(pInfo->tableIds + t * TSDB_TABLE_FNAME_LEN);
S
TD-1915  
Shengliang Guan 已提交
2783
    SCTableObj *pTable = mnodeGetChildTable(tableId);
S
slguan 已提交
2784 2785
    if (pTable == NULL) continue;

2786
    if (pMsg->pDb == NULL) pMsg->pDb = mnodeGetDbByTableId(tableId);
H
Hui Li 已提交
2787
    if (pMsg->pDb == NULL || pMsg->pDb->status != TSDB_DB_STATUS_READY) {
2788
      mnodeDecTableRef(pTable);
2789 2790
      continue;
    }
S
slguan 已提交
2791 2792

    int availLen = totalMallocLen - pMultiMeta->contLen;
2793
    if (availLen <= sizeof(STableMetaMsg) + sizeof(SSchema) * (TSDB_MAX_TAGS + TSDB_MAX_COLUMNS + 16)) {
2794 2795 2796
      totalMallocLen *= 2;
      pMultiMeta = rpcReallocCont(pMultiMeta, totalMallocLen);
      if (pMultiMeta == NULL) {
2797
        mnodeDecTableRef(pTable);
2798
        return TSDB_CODE_MND_OUT_OF_MEMORY;
2799 2800
      } else {
        t--;
2801
        mnodeDecTableRef(pTable);
2802 2803
        continue;
      }
S
slguan 已提交
2804 2805 2806
    }

    STableMetaMsg *pMeta = (STableMetaMsg *)(pMultiMeta->metas + pMultiMeta->contLen);
2807
    int32_t code = mnodeDoGetChildTableMeta(pMsg, pMeta);
S
slguan 已提交
2808 2809 2810 2811
    if (code == TSDB_CODE_SUCCESS) {
      pMultiMeta->numOfTables ++;
      pMultiMeta->contLen += pMeta->contLen;
    }
2812

2813
    mnodeDecTableRef(pTable);
S
slguan 已提交
2814 2815
  }

2816 2817 2818 2819
  pMsg->rpcRsp.rsp = pMultiMeta;
  pMsg->rpcRsp.len = pMultiMeta->contLen;

  return TSDB_CODE_SUCCESS;
S
slguan 已提交
2820 2821
}

2822 2823
static int32_t mnodeGetShowTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
  SDbObj *pDb = mnodeGetDb(pShow->db);
2824
  if (pDb == NULL) return TSDB_CODE_MND_DB_NOT_SELECTED;
H
Hui Li 已提交
2825 2826 2827
  
  if (pDb->status != TSDB_DB_STATUS_READY) {
    mError("db:%s, status:%d, in dropping", pDb->name, pDb->status);
S
Shengliang Guan 已提交
2828
    mnodeDecDbRef(pDb);
H
Hui Li 已提交
2829 2830
    return TSDB_CODE_MND_DB_IN_DROPPING;
  }
S
slguan 已提交
2831 2832 2833 2834

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

H
Haojun Liao 已提交
2835 2836 2837
  SSchema s = tGetTableNameColumnSchema();
  pShow->bytes[cols] = s.bytes;
  pSchema[cols].type = s.type;
2838
  strcpy(pSchema[cols].name, "table_name");
S
slguan 已提交
2839 2840 2841 2842 2843
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
2844
  strcpy(pSchema[cols].name, "created_time");
S
slguan 已提交
2845 2846 2847 2848 2849 2850 2851 2852 2853
  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 已提交
2854
  SSchema tbCol = tGetTableNameColumnSchema();
H
Hui Li 已提交
2855
  pShow->bytes[cols] = tbCol.bytes + VARSTR_HEADER_SIZE;
H
Haojun Liao 已提交
2856
  pSchema[cols].type = tbCol.type;
2857
  strcpy(pSchema[cols].name, "stable_name");
S
slguan 已提交
2858 2859 2860
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

H
Haojun Liao 已提交
2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879
  pShow->bytes[cols] = 8; // table uid
  pSchema[cols].type = TSDB_DATA_TYPE_BIGINT;
  strcpy(pSchema[cols].name, "uid");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

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

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


S
slguan 已提交
2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890
  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];

2891
  mnodeDecDbRef(pDb);
S
slguan 已提交
2892 2893 2894
  return 0;
}

2895 2896
static int32_t mnodeRetrieveShowTables(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
  SDbObj *pDb = mnodeGetDb(pShow->db);
S
slguan 已提交
2897
  if (pDb == NULL) return 0;
H
Hui Li 已提交
2898 2899 2900
  
  if (pDb->status != TSDB_DB_STATUS_READY) {
    mError("db:%s, status:%d, in dropping", pDb->name, pDb->status);
S
Shengliang Guan 已提交
2901
    mnodeDecDbRef(pDb);
H
Hui Li 已提交
2902 2903
    return 0;
  }
S
slguan 已提交
2904

H
Haojun Liao 已提交
2905
  int32_t cols       = 0;
S
slguan 已提交
2906
  int32_t numOfRows  = 0;
S
TD-1915  
Shengliang Guan 已提交
2907
  SCTableObj *pTable = NULL;
S
slguan 已提交
2908 2909 2910
  SPatternCompareInfo info = PATTERN_COMPARE_INFO_INITIALIZER;

  char prefix[64] = {0};
S
TD-1207  
Shengliang Guan 已提交
2911
  int32_t prefixLen = (int32_t)tableIdPrefix(pDb->name, prefix, 64);
S
slguan 已提交
2912

B
Bomin Zhang 已提交
2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923
  char* pattern = NULL;
  if (pShow->payloadLen > 0) {
    pattern = (char*)malloc(pShow->payloadLen + 1);
    if (pattern == NULL) {
      terrno = TSDB_CODE_QRY_OUT_OF_MEMORY;
      return 0;
    }
    memcpy(pattern, pShow->payload, pShow->payloadLen);
    pattern[pShow->payloadLen] = 0;
  }

S
slguan 已提交
2924
  while (numOfRows < rows) {
2925
    pShow->pIter = mnodeGetNextChildTable(pShow->pIter, &pTable);
S
slguan 已提交
2926 2927 2928 2929
    if (pTable == NULL) break;

    // not belong to current db
    if (strncmp(pTable->info.tableId, prefix, prefixLen)) {
2930
      mnodeDecTableRef(pTable);
S
slguan 已提交
2931 2932 2933
      continue;
    }

B
Bomin Zhang 已提交
2934
    char tableName[TSDB_TABLE_NAME_LEN] = {0};
S
slguan 已提交
2935
    
2936
    // pattern compare for table name
S
Shengliang Guan 已提交
2937
    mnodeExtractTableName(pTable->info.tableId, tableName);
S
slguan 已提交
2938

B
Bomin Zhang 已提交
2939
    if (pattern != NULL && patternMatch(pattern, tableName, sizeof(tableName) - 1, &info) != TSDB_PATTERN_MATCH) {
2940
      mnodeDecTableRef(pTable);
S
slguan 已提交
2941 2942 2943
      continue;
    }

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

H
Hui Li 已提交
2947
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, tableName, pShow->bytes[cols]);
S
slguan 已提交
2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963
    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;
2964
    
B
Bomin Zhang 已提交
2965
    memset(tableName, 0, sizeof(tableName));
S
slguan 已提交
2966
    if (pTable->info.type == TSDB_CHILD_TABLE) {
S
Shengliang Guan 已提交
2967
      mnodeExtractTableName(pTable->superTable->info.tableId, tableName);
H
Hui Li 已提交
2968
      STR_WITH_MAXSIZE_TO_VARSTR(pWrite, tableName, pShow->bytes[cols]);
S
slguan 已提交
2969
    }
2970
    
S
slguan 已提交
2971 2972
    cols++;

H
Haojun Liao 已提交
2973 2974 2975 2976 2977 2978 2979 2980
    // uid
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int64_t*) pWrite = pTable->uid;
    cols++;


    // tid
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
H
Haojun Liao 已提交
2981
    *(int32_t*) pWrite = pTable->tid;
H
Haojun Liao 已提交
2982 2983 2984 2985 2986 2987 2988
    cols++;

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

S
slguan 已提交
2989
    numOfRows++;
2990
    mnodeDecTableRef(pTable);
S
slguan 已提交
2991 2992 2993 2994
  }

  pShow->numOfReads += numOfRows;

2995
  mnodeVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
2996
  mnodeDecDbRef(pDb);
B
Bomin Zhang 已提交
2997
  free(pattern);
S
slguan 已提交
2998 2999 3000 3001

  return numOfRows;
}

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

3007
  if (pMsg->pDb == NULL) pMsg->pDb = mnodeGetDbByTableId(pAlter->tableId);
H
Hui Li 已提交
3008
  if (pMsg->pDb == NULL) {
S
Shengliang Guan 已提交
3009
    mError("msg:%p, app:%p table:%s, failed to alter table, db not selected", pMsg, pMsg->rpcMsg.ahandle, pAlter->tableId);
3010
    return TSDB_CODE_MND_DB_NOT_SELECTED;
S
[TD-17]  
slguan 已提交
3011
  }
H
Hui Li 已提交
3012 3013 3014 3015 3016
  
  if (pMsg->pDb->status != TSDB_DB_STATUS_READY) {
    mError("db:%s, status:%d, in dropping", pMsg->pDb->name, pMsg->pDb->status);
    return TSDB_CODE_MND_DB_IN_DROPPING;
  }
S
[TD-17]  
slguan 已提交
3017

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

3023
  if (pMsg->pTable == NULL) pMsg->pTable = mnodeGetTable(pAlter->tableId);
S
[TD-17]  
slguan 已提交
3024
  if (pMsg->pTable == NULL) {
S
Shengliang Guan 已提交
3025
    mError("msg:%p, app:%p table:%s, failed to alter table, table not exist", pMsg, pMsg->rpcMsg.ahandle, pAlter->tableId);
3026
    return TSDB_CODE_MND_INVALID_TABLE_NAME;
S
[TD-17]  
slguan 已提交
3027 3028
  }

S
slguan 已提交
3029
  pAlter->type = htons(pAlter->type);
3030 3031
  pAlter->numOfCols = htons(pAlter->numOfCols);
  pAlter->tagValLen = htonl(pAlter->tagValLen);
S
slguan 已提交
3032

S
[TD-17]  
slguan 已提交
3033
  if (pAlter->numOfCols > 2) {
S
Shengliang Guan 已提交
3034
    mError("msg:%p, app:%p table:%s, error numOfCols:%d in alter table", pMsg, pMsg->rpcMsg.ahandle, pAlter->tableId,
S
Shengliang Guan 已提交
3035
           pAlter->numOfCols);
3036
    return TSDB_CODE_MND_APP_ERROR;
S
[TD-17]  
slguan 已提交
3037 3038 3039 3040 3041 3042
  }

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

3043
  int32_t code = TSDB_CODE_COM_OPS_NOT_SUPPORT;
S
[TD-17]  
slguan 已提交
3044
  if (pMsg->pTable->type == TSDB_SUPER_TABLE) {
S
Shengliang Guan 已提交
3045
    mDebug("msg:%p, app:%p table:%s, start to alter stable", pMsg, pMsg->rpcMsg.ahandle, pAlter->tableId);
S
[TD-17]  
slguan 已提交
3046
    if (pAlter->type == TSDB_ALTER_TABLE_ADD_TAG_COLUMN) {
3047
      code = mnodeAddSuperTableTag(pMsg, pAlter->schema, 1);
S
[TD-17]  
slguan 已提交
3048
    } else if (pAlter->type == TSDB_ALTER_TABLE_DROP_TAG_COLUMN) {
3049
      code = mnodeDropSuperTableTag(pMsg, pAlter->schema[0].name);
S
[TD-17]  
slguan 已提交
3050
    } else if (pAlter->type == TSDB_ALTER_TABLE_CHANGE_TAG_COLUMN) {
3051
      code = mnodeModifySuperTableTagName(pMsg, pAlter->schema[0].name, pAlter->schema[1].name);
S
[TD-17]  
slguan 已提交
3052
    } else if (pAlter->type == TSDB_ALTER_TABLE_ADD_COLUMN) {
3053
      code = mnodeAddSuperTableColumn(pMsg, pAlter->schema, 1);
S
[TD-17]  
slguan 已提交
3054
    } else if (pAlter->type == TSDB_ALTER_TABLE_DROP_COLUMN) {
3055
      code = mnodeDropSuperTableColumn(pMsg, pAlter->schema[0].name);
S
Shengliang Guan 已提交
3056 3057
    } else if (pAlter->type == TSDB_ALTER_TABLE_CHANGE_COLUMN) {
      code = mnodeChangeSuperTableColumn(pMsg, pAlter->schema[0].name, pAlter->schema[1].name);
S
[TD-17]  
slguan 已提交
3058 3059
    } else {
    }
S
slguan 已提交
3060
  } else {
S
Shengliang Guan 已提交
3061
    mDebug("msg:%p, app:%p table:%s, start to alter ctable", pMsg, pMsg->rpcMsg.ahandle, pAlter->tableId);
S
[TD-17]  
slguan 已提交
3062
    if (pAlter->type == TSDB_ALTER_TABLE_UPDATE_TAG_VAL) {
3063
      return TSDB_CODE_COM_OPS_NOT_SUPPORT;
S
[TD-17]  
slguan 已提交
3064
    } else if (pAlter->type == TSDB_ALTER_TABLE_ADD_COLUMN) {
3065
      code = mnodeAddNormalTableColumn(pMsg, pAlter->schema, 1);
S
[TD-17]  
slguan 已提交
3066
    } else if (pAlter->type == TSDB_ALTER_TABLE_DROP_COLUMN) {
3067
      code = mnodeDropNormalTableColumn(pMsg, pAlter->schema[0].name);
S
Shengliang Guan 已提交
3068 3069
    } else if (pAlter->type == TSDB_ALTER_TABLE_CHANGE_COLUMN) {
      code = mnodeChangeNormalTableColumn(pMsg, pAlter->schema[0].name, pAlter->schema[1].name);
S
[TD-17]  
slguan 已提交
3070 3071
    } else {
    }
S
slguan 已提交
3072 3073
  }

3074
 return code;
S
[TD-17]  
slguan 已提交
3075
}
3076

S
Shengliang Guan 已提交
3077
static int32_t mnodeGetStreamTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
3078
  SDbObj *pDb = mnodeGetDb(pShow->db);
3079
  if (pDb == NULL) return TSDB_CODE_MND_DB_NOT_SELECTED;
H
Hui Li 已提交
3080 3081 3082
  
  if (pDb->status != TSDB_DB_STATUS_READY) {
    mError("db:%s, status:%d, in dropping", pDb->name, pDb->status);
S
Shengliang Guan 已提交
3083
    mnodeDecDbRef(pDb);
H
Hui Li 已提交
3084 3085
    return TSDB_CODE_MND_DB_IN_DROPPING;
  }
3086 3087 3088 3089

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

H
Haojun Liao 已提交
3090 3091 3092
  SSchema tbnameColSchema = tGetTableNameColumnSchema();
  pShow->bytes[cols] = tbnameColSchema.bytes;
  pSchema[cols].type = tbnameColSchema.type;
3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129
  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 已提交
3130
static int32_t mnodeRetrieveStreamTables(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
3131 3132
  SDbObj *pDb = mnodeGetDb(pShow->db);
  if (pDb == NULL) return 0;
H
Hui Li 已提交
3133 3134 3135
  
  if (pDb->status != TSDB_DB_STATUS_READY) {
    mError("db:%s, status:%d, in dropping", pDb->name, pDb->status);
S
Shengliang Guan 已提交
3136
    mnodeDecDbRef(pDb);
H
Hui Li 已提交
3137 3138
    return 0;
  }
3139 3140
  
  int32_t numOfRows  = 0;
S
TD-1915  
Shengliang Guan 已提交
3141
  SCTableObj *pTable = NULL;
3142 3143 3144
  SPatternCompareInfo info = PATTERN_COMPARE_INFO_INITIALIZER;

  char prefix[64] = {0};
S
Shengliang Guan 已提交
3145
  tstrncpy(prefix, pDb->name, 64);
3146
  strcat(prefix, TS_PATH_DELIMITER);
S
TD-1207  
Shengliang Guan 已提交
3147
  int32_t prefixLen = (int32_t)strlen(prefix);
3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158

  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 已提交
3159
    char tableName[TSDB_TABLE_NAME_LEN] = {0};
3160 3161 3162 3163
    
    // pattern compare for table name
    mnodeExtractTableName(pTable->info.tableId, tableName);

B
Bomin Zhang 已提交
3164
    if (pShow->payloadLen > 0 && patternMatch(pShow->payload, tableName, sizeof(tableName) - 1, &info) != TSDB_PATTERN_MATCH) {
3165 3166 3167 3168 3169 3170 3171 3172
      mnodeDecTableRef(pTable);
      continue;
    }

    int32_t cols = 0;

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

H
Hui Li 已提交
3173
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, tableName, pShow->bytes[cols]);
3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184
    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 已提交
3185
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, pTable->sql, pShow->bytes[cols]);    
3186 3187 3188 3189 3190 3191 3192 3193
    cols++;

    numOfRows++;
    mnodeDecTableRef(pTable);
  }

  pShow->numOfReads += numOfRows;

3194
  mnodeVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
3195 3196 3197
  mnodeDecDbRef(pDb);

  return numOfRows;
3198
}